]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
f9ef2270 | 2 | * Copyright (c) 2009, 2010, 2012, 2013, 2014, 2015 Nicira, Inc. |
064af421 | 3 | * |
a14bc59f BP |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
064af421 | 7 | * |
a14bc59f BP |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
064af421 BP |
15 | */ |
16 | ||
17 | #include <config.h> | |
2c78a3e6 | 18 | #include "pcap-file.h" |
064af421 BP |
19 | #include <errno.h> |
20 | #include <inttypes.h> | |
b6cdfbb4 | 21 | #include <stdlib.h> |
064af421 | 22 | #include <string.h> |
b6cdfbb4 | 23 | #include <sys/stat.h> |
4cf5406b | 24 | #include "byte-order.h" |
064af421 | 25 | #include "compiler.h" |
cf62fa4c | 26 | #include "dp-packet.h" |
f3dd1419 BP |
27 | #include "flow.h" |
28 | #include "hmap.h" | |
f3dd1419 | 29 | #include "packets.h" |
a797eab3 | 30 | #include "timeval.h" |
f3dd1419 | 31 | #include "unaligned.h" |
e6211adc | 32 | #include "openvswitch/vlog.h" |
064af421 | 33 | |
d98e6007 | 34 | VLOG_DEFINE_THIS_MODULE(pcap); |
5136ce49 | 35 | |
064af421 BP |
36 | struct pcap_hdr { |
37 | uint32_t magic_number; /* magic number */ | |
38 | uint16_t version_major; /* major version number */ | |
39 | uint16_t version_minor; /* minor version number */ | |
40 | int32_t thiszone; /* GMT to local correction */ | |
41 | uint32_t sigfigs; /* accuracy of timestamps */ | |
42 | uint32_t snaplen; /* max length of captured packets */ | |
43 | uint32_t network; /* data link type */ | |
381328fe BP |
44 | }; |
45 | BUILD_ASSERT_DECL(sizeof(struct pcap_hdr) == 24); | |
064af421 BP |
46 | |
47 | struct pcaprec_hdr { | |
48 | uint32_t ts_sec; /* timestamp seconds */ | |
49 | uint32_t ts_usec; /* timestamp microseconds */ | |
50 | uint32_t incl_len; /* number of octets of packet saved in file */ | |
51 | uint32_t orig_len; /* actual length of packet */ | |
381328fe BP |
52 | }; |
53 | BUILD_ASSERT_DECL(sizeof(struct pcaprec_hdr) == 16); | |
064af421 BP |
54 | |
55 | FILE * | |
50aa0364 | 56 | ovs_pcap_open(const char *file_name, const char *mode) |
064af421 | 57 | { |
b6cdfbb4 | 58 | struct stat s; |
064af421 | 59 | FILE *file; |
b6cdfbb4 | 60 | int error; |
064af421 | 61 | |
b6cdfbb4 BP |
62 | ovs_assert(!strcmp(mode, "rb") || |
63 | !strcmp(mode, "wb") || | |
64 | !strcmp(mode, "ab")); | |
064af421 BP |
65 | |
66 | file = fopen(file_name, mode); | |
67 | if (file == NULL) { | |
b6cdfbb4 BP |
68 | VLOG_WARN("%s: failed to open pcap file for %s (%s)", file_name, |
69 | (mode[0] == 'r' ? "reading" | |
70 | : mode[0] == 'w' ? "writing" | |
71 | : "appending"), | |
72 | ovs_strerror(errno)); | |
064af421 BP |
73 | return NULL; |
74 | } | |
75 | ||
b6cdfbb4 BP |
76 | switch (mode[0]) { |
77 | case 'r': | |
50aa0364 | 78 | error = ovs_pcap_read_header(file); |
b6cdfbb4 BP |
79 | if (error) { |
80 | errno = error; | |
064af421 BP |
81 | fclose(file); |
82 | return NULL; | |
83 | } | |
b6cdfbb4 BP |
84 | break; |
85 | ||
86 | case 'w': | |
50aa0364 | 87 | ovs_pcap_write_header(file); |
b6cdfbb4 BP |
88 | break; |
89 | ||
90 | case 'a': | |
91 | if (!fstat(fileno(file), &s) && !s.st_size) { | |
50aa0364 | 92 | ovs_pcap_write_header(file); |
b6cdfbb4 BP |
93 | } |
94 | break; | |
95 | ||
96 | default: | |
428b2edd | 97 | OVS_NOT_REACHED(); |
064af421 BP |
98 | } |
99 | return file; | |
100 | } | |
101 | ||
102 | int | |
50aa0364 | 103 | ovs_pcap_read_header(FILE *file) |
064af421 BP |
104 | { |
105 | struct pcap_hdr ph; | |
106 | if (fread(&ph, sizeof ph, 1, file) != 1) { | |
107 | int error = ferror(file) ? errno : EOF; | |
c18ea70d | 108 | VLOG_WARN("failed to read pcap header: %s", ovs_retval_to_string(error)); |
064af421 BP |
109 | return error; |
110 | } | |
111 | if (ph.magic_number != 0xa1b2c3d4 && ph.magic_number != 0xd4c3b2a1) { | |
112 | VLOG_WARN("bad magic 0x%08"PRIx32" reading pcap file " | |
113 | "(expected 0xa1b2c3d4 or 0xd4c3b2a1)", ph.magic_number); | |
114 | return EPROTO; | |
115 | } | |
116 | return 0; | |
117 | } | |
118 | ||
119 | void | |
50aa0364 | 120 | ovs_pcap_write_header(FILE *file) |
064af421 BP |
121 | { |
122 | /* The pcap reader is responsible for figuring out endianness based on the | |
123 | * magic number, so the lack of htonX calls here is intentional. */ | |
124 | struct pcap_hdr ph; | |
125 | ph.magic_number = 0xa1b2c3d4; | |
126 | ph.version_major = 2; | |
127 | ph.version_minor = 4; | |
128 | ph.thiszone = 0; | |
129 | ph.sigfigs = 0; | |
130 | ph.snaplen = 1518; | |
131 | ph.network = 1; /* Ethernet */ | |
b1b5cd8e | 132 | ignore(fwrite(&ph, sizeof ph, 1, file)); |
f9ef2270 | 133 | fflush(file); |
064af421 BP |
134 | } |
135 | ||
136 | int | |
cf62fa4c | 137 | ovs_pcap_read(FILE *file, struct dp_packet **bufp, long long int *when) |
064af421 BP |
138 | { |
139 | struct pcaprec_hdr prh; | |
cf62fa4c | 140 | struct dp_packet *buf; |
064af421 BP |
141 | void *data; |
142 | size_t len; | |
a797eab3 | 143 | bool swap; |
064af421 BP |
144 | |
145 | *bufp = NULL; | |
146 | ||
147 | /* Read header. */ | |
148 | if (fread(&prh, sizeof prh, 1, file) != 1) { | |
f6cdbd3e BP |
149 | if (ferror(file)) { |
150 | int error = errno; | |
151 | VLOG_WARN("failed to read pcap record header: %s", | |
152 | ovs_retval_to_string(error)); | |
153 | return error; | |
154 | } else { | |
155 | return EOF; | |
156 | } | |
064af421 BP |
157 | } |
158 | ||
159 | /* Calculate length. */ | |
160 | len = prh.incl_len; | |
a797eab3 BP |
161 | swap = len > 0xffff; |
162 | if (swap) { | |
163 | len = uint32_byteswap(len); | |
164 | if (len > 0xffff) { | |
165 | VLOG_WARN("bad packet length %"PRIuSIZE" or %"PRIu32 | |
064af421 | 166 | "reading pcap file", |
a797eab3 | 167 | len, uint32_byteswap(len)); |
064af421 BP |
168 | return EPROTO; |
169 | } | |
a797eab3 BP |
170 | } |
171 | ||
172 | /* Calculate time. */ | |
173 | if (when) { | |
174 | uint32_t ts_sec = swap ? uint32_byteswap(prh.ts_sec) : prh.ts_sec; | |
175 | uint32_t ts_usec = swap ? uint32_byteswap(prh.ts_usec) : prh.ts_usec; | |
176 | *when = ts_sec * 1000LL + ts_usec / 1000; | |
064af421 BP |
177 | } |
178 | ||
179 | /* Read packet. */ | |
cf62fa4c PS |
180 | buf = dp_packet_new(len); |
181 | data = dp_packet_put_uninit(buf, len); | |
064af421 BP |
182 | if (fread(data, len, 1, file) != 1) { |
183 | int error = ferror(file) ? errno : EOF; | |
184 | VLOG_WARN("failed to read pcap packet: %s", | |
c18ea70d | 185 | ovs_retval_to_string(error)); |
cf62fa4c | 186 | dp_packet_delete(buf); |
064af421 BP |
187 | return error; |
188 | } | |
189 | *bufp = buf; | |
190 | return 0; | |
191 | } | |
192 | ||
193 | void | |
cf62fa4c | 194 | ovs_pcap_write(FILE *file, struct dp_packet *buf) |
064af421 BP |
195 | { |
196 | struct pcaprec_hdr prh; | |
a797eab3 BP |
197 | struct timeval tv; |
198 | ||
199 | xgettimeofday(&tv); | |
200 | prh.ts_sec = tv.tv_sec; | |
201 | prh.ts_usec = tv.tv_usec; | |
cf62fa4c PS |
202 | prh.incl_len = dp_packet_size(buf); |
203 | prh.orig_len = dp_packet_size(buf); | |
b1b5cd8e | 204 | ignore(fwrite(&prh, sizeof prh, 1, file)); |
cf62fa4c | 205 | ignore(fwrite(dp_packet_data(buf), dp_packet_size(buf), 1, file)); |
f9ef2270 | 206 | fflush(file); |
064af421 | 207 | } |
f3dd1419 BP |
208 | \f |
209 | struct tcp_key { | |
210 | ovs_be32 nw_src, nw_dst; | |
211 | ovs_be16 tp_src, tp_dst; | |
212 | }; | |
213 | ||
214 | struct tcp_stream { | |
215 | struct hmap_node hmap_node; | |
216 | struct tcp_key key; | |
217 | uint32_t seq_no; | |
cf62fa4c | 218 | struct dp_packet payload; |
f3dd1419 BP |
219 | }; |
220 | ||
221 | struct tcp_reader { | |
222 | struct hmap streams; | |
223 | }; | |
224 | ||
225 | static void | |
226 | tcp_stream_destroy(struct tcp_reader *r, struct tcp_stream *stream) | |
227 | { | |
228 | hmap_remove(&r->streams, &stream->hmap_node); | |
cf62fa4c | 229 | dp_packet_uninit(&stream->payload); |
f3dd1419 BP |
230 | free(stream); |
231 | } | |
232 | ||
233 | /* Returns a new data structure for extracting TCP stream data from an | |
234 | * Ethernet packet capture */ | |
235 | struct tcp_reader * | |
236 | tcp_reader_open(void) | |
237 | { | |
238 | struct tcp_reader *r; | |
239 | ||
240 | r = xmalloc(sizeof *r); | |
241 | hmap_init(&r->streams); | |
242 | return r; | |
243 | } | |
244 | ||
245 | /* Closes and frees 'r'. */ | |
246 | void | |
247 | tcp_reader_close(struct tcp_reader *r) | |
248 | { | |
249 | struct tcp_stream *stream, *next_stream; | |
250 | ||
251 | HMAP_FOR_EACH_SAFE (stream, next_stream, hmap_node, &r->streams) { | |
252 | tcp_stream_destroy(r, stream); | |
253 | } | |
254 | hmap_destroy(&r->streams); | |
255 | free(r); | |
256 | } | |
257 | ||
258 | static struct tcp_stream * | |
276e2864 BP |
259 | tcp_stream_lookup(struct tcp_reader *r, |
260 | const struct tcp_key *key, uint32_t hash) | |
f3dd1419 BP |
261 | { |
262 | struct tcp_stream *stream; | |
f3dd1419 BP |
263 | |
264 | HMAP_FOR_EACH_WITH_HASH (stream, hmap_node, hash, &r->streams) { | |
276e2864 | 265 | if (!memcmp(&stream->key, key, sizeof *key)) { |
f3dd1419 BP |
266 | return stream; |
267 | } | |
268 | } | |
276e2864 BP |
269 | return NULL; |
270 | } | |
271 | ||
272 | static struct tcp_stream * | |
273 | tcp_stream_new(struct tcp_reader *r, const struct tcp_key *key, uint32_t hash) | |
274 | { | |
275 | struct tcp_stream *stream; | |
f3dd1419 BP |
276 | |
277 | stream = xmalloc(sizeof *stream); | |
278 | hmap_insert(&r->streams, &stream->hmap_node, hash); | |
276e2864 | 279 | memcpy(&stream->key, key, sizeof *key); |
f3dd1419 | 280 | stream->seq_no = 0; |
cf62fa4c | 281 | dp_packet_init(&stream->payload, 2048); |
f3dd1419 BP |
282 | return stream; |
283 | } | |
284 | ||
285 | /* Processes 'packet' through TCP reader 'r'. The caller must have already | |
286 | * extracted the packet's headers into 'flow', using flow_extract(). | |
287 | * | |
288 | * If 'packet' is a TCP packet, then the reader attempts to reconstruct the | |
cf62fa4c PS |
289 | * data stream. If successful, it returns an dp_packet that represents the data |
290 | * stream so far. The caller may examine the data in the dp_packet and pull off | |
f3dd1419 BP |
291 | * any data that it has fully processed. The remaining data that the caller |
292 | * does not pull off will be presented again in future calls if more data | |
293 | * arrives in the stream. | |
294 | * | |
295 | * Returns null if 'packet' doesn't add new data to a TCP stream. */ | |
cf62fa4c | 296 | struct dp_packet * |
f3dd1419 | 297 | tcp_reader_run(struct tcp_reader *r, const struct flow *flow, |
cf62fa4c | 298 | const struct dp_packet *packet) |
f3dd1419 BP |
299 | { |
300 | struct tcp_stream *stream; | |
301 | struct tcp_header *tcp; | |
cf62fa4c | 302 | struct dp_packet *payload; |
276e2864 BP |
303 | unsigned int l7_length; |
304 | struct tcp_key key; | |
305 | uint32_t hash; | |
f3dd1419 BP |
306 | uint32_t seq; |
307 | uint8_t flags; | |
cf62fa4c | 308 | const char *l7 = dp_packet_get_tcp_payload(packet); |
f3dd1419 BP |
309 | |
310 | if (flow->dl_type != htons(ETH_TYPE_IP) | |
311 | || flow->nw_proto != IPPROTO_TCP | |
5a51b2cd | 312 | || !l7) { |
f3dd1419 BP |
313 | return NULL; |
314 | } | |
cf62fa4c | 315 | tcp = dp_packet_l4(packet); |
f3dd1419 | 316 | flags = TCP_FLAGS(tcp->tcp_ctl); |
cf62fa4c | 317 | l7_length = (char *) dp_packet_tail(packet) - l7; |
f3dd1419 | 318 | seq = ntohl(get_16aligned_be32(&tcp->tcp_seq)); |
276e2864 BP |
319 | |
320 | /* Construct key. */ | |
321 | memset(&key, 0, sizeof key); | |
322 | key.nw_src = flow->nw_src; | |
323 | key.nw_dst = flow->nw_dst; | |
324 | key.tp_src = flow->tp_src; | |
325 | key.tp_dst = flow->tp_dst; | |
326 | hash = hash_bytes(&key, sizeof key, 0); | |
327 | ||
328 | /* Find existing stream or start a new one for a SYN or if there's data. */ | |
329 | stream = tcp_stream_lookup(r, &key, hash); | |
330 | if (!stream) { | |
331 | if (flags & TCP_SYN || l7_length) { | |
332 | stream = tcp_stream_new(r, &key, hash); | |
333 | stream->seq_no = flags & TCP_SYN ? seq + 1 : seq; | |
334 | } else { | |
335 | return NULL; | |
336 | } | |
337 | } | |
338 | ||
339 | payload = &stream->payload; | |
340 | if (flags & TCP_SYN || !stream->seq_no) { | |
cf62fa4c | 341 | dp_packet_clear(payload); |
f3dd1419 BP |
342 | stream->seq_no = seq + 1; |
343 | return NULL; | |
344 | } else if (flags & (TCP_FIN | TCP_RST)) { | |
345 | tcp_stream_destroy(r, stream); | |
346 | return NULL; | |
347 | } else if (seq == stream->seq_no) { | |
f3dd1419 BP |
348 | /* Shift all of the existing payload to the very beginning of the |
349 | * allocated space, so that we reuse allocated space instead of | |
350 | * continually expanding it. */ | |
cf62fa4c | 351 | dp_packet_shift(payload, (char *) dp_packet_base(payload) - (char *) dp_packet_data(payload)); |
f3dd1419 | 352 | |
cf62fa4c | 353 | dp_packet_put(payload, l7, l7_length); |
276e2864 | 354 | stream->seq_no += l7_length; |
f3dd1419 BP |
355 | return payload; |
356 | } else { | |
357 | return NULL; | |
358 | } | |
359 | } |