xref: /csrg-svn/sys/net/bpf.h (revision 48930)
147638Smccanne /*
247638Smccanne  * Copyright (c) 1990 The Regents of the University of California.
347638Smccanne  * All rights reserved.
447638Smccanne  *
547638Smccanne  * Redistribution and use in source and binary forms, with or without
647638Smccanne  * modification, are permitted provided that: (1) source code distributions
747638Smccanne  * retain the above copyright notice and this paragraph in its entirety, (2)
847638Smccanne  * distributions including binary code include the above copyright notice and
947638Smccanne  * this paragraph in its entirety in the documentation or other materials
1047638Smccanne  * provided with the distribution, and (3) all advertising materials mentioning
1147638Smccanne  * features or use of this software display the following acknowledgement:
1247638Smccanne  * ``This product includes software developed by the University of California,
1347638Smccanne  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1447638Smccanne  * the University nor the names of its contributors may be used to endorse
1547638Smccanne  * or promote products derived from this software without specific prior
1647638Smccanne  * written permission.
1747638Smccanne  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1847638Smccanne  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1947638Smccanne  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2047638Smccanne  *
2147638Smccanne  * @(#) $Header: bpf.h,v 1.19 91/01/30 18:20:21 mccanne Exp $ (LBL)
2247638Smccanne  *
2347638Smccanne  * This code is derived from the Stanford/CMU enet packet filter,
2447638Smccanne  * (net/enet.h) distributed with 4.3BSD Unix.
2547638Smccanne  */
2647638Smccanne 
2747638Smccanne /*
2847638Smccanne  * Alignment macros.  BPF_WORDALIGN rounds up to the next
2947638Smccanne  * even multiple of BPF_ALIGNMENT.
3047638Smccanne  */
3147638Smccanne #define BPF_ALIGNMENT sizeof(long)
3247638Smccanne #define BPF_WORDALIGN(x) (((x)+(BPF_ALIGNMENT-1))&~(BPF_ALIGNMENT-1))
3347638Smccanne 
34*48930Smccanne #define BPF_MAXINSNS 512
35*48930Smccanne #define BPF_MAXBUFSIZE 0x8000
36*48930Smccanne 
3747638Smccanne /*
3847638Smccanne  *  Structure for BIOCSETF.
3947638Smccanne  */
4047638Smccanne struct bpf_program {
4147638Smccanne 	u_int bf_len;
4247638Smccanne 	struct bpf_insn *bf_insns;
4347638Smccanne };
4447638Smccanne 
4547638Smccanne /*
4647638Smccanne  * Struct returned by BIOCGSTATS.
4747638Smccanne  */
4847638Smccanne struct bpf_stat {
4947638Smccanne 	u_int bs_recv;		/* number of packets received */
5047638Smccanne 	u_int bs_drop;		/* number of packets dropped */
5147638Smccanne };
5247638Smccanne 
5347638Smccanne /*
5447638Smccanne  * BPF ioctls
5547638Smccanne  *
5647638Smccanne  * The first set is for compatibility with Sun's pcc style
5747638Smccanne  * header files.  If your using gcc, we assume that you
5847638Smccanne  * have run fixincludes so the latter set should work.
5947638Smccanne  */
6047638Smccanne #if defined(sun) && !defined(__GNUC__)
6147638Smccanne #define	BIOCGFLEN	_IOR(B,101, u_int)
6247638Smccanne #define	BIOCGBLEN	_IOR(B,102, u_int)
6347638Smccanne #define	BIOCSETF	_IOW(B,103, struct bpf_program)
6447638Smccanne #define	BIOCFLUSH	_IO(B,104)
6547638Smccanne #define BIOCPROMISC	_IO(B,105)
6647638Smccanne #define	BIOCDEVP	_IOR(B,106, struct bpf_devp)
6747638Smccanne #define BIOCGETIF	_IOR(B,107, struct ifreq)
6847638Smccanne #define BIOCSETIF	_IOW(B,108, struct ifreq)
6947638Smccanne #define BIOCSRTIMEOUT	_IOW(B,109, struct timeval)
7047638Smccanne #define BIOCGRTIMEOUT	_IOR(B,110, struct timeval)
7147638Smccanne #define BIOCGSTATS	_IOR(B,111, struct bpf_stat)
7247638Smccanne #define BIOCIMMEDIATE	_IOW(B,112, u_int)
7347638Smccanne #else
7447638Smccanne #define	BIOCGFLEN	_IOR('B',101, u_int)
7547638Smccanne #define	BIOCGBLEN	_IOR('B',102, u_int)
7647638Smccanne #define	BIOCSETF	_IOW('B',103, struct bpf_program)
7747638Smccanne #define	BIOCFLUSH	_IO('B',104)
7847638Smccanne #define BIOCPROMISC	_IO('B',105)
7947638Smccanne #define	BIOCDEVP	_IOR('B',106, struct bpf_devp)
8047638Smccanne #define BIOCGETIF	_IOR('B',107, struct ifreq)
8147638Smccanne #define BIOCSETIF	_IOW('B',108, struct ifreq)
8247638Smccanne #define BIOCSRTIMEOUT	_IOW('B',109, struct timeval)
8347638Smccanne #define BIOCGRTIMEOUT	_IOR('B',110, struct timeval)
8447638Smccanne #define BIOCGSTATS	_IOR('B',111, struct bpf_stat)
8547638Smccanne #define BIOCIMMEDIATE	_IOW('B',112, u_int)
8647638Smccanne #endif
8747638Smccanne 
8847638Smccanne /*
8947638Smccanne  * The device parameters of a network interface.
9047638Smccanne  */
9147638Smccanne struct bpf_devp {
9247638Smccanne 	u_short	bdev_type;	/* data link layer type, codes below */
9347638Smccanne 	u_short	bdev_hdrlen;	/* length of a hardware packet header */
9447638Smccanne };
9547638Smccanne 
9647638Smccanne /*
9747638Smccanne  * Structure prepended to each packet.
9847638Smccanne  */
9947638Smccanne struct bpf_hdr {
10047638Smccanne 	struct timeval	bh_tstamp;	/* time stamp */
10147638Smccanne 	u_long		bh_caplen;	/* length of captured portion */
10247638Smccanne 	u_long		bh_datalen;	/* original length of packet */
10347638Smccanne 	u_short		bh_hdrlen;	/* length of bpf header (this struct
10447638Smccanne 					   plus alignment padding) */
10547638Smccanne };
10647638Smccanne /*
10747638Smccanne  * Because the structure above is not a multiple of 4 bytes, some compilers
10847638Smccanne  * will insist on inserting padding; hence, sizeof(struct bpf_hdr) won't work.
10947638Smccanne  * Only the kernel needs to know about it; applications use bh_hdrlen.
11047638Smccanne  */
11147638Smccanne #ifdef KERNEL
11247638Smccanne #define SIZEOF_BPF_HDR 18
11347638Smccanne #endif
11447638Smccanne 
11547638Smccanne /*
11647638Smccanne  * Data-link level type codes.
11747638Smccanne  * Currently, only ENDT_10MB and DLT_SLIP are supported.
11847638Smccanne  */
11947638Smccanne #define DLT_EN10MB	1	/* Ethernet (10Mb) */
12047638Smccanne #define DLT_EN3MB	2	/* Experimental Ethernet (3Mb) */
12147638Smccanne #define DLT_AX25	3	/* Amateur Radio AX.25 */
12247638Smccanne #define DLT_PRONET	4	/* Proteon ProNET Token Ring */
12347638Smccanne #define DLT_CHAOS	5	/* Chaos */
12447638Smccanne #define DLT_IEEE802	6	/* IEEE 802 Networks */
12547638Smccanne #define DLT_ARCNET	7	/* ARCNET */
12647638Smccanne #define DLT_SLIP	8	/* Serial Line IP */
12747638Smccanne #define DLT_PPP		9	/* Point-to-point Protocol */
12847638Smccanne #define DLT_FDDI	10	/* FDDI */
12947638Smccanne 
13047638Smccanne /*
13147638Smccanne  * The opcodes are defined as an enumeration.  However, they are stored
13247638Smccanne  * explicitly in the code array as 'u_short'.
13347638Smccanne  */
13447638Smccanne enum bpf_code {
13547638Smccanne #define OPDEF(opcode, opstr) opcode ,
13647638Smccanne #include <net/bpfcodes.h>
13747638Smccanne #undef OPDEF
13847638Smccanne 	/* this idea is borrowed from gcc */
13947638Smccanne 	LAST_AND_UNUSED_ENUM
14047638Smccanne };
14147638Smccanne 
14247638Smccanne #define BPF_NCODES ((unsigned)LAST_AND_UNUSED_ENUM)
14347638Smccanne #define BPF_VALIDCODE(code) ((unsigned)(code) < BPF_NCODES)
14447638Smccanne 
14547638Smccanne /*
14647638Smccanne  * The instruction data structure.
14747638Smccanne  */
14847638Smccanne struct bpf_insn {
14947638Smccanne 	u_short	code;
15047638Smccanne 	u_char 	jt;
15147638Smccanne 	u_char 	jf;
15247638Smccanne 	long	k;
15347638Smccanne };
15447638Smccanne 
15547638Smccanne /*
15647638Smccanne  * Macros for array initializers.
15747638Smccanne  */
15847638Smccanne #define BPF_STMT(code, k) { (u_short)code, 0, 0, k }
15947638Smccanne #define BPF_JUMP(code, k, jt, jf) { (u_short)code, jt, jf, k }
16047638Smccanne 
16147638Smccanne #ifdef KERNEL
16247638Smccanne extern u_int bpf_filter();
16347638Smccanne extern void bpfattach();
16447638Smccanne extern void bpf_tap();
16547638Smccanne extern void bpf_mtap();
16647638Smccanne #endif
16747638Smccanne 
16847638Smccanne /*
16947638Smccanne  * These two macros are sensitive to the order in which the
17047638Smccanne  * opcodes appear in bpfcodes.h.
17147638Smccanne  */
17247638Smccanne #define BPF_ISJUMP(code) ((unsigned)(code) <= (unsigned)EQOp)
17347638Smccanne #define BPF_ISLEAF(code) ((unsigned)(code) >= (unsigned)RetOp)
17447638Smccanne 
17547638Smccanne /*
17647638Smccanne  * Number of scratch memory words.
17747638Smccanne  */
17847638Smccanne #define BPF_MEMWORDS 16
179