1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Linux Socket Filter Data Structures 4 */ 5 #ifndef __LINUX_FILTER_H__ 6 #define __LINUX_FILTER_H__ 7 8 #include <linux/atomic.h> 9 #include <linux/bpf.h> 10 #include <linux/refcount.h> 11 #include <linux/compat.h> 12 #include <linux/skbuff.h> 13 #include <linux/linkage.h> 14 #include <linux/printk.h> 15 #include <linux/workqueue.h> 16 #include <linux/sched.h> 17 #include <linux/sched/clock.h> 18 #include <linux/capability.h> 19 #include <linux/set_memory.h> 20 #include <linux/kallsyms.h> 21 #include <linux/if_vlan.h> 22 #include <linux/vmalloc.h> 23 #include <linux/sockptr.h> 24 #include <linux/static_call.h> 25 #include <linux/u64_stats_sync.h> 26 27 #include <net/sch_generic.h> 28 29 #include <asm/byteorder.h> 30 #include <uapi/linux/filter.h> 31 32 struct sk_buff; 33 struct sock; 34 struct seccomp_data; 35 struct bpf_prog_aux; 36 struct xdp_rxq_info; 37 struct xdp_buff; 38 struct sock_reuseport; 39 struct ctl_table; 40 struct ctl_table_header; 41 42 /* ArgX, context and stack frame pointer register positions. Note, 43 * Arg1, Arg2, Arg3, etc are used as argument mappings of function 44 * calls in BPF_CALL instruction. 45 */ 46 #define BPF_REG_ARG1 BPF_REG_1 47 #define BPF_REG_ARG2 BPF_REG_2 48 #define BPF_REG_ARG3 BPF_REG_3 49 #define BPF_REG_ARG4 BPF_REG_4 50 #define BPF_REG_ARG5 BPF_REG_5 51 #define BPF_REG_CTX BPF_REG_6 52 #define BPF_REG_FP BPF_REG_10 53 54 /* Additional register mappings for converted user programs. */ 55 #define BPF_REG_A BPF_REG_0 56 #define BPF_REG_X BPF_REG_7 57 #define BPF_REG_TMP BPF_REG_2 /* scratch reg */ 58 #define BPF_REG_D BPF_REG_8 /* data, callee-saved */ 59 #define BPF_REG_H BPF_REG_9 /* hlen, callee-saved */ 60 61 /* Kernel hidden auxiliary/helper register. */ 62 #define BPF_REG_PARAMS MAX_BPF_REG 63 #define BPF_REG_AX (MAX_BPF_REG + 1) 64 #define MAX_BPF_EXT_REG (MAX_BPF_REG + 2) 65 #define MAX_BPF_JIT_REG MAX_BPF_EXT_REG 66 67 /* unused opcode to mark special call to bpf_tail_call() helper */ 68 #define BPF_TAIL_CALL 0xf0 69 70 /* unused opcode to mark special load instruction. Same as BPF_ABS */ 71 #define BPF_PROBE_MEM 0x20 72 73 /* unused opcode to mark special ldsx instruction. Same as BPF_IND */ 74 #define BPF_PROBE_MEMSX 0x40 75 76 /* unused opcode to mark special load instruction. Same as BPF_MSH */ 77 #define BPF_PROBE_MEM32 0xa0 78 79 /* unused opcode to mark special atomic instruction */ 80 #define BPF_PROBE_ATOMIC 0xe0 81 82 /* unused opcode to mark special ldsx instruction. Same as BPF_NOSPEC */ 83 #define BPF_PROBE_MEM32SX 0xc0 84 85 /* unused opcode to mark call to interpreter with arguments */ 86 #define BPF_CALL_ARGS 0xe0 87 88 /* unused opcode to mark speculation barrier for mitigating 89 * Spectre v1 and v4 90 */ 91 #define BPF_NOSPEC 0xc0 92 93 /* As per nm, we expose JITed images as text (code) section for 94 * kallsyms. That way, tools like perf can find it to match 95 * addresses. 96 */ 97 #define BPF_SYM_ELF_TYPE 't' 98 99 /* BPF program can access up to 512 bytes of stack space. */ 100 #define MAX_BPF_STACK 512 101 /* 102 * Stack budget of a program on a JIT that lays out frames of that size. 103 * The interpreter and JITs without such support keep MAX_BPF_STACK. 104 */ 105 #define MAX_BPF_STACK_JIT 2048 106 107 /* Helper macros for filter block array initializers. */ 108 109 /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */ 110 111 #define BPF_ALU64_REG_OFF(OP, DST, SRC, OFF) \ 112 ((struct bpf_insn) { \ 113 .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \ 114 .dst_reg = DST, \ 115 .src_reg = SRC, \ 116 .off = OFF, \ 117 .imm = 0 }) 118 119 #define BPF_ALU64_REG(OP, DST, SRC) \ 120 BPF_ALU64_REG_OFF(OP, DST, SRC, 0) 121 122 #define BPF_ALU32_REG_OFF(OP, DST, SRC, OFF) \ 123 ((struct bpf_insn) { \ 124 .code = BPF_ALU | BPF_OP(OP) | BPF_X, \ 125 .dst_reg = DST, \ 126 .src_reg = SRC, \ 127 .off = OFF, \ 128 .imm = 0 }) 129 130 #define BPF_ALU32_REG(OP, DST, SRC) \ 131 BPF_ALU32_REG_OFF(OP, DST, SRC, 0) 132 133 /* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */ 134 135 #define BPF_ALU64_IMM_OFF(OP, DST, IMM, OFF) \ 136 ((struct bpf_insn) { \ 137 .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \ 138 .dst_reg = DST, \ 139 .src_reg = 0, \ 140 .off = OFF, \ 141 .imm = IMM }) 142 #define BPF_ALU64_IMM(OP, DST, IMM) \ 143 BPF_ALU64_IMM_OFF(OP, DST, IMM, 0) 144 145 #define BPF_ALU32_IMM_OFF(OP, DST, IMM, OFF) \ 146 ((struct bpf_insn) { \ 147 .code = BPF_ALU | BPF_OP(OP) | BPF_K, \ 148 .dst_reg = DST, \ 149 .src_reg = 0, \ 150 .off = OFF, \ 151 .imm = IMM }) 152 #define BPF_ALU32_IMM(OP, DST, IMM) \ 153 BPF_ALU32_IMM_OFF(OP, DST, IMM, 0) 154 155 /* Endianess conversion, cpu_to_{l,b}e(), {l,b}e_to_cpu() */ 156 157 #define BPF_ENDIAN(TYPE, DST, LEN) \ 158 ((struct bpf_insn) { \ 159 .code = BPF_ALU | BPF_END | BPF_SRC(TYPE), \ 160 .dst_reg = DST, \ 161 .src_reg = 0, \ 162 .off = 0, \ 163 .imm = LEN }) 164 165 /* Byte Swap, bswap16/32/64 */ 166 167 #define BPF_BSWAP(DST, LEN) \ 168 ((struct bpf_insn) { \ 169 .code = BPF_ALU64 | BPF_END | BPF_SRC(BPF_TO_LE), \ 170 .dst_reg = DST, \ 171 .src_reg = 0, \ 172 .off = 0, \ 173 .imm = LEN }) 174 175 /* Short form of mov, dst_reg = src_reg */ 176 177 #define BPF_MOV64_REG(DST, SRC) \ 178 ((struct bpf_insn) { \ 179 .code = BPF_ALU64 | BPF_MOV | BPF_X, \ 180 .dst_reg = DST, \ 181 .src_reg = SRC, \ 182 .off = 0, \ 183 .imm = 0 }) 184 185 #define BPF_MOV32_REG(DST, SRC) \ 186 ((struct bpf_insn) { \ 187 .code = BPF_ALU | BPF_MOV | BPF_X, \ 188 .dst_reg = DST, \ 189 .src_reg = SRC, \ 190 .off = 0, \ 191 .imm = 0 }) 192 193 /* Special (internal-only) form of mov, used to resolve per-CPU addrs: 194 * dst_reg = src_reg + <percpu_base_off> 195 * BPF_ADDR_PERCPU is used as a special insn->off value. 196 */ 197 #define BPF_ADDR_PERCPU (-1) 198 199 #define BPF_MOV64_PERCPU_REG(DST, SRC) \ 200 ((struct bpf_insn) { \ 201 .code = BPF_ALU64 | BPF_MOV | BPF_X, \ 202 .dst_reg = DST, \ 203 .src_reg = SRC, \ 204 .off = BPF_ADDR_PERCPU, \ 205 .imm = 0 }) 206 207 static inline bool insn_is_mov_percpu_addr(const struct bpf_insn *insn) 208 { 209 return insn->code == (BPF_ALU64 | BPF_MOV | BPF_X) && insn->off == BPF_ADDR_PERCPU; 210 } 211 212 /* Short form of mov, dst_reg = imm32 */ 213 214 #define BPF_MOV64_IMM(DST, IMM) \ 215 ((struct bpf_insn) { \ 216 .code = BPF_ALU64 | BPF_MOV | BPF_K, \ 217 .dst_reg = DST, \ 218 .src_reg = 0, \ 219 .off = 0, \ 220 .imm = IMM }) 221 222 #define BPF_MOV32_IMM(DST, IMM) \ 223 ((struct bpf_insn) { \ 224 .code = BPF_ALU | BPF_MOV | BPF_K, \ 225 .dst_reg = DST, \ 226 .src_reg = 0, \ 227 .off = 0, \ 228 .imm = IMM }) 229 230 /* Short form of movsx, dst_reg = (s8,s16,s32)src_reg */ 231 232 #define BPF_MOVSX64_REG(DST, SRC, OFF) \ 233 ((struct bpf_insn) { \ 234 .code = BPF_ALU64 | BPF_MOV | BPF_X, \ 235 .dst_reg = DST, \ 236 .src_reg = SRC, \ 237 .off = OFF, \ 238 .imm = 0 }) 239 240 #define BPF_MOVSX32_REG(DST, SRC, OFF) \ 241 ((struct bpf_insn) { \ 242 .code = BPF_ALU | BPF_MOV | BPF_X, \ 243 .dst_reg = DST, \ 244 .src_reg = SRC, \ 245 .off = OFF, \ 246 .imm = 0 }) 247 248 /* Special form of mov32, used for doing explicit zero extension on dst. */ 249 #define BPF_ZEXT_REG(DST) \ 250 ((struct bpf_insn) { \ 251 .code = BPF_ALU | BPF_MOV | BPF_X, \ 252 .dst_reg = DST, \ 253 .src_reg = DST, \ 254 .off = 0, \ 255 .imm = 1 }) 256 257 static inline bool bpf_is_unwind_goto(const struct bpf_insn *insn) 258 { 259 return insn->code == (BPF_JMP | BPF_JCOND) && insn->src_reg == BPF_UNWIND_GOTO; 260 } 261 262 static inline bool bpf_is_unwind_exit(const struct bpf_insn *insn) 263 { 264 return insn->code == (BPF_JMP | BPF_EXIT) && insn->src_reg == BPF_UNWIND_EXIT; 265 } 266 267 /* 268 * The caller of the main prog doesn't tell unwind_exit from exit. An extension 269 * is different: its caller is the caller of the subprog that it replaced. 270 * An exception callback returns from the frame of the main prog. After freplace 271 * or a tail call it returns to a prog that wasn't verified with the callback. 272 */ 273 static inline bool bpf_prog_can_unwind(const struct bpf_prog *prog) 274 { 275 if (prog->aux->exception_cb) 276 return false; 277 return bpf_is_subprog(prog) || prog->type == BPF_PROG_TYPE_EXT; 278 } 279 280 static inline bool insn_is_zext(const struct bpf_insn *insn) 281 { 282 return insn->code == (BPF_ALU | BPF_MOV | BPF_X) && insn->imm == 1; 283 } 284 285 /* addr_space_cast from as(0) to as(1) is for converting bpf arena pointers 286 * to pointers in user vma. 287 */ 288 static inline bool insn_is_cast_user(const struct bpf_insn *insn) 289 { 290 return insn->code == (BPF_ALU64 | BPF_MOV | BPF_X) && 291 insn->off == BPF_ADDR_SPACE_CAST && 292 insn->imm == 1U << 16; 293 } 294 295 /* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */ 296 #define BPF_LD_IMM64(DST, IMM) \ 297 BPF_LD_IMM64_RAW(DST, 0, IMM) 298 299 #define BPF_LD_IMM64_RAW(DST, SRC, IMM) \ 300 ((struct bpf_insn) { \ 301 .code = BPF_LD | BPF_DW | BPF_IMM, \ 302 .dst_reg = DST, \ 303 .src_reg = SRC, \ 304 .off = 0, \ 305 .imm = (__u32) (IMM) }), \ 306 ((struct bpf_insn) { \ 307 .code = 0, /* zero is reserved opcode */ \ 308 .dst_reg = 0, \ 309 .src_reg = 0, \ 310 .off = 0, \ 311 .imm = ((__u64) (IMM)) >> 32 }) 312 313 /* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */ 314 #define BPF_LD_MAP_FD(DST, MAP_FD) \ 315 BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD) 316 317 /* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */ 318 319 #define BPF_MOV64_RAW(TYPE, DST, SRC, IMM) \ 320 ((struct bpf_insn) { \ 321 .code = BPF_ALU64 | BPF_MOV | BPF_SRC(TYPE), \ 322 .dst_reg = DST, \ 323 .src_reg = SRC, \ 324 .off = 0, \ 325 .imm = IMM }) 326 327 #define BPF_MOV32_RAW(TYPE, DST, SRC, IMM) \ 328 ((struct bpf_insn) { \ 329 .code = BPF_ALU | BPF_MOV | BPF_SRC(TYPE), \ 330 .dst_reg = DST, \ 331 .src_reg = SRC, \ 332 .off = 0, \ 333 .imm = IMM }) 334 335 /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */ 336 337 #define BPF_LD_ABS(SIZE, IMM) \ 338 ((struct bpf_insn) { \ 339 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \ 340 .dst_reg = 0, \ 341 .src_reg = 0, \ 342 .off = 0, \ 343 .imm = IMM }) 344 345 /* Indirect packet access, R0 = *(uint *) (skb->data + src_reg + imm32) */ 346 347 #define BPF_LD_IND(SIZE, SRC, IMM) \ 348 ((struct bpf_insn) { \ 349 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_IND, \ 350 .dst_reg = 0, \ 351 .src_reg = SRC, \ 352 .off = 0, \ 353 .imm = IMM }) 354 355 /* Memory load, dst_reg = *(uint *) (src_reg + off16) */ 356 357 #define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \ 358 ((struct bpf_insn) { \ 359 .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \ 360 .dst_reg = DST, \ 361 .src_reg = SRC, \ 362 .off = OFF, \ 363 .imm = 0 }) 364 365 /* Memory load, dst_reg = *(signed size *) (src_reg + off16) */ 366 367 #define BPF_LDX_MEMSX(SIZE, DST, SRC, OFF) \ 368 ((struct bpf_insn) { \ 369 .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEMSX, \ 370 .dst_reg = DST, \ 371 .src_reg = SRC, \ 372 .off = OFF, \ 373 .imm = 0 }) 374 375 /* Memory store, *(uint *) (dst_reg + off16) = src_reg */ 376 377 #define BPF_STX_MEM(SIZE, DST, SRC, OFF) \ 378 ((struct bpf_insn) { \ 379 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \ 380 .dst_reg = DST, \ 381 .src_reg = SRC, \ 382 .off = OFF, \ 383 .imm = 0 }) 384 385 386 /* 387 * Atomic operations: 388 * 389 * BPF_ADD *(uint *) (dst_reg + off16) += src_reg 390 * BPF_AND *(uint *) (dst_reg + off16) &= src_reg 391 * BPF_OR *(uint *) (dst_reg + off16) |= src_reg 392 * BPF_XOR *(uint *) (dst_reg + off16) ^= src_reg 393 * BPF_ADD | BPF_FETCH src_reg = atomic_fetch_add(dst_reg + off16, src_reg); 394 * BPF_AND | BPF_FETCH src_reg = atomic_fetch_and(dst_reg + off16, src_reg); 395 * BPF_OR | BPF_FETCH src_reg = atomic_fetch_or(dst_reg + off16, src_reg); 396 * BPF_XOR | BPF_FETCH src_reg = atomic_fetch_xor(dst_reg + off16, src_reg); 397 * BPF_XCHG src_reg = atomic_xchg(dst_reg + off16, src_reg) 398 * BPF_CMPXCHG r0 = atomic_cmpxchg(dst_reg + off16, r0, src_reg) 399 * BPF_LOAD_ACQ dst_reg = smp_load_acquire(src_reg + off16) 400 * BPF_STORE_REL smp_store_release(dst_reg + off16, src_reg) 401 */ 402 403 #define BPF_ATOMIC_OP(SIZE, OP, DST, SRC, OFF) \ 404 ((struct bpf_insn) { \ 405 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_ATOMIC, \ 406 .dst_reg = DST, \ 407 .src_reg = SRC, \ 408 .off = OFF, \ 409 .imm = OP }) 410 411 /* Legacy alias */ 412 #define BPF_STX_XADD(SIZE, DST, SRC, OFF) BPF_ATOMIC_OP(SIZE, BPF_ADD, DST, SRC, OFF) 413 414 /* 415 * Given a BPF_ATOMIC instruction @atomic_insn, return true if it is an 416 * atomic load or store, and false if it is a read-modify-write instruction. 417 */ 418 static inline bool 419 bpf_atomic_is_load_store(const struct bpf_insn *atomic_insn) 420 { 421 switch (atomic_insn->imm) { 422 case BPF_LOAD_ACQ: 423 case BPF_STORE_REL: 424 return true; 425 default: 426 return false; 427 } 428 } 429 430 /* 431 * A load-acquire is the only BPF_STX class instruction that reads into 432 * dst_reg from src_reg + off16, i.e. it has the operand roles of a BPF_LDX. 433 * Unlike bpf_atomic_is_load_store(), @insn is not assumed to be a BPF_ATOMIC 434 * instruction here, so that callers which walk all instruction classes can 435 * use this directly. 436 */ 437 static inline bool bpf_atomic_is_load_acq(const struct bpf_insn *insn) 438 { 439 return BPF_CLASS(insn->code) == BPF_STX && 440 (BPF_MODE(insn->code) == BPF_ATOMIC || 441 BPF_MODE(insn->code) == BPF_PROBE_ATOMIC) && 442 insn->imm == BPF_LOAD_ACQ; 443 } 444 445 /* 446 * Given an instruction @insn, return the number of the BPF register that a 447 * BPF_ATOMIC reads the value at its memory operand into, or -1 if there is 448 * no such register. That is the register a BPF_PROBE_ATOMIC has to clear when 449 * the access faults. Like bpf_atomic_is_load_acq(), @insn is not assumed to 450 * be a BPF_ATOMIC here. 451 */ 452 static inline int bpf_atomic_load_reg(const struct bpf_insn *insn) 453 { 454 if (BPF_CLASS(insn->code) != BPF_STX || 455 (BPF_MODE(insn->code) != BPF_ATOMIC && 456 BPF_MODE(insn->code) != BPF_PROBE_ATOMIC)) 457 return -1; 458 459 switch (insn->imm) { 460 case BPF_LOAD_ACQ: 461 return insn->dst_reg; 462 case BPF_CMPXCHG: 463 return BPF_REG_0; 464 default: 465 return (insn->imm & BPF_FETCH) ? insn->src_reg : -1; 466 } 467 } 468 469 /* Memory store, *(uint *) (dst_reg + off16) = imm32 */ 470 471 #define BPF_ST_MEM(SIZE, DST, OFF, IMM) \ 472 ((struct bpf_insn) { \ 473 .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \ 474 .dst_reg = DST, \ 475 .src_reg = 0, \ 476 .off = OFF, \ 477 .imm = IMM }) 478 479 /* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */ 480 481 #define BPF_JMP_REG(OP, DST, SRC, OFF) \ 482 ((struct bpf_insn) { \ 483 .code = BPF_JMP | BPF_OP(OP) | BPF_X, \ 484 .dst_reg = DST, \ 485 .src_reg = SRC, \ 486 .off = OFF, \ 487 .imm = 0 }) 488 489 /* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */ 490 491 #define BPF_JMP_IMM(OP, DST, IMM, OFF) \ 492 ((struct bpf_insn) { \ 493 .code = BPF_JMP | BPF_OP(OP) | BPF_K, \ 494 .dst_reg = DST, \ 495 .src_reg = 0, \ 496 .off = OFF, \ 497 .imm = IMM }) 498 499 /* Like BPF_JMP_REG, but with 32-bit wide operands for comparison. */ 500 501 #define BPF_JMP32_REG(OP, DST, SRC, OFF) \ 502 ((struct bpf_insn) { \ 503 .code = BPF_JMP32 | BPF_OP(OP) | BPF_X, \ 504 .dst_reg = DST, \ 505 .src_reg = SRC, \ 506 .off = OFF, \ 507 .imm = 0 }) 508 509 /* Like BPF_JMP_IMM, but with 32-bit wide operands for comparison. */ 510 511 #define BPF_JMP32_IMM(OP, DST, IMM, OFF) \ 512 ((struct bpf_insn) { \ 513 .code = BPF_JMP32 | BPF_OP(OP) | BPF_K, \ 514 .dst_reg = DST, \ 515 .src_reg = 0, \ 516 .off = OFF, \ 517 .imm = IMM }) 518 519 /* Unconditional jumps, goto pc + off16 */ 520 521 #define BPF_JMP_A(OFF) \ 522 ((struct bpf_insn) { \ 523 .code = BPF_JMP | BPF_JA, \ 524 .dst_reg = 0, \ 525 .src_reg = 0, \ 526 .off = OFF, \ 527 .imm = 0 }) 528 529 /* Unconditional jumps, gotol pc + imm32 */ 530 531 #define BPF_JMP32_A(IMM) \ 532 ((struct bpf_insn) { \ 533 .code = BPF_JMP32 | BPF_JA, \ 534 .dst_reg = 0, \ 535 .src_reg = 0, \ 536 .off = 0, \ 537 .imm = IMM }) 538 539 /* Relative call */ 540 541 #define BPF_CALL_REL(TGT) \ 542 ((struct bpf_insn) { \ 543 .code = BPF_JMP | BPF_CALL, \ 544 .dst_reg = 0, \ 545 .src_reg = BPF_PSEUDO_CALL, \ 546 .off = 0, \ 547 .imm = TGT }) 548 549 /* Convert function address to BPF immediate */ 550 551 #define BPF_CALL_IMM(x) ((void *)(x) - (void *)__bpf_call_base) 552 553 #define BPF_EMIT_CALL(FUNC) \ 554 ((struct bpf_insn) { \ 555 .code = BPF_JMP | BPF_CALL, \ 556 .dst_reg = 0, \ 557 .src_reg = 0, \ 558 .off = 0, \ 559 .imm = BPF_CALL_IMM(FUNC) }) 560 561 /* Kfunc call */ 562 563 #define BPF_CALL_KFUNC(OFF, IMM) \ 564 ((struct bpf_insn) { \ 565 .code = BPF_JMP | BPF_CALL, \ 566 .dst_reg = 0, \ 567 .src_reg = BPF_PSEUDO_KFUNC_CALL, \ 568 .off = OFF, \ 569 .imm = IMM }) 570 571 /* Raw code statement block */ 572 573 #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \ 574 ((struct bpf_insn) { \ 575 .code = CODE, \ 576 .dst_reg = DST, \ 577 .src_reg = SRC, \ 578 .off = OFF, \ 579 .imm = IMM }) 580 581 /* Program exit */ 582 583 #define BPF_EXIT_INSN() \ 584 ((struct bpf_insn) { \ 585 .code = BPF_JMP | BPF_EXIT, \ 586 .dst_reg = 0, \ 587 .src_reg = 0, \ 588 .off = 0, \ 589 .imm = 0 }) 590 591 /* Speculation barrier */ 592 593 #define BPF_ST_NOSPEC() \ 594 ((struct bpf_insn) { \ 595 .code = BPF_ST | BPF_NOSPEC, \ 596 .dst_reg = 0, \ 597 .src_reg = 0, \ 598 .off = 0, \ 599 .imm = 0 }) 600 601 /* Internal classic blocks for direct assignment */ 602 603 #define __BPF_STMT(CODE, K) \ 604 ((struct sock_filter) BPF_STMT(CODE, K)) 605 606 #define __BPF_JUMP(CODE, K, JT, JF) \ 607 ((struct sock_filter) BPF_JUMP(CODE, K, JT, JF)) 608 609 #define bytes_to_bpf_size(bytes) \ 610 ({ \ 611 int bpf_size = -EINVAL; \ 612 \ 613 if (bytes == sizeof(u8)) \ 614 bpf_size = BPF_B; \ 615 else if (bytes == sizeof(u16)) \ 616 bpf_size = BPF_H; \ 617 else if (bytes == sizeof(u32)) \ 618 bpf_size = BPF_W; \ 619 else if (bytes == sizeof(u64)) \ 620 bpf_size = BPF_DW; \ 621 \ 622 bpf_size; \ 623 }) 624 625 #define bpf_size_to_bytes(bpf_size) \ 626 ({ \ 627 int bytes = -EINVAL; \ 628 \ 629 if (bpf_size == BPF_B) \ 630 bytes = sizeof(u8); \ 631 else if (bpf_size == BPF_H) \ 632 bytes = sizeof(u16); \ 633 else if (bpf_size == BPF_W) \ 634 bytes = sizeof(u32); \ 635 else if (bpf_size == BPF_DW) \ 636 bytes = sizeof(u64); \ 637 \ 638 bytes; \ 639 }) 640 641 #define BPF_SIZEOF(type) \ 642 ({ \ 643 const int __size = bytes_to_bpf_size(sizeof(type)); \ 644 BUILD_BUG_ON(__size < 0); \ 645 __size; \ 646 }) 647 648 #define BPF_FIELD_SIZEOF(type, field) \ 649 ({ \ 650 const int __size = bytes_to_bpf_size(sizeof_field(type, field)); \ 651 BUILD_BUG_ON(__size < 0); \ 652 __size; \ 653 }) 654 655 #define BPF_LDST_BYTES(insn) \ 656 ({ \ 657 const int __size = bpf_size_to_bytes(BPF_SIZE((insn)->code)); \ 658 WARN_ON(__size < 0); \ 659 __size; \ 660 }) 661 662 #define __BPF_MAP_0(m, v, ...) v 663 #define __BPF_MAP_1(m, v, t, a, ...) m(t, a) 664 #define __BPF_MAP_2(m, v, t, a, ...) m(t, a), __BPF_MAP_1(m, v, __VA_ARGS__) 665 #define __BPF_MAP_3(m, v, t, a, ...) m(t, a), __BPF_MAP_2(m, v, __VA_ARGS__) 666 #define __BPF_MAP_4(m, v, t, a, ...) m(t, a), __BPF_MAP_3(m, v, __VA_ARGS__) 667 #define __BPF_MAP_5(m, v, t, a, ...) m(t, a), __BPF_MAP_4(m, v, __VA_ARGS__) 668 669 #define __BPF_REG_0(...) __BPF_PAD(5) 670 #define __BPF_REG_1(...) __BPF_MAP(1, __VA_ARGS__), __BPF_PAD(4) 671 #define __BPF_REG_2(...) __BPF_MAP(2, __VA_ARGS__), __BPF_PAD(3) 672 #define __BPF_REG_3(...) __BPF_MAP(3, __VA_ARGS__), __BPF_PAD(2) 673 #define __BPF_REG_4(...) __BPF_MAP(4, __VA_ARGS__), __BPF_PAD(1) 674 #define __BPF_REG_5(...) __BPF_MAP(5, __VA_ARGS__) 675 676 #define __BPF_MAP(n, ...) __BPF_MAP_##n(__VA_ARGS__) 677 #define __BPF_REG(n, ...) __BPF_REG_##n(__VA_ARGS__) 678 679 #define __BPF_CAST(t, a) \ 680 (__force t) \ 681 (__force \ 682 typeof(__builtin_choose_expr(sizeof(t) == sizeof(unsigned long), \ 683 (unsigned long)0, (t)0))) a 684 #define __BPF_V void 685 #define __BPF_N 686 687 #define __BPF_DECL_ARGS(t, a) t a 688 #define __BPF_DECL_REGS(t, a) u64 a 689 690 #define __BPF_PAD(n) \ 691 __BPF_MAP(n, __BPF_DECL_ARGS, __BPF_N, u64, __ur_1, u64, __ur_2, \ 692 u64, __ur_3, u64, __ur_4, u64, __ur_5) 693 694 #define BPF_CALL_x(x, attr, name, ...) \ 695 static __always_inline \ 696 u64 ____##name(__BPF_MAP(x, __BPF_DECL_ARGS, __BPF_V, __VA_ARGS__)); \ 697 typedef u64 (*btf_##name)(__BPF_MAP(x, __BPF_DECL_ARGS, __BPF_V, __VA_ARGS__)); \ 698 attr u64 name(__BPF_REG(x, __BPF_DECL_REGS, __BPF_N, __VA_ARGS__)); \ 699 attr u64 name(__BPF_REG(x, __BPF_DECL_REGS, __BPF_N, __VA_ARGS__)) \ 700 { \ 701 return ((btf_##name)____##name)(__BPF_MAP(x,__BPF_CAST,__BPF_N,__VA_ARGS__));\ 702 } \ 703 static __always_inline \ 704 u64 ____##name(__BPF_MAP(x, __BPF_DECL_ARGS, __BPF_V, __VA_ARGS__)) 705 706 #define __NOATTR 707 #define BPF_CALL_0(name, ...) BPF_CALL_x(0, __NOATTR, name, __VA_ARGS__) 708 #define BPF_CALL_1(name, ...) BPF_CALL_x(1, __NOATTR, name, __VA_ARGS__) 709 #define BPF_CALL_2(name, ...) BPF_CALL_x(2, __NOATTR, name, __VA_ARGS__) 710 #define BPF_CALL_3(name, ...) BPF_CALL_x(3, __NOATTR, name, __VA_ARGS__) 711 #define BPF_CALL_4(name, ...) BPF_CALL_x(4, __NOATTR, name, __VA_ARGS__) 712 #define BPF_CALL_5(name, ...) BPF_CALL_x(5, __NOATTR, name, __VA_ARGS__) 713 714 #define NOTRACE_BPF_CALL_1(name, ...) BPF_CALL_x(1, notrace, name, __VA_ARGS__) 715 716 #define bpf_ctx_range(TYPE, MEMBER) \ 717 offsetof(TYPE, MEMBER) ... offsetofend(TYPE, MEMBER) - 1 718 #define bpf_ctx_range_till(TYPE, MEMBER1, MEMBER2) \ 719 offsetof(TYPE, MEMBER1) ... offsetofend(TYPE, MEMBER2) - 1 720 #if BITS_PER_LONG == 64 721 # define bpf_ctx_range_ptr(TYPE, MEMBER) \ 722 offsetof(TYPE, MEMBER) ... offsetofend(TYPE, MEMBER) - 1 723 #else 724 # define bpf_ctx_range_ptr(TYPE, MEMBER) \ 725 offsetof(TYPE, MEMBER) ... offsetof(TYPE, MEMBER) + 8 - 1 726 #endif /* BITS_PER_LONG == 64 */ 727 728 #define bpf_target_off(TYPE, MEMBER, SIZE, PTR_SIZE) \ 729 ({ \ 730 BUILD_BUG_ON(sizeof_field(TYPE, MEMBER) != (SIZE)); \ 731 *(PTR_SIZE) = (SIZE); \ 732 offsetof(TYPE, MEMBER); \ 733 }) 734 735 /* A struct sock_filter is architecture independent. */ 736 struct compat_sock_fprog { 737 u16 len; 738 compat_uptr_t filter; /* struct sock_filter * */ 739 }; 740 741 struct sock_fprog_kern { 742 u16 len; 743 struct sock_filter *filter; 744 }; 745 746 /* Some arches need doubleword alignment for their instructions and/or data */ 747 #define BPF_IMAGE_ALIGNMENT 8 748 749 struct bpf_binary_header { 750 u32 size; 751 u8 image[] __aligned(BPF_IMAGE_ALIGNMENT); 752 }; 753 754 struct bpf_prog_stats { 755 u64_stats_t cnt; 756 u64_stats_t nsecs; 757 u64_stats_t misses; 758 struct u64_stats_sync syncp; 759 } __aligned(2 * sizeof(u64)); 760 761 /* 762 * Per-cpu memory of a prog. All may_goto of one invocation of the prog use it. An invocation is 763 * one call from the kernel into the main prog, with the subprogs and sync callbacks it reaches. 764 */ 765 struct bpf_timed_may_goto { 766 u64 count; 767 u64 timestamp; 768 /* the async callback that has set count and timestamp, see bpf_timed_may_goto_save() */ 769 void *owner; 770 }; 771 772 struct sk_filter { 773 refcount_t refcnt; 774 struct rcu_head rcu; 775 struct bpf_prog *prog; 776 }; 777 778 DECLARE_STATIC_KEY_FALSE(bpf_stats_enabled_key); 779 780 extern struct mutex nf_conn_btf_access_lock; 781 extern int (*nfct_btf_struct_access)(struct bpf_verifier_log *log, 782 const struct bpf_reg_state *reg, 783 int off, int size); 784 785 typedef unsigned int (*bpf_dispatcher_fn)(const void *ctx, 786 const struct bpf_insn *insnsi, 787 unsigned int (*bpf_func)(const void *, 788 const struct bpf_insn *)); 789 790 static __always_inline u32 __bpf_prog_run(const struct bpf_prog *prog, 791 const void *ctx, 792 bpf_dispatcher_fn dfunc) 793 { 794 u32 ret; 795 796 cant_migrate(); 797 if (static_branch_unlikely(&bpf_stats_enabled_key)) { 798 struct bpf_prog_stats *stats; 799 u64 duration, start = sched_clock(); 800 unsigned long flags; 801 802 ret = dfunc(ctx, prog->insnsi, prog->bpf_func); 803 804 duration = sched_clock() - start; 805 if (likely(prog->stats)) { 806 stats = this_cpu_ptr(prog->stats); 807 flags = u64_stats_update_begin_irqsave(&stats->syncp); 808 u64_stats_inc(&stats->cnt); 809 u64_stats_add(&stats->nsecs, duration); 810 u64_stats_update_end_irqrestore(&stats->syncp, flags); 811 } 812 } else { 813 ret = dfunc(ctx, prog->insnsi, prog->bpf_func); 814 } 815 return ret; 816 } 817 818 static __always_inline u32 bpf_prog_run(const struct bpf_prog *prog, const void *ctx) 819 { 820 return __bpf_prog_run(prog, ctx, bpf_dispatcher_nop_func); 821 } 822 823 /* 824 * Use in preemptible and therefore migratable context to make sure that 825 * the execution of the BPF program runs on one CPU. 826 * 827 * This uses migrate_disable/enable() explicitly to document that the 828 * invocation of a BPF program does not require reentrancy protection 829 * against a BPF program which is invoked from a preempting task. 830 */ 831 static inline u32 bpf_prog_run_pin_on_cpu(const struct bpf_prog *prog, 832 const void *ctx) 833 { 834 u32 ret; 835 836 migrate_disable(); 837 ret = bpf_prog_run(prog, ctx); 838 migrate_enable(); 839 return ret; 840 } 841 842 static inline bool is_stack_arg_ldx(const struct bpf_insn *insn) 843 { 844 return insn->code == (BPF_LDX | BPF_MEM | BPF_DW) && 845 insn->src_reg == BPF_REG_PARAMS && 846 insn->off > 0 && insn->off % 8 == 0; 847 } 848 849 static inline bool is_stack_arg_st(const struct bpf_insn *insn) 850 { 851 return insn->code == (BPF_ST | BPF_MEM | BPF_DW) && 852 insn->dst_reg == BPF_REG_PARAMS && 853 insn->off < 0 && insn->off % 8 == 0; 854 } 855 856 static inline bool is_stack_arg_stx(const struct bpf_insn *insn) 857 { 858 return insn->code == (BPF_STX | BPF_MEM | BPF_DW) && 859 insn->dst_reg == BPF_REG_PARAMS && 860 insn->off < 0 && insn->off % 8 == 0; 861 } 862 863 #define BPF_SKB_CB_LEN QDISC_CB_PRIV_LEN 864 865 struct bpf_skb_data_end { 866 struct qdisc_skb_cb qdisc_cb; 867 void *data_meta; 868 void *data_end; 869 }; 870 871 struct bpf_nh_params { 872 u32 nh_family; 873 union { 874 u32 ipv4_nh; 875 struct in6_addr ipv6_nh; 876 }; 877 }; 878 879 /* flags for bpf_redirect_info kern_flags */ 880 #define BPF_RI_F_RF_NO_DIRECT BIT(0) /* no napi_direct on return_frame */ 881 #define BPF_RI_F_RI_INIT BIT(1) 882 #define BPF_RI_F_CPU_MAP_INIT BIT(2) 883 #define BPF_RI_F_DEV_MAP_INIT BIT(3) 884 #define BPF_RI_F_XSK_MAP_INIT BIT(4) 885 886 struct bpf_redirect_info { 887 u64 tgt_index; 888 void *tgt_value; 889 struct bpf_map *map; 890 u32 flags; 891 u32 map_id; 892 enum bpf_map_type map_type; 893 struct bpf_nh_params nh; 894 u32 kern_flags; 895 }; 896 897 struct bpf_net_context { 898 struct bpf_redirect_info ri; 899 struct list_head cpu_map_flush_list; 900 struct list_head dev_map_flush_list; 901 struct list_head xskmap_map_flush_list; 902 }; 903 904 static inline struct bpf_net_context *bpf_net_ctx_set(struct bpf_net_context *bpf_net_ctx) 905 { 906 struct task_struct *tsk = current; 907 908 if (tsk->bpf_net_context != NULL) 909 return NULL; 910 bpf_net_ctx->ri.kern_flags = 0; 911 912 tsk->bpf_net_context = bpf_net_ctx; 913 return bpf_net_ctx; 914 } 915 916 static inline void bpf_net_ctx_clear(struct bpf_net_context *bpf_net_ctx) 917 { 918 if (bpf_net_ctx) 919 current->bpf_net_context = NULL; 920 } 921 922 static inline struct bpf_net_context *bpf_net_ctx_get(void) 923 { 924 return current->bpf_net_context; 925 } 926 927 static inline struct bpf_redirect_info *bpf_net_ctx_get_ri(void) 928 { 929 struct bpf_net_context *bpf_net_ctx = bpf_net_ctx_get(); 930 931 if (!(bpf_net_ctx->ri.kern_flags & BPF_RI_F_RI_INIT)) { 932 memset(&bpf_net_ctx->ri, 0, offsetof(struct bpf_net_context, ri.nh)); 933 bpf_net_ctx->ri.kern_flags |= BPF_RI_F_RI_INIT; 934 } 935 936 return &bpf_net_ctx->ri; 937 } 938 939 static inline struct list_head *bpf_net_ctx_get_cpu_map_flush_list(void) 940 { 941 struct bpf_net_context *bpf_net_ctx = bpf_net_ctx_get(); 942 943 if (!(bpf_net_ctx->ri.kern_flags & BPF_RI_F_CPU_MAP_INIT)) { 944 INIT_LIST_HEAD(&bpf_net_ctx->cpu_map_flush_list); 945 bpf_net_ctx->ri.kern_flags |= BPF_RI_F_CPU_MAP_INIT; 946 } 947 948 return &bpf_net_ctx->cpu_map_flush_list; 949 } 950 951 static inline struct list_head *bpf_net_ctx_get_dev_flush_list(void) 952 { 953 struct bpf_net_context *bpf_net_ctx = bpf_net_ctx_get(); 954 955 if (!(bpf_net_ctx->ri.kern_flags & BPF_RI_F_DEV_MAP_INIT)) { 956 INIT_LIST_HEAD(&bpf_net_ctx->dev_map_flush_list); 957 bpf_net_ctx->ri.kern_flags |= BPF_RI_F_DEV_MAP_INIT; 958 } 959 960 return &bpf_net_ctx->dev_map_flush_list; 961 } 962 963 static inline struct list_head *bpf_net_ctx_get_xskmap_flush_list(void) 964 { 965 struct bpf_net_context *bpf_net_ctx = bpf_net_ctx_get(); 966 967 if (!(bpf_net_ctx->ri.kern_flags & BPF_RI_F_XSK_MAP_INIT)) { 968 INIT_LIST_HEAD(&bpf_net_ctx->xskmap_map_flush_list); 969 bpf_net_ctx->ri.kern_flags |= BPF_RI_F_XSK_MAP_INIT; 970 } 971 972 return &bpf_net_ctx->xskmap_map_flush_list; 973 } 974 975 static inline void bpf_net_ctx_get_all_used_flush_lists(struct list_head **lh_map, 976 struct list_head **lh_dev, 977 struct list_head **lh_xsk) 978 { 979 struct bpf_net_context *bpf_net_ctx = bpf_net_ctx_get(); 980 u32 kern_flags = bpf_net_ctx->ri.kern_flags; 981 struct list_head *lh; 982 983 *lh_map = *lh_dev = *lh_xsk = NULL; 984 985 if (!IS_ENABLED(CONFIG_BPF_SYSCALL)) 986 return; 987 988 lh = &bpf_net_ctx->dev_map_flush_list; 989 if (kern_flags & BPF_RI_F_DEV_MAP_INIT && !list_empty(lh)) 990 *lh_dev = lh; 991 992 lh = &bpf_net_ctx->cpu_map_flush_list; 993 if (kern_flags & BPF_RI_F_CPU_MAP_INIT && !list_empty(lh)) 994 *lh_map = lh; 995 996 lh = &bpf_net_ctx->xskmap_map_flush_list; 997 if (IS_ENABLED(CONFIG_XDP_SOCKETS) && 998 kern_flags & BPF_RI_F_XSK_MAP_INIT && !list_empty(lh)) 999 *lh_xsk = lh; 1000 } 1001 1002 /* Compute the linear packet data range [data, data_end) which 1003 * will be accessed by various program types (cls_bpf, act_bpf, 1004 * lwt, ...). Subsystems allowing direct data access must (!) 1005 * ensure that cb[] area can be written to when BPF program is 1006 * invoked (otherwise cb[] save/restore is necessary). 1007 */ 1008 static inline void bpf_compute_data_pointers(struct sk_buff *skb) 1009 { 1010 struct bpf_skb_data_end *cb = (struct bpf_skb_data_end *)skb->cb; 1011 1012 BUILD_BUG_ON(sizeof(*cb) > sizeof_field(struct sk_buff, cb)); 1013 cb->data_meta = skb->data - skb_metadata_len(skb); 1014 cb->data_end = skb->data + skb_headlen(skb); 1015 } 1016 1017 static inline int bpf_prog_run_data_pointers( 1018 const struct bpf_prog *prog, 1019 struct sk_buff *skb) 1020 { 1021 struct bpf_skb_data_end *cb = (struct bpf_skb_data_end *)skb->cb; 1022 void *save_data_meta, *save_data_end; 1023 int res; 1024 1025 save_data_meta = cb->data_meta; 1026 save_data_end = cb->data_end; 1027 1028 bpf_compute_data_pointers(skb); 1029 res = bpf_prog_run(prog, skb); 1030 1031 cb->data_meta = save_data_meta; 1032 cb->data_end = save_data_end; 1033 1034 return res; 1035 } 1036 1037 /* Similar to bpf_compute_data_pointers(), except that save orginal 1038 * data in cb->data and cb->meta_data for restore. 1039 */ 1040 static inline void bpf_compute_and_save_data_end( 1041 struct sk_buff *skb, void **saved_data_end) 1042 { 1043 struct bpf_skb_data_end *cb = (struct bpf_skb_data_end *)skb->cb; 1044 1045 *saved_data_end = cb->data_end; 1046 cb->data_end = skb->data + skb_headlen(skb); 1047 } 1048 1049 /* Restore data saved by bpf_compute_and_save_data_end(). */ 1050 static inline void bpf_restore_data_end( 1051 struct sk_buff *skb, void *saved_data_end) 1052 { 1053 struct bpf_skb_data_end *cb = (struct bpf_skb_data_end *)skb->cb; 1054 1055 cb->data_end = saved_data_end; 1056 } 1057 1058 /* 1059 * If nothing has set the header, it points to headroom that was not written. 1060 * The packet starts at skb->head when skb->data or the MAC header is there. 1061 */ 1062 static inline bool bpf_skb_has_network_header(const struct sk_buff *skb) 1063 { 1064 return skb->network_header || !skb->mac_header || !skb_headroom(skb); 1065 } 1066 1067 static inline u8 *bpf_skb_cb(const struct sk_buff *skb) 1068 { 1069 /* eBPF programs may read/write skb->cb[] area to transfer meta 1070 * data between tail calls. Since this also needs to work with 1071 * tc, that scratch memory is mapped to qdisc_skb_cb's data area. 1072 * 1073 * In some socket filter cases, the cb unfortunately needs to be 1074 * saved/restored so that protocol specific skb->cb[] data won't 1075 * be lost. In any case, due to unpriviledged eBPF programs 1076 * attached to sockets, we need to clear the bpf_skb_cb() area 1077 * to not leak previous contents to user space. 1078 */ 1079 BUILD_BUG_ON(sizeof_field(struct __sk_buff, cb) != BPF_SKB_CB_LEN); 1080 BUILD_BUG_ON(sizeof_field(struct __sk_buff, cb) != 1081 sizeof_field(struct qdisc_skb_cb, data)); 1082 1083 return qdisc_skb_cb(skb)->data; 1084 } 1085 1086 /* Must be invoked with migration disabled */ 1087 static inline u32 __bpf_prog_run_save_cb(const struct bpf_prog *prog, 1088 const void *ctx) 1089 { 1090 const struct sk_buff *skb = ctx; 1091 u8 *cb_data = bpf_skb_cb(skb); 1092 u8 cb_saved[BPF_SKB_CB_LEN]; 1093 u32 res; 1094 1095 if (unlikely(prog->cb_access)) { 1096 memcpy(cb_saved, cb_data, sizeof(cb_saved)); 1097 memset(cb_data, 0, sizeof(cb_saved)); 1098 } 1099 1100 res = bpf_prog_run(prog, skb); 1101 1102 if (unlikely(prog->cb_access)) 1103 memcpy(cb_data, cb_saved, sizeof(cb_saved)); 1104 1105 return res; 1106 } 1107 1108 static inline u32 bpf_prog_run_save_cb(const struct bpf_prog *prog, 1109 struct sk_buff *skb) 1110 { 1111 u32 res; 1112 1113 migrate_disable(); 1114 res = __bpf_prog_run_save_cb(prog, skb); 1115 migrate_enable(); 1116 return res; 1117 } 1118 1119 static inline u32 bpf_prog_run_clear_cb(const struct bpf_prog *prog, 1120 struct sk_buff *skb) 1121 { 1122 u8 *cb_data = bpf_skb_cb(skb); 1123 u32 res; 1124 1125 if (unlikely(prog->cb_access)) 1126 memset(cb_data, 0, BPF_SKB_CB_LEN); 1127 1128 res = bpf_prog_run_pin_on_cpu(prog, skb); 1129 return res; 1130 } 1131 1132 DECLARE_BPF_DISPATCHER(xdp) 1133 1134 DECLARE_STATIC_KEY_FALSE(bpf_master_redirect_enabled_key); 1135 1136 u32 xdp_master_redirect(struct xdp_buff *xdp); 1137 1138 void bpf_prog_change_xdp(struct bpf_prog *prev_prog, struct bpf_prog *prog); 1139 1140 static inline u32 bpf_prog_insn_size(const struct bpf_prog *prog) 1141 { 1142 return prog->len * sizeof(struct bpf_insn); 1143 } 1144 1145 static inline unsigned int bpf_prog_size(unsigned int proglen) 1146 { 1147 return max(sizeof(struct bpf_prog), 1148 offsetof(struct bpf_prog, insns[proglen])); 1149 } 1150 1151 bool bpf_prog_has_arena_insns(const struct bpf_prog *prog); 1152 1153 /* 1154 * Whether a JIT has to set up the arena base for @prog. A program bound to 1155 * an arena need not touch it, libbpf binds the arena of an object to every 1156 * program in it. The flag caches bpf_prog_has_arena_insns() for the function 1157 * about to be JITed, see bpf_prog_jit_compile() and jit_subprogs(). 1158 */ 1159 static inline bool bpf_prog_uses_arena_insns(const struct bpf_prog *prog) 1160 { 1161 return prog->aux->uses_arena_insns; 1162 } 1163 1164 static inline bool bpf_prog_was_classic(const struct bpf_prog *prog) 1165 { 1166 /* When classic BPF programs have been loaded and the arch 1167 * does not have a classic BPF JIT (anymore), they have been 1168 * converted via bpf_migrate_filter() to eBPF and thus always 1169 * have an unspec program type. 1170 */ 1171 return prog->type == BPF_PROG_TYPE_UNSPEC; 1172 } 1173 1174 static inline u32 bpf_ctx_off_adjust_machine(u32 size) 1175 { 1176 const u32 size_machine = sizeof(unsigned long); 1177 1178 if (size > size_machine && size % size_machine == 0) 1179 size = size_machine; 1180 1181 return size; 1182 } 1183 1184 static inline bool 1185 bpf_ctx_narrow_access_ok(u32 off, u32 size, u32 size_default) 1186 { 1187 return size <= size_default && (size & (size - 1)) == 0; 1188 } 1189 1190 static inline u8 1191 bpf_ctx_narrow_access_offset(u32 off, u32 size, u32 size_default) 1192 { 1193 u8 access_off = off & (size_default - 1); 1194 1195 #ifdef __LITTLE_ENDIAN 1196 return access_off; 1197 #else 1198 return size_default - (access_off + size); 1199 #endif 1200 } 1201 1202 #define bpf_ctx_wide_access_ok(off, size, type, field) \ 1203 (size == sizeof(__u64) && \ 1204 off >= offsetof(type, field) && \ 1205 off + sizeof(__u64) <= offsetofend(type, field) && \ 1206 off % sizeof(__u64) == 0) 1207 1208 #define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0])) 1209 1210 static inline int __must_check bpf_prog_lock_ro(struct bpf_prog *fp) 1211 { 1212 #ifndef CONFIG_BPF_JIT_ALWAYS_ON 1213 if (!fp->jited) { 1214 set_vm_flush_reset_perms(fp); 1215 return set_memory_ro((unsigned long)fp, fp->pages); 1216 } 1217 #endif 1218 return 0; 1219 } 1220 1221 static inline int __must_check 1222 bpf_jit_binary_lock_ro(struct bpf_binary_header *hdr) 1223 { 1224 set_vm_flush_reset_perms(hdr); 1225 return set_memory_rox((unsigned long)hdr, hdr->size >> PAGE_SHIFT); 1226 } 1227 1228 enum skb_drop_reason 1229 sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap); 1230 1231 static inline int sk_filter(struct sock *sk, struct sk_buff *skb) 1232 { 1233 enum skb_drop_reason drop_reason; 1234 1235 drop_reason = sk_filter_trim_cap(sk, skb, 1); 1236 return drop_reason ? -EPERM : 0; 1237 } 1238 1239 static inline enum skb_drop_reason 1240 sk_filter_reason(struct sock *sk, struct sk_buff *skb) 1241 { 1242 return sk_filter_trim_cap(sk, skb, 1); 1243 } 1244 1245 struct bpf_prog *__bpf_prog_select_runtime(struct bpf_verifier_env *env, struct bpf_prog *fp, 1246 int *err); 1247 struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err); 1248 void bpf_prog_free(struct bpf_prog *fp); 1249 1250 bool bpf_opcode_in_insntable(u8 code); 1251 1252 void bpf_prog_fill_jited_linfo(struct bpf_prog *prog, 1253 const u32 *insn_to_jit_off); 1254 int bpf_prog_alloc_jited_linfo(struct bpf_prog *prog); 1255 void bpf_prog_jit_attempt_done(struct bpf_prog *prog); 1256 1257 struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags); 1258 struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flags); 1259 struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size, 1260 gfp_t gfp_extra_flags); 1261 void __bpf_prog_free(struct bpf_prog *fp); 1262 1263 static inline void bpf_prog_unlock_free(struct bpf_prog *fp) 1264 { 1265 __bpf_prog_free(fp); 1266 } 1267 1268 typedef int (*bpf_aux_classic_check_t)(struct sock_filter *filter, 1269 unsigned int flen); 1270 1271 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog); 1272 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog, 1273 bpf_aux_classic_check_t trans, bool save_orig); 1274 void bpf_prog_destroy(struct bpf_prog *fp); 1275 1276 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk); 1277 int sk_attach_bpf(u32 ufd, struct sock *sk); 1278 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk); 1279 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk); 1280 void sk_reuseport_prog_free(struct bpf_prog *prog); 1281 int sk_detach_filter(struct sock *sk); 1282 int sk_get_filter(struct sock *sk, sockptr_t optval, unsigned int len); 1283 1284 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp); 1285 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp); 1286 1287 u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5); 1288 1289 struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog); 1290 void bpf_jit_compile(struct bpf_prog *prog); 1291 bool bpf_jit_needs_zext(void); 1292 bool bpf_jit_inlines_helper_call(s32 imm); 1293 bool bpf_jit_supports_subprog_tailcalls(void); 1294 bool bpf_jit_supports_percpu_insn(void); 1295 bool bpf_jit_supports_kfunc_call(void); 1296 bool bpf_jit_supports_callx(void); 1297 bool bpf_jit_supports_kfunc_ret_reg_pair(void); 1298 bool bpf_jit_supports_unwind(void); 1299 bool bpf_jit_supports_stack_args(void); 1300 bool bpf_jit_supports_arena_args(void); 1301 bool bpf_jit_supports_far_kfunc_call(void); 1302 bool bpf_jit_supports_exceptions(void); 1303 bool bpf_jit_supports_ptr_xchg(void); 1304 bool bpf_jit_supports_arena(void); 1305 bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena); 1306 bool bpf_jit_supports_private_stack(void); 1307 bool bpf_jit_supports_large_stack(void); 1308 bool bpf_jit_supports_timed_may_goto(void); 1309 bool bpf_jit_supports_fsession(void); 1310 1311 struct bpf_jit_arg_abi { 1312 /* Argument registers of the kernel convention. */ 1313 u8 nr_arg_regs; 1314 /* Round the register number up to an even one for 16-byte alignment. */ 1315 bool even_reg_align; 1316 /* Round the stack slot up to an even one for 16-byte alignment. */ 1317 bool even_stack_align; 1318 /* An argument may straddle the last register and the stack. */ 1319 bool split_at_boundary; 1320 /* A later argument may reuse a register a stack-passed one skipped. */ 1321 bool backfill_after_stack; 1322 }; 1323 1324 const struct bpf_jit_arg_abi *bpf_jit_arg_abi(void); 1325 u32 bpf_jit_place_args(const struct bpf_jit_arg_abi *abi, 1326 const struct btf_func_model *fm, u8 *pos_of_slot); 1327 1328 /* The JIT's scratch register, in place of an argument slot. */ 1329 #define BPF_JIT_ARG_TMP 0xff 1330 1331 /* Every argument slot moves at most once, and the scratch goes out and back. */ 1332 #define BPF_JIT_MAX_ARG_MOVES (MAX_BPF_FUNC_ARG_SLOTS + 2) 1333 1334 struct bpf_jit_arg_move { 1335 u8 dst; 1336 u8 src; 1337 }; 1338 1339 u32 bpf_jit_plan_arg_moves(const struct bpf_jit_arg_abi *abi, 1340 const struct btf_func_model *fm, 1341 struct bpf_jit_arg_move *moves); 1342 u64 bpf_arch_uaddress_limit(void); 1343 void arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp, u64 bp), void *cookie); 1344 u64 arch_bpf_timed_may_goto(void); 1345 u64 bpf_check_timed_may_goto(struct bpf_timed_may_goto *); 1346 void bpf_timed_may_goto_check_time(const struct bpf_prog *prog); 1347 void bpf_timed_may_goto_save(const struct bpf_prog *prog, struct bpf_timed_may_goto *saved); 1348 void bpf_timed_may_goto_restore(const struct bpf_prog *prog, struct bpf_timed_may_goto *saved); 1349 bool bpf_helper_changes_pkt_data(enum bpf_func_id func_id); 1350 1351 static inline bool bpf_dump_raw_ok(const struct cred *cred) 1352 { 1353 /* Reconstruction of call-sites is dependent on kallsyms, 1354 * thus make dump the same restriction. 1355 */ 1356 return kallsyms_show_value(cred); 1357 } 1358 1359 struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off, 1360 const struct bpf_insn *patch, u32 len); 1361 1362 #ifdef CONFIG_BPF_SYSCALL 1363 struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, 1364 const struct bpf_insn *patch, u32 len); 1365 #else 1366 static inline struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, 1367 const struct bpf_insn *patch, u32 len) 1368 { 1369 return ERR_PTR(-ENOTSUPP); 1370 } 1371 #endif /* CONFIG_BPF_SYSCALL */ 1372 1373 int bpf_remove_insns(struct bpf_prog *prog, u32 off, u32 cnt); 1374 1375 static inline bool xdp_return_frame_no_direct(void) 1376 { 1377 struct bpf_redirect_info *ri = bpf_net_ctx_get_ri(); 1378 1379 return ri->kern_flags & BPF_RI_F_RF_NO_DIRECT; 1380 } 1381 1382 static inline void xdp_set_return_frame_no_direct(void) 1383 { 1384 struct bpf_redirect_info *ri = bpf_net_ctx_get_ri(); 1385 1386 ri->kern_flags |= BPF_RI_F_RF_NO_DIRECT; 1387 } 1388 1389 static inline void xdp_clear_return_frame_no_direct(void) 1390 { 1391 struct bpf_redirect_info *ri = bpf_net_ctx_get_ri(); 1392 1393 ri->kern_flags &= ~BPF_RI_F_RF_NO_DIRECT; 1394 } 1395 1396 static inline int xdp_ok_fwd_dev(const struct net_device *fwd, 1397 unsigned int pktlen) 1398 { 1399 unsigned int len; 1400 1401 if (unlikely(!(fwd->flags & IFF_UP))) 1402 return -ENETDOWN; 1403 1404 len = fwd->mtu + fwd->hard_header_len + VLAN_HLEN; 1405 if (pktlen > len) 1406 return -EMSGSIZE; 1407 1408 return 0; 1409 } 1410 1411 /* The pair of xdp_do_redirect and xdp_do_flush MUST be called in the 1412 * same cpu context. Further for best results no more than a single map 1413 * for the do_redirect/do_flush pair should be used. This limitation is 1414 * because we only track one map and force a flush when the map changes. 1415 * This does not appear to be a real limitation for existing software. 1416 */ 1417 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb, 1418 struct xdp_buff *xdp, const struct bpf_prog *prog); 1419 int xdp_do_redirect(struct net_device *dev, 1420 struct xdp_buff *xdp, 1421 const struct bpf_prog *prog); 1422 int xdp_do_redirect_frame(struct net_device *dev, 1423 struct xdp_buff *xdp, 1424 struct xdp_frame *xdpf, 1425 const struct bpf_prog *prog); 1426 void xdp_do_flush(void); 1427 1428 void bpf_warn_invalid_xdp_action(const struct net_device *dev, 1429 const struct bpf_prog *prog, u32 act); 1430 1431 #ifdef CONFIG_INET 1432 struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk, 1433 struct bpf_prog *prog, struct sk_buff *skb, 1434 struct sock *migrating_sk, 1435 u32 hash); 1436 #else 1437 static inline struct sock * 1438 bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk, 1439 struct bpf_prog *prog, struct sk_buff *skb, 1440 struct sock *migrating_sk, 1441 u32 hash) 1442 { 1443 return NULL; 1444 } 1445 #endif 1446 1447 #ifdef CONFIG_BPF_JIT 1448 extern int bpf_jit_enable; 1449 extern int bpf_jit_harden; 1450 extern int bpf_jit_kallsyms; 1451 extern long bpf_jit_limit; 1452 extern long bpf_jit_limit_max; 1453 1454 typedef void (*bpf_jit_fill_hole_t)(void *area, unsigned int size); 1455 1456 /* 1457 * Flush the indirect branch predictors before reusing JIT memory, so that 1458 * indirect jumps into a newly written program don't reuse predictions left 1459 * behind by an old program that occupied the same space. 1460 */ 1461 void bpf_arch_pred_flush(void); 1462 DECLARE_STATIC_CALL(bpf_arch_pred_flush, bpf_arch_pred_flush); 1463 DECLARE_STATIC_KEY_FALSE(bpf_pred_flush_enabled); 1464 1465 void bpf_jit_fill_hole_with_zero(void *area, unsigned int size); 1466 1467 struct bpf_binary_header * 1468 bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr, 1469 unsigned int alignment, 1470 bpf_jit_fill_hole_t bpf_fill_ill_insns); 1471 void bpf_jit_binary_free(struct bpf_binary_header *hdr); 1472 u64 bpf_jit_alloc_exec_limit(void); 1473 void *bpf_jit_alloc_exec(unsigned long size); 1474 void bpf_jit_free_exec(void *addr); 1475 void bpf_jit_free(struct bpf_prog *fp); 1476 struct bpf_binary_header * 1477 bpf_jit_binary_pack_hdr(const struct bpf_prog *fp); 1478 1479 void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns, bool was_classic); 1480 void bpf_prog_pack_free(void *ptr, u32 size); 1481 1482 static inline bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp) 1483 { 1484 return list_empty(&fp->aux->ksym.lnode) || 1485 fp->aux->ksym.lnode.prev == LIST_POISON2; 1486 } 1487 1488 struct bpf_binary_header * 1489 bpf_jit_binary_pack_alloc(unsigned int proglen, u8 **ro_image, 1490 unsigned int alignment, 1491 struct bpf_binary_header **rw_hdr, 1492 u8 **rw_image, 1493 bpf_jit_fill_hole_t bpf_fill_ill_insns, 1494 bool was_classic); 1495 int bpf_jit_binary_pack_finalize(struct bpf_binary_header *ro_header, 1496 struct bpf_binary_header *rw_header); 1497 void bpf_jit_binary_pack_free(struct bpf_binary_header *ro_header, 1498 struct bpf_binary_header *rw_header); 1499 1500 int bpf_jit_add_poke_descriptor(struct bpf_prog *prog, 1501 struct bpf_jit_poke_descriptor *poke); 1502 1503 int bpf_jit_get_func_addr(const struct bpf_prog *prog, 1504 const struct bpf_insn *insn, bool extra_pass, 1505 u64 *func_addr, bool *func_addr_fixed); 1506 1507 const char *bpf_jit_get_prog_name(struct bpf_prog *prog); 1508 1509 struct bpf_prog *bpf_jit_blind_constants(struct bpf_verifier_env *env, struct bpf_prog *prog); 1510 void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other); 1511 1512 static inline bool bpf_prog_need_blind(const struct bpf_prog *prog) 1513 { 1514 return prog->blinding_requested && !prog->blinded; 1515 } 1516 1517 static inline void bpf_jit_dump(unsigned int flen, unsigned int proglen, 1518 u32 pass, void *image) 1519 { 1520 pr_err("flen=%u proglen=%u pass=%u image=%p from=%s pid=%d\n", flen, 1521 proglen, pass, image, current->comm, task_pid_nr(current)); 1522 1523 if (image) 1524 print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_OFFSET, 1525 16, 1, image, proglen, false); 1526 } 1527 1528 static inline bool bpf_jit_is_ebpf(void) 1529 { 1530 # ifdef CONFIG_HAVE_EBPF_JIT 1531 return true; 1532 # else 1533 return false; 1534 # endif 1535 } 1536 1537 static inline bool ebpf_jit_enabled(void) 1538 { 1539 return bpf_jit_enable && bpf_jit_is_ebpf(); 1540 } 1541 1542 static inline bool bpf_prog_ebpf_jited(const struct bpf_prog *fp) 1543 { 1544 return fp->jited && bpf_jit_is_ebpf(); 1545 } 1546 1547 static inline bool bpf_jit_blinding_enabled(struct bpf_prog *prog) 1548 { 1549 /* These are the prerequisites, should someone ever have the 1550 * idea to call blinding outside of them, we make sure to 1551 * bail out. 1552 */ 1553 if (!bpf_jit_is_ebpf()) 1554 return false; 1555 if (!prog->jit_requested) 1556 return false; 1557 if (!bpf_jit_harden) 1558 return false; 1559 if (bpf_jit_harden == 1 && bpf_token_capable(prog->aux->token, CAP_BPF)) 1560 return false; 1561 1562 return true; 1563 } 1564 1565 static inline bool bpf_jit_kallsyms_enabled(void) 1566 { 1567 /* There are a couple of corner cases where kallsyms should 1568 * not be enabled f.e. on hardening. 1569 */ 1570 if (bpf_jit_harden) 1571 return false; 1572 if (!bpf_jit_kallsyms) 1573 return false; 1574 if (bpf_jit_kallsyms == 1) 1575 return true; 1576 1577 return false; 1578 } 1579 1580 int bpf_address_lookup(unsigned long addr, unsigned long *size, 1581 unsigned long *off, char *sym); 1582 bool is_bpf_text_address(unsigned long addr); 1583 int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type, 1584 char *sym); 1585 struct bpf_prog *bpf_prog_ksym_find(unsigned long addr); 1586 1587 void bpf_prog_kallsyms_add(struct bpf_prog *fp); 1588 void bpf_prog_kallsyms_del(struct bpf_prog *fp); 1589 1590 #else /* CONFIG_BPF_JIT */ 1591 1592 static inline bool ebpf_jit_enabled(void) 1593 { 1594 return false; 1595 } 1596 1597 static inline bool bpf_jit_blinding_enabled(struct bpf_prog *prog) 1598 { 1599 return false; 1600 } 1601 1602 static inline bool bpf_prog_ebpf_jited(const struct bpf_prog *fp) 1603 { 1604 return false; 1605 } 1606 1607 static inline int 1608 bpf_jit_add_poke_descriptor(struct bpf_prog *prog, 1609 struct bpf_jit_poke_descriptor *poke) 1610 { 1611 return -ENOTSUPP; 1612 } 1613 1614 static inline void bpf_jit_free(struct bpf_prog *fp) 1615 { 1616 bpf_prog_unlock_free(fp); 1617 } 1618 1619 static inline bool bpf_jit_kallsyms_enabled(void) 1620 { 1621 return false; 1622 } 1623 1624 static inline int 1625 bpf_address_lookup(unsigned long addr, unsigned long *size, 1626 unsigned long *off, char *sym) 1627 { 1628 return 0; 1629 } 1630 1631 static inline bool is_bpf_text_address(unsigned long addr) 1632 { 1633 return false; 1634 } 1635 1636 static inline int bpf_get_kallsym(unsigned int symnum, unsigned long *value, 1637 char *type, char *sym) 1638 { 1639 return -ERANGE; 1640 } 1641 1642 static inline struct bpf_prog *bpf_prog_ksym_find(unsigned long addr) 1643 { 1644 return NULL; 1645 } 1646 1647 static inline void bpf_prog_kallsyms_add(struct bpf_prog *fp) 1648 { 1649 } 1650 1651 static inline void bpf_prog_kallsyms_del(struct bpf_prog *fp) 1652 { 1653 } 1654 1655 static inline bool bpf_prog_need_blind(const struct bpf_prog *prog) 1656 { 1657 return false; 1658 } 1659 1660 static inline 1661 struct bpf_prog *bpf_jit_blind_constants(struct bpf_verifier_env *env, struct bpf_prog *prog) 1662 { 1663 return prog; 1664 } 1665 1666 static inline void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other) 1667 { 1668 } 1669 #endif /* CONFIG_BPF_JIT */ 1670 1671 void bpf_prog_kallsyms_del_all(struct bpf_prog *fp); 1672 1673 #define BPF_ANC BIT(15) 1674 1675 static inline bool bpf_needs_clear_a(const struct sock_filter *first) 1676 { 1677 switch (first->code) { 1678 case BPF_RET | BPF_K: 1679 case BPF_LD | BPF_W | BPF_LEN: 1680 return false; 1681 1682 case BPF_LD | BPF_W | BPF_ABS: 1683 case BPF_LD | BPF_H | BPF_ABS: 1684 case BPF_LD | BPF_B | BPF_ABS: 1685 if (first->k == SKF_AD_OFF + SKF_AD_ALU_XOR_X) 1686 return true; 1687 return false; 1688 1689 default: 1690 return true; 1691 } 1692 } 1693 1694 static inline u16 bpf_anc_helper(const struct sock_filter *ftest) 1695 { 1696 BUG_ON(ftest->code & BPF_ANC); 1697 1698 switch (ftest->code) { 1699 case BPF_LD | BPF_W | BPF_ABS: 1700 case BPF_LD | BPF_H | BPF_ABS: 1701 case BPF_LD | BPF_B | BPF_ABS: 1702 #define BPF_ANCILLARY(CODE) case SKF_AD_OFF + SKF_AD_##CODE: \ 1703 return BPF_ANC | SKF_AD_##CODE 1704 switch (ftest->k) { 1705 BPF_ANCILLARY(PROTOCOL); 1706 BPF_ANCILLARY(PKTTYPE); 1707 BPF_ANCILLARY(IFINDEX); 1708 BPF_ANCILLARY(NLATTR); 1709 BPF_ANCILLARY(NLATTR_NEST); 1710 BPF_ANCILLARY(MARK); 1711 BPF_ANCILLARY(QUEUE); 1712 BPF_ANCILLARY(HATYPE); 1713 BPF_ANCILLARY(RXHASH); 1714 BPF_ANCILLARY(CPU); 1715 BPF_ANCILLARY(ALU_XOR_X); 1716 BPF_ANCILLARY(VLAN_TAG); 1717 BPF_ANCILLARY(VLAN_TAG_PRESENT); 1718 BPF_ANCILLARY(PAY_OFFSET); 1719 BPF_ANCILLARY(RANDOM); 1720 BPF_ANCILLARY(VLAN_TPID); 1721 } 1722 fallthrough; 1723 default: 1724 return ftest->code; 1725 } 1726 } 1727 1728 void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, 1729 int k, unsigned int size); 1730 1731 static inline int bpf_tell_extensions(void) 1732 { 1733 return SKF_AD_MAX; 1734 } 1735 1736 struct bpf_sock_addr_kern { 1737 struct sock *sk; 1738 struct sockaddr_unsized *uaddr; 1739 /* Temporary "register" to make indirect stores to nested structures 1740 * defined above. We need three registers to make such a store, but 1741 * only two (src and dst) are available at convert_ctx_access time 1742 */ 1743 u64 tmp_reg; 1744 void *t_ctx; /* Attach type specific context. */ 1745 u32 uaddrlen; 1746 }; 1747 1748 struct bpf_sock_ops_kern { 1749 struct sock *sk; 1750 union { 1751 u32 args[4]; 1752 u32 reply; 1753 u32 replylong[4]; 1754 }; 1755 struct sk_buff *syn_skb; 1756 struct sk_buff *skb; 1757 void *skb_data_end; 1758 u8 op; 1759 u8 is_fullsock; 1760 u8 is_locked_tcp_sock; 1761 u8 remaining_opt_len; 1762 u64 temp; /* temp and everything after is not 1763 * initialized to 0 before calling 1764 * the BPF program. New fields that 1765 * should be initialized to 0 should 1766 * be inserted before temp. 1767 * temp is scratch storage used by 1768 * sock_ops_convert_ctx_access 1769 * as temporary storage of a register. 1770 */ 1771 }; 1772 1773 struct bpf_sysctl_kern { 1774 struct ctl_table_header *head; 1775 const struct ctl_table *table; 1776 void *cur_val; 1777 size_t cur_len; 1778 void *new_val; 1779 size_t new_len; 1780 int new_updated; 1781 int write; 1782 loff_t *ppos; 1783 /* Temporary "register" for indirect stores to ppos. */ 1784 u64 tmp_reg; 1785 }; 1786 1787 #define BPF_SOCKOPT_KERN_BUF_SIZE 32 1788 struct bpf_sockopt_buf { 1789 u8 data[BPF_SOCKOPT_KERN_BUF_SIZE]; 1790 }; 1791 1792 struct bpf_sockopt_kern { 1793 struct sock *sk; 1794 u8 *optval; 1795 u8 *optval_end; 1796 s32 level; 1797 s32 optname; 1798 s32 optlen; 1799 /* for retval in struct bpf_cg_run_ctx */ 1800 struct task_struct *current_task; 1801 /* Temporary "register" for indirect stores to ppos. */ 1802 u64 tmp_reg; 1803 }; 1804 1805 int copy_bpf_fprog_from_user(struct sock_fprog *dst, sockptr_t src, int len); 1806 1807 struct bpf_sk_lookup_kern { 1808 u16 family; 1809 u16 protocol; 1810 __be16 sport; 1811 u16 dport; 1812 struct { 1813 __be32 saddr; 1814 __be32 daddr; 1815 } v4; 1816 struct { 1817 const struct in6_addr *saddr; 1818 const struct in6_addr *daddr; 1819 } v6; 1820 struct sock *selected_sk; 1821 u32 ingress_ifindex; 1822 bool no_reuseport; 1823 }; 1824 1825 extern struct static_key_false bpf_sk_lookup_enabled; 1826 1827 /* Runners for BPF_SK_LOOKUP programs to invoke on socket lookup. 1828 * 1829 * Allowed return values for a BPF SK_LOOKUP program are SK_PASS and 1830 * SK_DROP. Their meaning is as follows: 1831 * 1832 * SK_PASS && ctx.selected_sk != NULL: use selected_sk as lookup result 1833 * SK_PASS && ctx.selected_sk == NULL: continue to htable-based socket lookup 1834 * SK_DROP : terminate lookup with -ECONNREFUSED 1835 * 1836 * This macro aggregates return values and selected sockets from 1837 * multiple BPF programs according to following rules in order: 1838 * 1839 * 1. If any program returned SK_PASS and a non-NULL ctx.selected_sk, 1840 * macro result is SK_PASS and last ctx.selected_sk is used. 1841 * 2. If any program returned SK_DROP return value, 1842 * macro result is SK_DROP. 1843 * 3. Otherwise result is SK_PASS and ctx.selected_sk is NULL. 1844 * 1845 * Caller must ensure that the prog array is non-NULL, and that the 1846 * array as well as the programs it contains remain valid. 1847 */ 1848 #define BPF_PROG_SK_LOOKUP_RUN_ARRAY(array, ctx, func) \ 1849 ({ \ 1850 struct bpf_sk_lookup_kern *_ctx = &(ctx); \ 1851 struct bpf_prog_array_item *_item; \ 1852 struct sock *_selected_sk = NULL; \ 1853 bool _no_reuseport = false; \ 1854 struct bpf_prog *_prog; \ 1855 bool _all_pass = true; \ 1856 u32 _ret; \ 1857 \ 1858 migrate_disable(); \ 1859 _item = &(array)->items[0]; \ 1860 while ((_prog = READ_ONCE(_item->prog))) { \ 1861 /* restore most recent selection */ \ 1862 _ctx->selected_sk = _selected_sk; \ 1863 _ctx->no_reuseport = _no_reuseport; \ 1864 \ 1865 _ret = func(_prog, _ctx); \ 1866 if (_ret == SK_PASS && _ctx->selected_sk) { \ 1867 /* remember last non-NULL socket */ \ 1868 _selected_sk = _ctx->selected_sk; \ 1869 _no_reuseport = _ctx->no_reuseport; \ 1870 } else if (_ret == SK_DROP && _all_pass) { \ 1871 _all_pass = false; \ 1872 } \ 1873 _item++; \ 1874 } \ 1875 _ctx->selected_sk = _selected_sk; \ 1876 _ctx->no_reuseport = _no_reuseport; \ 1877 migrate_enable(); \ 1878 _all_pass || _selected_sk ? SK_PASS : SK_DROP; \ 1879 }) 1880 1881 static inline bool bpf_sk_lookup_run_v4(const struct net *net, int protocol, 1882 const __be32 saddr, const __be16 sport, 1883 const __be32 daddr, const u16 dport, 1884 const int ifindex, struct sock **psk) 1885 { 1886 struct bpf_prog_array *run_array; 1887 struct sock *selected_sk = NULL; 1888 bool no_reuseport = false; 1889 1890 rcu_read_lock(); 1891 run_array = rcu_dereference(net->bpf.run_array[NETNS_BPF_SK_LOOKUP]); 1892 if (run_array) { 1893 struct bpf_sk_lookup_kern ctx = { 1894 .family = AF_INET, 1895 .protocol = protocol, 1896 .v4.saddr = saddr, 1897 .v4.daddr = daddr, 1898 .sport = sport, 1899 .dport = dport, 1900 .ingress_ifindex = ifindex, 1901 }; 1902 u32 act; 1903 1904 act = BPF_PROG_SK_LOOKUP_RUN_ARRAY(run_array, ctx, bpf_prog_run); 1905 if (act == SK_PASS) { 1906 selected_sk = ctx.selected_sk; 1907 no_reuseport = ctx.no_reuseport; 1908 } else { 1909 selected_sk = ERR_PTR(-ECONNREFUSED); 1910 } 1911 } 1912 rcu_read_unlock(); 1913 *psk = selected_sk; 1914 return no_reuseport; 1915 } 1916 1917 #if IS_ENABLED(CONFIG_IPV6) 1918 static inline bool bpf_sk_lookup_run_v6(const struct net *net, int protocol, 1919 const struct in6_addr *saddr, 1920 const __be16 sport, 1921 const struct in6_addr *daddr, 1922 const u16 dport, 1923 const int ifindex, struct sock **psk) 1924 { 1925 struct bpf_prog_array *run_array; 1926 struct sock *selected_sk = NULL; 1927 bool no_reuseport = false; 1928 1929 rcu_read_lock(); 1930 run_array = rcu_dereference(net->bpf.run_array[NETNS_BPF_SK_LOOKUP]); 1931 if (run_array) { 1932 struct bpf_sk_lookup_kern ctx = { 1933 .family = AF_INET6, 1934 .protocol = protocol, 1935 .v6.saddr = saddr, 1936 .v6.daddr = daddr, 1937 .sport = sport, 1938 .dport = dport, 1939 .ingress_ifindex = ifindex, 1940 }; 1941 u32 act; 1942 1943 act = BPF_PROG_SK_LOOKUP_RUN_ARRAY(run_array, ctx, bpf_prog_run); 1944 if (act == SK_PASS) { 1945 selected_sk = ctx.selected_sk; 1946 no_reuseport = ctx.no_reuseport; 1947 } else { 1948 selected_sk = ERR_PTR(-ECONNREFUSED); 1949 } 1950 } 1951 rcu_read_unlock(); 1952 *psk = selected_sk; 1953 return no_reuseport; 1954 } 1955 #endif /* IS_ENABLED(CONFIG_IPV6) */ 1956 1957 static __always_inline long __bpf_xdp_redirect_map(struct bpf_map *map, u64 index, 1958 u64 flags, const u64 flag_mask, 1959 void *lookup_elem(struct bpf_map *map, u32 key)) 1960 { 1961 const u64 action_mask = XDP_ABORTED | XDP_DROP | XDP_PASS | XDP_TX; 1962 struct bpf_redirect_info *ri; 1963 1964 /* Lower bits of the flags are used as return code on lookup failure */ 1965 if (unlikely(!bpf_net_ctx_get() || flags & ~(action_mask | flag_mask))) 1966 return XDP_ABORTED; 1967 1968 ri = bpf_net_ctx_get_ri(); 1969 ri->tgt_value = lookup_elem(map, index); 1970 if (unlikely(!ri->tgt_value) && !(flags & BPF_F_BROADCAST)) { 1971 /* If the lookup fails we want to clear out the state in the 1972 * redirect_info struct completely, so that if an eBPF program 1973 * performs multiple lookups, the last one always takes 1974 * precedence. 1975 */ 1976 ri->map_id = INT_MAX; /* Valid map id idr range: [1,INT_MAX[ */ 1977 ri->map_type = BPF_MAP_TYPE_UNSPEC; 1978 return flags & action_mask; 1979 } 1980 1981 ri->tgt_index = index; 1982 ri->map_id = map->id; 1983 ri->map_type = map->map_type; 1984 1985 if (flags & BPF_F_BROADCAST) { 1986 WRITE_ONCE(ri->map, map); 1987 ri->flags = flags; 1988 } else { 1989 WRITE_ONCE(ri->map, NULL); 1990 ri->flags = 0; 1991 } 1992 1993 return XDP_REDIRECT; 1994 } 1995 1996 int __bpf_sock_ops_load_hdr_opt(struct bpf_sock_ops_kern *bpf_sock, 1997 void *search_res, u32 len, u64 flags); 1998 int __bpf_sock_ops_store_hdr_opt(struct bpf_sock_ops_kern *bpf_sock, 1999 const void *from, u32 len, u64 flags); 2000 2001 #ifdef CONFIG_NET 2002 int __bpf_skb_load_bytes(const struct sk_buff *skb, u32 offset, void *to, u32 len); 2003 int __bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from, 2004 u32 len, u64 flags); 2005 int __bpf_xdp_load_bytes(struct xdp_buff *xdp, u32 offset, void *buf, u32 len); 2006 int __bpf_xdp_store_bytes(struct xdp_buff *xdp, u32 offset, void *buf, u32 len); 2007 void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len); 2008 void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off, 2009 void *buf, unsigned long len, bool flush); 2010 int __bpf_skb_meta_store_bytes(struct sk_buff *skb, u32 offset, 2011 const void *from, u32 len, u64 flags); 2012 void *bpf_skb_meta_pointer(struct sk_buff *skb, u32 offset); 2013 #else /* CONFIG_NET */ 2014 static inline int __bpf_skb_load_bytes(const struct sk_buff *skb, u32 offset, 2015 void *to, u32 len) 2016 { 2017 return -EOPNOTSUPP; 2018 } 2019 2020 static inline int __bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, 2021 const void *from, u32 len, u64 flags) 2022 { 2023 return -EOPNOTSUPP; 2024 } 2025 2026 static inline int __bpf_xdp_load_bytes(struct xdp_buff *xdp, u32 offset, 2027 void *buf, u32 len) 2028 { 2029 return -EOPNOTSUPP; 2030 } 2031 2032 static inline int __bpf_xdp_store_bytes(struct xdp_buff *xdp, u32 offset, 2033 void *buf, u32 len) 2034 { 2035 return -EOPNOTSUPP; 2036 } 2037 2038 static inline void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len) 2039 { 2040 return NULL; 2041 } 2042 2043 static inline void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off, void *buf, 2044 unsigned long len, bool flush) 2045 { 2046 } 2047 2048 static inline int __bpf_skb_meta_store_bytes(struct sk_buff *skb, u32 offset, 2049 const void *from, u32 len, 2050 u64 flags) 2051 { 2052 return -EOPNOTSUPP; 2053 } 2054 2055 static inline void *bpf_skb_meta_pointer(struct sk_buff *skb, u32 offset) 2056 { 2057 return ERR_PTR(-EOPNOTSUPP); 2058 } 2059 #endif /* CONFIG_NET */ 2060 2061 #endif /* __LINUX_FILTER_H__ */ 2062