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 24 #include <linux/module.h> 25 #include <linux/types.h> 26 #include <linux/mm.h> 27 #include <linux/fcntl.h> 28 #include <linux/socket.h> 29 #include <linux/sock_diag.h> 30 #include <linux/in.h> 31 #include <linux/inet.h> 32 #include <linux/netdevice.h> 33 #include <linux/if_packet.h> 34 #include <linux/if_arp.h> 35 #include <linux/gfp.h> 36 #include <net/inet_common.h> 37 #include <net/ip.h> 38 #include <net/protocol.h> 39 #include <net/netlink.h> 40 #include <linux/skbuff.h> 41 #include <net/sock.h> 42 #include <net/flow_dissector.h> 43 #include <linux/errno.h> 44 #include <linux/timer.h> 45 #include <linux/uaccess.h> 46 #include <asm/unaligned.h> 47 #include <asm/cmpxchg.h> 48 #include <linux/filter.h> 49 #include <linux/ratelimit.h> 50 #include <linux/seccomp.h> 51 #include <linux/if_vlan.h> 52 #include <linux/bpf.h> 53 #include <net/sch_generic.h> 54 #include <net/cls_cgroup.h> 55 #include <net/dst_metadata.h> 56 #include <net/dst.h> 57 #include <net/sock_reuseport.h> 58 #include <net/busy_poll.h> 59 #include <net/tcp.h> 60 #include <net/xfrm.h> 61 #include <linux/bpf_trace.h> 62 #include <net/xdp_sock.h> 63 #include <linux/inetdevice.h> 64 #include <net/ip_fib.h> 65 #include <net/flow.h> 66 #include <net/arp.h> 67 #include <net/ipv6.h> 68 #include <linux/seg6_local.h> 69 #include <net/seg6.h> 70 #include <net/seg6_local.h> 71 72 /** 73 * sk_filter_trim_cap - run a packet through a socket filter 74 * @sk: sock associated with &sk_buff 75 * @skb: buffer to filter 76 * @cap: limit on how short the eBPF program may trim the packet 77 * 78 * Run the eBPF program and then cut skb->data to correct size returned by 79 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller 80 * than pkt_len we keep whole skb->data. This is the socket level 81 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should 82 * be accepted or -EPERM if the packet should be tossed. 83 * 84 */ 85 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap) 86 { 87 int err; 88 struct sk_filter *filter; 89 90 /* 91 * If the skb was allocated from pfmemalloc reserves, only 92 * allow SOCK_MEMALLOC sockets to use it as this socket is 93 * helping free memory 94 */ 95 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) { 96 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP); 97 return -ENOMEM; 98 } 99 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb); 100 if (err) 101 return err; 102 103 err = security_sock_rcv_skb(sk, skb); 104 if (err) 105 return err; 106 107 rcu_read_lock(); 108 filter = rcu_dereference(sk->sk_filter); 109 if (filter) { 110 struct sock *save_sk = skb->sk; 111 unsigned int pkt_len; 112 113 skb->sk = sk; 114 pkt_len = bpf_prog_run_save_cb(filter->prog, skb); 115 skb->sk = save_sk; 116 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM; 117 } 118 rcu_read_unlock(); 119 120 return err; 121 } 122 EXPORT_SYMBOL(sk_filter_trim_cap); 123 124 BPF_CALL_1(bpf_skb_get_pay_offset, struct sk_buff *, skb) 125 { 126 return skb_get_poff(skb); 127 } 128 129 BPF_CALL_3(bpf_skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x) 130 { 131 struct nlattr *nla; 132 133 if (skb_is_nonlinear(skb)) 134 return 0; 135 136 if (skb->len < sizeof(struct nlattr)) 137 return 0; 138 139 if (a > skb->len - sizeof(struct nlattr)) 140 return 0; 141 142 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x); 143 if (nla) 144 return (void *) nla - (void *) skb->data; 145 146 return 0; 147 } 148 149 BPF_CALL_3(bpf_skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x) 150 { 151 struct nlattr *nla; 152 153 if (skb_is_nonlinear(skb)) 154 return 0; 155 156 if (skb->len < sizeof(struct nlattr)) 157 return 0; 158 159 if (a > skb->len - sizeof(struct nlattr)) 160 return 0; 161 162 nla = (struct nlattr *) &skb->data[a]; 163 if (nla->nla_len > skb->len - a) 164 return 0; 165 166 nla = nla_find_nested(nla, x); 167 if (nla) 168 return (void *) nla - (void *) skb->data; 169 170 return 0; 171 } 172 173 BPF_CALL_4(bpf_skb_load_helper_8, const struct sk_buff *, skb, const void *, 174 data, int, headlen, int, offset) 175 { 176 u8 tmp, *ptr; 177 const int len = sizeof(tmp); 178 179 if (offset >= 0) { 180 if (headlen - offset >= len) 181 return *(u8 *)(data + offset); 182 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp))) 183 return tmp; 184 } else { 185 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len); 186 if (likely(ptr)) 187 return *(u8 *)ptr; 188 } 189 190 return -EFAULT; 191 } 192 193 BPF_CALL_2(bpf_skb_load_helper_8_no_cache, const struct sk_buff *, skb, 194 int, offset) 195 { 196 return ____bpf_skb_load_helper_8(skb, skb->data, skb->len - skb->data_len, 197 offset); 198 } 199 200 BPF_CALL_4(bpf_skb_load_helper_16, const struct sk_buff *, skb, const void *, 201 data, int, headlen, int, offset) 202 { 203 u16 tmp, *ptr; 204 const int len = sizeof(tmp); 205 206 if (offset >= 0) { 207 if (headlen - offset >= len) 208 return get_unaligned_be16(data + offset); 209 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp))) 210 return be16_to_cpu(tmp); 211 } else { 212 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len); 213 if (likely(ptr)) 214 return get_unaligned_be16(ptr); 215 } 216 217 return -EFAULT; 218 } 219 220 BPF_CALL_2(bpf_skb_load_helper_16_no_cache, const struct sk_buff *, skb, 221 int, offset) 222 { 223 return ____bpf_skb_load_helper_16(skb, skb->data, skb->len - skb->data_len, 224 offset); 225 } 226 227 BPF_CALL_4(bpf_skb_load_helper_32, const struct sk_buff *, skb, const void *, 228 data, int, headlen, int, offset) 229 { 230 u32 tmp, *ptr; 231 const int len = sizeof(tmp); 232 233 if (likely(offset >= 0)) { 234 if (headlen - offset >= len) 235 return get_unaligned_be32(data + offset); 236 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp))) 237 return be32_to_cpu(tmp); 238 } else { 239 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len); 240 if (likely(ptr)) 241 return get_unaligned_be32(ptr); 242 } 243 244 return -EFAULT; 245 } 246 247 BPF_CALL_2(bpf_skb_load_helper_32_no_cache, const struct sk_buff *, skb, 248 int, offset) 249 { 250 return ____bpf_skb_load_helper_32(skb, skb->data, skb->len - skb->data_len, 251 offset); 252 } 253 254 BPF_CALL_0(bpf_get_raw_cpu_id) 255 { 256 return raw_smp_processor_id(); 257 } 258 259 static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = { 260 .func = bpf_get_raw_cpu_id, 261 .gpl_only = false, 262 .ret_type = RET_INTEGER, 263 }; 264 265 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg, 266 struct bpf_insn *insn_buf) 267 { 268 struct bpf_insn *insn = insn_buf; 269 270 switch (skb_field) { 271 case SKF_AD_MARK: 272 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4); 273 274 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, 275 offsetof(struct sk_buff, mark)); 276 break; 277 278 case SKF_AD_PKTTYPE: 279 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET()); 280 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX); 281 #ifdef __BIG_ENDIAN_BITFIELD 282 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5); 283 #endif 284 break; 285 286 case SKF_AD_QUEUE: 287 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2); 288 289 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, 290 offsetof(struct sk_buff, queue_mapping)); 291 break; 292 293 case SKF_AD_VLAN_TAG: 294 case SKF_AD_VLAN_TAG_PRESENT: 295 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2); 296 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000); 297 298 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */ 299 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, 300 offsetof(struct sk_buff, vlan_tci)); 301 if (skb_field == SKF_AD_VLAN_TAG) { 302 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 303 ~VLAN_TAG_PRESENT); 304 } else { 305 /* dst_reg >>= 12 */ 306 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12); 307 /* dst_reg &= 1 */ 308 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1); 309 } 310 break; 311 } 312 313 return insn - insn_buf; 314 } 315 316 static bool convert_bpf_extensions(struct sock_filter *fp, 317 struct bpf_insn **insnp) 318 { 319 struct bpf_insn *insn = *insnp; 320 u32 cnt; 321 322 switch (fp->k) { 323 case SKF_AD_OFF + SKF_AD_PROTOCOL: 324 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2); 325 326 /* A = *(u16 *) (CTX + offsetof(protocol)) */ 327 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX, 328 offsetof(struct sk_buff, protocol)); 329 /* A = ntohs(A) [emitting a nop or swap16] */ 330 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16); 331 break; 332 333 case SKF_AD_OFF + SKF_AD_PKTTYPE: 334 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn); 335 insn += cnt - 1; 336 break; 337 338 case SKF_AD_OFF + SKF_AD_IFINDEX: 339 case SKF_AD_OFF + SKF_AD_HATYPE: 340 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4); 341 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2); 342 343 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev), 344 BPF_REG_TMP, BPF_REG_CTX, 345 offsetof(struct sk_buff, dev)); 346 /* if (tmp != 0) goto pc + 1 */ 347 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1); 348 *insn++ = BPF_EXIT_INSN(); 349 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX) 350 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP, 351 offsetof(struct net_device, ifindex)); 352 else 353 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP, 354 offsetof(struct net_device, type)); 355 break; 356 357 case SKF_AD_OFF + SKF_AD_MARK: 358 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn); 359 insn += cnt - 1; 360 break; 361 362 case SKF_AD_OFF + SKF_AD_RXHASH: 363 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4); 364 365 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, 366 offsetof(struct sk_buff, hash)); 367 break; 368 369 case SKF_AD_OFF + SKF_AD_QUEUE: 370 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn); 371 insn += cnt - 1; 372 break; 373 374 case SKF_AD_OFF + SKF_AD_VLAN_TAG: 375 cnt = convert_skb_access(SKF_AD_VLAN_TAG, 376 BPF_REG_A, BPF_REG_CTX, insn); 377 insn += cnt - 1; 378 break; 379 380 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT: 381 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT, 382 BPF_REG_A, BPF_REG_CTX, insn); 383 insn += cnt - 1; 384 break; 385 386 case SKF_AD_OFF + SKF_AD_VLAN_TPID: 387 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2); 388 389 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */ 390 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX, 391 offsetof(struct sk_buff, vlan_proto)); 392 /* A = ntohs(A) [emitting a nop or swap16] */ 393 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16); 394 break; 395 396 case SKF_AD_OFF + SKF_AD_PAY_OFFSET: 397 case SKF_AD_OFF + SKF_AD_NLATTR: 398 case SKF_AD_OFF + SKF_AD_NLATTR_NEST: 399 case SKF_AD_OFF + SKF_AD_CPU: 400 case SKF_AD_OFF + SKF_AD_RANDOM: 401 /* arg1 = CTX */ 402 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX); 403 /* arg2 = A */ 404 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A); 405 /* arg3 = X */ 406 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X); 407 /* Emit call(arg1=CTX, arg2=A, arg3=X) */ 408 switch (fp->k) { 409 case SKF_AD_OFF + SKF_AD_PAY_OFFSET: 410 *insn = BPF_EMIT_CALL(bpf_skb_get_pay_offset); 411 break; 412 case SKF_AD_OFF + SKF_AD_NLATTR: 413 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr); 414 break; 415 case SKF_AD_OFF + SKF_AD_NLATTR_NEST: 416 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr_nest); 417 break; 418 case SKF_AD_OFF + SKF_AD_CPU: 419 *insn = BPF_EMIT_CALL(bpf_get_raw_cpu_id); 420 break; 421 case SKF_AD_OFF + SKF_AD_RANDOM: 422 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32); 423 bpf_user_rnd_init_once(); 424 break; 425 } 426 break; 427 428 case SKF_AD_OFF + SKF_AD_ALU_XOR_X: 429 /* A ^= X */ 430 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X); 431 break; 432 433 default: 434 /* This is just a dummy call to avoid letting the compiler 435 * evict __bpf_call_base() as an optimization. Placed here 436 * where no-one bothers. 437 */ 438 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0); 439 return false; 440 } 441 442 *insnp = insn; 443 return true; 444 } 445 446 static bool convert_bpf_ld_abs(struct sock_filter *fp, struct bpf_insn **insnp) 447 { 448 const bool unaligned_ok = IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS); 449 int size = bpf_size_to_bytes(BPF_SIZE(fp->code)); 450 bool endian = BPF_SIZE(fp->code) == BPF_H || 451 BPF_SIZE(fp->code) == BPF_W; 452 bool indirect = BPF_MODE(fp->code) == BPF_IND; 453 const int ip_align = NET_IP_ALIGN; 454 struct bpf_insn *insn = *insnp; 455 int offset = fp->k; 456 457 if (!indirect && 458 ((unaligned_ok && offset >= 0) || 459 (!unaligned_ok && offset >= 0 && 460 offset + ip_align >= 0 && 461 offset + ip_align % size == 0))) { 462 bool ldx_off_ok = offset <= S16_MAX; 463 464 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_H); 465 *insn++ = BPF_ALU64_IMM(BPF_SUB, BPF_REG_TMP, offset); 466 *insn++ = BPF_JMP_IMM(BPF_JSLT, BPF_REG_TMP, 467 size, 2 + endian + (!ldx_off_ok * 2)); 468 if (ldx_off_ok) { 469 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A, 470 BPF_REG_D, offset); 471 } else { 472 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_D); 473 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_TMP, offset); 474 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A, 475 BPF_REG_TMP, 0); 476 } 477 if (endian) 478 *insn++ = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, size * 8); 479 *insn++ = BPF_JMP_A(8); 480 } 481 482 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX); 483 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_D); 484 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_H); 485 if (!indirect) { 486 *insn++ = BPF_MOV64_IMM(BPF_REG_ARG4, offset); 487 } else { 488 *insn++ = BPF_MOV64_REG(BPF_REG_ARG4, BPF_REG_X); 489 if (fp->k) 490 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG4, offset); 491 } 492 493 switch (BPF_SIZE(fp->code)) { 494 case BPF_B: 495 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8); 496 break; 497 case BPF_H: 498 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16); 499 break; 500 case BPF_W: 501 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32); 502 break; 503 default: 504 return false; 505 } 506 507 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_A, 0, 2); 508 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A); 509 *insn = BPF_EXIT_INSN(); 510 511 *insnp = insn; 512 return true; 513 } 514 515 /** 516 * bpf_convert_filter - convert filter program 517 * @prog: the user passed filter program 518 * @len: the length of the user passed filter program 519 * @new_prog: allocated 'struct bpf_prog' or NULL 520 * @new_len: pointer to store length of converted program 521 * @seen_ld_abs: bool whether we've seen ld_abs/ind 522 * 523 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn' 524 * style extended BPF (eBPF). 525 * Conversion workflow: 526 * 527 * 1) First pass for calculating the new program length: 528 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs) 529 * 530 * 2) 2nd pass to remap in two passes: 1st pass finds new 531 * jump offsets, 2nd pass remapping: 532 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs) 533 */ 534 static int bpf_convert_filter(struct sock_filter *prog, int len, 535 struct bpf_prog *new_prog, int *new_len, 536 bool *seen_ld_abs) 537 { 538 int new_flen = 0, pass = 0, target, i, stack_off; 539 struct bpf_insn *new_insn, *first_insn = NULL; 540 struct sock_filter *fp; 541 int *addrs = NULL; 542 u8 bpf_src; 543 544 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK); 545 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); 546 547 if (len <= 0 || len > BPF_MAXINSNS) 548 return -EINVAL; 549 550 if (new_prog) { 551 first_insn = new_prog->insnsi; 552 addrs = kcalloc(len, sizeof(*addrs), 553 GFP_KERNEL | __GFP_NOWARN); 554 if (!addrs) 555 return -ENOMEM; 556 } 557 558 do_pass: 559 new_insn = first_insn; 560 fp = prog; 561 562 /* Classic BPF related prologue emission. */ 563 if (new_prog) { 564 /* Classic BPF expects A and X to be reset first. These need 565 * to be guaranteed to be the first two instructions. 566 */ 567 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A); 568 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X); 569 570 /* All programs must keep CTX in callee saved BPF_REG_CTX. 571 * In eBPF case it's done by the compiler, here we need to 572 * do this ourself. Initial CTX is present in BPF_REG_ARG1. 573 */ 574 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1); 575 if (*seen_ld_abs) { 576 /* For packet access in classic BPF, cache skb->data 577 * in callee-saved BPF R8 and skb->len - skb->data_len 578 * (headlen) in BPF R9. Since classic BPF is read-only 579 * on CTX, we only need to cache it once. 580 */ 581 *new_insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data), 582 BPF_REG_D, BPF_REG_CTX, 583 offsetof(struct sk_buff, data)); 584 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_H, BPF_REG_CTX, 585 offsetof(struct sk_buff, len)); 586 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_TMP, BPF_REG_CTX, 587 offsetof(struct sk_buff, data_len)); 588 *new_insn++ = BPF_ALU32_REG(BPF_SUB, BPF_REG_H, BPF_REG_TMP); 589 } 590 } else { 591 new_insn += 3; 592 } 593 594 for (i = 0; i < len; fp++, i++) { 595 struct bpf_insn tmp_insns[32] = { }; 596 struct bpf_insn *insn = tmp_insns; 597 598 if (addrs) 599 addrs[i] = new_insn - first_insn; 600 601 switch (fp->code) { 602 /* All arithmetic insns and skb loads map as-is. */ 603 case BPF_ALU | BPF_ADD | BPF_X: 604 case BPF_ALU | BPF_ADD | BPF_K: 605 case BPF_ALU | BPF_SUB | BPF_X: 606 case BPF_ALU | BPF_SUB | BPF_K: 607 case BPF_ALU | BPF_AND | BPF_X: 608 case BPF_ALU | BPF_AND | BPF_K: 609 case BPF_ALU | BPF_OR | BPF_X: 610 case BPF_ALU | BPF_OR | BPF_K: 611 case BPF_ALU | BPF_LSH | BPF_X: 612 case BPF_ALU | BPF_LSH | BPF_K: 613 case BPF_ALU | BPF_RSH | BPF_X: 614 case BPF_ALU | BPF_RSH | BPF_K: 615 case BPF_ALU | BPF_XOR | BPF_X: 616 case BPF_ALU | BPF_XOR | BPF_K: 617 case BPF_ALU | BPF_MUL | BPF_X: 618 case BPF_ALU | BPF_MUL | BPF_K: 619 case BPF_ALU | BPF_DIV | BPF_X: 620 case BPF_ALU | BPF_DIV | BPF_K: 621 case BPF_ALU | BPF_MOD | BPF_X: 622 case BPF_ALU | BPF_MOD | BPF_K: 623 case BPF_ALU | BPF_NEG: 624 case BPF_LD | BPF_ABS | BPF_W: 625 case BPF_LD | BPF_ABS | BPF_H: 626 case BPF_LD | BPF_ABS | BPF_B: 627 case BPF_LD | BPF_IND | BPF_W: 628 case BPF_LD | BPF_IND | BPF_H: 629 case BPF_LD | BPF_IND | BPF_B: 630 /* Check for overloaded BPF extension and 631 * directly convert it if found, otherwise 632 * just move on with mapping. 633 */ 634 if (BPF_CLASS(fp->code) == BPF_LD && 635 BPF_MODE(fp->code) == BPF_ABS && 636 convert_bpf_extensions(fp, &insn)) 637 break; 638 if (BPF_CLASS(fp->code) == BPF_LD && 639 convert_bpf_ld_abs(fp, &insn)) { 640 *seen_ld_abs = true; 641 break; 642 } 643 644 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) || 645 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) { 646 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X); 647 /* Error with exception code on div/mod by 0. 648 * For cBPF programs, this was always return 0. 649 */ 650 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2); 651 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A); 652 *insn++ = BPF_EXIT_INSN(); 653 } 654 655 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k); 656 break; 657 658 /* Jump transformation cannot use BPF block macros 659 * everywhere as offset calculation and target updates 660 * require a bit more work than the rest, i.e. jump 661 * opcodes map as-is, but offsets need adjustment. 662 */ 663 664 #define BPF_EMIT_JMP \ 665 do { \ 666 const s32 off_min = S16_MIN, off_max = S16_MAX; \ 667 s32 off; \ 668 \ 669 if (target >= len || target < 0) \ 670 goto err; \ 671 off = addrs ? addrs[target] - addrs[i] - 1 : 0; \ 672 /* Adjust pc relative offset for 2nd or 3rd insn. */ \ 673 off -= insn - tmp_insns; \ 674 /* Reject anything not fitting into insn->off. */ \ 675 if (off < off_min || off > off_max) \ 676 goto err; \ 677 insn->off = off; \ 678 } while (0) 679 680 case BPF_JMP | BPF_JA: 681 target = i + fp->k + 1; 682 insn->code = fp->code; 683 BPF_EMIT_JMP; 684 break; 685 686 case BPF_JMP | BPF_JEQ | BPF_K: 687 case BPF_JMP | BPF_JEQ | BPF_X: 688 case BPF_JMP | BPF_JSET | BPF_K: 689 case BPF_JMP | BPF_JSET | BPF_X: 690 case BPF_JMP | BPF_JGT | BPF_K: 691 case BPF_JMP | BPF_JGT | BPF_X: 692 case BPF_JMP | BPF_JGE | BPF_K: 693 case BPF_JMP | BPF_JGE | BPF_X: 694 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) { 695 /* BPF immediates are signed, zero extend 696 * immediate into tmp register and use it 697 * in compare insn. 698 */ 699 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k); 700 701 insn->dst_reg = BPF_REG_A; 702 insn->src_reg = BPF_REG_TMP; 703 bpf_src = BPF_X; 704 } else { 705 insn->dst_reg = BPF_REG_A; 706 insn->imm = fp->k; 707 bpf_src = BPF_SRC(fp->code); 708 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0; 709 } 710 711 /* Common case where 'jump_false' is next insn. */ 712 if (fp->jf == 0) { 713 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src; 714 target = i + fp->jt + 1; 715 BPF_EMIT_JMP; 716 break; 717 } 718 719 /* Convert some jumps when 'jump_true' is next insn. */ 720 if (fp->jt == 0) { 721 switch (BPF_OP(fp->code)) { 722 case BPF_JEQ: 723 insn->code = BPF_JMP | BPF_JNE | bpf_src; 724 break; 725 case BPF_JGT: 726 insn->code = BPF_JMP | BPF_JLE | bpf_src; 727 break; 728 case BPF_JGE: 729 insn->code = BPF_JMP | BPF_JLT | bpf_src; 730 break; 731 default: 732 goto jmp_rest; 733 } 734 735 target = i + fp->jf + 1; 736 BPF_EMIT_JMP; 737 break; 738 } 739 jmp_rest: 740 /* Other jumps are mapped into two insns: Jxx and JA. */ 741 target = i + fp->jt + 1; 742 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src; 743 BPF_EMIT_JMP; 744 insn++; 745 746 insn->code = BPF_JMP | BPF_JA; 747 target = i + fp->jf + 1; 748 BPF_EMIT_JMP; 749 break; 750 751 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */ 752 case BPF_LDX | BPF_MSH | BPF_B: { 753 struct sock_filter tmp = { 754 .code = BPF_LD | BPF_ABS | BPF_B, 755 .k = fp->k, 756 }; 757 758 *seen_ld_abs = true; 759 760 /* X = A */ 761 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A); 762 /* A = BPF_R0 = *(u8 *) (skb->data + K) */ 763 convert_bpf_ld_abs(&tmp, &insn); 764 insn++; 765 /* A &= 0xf */ 766 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf); 767 /* A <<= 2 */ 768 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2); 769 /* tmp = X */ 770 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_X); 771 /* X = A */ 772 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A); 773 /* A = tmp */ 774 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP); 775 break; 776 } 777 /* RET_K is remaped into 2 insns. RET_A case doesn't need an 778 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A. 779 */ 780 case BPF_RET | BPF_A: 781 case BPF_RET | BPF_K: 782 if (BPF_RVAL(fp->code) == BPF_K) 783 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0, 784 0, fp->k); 785 *insn = BPF_EXIT_INSN(); 786 break; 787 788 /* Store to stack. */ 789 case BPF_ST: 790 case BPF_STX: 791 stack_off = fp->k * 4 + 4; 792 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) == 793 BPF_ST ? BPF_REG_A : BPF_REG_X, 794 -stack_off); 795 /* check_load_and_stores() verifies that classic BPF can 796 * load from stack only after write, so tracking 797 * stack_depth for ST|STX insns is enough 798 */ 799 if (new_prog && new_prog->aux->stack_depth < stack_off) 800 new_prog->aux->stack_depth = stack_off; 801 break; 802 803 /* Load from stack. */ 804 case BPF_LD | BPF_MEM: 805 case BPF_LDX | BPF_MEM: 806 stack_off = fp->k * 4 + 4; 807 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ? 808 BPF_REG_A : BPF_REG_X, BPF_REG_FP, 809 -stack_off); 810 break; 811 812 /* A = K or X = K */ 813 case BPF_LD | BPF_IMM: 814 case BPF_LDX | BPF_IMM: 815 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ? 816 BPF_REG_A : BPF_REG_X, fp->k); 817 break; 818 819 /* X = A */ 820 case BPF_MISC | BPF_TAX: 821 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A); 822 break; 823 824 /* A = X */ 825 case BPF_MISC | BPF_TXA: 826 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X); 827 break; 828 829 /* A = skb->len or X = skb->len */ 830 case BPF_LD | BPF_W | BPF_LEN: 831 case BPF_LDX | BPF_W | BPF_LEN: 832 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ? 833 BPF_REG_A : BPF_REG_X, BPF_REG_CTX, 834 offsetof(struct sk_buff, len)); 835 break; 836 837 /* Access seccomp_data fields. */ 838 case BPF_LDX | BPF_ABS | BPF_W: 839 /* A = *(u32 *) (ctx + K) */ 840 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k); 841 break; 842 843 /* Unknown instruction. */ 844 default: 845 goto err; 846 } 847 848 insn++; 849 if (new_prog) 850 memcpy(new_insn, tmp_insns, 851 sizeof(*insn) * (insn - tmp_insns)); 852 new_insn += insn - tmp_insns; 853 } 854 855 if (!new_prog) { 856 /* Only calculating new length. */ 857 *new_len = new_insn - first_insn; 858 if (*seen_ld_abs) 859 *new_len += 4; /* Prologue bits. */ 860 return 0; 861 } 862 863 pass++; 864 if (new_flen != new_insn - first_insn) { 865 new_flen = new_insn - first_insn; 866 if (pass > 2) 867 goto err; 868 goto do_pass; 869 } 870 871 kfree(addrs); 872 BUG_ON(*new_len != new_flen); 873 return 0; 874 err: 875 kfree(addrs); 876 return -EINVAL; 877 } 878 879 /* Security: 880 * 881 * As we dont want to clear mem[] array for each packet going through 882 * __bpf_prog_run(), we check that filter loaded by user never try to read 883 * a cell if not previously written, and we check all branches to be sure 884 * a malicious user doesn't try to abuse us. 885 */ 886 static int check_load_and_stores(const struct sock_filter *filter, int flen) 887 { 888 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */ 889 int pc, ret = 0; 890 891 BUILD_BUG_ON(BPF_MEMWORDS > 16); 892 893 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL); 894 if (!masks) 895 return -ENOMEM; 896 897 memset(masks, 0xff, flen * sizeof(*masks)); 898 899 for (pc = 0; pc < flen; pc++) { 900 memvalid &= masks[pc]; 901 902 switch (filter[pc].code) { 903 case BPF_ST: 904 case BPF_STX: 905 memvalid |= (1 << filter[pc].k); 906 break; 907 case BPF_LD | BPF_MEM: 908 case BPF_LDX | BPF_MEM: 909 if (!(memvalid & (1 << filter[pc].k))) { 910 ret = -EINVAL; 911 goto error; 912 } 913 break; 914 case BPF_JMP | BPF_JA: 915 /* A jump must set masks on target */ 916 masks[pc + 1 + filter[pc].k] &= memvalid; 917 memvalid = ~0; 918 break; 919 case BPF_JMP | BPF_JEQ | BPF_K: 920 case BPF_JMP | BPF_JEQ | BPF_X: 921 case BPF_JMP | BPF_JGE | BPF_K: 922 case BPF_JMP | BPF_JGE | BPF_X: 923 case BPF_JMP | BPF_JGT | BPF_K: 924 case BPF_JMP | BPF_JGT | BPF_X: 925 case BPF_JMP | BPF_JSET | BPF_K: 926 case BPF_JMP | BPF_JSET | BPF_X: 927 /* A jump must set masks on targets */ 928 masks[pc + 1 + filter[pc].jt] &= memvalid; 929 masks[pc + 1 + filter[pc].jf] &= memvalid; 930 memvalid = ~0; 931 break; 932 } 933 } 934 error: 935 kfree(masks); 936 return ret; 937 } 938 939 static bool chk_code_allowed(u16 code_to_probe) 940 { 941 static const bool codes[] = { 942 /* 32 bit ALU operations */ 943 [BPF_ALU | BPF_ADD | BPF_K] = true, 944 [BPF_ALU | BPF_ADD | BPF_X] = true, 945 [BPF_ALU | BPF_SUB | BPF_K] = true, 946 [BPF_ALU | BPF_SUB | BPF_X] = true, 947 [BPF_ALU | BPF_MUL | BPF_K] = true, 948 [BPF_ALU | BPF_MUL | BPF_X] = true, 949 [BPF_ALU | BPF_DIV | BPF_K] = true, 950 [BPF_ALU | BPF_DIV | BPF_X] = true, 951 [BPF_ALU | BPF_MOD | BPF_K] = true, 952 [BPF_ALU | BPF_MOD | BPF_X] = true, 953 [BPF_ALU | BPF_AND | BPF_K] = true, 954 [BPF_ALU | BPF_AND | BPF_X] = true, 955 [BPF_ALU | BPF_OR | BPF_K] = true, 956 [BPF_ALU | BPF_OR | BPF_X] = true, 957 [BPF_ALU | BPF_XOR | BPF_K] = true, 958 [BPF_ALU | BPF_XOR | BPF_X] = true, 959 [BPF_ALU | BPF_LSH | BPF_K] = true, 960 [BPF_ALU | BPF_LSH | BPF_X] = true, 961 [BPF_ALU | BPF_RSH | BPF_K] = true, 962 [BPF_ALU | BPF_RSH | BPF_X] = true, 963 [BPF_ALU | BPF_NEG] = true, 964 /* Load instructions */ 965 [BPF_LD | BPF_W | BPF_ABS] = true, 966 [BPF_LD | BPF_H | BPF_ABS] = true, 967 [BPF_LD | BPF_B | BPF_ABS] = true, 968 [BPF_LD | BPF_W | BPF_LEN] = true, 969 [BPF_LD | BPF_W | BPF_IND] = true, 970 [BPF_LD | BPF_H | BPF_IND] = true, 971 [BPF_LD | BPF_B | BPF_IND] = true, 972 [BPF_LD | BPF_IMM] = true, 973 [BPF_LD | BPF_MEM] = true, 974 [BPF_LDX | BPF_W | BPF_LEN] = true, 975 [BPF_LDX | BPF_B | BPF_MSH] = true, 976 [BPF_LDX | BPF_IMM] = true, 977 [BPF_LDX | BPF_MEM] = true, 978 /* Store instructions */ 979 [BPF_ST] = true, 980 [BPF_STX] = true, 981 /* Misc instructions */ 982 [BPF_MISC | BPF_TAX] = true, 983 [BPF_MISC | BPF_TXA] = true, 984 /* Return instructions */ 985 [BPF_RET | BPF_K] = true, 986 [BPF_RET | BPF_A] = true, 987 /* Jump instructions */ 988 [BPF_JMP | BPF_JA] = true, 989 [BPF_JMP | BPF_JEQ | BPF_K] = true, 990 [BPF_JMP | BPF_JEQ | BPF_X] = true, 991 [BPF_JMP | BPF_JGE | BPF_K] = true, 992 [BPF_JMP | BPF_JGE | BPF_X] = true, 993 [BPF_JMP | BPF_JGT | BPF_K] = true, 994 [BPF_JMP | BPF_JGT | BPF_X] = true, 995 [BPF_JMP | BPF_JSET | BPF_K] = true, 996 [BPF_JMP | BPF_JSET | BPF_X] = true, 997 }; 998 999 if (code_to_probe >= ARRAY_SIZE(codes)) 1000 return false; 1001 1002 return codes[code_to_probe]; 1003 } 1004 1005 static bool bpf_check_basics_ok(const struct sock_filter *filter, 1006 unsigned int flen) 1007 { 1008 if (filter == NULL) 1009 return false; 1010 if (flen == 0 || flen > BPF_MAXINSNS) 1011 return false; 1012 1013 return true; 1014 } 1015 1016 /** 1017 * bpf_check_classic - verify socket filter code 1018 * @filter: filter to verify 1019 * @flen: length of filter 1020 * 1021 * Check the user's filter code. If we let some ugly 1022 * filter code slip through kaboom! The filter must contain 1023 * no references or jumps that are out of range, no illegal 1024 * instructions, and must end with a RET instruction. 1025 * 1026 * All jumps are forward as they are not signed. 1027 * 1028 * Returns 0 if the rule set is legal or -EINVAL if not. 1029 */ 1030 static int bpf_check_classic(const struct sock_filter *filter, 1031 unsigned int flen) 1032 { 1033 bool anc_found; 1034 int pc; 1035 1036 /* Check the filter code now */ 1037 for (pc = 0; pc < flen; pc++) { 1038 const struct sock_filter *ftest = &filter[pc]; 1039 1040 /* May we actually operate on this code? */ 1041 if (!chk_code_allowed(ftest->code)) 1042 return -EINVAL; 1043 1044 /* Some instructions need special checks */ 1045 switch (ftest->code) { 1046 case BPF_ALU | BPF_DIV | BPF_K: 1047 case BPF_ALU | BPF_MOD | BPF_K: 1048 /* Check for division by zero */ 1049 if (ftest->k == 0) 1050 return -EINVAL; 1051 break; 1052 case BPF_ALU | BPF_LSH | BPF_K: 1053 case BPF_ALU | BPF_RSH | BPF_K: 1054 if (ftest->k >= 32) 1055 return -EINVAL; 1056 break; 1057 case BPF_LD | BPF_MEM: 1058 case BPF_LDX | BPF_MEM: 1059 case BPF_ST: 1060 case BPF_STX: 1061 /* Check for invalid memory addresses */ 1062 if (ftest->k >= BPF_MEMWORDS) 1063 return -EINVAL; 1064 break; 1065 case BPF_JMP | BPF_JA: 1066 /* Note, the large ftest->k might cause loops. 1067 * Compare this with conditional jumps below, 1068 * where offsets are limited. --ANK (981016) 1069 */ 1070 if (ftest->k >= (unsigned int)(flen - pc - 1)) 1071 return -EINVAL; 1072 break; 1073 case BPF_JMP | BPF_JEQ | BPF_K: 1074 case BPF_JMP | BPF_JEQ | BPF_X: 1075 case BPF_JMP | BPF_JGE | BPF_K: 1076 case BPF_JMP | BPF_JGE | BPF_X: 1077 case BPF_JMP | BPF_JGT | BPF_K: 1078 case BPF_JMP | BPF_JGT | BPF_X: 1079 case BPF_JMP | BPF_JSET | BPF_K: 1080 case BPF_JMP | BPF_JSET | BPF_X: 1081 /* Both conditionals must be safe */ 1082 if (pc + ftest->jt + 1 >= flen || 1083 pc + ftest->jf + 1 >= flen) 1084 return -EINVAL; 1085 break; 1086 case BPF_LD | BPF_W | BPF_ABS: 1087 case BPF_LD | BPF_H | BPF_ABS: 1088 case BPF_LD | BPF_B | BPF_ABS: 1089 anc_found = false; 1090 if (bpf_anc_helper(ftest) & BPF_ANC) 1091 anc_found = true; 1092 /* Ancillary operation unknown or unsupported */ 1093 if (anc_found == false && ftest->k >= SKF_AD_OFF) 1094 return -EINVAL; 1095 } 1096 } 1097 1098 /* Last instruction must be a RET code */ 1099 switch (filter[flen - 1].code) { 1100 case BPF_RET | BPF_K: 1101 case BPF_RET | BPF_A: 1102 return check_load_and_stores(filter, flen); 1103 } 1104 1105 return -EINVAL; 1106 } 1107 1108 static int bpf_prog_store_orig_filter(struct bpf_prog *fp, 1109 const struct sock_fprog *fprog) 1110 { 1111 unsigned int fsize = bpf_classic_proglen(fprog); 1112 struct sock_fprog_kern *fkprog; 1113 1114 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL); 1115 if (!fp->orig_prog) 1116 return -ENOMEM; 1117 1118 fkprog = fp->orig_prog; 1119 fkprog->len = fprog->len; 1120 1121 fkprog->filter = kmemdup(fp->insns, fsize, 1122 GFP_KERNEL | __GFP_NOWARN); 1123 if (!fkprog->filter) { 1124 kfree(fp->orig_prog); 1125 return -ENOMEM; 1126 } 1127 1128 return 0; 1129 } 1130 1131 static void bpf_release_orig_filter(struct bpf_prog *fp) 1132 { 1133 struct sock_fprog_kern *fprog = fp->orig_prog; 1134 1135 if (fprog) { 1136 kfree(fprog->filter); 1137 kfree(fprog); 1138 } 1139 } 1140 1141 static void __bpf_prog_release(struct bpf_prog *prog) 1142 { 1143 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) { 1144 bpf_prog_put(prog); 1145 } else { 1146 bpf_release_orig_filter(prog); 1147 bpf_prog_free(prog); 1148 } 1149 } 1150 1151 static void __sk_filter_release(struct sk_filter *fp) 1152 { 1153 __bpf_prog_release(fp->prog); 1154 kfree(fp); 1155 } 1156 1157 /** 1158 * sk_filter_release_rcu - Release a socket filter by rcu_head 1159 * @rcu: rcu_head that contains the sk_filter to free 1160 */ 1161 static void sk_filter_release_rcu(struct rcu_head *rcu) 1162 { 1163 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu); 1164 1165 __sk_filter_release(fp); 1166 } 1167 1168 /** 1169 * sk_filter_release - release a socket filter 1170 * @fp: filter to remove 1171 * 1172 * Remove a filter from a socket and release its resources. 1173 */ 1174 static void sk_filter_release(struct sk_filter *fp) 1175 { 1176 if (refcount_dec_and_test(&fp->refcnt)) 1177 call_rcu(&fp->rcu, sk_filter_release_rcu); 1178 } 1179 1180 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp) 1181 { 1182 u32 filter_size = bpf_prog_size(fp->prog->len); 1183 1184 atomic_sub(filter_size, &sk->sk_omem_alloc); 1185 sk_filter_release(fp); 1186 } 1187 1188 /* try to charge the socket memory if there is space available 1189 * return true on success 1190 */ 1191 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp) 1192 { 1193 u32 filter_size = bpf_prog_size(fp->prog->len); 1194 1195 /* same check as in sock_kmalloc() */ 1196 if (filter_size <= sysctl_optmem_max && 1197 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) { 1198 atomic_add(filter_size, &sk->sk_omem_alloc); 1199 return true; 1200 } 1201 return false; 1202 } 1203 1204 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp) 1205 { 1206 if (!refcount_inc_not_zero(&fp->refcnt)) 1207 return false; 1208 1209 if (!__sk_filter_charge(sk, fp)) { 1210 sk_filter_release(fp); 1211 return false; 1212 } 1213 return true; 1214 } 1215 1216 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp) 1217 { 1218 struct sock_filter *old_prog; 1219 struct bpf_prog *old_fp; 1220 int err, new_len, old_len = fp->len; 1221 bool seen_ld_abs = false; 1222 1223 /* We are free to overwrite insns et al right here as it 1224 * won't be used at this point in time anymore internally 1225 * after the migration to the internal BPF instruction 1226 * representation. 1227 */ 1228 BUILD_BUG_ON(sizeof(struct sock_filter) != 1229 sizeof(struct bpf_insn)); 1230 1231 /* Conversion cannot happen on overlapping memory areas, 1232 * so we need to keep the user BPF around until the 2nd 1233 * pass. At this time, the user BPF is stored in fp->insns. 1234 */ 1235 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter), 1236 GFP_KERNEL | __GFP_NOWARN); 1237 if (!old_prog) { 1238 err = -ENOMEM; 1239 goto out_err; 1240 } 1241 1242 /* 1st pass: calculate the new program length. */ 1243 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len, 1244 &seen_ld_abs); 1245 if (err) 1246 goto out_err_free; 1247 1248 /* Expand fp for appending the new filter representation. */ 1249 old_fp = fp; 1250 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0); 1251 if (!fp) { 1252 /* The old_fp is still around in case we couldn't 1253 * allocate new memory, so uncharge on that one. 1254 */ 1255 fp = old_fp; 1256 err = -ENOMEM; 1257 goto out_err_free; 1258 } 1259 1260 fp->len = new_len; 1261 1262 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */ 1263 err = bpf_convert_filter(old_prog, old_len, fp, &new_len, 1264 &seen_ld_abs); 1265 if (err) 1266 /* 2nd bpf_convert_filter() can fail only if it fails 1267 * to allocate memory, remapping must succeed. Note, 1268 * that at this time old_fp has already been released 1269 * by krealloc(). 1270 */ 1271 goto out_err_free; 1272 1273 fp = bpf_prog_select_runtime(fp, &err); 1274 if (err) 1275 goto out_err_free; 1276 1277 kfree(old_prog); 1278 return fp; 1279 1280 out_err_free: 1281 kfree(old_prog); 1282 out_err: 1283 __bpf_prog_release(fp); 1284 return ERR_PTR(err); 1285 } 1286 1287 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp, 1288 bpf_aux_classic_check_t trans) 1289 { 1290 int err; 1291 1292 fp->bpf_func = NULL; 1293 fp->jited = 0; 1294 1295 err = bpf_check_classic(fp->insns, fp->len); 1296 if (err) { 1297 __bpf_prog_release(fp); 1298 return ERR_PTR(err); 1299 } 1300 1301 /* There might be additional checks and transformations 1302 * needed on classic filters, f.e. in case of seccomp. 1303 */ 1304 if (trans) { 1305 err = trans(fp->insns, fp->len); 1306 if (err) { 1307 __bpf_prog_release(fp); 1308 return ERR_PTR(err); 1309 } 1310 } 1311 1312 /* Probe if we can JIT compile the filter and if so, do 1313 * the compilation of the filter. 1314 */ 1315 bpf_jit_compile(fp); 1316 1317 /* JIT compiler couldn't process this filter, so do the 1318 * internal BPF translation for the optimized interpreter. 1319 */ 1320 if (!fp->jited) 1321 fp = bpf_migrate_filter(fp); 1322 1323 return fp; 1324 } 1325 1326 /** 1327 * bpf_prog_create - create an unattached filter 1328 * @pfp: the unattached filter that is created 1329 * @fprog: the filter program 1330 * 1331 * Create a filter independent of any socket. We first run some 1332 * sanity checks on it to make sure it does not explode on us later. 1333 * If an error occurs or there is insufficient memory for the filter 1334 * a negative errno code is returned. On success the return is zero. 1335 */ 1336 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog) 1337 { 1338 unsigned int fsize = bpf_classic_proglen(fprog); 1339 struct bpf_prog *fp; 1340 1341 /* Make sure new filter is there and in the right amounts. */ 1342 if (!bpf_check_basics_ok(fprog->filter, fprog->len)) 1343 return -EINVAL; 1344 1345 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0); 1346 if (!fp) 1347 return -ENOMEM; 1348 1349 memcpy(fp->insns, fprog->filter, fsize); 1350 1351 fp->len = fprog->len; 1352 /* Since unattached filters are not copied back to user 1353 * space through sk_get_filter(), we do not need to hold 1354 * a copy here, and can spare us the work. 1355 */ 1356 fp->orig_prog = NULL; 1357 1358 /* bpf_prepare_filter() already takes care of freeing 1359 * memory in case something goes wrong. 1360 */ 1361 fp = bpf_prepare_filter(fp, NULL); 1362 if (IS_ERR(fp)) 1363 return PTR_ERR(fp); 1364 1365 *pfp = fp; 1366 return 0; 1367 } 1368 EXPORT_SYMBOL_GPL(bpf_prog_create); 1369 1370 /** 1371 * bpf_prog_create_from_user - create an unattached filter from user buffer 1372 * @pfp: the unattached filter that is created 1373 * @fprog: the filter program 1374 * @trans: post-classic verifier transformation handler 1375 * @save_orig: save classic BPF program 1376 * 1377 * This function effectively does the same as bpf_prog_create(), only 1378 * that it builds up its insns buffer from user space provided buffer. 1379 * It also allows for passing a bpf_aux_classic_check_t handler. 1380 */ 1381 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog, 1382 bpf_aux_classic_check_t trans, bool save_orig) 1383 { 1384 unsigned int fsize = bpf_classic_proglen(fprog); 1385 struct bpf_prog *fp; 1386 int err; 1387 1388 /* Make sure new filter is there and in the right amounts. */ 1389 if (!bpf_check_basics_ok(fprog->filter, fprog->len)) 1390 return -EINVAL; 1391 1392 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0); 1393 if (!fp) 1394 return -ENOMEM; 1395 1396 if (copy_from_user(fp->insns, fprog->filter, fsize)) { 1397 __bpf_prog_free(fp); 1398 return -EFAULT; 1399 } 1400 1401 fp->len = fprog->len; 1402 fp->orig_prog = NULL; 1403 1404 if (save_orig) { 1405 err = bpf_prog_store_orig_filter(fp, fprog); 1406 if (err) { 1407 __bpf_prog_free(fp); 1408 return -ENOMEM; 1409 } 1410 } 1411 1412 /* bpf_prepare_filter() already takes care of freeing 1413 * memory in case something goes wrong. 1414 */ 1415 fp = bpf_prepare_filter(fp, trans); 1416 if (IS_ERR(fp)) 1417 return PTR_ERR(fp); 1418 1419 *pfp = fp; 1420 return 0; 1421 } 1422 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user); 1423 1424 void bpf_prog_destroy(struct bpf_prog *fp) 1425 { 1426 __bpf_prog_release(fp); 1427 } 1428 EXPORT_SYMBOL_GPL(bpf_prog_destroy); 1429 1430 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk) 1431 { 1432 struct sk_filter *fp, *old_fp; 1433 1434 fp = kmalloc(sizeof(*fp), GFP_KERNEL); 1435 if (!fp) 1436 return -ENOMEM; 1437 1438 fp->prog = prog; 1439 1440 if (!__sk_filter_charge(sk, fp)) { 1441 kfree(fp); 1442 return -ENOMEM; 1443 } 1444 refcount_set(&fp->refcnt, 1); 1445 1446 old_fp = rcu_dereference_protected(sk->sk_filter, 1447 lockdep_sock_is_held(sk)); 1448 rcu_assign_pointer(sk->sk_filter, fp); 1449 1450 if (old_fp) 1451 sk_filter_uncharge(sk, old_fp); 1452 1453 return 0; 1454 } 1455 1456 static 1457 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk) 1458 { 1459 unsigned int fsize = bpf_classic_proglen(fprog); 1460 struct bpf_prog *prog; 1461 int err; 1462 1463 if (sock_flag(sk, SOCK_FILTER_LOCKED)) 1464 return ERR_PTR(-EPERM); 1465 1466 /* Make sure new filter is there and in the right amounts. */ 1467 if (!bpf_check_basics_ok(fprog->filter, fprog->len)) 1468 return ERR_PTR(-EINVAL); 1469 1470 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0); 1471 if (!prog) 1472 return ERR_PTR(-ENOMEM); 1473 1474 if (copy_from_user(prog->insns, fprog->filter, fsize)) { 1475 __bpf_prog_free(prog); 1476 return ERR_PTR(-EFAULT); 1477 } 1478 1479 prog->len = fprog->len; 1480 1481 err = bpf_prog_store_orig_filter(prog, fprog); 1482 if (err) { 1483 __bpf_prog_free(prog); 1484 return ERR_PTR(-ENOMEM); 1485 } 1486 1487 /* bpf_prepare_filter() already takes care of freeing 1488 * memory in case something goes wrong. 1489 */ 1490 return bpf_prepare_filter(prog, NULL); 1491 } 1492 1493 /** 1494 * sk_attach_filter - attach a socket filter 1495 * @fprog: the filter program 1496 * @sk: the socket to use 1497 * 1498 * Attach the user's filter code. We first run some sanity checks on 1499 * it to make sure it does not explode on us later. If an error 1500 * occurs or there is insufficient memory for the filter a negative 1501 * errno code is returned. On success the return is zero. 1502 */ 1503 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk) 1504 { 1505 struct bpf_prog *prog = __get_filter(fprog, sk); 1506 int err; 1507 1508 if (IS_ERR(prog)) 1509 return PTR_ERR(prog); 1510 1511 err = __sk_attach_prog(prog, sk); 1512 if (err < 0) { 1513 __bpf_prog_release(prog); 1514 return err; 1515 } 1516 1517 return 0; 1518 } 1519 EXPORT_SYMBOL_GPL(sk_attach_filter); 1520 1521 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk) 1522 { 1523 struct bpf_prog *prog = __get_filter(fprog, sk); 1524 int err; 1525 1526 if (IS_ERR(prog)) 1527 return PTR_ERR(prog); 1528 1529 if (bpf_prog_size(prog->len) > sysctl_optmem_max) 1530 err = -ENOMEM; 1531 else 1532 err = reuseport_attach_prog(sk, prog); 1533 1534 if (err) 1535 __bpf_prog_release(prog); 1536 1537 return err; 1538 } 1539 1540 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk) 1541 { 1542 if (sock_flag(sk, SOCK_FILTER_LOCKED)) 1543 return ERR_PTR(-EPERM); 1544 1545 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER); 1546 } 1547 1548 int sk_attach_bpf(u32 ufd, struct sock *sk) 1549 { 1550 struct bpf_prog *prog = __get_bpf(ufd, sk); 1551 int err; 1552 1553 if (IS_ERR(prog)) 1554 return PTR_ERR(prog); 1555 1556 err = __sk_attach_prog(prog, sk); 1557 if (err < 0) { 1558 bpf_prog_put(prog); 1559 return err; 1560 } 1561 1562 return 0; 1563 } 1564 1565 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk) 1566 { 1567 struct bpf_prog *prog; 1568 int err; 1569 1570 if (sock_flag(sk, SOCK_FILTER_LOCKED)) 1571 return -EPERM; 1572 1573 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER); 1574 if (IS_ERR(prog) && PTR_ERR(prog) == -EINVAL) 1575 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SK_REUSEPORT); 1576 if (IS_ERR(prog)) 1577 return PTR_ERR(prog); 1578 1579 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT) { 1580 /* Like other non BPF_PROG_TYPE_SOCKET_FILTER 1581 * bpf prog (e.g. sockmap). It depends on the 1582 * limitation imposed by bpf_prog_load(). 1583 * Hence, sysctl_optmem_max is not checked. 1584 */ 1585 if ((sk->sk_type != SOCK_STREAM && 1586 sk->sk_type != SOCK_DGRAM) || 1587 (sk->sk_protocol != IPPROTO_UDP && 1588 sk->sk_protocol != IPPROTO_TCP) || 1589 (sk->sk_family != AF_INET && 1590 sk->sk_family != AF_INET6)) { 1591 err = -ENOTSUPP; 1592 goto err_prog_put; 1593 } 1594 } else { 1595 /* BPF_PROG_TYPE_SOCKET_FILTER */ 1596 if (bpf_prog_size(prog->len) > sysctl_optmem_max) { 1597 err = -ENOMEM; 1598 goto err_prog_put; 1599 } 1600 } 1601 1602 err = reuseport_attach_prog(sk, prog); 1603 err_prog_put: 1604 if (err) 1605 bpf_prog_put(prog); 1606 1607 return err; 1608 } 1609 1610 void sk_reuseport_prog_free(struct bpf_prog *prog) 1611 { 1612 if (!prog) 1613 return; 1614 1615 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT) 1616 bpf_prog_put(prog); 1617 else 1618 bpf_prog_destroy(prog); 1619 } 1620 1621 struct bpf_scratchpad { 1622 union { 1623 __be32 diff[MAX_BPF_STACK / sizeof(__be32)]; 1624 u8 buff[MAX_BPF_STACK]; 1625 }; 1626 }; 1627 1628 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp); 1629 1630 static inline int __bpf_try_make_writable(struct sk_buff *skb, 1631 unsigned int write_len) 1632 { 1633 return skb_ensure_writable(skb, write_len); 1634 } 1635 1636 static inline int bpf_try_make_writable(struct sk_buff *skb, 1637 unsigned int write_len) 1638 { 1639 int err = __bpf_try_make_writable(skb, write_len); 1640 1641 bpf_compute_data_pointers(skb); 1642 return err; 1643 } 1644 1645 static int bpf_try_make_head_writable(struct sk_buff *skb) 1646 { 1647 return bpf_try_make_writable(skb, skb_headlen(skb)); 1648 } 1649 1650 static inline void bpf_push_mac_rcsum(struct sk_buff *skb) 1651 { 1652 if (skb_at_tc_ingress(skb)) 1653 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len); 1654 } 1655 1656 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb) 1657 { 1658 if (skb_at_tc_ingress(skb)) 1659 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len); 1660 } 1661 1662 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset, 1663 const void *, from, u32, len, u64, flags) 1664 { 1665 void *ptr; 1666 1667 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH))) 1668 return -EINVAL; 1669 if (unlikely(offset > 0xffff)) 1670 return -EFAULT; 1671 if (unlikely(bpf_try_make_writable(skb, offset + len))) 1672 return -EFAULT; 1673 1674 ptr = skb->data + offset; 1675 if (flags & BPF_F_RECOMPUTE_CSUM) 1676 __skb_postpull_rcsum(skb, ptr, len, offset); 1677 1678 memcpy(ptr, from, len); 1679 1680 if (flags & BPF_F_RECOMPUTE_CSUM) 1681 __skb_postpush_rcsum(skb, ptr, len, offset); 1682 if (flags & BPF_F_INVALIDATE_HASH) 1683 skb_clear_hash(skb); 1684 1685 return 0; 1686 } 1687 1688 static const struct bpf_func_proto bpf_skb_store_bytes_proto = { 1689 .func = bpf_skb_store_bytes, 1690 .gpl_only = false, 1691 .ret_type = RET_INTEGER, 1692 .arg1_type = ARG_PTR_TO_CTX, 1693 .arg2_type = ARG_ANYTHING, 1694 .arg3_type = ARG_PTR_TO_MEM, 1695 .arg4_type = ARG_CONST_SIZE, 1696 .arg5_type = ARG_ANYTHING, 1697 }; 1698 1699 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset, 1700 void *, to, u32, len) 1701 { 1702 void *ptr; 1703 1704 if (unlikely(offset > 0xffff)) 1705 goto err_clear; 1706 1707 ptr = skb_header_pointer(skb, offset, len, to); 1708 if (unlikely(!ptr)) 1709 goto err_clear; 1710 if (ptr != to) 1711 memcpy(to, ptr, len); 1712 1713 return 0; 1714 err_clear: 1715 memset(to, 0, len); 1716 return -EFAULT; 1717 } 1718 1719 static const struct bpf_func_proto bpf_skb_load_bytes_proto = { 1720 .func = bpf_skb_load_bytes, 1721 .gpl_only = false, 1722 .ret_type = RET_INTEGER, 1723 .arg1_type = ARG_PTR_TO_CTX, 1724 .arg2_type = ARG_ANYTHING, 1725 .arg3_type = ARG_PTR_TO_UNINIT_MEM, 1726 .arg4_type = ARG_CONST_SIZE, 1727 }; 1728 1729 BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb, 1730 u32, offset, void *, to, u32, len, u32, start_header) 1731 { 1732 u8 *end = skb_tail_pointer(skb); 1733 u8 *net = skb_network_header(skb); 1734 u8 *mac = skb_mac_header(skb); 1735 u8 *ptr; 1736 1737 if (unlikely(offset > 0xffff || len > (end - mac))) 1738 goto err_clear; 1739 1740 switch (start_header) { 1741 case BPF_HDR_START_MAC: 1742 ptr = mac + offset; 1743 break; 1744 case BPF_HDR_START_NET: 1745 ptr = net + offset; 1746 break; 1747 default: 1748 goto err_clear; 1749 } 1750 1751 if (likely(ptr >= mac && ptr + len <= end)) { 1752 memcpy(to, ptr, len); 1753 return 0; 1754 } 1755 1756 err_clear: 1757 memset(to, 0, len); 1758 return -EFAULT; 1759 } 1760 1761 static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto = { 1762 .func = bpf_skb_load_bytes_relative, 1763 .gpl_only = false, 1764 .ret_type = RET_INTEGER, 1765 .arg1_type = ARG_PTR_TO_CTX, 1766 .arg2_type = ARG_ANYTHING, 1767 .arg3_type = ARG_PTR_TO_UNINIT_MEM, 1768 .arg4_type = ARG_CONST_SIZE, 1769 .arg5_type = ARG_ANYTHING, 1770 }; 1771 1772 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len) 1773 { 1774 /* Idea is the following: should the needed direct read/write 1775 * test fail during runtime, we can pull in more data and redo 1776 * again, since implicitly, we invalidate previous checks here. 1777 * 1778 * Or, since we know how much we need to make read/writeable, 1779 * this can be done once at the program beginning for direct 1780 * access case. By this we overcome limitations of only current 1781 * headroom being accessible. 1782 */ 1783 return bpf_try_make_writable(skb, len ? : skb_headlen(skb)); 1784 } 1785 1786 static const struct bpf_func_proto bpf_skb_pull_data_proto = { 1787 .func = bpf_skb_pull_data, 1788 .gpl_only = false, 1789 .ret_type = RET_INTEGER, 1790 .arg1_type = ARG_PTR_TO_CTX, 1791 .arg2_type = ARG_ANYTHING, 1792 }; 1793 1794 static inline int sk_skb_try_make_writable(struct sk_buff *skb, 1795 unsigned int write_len) 1796 { 1797 int err = __bpf_try_make_writable(skb, write_len); 1798 1799 bpf_compute_data_end_sk_skb(skb); 1800 return err; 1801 } 1802 1803 BPF_CALL_2(sk_skb_pull_data, struct sk_buff *, skb, u32, len) 1804 { 1805 /* Idea is the following: should the needed direct read/write 1806 * test fail during runtime, we can pull in more data and redo 1807 * again, since implicitly, we invalidate previous checks here. 1808 * 1809 * Or, since we know how much we need to make read/writeable, 1810 * this can be done once at the program beginning for direct 1811 * access case. By this we overcome limitations of only current 1812 * headroom being accessible. 1813 */ 1814 return sk_skb_try_make_writable(skb, len ? : skb_headlen(skb)); 1815 } 1816 1817 static const struct bpf_func_proto sk_skb_pull_data_proto = { 1818 .func = sk_skb_pull_data, 1819 .gpl_only = false, 1820 .ret_type = RET_INTEGER, 1821 .arg1_type = ARG_PTR_TO_CTX, 1822 .arg2_type = ARG_ANYTHING, 1823 }; 1824 1825 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset, 1826 u64, from, u64, to, u64, flags) 1827 { 1828 __sum16 *ptr; 1829 1830 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK))) 1831 return -EINVAL; 1832 if (unlikely(offset > 0xffff || offset & 1)) 1833 return -EFAULT; 1834 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr)))) 1835 return -EFAULT; 1836 1837 ptr = (__sum16 *)(skb->data + offset); 1838 switch (flags & BPF_F_HDR_FIELD_MASK) { 1839 case 0: 1840 if (unlikely(from != 0)) 1841 return -EINVAL; 1842 1843 csum_replace_by_diff(ptr, to); 1844 break; 1845 case 2: 1846 csum_replace2(ptr, from, to); 1847 break; 1848 case 4: 1849 csum_replace4(ptr, from, to); 1850 break; 1851 default: 1852 return -EINVAL; 1853 } 1854 1855 return 0; 1856 } 1857 1858 static const struct bpf_func_proto bpf_l3_csum_replace_proto = { 1859 .func = bpf_l3_csum_replace, 1860 .gpl_only = false, 1861 .ret_type = RET_INTEGER, 1862 .arg1_type = ARG_PTR_TO_CTX, 1863 .arg2_type = ARG_ANYTHING, 1864 .arg3_type = ARG_ANYTHING, 1865 .arg4_type = ARG_ANYTHING, 1866 .arg5_type = ARG_ANYTHING, 1867 }; 1868 1869 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset, 1870 u64, from, u64, to, u64, flags) 1871 { 1872 bool is_pseudo = flags & BPF_F_PSEUDO_HDR; 1873 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0; 1874 bool do_mforce = flags & BPF_F_MARK_ENFORCE; 1875 __sum16 *ptr; 1876 1877 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE | 1878 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK))) 1879 return -EINVAL; 1880 if (unlikely(offset > 0xffff || offset & 1)) 1881 return -EFAULT; 1882 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr)))) 1883 return -EFAULT; 1884 1885 ptr = (__sum16 *)(skb->data + offset); 1886 if (is_mmzero && !do_mforce && !*ptr) 1887 return 0; 1888 1889 switch (flags & BPF_F_HDR_FIELD_MASK) { 1890 case 0: 1891 if (unlikely(from != 0)) 1892 return -EINVAL; 1893 1894 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo); 1895 break; 1896 case 2: 1897 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo); 1898 break; 1899 case 4: 1900 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo); 1901 break; 1902 default: 1903 return -EINVAL; 1904 } 1905 1906 if (is_mmzero && !*ptr) 1907 *ptr = CSUM_MANGLED_0; 1908 return 0; 1909 } 1910 1911 static const struct bpf_func_proto bpf_l4_csum_replace_proto = { 1912 .func = bpf_l4_csum_replace, 1913 .gpl_only = false, 1914 .ret_type = RET_INTEGER, 1915 .arg1_type = ARG_PTR_TO_CTX, 1916 .arg2_type = ARG_ANYTHING, 1917 .arg3_type = ARG_ANYTHING, 1918 .arg4_type = ARG_ANYTHING, 1919 .arg5_type = ARG_ANYTHING, 1920 }; 1921 1922 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size, 1923 __be32 *, to, u32, to_size, __wsum, seed) 1924 { 1925 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp); 1926 u32 diff_size = from_size + to_size; 1927 int i, j = 0; 1928 1929 /* This is quite flexible, some examples: 1930 * 1931 * from_size == 0, to_size > 0, seed := csum --> pushing data 1932 * from_size > 0, to_size == 0, seed := csum --> pulling data 1933 * from_size > 0, to_size > 0, seed := 0 --> diffing data 1934 * 1935 * Even for diffing, from_size and to_size don't need to be equal. 1936 */ 1937 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) || 1938 diff_size > sizeof(sp->diff))) 1939 return -EINVAL; 1940 1941 for (i = 0; i < from_size / sizeof(__be32); i++, j++) 1942 sp->diff[j] = ~from[i]; 1943 for (i = 0; i < to_size / sizeof(__be32); i++, j++) 1944 sp->diff[j] = to[i]; 1945 1946 return csum_partial(sp->diff, diff_size, seed); 1947 } 1948 1949 static const struct bpf_func_proto bpf_csum_diff_proto = { 1950 .func = bpf_csum_diff, 1951 .gpl_only = false, 1952 .pkt_access = true, 1953 .ret_type = RET_INTEGER, 1954 .arg1_type = ARG_PTR_TO_MEM_OR_NULL, 1955 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 1956 .arg3_type = ARG_PTR_TO_MEM_OR_NULL, 1957 .arg4_type = ARG_CONST_SIZE_OR_ZERO, 1958 .arg5_type = ARG_ANYTHING, 1959 }; 1960 1961 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum) 1962 { 1963 /* The interface is to be used in combination with bpf_csum_diff() 1964 * for direct packet writes. csum rotation for alignment as well 1965 * as emulating csum_sub() can be done from the eBPF program. 1966 */ 1967 if (skb->ip_summed == CHECKSUM_COMPLETE) 1968 return (skb->csum = csum_add(skb->csum, csum)); 1969 1970 return -ENOTSUPP; 1971 } 1972 1973 static const struct bpf_func_proto bpf_csum_update_proto = { 1974 .func = bpf_csum_update, 1975 .gpl_only = false, 1976 .ret_type = RET_INTEGER, 1977 .arg1_type = ARG_PTR_TO_CTX, 1978 .arg2_type = ARG_ANYTHING, 1979 }; 1980 1981 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb) 1982 { 1983 return dev_forward_skb(dev, skb); 1984 } 1985 1986 static inline int __bpf_rx_skb_no_mac(struct net_device *dev, 1987 struct sk_buff *skb) 1988 { 1989 int ret = ____dev_forward_skb(dev, skb); 1990 1991 if (likely(!ret)) { 1992 skb->dev = dev; 1993 ret = netif_rx(skb); 1994 } 1995 1996 return ret; 1997 } 1998 1999 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb) 2000 { 2001 int ret; 2002 2003 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) { 2004 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n"); 2005 kfree_skb(skb); 2006 return -ENETDOWN; 2007 } 2008 2009 skb->dev = dev; 2010 2011 __this_cpu_inc(xmit_recursion); 2012 ret = dev_queue_xmit(skb); 2013 __this_cpu_dec(xmit_recursion); 2014 2015 return ret; 2016 } 2017 2018 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev, 2019 u32 flags) 2020 { 2021 /* skb->mac_len is not set on normal egress */ 2022 unsigned int mlen = skb->network_header - skb->mac_header; 2023 2024 __skb_pull(skb, mlen); 2025 2026 /* At ingress, the mac header has already been pulled once. 2027 * At egress, skb_pospull_rcsum has to be done in case that 2028 * the skb is originated from ingress (i.e. a forwarded skb) 2029 * to ensure that rcsum starts at net header. 2030 */ 2031 if (!skb_at_tc_ingress(skb)) 2032 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen); 2033 skb_pop_mac_header(skb); 2034 skb_reset_mac_len(skb); 2035 return flags & BPF_F_INGRESS ? 2036 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb); 2037 } 2038 2039 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev, 2040 u32 flags) 2041 { 2042 /* Verify that a link layer header is carried */ 2043 if (unlikely(skb->mac_header >= skb->network_header)) { 2044 kfree_skb(skb); 2045 return -ERANGE; 2046 } 2047 2048 bpf_push_mac_rcsum(skb); 2049 return flags & BPF_F_INGRESS ? 2050 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb); 2051 } 2052 2053 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev, 2054 u32 flags) 2055 { 2056 if (dev_is_mac_header_xmit(dev)) 2057 return __bpf_redirect_common(skb, dev, flags); 2058 else 2059 return __bpf_redirect_no_mac(skb, dev, flags); 2060 } 2061 2062 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags) 2063 { 2064 struct net_device *dev; 2065 struct sk_buff *clone; 2066 int ret; 2067 2068 if (unlikely(flags & ~(BPF_F_INGRESS))) 2069 return -EINVAL; 2070 2071 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex); 2072 if (unlikely(!dev)) 2073 return -EINVAL; 2074 2075 clone = skb_clone(skb, GFP_ATOMIC); 2076 if (unlikely(!clone)) 2077 return -ENOMEM; 2078 2079 /* For direct write, we need to keep the invariant that the skbs 2080 * we're dealing with need to be uncloned. Should uncloning fail 2081 * here, we need to free the just generated clone to unclone once 2082 * again. 2083 */ 2084 ret = bpf_try_make_head_writable(skb); 2085 if (unlikely(ret)) { 2086 kfree_skb(clone); 2087 return -ENOMEM; 2088 } 2089 2090 return __bpf_redirect(clone, dev, flags); 2091 } 2092 2093 static const struct bpf_func_proto bpf_clone_redirect_proto = { 2094 .func = bpf_clone_redirect, 2095 .gpl_only = false, 2096 .ret_type = RET_INTEGER, 2097 .arg1_type = ARG_PTR_TO_CTX, 2098 .arg2_type = ARG_ANYTHING, 2099 .arg3_type = ARG_ANYTHING, 2100 }; 2101 2102 DEFINE_PER_CPU(struct bpf_redirect_info, bpf_redirect_info); 2103 EXPORT_PER_CPU_SYMBOL_GPL(bpf_redirect_info); 2104 2105 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags) 2106 { 2107 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 2108 2109 if (unlikely(flags & ~(BPF_F_INGRESS))) 2110 return TC_ACT_SHOT; 2111 2112 ri->ifindex = ifindex; 2113 ri->flags = flags; 2114 2115 return TC_ACT_REDIRECT; 2116 } 2117 2118 int skb_do_redirect(struct sk_buff *skb) 2119 { 2120 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 2121 struct net_device *dev; 2122 2123 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex); 2124 ri->ifindex = 0; 2125 if (unlikely(!dev)) { 2126 kfree_skb(skb); 2127 return -EINVAL; 2128 } 2129 2130 return __bpf_redirect(skb, dev, ri->flags); 2131 } 2132 2133 static const struct bpf_func_proto bpf_redirect_proto = { 2134 .func = bpf_redirect, 2135 .gpl_only = false, 2136 .ret_type = RET_INTEGER, 2137 .arg1_type = ARG_ANYTHING, 2138 .arg2_type = ARG_ANYTHING, 2139 }; 2140 2141 BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb, 2142 struct bpf_map *, map, void *, key, u64, flags) 2143 { 2144 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb); 2145 2146 /* If user passes invalid input drop the packet. */ 2147 if (unlikely(flags & ~(BPF_F_INGRESS))) 2148 return SK_DROP; 2149 2150 tcb->bpf.flags = flags; 2151 tcb->bpf.sk_redir = __sock_hash_lookup_elem(map, key); 2152 if (!tcb->bpf.sk_redir) 2153 return SK_DROP; 2154 2155 return SK_PASS; 2156 } 2157 2158 static const struct bpf_func_proto bpf_sk_redirect_hash_proto = { 2159 .func = bpf_sk_redirect_hash, 2160 .gpl_only = false, 2161 .ret_type = RET_INTEGER, 2162 .arg1_type = ARG_PTR_TO_CTX, 2163 .arg2_type = ARG_CONST_MAP_PTR, 2164 .arg3_type = ARG_PTR_TO_MAP_KEY, 2165 .arg4_type = ARG_ANYTHING, 2166 }; 2167 2168 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb, 2169 struct bpf_map *, map, u32, key, u64, flags) 2170 { 2171 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb); 2172 2173 /* If user passes invalid input drop the packet. */ 2174 if (unlikely(flags & ~(BPF_F_INGRESS))) 2175 return SK_DROP; 2176 2177 tcb->bpf.flags = flags; 2178 tcb->bpf.sk_redir = __sock_map_lookup_elem(map, key); 2179 if (!tcb->bpf.sk_redir) 2180 return SK_DROP; 2181 2182 return SK_PASS; 2183 } 2184 2185 struct sock *do_sk_redirect_map(struct sk_buff *skb) 2186 { 2187 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb); 2188 2189 return tcb->bpf.sk_redir; 2190 } 2191 2192 static const struct bpf_func_proto bpf_sk_redirect_map_proto = { 2193 .func = bpf_sk_redirect_map, 2194 .gpl_only = false, 2195 .ret_type = RET_INTEGER, 2196 .arg1_type = ARG_PTR_TO_CTX, 2197 .arg2_type = ARG_CONST_MAP_PTR, 2198 .arg3_type = ARG_ANYTHING, 2199 .arg4_type = ARG_ANYTHING, 2200 }; 2201 2202 BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg_buff *, msg, 2203 struct bpf_map *, map, void *, key, u64, flags) 2204 { 2205 /* If user passes invalid input drop the packet. */ 2206 if (unlikely(flags & ~(BPF_F_INGRESS))) 2207 return SK_DROP; 2208 2209 msg->flags = flags; 2210 msg->sk_redir = __sock_hash_lookup_elem(map, key); 2211 if (!msg->sk_redir) 2212 return SK_DROP; 2213 2214 return SK_PASS; 2215 } 2216 2217 static const struct bpf_func_proto bpf_msg_redirect_hash_proto = { 2218 .func = bpf_msg_redirect_hash, 2219 .gpl_only = false, 2220 .ret_type = RET_INTEGER, 2221 .arg1_type = ARG_PTR_TO_CTX, 2222 .arg2_type = ARG_CONST_MAP_PTR, 2223 .arg3_type = ARG_PTR_TO_MAP_KEY, 2224 .arg4_type = ARG_ANYTHING, 2225 }; 2226 2227 BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg_buff *, msg, 2228 struct bpf_map *, map, u32, key, u64, flags) 2229 { 2230 /* If user passes invalid input drop the packet. */ 2231 if (unlikely(flags & ~(BPF_F_INGRESS))) 2232 return SK_DROP; 2233 2234 msg->flags = flags; 2235 msg->sk_redir = __sock_map_lookup_elem(map, key); 2236 if (!msg->sk_redir) 2237 return SK_DROP; 2238 2239 return SK_PASS; 2240 } 2241 2242 struct sock *do_msg_redirect_map(struct sk_msg_buff *msg) 2243 { 2244 return msg->sk_redir; 2245 } 2246 2247 static const struct bpf_func_proto bpf_msg_redirect_map_proto = { 2248 .func = bpf_msg_redirect_map, 2249 .gpl_only = false, 2250 .ret_type = RET_INTEGER, 2251 .arg1_type = ARG_PTR_TO_CTX, 2252 .arg2_type = ARG_CONST_MAP_PTR, 2253 .arg3_type = ARG_ANYTHING, 2254 .arg4_type = ARG_ANYTHING, 2255 }; 2256 2257 BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg_buff *, msg, u32, bytes) 2258 { 2259 msg->apply_bytes = bytes; 2260 return 0; 2261 } 2262 2263 static const struct bpf_func_proto bpf_msg_apply_bytes_proto = { 2264 .func = bpf_msg_apply_bytes, 2265 .gpl_only = false, 2266 .ret_type = RET_INTEGER, 2267 .arg1_type = ARG_PTR_TO_CTX, 2268 .arg2_type = ARG_ANYTHING, 2269 }; 2270 2271 BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg_buff *, msg, u32, bytes) 2272 { 2273 msg->cork_bytes = bytes; 2274 return 0; 2275 } 2276 2277 static const struct bpf_func_proto bpf_msg_cork_bytes_proto = { 2278 .func = bpf_msg_cork_bytes, 2279 .gpl_only = false, 2280 .ret_type = RET_INTEGER, 2281 .arg1_type = ARG_PTR_TO_CTX, 2282 .arg2_type = ARG_ANYTHING, 2283 }; 2284 2285 BPF_CALL_4(bpf_msg_pull_data, 2286 struct sk_msg_buff *, msg, u32, start, u32, end, u64, flags) 2287 { 2288 unsigned int len = 0, offset = 0, copy = 0; 2289 struct scatterlist *sg = msg->sg_data; 2290 int first_sg, last_sg, i, shift; 2291 unsigned char *p, *to, *from; 2292 int bytes = end - start; 2293 struct page *page; 2294 2295 if (unlikely(flags || end <= start)) 2296 return -EINVAL; 2297 2298 /* First find the starting scatterlist element */ 2299 i = msg->sg_start; 2300 do { 2301 len = sg[i].length; 2302 offset += len; 2303 if (start < offset + len) 2304 break; 2305 i++; 2306 if (i == MAX_SKB_FRAGS) 2307 i = 0; 2308 } while (i != msg->sg_end); 2309 2310 if (unlikely(start >= offset + len)) 2311 return -EINVAL; 2312 2313 if (!msg->sg_copy[i] && bytes <= len) 2314 goto out; 2315 2316 first_sg = i; 2317 2318 /* At this point we need to linearize multiple scatterlist 2319 * elements or a single shared page. Either way we need to 2320 * copy into a linear buffer exclusively owned by BPF. Then 2321 * place the buffer in the scatterlist and fixup the original 2322 * entries by removing the entries now in the linear buffer 2323 * and shifting the remaining entries. For now we do not try 2324 * to copy partial entries to avoid complexity of running out 2325 * of sg_entry slots. The downside is reading a single byte 2326 * will copy the entire sg entry. 2327 */ 2328 do { 2329 copy += sg[i].length; 2330 i++; 2331 if (i == MAX_SKB_FRAGS) 2332 i = 0; 2333 if (bytes < copy) 2334 break; 2335 } while (i != msg->sg_end); 2336 last_sg = i; 2337 2338 if (unlikely(copy < end - start)) 2339 return -EINVAL; 2340 2341 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC, get_order(copy)); 2342 if (unlikely(!page)) 2343 return -ENOMEM; 2344 p = page_address(page); 2345 offset = 0; 2346 2347 i = first_sg; 2348 do { 2349 from = sg_virt(&sg[i]); 2350 len = sg[i].length; 2351 to = p + offset; 2352 2353 memcpy(to, from, len); 2354 offset += len; 2355 sg[i].length = 0; 2356 put_page(sg_page(&sg[i])); 2357 2358 i++; 2359 if (i == MAX_SKB_FRAGS) 2360 i = 0; 2361 } while (i != last_sg); 2362 2363 sg[first_sg].length = copy; 2364 sg_set_page(&sg[first_sg], page, copy, 0); 2365 2366 /* To repair sg ring we need to shift entries. If we only 2367 * had a single entry though we can just replace it and 2368 * be done. Otherwise walk the ring and shift the entries. 2369 */ 2370 shift = last_sg - first_sg - 1; 2371 if (!shift) 2372 goto out; 2373 2374 i = first_sg + 1; 2375 do { 2376 int move_from; 2377 2378 if (i + shift >= MAX_SKB_FRAGS) 2379 move_from = i + shift - MAX_SKB_FRAGS; 2380 else 2381 move_from = i + shift; 2382 2383 if (move_from == msg->sg_end) 2384 break; 2385 2386 sg[i] = sg[move_from]; 2387 sg[move_from].length = 0; 2388 sg[move_from].page_link = 0; 2389 sg[move_from].offset = 0; 2390 2391 i++; 2392 if (i == MAX_SKB_FRAGS) 2393 i = 0; 2394 } while (1); 2395 msg->sg_end -= shift; 2396 if (msg->sg_end < 0) 2397 msg->sg_end += MAX_SKB_FRAGS; 2398 out: 2399 msg->data = sg_virt(&sg[i]) + start - offset; 2400 msg->data_end = msg->data + bytes; 2401 2402 return 0; 2403 } 2404 2405 static const struct bpf_func_proto bpf_msg_pull_data_proto = { 2406 .func = bpf_msg_pull_data, 2407 .gpl_only = false, 2408 .ret_type = RET_INTEGER, 2409 .arg1_type = ARG_PTR_TO_CTX, 2410 .arg2_type = ARG_ANYTHING, 2411 .arg3_type = ARG_ANYTHING, 2412 .arg4_type = ARG_ANYTHING, 2413 }; 2414 2415 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb) 2416 { 2417 return task_get_classid(skb); 2418 } 2419 2420 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = { 2421 .func = bpf_get_cgroup_classid, 2422 .gpl_only = false, 2423 .ret_type = RET_INTEGER, 2424 .arg1_type = ARG_PTR_TO_CTX, 2425 }; 2426 2427 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb) 2428 { 2429 return dst_tclassid(skb); 2430 } 2431 2432 static const struct bpf_func_proto bpf_get_route_realm_proto = { 2433 .func = bpf_get_route_realm, 2434 .gpl_only = false, 2435 .ret_type = RET_INTEGER, 2436 .arg1_type = ARG_PTR_TO_CTX, 2437 }; 2438 2439 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb) 2440 { 2441 /* If skb_clear_hash() was called due to mangling, we can 2442 * trigger SW recalculation here. Later access to hash 2443 * can then use the inline skb->hash via context directly 2444 * instead of calling this helper again. 2445 */ 2446 return skb_get_hash(skb); 2447 } 2448 2449 static const struct bpf_func_proto bpf_get_hash_recalc_proto = { 2450 .func = bpf_get_hash_recalc, 2451 .gpl_only = false, 2452 .ret_type = RET_INTEGER, 2453 .arg1_type = ARG_PTR_TO_CTX, 2454 }; 2455 2456 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb) 2457 { 2458 /* After all direct packet write, this can be used once for 2459 * triggering a lazy recalc on next skb_get_hash() invocation. 2460 */ 2461 skb_clear_hash(skb); 2462 return 0; 2463 } 2464 2465 static const struct bpf_func_proto bpf_set_hash_invalid_proto = { 2466 .func = bpf_set_hash_invalid, 2467 .gpl_only = false, 2468 .ret_type = RET_INTEGER, 2469 .arg1_type = ARG_PTR_TO_CTX, 2470 }; 2471 2472 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash) 2473 { 2474 /* Set user specified hash as L4(+), so that it gets returned 2475 * on skb_get_hash() call unless BPF prog later on triggers a 2476 * skb_clear_hash(). 2477 */ 2478 __skb_set_sw_hash(skb, hash, true); 2479 return 0; 2480 } 2481 2482 static const struct bpf_func_proto bpf_set_hash_proto = { 2483 .func = bpf_set_hash, 2484 .gpl_only = false, 2485 .ret_type = RET_INTEGER, 2486 .arg1_type = ARG_PTR_TO_CTX, 2487 .arg2_type = ARG_ANYTHING, 2488 }; 2489 2490 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto, 2491 u16, vlan_tci) 2492 { 2493 int ret; 2494 2495 if (unlikely(vlan_proto != htons(ETH_P_8021Q) && 2496 vlan_proto != htons(ETH_P_8021AD))) 2497 vlan_proto = htons(ETH_P_8021Q); 2498 2499 bpf_push_mac_rcsum(skb); 2500 ret = skb_vlan_push(skb, vlan_proto, vlan_tci); 2501 bpf_pull_mac_rcsum(skb); 2502 2503 bpf_compute_data_pointers(skb); 2504 return ret; 2505 } 2506 2507 static const struct bpf_func_proto bpf_skb_vlan_push_proto = { 2508 .func = bpf_skb_vlan_push, 2509 .gpl_only = false, 2510 .ret_type = RET_INTEGER, 2511 .arg1_type = ARG_PTR_TO_CTX, 2512 .arg2_type = ARG_ANYTHING, 2513 .arg3_type = ARG_ANYTHING, 2514 }; 2515 2516 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb) 2517 { 2518 int ret; 2519 2520 bpf_push_mac_rcsum(skb); 2521 ret = skb_vlan_pop(skb); 2522 bpf_pull_mac_rcsum(skb); 2523 2524 bpf_compute_data_pointers(skb); 2525 return ret; 2526 } 2527 2528 static const struct bpf_func_proto bpf_skb_vlan_pop_proto = { 2529 .func = bpf_skb_vlan_pop, 2530 .gpl_only = false, 2531 .ret_type = RET_INTEGER, 2532 .arg1_type = ARG_PTR_TO_CTX, 2533 }; 2534 2535 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len) 2536 { 2537 /* Caller already did skb_cow() with len as headroom, 2538 * so no need to do it here. 2539 */ 2540 skb_push(skb, len); 2541 memmove(skb->data, skb->data + len, off); 2542 memset(skb->data + off, 0, len); 2543 2544 /* No skb_postpush_rcsum(skb, skb->data + off, len) 2545 * needed here as it does not change the skb->csum 2546 * result for checksum complete when summing over 2547 * zeroed blocks. 2548 */ 2549 return 0; 2550 } 2551 2552 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len) 2553 { 2554 /* skb_ensure_writable() is not needed here, as we're 2555 * already working on an uncloned skb. 2556 */ 2557 if (unlikely(!pskb_may_pull(skb, off + len))) 2558 return -ENOMEM; 2559 2560 skb_postpull_rcsum(skb, skb->data + off, len); 2561 memmove(skb->data + len, skb->data, off); 2562 __skb_pull(skb, len); 2563 2564 return 0; 2565 } 2566 2567 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len) 2568 { 2569 bool trans_same = skb->transport_header == skb->network_header; 2570 int ret; 2571 2572 /* There's no need for __skb_push()/__skb_pull() pair to 2573 * get to the start of the mac header as we're guaranteed 2574 * to always start from here under eBPF. 2575 */ 2576 ret = bpf_skb_generic_push(skb, off, len); 2577 if (likely(!ret)) { 2578 skb->mac_header -= len; 2579 skb->network_header -= len; 2580 if (trans_same) 2581 skb->transport_header = skb->network_header; 2582 } 2583 2584 return ret; 2585 } 2586 2587 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len) 2588 { 2589 bool trans_same = skb->transport_header == skb->network_header; 2590 int ret; 2591 2592 /* Same here, __skb_push()/__skb_pull() pair not needed. */ 2593 ret = bpf_skb_generic_pop(skb, off, len); 2594 if (likely(!ret)) { 2595 skb->mac_header += len; 2596 skb->network_header += len; 2597 if (trans_same) 2598 skb->transport_header = skb->network_header; 2599 } 2600 2601 return ret; 2602 } 2603 2604 static int bpf_skb_proto_4_to_6(struct sk_buff *skb) 2605 { 2606 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr); 2607 u32 off = skb_mac_header_len(skb); 2608 int ret; 2609 2610 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */ 2611 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb))) 2612 return -ENOTSUPP; 2613 2614 ret = skb_cow(skb, len_diff); 2615 if (unlikely(ret < 0)) 2616 return ret; 2617 2618 ret = bpf_skb_net_hdr_push(skb, off, len_diff); 2619 if (unlikely(ret < 0)) 2620 return ret; 2621 2622 if (skb_is_gso(skb)) { 2623 struct skb_shared_info *shinfo = skb_shinfo(skb); 2624 2625 /* SKB_GSO_TCPV4 needs to be changed into 2626 * SKB_GSO_TCPV6. 2627 */ 2628 if (shinfo->gso_type & SKB_GSO_TCPV4) { 2629 shinfo->gso_type &= ~SKB_GSO_TCPV4; 2630 shinfo->gso_type |= SKB_GSO_TCPV6; 2631 } 2632 2633 /* Due to IPv6 header, MSS needs to be downgraded. */ 2634 skb_decrease_gso_size(shinfo, len_diff); 2635 /* Header must be checked, and gso_segs recomputed. */ 2636 shinfo->gso_type |= SKB_GSO_DODGY; 2637 shinfo->gso_segs = 0; 2638 } 2639 2640 skb->protocol = htons(ETH_P_IPV6); 2641 skb_clear_hash(skb); 2642 2643 return 0; 2644 } 2645 2646 static int bpf_skb_proto_6_to_4(struct sk_buff *skb) 2647 { 2648 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr); 2649 u32 off = skb_mac_header_len(skb); 2650 int ret; 2651 2652 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */ 2653 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb))) 2654 return -ENOTSUPP; 2655 2656 ret = skb_unclone(skb, GFP_ATOMIC); 2657 if (unlikely(ret < 0)) 2658 return ret; 2659 2660 ret = bpf_skb_net_hdr_pop(skb, off, len_diff); 2661 if (unlikely(ret < 0)) 2662 return ret; 2663 2664 if (skb_is_gso(skb)) { 2665 struct skb_shared_info *shinfo = skb_shinfo(skb); 2666 2667 /* SKB_GSO_TCPV6 needs to be changed into 2668 * SKB_GSO_TCPV4. 2669 */ 2670 if (shinfo->gso_type & SKB_GSO_TCPV6) { 2671 shinfo->gso_type &= ~SKB_GSO_TCPV6; 2672 shinfo->gso_type |= SKB_GSO_TCPV4; 2673 } 2674 2675 /* Due to IPv4 header, MSS can be upgraded. */ 2676 skb_increase_gso_size(shinfo, len_diff); 2677 /* Header must be checked, and gso_segs recomputed. */ 2678 shinfo->gso_type |= SKB_GSO_DODGY; 2679 shinfo->gso_segs = 0; 2680 } 2681 2682 skb->protocol = htons(ETH_P_IP); 2683 skb_clear_hash(skb); 2684 2685 return 0; 2686 } 2687 2688 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto) 2689 { 2690 __be16 from_proto = skb->protocol; 2691 2692 if (from_proto == htons(ETH_P_IP) && 2693 to_proto == htons(ETH_P_IPV6)) 2694 return bpf_skb_proto_4_to_6(skb); 2695 2696 if (from_proto == htons(ETH_P_IPV6) && 2697 to_proto == htons(ETH_P_IP)) 2698 return bpf_skb_proto_6_to_4(skb); 2699 2700 return -ENOTSUPP; 2701 } 2702 2703 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto, 2704 u64, flags) 2705 { 2706 int ret; 2707 2708 if (unlikely(flags)) 2709 return -EINVAL; 2710 2711 /* General idea is that this helper does the basic groundwork 2712 * needed for changing the protocol, and eBPF program fills the 2713 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace() 2714 * and other helpers, rather than passing a raw buffer here. 2715 * 2716 * The rationale is to keep this minimal and without a need to 2717 * deal with raw packet data. F.e. even if we would pass buffers 2718 * here, the program still needs to call the bpf_lX_csum_replace() 2719 * helpers anyway. Plus, this way we keep also separation of 2720 * concerns, since f.e. bpf_skb_store_bytes() should only take 2721 * care of stores. 2722 * 2723 * Currently, additional options and extension header space are 2724 * not supported, but flags register is reserved so we can adapt 2725 * that. For offloads, we mark packet as dodgy, so that headers 2726 * need to be verified first. 2727 */ 2728 ret = bpf_skb_proto_xlat(skb, proto); 2729 bpf_compute_data_pointers(skb); 2730 return ret; 2731 } 2732 2733 static const struct bpf_func_proto bpf_skb_change_proto_proto = { 2734 .func = bpf_skb_change_proto, 2735 .gpl_only = false, 2736 .ret_type = RET_INTEGER, 2737 .arg1_type = ARG_PTR_TO_CTX, 2738 .arg2_type = ARG_ANYTHING, 2739 .arg3_type = ARG_ANYTHING, 2740 }; 2741 2742 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type) 2743 { 2744 /* We only allow a restricted subset to be changed for now. */ 2745 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) || 2746 !skb_pkt_type_ok(pkt_type))) 2747 return -EINVAL; 2748 2749 skb->pkt_type = pkt_type; 2750 return 0; 2751 } 2752 2753 static const struct bpf_func_proto bpf_skb_change_type_proto = { 2754 .func = bpf_skb_change_type, 2755 .gpl_only = false, 2756 .ret_type = RET_INTEGER, 2757 .arg1_type = ARG_PTR_TO_CTX, 2758 .arg2_type = ARG_ANYTHING, 2759 }; 2760 2761 static u32 bpf_skb_net_base_len(const struct sk_buff *skb) 2762 { 2763 switch (skb->protocol) { 2764 case htons(ETH_P_IP): 2765 return sizeof(struct iphdr); 2766 case htons(ETH_P_IPV6): 2767 return sizeof(struct ipv6hdr); 2768 default: 2769 return ~0U; 2770 } 2771 } 2772 2773 static int bpf_skb_net_grow(struct sk_buff *skb, u32 len_diff) 2774 { 2775 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb); 2776 int ret; 2777 2778 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */ 2779 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb))) 2780 return -ENOTSUPP; 2781 2782 ret = skb_cow(skb, len_diff); 2783 if (unlikely(ret < 0)) 2784 return ret; 2785 2786 ret = bpf_skb_net_hdr_push(skb, off, len_diff); 2787 if (unlikely(ret < 0)) 2788 return ret; 2789 2790 if (skb_is_gso(skb)) { 2791 struct skb_shared_info *shinfo = skb_shinfo(skb); 2792 2793 /* Due to header grow, MSS needs to be downgraded. */ 2794 skb_decrease_gso_size(shinfo, len_diff); 2795 /* Header must be checked, and gso_segs recomputed. */ 2796 shinfo->gso_type |= SKB_GSO_DODGY; 2797 shinfo->gso_segs = 0; 2798 } 2799 2800 return 0; 2801 } 2802 2803 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 len_diff) 2804 { 2805 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb); 2806 int ret; 2807 2808 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */ 2809 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb))) 2810 return -ENOTSUPP; 2811 2812 ret = skb_unclone(skb, GFP_ATOMIC); 2813 if (unlikely(ret < 0)) 2814 return ret; 2815 2816 ret = bpf_skb_net_hdr_pop(skb, off, len_diff); 2817 if (unlikely(ret < 0)) 2818 return ret; 2819 2820 if (skb_is_gso(skb)) { 2821 struct skb_shared_info *shinfo = skb_shinfo(skb); 2822 2823 /* Due to header shrink, MSS can be upgraded. */ 2824 skb_increase_gso_size(shinfo, len_diff); 2825 /* Header must be checked, and gso_segs recomputed. */ 2826 shinfo->gso_type |= SKB_GSO_DODGY; 2827 shinfo->gso_segs = 0; 2828 } 2829 2830 return 0; 2831 } 2832 2833 static u32 __bpf_skb_max_len(const struct sk_buff *skb) 2834 { 2835 return skb->dev ? skb->dev->mtu + skb->dev->hard_header_len : 2836 SKB_MAX_ALLOC; 2837 } 2838 2839 static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff) 2840 { 2841 bool trans_same = skb->transport_header == skb->network_header; 2842 u32 len_cur, len_diff_abs = abs(len_diff); 2843 u32 len_min = bpf_skb_net_base_len(skb); 2844 u32 len_max = __bpf_skb_max_len(skb); 2845 __be16 proto = skb->protocol; 2846 bool shrink = len_diff < 0; 2847 int ret; 2848 2849 if (unlikely(len_diff_abs > 0xfffU)) 2850 return -EFAULT; 2851 if (unlikely(proto != htons(ETH_P_IP) && 2852 proto != htons(ETH_P_IPV6))) 2853 return -ENOTSUPP; 2854 2855 len_cur = skb->len - skb_network_offset(skb); 2856 if (skb_transport_header_was_set(skb) && !trans_same) 2857 len_cur = skb_network_header_len(skb); 2858 if ((shrink && (len_diff_abs >= len_cur || 2859 len_cur - len_diff_abs < len_min)) || 2860 (!shrink && (skb->len + len_diff_abs > len_max && 2861 !skb_is_gso(skb)))) 2862 return -ENOTSUPP; 2863 2864 ret = shrink ? bpf_skb_net_shrink(skb, len_diff_abs) : 2865 bpf_skb_net_grow(skb, len_diff_abs); 2866 2867 bpf_compute_data_pointers(skb); 2868 return ret; 2869 } 2870 2871 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff, 2872 u32, mode, u64, flags) 2873 { 2874 if (unlikely(flags)) 2875 return -EINVAL; 2876 if (likely(mode == BPF_ADJ_ROOM_NET)) 2877 return bpf_skb_adjust_net(skb, len_diff); 2878 2879 return -ENOTSUPP; 2880 } 2881 2882 static const struct bpf_func_proto bpf_skb_adjust_room_proto = { 2883 .func = bpf_skb_adjust_room, 2884 .gpl_only = false, 2885 .ret_type = RET_INTEGER, 2886 .arg1_type = ARG_PTR_TO_CTX, 2887 .arg2_type = ARG_ANYTHING, 2888 .arg3_type = ARG_ANYTHING, 2889 .arg4_type = ARG_ANYTHING, 2890 }; 2891 2892 static u32 __bpf_skb_min_len(const struct sk_buff *skb) 2893 { 2894 u32 min_len = skb_network_offset(skb); 2895 2896 if (skb_transport_header_was_set(skb)) 2897 min_len = skb_transport_offset(skb); 2898 if (skb->ip_summed == CHECKSUM_PARTIAL) 2899 min_len = skb_checksum_start_offset(skb) + 2900 skb->csum_offset + sizeof(__sum16); 2901 return min_len; 2902 } 2903 2904 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len) 2905 { 2906 unsigned int old_len = skb->len; 2907 int ret; 2908 2909 ret = __skb_grow_rcsum(skb, new_len); 2910 if (!ret) 2911 memset(skb->data + old_len, 0, new_len - old_len); 2912 return ret; 2913 } 2914 2915 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len) 2916 { 2917 return __skb_trim_rcsum(skb, new_len); 2918 } 2919 2920 static inline int __bpf_skb_change_tail(struct sk_buff *skb, u32 new_len, 2921 u64 flags) 2922 { 2923 u32 max_len = __bpf_skb_max_len(skb); 2924 u32 min_len = __bpf_skb_min_len(skb); 2925 int ret; 2926 2927 if (unlikely(flags || new_len > max_len || new_len < min_len)) 2928 return -EINVAL; 2929 if (skb->encapsulation) 2930 return -ENOTSUPP; 2931 2932 /* The basic idea of this helper is that it's performing the 2933 * needed work to either grow or trim an skb, and eBPF program 2934 * rewrites the rest via helpers like bpf_skb_store_bytes(), 2935 * bpf_lX_csum_replace() and others rather than passing a raw 2936 * buffer here. This one is a slow path helper and intended 2937 * for replies with control messages. 2938 * 2939 * Like in bpf_skb_change_proto(), we want to keep this rather 2940 * minimal and without protocol specifics so that we are able 2941 * to separate concerns as in bpf_skb_store_bytes() should only 2942 * be the one responsible for writing buffers. 2943 * 2944 * It's really expected to be a slow path operation here for 2945 * control message replies, so we're implicitly linearizing, 2946 * uncloning and drop offloads from the skb by this. 2947 */ 2948 ret = __bpf_try_make_writable(skb, skb->len); 2949 if (!ret) { 2950 if (new_len > skb->len) 2951 ret = bpf_skb_grow_rcsum(skb, new_len); 2952 else if (new_len < skb->len) 2953 ret = bpf_skb_trim_rcsum(skb, new_len); 2954 if (!ret && skb_is_gso(skb)) 2955 skb_gso_reset(skb); 2956 } 2957 return ret; 2958 } 2959 2960 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len, 2961 u64, flags) 2962 { 2963 int ret = __bpf_skb_change_tail(skb, new_len, flags); 2964 2965 bpf_compute_data_pointers(skb); 2966 return ret; 2967 } 2968 2969 static const struct bpf_func_proto bpf_skb_change_tail_proto = { 2970 .func = bpf_skb_change_tail, 2971 .gpl_only = false, 2972 .ret_type = RET_INTEGER, 2973 .arg1_type = ARG_PTR_TO_CTX, 2974 .arg2_type = ARG_ANYTHING, 2975 .arg3_type = ARG_ANYTHING, 2976 }; 2977 2978 BPF_CALL_3(sk_skb_change_tail, struct sk_buff *, skb, u32, new_len, 2979 u64, flags) 2980 { 2981 int ret = __bpf_skb_change_tail(skb, new_len, flags); 2982 2983 bpf_compute_data_end_sk_skb(skb); 2984 return ret; 2985 } 2986 2987 static const struct bpf_func_proto sk_skb_change_tail_proto = { 2988 .func = sk_skb_change_tail, 2989 .gpl_only = false, 2990 .ret_type = RET_INTEGER, 2991 .arg1_type = ARG_PTR_TO_CTX, 2992 .arg2_type = ARG_ANYTHING, 2993 .arg3_type = ARG_ANYTHING, 2994 }; 2995 2996 static inline int __bpf_skb_change_head(struct sk_buff *skb, u32 head_room, 2997 u64 flags) 2998 { 2999 u32 max_len = __bpf_skb_max_len(skb); 3000 u32 new_len = skb->len + head_room; 3001 int ret; 3002 3003 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) || 3004 new_len < skb->len)) 3005 return -EINVAL; 3006 3007 ret = skb_cow(skb, head_room); 3008 if (likely(!ret)) { 3009 /* Idea for this helper is that we currently only 3010 * allow to expand on mac header. This means that 3011 * skb->protocol network header, etc, stay as is. 3012 * Compared to bpf_skb_change_tail(), we're more 3013 * flexible due to not needing to linearize or 3014 * reset GSO. Intention for this helper is to be 3015 * used by an L3 skb that needs to push mac header 3016 * for redirection into L2 device. 3017 */ 3018 __skb_push(skb, head_room); 3019 memset(skb->data, 0, head_room); 3020 skb_reset_mac_header(skb); 3021 } 3022 3023 return ret; 3024 } 3025 3026 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room, 3027 u64, flags) 3028 { 3029 int ret = __bpf_skb_change_head(skb, head_room, flags); 3030 3031 bpf_compute_data_pointers(skb); 3032 return ret; 3033 } 3034 3035 static const struct bpf_func_proto bpf_skb_change_head_proto = { 3036 .func = bpf_skb_change_head, 3037 .gpl_only = false, 3038 .ret_type = RET_INTEGER, 3039 .arg1_type = ARG_PTR_TO_CTX, 3040 .arg2_type = ARG_ANYTHING, 3041 .arg3_type = ARG_ANYTHING, 3042 }; 3043 3044 BPF_CALL_3(sk_skb_change_head, struct sk_buff *, skb, u32, head_room, 3045 u64, flags) 3046 { 3047 int ret = __bpf_skb_change_head(skb, head_room, flags); 3048 3049 bpf_compute_data_end_sk_skb(skb); 3050 return ret; 3051 } 3052 3053 static const struct bpf_func_proto sk_skb_change_head_proto = { 3054 .func = sk_skb_change_head, 3055 .gpl_only = false, 3056 .ret_type = RET_INTEGER, 3057 .arg1_type = ARG_PTR_TO_CTX, 3058 .arg2_type = ARG_ANYTHING, 3059 .arg3_type = ARG_ANYTHING, 3060 }; 3061 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp) 3062 { 3063 return xdp_data_meta_unsupported(xdp) ? 0 : 3064 xdp->data - xdp->data_meta; 3065 } 3066 3067 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset) 3068 { 3069 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame); 3070 unsigned long metalen = xdp_get_metalen(xdp); 3071 void *data_start = xdp_frame_end + metalen; 3072 void *data = xdp->data + offset; 3073 3074 if (unlikely(data < data_start || 3075 data > xdp->data_end - ETH_HLEN)) 3076 return -EINVAL; 3077 3078 if (metalen) 3079 memmove(xdp->data_meta + offset, 3080 xdp->data_meta, metalen); 3081 xdp->data_meta += offset; 3082 xdp->data = data; 3083 3084 return 0; 3085 } 3086 3087 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = { 3088 .func = bpf_xdp_adjust_head, 3089 .gpl_only = false, 3090 .ret_type = RET_INTEGER, 3091 .arg1_type = ARG_PTR_TO_CTX, 3092 .arg2_type = ARG_ANYTHING, 3093 }; 3094 3095 BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset) 3096 { 3097 void *data_end = xdp->data_end + offset; 3098 3099 /* only shrinking is allowed for now. */ 3100 if (unlikely(offset >= 0)) 3101 return -EINVAL; 3102 3103 if (unlikely(data_end < xdp->data + ETH_HLEN)) 3104 return -EINVAL; 3105 3106 xdp->data_end = data_end; 3107 3108 return 0; 3109 } 3110 3111 static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = { 3112 .func = bpf_xdp_adjust_tail, 3113 .gpl_only = false, 3114 .ret_type = RET_INTEGER, 3115 .arg1_type = ARG_PTR_TO_CTX, 3116 .arg2_type = ARG_ANYTHING, 3117 }; 3118 3119 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset) 3120 { 3121 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame); 3122 void *meta = xdp->data_meta + offset; 3123 unsigned long metalen = xdp->data - meta; 3124 3125 if (xdp_data_meta_unsupported(xdp)) 3126 return -ENOTSUPP; 3127 if (unlikely(meta < xdp_frame_end || 3128 meta > xdp->data)) 3129 return -EINVAL; 3130 if (unlikely((metalen & (sizeof(__u32) - 1)) || 3131 (metalen > 32))) 3132 return -EACCES; 3133 3134 xdp->data_meta = meta; 3135 3136 return 0; 3137 } 3138 3139 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = { 3140 .func = bpf_xdp_adjust_meta, 3141 .gpl_only = false, 3142 .ret_type = RET_INTEGER, 3143 .arg1_type = ARG_PTR_TO_CTX, 3144 .arg2_type = ARG_ANYTHING, 3145 }; 3146 3147 static int __bpf_tx_xdp(struct net_device *dev, 3148 struct bpf_map *map, 3149 struct xdp_buff *xdp, 3150 u32 index) 3151 { 3152 struct xdp_frame *xdpf; 3153 int err, sent; 3154 3155 if (!dev->netdev_ops->ndo_xdp_xmit) { 3156 return -EOPNOTSUPP; 3157 } 3158 3159 err = xdp_ok_fwd_dev(dev, xdp->data_end - xdp->data); 3160 if (unlikely(err)) 3161 return err; 3162 3163 xdpf = convert_to_xdp_frame(xdp); 3164 if (unlikely(!xdpf)) 3165 return -EOVERFLOW; 3166 3167 sent = dev->netdev_ops->ndo_xdp_xmit(dev, 1, &xdpf, XDP_XMIT_FLUSH); 3168 if (sent <= 0) 3169 return sent; 3170 return 0; 3171 } 3172 3173 static int __bpf_tx_xdp_map(struct net_device *dev_rx, void *fwd, 3174 struct bpf_map *map, 3175 struct xdp_buff *xdp, 3176 u32 index) 3177 { 3178 int err; 3179 3180 switch (map->map_type) { 3181 case BPF_MAP_TYPE_DEVMAP: { 3182 struct bpf_dtab_netdev *dst = fwd; 3183 3184 err = dev_map_enqueue(dst, xdp, dev_rx); 3185 if (err) 3186 return err; 3187 __dev_map_insert_ctx(map, index); 3188 break; 3189 } 3190 case BPF_MAP_TYPE_CPUMAP: { 3191 struct bpf_cpu_map_entry *rcpu = fwd; 3192 3193 err = cpu_map_enqueue(rcpu, xdp, dev_rx); 3194 if (err) 3195 return err; 3196 __cpu_map_insert_ctx(map, index); 3197 break; 3198 } 3199 case BPF_MAP_TYPE_XSKMAP: { 3200 struct xdp_sock *xs = fwd; 3201 3202 err = __xsk_map_redirect(map, xdp, xs); 3203 return err; 3204 } 3205 default: 3206 break; 3207 } 3208 return 0; 3209 } 3210 3211 void xdp_do_flush_map(void) 3212 { 3213 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 3214 struct bpf_map *map = ri->map_to_flush; 3215 3216 ri->map_to_flush = NULL; 3217 if (map) { 3218 switch (map->map_type) { 3219 case BPF_MAP_TYPE_DEVMAP: 3220 __dev_map_flush(map); 3221 break; 3222 case BPF_MAP_TYPE_CPUMAP: 3223 __cpu_map_flush(map); 3224 break; 3225 case BPF_MAP_TYPE_XSKMAP: 3226 __xsk_map_flush(map); 3227 break; 3228 default: 3229 break; 3230 } 3231 } 3232 } 3233 EXPORT_SYMBOL_GPL(xdp_do_flush_map); 3234 3235 static void *__xdp_map_lookup_elem(struct bpf_map *map, u32 index) 3236 { 3237 switch (map->map_type) { 3238 case BPF_MAP_TYPE_DEVMAP: 3239 return __dev_map_lookup_elem(map, index); 3240 case BPF_MAP_TYPE_CPUMAP: 3241 return __cpu_map_lookup_elem(map, index); 3242 case BPF_MAP_TYPE_XSKMAP: 3243 return __xsk_map_lookup_elem(map, index); 3244 default: 3245 return NULL; 3246 } 3247 } 3248 3249 void bpf_clear_redirect_map(struct bpf_map *map) 3250 { 3251 struct bpf_redirect_info *ri; 3252 int cpu; 3253 3254 for_each_possible_cpu(cpu) { 3255 ri = per_cpu_ptr(&bpf_redirect_info, cpu); 3256 /* Avoid polluting remote cacheline due to writes if 3257 * not needed. Once we pass this test, we need the 3258 * cmpxchg() to make sure it hasn't been changed in 3259 * the meantime by remote CPU. 3260 */ 3261 if (unlikely(READ_ONCE(ri->map) == map)) 3262 cmpxchg(&ri->map, map, NULL); 3263 } 3264 } 3265 3266 static int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp, 3267 struct bpf_prog *xdp_prog, struct bpf_map *map) 3268 { 3269 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 3270 u32 index = ri->ifindex; 3271 void *fwd = NULL; 3272 int err; 3273 3274 ri->ifindex = 0; 3275 WRITE_ONCE(ri->map, NULL); 3276 3277 fwd = __xdp_map_lookup_elem(map, index); 3278 if (!fwd) { 3279 err = -EINVAL; 3280 goto err; 3281 } 3282 if (ri->map_to_flush && ri->map_to_flush != map) 3283 xdp_do_flush_map(); 3284 3285 err = __bpf_tx_xdp_map(dev, fwd, map, xdp, index); 3286 if (unlikely(err)) 3287 goto err; 3288 3289 ri->map_to_flush = map; 3290 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index); 3291 return 0; 3292 err: 3293 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err); 3294 return err; 3295 } 3296 3297 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp, 3298 struct bpf_prog *xdp_prog) 3299 { 3300 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 3301 struct bpf_map *map = READ_ONCE(ri->map); 3302 struct net_device *fwd; 3303 u32 index = ri->ifindex; 3304 int err; 3305 3306 if (map) 3307 return xdp_do_redirect_map(dev, xdp, xdp_prog, map); 3308 3309 fwd = dev_get_by_index_rcu(dev_net(dev), index); 3310 ri->ifindex = 0; 3311 if (unlikely(!fwd)) { 3312 err = -EINVAL; 3313 goto err; 3314 } 3315 3316 err = __bpf_tx_xdp(fwd, NULL, xdp, 0); 3317 if (unlikely(err)) 3318 goto err; 3319 3320 _trace_xdp_redirect(dev, xdp_prog, index); 3321 return 0; 3322 err: 3323 _trace_xdp_redirect_err(dev, xdp_prog, index, err); 3324 return err; 3325 } 3326 EXPORT_SYMBOL_GPL(xdp_do_redirect); 3327 3328 static int xdp_do_generic_redirect_map(struct net_device *dev, 3329 struct sk_buff *skb, 3330 struct xdp_buff *xdp, 3331 struct bpf_prog *xdp_prog, 3332 struct bpf_map *map) 3333 { 3334 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 3335 u32 index = ri->ifindex; 3336 void *fwd = NULL; 3337 int err = 0; 3338 3339 ri->ifindex = 0; 3340 WRITE_ONCE(ri->map, NULL); 3341 3342 fwd = __xdp_map_lookup_elem(map, index); 3343 if (unlikely(!fwd)) { 3344 err = -EINVAL; 3345 goto err; 3346 } 3347 3348 if (map->map_type == BPF_MAP_TYPE_DEVMAP) { 3349 struct bpf_dtab_netdev *dst = fwd; 3350 3351 err = dev_map_generic_redirect(dst, skb, xdp_prog); 3352 if (unlikely(err)) 3353 goto err; 3354 } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { 3355 struct xdp_sock *xs = fwd; 3356 3357 err = xsk_generic_rcv(xs, xdp); 3358 if (err) 3359 goto err; 3360 consume_skb(skb); 3361 } else { 3362 /* TODO: Handle BPF_MAP_TYPE_CPUMAP */ 3363 err = -EBADRQC; 3364 goto err; 3365 } 3366 3367 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index); 3368 return 0; 3369 err: 3370 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err); 3371 return err; 3372 } 3373 3374 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb, 3375 struct xdp_buff *xdp, struct bpf_prog *xdp_prog) 3376 { 3377 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 3378 struct bpf_map *map = READ_ONCE(ri->map); 3379 u32 index = ri->ifindex; 3380 struct net_device *fwd; 3381 int err = 0; 3382 3383 if (map) 3384 return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog, 3385 map); 3386 ri->ifindex = 0; 3387 fwd = dev_get_by_index_rcu(dev_net(dev), index); 3388 if (unlikely(!fwd)) { 3389 err = -EINVAL; 3390 goto err; 3391 } 3392 3393 err = xdp_ok_fwd_dev(fwd, skb->len); 3394 if (unlikely(err)) 3395 goto err; 3396 3397 skb->dev = fwd; 3398 _trace_xdp_redirect(dev, xdp_prog, index); 3399 generic_xdp_tx(skb, xdp_prog); 3400 return 0; 3401 err: 3402 _trace_xdp_redirect_err(dev, xdp_prog, index, err); 3403 return err; 3404 } 3405 EXPORT_SYMBOL_GPL(xdp_do_generic_redirect); 3406 3407 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags) 3408 { 3409 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 3410 3411 if (unlikely(flags)) 3412 return XDP_ABORTED; 3413 3414 ri->ifindex = ifindex; 3415 ri->flags = flags; 3416 WRITE_ONCE(ri->map, NULL); 3417 3418 return XDP_REDIRECT; 3419 } 3420 3421 static const struct bpf_func_proto bpf_xdp_redirect_proto = { 3422 .func = bpf_xdp_redirect, 3423 .gpl_only = false, 3424 .ret_type = RET_INTEGER, 3425 .arg1_type = ARG_ANYTHING, 3426 .arg2_type = ARG_ANYTHING, 3427 }; 3428 3429 BPF_CALL_3(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex, 3430 u64, flags) 3431 { 3432 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 3433 3434 if (unlikely(flags)) 3435 return XDP_ABORTED; 3436 3437 ri->ifindex = ifindex; 3438 ri->flags = flags; 3439 WRITE_ONCE(ri->map, map); 3440 3441 return XDP_REDIRECT; 3442 } 3443 3444 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = { 3445 .func = bpf_xdp_redirect_map, 3446 .gpl_only = false, 3447 .ret_type = RET_INTEGER, 3448 .arg1_type = ARG_CONST_MAP_PTR, 3449 .arg2_type = ARG_ANYTHING, 3450 .arg3_type = ARG_ANYTHING, 3451 }; 3452 3453 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb, 3454 unsigned long off, unsigned long len) 3455 { 3456 void *ptr = skb_header_pointer(skb, off, len, dst_buff); 3457 3458 if (unlikely(!ptr)) 3459 return len; 3460 if (ptr != dst_buff) 3461 memcpy(dst_buff, ptr, len); 3462 3463 return 0; 3464 } 3465 3466 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map, 3467 u64, flags, void *, meta, u64, meta_size) 3468 { 3469 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32; 3470 3471 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK))) 3472 return -EINVAL; 3473 if (unlikely(skb_size > skb->len)) 3474 return -EFAULT; 3475 3476 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size, 3477 bpf_skb_copy); 3478 } 3479 3480 static const struct bpf_func_proto bpf_skb_event_output_proto = { 3481 .func = bpf_skb_event_output, 3482 .gpl_only = true, 3483 .ret_type = RET_INTEGER, 3484 .arg1_type = ARG_PTR_TO_CTX, 3485 .arg2_type = ARG_CONST_MAP_PTR, 3486 .arg3_type = ARG_ANYTHING, 3487 .arg4_type = ARG_PTR_TO_MEM, 3488 .arg5_type = ARG_CONST_SIZE_OR_ZERO, 3489 }; 3490 3491 static unsigned short bpf_tunnel_key_af(u64 flags) 3492 { 3493 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET; 3494 } 3495 3496 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to, 3497 u32, size, u64, flags) 3498 { 3499 const struct ip_tunnel_info *info = skb_tunnel_info(skb); 3500 u8 compat[sizeof(struct bpf_tunnel_key)]; 3501 void *to_orig = to; 3502 int err; 3503 3504 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) { 3505 err = -EINVAL; 3506 goto err_clear; 3507 } 3508 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) { 3509 err = -EPROTO; 3510 goto err_clear; 3511 } 3512 if (unlikely(size != sizeof(struct bpf_tunnel_key))) { 3513 err = -EINVAL; 3514 switch (size) { 3515 case offsetof(struct bpf_tunnel_key, tunnel_label): 3516 case offsetof(struct bpf_tunnel_key, tunnel_ext): 3517 goto set_compat; 3518 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]): 3519 /* Fixup deprecated structure layouts here, so we have 3520 * a common path later on. 3521 */ 3522 if (ip_tunnel_info_af(info) != AF_INET) 3523 goto err_clear; 3524 set_compat: 3525 to = (struct bpf_tunnel_key *)compat; 3526 break; 3527 default: 3528 goto err_clear; 3529 } 3530 } 3531 3532 to->tunnel_id = be64_to_cpu(info->key.tun_id); 3533 to->tunnel_tos = info->key.tos; 3534 to->tunnel_ttl = info->key.ttl; 3535 to->tunnel_ext = 0; 3536 3537 if (flags & BPF_F_TUNINFO_IPV6) { 3538 memcpy(to->remote_ipv6, &info->key.u.ipv6.src, 3539 sizeof(to->remote_ipv6)); 3540 to->tunnel_label = be32_to_cpu(info->key.label); 3541 } else { 3542 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src); 3543 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3); 3544 to->tunnel_label = 0; 3545 } 3546 3547 if (unlikely(size != sizeof(struct bpf_tunnel_key))) 3548 memcpy(to_orig, to, size); 3549 3550 return 0; 3551 err_clear: 3552 memset(to_orig, 0, size); 3553 return err; 3554 } 3555 3556 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = { 3557 .func = bpf_skb_get_tunnel_key, 3558 .gpl_only = false, 3559 .ret_type = RET_INTEGER, 3560 .arg1_type = ARG_PTR_TO_CTX, 3561 .arg2_type = ARG_PTR_TO_UNINIT_MEM, 3562 .arg3_type = ARG_CONST_SIZE, 3563 .arg4_type = ARG_ANYTHING, 3564 }; 3565 3566 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size) 3567 { 3568 const struct ip_tunnel_info *info = skb_tunnel_info(skb); 3569 int err; 3570 3571 if (unlikely(!info || 3572 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) { 3573 err = -ENOENT; 3574 goto err_clear; 3575 } 3576 if (unlikely(size < info->options_len)) { 3577 err = -ENOMEM; 3578 goto err_clear; 3579 } 3580 3581 ip_tunnel_info_opts_get(to, info); 3582 if (size > info->options_len) 3583 memset(to + info->options_len, 0, size - info->options_len); 3584 3585 return info->options_len; 3586 err_clear: 3587 memset(to, 0, size); 3588 return err; 3589 } 3590 3591 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = { 3592 .func = bpf_skb_get_tunnel_opt, 3593 .gpl_only = false, 3594 .ret_type = RET_INTEGER, 3595 .arg1_type = ARG_PTR_TO_CTX, 3596 .arg2_type = ARG_PTR_TO_UNINIT_MEM, 3597 .arg3_type = ARG_CONST_SIZE, 3598 }; 3599 3600 static struct metadata_dst __percpu *md_dst; 3601 3602 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb, 3603 const struct bpf_tunnel_key *, from, u32, size, u64, flags) 3604 { 3605 struct metadata_dst *md = this_cpu_ptr(md_dst); 3606 u8 compat[sizeof(struct bpf_tunnel_key)]; 3607 struct ip_tunnel_info *info; 3608 3609 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX | 3610 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER))) 3611 return -EINVAL; 3612 if (unlikely(size != sizeof(struct bpf_tunnel_key))) { 3613 switch (size) { 3614 case offsetof(struct bpf_tunnel_key, tunnel_label): 3615 case offsetof(struct bpf_tunnel_key, tunnel_ext): 3616 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]): 3617 /* Fixup deprecated structure layouts here, so we have 3618 * a common path later on. 3619 */ 3620 memcpy(compat, from, size); 3621 memset(compat + size, 0, sizeof(compat) - size); 3622 from = (const struct bpf_tunnel_key *) compat; 3623 break; 3624 default: 3625 return -EINVAL; 3626 } 3627 } 3628 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) || 3629 from->tunnel_ext)) 3630 return -EINVAL; 3631 3632 skb_dst_drop(skb); 3633 dst_hold((struct dst_entry *) md); 3634 skb_dst_set(skb, (struct dst_entry *) md); 3635 3636 info = &md->u.tun_info; 3637 memset(info, 0, sizeof(*info)); 3638 info->mode = IP_TUNNEL_INFO_TX; 3639 3640 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE; 3641 if (flags & BPF_F_DONT_FRAGMENT) 3642 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT; 3643 if (flags & BPF_F_ZERO_CSUM_TX) 3644 info->key.tun_flags &= ~TUNNEL_CSUM; 3645 if (flags & BPF_F_SEQ_NUMBER) 3646 info->key.tun_flags |= TUNNEL_SEQ; 3647 3648 info->key.tun_id = cpu_to_be64(from->tunnel_id); 3649 info->key.tos = from->tunnel_tos; 3650 info->key.ttl = from->tunnel_ttl; 3651 3652 if (flags & BPF_F_TUNINFO_IPV6) { 3653 info->mode |= IP_TUNNEL_INFO_IPV6; 3654 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6, 3655 sizeof(from->remote_ipv6)); 3656 info->key.label = cpu_to_be32(from->tunnel_label) & 3657 IPV6_FLOWLABEL_MASK; 3658 } else { 3659 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4); 3660 } 3661 3662 return 0; 3663 } 3664 3665 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = { 3666 .func = bpf_skb_set_tunnel_key, 3667 .gpl_only = false, 3668 .ret_type = RET_INTEGER, 3669 .arg1_type = ARG_PTR_TO_CTX, 3670 .arg2_type = ARG_PTR_TO_MEM, 3671 .arg3_type = ARG_CONST_SIZE, 3672 .arg4_type = ARG_ANYTHING, 3673 }; 3674 3675 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb, 3676 const u8 *, from, u32, size) 3677 { 3678 struct ip_tunnel_info *info = skb_tunnel_info(skb); 3679 const struct metadata_dst *md = this_cpu_ptr(md_dst); 3680 3681 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1)))) 3682 return -EINVAL; 3683 if (unlikely(size > IP_TUNNEL_OPTS_MAX)) 3684 return -ENOMEM; 3685 3686 ip_tunnel_info_opts_set(info, from, size, TUNNEL_OPTIONS_PRESENT); 3687 3688 return 0; 3689 } 3690 3691 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = { 3692 .func = bpf_skb_set_tunnel_opt, 3693 .gpl_only = false, 3694 .ret_type = RET_INTEGER, 3695 .arg1_type = ARG_PTR_TO_CTX, 3696 .arg2_type = ARG_PTR_TO_MEM, 3697 .arg3_type = ARG_CONST_SIZE, 3698 }; 3699 3700 static const struct bpf_func_proto * 3701 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which) 3702 { 3703 if (!md_dst) { 3704 struct metadata_dst __percpu *tmp; 3705 3706 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX, 3707 METADATA_IP_TUNNEL, 3708 GFP_KERNEL); 3709 if (!tmp) 3710 return NULL; 3711 if (cmpxchg(&md_dst, NULL, tmp)) 3712 metadata_dst_free_percpu(tmp); 3713 } 3714 3715 switch (which) { 3716 case BPF_FUNC_skb_set_tunnel_key: 3717 return &bpf_skb_set_tunnel_key_proto; 3718 case BPF_FUNC_skb_set_tunnel_opt: 3719 return &bpf_skb_set_tunnel_opt_proto; 3720 default: 3721 return NULL; 3722 } 3723 } 3724 3725 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map, 3726 u32, idx) 3727 { 3728 struct bpf_array *array = container_of(map, struct bpf_array, map); 3729 struct cgroup *cgrp; 3730 struct sock *sk; 3731 3732 sk = skb_to_full_sk(skb); 3733 if (!sk || !sk_fullsock(sk)) 3734 return -ENOENT; 3735 if (unlikely(idx >= array->map.max_entries)) 3736 return -E2BIG; 3737 3738 cgrp = READ_ONCE(array->ptrs[idx]); 3739 if (unlikely(!cgrp)) 3740 return -EAGAIN; 3741 3742 return sk_under_cgroup_hierarchy(sk, cgrp); 3743 } 3744 3745 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = { 3746 .func = bpf_skb_under_cgroup, 3747 .gpl_only = false, 3748 .ret_type = RET_INTEGER, 3749 .arg1_type = ARG_PTR_TO_CTX, 3750 .arg2_type = ARG_CONST_MAP_PTR, 3751 .arg3_type = ARG_ANYTHING, 3752 }; 3753 3754 #ifdef CONFIG_SOCK_CGROUP_DATA 3755 BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb) 3756 { 3757 struct sock *sk = skb_to_full_sk(skb); 3758 struct cgroup *cgrp; 3759 3760 if (!sk || !sk_fullsock(sk)) 3761 return 0; 3762 3763 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); 3764 return cgrp->kn->id.id; 3765 } 3766 3767 static const struct bpf_func_proto bpf_skb_cgroup_id_proto = { 3768 .func = bpf_skb_cgroup_id, 3769 .gpl_only = false, 3770 .ret_type = RET_INTEGER, 3771 .arg1_type = ARG_PTR_TO_CTX, 3772 }; 3773 3774 BPF_CALL_2(bpf_skb_ancestor_cgroup_id, const struct sk_buff *, skb, int, 3775 ancestor_level) 3776 { 3777 struct sock *sk = skb_to_full_sk(skb); 3778 struct cgroup *ancestor; 3779 struct cgroup *cgrp; 3780 3781 if (!sk || !sk_fullsock(sk)) 3782 return 0; 3783 3784 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); 3785 ancestor = cgroup_ancestor(cgrp, ancestor_level); 3786 if (!ancestor) 3787 return 0; 3788 3789 return ancestor->kn->id.id; 3790 } 3791 3792 static const struct bpf_func_proto bpf_skb_ancestor_cgroup_id_proto = { 3793 .func = bpf_skb_ancestor_cgroup_id, 3794 .gpl_only = false, 3795 .ret_type = RET_INTEGER, 3796 .arg1_type = ARG_PTR_TO_CTX, 3797 .arg2_type = ARG_ANYTHING, 3798 }; 3799 #endif 3800 3801 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff, 3802 unsigned long off, unsigned long len) 3803 { 3804 memcpy(dst_buff, src_buff + off, len); 3805 return 0; 3806 } 3807 3808 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map, 3809 u64, flags, void *, meta, u64, meta_size) 3810 { 3811 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32; 3812 3813 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK))) 3814 return -EINVAL; 3815 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data))) 3816 return -EFAULT; 3817 3818 return bpf_event_output(map, flags, meta, meta_size, xdp->data, 3819 xdp_size, bpf_xdp_copy); 3820 } 3821 3822 static const struct bpf_func_proto bpf_xdp_event_output_proto = { 3823 .func = bpf_xdp_event_output, 3824 .gpl_only = true, 3825 .ret_type = RET_INTEGER, 3826 .arg1_type = ARG_PTR_TO_CTX, 3827 .arg2_type = ARG_CONST_MAP_PTR, 3828 .arg3_type = ARG_ANYTHING, 3829 .arg4_type = ARG_PTR_TO_MEM, 3830 .arg5_type = ARG_CONST_SIZE_OR_ZERO, 3831 }; 3832 3833 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb) 3834 { 3835 return skb->sk ? sock_gen_cookie(skb->sk) : 0; 3836 } 3837 3838 static const struct bpf_func_proto bpf_get_socket_cookie_proto = { 3839 .func = bpf_get_socket_cookie, 3840 .gpl_only = false, 3841 .ret_type = RET_INTEGER, 3842 .arg1_type = ARG_PTR_TO_CTX, 3843 }; 3844 3845 BPF_CALL_1(bpf_get_socket_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx) 3846 { 3847 return sock_gen_cookie(ctx->sk); 3848 } 3849 3850 static const struct bpf_func_proto bpf_get_socket_cookie_sock_addr_proto = { 3851 .func = bpf_get_socket_cookie_sock_addr, 3852 .gpl_only = false, 3853 .ret_type = RET_INTEGER, 3854 .arg1_type = ARG_PTR_TO_CTX, 3855 }; 3856 3857 BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx) 3858 { 3859 return sock_gen_cookie(ctx->sk); 3860 } 3861 3862 static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = { 3863 .func = bpf_get_socket_cookie_sock_ops, 3864 .gpl_only = false, 3865 .ret_type = RET_INTEGER, 3866 .arg1_type = ARG_PTR_TO_CTX, 3867 }; 3868 3869 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb) 3870 { 3871 struct sock *sk = sk_to_full_sk(skb->sk); 3872 kuid_t kuid; 3873 3874 if (!sk || !sk_fullsock(sk)) 3875 return overflowuid; 3876 kuid = sock_net_uid(sock_net(sk), sk); 3877 return from_kuid_munged(sock_net(sk)->user_ns, kuid); 3878 } 3879 3880 static const struct bpf_func_proto bpf_get_socket_uid_proto = { 3881 .func = bpf_get_socket_uid, 3882 .gpl_only = false, 3883 .ret_type = RET_INTEGER, 3884 .arg1_type = ARG_PTR_TO_CTX, 3885 }; 3886 3887 BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock, 3888 int, level, int, optname, char *, optval, int, optlen) 3889 { 3890 struct sock *sk = bpf_sock->sk; 3891 int ret = 0; 3892 int val; 3893 3894 if (!sk_fullsock(sk)) 3895 return -EINVAL; 3896 3897 if (level == SOL_SOCKET) { 3898 if (optlen != sizeof(int)) 3899 return -EINVAL; 3900 val = *((int *)optval); 3901 3902 /* Only some socketops are supported */ 3903 switch (optname) { 3904 case SO_RCVBUF: 3905 sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 3906 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF); 3907 break; 3908 case SO_SNDBUF: 3909 sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 3910 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF); 3911 break; 3912 case SO_MAX_PACING_RATE: 3913 sk->sk_max_pacing_rate = val; 3914 sk->sk_pacing_rate = min(sk->sk_pacing_rate, 3915 sk->sk_max_pacing_rate); 3916 break; 3917 case SO_PRIORITY: 3918 sk->sk_priority = val; 3919 break; 3920 case SO_RCVLOWAT: 3921 if (val < 0) 3922 val = INT_MAX; 3923 sk->sk_rcvlowat = val ? : 1; 3924 break; 3925 case SO_MARK: 3926 sk->sk_mark = val; 3927 break; 3928 default: 3929 ret = -EINVAL; 3930 } 3931 #ifdef CONFIG_INET 3932 } else if (level == SOL_IP) { 3933 if (optlen != sizeof(int) || sk->sk_family != AF_INET) 3934 return -EINVAL; 3935 3936 val = *((int *)optval); 3937 /* Only some options are supported */ 3938 switch (optname) { 3939 case IP_TOS: 3940 if (val < -1 || val > 0xff) { 3941 ret = -EINVAL; 3942 } else { 3943 struct inet_sock *inet = inet_sk(sk); 3944 3945 if (val == -1) 3946 val = 0; 3947 inet->tos = val; 3948 } 3949 break; 3950 default: 3951 ret = -EINVAL; 3952 } 3953 #if IS_ENABLED(CONFIG_IPV6) 3954 } else if (level == SOL_IPV6) { 3955 if (optlen != sizeof(int) || sk->sk_family != AF_INET6) 3956 return -EINVAL; 3957 3958 val = *((int *)optval); 3959 /* Only some options are supported */ 3960 switch (optname) { 3961 case IPV6_TCLASS: 3962 if (val < -1 || val > 0xff) { 3963 ret = -EINVAL; 3964 } else { 3965 struct ipv6_pinfo *np = inet6_sk(sk); 3966 3967 if (val == -1) 3968 val = 0; 3969 np->tclass = val; 3970 } 3971 break; 3972 default: 3973 ret = -EINVAL; 3974 } 3975 #endif 3976 } else if (level == SOL_TCP && 3977 sk->sk_prot->setsockopt == tcp_setsockopt) { 3978 if (optname == TCP_CONGESTION) { 3979 char name[TCP_CA_NAME_MAX]; 3980 bool reinit = bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN; 3981 3982 strncpy(name, optval, min_t(long, optlen, 3983 TCP_CA_NAME_MAX-1)); 3984 name[TCP_CA_NAME_MAX-1] = 0; 3985 ret = tcp_set_congestion_control(sk, name, false, 3986 reinit); 3987 } else { 3988 struct tcp_sock *tp = tcp_sk(sk); 3989 3990 if (optlen != sizeof(int)) 3991 return -EINVAL; 3992 3993 val = *((int *)optval); 3994 /* Only some options are supported */ 3995 switch (optname) { 3996 case TCP_BPF_IW: 3997 if (val <= 0 || tp->data_segs_out > 0) 3998 ret = -EINVAL; 3999 else 4000 tp->snd_cwnd = val; 4001 break; 4002 case TCP_BPF_SNDCWND_CLAMP: 4003 if (val <= 0) { 4004 ret = -EINVAL; 4005 } else { 4006 tp->snd_cwnd_clamp = val; 4007 tp->snd_ssthresh = val; 4008 } 4009 break; 4010 default: 4011 ret = -EINVAL; 4012 } 4013 } 4014 #endif 4015 } else { 4016 ret = -EINVAL; 4017 } 4018 return ret; 4019 } 4020 4021 static const struct bpf_func_proto bpf_setsockopt_proto = { 4022 .func = bpf_setsockopt, 4023 .gpl_only = false, 4024 .ret_type = RET_INTEGER, 4025 .arg1_type = ARG_PTR_TO_CTX, 4026 .arg2_type = ARG_ANYTHING, 4027 .arg3_type = ARG_ANYTHING, 4028 .arg4_type = ARG_PTR_TO_MEM, 4029 .arg5_type = ARG_CONST_SIZE, 4030 }; 4031 4032 BPF_CALL_5(bpf_getsockopt, struct bpf_sock_ops_kern *, bpf_sock, 4033 int, level, int, optname, char *, optval, int, optlen) 4034 { 4035 struct sock *sk = bpf_sock->sk; 4036 4037 if (!sk_fullsock(sk)) 4038 goto err_clear; 4039 4040 #ifdef CONFIG_INET 4041 if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) { 4042 if (optname == TCP_CONGESTION) { 4043 struct inet_connection_sock *icsk = inet_csk(sk); 4044 4045 if (!icsk->icsk_ca_ops || optlen <= 1) 4046 goto err_clear; 4047 strncpy(optval, icsk->icsk_ca_ops->name, optlen); 4048 optval[optlen - 1] = 0; 4049 } else { 4050 goto err_clear; 4051 } 4052 } else if (level == SOL_IP) { 4053 struct inet_sock *inet = inet_sk(sk); 4054 4055 if (optlen != sizeof(int) || sk->sk_family != AF_INET) 4056 goto err_clear; 4057 4058 /* Only some options are supported */ 4059 switch (optname) { 4060 case IP_TOS: 4061 *((int *)optval) = (int)inet->tos; 4062 break; 4063 default: 4064 goto err_clear; 4065 } 4066 #if IS_ENABLED(CONFIG_IPV6) 4067 } else if (level == SOL_IPV6) { 4068 struct ipv6_pinfo *np = inet6_sk(sk); 4069 4070 if (optlen != sizeof(int) || sk->sk_family != AF_INET6) 4071 goto err_clear; 4072 4073 /* Only some options are supported */ 4074 switch (optname) { 4075 case IPV6_TCLASS: 4076 *((int *)optval) = (int)np->tclass; 4077 break; 4078 default: 4079 goto err_clear; 4080 } 4081 #endif 4082 } else { 4083 goto err_clear; 4084 } 4085 return 0; 4086 #endif 4087 err_clear: 4088 memset(optval, 0, optlen); 4089 return -EINVAL; 4090 } 4091 4092 static const struct bpf_func_proto bpf_getsockopt_proto = { 4093 .func = bpf_getsockopt, 4094 .gpl_only = false, 4095 .ret_type = RET_INTEGER, 4096 .arg1_type = ARG_PTR_TO_CTX, 4097 .arg2_type = ARG_ANYTHING, 4098 .arg3_type = ARG_ANYTHING, 4099 .arg4_type = ARG_PTR_TO_UNINIT_MEM, 4100 .arg5_type = ARG_CONST_SIZE, 4101 }; 4102 4103 BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock, 4104 int, argval) 4105 { 4106 struct sock *sk = bpf_sock->sk; 4107 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS; 4108 4109 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk)) 4110 return -EINVAL; 4111 4112 if (val) 4113 tcp_sk(sk)->bpf_sock_ops_cb_flags = val; 4114 4115 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS); 4116 } 4117 4118 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = { 4119 .func = bpf_sock_ops_cb_flags_set, 4120 .gpl_only = false, 4121 .ret_type = RET_INTEGER, 4122 .arg1_type = ARG_PTR_TO_CTX, 4123 .arg2_type = ARG_ANYTHING, 4124 }; 4125 4126 const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly; 4127 EXPORT_SYMBOL_GPL(ipv6_bpf_stub); 4128 4129 BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr, 4130 int, addr_len) 4131 { 4132 #ifdef CONFIG_INET 4133 struct sock *sk = ctx->sk; 4134 int err; 4135 4136 /* Binding to port can be expensive so it's prohibited in the helper. 4137 * Only binding to IP is supported. 4138 */ 4139 err = -EINVAL; 4140 if (addr->sa_family == AF_INET) { 4141 if (addr_len < sizeof(struct sockaddr_in)) 4142 return err; 4143 if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 4144 return err; 4145 return __inet_bind(sk, addr, addr_len, true, false); 4146 #if IS_ENABLED(CONFIG_IPV6) 4147 } else if (addr->sa_family == AF_INET6) { 4148 if (addr_len < SIN6_LEN_RFC2133) 4149 return err; 4150 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 4151 return err; 4152 /* ipv6_bpf_stub cannot be NULL, since it's called from 4153 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded 4154 */ 4155 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, true, false); 4156 #endif /* CONFIG_IPV6 */ 4157 } 4158 #endif /* CONFIG_INET */ 4159 4160 return -EAFNOSUPPORT; 4161 } 4162 4163 static const struct bpf_func_proto bpf_bind_proto = { 4164 .func = bpf_bind, 4165 .gpl_only = false, 4166 .ret_type = RET_INTEGER, 4167 .arg1_type = ARG_PTR_TO_CTX, 4168 .arg2_type = ARG_PTR_TO_MEM, 4169 .arg3_type = ARG_CONST_SIZE, 4170 }; 4171 4172 #ifdef CONFIG_XFRM 4173 BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index, 4174 struct bpf_xfrm_state *, to, u32, size, u64, flags) 4175 { 4176 const struct sec_path *sp = skb_sec_path(skb); 4177 const struct xfrm_state *x; 4178 4179 if (!sp || unlikely(index >= sp->len || flags)) 4180 goto err_clear; 4181 4182 x = sp->xvec[index]; 4183 4184 if (unlikely(size != sizeof(struct bpf_xfrm_state))) 4185 goto err_clear; 4186 4187 to->reqid = x->props.reqid; 4188 to->spi = x->id.spi; 4189 to->family = x->props.family; 4190 to->ext = 0; 4191 4192 if (to->family == AF_INET6) { 4193 memcpy(to->remote_ipv6, x->props.saddr.a6, 4194 sizeof(to->remote_ipv6)); 4195 } else { 4196 to->remote_ipv4 = x->props.saddr.a4; 4197 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3); 4198 } 4199 4200 return 0; 4201 err_clear: 4202 memset(to, 0, size); 4203 return -EINVAL; 4204 } 4205 4206 static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = { 4207 .func = bpf_skb_get_xfrm_state, 4208 .gpl_only = false, 4209 .ret_type = RET_INTEGER, 4210 .arg1_type = ARG_PTR_TO_CTX, 4211 .arg2_type = ARG_ANYTHING, 4212 .arg3_type = ARG_PTR_TO_UNINIT_MEM, 4213 .arg4_type = ARG_CONST_SIZE, 4214 .arg5_type = ARG_ANYTHING, 4215 }; 4216 #endif 4217 4218 #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6) 4219 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params, 4220 const struct neighbour *neigh, 4221 const struct net_device *dev) 4222 { 4223 memcpy(params->dmac, neigh->ha, ETH_ALEN); 4224 memcpy(params->smac, dev->dev_addr, ETH_ALEN); 4225 params->h_vlan_TCI = 0; 4226 params->h_vlan_proto = 0; 4227 params->ifindex = dev->ifindex; 4228 4229 return 0; 4230 } 4231 #endif 4232 4233 #if IS_ENABLED(CONFIG_INET) 4234 static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params, 4235 u32 flags, bool check_mtu) 4236 { 4237 struct in_device *in_dev; 4238 struct neighbour *neigh; 4239 struct net_device *dev; 4240 struct fib_result res; 4241 struct fib_nh *nh; 4242 struct flowi4 fl4; 4243 int err; 4244 u32 mtu; 4245 4246 dev = dev_get_by_index_rcu(net, params->ifindex); 4247 if (unlikely(!dev)) 4248 return -ENODEV; 4249 4250 /* verify forwarding is enabled on this interface */ 4251 in_dev = __in_dev_get_rcu(dev); 4252 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev))) 4253 return BPF_FIB_LKUP_RET_FWD_DISABLED; 4254 4255 if (flags & BPF_FIB_LOOKUP_OUTPUT) { 4256 fl4.flowi4_iif = 1; 4257 fl4.flowi4_oif = params->ifindex; 4258 } else { 4259 fl4.flowi4_iif = params->ifindex; 4260 fl4.flowi4_oif = 0; 4261 } 4262 fl4.flowi4_tos = params->tos & IPTOS_RT_MASK; 4263 fl4.flowi4_scope = RT_SCOPE_UNIVERSE; 4264 fl4.flowi4_flags = 0; 4265 4266 fl4.flowi4_proto = params->l4_protocol; 4267 fl4.daddr = params->ipv4_dst; 4268 fl4.saddr = params->ipv4_src; 4269 fl4.fl4_sport = params->sport; 4270 fl4.fl4_dport = params->dport; 4271 4272 if (flags & BPF_FIB_LOOKUP_DIRECT) { 4273 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN; 4274 struct fib_table *tb; 4275 4276 tb = fib_get_table(net, tbid); 4277 if (unlikely(!tb)) 4278 return BPF_FIB_LKUP_RET_NOT_FWDED; 4279 4280 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF); 4281 } else { 4282 fl4.flowi4_mark = 0; 4283 fl4.flowi4_secid = 0; 4284 fl4.flowi4_tun_key.tun_id = 0; 4285 fl4.flowi4_uid = sock_net_uid(net, NULL); 4286 4287 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF); 4288 } 4289 4290 if (err) { 4291 /* map fib lookup errors to RTN_ type */ 4292 if (err == -EINVAL) 4293 return BPF_FIB_LKUP_RET_BLACKHOLE; 4294 if (err == -EHOSTUNREACH) 4295 return BPF_FIB_LKUP_RET_UNREACHABLE; 4296 if (err == -EACCES) 4297 return BPF_FIB_LKUP_RET_PROHIBIT; 4298 4299 return BPF_FIB_LKUP_RET_NOT_FWDED; 4300 } 4301 4302 if (res.type != RTN_UNICAST) 4303 return BPF_FIB_LKUP_RET_NOT_FWDED; 4304 4305 if (res.fi->fib_nhs > 1) 4306 fib_select_path(net, &res, &fl4, NULL); 4307 4308 if (check_mtu) { 4309 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst); 4310 if (params->tot_len > mtu) 4311 return BPF_FIB_LKUP_RET_FRAG_NEEDED; 4312 } 4313 4314 nh = &res.fi->fib_nh[res.nh_sel]; 4315 4316 /* do not handle lwt encaps right now */ 4317 if (nh->nh_lwtstate) 4318 return BPF_FIB_LKUP_RET_UNSUPP_LWT; 4319 4320 dev = nh->nh_dev; 4321 if (nh->nh_gw) 4322 params->ipv4_dst = nh->nh_gw; 4323 4324 params->rt_metric = res.fi->fib_priority; 4325 4326 /* xdp and cls_bpf programs are run in RCU-bh so 4327 * rcu_read_lock_bh is not needed here 4328 */ 4329 neigh = __ipv4_neigh_lookup_noref(dev, (__force u32)params->ipv4_dst); 4330 if (!neigh) 4331 return BPF_FIB_LKUP_RET_NO_NEIGH; 4332 4333 return bpf_fib_set_fwd_params(params, neigh, dev); 4334 } 4335 #endif 4336 4337 #if IS_ENABLED(CONFIG_IPV6) 4338 static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params, 4339 u32 flags, bool check_mtu) 4340 { 4341 struct in6_addr *src = (struct in6_addr *) params->ipv6_src; 4342 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst; 4343 struct neighbour *neigh; 4344 struct net_device *dev; 4345 struct inet6_dev *idev; 4346 struct fib6_info *f6i; 4347 struct flowi6 fl6; 4348 int strict = 0; 4349 int oif; 4350 u32 mtu; 4351 4352 /* link local addresses are never forwarded */ 4353 if (rt6_need_strict(dst) || rt6_need_strict(src)) 4354 return BPF_FIB_LKUP_RET_NOT_FWDED; 4355 4356 dev = dev_get_by_index_rcu(net, params->ifindex); 4357 if (unlikely(!dev)) 4358 return -ENODEV; 4359 4360 idev = __in6_dev_get_safely(dev); 4361 if (unlikely(!idev || !net->ipv6.devconf_all->forwarding)) 4362 return BPF_FIB_LKUP_RET_FWD_DISABLED; 4363 4364 if (flags & BPF_FIB_LOOKUP_OUTPUT) { 4365 fl6.flowi6_iif = 1; 4366 oif = fl6.flowi6_oif = params->ifindex; 4367 } else { 4368 oif = fl6.flowi6_iif = params->ifindex; 4369 fl6.flowi6_oif = 0; 4370 strict = RT6_LOOKUP_F_HAS_SADDR; 4371 } 4372 fl6.flowlabel = params->flowinfo; 4373 fl6.flowi6_scope = 0; 4374 fl6.flowi6_flags = 0; 4375 fl6.mp_hash = 0; 4376 4377 fl6.flowi6_proto = params->l4_protocol; 4378 fl6.daddr = *dst; 4379 fl6.saddr = *src; 4380 fl6.fl6_sport = params->sport; 4381 fl6.fl6_dport = params->dport; 4382 4383 if (flags & BPF_FIB_LOOKUP_DIRECT) { 4384 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN; 4385 struct fib6_table *tb; 4386 4387 tb = ipv6_stub->fib6_get_table(net, tbid); 4388 if (unlikely(!tb)) 4389 return BPF_FIB_LKUP_RET_NOT_FWDED; 4390 4391 f6i = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, strict); 4392 } else { 4393 fl6.flowi6_mark = 0; 4394 fl6.flowi6_secid = 0; 4395 fl6.flowi6_tun_key.tun_id = 0; 4396 fl6.flowi6_uid = sock_net_uid(net, NULL); 4397 4398 f6i = ipv6_stub->fib6_lookup(net, oif, &fl6, strict); 4399 } 4400 4401 if (unlikely(IS_ERR_OR_NULL(f6i) || f6i == net->ipv6.fib6_null_entry)) 4402 return BPF_FIB_LKUP_RET_NOT_FWDED; 4403 4404 if (unlikely(f6i->fib6_flags & RTF_REJECT)) { 4405 switch (f6i->fib6_type) { 4406 case RTN_BLACKHOLE: 4407 return BPF_FIB_LKUP_RET_BLACKHOLE; 4408 case RTN_UNREACHABLE: 4409 return BPF_FIB_LKUP_RET_UNREACHABLE; 4410 case RTN_PROHIBIT: 4411 return BPF_FIB_LKUP_RET_PROHIBIT; 4412 default: 4413 return BPF_FIB_LKUP_RET_NOT_FWDED; 4414 } 4415 } 4416 4417 if (f6i->fib6_type != RTN_UNICAST) 4418 return BPF_FIB_LKUP_RET_NOT_FWDED; 4419 4420 if (f6i->fib6_nsiblings && fl6.flowi6_oif == 0) 4421 f6i = ipv6_stub->fib6_multipath_select(net, f6i, &fl6, 4422 fl6.flowi6_oif, NULL, 4423 strict); 4424 4425 if (check_mtu) { 4426 mtu = ipv6_stub->ip6_mtu_from_fib6(f6i, dst, src); 4427 if (params->tot_len > mtu) 4428 return BPF_FIB_LKUP_RET_FRAG_NEEDED; 4429 } 4430 4431 if (f6i->fib6_nh.nh_lwtstate) 4432 return BPF_FIB_LKUP_RET_UNSUPP_LWT; 4433 4434 if (f6i->fib6_flags & RTF_GATEWAY) 4435 *dst = f6i->fib6_nh.nh_gw; 4436 4437 dev = f6i->fib6_nh.nh_dev; 4438 params->rt_metric = f6i->fib6_metric; 4439 4440 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is 4441 * not needed here. Can not use __ipv6_neigh_lookup_noref here 4442 * because we need to get nd_tbl via the stub 4443 */ 4444 neigh = ___neigh_lookup_noref(ipv6_stub->nd_tbl, neigh_key_eq128, 4445 ndisc_hashfn, dst, dev); 4446 if (!neigh) 4447 return BPF_FIB_LKUP_RET_NO_NEIGH; 4448 4449 return bpf_fib_set_fwd_params(params, neigh, dev); 4450 } 4451 #endif 4452 4453 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx, 4454 struct bpf_fib_lookup *, params, int, plen, u32, flags) 4455 { 4456 if (plen < sizeof(*params)) 4457 return -EINVAL; 4458 4459 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT)) 4460 return -EINVAL; 4461 4462 switch (params->family) { 4463 #if IS_ENABLED(CONFIG_INET) 4464 case AF_INET: 4465 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params, 4466 flags, true); 4467 #endif 4468 #if IS_ENABLED(CONFIG_IPV6) 4469 case AF_INET6: 4470 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params, 4471 flags, true); 4472 #endif 4473 } 4474 return -EAFNOSUPPORT; 4475 } 4476 4477 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = { 4478 .func = bpf_xdp_fib_lookup, 4479 .gpl_only = true, 4480 .ret_type = RET_INTEGER, 4481 .arg1_type = ARG_PTR_TO_CTX, 4482 .arg2_type = ARG_PTR_TO_MEM, 4483 .arg3_type = ARG_CONST_SIZE, 4484 .arg4_type = ARG_ANYTHING, 4485 }; 4486 4487 BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb, 4488 struct bpf_fib_lookup *, params, int, plen, u32, flags) 4489 { 4490 struct net *net = dev_net(skb->dev); 4491 int rc = -EAFNOSUPPORT; 4492 4493 if (plen < sizeof(*params)) 4494 return -EINVAL; 4495 4496 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT)) 4497 return -EINVAL; 4498 4499 switch (params->family) { 4500 #if IS_ENABLED(CONFIG_INET) 4501 case AF_INET: 4502 rc = bpf_ipv4_fib_lookup(net, params, flags, false); 4503 break; 4504 #endif 4505 #if IS_ENABLED(CONFIG_IPV6) 4506 case AF_INET6: 4507 rc = bpf_ipv6_fib_lookup(net, params, flags, false); 4508 break; 4509 #endif 4510 } 4511 4512 if (!rc) { 4513 struct net_device *dev; 4514 4515 dev = dev_get_by_index_rcu(net, params->ifindex); 4516 if (!is_skb_forwardable(dev, skb)) 4517 rc = BPF_FIB_LKUP_RET_FRAG_NEEDED; 4518 } 4519 4520 return rc; 4521 } 4522 4523 static const struct bpf_func_proto bpf_skb_fib_lookup_proto = { 4524 .func = bpf_skb_fib_lookup, 4525 .gpl_only = true, 4526 .ret_type = RET_INTEGER, 4527 .arg1_type = ARG_PTR_TO_CTX, 4528 .arg2_type = ARG_PTR_TO_MEM, 4529 .arg3_type = ARG_CONST_SIZE, 4530 .arg4_type = ARG_ANYTHING, 4531 }; 4532 4533 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF) 4534 static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len) 4535 { 4536 int err; 4537 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr; 4538 4539 if (!seg6_validate_srh(srh, len)) 4540 return -EINVAL; 4541 4542 switch (type) { 4543 case BPF_LWT_ENCAP_SEG6_INLINE: 4544 if (skb->protocol != htons(ETH_P_IPV6)) 4545 return -EBADMSG; 4546 4547 err = seg6_do_srh_inline(skb, srh); 4548 break; 4549 case BPF_LWT_ENCAP_SEG6: 4550 skb_reset_inner_headers(skb); 4551 skb->encapsulation = 1; 4552 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6); 4553 break; 4554 default: 4555 return -EINVAL; 4556 } 4557 4558 bpf_compute_data_pointers(skb); 4559 if (err) 4560 return err; 4561 4562 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); 4563 skb_set_transport_header(skb, sizeof(struct ipv6hdr)); 4564 4565 return seg6_lookup_nexthop(skb, NULL, 0); 4566 } 4567 #endif /* CONFIG_IPV6_SEG6_BPF */ 4568 4569 BPF_CALL_4(bpf_lwt_push_encap, struct sk_buff *, skb, u32, type, void *, hdr, 4570 u32, len) 4571 { 4572 switch (type) { 4573 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF) 4574 case BPF_LWT_ENCAP_SEG6: 4575 case BPF_LWT_ENCAP_SEG6_INLINE: 4576 return bpf_push_seg6_encap(skb, type, hdr, len); 4577 #endif 4578 default: 4579 return -EINVAL; 4580 } 4581 } 4582 4583 static const struct bpf_func_proto bpf_lwt_push_encap_proto = { 4584 .func = bpf_lwt_push_encap, 4585 .gpl_only = false, 4586 .ret_type = RET_INTEGER, 4587 .arg1_type = ARG_PTR_TO_CTX, 4588 .arg2_type = ARG_ANYTHING, 4589 .arg3_type = ARG_PTR_TO_MEM, 4590 .arg4_type = ARG_CONST_SIZE 4591 }; 4592 4593 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF) 4594 BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset, 4595 const void *, from, u32, len) 4596 { 4597 struct seg6_bpf_srh_state *srh_state = 4598 this_cpu_ptr(&seg6_bpf_srh_states); 4599 struct ipv6_sr_hdr *srh = srh_state->srh; 4600 void *srh_tlvs, *srh_end, *ptr; 4601 int srhoff = 0; 4602 4603 if (srh == NULL) 4604 return -EINVAL; 4605 4606 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4)); 4607 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen); 4608 4609 ptr = skb->data + offset; 4610 if (ptr >= srh_tlvs && ptr + len <= srh_end) 4611 srh_state->valid = false; 4612 else if (ptr < (void *)&srh->flags || 4613 ptr + len > (void *)&srh->segments) 4614 return -EFAULT; 4615 4616 if (unlikely(bpf_try_make_writable(skb, offset + len))) 4617 return -EFAULT; 4618 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) 4619 return -EINVAL; 4620 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); 4621 4622 memcpy(skb->data + offset, from, len); 4623 return 0; 4624 } 4625 4626 static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = { 4627 .func = bpf_lwt_seg6_store_bytes, 4628 .gpl_only = false, 4629 .ret_type = RET_INTEGER, 4630 .arg1_type = ARG_PTR_TO_CTX, 4631 .arg2_type = ARG_ANYTHING, 4632 .arg3_type = ARG_PTR_TO_MEM, 4633 .arg4_type = ARG_CONST_SIZE 4634 }; 4635 4636 static void bpf_update_srh_state(struct sk_buff *skb) 4637 { 4638 struct seg6_bpf_srh_state *srh_state = 4639 this_cpu_ptr(&seg6_bpf_srh_states); 4640 int srhoff = 0; 4641 4642 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) { 4643 srh_state->srh = NULL; 4644 } else { 4645 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); 4646 srh_state->hdrlen = srh_state->srh->hdrlen << 3; 4647 srh_state->valid = true; 4648 } 4649 } 4650 4651 BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb, 4652 u32, action, void *, param, u32, param_len) 4653 { 4654 struct seg6_bpf_srh_state *srh_state = 4655 this_cpu_ptr(&seg6_bpf_srh_states); 4656 int hdroff = 0; 4657 int err; 4658 4659 switch (action) { 4660 case SEG6_LOCAL_ACTION_END_X: 4661 if (!seg6_bpf_has_valid_srh(skb)) 4662 return -EBADMSG; 4663 if (param_len != sizeof(struct in6_addr)) 4664 return -EINVAL; 4665 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0); 4666 case SEG6_LOCAL_ACTION_END_T: 4667 if (!seg6_bpf_has_valid_srh(skb)) 4668 return -EBADMSG; 4669 if (param_len != sizeof(int)) 4670 return -EINVAL; 4671 return seg6_lookup_nexthop(skb, NULL, *(int *)param); 4672 case SEG6_LOCAL_ACTION_END_DT6: 4673 if (!seg6_bpf_has_valid_srh(skb)) 4674 return -EBADMSG; 4675 if (param_len != sizeof(int)) 4676 return -EINVAL; 4677 4678 if (ipv6_find_hdr(skb, &hdroff, IPPROTO_IPV6, NULL, NULL) < 0) 4679 return -EBADMSG; 4680 if (!pskb_pull(skb, hdroff)) 4681 return -EBADMSG; 4682 4683 skb_postpull_rcsum(skb, skb_network_header(skb), hdroff); 4684 skb_reset_network_header(skb); 4685 skb_reset_transport_header(skb); 4686 skb->encapsulation = 0; 4687 4688 bpf_compute_data_pointers(skb); 4689 bpf_update_srh_state(skb); 4690 return seg6_lookup_nexthop(skb, NULL, *(int *)param); 4691 case SEG6_LOCAL_ACTION_END_B6: 4692 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb)) 4693 return -EBADMSG; 4694 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE, 4695 param, param_len); 4696 if (!err) 4697 bpf_update_srh_state(skb); 4698 4699 return err; 4700 case SEG6_LOCAL_ACTION_END_B6_ENCAP: 4701 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb)) 4702 return -EBADMSG; 4703 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6, 4704 param, param_len); 4705 if (!err) 4706 bpf_update_srh_state(skb); 4707 4708 return err; 4709 default: 4710 return -EINVAL; 4711 } 4712 } 4713 4714 static const struct bpf_func_proto bpf_lwt_seg6_action_proto = { 4715 .func = bpf_lwt_seg6_action, 4716 .gpl_only = false, 4717 .ret_type = RET_INTEGER, 4718 .arg1_type = ARG_PTR_TO_CTX, 4719 .arg2_type = ARG_ANYTHING, 4720 .arg3_type = ARG_PTR_TO_MEM, 4721 .arg4_type = ARG_CONST_SIZE 4722 }; 4723 4724 BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset, 4725 s32, len) 4726 { 4727 struct seg6_bpf_srh_state *srh_state = 4728 this_cpu_ptr(&seg6_bpf_srh_states); 4729 struct ipv6_sr_hdr *srh = srh_state->srh; 4730 void *srh_end, *srh_tlvs, *ptr; 4731 struct ipv6hdr *hdr; 4732 int srhoff = 0; 4733 int ret; 4734 4735 if (unlikely(srh == NULL)) 4736 return -EINVAL; 4737 4738 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) + 4739 ((srh->first_segment + 1) << 4)); 4740 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) + 4741 srh_state->hdrlen); 4742 ptr = skb->data + offset; 4743 4744 if (unlikely(ptr < srh_tlvs || ptr > srh_end)) 4745 return -EFAULT; 4746 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end)) 4747 return -EFAULT; 4748 4749 if (len > 0) { 4750 ret = skb_cow_head(skb, len); 4751 if (unlikely(ret < 0)) 4752 return ret; 4753 4754 ret = bpf_skb_net_hdr_push(skb, offset, len); 4755 } else { 4756 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len); 4757 } 4758 4759 bpf_compute_data_pointers(skb); 4760 if (unlikely(ret < 0)) 4761 return ret; 4762 4763 hdr = (struct ipv6hdr *)skb->data; 4764 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); 4765 4766 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) 4767 return -EINVAL; 4768 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); 4769 srh_state->hdrlen += len; 4770 srh_state->valid = false; 4771 return 0; 4772 } 4773 4774 static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = { 4775 .func = bpf_lwt_seg6_adjust_srh, 4776 .gpl_only = false, 4777 .ret_type = RET_INTEGER, 4778 .arg1_type = ARG_PTR_TO_CTX, 4779 .arg2_type = ARG_ANYTHING, 4780 .arg3_type = ARG_ANYTHING, 4781 }; 4782 #endif /* CONFIG_IPV6_SEG6_BPF */ 4783 4784 bool bpf_helper_changes_pkt_data(void *func) 4785 { 4786 if (func == bpf_skb_vlan_push || 4787 func == bpf_skb_vlan_pop || 4788 func == bpf_skb_store_bytes || 4789 func == bpf_skb_change_proto || 4790 func == bpf_skb_change_head || 4791 func == sk_skb_change_head || 4792 func == bpf_skb_change_tail || 4793 func == sk_skb_change_tail || 4794 func == bpf_skb_adjust_room || 4795 func == bpf_skb_pull_data || 4796 func == sk_skb_pull_data || 4797 func == bpf_clone_redirect || 4798 func == bpf_l3_csum_replace || 4799 func == bpf_l4_csum_replace || 4800 func == bpf_xdp_adjust_head || 4801 func == bpf_xdp_adjust_meta || 4802 func == bpf_msg_pull_data || 4803 func == bpf_xdp_adjust_tail || 4804 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF) 4805 func == bpf_lwt_seg6_store_bytes || 4806 func == bpf_lwt_seg6_adjust_srh || 4807 func == bpf_lwt_seg6_action || 4808 #endif 4809 func == bpf_lwt_push_encap) 4810 return true; 4811 4812 return false; 4813 } 4814 4815 static const struct bpf_func_proto * 4816 bpf_base_func_proto(enum bpf_func_id func_id) 4817 { 4818 switch (func_id) { 4819 case BPF_FUNC_map_lookup_elem: 4820 return &bpf_map_lookup_elem_proto; 4821 case BPF_FUNC_map_update_elem: 4822 return &bpf_map_update_elem_proto; 4823 case BPF_FUNC_map_delete_elem: 4824 return &bpf_map_delete_elem_proto; 4825 case BPF_FUNC_get_prandom_u32: 4826 return &bpf_get_prandom_u32_proto; 4827 case BPF_FUNC_get_smp_processor_id: 4828 return &bpf_get_raw_smp_processor_id_proto; 4829 case BPF_FUNC_get_numa_node_id: 4830 return &bpf_get_numa_node_id_proto; 4831 case BPF_FUNC_tail_call: 4832 return &bpf_tail_call_proto; 4833 case BPF_FUNC_ktime_get_ns: 4834 return &bpf_ktime_get_ns_proto; 4835 case BPF_FUNC_trace_printk: 4836 if (capable(CAP_SYS_ADMIN)) 4837 return bpf_get_trace_printk_proto(); 4838 /* else: fall through */ 4839 default: 4840 return NULL; 4841 } 4842 } 4843 4844 static const struct bpf_func_proto * 4845 sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 4846 { 4847 switch (func_id) { 4848 /* inet and inet6 sockets are created in a process 4849 * context so there is always a valid uid/gid 4850 */ 4851 case BPF_FUNC_get_current_uid_gid: 4852 return &bpf_get_current_uid_gid_proto; 4853 case BPF_FUNC_get_local_storage: 4854 return &bpf_get_local_storage_proto; 4855 default: 4856 return bpf_base_func_proto(func_id); 4857 } 4858 } 4859 4860 static const struct bpf_func_proto * 4861 sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 4862 { 4863 switch (func_id) { 4864 /* inet and inet6 sockets are created in a process 4865 * context so there is always a valid uid/gid 4866 */ 4867 case BPF_FUNC_get_current_uid_gid: 4868 return &bpf_get_current_uid_gid_proto; 4869 case BPF_FUNC_bind: 4870 switch (prog->expected_attach_type) { 4871 case BPF_CGROUP_INET4_CONNECT: 4872 case BPF_CGROUP_INET6_CONNECT: 4873 return &bpf_bind_proto; 4874 default: 4875 return NULL; 4876 } 4877 case BPF_FUNC_get_socket_cookie: 4878 return &bpf_get_socket_cookie_sock_addr_proto; 4879 case BPF_FUNC_get_local_storage: 4880 return &bpf_get_local_storage_proto; 4881 default: 4882 return bpf_base_func_proto(func_id); 4883 } 4884 } 4885 4886 static const struct bpf_func_proto * 4887 sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 4888 { 4889 switch (func_id) { 4890 case BPF_FUNC_skb_load_bytes: 4891 return &bpf_skb_load_bytes_proto; 4892 case BPF_FUNC_skb_load_bytes_relative: 4893 return &bpf_skb_load_bytes_relative_proto; 4894 case BPF_FUNC_get_socket_cookie: 4895 return &bpf_get_socket_cookie_proto; 4896 case BPF_FUNC_get_socket_uid: 4897 return &bpf_get_socket_uid_proto; 4898 default: 4899 return bpf_base_func_proto(func_id); 4900 } 4901 } 4902 4903 static const struct bpf_func_proto * 4904 cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 4905 { 4906 switch (func_id) { 4907 case BPF_FUNC_get_local_storage: 4908 return &bpf_get_local_storage_proto; 4909 default: 4910 return sk_filter_func_proto(func_id, prog); 4911 } 4912 } 4913 4914 static const struct bpf_func_proto * 4915 tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 4916 { 4917 switch (func_id) { 4918 case BPF_FUNC_skb_store_bytes: 4919 return &bpf_skb_store_bytes_proto; 4920 case BPF_FUNC_skb_load_bytes: 4921 return &bpf_skb_load_bytes_proto; 4922 case BPF_FUNC_skb_load_bytes_relative: 4923 return &bpf_skb_load_bytes_relative_proto; 4924 case BPF_FUNC_skb_pull_data: 4925 return &bpf_skb_pull_data_proto; 4926 case BPF_FUNC_csum_diff: 4927 return &bpf_csum_diff_proto; 4928 case BPF_FUNC_csum_update: 4929 return &bpf_csum_update_proto; 4930 case BPF_FUNC_l3_csum_replace: 4931 return &bpf_l3_csum_replace_proto; 4932 case BPF_FUNC_l4_csum_replace: 4933 return &bpf_l4_csum_replace_proto; 4934 case BPF_FUNC_clone_redirect: 4935 return &bpf_clone_redirect_proto; 4936 case BPF_FUNC_get_cgroup_classid: 4937 return &bpf_get_cgroup_classid_proto; 4938 case BPF_FUNC_skb_vlan_push: 4939 return &bpf_skb_vlan_push_proto; 4940 case BPF_FUNC_skb_vlan_pop: 4941 return &bpf_skb_vlan_pop_proto; 4942 case BPF_FUNC_skb_change_proto: 4943 return &bpf_skb_change_proto_proto; 4944 case BPF_FUNC_skb_change_type: 4945 return &bpf_skb_change_type_proto; 4946 case BPF_FUNC_skb_adjust_room: 4947 return &bpf_skb_adjust_room_proto; 4948 case BPF_FUNC_skb_change_tail: 4949 return &bpf_skb_change_tail_proto; 4950 case BPF_FUNC_skb_get_tunnel_key: 4951 return &bpf_skb_get_tunnel_key_proto; 4952 case BPF_FUNC_skb_set_tunnel_key: 4953 return bpf_get_skb_set_tunnel_proto(func_id); 4954 case BPF_FUNC_skb_get_tunnel_opt: 4955 return &bpf_skb_get_tunnel_opt_proto; 4956 case BPF_FUNC_skb_set_tunnel_opt: 4957 return bpf_get_skb_set_tunnel_proto(func_id); 4958 case BPF_FUNC_redirect: 4959 return &bpf_redirect_proto; 4960 case BPF_FUNC_get_route_realm: 4961 return &bpf_get_route_realm_proto; 4962 case BPF_FUNC_get_hash_recalc: 4963 return &bpf_get_hash_recalc_proto; 4964 case BPF_FUNC_set_hash_invalid: 4965 return &bpf_set_hash_invalid_proto; 4966 case BPF_FUNC_set_hash: 4967 return &bpf_set_hash_proto; 4968 case BPF_FUNC_perf_event_output: 4969 return &bpf_skb_event_output_proto; 4970 case BPF_FUNC_get_smp_processor_id: 4971 return &bpf_get_smp_processor_id_proto; 4972 case BPF_FUNC_skb_under_cgroup: 4973 return &bpf_skb_under_cgroup_proto; 4974 case BPF_FUNC_get_socket_cookie: 4975 return &bpf_get_socket_cookie_proto; 4976 case BPF_FUNC_get_socket_uid: 4977 return &bpf_get_socket_uid_proto; 4978 case BPF_FUNC_fib_lookup: 4979 return &bpf_skb_fib_lookup_proto; 4980 #ifdef CONFIG_XFRM 4981 case BPF_FUNC_skb_get_xfrm_state: 4982 return &bpf_skb_get_xfrm_state_proto; 4983 #endif 4984 #ifdef CONFIG_SOCK_CGROUP_DATA 4985 case BPF_FUNC_skb_cgroup_id: 4986 return &bpf_skb_cgroup_id_proto; 4987 case BPF_FUNC_skb_ancestor_cgroup_id: 4988 return &bpf_skb_ancestor_cgroup_id_proto; 4989 #endif 4990 default: 4991 return bpf_base_func_proto(func_id); 4992 } 4993 } 4994 4995 static const struct bpf_func_proto * 4996 xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 4997 { 4998 switch (func_id) { 4999 case BPF_FUNC_perf_event_output: 5000 return &bpf_xdp_event_output_proto; 5001 case BPF_FUNC_get_smp_processor_id: 5002 return &bpf_get_smp_processor_id_proto; 5003 case BPF_FUNC_csum_diff: 5004 return &bpf_csum_diff_proto; 5005 case BPF_FUNC_xdp_adjust_head: 5006 return &bpf_xdp_adjust_head_proto; 5007 case BPF_FUNC_xdp_adjust_meta: 5008 return &bpf_xdp_adjust_meta_proto; 5009 case BPF_FUNC_redirect: 5010 return &bpf_xdp_redirect_proto; 5011 case BPF_FUNC_redirect_map: 5012 return &bpf_xdp_redirect_map_proto; 5013 case BPF_FUNC_xdp_adjust_tail: 5014 return &bpf_xdp_adjust_tail_proto; 5015 case BPF_FUNC_fib_lookup: 5016 return &bpf_xdp_fib_lookup_proto; 5017 default: 5018 return bpf_base_func_proto(func_id); 5019 } 5020 } 5021 5022 static const struct bpf_func_proto * 5023 sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 5024 { 5025 switch (func_id) { 5026 case BPF_FUNC_setsockopt: 5027 return &bpf_setsockopt_proto; 5028 case BPF_FUNC_getsockopt: 5029 return &bpf_getsockopt_proto; 5030 case BPF_FUNC_sock_ops_cb_flags_set: 5031 return &bpf_sock_ops_cb_flags_set_proto; 5032 case BPF_FUNC_sock_map_update: 5033 return &bpf_sock_map_update_proto; 5034 case BPF_FUNC_sock_hash_update: 5035 return &bpf_sock_hash_update_proto; 5036 case BPF_FUNC_get_socket_cookie: 5037 return &bpf_get_socket_cookie_sock_ops_proto; 5038 case BPF_FUNC_get_local_storage: 5039 return &bpf_get_local_storage_proto; 5040 default: 5041 return bpf_base_func_proto(func_id); 5042 } 5043 } 5044 5045 static const struct bpf_func_proto * 5046 sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 5047 { 5048 switch (func_id) { 5049 case BPF_FUNC_msg_redirect_map: 5050 return &bpf_msg_redirect_map_proto; 5051 case BPF_FUNC_msg_redirect_hash: 5052 return &bpf_msg_redirect_hash_proto; 5053 case BPF_FUNC_msg_apply_bytes: 5054 return &bpf_msg_apply_bytes_proto; 5055 case BPF_FUNC_msg_cork_bytes: 5056 return &bpf_msg_cork_bytes_proto; 5057 case BPF_FUNC_msg_pull_data: 5058 return &bpf_msg_pull_data_proto; 5059 case BPF_FUNC_get_local_storage: 5060 return &bpf_get_local_storage_proto; 5061 default: 5062 return bpf_base_func_proto(func_id); 5063 } 5064 } 5065 5066 static const struct bpf_func_proto * 5067 sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 5068 { 5069 switch (func_id) { 5070 case BPF_FUNC_skb_store_bytes: 5071 return &bpf_skb_store_bytes_proto; 5072 case BPF_FUNC_skb_load_bytes: 5073 return &bpf_skb_load_bytes_proto; 5074 case BPF_FUNC_skb_pull_data: 5075 return &sk_skb_pull_data_proto; 5076 case BPF_FUNC_skb_change_tail: 5077 return &sk_skb_change_tail_proto; 5078 case BPF_FUNC_skb_change_head: 5079 return &sk_skb_change_head_proto; 5080 case BPF_FUNC_get_socket_cookie: 5081 return &bpf_get_socket_cookie_proto; 5082 case BPF_FUNC_get_socket_uid: 5083 return &bpf_get_socket_uid_proto; 5084 case BPF_FUNC_sk_redirect_map: 5085 return &bpf_sk_redirect_map_proto; 5086 case BPF_FUNC_sk_redirect_hash: 5087 return &bpf_sk_redirect_hash_proto; 5088 case BPF_FUNC_get_local_storage: 5089 return &bpf_get_local_storage_proto; 5090 default: 5091 return bpf_base_func_proto(func_id); 5092 } 5093 } 5094 5095 static const struct bpf_func_proto * 5096 lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 5097 { 5098 switch (func_id) { 5099 case BPF_FUNC_skb_load_bytes: 5100 return &bpf_skb_load_bytes_proto; 5101 case BPF_FUNC_skb_pull_data: 5102 return &bpf_skb_pull_data_proto; 5103 case BPF_FUNC_csum_diff: 5104 return &bpf_csum_diff_proto; 5105 case BPF_FUNC_get_cgroup_classid: 5106 return &bpf_get_cgroup_classid_proto; 5107 case BPF_FUNC_get_route_realm: 5108 return &bpf_get_route_realm_proto; 5109 case BPF_FUNC_get_hash_recalc: 5110 return &bpf_get_hash_recalc_proto; 5111 case BPF_FUNC_perf_event_output: 5112 return &bpf_skb_event_output_proto; 5113 case BPF_FUNC_get_smp_processor_id: 5114 return &bpf_get_smp_processor_id_proto; 5115 case BPF_FUNC_skb_under_cgroup: 5116 return &bpf_skb_under_cgroup_proto; 5117 default: 5118 return bpf_base_func_proto(func_id); 5119 } 5120 } 5121 5122 static const struct bpf_func_proto * 5123 lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 5124 { 5125 switch (func_id) { 5126 case BPF_FUNC_lwt_push_encap: 5127 return &bpf_lwt_push_encap_proto; 5128 default: 5129 return lwt_out_func_proto(func_id, prog); 5130 } 5131 } 5132 5133 static const struct bpf_func_proto * 5134 lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 5135 { 5136 switch (func_id) { 5137 case BPF_FUNC_skb_get_tunnel_key: 5138 return &bpf_skb_get_tunnel_key_proto; 5139 case BPF_FUNC_skb_set_tunnel_key: 5140 return bpf_get_skb_set_tunnel_proto(func_id); 5141 case BPF_FUNC_skb_get_tunnel_opt: 5142 return &bpf_skb_get_tunnel_opt_proto; 5143 case BPF_FUNC_skb_set_tunnel_opt: 5144 return bpf_get_skb_set_tunnel_proto(func_id); 5145 case BPF_FUNC_redirect: 5146 return &bpf_redirect_proto; 5147 case BPF_FUNC_clone_redirect: 5148 return &bpf_clone_redirect_proto; 5149 case BPF_FUNC_skb_change_tail: 5150 return &bpf_skb_change_tail_proto; 5151 case BPF_FUNC_skb_change_head: 5152 return &bpf_skb_change_head_proto; 5153 case BPF_FUNC_skb_store_bytes: 5154 return &bpf_skb_store_bytes_proto; 5155 case BPF_FUNC_csum_update: 5156 return &bpf_csum_update_proto; 5157 case BPF_FUNC_l3_csum_replace: 5158 return &bpf_l3_csum_replace_proto; 5159 case BPF_FUNC_l4_csum_replace: 5160 return &bpf_l4_csum_replace_proto; 5161 case BPF_FUNC_set_hash_invalid: 5162 return &bpf_set_hash_invalid_proto; 5163 default: 5164 return lwt_out_func_proto(func_id, prog); 5165 } 5166 } 5167 5168 static const struct bpf_func_proto * 5169 lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 5170 { 5171 switch (func_id) { 5172 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF) 5173 case BPF_FUNC_lwt_seg6_store_bytes: 5174 return &bpf_lwt_seg6_store_bytes_proto; 5175 case BPF_FUNC_lwt_seg6_action: 5176 return &bpf_lwt_seg6_action_proto; 5177 case BPF_FUNC_lwt_seg6_adjust_srh: 5178 return &bpf_lwt_seg6_adjust_srh_proto; 5179 #endif 5180 default: 5181 return lwt_out_func_proto(func_id, prog); 5182 } 5183 } 5184 5185 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type, 5186 const struct bpf_prog *prog, 5187 struct bpf_insn_access_aux *info) 5188 { 5189 const int size_default = sizeof(__u32); 5190 5191 if (off < 0 || off >= sizeof(struct __sk_buff)) 5192 return false; 5193 5194 /* The verifier guarantees that size > 0. */ 5195 if (off % size != 0) 5196 return false; 5197 5198 switch (off) { 5199 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]): 5200 if (off + size > offsetofend(struct __sk_buff, cb[4])) 5201 return false; 5202 break; 5203 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]): 5204 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]): 5205 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4): 5206 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4): 5207 case bpf_ctx_range(struct __sk_buff, data): 5208 case bpf_ctx_range(struct __sk_buff, data_meta): 5209 case bpf_ctx_range(struct __sk_buff, data_end): 5210 if (size != size_default) 5211 return false; 5212 break; 5213 default: 5214 /* Only narrow read access allowed for now. */ 5215 if (type == BPF_WRITE) { 5216 if (size != size_default) 5217 return false; 5218 } else { 5219 bpf_ctx_record_field_size(info, size_default); 5220 if (!bpf_ctx_narrow_access_ok(off, size, size_default)) 5221 return false; 5222 } 5223 } 5224 5225 return true; 5226 } 5227 5228 static bool sk_filter_is_valid_access(int off, int size, 5229 enum bpf_access_type type, 5230 const struct bpf_prog *prog, 5231 struct bpf_insn_access_aux *info) 5232 { 5233 switch (off) { 5234 case bpf_ctx_range(struct __sk_buff, tc_classid): 5235 case bpf_ctx_range(struct __sk_buff, data): 5236 case bpf_ctx_range(struct __sk_buff, data_meta): 5237 case bpf_ctx_range(struct __sk_buff, data_end): 5238 case bpf_ctx_range_till(struct __sk_buff, family, local_port): 5239 return false; 5240 } 5241 5242 if (type == BPF_WRITE) { 5243 switch (off) { 5244 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]): 5245 break; 5246 default: 5247 return false; 5248 } 5249 } 5250 5251 return bpf_skb_is_valid_access(off, size, type, prog, info); 5252 } 5253 5254 static bool lwt_is_valid_access(int off, int size, 5255 enum bpf_access_type type, 5256 const struct bpf_prog *prog, 5257 struct bpf_insn_access_aux *info) 5258 { 5259 switch (off) { 5260 case bpf_ctx_range(struct __sk_buff, tc_classid): 5261 case bpf_ctx_range_till(struct __sk_buff, family, local_port): 5262 case bpf_ctx_range(struct __sk_buff, data_meta): 5263 return false; 5264 } 5265 5266 if (type == BPF_WRITE) { 5267 switch (off) { 5268 case bpf_ctx_range(struct __sk_buff, mark): 5269 case bpf_ctx_range(struct __sk_buff, priority): 5270 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]): 5271 break; 5272 default: 5273 return false; 5274 } 5275 } 5276 5277 switch (off) { 5278 case bpf_ctx_range(struct __sk_buff, data): 5279 info->reg_type = PTR_TO_PACKET; 5280 break; 5281 case bpf_ctx_range(struct __sk_buff, data_end): 5282 info->reg_type = PTR_TO_PACKET_END; 5283 break; 5284 } 5285 5286 return bpf_skb_is_valid_access(off, size, type, prog, info); 5287 } 5288 5289 /* Attach type specific accesses */ 5290 static bool __sock_filter_check_attach_type(int off, 5291 enum bpf_access_type access_type, 5292 enum bpf_attach_type attach_type) 5293 { 5294 switch (off) { 5295 case offsetof(struct bpf_sock, bound_dev_if): 5296 case offsetof(struct bpf_sock, mark): 5297 case offsetof(struct bpf_sock, priority): 5298 switch (attach_type) { 5299 case BPF_CGROUP_INET_SOCK_CREATE: 5300 goto full_access; 5301 default: 5302 return false; 5303 } 5304 case bpf_ctx_range(struct bpf_sock, src_ip4): 5305 switch (attach_type) { 5306 case BPF_CGROUP_INET4_POST_BIND: 5307 goto read_only; 5308 default: 5309 return false; 5310 } 5311 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]): 5312 switch (attach_type) { 5313 case BPF_CGROUP_INET6_POST_BIND: 5314 goto read_only; 5315 default: 5316 return false; 5317 } 5318 case bpf_ctx_range(struct bpf_sock, src_port): 5319 switch (attach_type) { 5320 case BPF_CGROUP_INET4_POST_BIND: 5321 case BPF_CGROUP_INET6_POST_BIND: 5322 goto read_only; 5323 default: 5324 return false; 5325 } 5326 } 5327 read_only: 5328 return access_type == BPF_READ; 5329 full_access: 5330 return true; 5331 } 5332 5333 static bool __sock_filter_check_size(int off, int size, 5334 struct bpf_insn_access_aux *info) 5335 { 5336 const int size_default = sizeof(__u32); 5337 5338 switch (off) { 5339 case bpf_ctx_range(struct bpf_sock, src_ip4): 5340 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]): 5341 bpf_ctx_record_field_size(info, size_default); 5342 return bpf_ctx_narrow_access_ok(off, size, size_default); 5343 } 5344 5345 return size == size_default; 5346 } 5347 5348 static bool sock_filter_is_valid_access(int off, int size, 5349 enum bpf_access_type type, 5350 const struct bpf_prog *prog, 5351 struct bpf_insn_access_aux *info) 5352 { 5353 if (off < 0 || off >= sizeof(struct bpf_sock)) 5354 return false; 5355 if (off % size != 0) 5356 return false; 5357 if (!__sock_filter_check_attach_type(off, type, 5358 prog->expected_attach_type)) 5359 return false; 5360 if (!__sock_filter_check_size(off, size, info)) 5361 return false; 5362 return true; 5363 } 5364 5365 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write, 5366 const struct bpf_prog *prog, int drop_verdict) 5367 { 5368 struct bpf_insn *insn = insn_buf; 5369 5370 if (!direct_write) 5371 return 0; 5372 5373 /* if (!skb->cloned) 5374 * goto start; 5375 * 5376 * (Fast-path, otherwise approximation that we might be 5377 * a clone, do the rest in helper.) 5378 */ 5379 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET()); 5380 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK); 5381 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7); 5382 5383 /* ret = bpf_skb_pull_data(skb, 0); */ 5384 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1); 5385 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2); 5386 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, 5387 BPF_FUNC_skb_pull_data); 5388 /* if (!ret) 5389 * goto restore; 5390 * return TC_ACT_SHOT; 5391 */ 5392 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2); 5393 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict); 5394 *insn++ = BPF_EXIT_INSN(); 5395 5396 /* restore: */ 5397 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6); 5398 /* start: */ 5399 *insn++ = prog->insnsi[0]; 5400 5401 return insn - insn_buf; 5402 } 5403 5404 static int bpf_gen_ld_abs(const struct bpf_insn *orig, 5405 struct bpf_insn *insn_buf) 5406 { 5407 bool indirect = BPF_MODE(orig->code) == BPF_IND; 5408 struct bpf_insn *insn = insn_buf; 5409 5410 /* We're guaranteed here that CTX is in R6. */ 5411 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX); 5412 if (!indirect) { 5413 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm); 5414 } else { 5415 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg); 5416 if (orig->imm) 5417 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm); 5418 } 5419 5420 switch (BPF_SIZE(orig->code)) { 5421 case BPF_B: 5422 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache); 5423 break; 5424 case BPF_H: 5425 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache); 5426 break; 5427 case BPF_W: 5428 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache); 5429 break; 5430 } 5431 5432 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2); 5433 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0); 5434 *insn++ = BPF_EXIT_INSN(); 5435 5436 return insn - insn_buf; 5437 } 5438 5439 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write, 5440 const struct bpf_prog *prog) 5441 { 5442 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT); 5443 } 5444 5445 static bool tc_cls_act_is_valid_access(int off, int size, 5446 enum bpf_access_type type, 5447 const struct bpf_prog *prog, 5448 struct bpf_insn_access_aux *info) 5449 { 5450 if (type == BPF_WRITE) { 5451 switch (off) { 5452 case bpf_ctx_range(struct __sk_buff, mark): 5453 case bpf_ctx_range(struct __sk_buff, tc_index): 5454 case bpf_ctx_range(struct __sk_buff, priority): 5455 case bpf_ctx_range(struct __sk_buff, tc_classid): 5456 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]): 5457 break; 5458 default: 5459 return false; 5460 } 5461 } 5462 5463 switch (off) { 5464 case bpf_ctx_range(struct __sk_buff, data): 5465 info->reg_type = PTR_TO_PACKET; 5466 break; 5467 case bpf_ctx_range(struct __sk_buff, data_meta): 5468 info->reg_type = PTR_TO_PACKET_META; 5469 break; 5470 case bpf_ctx_range(struct __sk_buff, data_end): 5471 info->reg_type = PTR_TO_PACKET_END; 5472 break; 5473 case bpf_ctx_range_till(struct __sk_buff, family, local_port): 5474 return false; 5475 } 5476 5477 return bpf_skb_is_valid_access(off, size, type, prog, info); 5478 } 5479 5480 static bool __is_valid_xdp_access(int off, int size) 5481 { 5482 if (off < 0 || off >= sizeof(struct xdp_md)) 5483 return false; 5484 if (off % size != 0) 5485 return false; 5486 if (size != sizeof(__u32)) 5487 return false; 5488 5489 return true; 5490 } 5491 5492 static bool xdp_is_valid_access(int off, int size, 5493 enum bpf_access_type type, 5494 const struct bpf_prog *prog, 5495 struct bpf_insn_access_aux *info) 5496 { 5497 if (type == BPF_WRITE) { 5498 if (bpf_prog_is_dev_bound(prog->aux)) { 5499 switch (off) { 5500 case offsetof(struct xdp_md, rx_queue_index): 5501 return __is_valid_xdp_access(off, size); 5502 } 5503 } 5504 return false; 5505 } 5506 5507 switch (off) { 5508 case offsetof(struct xdp_md, data): 5509 info->reg_type = PTR_TO_PACKET; 5510 break; 5511 case offsetof(struct xdp_md, data_meta): 5512 info->reg_type = PTR_TO_PACKET_META; 5513 break; 5514 case offsetof(struct xdp_md, data_end): 5515 info->reg_type = PTR_TO_PACKET_END; 5516 break; 5517 } 5518 5519 return __is_valid_xdp_access(off, size); 5520 } 5521 5522 void bpf_warn_invalid_xdp_action(u32 act) 5523 { 5524 const u32 act_max = XDP_REDIRECT; 5525 5526 WARN_ONCE(1, "%s XDP return value %u, expect packet loss!\n", 5527 act > act_max ? "Illegal" : "Driver unsupported", 5528 act); 5529 } 5530 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action); 5531 5532 static bool sock_addr_is_valid_access(int off, int size, 5533 enum bpf_access_type type, 5534 const struct bpf_prog *prog, 5535 struct bpf_insn_access_aux *info) 5536 { 5537 const int size_default = sizeof(__u32); 5538 5539 if (off < 0 || off >= sizeof(struct bpf_sock_addr)) 5540 return false; 5541 if (off % size != 0) 5542 return false; 5543 5544 /* Disallow access to IPv6 fields from IPv4 contex and vise 5545 * versa. 5546 */ 5547 switch (off) { 5548 case bpf_ctx_range(struct bpf_sock_addr, user_ip4): 5549 switch (prog->expected_attach_type) { 5550 case BPF_CGROUP_INET4_BIND: 5551 case BPF_CGROUP_INET4_CONNECT: 5552 case BPF_CGROUP_UDP4_SENDMSG: 5553 break; 5554 default: 5555 return false; 5556 } 5557 break; 5558 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]): 5559 switch (prog->expected_attach_type) { 5560 case BPF_CGROUP_INET6_BIND: 5561 case BPF_CGROUP_INET6_CONNECT: 5562 case BPF_CGROUP_UDP6_SENDMSG: 5563 break; 5564 default: 5565 return false; 5566 } 5567 break; 5568 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4): 5569 switch (prog->expected_attach_type) { 5570 case BPF_CGROUP_UDP4_SENDMSG: 5571 break; 5572 default: 5573 return false; 5574 } 5575 break; 5576 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0], 5577 msg_src_ip6[3]): 5578 switch (prog->expected_attach_type) { 5579 case BPF_CGROUP_UDP6_SENDMSG: 5580 break; 5581 default: 5582 return false; 5583 } 5584 break; 5585 } 5586 5587 switch (off) { 5588 case bpf_ctx_range(struct bpf_sock_addr, user_ip4): 5589 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]): 5590 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4): 5591 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0], 5592 msg_src_ip6[3]): 5593 /* Only narrow read access allowed for now. */ 5594 if (type == BPF_READ) { 5595 bpf_ctx_record_field_size(info, size_default); 5596 if (!bpf_ctx_narrow_access_ok(off, size, size_default)) 5597 return false; 5598 } else { 5599 if (size != size_default) 5600 return false; 5601 } 5602 break; 5603 case bpf_ctx_range(struct bpf_sock_addr, user_port): 5604 if (size != size_default) 5605 return false; 5606 break; 5607 default: 5608 if (type == BPF_READ) { 5609 if (size != size_default) 5610 return false; 5611 } else { 5612 return false; 5613 } 5614 } 5615 5616 return true; 5617 } 5618 5619 static bool sock_ops_is_valid_access(int off, int size, 5620 enum bpf_access_type type, 5621 const struct bpf_prog *prog, 5622 struct bpf_insn_access_aux *info) 5623 { 5624 const int size_default = sizeof(__u32); 5625 5626 if (off < 0 || off >= sizeof(struct bpf_sock_ops)) 5627 return false; 5628 5629 /* The verifier guarantees that size > 0. */ 5630 if (off % size != 0) 5631 return false; 5632 5633 if (type == BPF_WRITE) { 5634 switch (off) { 5635 case offsetof(struct bpf_sock_ops, reply): 5636 case offsetof(struct bpf_sock_ops, sk_txhash): 5637 if (size != size_default) 5638 return false; 5639 break; 5640 default: 5641 return false; 5642 } 5643 } else { 5644 switch (off) { 5645 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received, 5646 bytes_acked): 5647 if (size != sizeof(__u64)) 5648 return false; 5649 break; 5650 default: 5651 if (size != size_default) 5652 return false; 5653 break; 5654 } 5655 } 5656 5657 return true; 5658 } 5659 5660 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write, 5661 const struct bpf_prog *prog) 5662 { 5663 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP); 5664 } 5665 5666 static bool sk_skb_is_valid_access(int off, int size, 5667 enum bpf_access_type type, 5668 const struct bpf_prog *prog, 5669 struct bpf_insn_access_aux *info) 5670 { 5671 switch (off) { 5672 case bpf_ctx_range(struct __sk_buff, tc_classid): 5673 case bpf_ctx_range(struct __sk_buff, data_meta): 5674 return false; 5675 } 5676 5677 if (type == BPF_WRITE) { 5678 switch (off) { 5679 case bpf_ctx_range(struct __sk_buff, tc_index): 5680 case bpf_ctx_range(struct __sk_buff, priority): 5681 break; 5682 default: 5683 return false; 5684 } 5685 } 5686 5687 switch (off) { 5688 case bpf_ctx_range(struct __sk_buff, mark): 5689 return false; 5690 case bpf_ctx_range(struct __sk_buff, data): 5691 info->reg_type = PTR_TO_PACKET; 5692 break; 5693 case bpf_ctx_range(struct __sk_buff, data_end): 5694 info->reg_type = PTR_TO_PACKET_END; 5695 break; 5696 } 5697 5698 return bpf_skb_is_valid_access(off, size, type, prog, info); 5699 } 5700 5701 static bool sk_msg_is_valid_access(int off, int size, 5702 enum bpf_access_type type, 5703 const struct bpf_prog *prog, 5704 struct bpf_insn_access_aux *info) 5705 { 5706 if (type == BPF_WRITE) 5707 return false; 5708 5709 switch (off) { 5710 case offsetof(struct sk_msg_md, data): 5711 info->reg_type = PTR_TO_PACKET; 5712 if (size != sizeof(__u64)) 5713 return false; 5714 break; 5715 case offsetof(struct sk_msg_md, data_end): 5716 info->reg_type = PTR_TO_PACKET_END; 5717 if (size != sizeof(__u64)) 5718 return false; 5719 break; 5720 default: 5721 if (size != sizeof(__u32)) 5722 return false; 5723 } 5724 5725 if (off < 0 || off >= sizeof(struct sk_msg_md)) 5726 return false; 5727 if (off % size != 0) 5728 return false; 5729 5730 return true; 5731 } 5732 5733 static u32 bpf_convert_ctx_access(enum bpf_access_type type, 5734 const struct bpf_insn *si, 5735 struct bpf_insn *insn_buf, 5736 struct bpf_prog *prog, u32 *target_size) 5737 { 5738 struct bpf_insn *insn = insn_buf; 5739 int off; 5740 5741 switch (si->off) { 5742 case offsetof(struct __sk_buff, len): 5743 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 5744 bpf_target_off(struct sk_buff, len, 4, 5745 target_size)); 5746 break; 5747 5748 case offsetof(struct __sk_buff, protocol): 5749 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, 5750 bpf_target_off(struct sk_buff, protocol, 2, 5751 target_size)); 5752 break; 5753 5754 case offsetof(struct __sk_buff, vlan_proto): 5755 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, 5756 bpf_target_off(struct sk_buff, vlan_proto, 2, 5757 target_size)); 5758 break; 5759 5760 case offsetof(struct __sk_buff, priority): 5761 if (type == BPF_WRITE) 5762 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg, 5763 bpf_target_off(struct sk_buff, priority, 4, 5764 target_size)); 5765 else 5766 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 5767 bpf_target_off(struct sk_buff, priority, 4, 5768 target_size)); 5769 break; 5770 5771 case offsetof(struct __sk_buff, ingress_ifindex): 5772 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 5773 bpf_target_off(struct sk_buff, skb_iif, 4, 5774 target_size)); 5775 break; 5776 5777 case offsetof(struct __sk_buff, ifindex): 5778 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev), 5779 si->dst_reg, si->src_reg, 5780 offsetof(struct sk_buff, dev)); 5781 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1); 5782 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 5783 bpf_target_off(struct net_device, ifindex, 4, 5784 target_size)); 5785 break; 5786 5787 case offsetof(struct __sk_buff, hash): 5788 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 5789 bpf_target_off(struct sk_buff, hash, 4, 5790 target_size)); 5791 break; 5792 5793 case offsetof(struct __sk_buff, mark): 5794 if (type == BPF_WRITE) 5795 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg, 5796 bpf_target_off(struct sk_buff, mark, 4, 5797 target_size)); 5798 else 5799 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 5800 bpf_target_off(struct sk_buff, mark, 4, 5801 target_size)); 5802 break; 5803 5804 case offsetof(struct __sk_buff, pkt_type): 5805 *target_size = 1; 5806 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg, 5807 PKT_TYPE_OFFSET()); 5808 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX); 5809 #ifdef __BIG_ENDIAN_BITFIELD 5810 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5); 5811 #endif 5812 break; 5813 5814 case offsetof(struct __sk_buff, queue_mapping): 5815 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, 5816 bpf_target_off(struct sk_buff, queue_mapping, 2, 5817 target_size)); 5818 break; 5819 5820 case offsetof(struct __sk_buff, vlan_present): 5821 case offsetof(struct __sk_buff, vlan_tci): 5822 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000); 5823 5824 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, 5825 bpf_target_off(struct sk_buff, vlan_tci, 2, 5826 target_size)); 5827 if (si->off == offsetof(struct __sk_buff, vlan_tci)) { 5828 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 5829 ~VLAN_TAG_PRESENT); 5830 } else { 5831 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 12); 5832 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1); 5833 } 5834 break; 5835 5836 case offsetof(struct __sk_buff, cb[0]) ... 5837 offsetofend(struct __sk_buff, cb[4]) - 1: 5838 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20); 5839 BUILD_BUG_ON((offsetof(struct sk_buff, cb) + 5840 offsetof(struct qdisc_skb_cb, data)) % 5841 sizeof(__u64)); 5842 5843 prog->cb_access = 1; 5844 off = si->off; 5845 off -= offsetof(struct __sk_buff, cb[0]); 5846 off += offsetof(struct sk_buff, cb); 5847 off += offsetof(struct qdisc_skb_cb, data); 5848 if (type == BPF_WRITE) 5849 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg, 5850 si->src_reg, off); 5851 else 5852 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg, 5853 si->src_reg, off); 5854 break; 5855 5856 case offsetof(struct __sk_buff, tc_classid): 5857 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, tc_classid) != 2); 5858 5859 off = si->off; 5860 off -= offsetof(struct __sk_buff, tc_classid); 5861 off += offsetof(struct sk_buff, cb); 5862 off += offsetof(struct qdisc_skb_cb, tc_classid); 5863 *target_size = 2; 5864 if (type == BPF_WRITE) 5865 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, 5866 si->src_reg, off); 5867 else 5868 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, 5869 si->src_reg, off); 5870 break; 5871 5872 case offsetof(struct __sk_buff, data): 5873 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data), 5874 si->dst_reg, si->src_reg, 5875 offsetof(struct sk_buff, data)); 5876 break; 5877 5878 case offsetof(struct __sk_buff, data_meta): 5879 off = si->off; 5880 off -= offsetof(struct __sk_buff, data_meta); 5881 off += offsetof(struct sk_buff, cb); 5882 off += offsetof(struct bpf_skb_data_end, data_meta); 5883 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, 5884 si->src_reg, off); 5885 break; 5886 5887 case offsetof(struct __sk_buff, data_end): 5888 off = si->off; 5889 off -= offsetof(struct __sk_buff, data_end); 5890 off += offsetof(struct sk_buff, cb); 5891 off += offsetof(struct bpf_skb_data_end, data_end); 5892 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, 5893 si->src_reg, off); 5894 break; 5895 5896 case offsetof(struct __sk_buff, tc_index): 5897 #ifdef CONFIG_NET_SCHED 5898 if (type == BPF_WRITE) 5899 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg, 5900 bpf_target_off(struct sk_buff, tc_index, 2, 5901 target_size)); 5902 else 5903 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, 5904 bpf_target_off(struct sk_buff, tc_index, 2, 5905 target_size)); 5906 #else 5907 *target_size = 2; 5908 if (type == BPF_WRITE) 5909 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg); 5910 else 5911 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0); 5912 #endif 5913 break; 5914 5915 case offsetof(struct __sk_buff, napi_id): 5916 #if defined(CONFIG_NET_RX_BUSY_POLL) 5917 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 5918 bpf_target_off(struct sk_buff, napi_id, 4, 5919 target_size)); 5920 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1); 5921 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0); 5922 #else 5923 *target_size = 4; 5924 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0); 5925 #endif 5926 break; 5927 case offsetof(struct __sk_buff, family): 5928 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2); 5929 5930 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), 5931 si->dst_reg, si->src_reg, 5932 offsetof(struct sk_buff, sk)); 5933 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 5934 bpf_target_off(struct sock_common, 5935 skc_family, 5936 2, target_size)); 5937 break; 5938 case offsetof(struct __sk_buff, remote_ip4): 5939 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4); 5940 5941 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), 5942 si->dst_reg, si->src_reg, 5943 offsetof(struct sk_buff, sk)); 5944 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 5945 bpf_target_off(struct sock_common, 5946 skc_daddr, 5947 4, target_size)); 5948 break; 5949 case offsetof(struct __sk_buff, local_ip4): 5950 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, 5951 skc_rcv_saddr) != 4); 5952 5953 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), 5954 si->dst_reg, si->src_reg, 5955 offsetof(struct sk_buff, sk)); 5956 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 5957 bpf_target_off(struct sock_common, 5958 skc_rcv_saddr, 5959 4, target_size)); 5960 break; 5961 case offsetof(struct __sk_buff, remote_ip6[0]) ... 5962 offsetof(struct __sk_buff, remote_ip6[3]): 5963 #if IS_ENABLED(CONFIG_IPV6) 5964 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, 5965 skc_v6_daddr.s6_addr32[0]) != 4); 5966 5967 off = si->off; 5968 off -= offsetof(struct __sk_buff, remote_ip6[0]); 5969 5970 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), 5971 si->dst_reg, si->src_reg, 5972 offsetof(struct sk_buff, sk)); 5973 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 5974 offsetof(struct sock_common, 5975 skc_v6_daddr.s6_addr32[0]) + 5976 off); 5977 #else 5978 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 5979 #endif 5980 break; 5981 case offsetof(struct __sk_buff, local_ip6[0]) ... 5982 offsetof(struct __sk_buff, local_ip6[3]): 5983 #if IS_ENABLED(CONFIG_IPV6) 5984 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, 5985 skc_v6_rcv_saddr.s6_addr32[0]) != 4); 5986 5987 off = si->off; 5988 off -= offsetof(struct __sk_buff, local_ip6[0]); 5989 5990 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), 5991 si->dst_reg, si->src_reg, 5992 offsetof(struct sk_buff, sk)); 5993 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 5994 offsetof(struct sock_common, 5995 skc_v6_rcv_saddr.s6_addr32[0]) + 5996 off); 5997 #else 5998 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 5999 #endif 6000 break; 6001 6002 case offsetof(struct __sk_buff, remote_port): 6003 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2); 6004 6005 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), 6006 si->dst_reg, si->src_reg, 6007 offsetof(struct sk_buff, sk)); 6008 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 6009 bpf_target_off(struct sock_common, 6010 skc_dport, 6011 2, target_size)); 6012 #ifndef __BIG_ENDIAN_BITFIELD 6013 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16); 6014 #endif 6015 break; 6016 6017 case offsetof(struct __sk_buff, local_port): 6018 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2); 6019 6020 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), 6021 si->dst_reg, si->src_reg, 6022 offsetof(struct sk_buff, sk)); 6023 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 6024 bpf_target_off(struct sock_common, 6025 skc_num, 2, target_size)); 6026 break; 6027 } 6028 6029 return insn - insn_buf; 6030 } 6031 6032 static u32 sock_filter_convert_ctx_access(enum bpf_access_type type, 6033 const struct bpf_insn *si, 6034 struct bpf_insn *insn_buf, 6035 struct bpf_prog *prog, u32 *target_size) 6036 { 6037 struct bpf_insn *insn = insn_buf; 6038 int off; 6039 6040 switch (si->off) { 6041 case offsetof(struct bpf_sock, bound_dev_if): 6042 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_bound_dev_if) != 4); 6043 6044 if (type == BPF_WRITE) 6045 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg, 6046 offsetof(struct sock, sk_bound_dev_if)); 6047 else 6048 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 6049 offsetof(struct sock, sk_bound_dev_if)); 6050 break; 6051 6052 case offsetof(struct bpf_sock, mark): 6053 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_mark) != 4); 6054 6055 if (type == BPF_WRITE) 6056 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg, 6057 offsetof(struct sock, sk_mark)); 6058 else 6059 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 6060 offsetof(struct sock, sk_mark)); 6061 break; 6062 6063 case offsetof(struct bpf_sock, priority): 6064 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_priority) != 4); 6065 6066 if (type == BPF_WRITE) 6067 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg, 6068 offsetof(struct sock, sk_priority)); 6069 else 6070 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 6071 offsetof(struct sock, sk_priority)); 6072 break; 6073 6074 case offsetof(struct bpf_sock, family): 6075 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_family) != 2); 6076 6077 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, 6078 offsetof(struct sock, sk_family)); 6079 break; 6080 6081 case offsetof(struct bpf_sock, type): 6082 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 6083 offsetof(struct sock, __sk_flags_offset)); 6084 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK); 6085 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT); 6086 break; 6087 6088 case offsetof(struct bpf_sock, protocol): 6089 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 6090 offsetof(struct sock, __sk_flags_offset)); 6091 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK); 6092 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_PROTO_SHIFT); 6093 break; 6094 6095 case offsetof(struct bpf_sock, src_ip4): 6096 *insn++ = BPF_LDX_MEM( 6097 BPF_SIZE(si->code), si->dst_reg, si->src_reg, 6098 bpf_target_off(struct sock_common, skc_rcv_saddr, 6099 FIELD_SIZEOF(struct sock_common, 6100 skc_rcv_saddr), 6101 target_size)); 6102 break; 6103 6104 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]): 6105 #if IS_ENABLED(CONFIG_IPV6) 6106 off = si->off; 6107 off -= offsetof(struct bpf_sock, src_ip6[0]); 6108 *insn++ = BPF_LDX_MEM( 6109 BPF_SIZE(si->code), si->dst_reg, si->src_reg, 6110 bpf_target_off( 6111 struct sock_common, 6112 skc_v6_rcv_saddr.s6_addr32[0], 6113 FIELD_SIZEOF(struct sock_common, 6114 skc_v6_rcv_saddr.s6_addr32[0]), 6115 target_size) + off); 6116 #else 6117 (void)off; 6118 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 6119 #endif 6120 break; 6121 6122 case offsetof(struct bpf_sock, src_port): 6123 *insn++ = BPF_LDX_MEM( 6124 BPF_FIELD_SIZEOF(struct sock_common, skc_num), 6125 si->dst_reg, si->src_reg, 6126 bpf_target_off(struct sock_common, skc_num, 6127 FIELD_SIZEOF(struct sock_common, 6128 skc_num), 6129 target_size)); 6130 break; 6131 } 6132 6133 return insn - insn_buf; 6134 } 6135 6136 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type, 6137 const struct bpf_insn *si, 6138 struct bpf_insn *insn_buf, 6139 struct bpf_prog *prog, u32 *target_size) 6140 { 6141 struct bpf_insn *insn = insn_buf; 6142 6143 switch (si->off) { 6144 case offsetof(struct __sk_buff, ifindex): 6145 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev), 6146 si->dst_reg, si->src_reg, 6147 offsetof(struct sk_buff, dev)); 6148 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 6149 bpf_target_off(struct net_device, ifindex, 4, 6150 target_size)); 6151 break; 6152 default: 6153 return bpf_convert_ctx_access(type, si, insn_buf, prog, 6154 target_size); 6155 } 6156 6157 return insn - insn_buf; 6158 } 6159 6160 static u32 xdp_convert_ctx_access(enum bpf_access_type type, 6161 const struct bpf_insn *si, 6162 struct bpf_insn *insn_buf, 6163 struct bpf_prog *prog, u32 *target_size) 6164 { 6165 struct bpf_insn *insn = insn_buf; 6166 6167 switch (si->off) { 6168 case offsetof(struct xdp_md, data): 6169 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data), 6170 si->dst_reg, si->src_reg, 6171 offsetof(struct xdp_buff, data)); 6172 break; 6173 case offsetof(struct xdp_md, data_meta): 6174 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta), 6175 si->dst_reg, si->src_reg, 6176 offsetof(struct xdp_buff, data_meta)); 6177 break; 6178 case offsetof(struct xdp_md, data_end): 6179 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end), 6180 si->dst_reg, si->src_reg, 6181 offsetof(struct xdp_buff, data_end)); 6182 break; 6183 case offsetof(struct xdp_md, ingress_ifindex): 6184 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq), 6185 si->dst_reg, si->src_reg, 6186 offsetof(struct xdp_buff, rxq)); 6187 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev), 6188 si->dst_reg, si->dst_reg, 6189 offsetof(struct xdp_rxq_info, dev)); 6190 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 6191 offsetof(struct net_device, ifindex)); 6192 break; 6193 case offsetof(struct xdp_md, rx_queue_index): 6194 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq), 6195 si->dst_reg, si->src_reg, 6196 offsetof(struct xdp_buff, rxq)); 6197 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 6198 offsetof(struct xdp_rxq_info, 6199 queue_index)); 6200 break; 6201 } 6202 6203 return insn - insn_buf; 6204 } 6205 6206 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of 6207 * context Structure, F is Field in context structure that contains a pointer 6208 * to Nested Structure of type NS that has the field NF. 6209 * 6210 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make 6211 * sure that SIZE is not greater than actual size of S.F.NF. 6212 * 6213 * If offset OFF is provided, the load happens from that offset relative to 6214 * offset of NF. 6215 */ 6216 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \ 6217 do { \ 6218 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \ 6219 si->src_reg, offsetof(S, F)); \ 6220 *insn++ = BPF_LDX_MEM( \ 6221 SIZE, si->dst_reg, si->dst_reg, \ 6222 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \ 6223 target_size) \ 6224 + OFF); \ 6225 } while (0) 6226 6227 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \ 6228 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \ 6229 BPF_FIELD_SIZEOF(NS, NF), 0) 6230 6231 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to 6232 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation. 6233 * 6234 * It doesn't support SIZE argument though since narrow stores are not 6235 * supported for now. 6236 * 6237 * In addition it uses Temporary Field TF (member of struct S) as the 3rd 6238 * "register" since two registers available in convert_ctx_access are not 6239 * enough: we can't override neither SRC, since it contains value to store, nor 6240 * DST since it contains pointer to context that may be used by later 6241 * instructions. But we need a temporary place to save pointer to nested 6242 * structure whose field we want to store to. 6243 */ 6244 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, TF) \ 6245 do { \ 6246 int tmp_reg = BPF_REG_9; \ 6247 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \ 6248 --tmp_reg; \ 6249 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \ 6250 --tmp_reg; \ 6251 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \ 6252 offsetof(S, TF)); \ 6253 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \ 6254 si->dst_reg, offsetof(S, F)); \ 6255 *insn++ = BPF_STX_MEM( \ 6256 BPF_FIELD_SIZEOF(NS, NF), tmp_reg, si->src_reg, \ 6257 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \ 6258 target_size) \ 6259 + OFF); \ 6260 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \ 6261 offsetof(S, TF)); \ 6262 } while (0) 6263 6264 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \ 6265 TF) \ 6266 do { \ 6267 if (type == BPF_WRITE) { \ 6268 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, \ 6269 TF); \ 6270 } else { \ 6271 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \ 6272 S, NS, F, NF, SIZE, OFF); \ 6273 } \ 6274 } while (0) 6275 6276 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \ 6277 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \ 6278 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF) 6279 6280 static u32 sock_addr_convert_ctx_access(enum bpf_access_type type, 6281 const struct bpf_insn *si, 6282 struct bpf_insn *insn_buf, 6283 struct bpf_prog *prog, u32 *target_size) 6284 { 6285 struct bpf_insn *insn = insn_buf; 6286 int off; 6287 6288 switch (si->off) { 6289 case offsetof(struct bpf_sock_addr, user_family): 6290 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern, 6291 struct sockaddr, uaddr, sa_family); 6292 break; 6293 6294 case offsetof(struct bpf_sock_addr, user_ip4): 6295 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( 6296 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr, 6297 sin_addr, BPF_SIZE(si->code), 0, tmp_reg); 6298 break; 6299 6300 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]): 6301 off = si->off; 6302 off -= offsetof(struct bpf_sock_addr, user_ip6[0]); 6303 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( 6304 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr, 6305 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off, 6306 tmp_reg); 6307 break; 6308 6309 case offsetof(struct bpf_sock_addr, user_port): 6310 /* To get port we need to know sa_family first and then treat 6311 * sockaddr as either sockaddr_in or sockaddr_in6. 6312 * Though we can simplify since port field has same offset and 6313 * size in both structures. 6314 * Here we check this invariant and use just one of the 6315 * structures if it's true. 6316 */ 6317 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) != 6318 offsetof(struct sockaddr_in6, sin6_port)); 6319 BUILD_BUG_ON(FIELD_SIZEOF(struct sockaddr_in, sin_port) != 6320 FIELD_SIZEOF(struct sockaddr_in6, sin6_port)); 6321 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(struct bpf_sock_addr_kern, 6322 struct sockaddr_in6, uaddr, 6323 sin6_port, tmp_reg); 6324 break; 6325 6326 case offsetof(struct bpf_sock_addr, family): 6327 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern, 6328 struct sock, sk, sk_family); 6329 break; 6330 6331 case offsetof(struct bpf_sock_addr, type): 6332 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( 6333 struct bpf_sock_addr_kern, struct sock, sk, 6334 __sk_flags_offset, BPF_W, 0); 6335 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK); 6336 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT); 6337 break; 6338 6339 case offsetof(struct bpf_sock_addr, protocol): 6340 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( 6341 struct bpf_sock_addr_kern, struct sock, sk, 6342 __sk_flags_offset, BPF_W, 0); 6343 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK); 6344 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 6345 SK_FL_PROTO_SHIFT); 6346 break; 6347 6348 case offsetof(struct bpf_sock_addr, msg_src_ip4): 6349 /* Treat t_ctx as struct in_addr for msg_src_ip4. */ 6350 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( 6351 struct bpf_sock_addr_kern, struct in_addr, t_ctx, 6352 s_addr, BPF_SIZE(si->code), 0, tmp_reg); 6353 break; 6354 6355 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0], 6356 msg_src_ip6[3]): 6357 off = si->off; 6358 off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]); 6359 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */ 6360 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( 6361 struct bpf_sock_addr_kern, struct in6_addr, t_ctx, 6362 s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg); 6363 break; 6364 } 6365 6366 return insn - insn_buf; 6367 } 6368 6369 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type, 6370 const struct bpf_insn *si, 6371 struct bpf_insn *insn_buf, 6372 struct bpf_prog *prog, 6373 u32 *target_size) 6374 { 6375 struct bpf_insn *insn = insn_buf; 6376 int off; 6377 6378 switch (si->off) { 6379 case offsetof(struct bpf_sock_ops, op) ... 6380 offsetof(struct bpf_sock_ops, replylong[3]): 6381 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, op) != 6382 FIELD_SIZEOF(struct bpf_sock_ops_kern, op)); 6383 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, reply) != 6384 FIELD_SIZEOF(struct bpf_sock_ops_kern, reply)); 6385 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, replylong) != 6386 FIELD_SIZEOF(struct bpf_sock_ops_kern, replylong)); 6387 off = si->off; 6388 off -= offsetof(struct bpf_sock_ops, op); 6389 off += offsetof(struct bpf_sock_ops_kern, op); 6390 if (type == BPF_WRITE) 6391 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg, 6392 off); 6393 else 6394 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 6395 off); 6396 break; 6397 6398 case offsetof(struct bpf_sock_ops, family): 6399 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2); 6400 6401 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 6402 struct bpf_sock_ops_kern, sk), 6403 si->dst_reg, si->src_reg, 6404 offsetof(struct bpf_sock_ops_kern, sk)); 6405 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 6406 offsetof(struct sock_common, skc_family)); 6407 break; 6408 6409 case offsetof(struct bpf_sock_ops, remote_ip4): 6410 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4); 6411 6412 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 6413 struct bpf_sock_ops_kern, sk), 6414 si->dst_reg, si->src_reg, 6415 offsetof(struct bpf_sock_ops_kern, sk)); 6416 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 6417 offsetof(struct sock_common, skc_daddr)); 6418 break; 6419 6420 case offsetof(struct bpf_sock_ops, local_ip4): 6421 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, 6422 skc_rcv_saddr) != 4); 6423 6424 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 6425 struct bpf_sock_ops_kern, sk), 6426 si->dst_reg, si->src_reg, 6427 offsetof(struct bpf_sock_ops_kern, sk)); 6428 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 6429 offsetof(struct sock_common, 6430 skc_rcv_saddr)); 6431 break; 6432 6433 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ... 6434 offsetof(struct bpf_sock_ops, remote_ip6[3]): 6435 #if IS_ENABLED(CONFIG_IPV6) 6436 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, 6437 skc_v6_daddr.s6_addr32[0]) != 4); 6438 6439 off = si->off; 6440 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]); 6441 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 6442 struct bpf_sock_ops_kern, sk), 6443 si->dst_reg, si->src_reg, 6444 offsetof(struct bpf_sock_ops_kern, sk)); 6445 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 6446 offsetof(struct sock_common, 6447 skc_v6_daddr.s6_addr32[0]) + 6448 off); 6449 #else 6450 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 6451 #endif 6452 break; 6453 6454 case offsetof(struct bpf_sock_ops, local_ip6[0]) ... 6455 offsetof(struct bpf_sock_ops, local_ip6[3]): 6456 #if IS_ENABLED(CONFIG_IPV6) 6457 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, 6458 skc_v6_rcv_saddr.s6_addr32[0]) != 4); 6459 6460 off = si->off; 6461 off -= offsetof(struct bpf_sock_ops, local_ip6[0]); 6462 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 6463 struct bpf_sock_ops_kern, sk), 6464 si->dst_reg, si->src_reg, 6465 offsetof(struct bpf_sock_ops_kern, sk)); 6466 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 6467 offsetof(struct sock_common, 6468 skc_v6_rcv_saddr.s6_addr32[0]) + 6469 off); 6470 #else 6471 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 6472 #endif 6473 break; 6474 6475 case offsetof(struct bpf_sock_ops, remote_port): 6476 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2); 6477 6478 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 6479 struct bpf_sock_ops_kern, sk), 6480 si->dst_reg, si->src_reg, 6481 offsetof(struct bpf_sock_ops_kern, sk)); 6482 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 6483 offsetof(struct sock_common, skc_dport)); 6484 #ifndef __BIG_ENDIAN_BITFIELD 6485 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16); 6486 #endif 6487 break; 6488 6489 case offsetof(struct bpf_sock_ops, local_port): 6490 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2); 6491 6492 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 6493 struct bpf_sock_ops_kern, sk), 6494 si->dst_reg, si->src_reg, 6495 offsetof(struct bpf_sock_ops_kern, sk)); 6496 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 6497 offsetof(struct sock_common, skc_num)); 6498 break; 6499 6500 case offsetof(struct bpf_sock_ops, is_fullsock): 6501 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 6502 struct bpf_sock_ops_kern, 6503 is_fullsock), 6504 si->dst_reg, si->src_reg, 6505 offsetof(struct bpf_sock_ops_kern, 6506 is_fullsock)); 6507 break; 6508 6509 case offsetof(struct bpf_sock_ops, state): 6510 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_state) != 1); 6511 6512 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 6513 struct bpf_sock_ops_kern, sk), 6514 si->dst_reg, si->src_reg, 6515 offsetof(struct bpf_sock_ops_kern, sk)); 6516 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg, 6517 offsetof(struct sock_common, skc_state)); 6518 break; 6519 6520 case offsetof(struct bpf_sock_ops, rtt_min): 6521 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, rtt_min) != 6522 sizeof(struct minmax)); 6523 BUILD_BUG_ON(sizeof(struct minmax) < 6524 sizeof(struct minmax_sample)); 6525 6526 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 6527 struct bpf_sock_ops_kern, sk), 6528 si->dst_reg, si->src_reg, 6529 offsetof(struct bpf_sock_ops_kern, sk)); 6530 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 6531 offsetof(struct tcp_sock, rtt_min) + 6532 FIELD_SIZEOF(struct minmax_sample, t)); 6533 break; 6534 6535 /* Helper macro for adding read access to tcp_sock or sock fields. */ 6536 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \ 6537 do { \ 6538 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \ 6539 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \ 6540 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ 6541 struct bpf_sock_ops_kern, \ 6542 is_fullsock), \ 6543 si->dst_reg, si->src_reg, \ 6544 offsetof(struct bpf_sock_ops_kern, \ 6545 is_fullsock)); \ 6546 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 2); \ 6547 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ 6548 struct bpf_sock_ops_kern, sk),\ 6549 si->dst_reg, si->src_reg, \ 6550 offsetof(struct bpf_sock_ops_kern, sk));\ 6551 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \ 6552 OBJ_FIELD), \ 6553 si->dst_reg, si->dst_reg, \ 6554 offsetof(OBJ, OBJ_FIELD)); \ 6555 } while (0) 6556 6557 /* Helper macro for adding write access to tcp_sock or sock fields. 6558 * The macro is called with two registers, dst_reg which contains a pointer 6559 * to ctx (context) and src_reg which contains the value that should be 6560 * stored. However, we need an additional register since we cannot overwrite 6561 * dst_reg because it may be used later in the program. 6562 * Instead we "borrow" one of the other register. We first save its value 6563 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore 6564 * it at the end of the macro. 6565 */ 6566 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \ 6567 do { \ 6568 int reg = BPF_REG_9; \ 6569 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \ 6570 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \ 6571 if (si->dst_reg == reg || si->src_reg == reg) \ 6572 reg--; \ 6573 if (si->dst_reg == reg || si->src_reg == reg) \ 6574 reg--; \ 6575 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \ 6576 offsetof(struct bpf_sock_ops_kern, \ 6577 temp)); \ 6578 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ 6579 struct bpf_sock_ops_kern, \ 6580 is_fullsock), \ 6581 reg, si->dst_reg, \ 6582 offsetof(struct bpf_sock_ops_kern, \ 6583 is_fullsock)); \ 6584 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \ 6585 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ 6586 struct bpf_sock_ops_kern, sk),\ 6587 reg, si->dst_reg, \ 6588 offsetof(struct bpf_sock_ops_kern, sk));\ 6589 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \ 6590 reg, si->src_reg, \ 6591 offsetof(OBJ, OBJ_FIELD)); \ 6592 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \ 6593 offsetof(struct bpf_sock_ops_kern, \ 6594 temp)); \ 6595 } while (0) 6596 6597 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \ 6598 do { \ 6599 if (TYPE == BPF_WRITE) \ 6600 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \ 6601 else \ 6602 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \ 6603 } while (0) 6604 6605 case offsetof(struct bpf_sock_ops, snd_cwnd): 6606 SOCK_OPS_GET_FIELD(snd_cwnd, snd_cwnd, struct tcp_sock); 6607 break; 6608 6609 case offsetof(struct bpf_sock_ops, srtt_us): 6610 SOCK_OPS_GET_FIELD(srtt_us, srtt_us, struct tcp_sock); 6611 break; 6612 6613 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags): 6614 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags, 6615 struct tcp_sock); 6616 break; 6617 6618 case offsetof(struct bpf_sock_ops, snd_ssthresh): 6619 SOCK_OPS_GET_FIELD(snd_ssthresh, snd_ssthresh, struct tcp_sock); 6620 break; 6621 6622 case offsetof(struct bpf_sock_ops, rcv_nxt): 6623 SOCK_OPS_GET_FIELD(rcv_nxt, rcv_nxt, struct tcp_sock); 6624 break; 6625 6626 case offsetof(struct bpf_sock_ops, snd_nxt): 6627 SOCK_OPS_GET_FIELD(snd_nxt, snd_nxt, struct tcp_sock); 6628 break; 6629 6630 case offsetof(struct bpf_sock_ops, snd_una): 6631 SOCK_OPS_GET_FIELD(snd_una, snd_una, struct tcp_sock); 6632 break; 6633 6634 case offsetof(struct bpf_sock_ops, mss_cache): 6635 SOCK_OPS_GET_FIELD(mss_cache, mss_cache, struct tcp_sock); 6636 break; 6637 6638 case offsetof(struct bpf_sock_ops, ecn_flags): 6639 SOCK_OPS_GET_FIELD(ecn_flags, ecn_flags, struct tcp_sock); 6640 break; 6641 6642 case offsetof(struct bpf_sock_ops, rate_delivered): 6643 SOCK_OPS_GET_FIELD(rate_delivered, rate_delivered, 6644 struct tcp_sock); 6645 break; 6646 6647 case offsetof(struct bpf_sock_ops, rate_interval_us): 6648 SOCK_OPS_GET_FIELD(rate_interval_us, rate_interval_us, 6649 struct tcp_sock); 6650 break; 6651 6652 case offsetof(struct bpf_sock_ops, packets_out): 6653 SOCK_OPS_GET_FIELD(packets_out, packets_out, struct tcp_sock); 6654 break; 6655 6656 case offsetof(struct bpf_sock_ops, retrans_out): 6657 SOCK_OPS_GET_FIELD(retrans_out, retrans_out, struct tcp_sock); 6658 break; 6659 6660 case offsetof(struct bpf_sock_ops, total_retrans): 6661 SOCK_OPS_GET_FIELD(total_retrans, total_retrans, 6662 struct tcp_sock); 6663 break; 6664 6665 case offsetof(struct bpf_sock_ops, segs_in): 6666 SOCK_OPS_GET_FIELD(segs_in, segs_in, struct tcp_sock); 6667 break; 6668 6669 case offsetof(struct bpf_sock_ops, data_segs_in): 6670 SOCK_OPS_GET_FIELD(data_segs_in, data_segs_in, struct tcp_sock); 6671 break; 6672 6673 case offsetof(struct bpf_sock_ops, segs_out): 6674 SOCK_OPS_GET_FIELD(segs_out, segs_out, struct tcp_sock); 6675 break; 6676 6677 case offsetof(struct bpf_sock_ops, data_segs_out): 6678 SOCK_OPS_GET_FIELD(data_segs_out, data_segs_out, 6679 struct tcp_sock); 6680 break; 6681 6682 case offsetof(struct bpf_sock_ops, lost_out): 6683 SOCK_OPS_GET_FIELD(lost_out, lost_out, struct tcp_sock); 6684 break; 6685 6686 case offsetof(struct bpf_sock_ops, sacked_out): 6687 SOCK_OPS_GET_FIELD(sacked_out, sacked_out, struct tcp_sock); 6688 break; 6689 6690 case offsetof(struct bpf_sock_ops, sk_txhash): 6691 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash, 6692 struct sock, type); 6693 break; 6694 6695 case offsetof(struct bpf_sock_ops, bytes_received): 6696 SOCK_OPS_GET_FIELD(bytes_received, bytes_received, 6697 struct tcp_sock); 6698 break; 6699 6700 case offsetof(struct bpf_sock_ops, bytes_acked): 6701 SOCK_OPS_GET_FIELD(bytes_acked, bytes_acked, struct tcp_sock); 6702 break; 6703 6704 } 6705 return insn - insn_buf; 6706 } 6707 6708 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type, 6709 const struct bpf_insn *si, 6710 struct bpf_insn *insn_buf, 6711 struct bpf_prog *prog, u32 *target_size) 6712 { 6713 struct bpf_insn *insn = insn_buf; 6714 int off; 6715 6716 switch (si->off) { 6717 case offsetof(struct __sk_buff, data_end): 6718 off = si->off; 6719 off -= offsetof(struct __sk_buff, data_end); 6720 off += offsetof(struct sk_buff, cb); 6721 off += offsetof(struct tcp_skb_cb, bpf.data_end); 6722 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, 6723 si->src_reg, off); 6724 break; 6725 default: 6726 return bpf_convert_ctx_access(type, si, insn_buf, prog, 6727 target_size); 6728 } 6729 6730 return insn - insn_buf; 6731 } 6732 6733 static u32 sk_msg_convert_ctx_access(enum bpf_access_type type, 6734 const struct bpf_insn *si, 6735 struct bpf_insn *insn_buf, 6736 struct bpf_prog *prog, u32 *target_size) 6737 { 6738 struct bpf_insn *insn = insn_buf; 6739 #if IS_ENABLED(CONFIG_IPV6) 6740 int off; 6741 #endif 6742 6743 switch (si->off) { 6744 case offsetof(struct sk_msg_md, data): 6745 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_buff, data), 6746 si->dst_reg, si->src_reg, 6747 offsetof(struct sk_msg_buff, data)); 6748 break; 6749 case offsetof(struct sk_msg_md, data_end): 6750 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_buff, data_end), 6751 si->dst_reg, si->src_reg, 6752 offsetof(struct sk_msg_buff, data_end)); 6753 break; 6754 case offsetof(struct sk_msg_md, family): 6755 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2); 6756 6757 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 6758 struct sk_msg_buff, sk), 6759 si->dst_reg, si->src_reg, 6760 offsetof(struct sk_msg_buff, sk)); 6761 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 6762 offsetof(struct sock_common, skc_family)); 6763 break; 6764 6765 case offsetof(struct sk_msg_md, remote_ip4): 6766 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4); 6767 6768 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 6769 struct sk_msg_buff, sk), 6770 si->dst_reg, si->src_reg, 6771 offsetof(struct sk_msg_buff, sk)); 6772 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 6773 offsetof(struct sock_common, skc_daddr)); 6774 break; 6775 6776 case offsetof(struct sk_msg_md, local_ip4): 6777 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, 6778 skc_rcv_saddr) != 4); 6779 6780 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 6781 struct sk_msg_buff, sk), 6782 si->dst_reg, si->src_reg, 6783 offsetof(struct sk_msg_buff, sk)); 6784 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 6785 offsetof(struct sock_common, 6786 skc_rcv_saddr)); 6787 break; 6788 6789 case offsetof(struct sk_msg_md, remote_ip6[0]) ... 6790 offsetof(struct sk_msg_md, remote_ip6[3]): 6791 #if IS_ENABLED(CONFIG_IPV6) 6792 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, 6793 skc_v6_daddr.s6_addr32[0]) != 4); 6794 6795 off = si->off; 6796 off -= offsetof(struct sk_msg_md, remote_ip6[0]); 6797 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 6798 struct sk_msg_buff, sk), 6799 si->dst_reg, si->src_reg, 6800 offsetof(struct sk_msg_buff, sk)); 6801 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 6802 offsetof(struct sock_common, 6803 skc_v6_daddr.s6_addr32[0]) + 6804 off); 6805 #else 6806 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 6807 #endif 6808 break; 6809 6810 case offsetof(struct sk_msg_md, local_ip6[0]) ... 6811 offsetof(struct sk_msg_md, local_ip6[3]): 6812 #if IS_ENABLED(CONFIG_IPV6) 6813 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, 6814 skc_v6_rcv_saddr.s6_addr32[0]) != 4); 6815 6816 off = si->off; 6817 off -= offsetof(struct sk_msg_md, local_ip6[0]); 6818 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 6819 struct sk_msg_buff, sk), 6820 si->dst_reg, si->src_reg, 6821 offsetof(struct sk_msg_buff, sk)); 6822 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 6823 offsetof(struct sock_common, 6824 skc_v6_rcv_saddr.s6_addr32[0]) + 6825 off); 6826 #else 6827 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 6828 #endif 6829 break; 6830 6831 case offsetof(struct sk_msg_md, remote_port): 6832 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2); 6833 6834 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 6835 struct sk_msg_buff, sk), 6836 si->dst_reg, si->src_reg, 6837 offsetof(struct sk_msg_buff, sk)); 6838 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 6839 offsetof(struct sock_common, skc_dport)); 6840 #ifndef __BIG_ENDIAN_BITFIELD 6841 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16); 6842 #endif 6843 break; 6844 6845 case offsetof(struct sk_msg_md, local_port): 6846 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2); 6847 6848 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 6849 struct sk_msg_buff, sk), 6850 si->dst_reg, si->src_reg, 6851 offsetof(struct sk_msg_buff, sk)); 6852 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 6853 offsetof(struct sock_common, skc_num)); 6854 break; 6855 } 6856 6857 return insn - insn_buf; 6858 } 6859 6860 const struct bpf_verifier_ops sk_filter_verifier_ops = { 6861 .get_func_proto = sk_filter_func_proto, 6862 .is_valid_access = sk_filter_is_valid_access, 6863 .convert_ctx_access = bpf_convert_ctx_access, 6864 .gen_ld_abs = bpf_gen_ld_abs, 6865 }; 6866 6867 const struct bpf_prog_ops sk_filter_prog_ops = { 6868 .test_run = bpf_prog_test_run_skb, 6869 }; 6870 6871 const struct bpf_verifier_ops tc_cls_act_verifier_ops = { 6872 .get_func_proto = tc_cls_act_func_proto, 6873 .is_valid_access = tc_cls_act_is_valid_access, 6874 .convert_ctx_access = tc_cls_act_convert_ctx_access, 6875 .gen_prologue = tc_cls_act_prologue, 6876 .gen_ld_abs = bpf_gen_ld_abs, 6877 }; 6878 6879 const struct bpf_prog_ops tc_cls_act_prog_ops = { 6880 .test_run = bpf_prog_test_run_skb, 6881 }; 6882 6883 const struct bpf_verifier_ops xdp_verifier_ops = { 6884 .get_func_proto = xdp_func_proto, 6885 .is_valid_access = xdp_is_valid_access, 6886 .convert_ctx_access = xdp_convert_ctx_access, 6887 }; 6888 6889 const struct bpf_prog_ops xdp_prog_ops = { 6890 .test_run = bpf_prog_test_run_xdp, 6891 }; 6892 6893 const struct bpf_verifier_ops cg_skb_verifier_ops = { 6894 .get_func_proto = cg_skb_func_proto, 6895 .is_valid_access = sk_filter_is_valid_access, 6896 .convert_ctx_access = bpf_convert_ctx_access, 6897 }; 6898 6899 const struct bpf_prog_ops cg_skb_prog_ops = { 6900 .test_run = bpf_prog_test_run_skb, 6901 }; 6902 6903 const struct bpf_verifier_ops lwt_in_verifier_ops = { 6904 .get_func_proto = lwt_in_func_proto, 6905 .is_valid_access = lwt_is_valid_access, 6906 .convert_ctx_access = bpf_convert_ctx_access, 6907 }; 6908 6909 const struct bpf_prog_ops lwt_in_prog_ops = { 6910 .test_run = bpf_prog_test_run_skb, 6911 }; 6912 6913 const struct bpf_verifier_ops lwt_out_verifier_ops = { 6914 .get_func_proto = lwt_out_func_proto, 6915 .is_valid_access = lwt_is_valid_access, 6916 .convert_ctx_access = bpf_convert_ctx_access, 6917 }; 6918 6919 const struct bpf_prog_ops lwt_out_prog_ops = { 6920 .test_run = bpf_prog_test_run_skb, 6921 }; 6922 6923 const struct bpf_verifier_ops lwt_xmit_verifier_ops = { 6924 .get_func_proto = lwt_xmit_func_proto, 6925 .is_valid_access = lwt_is_valid_access, 6926 .convert_ctx_access = bpf_convert_ctx_access, 6927 .gen_prologue = tc_cls_act_prologue, 6928 }; 6929 6930 const struct bpf_prog_ops lwt_xmit_prog_ops = { 6931 .test_run = bpf_prog_test_run_skb, 6932 }; 6933 6934 const struct bpf_verifier_ops lwt_seg6local_verifier_ops = { 6935 .get_func_proto = lwt_seg6local_func_proto, 6936 .is_valid_access = lwt_is_valid_access, 6937 .convert_ctx_access = bpf_convert_ctx_access, 6938 }; 6939 6940 const struct bpf_prog_ops lwt_seg6local_prog_ops = { 6941 .test_run = bpf_prog_test_run_skb, 6942 }; 6943 6944 const struct bpf_verifier_ops cg_sock_verifier_ops = { 6945 .get_func_proto = sock_filter_func_proto, 6946 .is_valid_access = sock_filter_is_valid_access, 6947 .convert_ctx_access = sock_filter_convert_ctx_access, 6948 }; 6949 6950 const struct bpf_prog_ops cg_sock_prog_ops = { 6951 }; 6952 6953 const struct bpf_verifier_ops cg_sock_addr_verifier_ops = { 6954 .get_func_proto = sock_addr_func_proto, 6955 .is_valid_access = sock_addr_is_valid_access, 6956 .convert_ctx_access = sock_addr_convert_ctx_access, 6957 }; 6958 6959 const struct bpf_prog_ops cg_sock_addr_prog_ops = { 6960 }; 6961 6962 const struct bpf_verifier_ops sock_ops_verifier_ops = { 6963 .get_func_proto = sock_ops_func_proto, 6964 .is_valid_access = sock_ops_is_valid_access, 6965 .convert_ctx_access = sock_ops_convert_ctx_access, 6966 }; 6967 6968 const struct bpf_prog_ops sock_ops_prog_ops = { 6969 }; 6970 6971 const struct bpf_verifier_ops sk_skb_verifier_ops = { 6972 .get_func_proto = sk_skb_func_proto, 6973 .is_valid_access = sk_skb_is_valid_access, 6974 .convert_ctx_access = sk_skb_convert_ctx_access, 6975 .gen_prologue = sk_skb_prologue, 6976 }; 6977 6978 const struct bpf_prog_ops sk_skb_prog_ops = { 6979 }; 6980 6981 const struct bpf_verifier_ops sk_msg_verifier_ops = { 6982 .get_func_proto = sk_msg_func_proto, 6983 .is_valid_access = sk_msg_is_valid_access, 6984 .convert_ctx_access = sk_msg_convert_ctx_access, 6985 }; 6986 6987 const struct bpf_prog_ops sk_msg_prog_ops = { 6988 }; 6989 6990 int sk_detach_filter(struct sock *sk) 6991 { 6992 int ret = -ENOENT; 6993 struct sk_filter *filter; 6994 6995 if (sock_flag(sk, SOCK_FILTER_LOCKED)) 6996 return -EPERM; 6997 6998 filter = rcu_dereference_protected(sk->sk_filter, 6999 lockdep_sock_is_held(sk)); 7000 if (filter) { 7001 RCU_INIT_POINTER(sk->sk_filter, NULL); 7002 sk_filter_uncharge(sk, filter); 7003 ret = 0; 7004 } 7005 7006 return ret; 7007 } 7008 EXPORT_SYMBOL_GPL(sk_detach_filter); 7009 7010 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf, 7011 unsigned int len) 7012 { 7013 struct sock_fprog_kern *fprog; 7014 struct sk_filter *filter; 7015 int ret = 0; 7016 7017 lock_sock(sk); 7018 filter = rcu_dereference_protected(sk->sk_filter, 7019 lockdep_sock_is_held(sk)); 7020 if (!filter) 7021 goto out; 7022 7023 /* We're copying the filter that has been originally attached, 7024 * so no conversion/decode needed anymore. eBPF programs that 7025 * have no original program cannot be dumped through this. 7026 */ 7027 ret = -EACCES; 7028 fprog = filter->prog->orig_prog; 7029 if (!fprog) 7030 goto out; 7031 7032 ret = fprog->len; 7033 if (!len) 7034 /* User space only enquires number of filter blocks. */ 7035 goto out; 7036 7037 ret = -EINVAL; 7038 if (len < fprog->len) 7039 goto out; 7040 7041 ret = -EFAULT; 7042 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog))) 7043 goto out; 7044 7045 /* Instead of bytes, the API requests to return the number 7046 * of filter blocks. 7047 */ 7048 ret = fprog->len; 7049 out: 7050 release_sock(sk); 7051 return ret; 7052 } 7053 7054 #ifdef CONFIG_INET 7055 struct sk_reuseport_kern { 7056 struct sk_buff *skb; 7057 struct sock *sk; 7058 struct sock *selected_sk; 7059 void *data_end; 7060 u32 hash; 7061 u32 reuseport_id; 7062 bool bind_inany; 7063 }; 7064 7065 static void bpf_init_reuseport_kern(struct sk_reuseport_kern *reuse_kern, 7066 struct sock_reuseport *reuse, 7067 struct sock *sk, struct sk_buff *skb, 7068 u32 hash) 7069 { 7070 reuse_kern->skb = skb; 7071 reuse_kern->sk = sk; 7072 reuse_kern->selected_sk = NULL; 7073 reuse_kern->data_end = skb->data + skb_headlen(skb); 7074 reuse_kern->hash = hash; 7075 reuse_kern->reuseport_id = reuse->reuseport_id; 7076 reuse_kern->bind_inany = reuse->bind_inany; 7077 } 7078 7079 struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk, 7080 struct bpf_prog *prog, struct sk_buff *skb, 7081 u32 hash) 7082 { 7083 struct sk_reuseport_kern reuse_kern; 7084 enum sk_action action; 7085 7086 bpf_init_reuseport_kern(&reuse_kern, reuse, sk, skb, hash); 7087 action = BPF_PROG_RUN(prog, &reuse_kern); 7088 7089 if (action == SK_PASS) 7090 return reuse_kern.selected_sk; 7091 else 7092 return ERR_PTR(-ECONNREFUSED); 7093 } 7094 7095 BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern, 7096 struct bpf_map *, map, void *, key, u32, flags) 7097 { 7098 struct sock_reuseport *reuse; 7099 struct sock *selected_sk; 7100 7101 selected_sk = map->ops->map_lookup_elem(map, key); 7102 if (!selected_sk) 7103 return -ENOENT; 7104 7105 reuse = rcu_dereference(selected_sk->sk_reuseport_cb); 7106 if (!reuse) 7107 /* selected_sk is unhashed (e.g. by close()) after the 7108 * above map_lookup_elem(). Treat selected_sk has already 7109 * been removed from the map. 7110 */ 7111 return -ENOENT; 7112 7113 if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) { 7114 struct sock *sk; 7115 7116 if (unlikely(!reuse_kern->reuseport_id)) 7117 /* There is a small race between adding the 7118 * sk to the map and setting the 7119 * reuse_kern->reuseport_id. 7120 * Treat it as the sk has not been added to 7121 * the bpf map yet. 7122 */ 7123 return -ENOENT; 7124 7125 sk = reuse_kern->sk; 7126 if (sk->sk_protocol != selected_sk->sk_protocol) 7127 return -EPROTOTYPE; 7128 else if (sk->sk_family != selected_sk->sk_family) 7129 return -EAFNOSUPPORT; 7130 7131 /* Catch all. Likely bound to a different sockaddr. */ 7132 return -EBADFD; 7133 } 7134 7135 reuse_kern->selected_sk = selected_sk; 7136 7137 return 0; 7138 } 7139 7140 static const struct bpf_func_proto sk_select_reuseport_proto = { 7141 .func = sk_select_reuseport, 7142 .gpl_only = false, 7143 .ret_type = RET_INTEGER, 7144 .arg1_type = ARG_PTR_TO_CTX, 7145 .arg2_type = ARG_CONST_MAP_PTR, 7146 .arg3_type = ARG_PTR_TO_MAP_KEY, 7147 .arg4_type = ARG_ANYTHING, 7148 }; 7149 7150 BPF_CALL_4(sk_reuseport_load_bytes, 7151 const struct sk_reuseport_kern *, reuse_kern, u32, offset, 7152 void *, to, u32, len) 7153 { 7154 return ____bpf_skb_load_bytes(reuse_kern->skb, offset, to, len); 7155 } 7156 7157 static const struct bpf_func_proto sk_reuseport_load_bytes_proto = { 7158 .func = sk_reuseport_load_bytes, 7159 .gpl_only = false, 7160 .ret_type = RET_INTEGER, 7161 .arg1_type = ARG_PTR_TO_CTX, 7162 .arg2_type = ARG_ANYTHING, 7163 .arg3_type = ARG_PTR_TO_UNINIT_MEM, 7164 .arg4_type = ARG_CONST_SIZE, 7165 }; 7166 7167 BPF_CALL_5(sk_reuseport_load_bytes_relative, 7168 const struct sk_reuseport_kern *, reuse_kern, u32, offset, 7169 void *, to, u32, len, u32, start_header) 7170 { 7171 return ____bpf_skb_load_bytes_relative(reuse_kern->skb, offset, to, 7172 len, start_header); 7173 } 7174 7175 static const struct bpf_func_proto sk_reuseport_load_bytes_relative_proto = { 7176 .func = sk_reuseport_load_bytes_relative, 7177 .gpl_only = false, 7178 .ret_type = RET_INTEGER, 7179 .arg1_type = ARG_PTR_TO_CTX, 7180 .arg2_type = ARG_ANYTHING, 7181 .arg3_type = ARG_PTR_TO_UNINIT_MEM, 7182 .arg4_type = ARG_CONST_SIZE, 7183 .arg5_type = ARG_ANYTHING, 7184 }; 7185 7186 static const struct bpf_func_proto * 7187 sk_reuseport_func_proto(enum bpf_func_id func_id, 7188 const struct bpf_prog *prog) 7189 { 7190 switch (func_id) { 7191 case BPF_FUNC_sk_select_reuseport: 7192 return &sk_select_reuseport_proto; 7193 case BPF_FUNC_skb_load_bytes: 7194 return &sk_reuseport_load_bytes_proto; 7195 case BPF_FUNC_skb_load_bytes_relative: 7196 return &sk_reuseport_load_bytes_relative_proto; 7197 default: 7198 return bpf_base_func_proto(func_id); 7199 } 7200 } 7201 7202 static bool 7203 sk_reuseport_is_valid_access(int off, int size, 7204 enum bpf_access_type type, 7205 const struct bpf_prog *prog, 7206 struct bpf_insn_access_aux *info) 7207 { 7208 const u32 size_default = sizeof(__u32); 7209 7210 if (off < 0 || off >= sizeof(struct sk_reuseport_md) || 7211 off % size || type != BPF_READ) 7212 return false; 7213 7214 switch (off) { 7215 case offsetof(struct sk_reuseport_md, data): 7216 info->reg_type = PTR_TO_PACKET; 7217 return size == sizeof(__u64); 7218 7219 case offsetof(struct sk_reuseport_md, data_end): 7220 info->reg_type = PTR_TO_PACKET_END; 7221 return size == sizeof(__u64); 7222 7223 case offsetof(struct sk_reuseport_md, hash): 7224 return size == size_default; 7225 7226 /* Fields that allow narrowing */ 7227 case offsetof(struct sk_reuseport_md, eth_protocol): 7228 if (size < FIELD_SIZEOF(struct sk_buff, protocol)) 7229 return false; 7230 /* fall through */ 7231 case offsetof(struct sk_reuseport_md, ip_protocol): 7232 case offsetof(struct sk_reuseport_md, bind_inany): 7233 case offsetof(struct sk_reuseport_md, len): 7234 bpf_ctx_record_field_size(info, size_default); 7235 return bpf_ctx_narrow_access_ok(off, size, size_default); 7236 7237 default: 7238 return false; 7239 } 7240 } 7241 7242 #define SK_REUSEPORT_LOAD_FIELD(F) ({ \ 7243 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_reuseport_kern, F), \ 7244 si->dst_reg, si->src_reg, \ 7245 bpf_target_off(struct sk_reuseport_kern, F, \ 7246 FIELD_SIZEOF(struct sk_reuseport_kern, F), \ 7247 target_size)); \ 7248 }) 7249 7250 #define SK_REUSEPORT_LOAD_SKB_FIELD(SKB_FIELD) \ 7251 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \ 7252 struct sk_buff, \ 7253 skb, \ 7254 SKB_FIELD) 7255 7256 #define SK_REUSEPORT_LOAD_SK_FIELD_SIZE_OFF(SK_FIELD, BPF_SIZE, EXTRA_OFF) \ 7257 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(struct sk_reuseport_kern, \ 7258 struct sock, \ 7259 sk, \ 7260 SK_FIELD, BPF_SIZE, EXTRA_OFF) 7261 7262 static u32 sk_reuseport_convert_ctx_access(enum bpf_access_type type, 7263 const struct bpf_insn *si, 7264 struct bpf_insn *insn_buf, 7265 struct bpf_prog *prog, 7266 u32 *target_size) 7267 { 7268 struct bpf_insn *insn = insn_buf; 7269 7270 switch (si->off) { 7271 case offsetof(struct sk_reuseport_md, data): 7272 SK_REUSEPORT_LOAD_SKB_FIELD(data); 7273 break; 7274 7275 case offsetof(struct sk_reuseport_md, len): 7276 SK_REUSEPORT_LOAD_SKB_FIELD(len); 7277 break; 7278 7279 case offsetof(struct sk_reuseport_md, eth_protocol): 7280 SK_REUSEPORT_LOAD_SKB_FIELD(protocol); 7281 break; 7282 7283 case offsetof(struct sk_reuseport_md, ip_protocol): 7284 BUILD_BUG_ON(hweight_long(SK_FL_PROTO_MASK) != BITS_PER_BYTE); 7285 SK_REUSEPORT_LOAD_SK_FIELD_SIZE_OFF(__sk_flags_offset, 7286 BPF_W, 0); 7287 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK); 7288 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 7289 SK_FL_PROTO_SHIFT); 7290 /* SK_FL_PROTO_MASK and SK_FL_PROTO_SHIFT are endian 7291 * aware. No further narrowing or masking is needed. 7292 */ 7293 *target_size = 1; 7294 break; 7295 7296 case offsetof(struct sk_reuseport_md, data_end): 7297 SK_REUSEPORT_LOAD_FIELD(data_end); 7298 break; 7299 7300 case offsetof(struct sk_reuseport_md, hash): 7301 SK_REUSEPORT_LOAD_FIELD(hash); 7302 break; 7303 7304 case offsetof(struct sk_reuseport_md, bind_inany): 7305 SK_REUSEPORT_LOAD_FIELD(bind_inany); 7306 break; 7307 } 7308 7309 return insn - insn_buf; 7310 } 7311 7312 const struct bpf_verifier_ops sk_reuseport_verifier_ops = { 7313 .get_func_proto = sk_reuseport_func_proto, 7314 .is_valid_access = sk_reuseport_is_valid_access, 7315 .convert_ctx_access = sk_reuseport_convert_ctx_access, 7316 }; 7317 7318 const struct bpf_prog_ops sk_reuseport_prog_ops = { 7319 }; 7320 #endif /* CONFIG_INET */ 7321