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.h: 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 | */ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 39 | /** |
| 40 | * @file |
| 41 | * @brief PCAP utility definitions |
| 42 | */ |
Dave Barach | 3ae2873 | 2018-11-16 17:19:00 -0500 | [diff] [blame] | 43 | #ifndef included_vppinfra_pcap_h |
| 44 | #define included_vppinfra_pcap_h |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | |
Dave Barach | 3ae2873 | 2018-11-16 17:19:00 -0500 | [diff] [blame] | 46 | #include <vppinfra/types.h> |
| 47 | #include <vppinfra/cache.h> |
| 48 | #include <vppinfra/mem.h> |
| 49 | #include <vppinfra/lock.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 51 | /** |
| 52 | * @brief Packet types supported by PCAP |
| 53 | * |
Dave Barach | 3ae2873 | 2018-11-16 17:19:00 -0500 | [diff] [blame] | 54 | * These codes end up in the pcap file header. |
| 55 | * If you decide to build a wireshark dissector, |
| 56 | * you'll need to know that these codes are mapped |
| 57 | * through the pcap_to_wtap_map[] array in .../wiretap/pcap-common.c. |
| 58 | * |
| 59 | * For example: |
| 60 | * |
| 61 | * { 160, WTAP_ENCAP_USER13 }, |
| 62 | * |
| 63 | * A file with (vpp) packet type PCAP_PACKET_TYPE_user13 |
| 64 | * aka 160, will need a top-level dissector registered to |
| 65 | * deal with WTAP_ENCAP_USER13 [=58]. |
| 66 | * |
| 67 | * Something like so: |
| 68 | * |
| 69 | * dissector_add_uint("wtap_encap", WTAP_ENCAP_USER13, vpp_dissector_handle); |
| 70 | * |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 71 | * null 0 |
| 72 | * ethernet 1 |
| 73 | * ppp 9 |
| 74 | * ip 12 |
| 75 | * hdlc 104 |
Dave Barach | 3ae2873 | 2018-11-16 17:19:00 -0500 | [diff] [blame] | 76 | * user0 147 ... user15 162 |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 77 | */ |
| 78 | #define foreach_vnet_pcap_packet_type \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 79 | _ (null, 0) \ |
| 80 | _ (ethernet, 1) \ |
| 81 | _ (ppp, 9) \ |
| 82 | _ (ip, 12) \ |
Dave Barach | 3ae2873 | 2018-11-16 17:19:00 -0500 | [diff] [blame] | 83 | _ (hdlc, 104) \ |
| 84 | _ (user0, 147) \ |
| 85 | _ (user1, 148) \ |
| 86 | _ (user2, 149) \ |
| 87 | _ (user3, 150) \ |
| 88 | _ (user4, 151) \ |
| 89 | _ (user5, 152) \ |
| 90 | _ (user6, 153) \ |
| 91 | _ (user7, 154) \ |
| 92 | _ (user8, 155) \ |
| 93 | _ (user9, 156) \ |
| 94 | _ (user10, 157) \ |
| 95 | _ (user11, 158) \ |
| 96 | _ (user12, 159) \ |
| 97 | _ (user13, 160) \ |
| 98 | _ (user14, 161) \ |
| 99 | _ (user15, 162) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 100 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 101 | typedef enum |
| 102 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 103 | #define _(f,n) PCAP_PACKET_TYPE_##f = (n), |
| 104 | foreach_vnet_pcap_packet_type |
| 105 | #undef _ |
| 106 | } pcap_packet_type_t; |
| 107 | |
| 108 | #define foreach_pcap_file_header \ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 109 | /** 0xa1b2c3d4 host byte order. \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | 0xd4c3b2a1 => need to byte swap everything. */ \ |
| 111 | _ (u32, magic) \ |
| 112 | \ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 113 | /** Currently major 2 minor 4. */ \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 114 | _ (u16, major_version) \ |
| 115 | _ (u16, minor_version) \ |
| 116 | \ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 117 | /** 0 for GMT. */ \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | _ (u32, time_zone) \ |
| 119 | \ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 120 | /** Accuracy of timestamps. Typically set to 0. */ \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | _ (u32, sigfigs) \ |
| 122 | \ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 123 | /** Size of largest packet in file. */ \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 124 | _ (u32, max_packet_size_in_bytes) \ |
| 125 | \ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 126 | /** One of vnet_pcap_packet_type_t. */ \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | _ (u32, packet_type) |
| 128 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 129 | /** File header struct */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 130 | typedef struct |
| 131 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | #define _(t, f) t f; |
| 133 | foreach_pcap_file_header |
| 134 | #undef _ |
| 135 | } pcap_file_header_t; |
| 136 | |
| 137 | #define foreach_pcap_packet_header \ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 138 | /** Time stamp in seconds */ \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | _ (u32, time_in_sec) \ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 140 | /** Time stamp in microseconds. */ \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | _ (u32, time_in_usec) \ |
| 142 | \ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 143 | /** Number of bytes stored in file. */ \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | _ (u32, n_packet_bytes_stored_in_file) \ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 145 | /** Number of bytes in actual packet. */ \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 146 | _ (u32, n_bytes_in_packet) |
| 147 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 148 | /** Packet header. */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 149 | typedef struct |
| 150 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 151 | #define _(t, f) t f; |
| 152 | foreach_pcap_packet_header |
| 153 | #undef _ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 154 | /** Packet data follows. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 155 | u8 data[0]; |
| 156 | } pcap_packet_header_t; |
| 157 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 158 | /** |
| 159 | * @brief PCAP main state data structure |
| 160 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 161 | typedef struct |
| 162 | { |
Dave Barach | f91080c | 2018-07-26 12:27:27 -0400 | [diff] [blame] | 163 | /** spinlock to protect e.g. pcap_data */ |
| 164 | clib_spinlock_t lock; |
| 165 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 166 | /** File name of pcap output. */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 167 | char *file_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 169 | /** Number of packets to capture. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | u32 n_packets_to_capture; |
| 171 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 172 | /** Packet type */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 173 | pcap_packet_type_t packet_type; |
| 174 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 175 | /** Number of packets currently captured. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 176 | u32 n_packets_captured; |
| 177 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 178 | /** flags */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 179 | u32 flags; |
| 180 | #define PCAP_MAIN_INIT_DONE (1 << 0) |
| 181 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 182 | /** File descriptor for reading/writing. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 183 | int file_descriptor; |
| 184 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 185 | /** Bytes written */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 186 | u32 n_pcap_data_written; |
| 187 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 188 | /** Vector of pcap data. */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 189 | u8 *pcap_data; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 190 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 191 | /** Packets read from file. */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 192 | u8 **packets_read; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 193 | |
Dave Barach | 78c5689 | 2018-05-16 11:34:35 -0400 | [diff] [blame] | 194 | /** Timestamps */ |
| 195 | u64 *timestamps; |
| 196 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 197 | /** Min/Max Packet bytes */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 198 | u32 min_packet_bytes, max_packet_bytes; |
| 199 | } pcap_main_t; |
| 200 | |
Dave Barach | 3ae2873 | 2018-11-16 17:19:00 -0500 | [diff] [blame] | 201 | #endif /* included_vppinfra_pcap_h */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 202 | |
| 203 | /* |
| 204 | * fd.io coding-style-patch-verification: ON |
| 205 | * |
| 206 | * Local Variables: |
| 207 | * eval: (c-set-style "gnu") |
| 208 | * End: |
| 209 | */ |