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 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 instruction encondings. 132 */ 133 /* classes <2:0> */ 134 #define BPF_CLASS(code) ((code) & 0x07) 135 #define BPF_LD 0x00 136 #define BPF_LDX 0x01 137 #define BPF_ST 0x02 138 #define BPF_STX 0x03 139 #define BPF_ALU 0x04 140 #define BPF_JMP 0x05 141 #define BPF_RET 0x06 142 #define BPF_MISC 0x07 143 144 /* ld/ldx fields */ 145 #define BPF_SIZE(code) ((code) & 0x18) 146 #define BPF_W 0x00 147 #define BPF_H 0x08 148 #define BPF_B 0x10 149 #define BPF_MODE(code) ((code) & 0xe0) 150 #define BPF_IMM 0x00 151 #define BPF_ABS 0x20 152 #define BPF_IND 0x40 153 #define BPF_MEM 0x60 154 #define BPF_LEN 0x80 155 #define BPF_MSH 0xa0 156 157 /* alu/jmp fields */ 158 #define BPF_OP(code) ((code) & 0xf0) 159 #define BPF_ADD 0x00 160 #define BPF_SUB 0x10 161 #define BPF_MUL 0x20 162 #define BPF_DIV 0x30 163 #define BPF_OR 0x40 164 #define BPF_AND 0x50 165 #define BPF_LSH 0x60 166 #define BPF_RSH 0x70 167 #define BPF_NEG 0x80 168 #define BPF_JA 0x00 169 #define BPF_JEQ 0x10 170 #define BPF_JGT 0x20 171 #define BPF_JGE 0x30 172 #define BPF_JSET 0x40 173 #define BPF_SRC(code) ((code) & 0x08) 174 #define BPF_K 0x00 175 #define BPF_X 0x08 176 177 /* ret - BPF_K and BPF_X also apply */ 178 #define BPF_RVAL(code) ((code) & 0x18) 179 #define BPF_A 0x10 180 181 /* misc */ 182 #define BPF_MISCOP(code) ((code) & 0xf8) 183 #define BPF_TAX 0x00 184 #define BPF_TXA 0x80 185 186 /* 187 * The instruction data structure. 188 */ 189 struct bpf_insn { 190 u_short code; 191 u_char jt; 192 u_char jf; 193 long k; 194 }; 195 196 /* 197 * Macros for insn array initializers. 198 */ 199 #define BPF_STMT(code, k) { (u_short)(code), 0, 0, k } 200 #define BPF_JUMP(code, k, jt, jf) { (u_short)(code), jt, jf, k } 201 202 #ifdef KERNEL 203 extern u_int bpf_filter(); 204 extern void bpfattach(); 205 extern void bpf_tap(); 206 extern void bpf_mtap(); 207 #endif 208 209 /* 210 * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST). 211 */ 212 #define BPF_MEMWORDS 16 213