blob: 0832b16e7530d12108abd6650f6db3421bff131c [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 * pcap.c: libpcap packet capture format
17 *
18 * Copyright (c) 2008 Eliot Dresselhaus
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 */
39
40#include <vnet/unix/pcap.h>
41#include <sys/fcntl.h>
42
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070043/**
44 * @file
45 * @brief PCAP function.
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070046 *
Chris Luked4024f52016-09-06 09:32:36 -040047 * Usage:
48 *
49 * <code><pre>
50 * \#include <vnet/unix/pcap.h>
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070051 *
52 * static pcap_main_t pcap = {
53 * .file_name = "/tmp/ip4",
54 * .n_packets_to_capture = 2,
55 * .packet_type = PCAP_PACKET_TYPE_ip,
56 * };
Chris Luked4024f52016-09-06 09:32:36 -040057 * </pre></code>
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070058 *
59 * To add a buffer:
60 *
Chris Luked4024f52016-09-06 09:32:36 -040061 * <code><pre>pcap_add_buffer (&pcap, vm, pi0, 128);</pre></code>
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070062 *
Chris Luked4024f52016-09-06 09:32:36 -040063 * File will be written after @c n_packets_to_capture or call to pcap_write (&amp;pcap).
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070064 *
Ed Warnickecb9cada2015-12-08 15:45:58 -070065*/
66
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070067/**
68 * @brief Close PCAP file
69 *
70 * @return rc - clib_error_t
71 *
72 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070073clib_error_t *
Damjan Marion92217f32016-07-12 21:58:19 +020074pcap_close (pcap_main_t * pm)
75{
76 close (pm->file_descriptor);
77 pm->flags &= ~PCAP_MAIN_INIT_DONE;
78 pm->file_descriptor = -1;
79 return 0;
80}
81
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070082/**
83 * @brief Write PCAP file
84 *
85 * @return rc - clib_error_t
86 *
87 */
Damjan Marion92217f32016-07-12 21:58:19 +020088clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -070089pcap_write (pcap_main_t * pm)
90{
sharath reddy1b0c9832017-11-29 20:08:11 +053091 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070092
sharath reddy1b0c9832017-11-29 20:08:11 +053093 if (!(pm->flags & PCAP_MAIN_INIT_DONE))
Ed Warnickecb9cada2015-12-08 15:45:58 -070094 {
95 pcap_file_header_t fh;
96 int n;
97
sharath reddy1b0c9832017-11-29 20:08:11 +053098 if (!pm->file_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -070099 pm->file_name = "/tmp/vnet.pcap";
100
sharath reddy1b0c9832017-11-29 20:08:11 +0530101 pm->file_descriptor =
102 open (pm->file_name, O_CREAT | O_TRUNC | O_WRONLY, 0664);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103 if (pm->file_descriptor < 0)
104 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530105 error =
106 clib_error_return_unix (0, "failed to open `%s'", pm->file_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107 goto done;
108 }
109
110 pm->flags |= PCAP_MAIN_INIT_DONE;
111 pm->n_packets_captured = 0;
112 pm->n_pcap_data_written = 0;
113
114 /* Write file header. */
115 memset (&fh, 0, sizeof (fh));
116 fh.magic = 0xa1b2c3d4;
117 fh.major_version = 2;
118 fh.minor_version = 4;
119 fh.time_zone = 0;
120 fh.max_packet_size_in_bytes = 1 << 16;
121 fh.packet_type = pm->packet_type;
122 n = write (pm->file_descriptor, &fh, sizeof (fh));
123 if (n != sizeof (fh))
124 {
125 if (n < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530126 error =
127 clib_error_return_unix (0, "write file header `%s'",
128 pm->file_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129 else
sharath reddy1b0c9832017-11-29 20:08:11 +0530130 error =
131 clib_error_return (0, "short write of file header `%s'",
132 pm->file_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133 goto done;
134 }
135 }
136
Damjan Marion92217f32016-07-12 21:58:19 +0200137 while (vec_len (pm->pcap_data) > pm->n_pcap_data_written)
138 {
139 int n = vec_len (pm->pcap_data) - pm->n_pcap_data_written;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140
Damjan Marion92217f32016-07-12 21:58:19 +0200141 n = write (pm->file_descriptor,
sharath reddy1b0c9832017-11-29 20:08:11 +0530142 vec_elt_at_index (pm->pcap_data, pm->n_pcap_data_written),
143 n);
Damjan Marion92217f32016-07-12 21:58:19 +0200144
145 if (n < 0 && unix_error_is_fatal (errno))
146 {
147 error = clib_error_return_unix (0, "write `%s'", pm->file_name);
148 goto done;
149 }
sharath reddy1b0c9832017-11-29 20:08:11 +0530150 pm->n_pcap_data_written += n;
151 }
Damjan Marion92217f32016-07-12 21:58:19 +0200152
153 if (pm->n_pcap_data_written >= vec_len (pm->pcap_data))
154 {
155 vec_reset_length (pm->pcap_data);
156 pm->n_pcap_data_written = 0;
157 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158
159 if (pm->n_packets_captured >= pm->n_packets_to_capture)
sharath reddy1b0c9832017-11-29 20:08:11 +0530160 pcap_close (pm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161
sharath reddy1b0c9832017-11-29 20:08:11 +0530162done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163 if (error)
164 {
165 if (pm->file_descriptor >= 0)
166 close (pm->file_descriptor);
167 }
168 return error;
169}
170
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700171/**
172 * @brief Read PCAP file
173 *
174 * @return rc - clib_error_t
175 *
176 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530177clib_error_t *
178pcap_read (pcap_main_t * pm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179{
sharath reddy1b0c9832017-11-29 20:08:11 +0530180 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181 int fd, need_swap, n;
182 pcap_file_header_t fh;
183 pcap_packet_header_t ph;
184
185 fd = open (pm->file_name, O_RDONLY);
186 if (fd < 0)
187 {
188 error = clib_error_return_unix (0, "open `%s'", pm->file_name);
189 goto done;
190 }
191
192 if (read (fd, &fh, sizeof (fh)) != sizeof (fh))
193 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530194 error =
195 clib_error_return_unix (0, "read file header `%s'", pm->file_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196 goto done;
197 }
198
199 need_swap = 0;
200 if (fh.magic == 0xd4c3b2a1)
201 {
202 need_swap = 1;
203#define _(t,f) fh.f = clib_byte_swap_##t (fh.f);
204 foreach_pcap_file_header;
205#undef _
sharath reddy1b0c9832017-11-29 20:08:11 +0530206 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207
208 if (fh.magic != 0xa1b2c3d4)
209 {
210 error = clib_error_return (0, "bad magic `%s'", pm->file_name);
211 goto done;
212 }
213
214 pm->min_packet_bytes = 0;
215 pm->max_packet_bytes = 0;
216 while ((n = read (fd, &ph, sizeof (ph))) != 0)
217 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530218 u8 *data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700219
220 if (need_swap)
221 {
222#define _(t,f) ph.f = clib_byte_swap_##t (ph.f);
223 foreach_pcap_packet_header;
224#undef _
225 }
226
227 data = vec_new (u8, ph.n_bytes_in_packet);
sharath reddy1b0c9832017-11-29 20:08:11 +0530228 if (read (fd, data, ph.n_packet_bytes_stored_in_file) !=
229 ph.n_packet_bytes_stored_in_file)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700230 {
231 error = clib_error_return (0, "short read `%s'", pm->file_name);
232 goto done;
233 }
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700234
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235 if (vec_len (pm->packets_read) == 0)
236 pm->min_packet_bytes = pm->max_packet_bytes = ph.n_bytes_in_packet;
237 else
238 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530239 pm->min_packet_bytes =
240 clib_min (pm->min_packet_bytes, ph.n_bytes_in_packet);
241 pm->max_packet_bytes =
242 clib_max (pm->max_packet_bytes, ph.n_bytes_in_packet);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243 }
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700244
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245 vec_add1 (pm->packets_read, data);
246 }
247
sharath reddy1b0c9832017-11-29 20:08:11 +0530248done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249 if (fd >= 0)
250 close (fd);
251 return error;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700252
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253}
sharath reddy1b0c9832017-11-29 20:08:11 +0530254
255/*
256 * fd.io coding-style-patch-verification: ON
257 *
258 * Local Variables:
259 * eval: (c-set-style "gnu")
260 * End:
261 */