xref: /f-stack/tools/compat/include/net/bpf.h (revision d4a07e70)
11eaf0ac3Slogwang /*-
2*d4a07e70Sfengbojiang  * SPDX-License-Identifier: BSD-3-Clause
3*d4a07e70Sfengbojiang  *
41eaf0ac3Slogwang  * Copyright (c) 1990, 1991, 1993
51eaf0ac3Slogwang  *	The Regents of the University of California.  All rights reserved.
61eaf0ac3Slogwang  *
71eaf0ac3Slogwang  * This code is derived from the Stanford/CMU enet packet filter,
81eaf0ac3Slogwang  * (net/enet.c) distributed as part of 4.3BSD, and code contributed
91eaf0ac3Slogwang  * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
101eaf0ac3Slogwang  * Berkeley Laboratory.
111eaf0ac3Slogwang  *
121eaf0ac3Slogwang  * Redistribution and use in source and binary forms, with or without
131eaf0ac3Slogwang  * modification, are permitted provided that the following conditions
141eaf0ac3Slogwang  * are met:
151eaf0ac3Slogwang  * 1. Redistributions of source code must retain the above copyright
161eaf0ac3Slogwang  *    notice, this list of conditions and the following disclaimer.
171eaf0ac3Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
181eaf0ac3Slogwang  *    notice, this list of conditions and the following disclaimer in the
191eaf0ac3Slogwang  *    documentation and/or other materials provided with the distribution.
20*d4a07e70Sfengbojiang  * 3. Neither the name of the University nor the names of its contributors
211eaf0ac3Slogwang  *    may be used to endorse or promote products derived from this software
221eaf0ac3Slogwang  *    without specific prior written permission.
231eaf0ac3Slogwang  *
241eaf0ac3Slogwang  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251eaf0ac3Slogwang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261eaf0ac3Slogwang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271eaf0ac3Slogwang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281eaf0ac3Slogwang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291eaf0ac3Slogwang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301eaf0ac3Slogwang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311eaf0ac3Slogwang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321eaf0ac3Slogwang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331eaf0ac3Slogwang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341eaf0ac3Slogwang  * SUCH DAMAGE.
351eaf0ac3Slogwang  *
361eaf0ac3Slogwang  *      @(#)bpf.h	8.1 (Berkeley) 6/10/93
371eaf0ac3Slogwang  *	@(#)bpf.h	1.34 (LBL)     6/16/96
381eaf0ac3Slogwang  *
391eaf0ac3Slogwang  * $FreeBSD$
401eaf0ac3Slogwang  */
411eaf0ac3Slogwang 
421eaf0ac3Slogwang #ifndef _NET_BPF_H_
431eaf0ac3Slogwang #define _NET_BPF_H_
441eaf0ac3Slogwang 
45*d4a07e70Sfengbojiang #include <sys/ck.h>
46*d4a07e70Sfengbojiang #include <net/dlt.h>
47*d4a07e70Sfengbojiang 
481eaf0ac3Slogwang /* BSD style release date */
491eaf0ac3Slogwang #define	BPF_RELEASE 199606
501eaf0ac3Slogwang 
511eaf0ac3Slogwang typedef	int32_t	  bpf_int32;
521eaf0ac3Slogwang typedef	u_int32_t bpf_u_int32;
531eaf0ac3Slogwang typedef	int64_t	  bpf_int64;
541eaf0ac3Slogwang typedef	u_int64_t bpf_u_int64;
551eaf0ac3Slogwang 
561eaf0ac3Slogwang /*
571eaf0ac3Slogwang  * Alignment macros.  BPF_WORDALIGN rounds up to the next
581eaf0ac3Slogwang  * even multiple of BPF_ALIGNMENT.
591eaf0ac3Slogwang  */
601eaf0ac3Slogwang #define BPF_ALIGNMENT sizeof(long)
611eaf0ac3Slogwang #define BPF_WORDALIGN(x) (((x)+(BPF_ALIGNMENT-1))&~(BPF_ALIGNMENT-1))
621eaf0ac3Slogwang 
631eaf0ac3Slogwang #define BPF_MAXINSNS 512
641eaf0ac3Slogwang #define BPF_MAXBUFSIZE 0x80000
651eaf0ac3Slogwang #define BPF_MINBUFSIZE 32
661eaf0ac3Slogwang 
671eaf0ac3Slogwang /*
681eaf0ac3Slogwang  *  Structure for BIOCSETF.
691eaf0ac3Slogwang  */
701eaf0ac3Slogwang struct bpf_program {
711eaf0ac3Slogwang 	u_int bf_len;
721eaf0ac3Slogwang 	struct bpf_insn *bf_insns;
731eaf0ac3Slogwang };
741eaf0ac3Slogwang 
751eaf0ac3Slogwang /*
761eaf0ac3Slogwang  * Struct returned by BIOCGSTATS.
771eaf0ac3Slogwang  */
781eaf0ac3Slogwang struct bpf_stat {
791eaf0ac3Slogwang 	u_int bs_recv;		/* number of packets received */
801eaf0ac3Slogwang 	u_int bs_drop;		/* number of packets dropped */
811eaf0ac3Slogwang };
821eaf0ac3Slogwang 
831eaf0ac3Slogwang /*
841eaf0ac3Slogwang  * Struct return by BIOCVERSION.  This represents the version number of
851eaf0ac3Slogwang  * the filter language described by the instruction encodings below.
861eaf0ac3Slogwang  * bpf understands a program iff kernel_major == filter_major &&
871eaf0ac3Slogwang  * kernel_minor >= filter_minor, that is, if the value returned by the
881eaf0ac3Slogwang  * running kernel has the same major number and a minor number equal
891eaf0ac3Slogwang  * equal to or less than the filter being downloaded.  Otherwise, the
901eaf0ac3Slogwang  * results are undefined, meaning an error may be returned or packets
911eaf0ac3Slogwang  * may be accepted haphazardly.
921eaf0ac3Slogwang  * It has nothing to do with the source code version.
931eaf0ac3Slogwang  */
941eaf0ac3Slogwang struct bpf_version {
951eaf0ac3Slogwang 	u_short bv_major;
961eaf0ac3Slogwang 	u_short bv_minor;
971eaf0ac3Slogwang };
981eaf0ac3Slogwang /* Current version number of filter architecture. */
991eaf0ac3Slogwang #define BPF_MAJOR_VERSION 1
1001eaf0ac3Slogwang #define BPF_MINOR_VERSION 1
1011eaf0ac3Slogwang 
1021eaf0ac3Slogwang /*
1031eaf0ac3Slogwang  * Historically, BPF has supported a single buffering model, first using mbuf
1041eaf0ac3Slogwang  * clusters in kernel, and later using malloc(9) buffers in kernel.  We now
1051eaf0ac3Slogwang  * support multiple buffering modes, which may be queried and set using
1061eaf0ac3Slogwang  * BIOCGETBUFMODE and BIOCSETBUFMODE.  So as to avoid handling the complexity
1071eaf0ac3Slogwang  * of changing modes while sniffing packets, the mode becomes fixed once an
1081eaf0ac3Slogwang  * interface has been attached to the BPF descriptor.
1091eaf0ac3Slogwang  */
1101eaf0ac3Slogwang #define	BPF_BUFMODE_BUFFER	1	/* Kernel buffers with read(). */
1111eaf0ac3Slogwang #define	BPF_BUFMODE_ZBUF	2	/* Zero-copy buffers. */
1121eaf0ac3Slogwang 
1131eaf0ac3Slogwang /*-
1141eaf0ac3Slogwang  * Struct used by BIOCSETZBUF, BIOCROTZBUF: describes up to two zero-copy
1151eaf0ac3Slogwang  * buffer as used by BPF.
1161eaf0ac3Slogwang  */
1171eaf0ac3Slogwang struct bpf_zbuf {
1181eaf0ac3Slogwang 	void	*bz_bufa;	/* Location of 'a' zero-copy buffer. */
1191eaf0ac3Slogwang 	void	*bz_bufb;	/* Location of 'b' zero-copy buffer. */
1201eaf0ac3Slogwang 	size_t	 bz_buflen;	/* Size of zero-copy buffers. */
1211eaf0ac3Slogwang };
1221eaf0ac3Slogwang 
1231eaf0ac3Slogwang #define	BIOCGBLEN	_IOR('B', 102, u_int)
1241eaf0ac3Slogwang #define	BIOCSBLEN	_IOWR('B', 102, u_int)
1251eaf0ac3Slogwang #define	BIOCSETF	_IOW('B', 103, struct bpf_program)
1261eaf0ac3Slogwang #define	BIOCFLUSH	_IO('B', 104)
1271eaf0ac3Slogwang #define	BIOCPROMISC	_IO('B', 105)
1281eaf0ac3Slogwang #define	BIOCGDLT	_IOR('B', 106, u_int)
1291eaf0ac3Slogwang #define	BIOCGETIF	_IOR('B', 107, struct ifreq)
1301eaf0ac3Slogwang #define	BIOCSETIF	_IOW('B', 108, struct ifreq)
1311eaf0ac3Slogwang #define	BIOCSRTIMEOUT	_IOW('B', 109, struct timeval)
1321eaf0ac3Slogwang #define	BIOCGRTIMEOUT	_IOR('B', 110, struct timeval)
1331eaf0ac3Slogwang #define	BIOCGSTATS	_IOR('B', 111, struct bpf_stat)
1341eaf0ac3Slogwang #define	BIOCIMMEDIATE	_IOW('B', 112, u_int)
1351eaf0ac3Slogwang #define	BIOCVERSION	_IOR('B', 113, struct bpf_version)
1361eaf0ac3Slogwang #define	BIOCGRSIG	_IOR('B', 114, u_int)
1371eaf0ac3Slogwang #define	BIOCSRSIG	_IOW('B', 115, u_int)
1381eaf0ac3Slogwang #define	BIOCGHDRCMPLT	_IOR('B', 116, u_int)
1391eaf0ac3Slogwang #define	BIOCSHDRCMPLT	_IOW('B', 117, u_int)
1401eaf0ac3Slogwang #define	BIOCGDIRECTION	_IOR('B', 118, u_int)
1411eaf0ac3Slogwang #define	BIOCSDIRECTION	_IOW('B', 119, u_int)
1421eaf0ac3Slogwang #define	BIOCSDLT	_IOW('B', 120, u_int)
1431eaf0ac3Slogwang #define	BIOCGDLTLIST	_IOWR('B', 121, struct bpf_dltlist)
1441eaf0ac3Slogwang #define	BIOCLOCK	_IO('B', 122)
1451eaf0ac3Slogwang #define	BIOCSETWF	_IOW('B', 123, struct bpf_program)
1461eaf0ac3Slogwang #define	BIOCFEEDBACK	_IOW('B', 124, u_int)
1471eaf0ac3Slogwang #define	BIOCGETBUFMODE	_IOR('B', 125, u_int)
1481eaf0ac3Slogwang #define	BIOCSETBUFMODE	_IOW('B', 126, u_int)
1491eaf0ac3Slogwang #define	BIOCGETZMAX	_IOR('B', 127, size_t)
1501eaf0ac3Slogwang #define	BIOCROTZBUF	_IOR('B', 128, struct bpf_zbuf)
1511eaf0ac3Slogwang #define	BIOCSETZBUF	_IOW('B', 129, struct bpf_zbuf)
1521eaf0ac3Slogwang #define	BIOCSETFNR	_IOW('B', 130, struct bpf_program)
1531eaf0ac3Slogwang #define	BIOCGTSTAMP	_IOR('B', 131, u_int)
1541eaf0ac3Slogwang #define	BIOCSTSTAMP	_IOW('B', 132, u_int)
1551eaf0ac3Slogwang 
1561eaf0ac3Slogwang /* Obsolete */
1571eaf0ac3Slogwang #define	BIOCGSEESENT	BIOCGDIRECTION
1581eaf0ac3Slogwang #define	BIOCSSEESENT	BIOCSDIRECTION
1591eaf0ac3Slogwang 
1601eaf0ac3Slogwang /* Packet directions */
1611eaf0ac3Slogwang enum bpf_direction {
1621eaf0ac3Slogwang 	BPF_D_IN,	/* See incoming packets */
1631eaf0ac3Slogwang 	BPF_D_INOUT,	/* See incoming and outgoing packets */
1641eaf0ac3Slogwang 	BPF_D_OUT	/* See outgoing packets */
1651eaf0ac3Slogwang };
1661eaf0ac3Slogwang 
1671eaf0ac3Slogwang /* Time stamping functions */
1681eaf0ac3Slogwang #define	BPF_T_MICROTIME		0x0000
1691eaf0ac3Slogwang #define	BPF_T_NANOTIME		0x0001
1701eaf0ac3Slogwang #define	BPF_T_BINTIME		0x0002
1711eaf0ac3Slogwang #define	BPF_T_NONE		0x0003
1721eaf0ac3Slogwang #define	BPF_T_FORMAT_MASK	0x0003
1731eaf0ac3Slogwang #define	BPF_T_NORMAL		0x0000
1741eaf0ac3Slogwang #define	BPF_T_FAST		0x0100
1751eaf0ac3Slogwang #define	BPF_T_MONOTONIC		0x0200
1761eaf0ac3Slogwang #define	BPF_T_MONOTONIC_FAST	(BPF_T_FAST | BPF_T_MONOTONIC)
1771eaf0ac3Slogwang #define	BPF_T_FLAG_MASK		0x0300
1781eaf0ac3Slogwang #define	BPF_T_FORMAT(t)		((t) & BPF_T_FORMAT_MASK)
1791eaf0ac3Slogwang #define	BPF_T_FLAG(t)		((t) & BPF_T_FLAG_MASK)
1801eaf0ac3Slogwang #define	BPF_T_VALID(t)						\
1811eaf0ac3Slogwang     ((t) == BPF_T_NONE || (BPF_T_FORMAT(t) != BPF_T_NONE &&	\
1821eaf0ac3Slogwang     ((t) & ~(BPF_T_FORMAT_MASK | BPF_T_FLAG_MASK)) == 0))
1831eaf0ac3Slogwang 
1841eaf0ac3Slogwang #define	BPF_T_MICROTIME_FAST		(BPF_T_MICROTIME | BPF_T_FAST)
1851eaf0ac3Slogwang #define	BPF_T_NANOTIME_FAST		(BPF_T_NANOTIME | BPF_T_FAST)
1861eaf0ac3Slogwang #define	BPF_T_BINTIME_FAST		(BPF_T_BINTIME | BPF_T_FAST)
1871eaf0ac3Slogwang #define	BPF_T_MICROTIME_MONOTONIC	(BPF_T_MICROTIME | BPF_T_MONOTONIC)
1881eaf0ac3Slogwang #define	BPF_T_NANOTIME_MONOTONIC	(BPF_T_NANOTIME | BPF_T_MONOTONIC)
1891eaf0ac3Slogwang #define	BPF_T_BINTIME_MONOTONIC		(BPF_T_BINTIME | BPF_T_MONOTONIC)
1901eaf0ac3Slogwang #define	BPF_T_MICROTIME_MONOTONIC_FAST	(BPF_T_MICROTIME | BPF_T_MONOTONIC_FAST)
1911eaf0ac3Slogwang #define	BPF_T_NANOTIME_MONOTONIC_FAST	(BPF_T_NANOTIME | BPF_T_MONOTONIC_FAST)
1921eaf0ac3Slogwang #define	BPF_T_BINTIME_MONOTONIC_FAST	(BPF_T_BINTIME | BPF_T_MONOTONIC_FAST)
1931eaf0ac3Slogwang 
1941eaf0ac3Slogwang /*
1951eaf0ac3Slogwang  * Structure prepended to each packet.
1961eaf0ac3Slogwang  */
1971eaf0ac3Slogwang struct bpf_ts {
1981eaf0ac3Slogwang 	bpf_int64	bt_sec;		/* seconds */
1991eaf0ac3Slogwang 	bpf_u_int64	bt_frac;	/* fraction */
2001eaf0ac3Slogwang };
2011eaf0ac3Slogwang struct bpf_xhdr {
2021eaf0ac3Slogwang 	struct bpf_ts	bh_tstamp;	/* time stamp */
2031eaf0ac3Slogwang 	bpf_u_int32	bh_caplen;	/* length of captured portion */
2041eaf0ac3Slogwang 	bpf_u_int32	bh_datalen;	/* original length of packet */
2051eaf0ac3Slogwang 	u_short		bh_hdrlen;	/* length of bpf header (this struct
2061eaf0ac3Slogwang 					   plus alignment padding) */
2071eaf0ac3Slogwang };
2081eaf0ac3Slogwang /* Obsolete */
2091eaf0ac3Slogwang struct bpf_hdr {
2101eaf0ac3Slogwang 	struct timeval	bh_tstamp;	/* time stamp */
2111eaf0ac3Slogwang 	bpf_u_int32	bh_caplen;	/* length of captured portion */
2121eaf0ac3Slogwang 	bpf_u_int32	bh_datalen;	/* original length of packet */
2131eaf0ac3Slogwang 	u_short		bh_hdrlen;	/* length of bpf header (this struct
2141eaf0ac3Slogwang 					   plus alignment padding) */
2151eaf0ac3Slogwang };
2161eaf0ac3Slogwang #ifdef _KERNEL
2171eaf0ac3Slogwang #define	MTAG_BPF		0x627066
2181eaf0ac3Slogwang #define	MTAG_BPF_TIMESTAMP	0
2191eaf0ac3Slogwang #endif
2201eaf0ac3Slogwang 
2211eaf0ac3Slogwang /*
2221eaf0ac3Slogwang  * When using zero-copy BPF buffers, a shared memory header is present
2231eaf0ac3Slogwang  * allowing the kernel BPF implementation and user process to synchronize
2241eaf0ac3Slogwang  * without using system calls.  This structure defines that header.  When
2251eaf0ac3Slogwang  * accessing these fields, appropriate atomic operation and memory barriers
2261eaf0ac3Slogwang  * are required in order not to see stale or out-of-order data; see bpf(4)
2271eaf0ac3Slogwang  * for reference code to access these fields from userspace.
2281eaf0ac3Slogwang  *
2291eaf0ac3Slogwang  * The layout of this structure is critical, and must not be changed; if must
2301eaf0ac3Slogwang  * fit in a single page on all architectures.
2311eaf0ac3Slogwang  */
2321eaf0ac3Slogwang struct bpf_zbuf_header {
2331eaf0ac3Slogwang 	volatile u_int	bzh_kernel_gen;	/* Kernel generation number. */
2341eaf0ac3Slogwang 	volatile u_int	bzh_kernel_len;	/* Length of data in the buffer. */
2351eaf0ac3Slogwang 	volatile u_int	bzh_user_gen;	/* User generation number. */
2361eaf0ac3Slogwang 	u_int _bzh_pad[5];
2371eaf0ac3Slogwang };
2381eaf0ac3Slogwang 
2391eaf0ac3Slogwang /*
2401eaf0ac3Slogwang  * The instruction encodings.
2411eaf0ac3Slogwang  *
2421eaf0ac3Slogwang  * Please inform [email protected] if you use any
2431eaf0ac3Slogwang  * of the reserved values, so that we can note that they're used
2441eaf0ac3Slogwang  * (and perhaps implement it in the reference BPF implementation
2451eaf0ac3Slogwang  * and encourage its implementation elsewhere).
2461eaf0ac3Slogwang  */
2471eaf0ac3Slogwang 
2481eaf0ac3Slogwang /*
2491eaf0ac3Slogwang  * The upper 8 bits of the opcode aren't used. BSD/OS used 0x8000.
2501eaf0ac3Slogwang  */
2511eaf0ac3Slogwang 
2521eaf0ac3Slogwang /* instruction classes */
2531eaf0ac3Slogwang #define BPF_CLASS(code) ((code) & 0x07)
2541eaf0ac3Slogwang #define		BPF_LD		0x00
2551eaf0ac3Slogwang #define		BPF_LDX		0x01
2561eaf0ac3Slogwang #define		BPF_ST		0x02
2571eaf0ac3Slogwang #define		BPF_STX		0x03
2581eaf0ac3Slogwang #define		BPF_ALU		0x04
2591eaf0ac3Slogwang #define		BPF_JMP		0x05
2601eaf0ac3Slogwang #define		BPF_RET		0x06
2611eaf0ac3Slogwang #define		BPF_MISC	0x07
2621eaf0ac3Slogwang 
2631eaf0ac3Slogwang /* ld/ldx fields */
2641eaf0ac3Slogwang #define BPF_SIZE(code)	((code) & 0x18)
2651eaf0ac3Slogwang #define		BPF_W		0x00
2661eaf0ac3Slogwang #define		BPF_H		0x08
2671eaf0ac3Slogwang #define		BPF_B		0x10
2681eaf0ac3Slogwang /*				0x18	reserved; used by BSD/OS */
2691eaf0ac3Slogwang #define BPF_MODE(code)	((code) & 0xe0)
2701eaf0ac3Slogwang #define		BPF_IMM 	0x00
2711eaf0ac3Slogwang #define		BPF_ABS		0x20
2721eaf0ac3Slogwang #define		BPF_IND		0x40
2731eaf0ac3Slogwang #define		BPF_MEM		0x60
2741eaf0ac3Slogwang #define		BPF_LEN		0x80
2751eaf0ac3Slogwang #define		BPF_MSH		0xa0
2761eaf0ac3Slogwang /*				0xc0	reserved; used by BSD/OS */
2771eaf0ac3Slogwang /*				0xe0	reserved; used by BSD/OS */
2781eaf0ac3Slogwang 
2791eaf0ac3Slogwang /* alu/jmp fields */
2801eaf0ac3Slogwang #define BPF_OP(code)	((code) & 0xf0)
2811eaf0ac3Slogwang #define		BPF_ADD		0x00
2821eaf0ac3Slogwang #define		BPF_SUB		0x10
2831eaf0ac3Slogwang #define		BPF_MUL		0x20
2841eaf0ac3Slogwang #define		BPF_DIV		0x30
2851eaf0ac3Slogwang #define		BPF_OR		0x40
2861eaf0ac3Slogwang #define		BPF_AND		0x50
2871eaf0ac3Slogwang #define		BPF_LSH		0x60
2881eaf0ac3Slogwang #define		BPF_RSH		0x70
2891eaf0ac3Slogwang #define		BPF_NEG		0x80
2901eaf0ac3Slogwang #define		BPF_MOD		0x90
2911eaf0ac3Slogwang #define		BPF_XOR		0xa0
2921eaf0ac3Slogwang /*				0xb0	reserved */
2931eaf0ac3Slogwang /*				0xc0	reserved */
2941eaf0ac3Slogwang /*				0xd0	reserved */
2951eaf0ac3Slogwang /*				0xe0	reserved */
2961eaf0ac3Slogwang /*				0xf0	reserved */
2971eaf0ac3Slogwang 
2981eaf0ac3Slogwang #define		BPF_JA		0x00
2991eaf0ac3Slogwang #define		BPF_JEQ		0x10
3001eaf0ac3Slogwang #define		BPF_JGT		0x20
3011eaf0ac3Slogwang #define		BPF_JGE		0x30
3021eaf0ac3Slogwang #define		BPF_JSET	0x40
3031eaf0ac3Slogwang /*				0x50	reserved; used on BSD/OS */
3041eaf0ac3Slogwang /*				0x60	reserved */
3051eaf0ac3Slogwang /*				0x70	reserved */
3061eaf0ac3Slogwang /*				0x80	reserved */
3071eaf0ac3Slogwang /*				0x90	reserved */
3081eaf0ac3Slogwang /*				0xa0	reserved */
3091eaf0ac3Slogwang /*				0xb0	reserved */
3101eaf0ac3Slogwang /*				0xc0	reserved */
3111eaf0ac3Slogwang /*				0xd0	reserved */
3121eaf0ac3Slogwang /*				0xe0	reserved */
3131eaf0ac3Slogwang /*				0xf0	reserved */
3141eaf0ac3Slogwang #define BPF_SRC(code)	((code) & 0x08)
3151eaf0ac3Slogwang #define		BPF_K		0x00
3161eaf0ac3Slogwang #define		BPF_X		0x08
3171eaf0ac3Slogwang 
3181eaf0ac3Slogwang /* ret - BPF_K and BPF_X also apply */
3191eaf0ac3Slogwang #define BPF_RVAL(code)	((code) & 0x18)
3201eaf0ac3Slogwang #define		BPF_A		0x10
3211eaf0ac3Slogwang /*				0x18	reserved */
3221eaf0ac3Slogwang 
3231eaf0ac3Slogwang /* misc */
3241eaf0ac3Slogwang #define BPF_MISCOP(code) ((code) & 0xf8)
3251eaf0ac3Slogwang #define		BPF_TAX		0x00
3261eaf0ac3Slogwang /*				0x08	reserved */
3271eaf0ac3Slogwang /*				0x10	reserved */
3281eaf0ac3Slogwang /*				0x18	reserved */
3291eaf0ac3Slogwang /* #define	BPF_COP		0x20	NetBSD "coprocessor" extensions */
3301eaf0ac3Slogwang /*				0x28	reserved */
3311eaf0ac3Slogwang /*				0x30	reserved */
3321eaf0ac3Slogwang /*				0x38	reserved */
3331eaf0ac3Slogwang /* #define	BPF_COPX	0x40	NetBSD "coprocessor" extensions */
3341eaf0ac3Slogwang /*					also used on BSD/OS */
3351eaf0ac3Slogwang /*				0x48	reserved */
3361eaf0ac3Slogwang /*				0x50	reserved */
3371eaf0ac3Slogwang /*				0x58	reserved */
3381eaf0ac3Slogwang /*				0x60	reserved */
3391eaf0ac3Slogwang /*				0x68	reserved */
3401eaf0ac3Slogwang /*				0x70	reserved */
3411eaf0ac3Slogwang /*				0x78	reserved */
3421eaf0ac3Slogwang #define		BPF_TXA		0x80
3431eaf0ac3Slogwang /*				0x88	reserved */
3441eaf0ac3Slogwang /*				0x90	reserved */
3451eaf0ac3Slogwang /*				0x98	reserved */
3461eaf0ac3Slogwang /*				0xa0	reserved */
3471eaf0ac3Slogwang /*				0xa8	reserved */
3481eaf0ac3Slogwang /*				0xb0	reserved */
3491eaf0ac3Slogwang /*				0xb8	reserved */
3501eaf0ac3Slogwang /*				0xc0	reserved; used on BSD/OS */
3511eaf0ac3Slogwang /*				0xc8	reserved */
3521eaf0ac3Slogwang /*				0xd0	reserved */
3531eaf0ac3Slogwang /*				0xd8	reserved */
3541eaf0ac3Slogwang /*				0xe0	reserved */
3551eaf0ac3Slogwang /*				0xe8	reserved */
3561eaf0ac3Slogwang /*				0xf0	reserved */
3571eaf0ac3Slogwang /*				0xf8	reserved */
3581eaf0ac3Slogwang 
3591eaf0ac3Slogwang /*
3601eaf0ac3Slogwang  * The instruction data structure.
3611eaf0ac3Slogwang  */
3621eaf0ac3Slogwang struct bpf_insn {
3631eaf0ac3Slogwang 	u_short		code;
3641eaf0ac3Slogwang 	u_char		jt;
3651eaf0ac3Slogwang 	u_char		jf;
3661eaf0ac3Slogwang 	bpf_u_int32	k;
3671eaf0ac3Slogwang };
3681eaf0ac3Slogwang 
3691eaf0ac3Slogwang /*
3701eaf0ac3Slogwang  * Macros for insn array initializers.
3711eaf0ac3Slogwang  */
3721eaf0ac3Slogwang #define BPF_STMT(code, k) { (u_short)(code), 0, 0, k }
3731eaf0ac3Slogwang #define BPF_JUMP(code, k, jt, jf) { (u_short)(code), jt, jf, k }
3741eaf0ac3Slogwang 
3751eaf0ac3Slogwang /*
3761eaf0ac3Slogwang  * Structure to retrieve available DLTs for the interface.
3771eaf0ac3Slogwang  */
3781eaf0ac3Slogwang struct bpf_dltlist {
3791eaf0ac3Slogwang 	u_int	bfl_len;	/* number of bfd_list array */
3801eaf0ac3Slogwang 	u_int	*bfl_list;	/* array of DLTs */
3811eaf0ac3Slogwang };
3821eaf0ac3Slogwang 
3831eaf0ac3Slogwang #ifdef _KERNEL
3841eaf0ac3Slogwang #ifdef MALLOC_DECLARE
3851eaf0ac3Slogwang MALLOC_DECLARE(M_BPF);
3861eaf0ac3Slogwang #endif
3871eaf0ac3Slogwang #ifdef SYSCTL_DECL
3881eaf0ac3Slogwang SYSCTL_DECL(_net_bpf);
3891eaf0ac3Slogwang #endif
3901eaf0ac3Slogwang 
3911eaf0ac3Slogwang /*
3921eaf0ac3Slogwang  * Rotate the packet buffers in descriptor d.  Move the store buffer into the
3931eaf0ac3Slogwang  * hold slot, and the free buffer into the store slot.  Zero the length of the
3941eaf0ac3Slogwang  * new store buffer.  Descriptor lock should be held.  One must be careful to
3951eaf0ac3Slogwang  * not rotate the buffers twice, i.e. if fbuf != NULL.
3961eaf0ac3Slogwang  */
3971eaf0ac3Slogwang #define	ROTATE_BUFFERS(d)	do {					\
3981eaf0ac3Slogwang 	(d)->bd_hbuf = (d)->bd_sbuf;					\
3991eaf0ac3Slogwang 	(d)->bd_hlen = (d)->bd_slen;					\
4001eaf0ac3Slogwang 	(d)->bd_sbuf = (d)->bd_fbuf;					\
4011eaf0ac3Slogwang 	(d)->bd_slen = 0;						\
4021eaf0ac3Slogwang 	(d)->bd_fbuf = NULL;						\
4031eaf0ac3Slogwang 	bpf_bufheld(d);							\
4041eaf0ac3Slogwang } while (0)
4051eaf0ac3Slogwang 
4061eaf0ac3Slogwang /*
4071eaf0ac3Slogwang  * Descriptor associated with each attached hardware interface.
4081eaf0ac3Slogwang  * Part of this structure is exposed to external callers to speed up
4091eaf0ac3Slogwang  * bpf_peers_present() calls.
4101eaf0ac3Slogwang  */
4111eaf0ac3Slogwang struct bpf_if;
412*d4a07e70Sfengbojiang CK_LIST_HEAD(bpfd_list, bpf_d);
4131eaf0ac3Slogwang 
4141eaf0ac3Slogwang struct bpf_if_ext {
415*d4a07e70Sfengbojiang 	CK_LIST_ENTRY(bpf_if)	bif_next;	/* list of all interfaces */
416*d4a07e70Sfengbojiang 	struct bpfd_list	bif_dlist;	/* descriptor list */
4171eaf0ac3Slogwang };
4181eaf0ac3Slogwang 
4191eaf0ac3Slogwang void	 bpf_bufheld(struct bpf_d *d);
4201eaf0ac3Slogwang int	 bpf_validate(const struct bpf_insn *, int);
4211eaf0ac3Slogwang void	 bpf_tap(struct bpf_if *, u_char *, u_int);
4221eaf0ac3Slogwang void	 bpf_mtap(struct bpf_if *, struct mbuf *);
4231eaf0ac3Slogwang void	 bpf_mtap2(struct bpf_if *, void *, u_int, struct mbuf *);
4241eaf0ac3Slogwang void	 bpfattach(struct ifnet *, u_int, u_int);
4251eaf0ac3Slogwang void	 bpfattach2(struct ifnet *, u_int, u_int, struct bpf_if **);
4261eaf0ac3Slogwang void	 bpfdetach(struct ifnet *);
4271eaf0ac3Slogwang #ifdef VIMAGE
4281eaf0ac3Slogwang int	 bpf_get_bp_params(struct bpf_if *, u_int *, u_int *);
4291eaf0ac3Slogwang #endif
4301eaf0ac3Slogwang 
4311eaf0ac3Slogwang void	 bpfilterattach(int);
4321eaf0ac3Slogwang u_int	 bpf_filter(const struct bpf_insn *, u_char *, u_int, u_int);
4331eaf0ac3Slogwang 
4341eaf0ac3Slogwang static __inline int
bpf_peers_present(struct bpf_if * bpf)4351eaf0ac3Slogwang bpf_peers_present(struct bpf_if *bpf)
4361eaf0ac3Slogwang {
4371eaf0ac3Slogwang 	struct bpf_if_ext *ext;
4381eaf0ac3Slogwang 
4391eaf0ac3Slogwang 	ext = (struct bpf_if_ext *)bpf;
440*d4a07e70Sfengbojiang 	if (!CK_LIST_EMPTY(&ext->bif_dlist))
4411eaf0ac3Slogwang 		return (1);
4421eaf0ac3Slogwang 	return (0);
4431eaf0ac3Slogwang }
4441eaf0ac3Slogwang 
4451eaf0ac3Slogwang #define	BPF_TAP(_ifp,_pkt,_pktlen) do {				\
4461eaf0ac3Slogwang 	if (bpf_peers_present((_ifp)->if_bpf))			\
4471eaf0ac3Slogwang 		bpf_tap((_ifp)->if_bpf, (_pkt), (_pktlen));	\
4481eaf0ac3Slogwang } while (0)
4491eaf0ac3Slogwang #define	BPF_MTAP(_ifp,_m) do {					\
4501eaf0ac3Slogwang 	if (bpf_peers_present((_ifp)->if_bpf)) {		\
4511eaf0ac3Slogwang 		M_ASSERTVALID(_m);				\
4521eaf0ac3Slogwang 		bpf_mtap((_ifp)->if_bpf, (_m));			\
4531eaf0ac3Slogwang 	}							\
4541eaf0ac3Slogwang } while (0)
4551eaf0ac3Slogwang #define	BPF_MTAP2(_ifp,_data,_dlen,_m) do {			\
4561eaf0ac3Slogwang 	if (bpf_peers_present((_ifp)->if_bpf)) {		\
4571eaf0ac3Slogwang 		M_ASSERTVALID(_m);				\
4581eaf0ac3Slogwang 		bpf_mtap2((_ifp)->if_bpf,(_data),(_dlen),(_m));	\
4591eaf0ac3Slogwang 	}							\
4601eaf0ac3Slogwang } while (0)
4611eaf0ac3Slogwang #endif
4621eaf0ac3Slogwang 
4631eaf0ac3Slogwang /*
4641eaf0ac3Slogwang  * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST).
4651eaf0ac3Slogwang  */
4661eaf0ac3Slogwang #define BPF_MEMWORDS 16
4671eaf0ac3Slogwang 
4681eaf0ac3Slogwang #ifdef _SYS_EVENTHANDLER_H_
4691eaf0ac3Slogwang /* BPF attach/detach events */
4701eaf0ac3Slogwang struct ifnet;
4711eaf0ac3Slogwang typedef void (*bpf_track_fn)(void *, struct ifnet *, int /* dlt */,
4721eaf0ac3Slogwang     int /* 1 =>'s attach */);
4731eaf0ac3Slogwang EVENTHANDLER_DECLARE(bpf_track, bpf_track_fn);
4741eaf0ac3Slogwang #endif /* _SYS_EVENTHANDLER_H_ */
4751eaf0ac3Slogwang 
4761eaf0ac3Slogwang #endif /* _NET_BPF_H_ */
477