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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * pcap.c: libpcap packet capture format
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #include <vnet/unix/pcap.h>
41 #include <sys/fcntl.h>
45 * @brief PCAP function.
50 * \#include <vnet/unix/pcap.h>
52 * static pcap_main_t pcap = {
53 * .file_name = "/tmp/ip4",
54 * .n_packets_to_capture = 2,
55 * .packet_type = PCAP_PACKET_TYPE_ip,
61 * <code><pre>pcap_add_buffer (&pcap, vm, pi0, 128);</pre></code>
63 * File will be written after @c n_packets_to_capture or call to pcap_write (&pcap).
68 * @brief Close PCAP file
70 * @return rc - clib_error_t
74 pcap_close (pcap_main_t * pm)
76 close (pm->file_descriptor);
77 pm->flags &= ~PCAP_MAIN_INIT_DONE;
78 pm->file_descriptor = -1;
83 * @brief Write PCAP file
85 * @return rc - clib_error_t
89 pcap_write (pcap_main_t * pm)
91 clib_error_t * error = 0;
93 if (! (pm->flags & PCAP_MAIN_INIT_DONE))
95 pcap_file_header_t fh;
99 pm->file_name = "/tmp/vnet.pcap";
101 pm->file_descriptor = open (pm->file_name, O_CREAT | O_TRUNC | O_WRONLY, 0664);
102 if (pm->file_descriptor < 0)
104 error = clib_error_return_unix (0, "failed to open `%s'", pm->file_name);
108 pm->flags |= PCAP_MAIN_INIT_DONE;
109 pm->n_packets_captured = 0;
110 pm->n_pcap_data_written = 0;
112 /* Write file header. */
113 memset (&fh, 0, sizeof (fh));
114 fh.magic = 0xa1b2c3d4;
115 fh.major_version = 2;
116 fh.minor_version = 4;
118 fh.max_packet_size_in_bytes = 1 << 16;
119 fh.packet_type = pm->packet_type;
120 n = write (pm->file_descriptor, &fh, sizeof (fh));
121 if (n != sizeof (fh))
124 error = clib_error_return_unix (0, "write file header `%s'", pm->file_name);
126 error = clib_error_return (0, "short write of file header `%s'", pm->file_name);
131 while (vec_len (pm->pcap_data) > pm->n_pcap_data_written)
133 int n = vec_len (pm->pcap_data) - pm->n_pcap_data_written;
135 n = write (pm->file_descriptor,
136 vec_elt_at_index (pm->pcap_data, pm->n_pcap_data_written), n);
138 if (n < 0 && unix_error_is_fatal (errno))
140 error = clib_error_return_unix (0, "write `%s'", pm->file_name);
143 pm->n_pcap_data_written += n;
146 if (pm->n_pcap_data_written >= vec_len (pm->pcap_data))
148 vec_reset_length (pm->pcap_data);
149 pm->n_pcap_data_written = 0;
152 if (pm->n_packets_captured >= pm->n_packets_to_capture)
158 if (pm->file_descriptor >= 0)
159 close (pm->file_descriptor);
165 * @brief Read PCAP file
167 * @return rc - clib_error_t
170 clib_error_t * pcap_read (pcap_main_t * pm)
172 clib_error_t * error = 0;
173 int fd, need_swap, n;
174 pcap_file_header_t fh;
175 pcap_packet_header_t ph;
177 fd = open (pm->file_name, O_RDONLY);
180 error = clib_error_return_unix (0, "open `%s'", pm->file_name);
184 if (read (fd, &fh, sizeof (fh)) != sizeof (fh))
186 error = clib_error_return_unix (0, "read file header `%s'", pm->file_name);
191 if (fh.magic == 0xd4c3b2a1)
194 #define _(t,f) fh.f = clib_byte_swap_##t (fh.f);
195 foreach_pcap_file_header;
199 if (fh.magic != 0xa1b2c3d4)
201 error = clib_error_return (0, "bad magic `%s'", pm->file_name);
205 pm->min_packet_bytes = 0;
206 pm->max_packet_bytes = 0;
207 while ((n = read (fd, &ph, sizeof (ph))) != 0)
213 #define _(t,f) ph.f = clib_byte_swap_##t (ph.f);
214 foreach_pcap_packet_header;
218 data = vec_new (u8, ph.n_bytes_in_packet);
219 if (read (fd, data, ph.n_packet_bytes_stored_in_file) != ph.n_packet_bytes_stored_in_file)
221 error = clib_error_return (0, "short read `%s'", pm->file_name);
225 if (vec_len (pm->packets_read) == 0)
226 pm->min_packet_bytes = pm->max_packet_bytes = ph.n_bytes_in_packet;
229 pm->min_packet_bytes = clib_min (pm->min_packet_bytes, ph.n_bytes_in_packet);
230 pm->max_packet_bytes = clib_max (pm->max_packet_bytes, ph.n_bytes_in_packet);
233 vec_add1 (pm->packets_read, data);