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