bpf.h revision 109580
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 109580 2003-01-20 19:08:46Z sam $
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
6156057Sphk#define BPF_MAXBUFSIZE 0x80000
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)
11658192Srwatson#define BIOCGSEESENT	_IOR('B',118, u_int)
11758192Srwatson#define BIOCSSEESENT	_IOW('B',119, u_int)
118109580Ssam#define	BIOCSDLT	_IOW('B',120, u_int)
119109580Ssam#define	BIOCGDLTLIST	_IOWR('B',121, struct bpf_dltlist)
1201541Srgrimes
1211541Srgrimes/*
1221541Srgrimes * Structure prepended to each packet.
1231541Srgrimes */
1241541Srgrimesstruct bpf_hdr {
1251541Srgrimes	struct timeval	bh_tstamp;	/* time stamp */
12617679Spst	bpf_u_int32	bh_caplen;	/* length of captured portion */
12717679Spst	bpf_u_int32	bh_datalen;	/* original length of packet */
1281541Srgrimes	u_short		bh_hdrlen;	/* length of bpf header (this struct
1291541Srgrimes					   plus alignment padding) */
1301541Srgrimes};
1311541Srgrimes/*
1321541Srgrimes * Because the structure above is not a multiple of 4 bytes, some compilers
1331541Srgrimes * will insist on inserting padding; hence, sizeof(struct bpf_hdr) won't work.
1341541Srgrimes * Only the kernel needs to know about it; applications use bh_hdrlen.
1351541Srgrimes */
13655205Speter#ifdef _KERNEL
13737619Sbde#define	SIZEOF_BPF_HDR	(sizeof(struct bpf_hdr) <= 20 ? 18 : \
13837619Sbde    sizeof(struct bpf_hdr))
1391541Srgrimes#endif
1401541Srgrimes
1411541Srgrimes/*
1421541Srgrimes * Data-link level type codes.
1431541Srgrimes */
1441541Srgrimes#define DLT_NULL	0	/* no link-layer encapsulation */
1451541Srgrimes#define DLT_EN10MB	1	/* Ethernet (10Mb) */
1461541Srgrimes#define DLT_EN3MB	2	/* Experimental Ethernet (3Mb) */
1471541Srgrimes#define DLT_AX25	3	/* Amateur Radio AX.25 */
1481541Srgrimes#define DLT_PRONET	4	/* Proteon ProNET Token Ring */
1491541Srgrimes#define DLT_CHAOS	5	/* Chaos */
1501541Srgrimes#define DLT_IEEE802	6	/* IEEE 802 Networks */
1511541Srgrimes#define DLT_ARCNET	7	/* ARCNET */
1521541Srgrimes#define DLT_SLIP	8	/* Serial Line IP */
1531541Srgrimes#define DLT_PPP		9	/* Point-to-point Protocol */
1541541Srgrimes#define DLT_FDDI	10	/* FDDI */
15517679Spst#define DLT_ATM_RFC1483	11	/* LLC/SNAP encapsulated atm */
15638423Sache#define DLT_RAW		12	/* raw IP */
1571541Srgrimes
1581541Srgrimes/*
15980767Sfenner * These are values from BSD/OS's "bpf.h".
16080767Sfenner * These are not the same as the values from the traditional libpcap
16180767Sfenner * "bpf.h"; however, these values shouldn't be generated by any
16280767Sfenner * OS other than BSD/OS, so the correct values to use here are the
16380767Sfenner * BSD/OS values.
16480767Sfenner *
16580767Sfenner * Platforms that have already assigned these values to other
16680767Sfenner * DLT_ codes, however, should give these codes the values
16780767Sfenner * from that platform, so that programs that use these codes will
16880767Sfenner * continue to compile - even though they won't correctly read
16980767Sfenner * files of these types.
17080767Sfenner */
17180767Sfenner#define DLT_SLIP_BSDOS	15	/* BSD/OS Serial Line IP */
17280767Sfenner#define DLT_PPP_BSDOS	16	/* BSD/OS Point-to-point Protocol */
17380767Sfenner
17480767Sfenner#define DLT_ATM_CLIP	19	/* Linux Classical-IP over ATM */
17580767Sfenner
17680767Sfenner/*
17798540Sfenner * These values are defined by NetBSD; other platforms should refrain from
17898540Sfenner * using them for other purposes, so that NetBSD savefiles with link
17998540Sfenner * types of 50 or 51 can be read as this type on all platforms.
18080767Sfenner */
18180767Sfenner#define DLT_PPP_SERIAL	50	/* PPP over serial with HDLC encapsulation */
18298540Sfenner#define DLT_PPP_ETHER	51	/* PPP over Ethernet */
18380767Sfenner
18480767Sfenner/*
18580767Sfenner * This value was defined by libpcap 0.5; platforms that have defined
18680767Sfenner * it with a different value should define it here with that value -
18780767Sfenner * a link type of 104 in a save file will be mapped to DLT_C_HDLC,
18880767Sfenner * whatever value that happens to be, so programs will correctly
18980767Sfenner * handle files with that link type regardless of the value of
19080767Sfenner * DLT_C_HDLC.
19180767Sfenner *
19280767Sfenner * The name DLT_C_HDLC was used by BSD/OS; we use that name for source
19380767Sfenner * compatibility with programs written for BSD/OS.
19480767Sfenner *
19580767Sfenner * libpcap 0.5 defined it as DLT_CHDLC; we define DLT_CHDLC as well,
19680767Sfenner * for source compatibility with programs written for libpcap 0.5.
19780767Sfenner */
19880767Sfenner#define DLT_C_HDLC	104	/* Cisco HDLC */
19980767Sfenner#define DLT_CHDLC	DLT_C_HDLC
20080767Sfenner
20180767Sfenner#define DLT_IEEE802_11	105	/* IEEE 802.11 wireless */
20280767Sfenner
20380767Sfenner/*
20480767Sfenner * Values between 106 and 107 are used in capture file headers as
20580767Sfenner * link-layer types corresponding to DLT_ types that might differ
20680767Sfenner * between platforms; don't use those values for new DLT_ new types.
20780767Sfenner */
20880767Sfenner
20980767Sfenner/*
21080767Sfenner * OpenBSD DLT_LOOP, for loopback devices; it's like DLT_NULL, except
21180767Sfenner * that the AF_ type in the link-layer header is in network byte order.
21280767Sfenner *
21380767Sfenner * OpenBSD defines it as 12, but that collides with DLT_RAW, so we
21480767Sfenner * define it as 108 here.  If OpenBSD picks up this file, it should
21580767Sfenner * define DLT_LOOP as 12 in its version, as per the comment above -
21698540Sfenner * and should not use 108 as a DLT_ value.
21780767Sfenner */
21880767Sfenner#define DLT_LOOP	108
21980767Sfenner
22080767Sfenner/*
22180767Sfenner * Values between 109 and 112 are used in capture file headers as
22280767Sfenner * link-layer types corresponding to DLT_ types that might differ
22380767Sfenner * between platforms; don't use those values for new DLT_ new types.
22480767Sfenner */
22580767Sfenner
22680767Sfenner/*
22780767Sfenner * This is for Linux cooked sockets.
22880767Sfenner */
22980767Sfenner#define DLT_LINUX_SLL	113
23080767Sfenner
23180767Sfenner/*
23298540Sfenner * Apple LocalTalk hardware.
23398540Sfenner */
23498540Sfenner#define DLT_LTALK	114
23598540Sfenner
23698540Sfenner/*
23798540Sfenner * Acorn Econet.
23898540Sfenner */
23998540Sfenner#define DLT_ECONET	115
24098540Sfenner
24198540Sfenner/*
24298540Sfenner * Reserved for use with OpenBSD ipfilter.
24398540Sfenner */
24498540Sfenner#define DLT_IPFILTER	116
24598540Sfenner
24698540Sfenner/*
24798540Sfenner * Reserved for use in capture-file headers as a link-layer type
24898540Sfenner * corresponding to OpenBSD DLT_PFLOG; DLT_PFLOG is 17 in OpenBSD,
24998540Sfenner * but that's DLT_LANE8023 in SuSE 6.3, so we can't use 17 for it
25098540Sfenner * in capture-file headers.
25198540Sfenner */
25298540Sfenner#define DLT_PFLOG	117
25398540Sfenner
25498540Sfenner/*
25598540Sfenner * Registered for Cisco-internal use.
25698540Sfenner */
25798540Sfenner#define DLT_CISCO_IOS	118
25898540Sfenner
25998540Sfenner/*
26098540Sfenner * Reserved for 802.11 cards using the Prism II chips, with a link-layer
26198540Sfenner * header including Prism monitor mode information plus an 802.11
26298540Sfenner * header.
26398540Sfenner */
26498540Sfenner#define DLT_PRISM_HEADER	119
26598540Sfenner
26698540Sfenner/*
26798540Sfenner * Reserved for Aironet 802.11 cards, with an Aironet link-layer header
26898540Sfenner * (see Doug Ambrisko's FreeBSD patches).
26998540Sfenner */
27098540Sfenner#define DLT_AIRONET_HEADER	120
27198540Sfenner
27298540Sfenner/*
27313765Smpp * The instruction encodings.
2741541Srgrimes */
2751541Srgrimes/* instruction classes */
2761541Srgrimes#define BPF_CLASS(code) ((code) & 0x07)
2771541Srgrimes#define		BPF_LD		0x00
2781541Srgrimes#define		BPF_LDX		0x01
2791541Srgrimes#define		BPF_ST		0x02
2801541Srgrimes#define		BPF_STX		0x03
2811541Srgrimes#define		BPF_ALU		0x04
2821541Srgrimes#define		BPF_JMP		0x05
2831541Srgrimes#define		BPF_RET		0x06
2841541Srgrimes#define		BPF_MISC	0x07
2851541Srgrimes
2861541Srgrimes/* ld/ldx fields */
2871541Srgrimes#define BPF_SIZE(code)	((code) & 0x18)
2881541Srgrimes#define		BPF_W		0x00
2891541Srgrimes#define		BPF_H		0x08
2901541Srgrimes#define		BPF_B		0x10
2911541Srgrimes#define BPF_MODE(code)	((code) & 0xe0)
2921541Srgrimes#define		BPF_IMM 	0x00
2931541Srgrimes#define		BPF_ABS		0x20
2941541Srgrimes#define		BPF_IND		0x40
2951541Srgrimes#define		BPF_MEM		0x60
2961541Srgrimes#define		BPF_LEN		0x80
2971541Srgrimes#define		BPF_MSH		0xa0
2981541Srgrimes
2991541Srgrimes/* alu/jmp fields */
3001541Srgrimes#define BPF_OP(code)	((code) & 0xf0)
3011541Srgrimes#define		BPF_ADD		0x00
3021541Srgrimes#define		BPF_SUB		0x10
3031541Srgrimes#define		BPF_MUL		0x20
3041541Srgrimes#define		BPF_DIV		0x30
3051541Srgrimes#define		BPF_OR		0x40
3061541Srgrimes#define		BPF_AND		0x50
3071541Srgrimes#define		BPF_LSH		0x60
3081541Srgrimes#define		BPF_RSH		0x70
3091541Srgrimes#define		BPF_NEG		0x80
3101541Srgrimes#define		BPF_JA		0x00
3111541Srgrimes#define		BPF_JEQ		0x10
3121541Srgrimes#define		BPF_JGT		0x20
3131541Srgrimes#define		BPF_JGE		0x30
3141541Srgrimes#define		BPF_JSET	0x40
3151541Srgrimes#define BPF_SRC(code)	((code) & 0x08)
3161541Srgrimes#define		BPF_K		0x00
3171541Srgrimes#define		BPF_X		0x08
3181541Srgrimes
3191541Srgrimes/* ret - BPF_K and BPF_X also apply */
3201541Srgrimes#define BPF_RVAL(code)	((code) & 0x18)
3211541Srgrimes#define		BPF_A		0x10
3221541Srgrimes
3231541Srgrimes/* misc */
3241541Srgrimes#define BPF_MISCOP(code) ((code) & 0xf8)
3251541Srgrimes#define		BPF_TAX		0x00
3261541Srgrimes#define		BPF_TXA		0x80
3271541Srgrimes
3281541Srgrimes/*
3291541Srgrimes * The instruction data structure.
3301541Srgrimes */
3311541Srgrimesstruct bpf_insn {
33217679Spst	u_short		code;
33317679Spst	u_char		jt;
33417679Spst	u_char		jf;
33517679Spst	bpf_u_int32	k;
3361541Srgrimes};
3371541Srgrimes
3381541Srgrimes/*
3391541Srgrimes * Macros for insn array initializers.
3401541Srgrimes */
3411541Srgrimes#define BPF_STMT(code, k) { (u_short)(code), 0, 0, k }
3421541Srgrimes#define BPF_JUMP(code, k, jt, jf) { (u_short)(code), jt, jf, k }
3431541Srgrimes
344109580Ssam/*
345109580Ssam * Structure to retrieve available DLTs for the interface.
346109580Ssam */
347109580Ssamstruct bpf_dltlist {
348109580Ssam	u_int	bfl_len;	/* number of bfd_list array */
349109580Ssam	u_int	*bfl_list;	/* array of DLTs */
350109580Ssam};
351109580Ssam
35255205Speter#ifdef _KERNEL
353106927Ssamstruct bpf_if;
35492725Salfredint	 bpf_validate(const struct bpf_insn *, int);
355106927Ssamvoid	 bpf_tap(struct bpf_if *, u_char *, u_int);
356106927Ssamvoid	 bpf_mtap(struct bpf_if *, struct mbuf *);
35792725Salfredvoid	 bpfattach(struct ifnet *, u_int, u_int);
358106927Ssamvoid	 bpfattach2(struct ifnet *, u_int, u_int, struct bpf_if **);
35992725Salfredvoid	 bpfdetach(struct ifnet *);
36058273Srwatson
36192725Salfredvoid	 bpfilterattach(int);
36292725Salfredu_int	 bpf_filter(const struct bpf_insn *, u_char *, u_int, u_int);
363106927Ssam
364106927Ssam#define	BPF_TAP(_ifp,_pkt,_pktlen) do {				\
365106927Ssam	if ((_ifp)->if_bpf)					\
366106927Ssam		bpf_tap((_ifp)->if_bpf, (_pkt), (_pktlen));	\
367106927Ssam} while (0)
368106927Ssam#define	BPF_MTAP(_ifp,_m) do {					\
369106927Ssam	if ((_ifp)->if_bpf)					\
370106927Ssam		bpf_mtap((_ifp)->if_bpf, (_m));			\
371106927Ssam} while (0)
3721541Srgrimes#endif
3731541Srgrimes
3741541Srgrimes/*
3751541Srgrimes * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST).
3761541Srgrimes */
3771541Srgrimes#define BPF_MEMWORDS 16
3781541Srgrimes
379106927Ssam#endif /* _NET_BPF_H_ */
380