blob: 4f8b6bb429c66fa35167eabc6139079126a3647c [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
Ed Warnickecb9cada2015-12-08 15:45:58 -070040#include <sys/fcntl.h>
Dave Barach3ae28732018-11-16 17:19:00 -050041#include <vppinfra/pcap.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070042
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>
Dave Barach3ae28732018-11-16 17:19:00 -050050 * \#include <vppinfra/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 */
Damjan Mariondae1c7e2020-10-17 13:32:25 +020073__clib_export clib_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 Mariondae1c7e2020-10-17 13:32:25 +020088__clib_export clib_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;
Dave Barachf91080c2018-07-26 12:27:27 -0400113 clib_spinlock_init (&pm->lock);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114
115 /* Write file header. */
Dave Barachb7b92992018-10-17 10:38:51 -0400116 clib_memset (&fh, 0, sizeof (fh));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117 fh.magic = 0xa1b2c3d4;
118 fh.major_version = 2;
119 fh.minor_version = 4;
120 fh.time_zone = 0;
121 fh.max_packet_size_in_bytes = 1 << 16;
122 fh.packet_type = pm->packet_type;
123 n = write (pm->file_descriptor, &fh, sizeof (fh));
124 if (n != sizeof (fh))
125 {
126 if (n < 0)
sharath reddy1b0c9832017-11-29 20:08:11 +0530127 error =
128 clib_error_return_unix (0, "write file header `%s'",
129 pm->file_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130 else
sharath reddy1b0c9832017-11-29 20:08:11 +0530131 error =
132 clib_error_return (0, "short write of file header `%s'",
133 pm->file_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134 goto done;
135 }
136 }
137
Damjan Marion92217f32016-07-12 21:58:19 +0200138 while (vec_len (pm->pcap_data) > pm->n_pcap_data_written)
139 {
140 int n = vec_len (pm->pcap_data) - pm->n_pcap_data_written;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141
Damjan Marion92217f32016-07-12 21:58:19 +0200142 n = write (pm->file_descriptor,
sharath reddy1b0c9832017-11-29 20:08:11 +0530143 vec_elt_at_index (pm->pcap_data, pm->n_pcap_data_written),
144 n);
Damjan Marion92217f32016-07-12 21:58:19 +0200145
146 if (n < 0 && unix_error_is_fatal (errno))
147 {
148 error = clib_error_return_unix (0, "write `%s'", pm->file_name);
149 goto done;
150 }
sharath reddy1b0c9832017-11-29 20:08:11 +0530151 pm->n_pcap_data_written += n;
152 }
Damjan Marion92217f32016-07-12 21:58:19 +0200153
154 if (pm->n_pcap_data_written >= vec_len (pm->pcap_data))
155 {
156 vec_reset_length (pm->pcap_data);
157 pm->n_pcap_data_written = 0;
158 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159
sharath reddy1b0c9832017-11-29 20:08:11 +0530160done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161 if (error)
162 {
163 if (pm->file_descriptor >= 0)
164 close (pm->file_descriptor);
165 }
166 return error;
167}
168
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700169/**
170 * @brief Read PCAP file
171 *
172 * @return rc - clib_error_t
173 *
174 */
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200175__clib_export clib_error_t *
sharath reddy1b0c9832017-11-29 20:08:11 +0530176pcap_read (pcap_main_t * pm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177{
sharath reddy1b0c9832017-11-29 20:08:11 +0530178 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179 int fd, need_swap, n;
180 pcap_file_header_t fh;
181 pcap_packet_header_t ph;
182
183 fd = open (pm->file_name, O_RDONLY);
184 if (fd < 0)
185 {
186 error = clib_error_return_unix (0, "open `%s'", pm->file_name);
187 goto done;
188 }
189
190 if (read (fd, &fh, sizeof (fh)) != sizeof (fh))
191 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530192 error =
193 clib_error_return_unix (0, "read file header `%s'", pm->file_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194 goto done;
195 }
196
197 need_swap = 0;
198 if (fh.magic == 0xd4c3b2a1)
199 {
200 need_swap = 1;
201#define _(t,f) fh.f = clib_byte_swap_##t (fh.f);
202 foreach_pcap_file_header;
203#undef _
sharath reddy1b0c9832017-11-29 20:08:11 +0530204 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205
206 if (fh.magic != 0xa1b2c3d4)
207 {
208 error = clib_error_return (0, "bad magic `%s'", pm->file_name);
209 goto done;
210 }
211
212 pm->min_packet_bytes = 0;
213 pm->max_packet_bytes = 0;
214 while ((n = read (fd, &ph, sizeof (ph))) != 0)
215 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530216 u8 *data;
Dave Barach78c56892018-05-16 11:34:35 -0400217 u64 timestamp;
218 u32 timestamp_sec;
219 u32 timestamp_usec;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220
221 if (need_swap)
222 {
223#define _(t,f) ph.f = clib_byte_swap_##t (ph.f);
224 foreach_pcap_packet_header;
225#undef _
226 }
227
228 data = vec_new (u8, ph.n_bytes_in_packet);
sharath reddy1b0c9832017-11-29 20:08:11 +0530229 if (read (fd, data, ph.n_packet_bytes_stored_in_file) !=
230 ph.n_packet_bytes_stored_in_file)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231 {
232 error = clib_error_return (0, "short read `%s'", pm->file_name);
233 goto done;
234 }
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700235
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236 if (vec_len (pm->packets_read) == 0)
237 pm->min_packet_bytes = pm->max_packet_bytes = ph.n_bytes_in_packet;
238 else
239 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530240 pm->min_packet_bytes =
241 clib_min (pm->min_packet_bytes, ph.n_bytes_in_packet);
242 pm->max_packet_bytes =
243 clib_max (pm->max_packet_bytes, ph.n_bytes_in_packet);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244 }
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700245
Dave Barach78c56892018-05-16 11:34:35 -0400246 timestamp_sec = ph.time_in_sec;
247 timestamp_usec = ph.time_in_usec;
248 timestamp = ((u64) timestamp_sec) * 1000000 + (u64) timestamp_usec;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249 vec_add1 (pm->packets_read, data);
Dave Barach78c56892018-05-16 11:34:35 -0400250 vec_add1 (pm->timestamps, timestamp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700251 }
252
sharath reddy1b0c9832017-11-29 20:08:11 +0530253done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254 if (fd >= 0)
255 close (fd);
256 return error;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700257
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258}
sharath reddy1b0c9832017-11-29 20:08:11 +0530259
260/*
261 * fd.io coding-style-patch-verification: ON
262 *
263 * Local Variables:
264 * eval: (c-set-style "gnu")
265 * End:
266 */