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 * 2148968Smccanne * @(#) $Header: bpf.h,v 1.20 91/04/24 22:06:24 mccanne Locked $ (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 3448930Smccanne #define BPF_MAXINSNS 512 3548930Smccanne #define BPF_MAXBUFSIZE 0x8000 3648930Smccanne 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) 66*49201Smccanne #define BIOCGDLT _IOR(B,106, u_int) 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) 79*49201Smccanne #define BIOCGDLT _IOR('B',106, u_int) 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 * Structure prepended to each packet. 9047638Smccanne */ 9147638Smccanne struct bpf_hdr { 9247638Smccanne struct timeval bh_tstamp; /* time stamp */ 9347638Smccanne u_long bh_caplen; /* length of captured portion */ 9447638Smccanne u_long bh_datalen; /* original length of packet */ 9547638Smccanne u_short bh_hdrlen; /* length of bpf header (this struct 9647638Smccanne plus alignment padding) */ 9747638Smccanne }; 9847638Smccanne /* 9947638Smccanne * Because the structure above is not a multiple of 4 bytes, some compilers 10047638Smccanne * will insist on inserting padding; hence, sizeof(struct bpf_hdr) won't work. 10147638Smccanne * Only the kernel needs to know about it; applications use bh_hdrlen. 10247638Smccanne */ 10347638Smccanne #ifdef KERNEL 10447638Smccanne #define SIZEOF_BPF_HDR 18 10547638Smccanne #endif 10647638Smccanne 10747638Smccanne /* 10847638Smccanne * Data-link level type codes. 109*49201Smccanne * Currently, only DLT_EN10MB and DLT_SLIP are supported. 11047638Smccanne */ 11147638Smccanne #define DLT_EN10MB 1 /* Ethernet (10Mb) */ 11247638Smccanne #define DLT_EN3MB 2 /* Experimental Ethernet (3Mb) */ 11347638Smccanne #define DLT_AX25 3 /* Amateur Radio AX.25 */ 11447638Smccanne #define DLT_PRONET 4 /* Proteon ProNET Token Ring */ 11547638Smccanne #define DLT_CHAOS 5 /* Chaos */ 11647638Smccanne #define DLT_IEEE802 6 /* IEEE 802 Networks */ 11747638Smccanne #define DLT_ARCNET 7 /* ARCNET */ 11847638Smccanne #define DLT_SLIP 8 /* Serial Line IP */ 11947638Smccanne #define DLT_PPP 9 /* Point-to-point Protocol */ 12047638Smccanne #define DLT_FDDI 10 /* FDDI */ 12147638Smccanne 12247638Smccanne /* 12348968Smccanne * The instruction encondings. 12447638Smccanne */ 12548968Smccanne /* classes <2:0> */ 12648968Smccanne #define BPF_CLASS(code) ((code) & 0x07) 12748968Smccanne #define BPF_LD 0x00 12848968Smccanne #define BPF_LDX 0x01 12948968Smccanne #define BPF_ST 0x02 13048968Smccanne #define BPF_STX 0x03 13148968Smccanne #define BPF_ALU 0x04 13248968Smccanne #define BPF_JMP 0x05 13348968Smccanne #define BPF_RET 0x06 13448968Smccanne #define BPF_MISC 0x07 13547638Smccanne 13648968Smccanne /* ld/ldx fields */ 13748968Smccanne #define BPF_SIZE(code) ((code) & 0x18) 13848968Smccanne #define BPF_W 0x00 13948968Smccanne #define BPF_H 0x08 14048968Smccanne #define BPF_B 0x10 14148968Smccanne #define BPF_MODE(code) ((code) & 0xe0) 14248968Smccanne #define BPF_IMM 0x00 14348968Smccanne #define BPF_ABS 0x20 14448968Smccanne #define BPF_IND 0x40 14548968Smccanne #define BPF_MEM 0x60 14648968Smccanne #define BPF_LEN 0x80 14748968Smccanne #define BPF_MSH 0xa0 14847638Smccanne 14948968Smccanne /* alu/jmp fields */ 15048968Smccanne #define BPF_OP(code) ((code) & 0xf0) 15148968Smccanne #define BPF_ADD 0x00 15248968Smccanne #define BPF_SUB 0x10 15348968Smccanne #define BPF_MUL 0x20 15448968Smccanne #define BPF_DIV 0x30 15548968Smccanne #define BPF_OR 0x40 15648968Smccanne #define BPF_AND 0x50 15748968Smccanne #define BPF_LSH 0x60 15848968Smccanne #define BPF_RSH 0x70 15948968Smccanne #define BPF_NEG 0x80 16048968Smccanne #define BPF_JA 0x00 16148968Smccanne #define BPF_JEQ 0x10 16248968Smccanne #define BPF_JGT 0x20 16348968Smccanne #define BPF_JGE 0x30 16448968Smccanne #define BPF_JSET 0x40 16548968Smccanne #define BPF_SRC(code) ((code) & 0x08) 16648968Smccanne #define BPF_K 0x00 16748968Smccanne #define BPF_X 0x08 16848968Smccanne 16948968Smccanne /* ret - BPF_K and BPF_X also apply */ 17048968Smccanne #define BPF_RVAL(code) ((code) & 0x18) 17148968Smccanne #define BPF_A 0x10 17248968Smccanne 17348968Smccanne /* misc */ 17448968Smccanne #define BPF_MISCOP(code) ((code) & 0xf8) 17548968Smccanne #define BPF_TAX 0x00 17648968Smccanne #define BPF_TXA 0x80 17748968Smccanne 17847638Smccanne /* 17947638Smccanne * The instruction data structure. 18047638Smccanne */ 18147638Smccanne struct bpf_insn { 18247638Smccanne u_short code; 18347638Smccanne u_char jt; 18447638Smccanne u_char jf; 18547638Smccanne long k; 18647638Smccanne }; 18747638Smccanne 18847638Smccanne /* 18948968Smccanne * Macros for insn array initializers. 19047638Smccanne */ 19148968Smccanne #define BPF_STMT(code, k) { (u_short)(code), 0, 0, k } 19248968Smccanne #define BPF_JUMP(code, k, jt, jf) { (u_short)(code), jt, jf, k } 19347638Smccanne 19447638Smccanne #ifdef KERNEL 19547638Smccanne extern u_int bpf_filter(); 19647638Smccanne extern void bpfattach(); 19747638Smccanne extern void bpf_tap(); 19847638Smccanne extern void bpf_mtap(); 19947638Smccanne #endif 20047638Smccanne 20147638Smccanne /* 20248968Smccanne * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST). 20347638Smccanne */ 20447638Smccanne #define BPF_MEMWORDS 16 205