bpf.h revision 13937
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
391541Srgrimes *
4013937Swollman * $Id: bpf.h,v 1.8 1996/01/30 22:57:38 mpp Exp $
411541Srgrimes */
421541Srgrimes
432168Spaul#ifndef _NET_BPF_H_
442168Spaul#define _NET_BPF_H_
452168Spaul
461541Srgrimes/*
478876Srgrimes * Alignment macros.  BPF_WORDALIGN rounds up to the next
488876Srgrimes * even multiple of BPF_ALIGNMENT.
491541Srgrimes */
501541Srgrimes#define BPF_ALIGNMENT sizeof(long)
511541Srgrimes#define BPF_WORDALIGN(x) (((x)+(BPF_ALIGNMENT-1))&~(BPF_ALIGNMENT-1))
521541Srgrimes
531541Srgrimes#define BPF_MAXINSNS 512
541541Srgrimes#define BPF_MAXBUFSIZE 0x8000
551541Srgrimes#define BPF_MINBUFSIZE 32
561541Srgrimes
571541Srgrimes/*
581541Srgrimes *  Structure for BIOCSETF.
591541Srgrimes */
601541Srgrimesstruct bpf_program {
611541Srgrimes	u_int bf_len;
621541Srgrimes	struct bpf_insn *bf_insns;
631541Srgrimes};
648876Srgrimes
651541Srgrimes/*
661541Srgrimes * Struct returned by BIOCGSTATS.
671541Srgrimes */
681541Srgrimesstruct bpf_stat {
691541Srgrimes	u_int bs_recv;		/* number of packets received */
701541Srgrimes	u_int bs_drop;		/* number of packets dropped */
711541Srgrimes};
721541Srgrimes
731541Srgrimes/*
748876Srgrimes * Struct return by BIOCVERSION.  This represents the version number of
751541Srgrimes * the filter language described by the instruction encodings below.
761541Srgrimes * bpf understands a program iff kernel_major == filter_major &&
771541Srgrimes * kernel_minor >= filter_minor, that is, if the value returned by the
781541Srgrimes * running kernel has the same major number and a minor number equal
791541Srgrimes * equal to or less than the filter being downloaded.  Otherwise, the
801541Srgrimes * results are undefined, meaning an error may be returned or packets
811541Srgrimes * may be accepted haphazardly.
821541Srgrimes * It has nothing to do with the source code version.
831541Srgrimes */
841541Srgrimesstruct bpf_version {
851541Srgrimes	u_short bv_major;
861541Srgrimes	u_short bv_minor;
871541Srgrimes};
881541Srgrimes/* Current version number. */
891541Srgrimes#define BPF_MAJOR_VERSION 1
901541Srgrimes#define BPF_MINOR_VERSION 1
911541Srgrimes
921541Srgrimes#define	BIOCGBLEN	_IOR('B',102, u_int)
931541Srgrimes#define	BIOCSBLEN	_IOWR('B',102, u_int)
941541Srgrimes#define	BIOCSETF	_IOW('B',103, struct bpf_program)
951541Srgrimes#define	BIOCFLUSH	_IO('B',104)
961541Srgrimes#define BIOCPROMISC	_IO('B',105)
971541Srgrimes#define	BIOCGDLT	_IOR('B',106, u_int)
981541Srgrimes#define BIOCGETIF	_IOR('B',107, struct ifreq)
991541Srgrimes#define BIOCSETIF	_IOW('B',108, struct ifreq)
1001541Srgrimes#define BIOCSRTIMEOUT	_IOW('B',109, struct timeval)
1011541Srgrimes#define BIOCGRTIMEOUT	_IOR('B',110, struct timeval)
1021541Srgrimes#define BIOCGSTATS	_IOR('B',111, struct bpf_stat)
1031541Srgrimes#define BIOCIMMEDIATE	_IOW('B',112, u_int)
1041541Srgrimes#define BIOCVERSION	_IOR('B',113, struct bpf_version)
1059235Spst#define BIOCGRSIG	_IOR('B',114, u_int)
1069235Spst#define BIOCSRSIG	_IOW('B',115, u_int)
1071541Srgrimes
1081541Srgrimes/*
1091541Srgrimes * Structure prepended to each packet.
1101541Srgrimes */
1111541Srgrimesstruct bpf_hdr {
1121541Srgrimes	struct timeval	bh_tstamp;	/* time stamp */
1131541Srgrimes	u_long		bh_caplen;	/* length of captured portion */
1141541Srgrimes	u_long		bh_datalen;	/* original length of packet */
1151541Srgrimes	u_short		bh_hdrlen;	/* length of bpf header (this struct
1161541Srgrimes					   plus alignment padding) */
1171541Srgrimes};
1181541Srgrimes/*
1191541Srgrimes * Because the structure above is not a multiple of 4 bytes, some compilers
1201541Srgrimes * will insist on inserting padding; hence, sizeof(struct bpf_hdr) won't work.
1211541Srgrimes * Only the kernel needs to know about it; applications use bh_hdrlen.
1221541Srgrimes */
1231541Srgrimes#ifdef KERNEL
1241541Srgrimes#define SIZEOF_BPF_HDR 18
1251541Srgrimes#endif
1261541Srgrimes
1271541Srgrimes/*
1281541Srgrimes * Data-link level type codes.
1291541Srgrimes * Currently, only DLT_EN10MB and DLT_SLIP are supported.
1301541Srgrimes */
1311541Srgrimes#define DLT_NULL	0	/* no link-layer encapsulation */
1321541Srgrimes#define DLT_EN10MB	1	/* Ethernet (10Mb) */
1331541Srgrimes#define DLT_EN3MB	2	/* Experimental Ethernet (3Mb) */
1341541Srgrimes#define DLT_AX25	3	/* Amateur Radio AX.25 */
1351541Srgrimes#define DLT_PRONET	4	/* Proteon ProNET Token Ring */
1361541Srgrimes#define DLT_CHAOS	5	/* Chaos */
1371541Srgrimes#define DLT_IEEE802	6	/* IEEE 802 Networks */
1381541Srgrimes#define DLT_ARCNET	7	/* ARCNET */
1391541Srgrimes#define DLT_SLIP	8	/* Serial Line IP */
1401541Srgrimes#define DLT_PPP		9	/* Point-to-point Protocol */
1411541Srgrimes#define DLT_FDDI	10	/* FDDI */
1421541Srgrimes
1431541Srgrimes/*
14413765Smpp * The instruction encodings.
1451541Srgrimes */
1461541Srgrimes/* instruction classes */
1471541Srgrimes#define BPF_CLASS(code) ((code) & 0x07)
1481541Srgrimes#define		BPF_LD		0x00
1491541Srgrimes#define		BPF_LDX		0x01
1501541Srgrimes#define		BPF_ST		0x02
1511541Srgrimes#define		BPF_STX		0x03
1521541Srgrimes#define		BPF_ALU		0x04
1531541Srgrimes#define		BPF_JMP		0x05
1541541Srgrimes#define		BPF_RET		0x06
1551541Srgrimes#define		BPF_MISC	0x07
1561541Srgrimes
1571541Srgrimes/* ld/ldx fields */
1581541Srgrimes#define BPF_SIZE(code)	((code) & 0x18)
1591541Srgrimes#define		BPF_W		0x00
1601541Srgrimes#define		BPF_H		0x08
1611541Srgrimes#define		BPF_B		0x10
1621541Srgrimes#define BPF_MODE(code)	((code) & 0xe0)
1631541Srgrimes#define		BPF_IMM 	0x00
1641541Srgrimes#define		BPF_ABS		0x20
1651541Srgrimes#define		BPF_IND		0x40
1661541Srgrimes#define		BPF_MEM		0x60
1671541Srgrimes#define		BPF_LEN		0x80
1681541Srgrimes#define		BPF_MSH		0xa0
1691541Srgrimes
1701541Srgrimes/* alu/jmp fields */
1711541Srgrimes#define BPF_OP(code)	((code) & 0xf0)
1721541Srgrimes#define		BPF_ADD		0x00
1731541Srgrimes#define		BPF_SUB		0x10
1741541Srgrimes#define		BPF_MUL		0x20
1751541Srgrimes#define		BPF_DIV		0x30
1761541Srgrimes#define		BPF_OR		0x40
1771541Srgrimes#define		BPF_AND		0x50
1781541Srgrimes#define		BPF_LSH		0x60
1791541Srgrimes#define		BPF_RSH		0x70
1801541Srgrimes#define		BPF_NEG		0x80
1811541Srgrimes#define		BPF_JA		0x00
1821541Srgrimes#define		BPF_JEQ		0x10
1831541Srgrimes#define		BPF_JGT		0x20
1841541Srgrimes#define		BPF_JGE		0x30
1851541Srgrimes#define		BPF_JSET	0x40
1861541Srgrimes#define BPF_SRC(code)	((code) & 0x08)
1871541Srgrimes#define		BPF_K		0x00
1881541Srgrimes#define		BPF_X		0x08
1891541Srgrimes
1901541Srgrimes/* ret - BPF_K and BPF_X also apply */
1911541Srgrimes#define BPF_RVAL(code)	((code) & 0x18)
1921541Srgrimes#define		BPF_A		0x10
1931541Srgrimes
1941541Srgrimes/* misc */
1951541Srgrimes#define BPF_MISCOP(code) ((code) & 0xf8)
1961541Srgrimes#define		BPF_TAX		0x00
1971541Srgrimes#define		BPF_TXA		0x80
1981541Srgrimes
1991541Srgrimes/*
2001541Srgrimes * The instruction data structure.
2011541Srgrimes */
2021541Srgrimesstruct bpf_insn {
2031541Srgrimes	u_short	code;
2041541Srgrimes	u_char 	jt;
2051541Srgrimes	u_char 	jf;
2061541Srgrimes	long	k;
2071541Srgrimes};
2081541Srgrimes
2091541Srgrimes/*
2101541Srgrimes * Macros for insn array initializers.
2111541Srgrimes */
2121541Srgrimes#define BPF_STMT(code, k) { (u_short)(code), 0, 0, k }
2131541Srgrimes#define BPF_JUMP(code, k, jt, jf) { (u_short)(code), jt, jf, k }
2141541Srgrimes
2151541Srgrimes#ifdef KERNEL
2161541Srgrimesint	 bpf_validate __P((struct bpf_insn *, int));
21713937Swollmanvoid	 bpf_tap __P((struct ifnet *, u_char *, u_int));
21813937Swollmanvoid	 bpf_mtap __P((struct ifnet *, struct mbuf *));
21913937Swollmanvoid	 bpfattach __P((struct ifnet *, u_int, u_int));
2201541Srgrimesvoid	 bpfilterattach __P((int));
2211541Srgrimesu_int	 bpf_filter __P((struct bpf_insn *, u_char *, u_int, u_int));
2221541Srgrimes#endif
2231541Srgrimes
2241541Srgrimes/*
2251541Srgrimes * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST).
2261541Srgrimes */
2271541Srgrimes#define BPF_MEMWORDS 16
2281541Srgrimes
2292168Spaul#endif
230