]>
Commit | Line | Data |
---|---|---|
1abecf77 MM |
1 | /* |
2 | * QEMU System Emulator | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
2744d920 | 25 | #include "qemu/osdep.h" |
a245fc18 | 26 | #include "clients.h" |
da34e65c | 27 | #include "qapi/error.h" |
1de7afc9 | 28 | #include "qemu/error-report.h" |
43192fcc | 29 | #include "qemu/iov.h" |
0b8fa32f | 30 | #include "qemu/module.h" |
1de7afc9 | 31 | #include "qemu/timer.h" |
9d3e12e8 TH |
32 | #include "qapi/visitor.h" |
33 | #include "net/filter.h" | |
db1015e9 | 34 | #include "qom/object.h" |
2f93d8b0 | 35 | #include "sysemu/rtc.h" |
1abecf77 MM |
36 | |
37 | typedef struct DumpState { | |
0fa29915 | 38 | int64_t start_ts; |
1abecf77 MM |
39 | int fd; |
40 | int pcap_caplen; | |
41 | } DumpState; | |
42 | ||
43 | #define PCAP_MAGIC 0xa1b2c3d4 | |
44 | ||
45 | struct pcap_file_hdr { | |
46 | uint32_t magic; | |
47 | uint16_t version_major; | |
48 | uint16_t version_minor; | |
49 | int32_t thiszone; | |
50 | uint32_t sigfigs; | |
51 | uint32_t snaplen; | |
52 | uint32_t linktype; | |
53 | }; | |
54 | ||
55 | struct pcap_sf_pkthdr { | |
56 | struct { | |
57 | int32_t tv_sec; | |
58 | int32_t tv_usec; | |
59 | } ts; | |
60 | uint32_t caplen; | |
61 | uint32_t len; | |
62 | }; | |
63 | ||
481c5232 AO |
64 | static ssize_t dump_receive_iov(DumpState *s, const struct iovec *iov, int cnt, |
65 | int offset) | |
1abecf77 | 66 | { |
1abecf77 MM |
67 | struct pcap_sf_pkthdr hdr; |
68 | int64_t ts; | |
69 | int caplen; | |
481c5232 | 70 | size_t size = iov_size(iov, cnt) - offset; |
43192fcc | 71 | struct iovec dumpiov[cnt + 1]; |
1abecf77 MM |
72 | |
73 | /* Early return in case of previous error. */ | |
74 | if (s->fd < 0) { | |
75 | return size; | |
76 | } | |
77 | ||
ab60b748 | 78 | ts = qemu_clock_get_us(QEMU_CLOCK_VIRTUAL); |
1abecf77 MM |
79 | caplen = size > s->pcap_caplen ? s->pcap_caplen : size; |
80 | ||
0fa29915 | 81 | hdr.ts.tv_sec = ts / 1000000 + s->start_ts; |
1abecf77 MM |
82 | hdr.ts.tv_usec = ts % 1000000; |
83 | hdr.caplen = caplen; | |
84 | hdr.len = size; | |
43192fcc TH |
85 | |
86 | dumpiov[0].iov_base = &hdr; | |
87 | dumpiov[0].iov_len = sizeof(hdr); | |
481c5232 | 88 | cnt = iov_copy(&dumpiov[1], cnt, iov, cnt, offset, caplen); |
43192fcc TH |
89 | |
90 | if (writev(s->fd, dumpiov, cnt + 1) != sizeof(hdr) + caplen) { | |
34f22fc0 | 91 | error_report("network dump write error - stopping dump"); |
1abecf77 MM |
92 | close(s->fd); |
93 | s->fd = -1; | |
94 | } | |
95 | ||
96 | return size; | |
97 | } | |
98 | ||
75310e34 | 99 | static void dump_cleanup(DumpState *s) |
1abecf77 | 100 | { |
1abecf77 | 101 | close(s->fd); |
75310e34 | 102 | s->fd = -1; |
1abecf77 MM |
103 | } |
104 | ||
7bc3074c TH |
105 | static int net_dump_state_init(DumpState *s, const char *filename, |
106 | int len, Error **errp) | |
1abecf77 MM |
107 | { |
108 | struct pcap_file_hdr hdr; | |
0fa29915 | 109 | struct tm tm; |
731d5856 | 110 | int fd; |
1abecf77 | 111 | |
6514ed52 | 112 | fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0644); |
731d5856 | 113 | if (fd < 0) { |
857d2087 | 114 | error_setg_errno(errp, errno, "net dump: can't open %s", filename); |
1abecf77 MM |
115 | return -1; |
116 | } | |
117 | ||
1abecf77 MM |
118 | hdr.magic = PCAP_MAGIC; |
119 | hdr.version_major = 2; | |
120 | hdr.version_minor = 4; | |
121 | hdr.thiszone = 0; | |
122 | hdr.sigfigs = 0; | |
731d5856 | 123 | hdr.snaplen = len; |
1abecf77 MM |
124 | hdr.linktype = 1; |
125 | ||
731d5856 | 126 | if (write(fd, &hdr, sizeof(hdr)) < sizeof(hdr)) { |
857d2087 | 127 | error_setg_errno(errp, errno, "net dump write error"); |
731d5856 | 128 | close(fd); |
1abecf77 MM |
129 | return -1; |
130 | } | |
131 | ||
731d5856 MM |
132 | s->fd = fd; |
133 | s->pcap_caplen = len; | |
134 | ||
0fa29915 HP |
135 | qemu_get_timedate(&tm, 0); |
136 | s->start_ts = mktime(&tm); | |
137 | ||
1abecf77 MM |
138 | return 0; |
139 | } | |
140 | ||
9d3e12e8 TH |
141 | #define TYPE_FILTER_DUMP "filter-dump" |
142 | ||
8063396b | 143 | OBJECT_DECLARE_SIMPLE_TYPE(NetFilterDumpState, FILTER_DUMP) |
9d3e12e8 TH |
144 | |
145 | struct NetFilterDumpState { | |
146 | NetFilterState nfs; | |
147 | DumpState ds; | |
148 | char *filename; | |
149 | uint32_t maxlen; | |
150 | }; | |
9d3e12e8 TH |
151 | |
152 | static ssize_t filter_dump_receive_iov(NetFilterState *nf, NetClientState *sndr, | |
153 | unsigned flags, const struct iovec *iov, | |
154 | int iovcnt, NetPacketSent *sent_cb) | |
155 | { | |
156 | NetFilterDumpState *nfds = FILTER_DUMP(nf); | |
481c5232 AO |
157 | int offset = qemu_get_using_vnet_hdr(nf->netdev) ? |
158 | qemu_get_vnet_hdr_len(nf->netdev) : 0; | |
9d3e12e8 | 159 | |
481c5232 | 160 | dump_receive_iov(&nfds->ds, iov, iovcnt, offset); |
9d3e12e8 TH |
161 | return 0; |
162 | } | |
163 | ||
164 | static void filter_dump_cleanup(NetFilterState *nf) | |
165 | { | |
166 | NetFilterDumpState *nfds = FILTER_DUMP(nf); | |
167 | ||
168 | dump_cleanup(&nfds->ds); | |
169 | } | |
170 | ||
171 | static void filter_dump_setup(NetFilterState *nf, Error **errp) | |
172 | { | |
173 | NetFilterDumpState *nfds = FILTER_DUMP(nf); | |
174 | ||
175 | if (!nfds->filename) { | |
176 | error_setg(errp, "dump filter needs 'file' property set!"); | |
177 | return; | |
178 | } | |
179 | ||
180 | net_dump_state_init(&nfds->ds, nfds->filename, nfds->maxlen, errp); | |
181 | } | |
182 | ||
d7bce999 EB |
183 | static void filter_dump_get_maxlen(Object *obj, Visitor *v, const char *name, |
184 | void *opaque, Error **errp) | |
9d3e12e8 TH |
185 | { |
186 | NetFilterDumpState *nfds = FILTER_DUMP(obj); | |
187 | uint32_t value = nfds->maxlen; | |
188 | ||
51e72bc1 | 189 | visit_type_uint32(v, name, &value, errp); |
9d3e12e8 TH |
190 | } |
191 | ||
d7bce999 EB |
192 | static void filter_dump_set_maxlen(Object *obj, Visitor *v, const char *name, |
193 | void *opaque, Error **errp) | |
9d3e12e8 TH |
194 | { |
195 | NetFilterDumpState *nfds = FILTER_DUMP(obj); | |
9d3e12e8 TH |
196 | uint32_t value; |
197 | ||
668f62ec | 198 | if (!visit_type_uint32(v, name, &value, errp)) { |
dcfe4805 | 199 | return; |
9d3e12e8 TH |
200 | } |
201 | if (value == 0) { | |
dcfe4805 | 202 | error_setg(errp, "Property '%s.%s' doesn't take value '%u'", |
9d3e12e8 | 203 | object_get_typename(obj), name, value); |
dcfe4805 | 204 | return; |
9d3e12e8 TH |
205 | } |
206 | nfds->maxlen = value; | |
9d3e12e8 TH |
207 | } |
208 | ||
209 | static char *file_dump_get_filename(Object *obj, Error **errp) | |
210 | { | |
211 | NetFilterDumpState *nfds = FILTER_DUMP(obj); | |
212 | ||
213 | return g_strdup(nfds->filename); | |
214 | } | |
215 | ||
216 | static void file_dump_set_filename(Object *obj, const char *value, Error **errp) | |
217 | { | |
218 | NetFilterDumpState *nfds = FILTER_DUMP(obj); | |
219 | ||
220 | g_free(nfds->filename); | |
221 | nfds->filename = g_strdup(value); | |
222 | } | |
223 | ||
224 | static void filter_dump_instance_init(Object *obj) | |
225 | { | |
226 | NetFilterDumpState *nfds = FILTER_DUMP(obj); | |
227 | ||
228 | nfds->maxlen = 65536; | |
9d3e12e8 TH |
229 | } |
230 | ||
b50c7d45 LZ |
231 | static void filter_dump_instance_finalize(Object *obj) |
232 | { | |
233 | NetFilterDumpState *nfds = FILTER_DUMP(obj); | |
234 | ||
235 | g_free(nfds->filename); | |
236 | } | |
237 | ||
9d3e12e8 TH |
238 | static void filter_dump_class_init(ObjectClass *oc, void *data) |
239 | { | |
240 | NetFilterClass *nfc = NETFILTER_CLASS(oc); | |
241 | ||
f0e34a06 EH |
242 | object_class_property_add(oc, "maxlen", "uint32", filter_dump_get_maxlen, |
243 | filter_dump_set_maxlen, NULL, NULL); | |
244 | object_class_property_add_str(oc, "file", file_dump_get_filename, | |
245 | file_dump_set_filename); | |
246 | ||
9d3e12e8 TH |
247 | nfc->setup = filter_dump_setup; |
248 | nfc->cleanup = filter_dump_cleanup; | |
249 | nfc->receive_iov = filter_dump_receive_iov; | |
250 | } | |
251 | ||
252 | static const TypeInfo filter_dump_info = { | |
253 | .name = TYPE_FILTER_DUMP, | |
254 | .parent = TYPE_NETFILTER, | |
255 | .class_init = filter_dump_class_init, | |
256 | .instance_init = filter_dump_instance_init, | |
b50c7d45 | 257 | .instance_finalize = filter_dump_instance_finalize, |
9d3e12e8 TH |
258 | .instance_size = sizeof(NetFilterDumpState), |
259 | }; | |
260 | ||
261 | static void filter_dump_register_types(void) | |
262 | { | |
263 | type_register_static(&filter_dump_info); | |
264 | } | |
265 | ||
266 | type_init(filter_dump_register_types); |