xref: /csrg-svn/sys/net/bpf.h (revision 49201)
1 /*
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  *
21  * @(#) $Header: bpf.h,v 1.20 91/04/24 22:06:24 mccanne Locked $ (LBL)
22  *
23  * This code is derived from the Stanford/CMU enet packet filter,
24  * (net/enet.h) distributed with 4.3BSD Unix.
25  */
26 
27 /*
28  * Alignment macros.  BPF_WORDALIGN rounds up to the next
29  * even multiple of BPF_ALIGNMENT.
30  */
31 #define BPF_ALIGNMENT sizeof(long)
32 #define BPF_WORDALIGN(x) (((x)+(BPF_ALIGNMENT-1))&~(BPF_ALIGNMENT-1))
33 
34 #define BPF_MAXINSNS 512
35 #define BPF_MAXBUFSIZE 0x8000
36 
37 /*
38  *  Structure for BIOCSETF.
39  */
40 struct bpf_program {
41 	u_int bf_len;
42 	struct bpf_insn *bf_insns;
43 };
44 
45 /*
46  * Struct returned by BIOCGSTATS.
47  */
48 struct bpf_stat {
49 	u_int bs_recv;		/* number of packets received */
50 	u_int bs_drop;		/* number of packets dropped */
51 };
52 
53 /*
54  * BPF ioctls
55  *
56  * The first set is for compatibility with Sun's pcc style
57  * header files.  If your using gcc, we assume that you
58  * have run fixincludes so the latter set should work.
59  */
60 #if defined(sun) && !defined(__GNUC__)
61 #define	BIOCGFLEN	_IOR(B,101, u_int)
62 #define	BIOCGBLEN	_IOR(B,102, u_int)
63 #define	BIOCSETF	_IOW(B,103, struct bpf_program)
64 #define	BIOCFLUSH	_IO(B,104)
65 #define BIOCPROMISC	_IO(B,105)
66 #define	BIOCGDLT	_IOR(B,106, u_int)
67 #define BIOCGETIF	_IOR(B,107, struct ifreq)
68 #define BIOCSETIF	_IOW(B,108, struct ifreq)
69 #define BIOCSRTIMEOUT	_IOW(B,109, struct timeval)
70 #define BIOCGRTIMEOUT	_IOR(B,110, struct timeval)
71 #define BIOCGSTATS	_IOR(B,111, struct bpf_stat)
72 #define BIOCIMMEDIATE	_IOW(B,112, u_int)
73 #else
74 #define	BIOCGFLEN	_IOR('B',101, u_int)
75 #define	BIOCGBLEN	_IOR('B',102, u_int)
76 #define	BIOCSETF	_IOW('B',103, struct bpf_program)
77 #define	BIOCFLUSH	_IO('B',104)
78 #define BIOCPROMISC	_IO('B',105)
79 #define	BIOCGDLT	_IOR('B',106, u_int)
80 #define BIOCGETIF	_IOR('B',107, struct ifreq)
81 #define BIOCSETIF	_IOW('B',108, struct ifreq)
82 #define BIOCSRTIMEOUT	_IOW('B',109, struct timeval)
83 #define BIOCGRTIMEOUT	_IOR('B',110, struct timeval)
84 #define BIOCGSTATS	_IOR('B',111, struct bpf_stat)
85 #define BIOCIMMEDIATE	_IOW('B',112, u_int)
86 #endif
87 
88 /*
89  * Structure prepended to each packet.
90  */
91 struct bpf_hdr {
92 	struct timeval	bh_tstamp;	/* time stamp */
93 	u_long		bh_caplen;	/* length of captured portion */
94 	u_long		bh_datalen;	/* original length of packet */
95 	u_short		bh_hdrlen;	/* length of bpf header (this struct
96 					   plus alignment padding) */
97 };
98 /*
99  * Because the structure above is not a multiple of 4 bytes, some compilers
100  * will insist on inserting padding; hence, sizeof(struct bpf_hdr) won't work.
101  * Only the kernel needs to know about it; applications use bh_hdrlen.
102  */
103 #ifdef KERNEL
104 #define SIZEOF_BPF_HDR 18
105 #endif
106 
107 /*
108  * Data-link level type codes.
109  * Currently, only DLT_EN10MB and DLT_SLIP are supported.
110  */
111 #define DLT_EN10MB	1	/* Ethernet (10Mb) */
112 #define DLT_EN3MB	2	/* Experimental Ethernet (3Mb) */
113 #define DLT_AX25	3	/* Amateur Radio AX.25 */
114 #define DLT_PRONET	4	/* Proteon ProNET Token Ring */
115 #define DLT_CHAOS	5	/* Chaos */
116 #define DLT_IEEE802	6	/* IEEE 802 Networks */
117 #define DLT_ARCNET	7	/* ARCNET */
118 #define DLT_SLIP	8	/* Serial Line IP */
119 #define DLT_PPP		9	/* Point-to-point Protocol */
120 #define DLT_FDDI	10	/* FDDI */
121 
122 /*
123  * The instruction encondings.
124  */
125 /* classes <2:0> */
126 #define BPF_CLASS(code) ((code) & 0x07)
127 #define		BPF_LD		0x00
128 #define		BPF_LDX		0x01
129 #define		BPF_ST		0x02
130 #define		BPF_STX		0x03
131 #define		BPF_ALU		0x04
132 #define		BPF_JMP		0x05
133 #define		BPF_RET		0x06
134 #define		BPF_MISC	0x07
135 
136 /* ld/ldx fields */
137 #define BPF_SIZE(code)	((code) & 0x18)
138 #define		BPF_W		0x00
139 #define		BPF_H		0x08
140 #define		BPF_B		0x10
141 #define BPF_MODE(code)	((code) & 0xe0)
142 #define		BPF_IMM 	0x00
143 #define		BPF_ABS		0x20
144 #define		BPF_IND		0x40
145 #define		BPF_MEM		0x60
146 #define		BPF_LEN		0x80
147 #define		BPF_MSH		0xa0
148 
149 /* alu/jmp fields */
150 #define BPF_OP(code)	((code) & 0xf0)
151 #define		BPF_ADD		0x00
152 #define		BPF_SUB		0x10
153 #define		BPF_MUL		0x20
154 #define		BPF_DIV		0x30
155 #define		BPF_OR		0x40
156 #define		BPF_AND		0x50
157 #define		BPF_LSH		0x60
158 #define		BPF_RSH		0x70
159 #define		BPF_NEG		0x80
160 #define		BPF_JA		0x00
161 #define		BPF_JEQ		0x10
162 #define		BPF_JGT		0x20
163 #define		BPF_JGE		0x30
164 #define		BPF_JSET	0x40
165 #define BPF_SRC(code)	((code) & 0x08)
166 #define		BPF_K		0x00
167 #define		BPF_X		0x08
168 
169 /* ret - BPF_K and BPF_X also apply */
170 #define BPF_RVAL(code)	((code) & 0x18)
171 #define		BPF_A		0x10
172 
173 /* misc */
174 #define BPF_MISCOP(code) ((code) & 0xf8)
175 #define		BPF_TAX		0x00
176 #define		BPF_TXA		0x80
177 
178 /*
179  * The instruction data structure.
180  */
181 struct bpf_insn {
182 	u_short	code;
183 	u_char 	jt;
184 	u_char 	jf;
185 	long	k;
186 };
187 
188 /*
189  * Macros for insn array initializers.
190  */
191 #define BPF_STMT(code, k) { (u_short)(code), 0, 0, k }
192 #define BPF_JUMP(code, k, jt, jf) { (u_short)(code), jt, jf, k }
193 
194 #ifdef KERNEL
195 extern u_int bpf_filter();
196 extern void bpfattach();
197 extern void bpf_tap();
198 extern void bpf_mtap();
199 #endif
200 
201 /*
202  * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST).
203  */
204 #define BPF_MEMWORDS 16
205