xref: /original-bsd/sys/net/bpf.h (revision ba762ddc)
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.19 91/01/30 18:20:21 mccanne Exp $ (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	BIOCDEVP	_IOR(B,106, struct bpf_devp)
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	BIOCDEVP	_IOR('B',106, struct bpf_devp)
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  * The device parameters of a network interface.
90  */
91 struct bpf_devp {
92 	u_short	bdev_type;	/* data link layer type, codes below */
93 	u_short	bdev_hdrlen;	/* length of a hardware packet header */
94 };
95 
96 /*
97  * Structure prepended to each packet.
98  */
99 struct bpf_hdr {
100 	struct timeval	bh_tstamp;	/* time stamp */
101 	u_long		bh_caplen;	/* length of captured portion */
102 	u_long		bh_datalen;	/* original length of packet */
103 	u_short		bh_hdrlen;	/* length of bpf header (this struct
104 					   plus alignment padding) */
105 };
106 /*
107  * Because the structure above is not a multiple of 4 bytes, some compilers
108  * will insist on inserting padding; hence, sizeof(struct bpf_hdr) won't work.
109  * Only the kernel needs to know about it; applications use bh_hdrlen.
110  */
111 #ifdef KERNEL
112 #define SIZEOF_BPF_HDR 18
113 #endif
114 
115 /*
116  * Data-link level type codes.
117  * Currently, only ENDT_10MB and DLT_SLIP are supported.
118  */
119 #define DLT_EN10MB	1	/* Ethernet (10Mb) */
120 #define DLT_EN3MB	2	/* Experimental Ethernet (3Mb) */
121 #define DLT_AX25	3	/* Amateur Radio AX.25 */
122 #define DLT_PRONET	4	/* Proteon ProNET Token Ring */
123 #define DLT_CHAOS	5	/* Chaos */
124 #define DLT_IEEE802	6	/* IEEE 802 Networks */
125 #define DLT_ARCNET	7	/* ARCNET */
126 #define DLT_SLIP	8	/* Serial Line IP */
127 #define DLT_PPP		9	/* Point-to-point Protocol */
128 #define DLT_FDDI	10	/* FDDI */
129 
130 /*
131  * The opcodes are defined as an enumeration.  However, they are stored
132  * explicitly in the code array as 'u_short'.
133  */
134 enum bpf_code {
135 #define OPDEF(opcode, opstr) opcode ,
136 #include <net/bpfcodes.h>
137 #undef OPDEF
138 	/* this idea is borrowed from gcc */
139 	LAST_AND_UNUSED_ENUM
140 };
141 
142 #define BPF_NCODES ((unsigned)LAST_AND_UNUSED_ENUM)
143 #define BPF_VALIDCODE(code) ((unsigned)(code) < BPF_NCODES)
144 
145 /*
146  * The instruction data structure.
147  */
148 struct bpf_insn {
149 	u_short	code;
150 	u_char 	jt;
151 	u_char 	jf;
152 	long	k;
153 };
154 
155 /*
156  * Macros for array initializers.
157  */
158 #define BPF_STMT(code, k) { (u_short)code, 0, 0, k }
159 #define BPF_JUMP(code, k, jt, jf) { (u_short)code, jt, jf, k }
160 
161 #ifdef KERNEL
162 extern u_int bpf_filter();
163 extern void bpfattach();
164 extern void bpf_tap();
165 extern void bpf_mtap();
166 #endif
167 
168 /*
169  * These two macros are sensitive to the order in which the
170  * opcodes appear in bpfcodes.h.
171  */
172 #define BPF_ISJUMP(code) ((unsigned)(code) <= (unsigned)EQOp)
173 #define BPF_ISLEAF(code) ((unsigned)(code) >= (unsigned)RetOp)
174 
175 /*
176  * Number of scratch memory words.
177  */
178 #define BPF_MEMWORDS 16
179