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 /* 35 * Structure for BIOCSETF. 36 */ 37 struct bpf_program { 38 u_int bf_len; 39 struct bpf_insn *bf_insns; 40 }; 41 42 /* 43 * Struct returned by BIOCGSTATS. 44 */ 45 struct bpf_stat { 46 u_int bs_recv; /* number of packets received */ 47 u_int bs_drop; /* number of packets dropped */ 48 }; 49 50 /* 51 * BPF ioctls 52 * 53 * The first set is for compatibility with Sun's pcc style 54 * header files. If your using gcc, we assume that you 55 * have run fixincludes so the latter set should work. 56 */ 57 #if defined(sun) && !defined(__GNUC__) 58 #define BIOCGFLEN _IOR(B,101, u_int) 59 #define BIOCGBLEN _IOR(B,102, u_int) 60 #define BIOCSETF _IOW(B,103, struct bpf_program) 61 #define BIOCFLUSH _IO(B,104) 62 #define BIOCPROMISC _IO(B,105) 63 #define BIOCDEVP _IOR(B,106, struct bpf_devp) 64 #define BIOCGETIF _IOR(B,107, struct ifreq) 65 #define BIOCSETIF _IOW(B,108, struct ifreq) 66 #define BIOCSRTIMEOUT _IOW(B,109, struct timeval) 67 #define BIOCGRTIMEOUT _IOR(B,110, struct timeval) 68 #define BIOCGSTATS _IOR(B,111, struct bpf_stat) 69 #define BIOCIMMEDIATE _IOW(B,112, u_int) 70 #else 71 #define BIOCGFLEN _IOR('B',101, u_int) 72 #define BIOCGBLEN _IOR('B',102, u_int) 73 #define BIOCSETF _IOW('B',103, struct bpf_program) 74 #define BIOCFLUSH _IO('B',104) 75 #define BIOCPROMISC _IO('B',105) 76 #define BIOCDEVP _IOR('B',106, struct bpf_devp) 77 #define BIOCGETIF _IOR('B',107, struct ifreq) 78 #define BIOCSETIF _IOW('B',108, struct ifreq) 79 #define BIOCSRTIMEOUT _IOW('B',109, struct timeval) 80 #define BIOCGRTIMEOUT _IOR('B',110, struct timeval) 81 #define BIOCGSTATS _IOR('B',111, struct bpf_stat) 82 #define BIOCIMMEDIATE _IOW('B',112, u_int) 83 #endif 84 85 /* 86 * The device parameters of a network interface. 87 */ 88 struct bpf_devp { 89 u_short bdev_type; /* data link layer type, codes below */ 90 u_short bdev_hdrlen; /* length of a hardware packet header */ 91 }; 92 93 /* 94 * Structure prepended to each packet. 95 */ 96 struct bpf_hdr { 97 struct timeval bh_tstamp; /* time stamp */ 98 u_long bh_caplen; /* length of captured portion */ 99 u_long bh_datalen; /* original length of packet */ 100 u_short bh_hdrlen; /* length of bpf header (this struct 101 plus alignment padding) */ 102 }; 103 /* 104 * Because the structure above is not a multiple of 4 bytes, some compilers 105 * will insist on inserting padding; hence, sizeof(struct bpf_hdr) won't work. 106 * Only the kernel needs to know about it; applications use bh_hdrlen. 107 */ 108 #ifdef KERNEL 109 #define SIZEOF_BPF_HDR 18 110 #endif 111 112 /* 113 * Data-link level type codes. 114 * Currently, only ENDT_10MB and DLT_SLIP are supported. 115 */ 116 #define DLT_EN10MB 1 /* Ethernet (10Mb) */ 117 #define DLT_EN3MB 2 /* Experimental Ethernet (3Mb) */ 118 #define DLT_AX25 3 /* Amateur Radio AX.25 */ 119 #define DLT_PRONET 4 /* Proteon ProNET Token Ring */ 120 #define DLT_CHAOS 5 /* Chaos */ 121 #define DLT_IEEE802 6 /* IEEE 802 Networks */ 122 #define DLT_ARCNET 7 /* ARCNET */ 123 #define DLT_SLIP 8 /* Serial Line IP */ 124 #define DLT_PPP 9 /* Point-to-point Protocol */ 125 #define DLT_FDDI 10 /* FDDI */ 126 127 /* 128 * The opcodes are defined as an enumeration. However, they are stored 129 * explicitly in the code array as 'u_short'. 130 */ 131 enum bpf_code { 132 #define OPDEF(opcode, opstr) opcode , 133 #include <net/bpfcodes.h> 134 #undef OPDEF 135 /* this idea is borrowed from gcc */ 136 LAST_AND_UNUSED_ENUM 137 }; 138 139 #define BPF_NCODES ((unsigned)LAST_AND_UNUSED_ENUM) 140 #define BPF_VALIDCODE(code) ((unsigned)(code) < BPF_NCODES) 141 142 /* 143 * The instruction data structure. 144 */ 145 struct bpf_insn { 146 u_short code; 147 u_char jt; 148 u_char jf; 149 long k; 150 }; 151 152 /* 153 * Macros for array initializers. 154 */ 155 #define BPF_STMT(code, k) { (u_short)code, 0, 0, k } 156 #define BPF_JUMP(code, k, jt, jf) { (u_short)code, jt, jf, k } 157 158 #ifdef KERNEL 159 extern u_int bpf_filter(); 160 extern void bpfattach(); 161 extern void bpf_tap(); 162 extern void bpf_mtap(); 163 #endif 164 165 /* 166 * These two macros are sensitive to the order in which the 167 * opcodes appear in bpfcodes.h. 168 */ 169 #define BPF_ISJUMP(code) ((unsigned)(code) <= (unsigned)EQOp) 170 #define BPF_ISLEAF(code) ((unsigned)(code) >= (unsigned)RetOp) 171 172 /* 173 * Number of scratch memory words. 174 */ 175 #define BPF_MEMWORDS 16 176