bpf.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* $OpenBSD: bpf.h,v 1.49 2015/06/16 11:09:39 mpi Exp $ */
  2. /* $NetBSD: bpf.h,v 1.15 1996/12/13 07:57:33 mikel Exp $ */
  3. /*
  4. * Copyright (c) 1990, 1991, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * This code is derived from the Stanford/CMU enet packet filter,
  8. * (net/enet.c) distributed as part of 4.3BSD, and code contributed
  9. * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
  10. * Berkeley Laboratory.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. Neither the name of the University nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. *
  36. * @(#)bpf.h 8.1 (Berkeley) 6/10/93
  37. */
  38. #ifndef _NET_BPF_H_
  39. #define _NET_BPF_H_
  40. /* BSD style release date */
  41. #define BPF_RELEASE 199606
  42. typedef int32_t bpf_int32;
  43. typedef u_int32_t bpf_u_int32;
  44. /*
  45. * Alignment macros. BPF_WORDALIGN rounds up to the next even multiple of
  46. * BPF_ALIGNMENT (which is at least as much as what a timeval needs).
  47. */
  48. #define BPF_ALIGNMENT sizeof(u_int32_t)
  49. #define BPF_WORDALIGN(x) (((x) + (BPF_ALIGNMENT - 1)) & ~(BPF_ALIGNMENT - 1))
  50. #define BPF_MAXINSNS 512
  51. #define BPF_MAXBUFSIZE (2 * 1024 * 1024)
  52. #define BPF_MINBUFSIZE 32
  53. /*
  54. * Structure for BIOCSETF.
  55. */
  56. struct bpf_program {
  57. u_int bf_len;
  58. struct bpf_insn *bf_insns;
  59. };
  60. /*
  61. * Struct returned by BIOCGSTATS.
  62. */
  63. struct bpf_stat {
  64. u_int bs_recv; /* number of packets received */
  65. u_int bs_drop; /* number of packets dropped */
  66. };
  67. /*
  68. * Struct return by BIOCVERSION. This represents the version number of
  69. * the filter language described by the instruction encodings below.
  70. * bpf understands a program iff kernel_major == filter_major &&
  71. * kernel_minor >= filter_minor, that is, if the value returned by the
  72. * running kernel has the same major number and a minor number equal
  73. * equal to or less than the filter being downloaded. Otherwise, the
  74. * results are undefined, meaning an error may be returned or packets
  75. * may be accepted haphazardly.
  76. * It has nothing to do with the source code version.
  77. */
  78. struct bpf_version {
  79. u_short bv_major;
  80. u_short bv_minor;
  81. };
  82. /* Current version number of filter architecture. */
  83. #define BPF_MAJOR_VERSION 1
  84. #define BPF_MINOR_VERSION 1
  85. /*
  86. * BPF ioctls
  87. */
  88. #define BIOCGBLEN _IOR('B',102, u_int)
  89. #define BIOCSBLEN _IOWR('B',102, u_int)
  90. #define BIOCSETF _IOW('B',103, struct bpf_program)
  91. #define BIOCFLUSH _IO('B',104)
  92. #define BIOCPROMISC _IO('B',105)
  93. #define BIOCGDLT _IOR('B',106, u_int)
  94. #define BIOCGETIF _IOR('B',107, struct ifreq)
  95. #define BIOCSETIF _IOW('B',108, struct ifreq)
  96. #define BIOCSRTIMEOUT _IOW('B',109, struct timeval)
  97. #define BIOCGRTIMEOUT _IOR('B',110, struct timeval)
  98. #define BIOCGSTATS _IOR('B',111, struct bpf_stat)
  99. #define BIOCIMMEDIATE _IOW('B',112, u_int)
  100. #define BIOCVERSION _IOR('B',113, struct bpf_version)
  101. #define BIOCSRSIG _IOW('B',114, u_int)
  102. #define BIOCGRSIG _IOR('B',115, u_int)
  103. #define BIOCGHDRCMPLT _IOR('B',116, u_int)
  104. #define BIOCSHDRCMPLT _IOW('B',117, u_int)
  105. #define BIOCLOCK _IO('B',118)
  106. #define BIOCSETWF _IOW('B',119, struct bpf_program)
  107. #define BIOCGFILDROP _IOR('B',120, u_int)
  108. #define BIOCSFILDROP _IOW('B',121, u_int)
  109. #define BIOCSDLT _IOW('B',122, u_int)
  110. #define BIOCGDLTLIST _IOWR('B',123, struct bpf_dltlist)
  111. #define BIOCGDIRFILT _IOR('B',124, u_int)
  112. #define BIOCSDIRFILT _IOW('B',125, u_int)
  113. #define BIOCGQUEUE _IOR('B',126, u_int)
  114. #define BIOCSQUEUE _IOW('B',127, u_int)
  115. /*
  116. * Direction filters for BIOCSDIRFILT/BIOCGDIRFILT
  117. */
  118. #define BPF_DIRECTION_IN 1
  119. #define BPF_DIRECTION_OUT (1<<1)
  120. struct bpf_timeval {
  121. u_int32_t tv_sec;
  122. u_int32_t tv_usec;
  123. };
  124. /*
  125. * Structure prepended to each packet.
  126. */
  127. struct bpf_hdr {
  128. struct bpf_timeval bh_tstamp; /* time stamp */
  129. u_int32_t bh_caplen; /* length of captured portion */
  130. u_int32_t bh_datalen; /* original length of packet */
  131. u_int16_t bh_hdrlen; /* length of bpf header (this struct
  132. plus alignment padding) */
  133. };
  134. /*
  135. * Because the structure above is not a multiple of 4 bytes, some compilers
  136. * will insist on inserting padding; hence, sizeof(struct bpf_hdr) won't work.
  137. * Only the kernel needs to know about it; applications use bh_hdrlen.
  138. * XXX To save a few bytes on 32-bit machines, we avoid end-of-struct
  139. * XXX padding by using the size of the header data elements. This is
  140. * XXX fail-safe: on new machines, we just use the 'safe' sizeof.
  141. */
  142. #ifdef _KERNEL
  143. #if defined(__arm__) || defined(__i386__) || defined(__m68k__) || \
  144. defined(__mips__) || defined(__ns32k__) || defined(__sparc__) || \
  145. defined(__sparc64__) || defined(__vax__)
  146. #define SIZEOF_BPF_HDR 18
  147. #else
  148. #define SIZEOF_BPF_HDR sizeof(struct bpf_hdr)
  149. #endif
  150. #endif
  151. /*
  152. * Data-link level type codes.
  153. */
  154. #define DLT_NULL 0 /* no link-layer encapsulation */
  155. #define DLT_EN10MB 1 /* Ethernet (10Mb) */
  156. #define DLT_EN3MB 2 /* Experimental Ethernet (3Mb) */
  157. #define DLT_AX25 3 /* Amateur Radio AX.25 */
  158. #define DLT_PRONET 4 /* Proteon ProNET Token Ring */
  159. #define DLT_CHAOS 5 /* Chaos */
  160. #define DLT_IEEE802 6 /* IEEE 802 Networks */
  161. #define DLT_ARCNET 7 /* ARCNET */
  162. #define DLT_SLIP 8 /* Serial Line IP */
  163. #define DLT_PPP 9 /* Point-to-point Protocol */
  164. #define DLT_FDDI 10 /* FDDI */
  165. #define DLT_ATM_RFC1483 11 /* LLC/SNAP encapsulated atm */
  166. #define DLT_LOOP 12 /* loopback type (af header) */
  167. #define DLT_ENC 13 /* IPSEC enc type (af header, spi, flags) */
  168. #define DLT_RAW 14 /* raw IP */
  169. #define DLT_SLIP_BSDOS 15 /* BSD/OS Serial Line IP */
  170. #define DLT_PPP_BSDOS 16 /* BSD/OS Point-to-point Protocol */
  171. #define DLT_PFSYNC 18 /* Packet filter state syncing */
  172. #define DLT_PPP_SERIAL 50 /* PPP over Serial with HDLC */
  173. #define DLT_PPP_ETHER 51 /* PPP over Ethernet; session only w/o ether header */
  174. #define DLT_C_HDLC 104 /* Cisco HDLC */
  175. #define DLT_IEEE802_11 105 /* IEEE 802.11 wireless */
  176. #define DLT_PFLOG 117 /* Packet filter logging, by pcap people */
  177. #define DLT_IEEE802_11_RADIO 127 /* IEEE 802.11 plus WLAN header */
  178. #define DLT_MPLS 219 /* MPLS Provider Edge header */
  179. /*
  180. * The instruction encodings.
  181. */
  182. /* instruction classes */
  183. #define BPF_CLASS(code) ((code) & 0x07)
  184. #define BPF_LD 0x00
  185. #define BPF_LDX 0x01
  186. #define BPF_ST 0x02
  187. #define BPF_STX 0x03
  188. #define BPF_ALU 0x04
  189. #define BPF_JMP 0x05
  190. #define BPF_RET 0x06
  191. #define BPF_MISC 0x07
  192. /* ld/ldx fields */
  193. #define BPF_SIZE(code) ((code) & 0x18)
  194. #define BPF_W 0x00
  195. #define BPF_H 0x08
  196. #define BPF_B 0x10
  197. #define BPF_MODE(code) ((code) & 0xe0)
  198. #define BPF_IMM 0x00
  199. #define BPF_ABS 0x20
  200. #define BPF_IND 0x40
  201. #define BPF_MEM 0x60
  202. #define BPF_LEN 0x80
  203. #define BPF_MSH 0xa0
  204. /* alu/jmp fields */
  205. #define BPF_OP(code) ((code) & 0xf0)
  206. #define BPF_ADD 0x00
  207. #define BPF_SUB 0x10
  208. #define BPF_MUL 0x20
  209. #define BPF_DIV 0x30
  210. #define BPF_OR 0x40
  211. #define BPF_AND 0x50
  212. #define BPF_LSH 0x60
  213. #define BPF_RSH 0x70
  214. #define BPF_NEG 0x80
  215. #define BPF_JA 0x00
  216. #define BPF_JEQ 0x10
  217. #define BPF_JGT 0x20
  218. #define BPF_JGE 0x30
  219. #define BPF_JSET 0x40
  220. #define BPF_SRC(code) ((code) & 0x08)
  221. #define BPF_K 0x00
  222. #define BPF_X 0x08
  223. /* ret - BPF_K and BPF_X also apply */
  224. #define BPF_RVAL(code) ((code) & 0x18)
  225. #define BPF_A 0x10
  226. /* misc */
  227. #define BPF_MISCOP(code) ((code) & 0xf8)
  228. #define BPF_TAX 0x00
  229. #define BPF_TXA 0x80
  230. /*
  231. * The instruction data structure.
  232. */
  233. struct bpf_insn {
  234. u_int16_t code;
  235. u_char jt;
  236. u_char jf;
  237. u_int32_t k;
  238. };
  239. /*
  240. * Structure to retrieve available DLTs for the interface.
  241. */
  242. struct bpf_dltlist {
  243. u_int bfl_len; /* number of bfd_list array */
  244. u_int *bfl_list; /* array of DLTs */
  245. };
  246. /*
  247. * Macros for insn array initializers.
  248. */
  249. #define BPF_STMT(code, k) { (u_int16_t)(code), 0, 0, k }
  250. #define BPF_JUMP(code, k, jt, jf) { (u_int16_t)(code), jt, jf, k }
  251. #ifdef _KERNEL
  252. struct ifnet;
  253. int bpf_validate(struct bpf_insn *, int);
  254. int bpf_tap(caddr_t, u_char *, u_int, u_int);
  255. void bpf_mtap(caddr_t, struct mbuf *, u_int);
  256. void bpf_mtap_hdr(caddr_t, caddr_t, u_int, struct mbuf *, u_int,
  257. void (*)(const void *, void *, size_t));
  258. void bpf_mtap_af(caddr_t, u_int32_t, struct mbuf *, u_int);
  259. void bpf_mtap_ether(caddr_t, struct mbuf *, u_int);
  260. void bpfattach(caddr_t *, struct ifnet *, u_int, u_int);
  261. void bpfdetach(struct ifnet *);
  262. void bpfilterattach(int);
  263. u_int bpf_filter(struct bpf_insn *, u_char *, u_int, u_int);
  264. #endif /* _KERNEL */
  265. /*
  266. * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST).
  267. */
  268. #define BPF_MEMWORDS 16
  269. #endif /* _NET_BPF_H_ */