11abecf77SMark McLoughlin /*
21abecf77SMark McLoughlin * QEMU System Emulator
31abecf77SMark McLoughlin *
41abecf77SMark McLoughlin * Copyright (c) 2003-2008 Fabrice Bellard
51abecf77SMark McLoughlin *
61abecf77SMark McLoughlin * Permission is hereby granted, free of charge, to any person obtaining a copy
71abecf77SMark McLoughlin * of this software and associated documentation files (the "Software"), to deal
81abecf77SMark McLoughlin * in the Software without restriction, including without limitation the rights
91abecf77SMark McLoughlin * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
101abecf77SMark McLoughlin * copies of the Software, and to permit persons to whom the Software is
111abecf77SMark McLoughlin * furnished to do so, subject to the following conditions:
121abecf77SMark McLoughlin *
131abecf77SMark McLoughlin * The above copyright notice and this permission notice shall be included in
141abecf77SMark McLoughlin * all copies or substantial portions of the Software.
151abecf77SMark McLoughlin *
161abecf77SMark McLoughlin * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
171abecf77SMark McLoughlin * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
181abecf77SMark McLoughlin * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
191abecf77SMark McLoughlin * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
201abecf77SMark McLoughlin * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
211abecf77SMark McLoughlin * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
221abecf77SMark McLoughlin * THE SOFTWARE.
231abecf77SMark McLoughlin */
241abecf77SMark McLoughlin
252744d920SPeter Maydell #include "qemu/osdep.h"
26a245fc18SPaolo Bonzini #include "clients.h"
27da34e65cSMarkus Armbruster #include "qapi/error.h"
281de7afc9SPaolo Bonzini #include "qemu/error-report.h"
2943192fccSThomas Huth #include "qemu/iov.h"
300b8fa32fSMarkus Armbruster #include "qemu/module.h"
311de7afc9SPaolo Bonzini #include "qemu/timer.h"
329d3e12e8SThomas Huth #include "qapi/visitor.h"
339d3e12e8SThomas Huth #include "net/filter.h"
34db1015e9SEduardo Habkost #include "qom/object.h"
352f93d8b0SPeter Maydell #include "sysemu/rtc.h"
361abecf77SMark McLoughlin
371abecf77SMark McLoughlin typedef struct DumpState {
380fa29915SHervé Poussineau int64_t start_ts;
391abecf77SMark McLoughlin int fd;
401abecf77SMark McLoughlin int pcap_caplen;
411abecf77SMark McLoughlin } DumpState;
421abecf77SMark McLoughlin
431abecf77SMark McLoughlin #define PCAP_MAGIC 0xa1b2c3d4
441abecf77SMark McLoughlin
451abecf77SMark McLoughlin struct pcap_file_hdr {
461abecf77SMark McLoughlin uint32_t magic;
471abecf77SMark McLoughlin uint16_t version_major;
481abecf77SMark McLoughlin uint16_t version_minor;
491abecf77SMark McLoughlin int32_t thiszone;
501abecf77SMark McLoughlin uint32_t sigfigs;
511abecf77SMark McLoughlin uint32_t snaplen;
521abecf77SMark McLoughlin uint32_t linktype;
531abecf77SMark McLoughlin };
541abecf77SMark McLoughlin
551abecf77SMark McLoughlin struct pcap_sf_pkthdr {
561abecf77SMark McLoughlin struct {
571abecf77SMark McLoughlin int32_t tv_sec;
581abecf77SMark McLoughlin int32_t tv_usec;
591abecf77SMark McLoughlin } ts;
601abecf77SMark McLoughlin uint32_t caplen;
611abecf77SMark McLoughlin uint32_t len;
621abecf77SMark McLoughlin };
631abecf77SMark McLoughlin
dump_receive_iov(DumpState * s,const struct iovec * iov,int cnt,int offset)64481c5232SAkihiko Odaki static ssize_t dump_receive_iov(DumpState *s, const struct iovec *iov, int cnt,
65481c5232SAkihiko Odaki int offset)
661abecf77SMark McLoughlin {
671abecf77SMark McLoughlin struct pcap_sf_pkthdr hdr;
681abecf77SMark McLoughlin int64_t ts;
691abecf77SMark McLoughlin int caplen;
70481c5232SAkihiko Odaki size_t size = iov_size(iov, cnt) - offset;
71c4cf6819SPeter Maydell g_autofree struct iovec *dumpiov = g_new(struct iovec, cnt + 1);
721abecf77SMark McLoughlin
731abecf77SMark McLoughlin /* Early return in case of previous error. */
741abecf77SMark McLoughlin if (s->fd < 0) {
751abecf77SMark McLoughlin return size;
761abecf77SMark McLoughlin }
771abecf77SMark McLoughlin
78ab60b748SLaurent Vivier ts = qemu_clock_get_us(QEMU_CLOCK_VIRTUAL);
791abecf77SMark McLoughlin caplen = size > s->pcap_caplen ? s->pcap_caplen : size;
801abecf77SMark McLoughlin
810fa29915SHervé Poussineau hdr.ts.tv_sec = ts / 1000000 + s->start_ts;
821abecf77SMark McLoughlin hdr.ts.tv_usec = ts % 1000000;
831abecf77SMark McLoughlin hdr.caplen = caplen;
841abecf77SMark McLoughlin hdr.len = size;
8543192fccSThomas Huth
8643192fccSThomas Huth dumpiov[0].iov_base = &hdr;
8743192fccSThomas Huth dumpiov[0].iov_len = sizeof(hdr);
88481c5232SAkihiko Odaki cnt = iov_copy(&dumpiov[1], cnt, iov, cnt, offset, caplen);
8943192fccSThomas Huth
9043192fccSThomas Huth if (writev(s->fd, dumpiov, cnt + 1) != sizeof(hdr) + caplen) {
9134f22fc0SPaolo Bonzini error_report("network dump write error - stopping dump");
921abecf77SMark McLoughlin close(s->fd);
931abecf77SMark McLoughlin s->fd = -1;
941abecf77SMark McLoughlin }
951abecf77SMark McLoughlin
961abecf77SMark McLoughlin return size;
971abecf77SMark McLoughlin }
981abecf77SMark McLoughlin
dump_cleanup(DumpState * s)9975310e34SThomas Huth static void dump_cleanup(DumpState *s)
10043192fccSThomas Huth {
1011abecf77SMark McLoughlin close(s->fd);
10275310e34SThomas Huth s->fd = -1;
1031abecf77SMark McLoughlin }
1041abecf77SMark McLoughlin
net_dump_state_init(DumpState * s,const char * filename,int len,Error ** errp)1057bc3074cSThomas Huth static int net_dump_state_init(DumpState *s, const char *filename,
1067bc3074cSThomas Huth int len, Error **errp)
1071abecf77SMark McLoughlin {
1081abecf77SMark McLoughlin struct pcap_file_hdr hdr;
1090fa29915SHervé Poussineau struct tm tm;
110731d5856SMark McLoughlin int fd;
1111abecf77SMark McLoughlin
1126514ed52SHervé Poussineau fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0644);
113731d5856SMark McLoughlin if (fd < 0) {
114857d2087SThomas Huth error_setg_errno(errp, errno, "net dump: can't open %s", filename);
1151abecf77SMark McLoughlin return -1;
1161abecf77SMark McLoughlin }
1171abecf77SMark McLoughlin
1181abecf77SMark McLoughlin hdr.magic = PCAP_MAGIC;
1191abecf77SMark McLoughlin hdr.version_major = 2;
1201abecf77SMark McLoughlin hdr.version_minor = 4;
1211abecf77SMark McLoughlin hdr.thiszone = 0;
1221abecf77SMark McLoughlin hdr.sigfigs = 0;
123731d5856SMark McLoughlin hdr.snaplen = len;
1241abecf77SMark McLoughlin hdr.linktype = 1;
1251abecf77SMark McLoughlin
126731d5856SMark McLoughlin if (write(fd, &hdr, sizeof(hdr)) < sizeof(hdr)) {
127857d2087SThomas Huth error_setg_errno(errp, errno, "net dump write error");
128731d5856SMark McLoughlin close(fd);
1291abecf77SMark McLoughlin return -1;
1301abecf77SMark McLoughlin }
1311abecf77SMark McLoughlin
132731d5856SMark McLoughlin s->fd = fd;
133731d5856SMark McLoughlin s->pcap_caplen = len;
134731d5856SMark McLoughlin
1350fa29915SHervé Poussineau qemu_get_timedate(&tm, 0);
1360fa29915SHervé Poussineau s->start_ts = mktime(&tm);
1370fa29915SHervé Poussineau
1381abecf77SMark McLoughlin return 0;
1391abecf77SMark McLoughlin }
1401abecf77SMark McLoughlin
1419d3e12e8SThomas Huth #define TYPE_FILTER_DUMP "filter-dump"
1429d3e12e8SThomas Huth
1438063396bSEduardo Habkost OBJECT_DECLARE_SIMPLE_TYPE(NetFilterDumpState, FILTER_DUMP)
1449d3e12e8SThomas Huth
1459d3e12e8SThomas Huth struct NetFilterDumpState {
1469d3e12e8SThomas Huth NetFilterState nfs;
1479d3e12e8SThomas Huth DumpState ds;
1489d3e12e8SThomas Huth char *filename;
1499d3e12e8SThomas Huth uint32_t maxlen;
1509d3e12e8SThomas Huth };
1519d3e12e8SThomas Huth
filter_dump_receive_iov(NetFilterState * nf,NetClientState * sndr,unsigned flags,const struct iovec * iov,int iovcnt,NetPacketSent * sent_cb)1529d3e12e8SThomas Huth static ssize_t filter_dump_receive_iov(NetFilterState *nf, NetClientState *sndr,
1539d3e12e8SThomas Huth unsigned flags, const struct iovec *iov,
1549d3e12e8SThomas Huth int iovcnt, NetPacketSent *sent_cb)
1559d3e12e8SThomas Huth {
1569d3e12e8SThomas Huth NetFilterDumpState *nfds = FILTER_DUMP(nf);
1579d3e12e8SThomas Huth
158*7dc66edeSLaurent Vivier dump_receive_iov(&nfds->ds, iov, iovcnt, flags & QEMU_NET_PACKET_FLAG_RAW ?
159*7dc66edeSLaurent Vivier 0 : qemu_get_vnet_hdr_len(nf->netdev));
1609d3e12e8SThomas Huth return 0;
1619d3e12e8SThomas Huth }
1629d3e12e8SThomas Huth
filter_dump_cleanup(NetFilterState * nf)1639d3e12e8SThomas Huth static void filter_dump_cleanup(NetFilterState *nf)
1649d3e12e8SThomas Huth {
1659d3e12e8SThomas Huth NetFilterDumpState *nfds = FILTER_DUMP(nf);
1669d3e12e8SThomas Huth
1679d3e12e8SThomas Huth dump_cleanup(&nfds->ds);
1689d3e12e8SThomas Huth }
1699d3e12e8SThomas Huth
filter_dump_setup(NetFilterState * nf,Error ** errp)1709d3e12e8SThomas Huth static void filter_dump_setup(NetFilterState *nf, Error **errp)
1719d3e12e8SThomas Huth {
1729d3e12e8SThomas Huth NetFilterDumpState *nfds = FILTER_DUMP(nf);
1739d3e12e8SThomas Huth
1749d3e12e8SThomas Huth if (!nfds->filename) {
1759d3e12e8SThomas Huth error_setg(errp, "dump filter needs 'file' property set!");
1769d3e12e8SThomas Huth return;
1779d3e12e8SThomas Huth }
1789d3e12e8SThomas Huth
1799d3e12e8SThomas Huth net_dump_state_init(&nfds->ds, nfds->filename, nfds->maxlen, errp);
1809d3e12e8SThomas Huth }
1819d3e12e8SThomas Huth
filter_dump_get_maxlen(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)182d7bce999SEric Blake static void filter_dump_get_maxlen(Object *obj, Visitor *v, const char *name,
183d7bce999SEric Blake void *opaque, Error **errp)
1849d3e12e8SThomas Huth {
1859d3e12e8SThomas Huth NetFilterDumpState *nfds = FILTER_DUMP(obj);
1869d3e12e8SThomas Huth uint32_t value = nfds->maxlen;
1879d3e12e8SThomas Huth
18851e72bc1SEric Blake visit_type_uint32(v, name, &value, errp);
1899d3e12e8SThomas Huth }
1909d3e12e8SThomas Huth
filter_dump_set_maxlen(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)191d7bce999SEric Blake static void filter_dump_set_maxlen(Object *obj, Visitor *v, const char *name,
192d7bce999SEric Blake void *opaque, Error **errp)
1939d3e12e8SThomas Huth {
1949d3e12e8SThomas Huth NetFilterDumpState *nfds = FILTER_DUMP(obj);
1959d3e12e8SThomas Huth uint32_t value;
1969d3e12e8SThomas Huth
197668f62ecSMarkus Armbruster if (!visit_type_uint32(v, name, &value, errp)) {
198dcfe4805SMarkus Armbruster return;
1999d3e12e8SThomas Huth }
2009d3e12e8SThomas Huth if (value == 0) {
201dcfe4805SMarkus Armbruster error_setg(errp, "Property '%s.%s' doesn't take value '%u'",
2029d3e12e8SThomas Huth object_get_typename(obj), name, value);
203dcfe4805SMarkus Armbruster return;
2049d3e12e8SThomas Huth }
2059d3e12e8SThomas Huth nfds->maxlen = value;
2069d3e12e8SThomas Huth }
2079d3e12e8SThomas Huth
file_dump_get_filename(Object * obj,Error ** errp)2089d3e12e8SThomas Huth static char *file_dump_get_filename(Object *obj, Error **errp)
2099d3e12e8SThomas Huth {
2109d3e12e8SThomas Huth NetFilterDumpState *nfds = FILTER_DUMP(obj);
2119d3e12e8SThomas Huth
2129d3e12e8SThomas Huth return g_strdup(nfds->filename);
2139d3e12e8SThomas Huth }
2149d3e12e8SThomas Huth
file_dump_set_filename(Object * obj,const char * value,Error ** errp)2159d3e12e8SThomas Huth static void file_dump_set_filename(Object *obj, const char *value, Error **errp)
2169d3e12e8SThomas Huth {
2179d3e12e8SThomas Huth NetFilterDumpState *nfds = FILTER_DUMP(obj);
2189d3e12e8SThomas Huth
2199d3e12e8SThomas Huth g_free(nfds->filename);
2209d3e12e8SThomas Huth nfds->filename = g_strdup(value);
2219d3e12e8SThomas Huth }
2229d3e12e8SThomas Huth
filter_dump_instance_init(Object * obj)2239d3e12e8SThomas Huth static void filter_dump_instance_init(Object *obj)
2249d3e12e8SThomas Huth {
2259d3e12e8SThomas Huth NetFilterDumpState *nfds = FILTER_DUMP(obj);
2269d3e12e8SThomas Huth
2279d3e12e8SThomas Huth nfds->maxlen = 65536;
2289d3e12e8SThomas Huth }
2299d3e12e8SThomas Huth
filter_dump_instance_finalize(Object * obj)230b50c7d45SLi Zhijian static void filter_dump_instance_finalize(Object *obj)
231b50c7d45SLi Zhijian {
232b50c7d45SLi Zhijian NetFilterDumpState *nfds = FILTER_DUMP(obj);
233b50c7d45SLi Zhijian
234b50c7d45SLi Zhijian g_free(nfds->filename);
235b50c7d45SLi Zhijian }
236b50c7d45SLi Zhijian
filter_dump_class_init(ObjectClass * oc,void * data)2379d3e12e8SThomas Huth static void filter_dump_class_init(ObjectClass *oc, void *data)
2389d3e12e8SThomas Huth {
2399d3e12e8SThomas Huth NetFilterClass *nfc = NETFILTER_CLASS(oc);
2409d3e12e8SThomas Huth
241f0e34a06SEduardo Habkost object_class_property_add(oc, "maxlen", "uint32", filter_dump_get_maxlen,
242f0e34a06SEduardo Habkost filter_dump_set_maxlen, NULL, NULL);
243f0e34a06SEduardo Habkost object_class_property_add_str(oc, "file", file_dump_get_filename,
244f0e34a06SEduardo Habkost file_dump_set_filename);
245f0e34a06SEduardo Habkost
2469d3e12e8SThomas Huth nfc->setup = filter_dump_setup;
2479d3e12e8SThomas Huth nfc->cleanup = filter_dump_cleanup;
2489d3e12e8SThomas Huth nfc->receive_iov = filter_dump_receive_iov;
2499d3e12e8SThomas Huth }
2509d3e12e8SThomas Huth
2519d3e12e8SThomas Huth static const TypeInfo filter_dump_info = {
2529d3e12e8SThomas Huth .name = TYPE_FILTER_DUMP,
2539d3e12e8SThomas Huth .parent = TYPE_NETFILTER,
2549d3e12e8SThomas Huth .class_init = filter_dump_class_init,
2559d3e12e8SThomas Huth .instance_init = filter_dump_instance_init,
256b50c7d45SLi Zhijian .instance_finalize = filter_dump_instance_finalize,
2579d3e12e8SThomas Huth .instance_size = sizeof(NetFilterDumpState),
2589d3e12e8SThomas Huth };
2599d3e12e8SThomas Huth
filter_dump_register_types(void)2609d3e12e8SThomas Huth static void filter_dump_register_types(void)
2619d3e12e8SThomas Huth {
2629d3e12e8SThomas Huth type_register_static(&filter_dump_info);
2639d3e12e8SThomas Huth }
2649d3e12e8SThomas Huth
2659d3e12e8SThomas Huth type_init(filter_dump_register_types);
266