1 /* $NetBSD: pcap-int.h,v 1.4 2017/01/24 22:29:28 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1994, 1995, 1996 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the Computer Systems 18 * Engineering Group at Lawrence Berkeley Laboratory. 19 * 4. Neither the name of the University nor of the Laboratory may be used 20 * to endorse or promote products derived from this software without 21 * specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef pcap_int_h 37 #define pcap_int_h 38 39 #include <pcap/pcap.h> 40 41 #ifdef __cplusplus 42 extern "C" { 43 #endif 44 45 #if defined(_WIN32) 46 /* 47 * Make sure Packet32.h doesn't define BPF structures that we've 48 * probably already defined as a result of including <pcap/pcap.h>. 49 */ 50 #define BPF_MAJOR_VERSION 51 #include <Packet32.h> 52 #elif defined(MSDOS) 53 #include <fcntl.h> 54 #include <io.h> 55 #endif 56 57 #if (defined(_MSC_VER) && (_MSC_VER <= 1200)) /* we are compiling with Visual Studio 6, that doesn't support the LL suffix*/ 58 59 /* 60 * Swap byte ordering of unsigned long long timestamp on a big endian 61 * machine. 62 */ 63 #define SWAPLL(ull) ((ull & 0xff00000000000000) >> 56) | \ 64 ((ull & 0x00ff000000000000) >> 40) | \ 65 ((ull & 0x0000ff0000000000) >> 24) | \ 66 ((ull & 0x000000ff00000000) >> 8) | \ 67 ((ull & 0x00000000ff000000) << 8) | \ 68 ((ull & 0x0000000000ff0000) << 24) | \ 69 ((ull & 0x000000000000ff00) << 40) | \ 70 ((ull & 0x00000000000000ff) << 56) 71 72 #else /* A recent Visual studio compiler or not VC */ 73 74 /* 75 * Swap byte ordering of unsigned long long timestamp on a big endian 76 * machine. 77 */ 78 #define SWAPLL(ull) ((ull & 0xff00000000000000LL) >> 56) | \ 79 ((ull & 0x00ff000000000000LL) >> 40) | \ 80 ((ull & 0x0000ff0000000000LL) >> 24) | \ 81 ((ull & 0x000000ff00000000LL) >> 8) | \ 82 ((ull & 0x00000000ff000000LL) << 8) | \ 83 ((ull & 0x0000000000ff0000LL) << 24) | \ 84 ((ull & 0x000000000000ff00LL) << 40) | \ 85 ((ull & 0x00000000000000ffLL) << 56) 86 87 #endif /* _MSC_VER */ 88 89 /* 90 * Maximum snapshot length. 91 * 92 * Somewhat arbitrary, but chosen to be: 93 * 94 * 1) big enough for maximum-size Linux loopback packets (65549) 95 * and some USB packets captured with USBPcap: 96 * 97 * http://desowin.org/usbpcap/ 98 * 99 * (> 131072, < 262144) 100 * 101 * and 102 * 103 * 2) small enough not to cause attempts to allocate huge amounts of 104 * memory; some applications might use the snapshot length in a 105 * savefile header to control the size of the buffer they allocate, 106 * so a size of, say, 2^31-1 might not work well. 107 * 108 * We don't enforce this in pcap_set_snaplen(), but we use it internally. 109 */ 110 #define MAXIMUM_SNAPLEN 262144 111 112 struct pcap_opt { 113 char *device; 114 int timeout; /* timeout for buffering */ 115 u_int buffer_size; 116 int promisc; 117 int rfmon; /* monitor mode */ 118 int immediate; /* immediate mode - deliver packets as soon as they arrive */ 119 int tstamp_type; 120 int tstamp_precision; 121 }; 122 123 typedef int (*activate_op_t)(pcap_t *); 124 typedef int (*can_set_rfmon_op_t)(pcap_t *); 125 typedef int (*read_op_t)(pcap_t *, int cnt, pcap_handler, u_char *); 126 typedef int (*inject_op_t)(pcap_t *, const void *, size_t); 127 typedef int (*setfilter_op_t)(pcap_t *, struct bpf_program *); 128 typedef int (*setdirection_op_t)(pcap_t *, pcap_direction_t); 129 typedef int (*set_datalink_op_t)(pcap_t *, int); 130 typedef int (*getnonblock_op_t)(pcap_t *, char *); 131 typedef int (*setnonblock_op_t)(pcap_t *, int, char *); 132 typedef int (*stats_op_t)(pcap_t *, struct pcap_stat *); 133 #ifdef _WIN32 134 typedef struct pcap_stat *(*stats_ex_op_t)(pcap_t *, int *); 135 typedef int (*setbuff_op_t)(pcap_t *, int); 136 typedef int (*setmode_op_t)(pcap_t *, int); 137 typedef int (*setmintocopy_op_t)(pcap_t *, int); 138 typedef HANDLE (*getevent_op_t)(pcap_t *); 139 typedef int (*oid_get_request_op_t)(pcap_t *, bpf_u_int32, void *, size_t *); 140 typedef int (*oid_set_request_op_t)(pcap_t *, bpf_u_int32, const void *, size_t *); 141 typedef u_int (*sendqueue_transmit_op_t)(pcap_t *, pcap_send_queue *, int); 142 typedef int (*setuserbuffer_op_t)(pcap_t *, int); 143 typedef int (*live_dump_op_t)(pcap_t *, char *, int, int); 144 typedef int (*live_dump_ended_op_t)(pcap_t *, int); 145 typedef PAirpcapHandle (*get_airpcap_handle_op_t)(pcap_t *); 146 #endif 147 typedef void (*cleanup_op_t)(pcap_t *); 148 149 /* 150 * We put all the stuff used in the read code path at the beginning, 151 * to try to keep it together in the same cache line or lines. 152 */ 153 struct pcap { 154 /* 155 * Method to call to read packets on a live capture. 156 */ 157 read_op_t read_op; 158 159 /* 160 * Method to call to read packets from a savefile. 161 */ 162 int (*next_packet_op)(pcap_t *, struct pcap_pkthdr *, u_char **); 163 164 #ifdef _WIN32 165 ADAPTER *adapter; 166 #else 167 int fd; 168 int selectable_fd; 169 #endif /* _WIN32 */ 170 171 /* 172 * Read buffer. 173 */ 174 u_int bufsize; 175 void *buffer; 176 u_char *bp; 177 int cc; 178 179 int break_loop; /* flag set to force break from packet-reading loop */ 180 181 void *priv; /* private data for methods */ 182 183 int swapped; 184 FILE *rfile; /* null if live capture, non-null if savefile */ 185 u_int fddipad; 186 struct pcap *next; /* list of open pcaps that need stuff cleared on close */ 187 188 /* 189 * File version number; meaningful only for a savefile, but we 190 * keep it here so that apps that (mistakenly) ask for the 191 * version numbers will get the same zero values that they 192 * always did. 193 */ 194 int version_major; 195 int version_minor; 196 197 int snapshot; 198 int linktype; /* Network linktype */ 199 int linktype_ext; /* Extended information stored in the linktype field of a file */ 200 int tzoff; /* timezone offset */ 201 int offset; /* offset for proper alignment */ 202 int activated; /* true if the capture is really started */ 203 int oldstyle; /* if we're opening with pcap_open_live() */ 204 205 struct pcap_opt opt; 206 207 /* 208 * Place holder for pcap_next(). 209 */ 210 u_char *pkt; 211 212 #ifdef _WIN32 213 struct pcap_stat stat; /* used for pcap_stats_ex() */ 214 #endif 215 216 /* We're accepting only packets in this direction/these directions. */ 217 pcap_direction_t direction; 218 219 /* 220 * Flags to affect BPF code generation. 221 */ 222 int bpf_codegen_flags; 223 224 /* 225 * Placeholder for filter code if bpf not in kernel. 226 */ 227 struct bpf_program fcode; 228 229 char errbuf[PCAP_ERRBUF_SIZE + 1]; 230 int dlt_count; 231 u_int *dlt_list; 232 int tstamp_type_count; 233 u_int *tstamp_type_list; 234 int tstamp_precision_count; 235 u_int *tstamp_precision_list; 236 237 struct pcap_pkthdr pcap_header; /* This is needed for the pcap_next_ex() to work */ 238 239 /* 240 * More methods. 241 */ 242 activate_op_t activate_op; 243 can_set_rfmon_op_t can_set_rfmon_op; 244 inject_op_t inject_op; 245 setfilter_op_t setfilter_op; 246 setdirection_op_t setdirection_op; 247 set_datalink_op_t set_datalink_op; 248 getnonblock_op_t getnonblock_op; 249 setnonblock_op_t setnonblock_op; 250 stats_op_t stats_op; 251 252 /* 253 * Routine to use as callback for pcap_next()/pcap_next_ex(). 254 */ 255 pcap_handler oneshot_callback; 256 257 #ifdef _WIN32 258 /* 259 * These are, at least currently, specific to the Win32 NPF 260 * driver. 261 */ 262 stats_ex_op_t stats_ex_op; 263 setbuff_op_t setbuff_op; 264 setmode_op_t setmode_op; 265 setmintocopy_op_t setmintocopy_op; 266 getevent_op_t getevent_op; 267 oid_get_request_op_t oid_get_request_op; 268 oid_set_request_op_t oid_set_request_op; 269 sendqueue_transmit_op_t sendqueue_transmit_op; 270 setuserbuffer_op_t setuserbuffer_op; 271 live_dump_op_t live_dump_op; 272 live_dump_ended_op_t live_dump_ended_op; 273 get_airpcap_handle_op_t get_airpcap_handle_op; 274 #endif 275 cleanup_op_t cleanup_op; 276 }; 277 278 /* 279 * BPF code generation flags. 280 */ 281 #define BPF_SPECIAL_VLAN_HANDLING 0x00000001 /* special VLAN handling for Linux */ 282 283 /* 284 * This is a timeval as stored in a savefile. 285 * It has to use the same types everywhere, independent of the actual 286 * `struct timeval'; `struct timeval' has 32-bit tv_sec values on some 287 * platforms and 64-bit tv_sec values on other platforms, and writing 288 * out native `struct timeval' values would mean files could only be 289 * read on systems with the same tv_sec size as the system on which 290 * the file was written. 291 */ 292 293 struct pcap_timeval { 294 bpf_int32 tv_sec; /* seconds */ 295 bpf_int32 tv_usec; /* microseconds */ 296 }; 297 298 /* 299 * This is a `pcap_pkthdr' as actually stored in a savefile. 300 * 301 * Do not change the format of this structure, in any way (this includes 302 * changes that only affect the length of fields in this structure), 303 * and do not make the time stamp anything other than seconds and 304 * microseconds (e.g., seconds and nanoseconds). Instead: 305 * 306 * introduce a new structure for the new format; 307 * 308 * send mail to "tcpdump-workers@lists.tcpdump.org", requesting 309 * a new magic number for your new capture file format, and, when 310 * you get the new magic number, put it in "savefile.c"; 311 * 312 * use that magic number for save files with the changed record 313 * header; 314 * 315 * make the code in "savefile.c" capable of reading files with 316 * the old record header as well as files with the new record header 317 * (using the magic number to determine the header format). 318 * 319 * Then supply the changes by forking the branch at 320 * 321 * https://github.com/the-tcpdump-group/libpcap/issues 322 * 323 * and issuing a pull request, so that future versions of libpcap and 324 * programs that use it (such as tcpdump) will be able to read your new 325 * capture file format. 326 */ 327 328 struct pcap_sf_pkthdr { 329 struct pcap_timeval ts; /* time stamp */ 330 bpf_u_int32 caplen; /* length of portion present */ 331 bpf_u_int32 len; /* length this packet (off wire) */ 332 }; 333 334 /* 335 * How a `pcap_pkthdr' is actually stored in savefiles written 336 * by some patched versions of libpcap (e.g. the ones in Red 337 * Hat Linux 6.1 and 6.2). 338 * 339 * Do not change the format of this structure, in any way (this includes 340 * changes that only affect the length of fields in this structure). 341 * Instead, introduce a new structure, as per the above. 342 */ 343 344 struct pcap_sf_patched_pkthdr { 345 struct pcap_timeval ts; /* time stamp */ 346 bpf_u_int32 caplen; /* length of portion present */ 347 bpf_u_int32 len; /* length this packet (off wire) */ 348 int index; 349 unsigned short protocol; 350 unsigned char pkt_type; 351 }; 352 353 /* 354 * User data structure for the one-shot callback used for pcap_next() 355 * and pcap_next_ex(). 356 */ 357 struct oneshot_userdata { 358 struct pcap_pkthdr *hdr; 359 const u_char **pkt; 360 pcap_t *pd; 361 }; 362 363 #ifndef min 364 #define min(a, b) ((a) > (b) ? (b) : (a)) 365 #endif 366 367 int pcap_offline_read(pcap_t *, int, pcap_handler, u_char *); 368 369 #include <stdarg.h> 370 371 #include "portability.h" 372 373 /* 374 * Does the packet count argument to a module's read routine say 375 * "supply packets until you run out of packets"? 376 */ 377 #define PACKET_COUNT_IS_UNLIMITED(count) ((count) <= 0) 378 379 /* 380 * Routines that most pcap implementations can use for non-blocking mode. 381 */ 382 #if !defined(_WIN32) && !defined(MSDOS) 383 int pcap_getnonblock_fd(pcap_t *, char *); 384 int pcap_setnonblock_fd(pcap_t *p, int, char *); 385 #endif 386 387 /* 388 * Internal interfaces for "pcap_create()". 389 * 390 * "pcap_create_interface()" is the routine to do a pcap_create on 391 * a regular network interface. There are multiple implementations 392 * of this, one for each platform type (Linux, BPF, DLPI, etc.), 393 * with the one used chosen by the configure script. 394 * 395 * "pcap_create_common()" allocates and fills in a pcap_t, for use 396 * by pcap_create routines. 397 */ 398 pcap_t *pcap_create_interface(const char *, char *); 399 pcap_t *pcap_create_common(char *, size_t); 400 int pcap_do_addexit(pcap_t *); 401 void pcap_add_to_pcaps_to_close(pcap_t *); 402 void pcap_remove_from_pcaps_to_close(pcap_t *); 403 void pcap_cleanup_live_common(pcap_t *); 404 int pcap_check_activated(pcap_t *); 405 406 /* 407 * Internal interfaces for "pcap_findalldevs()". 408 * 409 * "pcap_platform_finddevs()" is a platform-dependent routine to 410 * find local network interfaces. 411 * 412 * "pcap_findalldevs_interfaces()" is a helper to find those interfaces 413 * using the "standard" mechanisms (SIOCGIFCONF, "getifaddrs()", etc.). 414 * 415 * "pcap_add_if()" adds an interface to the list of interfaces, for 416 * use by various "find interfaces" routines. 417 */ 418 int pcap_platform_finddevs(pcap_if_t **, char *); 419 #if !defined(_WIN32) && !defined(MSDOS) 420 int pcap_findalldevs_interfaces(pcap_if_t **, char *, 421 int (*)(const char *)); 422 #endif 423 int add_addr_to_iflist(pcap_if_t **, const char *, bpf_u_int32, 424 struct sockaddr *, size_t, struct sockaddr *, size_t, 425 struct sockaddr *, size_t, struct sockaddr *, size_t, char *); 426 int add_addr_to_dev(pcap_if_t *, struct sockaddr *, size_t, 427 struct sockaddr *, size_t, struct sockaddr *, size_t, 428 struct sockaddr *dstaddr, size_t, char *errbuf); 429 int pcap_add_if(pcap_if_t **, const char *, bpf_u_int32, const char *, 430 char *); 431 int add_or_find_if(pcap_if_t **, pcap_if_t **, const char *, bpf_u_int32, 432 const char *, char *); 433 #ifndef _WIN32 434 bpf_u_int32 if_flags_to_pcap_flags(const char *, u_int); 435 #endif 436 437 /* 438 * Internal interfaces for "pcap_open_offline()". 439 * 440 * "pcap_open_offline_common()" allocates and fills in a pcap_t, for use 441 * by pcap_open_offline routines. 442 * 443 * "sf_cleanup()" closes the file handle associated with a pcap_t, if 444 * appropriate, and frees all data common to all modules for handling 445 * savefile types. 446 */ 447 pcap_t *pcap_open_offline_common(char *ebuf, size_t size); 448 void sf_cleanup(pcap_t *p); 449 450 /* 451 * Internal interfaces for both "pcap_create()" and routines that 452 * open savefiles. 453 * 454 * "pcap_oneshot()" is the standard one-shot callback for "pcap_next()" 455 * and "pcap_next_ex()". 456 */ 457 void pcap_oneshot(u_char *, const struct pcap_pkthdr *, const u_char *); 458 459 #ifdef _WIN32 460 void pcap_win32_err_to_str(DWORD, char *); 461 #endif 462 463 int install_bpf_program(pcap_t *, struct bpf_program *); 464 465 int pcap_strcasecmp(const char *, const char *); 466 467 #ifdef __cplusplus 468 } 469 #endif 470 471 #endif 472