1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Linux Socket Filter - Kernel level socket filtering 4 * 5 * Based on the design of the Berkeley Packet Filter. The new 6 * internal format has been designed by PLUMgrid: 7 * 8 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com 9 * 10 * Authors: 11 * 12 * Jay Schulist <jschlst@samba.org> 13 * Alexei Starovoitov <ast@plumgrid.com> 14 * Daniel Borkmann <dborkman@redhat.com> 15 * 16 * Andi Kleen - Fix a few bad bugs and races. 17 * Kris Katterjohn - Added many additional checks in bpf_check_classic() 18 */ 19 20 #include <linux/atomic.h> 21 #include <linux/bpf_verifier.h> 22 #include <linux/module.h> 23 #include <linux/types.h> 24 #include <linux/mm.h> 25 #include <linux/fcntl.h> 26 #include <linux/socket.h> 27 #include <linux/sock_diag.h> 28 #include <linux/in.h> 29 #include <linux/inet.h> 30 #include <linux/netdevice.h> 31 #include <linux/if_packet.h> 32 #include <linux/if_arp.h> 33 #include <linux/gfp.h> 34 #include <net/inet_common.h> 35 #include <net/ip.h> 36 #include <net/protocol.h> 37 #include <net/netlink.h> 38 #include <linux/skbuff.h> 39 #include <linux/skmsg.h> 40 #include <net/sock.h> 41 #include <net/flow_dissector.h> 42 #include <linux/errno.h> 43 #include <linux/timer.h> 44 #include <linux/uaccess.h> 45 #include <asm/unaligned.h> 46 #include <linux/filter.h> 47 #include <linux/ratelimit.h> 48 #include <linux/seccomp.h> 49 #include <linux/if_vlan.h> 50 #include <linux/bpf.h> 51 #include <linux/btf.h> 52 #include <net/sch_generic.h> 53 #include <net/cls_cgroup.h> 54 #include <net/dst_metadata.h> 55 #include <net/dst.h> 56 #include <net/sock_reuseport.h> 57 #include <net/busy_poll.h> 58 #include <net/tcp.h> 59 #include <net/xfrm.h> 60 #include <net/udp.h> 61 #include <linux/bpf_trace.h> 62 #include <net/xdp_sock.h> 63 #include <linux/inetdevice.h> 64 #include <net/inet_hashtables.h> 65 #include <net/inet6_hashtables.h> 66 #include <net/ip_fib.h> 67 #include <net/nexthop.h> 68 #include <net/flow.h> 69 #include <net/arp.h> 70 #include <net/ipv6.h> 71 #include <net/net_namespace.h> 72 #include <linux/seg6_local.h> 73 #include <net/seg6.h> 74 #include <net/seg6_local.h> 75 #include <net/lwtunnel.h> 76 #include <net/ipv6_stubs.h> 77 #include <net/bpf_sk_storage.h> 78 #include <net/transp_v6.h> 79 #include <linux/btf_ids.h> 80 #include <net/tls.h> 81 #include <net/xdp.h> 82 #include <net/mptcp.h> 83 #include <net/netfilter/nf_conntrack_bpf.h> 84 #include <linux/un.h> 85 #include <net/xdp_sock_drv.h> 86 87 static const struct bpf_func_proto * 88 bpf_sk_base_func_proto(enum bpf_func_id func_id); 89 90 int copy_bpf_fprog_from_user(struct sock_fprog *dst, sockptr_t src, int len) 91 { 92 if (in_compat_syscall()) { 93 struct compat_sock_fprog f32; 94 95 if (len != sizeof(f32)) 96 return -EINVAL; 97 if (copy_from_sockptr(&f32, src, sizeof(f32))) 98 return -EFAULT; 99 memset(dst, 0, sizeof(*dst)); 100 dst->len = f32.len; 101 dst->filter = compat_ptr(f32.filter); 102 } else { 103 if (len != sizeof(*dst)) 104 return -EINVAL; 105 if (copy_from_sockptr(dst, src, sizeof(*dst))) 106 return -EFAULT; 107 } 108 109 return 0; 110 } 111 EXPORT_SYMBOL_GPL(copy_bpf_fprog_from_user); 112 113 /** 114 * sk_filter_trim_cap - run a packet through a socket filter 115 * @sk: sock associated with &sk_buff 116 * @skb: buffer to filter 117 * @cap: limit on how short the eBPF program may trim the packet 118 * 119 * Run the eBPF program and then cut skb->data to correct size returned by 120 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller 121 * than pkt_len we keep whole skb->data. This is the socket level 122 * wrapper to bpf_prog_run. It returns 0 if the packet should 123 * be accepted or -EPERM if the packet should be tossed. 124 * 125 */ 126 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap) 127 { 128 int err; 129 struct sk_filter *filter; 130 131 /* 132 * If the skb was allocated from pfmemalloc reserves, only 133 * allow SOCK_MEMALLOC sockets to use it as this socket is 134 * helping free memory 135 */ 136 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) { 137 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP); 138 return -ENOMEM; 139 } 140 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb); 141 if (err) 142 return err; 143 144 err = security_sock_rcv_skb(sk, skb); 145 if (err) 146 return err; 147 148 rcu_read_lock(); 149 filter = rcu_dereference(sk->sk_filter); 150 if (filter) { 151 struct sock *save_sk = skb->sk; 152 unsigned int pkt_len; 153 154 skb->sk = sk; 155 pkt_len = bpf_prog_run_save_cb(filter->prog, skb); 156 skb->sk = save_sk; 157 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM; 158 } 159 rcu_read_unlock(); 160 161 return err; 162 } 163 EXPORT_SYMBOL(sk_filter_trim_cap); 164 165 BPF_CALL_1(bpf_skb_get_pay_offset, struct sk_buff *, skb) 166 { 167 return skb_get_poff(skb); 168 } 169 170 BPF_CALL_3(bpf_skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x) 171 { 172 struct nlattr *nla; 173 174 if (skb_is_nonlinear(skb)) 175 return 0; 176 177 if (skb->len < sizeof(struct nlattr)) 178 return 0; 179 180 if (a > skb->len - sizeof(struct nlattr)) 181 return 0; 182 183 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x); 184 if (nla) 185 return (void *) nla - (void *) skb->data; 186 187 return 0; 188 } 189 190 BPF_CALL_3(bpf_skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x) 191 { 192 struct nlattr *nla; 193 194 if (skb_is_nonlinear(skb)) 195 return 0; 196 197 if (skb->len < sizeof(struct nlattr)) 198 return 0; 199 200 if (a > skb->len - sizeof(struct nlattr)) 201 return 0; 202 203 nla = (struct nlattr *) &skb->data[a]; 204 if (nla->nla_len > skb->len - a) 205 return 0; 206 207 nla = nla_find_nested(nla, x); 208 if (nla) 209 return (void *) nla - (void *) skb->data; 210 211 return 0; 212 } 213 214 BPF_CALL_4(bpf_skb_load_helper_8, const struct sk_buff *, skb, const void *, 215 data, int, headlen, int, offset) 216 { 217 u8 tmp, *ptr; 218 const int len = sizeof(tmp); 219 220 if (offset >= 0) { 221 if (headlen - offset >= len) 222 return *(u8 *)(data + offset); 223 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp))) 224 return tmp; 225 } else { 226 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len); 227 if (likely(ptr)) 228 return *(u8 *)ptr; 229 } 230 231 return -EFAULT; 232 } 233 234 BPF_CALL_2(bpf_skb_load_helper_8_no_cache, const struct sk_buff *, skb, 235 int, offset) 236 { 237 return ____bpf_skb_load_helper_8(skb, skb->data, skb->len - skb->data_len, 238 offset); 239 } 240 241 BPF_CALL_4(bpf_skb_load_helper_16, const struct sk_buff *, skb, const void *, 242 data, int, headlen, int, offset) 243 { 244 __be16 tmp, *ptr; 245 const int len = sizeof(tmp); 246 247 if (offset >= 0) { 248 if (headlen - offset >= len) 249 return get_unaligned_be16(data + offset); 250 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp))) 251 return be16_to_cpu(tmp); 252 } else { 253 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len); 254 if (likely(ptr)) 255 return get_unaligned_be16(ptr); 256 } 257 258 return -EFAULT; 259 } 260 261 BPF_CALL_2(bpf_skb_load_helper_16_no_cache, const struct sk_buff *, skb, 262 int, offset) 263 { 264 return ____bpf_skb_load_helper_16(skb, skb->data, skb->len - skb->data_len, 265 offset); 266 } 267 268 BPF_CALL_4(bpf_skb_load_helper_32, const struct sk_buff *, skb, const void *, 269 data, int, headlen, int, offset) 270 { 271 __be32 tmp, *ptr; 272 const int len = sizeof(tmp); 273 274 if (likely(offset >= 0)) { 275 if (headlen - offset >= len) 276 return get_unaligned_be32(data + offset); 277 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp))) 278 return be32_to_cpu(tmp); 279 } else { 280 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len); 281 if (likely(ptr)) 282 return get_unaligned_be32(ptr); 283 } 284 285 return -EFAULT; 286 } 287 288 BPF_CALL_2(bpf_skb_load_helper_32_no_cache, const struct sk_buff *, skb, 289 int, offset) 290 { 291 return ____bpf_skb_load_helper_32(skb, skb->data, skb->len - skb->data_len, 292 offset); 293 } 294 295 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg, 296 struct bpf_insn *insn_buf) 297 { 298 struct bpf_insn *insn = insn_buf; 299 300 switch (skb_field) { 301 case SKF_AD_MARK: 302 BUILD_BUG_ON(sizeof_field(struct sk_buff, mark) != 4); 303 304 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, 305 offsetof(struct sk_buff, mark)); 306 break; 307 308 case SKF_AD_PKTTYPE: 309 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET); 310 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX); 311 #ifdef __BIG_ENDIAN_BITFIELD 312 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5); 313 #endif 314 break; 315 316 case SKF_AD_QUEUE: 317 BUILD_BUG_ON(sizeof_field(struct sk_buff, queue_mapping) != 2); 318 319 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, 320 offsetof(struct sk_buff, queue_mapping)); 321 break; 322 323 case SKF_AD_VLAN_TAG: 324 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_tci) != 2); 325 326 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */ 327 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, 328 offsetof(struct sk_buff, vlan_tci)); 329 break; 330 case SKF_AD_VLAN_TAG_PRESENT: 331 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_all) != 4); 332 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, 333 offsetof(struct sk_buff, vlan_all)); 334 *insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1); 335 *insn++ = BPF_ALU32_IMM(BPF_MOV, dst_reg, 1); 336 break; 337 } 338 339 return insn - insn_buf; 340 } 341 342 static bool convert_bpf_extensions(struct sock_filter *fp, 343 struct bpf_insn **insnp) 344 { 345 struct bpf_insn *insn = *insnp; 346 u32 cnt; 347 348 switch (fp->k) { 349 case SKF_AD_OFF + SKF_AD_PROTOCOL: 350 BUILD_BUG_ON(sizeof_field(struct sk_buff, protocol) != 2); 351 352 /* A = *(u16 *) (CTX + offsetof(protocol)) */ 353 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX, 354 offsetof(struct sk_buff, protocol)); 355 /* A = ntohs(A) [emitting a nop or swap16] */ 356 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16); 357 break; 358 359 case SKF_AD_OFF + SKF_AD_PKTTYPE: 360 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn); 361 insn += cnt - 1; 362 break; 363 364 case SKF_AD_OFF + SKF_AD_IFINDEX: 365 case SKF_AD_OFF + SKF_AD_HATYPE: 366 BUILD_BUG_ON(sizeof_field(struct net_device, ifindex) != 4); 367 BUILD_BUG_ON(sizeof_field(struct net_device, type) != 2); 368 369 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev), 370 BPF_REG_TMP, BPF_REG_CTX, 371 offsetof(struct sk_buff, dev)); 372 /* if (tmp != 0) goto pc + 1 */ 373 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1); 374 *insn++ = BPF_EXIT_INSN(); 375 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX) 376 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP, 377 offsetof(struct net_device, ifindex)); 378 else 379 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP, 380 offsetof(struct net_device, type)); 381 break; 382 383 case SKF_AD_OFF + SKF_AD_MARK: 384 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn); 385 insn += cnt - 1; 386 break; 387 388 case SKF_AD_OFF + SKF_AD_RXHASH: 389 BUILD_BUG_ON(sizeof_field(struct sk_buff, hash) != 4); 390 391 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, 392 offsetof(struct sk_buff, hash)); 393 break; 394 395 case SKF_AD_OFF + SKF_AD_QUEUE: 396 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn); 397 insn += cnt - 1; 398 break; 399 400 case SKF_AD_OFF + SKF_AD_VLAN_TAG: 401 cnt = convert_skb_access(SKF_AD_VLAN_TAG, 402 BPF_REG_A, BPF_REG_CTX, insn); 403 insn += cnt - 1; 404 break; 405 406 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT: 407 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT, 408 BPF_REG_A, BPF_REG_CTX, insn); 409 insn += cnt - 1; 410 break; 411 412 case SKF_AD_OFF + SKF_AD_VLAN_TPID: 413 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_proto) != 2); 414 415 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */ 416 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX, 417 offsetof(struct sk_buff, vlan_proto)); 418 /* A = ntohs(A) [emitting a nop or swap16] */ 419 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16); 420 break; 421 422 case SKF_AD_OFF + SKF_AD_PAY_OFFSET: 423 case SKF_AD_OFF + SKF_AD_NLATTR: 424 case SKF_AD_OFF + SKF_AD_NLATTR_NEST: 425 case SKF_AD_OFF + SKF_AD_CPU: 426 case SKF_AD_OFF + SKF_AD_RANDOM: 427 /* arg1 = CTX */ 428 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX); 429 /* arg2 = A */ 430 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A); 431 /* arg3 = X */ 432 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X); 433 /* Emit call(arg1=CTX, arg2=A, arg3=X) */ 434 switch (fp->k) { 435 case SKF_AD_OFF + SKF_AD_PAY_OFFSET: 436 *insn = BPF_EMIT_CALL(bpf_skb_get_pay_offset); 437 break; 438 case SKF_AD_OFF + SKF_AD_NLATTR: 439 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr); 440 break; 441 case SKF_AD_OFF + SKF_AD_NLATTR_NEST: 442 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr_nest); 443 break; 444 case SKF_AD_OFF + SKF_AD_CPU: 445 *insn = BPF_EMIT_CALL(bpf_get_raw_cpu_id); 446 break; 447 case SKF_AD_OFF + SKF_AD_RANDOM: 448 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32); 449 bpf_user_rnd_init_once(); 450 break; 451 } 452 break; 453 454 case SKF_AD_OFF + SKF_AD_ALU_XOR_X: 455 /* A ^= X */ 456 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X); 457 break; 458 459 default: 460 /* This is just a dummy call to avoid letting the compiler 461 * evict __bpf_call_base() as an optimization. Placed here 462 * where no-one bothers. 463 */ 464 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0); 465 return false; 466 } 467 468 *insnp = insn; 469 return true; 470 } 471 472 static bool convert_bpf_ld_abs(struct sock_filter *fp, struct bpf_insn **insnp) 473 { 474 const bool unaligned_ok = IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS); 475 int size = bpf_size_to_bytes(BPF_SIZE(fp->code)); 476 bool endian = BPF_SIZE(fp->code) == BPF_H || 477 BPF_SIZE(fp->code) == BPF_W; 478 bool indirect = BPF_MODE(fp->code) == BPF_IND; 479 const int ip_align = NET_IP_ALIGN; 480 struct bpf_insn *insn = *insnp; 481 int offset = fp->k; 482 483 if (!indirect && 484 ((unaligned_ok && offset >= 0) || 485 (!unaligned_ok && offset >= 0 && 486 offset + ip_align >= 0 && 487 offset + ip_align % size == 0))) { 488 bool ldx_off_ok = offset <= S16_MAX; 489 490 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_H); 491 if (offset) 492 *insn++ = BPF_ALU64_IMM(BPF_SUB, BPF_REG_TMP, offset); 493 *insn++ = BPF_JMP_IMM(BPF_JSLT, BPF_REG_TMP, 494 size, 2 + endian + (!ldx_off_ok * 2)); 495 if (ldx_off_ok) { 496 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A, 497 BPF_REG_D, offset); 498 } else { 499 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_D); 500 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_TMP, offset); 501 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A, 502 BPF_REG_TMP, 0); 503 } 504 if (endian) 505 *insn++ = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, size * 8); 506 *insn++ = BPF_JMP_A(8); 507 } 508 509 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX); 510 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_D); 511 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_H); 512 if (!indirect) { 513 *insn++ = BPF_MOV64_IMM(BPF_REG_ARG4, offset); 514 } else { 515 *insn++ = BPF_MOV64_REG(BPF_REG_ARG4, BPF_REG_X); 516 if (fp->k) 517 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG4, offset); 518 } 519 520 switch (BPF_SIZE(fp->code)) { 521 case BPF_B: 522 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8); 523 break; 524 case BPF_H: 525 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16); 526 break; 527 case BPF_W: 528 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32); 529 break; 530 default: 531 return false; 532 } 533 534 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_A, 0, 2); 535 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A); 536 *insn = BPF_EXIT_INSN(); 537 538 *insnp = insn; 539 return true; 540 } 541 542 /** 543 * bpf_convert_filter - convert filter program 544 * @prog: the user passed filter program 545 * @len: the length of the user passed filter program 546 * @new_prog: allocated 'struct bpf_prog' or NULL 547 * @new_len: pointer to store length of converted program 548 * @seen_ld_abs: bool whether we've seen ld_abs/ind 549 * 550 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn' 551 * style extended BPF (eBPF). 552 * Conversion workflow: 553 * 554 * 1) First pass for calculating the new program length: 555 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs) 556 * 557 * 2) 2nd pass to remap in two passes: 1st pass finds new 558 * jump offsets, 2nd pass remapping: 559 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs) 560 */ 561 static int bpf_convert_filter(struct sock_filter *prog, int len, 562 struct bpf_prog *new_prog, int *new_len, 563 bool *seen_ld_abs) 564 { 565 int new_flen = 0, pass = 0, target, i, stack_off; 566 struct bpf_insn *new_insn, *first_insn = NULL; 567 struct sock_filter *fp; 568 int *addrs = NULL; 569 u8 bpf_src; 570 571 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK); 572 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); 573 574 if (len <= 0 || len > BPF_MAXINSNS) 575 return -EINVAL; 576 577 if (new_prog) { 578 first_insn = new_prog->insnsi; 579 addrs = kcalloc(len, sizeof(*addrs), 580 GFP_KERNEL | __GFP_NOWARN); 581 if (!addrs) 582 return -ENOMEM; 583 } 584 585 do_pass: 586 new_insn = first_insn; 587 fp = prog; 588 589 /* Classic BPF related prologue emission. */ 590 if (new_prog) { 591 /* Classic BPF expects A and X to be reset first. These need 592 * to be guaranteed to be the first two instructions. 593 */ 594 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A); 595 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X); 596 597 /* All programs must keep CTX in callee saved BPF_REG_CTX. 598 * In eBPF case it's done by the compiler, here we need to 599 * do this ourself. Initial CTX is present in BPF_REG_ARG1. 600 */ 601 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1); 602 if (*seen_ld_abs) { 603 /* For packet access in classic BPF, cache skb->data 604 * in callee-saved BPF R8 and skb->len - skb->data_len 605 * (headlen) in BPF R9. Since classic BPF is read-only 606 * on CTX, we only need to cache it once. 607 */ 608 *new_insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data), 609 BPF_REG_D, BPF_REG_CTX, 610 offsetof(struct sk_buff, data)); 611 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_H, BPF_REG_CTX, 612 offsetof(struct sk_buff, len)); 613 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_TMP, BPF_REG_CTX, 614 offsetof(struct sk_buff, data_len)); 615 *new_insn++ = BPF_ALU32_REG(BPF_SUB, BPF_REG_H, BPF_REG_TMP); 616 } 617 } else { 618 new_insn += 3; 619 } 620 621 for (i = 0; i < len; fp++, i++) { 622 struct bpf_insn tmp_insns[32] = { }; 623 struct bpf_insn *insn = tmp_insns; 624 625 if (addrs) 626 addrs[i] = new_insn - first_insn; 627 628 switch (fp->code) { 629 /* All arithmetic insns and skb loads map as-is. */ 630 case BPF_ALU | BPF_ADD | BPF_X: 631 case BPF_ALU | BPF_ADD | BPF_K: 632 case BPF_ALU | BPF_SUB | BPF_X: 633 case BPF_ALU | BPF_SUB | BPF_K: 634 case BPF_ALU | BPF_AND | BPF_X: 635 case BPF_ALU | BPF_AND | BPF_K: 636 case BPF_ALU | BPF_OR | BPF_X: 637 case BPF_ALU | BPF_OR | BPF_K: 638 case BPF_ALU | BPF_LSH | BPF_X: 639 case BPF_ALU | BPF_LSH | BPF_K: 640 case BPF_ALU | BPF_RSH | BPF_X: 641 case BPF_ALU | BPF_RSH | BPF_K: 642 case BPF_ALU | BPF_XOR | BPF_X: 643 case BPF_ALU | BPF_XOR | BPF_K: 644 case BPF_ALU | BPF_MUL | BPF_X: 645 case BPF_ALU | BPF_MUL | BPF_K: 646 case BPF_ALU | BPF_DIV | BPF_X: 647 case BPF_ALU | BPF_DIV | BPF_K: 648 case BPF_ALU | BPF_MOD | BPF_X: 649 case BPF_ALU | BPF_MOD | BPF_K: 650 case BPF_ALU | BPF_NEG: 651 case BPF_LD | BPF_ABS | BPF_W: 652 case BPF_LD | BPF_ABS | BPF_H: 653 case BPF_LD | BPF_ABS | BPF_B: 654 case BPF_LD | BPF_IND | BPF_W: 655 case BPF_LD | BPF_IND | BPF_H: 656 case BPF_LD | BPF_IND | BPF_B: 657 /* Check for overloaded BPF extension and 658 * directly convert it if found, otherwise 659 * just move on with mapping. 660 */ 661 if (BPF_CLASS(fp->code) == BPF_LD && 662 BPF_MODE(fp->code) == BPF_ABS && 663 convert_bpf_extensions(fp, &insn)) 664 break; 665 if (BPF_CLASS(fp->code) == BPF_LD && 666 convert_bpf_ld_abs(fp, &insn)) { 667 *seen_ld_abs = true; 668 break; 669 } 670 671 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) || 672 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) { 673 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X); 674 /* Error with exception code on div/mod by 0. 675 * For cBPF programs, this was always return 0. 676 */ 677 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2); 678 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A); 679 *insn++ = BPF_EXIT_INSN(); 680 } 681 682 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k); 683 break; 684 685 /* Jump transformation cannot use BPF block macros 686 * everywhere as offset calculation and target updates 687 * require a bit more work than the rest, i.e. jump 688 * opcodes map as-is, but offsets need adjustment. 689 */ 690 691 #define BPF_EMIT_JMP \ 692 do { \ 693 const s32 off_min = S16_MIN, off_max = S16_MAX; \ 694 s32 off; \ 695 \ 696 if (target >= len || target < 0) \ 697 goto err; \ 698 off = addrs ? addrs[target] - addrs[i] - 1 : 0; \ 699 /* Adjust pc relative offset for 2nd or 3rd insn. */ \ 700 off -= insn - tmp_insns; \ 701 /* Reject anything not fitting into insn->off. */ \ 702 if (off < off_min || off > off_max) \ 703 goto err; \ 704 insn->off = off; \ 705 } while (0) 706 707 case BPF_JMP | BPF_JA: 708 target = i + fp->k + 1; 709 insn->code = fp->code; 710 BPF_EMIT_JMP; 711 break; 712 713 case BPF_JMP | BPF_JEQ | BPF_K: 714 case BPF_JMP | BPF_JEQ | BPF_X: 715 case BPF_JMP | BPF_JSET | BPF_K: 716 case BPF_JMP | BPF_JSET | BPF_X: 717 case BPF_JMP | BPF_JGT | BPF_K: 718 case BPF_JMP | BPF_JGT | BPF_X: 719 case BPF_JMP | BPF_JGE | BPF_K: 720 case BPF_JMP | BPF_JGE | BPF_X: 721 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) { 722 /* BPF immediates are signed, zero extend 723 * immediate into tmp register and use it 724 * in compare insn. 725 */ 726 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k); 727 728 insn->dst_reg = BPF_REG_A; 729 insn->src_reg = BPF_REG_TMP; 730 bpf_src = BPF_X; 731 } else { 732 insn->dst_reg = BPF_REG_A; 733 insn->imm = fp->k; 734 bpf_src = BPF_SRC(fp->code); 735 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0; 736 } 737 738 /* Common case where 'jump_false' is next insn. */ 739 if (fp->jf == 0) { 740 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src; 741 target = i + fp->jt + 1; 742 BPF_EMIT_JMP; 743 break; 744 } 745 746 /* Convert some jumps when 'jump_true' is next insn. */ 747 if (fp->jt == 0) { 748 switch (BPF_OP(fp->code)) { 749 case BPF_JEQ: 750 insn->code = BPF_JMP | BPF_JNE | bpf_src; 751 break; 752 case BPF_JGT: 753 insn->code = BPF_JMP | BPF_JLE | bpf_src; 754 break; 755 case BPF_JGE: 756 insn->code = BPF_JMP | BPF_JLT | bpf_src; 757 break; 758 default: 759 goto jmp_rest; 760 } 761 762 target = i + fp->jf + 1; 763 BPF_EMIT_JMP; 764 break; 765 } 766 jmp_rest: 767 /* Other jumps are mapped into two insns: Jxx and JA. */ 768 target = i + fp->jt + 1; 769 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src; 770 BPF_EMIT_JMP; 771 insn++; 772 773 insn->code = BPF_JMP | BPF_JA; 774 target = i + fp->jf + 1; 775 BPF_EMIT_JMP; 776 break; 777 778 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */ 779 case BPF_LDX | BPF_MSH | BPF_B: { 780 struct sock_filter tmp = { 781 .code = BPF_LD | BPF_ABS | BPF_B, 782 .k = fp->k, 783 }; 784 785 *seen_ld_abs = true; 786 787 /* X = A */ 788 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A); 789 /* A = BPF_R0 = *(u8 *) (skb->data + K) */ 790 convert_bpf_ld_abs(&tmp, &insn); 791 insn++; 792 /* A &= 0xf */ 793 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf); 794 /* A <<= 2 */ 795 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2); 796 /* tmp = X */ 797 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_X); 798 /* X = A */ 799 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A); 800 /* A = tmp */ 801 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP); 802 break; 803 } 804 /* RET_K is remaped into 2 insns. RET_A case doesn't need an 805 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A. 806 */ 807 case BPF_RET | BPF_A: 808 case BPF_RET | BPF_K: 809 if (BPF_RVAL(fp->code) == BPF_K) 810 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0, 811 0, fp->k); 812 *insn = BPF_EXIT_INSN(); 813 break; 814 815 /* Store to stack. */ 816 case BPF_ST: 817 case BPF_STX: 818 stack_off = fp->k * 4 + 4; 819 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) == 820 BPF_ST ? BPF_REG_A : BPF_REG_X, 821 -stack_off); 822 /* check_load_and_stores() verifies that classic BPF can 823 * load from stack only after write, so tracking 824 * stack_depth for ST|STX insns is enough 825 */ 826 if (new_prog && new_prog->aux->stack_depth < stack_off) 827 new_prog->aux->stack_depth = stack_off; 828 break; 829 830 /* Load from stack. */ 831 case BPF_LD | BPF_MEM: 832 case BPF_LDX | BPF_MEM: 833 stack_off = fp->k * 4 + 4; 834 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ? 835 BPF_REG_A : BPF_REG_X, BPF_REG_FP, 836 -stack_off); 837 break; 838 839 /* A = K or X = K */ 840 case BPF_LD | BPF_IMM: 841 case BPF_LDX | BPF_IMM: 842 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ? 843 BPF_REG_A : BPF_REG_X, fp->k); 844 break; 845 846 /* X = A */ 847 case BPF_MISC | BPF_TAX: 848 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A); 849 break; 850 851 /* A = X */ 852 case BPF_MISC | BPF_TXA: 853 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X); 854 break; 855 856 /* A = skb->len or X = skb->len */ 857 case BPF_LD | BPF_W | BPF_LEN: 858 case BPF_LDX | BPF_W | BPF_LEN: 859 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ? 860 BPF_REG_A : BPF_REG_X, BPF_REG_CTX, 861 offsetof(struct sk_buff, len)); 862 break; 863 864 /* Access seccomp_data fields. */ 865 case BPF_LDX | BPF_ABS | BPF_W: 866 /* A = *(u32 *) (ctx + K) */ 867 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k); 868 break; 869 870 /* Unknown instruction. */ 871 default: 872 goto err; 873 } 874 875 insn++; 876 if (new_prog) 877 memcpy(new_insn, tmp_insns, 878 sizeof(*insn) * (insn - tmp_insns)); 879 new_insn += insn - tmp_insns; 880 } 881 882 if (!new_prog) { 883 /* Only calculating new length. */ 884 *new_len = new_insn - first_insn; 885 if (*seen_ld_abs) 886 *new_len += 4; /* Prologue bits. */ 887 return 0; 888 } 889 890 pass++; 891 if (new_flen != new_insn - first_insn) { 892 new_flen = new_insn - first_insn; 893 if (pass > 2) 894 goto err; 895 goto do_pass; 896 } 897 898 kfree(addrs); 899 BUG_ON(*new_len != new_flen); 900 return 0; 901 err: 902 kfree(addrs); 903 return -EINVAL; 904 } 905 906 /* Security: 907 * 908 * As we dont want to clear mem[] array for each packet going through 909 * __bpf_prog_run(), we check that filter loaded by user never try to read 910 * a cell if not previously written, and we check all branches to be sure 911 * a malicious user doesn't try to abuse us. 912 */ 913 static int check_load_and_stores(const struct sock_filter *filter, int flen) 914 { 915 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */ 916 int pc, ret = 0; 917 918 BUILD_BUG_ON(BPF_MEMWORDS > 16); 919 920 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL); 921 if (!masks) 922 return -ENOMEM; 923 924 memset(masks, 0xff, flen * sizeof(*masks)); 925 926 for (pc = 0; pc < flen; pc++) { 927 memvalid &= masks[pc]; 928 929 switch (filter[pc].code) { 930 case BPF_ST: 931 case BPF_STX: 932 memvalid |= (1 << filter[pc].k); 933 break; 934 case BPF_LD | BPF_MEM: 935 case BPF_LDX | BPF_MEM: 936 if (!(memvalid & (1 << filter[pc].k))) { 937 ret = -EINVAL; 938 goto error; 939 } 940 break; 941 case BPF_JMP | BPF_JA: 942 /* A jump must set masks on target */ 943 masks[pc + 1 + filter[pc].k] &= memvalid; 944 memvalid = ~0; 945 break; 946 case BPF_JMP | BPF_JEQ | BPF_K: 947 case BPF_JMP | BPF_JEQ | BPF_X: 948 case BPF_JMP | BPF_JGE | BPF_K: 949 case BPF_JMP | BPF_JGE | BPF_X: 950 case BPF_JMP | BPF_JGT | BPF_K: 951 case BPF_JMP | BPF_JGT | BPF_X: 952 case BPF_JMP | BPF_JSET | BPF_K: 953 case BPF_JMP | BPF_JSET | BPF_X: 954 /* A jump must set masks on targets */ 955 masks[pc + 1 + filter[pc].jt] &= memvalid; 956 masks[pc + 1 + filter[pc].jf] &= memvalid; 957 memvalid = ~0; 958 break; 959 } 960 } 961 error: 962 kfree(masks); 963 return ret; 964 } 965 966 static bool chk_code_allowed(u16 code_to_probe) 967 { 968 static const bool codes[] = { 969 /* 32 bit ALU operations */ 970 [BPF_ALU | BPF_ADD | BPF_K] = true, 971 [BPF_ALU | BPF_ADD | BPF_X] = true, 972 [BPF_ALU | BPF_SUB | BPF_K] = true, 973 [BPF_ALU | BPF_SUB | BPF_X] = true, 974 [BPF_ALU | BPF_MUL | BPF_K] = true, 975 [BPF_ALU | BPF_MUL | BPF_X] = true, 976 [BPF_ALU | BPF_DIV | BPF_K] = true, 977 [BPF_ALU | BPF_DIV | BPF_X] = true, 978 [BPF_ALU | BPF_MOD | BPF_K] = true, 979 [BPF_ALU | BPF_MOD | BPF_X] = true, 980 [BPF_ALU | BPF_AND | BPF_K] = true, 981 [BPF_ALU | BPF_AND | BPF_X] = true, 982 [BPF_ALU | BPF_OR | BPF_K] = true, 983 [BPF_ALU | BPF_OR | BPF_X] = true, 984 [BPF_ALU | BPF_XOR | BPF_K] = true, 985 [BPF_ALU | BPF_XOR | BPF_X] = true, 986 [BPF_ALU | BPF_LSH | BPF_K] = true, 987 [BPF_ALU | BPF_LSH | BPF_X] = true, 988 [BPF_ALU | BPF_RSH | BPF_K] = true, 989 [BPF_ALU | BPF_RSH | BPF_X] = true, 990 [BPF_ALU | BPF_NEG] = true, 991 /* Load instructions */ 992 [BPF_LD | BPF_W | BPF_ABS] = true, 993 [BPF_LD | BPF_H | BPF_ABS] = true, 994 [BPF_LD | BPF_B | BPF_ABS] = true, 995 [BPF_LD | BPF_W | BPF_LEN] = true, 996 [BPF_LD | BPF_W | BPF_IND] = true, 997 [BPF_LD | BPF_H | BPF_IND] = true, 998 [BPF_LD | BPF_B | BPF_IND] = true, 999 [BPF_LD | BPF_IMM] = true, 1000 [BPF_LD | BPF_MEM] = true, 1001 [BPF_LDX | BPF_W | BPF_LEN] = true, 1002 [BPF_LDX | BPF_B | BPF_MSH] = true, 1003 [BPF_LDX | BPF_IMM] = true, 1004 [BPF_LDX | BPF_MEM] = true, 1005 /* Store instructions */ 1006 [BPF_ST] = true, 1007 [BPF_STX] = true, 1008 /* Misc instructions */ 1009 [BPF_MISC | BPF_TAX] = true, 1010 [BPF_MISC | BPF_TXA] = true, 1011 /* Return instructions */ 1012 [BPF_RET | BPF_K] = true, 1013 [BPF_RET | BPF_A] = true, 1014 /* Jump instructions */ 1015 [BPF_JMP | BPF_JA] = true, 1016 [BPF_JMP | BPF_JEQ | BPF_K] = true, 1017 [BPF_JMP | BPF_JEQ | BPF_X] = true, 1018 [BPF_JMP | BPF_JGE | BPF_K] = true, 1019 [BPF_JMP | BPF_JGE | BPF_X] = true, 1020 [BPF_JMP | BPF_JGT | BPF_K] = true, 1021 [BPF_JMP | BPF_JGT | BPF_X] = true, 1022 [BPF_JMP | BPF_JSET | BPF_K] = true, 1023 [BPF_JMP | BPF_JSET | BPF_X] = true, 1024 }; 1025 1026 if (code_to_probe >= ARRAY_SIZE(codes)) 1027 return false; 1028 1029 return codes[code_to_probe]; 1030 } 1031 1032 static bool bpf_check_basics_ok(const struct sock_filter *filter, 1033 unsigned int flen) 1034 { 1035 if (filter == NULL) 1036 return false; 1037 if (flen == 0 || flen > BPF_MAXINSNS) 1038 return false; 1039 1040 return true; 1041 } 1042 1043 /** 1044 * bpf_check_classic - verify socket filter code 1045 * @filter: filter to verify 1046 * @flen: length of filter 1047 * 1048 * Check the user's filter code. If we let some ugly 1049 * filter code slip through kaboom! The filter must contain 1050 * no references or jumps that are out of range, no illegal 1051 * instructions, and must end with a RET instruction. 1052 * 1053 * All jumps are forward as they are not signed. 1054 * 1055 * Returns 0 if the rule set is legal or -EINVAL if not. 1056 */ 1057 static int bpf_check_classic(const struct sock_filter *filter, 1058 unsigned int flen) 1059 { 1060 bool anc_found; 1061 int pc; 1062 1063 /* Check the filter code now */ 1064 for (pc = 0; pc < flen; pc++) { 1065 const struct sock_filter *ftest = &filter[pc]; 1066 1067 /* May we actually operate on this code? */ 1068 if (!chk_code_allowed(ftest->code)) 1069 return -EINVAL; 1070 1071 /* Some instructions need special checks */ 1072 switch (ftest->code) { 1073 case BPF_ALU | BPF_DIV | BPF_K: 1074 case BPF_ALU | BPF_MOD | BPF_K: 1075 /* Check for division by zero */ 1076 if (ftest->k == 0) 1077 return -EINVAL; 1078 break; 1079 case BPF_ALU | BPF_LSH | BPF_K: 1080 case BPF_ALU | BPF_RSH | BPF_K: 1081 if (ftest->k >= 32) 1082 return -EINVAL; 1083 break; 1084 case BPF_LD | BPF_MEM: 1085 case BPF_LDX | BPF_MEM: 1086 case BPF_ST: 1087 case BPF_STX: 1088 /* Check for invalid memory addresses */ 1089 if (ftest->k >= BPF_MEMWORDS) 1090 return -EINVAL; 1091 break; 1092 case BPF_JMP | BPF_JA: 1093 /* Note, the large ftest->k might cause loops. 1094 * Compare this with conditional jumps below, 1095 * where offsets are limited. --ANK (981016) 1096 */ 1097 if (ftest->k >= (unsigned int)(flen - pc - 1)) 1098 return -EINVAL; 1099 break; 1100 case BPF_JMP | BPF_JEQ | BPF_K: 1101 case BPF_JMP | BPF_JEQ | BPF_X: 1102 case BPF_JMP | BPF_JGE | BPF_K: 1103 case BPF_JMP | BPF_JGE | BPF_X: 1104 case BPF_JMP | BPF_JGT | BPF_K: 1105 case BPF_JMP | BPF_JGT | BPF_X: 1106 case BPF_JMP | BPF_JSET | BPF_K: 1107 case BPF_JMP | BPF_JSET | BPF_X: 1108 /* Both conditionals must be safe */ 1109 if (pc + ftest->jt + 1 >= flen || 1110 pc + ftest->jf + 1 >= flen) 1111 return -EINVAL; 1112 break; 1113 case BPF_LD | BPF_W | BPF_ABS: 1114 case BPF_LD | BPF_H | BPF_ABS: 1115 case BPF_LD | BPF_B | BPF_ABS: 1116 anc_found = false; 1117 if (bpf_anc_helper(ftest) & BPF_ANC) 1118 anc_found = true; 1119 /* Ancillary operation unknown or unsupported */ 1120 if (anc_found == false && ftest->k >= SKF_AD_OFF) 1121 return -EINVAL; 1122 } 1123 } 1124 1125 /* Last instruction must be a RET code */ 1126 switch (filter[flen - 1].code) { 1127 case BPF_RET | BPF_K: 1128 case BPF_RET | BPF_A: 1129 return check_load_and_stores(filter, flen); 1130 } 1131 1132 return -EINVAL; 1133 } 1134 1135 static int bpf_prog_store_orig_filter(struct bpf_prog *fp, 1136 const struct sock_fprog *fprog) 1137 { 1138 unsigned int fsize = bpf_classic_proglen(fprog); 1139 struct sock_fprog_kern *fkprog; 1140 1141 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL); 1142 if (!fp->orig_prog) 1143 return -ENOMEM; 1144 1145 fkprog = fp->orig_prog; 1146 fkprog->len = fprog->len; 1147 1148 fkprog->filter = kmemdup(fp->insns, fsize, 1149 GFP_KERNEL | __GFP_NOWARN); 1150 if (!fkprog->filter) { 1151 kfree(fp->orig_prog); 1152 return -ENOMEM; 1153 } 1154 1155 return 0; 1156 } 1157 1158 static void bpf_release_orig_filter(struct bpf_prog *fp) 1159 { 1160 struct sock_fprog_kern *fprog = fp->orig_prog; 1161 1162 if (fprog) { 1163 kfree(fprog->filter); 1164 kfree(fprog); 1165 } 1166 } 1167 1168 static void __bpf_prog_release(struct bpf_prog *prog) 1169 { 1170 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) { 1171 bpf_prog_put(prog); 1172 } else { 1173 bpf_release_orig_filter(prog); 1174 bpf_prog_free(prog); 1175 } 1176 } 1177 1178 static void __sk_filter_release(struct sk_filter *fp) 1179 { 1180 __bpf_prog_release(fp->prog); 1181 kfree(fp); 1182 } 1183 1184 /** 1185 * sk_filter_release_rcu - Release a socket filter by rcu_head 1186 * @rcu: rcu_head that contains the sk_filter to free 1187 */ 1188 static void sk_filter_release_rcu(struct rcu_head *rcu) 1189 { 1190 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu); 1191 1192 __sk_filter_release(fp); 1193 } 1194 1195 /** 1196 * sk_filter_release - release a socket filter 1197 * @fp: filter to remove 1198 * 1199 * Remove a filter from a socket and release its resources. 1200 */ 1201 static void sk_filter_release(struct sk_filter *fp) 1202 { 1203 if (refcount_dec_and_test(&fp->refcnt)) 1204 call_rcu(&fp->rcu, sk_filter_release_rcu); 1205 } 1206 1207 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp) 1208 { 1209 u32 filter_size = bpf_prog_size(fp->prog->len); 1210 1211 atomic_sub(filter_size, &sk->sk_omem_alloc); 1212 sk_filter_release(fp); 1213 } 1214 1215 /* try to charge the socket memory if there is space available 1216 * return true on success 1217 */ 1218 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp) 1219 { 1220 u32 filter_size = bpf_prog_size(fp->prog->len); 1221 int optmem_max = READ_ONCE(sysctl_optmem_max); 1222 1223 /* same check as in sock_kmalloc() */ 1224 if (filter_size <= optmem_max && 1225 atomic_read(&sk->sk_omem_alloc) + filter_size < optmem_max) { 1226 atomic_add(filter_size, &sk->sk_omem_alloc); 1227 return true; 1228 } 1229 return false; 1230 } 1231 1232 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp) 1233 { 1234 if (!refcount_inc_not_zero(&fp->refcnt)) 1235 return false; 1236 1237 if (!__sk_filter_charge(sk, fp)) { 1238 sk_filter_release(fp); 1239 return false; 1240 } 1241 return true; 1242 } 1243 1244 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp) 1245 { 1246 struct sock_filter *old_prog; 1247 struct bpf_prog *old_fp; 1248 int err, new_len, old_len = fp->len; 1249 bool seen_ld_abs = false; 1250 1251 /* We are free to overwrite insns et al right here as it won't be used at 1252 * this point in time anymore internally after the migration to the eBPF 1253 * instruction representation. 1254 */ 1255 BUILD_BUG_ON(sizeof(struct sock_filter) != 1256 sizeof(struct bpf_insn)); 1257 1258 /* Conversion cannot happen on overlapping memory areas, 1259 * so we need to keep the user BPF around until the 2nd 1260 * pass. At this time, the user BPF is stored in fp->insns. 1261 */ 1262 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter), 1263 GFP_KERNEL | __GFP_NOWARN); 1264 if (!old_prog) { 1265 err = -ENOMEM; 1266 goto out_err; 1267 } 1268 1269 /* 1st pass: calculate the new program length. */ 1270 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len, 1271 &seen_ld_abs); 1272 if (err) 1273 goto out_err_free; 1274 1275 /* Expand fp for appending the new filter representation. */ 1276 old_fp = fp; 1277 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0); 1278 if (!fp) { 1279 /* The old_fp is still around in case we couldn't 1280 * allocate new memory, so uncharge on that one. 1281 */ 1282 fp = old_fp; 1283 err = -ENOMEM; 1284 goto out_err_free; 1285 } 1286 1287 fp->len = new_len; 1288 1289 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */ 1290 err = bpf_convert_filter(old_prog, old_len, fp, &new_len, 1291 &seen_ld_abs); 1292 if (err) 1293 /* 2nd bpf_convert_filter() can fail only if it fails 1294 * to allocate memory, remapping must succeed. Note, 1295 * that at this time old_fp has already been released 1296 * by krealloc(). 1297 */ 1298 goto out_err_free; 1299 1300 fp = bpf_prog_select_runtime(fp, &err); 1301 if (err) 1302 goto out_err_free; 1303 1304 kfree(old_prog); 1305 return fp; 1306 1307 out_err_free: 1308 kfree(old_prog); 1309 out_err: 1310 __bpf_prog_release(fp); 1311 return ERR_PTR(err); 1312 } 1313 1314 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp, 1315 bpf_aux_classic_check_t trans) 1316 { 1317 int err; 1318 1319 fp->bpf_func = NULL; 1320 fp->jited = 0; 1321 1322 err = bpf_check_classic(fp->insns, fp->len); 1323 if (err) { 1324 __bpf_prog_release(fp); 1325 return ERR_PTR(err); 1326 } 1327 1328 /* There might be additional checks and transformations 1329 * needed on classic filters, f.e. in case of seccomp. 1330 */ 1331 if (trans) { 1332 err = trans(fp->insns, fp->len); 1333 if (err) { 1334 __bpf_prog_release(fp); 1335 return ERR_PTR(err); 1336 } 1337 } 1338 1339 /* Probe if we can JIT compile the filter and if so, do 1340 * the compilation of the filter. 1341 */ 1342 bpf_jit_compile(fp); 1343 1344 /* JIT compiler couldn't process this filter, so do the eBPF translation 1345 * for the optimized interpreter. 1346 */ 1347 if (!fp->jited) 1348 fp = bpf_migrate_filter(fp); 1349 1350 return fp; 1351 } 1352 1353 /** 1354 * bpf_prog_create - create an unattached filter 1355 * @pfp: the unattached filter that is created 1356 * @fprog: the filter program 1357 * 1358 * Create a filter independent of any socket. We first run some 1359 * sanity checks on it to make sure it does not explode on us later. 1360 * If an error occurs or there is insufficient memory for the filter 1361 * a negative errno code is returned. On success the return is zero. 1362 */ 1363 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog) 1364 { 1365 unsigned int fsize = bpf_classic_proglen(fprog); 1366 struct bpf_prog *fp; 1367 1368 /* Make sure new filter is there and in the right amounts. */ 1369 if (!bpf_check_basics_ok(fprog->filter, fprog->len)) 1370 return -EINVAL; 1371 1372 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0); 1373 if (!fp) 1374 return -ENOMEM; 1375 1376 memcpy(fp->insns, fprog->filter, fsize); 1377 1378 fp->len = fprog->len; 1379 /* Since unattached filters are not copied back to user 1380 * space through sk_get_filter(), we do not need to hold 1381 * a copy here, and can spare us the work. 1382 */ 1383 fp->orig_prog = NULL; 1384 1385 /* bpf_prepare_filter() already takes care of freeing 1386 * memory in case something goes wrong. 1387 */ 1388 fp = bpf_prepare_filter(fp, NULL); 1389 if (IS_ERR(fp)) 1390 return PTR_ERR(fp); 1391 1392 *pfp = fp; 1393 return 0; 1394 } 1395 EXPORT_SYMBOL_GPL(bpf_prog_create); 1396 1397 /** 1398 * bpf_prog_create_from_user - create an unattached filter from user buffer 1399 * @pfp: the unattached filter that is created 1400 * @fprog: the filter program 1401 * @trans: post-classic verifier transformation handler 1402 * @save_orig: save classic BPF program 1403 * 1404 * This function effectively does the same as bpf_prog_create(), only 1405 * that it builds up its insns buffer from user space provided buffer. 1406 * It also allows for passing a bpf_aux_classic_check_t handler. 1407 */ 1408 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog, 1409 bpf_aux_classic_check_t trans, bool save_orig) 1410 { 1411 unsigned int fsize = bpf_classic_proglen(fprog); 1412 struct bpf_prog *fp; 1413 int err; 1414 1415 /* Make sure new filter is there and in the right amounts. */ 1416 if (!bpf_check_basics_ok(fprog->filter, fprog->len)) 1417 return -EINVAL; 1418 1419 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0); 1420 if (!fp) 1421 return -ENOMEM; 1422 1423 if (copy_from_user(fp->insns, fprog->filter, fsize)) { 1424 __bpf_prog_free(fp); 1425 return -EFAULT; 1426 } 1427 1428 fp->len = fprog->len; 1429 fp->orig_prog = NULL; 1430 1431 if (save_orig) { 1432 err = bpf_prog_store_orig_filter(fp, fprog); 1433 if (err) { 1434 __bpf_prog_free(fp); 1435 return -ENOMEM; 1436 } 1437 } 1438 1439 /* bpf_prepare_filter() already takes care of freeing 1440 * memory in case something goes wrong. 1441 */ 1442 fp = bpf_prepare_filter(fp, trans); 1443 if (IS_ERR(fp)) 1444 return PTR_ERR(fp); 1445 1446 *pfp = fp; 1447 return 0; 1448 } 1449 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user); 1450 1451 void bpf_prog_destroy(struct bpf_prog *fp) 1452 { 1453 __bpf_prog_release(fp); 1454 } 1455 EXPORT_SYMBOL_GPL(bpf_prog_destroy); 1456 1457 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk) 1458 { 1459 struct sk_filter *fp, *old_fp; 1460 1461 fp = kmalloc(sizeof(*fp), GFP_KERNEL); 1462 if (!fp) 1463 return -ENOMEM; 1464 1465 fp->prog = prog; 1466 1467 if (!__sk_filter_charge(sk, fp)) { 1468 kfree(fp); 1469 return -ENOMEM; 1470 } 1471 refcount_set(&fp->refcnt, 1); 1472 1473 old_fp = rcu_dereference_protected(sk->sk_filter, 1474 lockdep_sock_is_held(sk)); 1475 rcu_assign_pointer(sk->sk_filter, fp); 1476 1477 if (old_fp) 1478 sk_filter_uncharge(sk, old_fp); 1479 1480 return 0; 1481 } 1482 1483 static 1484 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk) 1485 { 1486 unsigned int fsize = bpf_classic_proglen(fprog); 1487 struct bpf_prog *prog; 1488 int err; 1489 1490 if (sock_flag(sk, SOCK_FILTER_LOCKED)) 1491 return ERR_PTR(-EPERM); 1492 1493 /* Make sure new filter is there and in the right amounts. */ 1494 if (!bpf_check_basics_ok(fprog->filter, fprog->len)) 1495 return ERR_PTR(-EINVAL); 1496 1497 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0); 1498 if (!prog) 1499 return ERR_PTR(-ENOMEM); 1500 1501 if (copy_from_user(prog->insns, fprog->filter, fsize)) { 1502 __bpf_prog_free(prog); 1503 return ERR_PTR(-EFAULT); 1504 } 1505 1506 prog->len = fprog->len; 1507 1508 err = bpf_prog_store_orig_filter(prog, fprog); 1509 if (err) { 1510 __bpf_prog_free(prog); 1511 return ERR_PTR(-ENOMEM); 1512 } 1513 1514 /* bpf_prepare_filter() already takes care of freeing 1515 * memory in case something goes wrong. 1516 */ 1517 return bpf_prepare_filter(prog, NULL); 1518 } 1519 1520 /** 1521 * sk_attach_filter - attach a socket filter 1522 * @fprog: the filter program 1523 * @sk: the socket to use 1524 * 1525 * Attach the user's filter code. We first run some sanity checks on 1526 * it to make sure it does not explode on us later. If an error 1527 * occurs or there is insufficient memory for the filter a negative 1528 * errno code is returned. On success the return is zero. 1529 */ 1530 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk) 1531 { 1532 struct bpf_prog *prog = __get_filter(fprog, sk); 1533 int err; 1534 1535 if (IS_ERR(prog)) 1536 return PTR_ERR(prog); 1537 1538 err = __sk_attach_prog(prog, sk); 1539 if (err < 0) { 1540 __bpf_prog_release(prog); 1541 return err; 1542 } 1543 1544 return 0; 1545 } 1546 EXPORT_SYMBOL_GPL(sk_attach_filter); 1547 1548 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk) 1549 { 1550 struct bpf_prog *prog = __get_filter(fprog, sk); 1551 int err; 1552 1553 if (IS_ERR(prog)) 1554 return PTR_ERR(prog); 1555 1556 if (bpf_prog_size(prog->len) > READ_ONCE(sysctl_optmem_max)) 1557 err = -ENOMEM; 1558 else 1559 err = reuseport_attach_prog(sk, prog); 1560 1561 if (err) 1562 __bpf_prog_release(prog); 1563 1564 return err; 1565 } 1566 1567 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk) 1568 { 1569 if (sock_flag(sk, SOCK_FILTER_LOCKED)) 1570 return ERR_PTR(-EPERM); 1571 1572 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER); 1573 } 1574 1575 int sk_attach_bpf(u32 ufd, struct sock *sk) 1576 { 1577 struct bpf_prog *prog = __get_bpf(ufd, sk); 1578 int err; 1579 1580 if (IS_ERR(prog)) 1581 return PTR_ERR(prog); 1582 1583 err = __sk_attach_prog(prog, sk); 1584 if (err < 0) { 1585 bpf_prog_put(prog); 1586 return err; 1587 } 1588 1589 return 0; 1590 } 1591 1592 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk) 1593 { 1594 struct bpf_prog *prog; 1595 int err; 1596 1597 if (sock_flag(sk, SOCK_FILTER_LOCKED)) 1598 return -EPERM; 1599 1600 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER); 1601 if (PTR_ERR(prog) == -EINVAL) 1602 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SK_REUSEPORT); 1603 if (IS_ERR(prog)) 1604 return PTR_ERR(prog); 1605 1606 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT) { 1607 /* Like other non BPF_PROG_TYPE_SOCKET_FILTER 1608 * bpf prog (e.g. sockmap). It depends on the 1609 * limitation imposed by bpf_prog_load(). 1610 * Hence, sysctl_optmem_max is not checked. 1611 */ 1612 if ((sk->sk_type != SOCK_STREAM && 1613 sk->sk_type != SOCK_DGRAM) || 1614 (sk->sk_protocol != IPPROTO_UDP && 1615 sk->sk_protocol != IPPROTO_TCP) || 1616 (sk->sk_family != AF_INET && 1617 sk->sk_family != AF_INET6)) { 1618 err = -ENOTSUPP; 1619 goto err_prog_put; 1620 } 1621 } else { 1622 /* BPF_PROG_TYPE_SOCKET_FILTER */ 1623 if (bpf_prog_size(prog->len) > READ_ONCE(sysctl_optmem_max)) { 1624 err = -ENOMEM; 1625 goto err_prog_put; 1626 } 1627 } 1628 1629 err = reuseport_attach_prog(sk, prog); 1630 err_prog_put: 1631 if (err) 1632 bpf_prog_put(prog); 1633 1634 return err; 1635 } 1636 1637 void sk_reuseport_prog_free(struct bpf_prog *prog) 1638 { 1639 if (!prog) 1640 return; 1641 1642 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT) 1643 bpf_prog_put(prog); 1644 else 1645 bpf_prog_destroy(prog); 1646 } 1647 1648 struct bpf_scratchpad { 1649 union { 1650 __be32 diff[MAX_BPF_STACK / sizeof(__be32)]; 1651 u8 buff[MAX_BPF_STACK]; 1652 }; 1653 }; 1654 1655 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp); 1656 1657 static inline int __bpf_try_make_writable(struct sk_buff *skb, 1658 unsigned int write_len) 1659 { 1660 return skb_ensure_writable(skb, write_len); 1661 } 1662 1663 static inline int bpf_try_make_writable(struct sk_buff *skb, 1664 unsigned int write_len) 1665 { 1666 int err = __bpf_try_make_writable(skb, write_len); 1667 1668 bpf_compute_data_pointers(skb); 1669 return err; 1670 } 1671 1672 static int bpf_try_make_head_writable(struct sk_buff *skb) 1673 { 1674 return bpf_try_make_writable(skb, skb_headlen(skb)); 1675 } 1676 1677 static inline void bpf_push_mac_rcsum(struct sk_buff *skb) 1678 { 1679 if (skb_at_tc_ingress(skb)) 1680 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len); 1681 } 1682 1683 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb) 1684 { 1685 if (skb_at_tc_ingress(skb)) 1686 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len); 1687 } 1688 1689 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset, 1690 const void *, from, u32, len, u64, flags) 1691 { 1692 void *ptr; 1693 1694 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH))) 1695 return -EINVAL; 1696 if (unlikely(offset > INT_MAX)) 1697 return -EFAULT; 1698 if (unlikely(bpf_try_make_writable(skb, offset + len))) 1699 return -EFAULT; 1700 1701 ptr = skb->data + offset; 1702 if (flags & BPF_F_RECOMPUTE_CSUM) 1703 __skb_postpull_rcsum(skb, ptr, len, offset); 1704 1705 memcpy(ptr, from, len); 1706 1707 if (flags & BPF_F_RECOMPUTE_CSUM) 1708 __skb_postpush_rcsum(skb, ptr, len, offset); 1709 if (flags & BPF_F_INVALIDATE_HASH) 1710 skb_clear_hash(skb); 1711 1712 return 0; 1713 } 1714 1715 static const struct bpf_func_proto bpf_skb_store_bytes_proto = { 1716 .func = bpf_skb_store_bytes, 1717 .gpl_only = false, 1718 .ret_type = RET_INTEGER, 1719 .arg1_type = ARG_PTR_TO_CTX, 1720 .arg2_type = ARG_ANYTHING, 1721 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY, 1722 .arg4_type = ARG_CONST_SIZE, 1723 .arg5_type = ARG_ANYTHING, 1724 }; 1725 1726 int __bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from, 1727 u32 len, u64 flags) 1728 { 1729 return ____bpf_skb_store_bytes(skb, offset, from, len, flags); 1730 } 1731 1732 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset, 1733 void *, to, u32, len) 1734 { 1735 void *ptr; 1736 1737 if (unlikely(offset > INT_MAX)) 1738 goto err_clear; 1739 1740 ptr = skb_header_pointer(skb, offset, len, to); 1741 if (unlikely(!ptr)) 1742 goto err_clear; 1743 if (ptr != to) 1744 memcpy(to, ptr, len); 1745 1746 return 0; 1747 err_clear: 1748 memset(to, 0, len); 1749 return -EFAULT; 1750 } 1751 1752 static const struct bpf_func_proto bpf_skb_load_bytes_proto = { 1753 .func = bpf_skb_load_bytes, 1754 .gpl_only = false, 1755 .ret_type = RET_INTEGER, 1756 .arg1_type = ARG_PTR_TO_CTX, 1757 .arg2_type = ARG_ANYTHING, 1758 .arg3_type = ARG_PTR_TO_UNINIT_MEM, 1759 .arg4_type = ARG_CONST_SIZE, 1760 }; 1761 1762 int __bpf_skb_load_bytes(const struct sk_buff *skb, u32 offset, void *to, u32 len) 1763 { 1764 return ____bpf_skb_load_bytes(skb, offset, to, len); 1765 } 1766 1767 BPF_CALL_4(bpf_flow_dissector_load_bytes, 1768 const struct bpf_flow_dissector *, ctx, u32, offset, 1769 void *, to, u32, len) 1770 { 1771 void *ptr; 1772 1773 if (unlikely(offset > 0xffff)) 1774 goto err_clear; 1775 1776 if (unlikely(!ctx->skb)) 1777 goto err_clear; 1778 1779 ptr = skb_header_pointer(ctx->skb, offset, len, to); 1780 if (unlikely(!ptr)) 1781 goto err_clear; 1782 if (ptr != to) 1783 memcpy(to, ptr, len); 1784 1785 return 0; 1786 err_clear: 1787 memset(to, 0, len); 1788 return -EFAULT; 1789 } 1790 1791 static const struct bpf_func_proto bpf_flow_dissector_load_bytes_proto = { 1792 .func = bpf_flow_dissector_load_bytes, 1793 .gpl_only = false, 1794 .ret_type = RET_INTEGER, 1795 .arg1_type = ARG_PTR_TO_CTX, 1796 .arg2_type = ARG_ANYTHING, 1797 .arg3_type = ARG_PTR_TO_UNINIT_MEM, 1798 .arg4_type = ARG_CONST_SIZE, 1799 }; 1800 1801 BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb, 1802 u32, offset, void *, to, u32, len, u32, start_header) 1803 { 1804 u8 *end = skb_tail_pointer(skb); 1805 u8 *start, *ptr; 1806 1807 if (unlikely(offset > 0xffff)) 1808 goto err_clear; 1809 1810 switch (start_header) { 1811 case BPF_HDR_START_MAC: 1812 if (unlikely(!skb_mac_header_was_set(skb))) 1813 goto err_clear; 1814 start = skb_mac_header(skb); 1815 break; 1816 case BPF_HDR_START_NET: 1817 start = skb_network_header(skb); 1818 break; 1819 default: 1820 goto err_clear; 1821 } 1822 1823 ptr = start + offset; 1824 1825 if (likely(ptr + len <= end)) { 1826 memcpy(to, ptr, len); 1827 return 0; 1828 } 1829 1830 err_clear: 1831 memset(to, 0, len); 1832 return -EFAULT; 1833 } 1834 1835 static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto = { 1836 .func = bpf_skb_load_bytes_relative, 1837 .gpl_only = false, 1838 .ret_type = RET_INTEGER, 1839 .arg1_type = ARG_PTR_TO_CTX, 1840 .arg2_type = ARG_ANYTHING, 1841 .arg3_type = ARG_PTR_TO_UNINIT_MEM, 1842 .arg4_type = ARG_CONST_SIZE, 1843 .arg5_type = ARG_ANYTHING, 1844 }; 1845 1846 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len) 1847 { 1848 /* Idea is the following: should the needed direct read/write 1849 * test fail during runtime, we can pull in more data and redo 1850 * again, since implicitly, we invalidate previous checks here. 1851 * 1852 * Or, since we know how much we need to make read/writeable, 1853 * this can be done once at the program beginning for direct 1854 * access case. By this we overcome limitations of only current 1855 * headroom being accessible. 1856 */ 1857 return bpf_try_make_writable(skb, len ? : skb_headlen(skb)); 1858 } 1859 1860 static const struct bpf_func_proto bpf_skb_pull_data_proto = { 1861 .func = bpf_skb_pull_data, 1862 .gpl_only = false, 1863 .ret_type = RET_INTEGER, 1864 .arg1_type = ARG_PTR_TO_CTX, 1865 .arg2_type = ARG_ANYTHING, 1866 }; 1867 1868 BPF_CALL_1(bpf_sk_fullsock, struct sock *, sk) 1869 { 1870 return sk_fullsock(sk) ? (unsigned long)sk : (unsigned long)NULL; 1871 } 1872 1873 static const struct bpf_func_proto bpf_sk_fullsock_proto = { 1874 .func = bpf_sk_fullsock, 1875 .gpl_only = false, 1876 .ret_type = RET_PTR_TO_SOCKET_OR_NULL, 1877 .arg1_type = ARG_PTR_TO_SOCK_COMMON, 1878 }; 1879 1880 static inline int sk_skb_try_make_writable(struct sk_buff *skb, 1881 unsigned int write_len) 1882 { 1883 return __bpf_try_make_writable(skb, write_len); 1884 } 1885 1886 BPF_CALL_2(sk_skb_pull_data, struct sk_buff *, skb, u32, len) 1887 { 1888 /* Idea is the following: should the needed direct read/write 1889 * test fail during runtime, we can pull in more data and redo 1890 * again, since implicitly, we invalidate previous checks here. 1891 * 1892 * Or, since we know how much we need to make read/writeable, 1893 * this can be done once at the program beginning for direct 1894 * access case. By this we overcome limitations of only current 1895 * headroom being accessible. 1896 */ 1897 return sk_skb_try_make_writable(skb, len ? : skb_headlen(skb)); 1898 } 1899 1900 static const struct bpf_func_proto sk_skb_pull_data_proto = { 1901 .func = sk_skb_pull_data, 1902 .gpl_only = false, 1903 .ret_type = RET_INTEGER, 1904 .arg1_type = ARG_PTR_TO_CTX, 1905 .arg2_type = ARG_ANYTHING, 1906 }; 1907 1908 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset, 1909 u64, from, u64, to, u64, flags) 1910 { 1911 __sum16 *ptr; 1912 1913 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK))) 1914 return -EINVAL; 1915 if (unlikely(offset > 0xffff || offset & 1)) 1916 return -EFAULT; 1917 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr)))) 1918 return -EFAULT; 1919 1920 ptr = (__sum16 *)(skb->data + offset); 1921 switch (flags & BPF_F_HDR_FIELD_MASK) { 1922 case 0: 1923 if (unlikely(from != 0)) 1924 return -EINVAL; 1925 1926 csum_replace_by_diff(ptr, to); 1927 break; 1928 case 2: 1929 csum_replace2(ptr, from, to); 1930 break; 1931 case 4: 1932 csum_replace4(ptr, from, to); 1933 break; 1934 default: 1935 return -EINVAL; 1936 } 1937 1938 return 0; 1939 } 1940 1941 static const struct bpf_func_proto bpf_l3_csum_replace_proto = { 1942 .func = bpf_l3_csum_replace, 1943 .gpl_only = false, 1944 .ret_type = RET_INTEGER, 1945 .arg1_type = ARG_PTR_TO_CTX, 1946 .arg2_type = ARG_ANYTHING, 1947 .arg3_type = ARG_ANYTHING, 1948 .arg4_type = ARG_ANYTHING, 1949 .arg5_type = ARG_ANYTHING, 1950 }; 1951 1952 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset, 1953 u64, from, u64, to, u64, flags) 1954 { 1955 bool is_pseudo = flags & BPF_F_PSEUDO_HDR; 1956 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0; 1957 bool do_mforce = flags & BPF_F_MARK_ENFORCE; 1958 __sum16 *ptr; 1959 1960 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE | 1961 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK))) 1962 return -EINVAL; 1963 if (unlikely(offset > 0xffff || offset & 1)) 1964 return -EFAULT; 1965 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr)))) 1966 return -EFAULT; 1967 1968 ptr = (__sum16 *)(skb->data + offset); 1969 if (is_mmzero && !do_mforce && !*ptr) 1970 return 0; 1971 1972 switch (flags & BPF_F_HDR_FIELD_MASK) { 1973 case 0: 1974 if (unlikely(from != 0)) 1975 return -EINVAL; 1976 1977 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo); 1978 break; 1979 case 2: 1980 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo); 1981 break; 1982 case 4: 1983 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo); 1984 break; 1985 default: 1986 return -EINVAL; 1987 } 1988 1989 if (is_mmzero && !*ptr) 1990 *ptr = CSUM_MANGLED_0; 1991 return 0; 1992 } 1993 1994 static const struct bpf_func_proto bpf_l4_csum_replace_proto = { 1995 .func = bpf_l4_csum_replace, 1996 .gpl_only = false, 1997 .ret_type = RET_INTEGER, 1998 .arg1_type = ARG_PTR_TO_CTX, 1999 .arg2_type = ARG_ANYTHING, 2000 .arg3_type = ARG_ANYTHING, 2001 .arg4_type = ARG_ANYTHING, 2002 .arg5_type = ARG_ANYTHING, 2003 }; 2004 2005 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size, 2006 __be32 *, to, u32, to_size, __wsum, seed) 2007 { 2008 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp); 2009 u32 diff_size = from_size + to_size; 2010 int i, j = 0; 2011 2012 /* This is quite flexible, some examples: 2013 * 2014 * from_size == 0, to_size > 0, seed := csum --> pushing data 2015 * from_size > 0, to_size == 0, seed := csum --> pulling data 2016 * from_size > 0, to_size > 0, seed := 0 --> diffing data 2017 * 2018 * Even for diffing, from_size and to_size don't need to be equal. 2019 */ 2020 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) || 2021 diff_size > sizeof(sp->diff))) 2022 return -EINVAL; 2023 2024 for (i = 0; i < from_size / sizeof(__be32); i++, j++) 2025 sp->diff[j] = ~from[i]; 2026 for (i = 0; i < to_size / sizeof(__be32); i++, j++) 2027 sp->diff[j] = to[i]; 2028 2029 return csum_partial(sp->diff, diff_size, seed); 2030 } 2031 2032 static const struct bpf_func_proto bpf_csum_diff_proto = { 2033 .func = bpf_csum_diff, 2034 .gpl_only = false, 2035 .pkt_access = true, 2036 .ret_type = RET_INTEGER, 2037 .arg1_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY, 2038 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 2039 .arg3_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY, 2040 .arg4_type = ARG_CONST_SIZE_OR_ZERO, 2041 .arg5_type = ARG_ANYTHING, 2042 }; 2043 2044 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum) 2045 { 2046 /* The interface is to be used in combination with bpf_csum_diff() 2047 * for direct packet writes. csum rotation for alignment as well 2048 * as emulating csum_sub() can be done from the eBPF program. 2049 */ 2050 if (skb->ip_summed == CHECKSUM_COMPLETE) 2051 return (skb->csum = csum_add(skb->csum, csum)); 2052 2053 return -ENOTSUPP; 2054 } 2055 2056 static const struct bpf_func_proto bpf_csum_update_proto = { 2057 .func = bpf_csum_update, 2058 .gpl_only = false, 2059 .ret_type = RET_INTEGER, 2060 .arg1_type = ARG_PTR_TO_CTX, 2061 .arg2_type = ARG_ANYTHING, 2062 }; 2063 2064 BPF_CALL_2(bpf_csum_level, struct sk_buff *, skb, u64, level) 2065 { 2066 /* The interface is to be used in combination with bpf_skb_adjust_room() 2067 * for encap/decap of packet headers when BPF_F_ADJ_ROOM_NO_CSUM_RESET 2068 * is passed as flags, for example. 2069 */ 2070 switch (level) { 2071 case BPF_CSUM_LEVEL_INC: 2072 __skb_incr_checksum_unnecessary(skb); 2073 break; 2074 case BPF_CSUM_LEVEL_DEC: 2075 __skb_decr_checksum_unnecessary(skb); 2076 break; 2077 case BPF_CSUM_LEVEL_RESET: 2078 __skb_reset_checksum_unnecessary(skb); 2079 break; 2080 case BPF_CSUM_LEVEL_QUERY: 2081 return skb->ip_summed == CHECKSUM_UNNECESSARY ? 2082 skb->csum_level : -EACCES; 2083 default: 2084 return -EINVAL; 2085 } 2086 2087 return 0; 2088 } 2089 2090 static const struct bpf_func_proto bpf_csum_level_proto = { 2091 .func = bpf_csum_level, 2092 .gpl_only = false, 2093 .ret_type = RET_INTEGER, 2094 .arg1_type = ARG_PTR_TO_CTX, 2095 .arg2_type = ARG_ANYTHING, 2096 }; 2097 2098 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb) 2099 { 2100 return dev_forward_skb_nomtu(dev, skb); 2101 } 2102 2103 static inline int __bpf_rx_skb_no_mac(struct net_device *dev, 2104 struct sk_buff *skb) 2105 { 2106 int ret = ____dev_forward_skb(dev, skb, false); 2107 2108 if (likely(!ret)) { 2109 skb->dev = dev; 2110 ret = netif_rx(skb); 2111 } 2112 2113 return ret; 2114 } 2115 2116 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb) 2117 { 2118 int ret; 2119 2120 if (dev_xmit_recursion()) { 2121 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n"); 2122 kfree_skb(skb); 2123 return -ENETDOWN; 2124 } 2125 2126 skb->dev = dev; 2127 skb_set_redirected_noclear(skb, skb_at_tc_ingress(skb)); 2128 skb_clear_tstamp(skb); 2129 2130 dev_xmit_recursion_inc(); 2131 ret = dev_queue_xmit(skb); 2132 dev_xmit_recursion_dec(); 2133 2134 return ret; 2135 } 2136 2137 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev, 2138 u32 flags) 2139 { 2140 unsigned int mlen = skb_network_offset(skb); 2141 2142 if (unlikely(skb->len <= mlen)) { 2143 kfree_skb(skb); 2144 return -ERANGE; 2145 } 2146 2147 if (mlen) { 2148 __skb_pull(skb, mlen); 2149 2150 /* At ingress, the mac header has already been pulled once. 2151 * At egress, skb_pospull_rcsum has to be done in case that 2152 * the skb is originated from ingress (i.e. a forwarded skb) 2153 * to ensure that rcsum starts at net header. 2154 */ 2155 if (!skb_at_tc_ingress(skb)) 2156 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen); 2157 } 2158 skb_pop_mac_header(skb); 2159 skb_reset_mac_len(skb); 2160 return flags & BPF_F_INGRESS ? 2161 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb); 2162 } 2163 2164 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev, 2165 u32 flags) 2166 { 2167 /* Verify that a link layer header is carried */ 2168 if (unlikely(skb->mac_header >= skb->network_header || skb->len == 0)) { 2169 kfree_skb(skb); 2170 return -ERANGE; 2171 } 2172 2173 bpf_push_mac_rcsum(skb); 2174 return flags & BPF_F_INGRESS ? 2175 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb); 2176 } 2177 2178 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev, 2179 u32 flags) 2180 { 2181 if (dev_is_mac_header_xmit(dev)) 2182 return __bpf_redirect_common(skb, dev, flags); 2183 else 2184 return __bpf_redirect_no_mac(skb, dev, flags); 2185 } 2186 2187 #if IS_ENABLED(CONFIG_IPV6) 2188 static int bpf_out_neigh_v6(struct net *net, struct sk_buff *skb, 2189 struct net_device *dev, struct bpf_nh_params *nh) 2190 { 2191 u32 hh_len = LL_RESERVED_SPACE(dev); 2192 const struct in6_addr *nexthop; 2193 struct dst_entry *dst = NULL; 2194 struct neighbour *neigh; 2195 2196 if (dev_xmit_recursion()) { 2197 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n"); 2198 goto out_drop; 2199 } 2200 2201 skb->dev = dev; 2202 skb_clear_tstamp(skb); 2203 2204 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) { 2205 skb = skb_expand_head(skb, hh_len); 2206 if (!skb) 2207 return -ENOMEM; 2208 } 2209 2210 rcu_read_lock(); 2211 if (!nh) { 2212 dst = skb_dst(skb); 2213 nexthop = rt6_nexthop(container_of(dst, struct rt6_info, dst), 2214 &ipv6_hdr(skb)->daddr); 2215 } else { 2216 nexthop = &nh->ipv6_nh; 2217 } 2218 neigh = ip_neigh_gw6(dev, nexthop); 2219 if (likely(!IS_ERR(neigh))) { 2220 int ret; 2221 2222 sock_confirm_neigh(skb, neigh); 2223 local_bh_disable(); 2224 dev_xmit_recursion_inc(); 2225 ret = neigh_output(neigh, skb, false); 2226 dev_xmit_recursion_dec(); 2227 local_bh_enable(); 2228 rcu_read_unlock(); 2229 return ret; 2230 } 2231 rcu_read_unlock_bh(); 2232 if (dst) 2233 IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES); 2234 out_drop: 2235 kfree_skb(skb); 2236 return -ENETDOWN; 2237 } 2238 2239 static int __bpf_redirect_neigh_v6(struct sk_buff *skb, struct net_device *dev, 2240 struct bpf_nh_params *nh) 2241 { 2242 const struct ipv6hdr *ip6h = ipv6_hdr(skb); 2243 struct net *net = dev_net(dev); 2244 int err, ret = NET_XMIT_DROP; 2245 2246 if (!nh) { 2247 struct dst_entry *dst; 2248 struct flowi6 fl6 = { 2249 .flowi6_flags = FLOWI_FLAG_ANYSRC, 2250 .flowi6_mark = skb->mark, 2251 .flowlabel = ip6_flowinfo(ip6h), 2252 .flowi6_oif = dev->ifindex, 2253 .flowi6_proto = ip6h->nexthdr, 2254 .daddr = ip6h->daddr, 2255 .saddr = ip6h->saddr, 2256 }; 2257 2258 dst = ipv6_stub->ipv6_dst_lookup_flow(net, NULL, &fl6, NULL); 2259 if (IS_ERR(dst)) 2260 goto out_drop; 2261 2262 skb_dst_set(skb, dst); 2263 } else if (nh->nh_family != AF_INET6) { 2264 goto out_drop; 2265 } 2266 2267 err = bpf_out_neigh_v6(net, skb, dev, nh); 2268 if (unlikely(net_xmit_eval(err))) 2269 dev->stats.tx_errors++; 2270 else 2271 ret = NET_XMIT_SUCCESS; 2272 goto out_xmit; 2273 out_drop: 2274 dev->stats.tx_errors++; 2275 kfree_skb(skb); 2276 out_xmit: 2277 return ret; 2278 } 2279 #else 2280 static int __bpf_redirect_neigh_v6(struct sk_buff *skb, struct net_device *dev, 2281 struct bpf_nh_params *nh) 2282 { 2283 kfree_skb(skb); 2284 return NET_XMIT_DROP; 2285 } 2286 #endif /* CONFIG_IPV6 */ 2287 2288 #if IS_ENABLED(CONFIG_INET) 2289 static int bpf_out_neigh_v4(struct net *net, struct sk_buff *skb, 2290 struct net_device *dev, struct bpf_nh_params *nh) 2291 { 2292 u32 hh_len = LL_RESERVED_SPACE(dev); 2293 struct neighbour *neigh; 2294 bool is_v6gw = false; 2295 2296 if (dev_xmit_recursion()) { 2297 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n"); 2298 goto out_drop; 2299 } 2300 2301 skb->dev = dev; 2302 skb_clear_tstamp(skb); 2303 2304 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) { 2305 skb = skb_expand_head(skb, hh_len); 2306 if (!skb) 2307 return -ENOMEM; 2308 } 2309 2310 rcu_read_lock(); 2311 if (!nh) { 2312 struct dst_entry *dst = skb_dst(skb); 2313 struct rtable *rt = container_of(dst, struct rtable, dst); 2314 2315 neigh = ip_neigh_for_gw(rt, skb, &is_v6gw); 2316 } else if (nh->nh_family == AF_INET6) { 2317 neigh = ip_neigh_gw6(dev, &nh->ipv6_nh); 2318 is_v6gw = true; 2319 } else if (nh->nh_family == AF_INET) { 2320 neigh = ip_neigh_gw4(dev, nh->ipv4_nh); 2321 } else { 2322 rcu_read_unlock(); 2323 goto out_drop; 2324 } 2325 2326 if (likely(!IS_ERR(neigh))) { 2327 int ret; 2328 2329 sock_confirm_neigh(skb, neigh); 2330 local_bh_disable(); 2331 dev_xmit_recursion_inc(); 2332 ret = neigh_output(neigh, skb, is_v6gw); 2333 dev_xmit_recursion_dec(); 2334 local_bh_enable(); 2335 rcu_read_unlock(); 2336 return ret; 2337 } 2338 rcu_read_unlock(); 2339 out_drop: 2340 kfree_skb(skb); 2341 return -ENETDOWN; 2342 } 2343 2344 static int __bpf_redirect_neigh_v4(struct sk_buff *skb, struct net_device *dev, 2345 struct bpf_nh_params *nh) 2346 { 2347 const struct iphdr *ip4h = ip_hdr(skb); 2348 struct net *net = dev_net(dev); 2349 int err, ret = NET_XMIT_DROP; 2350 2351 if (!nh) { 2352 struct flowi4 fl4 = { 2353 .flowi4_flags = FLOWI_FLAG_ANYSRC, 2354 .flowi4_mark = skb->mark, 2355 .flowi4_tos = RT_TOS(ip4h->tos), 2356 .flowi4_oif = dev->ifindex, 2357 .flowi4_proto = ip4h->protocol, 2358 .daddr = ip4h->daddr, 2359 .saddr = ip4h->saddr, 2360 }; 2361 struct rtable *rt; 2362 2363 rt = ip_route_output_flow(net, &fl4, NULL); 2364 if (IS_ERR(rt)) 2365 goto out_drop; 2366 if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) { 2367 ip_rt_put(rt); 2368 goto out_drop; 2369 } 2370 2371 skb_dst_set(skb, &rt->dst); 2372 } 2373 2374 err = bpf_out_neigh_v4(net, skb, dev, nh); 2375 if (unlikely(net_xmit_eval(err))) 2376 dev->stats.tx_errors++; 2377 else 2378 ret = NET_XMIT_SUCCESS; 2379 goto out_xmit; 2380 out_drop: 2381 dev->stats.tx_errors++; 2382 kfree_skb(skb); 2383 out_xmit: 2384 return ret; 2385 } 2386 #else 2387 static int __bpf_redirect_neigh_v4(struct sk_buff *skb, struct net_device *dev, 2388 struct bpf_nh_params *nh) 2389 { 2390 kfree_skb(skb); 2391 return NET_XMIT_DROP; 2392 } 2393 #endif /* CONFIG_INET */ 2394 2395 static int __bpf_redirect_neigh(struct sk_buff *skb, struct net_device *dev, 2396 struct bpf_nh_params *nh) 2397 { 2398 struct ethhdr *ethh = eth_hdr(skb); 2399 2400 if (unlikely(skb->mac_header >= skb->network_header)) 2401 goto out; 2402 bpf_push_mac_rcsum(skb); 2403 if (is_multicast_ether_addr(ethh->h_dest)) 2404 goto out; 2405 2406 skb_pull(skb, sizeof(*ethh)); 2407 skb_unset_mac_header(skb); 2408 skb_reset_network_header(skb); 2409 2410 if (skb->protocol == htons(ETH_P_IP)) 2411 return __bpf_redirect_neigh_v4(skb, dev, nh); 2412 else if (skb->protocol == htons(ETH_P_IPV6)) 2413 return __bpf_redirect_neigh_v6(skb, dev, nh); 2414 out: 2415 kfree_skb(skb); 2416 return -ENOTSUPP; 2417 } 2418 2419 /* Internal, non-exposed redirect flags. */ 2420 enum { 2421 BPF_F_NEIGH = (1ULL << 1), 2422 BPF_F_PEER = (1ULL << 2), 2423 BPF_F_NEXTHOP = (1ULL << 3), 2424 #define BPF_F_REDIRECT_INTERNAL (BPF_F_NEIGH | BPF_F_PEER | BPF_F_NEXTHOP) 2425 }; 2426 2427 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags) 2428 { 2429 struct net_device *dev; 2430 struct sk_buff *clone; 2431 int ret; 2432 2433 if (unlikely(flags & (~(BPF_F_INGRESS) | BPF_F_REDIRECT_INTERNAL))) 2434 return -EINVAL; 2435 2436 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex); 2437 if (unlikely(!dev)) 2438 return -EINVAL; 2439 2440 clone = skb_clone(skb, GFP_ATOMIC); 2441 if (unlikely(!clone)) 2442 return -ENOMEM; 2443 2444 /* For direct write, we need to keep the invariant that the skbs 2445 * we're dealing with need to be uncloned. Should uncloning fail 2446 * here, we need to free the just generated clone to unclone once 2447 * again. 2448 */ 2449 ret = bpf_try_make_head_writable(skb); 2450 if (unlikely(ret)) { 2451 kfree_skb(clone); 2452 return -ENOMEM; 2453 } 2454 2455 return __bpf_redirect(clone, dev, flags); 2456 } 2457 2458 static const struct bpf_func_proto bpf_clone_redirect_proto = { 2459 .func = bpf_clone_redirect, 2460 .gpl_only = false, 2461 .ret_type = RET_INTEGER, 2462 .arg1_type = ARG_PTR_TO_CTX, 2463 .arg2_type = ARG_ANYTHING, 2464 .arg3_type = ARG_ANYTHING, 2465 }; 2466 2467 DEFINE_PER_CPU(struct bpf_redirect_info, bpf_redirect_info); 2468 EXPORT_PER_CPU_SYMBOL_GPL(bpf_redirect_info); 2469 2470 int skb_do_redirect(struct sk_buff *skb) 2471 { 2472 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 2473 struct net *net = dev_net(skb->dev); 2474 struct net_device *dev; 2475 u32 flags = ri->flags; 2476 2477 dev = dev_get_by_index_rcu(net, ri->tgt_index); 2478 ri->tgt_index = 0; 2479 ri->flags = 0; 2480 if (unlikely(!dev)) 2481 goto out_drop; 2482 if (flags & BPF_F_PEER) { 2483 const struct net_device_ops *ops = dev->netdev_ops; 2484 2485 if (unlikely(!ops->ndo_get_peer_dev || 2486 !skb_at_tc_ingress(skb))) 2487 goto out_drop; 2488 dev = ops->ndo_get_peer_dev(dev); 2489 if (unlikely(!dev || 2490 !(dev->flags & IFF_UP) || 2491 net_eq(net, dev_net(dev)))) 2492 goto out_drop; 2493 skb->dev = dev; 2494 dev_sw_netstats_rx_add(dev, skb->len); 2495 return -EAGAIN; 2496 } 2497 return flags & BPF_F_NEIGH ? 2498 __bpf_redirect_neigh(skb, dev, flags & BPF_F_NEXTHOP ? 2499 &ri->nh : NULL) : 2500 __bpf_redirect(skb, dev, flags); 2501 out_drop: 2502 kfree_skb(skb); 2503 return -EINVAL; 2504 } 2505 2506 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags) 2507 { 2508 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 2509 2510 if (unlikely(flags & (~(BPF_F_INGRESS) | BPF_F_REDIRECT_INTERNAL))) 2511 return TC_ACT_SHOT; 2512 2513 ri->flags = flags; 2514 ri->tgt_index = ifindex; 2515 2516 return TC_ACT_REDIRECT; 2517 } 2518 2519 static const struct bpf_func_proto bpf_redirect_proto = { 2520 .func = bpf_redirect, 2521 .gpl_only = false, 2522 .ret_type = RET_INTEGER, 2523 .arg1_type = ARG_ANYTHING, 2524 .arg2_type = ARG_ANYTHING, 2525 }; 2526 2527 BPF_CALL_2(bpf_redirect_peer, u32, ifindex, u64, flags) 2528 { 2529 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 2530 2531 if (unlikely(flags)) 2532 return TC_ACT_SHOT; 2533 2534 ri->flags = BPF_F_PEER; 2535 ri->tgt_index = ifindex; 2536 2537 return TC_ACT_REDIRECT; 2538 } 2539 2540 static const struct bpf_func_proto bpf_redirect_peer_proto = { 2541 .func = bpf_redirect_peer, 2542 .gpl_only = false, 2543 .ret_type = RET_INTEGER, 2544 .arg1_type = ARG_ANYTHING, 2545 .arg2_type = ARG_ANYTHING, 2546 }; 2547 2548 BPF_CALL_4(bpf_redirect_neigh, u32, ifindex, struct bpf_redir_neigh *, params, 2549 int, plen, u64, flags) 2550 { 2551 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 2552 2553 if (unlikely((plen && plen < sizeof(*params)) || flags)) 2554 return TC_ACT_SHOT; 2555 2556 ri->flags = BPF_F_NEIGH | (plen ? BPF_F_NEXTHOP : 0); 2557 ri->tgt_index = ifindex; 2558 2559 BUILD_BUG_ON(sizeof(struct bpf_redir_neigh) != sizeof(struct bpf_nh_params)); 2560 if (plen) 2561 memcpy(&ri->nh, params, sizeof(ri->nh)); 2562 2563 return TC_ACT_REDIRECT; 2564 } 2565 2566 static const struct bpf_func_proto bpf_redirect_neigh_proto = { 2567 .func = bpf_redirect_neigh, 2568 .gpl_only = false, 2569 .ret_type = RET_INTEGER, 2570 .arg1_type = ARG_ANYTHING, 2571 .arg2_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY, 2572 .arg3_type = ARG_CONST_SIZE_OR_ZERO, 2573 .arg4_type = ARG_ANYTHING, 2574 }; 2575 2576 BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg *, msg, u32, bytes) 2577 { 2578 msg->apply_bytes = bytes; 2579 return 0; 2580 } 2581 2582 static const struct bpf_func_proto bpf_msg_apply_bytes_proto = { 2583 .func = bpf_msg_apply_bytes, 2584 .gpl_only = false, 2585 .ret_type = RET_INTEGER, 2586 .arg1_type = ARG_PTR_TO_CTX, 2587 .arg2_type = ARG_ANYTHING, 2588 }; 2589 2590 BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg *, msg, u32, bytes) 2591 { 2592 msg->cork_bytes = bytes; 2593 return 0; 2594 } 2595 2596 static void sk_msg_reset_curr(struct sk_msg *msg) 2597 { 2598 u32 i = msg->sg.start; 2599 u32 len = 0; 2600 2601 do { 2602 len += sk_msg_elem(msg, i)->length; 2603 sk_msg_iter_var_next(i); 2604 if (len >= msg->sg.size) 2605 break; 2606 } while (i != msg->sg.end); 2607 2608 msg->sg.curr = i; 2609 msg->sg.copybreak = 0; 2610 } 2611 2612 static const struct bpf_func_proto bpf_msg_cork_bytes_proto = { 2613 .func = bpf_msg_cork_bytes, 2614 .gpl_only = false, 2615 .ret_type = RET_INTEGER, 2616 .arg1_type = ARG_PTR_TO_CTX, 2617 .arg2_type = ARG_ANYTHING, 2618 }; 2619 2620 BPF_CALL_4(bpf_msg_pull_data, struct sk_msg *, msg, u32, start, 2621 u32, end, u64, flags) 2622 { 2623 u32 len = 0, offset = 0, copy = 0, poffset = 0, bytes = end - start; 2624 u32 first_sge, last_sge, i, shift, bytes_sg_total; 2625 struct scatterlist *sge; 2626 u8 *raw, *to, *from; 2627 struct page *page; 2628 2629 if (unlikely(flags || end <= start)) 2630 return -EINVAL; 2631 2632 /* First find the starting scatterlist element */ 2633 i = msg->sg.start; 2634 do { 2635 offset += len; 2636 len = sk_msg_elem(msg, i)->length; 2637 if (start < offset + len) 2638 break; 2639 sk_msg_iter_var_next(i); 2640 } while (i != msg->sg.end); 2641 2642 if (unlikely(start >= offset + len)) 2643 return -EINVAL; 2644 2645 first_sge = i; 2646 /* The start may point into the sg element so we need to also 2647 * account for the headroom. 2648 */ 2649 bytes_sg_total = start - offset + bytes; 2650 if (!test_bit(i, msg->sg.copy) && bytes_sg_total <= len) 2651 goto out; 2652 2653 /* At this point we need to linearize multiple scatterlist 2654 * elements or a single shared page. Either way we need to 2655 * copy into a linear buffer exclusively owned by BPF. Then 2656 * place the buffer in the scatterlist and fixup the original 2657 * entries by removing the entries now in the linear buffer 2658 * and shifting the remaining entries. For now we do not try 2659 * to copy partial entries to avoid complexity of running out 2660 * of sg_entry slots. The downside is reading a single byte 2661 * will copy the entire sg entry. 2662 */ 2663 do { 2664 copy += sk_msg_elem(msg, i)->length; 2665 sk_msg_iter_var_next(i); 2666 if (bytes_sg_total <= copy) 2667 break; 2668 } while (i != msg->sg.end); 2669 last_sge = i; 2670 2671 if (unlikely(bytes_sg_total > copy)) 2672 return -EINVAL; 2673 2674 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP, 2675 get_order(copy)); 2676 if (unlikely(!page)) 2677 return -ENOMEM; 2678 2679 raw = page_address(page); 2680 i = first_sge; 2681 do { 2682 sge = sk_msg_elem(msg, i); 2683 from = sg_virt(sge); 2684 len = sge->length; 2685 to = raw + poffset; 2686 2687 memcpy(to, from, len); 2688 poffset += len; 2689 sge->length = 0; 2690 put_page(sg_page(sge)); 2691 2692 sk_msg_iter_var_next(i); 2693 } while (i != last_sge); 2694 2695 sg_set_page(&msg->sg.data[first_sge], page, copy, 0); 2696 2697 /* To repair sg ring we need to shift entries. If we only 2698 * had a single entry though we can just replace it and 2699 * be done. Otherwise walk the ring and shift the entries. 2700 */ 2701 WARN_ON_ONCE(last_sge == first_sge); 2702 shift = last_sge > first_sge ? 2703 last_sge - first_sge - 1 : 2704 NR_MSG_FRAG_IDS - first_sge + last_sge - 1; 2705 if (!shift) 2706 goto out; 2707 2708 i = first_sge; 2709 sk_msg_iter_var_next(i); 2710 do { 2711 u32 move_from; 2712 2713 if (i + shift >= NR_MSG_FRAG_IDS) 2714 move_from = i + shift - NR_MSG_FRAG_IDS; 2715 else 2716 move_from = i + shift; 2717 if (move_from == msg->sg.end) 2718 break; 2719 2720 msg->sg.data[i] = msg->sg.data[move_from]; 2721 msg->sg.data[move_from].length = 0; 2722 msg->sg.data[move_from].page_link = 0; 2723 msg->sg.data[move_from].offset = 0; 2724 sk_msg_iter_var_next(i); 2725 } while (1); 2726 2727 msg->sg.end = msg->sg.end - shift > msg->sg.end ? 2728 msg->sg.end - shift + NR_MSG_FRAG_IDS : 2729 msg->sg.end - shift; 2730 out: 2731 sk_msg_reset_curr(msg); 2732 msg->data = sg_virt(&msg->sg.data[first_sge]) + start - offset; 2733 msg->data_end = msg->data + bytes; 2734 return 0; 2735 } 2736 2737 static const struct bpf_func_proto bpf_msg_pull_data_proto = { 2738 .func = bpf_msg_pull_data, 2739 .gpl_only = false, 2740 .ret_type = RET_INTEGER, 2741 .arg1_type = ARG_PTR_TO_CTX, 2742 .arg2_type = ARG_ANYTHING, 2743 .arg3_type = ARG_ANYTHING, 2744 .arg4_type = ARG_ANYTHING, 2745 }; 2746 2747 BPF_CALL_4(bpf_msg_push_data, struct sk_msg *, msg, u32, start, 2748 u32, len, u64, flags) 2749 { 2750 struct scatterlist sge, nsge, nnsge, rsge = {0}, *psge; 2751 u32 new, i = 0, l = 0, space, copy = 0, offset = 0; 2752 u8 *raw, *to, *from; 2753 struct page *page; 2754 2755 if (unlikely(flags)) 2756 return -EINVAL; 2757 2758 if (unlikely(len == 0)) 2759 return 0; 2760 2761 /* First find the starting scatterlist element */ 2762 i = msg->sg.start; 2763 do { 2764 offset += l; 2765 l = sk_msg_elem(msg, i)->length; 2766 2767 if (start < offset + l) 2768 break; 2769 sk_msg_iter_var_next(i); 2770 } while (i != msg->sg.end); 2771 2772 if (start >= offset + l) 2773 return -EINVAL; 2774 2775 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg); 2776 2777 /* If no space available will fallback to copy, we need at 2778 * least one scatterlist elem available to push data into 2779 * when start aligns to the beginning of an element or two 2780 * when it falls inside an element. We handle the start equals 2781 * offset case because its the common case for inserting a 2782 * header. 2783 */ 2784 if (!space || (space == 1 && start != offset)) 2785 copy = msg->sg.data[i].length; 2786 2787 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP, 2788 get_order(copy + len)); 2789 if (unlikely(!page)) 2790 return -ENOMEM; 2791 2792 if (copy) { 2793 int front, back; 2794 2795 raw = page_address(page); 2796 2797 psge = sk_msg_elem(msg, i); 2798 front = start - offset; 2799 back = psge->length - front; 2800 from = sg_virt(psge); 2801 2802 if (front) 2803 memcpy(raw, from, front); 2804 2805 if (back) { 2806 from += front; 2807 to = raw + front + len; 2808 2809 memcpy(to, from, back); 2810 } 2811 2812 put_page(sg_page(psge)); 2813 } else if (start - offset) { 2814 psge = sk_msg_elem(msg, i); 2815 rsge = sk_msg_elem_cpy(msg, i); 2816 2817 psge->length = start - offset; 2818 rsge.length -= psge->length; 2819 rsge.offset += start; 2820 2821 sk_msg_iter_var_next(i); 2822 sg_unmark_end(psge); 2823 sg_unmark_end(&rsge); 2824 sk_msg_iter_next(msg, end); 2825 } 2826 2827 /* Slot(s) to place newly allocated data */ 2828 new = i; 2829 2830 /* Shift one or two slots as needed */ 2831 if (!copy) { 2832 sge = sk_msg_elem_cpy(msg, i); 2833 2834 sk_msg_iter_var_next(i); 2835 sg_unmark_end(&sge); 2836 sk_msg_iter_next(msg, end); 2837 2838 nsge = sk_msg_elem_cpy(msg, i); 2839 if (rsge.length) { 2840 sk_msg_iter_var_next(i); 2841 nnsge = sk_msg_elem_cpy(msg, i); 2842 } 2843 2844 while (i != msg->sg.end) { 2845 msg->sg.data[i] = sge; 2846 sge = nsge; 2847 sk_msg_iter_var_next(i); 2848 if (rsge.length) { 2849 nsge = nnsge; 2850 nnsge = sk_msg_elem_cpy(msg, i); 2851 } else { 2852 nsge = sk_msg_elem_cpy(msg, i); 2853 } 2854 } 2855 } 2856 2857 /* Place newly allocated data buffer */ 2858 sk_mem_charge(msg->sk, len); 2859 msg->sg.size += len; 2860 __clear_bit(new, msg->sg.copy); 2861 sg_set_page(&msg->sg.data[new], page, len + copy, 0); 2862 if (rsge.length) { 2863 get_page(sg_page(&rsge)); 2864 sk_msg_iter_var_next(new); 2865 msg->sg.data[new] = rsge; 2866 } 2867 2868 sk_msg_reset_curr(msg); 2869 sk_msg_compute_data_pointers(msg); 2870 return 0; 2871 } 2872 2873 static const struct bpf_func_proto bpf_msg_push_data_proto = { 2874 .func = bpf_msg_push_data, 2875 .gpl_only = false, 2876 .ret_type = RET_INTEGER, 2877 .arg1_type = ARG_PTR_TO_CTX, 2878 .arg2_type = ARG_ANYTHING, 2879 .arg3_type = ARG_ANYTHING, 2880 .arg4_type = ARG_ANYTHING, 2881 }; 2882 2883 static void sk_msg_shift_left(struct sk_msg *msg, int i) 2884 { 2885 int prev; 2886 2887 do { 2888 prev = i; 2889 sk_msg_iter_var_next(i); 2890 msg->sg.data[prev] = msg->sg.data[i]; 2891 } while (i != msg->sg.end); 2892 2893 sk_msg_iter_prev(msg, end); 2894 } 2895 2896 static void sk_msg_shift_right(struct sk_msg *msg, int i) 2897 { 2898 struct scatterlist tmp, sge; 2899 2900 sk_msg_iter_next(msg, end); 2901 sge = sk_msg_elem_cpy(msg, i); 2902 sk_msg_iter_var_next(i); 2903 tmp = sk_msg_elem_cpy(msg, i); 2904 2905 while (i != msg->sg.end) { 2906 msg->sg.data[i] = sge; 2907 sk_msg_iter_var_next(i); 2908 sge = tmp; 2909 tmp = sk_msg_elem_cpy(msg, i); 2910 } 2911 } 2912 2913 BPF_CALL_4(bpf_msg_pop_data, struct sk_msg *, msg, u32, start, 2914 u32, len, u64, flags) 2915 { 2916 u32 i = 0, l = 0, space, offset = 0; 2917 u64 last = start + len; 2918 int pop; 2919 2920 if (unlikely(flags)) 2921 return -EINVAL; 2922 2923 /* First find the starting scatterlist element */ 2924 i = msg->sg.start; 2925 do { 2926 offset += l; 2927 l = sk_msg_elem(msg, i)->length; 2928 2929 if (start < offset + l) 2930 break; 2931 sk_msg_iter_var_next(i); 2932 } while (i != msg->sg.end); 2933 2934 /* Bounds checks: start and pop must be inside message */ 2935 if (start >= offset + l || last >= msg->sg.size) 2936 return -EINVAL; 2937 2938 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg); 2939 2940 pop = len; 2941 /* --------------| offset 2942 * -| start |-------- len -------| 2943 * 2944 * |----- a ----|-------- pop -------|----- b ----| 2945 * |______________________________________________| length 2946 * 2947 * 2948 * a: region at front of scatter element to save 2949 * b: region at back of scatter element to save when length > A + pop 2950 * pop: region to pop from element, same as input 'pop' here will be 2951 * decremented below per iteration. 2952 * 2953 * Two top-level cases to handle when start != offset, first B is non 2954 * zero and second B is zero corresponding to when a pop includes more 2955 * than one element. 2956 * 2957 * Then if B is non-zero AND there is no space allocate space and 2958 * compact A, B regions into page. If there is space shift ring to 2959 * the rigth free'ing the next element in ring to place B, leaving 2960 * A untouched except to reduce length. 2961 */ 2962 if (start != offset) { 2963 struct scatterlist *nsge, *sge = sk_msg_elem(msg, i); 2964 int a = start; 2965 int b = sge->length - pop - a; 2966 2967 sk_msg_iter_var_next(i); 2968 2969 if (pop < sge->length - a) { 2970 if (space) { 2971 sge->length = a; 2972 sk_msg_shift_right(msg, i); 2973 nsge = sk_msg_elem(msg, i); 2974 get_page(sg_page(sge)); 2975 sg_set_page(nsge, 2976 sg_page(sge), 2977 b, sge->offset + pop + a); 2978 } else { 2979 struct page *page, *orig; 2980 u8 *to, *from; 2981 2982 page = alloc_pages(__GFP_NOWARN | 2983 __GFP_COMP | GFP_ATOMIC, 2984 get_order(a + b)); 2985 if (unlikely(!page)) 2986 return -ENOMEM; 2987 2988 sge->length = a; 2989 orig = sg_page(sge); 2990 from = sg_virt(sge); 2991 to = page_address(page); 2992 memcpy(to, from, a); 2993 memcpy(to + a, from + a + pop, b); 2994 sg_set_page(sge, page, a + b, 0); 2995 put_page(orig); 2996 } 2997 pop = 0; 2998 } else if (pop >= sge->length - a) { 2999 pop -= (sge->length - a); 3000 sge->length = a; 3001 } 3002 } 3003 3004 /* From above the current layout _must_ be as follows, 3005 * 3006 * -| offset 3007 * -| start 3008 * 3009 * |---- pop ---|---------------- b ------------| 3010 * |____________________________________________| length 3011 * 3012 * Offset and start of the current msg elem are equal because in the 3013 * previous case we handled offset != start and either consumed the 3014 * entire element and advanced to the next element OR pop == 0. 3015 * 3016 * Two cases to handle here are first pop is less than the length 3017 * leaving some remainder b above. Simply adjust the element's layout 3018 * in this case. Or pop >= length of the element so that b = 0. In this 3019 * case advance to next element decrementing pop. 3020 */ 3021 while (pop) { 3022 struct scatterlist *sge = sk_msg_elem(msg, i); 3023 3024 if (pop < sge->length) { 3025 sge->length -= pop; 3026 sge->offset += pop; 3027 pop = 0; 3028 } else { 3029 pop -= sge->length; 3030 sk_msg_shift_left(msg, i); 3031 } 3032 sk_msg_iter_var_next(i); 3033 } 3034 3035 sk_mem_uncharge(msg->sk, len - pop); 3036 msg->sg.size -= (len - pop); 3037 sk_msg_reset_curr(msg); 3038 sk_msg_compute_data_pointers(msg); 3039 return 0; 3040 } 3041 3042 static const struct bpf_func_proto bpf_msg_pop_data_proto = { 3043 .func = bpf_msg_pop_data, 3044 .gpl_only = false, 3045 .ret_type = RET_INTEGER, 3046 .arg1_type = ARG_PTR_TO_CTX, 3047 .arg2_type = ARG_ANYTHING, 3048 .arg3_type = ARG_ANYTHING, 3049 .arg4_type = ARG_ANYTHING, 3050 }; 3051 3052 #ifdef CONFIG_CGROUP_NET_CLASSID 3053 BPF_CALL_0(bpf_get_cgroup_classid_curr) 3054 { 3055 return __task_get_classid(current); 3056 } 3057 3058 const struct bpf_func_proto bpf_get_cgroup_classid_curr_proto = { 3059 .func = bpf_get_cgroup_classid_curr, 3060 .gpl_only = false, 3061 .ret_type = RET_INTEGER, 3062 }; 3063 3064 BPF_CALL_1(bpf_skb_cgroup_classid, const struct sk_buff *, skb) 3065 { 3066 struct sock *sk = skb_to_full_sk(skb); 3067 3068 if (!sk || !sk_fullsock(sk)) 3069 return 0; 3070 3071 return sock_cgroup_classid(&sk->sk_cgrp_data); 3072 } 3073 3074 static const struct bpf_func_proto bpf_skb_cgroup_classid_proto = { 3075 .func = bpf_skb_cgroup_classid, 3076 .gpl_only = false, 3077 .ret_type = RET_INTEGER, 3078 .arg1_type = ARG_PTR_TO_CTX, 3079 }; 3080 #endif 3081 3082 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb) 3083 { 3084 return task_get_classid(skb); 3085 } 3086 3087 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = { 3088 .func = bpf_get_cgroup_classid, 3089 .gpl_only = false, 3090 .ret_type = RET_INTEGER, 3091 .arg1_type = ARG_PTR_TO_CTX, 3092 }; 3093 3094 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb) 3095 { 3096 return dst_tclassid(skb); 3097 } 3098 3099 static const struct bpf_func_proto bpf_get_route_realm_proto = { 3100 .func = bpf_get_route_realm, 3101 .gpl_only = false, 3102 .ret_type = RET_INTEGER, 3103 .arg1_type = ARG_PTR_TO_CTX, 3104 }; 3105 3106 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb) 3107 { 3108 /* If skb_clear_hash() was called due to mangling, we can 3109 * trigger SW recalculation here. Later access to hash 3110 * can then use the inline skb->hash via context directly 3111 * instead of calling this helper again. 3112 */ 3113 return skb_get_hash(skb); 3114 } 3115 3116 static const struct bpf_func_proto bpf_get_hash_recalc_proto = { 3117 .func = bpf_get_hash_recalc, 3118 .gpl_only = false, 3119 .ret_type = RET_INTEGER, 3120 .arg1_type = ARG_PTR_TO_CTX, 3121 }; 3122 3123 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb) 3124 { 3125 /* After all direct packet write, this can be used once for 3126 * triggering a lazy recalc on next skb_get_hash() invocation. 3127 */ 3128 skb_clear_hash(skb); 3129 return 0; 3130 } 3131 3132 static const struct bpf_func_proto bpf_set_hash_invalid_proto = { 3133 .func = bpf_set_hash_invalid, 3134 .gpl_only = false, 3135 .ret_type = RET_INTEGER, 3136 .arg1_type = ARG_PTR_TO_CTX, 3137 }; 3138 3139 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash) 3140 { 3141 /* Set user specified hash as L4(+), so that it gets returned 3142 * on skb_get_hash() call unless BPF prog later on triggers a 3143 * skb_clear_hash(). 3144 */ 3145 __skb_set_sw_hash(skb, hash, true); 3146 return 0; 3147 } 3148 3149 static const struct bpf_func_proto bpf_set_hash_proto = { 3150 .func = bpf_set_hash, 3151 .gpl_only = false, 3152 .ret_type = RET_INTEGER, 3153 .arg1_type = ARG_PTR_TO_CTX, 3154 .arg2_type = ARG_ANYTHING, 3155 }; 3156 3157 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto, 3158 u16, vlan_tci) 3159 { 3160 int ret; 3161 3162 if (unlikely(vlan_proto != htons(ETH_P_8021Q) && 3163 vlan_proto != htons(ETH_P_8021AD))) 3164 vlan_proto = htons(ETH_P_8021Q); 3165 3166 bpf_push_mac_rcsum(skb); 3167 ret = skb_vlan_push(skb, vlan_proto, vlan_tci); 3168 bpf_pull_mac_rcsum(skb); 3169 3170 bpf_compute_data_pointers(skb); 3171 return ret; 3172 } 3173 3174 static const struct bpf_func_proto bpf_skb_vlan_push_proto = { 3175 .func = bpf_skb_vlan_push, 3176 .gpl_only = false, 3177 .ret_type = RET_INTEGER, 3178 .arg1_type = ARG_PTR_TO_CTX, 3179 .arg2_type = ARG_ANYTHING, 3180 .arg3_type = ARG_ANYTHING, 3181 }; 3182 3183 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb) 3184 { 3185 int ret; 3186 3187 bpf_push_mac_rcsum(skb); 3188 ret = skb_vlan_pop(skb); 3189 bpf_pull_mac_rcsum(skb); 3190 3191 bpf_compute_data_pointers(skb); 3192 return ret; 3193 } 3194 3195 static const struct bpf_func_proto bpf_skb_vlan_pop_proto = { 3196 .func = bpf_skb_vlan_pop, 3197 .gpl_only = false, 3198 .ret_type = RET_INTEGER, 3199 .arg1_type = ARG_PTR_TO_CTX, 3200 }; 3201 3202 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len) 3203 { 3204 /* Caller already did skb_cow() with len as headroom, 3205 * so no need to do it here. 3206 */ 3207 skb_push(skb, len); 3208 memmove(skb->data, skb->data + len, off); 3209 memset(skb->data + off, 0, len); 3210 3211 /* No skb_postpush_rcsum(skb, skb->data + off, len) 3212 * needed here as it does not change the skb->csum 3213 * result for checksum complete when summing over 3214 * zeroed blocks. 3215 */ 3216 return 0; 3217 } 3218 3219 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len) 3220 { 3221 void *old_data; 3222 3223 /* skb_ensure_writable() is not needed here, as we're 3224 * already working on an uncloned skb. 3225 */ 3226 if (unlikely(!pskb_may_pull(skb, off + len))) 3227 return -ENOMEM; 3228 3229 old_data = skb->data; 3230 __skb_pull(skb, len); 3231 skb_postpull_rcsum(skb, old_data + off, len); 3232 memmove(skb->data, old_data, off); 3233 3234 return 0; 3235 } 3236 3237 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len) 3238 { 3239 bool trans_same = skb->transport_header == skb->network_header; 3240 int ret; 3241 3242 /* There's no need for __skb_push()/__skb_pull() pair to 3243 * get to the start of the mac header as we're guaranteed 3244 * to always start from here under eBPF. 3245 */ 3246 ret = bpf_skb_generic_push(skb, off, len); 3247 if (likely(!ret)) { 3248 skb->mac_header -= len; 3249 skb->network_header -= len; 3250 if (trans_same) 3251 skb->transport_header = skb->network_header; 3252 } 3253 3254 return ret; 3255 } 3256 3257 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len) 3258 { 3259 bool trans_same = skb->transport_header == skb->network_header; 3260 int ret; 3261 3262 /* Same here, __skb_push()/__skb_pull() pair not needed. */ 3263 ret = bpf_skb_generic_pop(skb, off, len); 3264 if (likely(!ret)) { 3265 skb->mac_header += len; 3266 skb->network_header += len; 3267 if (trans_same) 3268 skb->transport_header = skb->network_header; 3269 } 3270 3271 return ret; 3272 } 3273 3274 static int bpf_skb_proto_4_to_6(struct sk_buff *skb) 3275 { 3276 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr); 3277 u32 off = skb_mac_header_len(skb); 3278 int ret; 3279 3280 ret = skb_cow(skb, len_diff); 3281 if (unlikely(ret < 0)) 3282 return ret; 3283 3284 ret = bpf_skb_net_hdr_push(skb, off, len_diff); 3285 if (unlikely(ret < 0)) 3286 return ret; 3287 3288 if (skb_is_gso(skb)) { 3289 struct skb_shared_info *shinfo = skb_shinfo(skb); 3290 3291 /* SKB_GSO_TCPV4 needs to be changed into SKB_GSO_TCPV6. */ 3292 if (shinfo->gso_type & SKB_GSO_TCPV4) { 3293 shinfo->gso_type &= ~SKB_GSO_TCPV4; 3294 shinfo->gso_type |= SKB_GSO_TCPV6; 3295 } 3296 } 3297 3298 skb->protocol = htons(ETH_P_IPV6); 3299 skb_clear_hash(skb); 3300 3301 return 0; 3302 } 3303 3304 static int bpf_skb_proto_6_to_4(struct sk_buff *skb) 3305 { 3306 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr); 3307 u32 off = skb_mac_header_len(skb); 3308 int ret; 3309 3310 ret = skb_unclone(skb, GFP_ATOMIC); 3311 if (unlikely(ret < 0)) 3312 return ret; 3313 3314 ret = bpf_skb_net_hdr_pop(skb, off, len_diff); 3315 if (unlikely(ret < 0)) 3316 return ret; 3317 3318 if (skb_is_gso(skb)) { 3319 struct skb_shared_info *shinfo = skb_shinfo(skb); 3320 3321 /* SKB_GSO_TCPV6 needs to be changed into SKB_GSO_TCPV4. */ 3322 if (shinfo->gso_type & SKB_GSO_TCPV6) { 3323 shinfo->gso_type &= ~SKB_GSO_TCPV6; 3324 shinfo->gso_type |= SKB_GSO_TCPV4; 3325 } 3326 } 3327 3328 skb->protocol = htons(ETH_P_IP); 3329 skb_clear_hash(skb); 3330 3331 return 0; 3332 } 3333 3334 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto) 3335 { 3336 __be16 from_proto = skb->protocol; 3337 3338 if (from_proto == htons(ETH_P_IP) && 3339 to_proto == htons(ETH_P_IPV6)) 3340 return bpf_skb_proto_4_to_6(skb); 3341 3342 if (from_proto == htons(ETH_P_IPV6) && 3343 to_proto == htons(ETH_P_IP)) 3344 return bpf_skb_proto_6_to_4(skb); 3345 3346 return -ENOTSUPP; 3347 } 3348 3349 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto, 3350 u64, flags) 3351 { 3352 int ret; 3353 3354 if (unlikely(flags)) 3355 return -EINVAL; 3356 3357 /* General idea is that this helper does the basic groundwork 3358 * needed for changing the protocol, and eBPF program fills the 3359 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace() 3360 * and other helpers, rather than passing a raw buffer here. 3361 * 3362 * The rationale is to keep this minimal and without a need to 3363 * deal with raw packet data. F.e. even if we would pass buffers 3364 * here, the program still needs to call the bpf_lX_csum_replace() 3365 * helpers anyway. Plus, this way we keep also separation of 3366 * concerns, since f.e. bpf_skb_store_bytes() should only take 3367 * care of stores. 3368 * 3369 * Currently, additional options and extension header space are 3370 * not supported, but flags register is reserved so we can adapt 3371 * that. For offloads, we mark packet as dodgy, so that headers 3372 * need to be verified first. 3373 */ 3374 ret = bpf_skb_proto_xlat(skb, proto); 3375 bpf_compute_data_pointers(skb); 3376 return ret; 3377 } 3378 3379 static const struct bpf_func_proto bpf_skb_change_proto_proto = { 3380 .func = bpf_skb_change_proto, 3381 .gpl_only = false, 3382 .ret_type = RET_INTEGER, 3383 .arg1_type = ARG_PTR_TO_CTX, 3384 .arg2_type = ARG_ANYTHING, 3385 .arg3_type = ARG_ANYTHING, 3386 }; 3387 3388 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type) 3389 { 3390 /* We only allow a restricted subset to be changed for now. */ 3391 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) || 3392 !skb_pkt_type_ok(pkt_type))) 3393 return -EINVAL; 3394 3395 skb->pkt_type = pkt_type; 3396 return 0; 3397 } 3398 3399 static const struct bpf_func_proto bpf_skb_change_type_proto = { 3400 .func = bpf_skb_change_type, 3401 .gpl_only = false, 3402 .ret_type = RET_INTEGER, 3403 .arg1_type = ARG_PTR_TO_CTX, 3404 .arg2_type = ARG_ANYTHING, 3405 }; 3406 3407 static u32 bpf_skb_net_base_len(const struct sk_buff *skb) 3408 { 3409 switch (skb->protocol) { 3410 case htons(ETH_P_IP): 3411 return sizeof(struct iphdr); 3412 case htons(ETH_P_IPV6): 3413 return sizeof(struct ipv6hdr); 3414 default: 3415 return ~0U; 3416 } 3417 } 3418 3419 #define BPF_F_ADJ_ROOM_ENCAP_L3_MASK (BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 | \ 3420 BPF_F_ADJ_ROOM_ENCAP_L3_IPV6) 3421 3422 #define BPF_F_ADJ_ROOM_DECAP_L3_MASK (BPF_F_ADJ_ROOM_DECAP_L3_IPV4 | \ 3423 BPF_F_ADJ_ROOM_DECAP_L3_IPV6) 3424 3425 #define BPF_F_ADJ_ROOM_MASK (BPF_F_ADJ_ROOM_FIXED_GSO | \ 3426 BPF_F_ADJ_ROOM_ENCAP_L3_MASK | \ 3427 BPF_F_ADJ_ROOM_ENCAP_L4_GRE | \ 3428 BPF_F_ADJ_ROOM_ENCAP_L4_UDP | \ 3429 BPF_F_ADJ_ROOM_ENCAP_L2_ETH | \ 3430 BPF_F_ADJ_ROOM_ENCAP_L2( \ 3431 BPF_ADJ_ROOM_ENCAP_L2_MASK) | \ 3432 BPF_F_ADJ_ROOM_DECAP_L3_MASK) 3433 3434 static int bpf_skb_net_grow(struct sk_buff *skb, u32 off, u32 len_diff, 3435 u64 flags) 3436 { 3437 u8 inner_mac_len = flags >> BPF_ADJ_ROOM_ENCAP_L2_SHIFT; 3438 bool encap = flags & BPF_F_ADJ_ROOM_ENCAP_L3_MASK; 3439 u16 mac_len = 0, inner_net = 0, inner_trans = 0; 3440 unsigned int gso_type = SKB_GSO_DODGY; 3441 int ret; 3442 3443 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) { 3444 /* udp gso_size delineates datagrams, only allow if fixed */ 3445 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) || 3446 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO)) 3447 return -ENOTSUPP; 3448 } 3449 3450 ret = skb_cow_head(skb, len_diff); 3451 if (unlikely(ret < 0)) 3452 return ret; 3453 3454 if (encap) { 3455 if (skb->protocol != htons(ETH_P_IP) && 3456 skb->protocol != htons(ETH_P_IPV6)) 3457 return -ENOTSUPP; 3458 3459 if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 && 3460 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6) 3461 return -EINVAL; 3462 3463 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE && 3464 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP) 3465 return -EINVAL; 3466 3467 if (flags & BPF_F_ADJ_ROOM_ENCAP_L2_ETH && 3468 inner_mac_len < ETH_HLEN) 3469 return -EINVAL; 3470 3471 if (skb->encapsulation) 3472 return -EALREADY; 3473 3474 mac_len = skb->network_header - skb->mac_header; 3475 inner_net = skb->network_header; 3476 if (inner_mac_len > len_diff) 3477 return -EINVAL; 3478 inner_trans = skb->transport_header; 3479 } 3480 3481 ret = bpf_skb_net_hdr_push(skb, off, len_diff); 3482 if (unlikely(ret < 0)) 3483 return ret; 3484 3485 if (encap) { 3486 skb->inner_mac_header = inner_net - inner_mac_len; 3487 skb->inner_network_header = inner_net; 3488 skb->inner_transport_header = inner_trans; 3489 3490 if (flags & BPF_F_ADJ_ROOM_ENCAP_L2_ETH) 3491 skb_set_inner_protocol(skb, htons(ETH_P_TEB)); 3492 else 3493 skb_set_inner_protocol(skb, skb->protocol); 3494 3495 skb->encapsulation = 1; 3496 skb_set_network_header(skb, mac_len); 3497 3498 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP) 3499 gso_type |= SKB_GSO_UDP_TUNNEL; 3500 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE) 3501 gso_type |= SKB_GSO_GRE; 3502 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6) 3503 gso_type |= SKB_GSO_IPXIP6; 3504 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4) 3505 gso_type |= SKB_GSO_IPXIP4; 3506 3507 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE || 3508 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP) { 3509 int nh_len = flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 ? 3510 sizeof(struct ipv6hdr) : 3511 sizeof(struct iphdr); 3512 3513 skb_set_transport_header(skb, mac_len + nh_len); 3514 } 3515 3516 /* Match skb->protocol to new outer l3 protocol */ 3517 if (skb->protocol == htons(ETH_P_IP) && 3518 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6) 3519 skb->protocol = htons(ETH_P_IPV6); 3520 else if (skb->protocol == htons(ETH_P_IPV6) && 3521 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4) 3522 skb->protocol = htons(ETH_P_IP); 3523 } 3524 3525 if (skb_is_gso(skb)) { 3526 struct skb_shared_info *shinfo = skb_shinfo(skb); 3527 3528 /* Due to header grow, MSS needs to be downgraded. */ 3529 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO)) 3530 skb_decrease_gso_size(shinfo, len_diff); 3531 3532 /* Header must be checked, and gso_segs recomputed. */ 3533 shinfo->gso_type |= gso_type; 3534 shinfo->gso_segs = 0; 3535 } 3536 3537 return 0; 3538 } 3539 3540 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 off, u32 len_diff, 3541 u64 flags) 3542 { 3543 int ret; 3544 3545 if (unlikely(flags & ~(BPF_F_ADJ_ROOM_FIXED_GSO | 3546 BPF_F_ADJ_ROOM_DECAP_L3_MASK | 3547 BPF_F_ADJ_ROOM_NO_CSUM_RESET))) 3548 return -EINVAL; 3549 3550 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) { 3551 /* udp gso_size delineates datagrams, only allow if fixed */ 3552 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) || 3553 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO)) 3554 return -ENOTSUPP; 3555 } 3556 3557 ret = skb_unclone(skb, GFP_ATOMIC); 3558 if (unlikely(ret < 0)) 3559 return ret; 3560 3561 ret = bpf_skb_net_hdr_pop(skb, off, len_diff); 3562 if (unlikely(ret < 0)) 3563 return ret; 3564 3565 /* Match skb->protocol to new outer l3 protocol */ 3566 if (skb->protocol == htons(ETH_P_IP) && 3567 flags & BPF_F_ADJ_ROOM_DECAP_L3_IPV6) 3568 skb->protocol = htons(ETH_P_IPV6); 3569 else if (skb->protocol == htons(ETH_P_IPV6) && 3570 flags & BPF_F_ADJ_ROOM_DECAP_L3_IPV4) 3571 skb->protocol = htons(ETH_P_IP); 3572 3573 if (skb_is_gso(skb)) { 3574 struct skb_shared_info *shinfo = skb_shinfo(skb); 3575 3576 /* Due to header shrink, MSS can be upgraded. */ 3577 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO)) 3578 skb_increase_gso_size(shinfo, len_diff); 3579 3580 /* Header must be checked, and gso_segs recomputed. */ 3581 shinfo->gso_type |= SKB_GSO_DODGY; 3582 shinfo->gso_segs = 0; 3583 } 3584 3585 return 0; 3586 } 3587 3588 #define BPF_SKB_MAX_LEN SKB_MAX_ALLOC 3589 3590 BPF_CALL_4(sk_skb_adjust_room, struct sk_buff *, skb, s32, len_diff, 3591 u32, mode, u64, flags) 3592 { 3593 u32 len_diff_abs = abs(len_diff); 3594 bool shrink = len_diff < 0; 3595 int ret = 0; 3596 3597 if (unlikely(flags || mode)) 3598 return -EINVAL; 3599 if (unlikely(len_diff_abs > 0xfffU)) 3600 return -EFAULT; 3601 3602 if (!shrink) { 3603 ret = skb_cow(skb, len_diff); 3604 if (unlikely(ret < 0)) 3605 return ret; 3606 __skb_push(skb, len_diff_abs); 3607 memset(skb->data, 0, len_diff_abs); 3608 } else { 3609 if (unlikely(!pskb_may_pull(skb, len_diff_abs))) 3610 return -ENOMEM; 3611 __skb_pull(skb, len_diff_abs); 3612 } 3613 if (tls_sw_has_ctx_rx(skb->sk)) { 3614 struct strp_msg *rxm = strp_msg(skb); 3615 3616 rxm->full_len += len_diff; 3617 } 3618 return ret; 3619 } 3620 3621 static const struct bpf_func_proto sk_skb_adjust_room_proto = { 3622 .func = sk_skb_adjust_room, 3623 .gpl_only = false, 3624 .ret_type = RET_INTEGER, 3625 .arg1_type = ARG_PTR_TO_CTX, 3626 .arg2_type = ARG_ANYTHING, 3627 .arg3_type = ARG_ANYTHING, 3628 .arg4_type = ARG_ANYTHING, 3629 }; 3630 3631 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff, 3632 u32, mode, u64, flags) 3633 { 3634 u32 len_cur, len_diff_abs = abs(len_diff); 3635 u32 len_min = bpf_skb_net_base_len(skb); 3636 u32 len_max = BPF_SKB_MAX_LEN; 3637 __be16 proto = skb->protocol; 3638 bool shrink = len_diff < 0; 3639 u32 off; 3640 int ret; 3641 3642 if (unlikely(flags & ~(BPF_F_ADJ_ROOM_MASK | 3643 BPF_F_ADJ_ROOM_NO_CSUM_RESET))) 3644 return -EINVAL; 3645 if (unlikely(len_diff_abs > 0xfffU)) 3646 return -EFAULT; 3647 if (unlikely(proto != htons(ETH_P_IP) && 3648 proto != htons(ETH_P_IPV6))) 3649 return -ENOTSUPP; 3650 3651 off = skb_mac_header_len(skb); 3652 switch (mode) { 3653 case BPF_ADJ_ROOM_NET: 3654 off += bpf_skb_net_base_len(skb); 3655 break; 3656 case BPF_ADJ_ROOM_MAC: 3657 break; 3658 default: 3659 return -ENOTSUPP; 3660 } 3661 3662 if (flags & BPF_F_ADJ_ROOM_DECAP_L3_MASK) { 3663 if (!shrink) 3664 return -EINVAL; 3665 3666 switch (flags & BPF_F_ADJ_ROOM_DECAP_L3_MASK) { 3667 case BPF_F_ADJ_ROOM_DECAP_L3_IPV4: 3668 len_min = sizeof(struct iphdr); 3669 break; 3670 case BPF_F_ADJ_ROOM_DECAP_L3_IPV6: 3671 len_min = sizeof(struct ipv6hdr); 3672 break; 3673 default: 3674 return -EINVAL; 3675 } 3676 } 3677 3678 len_cur = skb->len - skb_network_offset(skb); 3679 if ((shrink && (len_diff_abs >= len_cur || 3680 len_cur - len_diff_abs < len_min)) || 3681 (!shrink && (skb->len + len_diff_abs > len_max && 3682 !skb_is_gso(skb)))) 3683 return -ENOTSUPP; 3684 3685 ret = shrink ? bpf_skb_net_shrink(skb, off, len_diff_abs, flags) : 3686 bpf_skb_net_grow(skb, off, len_diff_abs, flags); 3687 if (!ret && !(flags & BPF_F_ADJ_ROOM_NO_CSUM_RESET)) 3688 __skb_reset_checksum_unnecessary(skb); 3689 3690 bpf_compute_data_pointers(skb); 3691 return ret; 3692 } 3693 3694 static const struct bpf_func_proto bpf_skb_adjust_room_proto = { 3695 .func = bpf_skb_adjust_room, 3696 .gpl_only = false, 3697 .ret_type = RET_INTEGER, 3698 .arg1_type = ARG_PTR_TO_CTX, 3699 .arg2_type = ARG_ANYTHING, 3700 .arg3_type = ARG_ANYTHING, 3701 .arg4_type = ARG_ANYTHING, 3702 }; 3703 3704 static u32 __bpf_skb_min_len(const struct sk_buff *skb) 3705 { 3706 u32 min_len = skb_network_offset(skb); 3707 3708 if (skb_transport_header_was_set(skb)) 3709 min_len = skb_transport_offset(skb); 3710 if (skb->ip_summed == CHECKSUM_PARTIAL) 3711 min_len = skb_checksum_start_offset(skb) + 3712 skb->csum_offset + sizeof(__sum16); 3713 return min_len; 3714 } 3715 3716 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len) 3717 { 3718 unsigned int old_len = skb->len; 3719 int ret; 3720 3721 ret = __skb_grow_rcsum(skb, new_len); 3722 if (!ret) 3723 memset(skb->data + old_len, 0, new_len - old_len); 3724 return ret; 3725 } 3726 3727 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len) 3728 { 3729 return __skb_trim_rcsum(skb, new_len); 3730 } 3731 3732 static inline int __bpf_skb_change_tail(struct sk_buff *skb, u32 new_len, 3733 u64 flags) 3734 { 3735 u32 max_len = BPF_SKB_MAX_LEN; 3736 u32 min_len = __bpf_skb_min_len(skb); 3737 int ret; 3738 3739 if (unlikely(flags || new_len > max_len || new_len < min_len)) 3740 return -EINVAL; 3741 if (skb->encapsulation) 3742 return -ENOTSUPP; 3743 3744 /* The basic idea of this helper is that it's performing the 3745 * needed work to either grow or trim an skb, and eBPF program 3746 * rewrites the rest via helpers like bpf_skb_store_bytes(), 3747 * bpf_lX_csum_replace() and others rather than passing a raw 3748 * buffer here. This one is a slow path helper and intended 3749 * for replies with control messages. 3750 * 3751 * Like in bpf_skb_change_proto(), we want to keep this rather 3752 * minimal and without protocol specifics so that we are able 3753 * to separate concerns as in bpf_skb_store_bytes() should only 3754 * be the one responsible for writing buffers. 3755 * 3756 * It's really expected to be a slow path operation here for 3757 * control message replies, so we're implicitly linearizing, 3758 * uncloning and drop offloads from the skb by this. 3759 */ 3760 ret = __bpf_try_make_writable(skb, skb->len); 3761 if (!ret) { 3762 if (new_len > skb->len) 3763 ret = bpf_skb_grow_rcsum(skb, new_len); 3764 else if (new_len < skb->len) 3765 ret = bpf_skb_trim_rcsum(skb, new_len); 3766 if (!ret && skb_is_gso(skb)) 3767 skb_gso_reset(skb); 3768 } 3769 return ret; 3770 } 3771 3772 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len, 3773 u64, flags) 3774 { 3775 int ret = __bpf_skb_change_tail(skb, new_len, flags); 3776 3777 bpf_compute_data_pointers(skb); 3778 return ret; 3779 } 3780 3781 static const struct bpf_func_proto bpf_skb_change_tail_proto = { 3782 .func = bpf_skb_change_tail, 3783 .gpl_only = false, 3784 .ret_type = RET_INTEGER, 3785 .arg1_type = ARG_PTR_TO_CTX, 3786 .arg2_type = ARG_ANYTHING, 3787 .arg3_type = ARG_ANYTHING, 3788 }; 3789 3790 BPF_CALL_3(sk_skb_change_tail, struct sk_buff *, skb, u32, new_len, 3791 u64, flags) 3792 { 3793 return __bpf_skb_change_tail(skb, new_len, flags); 3794 } 3795 3796 static const struct bpf_func_proto sk_skb_change_tail_proto = { 3797 .func = sk_skb_change_tail, 3798 .gpl_only = false, 3799 .ret_type = RET_INTEGER, 3800 .arg1_type = ARG_PTR_TO_CTX, 3801 .arg2_type = ARG_ANYTHING, 3802 .arg3_type = ARG_ANYTHING, 3803 }; 3804 3805 static inline int __bpf_skb_change_head(struct sk_buff *skb, u32 head_room, 3806 u64 flags) 3807 { 3808 u32 max_len = BPF_SKB_MAX_LEN; 3809 u32 new_len = skb->len + head_room; 3810 int ret; 3811 3812 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) || 3813 new_len < skb->len)) 3814 return -EINVAL; 3815 3816 ret = skb_cow(skb, head_room); 3817 if (likely(!ret)) { 3818 /* Idea for this helper is that we currently only 3819 * allow to expand on mac header. This means that 3820 * skb->protocol network header, etc, stay as is. 3821 * Compared to bpf_skb_change_tail(), we're more 3822 * flexible due to not needing to linearize or 3823 * reset GSO. Intention for this helper is to be 3824 * used by an L3 skb that needs to push mac header 3825 * for redirection into L2 device. 3826 */ 3827 __skb_push(skb, head_room); 3828 memset(skb->data, 0, head_room); 3829 skb_reset_mac_header(skb); 3830 skb_reset_mac_len(skb); 3831 } 3832 3833 return ret; 3834 } 3835 3836 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room, 3837 u64, flags) 3838 { 3839 int ret = __bpf_skb_change_head(skb, head_room, flags); 3840 3841 bpf_compute_data_pointers(skb); 3842 return ret; 3843 } 3844 3845 static const struct bpf_func_proto bpf_skb_change_head_proto = { 3846 .func = bpf_skb_change_head, 3847 .gpl_only = false, 3848 .ret_type = RET_INTEGER, 3849 .arg1_type = ARG_PTR_TO_CTX, 3850 .arg2_type = ARG_ANYTHING, 3851 .arg3_type = ARG_ANYTHING, 3852 }; 3853 3854 BPF_CALL_3(sk_skb_change_head, struct sk_buff *, skb, u32, head_room, 3855 u64, flags) 3856 { 3857 return __bpf_skb_change_head(skb, head_room, flags); 3858 } 3859 3860 static const struct bpf_func_proto sk_skb_change_head_proto = { 3861 .func = sk_skb_change_head, 3862 .gpl_only = false, 3863 .ret_type = RET_INTEGER, 3864 .arg1_type = ARG_PTR_TO_CTX, 3865 .arg2_type = ARG_ANYTHING, 3866 .arg3_type = ARG_ANYTHING, 3867 }; 3868 3869 BPF_CALL_1(bpf_xdp_get_buff_len, struct xdp_buff*, xdp) 3870 { 3871 return xdp_get_buff_len(xdp); 3872 } 3873 3874 static const struct bpf_func_proto bpf_xdp_get_buff_len_proto = { 3875 .func = bpf_xdp_get_buff_len, 3876 .gpl_only = false, 3877 .ret_type = RET_INTEGER, 3878 .arg1_type = ARG_PTR_TO_CTX, 3879 }; 3880 3881 BTF_ID_LIST_SINGLE(bpf_xdp_get_buff_len_bpf_ids, struct, xdp_buff) 3882 3883 const struct bpf_func_proto bpf_xdp_get_buff_len_trace_proto = { 3884 .func = bpf_xdp_get_buff_len, 3885 .gpl_only = false, 3886 .arg1_type = ARG_PTR_TO_BTF_ID, 3887 .arg1_btf_id = &bpf_xdp_get_buff_len_bpf_ids[0], 3888 }; 3889 3890 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp) 3891 { 3892 return xdp_data_meta_unsupported(xdp) ? 0 : 3893 xdp->data - xdp->data_meta; 3894 } 3895 3896 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset) 3897 { 3898 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame); 3899 unsigned long metalen = xdp_get_metalen(xdp); 3900 void *data_start = xdp_frame_end + metalen; 3901 void *data = xdp->data + offset; 3902 3903 if (unlikely(data < data_start || 3904 data > xdp->data_end - ETH_HLEN)) 3905 return -EINVAL; 3906 3907 if (metalen) 3908 memmove(xdp->data_meta + offset, 3909 xdp->data_meta, metalen); 3910 xdp->data_meta += offset; 3911 xdp->data = data; 3912 3913 return 0; 3914 } 3915 3916 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = { 3917 .func = bpf_xdp_adjust_head, 3918 .gpl_only = false, 3919 .ret_type = RET_INTEGER, 3920 .arg1_type = ARG_PTR_TO_CTX, 3921 .arg2_type = ARG_ANYTHING, 3922 }; 3923 3924 void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off, 3925 void *buf, unsigned long len, bool flush) 3926 { 3927 unsigned long ptr_len, ptr_off = 0; 3928 skb_frag_t *next_frag, *end_frag; 3929 struct skb_shared_info *sinfo; 3930 void *src, *dst; 3931 u8 *ptr_buf; 3932 3933 if (likely(xdp->data_end - xdp->data >= off + len)) { 3934 src = flush ? buf : xdp->data + off; 3935 dst = flush ? xdp->data + off : buf; 3936 memcpy(dst, src, len); 3937 return; 3938 } 3939 3940 sinfo = xdp_get_shared_info_from_buff(xdp); 3941 end_frag = &sinfo->frags[sinfo->nr_frags]; 3942 next_frag = &sinfo->frags[0]; 3943 3944 ptr_len = xdp->data_end - xdp->data; 3945 ptr_buf = xdp->data; 3946 3947 while (true) { 3948 if (off < ptr_off + ptr_len) { 3949 unsigned long copy_off = off - ptr_off; 3950 unsigned long copy_len = min(len, ptr_len - copy_off); 3951 3952 src = flush ? buf : ptr_buf + copy_off; 3953 dst = flush ? ptr_buf + copy_off : buf; 3954 memcpy(dst, src, copy_len); 3955 3956 off += copy_len; 3957 len -= copy_len; 3958 buf += copy_len; 3959 } 3960 3961 if (!len || next_frag == end_frag) 3962 break; 3963 3964 ptr_off += ptr_len; 3965 ptr_buf = skb_frag_address(next_frag); 3966 ptr_len = skb_frag_size(next_frag); 3967 next_frag++; 3968 } 3969 } 3970 3971 void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len) 3972 { 3973 u32 size = xdp->data_end - xdp->data; 3974 struct skb_shared_info *sinfo; 3975 void *addr = xdp->data; 3976 int i; 3977 3978 if (unlikely(offset > 0xffff || len > 0xffff)) 3979 return ERR_PTR(-EFAULT); 3980 3981 if (unlikely(offset + len > xdp_get_buff_len(xdp))) 3982 return ERR_PTR(-EINVAL); 3983 3984 if (likely(offset < size)) /* linear area */ 3985 goto out; 3986 3987 sinfo = xdp_get_shared_info_from_buff(xdp); 3988 offset -= size; 3989 for (i = 0; i < sinfo->nr_frags; i++) { /* paged area */ 3990 u32 frag_size = skb_frag_size(&sinfo->frags[i]); 3991 3992 if (offset < frag_size) { 3993 addr = skb_frag_address(&sinfo->frags[i]); 3994 size = frag_size; 3995 break; 3996 } 3997 offset -= frag_size; 3998 } 3999 out: 4000 return offset + len <= size ? addr + offset : NULL; 4001 } 4002 4003 BPF_CALL_4(bpf_xdp_load_bytes, struct xdp_buff *, xdp, u32, offset, 4004 void *, buf, u32, len) 4005 { 4006 void *ptr; 4007 4008 ptr = bpf_xdp_pointer(xdp, offset, len); 4009 if (IS_ERR(ptr)) 4010 return PTR_ERR(ptr); 4011 4012 if (!ptr) 4013 bpf_xdp_copy_buf(xdp, offset, buf, len, false); 4014 else 4015 memcpy(buf, ptr, len); 4016 4017 return 0; 4018 } 4019 4020 static const struct bpf_func_proto bpf_xdp_load_bytes_proto = { 4021 .func = bpf_xdp_load_bytes, 4022 .gpl_only = false, 4023 .ret_type = RET_INTEGER, 4024 .arg1_type = ARG_PTR_TO_CTX, 4025 .arg2_type = ARG_ANYTHING, 4026 .arg3_type = ARG_PTR_TO_UNINIT_MEM, 4027 .arg4_type = ARG_CONST_SIZE, 4028 }; 4029 4030 int __bpf_xdp_load_bytes(struct xdp_buff *xdp, u32 offset, void *buf, u32 len) 4031 { 4032 return ____bpf_xdp_load_bytes(xdp, offset, buf, len); 4033 } 4034 4035 BPF_CALL_4(bpf_xdp_store_bytes, struct xdp_buff *, xdp, u32, offset, 4036 void *, buf, u32, len) 4037 { 4038 void *ptr; 4039 4040 ptr = bpf_xdp_pointer(xdp, offset, len); 4041 if (IS_ERR(ptr)) 4042 return PTR_ERR(ptr); 4043 4044 if (!ptr) 4045 bpf_xdp_copy_buf(xdp, offset, buf, len, true); 4046 else 4047 memcpy(ptr, buf, len); 4048 4049 return 0; 4050 } 4051 4052 static const struct bpf_func_proto bpf_xdp_store_bytes_proto = { 4053 .func = bpf_xdp_store_bytes, 4054 .gpl_only = false, 4055 .ret_type = RET_INTEGER, 4056 .arg1_type = ARG_PTR_TO_CTX, 4057 .arg2_type = ARG_ANYTHING, 4058 .arg3_type = ARG_PTR_TO_UNINIT_MEM, 4059 .arg4_type = ARG_CONST_SIZE, 4060 }; 4061 4062 int __bpf_xdp_store_bytes(struct xdp_buff *xdp, u32 offset, void *buf, u32 len) 4063 { 4064 return ____bpf_xdp_store_bytes(xdp, offset, buf, len); 4065 } 4066 4067 static int bpf_xdp_frags_increase_tail(struct xdp_buff *xdp, int offset) 4068 { 4069 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp); 4070 skb_frag_t *frag = &sinfo->frags[sinfo->nr_frags - 1]; 4071 struct xdp_rxq_info *rxq = xdp->rxq; 4072 unsigned int tailroom; 4073 4074 if (!rxq->frag_size || rxq->frag_size > xdp->frame_sz) 4075 return -EOPNOTSUPP; 4076 4077 tailroom = rxq->frag_size - skb_frag_size(frag) - skb_frag_off(frag); 4078 if (unlikely(offset > tailroom)) 4079 return -EINVAL; 4080 4081 memset(skb_frag_address(frag) + skb_frag_size(frag), 0, offset); 4082 skb_frag_size_add(frag, offset); 4083 sinfo->xdp_frags_size += offset; 4084 if (rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL) 4085 xsk_buff_get_tail(xdp)->data_end += offset; 4086 4087 return 0; 4088 } 4089 4090 static void bpf_xdp_shrink_data_zc(struct xdp_buff *xdp, int shrink, 4091 struct xdp_mem_info *mem_info, bool release) 4092 { 4093 struct xdp_buff *zc_frag = xsk_buff_get_tail(xdp); 4094 4095 if (release) { 4096 xsk_buff_del_tail(zc_frag); 4097 __xdp_return(NULL, mem_info, false, zc_frag); 4098 } else { 4099 zc_frag->data_end -= shrink; 4100 } 4101 } 4102 4103 static bool bpf_xdp_shrink_data(struct xdp_buff *xdp, skb_frag_t *frag, 4104 int shrink) 4105 { 4106 struct xdp_mem_info *mem_info = &xdp->rxq->mem; 4107 bool release = skb_frag_size(frag) == shrink; 4108 4109 if (mem_info->type == MEM_TYPE_XSK_BUFF_POOL) { 4110 bpf_xdp_shrink_data_zc(xdp, shrink, mem_info, release); 4111 goto out; 4112 } 4113 4114 if (release) { 4115 struct page *page = skb_frag_page(frag); 4116 4117 __xdp_return(page_address(page), mem_info, false, NULL); 4118 } 4119 4120 out: 4121 return release; 4122 } 4123 4124 static int bpf_xdp_frags_shrink_tail(struct xdp_buff *xdp, int offset) 4125 { 4126 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp); 4127 int i, n_frags_free = 0, len_free = 0; 4128 4129 if (unlikely(offset > (int)xdp_get_buff_len(xdp) - ETH_HLEN)) 4130 return -EINVAL; 4131 4132 for (i = sinfo->nr_frags - 1; i >= 0 && offset > 0; i--) { 4133 skb_frag_t *frag = &sinfo->frags[i]; 4134 int shrink = min_t(int, offset, skb_frag_size(frag)); 4135 4136 len_free += shrink; 4137 offset -= shrink; 4138 if (bpf_xdp_shrink_data(xdp, frag, shrink)) { 4139 n_frags_free++; 4140 } else { 4141 skb_frag_size_sub(frag, shrink); 4142 break; 4143 } 4144 } 4145 sinfo->nr_frags -= n_frags_free; 4146 sinfo->xdp_frags_size -= len_free; 4147 4148 if (unlikely(!sinfo->nr_frags)) { 4149 xdp_buff_clear_frags_flag(xdp); 4150 xdp->data_end -= offset; 4151 } 4152 4153 return 0; 4154 } 4155 4156 BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset) 4157 { 4158 void *data_hard_end = xdp_data_hard_end(xdp); /* use xdp->frame_sz */ 4159 void *data_end = xdp->data_end + offset; 4160 4161 if (unlikely(xdp_buff_has_frags(xdp))) { /* non-linear xdp buff */ 4162 if (offset < 0) 4163 return bpf_xdp_frags_shrink_tail(xdp, -offset); 4164 4165 return bpf_xdp_frags_increase_tail(xdp, offset); 4166 } 4167 4168 /* Notice that xdp_data_hard_end have reserved some tailroom */ 4169 if (unlikely(data_end > data_hard_end)) 4170 return -EINVAL; 4171 4172 if (unlikely(data_end < xdp->data + ETH_HLEN)) 4173 return -EINVAL; 4174 4175 /* Clear memory area on grow, can contain uninit kernel memory */ 4176 if (offset > 0) 4177 memset(xdp->data_end, 0, offset); 4178 4179 xdp->data_end = data_end; 4180 4181 return 0; 4182 } 4183 4184 static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = { 4185 .func = bpf_xdp_adjust_tail, 4186 .gpl_only = false, 4187 .ret_type = RET_INTEGER, 4188 .arg1_type = ARG_PTR_TO_CTX, 4189 .arg2_type = ARG_ANYTHING, 4190 }; 4191 4192 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset) 4193 { 4194 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame); 4195 void *meta = xdp->data_meta + offset; 4196 unsigned long metalen = xdp->data - meta; 4197 4198 if (xdp_data_meta_unsupported(xdp)) 4199 return -ENOTSUPP; 4200 if (unlikely(meta < xdp_frame_end || 4201 meta > xdp->data)) 4202 return -EINVAL; 4203 if (unlikely(xdp_metalen_invalid(metalen))) 4204 return -EACCES; 4205 4206 xdp->data_meta = meta; 4207 4208 return 0; 4209 } 4210 4211 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = { 4212 .func = bpf_xdp_adjust_meta, 4213 .gpl_only = false, 4214 .ret_type = RET_INTEGER, 4215 .arg1_type = ARG_PTR_TO_CTX, 4216 .arg2_type = ARG_ANYTHING, 4217 }; 4218 4219 /** 4220 * DOC: xdp redirect 4221 * 4222 * XDP_REDIRECT works by a three-step process, implemented in the functions 4223 * below: 4224 * 4225 * 1. The bpf_redirect() and bpf_redirect_map() helpers will lookup the target 4226 * of the redirect and store it (along with some other metadata) in a per-CPU 4227 * struct bpf_redirect_info. 4228 * 4229 * 2. When the program returns the XDP_REDIRECT return code, the driver will 4230 * call xdp_do_redirect() which will use the information in struct 4231 * bpf_redirect_info to actually enqueue the frame into a map type-specific 4232 * bulk queue structure. 4233 * 4234 * 3. Before exiting its NAPI poll loop, the driver will call 4235 * xdp_do_flush(), which will flush all the different bulk queues, 4236 * thus completing the redirect. Note that xdp_do_flush() must be 4237 * called before napi_complete_done() in the driver, as the 4238 * XDP_REDIRECT logic relies on being inside a single NAPI instance 4239 * through to the xdp_do_flush() call for RCU protection of all 4240 * in-kernel data structures. 4241 */ 4242 /* 4243 * Pointers to the map entries will be kept around for this whole sequence of 4244 * steps, protected by RCU. However, there is no top-level rcu_read_lock() in 4245 * the core code; instead, the RCU protection relies on everything happening 4246 * inside a single NAPI poll sequence, which means it's between a pair of calls 4247 * to local_bh_disable()/local_bh_enable(). 4248 * 4249 * The map entries are marked as __rcu and the map code makes sure to 4250 * dereference those pointers with rcu_dereference_check() in a way that works 4251 * for both sections that to hold an rcu_read_lock() and sections that are 4252 * called from NAPI without a separate rcu_read_lock(). The code below does not 4253 * use RCU annotations, but relies on those in the map code. 4254 */ 4255 void xdp_do_flush(void) 4256 { 4257 __dev_flush(); 4258 __cpu_map_flush(); 4259 __xsk_map_flush(); 4260 } 4261 EXPORT_SYMBOL_GPL(xdp_do_flush); 4262 4263 void bpf_clear_redirect_map(struct bpf_map *map) 4264 { 4265 struct bpf_redirect_info *ri; 4266 int cpu; 4267 4268 for_each_possible_cpu(cpu) { 4269 ri = per_cpu_ptr(&bpf_redirect_info, cpu); 4270 /* Avoid polluting remote cacheline due to writes if 4271 * not needed. Once we pass this test, we need the 4272 * cmpxchg() to make sure it hasn't been changed in 4273 * the meantime by remote CPU. 4274 */ 4275 if (unlikely(READ_ONCE(ri->map) == map)) 4276 cmpxchg(&ri->map, map, NULL); 4277 } 4278 } 4279 4280 DEFINE_STATIC_KEY_FALSE(bpf_master_redirect_enabled_key); 4281 EXPORT_SYMBOL_GPL(bpf_master_redirect_enabled_key); 4282 4283 u32 xdp_master_redirect(struct xdp_buff *xdp) 4284 { 4285 struct net_device *master, *slave; 4286 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 4287 4288 master = netdev_master_upper_dev_get_rcu(xdp->rxq->dev); 4289 slave = master->netdev_ops->ndo_xdp_get_xmit_slave(master, xdp); 4290 if (slave && slave != xdp->rxq->dev) { 4291 /* The target device is different from the receiving device, so 4292 * redirect it to the new device. 4293 * Using XDP_REDIRECT gets the correct behaviour from XDP enabled 4294 * drivers to unmap the packet from their rx ring. 4295 */ 4296 ri->tgt_index = slave->ifindex; 4297 ri->map_id = INT_MAX; 4298 ri->map_type = BPF_MAP_TYPE_UNSPEC; 4299 return XDP_REDIRECT; 4300 } 4301 return XDP_TX; 4302 } 4303 EXPORT_SYMBOL_GPL(xdp_master_redirect); 4304 4305 static inline int __xdp_do_redirect_xsk(struct bpf_redirect_info *ri, 4306 struct net_device *dev, 4307 struct xdp_buff *xdp, 4308 struct bpf_prog *xdp_prog) 4309 { 4310 enum bpf_map_type map_type = ri->map_type; 4311 void *fwd = ri->tgt_value; 4312 u32 map_id = ri->map_id; 4313 int err; 4314 4315 ri->map_id = 0; /* Valid map id idr range: [1,INT_MAX[ */ 4316 ri->map_type = BPF_MAP_TYPE_UNSPEC; 4317 4318 err = __xsk_map_redirect(fwd, xdp); 4319 if (unlikely(err)) 4320 goto err; 4321 4322 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index); 4323 return 0; 4324 err: 4325 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index, err); 4326 return err; 4327 } 4328 4329 static __always_inline int __xdp_do_redirect_frame(struct bpf_redirect_info *ri, 4330 struct net_device *dev, 4331 struct xdp_frame *xdpf, 4332 struct bpf_prog *xdp_prog) 4333 { 4334 enum bpf_map_type map_type = ri->map_type; 4335 void *fwd = ri->tgt_value; 4336 u32 map_id = ri->map_id; 4337 u32 flags = ri->flags; 4338 struct bpf_map *map; 4339 int err; 4340 4341 ri->map_id = 0; /* Valid map id idr range: [1,INT_MAX[ */ 4342 ri->flags = 0; 4343 ri->map_type = BPF_MAP_TYPE_UNSPEC; 4344 4345 if (unlikely(!xdpf)) { 4346 err = -EOVERFLOW; 4347 goto err; 4348 } 4349 4350 switch (map_type) { 4351 case BPF_MAP_TYPE_DEVMAP: 4352 fallthrough; 4353 case BPF_MAP_TYPE_DEVMAP_HASH: 4354 if (unlikely(flags & BPF_F_BROADCAST)) { 4355 map = READ_ONCE(ri->map); 4356 4357 /* The map pointer is cleared when the map is being torn 4358 * down by bpf_clear_redirect_map() 4359 */ 4360 if (unlikely(!map)) { 4361 err = -ENOENT; 4362 break; 4363 } 4364 4365 WRITE_ONCE(ri->map, NULL); 4366 err = dev_map_enqueue_multi(xdpf, dev, map, 4367 flags & BPF_F_EXCLUDE_INGRESS); 4368 } else { 4369 err = dev_map_enqueue(fwd, xdpf, dev); 4370 } 4371 break; 4372 case BPF_MAP_TYPE_CPUMAP: 4373 err = cpu_map_enqueue(fwd, xdpf, dev); 4374 break; 4375 case BPF_MAP_TYPE_UNSPEC: 4376 if (map_id == INT_MAX) { 4377 fwd = dev_get_by_index_rcu(dev_net(dev), ri->tgt_index); 4378 if (unlikely(!fwd)) { 4379 err = -EINVAL; 4380 break; 4381 } 4382 err = dev_xdp_enqueue(fwd, xdpf, dev); 4383 break; 4384 } 4385 fallthrough; 4386 default: 4387 err = -EBADRQC; 4388 } 4389 4390 if (unlikely(err)) 4391 goto err; 4392 4393 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index); 4394 return 0; 4395 err: 4396 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index, err); 4397 return err; 4398 } 4399 4400 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp, 4401 struct bpf_prog *xdp_prog) 4402 { 4403 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 4404 enum bpf_map_type map_type = ri->map_type; 4405 4406 if (map_type == BPF_MAP_TYPE_XSKMAP) 4407 return __xdp_do_redirect_xsk(ri, dev, xdp, xdp_prog); 4408 4409 return __xdp_do_redirect_frame(ri, dev, xdp_convert_buff_to_frame(xdp), 4410 xdp_prog); 4411 } 4412 EXPORT_SYMBOL_GPL(xdp_do_redirect); 4413 4414 int xdp_do_redirect_frame(struct net_device *dev, struct xdp_buff *xdp, 4415 struct xdp_frame *xdpf, struct bpf_prog *xdp_prog) 4416 { 4417 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 4418 enum bpf_map_type map_type = ri->map_type; 4419 4420 if (map_type == BPF_MAP_TYPE_XSKMAP) 4421 return __xdp_do_redirect_xsk(ri, dev, xdp, xdp_prog); 4422 4423 return __xdp_do_redirect_frame(ri, dev, xdpf, xdp_prog); 4424 } 4425 EXPORT_SYMBOL_GPL(xdp_do_redirect_frame); 4426 4427 static int xdp_do_generic_redirect_map(struct net_device *dev, 4428 struct sk_buff *skb, 4429 struct xdp_buff *xdp, 4430 struct bpf_prog *xdp_prog, void *fwd, 4431 enum bpf_map_type map_type, u32 map_id, 4432 u32 flags) 4433 { 4434 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 4435 struct bpf_map *map; 4436 int err; 4437 4438 switch (map_type) { 4439 case BPF_MAP_TYPE_DEVMAP: 4440 fallthrough; 4441 case BPF_MAP_TYPE_DEVMAP_HASH: 4442 if (unlikely(flags & BPF_F_BROADCAST)) { 4443 map = READ_ONCE(ri->map); 4444 4445 /* The map pointer is cleared when the map is being torn 4446 * down by bpf_clear_redirect_map() 4447 */ 4448 if (unlikely(!map)) { 4449 err = -ENOENT; 4450 break; 4451 } 4452 4453 WRITE_ONCE(ri->map, NULL); 4454 err = dev_map_redirect_multi(dev, skb, xdp_prog, map, 4455 flags & BPF_F_EXCLUDE_INGRESS); 4456 } else { 4457 err = dev_map_generic_redirect(fwd, skb, xdp_prog); 4458 } 4459 if (unlikely(err)) 4460 goto err; 4461 break; 4462 case BPF_MAP_TYPE_XSKMAP: 4463 err = xsk_generic_rcv(fwd, xdp); 4464 if (err) 4465 goto err; 4466 consume_skb(skb); 4467 break; 4468 case BPF_MAP_TYPE_CPUMAP: 4469 err = cpu_map_generic_redirect(fwd, skb); 4470 if (unlikely(err)) 4471 goto err; 4472 break; 4473 default: 4474 err = -EBADRQC; 4475 goto err; 4476 } 4477 4478 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index); 4479 return 0; 4480 err: 4481 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index, err); 4482 return err; 4483 } 4484 4485 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb, 4486 struct xdp_buff *xdp, struct bpf_prog *xdp_prog) 4487 { 4488 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 4489 enum bpf_map_type map_type = ri->map_type; 4490 void *fwd = ri->tgt_value; 4491 u32 map_id = ri->map_id; 4492 u32 flags = ri->flags; 4493 int err; 4494 4495 ri->map_id = 0; /* Valid map id idr range: [1,INT_MAX[ */ 4496 ri->flags = 0; 4497 ri->map_type = BPF_MAP_TYPE_UNSPEC; 4498 4499 if (map_type == BPF_MAP_TYPE_UNSPEC && map_id == INT_MAX) { 4500 fwd = dev_get_by_index_rcu(dev_net(dev), ri->tgt_index); 4501 if (unlikely(!fwd)) { 4502 err = -EINVAL; 4503 goto err; 4504 } 4505 4506 err = xdp_ok_fwd_dev(fwd, skb->len); 4507 if (unlikely(err)) 4508 goto err; 4509 4510 skb->dev = fwd; 4511 _trace_xdp_redirect(dev, xdp_prog, ri->tgt_index); 4512 generic_xdp_tx(skb, xdp_prog); 4513 return 0; 4514 } 4515 4516 return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog, fwd, map_type, map_id, flags); 4517 err: 4518 _trace_xdp_redirect_err(dev, xdp_prog, ri->tgt_index, err); 4519 return err; 4520 } 4521 4522 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags) 4523 { 4524 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 4525 4526 if (unlikely(flags)) 4527 return XDP_ABORTED; 4528 4529 /* NB! Map type UNSPEC and map_id == INT_MAX (never generated 4530 * by map_idr) is used for ifindex based XDP redirect. 4531 */ 4532 ri->tgt_index = ifindex; 4533 ri->map_id = INT_MAX; 4534 ri->map_type = BPF_MAP_TYPE_UNSPEC; 4535 4536 return XDP_REDIRECT; 4537 } 4538 4539 static const struct bpf_func_proto bpf_xdp_redirect_proto = { 4540 .func = bpf_xdp_redirect, 4541 .gpl_only = false, 4542 .ret_type = RET_INTEGER, 4543 .arg1_type = ARG_ANYTHING, 4544 .arg2_type = ARG_ANYTHING, 4545 }; 4546 4547 BPF_CALL_3(bpf_xdp_redirect_map, struct bpf_map *, map, u64, key, 4548 u64, flags) 4549 { 4550 return map->ops->map_redirect(map, key, flags); 4551 } 4552 4553 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = { 4554 .func = bpf_xdp_redirect_map, 4555 .gpl_only = false, 4556 .ret_type = RET_INTEGER, 4557 .arg1_type = ARG_CONST_MAP_PTR, 4558 .arg2_type = ARG_ANYTHING, 4559 .arg3_type = ARG_ANYTHING, 4560 }; 4561 4562 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb, 4563 unsigned long off, unsigned long len) 4564 { 4565 void *ptr = skb_header_pointer(skb, off, len, dst_buff); 4566 4567 if (unlikely(!ptr)) 4568 return len; 4569 if (ptr != dst_buff) 4570 memcpy(dst_buff, ptr, len); 4571 4572 return 0; 4573 } 4574 4575 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map, 4576 u64, flags, void *, meta, u64, meta_size) 4577 { 4578 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32; 4579 4580 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK))) 4581 return -EINVAL; 4582 if (unlikely(!skb || skb_size > skb->len)) 4583 return -EFAULT; 4584 4585 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size, 4586 bpf_skb_copy); 4587 } 4588 4589 static const struct bpf_func_proto bpf_skb_event_output_proto = { 4590 .func = bpf_skb_event_output, 4591 .gpl_only = true, 4592 .ret_type = RET_INTEGER, 4593 .arg1_type = ARG_PTR_TO_CTX, 4594 .arg2_type = ARG_CONST_MAP_PTR, 4595 .arg3_type = ARG_ANYTHING, 4596 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY, 4597 .arg5_type = ARG_CONST_SIZE_OR_ZERO, 4598 }; 4599 4600 BTF_ID_LIST_SINGLE(bpf_skb_output_btf_ids, struct, sk_buff) 4601 4602 const struct bpf_func_proto bpf_skb_output_proto = { 4603 .func = bpf_skb_event_output, 4604 .gpl_only = true, 4605 .ret_type = RET_INTEGER, 4606 .arg1_type = ARG_PTR_TO_BTF_ID, 4607 .arg1_btf_id = &bpf_skb_output_btf_ids[0], 4608 .arg2_type = ARG_CONST_MAP_PTR, 4609 .arg3_type = ARG_ANYTHING, 4610 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY, 4611 .arg5_type = ARG_CONST_SIZE_OR_ZERO, 4612 }; 4613 4614 static unsigned short bpf_tunnel_key_af(u64 flags) 4615 { 4616 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET; 4617 } 4618 4619 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to, 4620 u32, size, u64, flags) 4621 { 4622 const struct ip_tunnel_info *info = skb_tunnel_info(skb); 4623 u8 compat[sizeof(struct bpf_tunnel_key)]; 4624 void *to_orig = to; 4625 int err; 4626 4627 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6 | 4628 BPF_F_TUNINFO_FLAGS)))) { 4629 err = -EINVAL; 4630 goto err_clear; 4631 } 4632 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) { 4633 err = -EPROTO; 4634 goto err_clear; 4635 } 4636 if (unlikely(size != sizeof(struct bpf_tunnel_key))) { 4637 err = -EINVAL; 4638 switch (size) { 4639 case offsetof(struct bpf_tunnel_key, local_ipv6[0]): 4640 case offsetof(struct bpf_tunnel_key, tunnel_label): 4641 case offsetof(struct bpf_tunnel_key, tunnel_ext): 4642 goto set_compat; 4643 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]): 4644 /* Fixup deprecated structure layouts here, so we have 4645 * a common path later on. 4646 */ 4647 if (ip_tunnel_info_af(info) != AF_INET) 4648 goto err_clear; 4649 set_compat: 4650 to = (struct bpf_tunnel_key *)compat; 4651 break; 4652 default: 4653 goto err_clear; 4654 } 4655 } 4656 4657 to->tunnel_id = be64_to_cpu(info->key.tun_id); 4658 to->tunnel_tos = info->key.tos; 4659 to->tunnel_ttl = info->key.ttl; 4660 if (flags & BPF_F_TUNINFO_FLAGS) 4661 to->tunnel_flags = info->key.tun_flags; 4662 else 4663 to->tunnel_ext = 0; 4664 4665 if (flags & BPF_F_TUNINFO_IPV6) { 4666 memcpy(to->remote_ipv6, &info->key.u.ipv6.src, 4667 sizeof(to->remote_ipv6)); 4668 memcpy(to->local_ipv6, &info->key.u.ipv6.dst, 4669 sizeof(to->local_ipv6)); 4670 to->tunnel_label = be32_to_cpu(info->key.label); 4671 } else { 4672 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src); 4673 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3); 4674 to->local_ipv4 = be32_to_cpu(info->key.u.ipv4.dst); 4675 memset(&to->local_ipv6[1], 0, sizeof(__u32) * 3); 4676 to->tunnel_label = 0; 4677 } 4678 4679 if (unlikely(size != sizeof(struct bpf_tunnel_key))) 4680 memcpy(to_orig, to, size); 4681 4682 return 0; 4683 err_clear: 4684 memset(to_orig, 0, size); 4685 return err; 4686 } 4687 4688 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = { 4689 .func = bpf_skb_get_tunnel_key, 4690 .gpl_only = false, 4691 .ret_type = RET_INTEGER, 4692 .arg1_type = ARG_PTR_TO_CTX, 4693 .arg2_type = ARG_PTR_TO_UNINIT_MEM, 4694 .arg3_type = ARG_CONST_SIZE, 4695 .arg4_type = ARG_ANYTHING, 4696 }; 4697 4698 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size) 4699 { 4700 const struct ip_tunnel_info *info = skb_tunnel_info(skb); 4701 int err; 4702 4703 if (unlikely(!info || 4704 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) { 4705 err = -ENOENT; 4706 goto err_clear; 4707 } 4708 if (unlikely(size < info->options_len)) { 4709 err = -ENOMEM; 4710 goto err_clear; 4711 } 4712 4713 ip_tunnel_info_opts_get(to, info); 4714 if (size > info->options_len) 4715 memset(to + info->options_len, 0, size - info->options_len); 4716 4717 return info->options_len; 4718 err_clear: 4719 memset(to, 0, size); 4720 return err; 4721 } 4722 4723 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = { 4724 .func = bpf_skb_get_tunnel_opt, 4725 .gpl_only = false, 4726 .ret_type = RET_INTEGER, 4727 .arg1_type = ARG_PTR_TO_CTX, 4728 .arg2_type = ARG_PTR_TO_UNINIT_MEM, 4729 .arg3_type = ARG_CONST_SIZE, 4730 }; 4731 4732 static struct metadata_dst __percpu *md_dst; 4733 4734 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb, 4735 const struct bpf_tunnel_key *, from, u32, size, u64, flags) 4736 { 4737 struct metadata_dst *md = this_cpu_ptr(md_dst); 4738 u8 compat[sizeof(struct bpf_tunnel_key)]; 4739 struct ip_tunnel_info *info; 4740 4741 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX | 4742 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER | 4743 BPF_F_NO_TUNNEL_KEY))) 4744 return -EINVAL; 4745 if (unlikely(size != sizeof(struct bpf_tunnel_key))) { 4746 switch (size) { 4747 case offsetof(struct bpf_tunnel_key, local_ipv6[0]): 4748 case offsetof(struct bpf_tunnel_key, tunnel_label): 4749 case offsetof(struct bpf_tunnel_key, tunnel_ext): 4750 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]): 4751 /* Fixup deprecated structure layouts here, so we have 4752 * a common path later on. 4753 */ 4754 memcpy(compat, from, size); 4755 memset(compat + size, 0, sizeof(compat) - size); 4756 from = (const struct bpf_tunnel_key *) compat; 4757 break; 4758 default: 4759 return -EINVAL; 4760 } 4761 } 4762 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) || 4763 from->tunnel_ext)) 4764 return -EINVAL; 4765 4766 skb_dst_drop(skb); 4767 dst_hold((struct dst_entry *) md); 4768 skb_dst_set(skb, (struct dst_entry *) md); 4769 4770 info = &md->u.tun_info; 4771 memset(info, 0, sizeof(*info)); 4772 info->mode = IP_TUNNEL_INFO_TX; 4773 4774 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE; 4775 if (flags & BPF_F_DONT_FRAGMENT) 4776 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT; 4777 if (flags & BPF_F_ZERO_CSUM_TX) 4778 info->key.tun_flags &= ~TUNNEL_CSUM; 4779 if (flags & BPF_F_SEQ_NUMBER) 4780 info->key.tun_flags |= TUNNEL_SEQ; 4781 if (flags & BPF_F_NO_TUNNEL_KEY) 4782 info->key.tun_flags &= ~TUNNEL_KEY; 4783 4784 info->key.tun_id = cpu_to_be64(from->tunnel_id); 4785 info->key.tos = from->tunnel_tos; 4786 info->key.ttl = from->tunnel_ttl; 4787 4788 if (flags & BPF_F_TUNINFO_IPV6) { 4789 info->mode |= IP_TUNNEL_INFO_IPV6; 4790 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6, 4791 sizeof(from->remote_ipv6)); 4792 memcpy(&info->key.u.ipv6.src, from->local_ipv6, 4793 sizeof(from->local_ipv6)); 4794 info->key.label = cpu_to_be32(from->tunnel_label) & 4795 IPV6_FLOWLABEL_MASK; 4796 } else { 4797 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4); 4798 info->key.u.ipv4.src = cpu_to_be32(from->local_ipv4); 4799 info->key.flow_flags = FLOWI_FLAG_ANYSRC; 4800 } 4801 4802 return 0; 4803 } 4804 4805 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = { 4806 .func = bpf_skb_set_tunnel_key, 4807 .gpl_only = false, 4808 .ret_type = RET_INTEGER, 4809 .arg1_type = ARG_PTR_TO_CTX, 4810 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, 4811 .arg3_type = ARG_CONST_SIZE, 4812 .arg4_type = ARG_ANYTHING, 4813 }; 4814 4815 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb, 4816 const u8 *, from, u32, size) 4817 { 4818 struct ip_tunnel_info *info = skb_tunnel_info(skb); 4819 const struct metadata_dst *md = this_cpu_ptr(md_dst); 4820 4821 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1)))) 4822 return -EINVAL; 4823 if (unlikely(size > IP_TUNNEL_OPTS_MAX)) 4824 return -ENOMEM; 4825 4826 ip_tunnel_info_opts_set(info, from, size, TUNNEL_OPTIONS_PRESENT); 4827 4828 return 0; 4829 } 4830 4831 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = { 4832 .func = bpf_skb_set_tunnel_opt, 4833 .gpl_only = false, 4834 .ret_type = RET_INTEGER, 4835 .arg1_type = ARG_PTR_TO_CTX, 4836 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, 4837 .arg3_type = ARG_CONST_SIZE, 4838 }; 4839 4840 static const struct bpf_func_proto * 4841 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which) 4842 { 4843 if (!md_dst) { 4844 struct metadata_dst __percpu *tmp; 4845 4846 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX, 4847 METADATA_IP_TUNNEL, 4848 GFP_KERNEL); 4849 if (!tmp) 4850 return NULL; 4851 if (cmpxchg(&md_dst, NULL, tmp)) 4852 metadata_dst_free_percpu(tmp); 4853 } 4854 4855 switch (which) { 4856 case BPF_FUNC_skb_set_tunnel_key: 4857 return &bpf_skb_set_tunnel_key_proto; 4858 case BPF_FUNC_skb_set_tunnel_opt: 4859 return &bpf_skb_set_tunnel_opt_proto; 4860 default: 4861 return NULL; 4862 } 4863 } 4864 4865 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map, 4866 u32, idx) 4867 { 4868 struct bpf_array *array = container_of(map, struct bpf_array, map); 4869 struct cgroup *cgrp; 4870 struct sock *sk; 4871 4872 sk = skb_to_full_sk(skb); 4873 if (!sk || !sk_fullsock(sk)) 4874 return -ENOENT; 4875 if (unlikely(idx >= array->map.max_entries)) 4876 return -E2BIG; 4877 4878 cgrp = READ_ONCE(array->ptrs[idx]); 4879 if (unlikely(!cgrp)) 4880 return -EAGAIN; 4881 4882 return sk_under_cgroup_hierarchy(sk, cgrp); 4883 } 4884 4885 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = { 4886 .func = bpf_skb_under_cgroup, 4887 .gpl_only = false, 4888 .ret_type = RET_INTEGER, 4889 .arg1_type = ARG_PTR_TO_CTX, 4890 .arg2_type = ARG_CONST_MAP_PTR, 4891 .arg3_type = ARG_ANYTHING, 4892 }; 4893 4894 #ifdef CONFIG_SOCK_CGROUP_DATA 4895 static inline u64 __bpf_sk_cgroup_id(struct sock *sk) 4896 { 4897 struct cgroup *cgrp; 4898 4899 sk = sk_to_full_sk(sk); 4900 if (!sk || !sk_fullsock(sk)) 4901 return 0; 4902 4903 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); 4904 return cgroup_id(cgrp); 4905 } 4906 4907 BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb) 4908 { 4909 return __bpf_sk_cgroup_id(skb->sk); 4910 } 4911 4912 static const struct bpf_func_proto bpf_skb_cgroup_id_proto = { 4913 .func = bpf_skb_cgroup_id, 4914 .gpl_only = false, 4915 .ret_type = RET_INTEGER, 4916 .arg1_type = ARG_PTR_TO_CTX, 4917 }; 4918 4919 static inline u64 __bpf_sk_ancestor_cgroup_id(struct sock *sk, 4920 int ancestor_level) 4921 { 4922 struct cgroup *ancestor; 4923 struct cgroup *cgrp; 4924 4925 sk = sk_to_full_sk(sk); 4926 if (!sk || !sk_fullsock(sk)) 4927 return 0; 4928 4929 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); 4930 ancestor = cgroup_ancestor(cgrp, ancestor_level); 4931 if (!ancestor) 4932 return 0; 4933 4934 return cgroup_id(ancestor); 4935 } 4936 4937 BPF_CALL_2(bpf_skb_ancestor_cgroup_id, const struct sk_buff *, skb, int, 4938 ancestor_level) 4939 { 4940 return __bpf_sk_ancestor_cgroup_id(skb->sk, ancestor_level); 4941 } 4942 4943 static const struct bpf_func_proto bpf_skb_ancestor_cgroup_id_proto = { 4944 .func = bpf_skb_ancestor_cgroup_id, 4945 .gpl_only = false, 4946 .ret_type = RET_INTEGER, 4947 .arg1_type = ARG_PTR_TO_CTX, 4948 .arg2_type = ARG_ANYTHING, 4949 }; 4950 4951 BPF_CALL_1(bpf_sk_cgroup_id, struct sock *, sk) 4952 { 4953 return __bpf_sk_cgroup_id(sk); 4954 } 4955 4956 static const struct bpf_func_proto bpf_sk_cgroup_id_proto = { 4957 .func = bpf_sk_cgroup_id, 4958 .gpl_only = false, 4959 .ret_type = RET_INTEGER, 4960 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, 4961 }; 4962 4963 BPF_CALL_2(bpf_sk_ancestor_cgroup_id, struct sock *, sk, int, ancestor_level) 4964 { 4965 return __bpf_sk_ancestor_cgroup_id(sk, ancestor_level); 4966 } 4967 4968 static const struct bpf_func_proto bpf_sk_ancestor_cgroup_id_proto = { 4969 .func = bpf_sk_ancestor_cgroup_id, 4970 .gpl_only = false, 4971 .ret_type = RET_INTEGER, 4972 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, 4973 .arg2_type = ARG_ANYTHING, 4974 }; 4975 #endif 4976 4977 static unsigned long bpf_xdp_copy(void *dst, const void *ctx, 4978 unsigned long off, unsigned long len) 4979 { 4980 struct xdp_buff *xdp = (struct xdp_buff *)ctx; 4981 4982 bpf_xdp_copy_buf(xdp, off, dst, len, false); 4983 return 0; 4984 } 4985 4986 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map, 4987 u64, flags, void *, meta, u64, meta_size) 4988 { 4989 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32; 4990 4991 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK))) 4992 return -EINVAL; 4993 4994 if (unlikely(!xdp || xdp_size > xdp_get_buff_len(xdp))) 4995 return -EFAULT; 4996 4997 return bpf_event_output(map, flags, meta, meta_size, xdp, 4998 xdp_size, bpf_xdp_copy); 4999 } 5000 5001 static const struct bpf_func_proto bpf_xdp_event_output_proto = { 5002 .func = bpf_xdp_event_output, 5003 .gpl_only = true, 5004 .ret_type = RET_INTEGER, 5005 .arg1_type = ARG_PTR_TO_CTX, 5006 .arg2_type = ARG_CONST_MAP_PTR, 5007 .arg3_type = ARG_ANYTHING, 5008 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY, 5009 .arg5_type = ARG_CONST_SIZE_OR_ZERO, 5010 }; 5011 5012 BTF_ID_LIST_SINGLE(bpf_xdp_output_btf_ids, struct, xdp_buff) 5013 5014 const struct bpf_func_proto bpf_xdp_output_proto = { 5015 .func = bpf_xdp_event_output, 5016 .gpl_only = true, 5017 .ret_type = RET_INTEGER, 5018 .arg1_type = ARG_PTR_TO_BTF_ID, 5019 .arg1_btf_id = &bpf_xdp_output_btf_ids[0], 5020 .arg2_type = ARG_CONST_MAP_PTR, 5021 .arg3_type = ARG_ANYTHING, 5022 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY, 5023 .arg5_type = ARG_CONST_SIZE_OR_ZERO, 5024 }; 5025 5026 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb) 5027 { 5028 return skb->sk ? __sock_gen_cookie(skb->sk) : 0; 5029 } 5030 5031 static const struct bpf_func_proto bpf_get_socket_cookie_proto = { 5032 .func = bpf_get_socket_cookie, 5033 .gpl_only = false, 5034 .ret_type = RET_INTEGER, 5035 .arg1_type = ARG_PTR_TO_CTX, 5036 }; 5037 5038 BPF_CALL_1(bpf_get_socket_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx) 5039 { 5040 return __sock_gen_cookie(ctx->sk); 5041 } 5042 5043 static const struct bpf_func_proto bpf_get_socket_cookie_sock_addr_proto = { 5044 .func = bpf_get_socket_cookie_sock_addr, 5045 .gpl_only = false, 5046 .ret_type = RET_INTEGER, 5047 .arg1_type = ARG_PTR_TO_CTX, 5048 }; 5049 5050 BPF_CALL_1(bpf_get_socket_cookie_sock, struct sock *, ctx) 5051 { 5052 return __sock_gen_cookie(ctx); 5053 } 5054 5055 static const struct bpf_func_proto bpf_get_socket_cookie_sock_proto = { 5056 .func = bpf_get_socket_cookie_sock, 5057 .gpl_only = false, 5058 .ret_type = RET_INTEGER, 5059 .arg1_type = ARG_PTR_TO_CTX, 5060 }; 5061 5062 BPF_CALL_1(bpf_get_socket_ptr_cookie, struct sock *, sk) 5063 { 5064 return sk ? sock_gen_cookie(sk) : 0; 5065 } 5066 5067 const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto = { 5068 .func = bpf_get_socket_ptr_cookie, 5069 .gpl_only = false, 5070 .ret_type = RET_INTEGER, 5071 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON | PTR_MAYBE_NULL, 5072 }; 5073 5074 BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx) 5075 { 5076 return __sock_gen_cookie(ctx->sk); 5077 } 5078 5079 static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = { 5080 .func = bpf_get_socket_cookie_sock_ops, 5081 .gpl_only = false, 5082 .ret_type = RET_INTEGER, 5083 .arg1_type = ARG_PTR_TO_CTX, 5084 }; 5085 5086 static u64 __bpf_get_netns_cookie(struct sock *sk) 5087 { 5088 const struct net *net = sk ? sock_net(sk) : &init_net; 5089 5090 return net->net_cookie; 5091 } 5092 5093 BPF_CALL_1(bpf_get_netns_cookie_sock, struct sock *, ctx) 5094 { 5095 return __bpf_get_netns_cookie(ctx); 5096 } 5097 5098 static const struct bpf_func_proto bpf_get_netns_cookie_sock_proto = { 5099 .func = bpf_get_netns_cookie_sock, 5100 .gpl_only = false, 5101 .ret_type = RET_INTEGER, 5102 .arg1_type = ARG_PTR_TO_CTX_OR_NULL, 5103 }; 5104 5105 BPF_CALL_1(bpf_get_netns_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx) 5106 { 5107 return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL); 5108 } 5109 5110 static const struct bpf_func_proto bpf_get_netns_cookie_sock_addr_proto = { 5111 .func = bpf_get_netns_cookie_sock_addr, 5112 .gpl_only = false, 5113 .ret_type = RET_INTEGER, 5114 .arg1_type = ARG_PTR_TO_CTX_OR_NULL, 5115 }; 5116 5117 BPF_CALL_1(bpf_get_netns_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx) 5118 { 5119 return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL); 5120 } 5121 5122 static const struct bpf_func_proto bpf_get_netns_cookie_sock_ops_proto = { 5123 .func = bpf_get_netns_cookie_sock_ops, 5124 .gpl_only = false, 5125 .ret_type = RET_INTEGER, 5126 .arg1_type = ARG_PTR_TO_CTX_OR_NULL, 5127 }; 5128 5129 BPF_CALL_1(bpf_get_netns_cookie_sk_msg, struct sk_msg *, ctx) 5130 { 5131 return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL); 5132 } 5133 5134 static const struct bpf_func_proto bpf_get_netns_cookie_sk_msg_proto = { 5135 .func = bpf_get_netns_cookie_sk_msg, 5136 .gpl_only = false, 5137 .ret_type = RET_INTEGER, 5138 .arg1_type = ARG_PTR_TO_CTX_OR_NULL, 5139 }; 5140 5141 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb) 5142 { 5143 struct sock *sk = sk_to_full_sk(skb->sk); 5144 kuid_t kuid; 5145 5146 if (!sk || !sk_fullsock(sk)) 5147 return overflowuid; 5148 kuid = sock_net_uid(sock_net(sk), sk); 5149 return from_kuid_munged(sock_net(sk)->user_ns, kuid); 5150 } 5151 5152 static const struct bpf_func_proto bpf_get_socket_uid_proto = { 5153 .func = bpf_get_socket_uid, 5154 .gpl_only = false, 5155 .ret_type = RET_INTEGER, 5156 .arg1_type = ARG_PTR_TO_CTX, 5157 }; 5158 5159 static int sol_socket_sockopt(struct sock *sk, int optname, 5160 char *optval, int *optlen, 5161 bool getopt) 5162 { 5163 switch (optname) { 5164 case SO_REUSEADDR: 5165 case SO_SNDBUF: 5166 case SO_RCVBUF: 5167 case SO_KEEPALIVE: 5168 case SO_PRIORITY: 5169 case SO_REUSEPORT: 5170 case SO_RCVLOWAT: 5171 case SO_MARK: 5172 case SO_MAX_PACING_RATE: 5173 case SO_BINDTOIFINDEX: 5174 case SO_TXREHASH: 5175 if (*optlen != sizeof(int)) 5176 return -EINVAL; 5177 break; 5178 case SO_BINDTODEVICE: 5179 break; 5180 default: 5181 return -EINVAL; 5182 } 5183 5184 if (getopt) { 5185 if (optname == SO_BINDTODEVICE) 5186 return -EINVAL; 5187 return sk_getsockopt(sk, SOL_SOCKET, optname, 5188 KERNEL_SOCKPTR(optval), 5189 KERNEL_SOCKPTR(optlen)); 5190 } 5191 5192 return sk_setsockopt(sk, SOL_SOCKET, optname, 5193 KERNEL_SOCKPTR(optval), *optlen); 5194 } 5195 5196 static int bpf_sol_tcp_setsockopt(struct sock *sk, int optname, 5197 char *optval, int optlen) 5198 { 5199 struct tcp_sock *tp = tcp_sk(sk); 5200 unsigned long timeout; 5201 int val; 5202 5203 if (optlen != sizeof(int)) 5204 return -EINVAL; 5205 5206 val = *(int *)optval; 5207 5208 /* Only some options are supported */ 5209 switch (optname) { 5210 case TCP_BPF_IW: 5211 if (val <= 0 || tp->data_segs_out > tp->syn_data) 5212 return -EINVAL; 5213 tcp_snd_cwnd_set(tp, val); 5214 break; 5215 case TCP_BPF_SNDCWND_CLAMP: 5216 if (val <= 0) 5217 return -EINVAL; 5218 tp->snd_cwnd_clamp = val; 5219 tp->snd_ssthresh = val; 5220 break; 5221 case TCP_BPF_DELACK_MAX: 5222 timeout = usecs_to_jiffies(val); 5223 if (timeout > TCP_DELACK_MAX || 5224 timeout < TCP_TIMEOUT_MIN) 5225 return -EINVAL; 5226 inet_csk(sk)->icsk_delack_max = timeout; 5227 break; 5228 case TCP_BPF_RTO_MIN: 5229 timeout = usecs_to_jiffies(val); 5230 if (timeout > TCP_RTO_MIN || 5231 timeout < TCP_TIMEOUT_MIN) 5232 return -EINVAL; 5233 inet_csk(sk)->icsk_rto_min = timeout; 5234 break; 5235 default: 5236 return -EINVAL; 5237 } 5238 5239 return 0; 5240 } 5241 5242 static int sol_tcp_sockopt_congestion(struct sock *sk, char *optval, 5243 int *optlen, bool getopt) 5244 { 5245 struct tcp_sock *tp; 5246 int ret; 5247 5248 if (*optlen < 2) 5249 return -EINVAL; 5250 5251 if (getopt) { 5252 if (!inet_csk(sk)->icsk_ca_ops) 5253 return -EINVAL; 5254 /* BPF expects NULL-terminated tcp-cc string */ 5255 optval[--(*optlen)] = '\0'; 5256 return do_tcp_getsockopt(sk, SOL_TCP, TCP_CONGESTION, 5257 KERNEL_SOCKPTR(optval), 5258 KERNEL_SOCKPTR(optlen)); 5259 } 5260 5261 /* "cdg" is the only cc that alloc a ptr 5262 * in inet_csk_ca area. The bpf-tcp-cc may 5263 * overwrite this ptr after switching to cdg. 5264 */ 5265 if (*optlen >= sizeof("cdg") - 1 && !strncmp("cdg", optval, *optlen)) 5266 return -ENOTSUPP; 5267 5268 /* It stops this looping 5269 * 5270 * .init => bpf_setsockopt(tcp_cc) => .init => 5271 * bpf_setsockopt(tcp_cc)" => .init => .... 5272 * 5273 * The second bpf_setsockopt(tcp_cc) is not allowed 5274 * in order to break the loop when both .init 5275 * are the same bpf prog. 5276 * 5277 * This applies even the second bpf_setsockopt(tcp_cc) 5278 * does not cause a loop. This limits only the first 5279 * '.init' can call bpf_setsockopt(TCP_CONGESTION) to 5280 * pick a fallback cc (eg. peer does not support ECN) 5281 * and the second '.init' cannot fallback to 5282 * another. 5283 */ 5284 tp = tcp_sk(sk); 5285 if (tp->bpf_chg_cc_inprogress) 5286 return -EBUSY; 5287 5288 tp->bpf_chg_cc_inprogress = 1; 5289 ret = do_tcp_setsockopt(sk, SOL_TCP, TCP_CONGESTION, 5290 KERNEL_SOCKPTR(optval), *optlen); 5291 tp->bpf_chg_cc_inprogress = 0; 5292 return ret; 5293 } 5294 5295 static int sol_tcp_sockopt(struct sock *sk, int optname, 5296 char *optval, int *optlen, 5297 bool getopt) 5298 { 5299 if (sk->sk_protocol != IPPROTO_TCP) 5300 return -EINVAL; 5301 5302 switch (optname) { 5303 case TCP_NODELAY: 5304 case TCP_MAXSEG: 5305 case TCP_KEEPIDLE: 5306 case TCP_KEEPINTVL: 5307 case TCP_KEEPCNT: 5308 case TCP_SYNCNT: 5309 case TCP_WINDOW_CLAMP: 5310 case TCP_THIN_LINEAR_TIMEOUTS: 5311 case TCP_USER_TIMEOUT: 5312 case TCP_NOTSENT_LOWAT: 5313 case TCP_SAVE_SYN: 5314 if (*optlen != sizeof(int)) 5315 return -EINVAL; 5316 break; 5317 case TCP_CONGESTION: 5318 return sol_tcp_sockopt_congestion(sk, optval, optlen, getopt); 5319 case TCP_SAVED_SYN: 5320 if (*optlen < 1) 5321 return -EINVAL; 5322 break; 5323 default: 5324 if (getopt) 5325 return -EINVAL; 5326 return bpf_sol_tcp_setsockopt(sk, optname, optval, *optlen); 5327 } 5328 5329 if (getopt) { 5330 if (optname == TCP_SAVED_SYN) { 5331 struct tcp_sock *tp = tcp_sk(sk); 5332 5333 if (!tp->saved_syn || 5334 *optlen > tcp_saved_syn_len(tp->saved_syn)) 5335 return -EINVAL; 5336 memcpy(optval, tp->saved_syn->data, *optlen); 5337 /* It cannot free tp->saved_syn here because it 5338 * does not know if the user space still needs it. 5339 */ 5340 return 0; 5341 } 5342 5343 return do_tcp_getsockopt(sk, SOL_TCP, optname, 5344 KERNEL_SOCKPTR(optval), 5345 KERNEL_SOCKPTR(optlen)); 5346 } 5347 5348 return do_tcp_setsockopt(sk, SOL_TCP, optname, 5349 KERNEL_SOCKPTR(optval), *optlen); 5350 } 5351 5352 static int sol_ip_sockopt(struct sock *sk, int optname, 5353 char *optval, int *optlen, 5354 bool getopt) 5355 { 5356 if (sk->sk_family != AF_INET) 5357 return -EINVAL; 5358 5359 switch (optname) { 5360 case IP_TOS: 5361 if (*optlen != sizeof(int)) 5362 return -EINVAL; 5363 break; 5364 default: 5365 return -EINVAL; 5366 } 5367 5368 if (getopt) 5369 return do_ip_getsockopt(sk, SOL_IP, optname, 5370 KERNEL_SOCKPTR(optval), 5371 KERNEL_SOCKPTR(optlen)); 5372 5373 return do_ip_setsockopt(sk, SOL_IP, optname, 5374 KERNEL_SOCKPTR(optval), *optlen); 5375 } 5376 5377 static int sol_ipv6_sockopt(struct sock *sk, int optname, 5378 char *optval, int *optlen, 5379 bool getopt) 5380 { 5381 if (sk->sk_family != AF_INET6) 5382 return -EINVAL; 5383 5384 switch (optname) { 5385 case IPV6_TCLASS: 5386 case IPV6_AUTOFLOWLABEL: 5387 if (*optlen != sizeof(int)) 5388 return -EINVAL; 5389 break; 5390 default: 5391 return -EINVAL; 5392 } 5393 5394 if (getopt) 5395 return ipv6_bpf_stub->ipv6_getsockopt(sk, SOL_IPV6, optname, 5396 KERNEL_SOCKPTR(optval), 5397 KERNEL_SOCKPTR(optlen)); 5398 5399 return ipv6_bpf_stub->ipv6_setsockopt(sk, SOL_IPV6, optname, 5400 KERNEL_SOCKPTR(optval), *optlen); 5401 } 5402 5403 static int __bpf_setsockopt(struct sock *sk, int level, int optname, 5404 char *optval, int optlen) 5405 { 5406 if (!sk_fullsock(sk)) 5407 return -EINVAL; 5408 5409 if (level == SOL_SOCKET) 5410 return sol_socket_sockopt(sk, optname, optval, &optlen, false); 5411 else if (IS_ENABLED(CONFIG_INET) && level == SOL_IP) 5412 return sol_ip_sockopt(sk, optname, optval, &optlen, false); 5413 else if (IS_ENABLED(CONFIG_IPV6) && level == SOL_IPV6) 5414 return sol_ipv6_sockopt(sk, optname, optval, &optlen, false); 5415 else if (IS_ENABLED(CONFIG_INET) && level == SOL_TCP) 5416 return sol_tcp_sockopt(sk, optname, optval, &optlen, false); 5417 5418 return -EINVAL; 5419 } 5420 5421 static int _bpf_setsockopt(struct sock *sk, int level, int optname, 5422 char *optval, int optlen) 5423 { 5424 if (sk_fullsock(sk)) 5425 sock_owned_by_me(sk); 5426 return __bpf_setsockopt(sk, level, optname, optval, optlen); 5427 } 5428 5429 static int __bpf_getsockopt(struct sock *sk, int level, int optname, 5430 char *optval, int optlen) 5431 { 5432 int err, saved_optlen = optlen; 5433 5434 if (!sk_fullsock(sk)) { 5435 err = -EINVAL; 5436 goto done; 5437 } 5438 5439 if (level == SOL_SOCKET) 5440 err = sol_socket_sockopt(sk, optname, optval, &optlen, true); 5441 else if (IS_ENABLED(CONFIG_INET) && level == SOL_TCP) 5442 err = sol_tcp_sockopt(sk, optname, optval, &optlen, true); 5443 else if (IS_ENABLED(CONFIG_INET) && level == SOL_IP) 5444 err = sol_ip_sockopt(sk, optname, optval, &optlen, true); 5445 else if (IS_ENABLED(CONFIG_IPV6) && level == SOL_IPV6) 5446 err = sol_ipv6_sockopt(sk, optname, optval, &optlen, true); 5447 else 5448 err = -EINVAL; 5449 5450 done: 5451 if (err) 5452 optlen = 0; 5453 if (optlen < saved_optlen) 5454 memset(optval + optlen, 0, saved_optlen - optlen); 5455 return err; 5456 } 5457 5458 static int _bpf_getsockopt(struct sock *sk, int level, int optname, 5459 char *optval, int optlen) 5460 { 5461 if (sk_fullsock(sk)) 5462 sock_owned_by_me(sk); 5463 return __bpf_getsockopt(sk, level, optname, optval, optlen); 5464 } 5465 5466 BPF_CALL_5(bpf_sk_setsockopt, struct sock *, sk, int, level, 5467 int, optname, char *, optval, int, optlen) 5468 { 5469 return _bpf_setsockopt(sk, level, optname, optval, optlen); 5470 } 5471 5472 const struct bpf_func_proto bpf_sk_setsockopt_proto = { 5473 .func = bpf_sk_setsockopt, 5474 .gpl_only = false, 5475 .ret_type = RET_INTEGER, 5476 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, 5477 .arg2_type = ARG_ANYTHING, 5478 .arg3_type = ARG_ANYTHING, 5479 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY, 5480 .arg5_type = ARG_CONST_SIZE, 5481 }; 5482 5483 BPF_CALL_5(bpf_sk_getsockopt, struct sock *, sk, int, level, 5484 int, optname, char *, optval, int, optlen) 5485 { 5486 return _bpf_getsockopt(sk, level, optname, optval, optlen); 5487 } 5488 5489 const struct bpf_func_proto bpf_sk_getsockopt_proto = { 5490 .func = bpf_sk_getsockopt, 5491 .gpl_only = false, 5492 .ret_type = RET_INTEGER, 5493 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, 5494 .arg2_type = ARG_ANYTHING, 5495 .arg3_type = ARG_ANYTHING, 5496 .arg4_type = ARG_PTR_TO_UNINIT_MEM, 5497 .arg5_type = ARG_CONST_SIZE, 5498 }; 5499 5500 BPF_CALL_5(bpf_unlocked_sk_setsockopt, struct sock *, sk, int, level, 5501 int, optname, char *, optval, int, optlen) 5502 { 5503 return __bpf_setsockopt(sk, level, optname, optval, optlen); 5504 } 5505 5506 const struct bpf_func_proto bpf_unlocked_sk_setsockopt_proto = { 5507 .func = bpf_unlocked_sk_setsockopt, 5508 .gpl_only = false, 5509 .ret_type = RET_INTEGER, 5510 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, 5511 .arg2_type = ARG_ANYTHING, 5512 .arg3_type = ARG_ANYTHING, 5513 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY, 5514 .arg5_type = ARG_CONST_SIZE, 5515 }; 5516 5517 BPF_CALL_5(bpf_unlocked_sk_getsockopt, struct sock *, sk, int, level, 5518 int, optname, char *, optval, int, optlen) 5519 { 5520 return __bpf_getsockopt(sk, level, optname, optval, optlen); 5521 } 5522 5523 const struct bpf_func_proto bpf_unlocked_sk_getsockopt_proto = { 5524 .func = bpf_unlocked_sk_getsockopt, 5525 .gpl_only = false, 5526 .ret_type = RET_INTEGER, 5527 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, 5528 .arg2_type = ARG_ANYTHING, 5529 .arg3_type = ARG_ANYTHING, 5530 .arg4_type = ARG_PTR_TO_UNINIT_MEM, 5531 .arg5_type = ARG_CONST_SIZE, 5532 }; 5533 5534 BPF_CALL_5(bpf_sock_addr_setsockopt, struct bpf_sock_addr_kern *, ctx, 5535 int, level, int, optname, char *, optval, int, optlen) 5536 { 5537 return _bpf_setsockopt(ctx->sk, level, optname, optval, optlen); 5538 } 5539 5540 static const struct bpf_func_proto bpf_sock_addr_setsockopt_proto = { 5541 .func = bpf_sock_addr_setsockopt, 5542 .gpl_only = false, 5543 .ret_type = RET_INTEGER, 5544 .arg1_type = ARG_PTR_TO_CTX, 5545 .arg2_type = ARG_ANYTHING, 5546 .arg3_type = ARG_ANYTHING, 5547 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY, 5548 .arg5_type = ARG_CONST_SIZE, 5549 }; 5550 5551 BPF_CALL_5(bpf_sock_addr_getsockopt, struct bpf_sock_addr_kern *, ctx, 5552 int, level, int, optname, char *, optval, int, optlen) 5553 { 5554 return _bpf_getsockopt(ctx->sk, level, optname, optval, optlen); 5555 } 5556 5557 static const struct bpf_func_proto bpf_sock_addr_getsockopt_proto = { 5558 .func = bpf_sock_addr_getsockopt, 5559 .gpl_only = false, 5560 .ret_type = RET_INTEGER, 5561 .arg1_type = ARG_PTR_TO_CTX, 5562 .arg2_type = ARG_ANYTHING, 5563 .arg3_type = ARG_ANYTHING, 5564 .arg4_type = ARG_PTR_TO_UNINIT_MEM, 5565 .arg5_type = ARG_CONST_SIZE, 5566 }; 5567 5568 BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock, 5569 int, level, int, optname, char *, optval, int, optlen) 5570 { 5571 return _bpf_setsockopt(bpf_sock->sk, level, optname, optval, optlen); 5572 } 5573 5574 static const struct bpf_func_proto bpf_sock_ops_setsockopt_proto = { 5575 .func = bpf_sock_ops_setsockopt, 5576 .gpl_only = false, 5577 .ret_type = RET_INTEGER, 5578 .arg1_type = ARG_PTR_TO_CTX, 5579 .arg2_type = ARG_ANYTHING, 5580 .arg3_type = ARG_ANYTHING, 5581 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY, 5582 .arg5_type = ARG_CONST_SIZE, 5583 }; 5584 5585 static int bpf_sock_ops_get_syn(struct bpf_sock_ops_kern *bpf_sock, 5586 int optname, const u8 **start) 5587 { 5588 struct sk_buff *syn_skb = bpf_sock->syn_skb; 5589 const u8 *hdr_start; 5590 int ret; 5591 5592 if (syn_skb) { 5593 /* sk is a request_sock here */ 5594 5595 if (optname == TCP_BPF_SYN) { 5596 hdr_start = syn_skb->data; 5597 ret = tcp_hdrlen(syn_skb); 5598 } else if (optname == TCP_BPF_SYN_IP) { 5599 hdr_start = skb_network_header(syn_skb); 5600 ret = skb_network_header_len(syn_skb) + 5601 tcp_hdrlen(syn_skb); 5602 } else { 5603 /* optname == TCP_BPF_SYN_MAC */ 5604 hdr_start = skb_mac_header(syn_skb); 5605 ret = skb_mac_header_len(syn_skb) + 5606 skb_network_header_len(syn_skb) + 5607 tcp_hdrlen(syn_skb); 5608 } 5609 } else { 5610 struct sock *sk = bpf_sock->sk; 5611 struct saved_syn *saved_syn; 5612 5613 if (sk->sk_state == TCP_NEW_SYN_RECV) 5614 /* synack retransmit. bpf_sock->syn_skb will 5615 * not be available. It has to resort to 5616 * saved_syn (if it is saved). 5617 */ 5618 saved_syn = inet_reqsk(sk)->saved_syn; 5619 else 5620 saved_syn = tcp_sk(sk)->saved_syn; 5621 5622 if (!saved_syn) 5623 return -ENOENT; 5624 5625 if (optname == TCP_BPF_SYN) { 5626 hdr_start = saved_syn->data + 5627 saved_syn->mac_hdrlen + 5628 saved_syn->network_hdrlen; 5629 ret = saved_syn->tcp_hdrlen; 5630 } else if (optname == TCP_BPF_SYN_IP) { 5631 hdr_start = saved_syn->data + 5632 saved_syn->mac_hdrlen; 5633 ret = saved_syn->network_hdrlen + 5634 saved_syn->tcp_hdrlen; 5635 } else { 5636 /* optname == TCP_BPF_SYN_MAC */ 5637 5638 /* TCP_SAVE_SYN may not have saved the mac hdr */ 5639 if (!saved_syn->mac_hdrlen) 5640 return -ENOENT; 5641 5642 hdr_start = saved_syn->data; 5643 ret = saved_syn->mac_hdrlen + 5644 saved_syn->network_hdrlen + 5645 saved_syn->tcp_hdrlen; 5646 } 5647 } 5648 5649 *start = hdr_start; 5650 return ret; 5651 } 5652 5653 BPF_CALL_5(bpf_sock_ops_getsockopt, struct bpf_sock_ops_kern *, bpf_sock, 5654 int, level, int, optname, char *, optval, int, optlen) 5655 { 5656 if (IS_ENABLED(CONFIG_INET) && level == SOL_TCP && 5657 optname >= TCP_BPF_SYN && optname <= TCP_BPF_SYN_MAC) { 5658 int ret, copy_len = 0; 5659 const u8 *start; 5660 5661 ret = bpf_sock_ops_get_syn(bpf_sock, optname, &start); 5662 if (ret > 0) { 5663 copy_len = ret; 5664 if (optlen < copy_len) { 5665 copy_len = optlen; 5666 ret = -ENOSPC; 5667 } 5668 5669 memcpy(optval, start, copy_len); 5670 } 5671 5672 /* Zero out unused buffer at the end */ 5673 memset(optval + copy_len, 0, optlen - copy_len); 5674 5675 return ret; 5676 } 5677 5678 return _bpf_getsockopt(bpf_sock->sk, level, optname, optval, optlen); 5679 } 5680 5681 static const struct bpf_func_proto bpf_sock_ops_getsockopt_proto = { 5682 .func = bpf_sock_ops_getsockopt, 5683 .gpl_only = false, 5684 .ret_type = RET_INTEGER, 5685 .arg1_type = ARG_PTR_TO_CTX, 5686 .arg2_type = ARG_ANYTHING, 5687 .arg3_type = ARG_ANYTHING, 5688 .arg4_type = ARG_PTR_TO_UNINIT_MEM, 5689 .arg5_type = ARG_CONST_SIZE, 5690 }; 5691 5692 BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock, 5693 int, argval) 5694 { 5695 struct sock *sk = bpf_sock->sk; 5696 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS; 5697 5698 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk)) 5699 return -EINVAL; 5700 5701 tcp_sk(sk)->bpf_sock_ops_cb_flags = val; 5702 5703 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS); 5704 } 5705 5706 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = { 5707 .func = bpf_sock_ops_cb_flags_set, 5708 .gpl_only = false, 5709 .ret_type = RET_INTEGER, 5710 .arg1_type = ARG_PTR_TO_CTX, 5711 .arg2_type = ARG_ANYTHING, 5712 }; 5713 5714 const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly; 5715 EXPORT_SYMBOL_GPL(ipv6_bpf_stub); 5716 5717 BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr, 5718 int, addr_len) 5719 { 5720 #ifdef CONFIG_INET 5721 struct sock *sk = ctx->sk; 5722 u32 flags = BIND_FROM_BPF; 5723 int err; 5724 5725 err = -EINVAL; 5726 if (addr_len < offsetofend(struct sockaddr, sa_family)) 5727 return err; 5728 if (addr->sa_family == AF_INET) { 5729 if (addr_len < sizeof(struct sockaddr_in)) 5730 return err; 5731 if (((struct sockaddr_in *)addr)->sin_port == htons(0)) 5732 flags |= BIND_FORCE_ADDRESS_NO_PORT; 5733 return __inet_bind(sk, addr, addr_len, flags); 5734 #if IS_ENABLED(CONFIG_IPV6) 5735 } else if (addr->sa_family == AF_INET6) { 5736 if (addr_len < SIN6_LEN_RFC2133) 5737 return err; 5738 if (((struct sockaddr_in6 *)addr)->sin6_port == htons(0)) 5739 flags |= BIND_FORCE_ADDRESS_NO_PORT; 5740 /* ipv6_bpf_stub cannot be NULL, since it's called from 5741 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded 5742 */ 5743 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, flags); 5744 #endif /* CONFIG_IPV6 */ 5745 } 5746 #endif /* CONFIG_INET */ 5747 5748 return -EAFNOSUPPORT; 5749 } 5750 5751 static const struct bpf_func_proto bpf_bind_proto = { 5752 .func = bpf_bind, 5753 .gpl_only = false, 5754 .ret_type = RET_INTEGER, 5755 .arg1_type = ARG_PTR_TO_CTX, 5756 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, 5757 .arg3_type = ARG_CONST_SIZE, 5758 }; 5759 5760 #ifdef CONFIG_XFRM 5761 5762 #if (IS_BUILTIN(CONFIG_XFRM_INTERFACE) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) || \ 5763 (IS_MODULE(CONFIG_XFRM_INTERFACE) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES)) 5764 5765 struct metadata_dst __percpu *xfrm_bpf_md_dst; 5766 EXPORT_SYMBOL_GPL(xfrm_bpf_md_dst); 5767 5768 #endif 5769 5770 BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index, 5771 struct bpf_xfrm_state *, to, u32, size, u64, flags) 5772 { 5773 const struct sec_path *sp = skb_sec_path(skb); 5774 const struct xfrm_state *x; 5775 5776 if (!sp || unlikely(index >= sp->len || flags)) 5777 goto err_clear; 5778 5779 x = sp->xvec[index]; 5780 5781 if (unlikely(size != sizeof(struct bpf_xfrm_state))) 5782 goto err_clear; 5783 5784 to->reqid = x->props.reqid; 5785 to->spi = x->id.spi; 5786 to->family = x->props.family; 5787 to->ext = 0; 5788 5789 if (to->family == AF_INET6) { 5790 memcpy(to->remote_ipv6, x->props.saddr.a6, 5791 sizeof(to->remote_ipv6)); 5792 } else { 5793 to->remote_ipv4 = x->props.saddr.a4; 5794 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3); 5795 } 5796 5797 return 0; 5798 err_clear: 5799 memset(to, 0, size); 5800 return -EINVAL; 5801 } 5802 5803 static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = { 5804 .func = bpf_skb_get_xfrm_state, 5805 .gpl_only = false, 5806 .ret_type = RET_INTEGER, 5807 .arg1_type = ARG_PTR_TO_CTX, 5808 .arg2_type = ARG_ANYTHING, 5809 .arg3_type = ARG_PTR_TO_UNINIT_MEM, 5810 .arg4_type = ARG_CONST_SIZE, 5811 .arg5_type = ARG_ANYTHING, 5812 }; 5813 #endif 5814 5815 #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6) 5816 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params, u32 mtu) 5817 { 5818 params->h_vlan_TCI = 0; 5819 params->h_vlan_proto = 0; 5820 if (mtu) 5821 params->mtu_result = mtu; /* union with tot_len */ 5822 5823 return 0; 5824 } 5825 #endif 5826 5827 #if IS_ENABLED(CONFIG_INET) 5828 static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params, 5829 u32 flags, bool check_mtu) 5830 { 5831 struct fib_nh_common *nhc; 5832 struct in_device *in_dev; 5833 struct neighbour *neigh; 5834 struct net_device *dev; 5835 struct fib_result res; 5836 struct flowi4 fl4; 5837 u32 mtu = 0; 5838 int err; 5839 5840 dev = dev_get_by_index_rcu(net, params->ifindex); 5841 if (unlikely(!dev)) 5842 return -ENODEV; 5843 5844 /* verify forwarding is enabled on this interface */ 5845 in_dev = __in_dev_get_rcu(dev); 5846 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev))) 5847 return BPF_FIB_LKUP_RET_FWD_DISABLED; 5848 5849 if (flags & BPF_FIB_LOOKUP_OUTPUT) { 5850 fl4.flowi4_iif = 1; 5851 fl4.flowi4_oif = params->ifindex; 5852 } else { 5853 fl4.flowi4_iif = params->ifindex; 5854 fl4.flowi4_oif = 0; 5855 } 5856 fl4.flowi4_tos = params->tos & IPTOS_RT_MASK; 5857 fl4.flowi4_scope = RT_SCOPE_UNIVERSE; 5858 fl4.flowi4_flags = 0; 5859 5860 fl4.flowi4_proto = params->l4_protocol; 5861 fl4.daddr = params->ipv4_dst; 5862 fl4.saddr = params->ipv4_src; 5863 fl4.fl4_sport = params->sport; 5864 fl4.fl4_dport = params->dport; 5865 fl4.flowi4_multipath_hash = 0; 5866 5867 if (flags & BPF_FIB_LOOKUP_DIRECT) { 5868 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN; 5869 struct fib_table *tb; 5870 5871 if (flags & BPF_FIB_LOOKUP_TBID) { 5872 tbid = params->tbid; 5873 /* zero out for vlan output */ 5874 params->tbid = 0; 5875 } 5876 5877 tb = fib_get_table(net, tbid); 5878 if (unlikely(!tb)) 5879 return BPF_FIB_LKUP_RET_NOT_FWDED; 5880 5881 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF); 5882 } else { 5883 fl4.flowi4_mark = 0; 5884 fl4.flowi4_secid = 0; 5885 fl4.flowi4_tun_key.tun_id = 0; 5886 fl4.flowi4_uid = sock_net_uid(net, NULL); 5887 5888 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF); 5889 } 5890 5891 if (err) { 5892 /* map fib lookup errors to RTN_ type */ 5893 if (err == -EINVAL) 5894 return BPF_FIB_LKUP_RET_BLACKHOLE; 5895 if (err == -EHOSTUNREACH) 5896 return BPF_FIB_LKUP_RET_UNREACHABLE; 5897 if (err == -EACCES) 5898 return BPF_FIB_LKUP_RET_PROHIBIT; 5899 5900 return BPF_FIB_LKUP_RET_NOT_FWDED; 5901 } 5902 5903 if (res.type != RTN_UNICAST) 5904 return BPF_FIB_LKUP_RET_NOT_FWDED; 5905 5906 if (fib_info_num_path(res.fi) > 1) 5907 fib_select_path(net, &res, &fl4, NULL); 5908 5909 if (check_mtu) { 5910 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst); 5911 if (params->tot_len > mtu) { 5912 params->mtu_result = mtu; /* union with tot_len */ 5913 return BPF_FIB_LKUP_RET_FRAG_NEEDED; 5914 } 5915 } 5916 5917 nhc = res.nhc; 5918 5919 /* do not handle lwt encaps right now */ 5920 if (nhc->nhc_lwtstate) 5921 return BPF_FIB_LKUP_RET_UNSUPP_LWT; 5922 5923 dev = nhc->nhc_dev; 5924 5925 params->rt_metric = res.fi->fib_priority; 5926 params->ifindex = dev->ifindex; 5927 5928 if (flags & BPF_FIB_LOOKUP_SRC) 5929 params->ipv4_src = fib_result_prefsrc(net, &res); 5930 5931 /* xdp and cls_bpf programs are run in RCU-bh so 5932 * rcu_read_lock_bh is not needed here 5933 */ 5934 if (likely(nhc->nhc_gw_family != AF_INET6)) { 5935 if (nhc->nhc_gw_family) 5936 params->ipv4_dst = nhc->nhc_gw.ipv4; 5937 } else { 5938 struct in6_addr *dst = (struct in6_addr *)params->ipv6_dst; 5939 5940 params->family = AF_INET6; 5941 *dst = nhc->nhc_gw.ipv6; 5942 } 5943 5944 if (flags & BPF_FIB_LOOKUP_SKIP_NEIGH) 5945 goto set_fwd_params; 5946 5947 if (likely(nhc->nhc_gw_family != AF_INET6)) 5948 neigh = __ipv4_neigh_lookup_noref(dev, 5949 (__force u32)params->ipv4_dst); 5950 else 5951 neigh = __ipv6_neigh_lookup_noref_stub(dev, params->ipv6_dst); 5952 5953 if (!neigh || !(READ_ONCE(neigh->nud_state) & NUD_VALID)) 5954 return BPF_FIB_LKUP_RET_NO_NEIGH; 5955 memcpy(params->dmac, neigh->ha, ETH_ALEN); 5956 memcpy(params->smac, dev->dev_addr, ETH_ALEN); 5957 5958 set_fwd_params: 5959 return bpf_fib_set_fwd_params(params, mtu); 5960 } 5961 #endif 5962 5963 #if IS_ENABLED(CONFIG_IPV6) 5964 static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params, 5965 u32 flags, bool check_mtu) 5966 { 5967 struct in6_addr *src = (struct in6_addr *) params->ipv6_src; 5968 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst; 5969 struct fib6_result res = {}; 5970 struct neighbour *neigh; 5971 struct net_device *dev; 5972 struct inet6_dev *idev; 5973 struct flowi6 fl6; 5974 int strict = 0; 5975 int oif, err; 5976 u32 mtu = 0; 5977 5978 /* link local addresses are never forwarded */ 5979 if (rt6_need_strict(dst) || rt6_need_strict(src)) 5980 return BPF_FIB_LKUP_RET_NOT_FWDED; 5981 5982 dev = dev_get_by_index_rcu(net, params->ifindex); 5983 if (unlikely(!dev)) 5984 return -ENODEV; 5985 5986 idev = __in6_dev_get_safely(dev); 5987 if (unlikely(!idev || !idev->cnf.forwarding)) 5988 return BPF_FIB_LKUP_RET_FWD_DISABLED; 5989 5990 if (flags & BPF_FIB_LOOKUP_OUTPUT) { 5991 fl6.flowi6_iif = 1; 5992 oif = fl6.flowi6_oif = params->ifindex; 5993 } else { 5994 oif = fl6.flowi6_iif = params->ifindex; 5995 fl6.flowi6_oif = 0; 5996 strict = RT6_LOOKUP_F_HAS_SADDR; 5997 } 5998 fl6.flowlabel = params->flowinfo; 5999 fl6.flowi6_scope = 0; 6000 fl6.flowi6_flags = 0; 6001 fl6.mp_hash = 0; 6002 6003 fl6.flowi6_proto = params->l4_protocol; 6004 fl6.daddr = *dst; 6005 fl6.saddr = *src; 6006 fl6.fl6_sport = params->sport; 6007 fl6.fl6_dport = params->dport; 6008 6009 if (flags & BPF_FIB_LOOKUP_DIRECT) { 6010 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN; 6011 struct fib6_table *tb; 6012 6013 if (flags & BPF_FIB_LOOKUP_TBID) { 6014 tbid = params->tbid; 6015 /* zero out for vlan output */ 6016 params->tbid = 0; 6017 } 6018 6019 tb = ipv6_stub->fib6_get_table(net, tbid); 6020 if (unlikely(!tb)) 6021 return BPF_FIB_LKUP_RET_NOT_FWDED; 6022 6023 err = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, &res, 6024 strict); 6025 } else { 6026 fl6.flowi6_mark = 0; 6027 fl6.flowi6_secid = 0; 6028 fl6.flowi6_tun_key.tun_id = 0; 6029 fl6.flowi6_uid = sock_net_uid(net, NULL); 6030 6031 err = ipv6_stub->fib6_lookup(net, oif, &fl6, &res, strict); 6032 } 6033 6034 if (unlikely(err || IS_ERR_OR_NULL(res.f6i) || 6035 res.f6i == net->ipv6.fib6_null_entry)) 6036 return BPF_FIB_LKUP_RET_NOT_FWDED; 6037 6038 switch (res.fib6_type) { 6039 /* only unicast is forwarded */ 6040 case RTN_UNICAST: 6041 break; 6042 case RTN_BLACKHOLE: 6043 return BPF_FIB_LKUP_RET_BLACKHOLE; 6044 case RTN_UNREACHABLE: 6045 return BPF_FIB_LKUP_RET_UNREACHABLE; 6046 case RTN_PROHIBIT: 6047 return BPF_FIB_LKUP_RET_PROHIBIT; 6048 default: 6049 return BPF_FIB_LKUP_RET_NOT_FWDED; 6050 } 6051 6052 ipv6_stub->fib6_select_path(net, &res, &fl6, fl6.flowi6_oif, 6053 fl6.flowi6_oif != 0, NULL, strict); 6054 6055 if (check_mtu) { 6056 mtu = ipv6_stub->ip6_mtu_from_fib6(&res, dst, src); 6057 if (params->tot_len > mtu) { 6058 params->mtu_result = mtu; /* union with tot_len */ 6059 return BPF_FIB_LKUP_RET_FRAG_NEEDED; 6060 } 6061 } 6062 6063 if (res.nh->fib_nh_lws) 6064 return BPF_FIB_LKUP_RET_UNSUPP_LWT; 6065 6066 if (res.nh->fib_nh_gw_family) 6067 *dst = res.nh->fib_nh_gw6; 6068 6069 dev = res.nh->fib_nh_dev; 6070 params->rt_metric = res.f6i->fib6_metric; 6071 params->ifindex = dev->ifindex; 6072 6073 if (flags & BPF_FIB_LOOKUP_SRC) { 6074 if (res.f6i->fib6_prefsrc.plen) { 6075 *src = res.f6i->fib6_prefsrc.addr; 6076 } else { 6077 err = ipv6_bpf_stub->ipv6_dev_get_saddr(net, dev, 6078 &fl6.daddr, 0, 6079 src); 6080 if (err) 6081 return BPF_FIB_LKUP_RET_NO_SRC_ADDR; 6082 } 6083 } 6084 6085 if (flags & BPF_FIB_LOOKUP_SKIP_NEIGH) 6086 goto set_fwd_params; 6087 6088 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is 6089 * not needed here. 6090 */ 6091 neigh = __ipv6_neigh_lookup_noref_stub(dev, dst); 6092 if (!neigh || !(READ_ONCE(neigh->nud_state) & NUD_VALID)) 6093 return BPF_FIB_LKUP_RET_NO_NEIGH; 6094 memcpy(params->dmac, neigh->ha, ETH_ALEN); 6095 memcpy(params->smac, dev->dev_addr, ETH_ALEN); 6096 6097 set_fwd_params: 6098 return bpf_fib_set_fwd_params(params, mtu); 6099 } 6100 #endif 6101 6102 #define BPF_FIB_LOOKUP_MASK (BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT | \ 6103 BPF_FIB_LOOKUP_SKIP_NEIGH | BPF_FIB_LOOKUP_TBID | \ 6104 BPF_FIB_LOOKUP_SRC) 6105 6106 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx, 6107 struct bpf_fib_lookup *, params, int, plen, u32, flags) 6108 { 6109 if (plen < sizeof(*params)) 6110 return -EINVAL; 6111 6112 if (flags & ~BPF_FIB_LOOKUP_MASK) 6113 return -EINVAL; 6114 6115 switch (params->family) { 6116 #if IS_ENABLED(CONFIG_INET) 6117 case AF_INET: 6118 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params, 6119 flags, true); 6120 #endif 6121 #if IS_ENABLED(CONFIG_IPV6) 6122 case AF_INET6: 6123 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params, 6124 flags, true); 6125 #endif 6126 } 6127 return -EAFNOSUPPORT; 6128 } 6129 6130 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = { 6131 .func = bpf_xdp_fib_lookup, 6132 .gpl_only = true, 6133 .ret_type = RET_INTEGER, 6134 .arg1_type = ARG_PTR_TO_CTX, 6135 .arg2_type = ARG_PTR_TO_MEM, 6136 .arg3_type = ARG_CONST_SIZE, 6137 .arg4_type = ARG_ANYTHING, 6138 }; 6139 6140 BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb, 6141 struct bpf_fib_lookup *, params, int, plen, u32, flags) 6142 { 6143 struct net *net = dev_net(skb->dev); 6144 int rc = -EAFNOSUPPORT; 6145 bool check_mtu = false; 6146 6147 if (plen < sizeof(*params)) 6148 return -EINVAL; 6149 6150 if (flags & ~BPF_FIB_LOOKUP_MASK) 6151 return -EINVAL; 6152 6153 if (params->tot_len) 6154 check_mtu = true; 6155 6156 switch (params->family) { 6157 #if IS_ENABLED(CONFIG_INET) 6158 case AF_INET: 6159 rc = bpf_ipv4_fib_lookup(net, params, flags, check_mtu); 6160 break; 6161 #endif 6162 #if IS_ENABLED(CONFIG_IPV6) 6163 case AF_INET6: 6164 rc = bpf_ipv6_fib_lookup(net, params, flags, check_mtu); 6165 break; 6166 #endif 6167 } 6168 6169 if (rc == BPF_FIB_LKUP_RET_SUCCESS && !check_mtu) { 6170 struct net_device *dev; 6171 6172 /* When tot_len isn't provided by user, check skb 6173 * against MTU of FIB lookup resulting net_device 6174 */ 6175 dev = dev_get_by_index_rcu(net, params->ifindex); 6176 if (!is_skb_forwardable(dev, skb)) 6177 rc = BPF_FIB_LKUP_RET_FRAG_NEEDED; 6178 6179 params->mtu_result = dev->mtu; /* union with tot_len */ 6180 } 6181 6182 return rc; 6183 } 6184 6185 static const struct bpf_func_proto bpf_skb_fib_lookup_proto = { 6186 .func = bpf_skb_fib_lookup, 6187 .gpl_only = true, 6188 .ret_type = RET_INTEGER, 6189 .arg1_type = ARG_PTR_TO_CTX, 6190 .arg2_type = ARG_PTR_TO_MEM, 6191 .arg3_type = ARG_CONST_SIZE, 6192 .arg4_type = ARG_ANYTHING, 6193 }; 6194 6195 static struct net_device *__dev_via_ifindex(struct net_device *dev_curr, 6196 u32 ifindex) 6197 { 6198 struct net *netns = dev_net(dev_curr); 6199 6200 /* Non-redirect use-cases can use ifindex=0 and save ifindex lookup */ 6201 if (ifindex == 0) 6202 return dev_curr; 6203 6204 return dev_get_by_index_rcu(netns, ifindex); 6205 } 6206 6207 BPF_CALL_5(bpf_skb_check_mtu, struct sk_buff *, skb, 6208 u32, ifindex, u32 *, mtu_len, s32, len_diff, u64, flags) 6209 { 6210 int ret = BPF_MTU_CHK_RET_FRAG_NEEDED; 6211 struct net_device *dev = skb->dev; 6212 int skb_len, dev_len; 6213 int mtu; 6214 6215 if (unlikely(flags & ~(BPF_MTU_CHK_SEGS))) 6216 return -EINVAL; 6217 6218 if (unlikely(flags & BPF_MTU_CHK_SEGS && (len_diff || *mtu_len))) 6219 return -EINVAL; 6220 6221 dev = __dev_via_ifindex(dev, ifindex); 6222 if (unlikely(!dev)) 6223 return -ENODEV; 6224 6225 mtu = READ_ONCE(dev->mtu); 6226 6227 dev_len = mtu + dev->hard_header_len; 6228 6229 /* If set use *mtu_len as input, L3 as iph->tot_len (like fib_lookup) */ 6230 skb_len = *mtu_len ? *mtu_len + dev->hard_header_len : skb->len; 6231 6232 skb_len += len_diff; /* minus result pass check */ 6233 if (skb_len <= dev_len) { 6234 ret = BPF_MTU_CHK_RET_SUCCESS; 6235 goto out; 6236 } 6237 /* At this point, skb->len exceed MTU, but as it include length of all 6238 * segments, it can still be below MTU. The SKB can possibly get 6239 * re-segmented in transmit path (see validate_xmit_skb). Thus, user 6240 * must choose if segs are to be MTU checked. 6241 */ 6242 if (skb_is_gso(skb)) { 6243 ret = BPF_MTU_CHK_RET_SUCCESS; 6244 6245 if (flags & BPF_MTU_CHK_SEGS && 6246 !skb_gso_validate_network_len(skb, mtu)) 6247 ret = BPF_MTU_CHK_RET_SEGS_TOOBIG; 6248 } 6249 out: 6250 /* BPF verifier guarantees valid pointer */ 6251 *mtu_len = mtu; 6252 6253 return ret; 6254 } 6255 6256 BPF_CALL_5(bpf_xdp_check_mtu, struct xdp_buff *, xdp, 6257 u32, ifindex, u32 *, mtu_len, s32, len_diff, u64, flags) 6258 { 6259 struct net_device *dev = xdp->rxq->dev; 6260 int xdp_len = xdp->data_end - xdp->data; 6261 int ret = BPF_MTU_CHK_RET_SUCCESS; 6262 int mtu, dev_len; 6263 6264 /* XDP variant doesn't support multi-buffer segment check (yet) */ 6265 if (unlikely(flags)) 6266 return -EINVAL; 6267 6268 dev = __dev_via_ifindex(dev, ifindex); 6269 if (unlikely(!dev)) 6270 return -ENODEV; 6271 6272 mtu = READ_ONCE(dev->mtu); 6273 6274 /* Add L2-header as dev MTU is L3 size */ 6275 dev_len = mtu + dev->hard_header_len; 6276 6277 /* Use *mtu_len as input, L3 as iph->tot_len (like fib_lookup) */ 6278 if (*mtu_len) 6279 xdp_len = *mtu_len + dev->hard_header_len; 6280 6281 xdp_len += len_diff; /* minus result pass check */ 6282 if (xdp_len > dev_len) 6283 ret = BPF_MTU_CHK_RET_FRAG_NEEDED; 6284 6285 /* BPF verifier guarantees valid pointer */ 6286 *mtu_len = mtu; 6287 6288 return ret; 6289 } 6290 6291 static const struct bpf_func_proto bpf_skb_check_mtu_proto = { 6292 .func = bpf_skb_check_mtu, 6293 .gpl_only = true, 6294 .ret_type = RET_INTEGER, 6295 .arg1_type = ARG_PTR_TO_CTX, 6296 .arg2_type = ARG_ANYTHING, 6297 .arg3_type = ARG_PTR_TO_INT, 6298 .arg4_type = ARG_ANYTHING, 6299 .arg5_type = ARG_ANYTHING, 6300 }; 6301 6302 static const struct bpf_func_proto bpf_xdp_check_mtu_proto = { 6303 .func = bpf_xdp_check_mtu, 6304 .gpl_only = true, 6305 .ret_type = RET_INTEGER, 6306 .arg1_type = ARG_PTR_TO_CTX, 6307 .arg2_type = ARG_ANYTHING, 6308 .arg3_type = ARG_PTR_TO_INT, 6309 .arg4_type = ARG_ANYTHING, 6310 .arg5_type = ARG_ANYTHING, 6311 }; 6312 6313 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF) 6314 static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len) 6315 { 6316 int err; 6317 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr; 6318 6319 if (!seg6_validate_srh(srh, len, false)) 6320 return -EINVAL; 6321 6322 switch (type) { 6323 case BPF_LWT_ENCAP_SEG6_INLINE: 6324 if (skb->protocol != htons(ETH_P_IPV6)) 6325 return -EBADMSG; 6326 6327 err = seg6_do_srh_inline(skb, srh); 6328 break; 6329 case BPF_LWT_ENCAP_SEG6: 6330 skb_reset_inner_headers(skb); 6331 skb->encapsulation = 1; 6332 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6); 6333 break; 6334 default: 6335 return -EINVAL; 6336 } 6337 6338 bpf_compute_data_pointers(skb); 6339 if (err) 6340 return err; 6341 6342 skb_set_transport_header(skb, sizeof(struct ipv6hdr)); 6343 6344 return seg6_lookup_nexthop(skb, NULL, 0); 6345 } 6346 #endif /* CONFIG_IPV6_SEG6_BPF */ 6347 6348 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF) 6349 static int bpf_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len, 6350 bool ingress) 6351 { 6352 return bpf_lwt_push_ip_encap(skb, hdr, len, ingress); 6353 } 6354 #endif 6355 6356 BPF_CALL_4(bpf_lwt_in_push_encap, struct sk_buff *, skb, u32, type, void *, hdr, 6357 u32, len) 6358 { 6359 switch (type) { 6360 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF) 6361 case BPF_LWT_ENCAP_SEG6: 6362 case BPF_LWT_ENCAP_SEG6_INLINE: 6363 return bpf_push_seg6_encap(skb, type, hdr, len); 6364 #endif 6365 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF) 6366 case BPF_LWT_ENCAP_IP: 6367 return bpf_push_ip_encap(skb, hdr, len, true /* ingress */); 6368 #endif 6369 default: 6370 return -EINVAL; 6371 } 6372 } 6373 6374 BPF_CALL_4(bpf_lwt_xmit_push_encap, struct sk_buff *, skb, u32, type, 6375 void *, hdr, u32, len) 6376 { 6377 switch (type) { 6378 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF) 6379 case BPF_LWT_ENCAP_IP: 6380 return bpf_push_ip_encap(skb, hdr, len, false /* egress */); 6381 #endif 6382 default: 6383 return -EINVAL; 6384 } 6385 } 6386 6387 static const struct bpf_func_proto bpf_lwt_in_push_encap_proto = { 6388 .func = bpf_lwt_in_push_encap, 6389 .gpl_only = false, 6390 .ret_type = RET_INTEGER, 6391 .arg1_type = ARG_PTR_TO_CTX, 6392 .arg2_type = ARG_ANYTHING, 6393 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY, 6394 .arg4_type = ARG_CONST_SIZE 6395 }; 6396 6397 static const struct bpf_func_proto bpf_lwt_xmit_push_encap_proto = { 6398 .func = bpf_lwt_xmit_push_encap, 6399 .gpl_only = false, 6400 .ret_type = RET_INTEGER, 6401 .arg1_type = ARG_PTR_TO_CTX, 6402 .arg2_type = ARG_ANYTHING, 6403 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY, 6404 .arg4_type = ARG_CONST_SIZE 6405 }; 6406 6407 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF) 6408 BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset, 6409 const void *, from, u32, len) 6410 { 6411 struct seg6_bpf_srh_state *srh_state = 6412 this_cpu_ptr(&seg6_bpf_srh_states); 6413 struct ipv6_sr_hdr *srh = srh_state->srh; 6414 void *srh_tlvs, *srh_end, *ptr; 6415 int srhoff = 0; 6416 6417 if (srh == NULL) 6418 return -EINVAL; 6419 6420 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4)); 6421 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen); 6422 6423 ptr = skb->data + offset; 6424 if (ptr >= srh_tlvs && ptr + len <= srh_end) 6425 srh_state->valid = false; 6426 else if (ptr < (void *)&srh->flags || 6427 ptr + len > (void *)&srh->segments) 6428 return -EFAULT; 6429 6430 if (unlikely(bpf_try_make_writable(skb, offset + len))) 6431 return -EFAULT; 6432 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) 6433 return -EINVAL; 6434 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); 6435 6436 memcpy(skb->data + offset, from, len); 6437 return 0; 6438 } 6439 6440 static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = { 6441 .func = bpf_lwt_seg6_store_bytes, 6442 .gpl_only = false, 6443 .ret_type = RET_INTEGER, 6444 .arg1_type = ARG_PTR_TO_CTX, 6445 .arg2_type = ARG_ANYTHING, 6446 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY, 6447 .arg4_type = ARG_CONST_SIZE 6448 }; 6449 6450 static void bpf_update_srh_state(struct sk_buff *skb) 6451 { 6452 struct seg6_bpf_srh_state *srh_state = 6453 this_cpu_ptr(&seg6_bpf_srh_states); 6454 int srhoff = 0; 6455 6456 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) { 6457 srh_state->srh = NULL; 6458 } else { 6459 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); 6460 srh_state->hdrlen = srh_state->srh->hdrlen << 3; 6461 srh_state->valid = true; 6462 } 6463 } 6464 6465 BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb, 6466 u32, action, void *, param, u32, param_len) 6467 { 6468 struct seg6_bpf_srh_state *srh_state = 6469 this_cpu_ptr(&seg6_bpf_srh_states); 6470 int hdroff = 0; 6471 int err; 6472 6473 switch (action) { 6474 case SEG6_LOCAL_ACTION_END_X: 6475 if (!seg6_bpf_has_valid_srh(skb)) 6476 return -EBADMSG; 6477 if (param_len != sizeof(struct in6_addr)) 6478 return -EINVAL; 6479 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0); 6480 case SEG6_LOCAL_ACTION_END_T: 6481 if (!seg6_bpf_has_valid_srh(skb)) 6482 return -EBADMSG; 6483 if (param_len != sizeof(int)) 6484 return -EINVAL; 6485 return seg6_lookup_nexthop(skb, NULL, *(int *)param); 6486 case SEG6_LOCAL_ACTION_END_DT6: 6487 if (!seg6_bpf_has_valid_srh(skb)) 6488 return -EBADMSG; 6489 if (param_len != sizeof(int)) 6490 return -EINVAL; 6491 6492 if (ipv6_find_hdr(skb, &hdroff, IPPROTO_IPV6, NULL, NULL) < 0) 6493 return -EBADMSG; 6494 if (!pskb_pull(skb, hdroff)) 6495 return -EBADMSG; 6496 6497 skb_postpull_rcsum(skb, skb_network_header(skb), hdroff); 6498 skb_reset_network_header(skb); 6499 skb_reset_transport_header(skb); 6500 skb->encapsulation = 0; 6501 6502 bpf_compute_data_pointers(skb); 6503 bpf_update_srh_state(skb); 6504 return seg6_lookup_nexthop(skb, NULL, *(int *)param); 6505 case SEG6_LOCAL_ACTION_END_B6: 6506 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb)) 6507 return -EBADMSG; 6508 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE, 6509 param, param_len); 6510 if (!err) 6511 bpf_update_srh_state(skb); 6512 6513 return err; 6514 case SEG6_LOCAL_ACTION_END_B6_ENCAP: 6515 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb)) 6516 return -EBADMSG; 6517 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6, 6518 param, param_len); 6519 if (!err) 6520 bpf_update_srh_state(skb); 6521 6522 return err; 6523 default: 6524 return -EINVAL; 6525 } 6526 } 6527 6528 static const struct bpf_func_proto bpf_lwt_seg6_action_proto = { 6529 .func = bpf_lwt_seg6_action, 6530 .gpl_only = false, 6531 .ret_type = RET_INTEGER, 6532 .arg1_type = ARG_PTR_TO_CTX, 6533 .arg2_type = ARG_ANYTHING, 6534 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY, 6535 .arg4_type = ARG_CONST_SIZE 6536 }; 6537 6538 BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset, 6539 s32, len) 6540 { 6541 struct seg6_bpf_srh_state *srh_state = 6542 this_cpu_ptr(&seg6_bpf_srh_states); 6543 struct ipv6_sr_hdr *srh = srh_state->srh; 6544 void *srh_end, *srh_tlvs, *ptr; 6545 struct ipv6hdr *hdr; 6546 int srhoff = 0; 6547 int ret; 6548 6549 if (unlikely(srh == NULL)) 6550 return -EINVAL; 6551 6552 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) + 6553 ((srh->first_segment + 1) << 4)); 6554 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) + 6555 srh_state->hdrlen); 6556 ptr = skb->data + offset; 6557 6558 if (unlikely(ptr < srh_tlvs || ptr > srh_end)) 6559 return -EFAULT; 6560 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end)) 6561 return -EFAULT; 6562 6563 if (len > 0) { 6564 ret = skb_cow_head(skb, len); 6565 if (unlikely(ret < 0)) 6566 return ret; 6567 6568 ret = bpf_skb_net_hdr_push(skb, offset, len); 6569 } else { 6570 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len); 6571 } 6572 6573 bpf_compute_data_pointers(skb); 6574 if (unlikely(ret < 0)) 6575 return ret; 6576 6577 hdr = (struct ipv6hdr *)skb->data; 6578 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); 6579 6580 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) 6581 return -EINVAL; 6582 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); 6583 srh_state->hdrlen += len; 6584 srh_state->valid = false; 6585 return 0; 6586 } 6587 6588 static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = { 6589 .func = bpf_lwt_seg6_adjust_srh, 6590 .gpl_only = false, 6591 .ret_type = RET_INTEGER, 6592 .arg1_type = ARG_PTR_TO_CTX, 6593 .arg2_type = ARG_ANYTHING, 6594 .arg3_type = ARG_ANYTHING, 6595 }; 6596 #endif /* CONFIG_IPV6_SEG6_BPF */ 6597 6598 #ifdef CONFIG_INET 6599 static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple, 6600 int dif, int sdif, u8 family, u8 proto) 6601 { 6602 struct inet_hashinfo *hinfo = net->ipv4.tcp_death_row.hashinfo; 6603 bool refcounted = false; 6604 struct sock *sk = NULL; 6605 6606 if (family == AF_INET) { 6607 __be32 src4 = tuple->ipv4.saddr; 6608 __be32 dst4 = tuple->ipv4.daddr; 6609 6610 if (proto == IPPROTO_TCP) 6611 sk = __inet_lookup(net, hinfo, NULL, 0, 6612 src4, tuple->ipv4.sport, 6613 dst4, tuple->ipv4.dport, 6614 dif, sdif, &refcounted); 6615 else 6616 sk = __udp4_lib_lookup(net, src4, tuple->ipv4.sport, 6617 dst4, tuple->ipv4.dport, 6618 dif, sdif, net->ipv4.udp_table, NULL); 6619 #if IS_ENABLED(CONFIG_IPV6) 6620 } else { 6621 struct in6_addr *src6 = (struct in6_addr *)&tuple->ipv6.saddr; 6622 struct in6_addr *dst6 = (struct in6_addr *)&tuple->ipv6.daddr; 6623 6624 if (proto == IPPROTO_TCP) 6625 sk = __inet6_lookup(net, hinfo, NULL, 0, 6626 src6, tuple->ipv6.sport, 6627 dst6, ntohs(tuple->ipv6.dport), 6628 dif, sdif, &refcounted); 6629 else if (likely(ipv6_bpf_stub)) 6630 sk = ipv6_bpf_stub->udp6_lib_lookup(net, 6631 src6, tuple->ipv6.sport, 6632 dst6, tuple->ipv6.dport, 6633 dif, sdif, 6634 net->ipv4.udp_table, NULL); 6635 #endif 6636 } 6637 6638 if (unlikely(sk && !refcounted && !sock_flag(sk, SOCK_RCU_FREE))) { 6639 WARN_ONCE(1, "Found non-RCU, unreferenced socket!"); 6640 sk = NULL; 6641 } 6642 return sk; 6643 } 6644 6645 /* bpf_skc_lookup performs the core lookup for different types of sockets, 6646 * taking a reference on the socket if it doesn't have the flag SOCK_RCU_FREE. 6647 */ 6648 static struct sock * 6649 __bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, 6650 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id, 6651 u64 flags, int sdif) 6652 { 6653 struct sock *sk = NULL; 6654 struct net *net; 6655 u8 family; 6656 6657 if (len == sizeof(tuple->ipv4)) 6658 family = AF_INET; 6659 else if (len == sizeof(tuple->ipv6)) 6660 family = AF_INET6; 6661 else 6662 return NULL; 6663 6664 if (unlikely(flags || !((s32)netns_id < 0 || netns_id <= S32_MAX))) 6665 goto out; 6666 6667 if (sdif < 0) { 6668 if (family == AF_INET) 6669 sdif = inet_sdif(skb); 6670 else 6671 sdif = inet6_sdif(skb); 6672 } 6673 6674 if ((s32)netns_id < 0) { 6675 net = caller_net; 6676 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto); 6677 } else { 6678 net = get_net_ns_by_id(caller_net, netns_id); 6679 if (unlikely(!net)) 6680 goto out; 6681 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto); 6682 put_net(net); 6683 } 6684 6685 out: 6686 return sk; 6687 } 6688 6689 static struct sock * 6690 __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, 6691 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id, 6692 u64 flags, int sdif) 6693 { 6694 struct sock *sk = __bpf_skc_lookup(skb, tuple, len, caller_net, 6695 ifindex, proto, netns_id, flags, 6696 sdif); 6697 6698 if (sk) { 6699 struct sock *sk2 = sk_to_full_sk(sk); 6700 6701 /* sk_to_full_sk() may return (sk)->rsk_listener, so make sure the original sk 6702 * sock refcnt is decremented to prevent a request_sock leak. 6703 */ 6704 if (!sk_fullsock(sk2)) 6705 sk2 = NULL; 6706 if (sk2 != sk) { 6707 sock_gen_put(sk); 6708 /* Ensure there is no need to bump sk2 refcnt */ 6709 if (unlikely(sk2 && !sock_flag(sk2, SOCK_RCU_FREE))) { 6710 WARN_ONCE(1, "Found non-RCU, unreferenced socket!"); 6711 return NULL; 6712 } 6713 sk = sk2; 6714 } 6715 } 6716 6717 return sk; 6718 } 6719 6720 static struct sock * 6721 bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, 6722 u8 proto, u64 netns_id, u64 flags) 6723 { 6724 struct net *caller_net; 6725 int ifindex; 6726 6727 if (skb->dev) { 6728 caller_net = dev_net(skb->dev); 6729 ifindex = skb->dev->ifindex; 6730 } else { 6731 caller_net = sock_net(skb->sk); 6732 ifindex = 0; 6733 } 6734 6735 return __bpf_skc_lookup(skb, tuple, len, caller_net, ifindex, proto, 6736 netns_id, flags, -1); 6737 } 6738 6739 static struct sock * 6740 bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, 6741 u8 proto, u64 netns_id, u64 flags) 6742 { 6743 struct sock *sk = bpf_skc_lookup(skb, tuple, len, proto, netns_id, 6744 flags); 6745 6746 if (sk) { 6747 struct sock *sk2 = sk_to_full_sk(sk); 6748 6749 /* sk_to_full_sk() may return (sk)->rsk_listener, so make sure the original sk 6750 * sock refcnt is decremented to prevent a request_sock leak. 6751 */ 6752 if (!sk_fullsock(sk2)) 6753 sk2 = NULL; 6754 if (sk2 != sk) { 6755 sock_gen_put(sk); 6756 /* Ensure there is no need to bump sk2 refcnt */ 6757 if (unlikely(sk2 && !sock_flag(sk2, SOCK_RCU_FREE))) { 6758 WARN_ONCE(1, "Found non-RCU, unreferenced socket!"); 6759 return NULL; 6760 } 6761 sk = sk2; 6762 } 6763 } 6764 6765 return sk; 6766 } 6767 6768 BPF_CALL_5(bpf_skc_lookup_tcp, struct sk_buff *, skb, 6769 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) 6770 { 6771 return (unsigned long)bpf_skc_lookup(skb, tuple, len, IPPROTO_TCP, 6772 netns_id, flags); 6773 } 6774 6775 static const struct bpf_func_proto bpf_skc_lookup_tcp_proto = { 6776 .func = bpf_skc_lookup_tcp, 6777 .gpl_only = false, 6778 .pkt_access = true, 6779 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL, 6780 .arg1_type = ARG_PTR_TO_CTX, 6781 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, 6782 .arg3_type = ARG_CONST_SIZE, 6783 .arg4_type = ARG_ANYTHING, 6784 .arg5_type = ARG_ANYTHING, 6785 }; 6786 6787 BPF_CALL_5(bpf_sk_lookup_tcp, struct sk_buff *, skb, 6788 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) 6789 { 6790 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_TCP, 6791 netns_id, flags); 6792 } 6793 6794 static const struct bpf_func_proto bpf_sk_lookup_tcp_proto = { 6795 .func = bpf_sk_lookup_tcp, 6796 .gpl_only = false, 6797 .pkt_access = true, 6798 .ret_type = RET_PTR_TO_SOCKET_OR_NULL, 6799 .arg1_type = ARG_PTR_TO_CTX, 6800 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, 6801 .arg3_type = ARG_CONST_SIZE, 6802 .arg4_type = ARG_ANYTHING, 6803 .arg5_type = ARG_ANYTHING, 6804 }; 6805 6806 BPF_CALL_5(bpf_sk_lookup_udp, struct sk_buff *, skb, 6807 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) 6808 { 6809 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_UDP, 6810 netns_id, flags); 6811 } 6812 6813 static const struct bpf_func_proto bpf_sk_lookup_udp_proto = { 6814 .func = bpf_sk_lookup_udp, 6815 .gpl_only = false, 6816 .pkt_access = true, 6817 .ret_type = RET_PTR_TO_SOCKET_OR_NULL, 6818 .arg1_type = ARG_PTR_TO_CTX, 6819 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, 6820 .arg3_type = ARG_CONST_SIZE, 6821 .arg4_type = ARG_ANYTHING, 6822 .arg5_type = ARG_ANYTHING, 6823 }; 6824 6825 BPF_CALL_5(bpf_tc_skc_lookup_tcp, struct sk_buff *, skb, 6826 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) 6827 { 6828 struct net_device *dev = skb->dev; 6829 int ifindex = dev->ifindex, sdif = dev_sdif(dev); 6830 struct net *caller_net = dev_net(dev); 6831 6832 return (unsigned long)__bpf_skc_lookup(skb, tuple, len, caller_net, 6833 ifindex, IPPROTO_TCP, netns_id, 6834 flags, sdif); 6835 } 6836 6837 static const struct bpf_func_proto bpf_tc_skc_lookup_tcp_proto = { 6838 .func = bpf_tc_skc_lookup_tcp, 6839 .gpl_only = false, 6840 .pkt_access = true, 6841 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL, 6842 .arg1_type = ARG_PTR_TO_CTX, 6843 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, 6844 .arg3_type = ARG_CONST_SIZE, 6845 .arg4_type = ARG_ANYTHING, 6846 .arg5_type = ARG_ANYTHING, 6847 }; 6848 6849 BPF_CALL_5(bpf_tc_sk_lookup_tcp, struct sk_buff *, skb, 6850 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) 6851 { 6852 struct net_device *dev = skb->dev; 6853 int ifindex = dev->ifindex, sdif = dev_sdif(dev); 6854 struct net *caller_net = dev_net(dev); 6855 6856 return (unsigned long)__bpf_sk_lookup(skb, tuple, len, caller_net, 6857 ifindex, IPPROTO_TCP, netns_id, 6858 flags, sdif); 6859 } 6860 6861 static const struct bpf_func_proto bpf_tc_sk_lookup_tcp_proto = { 6862 .func = bpf_tc_sk_lookup_tcp, 6863 .gpl_only = false, 6864 .pkt_access = true, 6865 .ret_type = RET_PTR_TO_SOCKET_OR_NULL, 6866 .arg1_type = ARG_PTR_TO_CTX, 6867 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, 6868 .arg3_type = ARG_CONST_SIZE, 6869 .arg4_type = ARG_ANYTHING, 6870 .arg5_type = ARG_ANYTHING, 6871 }; 6872 6873 BPF_CALL_5(bpf_tc_sk_lookup_udp, struct sk_buff *, skb, 6874 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) 6875 { 6876 struct net_device *dev = skb->dev; 6877 int ifindex = dev->ifindex, sdif = dev_sdif(dev); 6878 struct net *caller_net = dev_net(dev); 6879 6880 return (unsigned long)__bpf_sk_lookup(skb, tuple, len, caller_net, 6881 ifindex, IPPROTO_UDP, netns_id, 6882 flags, sdif); 6883 } 6884 6885 static const struct bpf_func_proto bpf_tc_sk_lookup_udp_proto = { 6886 .func = bpf_tc_sk_lookup_udp, 6887 .gpl_only = false, 6888 .pkt_access = true, 6889 .ret_type = RET_PTR_TO_SOCKET_OR_NULL, 6890 .arg1_type = ARG_PTR_TO_CTX, 6891 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, 6892 .arg3_type = ARG_CONST_SIZE, 6893 .arg4_type = ARG_ANYTHING, 6894 .arg5_type = ARG_ANYTHING, 6895 }; 6896 6897 BPF_CALL_1(bpf_sk_release, struct sock *, sk) 6898 { 6899 if (sk && sk_is_refcounted(sk)) 6900 sock_gen_put(sk); 6901 return 0; 6902 } 6903 6904 static const struct bpf_func_proto bpf_sk_release_proto = { 6905 .func = bpf_sk_release, 6906 .gpl_only = false, 6907 .ret_type = RET_INTEGER, 6908 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON | OBJ_RELEASE, 6909 }; 6910 6911 BPF_CALL_5(bpf_xdp_sk_lookup_udp, struct xdp_buff *, ctx, 6912 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags) 6913 { 6914 struct net_device *dev = ctx->rxq->dev; 6915 int ifindex = dev->ifindex, sdif = dev_sdif(dev); 6916 struct net *caller_net = dev_net(dev); 6917 6918 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net, 6919 ifindex, IPPROTO_UDP, netns_id, 6920 flags, sdif); 6921 } 6922 6923 static const struct bpf_func_proto bpf_xdp_sk_lookup_udp_proto = { 6924 .func = bpf_xdp_sk_lookup_udp, 6925 .gpl_only = false, 6926 .pkt_access = true, 6927 .ret_type = RET_PTR_TO_SOCKET_OR_NULL, 6928 .arg1_type = ARG_PTR_TO_CTX, 6929 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, 6930 .arg3_type = ARG_CONST_SIZE, 6931 .arg4_type = ARG_ANYTHING, 6932 .arg5_type = ARG_ANYTHING, 6933 }; 6934 6935 BPF_CALL_5(bpf_xdp_skc_lookup_tcp, struct xdp_buff *, ctx, 6936 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags) 6937 { 6938 struct net_device *dev = ctx->rxq->dev; 6939 int ifindex = dev->ifindex, sdif = dev_sdif(dev); 6940 struct net *caller_net = dev_net(dev); 6941 6942 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len, caller_net, 6943 ifindex, IPPROTO_TCP, netns_id, 6944 flags, sdif); 6945 } 6946 6947 static const struct bpf_func_proto bpf_xdp_skc_lookup_tcp_proto = { 6948 .func = bpf_xdp_skc_lookup_tcp, 6949 .gpl_only = false, 6950 .pkt_access = true, 6951 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL, 6952 .arg1_type = ARG_PTR_TO_CTX, 6953 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, 6954 .arg3_type = ARG_CONST_SIZE, 6955 .arg4_type = ARG_ANYTHING, 6956 .arg5_type = ARG_ANYTHING, 6957 }; 6958 6959 BPF_CALL_5(bpf_xdp_sk_lookup_tcp, struct xdp_buff *, ctx, 6960 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags) 6961 { 6962 struct net_device *dev = ctx->rxq->dev; 6963 int ifindex = dev->ifindex, sdif = dev_sdif(dev); 6964 struct net *caller_net = dev_net(dev); 6965 6966 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net, 6967 ifindex, IPPROTO_TCP, netns_id, 6968 flags, sdif); 6969 } 6970 6971 static const struct bpf_func_proto bpf_xdp_sk_lookup_tcp_proto = { 6972 .func = bpf_xdp_sk_lookup_tcp, 6973 .gpl_only = false, 6974 .pkt_access = true, 6975 .ret_type = RET_PTR_TO_SOCKET_OR_NULL, 6976 .arg1_type = ARG_PTR_TO_CTX, 6977 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, 6978 .arg3_type = ARG_CONST_SIZE, 6979 .arg4_type = ARG_ANYTHING, 6980 .arg5_type = ARG_ANYTHING, 6981 }; 6982 6983 BPF_CALL_5(bpf_sock_addr_skc_lookup_tcp, struct bpf_sock_addr_kern *, ctx, 6984 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) 6985 { 6986 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len, 6987 sock_net(ctx->sk), 0, 6988 IPPROTO_TCP, netns_id, flags, 6989 -1); 6990 } 6991 6992 static const struct bpf_func_proto bpf_sock_addr_skc_lookup_tcp_proto = { 6993 .func = bpf_sock_addr_skc_lookup_tcp, 6994 .gpl_only = false, 6995 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL, 6996 .arg1_type = ARG_PTR_TO_CTX, 6997 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, 6998 .arg3_type = ARG_CONST_SIZE, 6999 .arg4_type = ARG_ANYTHING, 7000 .arg5_type = ARG_ANYTHING, 7001 }; 7002 7003 BPF_CALL_5(bpf_sock_addr_sk_lookup_tcp, struct bpf_sock_addr_kern *, ctx, 7004 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) 7005 { 7006 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, 7007 sock_net(ctx->sk), 0, IPPROTO_TCP, 7008 netns_id, flags, -1); 7009 } 7010 7011 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_tcp_proto = { 7012 .func = bpf_sock_addr_sk_lookup_tcp, 7013 .gpl_only = false, 7014 .ret_type = RET_PTR_TO_SOCKET_OR_NULL, 7015 .arg1_type = ARG_PTR_TO_CTX, 7016 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, 7017 .arg3_type = ARG_CONST_SIZE, 7018 .arg4_type = ARG_ANYTHING, 7019 .arg5_type = ARG_ANYTHING, 7020 }; 7021 7022 BPF_CALL_5(bpf_sock_addr_sk_lookup_udp, struct bpf_sock_addr_kern *, ctx, 7023 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) 7024 { 7025 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, 7026 sock_net(ctx->sk), 0, IPPROTO_UDP, 7027 netns_id, flags, -1); 7028 } 7029 7030 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_udp_proto = { 7031 .func = bpf_sock_addr_sk_lookup_udp, 7032 .gpl_only = false, 7033 .ret_type = RET_PTR_TO_SOCKET_OR_NULL, 7034 .arg1_type = ARG_PTR_TO_CTX, 7035 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, 7036 .arg3_type = ARG_CONST_SIZE, 7037 .arg4_type = ARG_ANYTHING, 7038 .arg5_type = ARG_ANYTHING, 7039 }; 7040 7041 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type, 7042 struct bpf_insn_access_aux *info) 7043 { 7044 if (off < 0 || off >= offsetofend(struct bpf_tcp_sock, 7045 icsk_retransmits)) 7046 return false; 7047 7048 if (off % size != 0) 7049 return false; 7050 7051 switch (off) { 7052 case offsetof(struct bpf_tcp_sock, bytes_received): 7053 case offsetof(struct bpf_tcp_sock, bytes_acked): 7054 return size == sizeof(__u64); 7055 default: 7056 return size == sizeof(__u32); 7057 } 7058 } 7059 7060 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type, 7061 const struct bpf_insn *si, 7062 struct bpf_insn *insn_buf, 7063 struct bpf_prog *prog, u32 *target_size) 7064 { 7065 struct bpf_insn *insn = insn_buf; 7066 7067 #define BPF_TCP_SOCK_GET_COMMON(FIELD) \ 7068 do { \ 7069 BUILD_BUG_ON(sizeof_field(struct tcp_sock, FIELD) > \ 7070 sizeof_field(struct bpf_tcp_sock, FIELD)); \ 7071 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_sock, FIELD),\ 7072 si->dst_reg, si->src_reg, \ 7073 offsetof(struct tcp_sock, FIELD)); \ 7074 } while (0) 7075 7076 #define BPF_INET_SOCK_GET_COMMON(FIELD) \ 7077 do { \ 7078 BUILD_BUG_ON(sizeof_field(struct inet_connection_sock, \ 7079 FIELD) > \ 7080 sizeof_field(struct bpf_tcp_sock, FIELD)); \ 7081 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ 7082 struct inet_connection_sock, \ 7083 FIELD), \ 7084 si->dst_reg, si->src_reg, \ 7085 offsetof( \ 7086 struct inet_connection_sock, \ 7087 FIELD)); \ 7088 } while (0) 7089 7090 BTF_TYPE_EMIT(struct bpf_tcp_sock); 7091 7092 switch (si->off) { 7093 case offsetof(struct bpf_tcp_sock, rtt_min): 7094 BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) != 7095 sizeof(struct minmax)); 7096 BUILD_BUG_ON(sizeof(struct minmax) < 7097 sizeof(struct minmax_sample)); 7098 7099 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 7100 offsetof(struct tcp_sock, rtt_min) + 7101 offsetof(struct minmax_sample, v)); 7102 break; 7103 case offsetof(struct bpf_tcp_sock, snd_cwnd): 7104 BPF_TCP_SOCK_GET_COMMON(snd_cwnd); 7105 break; 7106 case offsetof(struct bpf_tcp_sock, srtt_us): 7107 BPF_TCP_SOCK_GET_COMMON(srtt_us); 7108 break; 7109 case offsetof(struct bpf_tcp_sock, snd_ssthresh): 7110 BPF_TCP_SOCK_GET_COMMON(snd_ssthresh); 7111 break; 7112 case offsetof(struct bpf_tcp_sock, rcv_nxt): 7113 BPF_TCP_SOCK_GET_COMMON(rcv_nxt); 7114 break; 7115 case offsetof(struct bpf_tcp_sock, snd_nxt): 7116 BPF_TCP_SOCK_GET_COMMON(snd_nxt); 7117 break; 7118 case offsetof(struct bpf_tcp_sock, snd_una): 7119 BPF_TCP_SOCK_GET_COMMON(snd_una); 7120 break; 7121 case offsetof(struct bpf_tcp_sock, mss_cache): 7122 BPF_TCP_SOCK_GET_COMMON(mss_cache); 7123 break; 7124 case offsetof(struct bpf_tcp_sock, ecn_flags): 7125 BPF_TCP_SOCK_GET_COMMON(ecn_flags); 7126 break; 7127 case offsetof(struct bpf_tcp_sock, rate_delivered): 7128 BPF_TCP_SOCK_GET_COMMON(rate_delivered); 7129 break; 7130 case offsetof(struct bpf_tcp_sock, rate_interval_us): 7131 BPF_TCP_SOCK_GET_COMMON(rate_interval_us); 7132 break; 7133 case offsetof(struct bpf_tcp_sock, packets_out): 7134 BPF_TCP_SOCK_GET_COMMON(packets_out); 7135 break; 7136 case offsetof(struct bpf_tcp_sock, retrans_out): 7137 BPF_TCP_SOCK_GET_COMMON(retrans_out); 7138 break; 7139 case offsetof(struct bpf_tcp_sock, total_retrans): 7140 BPF_TCP_SOCK_GET_COMMON(total_retrans); 7141 break; 7142 case offsetof(struct bpf_tcp_sock, segs_in): 7143 BPF_TCP_SOCK_GET_COMMON(segs_in); 7144 break; 7145 case offsetof(struct bpf_tcp_sock, data_segs_in): 7146 BPF_TCP_SOCK_GET_COMMON(data_segs_in); 7147 break; 7148 case offsetof(struct bpf_tcp_sock, segs_out): 7149 BPF_TCP_SOCK_GET_COMMON(segs_out); 7150 break; 7151 case offsetof(struct bpf_tcp_sock, data_segs_out): 7152 BPF_TCP_SOCK_GET_COMMON(data_segs_out); 7153 break; 7154 case offsetof(struct bpf_tcp_sock, lost_out): 7155 BPF_TCP_SOCK_GET_COMMON(lost_out); 7156 break; 7157 case offsetof(struct bpf_tcp_sock, sacked_out): 7158 BPF_TCP_SOCK_GET_COMMON(sacked_out); 7159 break; 7160 case offsetof(struct bpf_tcp_sock, bytes_received): 7161 BPF_TCP_SOCK_GET_COMMON(bytes_received); 7162 break; 7163 case offsetof(struct bpf_tcp_sock, bytes_acked): 7164 BPF_TCP_SOCK_GET_COMMON(bytes_acked); 7165 break; 7166 case offsetof(struct bpf_tcp_sock, dsack_dups): 7167 BPF_TCP_SOCK_GET_COMMON(dsack_dups); 7168 break; 7169 case offsetof(struct bpf_tcp_sock, delivered): 7170 BPF_TCP_SOCK_GET_COMMON(delivered); 7171 break; 7172 case offsetof(struct bpf_tcp_sock, delivered_ce): 7173 BPF_TCP_SOCK_GET_COMMON(delivered_ce); 7174 break; 7175 case offsetof(struct bpf_tcp_sock, icsk_retransmits): 7176 BPF_INET_SOCK_GET_COMMON(icsk_retransmits); 7177 break; 7178 } 7179 7180 return insn - insn_buf; 7181 } 7182 7183 BPF_CALL_1(bpf_tcp_sock, struct sock *, sk) 7184 { 7185 if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP) 7186 return (unsigned long)sk; 7187 7188 return (unsigned long)NULL; 7189 } 7190 7191 const struct bpf_func_proto bpf_tcp_sock_proto = { 7192 .func = bpf_tcp_sock, 7193 .gpl_only = false, 7194 .ret_type = RET_PTR_TO_TCP_SOCK_OR_NULL, 7195 .arg1_type = ARG_PTR_TO_SOCK_COMMON, 7196 }; 7197 7198 BPF_CALL_1(bpf_get_listener_sock, struct sock *, sk) 7199 { 7200 sk = sk_to_full_sk(sk); 7201 7202 if (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_RCU_FREE)) 7203 return (unsigned long)sk; 7204 7205 return (unsigned long)NULL; 7206 } 7207 7208 static const struct bpf_func_proto bpf_get_listener_sock_proto = { 7209 .func = bpf_get_listener_sock, 7210 .gpl_only = false, 7211 .ret_type = RET_PTR_TO_SOCKET_OR_NULL, 7212 .arg1_type = ARG_PTR_TO_SOCK_COMMON, 7213 }; 7214 7215 BPF_CALL_1(bpf_skb_ecn_set_ce, struct sk_buff *, skb) 7216 { 7217 unsigned int iphdr_len; 7218 7219 switch (skb_protocol(skb, true)) { 7220 case cpu_to_be16(ETH_P_IP): 7221 iphdr_len = sizeof(struct iphdr); 7222 break; 7223 case cpu_to_be16(ETH_P_IPV6): 7224 iphdr_len = sizeof(struct ipv6hdr); 7225 break; 7226 default: 7227 return 0; 7228 } 7229 7230 if (skb_headlen(skb) < iphdr_len) 7231 return 0; 7232 7233 if (skb_cloned(skb) && !skb_clone_writable(skb, iphdr_len)) 7234 return 0; 7235 7236 return INET_ECN_set_ce(skb); 7237 } 7238 7239 bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type, 7240 struct bpf_insn_access_aux *info) 7241 { 7242 if (off < 0 || off >= offsetofend(struct bpf_xdp_sock, queue_id)) 7243 return false; 7244 7245 if (off % size != 0) 7246 return false; 7247 7248 switch (off) { 7249 default: 7250 return size == sizeof(__u32); 7251 } 7252 } 7253 7254 u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type, 7255 const struct bpf_insn *si, 7256 struct bpf_insn *insn_buf, 7257 struct bpf_prog *prog, u32 *target_size) 7258 { 7259 struct bpf_insn *insn = insn_buf; 7260 7261 #define BPF_XDP_SOCK_GET(FIELD) \ 7262 do { \ 7263 BUILD_BUG_ON(sizeof_field(struct xdp_sock, FIELD) > \ 7264 sizeof_field(struct bpf_xdp_sock, FIELD)); \ 7265 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_sock, FIELD),\ 7266 si->dst_reg, si->src_reg, \ 7267 offsetof(struct xdp_sock, FIELD)); \ 7268 } while (0) 7269 7270 switch (si->off) { 7271 case offsetof(struct bpf_xdp_sock, queue_id): 7272 BPF_XDP_SOCK_GET(queue_id); 7273 break; 7274 } 7275 7276 return insn - insn_buf; 7277 } 7278 7279 static const struct bpf_func_proto bpf_skb_ecn_set_ce_proto = { 7280 .func = bpf_skb_ecn_set_ce, 7281 .gpl_only = false, 7282 .ret_type = RET_INTEGER, 7283 .arg1_type = ARG_PTR_TO_CTX, 7284 }; 7285 7286 BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_len, 7287 struct tcphdr *, th, u32, th_len) 7288 { 7289 #ifdef CONFIG_SYN_COOKIES 7290 u32 cookie; 7291 int ret; 7292 7293 if (unlikely(!sk || th_len < sizeof(*th))) 7294 return -EINVAL; 7295 7296 /* sk_listener() allows TCP_NEW_SYN_RECV, which makes no sense here. */ 7297 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN) 7298 return -EINVAL; 7299 7300 if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies)) 7301 return -EINVAL; 7302 7303 if (!th->ack || th->rst || th->syn) 7304 return -ENOENT; 7305 7306 if (unlikely(iph_len < sizeof(struct iphdr))) 7307 return -EINVAL; 7308 7309 if (tcp_synq_no_recent_overflow(sk)) 7310 return -ENOENT; 7311 7312 cookie = ntohl(th->ack_seq) - 1; 7313 7314 /* Both struct iphdr and struct ipv6hdr have the version field at the 7315 * same offset so we can cast to the shorter header (struct iphdr). 7316 */ 7317 switch (((struct iphdr *)iph)->version) { 7318 case 4: 7319 if (sk->sk_family == AF_INET6 && ipv6_only_sock(sk)) 7320 return -EINVAL; 7321 7322 ret = __cookie_v4_check((struct iphdr *)iph, th, cookie); 7323 break; 7324 7325 #if IS_BUILTIN(CONFIG_IPV6) 7326 case 6: 7327 if (unlikely(iph_len < sizeof(struct ipv6hdr))) 7328 return -EINVAL; 7329 7330 if (sk->sk_family != AF_INET6) 7331 return -EINVAL; 7332 7333 ret = __cookie_v6_check((struct ipv6hdr *)iph, th, cookie); 7334 break; 7335 #endif /* CONFIG_IPV6 */ 7336 7337 default: 7338 return -EPROTONOSUPPORT; 7339 } 7340 7341 if (ret > 0) 7342 return 0; 7343 7344 return -ENOENT; 7345 #else 7346 return -ENOTSUPP; 7347 #endif 7348 } 7349 7350 static const struct bpf_func_proto bpf_tcp_check_syncookie_proto = { 7351 .func = bpf_tcp_check_syncookie, 7352 .gpl_only = true, 7353 .pkt_access = true, 7354 .ret_type = RET_INTEGER, 7355 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, 7356 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, 7357 .arg3_type = ARG_CONST_SIZE, 7358 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY, 7359 .arg5_type = ARG_CONST_SIZE, 7360 }; 7361 7362 BPF_CALL_5(bpf_tcp_gen_syncookie, struct sock *, sk, void *, iph, u32, iph_len, 7363 struct tcphdr *, th, u32, th_len) 7364 { 7365 #ifdef CONFIG_SYN_COOKIES 7366 u32 cookie; 7367 u16 mss; 7368 7369 if (unlikely(!sk || th_len < sizeof(*th) || th_len != th->doff * 4)) 7370 return -EINVAL; 7371 7372 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN) 7373 return -EINVAL; 7374 7375 if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies)) 7376 return -ENOENT; 7377 7378 if (!th->syn || th->ack || th->fin || th->rst) 7379 return -EINVAL; 7380 7381 if (unlikely(iph_len < sizeof(struct iphdr))) 7382 return -EINVAL; 7383 7384 /* Both struct iphdr and struct ipv6hdr have the version field at the 7385 * same offset so we can cast to the shorter header (struct iphdr). 7386 */ 7387 switch (((struct iphdr *)iph)->version) { 7388 case 4: 7389 if (sk->sk_family == AF_INET6 && ipv6_only_sock(sk)) 7390 return -EINVAL; 7391 7392 mss = tcp_v4_get_syncookie(sk, iph, th, &cookie); 7393 break; 7394 7395 #if IS_BUILTIN(CONFIG_IPV6) 7396 case 6: 7397 if (unlikely(iph_len < sizeof(struct ipv6hdr))) 7398 return -EINVAL; 7399 7400 if (sk->sk_family != AF_INET6) 7401 return -EINVAL; 7402 7403 mss = tcp_v6_get_syncookie(sk, iph, th, &cookie); 7404 break; 7405 #endif /* CONFIG_IPV6 */ 7406 7407 default: 7408 return -EPROTONOSUPPORT; 7409 } 7410 if (mss == 0) 7411 return -ENOENT; 7412 7413 return cookie | ((u64)mss << 32); 7414 #else 7415 return -EOPNOTSUPP; 7416 #endif /* CONFIG_SYN_COOKIES */ 7417 } 7418 7419 static const struct bpf_func_proto bpf_tcp_gen_syncookie_proto = { 7420 .func = bpf_tcp_gen_syncookie, 7421 .gpl_only = true, /* __cookie_v*_init_sequence() is GPL */ 7422 .pkt_access = true, 7423 .ret_type = RET_INTEGER, 7424 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, 7425 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, 7426 .arg3_type = ARG_CONST_SIZE, 7427 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY, 7428 .arg5_type = ARG_CONST_SIZE, 7429 }; 7430 7431 BPF_CALL_3(bpf_sk_assign, struct sk_buff *, skb, struct sock *, sk, u64, flags) 7432 { 7433 if (!sk || flags != 0) 7434 return -EINVAL; 7435 if (!skb_at_tc_ingress(skb)) 7436 return -EOPNOTSUPP; 7437 if (unlikely(dev_net(skb->dev) != sock_net(sk))) 7438 return -ENETUNREACH; 7439 if (sk_unhashed(sk)) 7440 return -EOPNOTSUPP; 7441 if (sk_is_refcounted(sk) && 7442 unlikely(!refcount_inc_not_zero(&sk->sk_refcnt))) 7443 return -ENOENT; 7444 7445 skb_orphan(skb); 7446 skb->sk = sk; 7447 skb->destructor = sock_pfree; 7448 7449 return 0; 7450 } 7451 7452 static const struct bpf_func_proto bpf_sk_assign_proto = { 7453 .func = bpf_sk_assign, 7454 .gpl_only = false, 7455 .ret_type = RET_INTEGER, 7456 .arg1_type = ARG_PTR_TO_CTX, 7457 .arg2_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, 7458 .arg3_type = ARG_ANYTHING, 7459 }; 7460 7461 static const u8 *bpf_search_tcp_opt(const u8 *op, const u8 *opend, 7462 u8 search_kind, const u8 *magic, 7463 u8 magic_len, bool *eol) 7464 { 7465 u8 kind, kind_len; 7466 7467 *eol = false; 7468 7469 while (op < opend) { 7470 kind = op[0]; 7471 7472 if (kind == TCPOPT_EOL) { 7473 *eol = true; 7474 return ERR_PTR(-ENOMSG); 7475 } else if (kind == TCPOPT_NOP) { 7476 op++; 7477 continue; 7478 } 7479 7480 if (opend - op < 2 || opend - op < op[1] || op[1] < 2) 7481 /* Something is wrong in the received header. 7482 * Follow the TCP stack's tcp_parse_options() 7483 * and just bail here. 7484 */ 7485 return ERR_PTR(-EFAULT); 7486 7487 kind_len = op[1]; 7488 if (search_kind == kind) { 7489 if (!magic_len) 7490 return op; 7491 7492 if (magic_len > kind_len - 2) 7493 return ERR_PTR(-ENOMSG); 7494 7495 if (!memcmp(&op[2], magic, magic_len)) 7496 return op; 7497 } 7498 7499 op += kind_len; 7500 } 7501 7502 return ERR_PTR(-ENOMSG); 7503 } 7504 7505 BPF_CALL_4(bpf_sock_ops_load_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock, 7506 void *, search_res, u32, len, u64, flags) 7507 { 7508 bool eol, load_syn = flags & BPF_LOAD_HDR_OPT_TCP_SYN; 7509 const u8 *op, *opend, *magic, *search = search_res; 7510 u8 search_kind, search_len, copy_len, magic_len; 7511 int ret; 7512 7513 /* 2 byte is the minimal option len except TCPOPT_NOP and 7514 * TCPOPT_EOL which are useless for the bpf prog to learn 7515 * and this helper disallow loading them also. 7516 */ 7517 if (len < 2 || flags & ~BPF_LOAD_HDR_OPT_TCP_SYN) 7518 return -EINVAL; 7519 7520 search_kind = search[0]; 7521 search_len = search[1]; 7522 7523 if (search_len > len || search_kind == TCPOPT_NOP || 7524 search_kind == TCPOPT_EOL) 7525 return -EINVAL; 7526 7527 if (search_kind == TCPOPT_EXP || search_kind == 253) { 7528 /* 16 or 32 bit magic. +2 for kind and kind length */ 7529 if (search_len != 4 && search_len != 6) 7530 return -EINVAL; 7531 magic = &search[2]; 7532 magic_len = search_len - 2; 7533 } else { 7534 if (search_len) 7535 return -EINVAL; 7536 magic = NULL; 7537 magic_len = 0; 7538 } 7539 7540 if (load_syn) { 7541 ret = bpf_sock_ops_get_syn(bpf_sock, TCP_BPF_SYN, &op); 7542 if (ret < 0) 7543 return ret; 7544 7545 opend = op + ret; 7546 op += sizeof(struct tcphdr); 7547 } else { 7548 if (!bpf_sock->skb || 7549 bpf_sock->op == BPF_SOCK_OPS_HDR_OPT_LEN_CB) 7550 /* This bpf_sock->op cannot call this helper */ 7551 return -EPERM; 7552 7553 opend = bpf_sock->skb_data_end; 7554 op = bpf_sock->skb->data + sizeof(struct tcphdr); 7555 } 7556 7557 op = bpf_search_tcp_opt(op, opend, search_kind, magic, magic_len, 7558 &eol); 7559 if (IS_ERR(op)) 7560 return PTR_ERR(op); 7561 7562 copy_len = op[1]; 7563 ret = copy_len; 7564 if (copy_len > len) { 7565 ret = -ENOSPC; 7566 copy_len = len; 7567 } 7568 7569 memcpy(search_res, op, copy_len); 7570 return ret; 7571 } 7572 7573 static const struct bpf_func_proto bpf_sock_ops_load_hdr_opt_proto = { 7574 .func = bpf_sock_ops_load_hdr_opt, 7575 .gpl_only = false, 7576 .ret_type = RET_INTEGER, 7577 .arg1_type = ARG_PTR_TO_CTX, 7578 .arg2_type = ARG_PTR_TO_MEM, 7579 .arg3_type = ARG_CONST_SIZE, 7580 .arg4_type = ARG_ANYTHING, 7581 }; 7582 7583 BPF_CALL_4(bpf_sock_ops_store_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock, 7584 const void *, from, u32, len, u64, flags) 7585 { 7586 u8 new_kind, new_kind_len, magic_len = 0, *opend; 7587 const u8 *op, *new_op, *magic = NULL; 7588 struct sk_buff *skb; 7589 bool eol; 7590 7591 if (bpf_sock->op != BPF_SOCK_OPS_WRITE_HDR_OPT_CB) 7592 return -EPERM; 7593 7594 if (len < 2 || flags) 7595 return -EINVAL; 7596 7597 new_op = from; 7598 new_kind = new_op[0]; 7599 new_kind_len = new_op[1]; 7600 7601 if (new_kind_len > len || new_kind == TCPOPT_NOP || 7602 new_kind == TCPOPT_EOL) 7603 return -EINVAL; 7604 7605 if (new_kind_len > bpf_sock->remaining_opt_len) 7606 return -ENOSPC; 7607 7608 /* 253 is another experimental kind */ 7609 if (new_kind == TCPOPT_EXP || new_kind == 253) { 7610 if (new_kind_len < 4) 7611 return -EINVAL; 7612 /* Match for the 2 byte magic also. 7613 * RFC 6994: the magic could be 2 or 4 bytes. 7614 * Hence, matching by 2 byte only is on the 7615 * conservative side but it is the right 7616 * thing to do for the 'search-for-duplication' 7617 * purpose. 7618 */ 7619 magic = &new_op[2]; 7620 magic_len = 2; 7621 } 7622 7623 /* Check for duplication */ 7624 skb = bpf_sock->skb; 7625 op = skb->data + sizeof(struct tcphdr); 7626 opend = bpf_sock->skb_data_end; 7627 7628 op = bpf_search_tcp_opt(op, opend, new_kind, magic, magic_len, 7629 &eol); 7630 if (!IS_ERR(op)) 7631 return -EEXIST; 7632 7633 if (PTR_ERR(op) != -ENOMSG) 7634 return PTR_ERR(op); 7635 7636 if (eol) 7637 /* The option has been ended. Treat it as no more 7638 * header option can be written. 7639 */ 7640 return -ENOSPC; 7641 7642 /* No duplication found. Store the header option. */ 7643 memcpy(opend, from, new_kind_len); 7644 7645 bpf_sock->remaining_opt_len -= new_kind_len; 7646 bpf_sock->skb_data_end += new_kind_len; 7647 7648 return 0; 7649 } 7650 7651 static const struct bpf_func_proto bpf_sock_ops_store_hdr_opt_proto = { 7652 .func = bpf_sock_ops_store_hdr_opt, 7653 .gpl_only = false, 7654 .ret_type = RET_INTEGER, 7655 .arg1_type = ARG_PTR_TO_CTX, 7656 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, 7657 .arg3_type = ARG_CONST_SIZE, 7658 .arg4_type = ARG_ANYTHING, 7659 }; 7660 7661 BPF_CALL_3(bpf_sock_ops_reserve_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock, 7662 u32, len, u64, flags) 7663 { 7664 if (bpf_sock->op != BPF_SOCK_OPS_HDR_OPT_LEN_CB) 7665 return -EPERM; 7666 7667 if (flags || len < 2) 7668 return -EINVAL; 7669 7670 if (len > bpf_sock->remaining_opt_len) 7671 return -ENOSPC; 7672 7673 bpf_sock->remaining_opt_len -= len; 7674 7675 return 0; 7676 } 7677 7678 static const struct bpf_func_proto bpf_sock_ops_reserve_hdr_opt_proto = { 7679 .func = bpf_sock_ops_reserve_hdr_opt, 7680 .gpl_only = false, 7681 .ret_type = RET_INTEGER, 7682 .arg1_type = ARG_PTR_TO_CTX, 7683 .arg2_type = ARG_ANYTHING, 7684 .arg3_type = ARG_ANYTHING, 7685 }; 7686 7687 BPF_CALL_3(bpf_skb_set_tstamp, struct sk_buff *, skb, 7688 u64, tstamp, u32, tstamp_type) 7689 { 7690 /* skb_clear_delivery_time() is done for inet protocol */ 7691 if (skb->protocol != htons(ETH_P_IP) && 7692 skb->protocol != htons(ETH_P_IPV6)) 7693 return -EOPNOTSUPP; 7694 7695 switch (tstamp_type) { 7696 case BPF_SKB_TSTAMP_DELIVERY_MONO: 7697 if (!tstamp) 7698 return -EINVAL; 7699 skb->tstamp = tstamp; 7700 skb->mono_delivery_time = 1; 7701 break; 7702 case BPF_SKB_TSTAMP_UNSPEC: 7703 if (tstamp) 7704 return -EINVAL; 7705 skb->tstamp = 0; 7706 skb->mono_delivery_time = 0; 7707 break; 7708 default: 7709 return -EINVAL; 7710 } 7711 7712 return 0; 7713 } 7714 7715 static const struct bpf_func_proto bpf_skb_set_tstamp_proto = { 7716 .func = bpf_skb_set_tstamp, 7717 .gpl_only = false, 7718 .ret_type = RET_INTEGER, 7719 .arg1_type = ARG_PTR_TO_CTX, 7720 .arg2_type = ARG_ANYTHING, 7721 .arg3_type = ARG_ANYTHING, 7722 }; 7723 7724 #ifdef CONFIG_SYN_COOKIES 7725 BPF_CALL_3(bpf_tcp_raw_gen_syncookie_ipv4, struct iphdr *, iph, 7726 struct tcphdr *, th, u32, th_len) 7727 { 7728 u32 cookie; 7729 u16 mss; 7730 7731 if (unlikely(th_len < sizeof(*th) || th_len != th->doff * 4)) 7732 return -EINVAL; 7733 7734 mss = tcp_parse_mss_option(th, 0) ?: TCP_MSS_DEFAULT; 7735 cookie = __cookie_v4_init_sequence(iph, th, &mss); 7736 7737 return cookie | ((u64)mss << 32); 7738 } 7739 7740 static const struct bpf_func_proto bpf_tcp_raw_gen_syncookie_ipv4_proto = { 7741 .func = bpf_tcp_raw_gen_syncookie_ipv4, 7742 .gpl_only = true, /* __cookie_v4_init_sequence() is GPL */ 7743 .pkt_access = true, 7744 .ret_type = RET_INTEGER, 7745 .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM, 7746 .arg1_size = sizeof(struct iphdr), 7747 .arg2_type = ARG_PTR_TO_MEM, 7748 .arg3_type = ARG_CONST_SIZE_OR_ZERO, 7749 }; 7750 7751 BPF_CALL_3(bpf_tcp_raw_gen_syncookie_ipv6, struct ipv6hdr *, iph, 7752 struct tcphdr *, th, u32, th_len) 7753 { 7754 #if IS_BUILTIN(CONFIG_IPV6) 7755 const u16 mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) - 7756 sizeof(struct ipv6hdr); 7757 u32 cookie; 7758 u16 mss; 7759 7760 if (unlikely(th_len < sizeof(*th) || th_len != th->doff * 4)) 7761 return -EINVAL; 7762 7763 mss = tcp_parse_mss_option(th, 0) ?: mss_clamp; 7764 cookie = __cookie_v6_init_sequence(iph, th, &mss); 7765 7766 return cookie | ((u64)mss << 32); 7767 #else 7768 return -EPROTONOSUPPORT; 7769 #endif 7770 } 7771 7772 static const struct bpf_func_proto bpf_tcp_raw_gen_syncookie_ipv6_proto = { 7773 .func = bpf_tcp_raw_gen_syncookie_ipv6, 7774 .gpl_only = true, /* __cookie_v6_init_sequence() is GPL */ 7775 .pkt_access = true, 7776 .ret_type = RET_INTEGER, 7777 .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM, 7778 .arg1_size = sizeof(struct ipv6hdr), 7779 .arg2_type = ARG_PTR_TO_MEM, 7780 .arg3_type = ARG_CONST_SIZE_OR_ZERO, 7781 }; 7782 7783 BPF_CALL_2(bpf_tcp_raw_check_syncookie_ipv4, struct iphdr *, iph, 7784 struct tcphdr *, th) 7785 { 7786 u32 cookie = ntohl(th->ack_seq) - 1; 7787 7788 if (__cookie_v4_check(iph, th, cookie) > 0) 7789 return 0; 7790 7791 return -EACCES; 7792 } 7793 7794 static const struct bpf_func_proto bpf_tcp_raw_check_syncookie_ipv4_proto = { 7795 .func = bpf_tcp_raw_check_syncookie_ipv4, 7796 .gpl_only = true, /* __cookie_v4_check is GPL */ 7797 .pkt_access = true, 7798 .ret_type = RET_INTEGER, 7799 .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM, 7800 .arg1_size = sizeof(struct iphdr), 7801 .arg2_type = ARG_PTR_TO_FIXED_SIZE_MEM, 7802 .arg2_size = sizeof(struct tcphdr), 7803 }; 7804 7805 BPF_CALL_2(bpf_tcp_raw_check_syncookie_ipv6, struct ipv6hdr *, iph, 7806 struct tcphdr *, th) 7807 { 7808 #if IS_BUILTIN(CONFIG_IPV6) 7809 u32 cookie = ntohl(th->ack_seq) - 1; 7810 7811 if (__cookie_v6_check(iph, th, cookie) > 0) 7812 return 0; 7813 7814 return -EACCES; 7815 #else 7816 return -EPROTONOSUPPORT; 7817 #endif 7818 } 7819 7820 static const struct bpf_func_proto bpf_tcp_raw_check_syncookie_ipv6_proto = { 7821 .func = bpf_tcp_raw_check_syncookie_ipv6, 7822 .gpl_only = true, /* __cookie_v6_check is GPL */ 7823 .pkt_access = true, 7824 .ret_type = RET_INTEGER, 7825 .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM, 7826 .arg1_size = sizeof(struct ipv6hdr), 7827 .arg2_type = ARG_PTR_TO_FIXED_SIZE_MEM, 7828 .arg2_size = sizeof(struct tcphdr), 7829 }; 7830 #endif /* CONFIG_SYN_COOKIES */ 7831 7832 #endif /* CONFIG_INET */ 7833 7834 bool bpf_helper_changes_pkt_data(void *func) 7835 { 7836 if (func == bpf_skb_vlan_push || 7837 func == bpf_skb_vlan_pop || 7838 func == bpf_skb_store_bytes || 7839 func == bpf_skb_change_proto || 7840 func == bpf_skb_change_head || 7841 func == sk_skb_change_head || 7842 func == bpf_skb_change_tail || 7843 func == sk_skb_change_tail || 7844 func == bpf_skb_adjust_room || 7845 func == sk_skb_adjust_room || 7846 func == bpf_skb_pull_data || 7847 func == sk_skb_pull_data || 7848 func == bpf_clone_redirect || 7849 func == bpf_l3_csum_replace || 7850 func == bpf_l4_csum_replace || 7851 func == bpf_xdp_adjust_head || 7852 func == bpf_xdp_adjust_meta || 7853 func == bpf_msg_pull_data || 7854 func == bpf_msg_push_data || 7855 func == bpf_msg_pop_data || 7856 func == bpf_xdp_adjust_tail || 7857 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF) 7858 func == bpf_lwt_seg6_store_bytes || 7859 func == bpf_lwt_seg6_adjust_srh || 7860 func == bpf_lwt_seg6_action || 7861 #endif 7862 #ifdef CONFIG_INET 7863 func == bpf_sock_ops_store_hdr_opt || 7864 #endif 7865 func == bpf_lwt_in_push_encap || 7866 func == bpf_lwt_xmit_push_encap) 7867 return true; 7868 7869 return false; 7870 } 7871 7872 const struct bpf_func_proto bpf_event_output_data_proto __weak; 7873 const struct bpf_func_proto bpf_sk_storage_get_cg_sock_proto __weak; 7874 7875 static const struct bpf_func_proto * 7876 sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 7877 { 7878 const struct bpf_func_proto *func_proto; 7879 7880 func_proto = cgroup_common_func_proto(func_id, prog); 7881 if (func_proto) 7882 return func_proto; 7883 7884 func_proto = cgroup_current_func_proto(func_id, prog); 7885 if (func_proto) 7886 return func_proto; 7887 7888 switch (func_id) { 7889 case BPF_FUNC_get_socket_cookie: 7890 return &bpf_get_socket_cookie_sock_proto; 7891 case BPF_FUNC_get_netns_cookie: 7892 return &bpf_get_netns_cookie_sock_proto; 7893 case BPF_FUNC_perf_event_output: 7894 return &bpf_event_output_data_proto; 7895 case BPF_FUNC_sk_storage_get: 7896 return &bpf_sk_storage_get_cg_sock_proto; 7897 case BPF_FUNC_ktime_get_coarse_ns: 7898 return &bpf_ktime_get_coarse_ns_proto; 7899 default: 7900 return bpf_base_func_proto(func_id); 7901 } 7902 } 7903 7904 static const struct bpf_func_proto * 7905 sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 7906 { 7907 const struct bpf_func_proto *func_proto; 7908 7909 func_proto = cgroup_common_func_proto(func_id, prog); 7910 if (func_proto) 7911 return func_proto; 7912 7913 func_proto = cgroup_current_func_proto(func_id, prog); 7914 if (func_proto) 7915 return func_proto; 7916 7917 switch (func_id) { 7918 case BPF_FUNC_bind: 7919 switch (prog->expected_attach_type) { 7920 case BPF_CGROUP_INET4_CONNECT: 7921 case BPF_CGROUP_INET6_CONNECT: 7922 return &bpf_bind_proto; 7923 default: 7924 return NULL; 7925 } 7926 case BPF_FUNC_get_socket_cookie: 7927 return &bpf_get_socket_cookie_sock_addr_proto; 7928 case BPF_FUNC_get_netns_cookie: 7929 return &bpf_get_netns_cookie_sock_addr_proto; 7930 case BPF_FUNC_perf_event_output: 7931 return &bpf_event_output_data_proto; 7932 #ifdef CONFIG_INET 7933 case BPF_FUNC_sk_lookup_tcp: 7934 return &bpf_sock_addr_sk_lookup_tcp_proto; 7935 case BPF_FUNC_sk_lookup_udp: 7936 return &bpf_sock_addr_sk_lookup_udp_proto; 7937 case BPF_FUNC_sk_release: 7938 return &bpf_sk_release_proto; 7939 case BPF_FUNC_skc_lookup_tcp: 7940 return &bpf_sock_addr_skc_lookup_tcp_proto; 7941 #endif /* CONFIG_INET */ 7942 case BPF_FUNC_sk_storage_get: 7943 return &bpf_sk_storage_get_proto; 7944 case BPF_FUNC_sk_storage_delete: 7945 return &bpf_sk_storage_delete_proto; 7946 case BPF_FUNC_setsockopt: 7947 switch (prog->expected_attach_type) { 7948 case BPF_CGROUP_INET4_BIND: 7949 case BPF_CGROUP_INET6_BIND: 7950 case BPF_CGROUP_INET4_CONNECT: 7951 case BPF_CGROUP_INET6_CONNECT: 7952 case BPF_CGROUP_UDP4_RECVMSG: 7953 case BPF_CGROUP_UDP6_RECVMSG: 7954 case BPF_CGROUP_UDP4_SENDMSG: 7955 case BPF_CGROUP_UDP6_SENDMSG: 7956 case BPF_CGROUP_INET4_GETPEERNAME: 7957 case BPF_CGROUP_INET6_GETPEERNAME: 7958 case BPF_CGROUP_INET4_GETSOCKNAME: 7959 case BPF_CGROUP_INET6_GETSOCKNAME: 7960 return &bpf_sock_addr_setsockopt_proto; 7961 default: 7962 return NULL; 7963 } 7964 case BPF_FUNC_getsockopt: 7965 switch (prog->expected_attach_type) { 7966 case BPF_CGROUP_INET4_BIND: 7967 case BPF_CGROUP_INET6_BIND: 7968 case BPF_CGROUP_INET4_CONNECT: 7969 case BPF_CGROUP_INET6_CONNECT: 7970 case BPF_CGROUP_UDP4_RECVMSG: 7971 case BPF_CGROUP_UDP6_RECVMSG: 7972 case BPF_CGROUP_UDP4_SENDMSG: 7973 case BPF_CGROUP_UDP6_SENDMSG: 7974 case BPF_CGROUP_INET4_GETPEERNAME: 7975 case BPF_CGROUP_INET6_GETPEERNAME: 7976 case BPF_CGROUP_INET4_GETSOCKNAME: 7977 case BPF_CGROUP_INET6_GETSOCKNAME: 7978 return &bpf_sock_addr_getsockopt_proto; 7979 default: 7980 return NULL; 7981 } 7982 default: 7983 return bpf_sk_base_func_proto(func_id); 7984 } 7985 } 7986 7987 static const struct bpf_func_proto * 7988 sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 7989 { 7990 switch (func_id) { 7991 case BPF_FUNC_skb_load_bytes: 7992 return &bpf_skb_load_bytes_proto; 7993 case BPF_FUNC_skb_load_bytes_relative: 7994 return &bpf_skb_load_bytes_relative_proto; 7995 case BPF_FUNC_get_socket_cookie: 7996 return &bpf_get_socket_cookie_proto; 7997 case BPF_FUNC_get_socket_uid: 7998 return &bpf_get_socket_uid_proto; 7999 case BPF_FUNC_perf_event_output: 8000 return &bpf_skb_event_output_proto; 8001 default: 8002 return bpf_sk_base_func_proto(func_id); 8003 } 8004 } 8005 8006 const struct bpf_func_proto bpf_sk_storage_get_proto __weak; 8007 const struct bpf_func_proto bpf_sk_storage_delete_proto __weak; 8008 8009 static const struct bpf_func_proto * 8010 cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 8011 { 8012 const struct bpf_func_proto *func_proto; 8013 8014 func_proto = cgroup_common_func_proto(func_id, prog); 8015 if (func_proto) 8016 return func_proto; 8017 8018 switch (func_id) { 8019 case BPF_FUNC_sk_fullsock: 8020 return &bpf_sk_fullsock_proto; 8021 case BPF_FUNC_sk_storage_get: 8022 return &bpf_sk_storage_get_proto; 8023 case BPF_FUNC_sk_storage_delete: 8024 return &bpf_sk_storage_delete_proto; 8025 case BPF_FUNC_perf_event_output: 8026 return &bpf_skb_event_output_proto; 8027 #ifdef CONFIG_SOCK_CGROUP_DATA 8028 case BPF_FUNC_skb_cgroup_id: 8029 return &bpf_skb_cgroup_id_proto; 8030 case BPF_FUNC_skb_ancestor_cgroup_id: 8031 return &bpf_skb_ancestor_cgroup_id_proto; 8032 case BPF_FUNC_sk_cgroup_id: 8033 return &bpf_sk_cgroup_id_proto; 8034 case BPF_FUNC_sk_ancestor_cgroup_id: 8035 return &bpf_sk_ancestor_cgroup_id_proto; 8036 #endif 8037 #ifdef CONFIG_INET 8038 case BPF_FUNC_sk_lookup_tcp: 8039 return &bpf_sk_lookup_tcp_proto; 8040 case BPF_FUNC_sk_lookup_udp: 8041 return &bpf_sk_lookup_udp_proto; 8042 case BPF_FUNC_sk_release: 8043 return &bpf_sk_release_proto; 8044 case BPF_FUNC_skc_lookup_tcp: 8045 return &bpf_skc_lookup_tcp_proto; 8046 case BPF_FUNC_tcp_sock: 8047 return &bpf_tcp_sock_proto; 8048 case BPF_FUNC_get_listener_sock: 8049 return &bpf_get_listener_sock_proto; 8050 case BPF_FUNC_skb_ecn_set_ce: 8051 return &bpf_skb_ecn_set_ce_proto; 8052 #endif 8053 default: 8054 return sk_filter_func_proto(func_id, prog); 8055 } 8056 } 8057 8058 static const struct bpf_func_proto * 8059 tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 8060 { 8061 switch (func_id) { 8062 case BPF_FUNC_skb_store_bytes: 8063 return &bpf_skb_store_bytes_proto; 8064 case BPF_FUNC_skb_load_bytes: 8065 return &bpf_skb_load_bytes_proto; 8066 case BPF_FUNC_skb_load_bytes_relative: 8067 return &bpf_skb_load_bytes_relative_proto; 8068 case BPF_FUNC_skb_pull_data: 8069 return &bpf_skb_pull_data_proto; 8070 case BPF_FUNC_csum_diff: 8071 return &bpf_csum_diff_proto; 8072 case BPF_FUNC_csum_update: 8073 return &bpf_csum_update_proto; 8074 case BPF_FUNC_csum_level: 8075 return &bpf_csum_level_proto; 8076 case BPF_FUNC_l3_csum_replace: 8077 return &bpf_l3_csum_replace_proto; 8078 case BPF_FUNC_l4_csum_replace: 8079 return &bpf_l4_csum_replace_proto; 8080 case BPF_FUNC_clone_redirect: 8081 return &bpf_clone_redirect_proto; 8082 case BPF_FUNC_get_cgroup_classid: 8083 return &bpf_get_cgroup_classid_proto; 8084 case BPF_FUNC_skb_vlan_push: 8085 return &bpf_skb_vlan_push_proto; 8086 case BPF_FUNC_skb_vlan_pop: 8087 return &bpf_skb_vlan_pop_proto; 8088 case BPF_FUNC_skb_change_proto: 8089 return &bpf_skb_change_proto_proto; 8090 case BPF_FUNC_skb_change_type: 8091 return &bpf_skb_change_type_proto; 8092 case BPF_FUNC_skb_adjust_room: 8093 return &bpf_skb_adjust_room_proto; 8094 case BPF_FUNC_skb_change_tail: 8095 return &bpf_skb_change_tail_proto; 8096 case BPF_FUNC_skb_change_head: 8097 return &bpf_skb_change_head_proto; 8098 case BPF_FUNC_skb_get_tunnel_key: 8099 return &bpf_skb_get_tunnel_key_proto; 8100 case BPF_FUNC_skb_set_tunnel_key: 8101 return bpf_get_skb_set_tunnel_proto(func_id); 8102 case BPF_FUNC_skb_get_tunnel_opt: 8103 return &bpf_skb_get_tunnel_opt_proto; 8104 case BPF_FUNC_skb_set_tunnel_opt: 8105 return bpf_get_skb_set_tunnel_proto(func_id); 8106 case BPF_FUNC_redirect: 8107 return &bpf_redirect_proto; 8108 case BPF_FUNC_redirect_neigh: 8109 return &bpf_redirect_neigh_proto; 8110 case BPF_FUNC_redirect_peer: 8111 return &bpf_redirect_peer_proto; 8112 case BPF_FUNC_get_route_realm: 8113 return &bpf_get_route_realm_proto; 8114 case BPF_FUNC_get_hash_recalc: 8115 return &bpf_get_hash_recalc_proto; 8116 case BPF_FUNC_set_hash_invalid: 8117 return &bpf_set_hash_invalid_proto; 8118 case BPF_FUNC_set_hash: 8119 return &bpf_set_hash_proto; 8120 case BPF_FUNC_perf_event_output: 8121 return &bpf_skb_event_output_proto; 8122 case BPF_FUNC_get_smp_processor_id: 8123 return &bpf_get_smp_processor_id_proto; 8124 case BPF_FUNC_skb_under_cgroup: 8125 return &bpf_skb_under_cgroup_proto; 8126 case BPF_FUNC_get_socket_cookie: 8127 return &bpf_get_socket_cookie_proto; 8128 case BPF_FUNC_get_socket_uid: 8129 return &bpf_get_socket_uid_proto; 8130 case BPF_FUNC_fib_lookup: 8131 return &bpf_skb_fib_lookup_proto; 8132 case BPF_FUNC_check_mtu: 8133 return &bpf_skb_check_mtu_proto; 8134 case BPF_FUNC_sk_fullsock: 8135 return &bpf_sk_fullsock_proto; 8136 case BPF_FUNC_sk_storage_get: 8137 return &bpf_sk_storage_get_proto; 8138 case BPF_FUNC_sk_storage_delete: 8139 return &bpf_sk_storage_delete_proto; 8140 #ifdef CONFIG_XFRM 8141 case BPF_FUNC_skb_get_xfrm_state: 8142 return &bpf_skb_get_xfrm_state_proto; 8143 #endif 8144 #ifdef CONFIG_CGROUP_NET_CLASSID 8145 case BPF_FUNC_skb_cgroup_classid: 8146 return &bpf_skb_cgroup_classid_proto; 8147 #endif 8148 #ifdef CONFIG_SOCK_CGROUP_DATA 8149 case BPF_FUNC_skb_cgroup_id: 8150 return &bpf_skb_cgroup_id_proto; 8151 case BPF_FUNC_skb_ancestor_cgroup_id: 8152 return &bpf_skb_ancestor_cgroup_id_proto; 8153 #endif 8154 #ifdef CONFIG_INET 8155 case BPF_FUNC_sk_lookup_tcp: 8156 return &bpf_tc_sk_lookup_tcp_proto; 8157 case BPF_FUNC_sk_lookup_udp: 8158 return &bpf_tc_sk_lookup_udp_proto; 8159 case BPF_FUNC_sk_release: 8160 return &bpf_sk_release_proto; 8161 case BPF_FUNC_tcp_sock: 8162 return &bpf_tcp_sock_proto; 8163 case BPF_FUNC_get_listener_sock: 8164 return &bpf_get_listener_sock_proto; 8165 case BPF_FUNC_skc_lookup_tcp: 8166 return &bpf_tc_skc_lookup_tcp_proto; 8167 case BPF_FUNC_tcp_check_syncookie: 8168 return &bpf_tcp_check_syncookie_proto; 8169 case BPF_FUNC_skb_ecn_set_ce: 8170 return &bpf_skb_ecn_set_ce_proto; 8171 case BPF_FUNC_tcp_gen_syncookie: 8172 return &bpf_tcp_gen_syncookie_proto; 8173 case BPF_FUNC_sk_assign: 8174 return &bpf_sk_assign_proto; 8175 case BPF_FUNC_skb_set_tstamp: 8176 return &bpf_skb_set_tstamp_proto; 8177 #ifdef CONFIG_SYN_COOKIES 8178 case BPF_FUNC_tcp_raw_gen_syncookie_ipv4: 8179 return &bpf_tcp_raw_gen_syncookie_ipv4_proto; 8180 case BPF_FUNC_tcp_raw_gen_syncookie_ipv6: 8181 return &bpf_tcp_raw_gen_syncookie_ipv6_proto; 8182 case BPF_FUNC_tcp_raw_check_syncookie_ipv4: 8183 return &bpf_tcp_raw_check_syncookie_ipv4_proto; 8184 case BPF_FUNC_tcp_raw_check_syncookie_ipv6: 8185 return &bpf_tcp_raw_check_syncookie_ipv6_proto; 8186 #endif 8187 #endif 8188 default: 8189 return bpf_sk_base_func_proto(func_id); 8190 } 8191 } 8192 8193 static const struct bpf_func_proto * 8194 xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 8195 { 8196 switch (func_id) { 8197 case BPF_FUNC_perf_event_output: 8198 return &bpf_xdp_event_output_proto; 8199 case BPF_FUNC_get_smp_processor_id: 8200 return &bpf_get_smp_processor_id_proto; 8201 case BPF_FUNC_csum_diff: 8202 return &bpf_csum_diff_proto; 8203 case BPF_FUNC_xdp_adjust_head: 8204 return &bpf_xdp_adjust_head_proto; 8205 case BPF_FUNC_xdp_adjust_meta: 8206 return &bpf_xdp_adjust_meta_proto; 8207 case BPF_FUNC_redirect: 8208 return &bpf_xdp_redirect_proto; 8209 case BPF_FUNC_redirect_map: 8210 return &bpf_xdp_redirect_map_proto; 8211 case BPF_FUNC_xdp_adjust_tail: 8212 return &bpf_xdp_adjust_tail_proto; 8213 case BPF_FUNC_xdp_get_buff_len: 8214 return &bpf_xdp_get_buff_len_proto; 8215 case BPF_FUNC_xdp_load_bytes: 8216 return &bpf_xdp_load_bytes_proto; 8217 case BPF_FUNC_xdp_store_bytes: 8218 return &bpf_xdp_store_bytes_proto; 8219 case BPF_FUNC_fib_lookup: 8220 return &bpf_xdp_fib_lookup_proto; 8221 case BPF_FUNC_check_mtu: 8222 return &bpf_xdp_check_mtu_proto; 8223 #ifdef CONFIG_INET 8224 case BPF_FUNC_sk_lookup_udp: 8225 return &bpf_xdp_sk_lookup_udp_proto; 8226 case BPF_FUNC_sk_lookup_tcp: 8227 return &bpf_xdp_sk_lookup_tcp_proto; 8228 case BPF_FUNC_sk_release: 8229 return &bpf_sk_release_proto; 8230 case BPF_FUNC_skc_lookup_tcp: 8231 return &bpf_xdp_skc_lookup_tcp_proto; 8232 case BPF_FUNC_tcp_check_syncookie: 8233 return &bpf_tcp_check_syncookie_proto; 8234 case BPF_FUNC_tcp_gen_syncookie: 8235 return &bpf_tcp_gen_syncookie_proto; 8236 #ifdef CONFIG_SYN_COOKIES 8237 case BPF_FUNC_tcp_raw_gen_syncookie_ipv4: 8238 return &bpf_tcp_raw_gen_syncookie_ipv4_proto; 8239 case BPF_FUNC_tcp_raw_gen_syncookie_ipv6: 8240 return &bpf_tcp_raw_gen_syncookie_ipv6_proto; 8241 case BPF_FUNC_tcp_raw_check_syncookie_ipv4: 8242 return &bpf_tcp_raw_check_syncookie_ipv4_proto; 8243 case BPF_FUNC_tcp_raw_check_syncookie_ipv6: 8244 return &bpf_tcp_raw_check_syncookie_ipv6_proto; 8245 #endif 8246 #endif 8247 default: 8248 return bpf_sk_base_func_proto(func_id); 8249 } 8250 8251 #if IS_MODULE(CONFIG_NF_CONNTRACK) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES) 8252 /* The nf_conn___init type is used in the NF_CONNTRACK kfuncs. The 8253 * kfuncs are defined in two different modules, and we want to be able 8254 * to use them interchangably with the same BTF type ID. Because modules 8255 * can't de-duplicate BTF IDs between each other, we need the type to be 8256 * referenced in the vmlinux BTF or the verifier will get confused about 8257 * the different types. So we add this dummy type reference which will 8258 * be included in vmlinux BTF, allowing both modules to refer to the 8259 * same type ID. 8260 */ 8261 BTF_TYPE_EMIT(struct nf_conn___init); 8262 #endif 8263 } 8264 8265 const struct bpf_func_proto bpf_sock_map_update_proto __weak; 8266 const struct bpf_func_proto bpf_sock_hash_update_proto __weak; 8267 8268 static const struct bpf_func_proto * 8269 sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 8270 { 8271 const struct bpf_func_proto *func_proto; 8272 8273 func_proto = cgroup_common_func_proto(func_id, prog); 8274 if (func_proto) 8275 return func_proto; 8276 8277 switch (func_id) { 8278 case BPF_FUNC_setsockopt: 8279 return &bpf_sock_ops_setsockopt_proto; 8280 case BPF_FUNC_getsockopt: 8281 return &bpf_sock_ops_getsockopt_proto; 8282 case BPF_FUNC_sock_ops_cb_flags_set: 8283 return &bpf_sock_ops_cb_flags_set_proto; 8284 case BPF_FUNC_sock_map_update: 8285 return &bpf_sock_map_update_proto; 8286 case BPF_FUNC_sock_hash_update: 8287 return &bpf_sock_hash_update_proto; 8288 case BPF_FUNC_get_socket_cookie: 8289 return &bpf_get_socket_cookie_sock_ops_proto; 8290 case BPF_FUNC_perf_event_output: 8291 return &bpf_event_output_data_proto; 8292 case BPF_FUNC_sk_storage_get: 8293 return &bpf_sk_storage_get_proto; 8294 case BPF_FUNC_sk_storage_delete: 8295 return &bpf_sk_storage_delete_proto; 8296 case BPF_FUNC_get_netns_cookie: 8297 return &bpf_get_netns_cookie_sock_ops_proto; 8298 #ifdef CONFIG_INET 8299 case BPF_FUNC_load_hdr_opt: 8300 return &bpf_sock_ops_load_hdr_opt_proto; 8301 case BPF_FUNC_store_hdr_opt: 8302 return &bpf_sock_ops_store_hdr_opt_proto; 8303 case BPF_FUNC_reserve_hdr_opt: 8304 return &bpf_sock_ops_reserve_hdr_opt_proto; 8305 case BPF_FUNC_tcp_sock: 8306 return &bpf_tcp_sock_proto; 8307 #endif /* CONFIG_INET */ 8308 default: 8309 return bpf_sk_base_func_proto(func_id); 8310 } 8311 } 8312 8313 const struct bpf_func_proto bpf_msg_redirect_map_proto __weak; 8314 const struct bpf_func_proto bpf_msg_redirect_hash_proto __weak; 8315 8316 static const struct bpf_func_proto * 8317 sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 8318 { 8319 switch (func_id) { 8320 case BPF_FUNC_msg_redirect_map: 8321 return &bpf_msg_redirect_map_proto; 8322 case BPF_FUNC_msg_redirect_hash: 8323 return &bpf_msg_redirect_hash_proto; 8324 case BPF_FUNC_msg_apply_bytes: 8325 return &bpf_msg_apply_bytes_proto; 8326 case BPF_FUNC_msg_cork_bytes: 8327 return &bpf_msg_cork_bytes_proto; 8328 case BPF_FUNC_msg_pull_data: 8329 return &bpf_msg_pull_data_proto; 8330 case BPF_FUNC_msg_push_data: 8331 return &bpf_msg_push_data_proto; 8332 case BPF_FUNC_msg_pop_data: 8333 return &bpf_msg_pop_data_proto; 8334 case BPF_FUNC_perf_event_output: 8335 return &bpf_event_output_data_proto; 8336 case BPF_FUNC_get_current_uid_gid: 8337 return &bpf_get_current_uid_gid_proto; 8338 case BPF_FUNC_get_current_pid_tgid: 8339 return &bpf_get_current_pid_tgid_proto; 8340 case BPF_FUNC_sk_storage_get: 8341 return &bpf_sk_storage_get_proto; 8342 case BPF_FUNC_sk_storage_delete: 8343 return &bpf_sk_storage_delete_proto; 8344 case BPF_FUNC_get_netns_cookie: 8345 return &bpf_get_netns_cookie_sk_msg_proto; 8346 #ifdef CONFIG_CGROUP_NET_CLASSID 8347 case BPF_FUNC_get_cgroup_classid: 8348 return &bpf_get_cgroup_classid_curr_proto; 8349 #endif 8350 default: 8351 return bpf_sk_base_func_proto(func_id); 8352 } 8353 } 8354 8355 const struct bpf_func_proto bpf_sk_redirect_map_proto __weak; 8356 const struct bpf_func_proto bpf_sk_redirect_hash_proto __weak; 8357 8358 static const struct bpf_func_proto * 8359 sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 8360 { 8361 switch (func_id) { 8362 case BPF_FUNC_skb_store_bytes: 8363 return &bpf_skb_store_bytes_proto; 8364 case BPF_FUNC_skb_load_bytes: 8365 return &bpf_skb_load_bytes_proto; 8366 case BPF_FUNC_skb_pull_data: 8367 return &sk_skb_pull_data_proto; 8368 case BPF_FUNC_skb_change_tail: 8369 return &sk_skb_change_tail_proto; 8370 case BPF_FUNC_skb_change_head: 8371 return &sk_skb_change_head_proto; 8372 case BPF_FUNC_skb_adjust_room: 8373 return &sk_skb_adjust_room_proto; 8374 case BPF_FUNC_get_socket_cookie: 8375 return &bpf_get_socket_cookie_proto; 8376 case BPF_FUNC_get_socket_uid: 8377 return &bpf_get_socket_uid_proto; 8378 case BPF_FUNC_sk_redirect_map: 8379 return &bpf_sk_redirect_map_proto; 8380 case BPF_FUNC_sk_redirect_hash: 8381 return &bpf_sk_redirect_hash_proto; 8382 case BPF_FUNC_perf_event_output: 8383 return &bpf_skb_event_output_proto; 8384 #ifdef CONFIG_INET 8385 case BPF_FUNC_sk_lookup_tcp: 8386 return &bpf_sk_lookup_tcp_proto; 8387 case BPF_FUNC_sk_lookup_udp: 8388 return &bpf_sk_lookup_udp_proto; 8389 case BPF_FUNC_sk_release: 8390 return &bpf_sk_release_proto; 8391 case BPF_FUNC_skc_lookup_tcp: 8392 return &bpf_skc_lookup_tcp_proto; 8393 #endif 8394 default: 8395 return bpf_sk_base_func_proto(func_id); 8396 } 8397 } 8398 8399 static const struct bpf_func_proto * 8400 flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 8401 { 8402 switch (func_id) { 8403 case BPF_FUNC_skb_load_bytes: 8404 return &bpf_flow_dissector_load_bytes_proto; 8405 default: 8406 return bpf_sk_base_func_proto(func_id); 8407 } 8408 } 8409 8410 static const struct bpf_func_proto * 8411 lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 8412 { 8413 switch (func_id) { 8414 case BPF_FUNC_skb_load_bytes: 8415 return &bpf_skb_load_bytes_proto; 8416 case BPF_FUNC_skb_pull_data: 8417 return &bpf_skb_pull_data_proto; 8418 case BPF_FUNC_csum_diff: 8419 return &bpf_csum_diff_proto; 8420 case BPF_FUNC_get_cgroup_classid: 8421 return &bpf_get_cgroup_classid_proto; 8422 case BPF_FUNC_get_route_realm: 8423 return &bpf_get_route_realm_proto; 8424 case BPF_FUNC_get_hash_recalc: 8425 return &bpf_get_hash_recalc_proto; 8426 case BPF_FUNC_perf_event_output: 8427 return &bpf_skb_event_output_proto; 8428 case BPF_FUNC_get_smp_processor_id: 8429 return &bpf_get_smp_processor_id_proto; 8430 case BPF_FUNC_skb_under_cgroup: 8431 return &bpf_skb_under_cgroup_proto; 8432 default: 8433 return bpf_sk_base_func_proto(func_id); 8434 } 8435 } 8436 8437 static const struct bpf_func_proto * 8438 lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 8439 { 8440 switch (func_id) { 8441 case BPF_FUNC_lwt_push_encap: 8442 return &bpf_lwt_in_push_encap_proto; 8443 default: 8444 return lwt_out_func_proto(func_id, prog); 8445 } 8446 } 8447 8448 static const struct bpf_func_proto * 8449 lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 8450 { 8451 switch (func_id) { 8452 case BPF_FUNC_skb_get_tunnel_key: 8453 return &bpf_skb_get_tunnel_key_proto; 8454 case BPF_FUNC_skb_set_tunnel_key: 8455 return bpf_get_skb_set_tunnel_proto(func_id); 8456 case BPF_FUNC_skb_get_tunnel_opt: 8457 return &bpf_skb_get_tunnel_opt_proto; 8458 case BPF_FUNC_skb_set_tunnel_opt: 8459 return bpf_get_skb_set_tunnel_proto(func_id); 8460 case BPF_FUNC_redirect: 8461 return &bpf_redirect_proto; 8462 case BPF_FUNC_clone_redirect: 8463 return &bpf_clone_redirect_proto; 8464 case BPF_FUNC_skb_change_tail: 8465 return &bpf_skb_change_tail_proto; 8466 case BPF_FUNC_skb_change_head: 8467 return &bpf_skb_change_head_proto; 8468 case BPF_FUNC_skb_store_bytes: 8469 return &bpf_skb_store_bytes_proto; 8470 case BPF_FUNC_csum_update: 8471 return &bpf_csum_update_proto; 8472 case BPF_FUNC_csum_level: 8473 return &bpf_csum_level_proto; 8474 case BPF_FUNC_l3_csum_replace: 8475 return &bpf_l3_csum_replace_proto; 8476 case BPF_FUNC_l4_csum_replace: 8477 return &bpf_l4_csum_replace_proto; 8478 case BPF_FUNC_set_hash_invalid: 8479 return &bpf_set_hash_invalid_proto; 8480 case BPF_FUNC_lwt_push_encap: 8481 return &bpf_lwt_xmit_push_encap_proto; 8482 default: 8483 return lwt_out_func_proto(func_id, prog); 8484 } 8485 } 8486 8487 static const struct bpf_func_proto * 8488 lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 8489 { 8490 switch (func_id) { 8491 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF) 8492 case BPF_FUNC_lwt_seg6_store_bytes: 8493 return &bpf_lwt_seg6_store_bytes_proto; 8494 case BPF_FUNC_lwt_seg6_action: 8495 return &bpf_lwt_seg6_action_proto; 8496 case BPF_FUNC_lwt_seg6_adjust_srh: 8497 return &bpf_lwt_seg6_adjust_srh_proto; 8498 #endif 8499 default: 8500 return lwt_out_func_proto(func_id, prog); 8501 } 8502 } 8503 8504 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type, 8505 const struct bpf_prog *prog, 8506 struct bpf_insn_access_aux *info) 8507 { 8508 const int size_default = sizeof(__u32); 8509 8510 if (off < 0 || off >= sizeof(struct __sk_buff)) 8511 return false; 8512 8513 /* The verifier guarantees that size > 0. */ 8514 if (off % size != 0) 8515 return false; 8516 8517 switch (off) { 8518 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]): 8519 if (off + size > offsetofend(struct __sk_buff, cb[4])) 8520 return false; 8521 break; 8522 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]): 8523 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]): 8524 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4): 8525 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4): 8526 case bpf_ctx_range(struct __sk_buff, data): 8527 case bpf_ctx_range(struct __sk_buff, data_meta): 8528 case bpf_ctx_range(struct __sk_buff, data_end): 8529 if (size != size_default) 8530 return false; 8531 break; 8532 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys): 8533 return false; 8534 case bpf_ctx_range(struct __sk_buff, hwtstamp): 8535 if (type == BPF_WRITE || size != sizeof(__u64)) 8536 return false; 8537 break; 8538 case bpf_ctx_range(struct __sk_buff, tstamp): 8539 if (size != sizeof(__u64)) 8540 return false; 8541 break; 8542 case offsetof(struct __sk_buff, sk): 8543 if (type == BPF_WRITE || size != sizeof(__u64)) 8544 return false; 8545 info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL; 8546 break; 8547 case offsetof(struct __sk_buff, tstamp_type): 8548 return false; 8549 case offsetofend(struct __sk_buff, tstamp_type) ... offsetof(struct __sk_buff, hwtstamp) - 1: 8550 /* Explicitly prohibit access to padding in __sk_buff. */ 8551 return false; 8552 default: 8553 /* Only narrow read access allowed for now. */ 8554 if (type == BPF_WRITE) { 8555 if (size != size_default) 8556 return false; 8557 } else { 8558 bpf_ctx_record_field_size(info, size_default); 8559 if (!bpf_ctx_narrow_access_ok(off, size, size_default)) 8560 return false; 8561 } 8562 } 8563 8564 return true; 8565 } 8566 8567 static bool sk_filter_is_valid_access(int off, int size, 8568 enum bpf_access_type type, 8569 const struct bpf_prog *prog, 8570 struct bpf_insn_access_aux *info) 8571 { 8572 switch (off) { 8573 case bpf_ctx_range(struct __sk_buff, tc_classid): 8574 case bpf_ctx_range(struct __sk_buff, data): 8575 case bpf_ctx_range(struct __sk_buff, data_meta): 8576 case bpf_ctx_range(struct __sk_buff, data_end): 8577 case bpf_ctx_range_till(struct __sk_buff, family, local_port): 8578 case bpf_ctx_range(struct __sk_buff, tstamp): 8579 case bpf_ctx_range(struct __sk_buff, wire_len): 8580 case bpf_ctx_range(struct __sk_buff, hwtstamp): 8581 return false; 8582 } 8583 8584 if (type == BPF_WRITE) { 8585 switch (off) { 8586 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]): 8587 break; 8588 default: 8589 return false; 8590 } 8591 } 8592 8593 return bpf_skb_is_valid_access(off, size, type, prog, info); 8594 } 8595 8596 static bool cg_skb_is_valid_access(int off, int size, 8597 enum bpf_access_type type, 8598 const struct bpf_prog *prog, 8599 struct bpf_insn_access_aux *info) 8600 { 8601 switch (off) { 8602 case bpf_ctx_range(struct __sk_buff, tc_classid): 8603 case bpf_ctx_range(struct __sk_buff, data_meta): 8604 case bpf_ctx_range(struct __sk_buff, wire_len): 8605 return false; 8606 case bpf_ctx_range(struct __sk_buff, data): 8607 case bpf_ctx_range(struct __sk_buff, data_end): 8608 if (!bpf_capable()) 8609 return false; 8610 break; 8611 } 8612 8613 if (type == BPF_WRITE) { 8614 switch (off) { 8615 case bpf_ctx_range(struct __sk_buff, mark): 8616 case bpf_ctx_range(struct __sk_buff, priority): 8617 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]): 8618 break; 8619 case bpf_ctx_range(struct __sk_buff, tstamp): 8620 if (!bpf_capable()) 8621 return false; 8622 break; 8623 default: 8624 return false; 8625 } 8626 } 8627 8628 switch (off) { 8629 case bpf_ctx_range(struct __sk_buff, data): 8630 info->reg_type = PTR_TO_PACKET; 8631 break; 8632 case bpf_ctx_range(struct __sk_buff, data_end): 8633 info->reg_type = PTR_TO_PACKET_END; 8634 break; 8635 } 8636 8637 return bpf_skb_is_valid_access(off, size, type, prog, info); 8638 } 8639 8640 static bool lwt_is_valid_access(int off, int size, 8641 enum bpf_access_type type, 8642 const struct bpf_prog *prog, 8643 struct bpf_insn_access_aux *info) 8644 { 8645 switch (off) { 8646 case bpf_ctx_range(struct __sk_buff, tc_classid): 8647 case bpf_ctx_range_till(struct __sk_buff, family, local_port): 8648 case bpf_ctx_range(struct __sk_buff, data_meta): 8649 case bpf_ctx_range(struct __sk_buff, tstamp): 8650 case bpf_ctx_range(struct __sk_buff, wire_len): 8651 case bpf_ctx_range(struct __sk_buff, hwtstamp): 8652 return false; 8653 } 8654 8655 if (type == BPF_WRITE) { 8656 switch (off) { 8657 case bpf_ctx_range(struct __sk_buff, mark): 8658 case bpf_ctx_range(struct __sk_buff, priority): 8659 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]): 8660 break; 8661 default: 8662 return false; 8663 } 8664 } 8665 8666 switch (off) { 8667 case bpf_ctx_range(struct __sk_buff, data): 8668 info->reg_type = PTR_TO_PACKET; 8669 break; 8670 case bpf_ctx_range(struct __sk_buff, data_end): 8671 info->reg_type = PTR_TO_PACKET_END; 8672 break; 8673 } 8674 8675 return bpf_skb_is_valid_access(off, size, type, prog, info); 8676 } 8677 8678 /* Attach type specific accesses */ 8679 static bool __sock_filter_check_attach_type(int off, 8680 enum bpf_access_type access_type, 8681 enum bpf_attach_type attach_type) 8682 { 8683 switch (off) { 8684 case offsetof(struct bpf_sock, bound_dev_if): 8685 case offsetof(struct bpf_sock, mark): 8686 case offsetof(struct bpf_sock, priority): 8687 switch (attach_type) { 8688 case BPF_CGROUP_INET_SOCK_CREATE: 8689 case BPF_CGROUP_INET_SOCK_RELEASE: 8690 goto full_access; 8691 default: 8692 return false; 8693 } 8694 case bpf_ctx_range(struct bpf_sock, src_ip4): 8695 switch (attach_type) { 8696 case BPF_CGROUP_INET4_POST_BIND: 8697 goto read_only; 8698 default: 8699 return false; 8700 } 8701 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]): 8702 switch (attach_type) { 8703 case BPF_CGROUP_INET6_POST_BIND: 8704 goto read_only; 8705 default: 8706 return false; 8707 } 8708 case bpf_ctx_range(struct bpf_sock, src_port): 8709 switch (attach_type) { 8710 case BPF_CGROUP_INET4_POST_BIND: 8711 case BPF_CGROUP_INET6_POST_BIND: 8712 goto read_only; 8713 default: 8714 return false; 8715 } 8716 } 8717 read_only: 8718 return access_type == BPF_READ; 8719 full_access: 8720 return true; 8721 } 8722 8723 bool bpf_sock_common_is_valid_access(int off, int size, 8724 enum bpf_access_type type, 8725 struct bpf_insn_access_aux *info) 8726 { 8727 switch (off) { 8728 case bpf_ctx_range_till(struct bpf_sock, type, priority): 8729 return false; 8730 default: 8731 return bpf_sock_is_valid_access(off, size, type, info); 8732 } 8733 } 8734 8735 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type, 8736 struct bpf_insn_access_aux *info) 8737 { 8738 const int size_default = sizeof(__u32); 8739 int field_size; 8740 8741 if (off < 0 || off >= sizeof(struct bpf_sock)) 8742 return false; 8743 if (off % size != 0) 8744 return false; 8745 8746 switch (off) { 8747 case offsetof(struct bpf_sock, state): 8748 case offsetof(struct bpf_sock, family): 8749 case offsetof(struct bpf_sock, type): 8750 case offsetof(struct bpf_sock, protocol): 8751 case offsetof(struct bpf_sock, src_port): 8752 case offsetof(struct bpf_sock, rx_queue_mapping): 8753 case bpf_ctx_range(struct bpf_sock, src_ip4): 8754 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]): 8755 case bpf_ctx_range(struct bpf_sock, dst_ip4): 8756 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]): 8757 bpf_ctx_record_field_size(info, size_default); 8758 return bpf_ctx_narrow_access_ok(off, size, size_default); 8759 case bpf_ctx_range(struct bpf_sock, dst_port): 8760 field_size = size == size_default ? 8761 size_default : sizeof_field(struct bpf_sock, dst_port); 8762 bpf_ctx_record_field_size(info, field_size); 8763 return bpf_ctx_narrow_access_ok(off, size, field_size); 8764 case offsetofend(struct bpf_sock, dst_port) ... 8765 offsetof(struct bpf_sock, dst_ip4) - 1: 8766 return false; 8767 } 8768 8769 return size == size_default; 8770 } 8771 8772 static bool sock_filter_is_valid_access(int off, int size, 8773 enum bpf_access_type type, 8774 const struct bpf_prog *prog, 8775 struct bpf_insn_access_aux *info) 8776 { 8777 if (!bpf_sock_is_valid_access(off, size, type, info)) 8778 return false; 8779 return __sock_filter_check_attach_type(off, type, 8780 prog->expected_attach_type); 8781 } 8782 8783 static int bpf_noop_prologue(struct bpf_insn *insn_buf, bool direct_write, 8784 const struct bpf_prog *prog) 8785 { 8786 /* Neither direct read nor direct write requires any preliminary 8787 * action. 8788 */ 8789 return 0; 8790 } 8791 8792 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write, 8793 const struct bpf_prog *prog, int drop_verdict) 8794 { 8795 struct bpf_insn *insn = insn_buf; 8796 8797 if (!direct_write) 8798 return 0; 8799 8800 /* if (!skb->cloned) 8801 * goto start; 8802 * 8803 * (Fast-path, otherwise approximation that we might be 8804 * a clone, do the rest in helper.) 8805 */ 8806 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET); 8807 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK); 8808 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7); 8809 8810 /* ret = bpf_skb_pull_data(skb, 0); */ 8811 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1); 8812 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2); 8813 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, 8814 BPF_FUNC_skb_pull_data); 8815 /* if (!ret) 8816 * goto restore; 8817 * return TC_ACT_SHOT; 8818 */ 8819 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2); 8820 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict); 8821 *insn++ = BPF_EXIT_INSN(); 8822 8823 /* restore: */ 8824 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6); 8825 /* start: */ 8826 *insn++ = prog->insnsi[0]; 8827 8828 return insn - insn_buf; 8829 } 8830 8831 static int bpf_gen_ld_abs(const struct bpf_insn *orig, 8832 struct bpf_insn *insn_buf) 8833 { 8834 bool indirect = BPF_MODE(orig->code) == BPF_IND; 8835 struct bpf_insn *insn = insn_buf; 8836 8837 if (!indirect) { 8838 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm); 8839 } else { 8840 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg); 8841 if (orig->imm) 8842 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm); 8843 } 8844 /* We're guaranteed here that CTX is in R6. */ 8845 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX); 8846 8847 switch (BPF_SIZE(orig->code)) { 8848 case BPF_B: 8849 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache); 8850 break; 8851 case BPF_H: 8852 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache); 8853 break; 8854 case BPF_W: 8855 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache); 8856 break; 8857 } 8858 8859 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2); 8860 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0); 8861 *insn++ = BPF_EXIT_INSN(); 8862 8863 return insn - insn_buf; 8864 } 8865 8866 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write, 8867 const struct bpf_prog *prog) 8868 { 8869 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT); 8870 } 8871 8872 static bool tc_cls_act_is_valid_access(int off, int size, 8873 enum bpf_access_type type, 8874 const struct bpf_prog *prog, 8875 struct bpf_insn_access_aux *info) 8876 { 8877 if (type == BPF_WRITE) { 8878 switch (off) { 8879 case bpf_ctx_range(struct __sk_buff, mark): 8880 case bpf_ctx_range(struct __sk_buff, tc_index): 8881 case bpf_ctx_range(struct __sk_buff, priority): 8882 case bpf_ctx_range(struct __sk_buff, tc_classid): 8883 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]): 8884 case bpf_ctx_range(struct __sk_buff, tstamp): 8885 case bpf_ctx_range(struct __sk_buff, queue_mapping): 8886 break; 8887 default: 8888 return false; 8889 } 8890 } 8891 8892 switch (off) { 8893 case bpf_ctx_range(struct __sk_buff, data): 8894 info->reg_type = PTR_TO_PACKET; 8895 break; 8896 case bpf_ctx_range(struct __sk_buff, data_meta): 8897 info->reg_type = PTR_TO_PACKET_META; 8898 break; 8899 case bpf_ctx_range(struct __sk_buff, data_end): 8900 info->reg_type = PTR_TO_PACKET_END; 8901 break; 8902 case bpf_ctx_range_till(struct __sk_buff, family, local_port): 8903 return false; 8904 case offsetof(struct __sk_buff, tstamp_type): 8905 /* The convert_ctx_access() on reading and writing 8906 * __sk_buff->tstamp depends on whether the bpf prog 8907 * has used __sk_buff->tstamp_type or not. 8908 * Thus, we need to set prog->tstamp_type_access 8909 * earlier during is_valid_access() here. 8910 */ 8911 ((struct bpf_prog *)prog)->tstamp_type_access = 1; 8912 return size == sizeof(__u8); 8913 } 8914 8915 return bpf_skb_is_valid_access(off, size, type, prog, info); 8916 } 8917 8918 DEFINE_MUTEX(nf_conn_btf_access_lock); 8919 EXPORT_SYMBOL_GPL(nf_conn_btf_access_lock); 8920 8921 int (*nfct_btf_struct_access)(struct bpf_verifier_log *log, 8922 const struct bpf_reg_state *reg, 8923 int off, int size); 8924 EXPORT_SYMBOL_GPL(nfct_btf_struct_access); 8925 8926 static int tc_cls_act_btf_struct_access(struct bpf_verifier_log *log, 8927 const struct bpf_reg_state *reg, 8928 int off, int size) 8929 { 8930 int ret = -EACCES; 8931 8932 mutex_lock(&nf_conn_btf_access_lock); 8933 if (nfct_btf_struct_access) 8934 ret = nfct_btf_struct_access(log, reg, off, size); 8935 mutex_unlock(&nf_conn_btf_access_lock); 8936 8937 return ret; 8938 } 8939 8940 static bool __is_valid_xdp_access(int off, int size) 8941 { 8942 if (off < 0 || off >= sizeof(struct xdp_md)) 8943 return false; 8944 if (off % size != 0) 8945 return false; 8946 if (size != sizeof(__u32)) 8947 return false; 8948 8949 return true; 8950 } 8951 8952 static bool xdp_is_valid_access(int off, int size, 8953 enum bpf_access_type type, 8954 const struct bpf_prog *prog, 8955 struct bpf_insn_access_aux *info) 8956 { 8957 if (prog->expected_attach_type != BPF_XDP_DEVMAP) { 8958 switch (off) { 8959 case offsetof(struct xdp_md, egress_ifindex): 8960 return false; 8961 } 8962 } 8963 8964 if (type == BPF_WRITE) { 8965 if (bpf_prog_is_offloaded(prog->aux)) { 8966 switch (off) { 8967 case offsetof(struct xdp_md, rx_queue_index): 8968 return __is_valid_xdp_access(off, size); 8969 } 8970 } 8971 return false; 8972 } 8973 8974 switch (off) { 8975 case offsetof(struct xdp_md, data): 8976 info->reg_type = PTR_TO_PACKET; 8977 break; 8978 case offsetof(struct xdp_md, data_meta): 8979 info->reg_type = PTR_TO_PACKET_META; 8980 break; 8981 case offsetof(struct xdp_md, data_end): 8982 info->reg_type = PTR_TO_PACKET_END; 8983 break; 8984 } 8985 8986 return __is_valid_xdp_access(off, size); 8987 } 8988 8989 void bpf_warn_invalid_xdp_action(struct net_device *dev, struct bpf_prog *prog, u32 act) 8990 { 8991 const u32 act_max = XDP_REDIRECT; 8992 8993 pr_warn_once("%s XDP return value %u on prog %s (id %d) dev %s, expect packet loss!\n", 8994 act > act_max ? "Illegal" : "Driver unsupported", 8995 act, prog->aux->name, prog->aux->id, dev ? dev->name : "N/A"); 8996 } 8997 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action); 8998 8999 static int xdp_btf_struct_access(struct bpf_verifier_log *log, 9000 const struct bpf_reg_state *reg, 9001 int off, int size) 9002 { 9003 int ret = -EACCES; 9004 9005 mutex_lock(&nf_conn_btf_access_lock); 9006 if (nfct_btf_struct_access) 9007 ret = nfct_btf_struct_access(log, reg, off, size); 9008 mutex_unlock(&nf_conn_btf_access_lock); 9009 9010 return ret; 9011 } 9012 9013 static bool sock_addr_is_valid_access(int off, int size, 9014 enum bpf_access_type type, 9015 const struct bpf_prog *prog, 9016 struct bpf_insn_access_aux *info) 9017 { 9018 const int size_default = sizeof(__u32); 9019 9020 if (off < 0 || off >= sizeof(struct bpf_sock_addr)) 9021 return false; 9022 if (off % size != 0) 9023 return false; 9024 9025 /* Disallow access to IPv6 fields from IPv4 contex and vise 9026 * versa. 9027 */ 9028 switch (off) { 9029 case bpf_ctx_range(struct bpf_sock_addr, user_ip4): 9030 switch (prog->expected_attach_type) { 9031 case BPF_CGROUP_INET4_BIND: 9032 case BPF_CGROUP_INET4_CONNECT: 9033 case BPF_CGROUP_INET4_GETPEERNAME: 9034 case BPF_CGROUP_INET4_GETSOCKNAME: 9035 case BPF_CGROUP_UDP4_SENDMSG: 9036 case BPF_CGROUP_UDP4_RECVMSG: 9037 break; 9038 default: 9039 return false; 9040 } 9041 break; 9042 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]): 9043 switch (prog->expected_attach_type) { 9044 case BPF_CGROUP_INET6_BIND: 9045 case BPF_CGROUP_INET6_CONNECT: 9046 case BPF_CGROUP_INET6_GETPEERNAME: 9047 case BPF_CGROUP_INET6_GETSOCKNAME: 9048 case BPF_CGROUP_UDP6_SENDMSG: 9049 case BPF_CGROUP_UDP6_RECVMSG: 9050 break; 9051 default: 9052 return false; 9053 } 9054 break; 9055 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4): 9056 switch (prog->expected_attach_type) { 9057 case BPF_CGROUP_UDP4_SENDMSG: 9058 break; 9059 default: 9060 return false; 9061 } 9062 break; 9063 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0], 9064 msg_src_ip6[3]): 9065 switch (prog->expected_attach_type) { 9066 case BPF_CGROUP_UDP6_SENDMSG: 9067 break; 9068 default: 9069 return false; 9070 } 9071 break; 9072 } 9073 9074 switch (off) { 9075 case bpf_ctx_range(struct bpf_sock_addr, user_ip4): 9076 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]): 9077 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4): 9078 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0], 9079 msg_src_ip6[3]): 9080 case bpf_ctx_range(struct bpf_sock_addr, user_port): 9081 if (type == BPF_READ) { 9082 bpf_ctx_record_field_size(info, size_default); 9083 9084 if (bpf_ctx_wide_access_ok(off, size, 9085 struct bpf_sock_addr, 9086 user_ip6)) 9087 return true; 9088 9089 if (bpf_ctx_wide_access_ok(off, size, 9090 struct bpf_sock_addr, 9091 msg_src_ip6)) 9092 return true; 9093 9094 if (!bpf_ctx_narrow_access_ok(off, size, size_default)) 9095 return false; 9096 } else { 9097 if (bpf_ctx_wide_access_ok(off, size, 9098 struct bpf_sock_addr, 9099 user_ip6)) 9100 return true; 9101 9102 if (bpf_ctx_wide_access_ok(off, size, 9103 struct bpf_sock_addr, 9104 msg_src_ip6)) 9105 return true; 9106 9107 if (size != size_default) 9108 return false; 9109 } 9110 break; 9111 case offsetof(struct bpf_sock_addr, sk): 9112 if (type != BPF_READ) 9113 return false; 9114 if (size != sizeof(__u64)) 9115 return false; 9116 info->reg_type = PTR_TO_SOCKET; 9117 break; 9118 default: 9119 if (type == BPF_READ) { 9120 if (size != size_default) 9121 return false; 9122 } else { 9123 return false; 9124 } 9125 } 9126 9127 return true; 9128 } 9129 9130 static bool sock_ops_is_valid_access(int off, int size, 9131 enum bpf_access_type type, 9132 const struct bpf_prog *prog, 9133 struct bpf_insn_access_aux *info) 9134 { 9135 const int size_default = sizeof(__u32); 9136 9137 if (off < 0 || off >= sizeof(struct bpf_sock_ops)) 9138 return false; 9139 9140 /* The verifier guarantees that size > 0. */ 9141 if (off % size != 0) 9142 return false; 9143 9144 if (type == BPF_WRITE) { 9145 switch (off) { 9146 case offsetof(struct bpf_sock_ops, reply): 9147 case offsetof(struct bpf_sock_ops, sk_txhash): 9148 if (size != size_default) 9149 return false; 9150 break; 9151 default: 9152 return false; 9153 } 9154 } else { 9155 switch (off) { 9156 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received, 9157 bytes_acked): 9158 if (size != sizeof(__u64)) 9159 return false; 9160 break; 9161 case offsetof(struct bpf_sock_ops, sk): 9162 if (size != sizeof(__u64)) 9163 return false; 9164 info->reg_type = PTR_TO_SOCKET_OR_NULL; 9165 break; 9166 case offsetof(struct bpf_sock_ops, skb_data): 9167 if (size != sizeof(__u64)) 9168 return false; 9169 info->reg_type = PTR_TO_PACKET; 9170 break; 9171 case offsetof(struct bpf_sock_ops, skb_data_end): 9172 if (size != sizeof(__u64)) 9173 return false; 9174 info->reg_type = PTR_TO_PACKET_END; 9175 break; 9176 case offsetof(struct bpf_sock_ops, skb_tcp_flags): 9177 bpf_ctx_record_field_size(info, size_default); 9178 return bpf_ctx_narrow_access_ok(off, size, 9179 size_default); 9180 case offsetof(struct bpf_sock_ops, skb_hwtstamp): 9181 if (size != sizeof(__u64)) 9182 return false; 9183 break; 9184 default: 9185 if (size != size_default) 9186 return false; 9187 break; 9188 } 9189 } 9190 9191 return true; 9192 } 9193 9194 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write, 9195 const struct bpf_prog *prog) 9196 { 9197 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP); 9198 } 9199 9200 static bool sk_skb_is_valid_access(int off, int size, 9201 enum bpf_access_type type, 9202 const struct bpf_prog *prog, 9203 struct bpf_insn_access_aux *info) 9204 { 9205 switch (off) { 9206 case bpf_ctx_range(struct __sk_buff, tc_classid): 9207 case bpf_ctx_range(struct __sk_buff, data_meta): 9208 case bpf_ctx_range(struct __sk_buff, tstamp): 9209 case bpf_ctx_range(struct __sk_buff, wire_len): 9210 case bpf_ctx_range(struct __sk_buff, hwtstamp): 9211 return false; 9212 } 9213 9214 if (type == BPF_WRITE) { 9215 switch (off) { 9216 case bpf_ctx_range(struct __sk_buff, tc_index): 9217 case bpf_ctx_range(struct __sk_buff, priority): 9218 break; 9219 default: 9220 return false; 9221 } 9222 } 9223 9224 switch (off) { 9225 case bpf_ctx_range(struct __sk_buff, mark): 9226 return false; 9227 case bpf_ctx_range(struct __sk_buff, data): 9228 info->reg_type = PTR_TO_PACKET; 9229 break; 9230 case bpf_ctx_range(struct __sk_buff, data_end): 9231 info->reg_type = PTR_TO_PACKET_END; 9232 break; 9233 } 9234 9235 return bpf_skb_is_valid_access(off, size, type, prog, info); 9236 } 9237 9238 static bool sk_msg_is_valid_access(int off, int size, 9239 enum bpf_access_type type, 9240 const struct bpf_prog *prog, 9241 struct bpf_insn_access_aux *info) 9242 { 9243 if (type == BPF_WRITE) 9244 return false; 9245 9246 if (off % size != 0) 9247 return false; 9248 9249 switch (off) { 9250 case offsetof(struct sk_msg_md, data): 9251 info->reg_type = PTR_TO_PACKET; 9252 if (size != sizeof(__u64)) 9253 return false; 9254 break; 9255 case offsetof(struct sk_msg_md, data_end): 9256 info->reg_type = PTR_TO_PACKET_END; 9257 if (size != sizeof(__u64)) 9258 return false; 9259 break; 9260 case offsetof(struct sk_msg_md, sk): 9261 if (size != sizeof(__u64)) 9262 return false; 9263 info->reg_type = PTR_TO_SOCKET; 9264 break; 9265 case bpf_ctx_range(struct sk_msg_md, family): 9266 case bpf_ctx_range(struct sk_msg_md, remote_ip4): 9267 case bpf_ctx_range(struct sk_msg_md, local_ip4): 9268 case bpf_ctx_range_till(struct sk_msg_md, remote_ip6[0], remote_ip6[3]): 9269 case bpf_ctx_range_till(struct sk_msg_md, local_ip6[0], local_ip6[3]): 9270 case bpf_ctx_range(struct sk_msg_md, remote_port): 9271 case bpf_ctx_range(struct sk_msg_md, local_port): 9272 case bpf_ctx_range(struct sk_msg_md, size): 9273 if (size != sizeof(__u32)) 9274 return false; 9275 break; 9276 default: 9277 return false; 9278 } 9279 return true; 9280 } 9281 9282 static bool flow_dissector_is_valid_access(int off, int size, 9283 enum bpf_access_type type, 9284 const struct bpf_prog *prog, 9285 struct bpf_insn_access_aux *info) 9286 { 9287 const int size_default = sizeof(__u32); 9288 9289 if (off < 0 || off >= sizeof(struct __sk_buff)) 9290 return false; 9291 9292 if (type == BPF_WRITE) 9293 return false; 9294 9295 switch (off) { 9296 case bpf_ctx_range(struct __sk_buff, data): 9297 if (size != size_default) 9298 return false; 9299 info->reg_type = PTR_TO_PACKET; 9300 return true; 9301 case bpf_ctx_range(struct __sk_buff, data_end): 9302 if (size != size_default) 9303 return false; 9304 info->reg_type = PTR_TO_PACKET_END; 9305 return true; 9306 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys): 9307 if (size != sizeof(__u64)) 9308 return false; 9309 info->reg_type = PTR_TO_FLOW_KEYS; 9310 return true; 9311 default: 9312 return false; 9313 } 9314 } 9315 9316 static u32 flow_dissector_convert_ctx_access(enum bpf_access_type type, 9317 const struct bpf_insn *si, 9318 struct bpf_insn *insn_buf, 9319 struct bpf_prog *prog, 9320 u32 *target_size) 9321 9322 { 9323 struct bpf_insn *insn = insn_buf; 9324 9325 switch (si->off) { 9326 case offsetof(struct __sk_buff, data): 9327 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data), 9328 si->dst_reg, si->src_reg, 9329 offsetof(struct bpf_flow_dissector, data)); 9330 break; 9331 9332 case offsetof(struct __sk_buff, data_end): 9333 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data_end), 9334 si->dst_reg, si->src_reg, 9335 offsetof(struct bpf_flow_dissector, data_end)); 9336 break; 9337 9338 case offsetof(struct __sk_buff, flow_keys): 9339 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, flow_keys), 9340 si->dst_reg, si->src_reg, 9341 offsetof(struct bpf_flow_dissector, flow_keys)); 9342 break; 9343 } 9344 9345 return insn - insn_buf; 9346 } 9347 9348 static struct bpf_insn *bpf_convert_tstamp_type_read(const struct bpf_insn *si, 9349 struct bpf_insn *insn) 9350 { 9351 __u8 value_reg = si->dst_reg; 9352 __u8 skb_reg = si->src_reg; 9353 /* AX is needed because src_reg and dst_reg could be the same */ 9354 __u8 tmp_reg = BPF_REG_AX; 9355 9356 *insn++ = BPF_LDX_MEM(BPF_B, tmp_reg, skb_reg, 9357 SKB_BF_MONO_TC_OFFSET); 9358 *insn++ = BPF_JMP32_IMM(BPF_JSET, tmp_reg, 9359 SKB_MONO_DELIVERY_TIME_MASK, 2); 9360 *insn++ = BPF_MOV32_IMM(value_reg, BPF_SKB_TSTAMP_UNSPEC); 9361 *insn++ = BPF_JMP_A(1); 9362 *insn++ = BPF_MOV32_IMM(value_reg, BPF_SKB_TSTAMP_DELIVERY_MONO); 9363 9364 return insn; 9365 } 9366 9367 static struct bpf_insn *bpf_convert_shinfo_access(__u8 dst_reg, __u8 skb_reg, 9368 struct bpf_insn *insn) 9369 { 9370 /* si->dst_reg = skb_shinfo(SKB); */ 9371 #ifdef NET_SKBUFF_DATA_USES_OFFSET 9372 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end), 9373 BPF_REG_AX, skb_reg, 9374 offsetof(struct sk_buff, end)); 9375 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, head), 9376 dst_reg, skb_reg, 9377 offsetof(struct sk_buff, head)); 9378 *insn++ = BPF_ALU64_REG(BPF_ADD, dst_reg, BPF_REG_AX); 9379 #else 9380 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end), 9381 dst_reg, skb_reg, 9382 offsetof(struct sk_buff, end)); 9383 #endif 9384 9385 return insn; 9386 } 9387 9388 static struct bpf_insn *bpf_convert_tstamp_read(const struct bpf_prog *prog, 9389 const struct bpf_insn *si, 9390 struct bpf_insn *insn) 9391 { 9392 __u8 value_reg = si->dst_reg; 9393 __u8 skb_reg = si->src_reg; 9394 9395 #ifdef CONFIG_NET_XGRESS 9396 /* If the tstamp_type is read, 9397 * the bpf prog is aware the tstamp could have delivery time. 9398 * Thus, read skb->tstamp as is if tstamp_type_access is true. 9399 */ 9400 if (!prog->tstamp_type_access) { 9401 /* AX is needed because src_reg and dst_reg could be the same */ 9402 __u8 tmp_reg = BPF_REG_AX; 9403 9404 *insn++ = BPF_LDX_MEM(BPF_B, tmp_reg, skb_reg, SKB_BF_MONO_TC_OFFSET); 9405 *insn++ = BPF_ALU32_IMM(BPF_AND, tmp_reg, 9406 TC_AT_INGRESS_MASK | SKB_MONO_DELIVERY_TIME_MASK); 9407 *insn++ = BPF_JMP32_IMM(BPF_JNE, tmp_reg, 9408 TC_AT_INGRESS_MASK | SKB_MONO_DELIVERY_TIME_MASK, 2); 9409 /* skb->tc_at_ingress && skb->mono_delivery_time, 9410 * read 0 as the (rcv) timestamp. 9411 */ 9412 *insn++ = BPF_MOV64_IMM(value_reg, 0); 9413 *insn++ = BPF_JMP_A(1); 9414 } 9415 #endif 9416 9417 *insn++ = BPF_LDX_MEM(BPF_DW, value_reg, skb_reg, 9418 offsetof(struct sk_buff, tstamp)); 9419 return insn; 9420 } 9421 9422 static struct bpf_insn *bpf_convert_tstamp_write(const struct bpf_prog *prog, 9423 const struct bpf_insn *si, 9424 struct bpf_insn *insn) 9425 { 9426 __u8 value_reg = si->src_reg; 9427 __u8 skb_reg = si->dst_reg; 9428 9429 #ifdef CONFIG_NET_XGRESS 9430 /* If the tstamp_type is read, 9431 * the bpf prog is aware the tstamp could have delivery time. 9432 * Thus, write skb->tstamp as is if tstamp_type_access is true. 9433 * Otherwise, writing at ingress will have to clear the 9434 * mono_delivery_time bit also. 9435 */ 9436 if (!prog->tstamp_type_access) { 9437 __u8 tmp_reg = BPF_REG_AX; 9438 9439 *insn++ = BPF_LDX_MEM(BPF_B, tmp_reg, skb_reg, SKB_BF_MONO_TC_OFFSET); 9440 /* Writing __sk_buff->tstamp as ingress, goto <clear> */ 9441 *insn++ = BPF_JMP32_IMM(BPF_JSET, tmp_reg, TC_AT_INGRESS_MASK, 1); 9442 /* goto <store> */ 9443 *insn++ = BPF_JMP_A(2); 9444 /* <clear>: mono_delivery_time */ 9445 *insn++ = BPF_ALU32_IMM(BPF_AND, tmp_reg, ~SKB_MONO_DELIVERY_TIME_MASK); 9446 *insn++ = BPF_STX_MEM(BPF_B, skb_reg, tmp_reg, SKB_BF_MONO_TC_OFFSET); 9447 } 9448 #endif 9449 9450 /* <store>: skb->tstamp = tstamp */ 9451 *insn++ = BPF_RAW_INSN(BPF_CLASS(si->code) | BPF_DW | BPF_MEM, 9452 skb_reg, value_reg, offsetof(struct sk_buff, tstamp), si->imm); 9453 return insn; 9454 } 9455 9456 #define BPF_EMIT_STORE(size, si, off) \ 9457 BPF_RAW_INSN(BPF_CLASS((si)->code) | (size) | BPF_MEM, \ 9458 (si)->dst_reg, (si)->src_reg, (off), (si)->imm) 9459 9460 static u32 bpf_convert_ctx_access(enum bpf_access_type type, 9461 const struct bpf_insn *si, 9462 struct bpf_insn *insn_buf, 9463 struct bpf_prog *prog, u32 *target_size) 9464 { 9465 struct bpf_insn *insn = insn_buf; 9466 int off; 9467 9468 switch (si->off) { 9469 case offsetof(struct __sk_buff, len): 9470 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 9471 bpf_target_off(struct sk_buff, len, 4, 9472 target_size)); 9473 break; 9474 9475 case offsetof(struct __sk_buff, protocol): 9476 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, 9477 bpf_target_off(struct sk_buff, protocol, 2, 9478 target_size)); 9479 break; 9480 9481 case offsetof(struct __sk_buff, vlan_proto): 9482 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, 9483 bpf_target_off(struct sk_buff, vlan_proto, 2, 9484 target_size)); 9485 break; 9486 9487 case offsetof(struct __sk_buff, priority): 9488 if (type == BPF_WRITE) 9489 *insn++ = BPF_EMIT_STORE(BPF_W, si, 9490 bpf_target_off(struct sk_buff, priority, 4, 9491 target_size)); 9492 else 9493 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 9494 bpf_target_off(struct sk_buff, priority, 4, 9495 target_size)); 9496 break; 9497 9498 case offsetof(struct __sk_buff, ingress_ifindex): 9499 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 9500 bpf_target_off(struct sk_buff, skb_iif, 4, 9501 target_size)); 9502 break; 9503 9504 case offsetof(struct __sk_buff, ifindex): 9505 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev), 9506 si->dst_reg, si->src_reg, 9507 offsetof(struct sk_buff, dev)); 9508 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1); 9509 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 9510 bpf_target_off(struct net_device, ifindex, 4, 9511 target_size)); 9512 break; 9513 9514 case offsetof(struct __sk_buff, hash): 9515 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 9516 bpf_target_off(struct sk_buff, hash, 4, 9517 target_size)); 9518 break; 9519 9520 case offsetof(struct __sk_buff, mark): 9521 if (type == BPF_WRITE) 9522 *insn++ = BPF_EMIT_STORE(BPF_W, si, 9523 bpf_target_off(struct sk_buff, mark, 4, 9524 target_size)); 9525 else 9526 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 9527 bpf_target_off(struct sk_buff, mark, 4, 9528 target_size)); 9529 break; 9530 9531 case offsetof(struct __sk_buff, pkt_type): 9532 *target_size = 1; 9533 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg, 9534 PKT_TYPE_OFFSET); 9535 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX); 9536 #ifdef __BIG_ENDIAN_BITFIELD 9537 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5); 9538 #endif 9539 break; 9540 9541 case offsetof(struct __sk_buff, queue_mapping): 9542 if (type == BPF_WRITE) { 9543 u32 off = bpf_target_off(struct sk_buff, queue_mapping, 2, target_size); 9544 9545 if (BPF_CLASS(si->code) == BPF_ST && si->imm >= NO_QUEUE_MAPPING) { 9546 *insn++ = BPF_JMP_A(0); /* noop */ 9547 break; 9548 } 9549 9550 if (BPF_CLASS(si->code) == BPF_STX) 9551 *insn++ = BPF_JMP_IMM(BPF_JGE, si->src_reg, NO_QUEUE_MAPPING, 1); 9552 *insn++ = BPF_EMIT_STORE(BPF_H, si, off); 9553 } else { 9554 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, 9555 bpf_target_off(struct sk_buff, 9556 queue_mapping, 9557 2, target_size)); 9558 } 9559 break; 9560 9561 case offsetof(struct __sk_buff, vlan_present): 9562 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 9563 bpf_target_off(struct sk_buff, 9564 vlan_all, 4, target_size)); 9565 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1); 9566 *insn++ = BPF_ALU32_IMM(BPF_MOV, si->dst_reg, 1); 9567 break; 9568 9569 case offsetof(struct __sk_buff, vlan_tci): 9570 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, 9571 bpf_target_off(struct sk_buff, vlan_tci, 2, 9572 target_size)); 9573 break; 9574 9575 case offsetof(struct __sk_buff, cb[0]) ... 9576 offsetofend(struct __sk_buff, cb[4]) - 1: 9577 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, data) < 20); 9578 BUILD_BUG_ON((offsetof(struct sk_buff, cb) + 9579 offsetof(struct qdisc_skb_cb, data)) % 9580 sizeof(__u64)); 9581 9582 prog->cb_access = 1; 9583 off = si->off; 9584 off -= offsetof(struct __sk_buff, cb[0]); 9585 off += offsetof(struct sk_buff, cb); 9586 off += offsetof(struct qdisc_skb_cb, data); 9587 if (type == BPF_WRITE) 9588 *insn++ = BPF_EMIT_STORE(BPF_SIZE(si->code), si, off); 9589 else 9590 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg, 9591 si->src_reg, off); 9592 break; 9593 9594 case offsetof(struct __sk_buff, tc_classid): 9595 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, tc_classid) != 2); 9596 9597 off = si->off; 9598 off -= offsetof(struct __sk_buff, tc_classid); 9599 off += offsetof(struct sk_buff, cb); 9600 off += offsetof(struct qdisc_skb_cb, tc_classid); 9601 *target_size = 2; 9602 if (type == BPF_WRITE) 9603 *insn++ = BPF_EMIT_STORE(BPF_H, si, off); 9604 else 9605 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, 9606 si->src_reg, off); 9607 break; 9608 9609 case offsetof(struct __sk_buff, data): 9610 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data), 9611 si->dst_reg, si->src_reg, 9612 offsetof(struct sk_buff, data)); 9613 break; 9614 9615 case offsetof(struct __sk_buff, data_meta): 9616 off = si->off; 9617 off -= offsetof(struct __sk_buff, data_meta); 9618 off += offsetof(struct sk_buff, cb); 9619 off += offsetof(struct bpf_skb_data_end, data_meta); 9620 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, 9621 si->src_reg, off); 9622 break; 9623 9624 case offsetof(struct __sk_buff, data_end): 9625 off = si->off; 9626 off -= offsetof(struct __sk_buff, data_end); 9627 off += offsetof(struct sk_buff, cb); 9628 off += offsetof(struct bpf_skb_data_end, data_end); 9629 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, 9630 si->src_reg, off); 9631 break; 9632 9633 case offsetof(struct __sk_buff, tc_index): 9634 #ifdef CONFIG_NET_SCHED 9635 if (type == BPF_WRITE) 9636 *insn++ = BPF_EMIT_STORE(BPF_H, si, 9637 bpf_target_off(struct sk_buff, tc_index, 2, 9638 target_size)); 9639 else 9640 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, 9641 bpf_target_off(struct sk_buff, tc_index, 2, 9642 target_size)); 9643 #else 9644 *target_size = 2; 9645 if (type == BPF_WRITE) 9646 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg); 9647 else 9648 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0); 9649 #endif 9650 break; 9651 9652 case offsetof(struct __sk_buff, napi_id): 9653 #if defined(CONFIG_NET_RX_BUSY_POLL) 9654 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 9655 bpf_target_off(struct sk_buff, napi_id, 4, 9656 target_size)); 9657 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1); 9658 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0); 9659 #else 9660 *target_size = 4; 9661 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0); 9662 #endif 9663 break; 9664 case offsetof(struct __sk_buff, family): 9665 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2); 9666 9667 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), 9668 si->dst_reg, si->src_reg, 9669 offsetof(struct sk_buff, sk)); 9670 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 9671 bpf_target_off(struct sock_common, 9672 skc_family, 9673 2, target_size)); 9674 break; 9675 case offsetof(struct __sk_buff, remote_ip4): 9676 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4); 9677 9678 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), 9679 si->dst_reg, si->src_reg, 9680 offsetof(struct sk_buff, sk)); 9681 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 9682 bpf_target_off(struct sock_common, 9683 skc_daddr, 9684 4, target_size)); 9685 break; 9686 case offsetof(struct __sk_buff, local_ip4): 9687 BUILD_BUG_ON(sizeof_field(struct sock_common, 9688 skc_rcv_saddr) != 4); 9689 9690 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), 9691 si->dst_reg, si->src_reg, 9692 offsetof(struct sk_buff, sk)); 9693 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 9694 bpf_target_off(struct sock_common, 9695 skc_rcv_saddr, 9696 4, target_size)); 9697 break; 9698 case offsetof(struct __sk_buff, remote_ip6[0]) ... 9699 offsetof(struct __sk_buff, remote_ip6[3]): 9700 #if IS_ENABLED(CONFIG_IPV6) 9701 BUILD_BUG_ON(sizeof_field(struct sock_common, 9702 skc_v6_daddr.s6_addr32[0]) != 4); 9703 9704 off = si->off; 9705 off -= offsetof(struct __sk_buff, remote_ip6[0]); 9706 9707 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), 9708 si->dst_reg, si->src_reg, 9709 offsetof(struct sk_buff, sk)); 9710 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 9711 offsetof(struct sock_common, 9712 skc_v6_daddr.s6_addr32[0]) + 9713 off); 9714 #else 9715 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 9716 #endif 9717 break; 9718 case offsetof(struct __sk_buff, local_ip6[0]) ... 9719 offsetof(struct __sk_buff, local_ip6[3]): 9720 #if IS_ENABLED(CONFIG_IPV6) 9721 BUILD_BUG_ON(sizeof_field(struct sock_common, 9722 skc_v6_rcv_saddr.s6_addr32[0]) != 4); 9723 9724 off = si->off; 9725 off -= offsetof(struct __sk_buff, local_ip6[0]); 9726 9727 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), 9728 si->dst_reg, si->src_reg, 9729 offsetof(struct sk_buff, sk)); 9730 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 9731 offsetof(struct sock_common, 9732 skc_v6_rcv_saddr.s6_addr32[0]) + 9733 off); 9734 #else 9735 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 9736 #endif 9737 break; 9738 9739 case offsetof(struct __sk_buff, remote_port): 9740 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2); 9741 9742 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), 9743 si->dst_reg, si->src_reg, 9744 offsetof(struct sk_buff, sk)); 9745 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 9746 bpf_target_off(struct sock_common, 9747 skc_dport, 9748 2, target_size)); 9749 #ifndef __BIG_ENDIAN_BITFIELD 9750 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16); 9751 #endif 9752 break; 9753 9754 case offsetof(struct __sk_buff, local_port): 9755 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2); 9756 9757 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), 9758 si->dst_reg, si->src_reg, 9759 offsetof(struct sk_buff, sk)); 9760 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 9761 bpf_target_off(struct sock_common, 9762 skc_num, 2, target_size)); 9763 break; 9764 9765 case offsetof(struct __sk_buff, tstamp): 9766 BUILD_BUG_ON(sizeof_field(struct sk_buff, tstamp) != 8); 9767 9768 if (type == BPF_WRITE) 9769 insn = bpf_convert_tstamp_write(prog, si, insn); 9770 else 9771 insn = bpf_convert_tstamp_read(prog, si, insn); 9772 break; 9773 9774 case offsetof(struct __sk_buff, tstamp_type): 9775 insn = bpf_convert_tstamp_type_read(si, insn); 9776 break; 9777 9778 case offsetof(struct __sk_buff, gso_segs): 9779 insn = bpf_convert_shinfo_access(si->dst_reg, si->src_reg, insn); 9780 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_segs), 9781 si->dst_reg, si->dst_reg, 9782 bpf_target_off(struct skb_shared_info, 9783 gso_segs, 2, 9784 target_size)); 9785 break; 9786 case offsetof(struct __sk_buff, gso_size): 9787 insn = bpf_convert_shinfo_access(si->dst_reg, si->src_reg, insn); 9788 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_size), 9789 si->dst_reg, si->dst_reg, 9790 bpf_target_off(struct skb_shared_info, 9791 gso_size, 2, 9792 target_size)); 9793 break; 9794 case offsetof(struct __sk_buff, wire_len): 9795 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, pkt_len) != 4); 9796 9797 off = si->off; 9798 off -= offsetof(struct __sk_buff, wire_len); 9799 off += offsetof(struct sk_buff, cb); 9800 off += offsetof(struct qdisc_skb_cb, pkt_len); 9801 *target_size = 4; 9802 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, off); 9803 break; 9804 9805 case offsetof(struct __sk_buff, sk): 9806 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), 9807 si->dst_reg, si->src_reg, 9808 offsetof(struct sk_buff, sk)); 9809 break; 9810 case offsetof(struct __sk_buff, hwtstamp): 9811 BUILD_BUG_ON(sizeof_field(struct skb_shared_hwtstamps, hwtstamp) != 8); 9812 BUILD_BUG_ON(offsetof(struct skb_shared_hwtstamps, hwtstamp) != 0); 9813 9814 insn = bpf_convert_shinfo_access(si->dst_reg, si->src_reg, insn); 9815 *insn++ = BPF_LDX_MEM(BPF_DW, 9816 si->dst_reg, si->dst_reg, 9817 bpf_target_off(struct skb_shared_info, 9818 hwtstamps, 8, 9819 target_size)); 9820 break; 9821 } 9822 9823 return insn - insn_buf; 9824 } 9825 9826 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type, 9827 const struct bpf_insn *si, 9828 struct bpf_insn *insn_buf, 9829 struct bpf_prog *prog, u32 *target_size) 9830 { 9831 struct bpf_insn *insn = insn_buf; 9832 int off; 9833 9834 switch (si->off) { 9835 case offsetof(struct bpf_sock, bound_dev_if): 9836 BUILD_BUG_ON(sizeof_field(struct sock, sk_bound_dev_if) != 4); 9837 9838 if (type == BPF_WRITE) 9839 *insn++ = BPF_EMIT_STORE(BPF_W, si, 9840 offsetof(struct sock, sk_bound_dev_if)); 9841 else 9842 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 9843 offsetof(struct sock, sk_bound_dev_if)); 9844 break; 9845 9846 case offsetof(struct bpf_sock, mark): 9847 BUILD_BUG_ON(sizeof_field(struct sock, sk_mark) != 4); 9848 9849 if (type == BPF_WRITE) 9850 *insn++ = BPF_EMIT_STORE(BPF_W, si, 9851 offsetof(struct sock, sk_mark)); 9852 else 9853 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 9854 offsetof(struct sock, sk_mark)); 9855 break; 9856 9857 case offsetof(struct bpf_sock, priority): 9858 BUILD_BUG_ON(sizeof_field(struct sock, sk_priority) != 4); 9859 9860 if (type == BPF_WRITE) 9861 *insn++ = BPF_EMIT_STORE(BPF_W, si, 9862 offsetof(struct sock, sk_priority)); 9863 else 9864 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 9865 offsetof(struct sock, sk_priority)); 9866 break; 9867 9868 case offsetof(struct bpf_sock, family): 9869 *insn++ = BPF_LDX_MEM( 9870 BPF_FIELD_SIZEOF(struct sock_common, skc_family), 9871 si->dst_reg, si->src_reg, 9872 bpf_target_off(struct sock_common, 9873 skc_family, 9874 sizeof_field(struct sock_common, 9875 skc_family), 9876 target_size)); 9877 break; 9878 9879 case offsetof(struct bpf_sock, type): 9880 *insn++ = BPF_LDX_MEM( 9881 BPF_FIELD_SIZEOF(struct sock, sk_type), 9882 si->dst_reg, si->src_reg, 9883 bpf_target_off(struct sock, sk_type, 9884 sizeof_field(struct sock, sk_type), 9885 target_size)); 9886 break; 9887 9888 case offsetof(struct bpf_sock, protocol): 9889 *insn++ = BPF_LDX_MEM( 9890 BPF_FIELD_SIZEOF(struct sock, sk_protocol), 9891 si->dst_reg, si->src_reg, 9892 bpf_target_off(struct sock, sk_protocol, 9893 sizeof_field(struct sock, sk_protocol), 9894 target_size)); 9895 break; 9896 9897 case offsetof(struct bpf_sock, src_ip4): 9898 *insn++ = BPF_LDX_MEM( 9899 BPF_SIZE(si->code), si->dst_reg, si->src_reg, 9900 bpf_target_off(struct sock_common, skc_rcv_saddr, 9901 sizeof_field(struct sock_common, 9902 skc_rcv_saddr), 9903 target_size)); 9904 break; 9905 9906 case offsetof(struct bpf_sock, dst_ip4): 9907 *insn++ = BPF_LDX_MEM( 9908 BPF_SIZE(si->code), si->dst_reg, si->src_reg, 9909 bpf_target_off(struct sock_common, skc_daddr, 9910 sizeof_field(struct sock_common, 9911 skc_daddr), 9912 target_size)); 9913 break; 9914 9915 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]): 9916 #if IS_ENABLED(CONFIG_IPV6) 9917 off = si->off; 9918 off -= offsetof(struct bpf_sock, src_ip6[0]); 9919 *insn++ = BPF_LDX_MEM( 9920 BPF_SIZE(si->code), si->dst_reg, si->src_reg, 9921 bpf_target_off( 9922 struct sock_common, 9923 skc_v6_rcv_saddr.s6_addr32[0], 9924 sizeof_field(struct sock_common, 9925 skc_v6_rcv_saddr.s6_addr32[0]), 9926 target_size) + off); 9927 #else 9928 (void)off; 9929 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 9930 #endif 9931 break; 9932 9933 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]): 9934 #if IS_ENABLED(CONFIG_IPV6) 9935 off = si->off; 9936 off -= offsetof(struct bpf_sock, dst_ip6[0]); 9937 *insn++ = BPF_LDX_MEM( 9938 BPF_SIZE(si->code), si->dst_reg, si->src_reg, 9939 bpf_target_off(struct sock_common, 9940 skc_v6_daddr.s6_addr32[0], 9941 sizeof_field(struct sock_common, 9942 skc_v6_daddr.s6_addr32[0]), 9943 target_size) + off); 9944 #else 9945 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 9946 *target_size = 4; 9947 #endif 9948 break; 9949 9950 case offsetof(struct bpf_sock, src_port): 9951 *insn++ = BPF_LDX_MEM( 9952 BPF_FIELD_SIZEOF(struct sock_common, skc_num), 9953 si->dst_reg, si->src_reg, 9954 bpf_target_off(struct sock_common, skc_num, 9955 sizeof_field(struct sock_common, 9956 skc_num), 9957 target_size)); 9958 break; 9959 9960 case offsetof(struct bpf_sock, dst_port): 9961 *insn++ = BPF_LDX_MEM( 9962 BPF_FIELD_SIZEOF(struct sock_common, skc_dport), 9963 si->dst_reg, si->src_reg, 9964 bpf_target_off(struct sock_common, skc_dport, 9965 sizeof_field(struct sock_common, 9966 skc_dport), 9967 target_size)); 9968 break; 9969 9970 case offsetof(struct bpf_sock, state): 9971 *insn++ = BPF_LDX_MEM( 9972 BPF_FIELD_SIZEOF(struct sock_common, skc_state), 9973 si->dst_reg, si->src_reg, 9974 bpf_target_off(struct sock_common, skc_state, 9975 sizeof_field(struct sock_common, 9976 skc_state), 9977 target_size)); 9978 break; 9979 case offsetof(struct bpf_sock, rx_queue_mapping): 9980 #ifdef CONFIG_SOCK_RX_QUEUE_MAPPING 9981 *insn++ = BPF_LDX_MEM( 9982 BPF_FIELD_SIZEOF(struct sock, sk_rx_queue_mapping), 9983 si->dst_reg, si->src_reg, 9984 bpf_target_off(struct sock, sk_rx_queue_mapping, 9985 sizeof_field(struct sock, 9986 sk_rx_queue_mapping), 9987 target_size)); 9988 *insn++ = BPF_JMP_IMM(BPF_JNE, si->dst_reg, NO_QUEUE_MAPPING, 9989 1); 9990 *insn++ = BPF_MOV64_IMM(si->dst_reg, -1); 9991 #else 9992 *insn++ = BPF_MOV64_IMM(si->dst_reg, -1); 9993 *target_size = 2; 9994 #endif 9995 break; 9996 } 9997 9998 return insn - insn_buf; 9999 } 10000 10001 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type, 10002 const struct bpf_insn *si, 10003 struct bpf_insn *insn_buf, 10004 struct bpf_prog *prog, u32 *target_size) 10005 { 10006 struct bpf_insn *insn = insn_buf; 10007 10008 switch (si->off) { 10009 case offsetof(struct __sk_buff, ifindex): 10010 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev), 10011 si->dst_reg, si->src_reg, 10012 offsetof(struct sk_buff, dev)); 10013 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 10014 bpf_target_off(struct net_device, ifindex, 4, 10015 target_size)); 10016 break; 10017 default: 10018 return bpf_convert_ctx_access(type, si, insn_buf, prog, 10019 target_size); 10020 } 10021 10022 return insn - insn_buf; 10023 } 10024 10025 static u32 xdp_convert_ctx_access(enum bpf_access_type type, 10026 const struct bpf_insn *si, 10027 struct bpf_insn *insn_buf, 10028 struct bpf_prog *prog, u32 *target_size) 10029 { 10030 struct bpf_insn *insn = insn_buf; 10031 10032 switch (si->off) { 10033 case offsetof(struct xdp_md, data): 10034 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data), 10035 si->dst_reg, si->src_reg, 10036 offsetof(struct xdp_buff, data)); 10037 break; 10038 case offsetof(struct xdp_md, data_meta): 10039 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta), 10040 si->dst_reg, si->src_reg, 10041 offsetof(struct xdp_buff, data_meta)); 10042 break; 10043 case offsetof(struct xdp_md, data_end): 10044 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end), 10045 si->dst_reg, si->src_reg, 10046 offsetof(struct xdp_buff, data_end)); 10047 break; 10048 case offsetof(struct xdp_md, ingress_ifindex): 10049 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq), 10050 si->dst_reg, si->src_reg, 10051 offsetof(struct xdp_buff, rxq)); 10052 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev), 10053 si->dst_reg, si->dst_reg, 10054 offsetof(struct xdp_rxq_info, dev)); 10055 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 10056 offsetof(struct net_device, ifindex)); 10057 break; 10058 case offsetof(struct xdp_md, rx_queue_index): 10059 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq), 10060 si->dst_reg, si->src_reg, 10061 offsetof(struct xdp_buff, rxq)); 10062 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 10063 offsetof(struct xdp_rxq_info, 10064 queue_index)); 10065 break; 10066 case offsetof(struct xdp_md, egress_ifindex): 10067 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, txq), 10068 si->dst_reg, si->src_reg, 10069 offsetof(struct xdp_buff, txq)); 10070 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_txq_info, dev), 10071 si->dst_reg, si->dst_reg, 10072 offsetof(struct xdp_txq_info, dev)); 10073 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 10074 offsetof(struct net_device, ifindex)); 10075 break; 10076 } 10077 10078 return insn - insn_buf; 10079 } 10080 10081 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of 10082 * context Structure, F is Field in context structure that contains a pointer 10083 * to Nested Structure of type NS that has the field NF. 10084 * 10085 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make 10086 * sure that SIZE is not greater than actual size of S.F.NF. 10087 * 10088 * If offset OFF is provided, the load happens from that offset relative to 10089 * offset of NF. 10090 */ 10091 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \ 10092 do { \ 10093 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \ 10094 si->src_reg, offsetof(S, F)); \ 10095 *insn++ = BPF_LDX_MEM( \ 10096 SIZE, si->dst_reg, si->dst_reg, \ 10097 bpf_target_off(NS, NF, sizeof_field(NS, NF), \ 10098 target_size) \ 10099 + OFF); \ 10100 } while (0) 10101 10102 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \ 10103 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \ 10104 BPF_FIELD_SIZEOF(NS, NF), 0) 10105 10106 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to 10107 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation. 10108 * 10109 * In addition it uses Temporary Field TF (member of struct S) as the 3rd 10110 * "register" since two registers available in convert_ctx_access are not 10111 * enough: we can't override neither SRC, since it contains value to store, nor 10112 * DST since it contains pointer to context that may be used by later 10113 * instructions. But we need a temporary place to save pointer to nested 10114 * structure whose field we want to store to. 10115 */ 10116 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, OFF, TF) \ 10117 do { \ 10118 int tmp_reg = BPF_REG_9; \ 10119 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \ 10120 --tmp_reg; \ 10121 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \ 10122 --tmp_reg; \ 10123 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \ 10124 offsetof(S, TF)); \ 10125 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \ 10126 si->dst_reg, offsetof(S, F)); \ 10127 *insn++ = BPF_RAW_INSN(SIZE | BPF_MEM | BPF_CLASS(si->code), \ 10128 tmp_reg, si->src_reg, \ 10129 bpf_target_off(NS, NF, sizeof_field(NS, NF), \ 10130 target_size) \ 10131 + OFF, \ 10132 si->imm); \ 10133 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \ 10134 offsetof(S, TF)); \ 10135 } while (0) 10136 10137 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \ 10138 TF) \ 10139 do { \ 10140 if (type == BPF_WRITE) { \ 10141 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, \ 10142 OFF, TF); \ 10143 } else { \ 10144 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \ 10145 S, NS, F, NF, SIZE, OFF); \ 10146 } \ 10147 } while (0) 10148 10149 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \ 10150 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \ 10151 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF) 10152 10153 static u32 sock_addr_convert_ctx_access(enum bpf_access_type type, 10154 const struct bpf_insn *si, 10155 struct bpf_insn *insn_buf, 10156 struct bpf_prog *prog, u32 *target_size) 10157 { 10158 int off, port_size = sizeof_field(struct sockaddr_in6, sin6_port); 10159 struct bpf_insn *insn = insn_buf; 10160 10161 switch (si->off) { 10162 case offsetof(struct bpf_sock_addr, user_family): 10163 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern, 10164 struct sockaddr, uaddr, sa_family); 10165 break; 10166 10167 case offsetof(struct bpf_sock_addr, user_ip4): 10168 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( 10169 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr, 10170 sin_addr, BPF_SIZE(si->code), 0, tmp_reg); 10171 break; 10172 10173 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]): 10174 off = si->off; 10175 off -= offsetof(struct bpf_sock_addr, user_ip6[0]); 10176 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( 10177 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr, 10178 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off, 10179 tmp_reg); 10180 break; 10181 10182 case offsetof(struct bpf_sock_addr, user_port): 10183 /* To get port we need to know sa_family first and then treat 10184 * sockaddr as either sockaddr_in or sockaddr_in6. 10185 * Though we can simplify since port field has same offset and 10186 * size in both structures. 10187 * Here we check this invariant and use just one of the 10188 * structures if it's true. 10189 */ 10190 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) != 10191 offsetof(struct sockaddr_in6, sin6_port)); 10192 BUILD_BUG_ON(sizeof_field(struct sockaddr_in, sin_port) != 10193 sizeof_field(struct sockaddr_in6, sin6_port)); 10194 /* Account for sin6_port being smaller than user_port. */ 10195 port_size = min(port_size, BPF_LDST_BYTES(si)); 10196 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( 10197 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr, 10198 sin6_port, bytes_to_bpf_size(port_size), 0, tmp_reg); 10199 break; 10200 10201 case offsetof(struct bpf_sock_addr, family): 10202 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern, 10203 struct sock, sk, sk_family); 10204 break; 10205 10206 case offsetof(struct bpf_sock_addr, type): 10207 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern, 10208 struct sock, sk, sk_type); 10209 break; 10210 10211 case offsetof(struct bpf_sock_addr, protocol): 10212 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern, 10213 struct sock, sk, sk_protocol); 10214 break; 10215 10216 case offsetof(struct bpf_sock_addr, msg_src_ip4): 10217 /* Treat t_ctx as struct in_addr for msg_src_ip4. */ 10218 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( 10219 struct bpf_sock_addr_kern, struct in_addr, t_ctx, 10220 s_addr, BPF_SIZE(si->code), 0, tmp_reg); 10221 break; 10222 10223 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0], 10224 msg_src_ip6[3]): 10225 off = si->off; 10226 off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]); 10227 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */ 10228 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( 10229 struct bpf_sock_addr_kern, struct in6_addr, t_ctx, 10230 s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg); 10231 break; 10232 case offsetof(struct bpf_sock_addr, sk): 10233 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_addr_kern, sk), 10234 si->dst_reg, si->src_reg, 10235 offsetof(struct bpf_sock_addr_kern, sk)); 10236 break; 10237 } 10238 10239 return insn - insn_buf; 10240 } 10241 10242 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type, 10243 const struct bpf_insn *si, 10244 struct bpf_insn *insn_buf, 10245 struct bpf_prog *prog, 10246 u32 *target_size) 10247 { 10248 struct bpf_insn *insn = insn_buf; 10249 int off; 10250 10251 /* Helper macro for adding read access to tcp_sock or sock fields. */ 10252 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \ 10253 do { \ 10254 int fullsock_reg = si->dst_reg, reg = BPF_REG_9, jmp = 2; \ 10255 BUILD_BUG_ON(sizeof_field(OBJ, OBJ_FIELD) > \ 10256 sizeof_field(struct bpf_sock_ops, BPF_FIELD)); \ 10257 if (si->dst_reg == reg || si->src_reg == reg) \ 10258 reg--; \ 10259 if (si->dst_reg == reg || si->src_reg == reg) \ 10260 reg--; \ 10261 if (si->dst_reg == si->src_reg) { \ 10262 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, \ 10263 offsetof(struct bpf_sock_ops_kern, \ 10264 temp)); \ 10265 fullsock_reg = reg; \ 10266 jmp += 2; \ 10267 } \ 10268 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ 10269 struct bpf_sock_ops_kern, \ 10270 is_fullsock), \ 10271 fullsock_reg, si->src_reg, \ 10272 offsetof(struct bpf_sock_ops_kern, \ 10273 is_fullsock)); \ 10274 *insn++ = BPF_JMP_IMM(BPF_JEQ, fullsock_reg, 0, jmp); \ 10275 if (si->dst_reg == si->src_reg) \ 10276 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \ 10277 offsetof(struct bpf_sock_ops_kern, \ 10278 temp)); \ 10279 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ 10280 struct bpf_sock_ops_kern, sk),\ 10281 si->dst_reg, si->src_reg, \ 10282 offsetof(struct bpf_sock_ops_kern, sk));\ 10283 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \ 10284 OBJ_FIELD), \ 10285 si->dst_reg, si->dst_reg, \ 10286 offsetof(OBJ, OBJ_FIELD)); \ 10287 if (si->dst_reg == si->src_reg) { \ 10288 *insn++ = BPF_JMP_A(1); \ 10289 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \ 10290 offsetof(struct bpf_sock_ops_kern, \ 10291 temp)); \ 10292 } \ 10293 } while (0) 10294 10295 #define SOCK_OPS_GET_SK() \ 10296 do { \ 10297 int fullsock_reg = si->dst_reg, reg = BPF_REG_9, jmp = 1; \ 10298 if (si->dst_reg == reg || si->src_reg == reg) \ 10299 reg--; \ 10300 if (si->dst_reg == reg || si->src_reg == reg) \ 10301 reg--; \ 10302 if (si->dst_reg == si->src_reg) { \ 10303 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, \ 10304 offsetof(struct bpf_sock_ops_kern, \ 10305 temp)); \ 10306 fullsock_reg = reg; \ 10307 jmp += 2; \ 10308 } \ 10309 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ 10310 struct bpf_sock_ops_kern, \ 10311 is_fullsock), \ 10312 fullsock_reg, si->src_reg, \ 10313 offsetof(struct bpf_sock_ops_kern, \ 10314 is_fullsock)); \ 10315 *insn++ = BPF_JMP_IMM(BPF_JEQ, fullsock_reg, 0, jmp); \ 10316 if (si->dst_reg == si->src_reg) \ 10317 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \ 10318 offsetof(struct bpf_sock_ops_kern, \ 10319 temp)); \ 10320 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ 10321 struct bpf_sock_ops_kern, sk),\ 10322 si->dst_reg, si->src_reg, \ 10323 offsetof(struct bpf_sock_ops_kern, sk));\ 10324 if (si->dst_reg == si->src_reg) { \ 10325 *insn++ = BPF_JMP_A(1); \ 10326 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \ 10327 offsetof(struct bpf_sock_ops_kern, \ 10328 temp)); \ 10329 } \ 10330 } while (0) 10331 10332 #define SOCK_OPS_GET_TCP_SOCK_FIELD(FIELD) \ 10333 SOCK_OPS_GET_FIELD(FIELD, FIELD, struct tcp_sock) 10334 10335 /* Helper macro for adding write access to tcp_sock or sock fields. 10336 * The macro is called with two registers, dst_reg which contains a pointer 10337 * to ctx (context) and src_reg which contains the value that should be 10338 * stored. However, we need an additional register since we cannot overwrite 10339 * dst_reg because it may be used later in the program. 10340 * Instead we "borrow" one of the other register. We first save its value 10341 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore 10342 * it at the end of the macro. 10343 */ 10344 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \ 10345 do { \ 10346 int reg = BPF_REG_9; \ 10347 BUILD_BUG_ON(sizeof_field(OBJ, OBJ_FIELD) > \ 10348 sizeof_field(struct bpf_sock_ops, BPF_FIELD)); \ 10349 if (si->dst_reg == reg || si->src_reg == reg) \ 10350 reg--; \ 10351 if (si->dst_reg == reg || si->src_reg == reg) \ 10352 reg--; \ 10353 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \ 10354 offsetof(struct bpf_sock_ops_kern, \ 10355 temp)); \ 10356 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ 10357 struct bpf_sock_ops_kern, \ 10358 is_fullsock), \ 10359 reg, si->dst_reg, \ 10360 offsetof(struct bpf_sock_ops_kern, \ 10361 is_fullsock)); \ 10362 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \ 10363 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ 10364 struct bpf_sock_ops_kern, sk),\ 10365 reg, si->dst_reg, \ 10366 offsetof(struct bpf_sock_ops_kern, sk));\ 10367 *insn++ = BPF_RAW_INSN(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD) | \ 10368 BPF_MEM | BPF_CLASS(si->code), \ 10369 reg, si->src_reg, \ 10370 offsetof(OBJ, OBJ_FIELD), \ 10371 si->imm); \ 10372 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \ 10373 offsetof(struct bpf_sock_ops_kern, \ 10374 temp)); \ 10375 } while (0) 10376 10377 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \ 10378 do { \ 10379 if (TYPE == BPF_WRITE) \ 10380 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \ 10381 else \ 10382 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \ 10383 } while (0) 10384 10385 switch (si->off) { 10386 case offsetof(struct bpf_sock_ops, op): 10387 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern, 10388 op), 10389 si->dst_reg, si->src_reg, 10390 offsetof(struct bpf_sock_ops_kern, op)); 10391 break; 10392 10393 case offsetof(struct bpf_sock_ops, replylong[0]) ... 10394 offsetof(struct bpf_sock_ops, replylong[3]): 10395 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, reply) != 10396 sizeof_field(struct bpf_sock_ops_kern, reply)); 10397 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, replylong) != 10398 sizeof_field(struct bpf_sock_ops_kern, replylong)); 10399 off = si->off; 10400 off -= offsetof(struct bpf_sock_ops, replylong[0]); 10401 off += offsetof(struct bpf_sock_ops_kern, replylong[0]); 10402 if (type == BPF_WRITE) 10403 *insn++ = BPF_EMIT_STORE(BPF_W, si, off); 10404 else 10405 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 10406 off); 10407 break; 10408 10409 case offsetof(struct bpf_sock_ops, family): 10410 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2); 10411 10412 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 10413 struct bpf_sock_ops_kern, sk), 10414 si->dst_reg, si->src_reg, 10415 offsetof(struct bpf_sock_ops_kern, sk)); 10416 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 10417 offsetof(struct sock_common, skc_family)); 10418 break; 10419 10420 case offsetof(struct bpf_sock_ops, remote_ip4): 10421 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4); 10422 10423 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 10424 struct bpf_sock_ops_kern, sk), 10425 si->dst_reg, si->src_reg, 10426 offsetof(struct bpf_sock_ops_kern, sk)); 10427 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 10428 offsetof(struct sock_common, skc_daddr)); 10429 break; 10430 10431 case offsetof(struct bpf_sock_ops, local_ip4): 10432 BUILD_BUG_ON(sizeof_field(struct sock_common, 10433 skc_rcv_saddr) != 4); 10434 10435 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 10436 struct bpf_sock_ops_kern, sk), 10437 si->dst_reg, si->src_reg, 10438 offsetof(struct bpf_sock_ops_kern, sk)); 10439 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 10440 offsetof(struct sock_common, 10441 skc_rcv_saddr)); 10442 break; 10443 10444 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ... 10445 offsetof(struct bpf_sock_ops, remote_ip6[3]): 10446 #if IS_ENABLED(CONFIG_IPV6) 10447 BUILD_BUG_ON(sizeof_field(struct sock_common, 10448 skc_v6_daddr.s6_addr32[0]) != 4); 10449 10450 off = si->off; 10451 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]); 10452 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 10453 struct bpf_sock_ops_kern, sk), 10454 si->dst_reg, si->src_reg, 10455 offsetof(struct bpf_sock_ops_kern, sk)); 10456 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 10457 offsetof(struct sock_common, 10458 skc_v6_daddr.s6_addr32[0]) + 10459 off); 10460 #else 10461 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 10462 #endif 10463 break; 10464 10465 case offsetof(struct bpf_sock_ops, local_ip6[0]) ... 10466 offsetof(struct bpf_sock_ops, local_ip6[3]): 10467 #if IS_ENABLED(CONFIG_IPV6) 10468 BUILD_BUG_ON(sizeof_field(struct sock_common, 10469 skc_v6_rcv_saddr.s6_addr32[0]) != 4); 10470 10471 off = si->off; 10472 off -= offsetof(struct bpf_sock_ops, local_ip6[0]); 10473 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 10474 struct bpf_sock_ops_kern, sk), 10475 si->dst_reg, si->src_reg, 10476 offsetof(struct bpf_sock_ops_kern, sk)); 10477 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 10478 offsetof(struct sock_common, 10479 skc_v6_rcv_saddr.s6_addr32[0]) + 10480 off); 10481 #else 10482 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 10483 #endif 10484 break; 10485 10486 case offsetof(struct bpf_sock_ops, remote_port): 10487 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2); 10488 10489 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 10490 struct bpf_sock_ops_kern, sk), 10491 si->dst_reg, si->src_reg, 10492 offsetof(struct bpf_sock_ops_kern, sk)); 10493 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 10494 offsetof(struct sock_common, skc_dport)); 10495 #ifndef __BIG_ENDIAN_BITFIELD 10496 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16); 10497 #endif 10498 break; 10499 10500 case offsetof(struct bpf_sock_ops, local_port): 10501 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2); 10502 10503 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 10504 struct bpf_sock_ops_kern, sk), 10505 si->dst_reg, si->src_reg, 10506 offsetof(struct bpf_sock_ops_kern, sk)); 10507 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 10508 offsetof(struct sock_common, skc_num)); 10509 break; 10510 10511 case offsetof(struct bpf_sock_ops, is_fullsock): 10512 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 10513 struct bpf_sock_ops_kern, 10514 is_fullsock), 10515 si->dst_reg, si->src_reg, 10516 offsetof(struct bpf_sock_ops_kern, 10517 is_fullsock)); 10518 break; 10519 10520 case offsetof(struct bpf_sock_ops, state): 10521 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_state) != 1); 10522 10523 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 10524 struct bpf_sock_ops_kern, sk), 10525 si->dst_reg, si->src_reg, 10526 offsetof(struct bpf_sock_ops_kern, sk)); 10527 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg, 10528 offsetof(struct sock_common, skc_state)); 10529 break; 10530 10531 case offsetof(struct bpf_sock_ops, rtt_min): 10532 BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) != 10533 sizeof(struct minmax)); 10534 BUILD_BUG_ON(sizeof(struct minmax) < 10535 sizeof(struct minmax_sample)); 10536 10537 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 10538 struct bpf_sock_ops_kern, sk), 10539 si->dst_reg, si->src_reg, 10540 offsetof(struct bpf_sock_ops_kern, sk)); 10541 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 10542 offsetof(struct tcp_sock, rtt_min) + 10543 sizeof_field(struct minmax_sample, t)); 10544 break; 10545 10546 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags): 10547 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags, 10548 struct tcp_sock); 10549 break; 10550 10551 case offsetof(struct bpf_sock_ops, sk_txhash): 10552 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash, 10553 struct sock, type); 10554 break; 10555 case offsetof(struct bpf_sock_ops, snd_cwnd): 10556 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_cwnd); 10557 break; 10558 case offsetof(struct bpf_sock_ops, srtt_us): 10559 SOCK_OPS_GET_TCP_SOCK_FIELD(srtt_us); 10560 break; 10561 case offsetof(struct bpf_sock_ops, snd_ssthresh): 10562 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_ssthresh); 10563 break; 10564 case offsetof(struct bpf_sock_ops, rcv_nxt): 10565 SOCK_OPS_GET_TCP_SOCK_FIELD(rcv_nxt); 10566 break; 10567 case offsetof(struct bpf_sock_ops, snd_nxt): 10568 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_nxt); 10569 break; 10570 case offsetof(struct bpf_sock_ops, snd_una): 10571 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_una); 10572 break; 10573 case offsetof(struct bpf_sock_ops, mss_cache): 10574 SOCK_OPS_GET_TCP_SOCK_FIELD(mss_cache); 10575 break; 10576 case offsetof(struct bpf_sock_ops, ecn_flags): 10577 SOCK_OPS_GET_TCP_SOCK_FIELD(ecn_flags); 10578 break; 10579 case offsetof(struct bpf_sock_ops, rate_delivered): 10580 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_delivered); 10581 break; 10582 case offsetof(struct bpf_sock_ops, rate_interval_us): 10583 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_interval_us); 10584 break; 10585 case offsetof(struct bpf_sock_ops, packets_out): 10586 SOCK_OPS_GET_TCP_SOCK_FIELD(packets_out); 10587 break; 10588 case offsetof(struct bpf_sock_ops, retrans_out): 10589 SOCK_OPS_GET_TCP_SOCK_FIELD(retrans_out); 10590 break; 10591 case offsetof(struct bpf_sock_ops, total_retrans): 10592 SOCK_OPS_GET_TCP_SOCK_FIELD(total_retrans); 10593 break; 10594 case offsetof(struct bpf_sock_ops, segs_in): 10595 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_in); 10596 break; 10597 case offsetof(struct bpf_sock_ops, data_segs_in): 10598 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_in); 10599 break; 10600 case offsetof(struct bpf_sock_ops, segs_out): 10601 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_out); 10602 break; 10603 case offsetof(struct bpf_sock_ops, data_segs_out): 10604 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_out); 10605 break; 10606 case offsetof(struct bpf_sock_ops, lost_out): 10607 SOCK_OPS_GET_TCP_SOCK_FIELD(lost_out); 10608 break; 10609 case offsetof(struct bpf_sock_ops, sacked_out): 10610 SOCK_OPS_GET_TCP_SOCK_FIELD(sacked_out); 10611 break; 10612 case offsetof(struct bpf_sock_ops, bytes_received): 10613 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_received); 10614 break; 10615 case offsetof(struct bpf_sock_ops, bytes_acked): 10616 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_acked); 10617 break; 10618 case offsetof(struct bpf_sock_ops, sk): 10619 SOCK_OPS_GET_SK(); 10620 break; 10621 case offsetof(struct bpf_sock_ops, skb_data_end): 10622 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern, 10623 skb_data_end), 10624 si->dst_reg, si->src_reg, 10625 offsetof(struct bpf_sock_ops_kern, 10626 skb_data_end)); 10627 break; 10628 case offsetof(struct bpf_sock_ops, skb_data): 10629 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern, 10630 skb), 10631 si->dst_reg, si->src_reg, 10632 offsetof(struct bpf_sock_ops_kern, 10633 skb)); 10634 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1); 10635 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data), 10636 si->dst_reg, si->dst_reg, 10637 offsetof(struct sk_buff, data)); 10638 break; 10639 case offsetof(struct bpf_sock_ops, skb_len): 10640 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern, 10641 skb), 10642 si->dst_reg, si->src_reg, 10643 offsetof(struct bpf_sock_ops_kern, 10644 skb)); 10645 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1); 10646 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, len), 10647 si->dst_reg, si->dst_reg, 10648 offsetof(struct sk_buff, len)); 10649 break; 10650 case offsetof(struct bpf_sock_ops, skb_tcp_flags): 10651 off = offsetof(struct sk_buff, cb); 10652 off += offsetof(struct tcp_skb_cb, tcp_flags); 10653 *target_size = sizeof_field(struct tcp_skb_cb, tcp_flags); 10654 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern, 10655 skb), 10656 si->dst_reg, si->src_reg, 10657 offsetof(struct bpf_sock_ops_kern, 10658 skb)); 10659 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1); 10660 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_skb_cb, 10661 tcp_flags), 10662 si->dst_reg, si->dst_reg, off); 10663 break; 10664 case offsetof(struct bpf_sock_ops, skb_hwtstamp): { 10665 struct bpf_insn *jmp_on_null_skb; 10666 10667 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern, 10668 skb), 10669 si->dst_reg, si->src_reg, 10670 offsetof(struct bpf_sock_ops_kern, 10671 skb)); 10672 /* Reserve one insn to test skb == NULL */ 10673 jmp_on_null_skb = insn++; 10674 insn = bpf_convert_shinfo_access(si->dst_reg, si->dst_reg, insn); 10675 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg, 10676 bpf_target_off(struct skb_shared_info, 10677 hwtstamps, 8, 10678 target_size)); 10679 *jmp_on_null_skb = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 10680 insn - jmp_on_null_skb - 1); 10681 break; 10682 } 10683 } 10684 return insn - insn_buf; 10685 } 10686 10687 /* data_end = skb->data + skb_headlen() */ 10688 static struct bpf_insn *bpf_convert_data_end_access(const struct bpf_insn *si, 10689 struct bpf_insn *insn) 10690 { 10691 int reg; 10692 int temp_reg_off = offsetof(struct sk_buff, cb) + 10693 offsetof(struct sk_skb_cb, temp_reg); 10694 10695 if (si->src_reg == si->dst_reg) { 10696 /* We need an extra register, choose and save a register. */ 10697 reg = BPF_REG_9; 10698 if (si->src_reg == reg || si->dst_reg == reg) 10699 reg--; 10700 if (si->src_reg == reg || si->dst_reg == reg) 10701 reg--; 10702 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, temp_reg_off); 10703 } else { 10704 reg = si->dst_reg; 10705 } 10706 10707 /* reg = skb->data */ 10708 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data), 10709 reg, si->src_reg, 10710 offsetof(struct sk_buff, data)); 10711 /* AX = skb->len */ 10712 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, len), 10713 BPF_REG_AX, si->src_reg, 10714 offsetof(struct sk_buff, len)); 10715 /* reg = skb->data + skb->len */ 10716 *insn++ = BPF_ALU64_REG(BPF_ADD, reg, BPF_REG_AX); 10717 /* AX = skb->data_len */ 10718 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data_len), 10719 BPF_REG_AX, si->src_reg, 10720 offsetof(struct sk_buff, data_len)); 10721 10722 /* reg = skb->data + skb->len - skb->data_len */ 10723 *insn++ = BPF_ALU64_REG(BPF_SUB, reg, BPF_REG_AX); 10724 10725 if (si->src_reg == si->dst_reg) { 10726 /* Restore the saved register */ 10727 *insn++ = BPF_MOV64_REG(BPF_REG_AX, si->src_reg); 10728 *insn++ = BPF_MOV64_REG(si->dst_reg, reg); 10729 *insn++ = BPF_LDX_MEM(BPF_DW, reg, BPF_REG_AX, temp_reg_off); 10730 } 10731 10732 return insn; 10733 } 10734 10735 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type, 10736 const struct bpf_insn *si, 10737 struct bpf_insn *insn_buf, 10738 struct bpf_prog *prog, u32 *target_size) 10739 { 10740 struct bpf_insn *insn = insn_buf; 10741 int off; 10742 10743 switch (si->off) { 10744 case offsetof(struct __sk_buff, data_end): 10745 insn = bpf_convert_data_end_access(si, insn); 10746 break; 10747 case offsetof(struct __sk_buff, cb[0]) ... 10748 offsetofend(struct __sk_buff, cb[4]) - 1: 10749 BUILD_BUG_ON(sizeof_field(struct sk_skb_cb, data) < 20); 10750 BUILD_BUG_ON((offsetof(struct sk_buff, cb) + 10751 offsetof(struct sk_skb_cb, data)) % 10752 sizeof(__u64)); 10753 10754 prog->cb_access = 1; 10755 off = si->off; 10756 off -= offsetof(struct __sk_buff, cb[0]); 10757 off += offsetof(struct sk_buff, cb); 10758 off += offsetof(struct sk_skb_cb, data); 10759 if (type == BPF_WRITE) 10760 *insn++ = BPF_EMIT_STORE(BPF_SIZE(si->code), si, off); 10761 else 10762 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg, 10763 si->src_reg, off); 10764 break; 10765 10766 10767 default: 10768 return bpf_convert_ctx_access(type, si, insn_buf, prog, 10769 target_size); 10770 } 10771 10772 return insn - insn_buf; 10773 } 10774 10775 static u32 sk_msg_convert_ctx_access(enum bpf_access_type type, 10776 const struct bpf_insn *si, 10777 struct bpf_insn *insn_buf, 10778 struct bpf_prog *prog, u32 *target_size) 10779 { 10780 struct bpf_insn *insn = insn_buf; 10781 #if IS_ENABLED(CONFIG_IPV6) 10782 int off; 10783 #endif 10784 10785 /* convert ctx uses the fact sg element is first in struct */ 10786 BUILD_BUG_ON(offsetof(struct sk_msg, sg) != 0); 10787 10788 switch (si->off) { 10789 case offsetof(struct sk_msg_md, data): 10790 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data), 10791 si->dst_reg, si->src_reg, 10792 offsetof(struct sk_msg, data)); 10793 break; 10794 case offsetof(struct sk_msg_md, data_end): 10795 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data_end), 10796 si->dst_reg, si->src_reg, 10797 offsetof(struct sk_msg, data_end)); 10798 break; 10799 case offsetof(struct sk_msg_md, family): 10800 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2); 10801 10802 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 10803 struct sk_msg, sk), 10804 si->dst_reg, si->src_reg, 10805 offsetof(struct sk_msg, sk)); 10806 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 10807 offsetof(struct sock_common, skc_family)); 10808 break; 10809 10810 case offsetof(struct sk_msg_md, remote_ip4): 10811 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4); 10812 10813 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 10814 struct sk_msg, sk), 10815 si->dst_reg, si->src_reg, 10816 offsetof(struct sk_msg, sk)); 10817 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 10818 offsetof(struct sock_common, skc_daddr)); 10819 break; 10820 10821 case offsetof(struct sk_msg_md, local_ip4): 10822 BUILD_BUG_ON(sizeof_field(struct sock_common, 10823 skc_rcv_saddr) != 4); 10824 10825 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 10826 struct sk_msg, sk), 10827 si->dst_reg, si->src_reg, 10828 offsetof(struct sk_msg, sk)); 10829 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 10830 offsetof(struct sock_common, 10831 skc_rcv_saddr)); 10832 break; 10833 10834 case offsetof(struct sk_msg_md, remote_ip6[0]) ... 10835 offsetof(struct sk_msg_md, remote_ip6[3]): 10836 #if IS_ENABLED(CONFIG_IPV6) 10837 BUILD_BUG_ON(sizeof_field(struct sock_common, 10838 skc_v6_daddr.s6_addr32[0]) != 4); 10839 10840 off = si->off; 10841 off -= offsetof(struct sk_msg_md, remote_ip6[0]); 10842 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 10843 struct sk_msg, sk), 10844 si->dst_reg, si->src_reg, 10845 offsetof(struct sk_msg, sk)); 10846 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 10847 offsetof(struct sock_common, 10848 skc_v6_daddr.s6_addr32[0]) + 10849 off); 10850 #else 10851 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 10852 #endif 10853 break; 10854 10855 case offsetof(struct sk_msg_md, local_ip6[0]) ... 10856 offsetof(struct sk_msg_md, local_ip6[3]): 10857 #if IS_ENABLED(CONFIG_IPV6) 10858 BUILD_BUG_ON(sizeof_field(struct sock_common, 10859 skc_v6_rcv_saddr.s6_addr32[0]) != 4); 10860 10861 off = si->off; 10862 off -= offsetof(struct sk_msg_md, local_ip6[0]); 10863 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 10864 struct sk_msg, sk), 10865 si->dst_reg, si->src_reg, 10866 offsetof(struct sk_msg, sk)); 10867 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 10868 offsetof(struct sock_common, 10869 skc_v6_rcv_saddr.s6_addr32[0]) + 10870 off); 10871 #else 10872 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 10873 #endif 10874 break; 10875 10876 case offsetof(struct sk_msg_md, remote_port): 10877 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2); 10878 10879 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 10880 struct sk_msg, sk), 10881 si->dst_reg, si->src_reg, 10882 offsetof(struct sk_msg, sk)); 10883 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 10884 offsetof(struct sock_common, skc_dport)); 10885 #ifndef __BIG_ENDIAN_BITFIELD 10886 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16); 10887 #endif 10888 break; 10889 10890 case offsetof(struct sk_msg_md, local_port): 10891 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2); 10892 10893 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 10894 struct sk_msg, sk), 10895 si->dst_reg, si->src_reg, 10896 offsetof(struct sk_msg, sk)); 10897 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 10898 offsetof(struct sock_common, skc_num)); 10899 break; 10900 10901 case offsetof(struct sk_msg_md, size): 10902 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_sg, size), 10903 si->dst_reg, si->src_reg, 10904 offsetof(struct sk_msg_sg, size)); 10905 break; 10906 10907 case offsetof(struct sk_msg_md, sk): 10908 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, sk), 10909 si->dst_reg, si->src_reg, 10910 offsetof(struct sk_msg, sk)); 10911 break; 10912 } 10913 10914 return insn - insn_buf; 10915 } 10916 10917 const struct bpf_verifier_ops sk_filter_verifier_ops = { 10918 .get_func_proto = sk_filter_func_proto, 10919 .is_valid_access = sk_filter_is_valid_access, 10920 .convert_ctx_access = bpf_convert_ctx_access, 10921 .gen_ld_abs = bpf_gen_ld_abs, 10922 }; 10923 10924 const struct bpf_prog_ops sk_filter_prog_ops = { 10925 .test_run = bpf_prog_test_run_skb, 10926 }; 10927 10928 const struct bpf_verifier_ops tc_cls_act_verifier_ops = { 10929 .get_func_proto = tc_cls_act_func_proto, 10930 .is_valid_access = tc_cls_act_is_valid_access, 10931 .convert_ctx_access = tc_cls_act_convert_ctx_access, 10932 .gen_prologue = tc_cls_act_prologue, 10933 .gen_ld_abs = bpf_gen_ld_abs, 10934 .btf_struct_access = tc_cls_act_btf_struct_access, 10935 }; 10936 10937 const struct bpf_prog_ops tc_cls_act_prog_ops = { 10938 .test_run = bpf_prog_test_run_skb, 10939 }; 10940 10941 const struct bpf_verifier_ops xdp_verifier_ops = { 10942 .get_func_proto = xdp_func_proto, 10943 .is_valid_access = xdp_is_valid_access, 10944 .convert_ctx_access = xdp_convert_ctx_access, 10945 .gen_prologue = bpf_noop_prologue, 10946 .btf_struct_access = xdp_btf_struct_access, 10947 }; 10948 10949 const struct bpf_prog_ops xdp_prog_ops = { 10950 .test_run = bpf_prog_test_run_xdp, 10951 }; 10952 10953 const struct bpf_verifier_ops cg_skb_verifier_ops = { 10954 .get_func_proto = cg_skb_func_proto, 10955 .is_valid_access = cg_skb_is_valid_access, 10956 .convert_ctx_access = bpf_convert_ctx_access, 10957 }; 10958 10959 const struct bpf_prog_ops cg_skb_prog_ops = { 10960 .test_run = bpf_prog_test_run_skb, 10961 }; 10962 10963 const struct bpf_verifier_ops lwt_in_verifier_ops = { 10964 .get_func_proto = lwt_in_func_proto, 10965 .is_valid_access = lwt_is_valid_access, 10966 .convert_ctx_access = bpf_convert_ctx_access, 10967 }; 10968 10969 const struct bpf_prog_ops lwt_in_prog_ops = { 10970 .test_run = bpf_prog_test_run_skb, 10971 }; 10972 10973 const struct bpf_verifier_ops lwt_out_verifier_ops = { 10974 .get_func_proto = lwt_out_func_proto, 10975 .is_valid_access = lwt_is_valid_access, 10976 .convert_ctx_access = bpf_convert_ctx_access, 10977 }; 10978 10979 const struct bpf_prog_ops lwt_out_prog_ops = { 10980 .test_run = bpf_prog_test_run_skb, 10981 }; 10982 10983 const struct bpf_verifier_ops lwt_xmit_verifier_ops = { 10984 .get_func_proto = lwt_xmit_func_proto, 10985 .is_valid_access = lwt_is_valid_access, 10986 .convert_ctx_access = bpf_convert_ctx_access, 10987 .gen_prologue = tc_cls_act_prologue, 10988 }; 10989 10990 const struct bpf_prog_ops lwt_xmit_prog_ops = { 10991 .test_run = bpf_prog_test_run_skb, 10992 }; 10993 10994 const struct bpf_verifier_ops lwt_seg6local_verifier_ops = { 10995 .get_func_proto = lwt_seg6local_func_proto, 10996 .is_valid_access = lwt_is_valid_access, 10997 .convert_ctx_access = bpf_convert_ctx_access, 10998 }; 10999 11000 const struct bpf_prog_ops lwt_seg6local_prog_ops = { 11001 .test_run = bpf_prog_test_run_skb, 11002 }; 11003 11004 const struct bpf_verifier_ops cg_sock_verifier_ops = { 11005 .get_func_proto = sock_filter_func_proto, 11006 .is_valid_access = sock_filter_is_valid_access, 11007 .convert_ctx_access = bpf_sock_convert_ctx_access, 11008 }; 11009 11010 const struct bpf_prog_ops cg_sock_prog_ops = { 11011 }; 11012 11013 const struct bpf_verifier_ops cg_sock_addr_verifier_ops = { 11014 .get_func_proto = sock_addr_func_proto, 11015 .is_valid_access = sock_addr_is_valid_access, 11016 .convert_ctx_access = sock_addr_convert_ctx_access, 11017 }; 11018 11019 const struct bpf_prog_ops cg_sock_addr_prog_ops = { 11020 }; 11021 11022 const struct bpf_verifier_ops sock_ops_verifier_ops = { 11023 .get_func_proto = sock_ops_func_proto, 11024 .is_valid_access = sock_ops_is_valid_access, 11025 .convert_ctx_access = sock_ops_convert_ctx_access, 11026 }; 11027 11028 const struct bpf_prog_ops sock_ops_prog_ops = { 11029 }; 11030 11031 const struct bpf_verifier_ops sk_skb_verifier_ops = { 11032 .get_func_proto = sk_skb_func_proto, 11033 .is_valid_access = sk_skb_is_valid_access, 11034 .convert_ctx_access = sk_skb_convert_ctx_access, 11035 .gen_prologue = sk_skb_prologue, 11036 }; 11037 11038 const struct bpf_prog_ops sk_skb_prog_ops = { 11039 }; 11040 11041 const struct bpf_verifier_ops sk_msg_verifier_ops = { 11042 .get_func_proto = sk_msg_func_proto, 11043 .is_valid_access = sk_msg_is_valid_access, 11044 .convert_ctx_access = sk_msg_convert_ctx_access, 11045 .gen_prologue = bpf_noop_prologue, 11046 }; 11047 11048 const struct bpf_prog_ops sk_msg_prog_ops = { 11049 }; 11050 11051 const struct bpf_verifier_ops flow_dissector_verifier_ops = { 11052 .get_func_proto = flow_dissector_func_proto, 11053 .is_valid_access = flow_dissector_is_valid_access, 11054 .convert_ctx_access = flow_dissector_convert_ctx_access, 11055 }; 11056 11057 const struct bpf_prog_ops flow_dissector_prog_ops = { 11058 .test_run = bpf_prog_test_run_flow_dissector, 11059 }; 11060 11061 int sk_detach_filter(struct sock *sk) 11062 { 11063 int ret = -ENOENT; 11064 struct sk_filter *filter; 11065 11066 if (sock_flag(sk, SOCK_FILTER_LOCKED)) 11067 return -EPERM; 11068 11069 filter = rcu_dereference_protected(sk->sk_filter, 11070 lockdep_sock_is_held(sk)); 11071 if (filter) { 11072 RCU_INIT_POINTER(sk->sk_filter, NULL); 11073 sk_filter_uncharge(sk, filter); 11074 ret = 0; 11075 } 11076 11077 return ret; 11078 } 11079 EXPORT_SYMBOL_GPL(sk_detach_filter); 11080 11081 int sk_get_filter(struct sock *sk, sockptr_t optval, unsigned int len) 11082 { 11083 struct sock_fprog_kern *fprog; 11084 struct sk_filter *filter; 11085 int ret = 0; 11086 11087 sockopt_lock_sock(sk); 11088 filter = rcu_dereference_protected(sk->sk_filter, 11089 lockdep_sock_is_held(sk)); 11090 if (!filter) 11091 goto out; 11092 11093 /* We're copying the filter that has been originally attached, 11094 * so no conversion/decode needed anymore. eBPF programs that 11095 * have no original program cannot be dumped through this. 11096 */ 11097 ret = -EACCES; 11098 fprog = filter->prog->orig_prog; 11099 if (!fprog) 11100 goto out; 11101 11102 ret = fprog->len; 11103 if (!len) 11104 /* User space only enquires number of filter blocks. */ 11105 goto out; 11106 11107 ret = -EINVAL; 11108 if (len < fprog->len) 11109 goto out; 11110 11111 ret = -EFAULT; 11112 if (copy_to_sockptr(optval, fprog->filter, bpf_classic_proglen(fprog))) 11113 goto out; 11114 11115 /* Instead of bytes, the API requests to return the number 11116 * of filter blocks. 11117 */ 11118 ret = fprog->len; 11119 out: 11120 sockopt_release_sock(sk); 11121 return ret; 11122 } 11123 11124 #ifdef CONFIG_INET 11125 static void bpf_init_reuseport_kern(struct sk_reuseport_kern *reuse_kern, 11126 struct sock_reuseport *reuse, 11127 struct sock *sk, struct sk_buff *skb, 11128 struct sock *migrating_sk, 11129 u32 hash) 11130 { 11131 reuse_kern->skb = skb; 11132 reuse_kern->sk = sk; 11133 reuse_kern->selected_sk = NULL; 11134 reuse_kern->migrating_sk = migrating_sk; 11135 reuse_kern->data_end = skb->data + skb_headlen(skb); 11136 reuse_kern->hash = hash; 11137 reuse_kern->reuseport_id = reuse->reuseport_id; 11138 reuse_kern->bind_inany = reuse->bind_inany; 11139 } 11140 11141 struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk, 11142 struct bpf_prog *prog, struct sk_buff *skb, 11143 struct sock *migrating_sk, 11144 u32 hash) 11145 { 11146 struct sk_reuseport_kern reuse_kern; 11147 enum sk_action action; 11148 11149 bpf_init_reuseport_kern(&reuse_kern, reuse, sk, skb, migrating_sk, hash); 11150 action = bpf_prog_run(prog, &reuse_kern); 11151 11152 if (action == SK_PASS) 11153 return reuse_kern.selected_sk; 11154 else 11155 return ERR_PTR(-ECONNREFUSED); 11156 } 11157 11158 BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern, 11159 struct bpf_map *, map, void *, key, u32, flags) 11160 { 11161 bool is_sockarray = map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY; 11162 struct sock_reuseport *reuse; 11163 struct sock *selected_sk; 11164 11165 selected_sk = map->ops->map_lookup_elem(map, key); 11166 if (!selected_sk) 11167 return -ENOENT; 11168 11169 reuse = rcu_dereference(selected_sk->sk_reuseport_cb); 11170 if (!reuse) { 11171 /* Lookup in sock_map can return TCP ESTABLISHED sockets. */ 11172 if (sk_is_refcounted(selected_sk)) 11173 sock_put(selected_sk); 11174 11175 /* reuseport_array has only sk with non NULL sk_reuseport_cb. 11176 * The only (!reuse) case here is - the sk has already been 11177 * unhashed (e.g. by close()), so treat it as -ENOENT. 11178 * 11179 * Other maps (e.g. sock_map) do not provide this guarantee and 11180 * the sk may never be in the reuseport group to begin with. 11181 */ 11182 return is_sockarray ? -ENOENT : -EINVAL; 11183 } 11184 11185 if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) { 11186 struct sock *sk = reuse_kern->sk; 11187 11188 if (sk->sk_protocol != selected_sk->sk_protocol) 11189 return -EPROTOTYPE; 11190 else if (sk->sk_family != selected_sk->sk_family) 11191 return -EAFNOSUPPORT; 11192 11193 /* Catch all. Likely bound to a different sockaddr. */ 11194 return -EBADFD; 11195 } 11196 11197 reuse_kern->selected_sk = selected_sk; 11198 11199 return 0; 11200 } 11201 11202 static const struct bpf_func_proto sk_select_reuseport_proto = { 11203 .func = sk_select_reuseport, 11204 .gpl_only = false, 11205 .ret_type = RET_INTEGER, 11206 .arg1_type = ARG_PTR_TO_CTX, 11207 .arg2_type = ARG_CONST_MAP_PTR, 11208 .arg3_type = ARG_PTR_TO_MAP_KEY, 11209 .arg4_type = ARG_ANYTHING, 11210 }; 11211 11212 BPF_CALL_4(sk_reuseport_load_bytes, 11213 const struct sk_reuseport_kern *, reuse_kern, u32, offset, 11214 void *, to, u32, len) 11215 { 11216 return ____bpf_skb_load_bytes(reuse_kern->skb, offset, to, len); 11217 } 11218 11219 static const struct bpf_func_proto sk_reuseport_load_bytes_proto = { 11220 .func = sk_reuseport_load_bytes, 11221 .gpl_only = false, 11222 .ret_type = RET_INTEGER, 11223 .arg1_type = ARG_PTR_TO_CTX, 11224 .arg2_type = ARG_ANYTHING, 11225 .arg3_type = ARG_PTR_TO_UNINIT_MEM, 11226 .arg4_type = ARG_CONST_SIZE, 11227 }; 11228 11229 BPF_CALL_5(sk_reuseport_load_bytes_relative, 11230 const struct sk_reuseport_kern *, reuse_kern, u32, offset, 11231 void *, to, u32, len, u32, start_header) 11232 { 11233 return ____bpf_skb_load_bytes_relative(reuse_kern->skb, offset, to, 11234 len, start_header); 11235 } 11236 11237 static const struct bpf_func_proto sk_reuseport_load_bytes_relative_proto = { 11238 .func = sk_reuseport_load_bytes_relative, 11239 .gpl_only = false, 11240 .ret_type = RET_INTEGER, 11241 .arg1_type = ARG_PTR_TO_CTX, 11242 .arg2_type = ARG_ANYTHING, 11243 .arg3_type = ARG_PTR_TO_UNINIT_MEM, 11244 .arg4_type = ARG_CONST_SIZE, 11245 .arg5_type = ARG_ANYTHING, 11246 }; 11247 11248 static const struct bpf_func_proto * 11249 sk_reuseport_func_proto(enum bpf_func_id func_id, 11250 const struct bpf_prog *prog) 11251 { 11252 switch (func_id) { 11253 case BPF_FUNC_sk_select_reuseport: 11254 return &sk_select_reuseport_proto; 11255 case BPF_FUNC_skb_load_bytes: 11256 return &sk_reuseport_load_bytes_proto; 11257 case BPF_FUNC_skb_load_bytes_relative: 11258 return &sk_reuseport_load_bytes_relative_proto; 11259 case BPF_FUNC_get_socket_cookie: 11260 return &bpf_get_socket_ptr_cookie_proto; 11261 case BPF_FUNC_ktime_get_coarse_ns: 11262 return &bpf_ktime_get_coarse_ns_proto; 11263 default: 11264 return bpf_base_func_proto(func_id); 11265 } 11266 } 11267 11268 static bool 11269 sk_reuseport_is_valid_access(int off, int size, 11270 enum bpf_access_type type, 11271 const struct bpf_prog *prog, 11272 struct bpf_insn_access_aux *info) 11273 { 11274 const u32 size_default = sizeof(__u32); 11275 11276 if (off < 0 || off >= sizeof(struct sk_reuseport_md) || 11277 off % size || type != BPF_READ) 11278 return false; 11279 11280 switch (off) { 11281 case offsetof(struct sk_reuseport_md, data): 11282 info->reg_type = PTR_TO_PACKET; 11283 return size == sizeof(__u64); 11284 11285 case offsetof(struct sk_reuseport_md, data_end): 11286 info->reg_type = PTR_TO_PACKET_END; 11287 return size == sizeof(__u64); 11288 11289 case offsetof(struct sk_reuseport_md, hash): 11290 return size == size_default; 11291 11292 case offsetof(struct sk_reuseport_md, sk): 11293 info->reg_type = PTR_TO_SOCKET; 11294 return size == sizeof(__u64); 11295 11296 case offsetof(struct sk_reuseport_md, migrating_sk): 11297 info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL; 11298 return size == sizeof(__u64); 11299 11300 /* Fields that allow narrowing */ 11301 case bpf_ctx_range(struct sk_reuseport_md, eth_protocol): 11302 if (size < sizeof_field(struct sk_buff, protocol)) 11303 return false; 11304 fallthrough; 11305 case bpf_ctx_range(struct sk_reuseport_md, ip_protocol): 11306 case bpf_ctx_range(struct sk_reuseport_md, bind_inany): 11307 case bpf_ctx_range(struct sk_reuseport_md, len): 11308 bpf_ctx_record_field_size(info, size_default); 11309 return bpf_ctx_narrow_access_ok(off, size, size_default); 11310 11311 default: 11312 return false; 11313 } 11314 } 11315 11316 #define SK_REUSEPORT_LOAD_FIELD(F) ({ \ 11317 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_reuseport_kern, F), \ 11318 si->dst_reg, si->src_reg, \ 11319 bpf_target_off(struct sk_reuseport_kern, F, \ 11320 sizeof_field(struct sk_reuseport_kern, F), \ 11321 target_size)); \ 11322 }) 11323 11324 #define SK_REUSEPORT_LOAD_SKB_FIELD(SKB_FIELD) \ 11325 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \ 11326 struct sk_buff, \ 11327 skb, \ 11328 SKB_FIELD) 11329 11330 #define SK_REUSEPORT_LOAD_SK_FIELD(SK_FIELD) \ 11331 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \ 11332 struct sock, \ 11333 sk, \ 11334 SK_FIELD) 11335 11336 static u32 sk_reuseport_convert_ctx_access(enum bpf_access_type type, 11337 const struct bpf_insn *si, 11338 struct bpf_insn *insn_buf, 11339 struct bpf_prog *prog, 11340 u32 *target_size) 11341 { 11342 struct bpf_insn *insn = insn_buf; 11343 11344 switch (si->off) { 11345 case offsetof(struct sk_reuseport_md, data): 11346 SK_REUSEPORT_LOAD_SKB_FIELD(data); 11347 break; 11348 11349 case offsetof(struct sk_reuseport_md, len): 11350 SK_REUSEPORT_LOAD_SKB_FIELD(len); 11351 break; 11352 11353 case offsetof(struct sk_reuseport_md, eth_protocol): 11354 SK_REUSEPORT_LOAD_SKB_FIELD(protocol); 11355 break; 11356 11357 case offsetof(struct sk_reuseport_md, ip_protocol): 11358 SK_REUSEPORT_LOAD_SK_FIELD(sk_protocol); 11359 break; 11360 11361 case offsetof(struct sk_reuseport_md, data_end): 11362 SK_REUSEPORT_LOAD_FIELD(data_end); 11363 break; 11364 11365 case offsetof(struct sk_reuseport_md, hash): 11366 SK_REUSEPORT_LOAD_FIELD(hash); 11367 break; 11368 11369 case offsetof(struct sk_reuseport_md, bind_inany): 11370 SK_REUSEPORT_LOAD_FIELD(bind_inany); 11371 break; 11372 11373 case offsetof(struct sk_reuseport_md, sk): 11374 SK_REUSEPORT_LOAD_FIELD(sk); 11375 break; 11376 11377 case offsetof(struct sk_reuseport_md, migrating_sk): 11378 SK_REUSEPORT_LOAD_FIELD(migrating_sk); 11379 break; 11380 } 11381 11382 return insn - insn_buf; 11383 } 11384 11385 const struct bpf_verifier_ops sk_reuseport_verifier_ops = { 11386 .get_func_proto = sk_reuseport_func_proto, 11387 .is_valid_access = sk_reuseport_is_valid_access, 11388 .convert_ctx_access = sk_reuseport_convert_ctx_access, 11389 }; 11390 11391 const struct bpf_prog_ops sk_reuseport_prog_ops = { 11392 }; 11393 11394 DEFINE_STATIC_KEY_FALSE(bpf_sk_lookup_enabled); 11395 EXPORT_SYMBOL(bpf_sk_lookup_enabled); 11396 11397 BPF_CALL_3(bpf_sk_lookup_assign, struct bpf_sk_lookup_kern *, ctx, 11398 struct sock *, sk, u64, flags) 11399 { 11400 if (unlikely(flags & ~(BPF_SK_LOOKUP_F_REPLACE | 11401 BPF_SK_LOOKUP_F_NO_REUSEPORT))) 11402 return -EINVAL; 11403 if (unlikely(sk && sk_is_refcounted(sk))) 11404 return -ESOCKTNOSUPPORT; /* reject non-RCU freed sockets */ 11405 if (unlikely(sk && sk_is_tcp(sk) && sk->sk_state != TCP_LISTEN)) 11406 return -ESOCKTNOSUPPORT; /* only accept TCP socket in LISTEN */ 11407 if (unlikely(sk && sk_is_udp(sk) && sk->sk_state != TCP_CLOSE)) 11408 return -ESOCKTNOSUPPORT; /* only accept UDP socket in CLOSE */ 11409 11410 /* Check if socket is suitable for packet L3/L4 protocol */ 11411 if (sk && sk->sk_protocol != ctx->protocol) 11412 return -EPROTOTYPE; 11413 if (sk && sk->sk_family != ctx->family && 11414 (sk->sk_family == AF_INET || ipv6_only_sock(sk))) 11415 return -EAFNOSUPPORT; 11416 11417 if (ctx->selected_sk && !(flags & BPF_SK_LOOKUP_F_REPLACE)) 11418 return -EEXIST; 11419 11420 /* Select socket as lookup result */ 11421 ctx->selected_sk = sk; 11422 ctx->no_reuseport = flags & BPF_SK_LOOKUP_F_NO_REUSEPORT; 11423 return 0; 11424 } 11425 11426 static const struct bpf_func_proto bpf_sk_lookup_assign_proto = { 11427 .func = bpf_sk_lookup_assign, 11428 .gpl_only = false, 11429 .ret_type = RET_INTEGER, 11430 .arg1_type = ARG_PTR_TO_CTX, 11431 .arg2_type = ARG_PTR_TO_SOCKET_OR_NULL, 11432 .arg3_type = ARG_ANYTHING, 11433 }; 11434 11435 static const struct bpf_func_proto * 11436 sk_lookup_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 11437 { 11438 switch (func_id) { 11439 case BPF_FUNC_perf_event_output: 11440 return &bpf_event_output_data_proto; 11441 case BPF_FUNC_sk_assign: 11442 return &bpf_sk_lookup_assign_proto; 11443 case BPF_FUNC_sk_release: 11444 return &bpf_sk_release_proto; 11445 default: 11446 return bpf_sk_base_func_proto(func_id); 11447 } 11448 } 11449 11450 static bool sk_lookup_is_valid_access(int off, int size, 11451 enum bpf_access_type type, 11452 const struct bpf_prog *prog, 11453 struct bpf_insn_access_aux *info) 11454 { 11455 if (off < 0 || off >= sizeof(struct bpf_sk_lookup)) 11456 return false; 11457 if (off % size != 0) 11458 return false; 11459 if (type != BPF_READ) 11460 return false; 11461 11462 switch (off) { 11463 case offsetof(struct bpf_sk_lookup, sk): 11464 info->reg_type = PTR_TO_SOCKET_OR_NULL; 11465 return size == sizeof(__u64); 11466 11467 case bpf_ctx_range(struct bpf_sk_lookup, family): 11468 case bpf_ctx_range(struct bpf_sk_lookup, protocol): 11469 case bpf_ctx_range(struct bpf_sk_lookup, remote_ip4): 11470 case bpf_ctx_range(struct bpf_sk_lookup, local_ip4): 11471 case bpf_ctx_range_till(struct bpf_sk_lookup, remote_ip6[0], remote_ip6[3]): 11472 case bpf_ctx_range_till(struct bpf_sk_lookup, local_ip6[0], local_ip6[3]): 11473 case bpf_ctx_range(struct bpf_sk_lookup, local_port): 11474 case bpf_ctx_range(struct bpf_sk_lookup, ingress_ifindex): 11475 bpf_ctx_record_field_size(info, sizeof(__u32)); 11476 return bpf_ctx_narrow_access_ok(off, size, sizeof(__u32)); 11477 11478 case bpf_ctx_range(struct bpf_sk_lookup, remote_port): 11479 /* Allow 4-byte access to 2-byte field for backward compatibility */ 11480 if (size == sizeof(__u32)) 11481 return true; 11482 bpf_ctx_record_field_size(info, sizeof(__be16)); 11483 return bpf_ctx_narrow_access_ok(off, size, sizeof(__be16)); 11484 11485 case offsetofend(struct bpf_sk_lookup, remote_port) ... 11486 offsetof(struct bpf_sk_lookup, local_ip4) - 1: 11487 /* Allow access to zero padding for backward compatibility */ 11488 bpf_ctx_record_field_size(info, sizeof(__u16)); 11489 return bpf_ctx_narrow_access_ok(off, size, sizeof(__u16)); 11490 11491 default: 11492 return false; 11493 } 11494 } 11495 11496 static u32 sk_lookup_convert_ctx_access(enum bpf_access_type type, 11497 const struct bpf_insn *si, 11498 struct bpf_insn *insn_buf, 11499 struct bpf_prog *prog, 11500 u32 *target_size) 11501 { 11502 struct bpf_insn *insn = insn_buf; 11503 11504 switch (si->off) { 11505 case offsetof(struct bpf_sk_lookup, sk): 11506 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg, 11507 offsetof(struct bpf_sk_lookup_kern, selected_sk)); 11508 break; 11509 11510 case offsetof(struct bpf_sk_lookup, family): 11511 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, 11512 bpf_target_off(struct bpf_sk_lookup_kern, 11513 family, 2, target_size)); 11514 break; 11515 11516 case offsetof(struct bpf_sk_lookup, protocol): 11517 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, 11518 bpf_target_off(struct bpf_sk_lookup_kern, 11519 protocol, 2, target_size)); 11520 break; 11521 11522 case offsetof(struct bpf_sk_lookup, remote_ip4): 11523 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 11524 bpf_target_off(struct bpf_sk_lookup_kern, 11525 v4.saddr, 4, target_size)); 11526 break; 11527 11528 case offsetof(struct bpf_sk_lookup, local_ip4): 11529 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 11530 bpf_target_off(struct bpf_sk_lookup_kern, 11531 v4.daddr, 4, target_size)); 11532 break; 11533 11534 case bpf_ctx_range_till(struct bpf_sk_lookup, 11535 remote_ip6[0], remote_ip6[3]): { 11536 #if IS_ENABLED(CONFIG_IPV6) 11537 int off = si->off; 11538 11539 off -= offsetof(struct bpf_sk_lookup, remote_ip6[0]); 11540 off += bpf_target_off(struct in6_addr, s6_addr32[0], 4, target_size); 11541 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg, 11542 offsetof(struct bpf_sk_lookup_kern, v6.saddr)); 11543 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1); 11544 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, off); 11545 #else 11546 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 11547 #endif 11548 break; 11549 } 11550 case bpf_ctx_range_till(struct bpf_sk_lookup, 11551 local_ip6[0], local_ip6[3]): { 11552 #if IS_ENABLED(CONFIG_IPV6) 11553 int off = si->off; 11554 11555 off -= offsetof(struct bpf_sk_lookup, local_ip6[0]); 11556 off += bpf_target_off(struct in6_addr, s6_addr32[0], 4, target_size); 11557 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg, 11558 offsetof(struct bpf_sk_lookup_kern, v6.daddr)); 11559 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1); 11560 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, off); 11561 #else 11562 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 11563 #endif 11564 break; 11565 } 11566 case offsetof(struct bpf_sk_lookup, remote_port): 11567 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, 11568 bpf_target_off(struct bpf_sk_lookup_kern, 11569 sport, 2, target_size)); 11570 break; 11571 11572 case offsetofend(struct bpf_sk_lookup, remote_port): 11573 *target_size = 2; 11574 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 11575 break; 11576 11577 case offsetof(struct bpf_sk_lookup, local_port): 11578 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, 11579 bpf_target_off(struct bpf_sk_lookup_kern, 11580 dport, 2, target_size)); 11581 break; 11582 11583 case offsetof(struct bpf_sk_lookup, ingress_ifindex): 11584 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 11585 bpf_target_off(struct bpf_sk_lookup_kern, 11586 ingress_ifindex, 4, target_size)); 11587 break; 11588 } 11589 11590 return insn - insn_buf; 11591 } 11592 11593 const struct bpf_prog_ops sk_lookup_prog_ops = { 11594 .test_run = bpf_prog_test_run_sk_lookup, 11595 }; 11596 11597 const struct bpf_verifier_ops sk_lookup_verifier_ops = { 11598 .get_func_proto = sk_lookup_func_proto, 11599 .is_valid_access = sk_lookup_is_valid_access, 11600 .convert_ctx_access = sk_lookup_convert_ctx_access, 11601 }; 11602 11603 #endif /* CONFIG_INET */ 11604 11605 DEFINE_BPF_DISPATCHER(xdp) 11606 11607 void bpf_prog_change_xdp(struct bpf_prog *prev_prog, struct bpf_prog *prog) 11608 { 11609 bpf_dispatcher_change_prog(BPF_DISPATCHER_PTR(xdp), prev_prog, prog); 11610 } 11611 11612 BTF_ID_LIST_GLOBAL(btf_sock_ids, MAX_BTF_SOCK_TYPE) 11613 #define BTF_SOCK_TYPE(name, type) BTF_ID(struct, type) 11614 BTF_SOCK_TYPE_xxx 11615 #undef BTF_SOCK_TYPE 11616 11617 BPF_CALL_1(bpf_skc_to_tcp6_sock, struct sock *, sk) 11618 { 11619 /* tcp6_sock type is not generated in dwarf and hence btf, 11620 * trigger an explicit type generation here. 11621 */ 11622 BTF_TYPE_EMIT(struct tcp6_sock); 11623 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP && 11624 sk->sk_family == AF_INET6) 11625 return (unsigned long)sk; 11626 11627 return (unsigned long)NULL; 11628 } 11629 11630 const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto = { 11631 .func = bpf_skc_to_tcp6_sock, 11632 .gpl_only = false, 11633 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, 11634 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, 11635 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP6], 11636 }; 11637 11638 BPF_CALL_1(bpf_skc_to_tcp_sock, struct sock *, sk) 11639 { 11640 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP) 11641 return (unsigned long)sk; 11642 11643 return (unsigned long)NULL; 11644 } 11645 11646 const struct bpf_func_proto bpf_skc_to_tcp_sock_proto = { 11647 .func = bpf_skc_to_tcp_sock, 11648 .gpl_only = false, 11649 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, 11650 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, 11651 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP], 11652 }; 11653 11654 BPF_CALL_1(bpf_skc_to_tcp_timewait_sock, struct sock *, sk) 11655 { 11656 /* BTF types for tcp_timewait_sock and inet_timewait_sock are not 11657 * generated if CONFIG_INET=n. Trigger an explicit generation here. 11658 */ 11659 BTF_TYPE_EMIT(struct inet_timewait_sock); 11660 BTF_TYPE_EMIT(struct tcp_timewait_sock); 11661 11662 #ifdef CONFIG_INET 11663 if (sk && sk->sk_prot == &tcp_prot && sk->sk_state == TCP_TIME_WAIT) 11664 return (unsigned long)sk; 11665 #endif 11666 11667 #if IS_BUILTIN(CONFIG_IPV6) 11668 if (sk && sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_TIME_WAIT) 11669 return (unsigned long)sk; 11670 #endif 11671 11672 return (unsigned long)NULL; 11673 } 11674 11675 const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto = { 11676 .func = bpf_skc_to_tcp_timewait_sock, 11677 .gpl_only = false, 11678 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, 11679 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, 11680 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_TW], 11681 }; 11682 11683 BPF_CALL_1(bpf_skc_to_tcp_request_sock, struct sock *, sk) 11684 { 11685 #ifdef CONFIG_INET 11686 if (sk && sk->sk_prot == &tcp_prot && sk->sk_state == TCP_NEW_SYN_RECV) 11687 return (unsigned long)sk; 11688 #endif 11689 11690 #if IS_BUILTIN(CONFIG_IPV6) 11691 if (sk && sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_NEW_SYN_RECV) 11692 return (unsigned long)sk; 11693 #endif 11694 11695 return (unsigned long)NULL; 11696 } 11697 11698 const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto = { 11699 .func = bpf_skc_to_tcp_request_sock, 11700 .gpl_only = false, 11701 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, 11702 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, 11703 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_REQ], 11704 }; 11705 11706 BPF_CALL_1(bpf_skc_to_udp6_sock, struct sock *, sk) 11707 { 11708 /* udp6_sock type is not generated in dwarf and hence btf, 11709 * trigger an explicit type generation here. 11710 */ 11711 BTF_TYPE_EMIT(struct udp6_sock); 11712 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_UDP && 11713 sk->sk_type == SOCK_DGRAM && sk->sk_family == AF_INET6) 11714 return (unsigned long)sk; 11715 11716 return (unsigned long)NULL; 11717 } 11718 11719 const struct bpf_func_proto bpf_skc_to_udp6_sock_proto = { 11720 .func = bpf_skc_to_udp6_sock, 11721 .gpl_only = false, 11722 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, 11723 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, 11724 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UDP6], 11725 }; 11726 11727 BPF_CALL_1(bpf_skc_to_unix_sock, struct sock *, sk) 11728 { 11729 /* unix_sock type is not generated in dwarf and hence btf, 11730 * trigger an explicit type generation here. 11731 */ 11732 BTF_TYPE_EMIT(struct unix_sock); 11733 if (sk && sk_fullsock(sk) && sk->sk_family == AF_UNIX) 11734 return (unsigned long)sk; 11735 11736 return (unsigned long)NULL; 11737 } 11738 11739 const struct bpf_func_proto bpf_skc_to_unix_sock_proto = { 11740 .func = bpf_skc_to_unix_sock, 11741 .gpl_only = false, 11742 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, 11743 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, 11744 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UNIX], 11745 }; 11746 11747 BPF_CALL_1(bpf_skc_to_mptcp_sock, struct sock *, sk) 11748 { 11749 BTF_TYPE_EMIT(struct mptcp_sock); 11750 return (unsigned long)bpf_mptcp_sock_from_subflow(sk); 11751 } 11752 11753 const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto = { 11754 .func = bpf_skc_to_mptcp_sock, 11755 .gpl_only = false, 11756 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, 11757 .arg1_type = ARG_PTR_TO_SOCK_COMMON, 11758 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_MPTCP], 11759 }; 11760 11761 BPF_CALL_1(bpf_sock_from_file, struct file *, file) 11762 { 11763 return (unsigned long)sock_from_file(file); 11764 } 11765 11766 BTF_ID_LIST(bpf_sock_from_file_btf_ids) 11767 BTF_ID(struct, socket) 11768 BTF_ID(struct, file) 11769 11770 const struct bpf_func_proto bpf_sock_from_file_proto = { 11771 .func = bpf_sock_from_file, 11772 .gpl_only = false, 11773 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, 11774 .ret_btf_id = &bpf_sock_from_file_btf_ids[0], 11775 .arg1_type = ARG_PTR_TO_BTF_ID, 11776 .arg1_btf_id = &bpf_sock_from_file_btf_ids[1], 11777 }; 11778 11779 static const struct bpf_func_proto * 11780 bpf_sk_base_func_proto(enum bpf_func_id func_id) 11781 { 11782 const struct bpf_func_proto *func; 11783 11784 switch (func_id) { 11785 case BPF_FUNC_skc_to_tcp6_sock: 11786 func = &bpf_skc_to_tcp6_sock_proto; 11787 break; 11788 case BPF_FUNC_skc_to_tcp_sock: 11789 func = &bpf_skc_to_tcp_sock_proto; 11790 break; 11791 case BPF_FUNC_skc_to_tcp_timewait_sock: 11792 func = &bpf_skc_to_tcp_timewait_sock_proto; 11793 break; 11794 case BPF_FUNC_skc_to_tcp_request_sock: 11795 func = &bpf_skc_to_tcp_request_sock_proto; 11796 break; 11797 case BPF_FUNC_skc_to_udp6_sock: 11798 func = &bpf_skc_to_udp6_sock_proto; 11799 break; 11800 case BPF_FUNC_skc_to_unix_sock: 11801 func = &bpf_skc_to_unix_sock_proto; 11802 break; 11803 case BPF_FUNC_skc_to_mptcp_sock: 11804 func = &bpf_skc_to_mptcp_sock_proto; 11805 break; 11806 case BPF_FUNC_ktime_get_coarse_ns: 11807 return &bpf_ktime_get_coarse_ns_proto; 11808 default: 11809 return bpf_base_func_proto(func_id); 11810 } 11811 11812 if (!perfmon_capable()) 11813 return NULL; 11814 11815 return func; 11816 } 11817 11818 __diag_push(); 11819 __diag_ignore_all("-Wmissing-prototypes", 11820 "Global functions as their definitions will be in vmlinux BTF"); 11821 __bpf_kfunc int bpf_dynptr_from_skb(struct sk_buff *skb, u64 flags, 11822 struct bpf_dynptr_kern *ptr__uninit) 11823 { 11824 if (flags) { 11825 bpf_dynptr_set_null(ptr__uninit); 11826 return -EINVAL; 11827 } 11828 11829 bpf_dynptr_init(ptr__uninit, skb, BPF_DYNPTR_TYPE_SKB, 0, skb->len); 11830 11831 return 0; 11832 } 11833 11834 __bpf_kfunc int bpf_dynptr_from_xdp(struct xdp_buff *xdp, u64 flags, 11835 struct bpf_dynptr_kern *ptr__uninit) 11836 { 11837 if (flags) { 11838 bpf_dynptr_set_null(ptr__uninit); 11839 return -EINVAL; 11840 } 11841 11842 bpf_dynptr_init(ptr__uninit, xdp, BPF_DYNPTR_TYPE_XDP, 0, xdp_get_buff_len(xdp)); 11843 11844 return 0; 11845 } 11846 11847 __bpf_kfunc int bpf_sock_addr_set_sun_path(struct bpf_sock_addr_kern *sa_kern, 11848 const u8 *sun_path, u32 sun_path__sz) 11849 { 11850 struct sockaddr_un *un; 11851 11852 if (sa_kern->sk->sk_family != AF_UNIX) 11853 return -EINVAL; 11854 11855 /* We do not allow changing the address to unnamed or larger than the 11856 * maximum allowed address size for a unix sockaddr. 11857 */ 11858 if (sun_path__sz == 0 || sun_path__sz > UNIX_PATH_MAX) 11859 return -EINVAL; 11860 11861 un = (struct sockaddr_un *)sa_kern->uaddr; 11862 memcpy(un->sun_path, sun_path, sun_path__sz); 11863 sa_kern->uaddrlen = offsetof(struct sockaddr_un, sun_path) + sun_path__sz; 11864 11865 return 0; 11866 } 11867 __diag_pop(); 11868 11869 int bpf_dynptr_from_skb_rdonly(struct sk_buff *skb, u64 flags, 11870 struct bpf_dynptr_kern *ptr__uninit) 11871 { 11872 int err; 11873 11874 err = bpf_dynptr_from_skb(skb, flags, ptr__uninit); 11875 if (err) 11876 return err; 11877 11878 bpf_dynptr_set_rdonly(ptr__uninit); 11879 11880 return 0; 11881 } 11882 11883 BTF_SET8_START(bpf_kfunc_check_set_skb) 11884 BTF_ID_FLAGS(func, bpf_dynptr_from_skb) 11885 BTF_SET8_END(bpf_kfunc_check_set_skb) 11886 11887 BTF_SET8_START(bpf_kfunc_check_set_xdp) 11888 BTF_ID_FLAGS(func, bpf_dynptr_from_xdp) 11889 BTF_SET8_END(bpf_kfunc_check_set_xdp) 11890 11891 BTF_SET8_START(bpf_kfunc_check_set_sock_addr) 11892 BTF_ID_FLAGS(func, bpf_sock_addr_set_sun_path) 11893 BTF_SET8_END(bpf_kfunc_check_set_sock_addr) 11894 11895 static const struct btf_kfunc_id_set bpf_kfunc_set_skb = { 11896 .owner = THIS_MODULE, 11897 .set = &bpf_kfunc_check_set_skb, 11898 }; 11899 11900 static const struct btf_kfunc_id_set bpf_kfunc_set_xdp = { 11901 .owner = THIS_MODULE, 11902 .set = &bpf_kfunc_check_set_xdp, 11903 }; 11904 11905 static const struct btf_kfunc_id_set bpf_kfunc_set_sock_addr = { 11906 .owner = THIS_MODULE, 11907 .set = &bpf_kfunc_check_set_sock_addr, 11908 }; 11909 11910 static int __init bpf_kfunc_init(void) 11911 { 11912 int ret; 11913 11914 ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_kfunc_set_skb); 11915 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_ACT, &bpf_kfunc_set_skb); 11916 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SK_SKB, &bpf_kfunc_set_skb); 11917 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SOCKET_FILTER, &bpf_kfunc_set_skb); 11918 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SKB, &bpf_kfunc_set_skb); 11919 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_OUT, &bpf_kfunc_set_skb); 11920 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_IN, &bpf_kfunc_set_skb); 11921 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_XMIT, &bpf_kfunc_set_skb); 11922 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_SEG6LOCAL, &bpf_kfunc_set_skb); 11923 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_NETFILTER, &bpf_kfunc_set_skb); 11924 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &bpf_kfunc_set_xdp); 11925 return ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SOCK_ADDR, 11926 &bpf_kfunc_set_sock_addr); 11927 } 11928 late_initcall(bpf_kfunc_init); 11929 11930 /* Disables missing prototype warnings */ 11931 __diag_push(); 11932 __diag_ignore_all("-Wmissing-prototypes", 11933 "Global functions as their definitions will be in vmlinux BTF"); 11934 11935 /* bpf_sock_destroy: Destroy the given socket with ECONNABORTED error code. 11936 * 11937 * The function expects a non-NULL pointer to a socket, and invokes the 11938 * protocol specific socket destroy handlers. 11939 * 11940 * The helper can only be called from BPF contexts that have acquired the socket 11941 * locks. 11942 * 11943 * Parameters: 11944 * @sock: Pointer to socket to be destroyed 11945 * 11946 * Return: 11947 * On error, may return EPROTONOSUPPORT, EINVAL. 11948 * EPROTONOSUPPORT if protocol specific destroy handler is not supported. 11949 * 0 otherwise 11950 */ 11951 __bpf_kfunc int bpf_sock_destroy(struct sock_common *sock) 11952 { 11953 struct sock *sk = (struct sock *)sock; 11954 11955 /* The locking semantics that allow for synchronous execution of the 11956 * destroy handlers are only supported for TCP and UDP. 11957 * Supporting protocols will need to acquire sock lock in the BPF context 11958 * prior to invoking this kfunc. 11959 */ 11960 if (!sk->sk_prot->diag_destroy || (sk->sk_protocol != IPPROTO_TCP && 11961 sk->sk_protocol != IPPROTO_UDP)) 11962 return -EOPNOTSUPP; 11963 11964 return sk->sk_prot->diag_destroy(sk, ECONNABORTED); 11965 } 11966 11967 __diag_pop() 11968 11969 BTF_SET8_START(bpf_sk_iter_kfunc_ids) 11970 BTF_ID_FLAGS(func, bpf_sock_destroy, KF_TRUSTED_ARGS) 11971 BTF_SET8_END(bpf_sk_iter_kfunc_ids) 11972 11973 static int tracing_iter_filter(const struct bpf_prog *prog, u32 kfunc_id) 11974 { 11975 if (btf_id_set8_contains(&bpf_sk_iter_kfunc_ids, kfunc_id) && 11976 prog->expected_attach_type != BPF_TRACE_ITER) 11977 return -EACCES; 11978 return 0; 11979 } 11980 11981 static const struct btf_kfunc_id_set bpf_sk_iter_kfunc_set = { 11982 .owner = THIS_MODULE, 11983 .set = &bpf_sk_iter_kfunc_ids, 11984 .filter = tracing_iter_filter, 11985 }; 11986 11987 static int init_subsystem(void) 11988 { 11989 return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_sk_iter_kfunc_set); 11990 } 11991 late_initcall(init_subsystem); 11992