]>
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" |
a8d25326 | 26 | #include "qemu-common.h" |
a245fc18 | 27 | #include "clients.h" |
da34e65c | 28 | #include "qapi/error.h" |
1de7afc9 | 29 | #include "qemu/error-report.h" |
43192fcc | 30 | #include "qemu/iov.h" |
0b8fa32f | 31 | #include "qemu/module.h" |
1de7afc9 | 32 | #include "qemu/timer.h" |
9d3e12e8 TH |
33 | #include "qapi/visitor.h" |
34 | #include "net/filter.h" | |
db1015e9 | 35 | #include "qom/object.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 | ||
75310e34 | 64 | static ssize_t dump_receive_iov(DumpState *s, const struct iovec *iov, int cnt) |
1abecf77 | 65 | { |
1abecf77 MM |
66 | struct pcap_sf_pkthdr hdr; |
67 | int64_t ts; | |
68 | int caplen; | |
43192fcc TH |
69 | size_t size = iov_size(iov, cnt); |
70 | struct iovec dumpiov[cnt + 1]; | |
1abecf77 MM |
71 | |
72 | /* Early return in case of previous error. */ | |
73 | if (s->fd < 0) { | |
74 | return size; | |
75 | } | |
76 | ||
ab60b748 | 77 | ts = qemu_clock_get_us(QEMU_CLOCK_VIRTUAL); |
1abecf77 MM |
78 | caplen = size > s->pcap_caplen ? s->pcap_caplen : size; |
79 | ||
0fa29915 | 80 | hdr.ts.tv_sec = ts / 1000000 + s->start_ts; |
1abecf77 MM |
81 | hdr.ts.tv_usec = ts % 1000000; |
82 | hdr.caplen = caplen; | |
83 | hdr.len = size; | |
43192fcc TH |
84 | |
85 | dumpiov[0].iov_base = &hdr; | |
86 | dumpiov[0].iov_len = sizeof(hdr); | |
87 | cnt = iov_copy(&dumpiov[1], cnt, iov, cnt, 0, caplen); | |
88 | ||
89 | if (writev(s->fd, dumpiov, cnt + 1) != sizeof(hdr) + caplen) { | |
34f22fc0 | 90 | error_report("network dump write error - stopping dump"); |
1abecf77 MM |
91 | close(s->fd); |
92 | s->fd = -1; | |
93 | } | |
94 | ||
95 | return size; | |
96 | } | |
97 | ||
75310e34 | 98 | static void dump_cleanup(DumpState *s) |
1abecf77 | 99 | { |
1abecf77 | 100 | close(s->fd); |
75310e34 | 101 | s->fd = -1; |
1abecf77 MM |
102 | } |
103 | ||
7bc3074c TH |
104 | static int net_dump_state_init(DumpState *s, const char *filename, |
105 | int len, Error **errp) | |
1abecf77 MM |
106 | { |
107 | struct pcap_file_hdr hdr; | |
0fa29915 | 108 | struct tm tm; |
731d5856 | 109 | int fd; |
1abecf77 | 110 | |
6514ed52 | 111 | fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0644); |
731d5856 | 112 | if (fd < 0) { |
857d2087 | 113 | error_setg_errno(errp, errno, "net dump: can't open %s", filename); |
1abecf77 MM |
114 | return -1; |
115 | } | |
116 | ||
1abecf77 MM |
117 | hdr.magic = PCAP_MAGIC; |
118 | hdr.version_major = 2; | |
119 | hdr.version_minor = 4; | |
120 | hdr.thiszone = 0; | |
121 | hdr.sigfigs = 0; | |
731d5856 | 122 | hdr.snaplen = len; |
1abecf77 MM |
123 | hdr.linktype = 1; |
124 | ||
731d5856 | 125 | if (write(fd, &hdr, sizeof(hdr)) < sizeof(hdr)) { |
857d2087 | 126 | error_setg_errno(errp, errno, "net dump write error"); |
731d5856 | 127 | close(fd); |
1abecf77 MM |
128 | return -1; |
129 | } | |
130 | ||
731d5856 MM |
131 | s->fd = fd; |
132 | s->pcap_caplen = len; | |
133 | ||
0fa29915 HP |
134 | qemu_get_timedate(&tm, 0); |
135 | s->start_ts = mktime(&tm); | |
136 | ||
1abecf77 MM |
137 | return 0; |
138 | } | |
139 | ||
9d3e12e8 TH |
140 | #define TYPE_FILTER_DUMP "filter-dump" |
141 | ||
8063396b | 142 | OBJECT_DECLARE_SIMPLE_TYPE(NetFilterDumpState, FILTER_DUMP) |
9d3e12e8 TH |
143 | |
144 | struct NetFilterDumpState { | |
145 | NetFilterState nfs; | |
146 | DumpState ds; | |
147 | char *filename; | |
148 | uint32_t maxlen; | |
149 | }; | |
9d3e12e8 TH |
150 | |
151 | static ssize_t filter_dump_receive_iov(NetFilterState *nf, NetClientState *sndr, | |
152 | unsigned flags, const struct iovec *iov, | |
153 | int iovcnt, NetPacketSent *sent_cb) | |
154 | { | |
155 | NetFilterDumpState *nfds = FILTER_DUMP(nf); | |
156 | ||
157 | dump_receive_iov(&nfds->ds, iov, iovcnt); | |
158 | return 0; | |
159 | } | |
160 | ||
161 | static void filter_dump_cleanup(NetFilterState *nf) | |
162 | { | |
163 | NetFilterDumpState *nfds = FILTER_DUMP(nf); | |
164 | ||
165 | dump_cleanup(&nfds->ds); | |
166 | } | |
167 | ||
168 | static void filter_dump_setup(NetFilterState *nf, Error **errp) | |
169 | { | |
170 | NetFilterDumpState *nfds = FILTER_DUMP(nf); | |
171 | ||
172 | if (!nfds->filename) { | |
173 | error_setg(errp, "dump filter needs 'file' property set!"); | |
174 | return; | |
175 | } | |
176 | ||
177 | net_dump_state_init(&nfds->ds, nfds->filename, nfds->maxlen, errp); | |
178 | } | |
179 | ||
d7bce999 EB |
180 | static void filter_dump_get_maxlen(Object *obj, Visitor *v, const char *name, |
181 | void *opaque, Error **errp) | |
9d3e12e8 TH |
182 | { |
183 | NetFilterDumpState *nfds = FILTER_DUMP(obj); | |
184 | uint32_t value = nfds->maxlen; | |
185 | ||
51e72bc1 | 186 | visit_type_uint32(v, name, &value, errp); |
9d3e12e8 TH |
187 | } |
188 | ||
d7bce999 EB |
189 | static void filter_dump_set_maxlen(Object *obj, Visitor *v, const char *name, |
190 | void *opaque, Error **errp) | |
9d3e12e8 TH |
191 | { |
192 | NetFilterDumpState *nfds = FILTER_DUMP(obj); | |
9d3e12e8 TH |
193 | uint32_t value; |
194 | ||
668f62ec | 195 | if (!visit_type_uint32(v, name, &value, errp)) { |
dcfe4805 | 196 | return; |
9d3e12e8 TH |
197 | } |
198 | if (value == 0) { | |
dcfe4805 | 199 | error_setg(errp, "Property '%s.%s' doesn't take value '%u'", |
9d3e12e8 | 200 | object_get_typename(obj), name, value); |
dcfe4805 | 201 | return; |
9d3e12e8 TH |
202 | } |
203 | nfds->maxlen = value; | |
9d3e12e8 TH |
204 | } |
205 | ||
206 | static char *file_dump_get_filename(Object *obj, Error **errp) | |
207 | { | |
208 | NetFilterDumpState *nfds = FILTER_DUMP(obj); | |
209 | ||
210 | return g_strdup(nfds->filename); | |
211 | } | |
212 | ||
213 | static void file_dump_set_filename(Object *obj, const char *value, Error **errp) | |
214 | { | |
215 | NetFilterDumpState *nfds = FILTER_DUMP(obj); | |
216 | ||
217 | g_free(nfds->filename); | |
218 | nfds->filename = g_strdup(value); | |
219 | } | |
220 | ||
221 | static void filter_dump_instance_init(Object *obj) | |
222 | { | |
223 | NetFilterDumpState *nfds = FILTER_DUMP(obj); | |
224 | ||
225 | nfds->maxlen = 65536; | |
9d3e12e8 TH |
226 | } |
227 | ||
b50c7d45 LZ |
228 | static void filter_dump_instance_finalize(Object *obj) |
229 | { | |
230 | NetFilterDumpState *nfds = FILTER_DUMP(obj); | |
231 | ||
232 | g_free(nfds->filename); | |
233 | } | |
234 | ||
9d3e12e8 TH |
235 | static void filter_dump_class_init(ObjectClass *oc, void *data) |
236 | { | |
237 | NetFilterClass *nfc = NETFILTER_CLASS(oc); | |
238 | ||
f0e34a06 EH |
239 | object_class_property_add(oc, "maxlen", "uint32", filter_dump_get_maxlen, |
240 | filter_dump_set_maxlen, NULL, NULL); | |
241 | object_class_property_add_str(oc, "file", file_dump_get_filename, | |
242 | file_dump_set_filename); | |
243 | ||
9d3e12e8 TH |
244 | nfc->setup = filter_dump_setup; |
245 | nfc->cleanup = filter_dump_cleanup; | |
246 | nfc->receive_iov = filter_dump_receive_iov; | |
247 | } | |
248 | ||
249 | static const TypeInfo filter_dump_info = { | |
250 | .name = TYPE_FILTER_DUMP, | |
251 | .parent = TYPE_NETFILTER, | |
252 | .class_init = filter_dump_class_init, | |
253 | .instance_init = filter_dump_instance_init, | |
b50c7d45 | 254 | .instance_finalize = filter_dump_instance_finalize, |
9d3e12e8 TH |
255 | .instance_size = sizeof(NetFilterDumpState), |
256 | }; | |
257 | ||
258 | static void filter_dump_register_types(void) | |
259 | { | |
260 | type_register_static(&filter_dump_info); | |
261 | } | |
262 | ||
263 | type_init(filter_dump_register_types); |