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