xref: /netbsd-src/external/bsd/libpcap/dist/pcap-int.h (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: pcap-int.h,v 1.1.1.3 2013/04/06 15:57:45 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  * @(#) Header: /tcpdump/master/libpcap/pcap-int.h,v 1.94 2008-09-16 00:20:23 guy Exp  (LBL)
36  */
37 
38 #ifndef pcap_int_h
39 #define	pcap_int_h
40 
41 #include <pcap/pcap.h>
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 #ifdef HAVE_LIBDLPI
48 #include <libdlpi.h>
49 #endif
50 
51 #ifdef WIN32
52 #include <Packet32.h>
53 extern CRITICAL_SECTION g_PcapCompileCriticalSection;
54 #endif /* WIN32 */
55 
56 #ifdef MSDOS
57 #include <fcntl.h>
58 #include <io.h>
59 #endif
60 
61 #ifdef HAVE_SNF_API
62 #include <snf.h>
63 #endif
64 
65 #if (defined(_MSC_VER) && (_MSC_VER <= 1200)) /* we are compiling with Visual Studio 6, that doesn't support the LL suffix*/
66 
67 /*
68  * Swap byte ordering of unsigned long long timestamp on a big endian
69  * machine.
70  */
71 #define SWAPLL(ull)  ((ull & 0xff00000000000000) >> 56) | \
72                       ((ull & 0x00ff000000000000) >> 40) | \
73                       ((ull & 0x0000ff0000000000) >> 24) | \
74                       ((ull & 0x000000ff00000000) >> 8)  | \
75                       ((ull & 0x00000000ff000000) << 8)  | \
76                       ((ull & 0x0000000000ff0000) << 24) | \
77                       ((ull & 0x000000000000ff00) << 40) | \
78                       ((ull & 0x00000000000000ff) << 56)
79 
80 #else /* A recent Visual studio compiler or not VC */
81 
82 /*
83  * Swap byte ordering of unsigned long long timestamp on a big endian
84  * machine.
85  */
86 #define SWAPLL(ull)  ((ull & 0xff00000000000000LL) >> 56) | \
87                       ((ull & 0x00ff000000000000LL) >> 40) | \
88                       ((ull & 0x0000ff0000000000LL) >> 24) | \
89                       ((ull & 0x000000ff00000000LL) >> 8)  | \
90                       ((ull & 0x00000000ff000000LL) << 8)  | \
91                       ((ull & 0x0000000000ff0000LL) << 24) | \
92                       ((ull & 0x000000000000ff00LL) << 40) | \
93                       ((ull & 0x00000000000000ffLL) << 56)
94 
95 #endif /* _MSC_VER */
96 
97 /*
98  * Savefile
99  */
100 typedef enum {
101 	NOT_SWAPPED,
102 	SWAPPED,
103 	MAYBE_SWAPPED
104 } swapped_type_t;
105 
106 /*
107  * Used when reading a savefile.
108  */
109 struct pcap_sf {
110 	FILE *rfile;
111 	int (*next_packet_op)(pcap_t *, struct pcap_pkthdr *, u_char **);
112 	int swapped;
113 	size_t hdrsize;
114 	swapped_type_t lengths_swapped;
115 	int version_major;
116 	int version_minor;
117 	bpf_u_int32 ifcount;	/* number of interfaces seen in this capture */
118 	u_int tsresol;		/* time stamp resolution */
119 	u_int tsscale;		/* scaling factor for resolution -> microseconds */
120 	u_int64_t tsoffset;	/* time stamp offset */
121 };
122 
123 /*
124  * Used when doing a live capture.
125  */
126 struct pcap_md {
127 	struct pcap_stat stat;
128 	/*XXX*/
129 	int use_bpf;		/* using kernel filter */
130 	u_long	TotPkts;	/* can't oflow for 79 hrs on ether */
131 	u_long	TotAccepted;	/* count accepted by filter */
132 	u_long	TotDrops;	/* count of dropped packets */
133 	long	TotMissed;	/* missed by i/f during this run */
134 	long	OrigMissed;	/* missed by i/f before this run */
135 	char	*device;	/* device name */
136 	int	timeout;	/* timeout for buffering */
137 	int	must_do_on_close; /* stuff we must do when we close */
138 	struct pcap *next;	/* list of open pcaps that need stuff cleared on close */
139 #ifdef linux
140 	int	sock_packet;	/* using Linux 2.0 compatible interface */
141 	int	cooked;		/* using SOCK_DGRAM rather than SOCK_RAW */
142 	int	ifindex;	/* interface index of device we're bound to */
143 	int	lo_ifindex;	/* interface index of the loopback device */
144 	u_int	packets_read;	/* count of packets read with recvfrom() */
145 	bpf_u_int32 oldmode;	/* mode to restore when turning monitor mode off */
146 	char	*mondevice;	/* mac80211 monitor device we created */
147 	u_char	*mmapbuf;	/* memory-mapped region pointer */
148 	size_t	mmapbuflen;	/* size of region */
149 	u_int	tp_version;	/* version of tpacket_hdr for mmaped ring */
150 	u_int	tp_hdrlen;	/* hdrlen of tpacket_hdr for mmaped ring */
151 	u_char	*oneshot_buffer; /* buffer for copy of packet */
152 	long	proc_dropped; /* packets reported dropped by /proc/net/dev */
153 #endif /* linux */
154 
155 #ifdef HAVE_DAG_API
156 #ifdef HAVE_DAG_STREAMS_API
157 	u_char	*dag_mem_bottom;	/* DAG card current memory bottom pointer */
158 	u_char	*dag_mem_top;	/* DAG card current memory top pointer */
159 #else /* HAVE_DAG_STREAMS_API */
160 	void	*dag_mem_base;	/* DAG card memory base address */
161 	u_int	dag_mem_bottom;	/* DAG card current memory bottom offset */
162 	u_int	dag_mem_top;	/* DAG card current memory top offset */
163 #endif /* HAVE_DAG_STREAMS_API */
164 	int	dag_fcs_bits;	/* Number of checksum bits from link layer */
165 	int	dag_offset_flags; /* Flags to pass to dag_offset(). */
166 	int	dag_stream;	/* DAG stream number */
167 	int	dag_timeout;	/* timeout specified to pcap_open_live.
168 				 * Same as in linux above, introduce
169 				 * generally? */
170 #endif /* HAVE_DAG_API */
171 #ifdef HAVE_SNF_API
172 	snf_handle_t snf_handle; /* opaque device handle */
173 	snf_ring_t   snf_ring;   /* opaque device ring handle */
174         int          snf_timeout;
175         int          snf_boardnum;
176 #endif /*HAVE_SNF_API*/
177 
178 #ifdef HAVE_ZEROCOPY_BPF
179        /*
180         * Zero-copy read buffer -- for zero-copy BPF.  'buffer' above will
181         * alternative between these two actual mmap'd buffers as required.
182         * As there is a header on the front size of the mmap'd buffer, only
183         * some of the buffer is exposed to libpcap as a whole via bufsize;
184         * zbufsize is the true size.  zbuffer tracks the current zbuf
185         * assocated with buffer so that it can be used to decide which the
186         * next buffer to read will be.
187         */
188        u_char *zbuf1, *zbuf2, *zbuffer;
189        u_int zbufsize;
190        u_int zerocopy;
191        u_int interrupted;
192        struct timespec firstsel;
193        /*
194         * If there's currently a buffer being actively processed, then it is
195         * referenced here; 'buffer' is also pointed at it, but offset by the
196         * size of the header.
197         */
198        struct bpf_zbuf_header *bzh;
199 #endif /* HAVE_ZEROCOPY_BPF */
200 };
201 
202 /*
203  * Stuff to do when we close.
204  */
205 #define MUST_CLEAR_PROMISC	0x00000001	/* clear promiscuous mode */
206 #define MUST_CLEAR_RFMON	0x00000002	/* clear rfmon (monitor) mode */
207 #define MUST_DELETE_MONIF	0x00000004	/* delete monitor-mode interface */
208 
209 struct pcap_opt {
210 	int	buffer_size;
211 	char	*source;
212 	int	promisc;
213 	int	rfmon;
214 	int	tstamp_type;
215 };
216 
217 /*
218  * Ultrix, DEC OSF/1^H^H^H^H^H^H^H^H^HDigital UNIX^H^H^H^H^H^H^H^H^H^H^H^H
219  * Tru64 UNIX, and some versions of NetBSD pad FDDI packets to make everything
220  * line up on a nice boundary.
221  */
222 #ifdef __NetBSD__
223 #include <sys/param.h>	/* needed to declare __NetBSD_Version__ */
224 #endif
225 
226 #if defined(ultrix) || defined(__osf__) || (defined(__NetBSD__) && __NetBSD_Version__ > 106000000)
227 #define       PCAP_FDDIPAD 3
228 #endif
229 
230 typedef int	(*activate_op_t)(pcap_t *);
231 typedef int	(*can_set_rfmon_op_t)(pcap_t *);
232 typedef int	(*read_op_t)(pcap_t *, int cnt, pcap_handler, u_char *);
233 typedef int	(*inject_op_t)(pcap_t *, const void *, size_t);
234 typedef int	(*setfilter_op_t)(pcap_t *, struct bpf_program *);
235 typedef int	(*setdirection_op_t)(pcap_t *, pcap_direction_t);
236 typedef int	(*set_datalink_op_t)(pcap_t *, int);
237 typedef int	(*getnonblock_op_t)(pcap_t *, char *);
238 typedef int	(*setnonblock_op_t)(pcap_t *, int, char *);
239 typedef int	(*stats_op_t)(pcap_t *, struct pcap_stat *);
240 #ifdef WIN32
241 typedef int	(*setbuff_op_t)(pcap_t *, int);
242 typedef int	(*setmode_op_t)(pcap_t *, int);
243 typedef int	(*setmintocopy_op_t)(pcap_t *, int);
244 #endif
245 typedef void	(*cleanup_op_t)(pcap_t *);
246 
247 struct pcap {
248 #ifdef WIN32
249 	ADAPTER *adapter;
250 	LPPACKET Packet;
251 	int nonblock;
252 #else
253 	int fd;
254 	int selectable_fd;
255 	int send_fd;
256 #endif /* WIN32 */
257 
258 #ifdef HAVE_LIBDLPI
259 	dlpi_handle_t dlpi_hd;
260 #endif
261 	int snapshot;
262 	int linktype;		/* Network linktype */
263 	int linktype_ext;       /* Extended information stored in the linktype field of a file */
264 	int tzoff;		/* timezone offset */
265 	int offset;		/* offset for proper alignment */
266 	int activated;		/* true if the capture is really started */
267 	int oldstyle;		/* if we're opening with pcap_open_live() */
268 
269 	int break_loop;		/* flag set to force break from packet-reading loop */
270 
271 #ifdef PCAP_FDDIPAD
272 	int fddipad;
273 #endif
274 
275 #ifdef MSDOS
276         void (*wait_proc)(void); /*          call proc while waiting */
277 #endif
278 
279 	struct pcap_sf sf;
280 	struct pcap_md md;
281 	struct pcap_opt opt;
282 
283 	/*
284 	 * Read buffer.
285 	 */
286 	int bufsize;
287 	u_char *buffer;
288 	u_char *bp;
289 	int cc;
290 
291 	/*
292 	 * Place holder for pcap_next().
293 	 */
294 	u_char *pkt;
295 
296 	/* We're accepting only packets in this direction/these directions. */
297 	pcap_direction_t direction;
298 
299 	/*
300 	 * Methods.
301 	 */
302 	activate_op_t activate_op;
303 	can_set_rfmon_op_t can_set_rfmon_op;
304 	read_op_t read_op;
305 	inject_op_t inject_op;
306 	setfilter_op_t setfilter_op;
307 	setdirection_op_t setdirection_op;
308 	set_datalink_op_t set_datalink_op;
309 	getnonblock_op_t getnonblock_op;
310 	setnonblock_op_t setnonblock_op;
311 	stats_op_t stats_op;
312 
313 	/*
314 	 * Routine to use as callback for pcap_next()/pcap_next_ex().
315 	 */
316 	pcap_handler oneshot_callback;
317 
318 #ifdef WIN32
319 	/*
320 	 * These are, at least currently, specific to the Win32 NPF
321 	 * driver.
322 	 */
323 	setbuff_op_t setbuff_op;
324 	setmode_op_t setmode_op;
325 	setmintocopy_op_t setmintocopy_op;
326 #endif
327 	cleanup_op_t cleanup_op;
328 
329 	/*
330 	 * Placeholder for filter code if bpf not in kernel.
331 	 */
332 	struct bpf_program fcode;
333 
334 	char errbuf[PCAP_ERRBUF_SIZE + 1];
335 	int dlt_count;
336 	u_int *dlt_list;
337 	int tstamp_type_count;
338 	u_int *tstamp_type_list;
339 
340 	struct pcap_pkthdr pcap_header;	/* This is needed for the pcap_next_ex() to work */
341 };
342 
343 /*
344  * This is a timeval as stored in a savefile.
345  * It has to use the same types everywhere, independent of the actual
346  * `struct timeval'; `struct timeval' has 32-bit tv_sec values on some
347  * platforms and 64-bit tv_sec values on other platforms, and writing
348  * out native `struct timeval' values would mean files could only be
349  * read on systems with the same tv_sec size as the system on which
350  * the file was written.
351  */
352 
353 struct pcap_timeval {
354     bpf_int32 tv_sec;		/* seconds */
355     bpf_int32 tv_usec;		/* microseconds */
356 };
357 
358 /*
359  * This is a `pcap_pkthdr' as actually stored in a savefile.
360  *
361  * Do not change the format of this structure, in any way (this includes
362  * changes that only affect the length of fields in this structure),
363  * and do not make the time stamp anything other than seconds and
364  * microseconds (e.g., seconds and nanoseconds).  Instead:
365  *
366  *	introduce a new structure for the new format;
367  *
368  *	send mail to "tcpdump-workers@lists.tcpdump.org", requesting
369  *	a new magic number for your new capture file format, and, when
370  *	you get the new magic number, put it in "savefile.c";
371  *
372  *	use that magic number for save files with the changed record
373  *	header;
374  *
375  *	make the code in "savefile.c" capable of reading files with
376  *	the old record header as well as files with the new record header
377  *	(using the magic number to determine the header format).
378  *
379  * Then supply the changes as a patch at
380  *
381  *	http://sourceforge.net/projects/libpcap/
382  *
383  * so that future versions of libpcap and programs that use it (such as
384  * tcpdump) will be able to read your new capture file format.
385  */
386 
387 struct pcap_sf_pkthdr {
388     struct pcap_timeval ts;	/* time stamp */
389     bpf_u_int32 caplen;		/* length of portion present */
390     bpf_u_int32 len;		/* length this packet (off wire) */
391 };
392 
393 /*
394  * How a `pcap_pkthdr' is actually stored in savefiles written
395  * by some patched versions of libpcap (e.g. the ones in Red
396  * Hat Linux 6.1 and 6.2).
397  *
398  * Do not change the format of this structure, in any way (this includes
399  * changes that only affect the length of fields in this structure).
400  * Instead, introduce a new structure, as per the above.
401  */
402 
403 struct pcap_sf_patched_pkthdr {
404     struct pcap_timeval ts;	/* time stamp */
405     bpf_u_int32 caplen;		/* length of portion present */
406     bpf_u_int32 len;		/* length this packet (off wire) */
407     int		index;
408     unsigned short protocol;
409     unsigned char pkt_type;
410 };
411 
412 /*
413  * User data structure for the one-shot callback used for pcap_next()
414  * and pcap_next_ex().
415  */
416 struct oneshot_userdata {
417 	struct pcap_pkthdr *hdr;
418 	const u_char **pkt;
419 	pcap_t *pd;
420 };
421 
422 int	yylex(void);
423 
424 #ifndef min
425 #define min(a, b) ((a) > (b) ? (b) : (a))
426 #endif
427 
428 /* XXX should these be in pcap.h? */
429 int	pcap_offline_read(pcap_t *, int, pcap_handler, u_char *);
430 int	pcap_read(pcap_t *, int cnt, pcap_handler, u_char *);
431 
432 #ifndef HAVE_STRLCPY
433 #define strlcpy(x, y, z) \
434 	(strncpy((x), (y), (z)), \
435 	 ((z) <= 0 ? 0 : ((x)[(z) - 1] = '\0')), \
436 	 strlen((y)))
437 #endif
438 
439 #include <stdarg.h>
440 
441 #if !defined(HAVE_SNPRINTF)
442 #define snprintf pcap_snprintf
443 extern int snprintf (char *, size_t, const char *, ...);
444 #endif
445 
446 #if !defined(HAVE_VSNPRINTF)
447 #define vsnprintf pcap_vsnprintf
448 extern int vsnprintf (char *, size_t, const char *, va_list ap);
449 #endif
450 
451 /*
452  * Routines that most pcap implementations can use for non-blocking mode.
453  */
454 #if !defined(WIN32) && !defined(MSDOS)
455 int	pcap_getnonblock_fd(pcap_t *, char *);
456 int	pcap_setnonblock_fd(pcap_t *p, int, char *);
457 #endif
458 
459 pcap_t	*pcap_create_common(const char *, char *);
460 int	pcap_do_addexit(pcap_t *);
461 void	pcap_add_to_pcaps_to_close(pcap_t *);
462 void	pcap_remove_from_pcaps_to_close(pcap_t *);
463 void	pcap_cleanup_live_common(pcap_t *);
464 int	pcap_not_initialized(pcap_t *);
465 int	pcap_check_activated(pcap_t *);
466 
467 /*
468  * Internal interfaces for "pcap_findalldevs()".
469  *
470  * "pcap_platform_finddevs()" is a platform-dependent routine to
471  * add devices not found by the "standard" mechanisms (SIOCGIFCONF,
472  * "getifaddrs()", etc..
473  *
474  * "pcap_add_if()" adds an interface to the list of interfaces.
475  */
476 int	pcap_platform_finddevs(pcap_if_t **, char *);
477 int	add_addr_to_iflist(pcap_if_t **, const char *, u_int, struct sockaddr *,
478 	    size_t, struct sockaddr *, size_t, struct sockaddr *, size_t,
479 	    struct sockaddr *, size_t, char *);
480 int	pcap_add_if(pcap_if_t **, const char *, u_int, const char *, char *);
481 struct sockaddr *dup_sockaddr(struct sockaddr *, size_t);
482 int	add_or_find_if(pcap_if_t **, pcap_if_t **, const char *, u_int,
483 	    const char *, char *);
484 
485 #ifdef WIN32
486 char	*pcap_win32strerror(void);
487 #endif
488 
489 int	install_bpf_program(pcap_t *, struct bpf_program *);
490 
491 int	pcap_strcasecmp(const char *, const char *);
492 
493 #ifdef __cplusplus
494 }
495 #endif
496 
497 #endif
498