bpf.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*-
  2. * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * This code is derived from the Stanford/CMU enet packet filter,
  6. * (net/enet.c) distributed as part of 4.3BSD, and code contributed
  7. * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
  8. * Berkeley Laboratory.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the University nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * @(#)bpf.h 7.1 (Berkeley) 5/7/91
  35. */
  36. /*
  37. * This is libpcap's cut-down version of bpf.h; it includes only
  38. * the stuff needed for the code generator and the userland BPF
  39. * interpreter, and the libpcap APIs for setting filters, etc..
  40. *
  41. * "pcap-bpf.c" will include the native OS version, as it deals with
  42. * the OS's BPF implementation.
  43. *
  44. * At least two programs found by Google Code Search explicitly includes
  45. * <pcap/bpf.h> (even though <pcap.h>/<pcap/pcap.h> includes it for you),
  46. * so moving that stuff to <pcap/pcap.h> would break the build for some
  47. * programs.
  48. */
  49. /*
  50. * If we've already included <net/bpf.h>, don't re-define this stuff.
  51. * We assume BSD-style multiple-include protection in <net/bpf.h>,
  52. * which is true of all but the oldest versions of FreeBSD and NetBSD,
  53. * or Tru64 UNIX-style multiple-include protection (or, at least,
  54. * Tru64 UNIX 5.x-style; I don't have earlier versions available to check),
  55. * or AIX-style multiple-include protection (or, at least, AIX 5.x-style;
  56. * I don't have earlier versions available to check), or QNX-style
  57. * multiple-include protection (as per GitHub pull request #394).
  58. *
  59. * We do not check for BPF_MAJOR_VERSION, as that's defined by
  60. * <linux/filter.h>, which is directly or indirectly included in some
  61. * programs that also include pcap.h, and <linux/filter.h> doesn't
  62. * define stuff we need.
  63. *
  64. * This also provides our own multiple-include protection.
  65. */
  66. #if !defined(_NET_BPF_H_) && !defined(_NET_BPF_H_INCLUDED) && !defined(_BPF_H_) && !defined(_H_BPF) && !defined(lib_pcap_bpf_h)
  67. #define lib_pcap_bpf_h
  68. #include <pcap/funcattrs.h>
  69. #ifdef __cplusplus
  70. extern "C" {
  71. #endif
  72. /* BSD style release date */
  73. #define BPF_RELEASE 199606
  74. #ifdef MSDOS /* must be 32-bit */
  75. typedef long bpf_int32;
  76. typedef unsigned long bpf_u_int32;
  77. #else
  78. typedef int bpf_int32;
  79. typedef u_int bpf_u_int32;
  80. #endif
  81. /*
  82. * Alignment macros. BPF_WORDALIGN rounds up to the next
  83. * even multiple of BPF_ALIGNMENT.
  84. *
  85. * Tcpdump's print-pflog.c uses this, so we define it here.
  86. */
  87. #ifndef __NetBSD__
  88. #define BPF_ALIGNMENT sizeof(bpf_int32)
  89. #else
  90. #define BPF_ALIGNMENT sizeof(long)
  91. #endif
  92. #define BPF_WORDALIGN(x) (((x)+(BPF_ALIGNMENT-1))&~(BPF_ALIGNMENT-1))
  93. /*
  94. * Structure for "pcap_compile()", "pcap_setfilter()", etc..
  95. */
  96. struct bpf_program {
  97. u_int bf_len;
  98. struct bpf_insn *bf_insns;
  99. };
  100. #include <pcap/dlt.h>
  101. /*
  102. * The instruction encodings.
  103. *
  104. * Please inform tcpdump-workers@lists.tcpdump.org if you use any
  105. * of the reserved values, so that we can note that they're used
  106. * (and perhaps implement it in the reference BPF implementation
  107. * and encourage its implementation elsewhere).
  108. */
  109. /*
  110. * The upper 8 bits of the opcode aren't used. BSD/OS used 0x8000.
  111. */
  112. /* instruction classes */
  113. #define BPF_CLASS(code) ((code) & 0x07)
  114. #define BPF_LD 0x00
  115. #define BPF_LDX 0x01
  116. #define BPF_ST 0x02
  117. #define BPF_STX 0x03
  118. #define BPF_ALU 0x04
  119. #define BPF_JMP 0x05
  120. #define BPF_RET 0x06
  121. #define BPF_MISC 0x07
  122. /* ld/ldx fields */
  123. #define BPF_SIZE(code) ((code) & 0x18)
  124. #define BPF_W 0x00
  125. #define BPF_H 0x08
  126. #define BPF_B 0x10
  127. /* 0x18 reserved; used by BSD/OS */
  128. #define BPF_MODE(code) ((code) & 0xe0)
  129. #define BPF_IMM 0x00
  130. #define BPF_ABS 0x20
  131. #define BPF_IND 0x40
  132. #define BPF_MEM 0x60
  133. #define BPF_LEN 0x80
  134. #define BPF_MSH 0xa0
  135. /* 0xc0 reserved; used by BSD/OS */
  136. /* 0xe0 reserved; used by BSD/OS */
  137. /* alu/jmp fields */
  138. #define BPF_OP(code) ((code) & 0xf0)
  139. #define BPF_ADD 0x00
  140. #define BPF_SUB 0x10
  141. #define BPF_MUL 0x20
  142. #define BPF_DIV 0x30
  143. #define BPF_OR 0x40
  144. #define BPF_AND 0x50
  145. #define BPF_LSH 0x60
  146. #define BPF_RSH 0x70
  147. #define BPF_NEG 0x80
  148. #define BPF_MOD 0x90
  149. #define BPF_XOR 0xa0
  150. /* 0xb0 reserved */
  151. /* 0xc0 reserved */
  152. /* 0xd0 reserved */
  153. /* 0xe0 reserved */
  154. /* 0xf0 reserved */
  155. #define BPF_JA 0x00
  156. #define BPF_JEQ 0x10
  157. #define BPF_JGT 0x20
  158. #define BPF_JGE 0x30
  159. #define BPF_JSET 0x40
  160. /* 0x50 reserved; used on BSD/OS */
  161. /* 0x60 reserved */
  162. /* 0x70 reserved */
  163. /* 0x80 reserved */
  164. /* 0x90 reserved */
  165. /* 0xa0 reserved */
  166. /* 0xb0 reserved */
  167. /* 0xc0 reserved */
  168. /* 0xd0 reserved */
  169. /* 0xe0 reserved */
  170. /* 0xf0 reserved */
  171. #define BPF_SRC(code) ((code) & 0x08)
  172. #define BPF_K 0x00
  173. #define BPF_X 0x08
  174. /* ret - BPF_K and BPF_X also apply */
  175. #define BPF_RVAL(code) ((code) & 0x18)
  176. #define BPF_A 0x10
  177. /* 0x18 reserved */
  178. /* misc */
  179. #define BPF_MISCOP(code) ((code) & 0xf8)
  180. #define BPF_TAX 0x00
  181. /* 0x08 reserved */
  182. /* 0x10 reserved */
  183. /* 0x18 reserved */
  184. /* #define BPF_COP 0x20 NetBSD "coprocessor" extensions */
  185. /* 0x28 reserved */
  186. /* 0x30 reserved */
  187. /* 0x38 reserved */
  188. /* #define BPF_COPX 0x40 NetBSD "coprocessor" extensions */
  189. /* also used on BSD/OS */
  190. /* 0x48 reserved */
  191. /* 0x50 reserved */
  192. /* 0x58 reserved */
  193. /* 0x60 reserved */
  194. /* 0x68 reserved */
  195. /* 0x70 reserved */
  196. /* 0x78 reserved */
  197. #define BPF_TXA 0x80
  198. /* 0x88 reserved */
  199. /* 0x90 reserved */
  200. /* 0x98 reserved */
  201. /* 0xa0 reserved */
  202. /* 0xa8 reserved */
  203. /* 0xb0 reserved */
  204. /* 0xb8 reserved */
  205. /* 0xc0 reserved; used on BSD/OS */
  206. /* 0xc8 reserved */
  207. /* 0xd0 reserved */
  208. /* 0xd8 reserved */
  209. /* 0xe0 reserved */
  210. /* 0xe8 reserved */
  211. /* 0xf0 reserved */
  212. /* 0xf8 reserved */
  213. /*
  214. * The instruction data structure.
  215. */
  216. struct bpf_insn {
  217. u_short code;
  218. u_char jt;
  219. u_char jf;
  220. bpf_u_int32 k;
  221. };
  222. /*
  223. * Auxiliary data, for use when interpreting a filter intended for the
  224. * Linux kernel when the kernel rejects the filter (requiring us to
  225. * run it in userland). It contains VLAN tag information.
  226. */
  227. struct bpf_aux_data {
  228. u_short vlan_tag_present;
  229. u_short vlan_tag;
  230. };
  231. /*
  232. * Macros for insn array initializers.
  233. */
  234. #define BPF_STMT(code, k) { (u_short)(code), 0, 0, k }
  235. #define BPF_JUMP(code, k, jt, jf) { (u_short)(code), jt, jf, k }
  236. PCAP_API int bpf_validate(const struct bpf_insn *, int);
  237. PCAP_API u_int bpf_filter(const struct bpf_insn *, const u_char *, u_int, u_int);
  238. extern u_int bpf_filter_with_aux_data(const struct bpf_insn *, const u_char *, u_int, u_int, const struct bpf_aux_data *);
  239. /*
  240. * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST).
  241. */
  242. #define BPF_MEMWORDS 16
  243. #ifdef __cplusplus
  244. }
  245. #endif
  246. #endif /* !defined(_NET_BPF_H_) && !defined(_BPF_H_) && !defined(_H_BPF) && !defined(lib_pcap_bpf_h) */