1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include "qemu/osdep.h"
26#include "clients.h"
27#include "qapi/error.h"
28#include "qemu-common.h"
29#include "qemu/error-report.h"
30#include "qemu/iov.h"
31#include "qemu/log.h"
32#include "qemu/timer.h"
33#include "qapi/visitor.h"
34#include "net/filter.h"
35
36typedef struct DumpState {
37 int64_t start_ts;
38 int fd;
39 int pcap_caplen;
40} DumpState;
41
42#define PCAP_MAGIC 0xa1b2c3d4
43
44struct pcap_file_hdr {
45 uint32_t magic;
46 uint16_t version_major;
47 uint16_t version_minor;
48 int32_t thiszone;
49 uint32_t sigfigs;
50 uint32_t snaplen;
51 uint32_t linktype;
52};
53
54struct pcap_sf_pkthdr {
55 struct {
56 int32_t tv_sec;
57 int32_t tv_usec;
58 } ts;
59 uint32_t caplen;
60 uint32_t len;
61};
62
63static ssize_t dump_receive_iov(DumpState *s, const struct iovec *iov, int cnt)
64{
65 struct pcap_sf_pkthdr hdr;
66 int64_t ts;
67 int caplen;
68 size_t size = iov_size(iov, cnt);
69 struct iovec dumpiov[cnt + 1];
70
71
72 if (s->fd < 0) {
73 return size;
74 }
75
76 ts = qemu_clock_get_us(QEMU_CLOCK_VIRTUAL);
77 caplen = size > s->pcap_caplen ? s->pcap_caplen : size;
78
79 hdr.ts.tv_sec = ts / 1000000 + s->start_ts;
80 hdr.ts.tv_usec = ts % 1000000;
81 hdr.caplen = caplen;
82 hdr.len = size;
83
84 dumpiov[0].iov_base = &hdr;
85 dumpiov[0].iov_len = sizeof(hdr);
86 cnt = iov_copy(&dumpiov[1], cnt, iov, cnt, 0, caplen);
87
88 if (writev(s->fd, dumpiov, cnt + 1) != sizeof(hdr) + caplen) {
89 error_report("network dump write error - stopping dump");
90 close(s->fd);
91 s->fd = -1;
92 }
93
94 return size;
95}
96
97static void dump_cleanup(DumpState *s)
98{
99 close(s->fd);
100 s->fd = -1;
101}
102
103static int net_dump_state_init(DumpState *s, const char *filename,
104 int len, Error **errp)
105{
106 struct pcap_file_hdr hdr;
107 struct tm tm;
108 int fd;
109
110 fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0644);
111 if (fd < 0) {
112 error_setg_errno(errp, errno, "-net dump: can't open %s", filename);
113 return -1;
114 }
115
116 hdr.magic = PCAP_MAGIC;
117 hdr.version_major = 2;
118 hdr.version_minor = 4;
119 hdr.thiszone = 0;
120 hdr.sigfigs = 0;
121 hdr.snaplen = len;
122 hdr.linktype = 1;
123
124 if (write(fd, &hdr, sizeof(hdr)) < sizeof(hdr)) {
125 error_setg_errno(errp, errno, "-net dump write error");
126 close(fd);
127 return -1;
128 }
129
130 s->fd = fd;
131 s->pcap_caplen = len;
132
133 qemu_get_timedate(&tm, 0);
134 s->start_ts = mktime(&tm);
135
136 return 0;
137}
138
139
140
141struct DumpNetClient {
142 NetClientState nc;
143 DumpState ds;
144};
145typedef struct DumpNetClient DumpNetClient;
146
147static ssize_t dumpclient_receive(NetClientState *nc, const uint8_t *buf,
148 size_t size)
149{
150 DumpNetClient *dc = DO_UPCAST(DumpNetClient, nc, nc);
151 struct iovec iov = {
152 .iov_base = (void *)buf,
153 .iov_len = size
154 };
155
156 return dump_receive_iov(&dc->ds, &iov, 1);
157}
158
159static ssize_t dumpclient_receive_iov(NetClientState *nc,
160 const struct iovec *iov, int cnt)
161{
162 DumpNetClient *dc = DO_UPCAST(DumpNetClient, nc, nc);
163
164 return dump_receive_iov(&dc->ds, iov, cnt);
165}
166
167static void dumpclient_cleanup(NetClientState *nc)
168{
169 DumpNetClient *dc = DO_UPCAST(DumpNetClient, nc, nc);
170
171 dump_cleanup(&dc->ds);
172}
173
174static NetClientInfo net_dump_info = {
175 .type = NET_CLIENT_DRIVER_DUMP,
176 .size = sizeof(DumpNetClient),
177 .receive = dumpclient_receive,
178 .receive_iov = dumpclient_receive_iov,
179 .cleanup = dumpclient_cleanup,
180};
181
182int net_init_dump(const Netdev *netdev, const char *name,
183 NetClientState *peer, Error **errp)
184{
185 int len, rc;
186 const char *file;
187 char def_file[128];
188 const NetdevDumpOptions *dump;
189 NetClientState *nc;
190 DumpNetClient *dnc;
191
192 assert(netdev->type == NET_CLIENT_DRIVER_DUMP);
193 dump = &netdev->u.dump;
194
195 assert(peer);
196
197 error_report("'-net dump' is deprecated. "
198 "Please use '-object filter-dump' instead.");
199
200 if (dump->has_file) {
201 file = dump->file;
202 } else {
203 int id;
204 int ret;
205
206 ret = net_hub_id_for_client(peer, &id);
207 assert(ret == 0);
208
209 snprintf(def_file, sizeof(def_file), "qemu-vlan%d.pcap", id);
210 file = def_file;
211 }
212
213 if (dump->has_len) {
214 if (dump->len > INT_MAX) {
215 error_setg(errp, "invalid length: %"PRIu64, dump->len);
216 return -1;
217 }
218 len = dump->len;
219 } else {
220 len = 65536;
221 }
222
223 nc = qemu_new_net_client(&net_dump_info, peer, "dump", name);
224 snprintf(nc->info_str, sizeof(nc->info_str),
225 "dump to %s (len=%d)", file, len);
226
227 dnc = DO_UPCAST(DumpNetClient, nc, nc);
228 rc = net_dump_state_init(&dnc->ds, file, len, errp);
229 if (rc) {
230 qemu_del_net_client(nc);
231 }
232 return rc;
233}
234
235
236
237#define TYPE_FILTER_DUMP "filter-dump"
238
239#define FILTER_DUMP(obj) \
240 OBJECT_CHECK(NetFilterDumpState, (obj), TYPE_FILTER_DUMP)
241
242struct NetFilterDumpState {
243 NetFilterState nfs;
244 DumpState ds;
245 char *filename;
246 uint32_t maxlen;
247};
248typedef struct NetFilterDumpState NetFilterDumpState;
249
250static ssize_t filter_dump_receive_iov(NetFilterState *nf, NetClientState *sndr,
251 unsigned flags, const struct iovec *iov,
252 int iovcnt, NetPacketSent *sent_cb)
253{
254 NetFilterDumpState *nfds = FILTER_DUMP(nf);
255
256 dump_receive_iov(&nfds->ds, iov, iovcnt);
257 return 0;
258}
259
260static void filter_dump_cleanup(NetFilterState *nf)
261{
262 NetFilterDumpState *nfds = FILTER_DUMP(nf);
263
264 dump_cleanup(&nfds->ds);
265}
266
267static void filter_dump_setup(NetFilterState *nf, Error **errp)
268{
269 NetFilterDumpState *nfds = FILTER_DUMP(nf);
270
271 if (!nfds->filename) {
272 error_setg(errp, "dump filter needs 'file' property set!");
273 return;
274 }
275
276 net_dump_state_init(&nfds->ds, nfds->filename, nfds->maxlen, errp);
277}
278
279static void filter_dump_get_maxlen(Object *obj, Visitor *v, const char *name,
280 void *opaque, Error **errp)
281{
282 NetFilterDumpState *nfds = FILTER_DUMP(obj);
283 uint32_t value = nfds->maxlen;
284
285 visit_type_uint32(v, name, &value, errp);
286}
287
288static void filter_dump_set_maxlen(Object *obj, Visitor *v, const char *name,
289 void *opaque, Error **errp)
290{
291 NetFilterDumpState *nfds = FILTER_DUMP(obj);
292 Error *local_err = NULL;
293 uint32_t value;
294
295 visit_type_uint32(v, name, &value, &local_err);
296 if (local_err) {
297 goto out;
298 }
299 if (value == 0) {
300 error_setg(&local_err, "Property '%s.%s' doesn't take value '%u'",
301 object_get_typename(obj), name, value);
302 goto out;
303 }
304 nfds->maxlen = value;
305
306out:
307 error_propagate(errp, local_err);
308}
309
310static char *file_dump_get_filename(Object *obj, Error **errp)
311{
312 NetFilterDumpState *nfds = FILTER_DUMP(obj);
313
314 return g_strdup(nfds->filename);
315}
316
317static void file_dump_set_filename(Object *obj, const char *value, Error **errp)
318{
319 NetFilterDumpState *nfds = FILTER_DUMP(obj);
320
321 g_free(nfds->filename);
322 nfds->filename = g_strdup(value);
323}
324
325static void filter_dump_instance_init(Object *obj)
326{
327 NetFilterDumpState *nfds = FILTER_DUMP(obj);
328
329 nfds->maxlen = 65536;
330
331 object_property_add(obj, "maxlen", "uint32", filter_dump_get_maxlen,
332 filter_dump_set_maxlen, NULL, NULL, NULL);
333 object_property_add_str(obj, "file", file_dump_get_filename,
334 file_dump_set_filename, NULL);
335}
336
337static void filter_dump_instance_finalize(Object *obj)
338{
339 NetFilterDumpState *nfds = FILTER_DUMP(obj);
340
341 g_free(nfds->filename);
342}
343
344static void filter_dump_class_init(ObjectClass *oc, void *data)
345{
346 NetFilterClass *nfc = NETFILTER_CLASS(oc);
347
348 nfc->setup = filter_dump_setup;
349 nfc->cleanup = filter_dump_cleanup;
350 nfc->receive_iov = filter_dump_receive_iov;
351}
352
353static const TypeInfo filter_dump_info = {
354 .name = TYPE_FILTER_DUMP,
355 .parent = TYPE_NETFILTER,
356 .class_init = filter_dump_class_init,
357 .instance_init = filter_dump_instance_init,
358 .instance_finalize = filter_dump_instance_finalize,
359 .instance_size = sizeof(NetFilterDumpState),
360};
361
362static void filter_dump_register_types(void)
363{
364 type_register_static(&filter_dump_info);
365}
366
367type_init(filter_dump_register_types);
368