filter.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /*
  2. * Linux Socket Filter Data Structures
  3. */
  4. #ifndef __LINUX_FILTER_H__
  5. #define __LINUX_FILTER_H__
  6. #include <stdarg.h>
  7. #include <linux/atomic.h>
  8. #include <linux/compat.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/linkage.h>
  11. #include <linux/printk.h>
  12. #include <linux/workqueue.h>
  13. #include <linux/sched.h>
  14. #include <linux/capability.h>
  15. #include <net/sch_generic.h>
  16. #include <asm/cacheflush.h>
  17. #include <uapi/linux/filter.h>
  18. #include <uapi/linux/bpf.h>
  19. struct sk_buff;
  20. struct sock;
  21. struct seccomp_data;
  22. struct bpf_prog_aux;
  23. /* ArgX, context and stack frame pointer register positions. Note,
  24. * Arg1, Arg2, Arg3, etc are used as argument mappings of function
  25. * calls in BPF_CALL instruction.
  26. */
  27. #define BPF_REG_ARG1 BPF_REG_1
  28. #define BPF_REG_ARG2 BPF_REG_2
  29. #define BPF_REG_ARG3 BPF_REG_3
  30. #define BPF_REG_ARG4 BPF_REG_4
  31. #define BPF_REG_ARG5 BPF_REG_5
  32. #define BPF_REG_CTX BPF_REG_6
  33. #define BPF_REG_FP BPF_REG_10
  34. /* Additional register mappings for converted user programs. */
  35. #define BPF_REG_A BPF_REG_0
  36. #define BPF_REG_X BPF_REG_7
  37. #define BPF_REG_TMP BPF_REG_8
  38. /* Kernel hidden auxiliary/helper register for hardening step.
  39. * Only used by eBPF JITs. It's nothing more than a temporary
  40. * register that JITs use internally, only that here it's part
  41. * of eBPF instructions that have been rewritten for blinding
  42. * constants. See JIT pre-step in bpf_jit_blind_constants().
  43. */
  44. #define BPF_REG_AX MAX_BPF_REG
  45. #define MAX_BPF_JIT_REG (MAX_BPF_REG + 1)
  46. /* BPF program can access up to 512 bytes of stack space. */
  47. #define MAX_BPF_STACK 512
  48. /* Helper macros for filter block array initializers. */
  49. /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
  50. #define BPF_ALU64_REG(OP, DST, SRC) \
  51. ((struct bpf_insn) { \
  52. .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \
  53. .dst_reg = DST, \
  54. .src_reg = SRC, \
  55. .off = 0, \
  56. .imm = 0 })
  57. #define BPF_ALU32_REG(OP, DST, SRC) \
  58. ((struct bpf_insn) { \
  59. .code = BPF_ALU | BPF_OP(OP) | BPF_X, \
  60. .dst_reg = DST, \
  61. .src_reg = SRC, \
  62. .off = 0, \
  63. .imm = 0 })
  64. /* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
  65. #define BPF_ALU64_IMM(OP, DST, IMM) \
  66. ((struct bpf_insn) { \
  67. .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
  68. .dst_reg = DST, \
  69. .src_reg = 0, \
  70. .off = 0, \
  71. .imm = IMM })
  72. #define BPF_ALU32_IMM(OP, DST, IMM) \
  73. ((struct bpf_insn) { \
  74. .code = BPF_ALU | BPF_OP(OP) | BPF_K, \
  75. .dst_reg = DST, \
  76. .src_reg = 0, \
  77. .off = 0, \
  78. .imm = IMM })
  79. /* Endianess conversion, cpu_to_{l,b}e(), {l,b}e_to_cpu() */
  80. #define BPF_ENDIAN(TYPE, DST, LEN) \
  81. ((struct bpf_insn) { \
  82. .code = BPF_ALU | BPF_END | BPF_SRC(TYPE), \
  83. .dst_reg = DST, \
  84. .src_reg = 0, \
  85. .off = 0, \
  86. .imm = LEN })
  87. /* Short form of mov, dst_reg = src_reg */
  88. #define BPF_MOV64_REG(DST, SRC) \
  89. ((struct bpf_insn) { \
  90. .code = BPF_ALU64 | BPF_MOV | BPF_X, \
  91. .dst_reg = DST, \
  92. .src_reg = SRC, \
  93. .off = 0, \
  94. .imm = 0 })
  95. #define BPF_MOV32_REG(DST, SRC) \
  96. ((struct bpf_insn) { \
  97. .code = BPF_ALU | BPF_MOV | BPF_X, \
  98. .dst_reg = DST, \
  99. .src_reg = SRC, \
  100. .off = 0, \
  101. .imm = 0 })
  102. /* Short form of mov, dst_reg = imm32 */
  103. #define BPF_MOV64_IMM(DST, IMM) \
  104. ((struct bpf_insn) { \
  105. .code = BPF_ALU64 | BPF_MOV | BPF_K, \
  106. .dst_reg = DST, \
  107. .src_reg = 0, \
  108. .off = 0, \
  109. .imm = IMM })
  110. #define BPF_MOV32_IMM(DST, IMM) \
  111. ((struct bpf_insn) { \
  112. .code = BPF_ALU | BPF_MOV | BPF_K, \
  113. .dst_reg = DST, \
  114. .src_reg = 0, \
  115. .off = 0, \
  116. .imm = IMM })
  117. /* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
  118. #define BPF_LD_IMM64(DST, IMM) \
  119. BPF_LD_IMM64_RAW(DST, 0, IMM)
  120. #define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
  121. ((struct bpf_insn) { \
  122. .code = BPF_LD | BPF_DW | BPF_IMM, \
  123. .dst_reg = DST, \
  124. .src_reg = SRC, \
  125. .off = 0, \
  126. .imm = (__u32) (IMM) }), \
  127. ((struct bpf_insn) { \
  128. .code = 0, /* zero is reserved opcode */ \
  129. .dst_reg = 0, \
  130. .src_reg = 0, \
  131. .off = 0, \
  132. .imm = ((__u64) (IMM)) >> 32 })
  133. /* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */
  134. #define BPF_LD_MAP_FD(DST, MAP_FD) \
  135. BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD)
  136. /* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */
  137. #define BPF_MOV64_RAW(TYPE, DST, SRC, IMM) \
  138. ((struct bpf_insn) { \
  139. .code = BPF_ALU64 | BPF_MOV | BPF_SRC(TYPE), \
  140. .dst_reg = DST, \
  141. .src_reg = SRC, \
  142. .off = 0, \
  143. .imm = IMM })
  144. #define BPF_MOV32_RAW(TYPE, DST, SRC, IMM) \
  145. ((struct bpf_insn) { \
  146. .code = BPF_ALU | BPF_MOV | BPF_SRC(TYPE), \
  147. .dst_reg = DST, \
  148. .src_reg = SRC, \
  149. .off = 0, \
  150. .imm = IMM })
  151. /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
  152. #define BPF_LD_ABS(SIZE, IMM) \
  153. ((struct bpf_insn) { \
  154. .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \
  155. .dst_reg = 0, \
  156. .src_reg = 0, \
  157. .off = 0, \
  158. .imm = IMM })
  159. /* Indirect packet access, R0 = *(uint *) (skb->data + src_reg + imm32) */
  160. #define BPF_LD_IND(SIZE, SRC, IMM) \
  161. ((struct bpf_insn) { \
  162. .code = BPF_LD | BPF_SIZE(SIZE) | BPF_IND, \
  163. .dst_reg = 0, \
  164. .src_reg = SRC, \
  165. .off = 0, \
  166. .imm = IMM })
  167. /* Memory load, dst_reg = *(uint *) (src_reg + off16) */
  168. #define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
  169. ((struct bpf_insn) { \
  170. .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
  171. .dst_reg = DST, \
  172. .src_reg = SRC, \
  173. .off = OFF, \
  174. .imm = 0 })
  175. /* Memory store, *(uint *) (dst_reg + off16) = src_reg */
  176. #define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
  177. ((struct bpf_insn) { \
  178. .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
  179. .dst_reg = DST, \
  180. .src_reg = SRC, \
  181. .off = OFF, \
  182. .imm = 0 })
  183. /* Atomic memory add, *(uint *)(dst_reg + off16) += src_reg */
  184. #define BPF_STX_XADD(SIZE, DST, SRC, OFF) \
  185. ((struct bpf_insn) { \
  186. .code = BPF_STX | BPF_SIZE(SIZE) | BPF_XADD, \
  187. .dst_reg = DST, \
  188. .src_reg = SRC, \
  189. .off = OFF, \
  190. .imm = 0 })
  191. /* Memory store, *(uint *) (dst_reg + off16) = imm32 */
  192. #define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
  193. ((struct bpf_insn) { \
  194. .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
  195. .dst_reg = DST, \
  196. .src_reg = 0, \
  197. .off = OFF, \
  198. .imm = IMM })
  199. /* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */
  200. #define BPF_JMP_REG(OP, DST, SRC, OFF) \
  201. ((struct bpf_insn) { \
  202. .code = BPF_JMP | BPF_OP(OP) | BPF_X, \
  203. .dst_reg = DST, \
  204. .src_reg = SRC, \
  205. .off = OFF, \
  206. .imm = 0 })
  207. /* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
  208. #define BPF_JMP_IMM(OP, DST, IMM, OFF) \
  209. ((struct bpf_insn) { \
  210. .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
  211. .dst_reg = DST, \
  212. .src_reg = 0, \
  213. .off = OFF, \
  214. .imm = IMM })
  215. /* Function call */
  216. #define BPF_EMIT_CALL(FUNC) \
  217. ((struct bpf_insn) { \
  218. .code = BPF_JMP | BPF_CALL, \
  219. .dst_reg = 0, \
  220. .src_reg = 0, \
  221. .off = 0, \
  222. .imm = ((FUNC) - __bpf_call_base) })
  223. /* Raw code statement block */
  224. #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
  225. ((struct bpf_insn) { \
  226. .code = CODE, \
  227. .dst_reg = DST, \
  228. .src_reg = SRC, \
  229. .off = OFF, \
  230. .imm = IMM })
  231. /* Program exit */
  232. #define BPF_EXIT_INSN() \
  233. ((struct bpf_insn) { \
  234. .code = BPF_JMP | BPF_EXIT, \
  235. .dst_reg = 0, \
  236. .src_reg = 0, \
  237. .off = 0, \
  238. .imm = 0 })
  239. /* Internal classic blocks for direct assignment */
  240. #define __BPF_STMT(CODE, K) \
  241. ((struct sock_filter) BPF_STMT(CODE, K))
  242. #define __BPF_JUMP(CODE, K, JT, JF) \
  243. ((struct sock_filter) BPF_JUMP(CODE, K, JT, JF))
  244. #define bytes_to_bpf_size(bytes) \
  245. ({ \
  246. int bpf_size = -EINVAL; \
  247. \
  248. if (bytes == sizeof(u8)) \
  249. bpf_size = BPF_B; \
  250. else if (bytes == sizeof(u16)) \
  251. bpf_size = BPF_H; \
  252. else if (bytes == sizeof(u32)) \
  253. bpf_size = BPF_W; \
  254. else if (bytes == sizeof(u64)) \
  255. bpf_size = BPF_DW; \
  256. \
  257. bpf_size; \
  258. })
  259. #ifdef CONFIG_COMPAT
  260. /* A struct sock_filter is architecture independent. */
  261. struct compat_sock_fprog {
  262. u16 len;
  263. compat_uptr_t filter; /* struct sock_filter * */
  264. };
  265. #endif
  266. struct sock_fprog_kern {
  267. u16 len;
  268. struct sock_filter *filter;
  269. };
  270. struct bpf_binary_header {
  271. unsigned int pages;
  272. u8 image[];
  273. };
  274. struct bpf_prog {
  275. u16 pages; /* Number of allocated pages */
  276. kmemcheck_bitfield_begin(meta);
  277. u16 jited:1, /* Is our filter JIT'ed? */
  278. gpl_compatible:1, /* Is filter GPL compatible? */
  279. cb_access:1, /* Is control block accessed? */
  280. dst_needed:1; /* Do we need dst entry? */
  281. kmemcheck_bitfield_end(meta);
  282. u32 len; /* Number of filter blocks */
  283. enum bpf_prog_type type; /* Type of BPF program */
  284. struct bpf_prog_aux *aux; /* Auxiliary fields */
  285. struct sock_fprog_kern *orig_prog; /* Original BPF program */
  286. unsigned int (*bpf_func)(const struct sk_buff *skb,
  287. const struct bpf_insn *filter);
  288. /* Instructions for interpreter */
  289. union {
  290. struct sock_filter insns[0];
  291. struct bpf_insn insnsi[0];
  292. };
  293. };
  294. struct sk_filter {
  295. atomic_t refcnt;
  296. struct rcu_head rcu;
  297. struct bpf_prog *prog;
  298. };
  299. #define BPF_PROG_RUN(filter, ctx) (*filter->bpf_func)(ctx, filter->insnsi)
  300. #define BPF_SKB_CB_LEN QDISC_CB_PRIV_LEN
  301. struct bpf_skb_data_end {
  302. struct qdisc_skb_cb qdisc_cb;
  303. void *data_end;
  304. };
  305. /* compute the linear packet data range [data, data_end) which
  306. * will be accessed by cls_bpf and act_bpf programs
  307. */
  308. static inline void bpf_compute_data_end(struct sk_buff *skb)
  309. {
  310. struct bpf_skb_data_end *cb = (struct bpf_skb_data_end *)skb->cb;
  311. BUILD_BUG_ON(sizeof(*cb) > FIELD_SIZEOF(struct sk_buff, cb));
  312. cb->data_end = skb->data + skb_headlen(skb);
  313. }
  314. static inline u8 *bpf_skb_cb(struct sk_buff *skb)
  315. {
  316. /* eBPF programs may read/write skb->cb[] area to transfer meta
  317. * data between tail calls. Since this also needs to work with
  318. * tc, that scratch memory is mapped to qdisc_skb_cb's data area.
  319. *
  320. * In some socket filter cases, the cb unfortunately needs to be
  321. * saved/restored so that protocol specific skb->cb[] data won't
  322. * be lost. In any case, due to unpriviledged eBPF programs
  323. * attached to sockets, we need to clear the bpf_skb_cb() area
  324. * to not leak previous contents to user space.
  325. */
  326. BUILD_BUG_ON(FIELD_SIZEOF(struct __sk_buff, cb) != BPF_SKB_CB_LEN);
  327. BUILD_BUG_ON(FIELD_SIZEOF(struct __sk_buff, cb) !=
  328. FIELD_SIZEOF(struct qdisc_skb_cb, data));
  329. return qdisc_skb_cb(skb)->data;
  330. }
  331. static inline u32 bpf_prog_run_save_cb(const struct bpf_prog *prog,
  332. struct sk_buff *skb)
  333. {
  334. u8 *cb_data = bpf_skb_cb(skb);
  335. u8 cb_saved[BPF_SKB_CB_LEN];
  336. u32 res;
  337. if (unlikely(prog->cb_access)) {
  338. memcpy(cb_saved, cb_data, sizeof(cb_saved));
  339. memset(cb_data, 0, sizeof(cb_saved));
  340. }
  341. res = BPF_PROG_RUN(prog, skb);
  342. if (unlikely(prog->cb_access))
  343. memcpy(cb_data, cb_saved, sizeof(cb_saved));
  344. return res;
  345. }
  346. static inline u32 bpf_prog_run_clear_cb(const struct bpf_prog *prog,
  347. struct sk_buff *skb)
  348. {
  349. u8 *cb_data = bpf_skb_cb(skb);
  350. if (unlikely(prog->cb_access))
  351. memset(cb_data, 0, BPF_SKB_CB_LEN);
  352. return BPF_PROG_RUN(prog, skb);
  353. }
  354. static inline unsigned int bpf_prog_size(unsigned int proglen)
  355. {
  356. return max(sizeof(struct bpf_prog),
  357. offsetof(struct bpf_prog, insns[proglen]));
  358. }
  359. static inline bool bpf_prog_was_classic(const struct bpf_prog *prog)
  360. {
  361. /* When classic BPF programs have been loaded and the arch
  362. * does not have a classic BPF JIT (anymore), they have been
  363. * converted via bpf_migrate_filter() to eBPF and thus always
  364. * have an unspec program type.
  365. */
  366. return prog->type == BPF_PROG_TYPE_UNSPEC;
  367. }
  368. #define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0]))
  369. #ifdef CONFIG_DEBUG_SET_MODULE_RONX
  370. static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
  371. {
  372. set_memory_ro((unsigned long)fp, fp->pages);
  373. }
  374. static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
  375. {
  376. set_memory_rw((unsigned long)fp, fp->pages);
  377. }
  378. #else
  379. static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
  380. {
  381. }
  382. static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
  383. {
  384. }
  385. #endif /* CONFIG_DEBUG_SET_MODULE_RONX */
  386. int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap);
  387. static inline int sk_filter(struct sock *sk, struct sk_buff *skb)
  388. {
  389. return sk_filter_trim_cap(sk, skb, 1);
  390. }
  391. struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err);
  392. void bpf_prog_free(struct bpf_prog *fp);
  393. struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags);
  394. struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
  395. gfp_t gfp_extra_flags);
  396. void __bpf_prog_free(struct bpf_prog *fp);
  397. static inline void bpf_prog_unlock_free(struct bpf_prog *fp)
  398. {
  399. bpf_prog_unlock_ro(fp);
  400. __bpf_prog_free(fp);
  401. }
  402. typedef int (*bpf_aux_classic_check_t)(struct sock_filter *filter,
  403. unsigned int flen);
  404. int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog);
  405. int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
  406. bpf_aux_classic_check_t trans, bool save_orig);
  407. void bpf_prog_destroy(struct bpf_prog *fp);
  408. int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
  409. int sk_attach_bpf(u32 ufd, struct sock *sk);
  410. int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk);
  411. int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk);
  412. int sk_detach_filter(struct sock *sk);
  413. int sk_get_filter(struct sock *sk, struct sock_filter __user *filter,
  414. unsigned int len);
  415. bool sk_filter_charge(struct sock *sk, struct sk_filter *fp);
  416. void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp);
  417. u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  418. struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog);
  419. bool bpf_helper_changes_skb_data(void *func);
  420. struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
  421. const struct bpf_insn *patch, u32 len);
  422. #ifdef CONFIG_BPF_JIT
  423. extern int bpf_jit_enable;
  424. extern int bpf_jit_harden;
  425. typedef void (*bpf_jit_fill_hole_t)(void *area, unsigned int size);
  426. struct bpf_binary_header *
  427. bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
  428. unsigned int alignment,
  429. bpf_jit_fill_hole_t bpf_fill_ill_insns);
  430. void bpf_jit_binary_free(struct bpf_binary_header *hdr);
  431. void bpf_jit_compile(struct bpf_prog *fp);
  432. void bpf_jit_free(struct bpf_prog *fp);
  433. struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *fp);
  434. void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other);
  435. static inline void bpf_jit_dump(unsigned int flen, unsigned int proglen,
  436. u32 pass, void *image)
  437. {
  438. pr_err("flen=%u proglen=%u pass=%u image=%pK from=%s pid=%d\n", flen,
  439. proglen, pass, image, current->comm, task_pid_nr(current));
  440. if (image)
  441. print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_OFFSET,
  442. 16, 1, image, proglen, false);
  443. }
  444. static inline bool bpf_jit_is_ebpf(void)
  445. {
  446. # ifdef CONFIG_HAVE_EBPF_JIT
  447. return true;
  448. # else
  449. return false;
  450. # endif
  451. }
  452. static inline bool bpf_jit_blinding_enabled(void)
  453. {
  454. /* These are the prerequisites, should someone ever have the
  455. * idea to call blinding outside of them, we make sure to
  456. * bail out.
  457. */
  458. if (!bpf_jit_is_ebpf())
  459. return false;
  460. if (!bpf_jit_enable)
  461. return false;
  462. if (!bpf_jit_harden)
  463. return false;
  464. if (bpf_jit_harden == 1 && capable(CAP_SYS_ADMIN))
  465. return false;
  466. return true;
  467. }
  468. #else
  469. static inline void bpf_jit_compile(struct bpf_prog *fp)
  470. {
  471. }
  472. static inline void bpf_jit_free(struct bpf_prog *fp)
  473. {
  474. bpf_prog_unlock_free(fp);
  475. }
  476. #endif /* CONFIG_BPF_JIT */
  477. #define BPF_ANC BIT(15)
  478. static inline bool bpf_needs_clear_a(const struct sock_filter *first)
  479. {
  480. switch (first->code) {
  481. case BPF_RET | BPF_K:
  482. case BPF_LD | BPF_W | BPF_LEN:
  483. return false;
  484. case BPF_LD | BPF_W | BPF_ABS:
  485. case BPF_LD | BPF_H | BPF_ABS:
  486. case BPF_LD | BPF_B | BPF_ABS:
  487. if (first->k == SKF_AD_OFF + SKF_AD_ALU_XOR_X)
  488. return true;
  489. return false;
  490. default:
  491. return true;
  492. }
  493. }
  494. static inline u16 bpf_anc_helper(const struct sock_filter *ftest)
  495. {
  496. BUG_ON(ftest->code & BPF_ANC);
  497. switch (ftest->code) {
  498. case BPF_LD | BPF_W | BPF_ABS:
  499. case BPF_LD | BPF_H | BPF_ABS:
  500. case BPF_LD | BPF_B | BPF_ABS:
  501. #define BPF_ANCILLARY(CODE) case SKF_AD_OFF + SKF_AD_##CODE: \
  502. return BPF_ANC | SKF_AD_##CODE
  503. switch (ftest->k) {
  504. BPF_ANCILLARY(PROTOCOL);
  505. BPF_ANCILLARY(PKTTYPE);
  506. BPF_ANCILLARY(IFINDEX);
  507. BPF_ANCILLARY(NLATTR);
  508. BPF_ANCILLARY(NLATTR_NEST);
  509. BPF_ANCILLARY(MARK);
  510. BPF_ANCILLARY(QUEUE);
  511. BPF_ANCILLARY(HATYPE);
  512. BPF_ANCILLARY(RXHASH);
  513. BPF_ANCILLARY(CPU);
  514. BPF_ANCILLARY(ALU_XOR_X);
  515. BPF_ANCILLARY(VLAN_TAG);
  516. BPF_ANCILLARY(VLAN_TAG_PRESENT);
  517. BPF_ANCILLARY(PAY_OFFSET);
  518. BPF_ANCILLARY(RANDOM);
  519. BPF_ANCILLARY(VLAN_TPID);
  520. }
  521. /* Fallthrough. */
  522. default:
  523. return ftest->code;
  524. }
  525. }
  526. void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb,
  527. int k, unsigned int size);
  528. static inline void *bpf_load_pointer(const struct sk_buff *skb, int k,
  529. unsigned int size, void *buffer)
  530. {
  531. if (k >= 0)
  532. return skb_header_pointer(skb, k, size, buffer);
  533. return bpf_internal_load_pointer_neg_helper(skb, k, size);
  534. }
  535. static inline int bpf_tell_extensions(void)
  536. {
  537. return SKF_AD_MAX;
  538. }
  539. #endif /* __LINUX_FILTER_H__ */