FD.io VPP  v17.04.2-2-ga8f93f8
Vector Packet Processing
pcap.c
Go to the documentation of this file.
1 /*
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 
43 /**
44  * @file
45  * @brief PCAP function.
46  *
47  * Usage:
48  *
49  * <code><pre>
50  * \#include <vnet/unix/pcap.h>
51  *
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  * };
57  * </pre></code>
58  *
59  * To add a buffer:
60  *
61  * <code><pre>pcap_add_buffer (&pcap, vm, pi0, 128);</pre></code>
62  *
63  * File will be written after @c n_packets_to_capture or call to pcap_write (&amp;pcap).
64  *
65 */
66 
67 /**
68  * @brief Close PCAP file
69  *
70  * @return rc - clib_error_t
71  *
72  */
75 {
76  close (pm->file_descriptor);
77  pm->flags &= ~PCAP_MAIN_INIT_DONE;
78  pm->file_descriptor = -1;
79  return 0;
80 }
81 
82 /**
83  * @brief Write PCAP file
84  *
85  * @return rc - clib_error_t
86  *
87  */
90 {
91  clib_error_t * error = 0;
92 
93  if (! (pm->flags & PCAP_MAIN_INIT_DONE))
94  {
96  int n;
97 
98  if (! pm->file_name)
99  pm->file_name = "/tmp/vnet.pcap";
100 
101  pm->file_descriptor = open (pm->file_name, O_CREAT | O_TRUNC | O_WRONLY, 0664);
102  if (pm->file_descriptor < 0)
103  {
104  error = clib_error_return_unix (0, "failed to open `%s'", pm->file_name);
105  goto done;
106  }
107 
108  pm->flags |= PCAP_MAIN_INIT_DONE;
109  pm->n_packets_captured = 0;
110  pm->n_pcap_data_written = 0;
111 
112  /* Write file header. */
113  memset (&fh, 0, sizeof (fh));
114  fh.magic = 0xa1b2c3d4;
115  fh.major_version = 2;
116  fh.minor_version = 4;
117  fh.time_zone = 0;
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))
122  {
123  if (n < 0)
124  error = clib_error_return_unix (0, "write file header `%s'", pm->file_name);
125  else
126  error = clib_error_return (0, "short write of file header `%s'", pm->file_name);
127  goto done;
128  }
129  }
130 
131  while (vec_len (pm->pcap_data) > pm->n_pcap_data_written)
132  {
133  int n = vec_len (pm->pcap_data) - pm->n_pcap_data_written;
134 
135  n = write (pm->file_descriptor,
137 
138  if (n < 0 && unix_error_is_fatal (errno))
139  {
140  error = clib_error_return_unix (0, "write `%s'", pm->file_name);
141  goto done;
142  }
143  pm->n_pcap_data_written += n;
144  }
145 
146  if (pm->n_pcap_data_written >= vec_len (pm->pcap_data))
147  {
149  pm->n_pcap_data_written = 0;
150  }
151 
153  pcap_close(pm);
154 
155  done:
156  if (error)
157  {
158  if (pm->file_descriptor >= 0)
159  close (pm->file_descriptor);
160  }
161  return error;
162 }
163 
164 /**
165  * @brief Read PCAP file
166  *
167  * @return rc - clib_error_t
168  *
169  */
171 {
172  clib_error_t * error = 0;
173  int fd, need_swap, n;
176 
177  fd = open (pm->file_name, O_RDONLY);
178  if (fd < 0)
179  {
180  error = clib_error_return_unix (0, "open `%s'", pm->file_name);
181  goto done;
182  }
183 
184  if (read (fd, &fh, sizeof (fh)) != sizeof (fh))
185  {
186  error = clib_error_return_unix (0, "read file header `%s'", pm->file_name);
187  goto done;
188  }
189 
190  need_swap = 0;
191  if (fh.magic == 0xd4c3b2a1)
192  {
193  need_swap = 1;
194 #define _(t,f) fh.f = clib_byte_swap_##t (fh.f);
196 #undef _
197  }
198 
199  if (fh.magic != 0xa1b2c3d4)
200  {
201  error = clib_error_return (0, "bad magic `%s'", pm->file_name);
202  goto done;
203  }
204 
205  pm->min_packet_bytes = 0;
206  pm->max_packet_bytes = 0;
207  while ((n = read (fd, &ph, sizeof (ph))) != 0)
208  {
209  u8 * data;
210 
211  if (need_swap)
212  {
213 #define _(t,f) ph.f = clib_byte_swap_##t (ph.f);
215 #undef _
216  }
217 
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)
220  {
221  error = clib_error_return (0, "short read `%s'", pm->file_name);
222  goto done;
223  }
224 
225  if (vec_len (pm->packets_read) == 0)
226  pm->min_packet_bytes = pm->max_packet_bytes = ph.n_bytes_in_packet;
227  else
228  {
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);
231  }
232 
233  vec_add1 (pm->packets_read, data);
234  }
235 
236  done:
237  if (fd >= 0)
238  close (fd);
239  return error;
240 
241 }
char * file_name
File name of pcap output.
Definition: pcap.h:124
#define clib_min(x, y)
Definition: clib.h:332
u32 flags
flags
Definition: pcap.h:136
int file_descriptor
File descriptor for reading/writing.
Definition: pcap.h:140
u32 n_packets_to_capture
Number of packets to capture.
Definition: pcap.h:127
PCAP utility definitions.
u32 max_packet_bytes
Definition: pcap.h:152
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
PCAP main state data structure.
Definition: pcap.h:122
#define vec_new(T, N)
Create new vector of given type and length (unspecified alignment, no header).
Definition: vec.h:310
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define clib_error_return(e, args...)
Definition: error.h:111
#define foreach_pcap_packet_header
Definition: pcap.h:98
u32 n_pcap_data_written
Bytes written.
Definition: pcap.h:143
u8 * pcap_data
Vector of pcap data.
Definition: pcap.h:146
#define clib_error_return_unix(e, args...)
Definition: error.h:114
clib_error_t * pcap_read(pcap_main_t *pm)
Read PCAP file.
Definition: pcap.c:170
clib_error_t * pcap_close(pcap_main_t *pm)
Close PCAP file.
Definition: pcap.c:74
clib_error_t * pcap_write(pcap_main_t *pm)
Write PCAP file.
Definition: pcap.c:89
Packet header.
Definition: pcap.h:110
#define PCAP_MAIN_INIT_DONE
Definition: pcap.h:137
static word unix_error_is_fatal(word error)
Definition: error.h:130
#define clib_max(x, y)
Definition: clib.h:325
u32 min_packet_bytes
Min/Max Packet bytes.
Definition: pcap.h:152
pcap_packet_type_t packet_type
Packet type.
Definition: pcap.h:130
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
File header struct.
Definition: pcap.h:92
u8 ** packets_read
Packets read from file.
Definition: pcap.h:149
u32 n_packets_captured
Number of packets currently captured.
Definition: pcap.h:133
#define foreach_pcap_file_header
Definition: pcap.h:70