bpf.h revision 52248
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1990, 1991, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * This code is derived from the Stanford/CMU enet packet filter,
61541Srgrimes * (net/enet.c) distributed as part of 4.3BSD, and code contributed
71541Srgrimes * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
81541Srgrimes * Berkeley Laboratory.
91541Srgrimes *
101541Srgrimes * Redistribution and use in source and binary forms, with or without
111541Srgrimes * modification, are permitted provided that the following conditions
121541Srgrimes * are met:
131541Srgrimes * 1. Redistributions of source code must retain the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer.
151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161541Srgrimes *    notice, this list of conditions and the following disclaimer in the
171541Srgrimes *    documentation and/or other materials provided with the distribution.
181541Srgrimes * 3. All advertising materials mentioning features or use of this software
191541Srgrimes *    must display the following acknowledgement:
201541Srgrimes *	This product includes software developed by the University of
211541Srgrimes *	California, Berkeley and its contributors.
221541Srgrimes * 4. Neither the name of the University nor the names of its contributors
231541Srgrimes *    may be used to endorse or promote products derived from this software
241541Srgrimes *    without specific prior written permission.
251541Srgrimes *
261541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
271541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
281541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
291541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
301541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
311541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
321541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
331541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
341541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
351541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
361541Srgrimes * SUCH DAMAGE.
371541Srgrimes *
381541Srgrimes *      @(#)bpf.h	8.1 (Berkeley) 6/10/93
3917679Spst *	@(#)bpf.h	1.34 (LBL)     6/16/96
401541Srgrimes *
4150477Speter * $FreeBSD: head/sys/net/bpf.h 52248 1999-10-15 05:07:00Z msmith $
421541Srgrimes */
431541Srgrimes
442168Spaul#ifndef _NET_BPF_H_
452168Spaul#define _NET_BPF_H_
462168Spaul
4717679Spst/* BSD style release date */
4817679Spst#define	BPF_RELEASE 199606
4917679Spst
5017679Spsttypedef	int32_t	  bpf_int32;
5117679Spsttypedef	u_int32_t bpf_u_int32;
5217679Spst
531541Srgrimes/*
548876Srgrimes * Alignment macros.  BPF_WORDALIGN rounds up to the next
558876Srgrimes * even multiple of BPF_ALIGNMENT.
561541Srgrimes */
5739963Salex#define BPF_ALIGNMENT sizeof(long)
581541Srgrimes#define BPF_WORDALIGN(x) (((x)+(BPF_ALIGNMENT-1))&~(BPF_ALIGNMENT-1))
591541Srgrimes
601541Srgrimes#define BPF_MAXINSNS 512
611541Srgrimes#define BPF_MAXBUFSIZE 0x8000
621541Srgrimes#define BPF_MINBUFSIZE 32
631541Srgrimes
641541Srgrimes/*
651541Srgrimes *  Structure for BIOCSETF.
661541Srgrimes */
671541Srgrimesstruct bpf_program {
681541Srgrimes	u_int bf_len;
691541Srgrimes	struct bpf_insn *bf_insns;
701541Srgrimes};
718876Srgrimes
721541Srgrimes/*
731541Srgrimes * Struct returned by BIOCGSTATS.
741541Srgrimes */
751541Srgrimesstruct bpf_stat {
761541Srgrimes	u_int bs_recv;		/* number of packets received */
771541Srgrimes	u_int bs_drop;		/* number of packets dropped */
781541Srgrimes};
791541Srgrimes
801541Srgrimes/*
818876Srgrimes * Struct return by BIOCVERSION.  This represents the version number of
821541Srgrimes * the filter language described by the instruction encodings below.
831541Srgrimes * bpf understands a program iff kernel_major == filter_major &&
841541Srgrimes * kernel_minor >= filter_minor, that is, if the value returned by the
851541Srgrimes * running kernel has the same major number and a minor number equal
861541Srgrimes * equal to or less than the filter being downloaded.  Otherwise, the
871541Srgrimes * results are undefined, meaning an error may be returned or packets
881541Srgrimes * may be accepted haphazardly.
891541Srgrimes * It has nothing to do with the source code version.
901541Srgrimes */
911541Srgrimesstruct bpf_version {
921541Srgrimes	u_short bv_major;
931541Srgrimes	u_short bv_minor;
941541Srgrimes};
9517679Spst/* Current version number of filter architecture. */
961541Srgrimes#define BPF_MAJOR_VERSION 1
971541Srgrimes#define BPF_MINOR_VERSION 1
981541Srgrimes
991541Srgrimes#define	BIOCGBLEN	_IOR('B',102, u_int)
1001541Srgrimes#define	BIOCSBLEN	_IOWR('B',102, u_int)
1011541Srgrimes#define	BIOCSETF	_IOW('B',103, struct bpf_program)
1021541Srgrimes#define	BIOCFLUSH	_IO('B',104)
1031541Srgrimes#define BIOCPROMISC	_IO('B',105)
1041541Srgrimes#define	BIOCGDLT	_IOR('B',106, u_int)
1051541Srgrimes#define BIOCGETIF	_IOR('B',107, struct ifreq)
1061541Srgrimes#define BIOCSETIF	_IOW('B',108, struct ifreq)
1071541Srgrimes#define BIOCSRTIMEOUT	_IOW('B',109, struct timeval)
1081541Srgrimes#define BIOCGRTIMEOUT	_IOR('B',110, struct timeval)
1091541Srgrimes#define BIOCGSTATS	_IOR('B',111, struct bpf_stat)
1101541Srgrimes#define BIOCIMMEDIATE	_IOW('B',112, u_int)
1111541Srgrimes#define BIOCVERSION	_IOR('B',113, struct bpf_version)
1129235Spst#define BIOCGRSIG	_IOR('B',114, u_int)
1139235Spst#define BIOCSRSIG	_IOW('B',115, u_int)
11452248Smsmith#define BIOCGHDRCMPLT	_IOR('B',116, u_int)
11552248Smsmith#define BIOCSHDRCMPLT	_IOW('B',117, u_int)
1161541Srgrimes
1171541Srgrimes/*
1181541Srgrimes * Structure prepended to each packet.
1191541Srgrimes */
1201541Srgrimesstruct bpf_hdr {
1211541Srgrimes	struct timeval	bh_tstamp;	/* time stamp */
12217679Spst	bpf_u_int32	bh_caplen;	/* length of captured portion */
12317679Spst	bpf_u_int32	bh_datalen;	/* original length of packet */
1241541Srgrimes	u_short		bh_hdrlen;	/* length of bpf header (this struct
1251541Srgrimes					   plus alignment padding) */
1261541Srgrimes};
1271541Srgrimes/*
1281541Srgrimes * Because the structure above is not a multiple of 4 bytes, some compilers
1291541Srgrimes * will insist on inserting padding; hence, sizeof(struct bpf_hdr) won't work.
1301541Srgrimes * Only the kernel needs to know about it; applications use bh_hdrlen.
1311541Srgrimes */
1321541Srgrimes#ifdef KERNEL
13337619Sbde#define	SIZEOF_BPF_HDR	(sizeof(struct bpf_hdr) <= 20 ? 18 : \
13437619Sbde    sizeof(struct bpf_hdr))
1351541Srgrimes#endif
1361541Srgrimes
1371541Srgrimes/*
1381541Srgrimes * Data-link level type codes.
1391541Srgrimes */
1401541Srgrimes#define DLT_NULL	0	/* no link-layer encapsulation */
1411541Srgrimes#define DLT_EN10MB	1	/* Ethernet (10Mb) */
1421541Srgrimes#define DLT_EN3MB	2	/* Experimental Ethernet (3Mb) */
1431541Srgrimes#define DLT_AX25	3	/* Amateur Radio AX.25 */
1441541Srgrimes#define DLT_PRONET	4	/* Proteon ProNET Token Ring */
1451541Srgrimes#define DLT_CHAOS	5	/* Chaos */
1461541Srgrimes#define DLT_IEEE802	6	/* IEEE 802 Networks */
1471541Srgrimes#define DLT_ARCNET	7	/* ARCNET */
1481541Srgrimes#define DLT_SLIP	8	/* Serial Line IP */
1491541Srgrimes#define DLT_PPP		9	/* Point-to-point Protocol */
1501541Srgrimes#define DLT_FDDI	10	/* FDDI */
15117679Spst#define DLT_ATM_RFC1483	11	/* LLC/SNAP encapsulated atm */
15238423Sache#define DLT_RAW		12	/* raw IP */
15339296Sfenner#define DLT_SLIP_BSDOS	13	/* BSD/OS Serial Line IP */
15439296Sfenner#define DLT_PPP_BSDOS	14	/* BSD/OS Point-to-point Protocol */
1551541Srgrimes
1561541Srgrimes/*
15713765Smpp * The instruction encodings.
1581541Srgrimes */
1591541Srgrimes/* instruction classes */
1601541Srgrimes#define BPF_CLASS(code) ((code) & 0x07)
1611541Srgrimes#define		BPF_LD		0x00
1621541Srgrimes#define		BPF_LDX		0x01
1631541Srgrimes#define		BPF_ST		0x02
1641541Srgrimes#define		BPF_STX		0x03
1651541Srgrimes#define		BPF_ALU		0x04
1661541Srgrimes#define		BPF_JMP		0x05
1671541Srgrimes#define		BPF_RET		0x06
1681541Srgrimes#define		BPF_MISC	0x07
1691541Srgrimes
1701541Srgrimes/* ld/ldx fields */
1711541Srgrimes#define BPF_SIZE(code)	((code) & 0x18)
1721541Srgrimes#define		BPF_W		0x00
1731541Srgrimes#define		BPF_H		0x08
1741541Srgrimes#define		BPF_B		0x10
1751541Srgrimes#define BPF_MODE(code)	((code) & 0xe0)
1761541Srgrimes#define		BPF_IMM 	0x00
1771541Srgrimes#define		BPF_ABS		0x20
1781541Srgrimes#define		BPF_IND		0x40
1791541Srgrimes#define		BPF_MEM		0x60
1801541Srgrimes#define		BPF_LEN		0x80
1811541Srgrimes#define		BPF_MSH		0xa0
1821541Srgrimes
1831541Srgrimes/* alu/jmp fields */
1841541Srgrimes#define BPF_OP(code)	((code) & 0xf0)
1851541Srgrimes#define		BPF_ADD		0x00
1861541Srgrimes#define		BPF_SUB		0x10
1871541Srgrimes#define		BPF_MUL		0x20
1881541Srgrimes#define		BPF_DIV		0x30
1891541Srgrimes#define		BPF_OR		0x40
1901541Srgrimes#define		BPF_AND		0x50
1911541Srgrimes#define		BPF_LSH		0x60
1921541Srgrimes#define		BPF_RSH		0x70
1931541Srgrimes#define		BPF_NEG		0x80
1941541Srgrimes#define		BPF_JA		0x00
1951541Srgrimes#define		BPF_JEQ		0x10
1961541Srgrimes#define		BPF_JGT		0x20
1971541Srgrimes#define		BPF_JGE		0x30
1981541Srgrimes#define		BPF_JSET	0x40
1991541Srgrimes#define BPF_SRC(code)	((code) & 0x08)
2001541Srgrimes#define		BPF_K		0x00
2011541Srgrimes#define		BPF_X		0x08
2021541Srgrimes
2031541Srgrimes/* ret - BPF_K and BPF_X also apply */
2041541Srgrimes#define BPF_RVAL(code)	((code) & 0x18)
2051541Srgrimes#define		BPF_A		0x10
2061541Srgrimes
2071541Srgrimes/* misc */
2081541Srgrimes#define BPF_MISCOP(code) ((code) & 0xf8)
2091541Srgrimes#define		BPF_TAX		0x00
2101541Srgrimes#define		BPF_TXA		0x80
2111541Srgrimes
2121541Srgrimes/*
2131541Srgrimes * The instruction data structure.
2141541Srgrimes */
2151541Srgrimesstruct bpf_insn {
21617679Spst	u_short		code;
21717679Spst	u_char		jt;
21817679Spst	u_char		jf;
21917679Spst	bpf_u_int32	k;
2201541Srgrimes};
2211541Srgrimes
2221541Srgrimes/*
2231541Srgrimes * Macros for insn array initializers.
2241541Srgrimes */
2251541Srgrimes#define BPF_STMT(code, k) { (u_short)(code), 0, 0, k }
2261541Srgrimes#define BPF_JUMP(code, k, jt, jf) { (u_short)(code), jt, jf, k }
2271541Srgrimes
2281541Srgrimes#ifdef KERNEL
2291541Srgrimesint	 bpf_validate __P((struct bpf_insn *, int));
23013937Swollmanvoid	 bpf_tap __P((struct ifnet *, u_char *, u_int));
23113937Swollmanvoid	 bpf_mtap __P((struct ifnet *, struct mbuf *));
23213937Swollmanvoid	 bpfattach __P((struct ifnet *, u_int, u_int));
2331541Srgrimesvoid	 bpfilterattach __P((int));
2341541Srgrimesu_int	 bpf_filter __P((struct bpf_insn *, u_char *, u_int, u_int));
2351541Srgrimes#endif
2361541Srgrimes
2371541Srgrimes/*
2381541Srgrimes * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST).
2391541Srgrimes */
2401541Srgrimes#define BPF_MEMWORDS 16
2411541Srgrimes
2422168Spaul#endif
243