bpf.h revision 39296
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 *
4139296Sfenner * $Id: bpf.h,v 1.14 1998/08/18 10:13:11 ache Exp $
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 */
5717679Spst#define BPF_ALIGNMENT sizeof(bpf_int32)
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)
1141541Srgrimes
1151541Srgrimes/*
1161541Srgrimes * Structure prepended to each packet.
1171541Srgrimes */
1181541Srgrimesstruct bpf_hdr {
1191541Srgrimes	struct timeval	bh_tstamp;	/* time stamp */
12017679Spst	bpf_u_int32	bh_caplen;	/* length of captured portion */
12117679Spst	bpf_u_int32	bh_datalen;	/* original length of packet */
1221541Srgrimes	u_short		bh_hdrlen;	/* length of bpf header (this struct
1231541Srgrimes					   plus alignment padding) */
1241541Srgrimes};
1251541Srgrimes/*
1261541Srgrimes * Because the structure above is not a multiple of 4 bytes, some compilers
1271541Srgrimes * will insist on inserting padding; hence, sizeof(struct bpf_hdr) won't work.
1281541Srgrimes * Only the kernel needs to know about it; applications use bh_hdrlen.
1291541Srgrimes */
1301541Srgrimes#ifdef KERNEL
13137619Sbde#define	SIZEOF_BPF_HDR	(sizeof(struct bpf_hdr) <= 20 ? 18 : \
13237619Sbde    sizeof(struct bpf_hdr))
1331541Srgrimes#endif
1341541Srgrimes
1351541Srgrimes/*
1361541Srgrimes * Data-link level type codes.
1371541Srgrimes */
1381541Srgrimes#define DLT_NULL	0	/* no link-layer encapsulation */
1391541Srgrimes#define DLT_EN10MB	1	/* Ethernet (10Mb) */
1401541Srgrimes#define DLT_EN3MB	2	/* Experimental Ethernet (3Mb) */
1411541Srgrimes#define DLT_AX25	3	/* Amateur Radio AX.25 */
1421541Srgrimes#define DLT_PRONET	4	/* Proteon ProNET Token Ring */
1431541Srgrimes#define DLT_CHAOS	5	/* Chaos */
1441541Srgrimes#define DLT_IEEE802	6	/* IEEE 802 Networks */
1451541Srgrimes#define DLT_ARCNET	7	/* ARCNET */
1461541Srgrimes#define DLT_SLIP	8	/* Serial Line IP */
1471541Srgrimes#define DLT_PPP		9	/* Point-to-point Protocol */
1481541Srgrimes#define DLT_FDDI	10	/* FDDI */
14917679Spst#define DLT_ATM_RFC1483	11	/* LLC/SNAP encapsulated atm */
15038423Sache#define DLT_RAW		12	/* raw IP */
15139296Sfenner#define DLT_SLIP_BSDOS	13	/* BSD/OS Serial Line IP */
15239296Sfenner#define DLT_PPP_BSDOS	14	/* BSD/OS Point-to-point Protocol */
1531541Srgrimes
1541541Srgrimes/*
15513765Smpp * The instruction encodings.
1561541Srgrimes */
1571541Srgrimes/* instruction classes */
1581541Srgrimes#define BPF_CLASS(code) ((code) & 0x07)
1591541Srgrimes#define		BPF_LD		0x00
1601541Srgrimes#define		BPF_LDX		0x01
1611541Srgrimes#define		BPF_ST		0x02
1621541Srgrimes#define		BPF_STX		0x03
1631541Srgrimes#define		BPF_ALU		0x04
1641541Srgrimes#define		BPF_JMP		0x05
1651541Srgrimes#define		BPF_RET		0x06
1661541Srgrimes#define		BPF_MISC	0x07
1671541Srgrimes
1681541Srgrimes/* ld/ldx fields */
1691541Srgrimes#define BPF_SIZE(code)	((code) & 0x18)
1701541Srgrimes#define		BPF_W		0x00
1711541Srgrimes#define		BPF_H		0x08
1721541Srgrimes#define		BPF_B		0x10
1731541Srgrimes#define BPF_MODE(code)	((code) & 0xe0)
1741541Srgrimes#define		BPF_IMM 	0x00
1751541Srgrimes#define		BPF_ABS		0x20
1761541Srgrimes#define		BPF_IND		0x40
1771541Srgrimes#define		BPF_MEM		0x60
1781541Srgrimes#define		BPF_LEN		0x80
1791541Srgrimes#define		BPF_MSH		0xa0
1801541Srgrimes
1811541Srgrimes/* alu/jmp fields */
1821541Srgrimes#define BPF_OP(code)	((code) & 0xf0)
1831541Srgrimes#define		BPF_ADD		0x00
1841541Srgrimes#define		BPF_SUB		0x10
1851541Srgrimes#define		BPF_MUL		0x20
1861541Srgrimes#define		BPF_DIV		0x30
1871541Srgrimes#define		BPF_OR		0x40
1881541Srgrimes#define		BPF_AND		0x50
1891541Srgrimes#define		BPF_LSH		0x60
1901541Srgrimes#define		BPF_RSH		0x70
1911541Srgrimes#define		BPF_NEG		0x80
1921541Srgrimes#define		BPF_JA		0x00
1931541Srgrimes#define		BPF_JEQ		0x10
1941541Srgrimes#define		BPF_JGT		0x20
1951541Srgrimes#define		BPF_JGE		0x30
1961541Srgrimes#define		BPF_JSET	0x40
1971541Srgrimes#define BPF_SRC(code)	((code) & 0x08)
1981541Srgrimes#define		BPF_K		0x00
1991541Srgrimes#define		BPF_X		0x08
2001541Srgrimes
2011541Srgrimes/* ret - BPF_K and BPF_X also apply */
2021541Srgrimes#define BPF_RVAL(code)	((code) & 0x18)
2031541Srgrimes#define		BPF_A		0x10
2041541Srgrimes
2051541Srgrimes/* misc */
2061541Srgrimes#define BPF_MISCOP(code) ((code) & 0xf8)
2071541Srgrimes#define		BPF_TAX		0x00
2081541Srgrimes#define		BPF_TXA		0x80
2091541Srgrimes
2101541Srgrimes/*
2111541Srgrimes * The instruction data structure.
2121541Srgrimes */
2131541Srgrimesstruct bpf_insn {
21417679Spst	u_short		code;
21517679Spst	u_char		jt;
21617679Spst	u_char		jf;
21717679Spst	bpf_u_int32	k;
2181541Srgrimes};
2191541Srgrimes
2201541Srgrimes/*
2211541Srgrimes * Macros for insn array initializers.
2221541Srgrimes */
2231541Srgrimes#define BPF_STMT(code, k) { (u_short)(code), 0, 0, k }
2241541Srgrimes#define BPF_JUMP(code, k, jt, jf) { (u_short)(code), jt, jf, k }
2251541Srgrimes
2261541Srgrimes#ifdef KERNEL
2271541Srgrimesint	 bpf_validate __P((struct bpf_insn *, int));
22813937Swollmanvoid	 bpf_tap __P((struct ifnet *, u_char *, u_int));
22913937Swollmanvoid	 bpf_mtap __P((struct ifnet *, struct mbuf *));
23013937Swollmanvoid	 bpfattach __P((struct ifnet *, u_int, u_int));
2311541Srgrimesvoid	 bpfilterattach __P((int));
2321541Srgrimesu_int	 bpf_filter __P((struct bpf_insn *, u_char *, u_int, u_int));
2331541Srgrimes#endif
2341541Srgrimes
2351541Srgrimes/*
2361541Srgrimes * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST).
2371541Srgrimes */
2381541Srgrimes#define BPF_MEMWORDS 16
2391541Srgrimes
2402168Spaul#endif
241