core.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. /*
  2. * Linux Socket Filter - Kernel level socket filtering
  3. *
  4. * Based on the design of the Berkeley Packet Filter. The new
  5. * internal format has been designed by PLUMgrid:
  6. *
  7. * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
  8. *
  9. * Authors:
  10. *
  11. * Jay Schulist <jschlst@samba.org>
  12. * Alexei Starovoitov <ast@plumgrid.com>
  13. * Daniel Borkmann <dborkman@redhat.com>
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. *
  20. * Andi Kleen - Fix a few bad bugs and races.
  21. * Kris Katterjohn - Added many additional checks in bpf_check_classic()
  22. */
  23. #include <linux/filter.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/random.h>
  27. #include <linux/moduleloader.h>
  28. #include <linux/bpf.h>
  29. #include <linux/frame.h>
  30. #include <asm/unaligned.h>
  31. /* Registers */
  32. #define BPF_R0 regs[BPF_REG_0]
  33. #define BPF_R1 regs[BPF_REG_1]
  34. #define BPF_R2 regs[BPF_REG_2]
  35. #define BPF_R3 regs[BPF_REG_3]
  36. #define BPF_R4 regs[BPF_REG_4]
  37. #define BPF_R5 regs[BPF_REG_5]
  38. #define BPF_R6 regs[BPF_REG_6]
  39. #define BPF_R7 regs[BPF_REG_7]
  40. #define BPF_R8 regs[BPF_REG_8]
  41. #define BPF_R9 regs[BPF_REG_9]
  42. #define BPF_R10 regs[BPF_REG_10]
  43. /* Named registers */
  44. #define DST regs[insn->dst_reg]
  45. #define SRC regs[insn->src_reg]
  46. #define FP regs[BPF_REG_FP]
  47. #define ARG1 regs[BPF_REG_ARG1]
  48. #define CTX regs[BPF_REG_CTX]
  49. #define IMM insn->imm
  50. /* No hurry in this branch
  51. *
  52. * Exported for the bpf jit load helper.
  53. */
  54. void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size)
  55. {
  56. u8 *ptr = NULL;
  57. if (k >= SKF_NET_OFF)
  58. ptr = skb_network_header(skb) + k - SKF_NET_OFF;
  59. else if (k >= SKF_LL_OFF)
  60. ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
  61. if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
  62. return ptr;
  63. return NULL;
  64. }
  65. struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
  66. {
  67. gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO |
  68. gfp_extra_flags;
  69. struct bpf_prog_aux *aux;
  70. struct bpf_prog *fp;
  71. size = round_up(size, PAGE_SIZE);
  72. fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
  73. if (fp == NULL)
  74. return NULL;
  75. kmemcheck_annotate_bitfield(fp, meta);
  76. aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags);
  77. if (aux == NULL) {
  78. vfree(fp);
  79. return NULL;
  80. }
  81. fp->pages = size / PAGE_SIZE;
  82. fp->aux = aux;
  83. fp->aux->prog = fp;
  84. return fp;
  85. }
  86. EXPORT_SYMBOL_GPL(bpf_prog_alloc);
  87. struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
  88. gfp_t gfp_extra_flags)
  89. {
  90. gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO |
  91. gfp_extra_flags;
  92. struct bpf_prog *fp;
  93. BUG_ON(fp_old == NULL);
  94. size = round_up(size, PAGE_SIZE);
  95. if (size <= fp_old->pages * PAGE_SIZE)
  96. return fp_old;
  97. fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
  98. if (fp != NULL) {
  99. kmemcheck_annotate_bitfield(fp, meta);
  100. memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
  101. fp->pages = size / PAGE_SIZE;
  102. fp->aux->prog = fp;
  103. /* We keep fp->aux from fp_old around in the new
  104. * reallocated structure.
  105. */
  106. fp_old->aux = NULL;
  107. __bpf_prog_free(fp_old);
  108. }
  109. return fp;
  110. }
  111. void __bpf_prog_free(struct bpf_prog *fp)
  112. {
  113. kfree(fp->aux);
  114. vfree(fp);
  115. }
  116. static bool bpf_is_jmp_and_has_target(const struct bpf_insn *insn)
  117. {
  118. return BPF_CLASS(insn->code) == BPF_JMP &&
  119. /* Call and Exit are both special jumps with no
  120. * target inside the BPF instruction image.
  121. */
  122. BPF_OP(insn->code) != BPF_CALL &&
  123. BPF_OP(insn->code) != BPF_EXIT;
  124. }
  125. static void bpf_adj_branches(struct bpf_prog *prog, u32 pos, u32 delta)
  126. {
  127. struct bpf_insn *insn = prog->insnsi;
  128. u32 i, insn_cnt = prog->len;
  129. for (i = 0; i < insn_cnt; i++, insn++) {
  130. if (!bpf_is_jmp_and_has_target(insn))
  131. continue;
  132. /* Adjust offset of jmps if we cross boundaries. */
  133. if (i < pos && i + insn->off + 1 > pos)
  134. insn->off += delta;
  135. else if (i > pos + delta && i + insn->off + 1 <= pos + delta)
  136. insn->off -= delta;
  137. }
  138. }
  139. struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
  140. const struct bpf_insn *patch, u32 len)
  141. {
  142. u32 insn_adj_cnt, insn_rest, insn_delta = len - 1;
  143. struct bpf_prog *prog_adj;
  144. /* Since our patchlet doesn't expand the image, we're done. */
  145. if (insn_delta == 0) {
  146. memcpy(prog->insnsi + off, patch, sizeof(*patch));
  147. return prog;
  148. }
  149. insn_adj_cnt = prog->len + insn_delta;
  150. /* Several new instructions need to be inserted. Make room
  151. * for them. Likely, there's no need for a new allocation as
  152. * last page could have large enough tailroom.
  153. */
  154. prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt),
  155. GFP_USER);
  156. if (!prog_adj)
  157. return NULL;
  158. prog_adj->len = insn_adj_cnt;
  159. /* Patching happens in 3 steps:
  160. *
  161. * 1) Move over tail of insnsi from next instruction onwards,
  162. * so we can patch the single target insn with one or more
  163. * new ones (patching is always from 1 to n insns, n > 0).
  164. * 2) Inject new instructions at the target location.
  165. * 3) Adjust branch offsets if necessary.
  166. */
  167. insn_rest = insn_adj_cnt - off - len;
  168. memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1,
  169. sizeof(*patch) * insn_rest);
  170. memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len);
  171. bpf_adj_branches(prog_adj, off, insn_delta);
  172. return prog_adj;
  173. }
  174. #ifdef CONFIG_BPF_JIT
  175. struct bpf_binary_header *
  176. bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
  177. unsigned int alignment,
  178. bpf_jit_fill_hole_t bpf_fill_ill_insns)
  179. {
  180. struct bpf_binary_header *hdr;
  181. unsigned int size, hole, start;
  182. /* Most of BPF filters are really small, but if some of them
  183. * fill a page, allow at least 128 extra bytes to insert a
  184. * random section of illegal instructions.
  185. */
  186. size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
  187. hdr = module_alloc(size);
  188. if (hdr == NULL)
  189. return NULL;
  190. /* Fill space with illegal/arch-dep instructions. */
  191. bpf_fill_ill_insns(hdr, size);
  192. hdr->pages = size / PAGE_SIZE;
  193. hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
  194. PAGE_SIZE - sizeof(*hdr));
  195. start = (get_random_int() % hole) & ~(alignment - 1);
  196. /* Leave a random number of instructions before BPF code. */
  197. *image_ptr = &hdr->image[start];
  198. return hdr;
  199. }
  200. void bpf_jit_binary_free(struct bpf_binary_header *hdr)
  201. {
  202. module_memfree(hdr);
  203. }
  204. int bpf_jit_harden __read_mostly;
  205. static int bpf_jit_blind_insn(const struct bpf_insn *from,
  206. const struct bpf_insn *aux,
  207. struct bpf_insn *to_buff)
  208. {
  209. struct bpf_insn *to = to_buff;
  210. u32 imm_rnd = get_random_int();
  211. s16 off;
  212. BUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG);
  213. BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG);
  214. if (from->imm == 0 &&
  215. (from->code == (BPF_ALU | BPF_MOV | BPF_K) ||
  216. from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) {
  217. *to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg);
  218. goto out;
  219. }
  220. switch (from->code) {
  221. case BPF_ALU | BPF_ADD | BPF_K:
  222. case BPF_ALU | BPF_SUB | BPF_K:
  223. case BPF_ALU | BPF_AND | BPF_K:
  224. case BPF_ALU | BPF_OR | BPF_K:
  225. case BPF_ALU | BPF_XOR | BPF_K:
  226. case BPF_ALU | BPF_MUL | BPF_K:
  227. case BPF_ALU | BPF_MOV | BPF_K:
  228. case BPF_ALU | BPF_DIV | BPF_K:
  229. case BPF_ALU | BPF_MOD | BPF_K:
  230. *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
  231. *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  232. *to++ = BPF_ALU32_REG(from->code, from->dst_reg, BPF_REG_AX);
  233. break;
  234. case BPF_ALU64 | BPF_ADD | BPF_K:
  235. case BPF_ALU64 | BPF_SUB | BPF_K:
  236. case BPF_ALU64 | BPF_AND | BPF_K:
  237. case BPF_ALU64 | BPF_OR | BPF_K:
  238. case BPF_ALU64 | BPF_XOR | BPF_K:
  239. case BPF_ALU64 | BPF_MUL | BPF_K:
  240. case BPF_ALU64 | BPF_MOV | BPF_K:
  241. case BPF_ALU64 | BPF_DIV | BPF_K:
  242. case BPF_ALU64 | BPF_MOD | BPF_K:
  243. *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
  244. *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  245. *to++ = BPF_ALU64_REG(from->code, from->dst_reg, BPF_REG_AX);
  246. break;
  247. case BPF_JMP | BPF_JEQ | BPF_K:
  248. case BPF_JMP | BPF_JNE | BPF_K:
  249. case BPF_JMP | BPF_JGT | BPF_K:
  250. case BPF_JMP | BPF_JGE | BPF_K:
  251. case BPF_JMP | BPF_JSGT | BPF_K:
  252. case BPF_JMP | BPF_JSGE | BPF_K:
  253. case BPF_JMP | BPF_JSET | BPF_K:
  254. /* Accommodate for extra offset in case of a backjump. */
  255. off = from->off;
  256. if (off < 0)
  257. off -= 2;
  258. *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
  259. *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  260. *to++ = BPF_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off);
  261. break;
  262. case BPF_LD | BPF_ABS | BPF_W:
  263. case BPF_LD | BPF_ABS | BPF_H:
  264. case BPF_LD | BPF_ABS | BPF_B:
  265. *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
  266. *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  267. *to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);
  268. break;
  269. case BPF_LD | BPF_IND | BPF_W:
  270. case BPF_LD | BPF_IND | BPF_H:
  271. case BPF_LD | BPF_IND | BPF_B:
  272. *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
  273. *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  274. *to++ = BPF_ALU32_REG(BPF_ADD, BPF_REG_AX, from->src_reg);
  275. *to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);
  276. break;
  277. case BPF_LD | BPF_IMM | BPF_DW:
  278. *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm);
  279. *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  280. *to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);
  281. *to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX);
  282. break;
  283. case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */
  284. *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm);
  285. *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  286. *to++ = BPF_ALU64_REG(BPF_OR, aux[0].dst_reg, BPF_REG_AX);
  287. break;
  288. case BPF_ST | BPF_MEM | BPF_DW:
  289. case BPF_ST | BPF_MEM | BPF_W:
  290. case BPF_ST | BPF_MEM | BPF_H:
  291. case BPF_ST | BPF_MEM | BPF_B:
  292. *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
  293. *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  294. *to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off);
  295. break;
  296. }
  297. out:
  298. return to - to_buff;
  299. }
  300. static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other,
  301. gfp_t gfp_extra_flags)
  302. {
  303. gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO |
  304. gfp_extra_flags;
  305. struct bpf_prog *fp;
  306. fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags, PAGE_KERNEL);
  307. if (fp != NULL) {
  308. kmemcheck_annotate_bitfield(fp, meta);
  309. /* aux->prog still points to the fp_other one, so
  310. * when promoting the clone to the real program,
  311. * this still needs to be adapted.
  312. */
  313. memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE);
  314. }
  315. return fp;
  316. }
  317. static void bpf_prog_clone_free(struct bpf_prog *fp)
  318. {
  319. /* aux was stolen by the other clone, so we cannot free
  320. * it from this path! It will be freed eventually by the
  321. * other program on release.
  322. *
  323. * At this point, we don't need a deferred release since
  324. * clone is guaranteed to not be locked.
  325. */
  326. fp->aux = NULL;
  327. __bpf_prog_free(fp);
  328. }
  329. void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other)
  330. {
  331. /* We have to repoint aux->prog to self, as we don't
  332. * know whether fp here is the clone or the original.
  333. */
  334. fp->aux->prog = fp;
  335. bpf_prog_clone_free(fp_other);
  336. }
  337. struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog)
  338. {
  339. struct bpf_insn insn_buff[16], aux[2];
  340. struct bpf_prog *clone, *tmp;
  341. int insn_delta, insn_cnt;
  342. struct bpf_insn *insn;
  343. int i, rewritten;
  344. if (!bpf_jit_blinding_enabled())
  345. return prog;
  346. clone = bpf_prog_clone_create(prog, GFP_USER);
  347. if (!clone)
  348. return ERR_PTR(-ENOMEM);
  349. insn_cnt = clone->len;
  350. insn = clone->insnsi;
  351. for (i = 0; i < insn_cnt; i++, insn++) {
  352. /* We temporarily need to hold the original ld64 insn
  353. * so that we can still access the first part in the
  354. * second blinding run.
  355. */
  356. if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) &&
  357. insn[1].code == 0)
  358. memcpy(aux, insn, sizeof(aux));
  359. rewritten = bpf_jit_blind_insn(insn, aux, insn_buff);
  360. if (!rewritten)
  361. continue;
  362. tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten);
  363. if (!tmp) {
  364. /* Patching may have repointed aux->prog during
  365. * realloc from the original one, so we need to
  366. * fix it up here on error.
  367. */
  368. bpf_jit_prog_release_other(prog, clone);
  369. return ERR_PTR(-ENOMEM);
  370. }
  371. clone = tmp;
  372. insn_delta = rewritten - 1;
  373. /* Walk new program and skip insns we just inserted. */
  374. insn = clone->insnsi + i + insn_delta;
  375. insn_cnt += insn_delta;
  376. i += insn_delta;
  377. }
  378. return clone;
  379. }
  380. #endif /* CONFIG_BPF_JIT */
  381. /* Base function for offset calculation. Needs to go into .text section,
  382. * therefore keeping it non-static as well; will also be used by JITs
  383. * anyway later on, so do not let the compiler omit it.
  384. */
  385. noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  386. {
  387. return 0;
  388. }
  389. EXPORT_SYMBOL_GPL(__bpf_call_base);
  390. /**
  391. * __bpf_prog_run - run eBPF program on a given context
  392. * @ctx: is the data we are operating on
  393. * @insn: is the array of eBPF instructions
  394. *
  395. * Decode and execute eBPF instructions.
  396. */
  397. static unsigned int __bpf_prog_run(void *ctx, const struct bpf_insn *insn)
  398. {
  399. u64 stack[MAX_BPF_STACK / sizeof(u64)];
  400. u64 regs[MAX_BPF_REG], tmp;
  401. static const void *jumptable[256] = {
  402. [0 ... 255] = &&default_label,
  403. /* Now overwrite non-defaults ... */
  404. /* 32 bit ALU operations */
  405. [BPF_ALU | BPF_ADD | BPF_X] = &&ALU_ADD_X,
  406. [BPF_ALU | BPF_ADD | BPF_K] = &&ALU_ADD_K,
  407. [BPF_ALU | BPF_SUB | BPF_X] = &&ALU_SUB_X,
  408. [BPF_ALU | BPF_SUB | BPF_K] = &&ALU_SUB_K,
  409. [BPF_ALU | BPF_AND | BPF_X] = &&ALU_AND_X,
  410. [BPF_ALU | BPF_AND | BPF_K] = &&ALU_AND_K,
  411. [BPF_ALU | BPF_OR | BPF_X] = &&ALU_OR_X,
  412. [BPF_ALU | BPF_OR | BPF_K] = &&ALU_OR_K,
  413. [BPF_ALU | BPF_LSH | BPF_X] = &&ALU_LSH_X,
  414. [BPF_ALU | BPF_LSH | BPF_K] = &&ALU_LSH_K,
  415. [BPF_ALU | BPF_RSH | BPF_X] = &&ALU_RSH_X,
  416. [BPF_ALU | BPF_RSH | BPF_K] = &&ALU_RSH_K,
  417. [BPF_ALU | BPF_XOR | BPF_X] = &&ALU_XOR_X,
  418. [BPF_ALU | BPF_XOR | BPF_K] = &&ALU_XOR_K,
  419. [BPF_ALU | BPF_MUL | BPF_X] = &&ALU_MUL_X,
  420. [BPF_ALU | BPF_MUL | BPF_K] = &&ALU_MUL_K,
  421. [BPF_ALU | BPF_MOV | BPF_X] = &&ALU_MOV_X,
  422. [BPF_ALU | BPF_MOV | BPF_K] = &&ALU_MOV_K,
  423. [BPF_ALU | BPF_DIV | BPF_X] = &&ALU_DIV_X,
  424. [BPF_ALU | BPF_DIV | BPF_K] = &&ALU_DIV_K,
  425. [BPF_ALU | BPF_MOD | BPF_X] = &&ALU_MOD_X,
  426. [BPF_ALU | BPF_MOD | BPF_K] = &&ALU_MOD_K,
  427. [BPF_ALU | BPF_NEG] = &&ALU_NEG,
  428. [BPF_ALU | BPF_END | BPF_TO_BE] = &&ALU_END_TO_BE,
  429. [BPF_ALU | BPF_END | BPF_TO_LE] = &&ALU_END_TO_LE,
  430. /* 64 bit ALU operations */
  431. [BPF_ALU64 | BPF_ADD | BPF_X] = &&ALU64_ADD_X,
  432. [BPF_ALU64 | BPF_ADD | BPF_K] = &&ALU64_ADD_K,
  433. [BPF_ALU64 | BPF_SUB | BPF_X] = &&ALU64_SUB_X,
  434. [BPF_ALU64 | BPF_SUB | BPF_K] = &&ALU64_SUB_K,
  435. [BPF_ALU64 | BPF_AND | BPF_X] = &&ALU64_AND_X,
  436. [BPF_ALU64 | BPF_AND | BPF_K] = &&ALU64_AND_K,
  437. [BPF_ALU64 | BPF_OR | BPF_X] = &&ALU64_OR_X,
  438. [BPF_ALU64 | BPF_OR | BPF_K] = &&ALU64_OR_K,
  439. [BPF_ALU64 | BPF_LSH | BPF_X] = &&ALU64_LSH_X,
  440. [BPF_ALU64 | BPF_LSH | BPF_K] = &&ALU64_LSH_K,
  441. [BPF_ALU64 | BPF_RSH | BPF_X] = &&ALU64_RSH_X,
  442. [BPF_ALU64 | BPF_RSH | BPF_K] = &&ALU64_RSH_K,
  443. [BPF_ALU64 | BPF_XOR | BPF_X] = &&ALU64_XOR_X,
  444. [BPF_ALU64 | BPF_XOR | BPF_K] = &&ALU64_XOR_K,
  445. [BPF_ALU64 | BPF_MUL | BPF_X] = &&ALU64_MUL_X,
  446. [BPF_ALU64 | BPF_MUL | BPF_K] = &&ALU64_MUL_K,
  447. [BPF_ALU64 | BPF_MOV | BPF_X] = &&ALU64_MOV_X,
  448. [BPF_ALU64 | BPF_MOV | BPF_K] = &&ALU64_MOV_K,
  449. [BPF_ALU64 | BPF_ARSH | BPF_X] = &&ALU64_ARSH_X,
  450. [BPF_ALU64 | BPF_ARSH | BPF_K] = &&ALU64_ARSH_K,
  451. [BPF_ALU64 | BPF_DIV | BPF_X] = &&ALU64_DIV_X,
  452. [BPF_ALU64 | BPF_DIV | BPF_K] = &&ALU64_DIV_K,
  453. [BPF_ALU64 | BPF_MOD | BPF_X] = &&ALU64_MOD_X,
  454. [BPF_ALU64 | BPF_MOD | BPF_K] = &&ALU64_MOD_K,
  455. [BPF_ALU64 | BPF_NEG] = &&ALU64_NEG,
  456. /* Call instruction */
  457. [BPF_JMP | BPF_CALL] = &&JMP_CALL,
  458. [BPF_JMP | BPF_CALL | BPF_X] = &&JMP_TAIL_CALL,
  459. /* Jumps */
  460. [BPF_JMP | BPF_JA] = &&JMP_JA,
  461. [BPF_JMP | BPF_JEQ | BPF_X] = &&JMP_JEQ_X,
  462. [BPF_JMP | BPF_JEQ | BPF_K] = &&JMP_JEQ_K,
  463. [BPF_JMP | BPF_JNE | BPF_X] = &&JMP_JNE_X,
  464. [BPF_JMP | BPF_JNE | BPF_K] = &&JMP_JNE_K,
  465. [BPF_JMP | BPF_JGT | BPF_X] = &&JMP_JGT_X,
  466. [BPF_JMP | BPF_JGT | BPF_K] = &&JMP_JGT_K,
  467. [BPF_JMP | BPF_JGE | BPF_X] = &&JMP_JGE_X,
  468. [BPF_JMP | BPF_JGE | BPF_K] = &&JMP_JGE_K,
  469. [BPF_JMP | BPF_JSGT | BPF_X] = &&JMP_JSGT_X,
  470. [BPF_JMP | BPF_JSGT | BPF_K] = &&JMP_JSGT_K,
  471. [BPF_JMP | BPF_JSGE | BPF_X] = &&JMP_JSGE_X,
  472. [BPF_JMP | BPF_JSGE | BPF_K] = &&JMP_JSGE_K,
  473. [BPF_JMP | BPF_JSET | BPF_X] = &&JMP_JSET_X,
  474. [BPF_JMP | BPF_JSET | BPF_K] = &&JMP_JSET_K,
  475. /* Program return */
  476. [BPF_JMP | BPF_EXIT] = &&JMP_EXIT,
  477. /* Store instructions */
  478. [BPF_STX | BPF_MEM | BPF_B] = &&STX_MEM_B,
  479. [BPF_STX | BPF_MEM | BPF_H] = &&STX_MEM_H,
  480. [BPF_STX | BPF_MEM | BPF_W] = &&STX_MEM_W,
  481. [BPF_STX | BPF_MEM | BPF_DW] = &&STX_MEM_DW,
  482. [BPF_STX | BPF_XADD | BPF_W] = &&STX_XADD_W,
  483. [BPF_STX | BPF_XADD | BPF_DW] = &&STX_XADD_DW,
  484. [BPF_ST | BPF_MEM | BPF_B] = &&ST_MEM_B,
  485. [BPF_ST | BPF_MEM | BPF_H] = &&ST_MEM_H,
  486. [BPF_ST | BPF_MEM | BPF_W] = &&ST_MEM_W,
  487. [BPF_ST | BPF_MEM | BPF_DW] = &&ST_MEM_DW,
  488. /* Load instructions */
  489. [BPF_LDX | BPF_MEM | BPF_B] = &&LDX_MEM_B,
  490. [BPF_LDX | BPF_MEM | BPF_H] = &&LDX_MEM_H,
  491. [BPF_LDX | BPF_MEM | BPF_W] = &&LDX_MEM_W,
  492. [BPF_LDX | BPF_MEM | BPF_DW] = &&LDX_MEM_DW,
  493. [BPF_LD | BPF_ABS | BPF_W] = &&LD_ABS_W,
  494. [BPF_LD | BPF_ABS | BPF_H] = &&LD_ABS_H,
  495. [BPF_LD | BPF_ABS | BPF_B] = &&LD_ABS_B,
  496. [BPF_LD | BPF_IND | BPF_W] = &&LD_IND_W,
  497. [BPF_LD | BPF_IND | BPF_H] = &&LD_IND_H,
  498. [BPF_LD | BPF_IND | BPF_B] = &&LD_IND_B,
  499. [BPF_LD | BPF_IMM | BPF_DW] = &&LD_IMM_DW,
  500. };
  501. u32 tail_call_cnt = 0;
  502. void *ptr;
  503. int off;
  504. #define CONT ({ insn++; goto select_insn; })
  505. #define CONT_JMP ({ insn++; goto select_insn; })
  506. FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)];
  507. ARG1 = (u64) (unsigned long) ctx;
  508. select_insn:
  509. goto *jumptable[insn->code];
  510. /* ALU */
  511. #define ALU(OPCODE, OP) \
  512. ALU64_##OPCODE##_X: \
  513. DST = DST OP SRC; \
  514. CONT; \
  515. ALU_##OPCODE##_X: \
  516. DST = (u32) DST OP (u32) SRC; \
  517. CONT; \
  518. ALU64_##OPCODE##_K: \
  519. DST = DST OP IMM; \
  520. CONT; \
  521. ALU_##OPCODE##_K: \
  522. DST = (u32) DST OP (u32) IMM; \
  523. CONT;
  524. ALU(ADD, +)
  525. ALU(SUB, -)
  526. ALU(AND, &)
  527. ALU(OR, |)
  528. ALU(LSH, <<)
  529. ALU(RSH, >>)
  530. ALU(XOR, ^)
  531. ALU(MUL, *)
  532. #undef ALU
  533. ALU_NEG:
  534. DST = (u32) -DST;
  535. CONT;
  536. ALU64_NEG:
  537. DST = -DST;
  538. CONT;
  539. ALU_MOV_X:
  540. DST = (u32) SRC;
  541. CONT;
  542. ALU_MOV_K:
  543. DST = (u32) IMM;
  544. CONT;
  545. ALU64_MOV_X:
  546. DST = SRC;
  547. CONT;
  548. ALU64_MOV_K:
  549. DST = IMM;
  550. CONT;
  551. LD_IMM_DW:
  552. DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
  553. insn++;
  554. CONT;
  555. ALU64_ARSH_X:
  556. (*(s64 *) &DST) >>= SRC;
  557. CONT;
  558. ALU64_ARSH_K:
  559. (*(s64 *) &DST) >>= IMM;
  560. CONT;
  561. ALU64_MOD_X:
  562. if (unlikely(SRC == 0))
  563. return 0;
  564. div64_u64_rem(DST, SRC, &tmp);
  565. DST = tmp;
  566. CONT;
  567. ALU_MOD_X:
  568. if (unlikely(SRC == 0))
  569. return 0;
  570. tmp = (u32) DST;
  571. DST = do_div(tmp, (u32) SRC);
  572. CONT;
  573. ALU64_MOD_K:
  574. div64_u64_rem(DST, IMM, &tmp);
  575. DST = tmp;
  576. CONT;
  577. ALU_MOD_K:
  578. tmp = (u32) DST;
  579. DST = do_div(tmp, (u32) IMM);
  580. CONT;
  581. ALU64_DIV_X:
  582. if (unlikely(SRC == 0))
  583. return 0;
  584. DST = div64_u64(DST, SRC);
  585. CONT;
  586. ALU_DIV_X:
  587. if (unlikely(SRC == 0))
  588. return 0;
  589. tmp = (u32) DST;
  590. do_div(tmp, (u32) SRC);
  591. DST = (u32) tmp;
  592. CONT;
  593. ALU64_DIV_K:
  594. DST = div64_u64(DST, IMM);
  595. CONT;
  596. ALU_DIV_K:
  597. tmp = (u32) DST;
  598. do_div(tmp, (u32) IMM);
  599. DST = (u32) tmp;
  600. CONT;
  601. ALU_END_TO_BE:
  602. switch (IMM) {
  603. case 16:
  604. DST = (__force u16) cpu_to_be16(DST);
  605. break;
  606. case 32:
  607. DST = (__force u32) cpu_to_be32(DST);
  608. break;
  609. case 64:
  610. DST = (__force u64) cpu_to_be64(DST);
  611. break;
  612. }
  613. CONT;
  614. ALU_END_TO_LE:
  615. switch (IMM) {
  616. case 16:
  617. DST = (__force u16) cpu_to_le16(DST);
  618. break;
  619. case 32:
  620. DST = (__force u32) cpu_to_le32(DST);
  621. break;
  622. case 64:
  623. DST = (__force u64) cpu_to_le64(DST);
  624. break;
  625. }
  626. CONT;
  627. /* CALL */
  628. JMP_CALL:
  629. /* Function call scratches BPF_R1-BPF_R5 registers,
  630. * preserves BPF_R6-BPF_R9, and stores return value
  631. * into BPF_R0.
  632. */
  633. BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
  634. BPF_R4, BPF_R5);
  635. CONT;
  636. JMP_TAIL_CALL: {
  637. struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
  638. struct bpf_array *array = container_of(map, struct bpf_array, map);
  639. struct bpf_prog *prog;
  640. u64 index = BPF_R3;
  641. if (unlikely(index >= array->map.max_entries))
  642. goto out;
  643. if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT))
  644. goto out;
  645. tail_call_cnt++;
  646. prog = READ_ONCE(array->ptrs[index]);
  647. if (!prog)
  648. goto out;
  649. /* ARG1 at this point is guaranteed to point to CTX from
  650. * the verifier side due to the fact that the tail call is
  651. * handeled like a helper, that is, bpf_tail_call_proto,
  652. * where arg1_type is ARG_PTR_TO_CTX.
  653. */
  654. insn = prog->insnsi;
  655. goto select_insn;
  656. out:
  657. CONT;
  658. }
  659. /* JMP */
  660. JMP_JA:
  661. insn += insn->off;
  662. CONT;
  663. JMP_JEQ_X:
  664. if (DST == SRC) {
  665. insn += insn->off;
  666. CONT_JMP;
  667. }
  668. CONT;
  669. JMP_JEQ_K:
  670. if (DST == IMM) {
  671. insn += insn->off;
  672. CONT_JMP;
  673. }
  674. CONT;
  675. JMP_JNE_X:
  676. if (DST != SRC) {
  677. insn += insn->off;
  678. CONT_JMP;
  679. }
  680. CONT;
  681. JMP_JNE_K:
  682. if (DST != IMM) {
  683. insn += insn->off;
  684. CONT_JMP;
  685. }
  686. CONT;
  687. JMP_JGT_X:
  688. if (DST > SRC) {
  689. insn += insn->off;
  690. CONT_JMP;
  691. }
  692. CONT;
  693. JMP_JGT_K:
  694. if (DST > IMM) {
  695. insn += insn->off;
  696. CONT_JMP;
  697. }
  698. CONT;
  699. JMP_JGE_X:
  700. if (DST >= SRC) {
  701. insn += insn->off;
  702. CONT_JMP;
  703. }
  704. CONT;
  705. JMP_JGE_K:
  706. if (DST >= IMM) {
  707. insn += insn->off;
  708. CONT_JMP;
  709. }
  710. CONT;
  711. JMP_JSGT_X:
  712. if (((s64) DST) > ((s64) SRC)) {
  713. insn += insn->off;
  714. CONT_JMP;
  715. }
  716. CONT;
  717. JMP_JSGT_K:
  718. if (((s64) DST) > ((s64) IMM)) {
  719. insn += insn->off;
  720. CONT_JMP;
  721. }
  722. CONT;
  723. JMP_JSGE_X:
  724. if (((s64) DST) >= ((s64) SRC)) {
  725. insn += insn->off;
  726. CONT_JMP;
  727. }
  728. CONT;
  729. JMP_JSGE_K:
  730. if (((s64) DST) >= ((s64) IMM)) {
  731. insn += insn->off;
  732. CONT_JMP;
  733. }
  734. CONT;
  735. JMP_JSET_X:
  736. if (DST & SRC) {
  737. insn += insn->off;
  738. CONT_JMP;
  739. }
  740. CONT;
  741. JMP_JSET_K:
  742. if (DST & IMM) {
  743. insn += insn->off;
  744. CONT_JMP;
  745. }
  746. CONT;
  747. JMP_EXIT:
  748. return BPF_R0;
  749. /* STX and ST and LDX*/
  750. #define LDST(SIZEOP, SIZE) \
  751. STX_MEM_##SIZEOP: \
  752. *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \
  753. CONT; \
  754. ST_MEM_##SIZEOP: \
  755. *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \
  756. CONT; \
  757. LDX_MEM_##SIZEOP: \
  758. DST = *(SIZE *)(unsigned long) (SRC + insn->off); \
  759. CONT;
  760. LDST(B, u8)
  761. LDST(H, u16)
  762. LDST(W, u32)
  763. LDST(DW, u64)
  764. #undef LDST
  765. STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */
  766. atomic_add((u32) SRC, (atomic_t *)(unsigned long)
  767. (DST + insn->off));
  768. CONT;
  769. STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */
  770. atomic64_add((u64) SRC, (atomic64_t *)(unsigned long)
  771. (DST + insn->off));
  772. CONT;
  773. LD_ABS_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + imm32)) */
  774. off = IMM;
  775. load_word:
  776. /* BPF_LD + BPD_ABS and BPF_LD + BPF_IND insns are
  777. * only appearing in the programs where ctx ==
  778. * skb. All programs keep 'ctx' in regs[BPF_REG_CTX]
  779. * == BPF_R6, bpf_convert_filter() saves it in BPF_R6,
  780. * internal BPF verifier will check that BPF_R6 ==
  781. * ctx.
  782. *
  783. * BPF_ABS and BPF_IND are wrappers of function calls,
  784. * so they scratch BPF_R1-BPF_R5 registers, preserve
  785. * BPF_R6-BPF_R9, and store return value into BPF_R0.
  786. *
  787. * Implicit input:
  788. * ctx == skb == BPF_R6 == CTX
  789. *
  790. * Explicit input:
  791. * SRC == any register
  792. * IMM == 32-bit immediate
  793. *
  794. * Output:
  795. * BPF_R0 - 8/16/32-bit skb data converted to cpu endianness
  796. */
  797. ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 4, &tmp);
  798. if (likely(ptr != NULL)) {
  799. BPF_R0 = get_unaligned_be32(ptr);
  800. CONT;
  801. }
  802. return 0;
  803. LD_ABS_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + imm32)) */
  804. off = IMM;
  805. load_half:
  806. ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 2, &tmp);
  807. if (likely(ptr != NULL)) {
  808. BPF_R0 = get_unaligned_be16(ptr);
  809. CONT;
  810. }
  811. return 0;
  812. LD_ABS_B: /* BPF_R0 = *(u8 *) (skb->data + imm32) */
  813. off = IMM;
  814. load_byte:
  815. ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 1, &tmp);
  816. if (likely(ptr != NULL)) {
  817. BPF_R0 = *(u8 *)ptr;
  818. CONT;
  819. }
  820. return 0;
  821. LD_IND_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + src_reg + imm32)) */
  822. off = IMM + SRC;
  823. goto load_word;
  824. LD_IND_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + src_reg + imm32)) */
  825. off = IMM + SRC;
  826. goto load_half;
  827. LD_IND_B: /* BPF_R0 = *(u8 *) (skb->data + src_reg + imm32) */
  828. off = IMM + SRC;
  829. goto load_byte;
  830. default_label:
  831. /* If we ever reach this, we have a bug somewhere. */
  832. WARN_RATELIMIT(1, "unknown opcode %02x\n", insn->code);
  833. return 0;
  834. }
  835. STACK_FRAME_NON_STANDARD(__bpf_prog_run); /* jump table */
  836. bool bpf_prog_array_compatible(struct bpf_array *array,
  837. const struct bpf_prog *fp)
  838. {
  839. if (!array->owner_prog_type) {
  840. /* There's no owner yet where we could check for
  841. * compatibility.
  842. */
  843. array->owner_prog_type = fp->type;
  844. array->owner_jited = fp->jited;
  845. return true;
  846. }
  847. return array->owner_prog_type == fp->type &&
  848. array->owner_jited == fp->jited;
  849. }
  850. static int bpf_check_tail_call(const struct bpf_prog *fp)
  851. {
  852. struct bpf_prog_aux *aux = fp->aux;
  853. int i;
  854. for (i = 0; i < aux->used_map_cnt; i++) {
  855. struct bpf_map *map = aux->used_maps[i];
  856. struct bpf_array *array;
  857. if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
  858. continue;
  859. array = container_of(map, struct bpf_array, map);
  860. if (!bpf_prog_array_compatible(array, fp))
  861. return -EINVAL;
  862. }
  863. return 0;
  864. }
  865. /**
  866. * bpf_prog_select_runtime - select exec runtime for BPF program
  867. * @fp: bpf_prog populated with internal BPF program
  868. * @err: pointer to error variable
  869. *
  870. * Try to JIT eBPF program, if JIT is not available, use interpreter.
  871. * The BPF program will be executed via BPF_PROG_RUN() macro.
  872. */
  873. struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
  874. {
  875. fp->bpf_func = (void *) __bpf_prog_run;
  876. /* eBPF JITs can rewrite the program in case constant
  877. * blinding is active. However, in case of error during
  878. * blinding, bpf_int_jit_compile() must always return a
  879. * valid program, which in this case would simply not
  880. * be JITed, but falls back to the interpreter.
  881. */
  882. fp = bpf_int_jit_compile(fp);
  883. bpf_prog_lock_ro(fp);
  884. /* The tail call compatibility check can only be done at
  885. * this late stage as we need to determine, if we deal
  886. * with JITed or non JITed program concatenations and not
  887. * all eBPF JITs might immediately support all features.
  888. */
  889. *err = bpf_check_tail_call(fp);
  890. return fp;
  891. }
  892. EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
  893. static void bpf_prog_free_deferred(struct work_struct *work)
  894. {
  895. struct bpf_prog_aux *aux;
  896. aux = container_of(work, struct bpf_prog_aux, work);
  897. bpf_jit_free(aux->prog);
  898. }
  899. /* Free internal BPF program */
  900. void bpf_prog_free(struct bpf_prog *fp)
  901. {
  902. struct bpf_prog_aux *aux = fp->aux;
  903. INIT_WORK(&aux->work, bpf_prog_free_deferred);
  904. schedule_work(&aux->work);
  905. }
  906. EXPORT_SYMBOL_GPL(bpf_prog_free);
  907. /* RNG for unpriviledged user space with separated state from prandom_u32(). */
  908. static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state);
  909. void bpf_user_rnd_init_once(void)
  910. {
  911. prandom_init_once(&bpf_user_rnd_state);
  912. }
  913. BPF_CALL_0(bpf_user_rnd_u32)
  914. {
  915. /* Should someone ever have the rather unwise idea to use some
  916. * of the registers passed into this function, then note that
  917. * this function is called from native eBPF and classic-to-eBPF
  918. * transformations. Register assignments from both sides are
  919. * different, f.e. classic always sets fn(ctx, A, X) here.
  920. */
  921. struct rnd_state *state;
  922. u32 res;
  923. state = &get_cpu_var(bpf_user_rnd_state);
  924. res = prandom_u32_state(state);
  925. put_cpu_var(bpf_user_rnd_state);
  926. return res;
  927. }
  928. /* Weak definitions of helper functions in case we don't have bpf syscall. */
  929. const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
  930. const struct bpf_func_proto bpf_map_update_elem_proto __weak;
  931. const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
  932. const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
  933. const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
  934. const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
  935. const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
  936. const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
  937. const struct bpf_func_proto bpf_get_current_comm_proto __weak;
  938. const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
  939. {
  940. return NULL;
  941. }
  942. u64 __weak
  943. bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
  944. void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
  945. {
  946. return -ENOTSUPP;
  947. }
  948. /* Always built-in helper functions. */
  949. const struct bpf_func_proto bpf_tail_call_proto = {
  950. .func = NULL,
  951. .gpl_only = false,
  952. .ret_type = RET_VOID,
  953. .arg1_type = ARG_PTR_TO_CTX,
  954. .arg2_type = ARG_CONST_MAP_PTR,
  955. .arg3_type = ARG_ANYTHING,
  956. };
  957. /* For classic BPF JITs that don't implement bpf_int_jit_compile(). */
  958. struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog)
  959. {
  960. return prog;
  961. }
  962. bool __weak bpf_helper_changes_skb_data(void *func)
  963. {
  964. return false;
  965. }
  966. /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
  967. * skb_copy_bits(), so provide a weak definition of it for NET-less config.
  968. */
  969. int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
  970. int len)
  971. {
  972. return -EFAULT;
  973. }