Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 43 | /** |
| 44 | * @file |
| 45 | * @brief PCAP function. |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 46 | * |
Chris Luke | d4024f5 | 2016-09-06 09:32:36 -0400 | [diff] [blame] | 47 | * Usage: |
| 48 | * |
| 49 | * <code><pre> |
| 50 | * \#include <vnet/unix/pcap.h> |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 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 | * }; |
Chris Luke | d4024f5 | 2016-09-06 09:32:36 -0400 | [diff] [blame] | 57 | * </pre></code> |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 58 | * |
| 59 | * To add a buffer: |
| 60 | * |
Chris Luke | d4024f5 | 2016-09-06 09:32:36 -0400 | [diff] [blame] | 61 | * <code><pre>pcap_add_buffer (&pcap, vm, pi0, 128);</pre></code> |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 62 | * |
Chris Luke | d4024f5 | 2016-09-06 09:32:36 -0400 | [diff] [blame] | 63 | * File will be written after @c n_packets_to_capture or call to pcap_write (&pcap). |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 64 | * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | */ |
| 66 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 67 | /** |
| 68 | * @brief Close PCAP file |
| 69 | * |
| 70 | * @return rc - clib_error_t |
| 71 | * |
| 72 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | clib_error_t * |
Damjan Marion | 92217f3 | 2016-07-12 21:58:19 +0200 | [diff] [blame] | 74 | pcap_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) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 82 | /** |
| 83 | * @brief Write PCAP file |
| 84 | * |
| 85 | * @return rc - clib_error_t |
| 86 | * |
| 87 | */ |
Damjan Marion | 92217f3 | 2016-07-12 21:58:19 +0200 | [diff] [blame] | 88 | clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | pcap_write (pcap_main_t * pm) |
| 90 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 91 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 92 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 93 | if (!(pm->flags & PCAP_MAIN_INIT_DONE)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | { |
| 95 | pcap_file_header_t fh; |
| 96 | int n; |
| 97 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 98 | if (!pm->file_name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | pm->file_name = "/tmp/vnet.pcap"; |
| 100 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 101 | pm->file_descriptor = |
| 102 | open (pm->file_name, O_CREAT | O_TRUNC | O_WRONLY, 0664); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 103 | if (pm->file_descriptor < 0) |
| 104 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 105 | error = |
| 106 | clib_error_return_unix (0, "failed to open `%s'", pm->file_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 107 | 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 Barach | f91080c | 2018-07-26 12:27:27 -0400 | [diff] [blame] | 113 | clib_spinlock_init (&pm->lock); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 114 | |
| 115 | /* Write file header. */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 116 | clib_memset (&fh, 0, sizeof (fh)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | 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 reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 127 | error = |
| 128 | clib_error_return_unix (0, "write file header `%s'", |
| 129 | pm->file_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | else |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 131 | error = |
| 132 | clib_error_return (0, "short write of file header `%s'", |
| 133 | pm->file_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | goto done; |
| 135 | } |
| 136 | } |
| 137 | |
Damjan Marion | 92217f3 | 2016-07-12 21:58:19 +0200 | [diff] [blame] | 138 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | |
Damjan Marion | 92217f3 | 2016-07-12 21:58:19 +0200 | [diff] [blame] | 142 | n = write (pm->file_descriptor, |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 143 | vec_elt_at_index (pm->pcap_data, pm->n_pcap_data_written), |
| 144 | n); |
Damjan Marion | 92217f3 | 2016-07-12 21:58:19 +0200 | [diff] [blame] | 145 | |
| 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 reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 151 | pm->n_pcap_data_written += n; |
| 152 | } |
Damjan Marion | 92217f3 | 2016-07-12 21:58:19 +0200 | [diff] [blame] | 153 | |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | |
| 160 | if (pm->n_packets_captured >= pm->n_packets_to_capture) |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 161 | pcap_close (pm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 162 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 163 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 164 | if (error) |
| 165 | { |
| 166 | if (pm->file_descriptor >= 0) |
| 167 | close (pm->file_descriptor); |
| 168 | } |
| 169 | return error; |
| 170 | } |
| 171 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 172 | /** |
| 173 | * @brief Read PCAP file |
| 174 | * |
| 175 | * @return rc - clib_error_t |
| 176 | * |
| 177 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 178 | clib_error_t * |
| 179 | pcap_read (pcap_main_t * pm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 180 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 181 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 182 | int fd, need_swap, n; |
| 183 | pcap_file_header_t fh; |
| 184 | pcap_packet_header_t ph; |
| 185 | |
| 186 | fd = open (pm->file_name, O_RDONLY); |
| 187 | if (fd < 0) |
| 188 | { |
| 189 | error = clib_error_return_unix (0, "open `%s'", pm->file_name); |
| 190 | goto done; |
| 191 | } |
| 192 | |
| 193 | if (read (fd, &fh, sizeof (fh)) != sizeof (fh)) |
| 194 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 195 | error = |
| 196 | clib_error_return_unix (0, "read file header `%s'", pm->file_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 197 | goto done; |
| 198 | } |
| 199 | |
| 200 | need_swap = 0; |
| 201 | if (fh.magic == 0xd4c3b2a1) |
| 202 | { |
| 203 | need_swap = 1; |
| 204 | #define _(t,f) fh.f = clib_byte_swap_##t (fh.f); |
| 205 | foreach_pcap_file_header; |
| 206 | #undef _ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 207 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 208 | |
| 209 | if (fh.magic != 0xa1b2c3d4) |
| 210 | { |
| 211 | error = clib_error_return (0, "bad magic `%s'", pm->file_name); |
| 212 | goto done; |
| 213 | } |
| 214 | |
| 215 | pm->min_packet_bytes = 0; |
| 216 | pm->max_packet_bytes = 0; |
| 217 | while ((n = read (fd, &ph, sizeof (ph))) != 0) |
| 218 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 219 | u8 *data; |
Dave Barach | 78c5689 | 2018-05-16 11:34:35 -0400 | [diff] [blame] | 220 | u64 timestamp; |
| 221 | u32 timestamp_sec; |
| 222 | u32 timestamp_usec; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 223 | |
| 224 | if (need_swap) |
| 225 | { |
| 226 | #define _(t,f) ph.f = clib_byte_swap_##t (ph.f); |
| 227 | foreach_pcap_packet_header; |
| 228 | #undef _ |
| 229 | } |
| 230 | |
| 231 | data = vec_new (u8, ph.n_bytes_in_packet); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 232 | if (read (fd, data, ph.n_packet_bytes_stored_in_file) != |
| 233 | ph.n_packet_bytes_stored_in_file) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 234 | { |
| 235 | error = clib_error_return (0, "short read `%s'", pm->file_name); |
| 236 | goto done; |
| 237 | } |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 238 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 239 | if (vec_len (pm->packets_read) == 0) |
| 240 | pm->min_packet_bytes = pm->max_packet_bytes = ph.n_bytes_in_packet; |
| 241 | else |
| 242 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 243 | pm->min_packet_bytes = |
| 244 | clib_min (pm->min_packet_bytes, ph.n_bytes_in_packet); |
| 245 | pm->max_packet_bytes = |
| 246 | clib_max (pm->max_packet_bytes, ph.n_bytes_in_packet); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 247 | } |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 248 | |
Dave Barach | 78c5689 | 2018-05-16 11:34:35 -0400 | [diff] [blame] | 249 | timestamp_sec = ph.time_in_sec; |
| 250 | timestamp_usec = ph.time_in_usec; |
| 251 | timestamp = ((u64) timestamp_sec) * 1000000 + (u64) timestamp_usec; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 252 | vec_add1 (pm->packets_read, data); |
Dave Barach | 78c5689 | 2018-05-16 11:34:35 -0400 | [diff] [blame] | 253 | vec_add1 (pm->timestamps, timestamp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 254 | } |
| 255 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 256 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 257 | if (fd >= 0) |
| 258 | close (fd); |
| 259 | return error; |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 260 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 261 | } |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 262 | |
| 263 | /* |
| 264 | * fd.io coding-style-patch-verification: ON |
| 265 | * |
| 266 | * Local Variables: |
| 267 | * eval: (c-set-style "gnu") |
| 268 | * End: |
| 269 | */ |