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/module.h> 21 #include <linux/types.h> 22 #include <linux/mm.h> 23 #include <linux/fcntl.h> 24 #include <linux/socket.h> 25 #include <linux/sock_diag.h> 26 #include <linux/in.h> 27 #include <linux/inet.h> 28 #include <linux/netdevice.h> 29 #include <linux/if_packet.h> 30 #include <linux/if_arp.h> 31 #include <linux/gfp.h> 32 #include <net/inet_common.h> 33 #include <net/ip.h> 34 #include <net/protocol.h> 35 #include <net/netlink.h> 36 #include <linux/skbuff.h> 37 #include <linux/skmsg.h> 38 #include <net/sock.h> 39 #include <net/flow_dissector.h> 40 #include <linux/errno.h> 41 #include <linux/timer.h> 42 #include <linux/uaccess.h> 43 #include <asm/unaligned.h> 44 #include <asm/cmpxchg.h> 45 #include <linux/filter.h> 46 #include <linux/ratelimit.h> 47 #include <linux/seccomp.h> 48 #include <linux/if_vlan.h> 49 #include <linux/bpf.h> 50 #include <linux/btf.h> 51 #include <net/sch_generic.h> 52 #include <net/cls_cgroup.h> 53 #include <net/dst_metadata.h> 54 #include <net/dst.h> 55 #include <net/sock_reuseport.h> 56 #include <net/busy_poll.h> 57 #include <net/tcp.h> 58 #include <net/xfrm.h> 59 #include <net/udp.h> 60 #include <linux/bpf_trace.h> 61 #include <net/xdp_sock.h> 62 #include <linux/inetdevice.h> 63 #include <net/inet_hashtables.h> 64 #include <net/inet6_hashtables.h> 65 #include <net/ip_fib.h> 66 #include <net/nexthop.h> 67 #include <net/flow.h> 68 #include <net/arp.h> 69 #include <net/ipv6.h> 70 #include <net/net_namespace.h> 71 #include <linux/seg6_local.h> 72 #include <net/seg6.h> 73 #include <net/seg6_local.h> 74 #include <net/lwtunnel.h> 75 #include <net/ipv6_stubs.h> 76 #include <net/bpf_sk_storage.h> 77 #include <net/transp_v6.h> 78 #include <linux/btf_ids.h> 79 80 int copy_bpf_fprog_from_user(struct sock_fprog *dst, sockptr_t src, int len) 81 { 82 if (in_compat_syscall()) { 83 struct compat_sock_fprog f32; 84 85 if (len != sizeof(f32)) 86 return -EINVAL; 87 if (copy_from_sockptr(&f32, src, sizeof(f32))) 88 return -EFAULT; 89 memset(dst, 0, sizeof(*dst)); 90 dst->len = f32.len; 91 dst->filter = compat_ptr(f32.filter); 92 } else { 93 if (len != sizeof(*dst)) 94 return -EINVAL; 95 if (copy_from_sockptr(dst, src, sizeof(*dst))) 96 return -EFAULT; 97 } 98 99 return 0; 100 } 101 EXPORT_SYMBOL_GPL(copy_bpf_fprog_from_user); 102 103 /** 104 * sk_filter_trim_cap - run a packet through a socket filter 105 * @sk: sock associated with &sk_buff 106 * @skb: buffer to filter 107 * @cap: limit on how short the eBPF program may trim the packet 108 * 109 * Run the eBPF program and then cut skb->data to correct size returned by 110 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller 111 * than pkt_len we keep whole skb->data. This is the socket level 112 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should 113 * be accepted or -EPERM if the packet should be tossed. 114 * 115 */ 116 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap) 117 { 118 int err; 119 struct sk_filter *filter; 120 121 /* 122 * If the skb was allocated from pfmemalloc reserves, only 123 * allow SOCK_MEMALLOC sockets to use it as this socket is 124 * helping free memory 125 */ 126 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) { 127 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP); 128 return -ENOMEM; 129 } 130 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb); 131 if (err) 132 return err; 133 134 err = security_sock_rcv_skb(sk, skb); 135 if (err) 136 return err; 137 138 rcu_read_lock(); 139 filter = rcu_dereference(sk->sk_filter); 140 if (filter) { 141 struct sock *save_sk = skb->sk; 142 unsigned int pkt_len; 143 144 skb->sk = sk; 145 pkt_len = bpf_prog_run_save_cb(filter->prog, skb); 146 skb->sk = save_sk; 147 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM; 148 } 149 rcu_read_unlock(); 150 151 return err; 152 } 153 EXPORT_SYMBOL(sk_filter_trim_cap); 154 155 BPF_CALL_1(bpf_skb_get_pay_offset, struct sk_buff *, skb) 156 { 157 return skb_get_poff(skb); 158 } 159 160 BPF_CALL_3(bpf_skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x) 161 { 162 struct nlattr *nla; 163 164 if (skb_is_nonlinear(skb)) 165 return 0; 166 167 if (skb->len < sizeof(struct nlattr)) 168 return 0; 169 170 if (a > skb->len - sizeof(struct nlattr)) 171 return 0; 172 173 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x); 174 if (nla) 175 return (void *) nla - (void *) skb->data; 176 177 return 0; 178 } 179 180 BPF_CALL_3(bpf_skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x) 181 { 182 struct nlattr *nla; 183 184 if (skb_is_nonlinear(skb)) 185 return 0; 186 187 if (skb->len < sizeof(struct nlattr)) 188 return 0; 189 190 if (a > skb->len - sizeof(struct nlattr)) 191 return 0; 192 193 nla = (struct nlattr *) &skb->data[a]; 194 if (nla->nla_len > skb->len - a) 195 return 0; 196 197 nla = nla_find_nested(nla, x); 198 if (nla) 199 return (void *) nla - (void *) skb->data; 200 201 return 0; 202 } 203 204 BPF_CALL_4(bpf_skb_load_helper_8, const struct sk_buff *, skb, const void *, 205 data, int, headlen, int, offset) 206 { 207 u8 tmp, *ptr; 208 const int len = sizeof(tmp); 209 210 if (offset >= 0) { 211 if (headlen - offset >= len) 212 return *(u8 *)(data + offset); 213 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp))) 214 return tmp; 215 } else { 216 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len); 217 if (likely(ptr)) 218 return *(u8 *)ptr; 219 } 220 221 return -EFAULT; 222 } 223 224 BPF_CALL_2(bpf_skb_load_helper_8_no_cache, const struct sk_buff *, skb, 225 int, offset) 226 { 227 return ____bpf_skb_load_helper_8(skb, skb->data, skb->len - skb->data_len, 228 offset); 229 } 230 231 BPF_CALL_4(bpf_skb_load_helper_16, const struct sk_buff *, skb, const void *, 232 data, int, headlen, int, offset) 233 { 234 u16 tmp, *ptr; 235 const int len = sizeof(tmp); 236 237 if (offset >= 0) { 238 if (headlen - offset >= len) 239 return get_unaligned_be16(data + offset); 240 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp))) 241 return be16_to_cpu(tmp); 242 } else { 243 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len); 244 if (likely(ptr)) 245 return get_unaligned_be16(ptr); 246 } 247 248 return -EFAULT; 249 } 250 251 BPF_CALL_2(bpf_skb_load_helper_16_no_cache, const struct sk_buff *, skb, 252 int, offset) 253 { 254 return ____bpf_skb_load_helper_16(skb, skb->data, skb->len - skb->data_len, 255 offset); 256 } 257 258 BPF_CALL_4(bpf_skb_load_helper_32, const struct sk_buff *, skb, const void *, 259 data, int, headlen, int, offset) 260 { 261 u32 tmp, *ptr; 262 const int len = sizeof(tmp); 263 264 if (likely(offset >= 0)) { 265 if (headlen - offset >= len) 266 return get_unaligned_be32(data + offset); 267 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp))) 268 return be32_to_cpu(tmp); 269 } else { 270 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len); 271 if (likely(ptr)) 272 return get_unaligned_be32(ptr); 273 } 274 275 return -EFAULT; 276 } 277 278 BPF_CALL_2(bpf_skb_load_helper_32_no_cache, const struct sk_buff *, skb, 279 int, offset) 280 { 281 return ____bpf_skb_load_helper_32(skb, skb->data, skb->len - skb->data_len, 282 offset); 283 } 284 285 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg, 286 struct bpf_insn *insn_buf) 287 { 288 struct bpf_insn *insn = insn_buf; 289 290 switch (skb_field) { 291 case SKF_AD_MARK: 292 BUILD_BUG_ON(sizeof_field(struct sk_buff, mark) != 4); 293 294 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, 295 offsetof(struct sk_buff, mark)); 296 break; 297 298 case SKF_AD_PKTTYPE: 299 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET()); 300 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX); 301 #ifdef __BIG_ENDIAN_BITFIELD 302 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5); 303 #endif 304 break; 305 306 case SKF_AD_QUEUE: 307 BUILD_BUG_ON(sizeof_field(struct sk_buff, queue_mapping) != 2); 308 309 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, 310 offsetof(struct sk_buff, queue_mapping)); 311 break; 312 313 case SKF_AD_VLAN_TAG: 314 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_tci) != 2); 315 316 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */ 317 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, 318 offsetof(struct sk_buff, vlan_tci)); 319 break; 320 case SKF_AD_VLAN_TAG_PRESENT: 321 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_VLAN_PRESENT_OFFSET()); 322 if (PKT_VLAN_PRESENT_BIT) 323 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, PKT_VLAN_PRESENT_BIT); 324 if (PKT_VLAN_PRESENT_BIT < 7) 325 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1); 326 break; 327 } 328 329 return insn - insn_buf; 330 } 331 332 static bool convert_bpf_extensions(struct sock_filter *fp, 333 struct bpf_insn **insnp) 334 { 335 struct bpf_insn *insn = *insnp; 336 u32 cnt; 337 338 switch (fp->k) { 339 case SKF_AD_OFF + SKF_AD_PROTOCOL: 340 BUILD_BUG_ON(sizeof_field(struct sk_buff, protocol) != 2); 341 342 /* A = *(u16 *) (CTX + offsetof(protocol)) */ 343 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX, 344 offsetof(struct sk_buff, protocol)); 345 /* A = ntohs(A) [emitting a nop or swap16] */ 346 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16); 347 break; 348 349 case SKF_AD_OFF + SKF_AD_PKTTYPE: 350 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn); 351 insn += cnt - 1; 352 break; 353 354 case SKF_AD_OFF + SKF_AD_IFINDEX: 355 case SKF_AD_OFF + SKF_AD_HATYPE: 356 BUILD_BUG_ON(sizeof_field(struct net_device, ifindex) != 4); 357 BUILD_BUG_ON(sizeof_field(struct net_device, type) != 2); 358 359 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev), 360 BPF_REG_TMP, BPF_REG_CTX, 361 offsetof(struct sk_buff, dev)); 362 /* if (tmp != 0) goto pc + 1 */ 363 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1); 364 *insn++ = BPF_EXIT_INSN(); 365 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX) 366 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP, 367 offsetof(struct net_device, ifindex)); 368 else 369 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP, 370 offsetof(struct net_device, type)); 371 break; 372 373 case SKF_AD_OFF + SKF_AD_MARK: 374 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn); 375 insn += cnt - 1; 376 break; 377 378 case SKF_AD_OFF + SKF_AD_RXHASH: 379 BUILD_BUG_ON(sizeof_field(struct sk_buff, hash) != 4); 380 381 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, 382 offsetof(struct sk_buff, hash)); 383 break; 384 385 case SKF_AD_OFF + SKF_AD_QUEUE: 386 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn); 387 insn += cnt - 1; 388 break; 389 390 case SKF_AD_OFF + SKF_AD_VLAN_TAG: 391 cnt = convert_skb_access(SKF_AD_VLAN_TAG, 392 BPF_REG_A, BPF_REG_CTX, insn); 393 insn += cnt - 1; 394 break; 395 396 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT: 397 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT, 398 BPF_REG_A, BPF_REG_CTX, insn); 399 insn += cnt - 1; 400 break; 401 402 case SKF_AD_OFF + SKF_AD_VLAN_TPID: 403 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_proto) != 2); 404 405 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */ 406 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX, 407 offsetof(struct sk_buff, vlan_proto)); 408 /* A = ntohs(A) [emitting a nop or swap16] */ 409 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16); 410 break; 411 412 case SKF_AD_OFF + SKF_AD_PAY_OFFSET: 413 case SKF_AD_OFF + SKF_AD_NLATTR: 414 case SKF_AD_OFF + SKF_AD_NLATTR_NEST: 415 case SKF_AD_OFF + SKF_AD_CPU: 416 case SKF_AD_OFF + SKF_AD_RANDOM: 417 /* arg1 = CTX */ 418 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX); 419 /* arg2 = A */ 420 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A); 421 /* arg3 = X */ 422 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X); 423 /* Emit call(arg1=CTX, arg2=A, arg3=X) */ 424 switch (fp->k) { 425 case SKF_AD_OFF + SKF_AD_PAY_OFFSET: 426 *insn = BPF_EMIT_CALL(bpf_skb_get_pay_offset); 427 break; 428 case SKF_AD_OFF + SKF_AD_NLATTR: 429 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr); 430 break; 431 case SKF_AD_OFF + SKF_AD_NLATTR_NEST: 432 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr_nest); 433 break; 434 case SKF_AD_OFF + SKF_AD_CPU: 435 *insn = BPF_EMIT_CALL(bpf_get_raw_cpu_id); 436 break; 437 case SKF_AD_OFF + SKF_AD_RANDOM: 438 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32); 439 bpf_user_rnd_init_once(); 440 break; 441 } 442 break; 443 444 case SKF_AD_OFF + SKF_AD_ALU_XOR_X: 445 /* A ^= X */ 446 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X); 447 break; 448 449 default: 450 /* This is just a dummy call to avoid letting the compiler 451 * evict __bpf_call_base() as an optimization. Placed here 452 * where no-one bothers. 453 */ 454 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0); 455 return false; 456 } 457 458 *insnp = insn; 459 return true; 460 } 461 462 static bool convert_bpf_ld_abs(struct sock_filter *fp, struct bpf_insn **insnp) 463 { 464 const bool unaligned_ok = IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS); 465 int size = bpf_size_to_bytes(BPF_SIZE(fp->code)); 466 bool endian = BPF_SIZE(fp->code) == BPF_H || 467 BPF_SIZE(fp->code) == BPF_W; 468 bool indirect = BPF_MODE(fp->code) == BPF_IND; 469 const int ip_align = NET_IP_ALIGN; 470 struct bpf_insn *insn = *insnp; 471 int offset = fp->k; 472 473 if (!indirect && 474 ((unaligned_ok && offset >= 0) || 475 (!unaligned_ok && offset >= 0 && 476 offset + ip_align >= 0 && 477 offset + ip_align % size == 0))) { 478 bool ldx_off_ok = offset <= S16_MAX; 479 480 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_H); 481 if (offset) 482 *insn++ = BPF_ALU64_IMM(BPF_SUB, BPF_REG_TMP, offset); 483 *insn++ = BPF_JMP_IMM(BPF_JSLT, BPF_REG_TMP, 484 size, 2 + endian + (!ldx_off_ok * 2)); 485 if (ldx_off_ok) { 486 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A, 487 BPF_REG_D, offset); 488 } else { 489 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_D); 490 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_TMP, offset); 491 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A, 492 BPF_REG_TMP, 0); 493 } 494 if (endian) 495 *insn++ = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, size * 8); 496 *insn++ = BPF_JMP_A(8); 497 } 498 499 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX); 500 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_D); 501 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_H); 502 if (!indirect) { 503 *insn++ = BPF_MOV64_IMM(BPF_REG_ARG4, offset); 504 } else { 505 *insn++ = BPF_MOV64_REG(BPF_REG_ARG4, BPF_REG_X); 506 if (fp->k) 507 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG4, offset); 508 } 509 510 switch (BPF_SIZE(fp->code)) { 511 case BPF_B: 512 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8); 513 break; 514 case BPF_H: 515 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16); 516 break; 517 case BPF_W: 518 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32); 519 break; 520 default: 521 return false; 522 } 523 524 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_A, 0, 2); 525 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A); 526 *insn = BPF_EXIT_INSN(); 527 528 *insnp = insn; 529 return true; 530 } 531 532 /** 533 * bpf_convert_filter - convert filter program 534 * @prog: the user passed filter program 535 * @len: the length of the user passed filter program 536 * @new_prog: allocated 'struct bpf_prog' or NULL 537 * @new_len: pointer to store length of converted program 538 * @seen_ld_abs: bool whether we've seen ld_abs/ind 539 * 540 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn' 541 * style extended BPF (eBPF). 542 * Conversion workflow: 543 * 544 * 1) First pass for calculating the new program length: 545 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs) 546 * 547 * 2) 2nd pass to remap in two passes: 1st pass finds new 548 * jump offsets, 2nd pass remapping: 549 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs) 550 */ 551 static int bpf_convert_filter(struct sock_filter *prog, int len, 552 struct bpf_prog *new_prog, int *new_len, 553 bool *seen_ld_abs) 554 { 555 int new_flen = 0, pass = 0, target, i, stack_off; 556 struct bpf_insn *new_insn, *first_insn = NULL; 557 struct sock_filter *fp; 558 int *addrs = NULL; 559 u8 bpf_src; 560 561 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK); 562 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); 563 564 if (len <= 0 || len > BPF_MAXINSNS) 565 return -EINVAL; 566 567 if (new_prog) { 568 first_insn = new_prog->insnsi; 569 addrs = kcalloc(len, sizeof(*addrs), 570 GFP_KERNEL | __GFP_NOWARN); 571 if (!addrs) 572 return -ENOMEM; 573 } 574 575 do_pass: 576 new_insn = first_insn; 577 fp = prog; 578 579 /* Classic BPF related prologue emission. */ 580 if (new_prog) { 581 /* Classic BPF expects A and X to be reset first. These need 582 * to be guaranteed to be the first two instructions. 583 */ 584 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A); 585 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X); 586 587 /* All programs must keep CTX in callee saved BPF_REG_CTX. 588 * In eBPF case it's done by the compiler, here we need to 589 * do this ourself. Initial CTX is present in BPF_REG_ARG1. 590 */ 591 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1); 592 if (*seen_ld_abs) { 593 /* For packet access in classic BPF, cache skb->data 594 * in callee-saved BPF R8 and skb->len - skb->data_len 595 * (headlen) in BPF R9. Since classic BPF is read-only 596 * on CTX, we only need to cache it once. 597 */ 598 *new_insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data), 599 BPF_REG_D, BPF_REG_CTX, 600 offsetof(struct sk_buff, data)); 601 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_H, BPF_REG_CTX, 602 offsetof(struct sk_buff, len)); 603 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_TMP, BPF_REG_CTX, 604 offsetof(struct sk_buff, data_len)); 605 *new_insn++ = BPF_ALU32_REG(BPF_SUB, BPF_REG_H, BPF_REG_TMP); 606 } 607 } else { 608 new_insn += 3; 609 } 610 611 for (i = 0; i < len; fp++, i++) { 612 struct bpf_insn tmp_insns[32] = { }; 613 struct bpf_insn *insn = tmp_insns; 614 615 if (addrs) 616 addrs[i] = new_insn - first_insn; 617 618 switch (fp->code) { 619 /* All arithmetic insns and skb loads map as-is. */ 620 case BPF_ALU | BPF_ADD | BPF_X: 621 case BPF_ALU | BPF_ADD | BPF_K: 622 case BPF_ALU | BPF_SUB | BPF_X: 623 case BPF_ALU | BPF_SUB | BPF_K: 624 case BPF_ALU | BPF_AND | BPF_X: 625 case BPF_ALU | BPF_AND | BPF_K: 626 case BPF_ALU | BPF_OR | BPF_X: 627 case BPF_ALU | BPF_OR | BPF_K: 628 case BPF_ALU | BPF_LSH | BPF_X: 629 case BPF_ALU | BPF_LSH | BPF_K: 630 case BPF_ALU | BPF_RSH | BPF_X: 631 case BPF_ALU | BPF_RSH | BPF_K: 632 case BPF_ALU | BPF_XOR | BPF_X: 633 case BPF_ALU | BPF_XOR | BPF_K: 634 case BPF_ALU | BPF_MUL | BPF_X: 635 case BPF_ALU | BPF_MUL | BPF_K: 636 case BPF_ALU | BPF_DIV | BPF_X: 637 case BPF_ALU | BPF_DIV | BPF_K: 638 case BPF_ALU | BPF_MOD | BPF_X: 639 case BPF_ALU | BPF_MOD | BPF_K: 640 case BPF_ALU | BPF_NEG: 641 case BPF_LD | BPF_ABS | BPF_W: 642 case BPF_LD | BPF_ABS | BPF_H: 643 case BPF_LD | BPF_ABS | BPF_B: 644 case BPF_LD | BPF_IND | BPF_W: 645 case BPF_LD | BPF_IND | BPF_H: 646 case BPF_LD | BPF_IND | BPF_B: 647 /* Check for overloaded BPF extension and 648 * directly convert it if found, otherwise 649 * just move on with mapping. 650 */ 651 if (BPF_CLASS(fp->code) == BPF_LD && 652 BPF_MODE(fp->code) == BPF_ABS && 653 convert_bpf_extensions(fp, &insn)) 654 break; 655 if (BPF_CLASS(fp->code) == BPF_LD && 656 convert_bpf_ld_abs(fp, &insn)) { 657 *seen_ld_abs = true; 658 break; 659 } 660 661 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) || 662 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) { 663 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X); 664 /* Error with exception code on div/mod by 0. 665 * For cBPF programs, this was always return 0. 666 */ 667 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2); 668 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A); 669 *insn++ = BPF_EXIT_INSN(); 670 } 671 672 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k); 673 break; 674 675 /* Jump transformation cannot use BPF block macros 676 * everywhere as offset calculation and target updates 677 * require a bit more work than the rest, i.e. jump 678 * opcodes map as-is, but offsets need adjustment. 679 */ 680 681 #define BPF_EMIT_JMP \ 682 do { \ 683 const s32 off_min = S16_MIN, off_max = S16_MAX; \ 684 s32 off; \ 685 \ 686 if (target >= len || target < 0) \ 687 goto err; \ 688 off = addrs ? addrs[target] - addrs[i] - 1 : 0; \ 689 /* Adjust pc relative offset for 2nd or 3rd insn. */ \ 690 off -= insn - tmp_insns; \ 691 /* Reject anything not fitting into insn->off. */ \ 692 if (off < off_min || off > off_max) \ 693 goto err; \ 694 insn->off = off; \ 695 } while (0) 696 697 case BPF_JMP | BPF_JA: 698 target = i + fp->k + 1; 699 insn->code = fp->code; 700 BPF_EMIT_JMP; 701 break; 702 703 case BPF_JMP | BPF_JEQ | BPF_K: 704 case BPF_JMP | BPF_JEQ | BPF_X: 705 case BPF_JMP | BPF_JSET | BPF_K: 706 case BPF_JMP | BPF_JSET | BPF_X: 707 case BPF_JMP | BPF_JGT | BPF_K: 708 case BPF_JMP | BPF_JGT | BPF_X: 709 case BPF_JMP | BPF_JGE | BPF_K: 710 case BPF_JMP | BPF_JGE | BPF_X: 711 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) { 712 /* BPF immediates are signed, zero extend 713 * immediate into tmp register and use it 714 * in compare insn. 715 */ 716 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k); 717 718 insn->dst_reg = BPF_REG_A; 719 insn->src_reg = BPF_REG_TMP; 720 bpf_src = BPF_X; 721 } else { 722 insn->dst_reg = BPF_REG_A; 723 insn->imm = fp->k; 724 bpf_src = BPF_SRC(fp->code); 725 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0; 726 } 727 728 /* Common case where 'jump_false' is next insn. */ 729 if (fp->jf == 0) { 730 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src; 731 target = i + fp->jt + 1; 732 BPF_EMIT_JMP; 733 break; 734 } 735 736 /* Convert some jumps when 'jump_true' is next insn. */ 737 if (fp->jt == 0) { 738 switch (BPF_OP(fp->code)) { 739 case BPF_JEQ: 740 insn->code = BPF_JMP | BPF_JNE | bpf_src; 741 break; 742 case BPF_JGT: 743 insn->code = BPF_JMP | BPF_JLE | bpf_src; 744 break; 745 case BPF_JGE: 746 insn->code = BPF_JMP | BPF_JLT | bpf_src; 747 break; 748 default: 749 goto jmp_rest; 750 } 751 752 target = i + fp->jf + 1; 753 BPF_EMIT_JMP; 754 break; 755 } 756 jmp_rest: 757 /* Other jumps are mapped into two insns: Jxx and JA. */ 758 target = i + fp->jt + 1; 759 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src; 760 BPF_EMIT_JMP; 761 insn++; 762 763 insn->code = BPF_JMP | BPF_JA; 764 target = i + fp->jf + 1; 765 BPF_EMIT_JMP; 766 break; 767 768 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */ 769 case BPF_LDX | BPF_MSH | BPF_B: { 770 struct sock_filter tmp = { 771 .code = BPF_LD | BPF_ABS | BPF_B, 772 .k = fp->k, 773 }; 774 775 *seen_ld_abs = true; 776 777 /* X = A */ 778 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A); 779 /* A = BPF_R0 = *(u8 *) (skb->data + K) */ 780 convert_bpf_ld_abs(&tmp, &insn); 781 insn++; 782 /* A &= 0xf */ 783 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf); 784 /* A <<= 2 */ 785 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2); 786 /* tmp = X */ 787 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_X); 788 /* X = A */ 789 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A); 790 /* A = tmp */ 791 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP); 792 break; 793 } 794 /* RET_K is remaped into 2 insns. RET_A case doesn't need an 795 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A. 796 */ 797 case BPF_RET | BPF_A: 798 case BPF_RET | BPF_K: 799 if (BPF_RVAL(fp->code) == BPF_K) 800 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0, 801 0, fp->k); 802 *insn = BPF_EXIT_INSN(); 803 break; 804 805 /* Store to stack. */ 806 case BPF_ST: 807 case BPF_STX: 808 stack_off = fp->k * 4 + 4; 809 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) == 810 BPF_ST ? BPF_REG_A : BPF_REG_X, 811 -stack_off); 812 /* check_load_and_stores() verifies that classic BPF can 813 * load from stack only after write, so tracking 814 * stack_depth for ST|STX insns is enough 815 */ 816 if (new_prog && new_prog->aux->stack_depth < stack_off) 817 new_prog->aux->stack_depth = stack_off; 818 break; 819 820 /* Load from stack. */ 821 case BPF_LD | BPF_MEM: 822 case BPF_LDX | BPF_MEM: 823 stack_off = fp->k * 4 + 4; 824 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ? 825 BPF_REG_A : BPF_REG_X, BPF_REG_FP, 826 -stack_off); 827 break; 828 829 /* A = K or X = K */ 830 case BPF_LD | BPF_IMM: 831 case BPF_LDX | BPF_IMM: 832 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ? 833 BPF_REG_A : BPF_REG_X, fp->k); 834 break; 835 836 /* X = A */ 837 case BPF_MISC | BPF_TAX: 838 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A); 839 break; 840 841 /* A = X */ 842 case BPF_MISC | BPF_TXA: 843 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X); 844 break; 845 846 /* A = skb->len or X = skb->len */ 847 case BPF_LD | BPF_W | BPF_LEN: 848 case BPF_LDX | BPF_W | BPF_LEN: 849 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ? 850 BPF_REG_A : BPF_REG_X, BPF_REG_CTX, 851 offsetof(struct sk_buff, len)); 852 break; 853 854 /* Access seccomp_data fields. */ 855 case BPF_LDX | BPF_ABS | BPF_W: 856 /* A = *(u32 *) (ctx + K) */ 857 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k); 858 break; 859 860 /* Unknown instruction. */ 861 default: 862 goto err; 863 } 864 865 insn++; 866 if (new_prog) 867 memcpy(new_insn, tmp_insns, 868 sizeof(*insn) * (insn - tmp_insns)); 869 new_insn += insn - tmp_insns; 870 } 871 872 if (!new_prog) { 873 /* Only calculating new length. */ 874 *new_len = new_insn - first_insn; 875 if (*seen_ld_abs) 876 *new_len += 4; /* Prologue bits. */ 877 return 0; 878 } 879 880 pass++; 881 if (new_flen != new_insn - first_insn) { 882 new_flen = new_insn - first_insn; 883 if (pass > 2) 884 goto err; 885 goto do_pass; 886 } 887 888 kfree(addrs); 889 BUG_ON(*new_len != new_flen); 890 return 0; 891 err: 892 kfree(addrs); 893 return -EINVAL; 894 } 895 896 /* Security: 897 * 898 * As we dont want to clear mem[] array for each packet going through 899 * __bpf_prog_run(), we check that filter loaded by user never try to read 900 * a cell if not previously written, and we check all branches to be sure 901 * a malicious user doesn't try to abuse us. 902 */ 903 static int check_load_and_stores(const struct sock_filter *filter, int flen) 904 { 905 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */ 906 int pc, ret = 0; 907 908 BUILD_BUG_ON(BPF_MEMWORDS > 16); 909 910 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL); 911 if (!masks) 912 return -ENOMEM; 913 914 memset(masks, 0xff, flen * sizeof(*masks)); 915 916 for (pc = 0; pc < flen; pc++) { 917 memvalid &= masks[pc]; 918 919 switch (filter[pc].code) { 920 case BPF_ST: 921 case BPF_STX: 922 memvalid |= (1 << filter[pc].k); 923 break; 924 case BPF_LD | BPF_MEM: 925 case BPF_LDX | BPF_MEM: 926 if (!(memvalid & (1 << filter[pc].k))) { 927 ret = -EINVAL; 928 goto error; 929 } 930 break; 931 case BPF_JMP | BPF_JA: 932 /* A jump must set masks on target */ 933 masks[pc + 1 + filter[pc].k] &= memvalid; 934 memvalid = ~0; 935 break; 936 case BPF_JMP | BPF_JEQ | BPF_K: 937 case BPF_JMP | BPF_JEQ | BPF_X: 938 case BPF_JMP | BPF_JGE | BPF_K: 939 case BPF_JMP | BPF_JGE | BPF_X: 940 case BPF_JMP | BPF_JGT | BPF_K: 941 case BPF_JMP | BPF_JGT | BPF_X: 942 case BPF_JMP | BPF_JSET | BPF_K: 943 case BPF_JMP | BPF_JSET | BPF_X: 944 /* A jump must set masks on targets */ 945 masks[pc + 1 + filter[pc].jt] &= memvalid; 946 masks[pc + 1 + filter[pc].jf] &= memvalid; 947 memvalid = ~0; 948 break; 949 } 950 } 951 error: 952 kfree(masks); 953 return ret; 954 } 955 956 static bool chk_code_allowed(u16 code_to_probe) 957 { 958 static const bool codes[] = { 959 /* 32 bit ALU operations */ 960 [BPF_ALU | BPF_ADD | BPF_K] = true, 961 [BPF_ALU | BPF_ADD | BPF_X] = true, 962 [BPF_ALU | BPF_SUB | BPF_K] = true, 963 [BPF_ALU | BPF_SUB | BPF_X] = true, 964 [BPF_ALU | BPF_MUL | BPF_K] = true, 965 [BPF_ALU | BPF_MUL | BPF_X] = true, 966 [BPF_ALU | BPF_DIV | BPF_K] = true, 967 [BPF_ALU | BPF_DIV | BPF_X] = true, 968 [BPF_ALU | BPF_MOD | BPF_K] = true, 969 [BPF_ALU | BPF_MOD | BPF_X] = true, 970 [BPF_ALU | BPF_AND | BPF_K] = true, 971 [BPF_ALU | BPF_AND | BPF_X] = true, 972 [BPF_ALU | BPF_OR | BPF_K] = true, 973 [BPF_ALU | BPF_OR | BPF_X] = true, 974 [BPF_ALU | BPF_XOR | BPF_K] = true, 975 [BPF_ALU | BPF_XOR | BPF_X] = true, 976 [BPF_ALU | BPF_LSH | BPF_K] = true, 977 [BPF_ALU | BPF_LSH | BPF_X] = true, 978 [BPF_ALU | BPF_RSH | BPF_K] = true, 979 [BPF_ALU | BPF_RSH | BPF_X] = true, 980 [BPF_ALU | BPF_NEG] = true, 981 /* Load instructions */ 982 [BPF_LD | BPF_W | BPF_ABS] = true, 983 [BPF_LD | BPF_H | BPF_ABS] = true, 984 [BPF_LD | BPF_B | BPF_ABS] = true, 985 [BPF_LD | BPF_W | BPF_LEN] = true, 986 [BPF_LD | BPF_W | BPF_IND] = true, 987 [BPF_LD | BPF_H | BPF_IND] = true, 988 [BPF_LD | BPF_B | BPF_IND] = true, 989 [BPF_LD | BPF_IMM] = true, 990 [BPF_LD | BPF_MEM] = true, 991 [BPF_LDX | BPF_W | BPF_LEN] = true, 992 [BPF_LDX | BPF_B | BPF_MSH] = true, 993 [BPF_LDX | BPF_IMM] = true, 994 [BPF_LDX | BPF_MEM] = true, 995 /* Store instructions */ 996 [BPF_ST] = true, 997 [BPF_STX] = true, 998 /* Misc instructions */ 999 [BPF_MISC | BPF_TAX] = true, 1000 [BPF_MISC | BPF_TXA] = true, 1001 /* Return instructions */ 1002 [BPF_RET | BPF_K] = true, 1003 [BPF_RET | BPF_A] = true, 1004 /* Jump instructions */ 1005 [BPF_JMP | BPF_JA] = true, 1006 [BPF_JMP | BPF_JEQ | BPF_K] = true, 1007 [BPF_JMP | BPF_JEQ | BPF_X] = true, 1008 [BPF_JMP | BPF_JGE | BPF_K] = true, 1009 [BPF_JMP | BPF_JGE | BPF_X] = true, 1010 [BPF_JMP | BPF_JGT | BPF_K] = true, 1011 [BPF_JMP | BPF_JGT | BPF_X] = true, 1012 [BPF_JMP | BPF_JSET | BPF_K] = true, 1013 [BPF_JMP | BPF_JSET | BPF_X] = true, 1014 }; 1015 1016 if (code_to_probe >= ARRAY_SIZE(codes)) 1017 return false; 1018 1019 return codes[code_to_probe]; 1020 } 1021 1022 static bool bpf_check_basics_ok(const struct sock_filter *filter, 1023 unsigned int flen) 1024 { 1025 if (filter == NULL) 1026 return false; 1027 if (flen == 0 || flen > BPF_MAXINSNS) 1028 return false; 1029 1030 return true; 1031 } 1032 1033 /** 1034 * bpf_check_classic - verify socket filter code 1035 * @filter: filter to verify 1036 * @flen: length of filter 1037 * 1038 * Check the user's filter code. If we let some ugly 1039 * filter code slip through kaboom! The filter must contain 1040 * no references or jumps that are out of range, no illegal 1041 * instructions, and must end with a RET instruction. 1042 * 1043 * All jumps are forward as they are not signed. 1044 * 1045 * Returns 0 if the rule set is legal or -EINVAL if not. 1046 */ 1047 static int bpf_check_classic(const struct sock_filter *filter, 1048 unsigned int flen) 1049 { 1050 bool anc_found; 1051 int pc; 1052 1053 /* Check the filter code now */ 1054 for (pc = 0; pc < flen; pc++) { 1055 const struct sock_filter *ftest = &filter[pc]; 1056 1057 /* May we actually operate on this code? */ 1058 if (!chk_code_allowed(ftest->code)) 1059 return -EINVAL; 1060 1061 /* Some instructions need special checks */ 1062 switch (ftest->code) { 1063 case BPF_ALU | BPF_DIV | BPF_K: 1064 case BPF_ALU | BPF_MOD | BPF_K: 1065 /* Check for division by zero */ 1066 if (ftest->k == 0) 1067 return -EINVAL; 1068 break; 1069 case BPF_ALU | BPF_LSH | BPF_K: 1070 case BPF_ALU | BPF_RSH | BPF_K: 1071 if (ftest->k >= 32) 1072 return -EINVAL; 1073 break; 1074 case BPF_LD | BPF_MEM: 1075 case BPF_LDX | BPF_MEM: 1076 case BPF_ST: 1077 case BPF_STX: 1078 /* Check for invalid memory addresses */ 1079 if (ftest->k >= BPF_MEMWORDS) 1080 return -EINVAL; 1081 break; 1082 case BPF_JMP | BPF_JA: 1083 /* Note, the large ftest->k might cause loops. 1084 * Compare this with conditional jumps below, 1085 * where offsets are limited. --ANK (981016) 1086 */ 1087 if (ftest->k >= (unsigned int)(flen - pc - 1)) 1088 return -EINVAL; 1089 break; 1090 case BPF_JMP | BPF_JEQ | BPF_K: 1091 case BPF_JMP | BPF_JEQ | BPF_X: 1092 case BPF_JMP | BPF_JGE | BPF_K: 1093 case BPF_JMP | BPF_JGE | BPF_X: 1094 case BPF_JMP | BPF_JGT | BPF_K: 1095 case BPF_JMP | BPF_JGT | BPF_X: 1096 case BPF_JMP | BPF_JSET | BPF_K: 1097 case BPF_JMP | BPF_JSET | BPF_X: 1098 /* Both conditionals must be safe */ 1099 if (pc + ftest->jt + 1 >= flen || 1100 pc + ftest->jf + 1 >= flen) 1101 return -EINVAL; 1102 break; 1103 case BPF_LD | BPF_W | BPF_ABS: 1104 case BPF_LD | BPF_H | BPF_ABS: 1105 case BPF_LD | BPF_B | BPF_ABS: 1106 anc_found = false; 1107 if (bpf_anc_helper(ftest) & BPF_ANC) 1108 anc_found = true; 1109 /* Ancillary operation unknown or unsupported */ 1110 if (anc_found == false && ftest->k >= SKF_AD_OFF) 1111 return -EINVAL; 1112 } 1113 } 1114 1115 /* Last instruction must be a RET code */ 1116 switch (filter[flen - 1].code) { 1117 case BPF_RET | BPF_K: 1118 case BPF_RET | BPF_A: 1119 return check_load_and_stores(filter, flen); 1120 } 1121 1122 return -EINVAL; 1123 } 1124 1125 static int bpf_prog_store_orig_filter(struct bpf_prog *fp, 1126 const struct sock_fprog *fprog) 1127 { 1128 unsigned int fsize = bpf_classic_proglen(fprog); 1129 struct sock_fprog_kern *fkprog; 1130 1131 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL); 1132 if (!fp->orig_prog) 1133 return -ENOMEM; 1134 1135 fkprog = fp->orig_prog; 1136 fkprog->len = fprog->len; 1137 1138 fkprog->filter = kmemdup(fp->insns, fsize, 1139 GFP_KERNEL | __GFP_NOWARN); 1140 if (!fkprog->filter) { 1141 kfree(fp->orig_prog); 1142 return -ENOMEM; 1143 } 1144 1145 return 0; 1146 } 1147 1148 static void bpf_release_orig_filter(struct bpf_prog *fp) 1149 { 1150 struct sock_fprog_kern *fprog = fp->orig_prog; 1151 1152 if (fprog) { 1153 kfree(fprog->filter); 1154 kfree(fprog); 1155 } 1156 } 1157 1158 static void __bpf_prog_release(struct bpf_prog *prog) 1159 { 1160 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) { 1161 bpf_prog_put(prog); 1162 } else { 1163 bpf_release_orig_filter(prog); 1164 bpf_prog_free(prog); 1165 } 1166 } 1167 1168 static void __sk_filter_release(struct sk_filter *fp) 1169 { 1170 __bpf_prog_release(fp->prog); 1171 kfree(fp); 1172 } 1173 1174 /** 1175 * sk_filter_release_rcu - Release a socket filter by rcu_head 1176 * @rcu: rcu_head that contains the sk_filter to free 1177 */ 1178 static void sk_filter_release_rcu(struct rcu_head *rcu) 1179 { 1180 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu); 1181 1182 __sk_filter_release(fp); 1183 } 1184 1185 /** 1186 * sk_filter_release - release a socket filter 1187 * @fp: filter to remove 1188 * 1189 * Remove a filter from a socket and release its resources. 1190 */ 1191 static void sk_filter_release(struct sk_filter *fp) 1192 { 1193 if (refcount_dec_and_test(&fp->refcnt)) 1194 call_rcu(&fp->rcu, sk_filter_release_rcu); 1195 } 1196 1197 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp) 1198 { 1199 u32 filter_size = bpf_prog_size(fp->prog->len); 1200 1201 atomic_sub(filter_size, &sk->sk_omem_alloc); 1202 sk_filter_release(fp); 1203 } 1204 1205 /* try to charge the socket memory if there is space available 1206 * return true on success 1207 */ 1208 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp) 1209 { 1210 u32 filter_size = bpf_prog_size(fp->prog->len); 1211 1212 /* same check as in sock_kmalloc() */ 1213 if (filter_size <= sysctl_optmem_max && 1214 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) { 1215 atomic_add(filter_size, &sk->sk_omem_alloc); 1216 return true; 1217 } 1218 return false; 1219 } 1220 1221 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp) 1222 { 1223 if (!refcount_inc_not_zero(&fp->refcnt)) 1224 return false; 1225 1226 if (!__sk_filter_charge(sk, fp)) { 1227 sk_filter_release(fp); 1228 return false; 1229 } 1230 return true; 1231 } 1232 1233 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp) 1234 { 1235 struct sock_filter *old_prog; 1236 struct bpf_prog *old_fp; 1237 int err, new_len, old_len = fp->len; 1238 bool seen_ld_abs = false; 1239 1240 /* We are free to overwrite insns et al right here as it 1241 * won't be used at this point in time anymore internally 1242 * after the migration to the internal BPF instruction 1243 * representation. 1244 */ 1245 BUILD_BUG_ON(sizeof(struct sock_filter) != 1246 sizeof(struct bpf_insn)); 1247 1248 /* Conversion cannot happen on overlapping memory areas, 1249 * so we need to keep the user BPF around until the 2nd 1250 * pass. At this time, the user BPF is stored in fp->insns. 1251 */ 1252 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter), 1253 GFP_KERNEL | __GFP_NOWARN); 1254 if (!old_prog) { 1255 err = -ENOMEM; 1256 goto out_err; 1257 } 1258 1259 /* 1st pass: calculate the new program length. */ 1260 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len, 1261 &seen_ld_abs); 1262 if (err) 1263 goto out_err_free; 1264 1265 /* Expand fp for appending the new filter representation. */ 1266 old_fp = fp; 1267 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0); 1268 if (!fp) { 1269 /* The old_fp is still around in case we couldn't 1270 * allocate new memory, so uncharge on that one. 1271 */ 1272 fp = old_fp; 1273 err = -ENOMEM; 1274 goto out_err_free; 1275 } 1276 1277 fp->len = new_len; 1278 1279 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */ 1280 err = bpf_convert_filter(old_prog, old_len, fp, &new_len, 1281 &seen_ld_abs); 1282 if (err) 1283 /* 2nd bpf_convert_filter() can fail only if it fails 1284 * to allocate memory, remapping must succeed. Note, 1285 * that at this time old_fp has already been released 1286 * by krealloc(). 1287 */ 1288 goto out_err_free; 1289 1290 fp = bpf_prog_select_runtime(fp, &err); 1291 if (err) 1292 goto out_err_free; 1293 1294 kfree(old_prog); 1295 return fp; 1296 1297 out_err_free: 1298 kfree(old_prog); 1299 out_err: 1300 __bpf_prog_release(fp); 1301 return ERR_PTR(err); 1302 } 1303 1304 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp, 1305 bpf_aux_classic_check_t trans) 1306 { 1307 int err; 1308 1309 fp->bpf_func = NULL; 1310 fp->jited = 0; 1311 1312 err = bpf_check_classic(fp->insns, fp->len); 1313 if (err) { 1314 __bpf_prog_release(fp); 1315 return ERR_PTR(err); 1316 } 1317 1318 /* There might be additional checks and transformations 1319 * needed on classic filters, f.e. in case of seccomp. 1320 */ 1321 if (trans) { 1322 err = trans(fp->insns, fp->len); 1323 if (err) { 1324 __bpf_prog_release(fp); 1325 return ERR_PTR(err); 1326 } 1327 } 1328 1329 /* Probe if we can JIT compile the filter and if so, do 1330 * the compilation of the filter. 1331 */ 1332 bpf_jit_compile(fp); 1333 1334 /* JIT compiler couldn't process this filter, so do the 1335 * internal BPF translation for the optimized interpreter. 1336 */ 1337 if (!fp->jited) 1338 fp = bpf_migrate_filter(fp); 1339 1340 return fp; 1341 } 1342 1343 /** 1344 * bpf_prog_create - create an unattached filter 1345 * @pfp: the unattached filter that is created 1346 * @fprog: the filter program 1347 * 1348 * Create a filter independent of any socket. We first run some 1349 * sanity checks on it to make sure it does not explode on us later. 1350 * If an error occurs or there is insufficient memory for the filter 1351 * a negative errno code is returned. On success the return is zero. 1352 */ 1353 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog) 1354 { 1355 unsigned int fsize = bpf_classic_proglen(fprog); 1356 struct bpf_prog *fp; 1357 1358 /* Make sure new filter is there and in the right amounts. */ 1359 if (!bpf_check_basics_ok(fprog->filter, fprog->len)) 1360 return -EINVAL; 1361 1362 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0); 1363 if (!fp) 1364 return -ENOMEM; 1365 1366 memcpy(fp->insns, fprog->filter, fsize); 1367 1368 fp->len = fprog->len; 1369 /* Since unattached filters are not copied back to user 1370 * space through sk_get_filter(), we do not need to hold 1371 * a copy here, and can spare us the work. 1372 */ 1373 fp->orig_prog = NULL; 1374 1375 /* bpf_prepare_filter() already takes care of freeing 1376 * memory in case something goes wrong. 1377 */ 1378 fp = bpf_prepare_filter(fp, NULL); 1379 if (IS_ERR(fp)) 1380 return PTR_ERR(fp); 1381 1382 *pfp = fp; 1383 return 0; 1384 } 1385 EXPORT_SYMBOL_GPL(bpf_prog_create); 1386 1387 /** 1388 * bpf_prog_create_from_user - create an unattached filter from user buffer 1389 * @pfp: the unattached filter that is created 1390 * @fprog: the filter program 1391 * @trans: post-classic verifier transformation handler 1392 * @save_orig: save classic BPF program 1393 * 1394 * This function effectively does the same as bpf_prog_create(), only 1395 * that it builds up its insns buffer from user space provided buffer. 1396 * It also allows for passing a bpf_aux_classic_check_t handler. 1397 */ 1398 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog, 1399 bpf_aux_classic_check_t trans, bool save_orig) 1400 { 1401 unsigned int fsize = bpf_classic_proglen(fprog); 1402 struct bpf_prog *fp; 1403 int err; 1404 1405 /* Make sure new filter is there and in the right amounts. */ 1406 if (!bpf_check_basics_ok(fprog->filter, fprog->len)) 1407 return -EINVAL; 1408 1409 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0); 1410 if (!fp) 1411 return -ENOMEM; 1412 1413 if (copy_from_user(fp->insns, fprog->filter, fsize)) { 1414 __bpf_prog_free(fp); 1415 return -EFAULT; 1416 } 1417 1418 fp->len = fprog->len; 1419 fp->orig_prog = NULL; 1420 1421 if (save_orig) { 1422 err = bpf_prog_store_orig_filter(fp, fprog); 1423 if (err) { 1424 __bpf_prog_free(fp); 1425 return -ENOMEM; 1426 } 1427 } 1428 1429 /* bpf_prepare_filter() already takes care of freeing 1430 * memory in case something goes wrong. 1431 */ 1432 fp = bpf_prepare_filter(fp, trans); 1433 if (IS_ERR(fp)) 1434 return PTR_ERR(fp); 1435 1436 *pfp = fp; 1437 return 0; 1438 } 1439 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user); 1440 1441 void bpf_prog_destroy(struct bpf_prog *fp) 1442 { 1443 __bpf_prog_release(fp); 1444 } 1445 EXPORT_SYMBOL_GPL(bpf_prog_destroy); 1446 1447 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk) 1448 { 1449 struct sk_filter *fp, *old_fp; 1450 1451 fp = kmalloc(sizeof(*fp), GFP_KERNEL); 1452 if (!fp) 1453 return -ENOMEM; 1454 1455 fp->prog = prog; 1456 1457 if (!__sk_filter_charge(sk, fp)) { 1458 kfree(fp); 1459 return -ENOMEM; 1460 } 1461 refcount_set(&fp->refcnt, 1); 1462 1463 old_fp = rcu_dereference_protected(sk->sk_filter, 1464 lockdep_sock_is_held(sk)); 1465 rcu_assign_pointer(sk->sk_filter, fp); 1466 1467 if (old_fp) 1468 sk_filter_uncharge(sk, old_fp); 1469 1470 return 0; 1471 } 1472 1473 static 1474 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk) 1475 { 1476 unsigned int fsize = bpf_classic_proglen(fprog); 1477 struct bpf_prog *prog; 1478 int err; 1479 1480 if (sock_flag(sk, SOCK_FILTER_LOCKED)) 1481 return ERR_PTR(-EPERM); 1482 1483 /* Make sure new filter is there and in the right amounts. */ 1484 if (!bpf_check_basics_ok(fprog->filter, fprog->len)) 1485 return ERR_PTR(-EINVAL); 1486 1487 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0); 1488 if (!prog) 1489 return ERR_PTR(-ENOMEM); 1490 1491 if (copy_from_user(prog->insns, fprog->filter, fsize)) { 1492 __bpf_prog_free(prog); 1493 return ERR_PTR(-EFAULT); 1494 } 1495 1496 prog->len = fprog->len; 1497 1498 err = bpf_prog_store_orig_filter(prog, fprog); 1499 if (err) { 1500 __bpf_prog_free(prog); 1501 return ERR_PTR(-ENOMEM); 1502 } 1503 1504 /* bpf_prepare_filter() already takes care of freeing 1505 * memory in case something goes wrong. 1506 */ 1507 return bpf_prepare_filter(prog, NULL); 1508 } 1509 1510 /** 1511 * sk_attach_filter - attach a socket filter 1512 * @fprog: the filter program 1513 * @sk: the socket to use 1514 * 1515 * Attach the user's filter code. We first run some sanity checks on 1516 * it to make sure it does not explode on us later. If an error 1517 * occurs or there is insufficient memory for the filter a negative 1518 * errno code is returned. On success the return is zero. 1519 */ 1520 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk) 1521 { 1522 struct bpf_prog *prog = __get_filter(fprog, sk); 1523 int err; 1524 1525 if (IS_ERR(prog)) 1526 return PTR_ERR(prog); 1527 1528 err = __sk_attach_prog(prog, sk); 1529 if (err < 0) { 1530 __bpf_prog_release(prog); 1531 return err; 1532 } 1533 1534 return 0; 1535 } 1536 EXPORT_SYMBOL_GPL(sk_attach_filter); 1537 1538 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk) 1539 { 1540 struct bpf_prog *prog = __get_filter(fprog, sk); 1541 int err; 1542 1543 if (IS_ERR(prog)) 1544 return PTR_ERR(prog); 1545 1546 if (bpf_prog_size(prog->len) > sysctl_optmem_max) 1547 err = -ENOMEM; 1548 else 1549 err = reuseport_attach_prog(sk, prog); 1550 1551 if (err) 1552 __bpf_prog_release(prog); 1553 1554 return err; 1555 } 1556 1557 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk) 1558 { 1559 if (sock_flag(sk, SOCK_FILTER_LOCKED)) 1560 return ERR_PTR(-EPERM); 1561 1562 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER); 1563 } 1564 1565 int sk_attach_bpf(u32 ufd, struct sock *sk) 1566 { 1567 struct bpf_prog *prog = __get_bpf(ufd, sk); 1568 int err; 1569 1570 if (IS_ERR(prog)) 1571 return PTR_ERR(prog); 1572 1573 err = __sk_attach_prog(prog, sk); 1574 if (err < 0) { 1575 bpf_prog_put(prog); 1576 return err; 1577 } 1578 1579 return 0; 1580 } 1581 1582 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk) 1583 { 1584 struct bpf_prog *prog; 1585 int err; 1586 1587 if (sock_flag(sk, SOCK_FILTER_LOCKED)) 1588 return -EPERM; 1589 1590 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER); 1591 if (PTR_ERR(prog) == -EINVAL) 1592 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SK_REUSEPORT); 1593 if (IS_ERR(prog)) 1594 return PTR_ERR(prog); 1595 1596 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT) { 1597 /* Like other non BPF_PROG_TYPE_SOCKET_FILTER 1598 * bpf prog (e.g. sockmap). It depends on the 1599 * limitation imposed by bpf_prog_load(). 1600 * Hence, sysctl_optmem_max is not checked. 1601 */ 1602 if ((sk->sk_type != SOCK_STREAM && 1603 sk->sk_type != SOCK_DGRAM) || 1604 (sk->sk_protocol != IPPROTO_UDP && 1605 sk->sk_protocol != IPPROTO_TCP) || 1606 (sk->sk_family != AF_INET && 1607 sk->sk_family != AF_INET6)) { 1608 err = -ENOTSUPP; 1609 goto err_prog_put; 1610 } 1611 } else { 1612 /* BPF_PROG_TYPE_SOCKET_FILTER */ 1613 if (bpf_prog_size(prog->len) > sysctl_optmem_max) { 1614 err = -ENOMEM; 1615 goto err_prog_put; 1616 } 1617 } 1618 1619 err = reuseport_attach_prog(sk, prog); 1620 err_prog_put: 1621 if (err) 1622 bpf_prog_put(prog); 1623 1624 return err; 1625 } 1626 1627 void sk_reuseport_prog_free(struct bpf_prog *prog) 1628 { 1629 if (!prog) 1630 return; 1631 1632 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT) 1633 bpf_prog_put(prog); 1634 else 1635 bpf_prog_destroy(prog); 1636 } 1637 1638 struct bpf_scratchpad { 1639 union { 1640 __be32 diff[MAX_BPF_STACK / sizeof(__be32)]; 1641 u8 buff[MAX_BPF_STACK]; 1642 }; 1643 }; 1644 1645 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp); 1646 1647 static inline int __bpf_try_make_writable(struct sk_buff *skb, 1648 unsigned int write_len) 1649 { 1650 return skb_ensure_writable(skb, write_len); 1651 } 1652 1653 static inline int bpf_try_make_writable(struct sk_buff *skb, 1654 unsigned int write_len) 1655 { 1656 int err = __bpf_try_make_writable(skb, write_len); 1657 1658 bpf_compute_data_pointers(skb); 1659 return err; 1660 } 1661 1662 static int bpf_try_make_head_writable(struct sk_buff *skb) 1663 { 1664 return bpf_try_make_writable(skb, skb_headlen(skb)); 1665 } 1666 1667 static inline void bpf_push_mac_rcsum(struct sk_buff *skb) 1668 { 1669 if (skb_at_tc_ingress(skb)) 1670 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len); 1671 } 1672 1673 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb) 1674 { 1675 if (skb_at_tc_ingress(skb)) 1676 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len); 1677 } 1678 1679 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset, 1680 const void *, from, u32, len, u64, flags) 1681 { 1682 void *ptr; 1683 1684 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH))) 1685 return -EINVAL; 1686 if (unlikely(offset > 0xffff)) 1687 return -EFAULT; 1688 if (unlikely(bpf_try_make_writable(skb, offset + len))) 1689 return -EFAULT; 1690 1691 ptr = skb->data + offset; 1692 if (flags & BPF_F_RECOMPUTE_CSUM) 1693 __skb_postpull_rcsum(skb, ptr, len, offset); 1694 1695 memcpy(ptr, from, len); 1696 1697 if (flags & BPF_F_RECOMPUTE_CSUM) 1698 __skb_postpush_rcsum(skb, ptr, len, offset); 1699 if (flags & BPF_F_INVALIDATE_HASH) 1700 skb_clear_hash(skb); 1701 1702 return 0; 1703 } 1704 1705 static const struct bpf_func_proto bpf_skb_store_bytes_proto = { 1706 .func = bpf_skb_store_bytes, 1707 .gpl_only = false, 1708 .ret_type = RET_INTEGER, 1709 .arg1_type = ARG_PTR_TO_CTX, 1710 .arg2_type = ARG_ANYTHING, 1711 .arg3_type = ARG_PTR_TO_MEM, 1712 .arg4_type = ARG_CONST_SIZE, 1713 .arg5_type = ARG_ANYTHING, 1714 }; 1715 1716 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset, 1717 void *, to, u32, len) 1718 { 1719 void *ptr; 1720 1721 if (unlikely(offset > 0xffff)) 1722 goto err_clear; 1723 1724 ptr = skb_header_pointer(skb, offset, len, to); 1725 if (unlikely(!ptr)) 1726 goto err_clear; 1727 if (ptr != to) 1728 memcpy(to, ptr, len); 1729 1730 return 0; 1731 err_clear: 1732 memset(to, 0, len); 1733 return -EFAULT; 1734 } 1735 1736 static const struct bpf_func_proto bpf_skb_load_bytes_proto = { 1737 .func = bpf_skb_load_bytes, 1738 .gpl_only = false, 1739 .ret_type = RET_INTEGER, 1740 .arg1_type = ARG_PTR_TO_CTX, 1741 .arg2_type = ARG_ANYTHING, 1742 .arg3_type = ARG_PTR_TO_UNINIT_MEM, 1743 .arg4_type = ARG_CONST_SIZE, 1744 }; 1745 1746 BPF_CALL_4(bpf_flow_dissector_load_bytes, 1747 const struct bpf_flow_dissector *, ctx, u32, offset, 1748 void *, to, u32, len) 1749 { 1750 void *ptr; 1751 1752 if (unlikely(offset > 0xffff)) 1753 goto err_clear; 1754 1755 if (unlikely(!ctx->skb)) 1756 goto err_clear; 1757 1758 ptr = skb_header_pointer(ctx->skb, offset, len, to); 1759 if (unlikely(!ptr)) 1760 goto err_clear; 1761 if (ptr != to) 1762 memcpy(to, ptr, len); 1763 1764 return 0; 1765 err_clear: 1766 memset(to, 0, len); 1767 return -EFAULT; 1768 } 1769 1770 static const struct bpf_func_proto bpf_flow_dissector_load_bytes_proto = { 1771 .func = bpf_flow_dissector_load_bytes, 1772 .gpl_only = false, 1773 .ret_type = RET_INTEGER, 1774 .arg1_type = ARG_PTR_TO_CTX, 1775 .arg2_type = ARG_ANYTHING, 1776 .arg3_type = ARG_PTR_TO_UNINIT_MEM, 1777 .arg4_type = ARG_CONST_SIZE, 1778 }; 1779 1780 BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb, 1781 u32, offset, void *, to, u32, len, u32, start_header) 1782 { 1783 u8 *end = skb_tail_pointer(skb); 1784 u8 *start, *ptr; 1785 1786 if (unlikely(offset > 0xffff)) 1787 goto err_clear; 1788 1789 switch (start_header) { 1790 case BPF_HDR_START_MAC: 1791 if (unlikely(!skb_mac_header_was_set(skb))) 1792 goto err_clear; 1793 start = skb_mac_header(skb); 1794 break; 1795 case BPF_HDR_START_NET: 1796 start = skb_network_header(skb); 1797 break; 1798 default: 1799 goto err_clear; 1800 } 1801 1802 ptr = start + offset; 1803 1804 if (likely(ptr + len <= end)) { 1805 memcpy(to, ptr, len); 1806 return 0; 1807 } 1808 1809 err_clear: 1810 memset(to, 0, len); 1811 return -EFAULT; 1812 } 1813 1814 static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto = { 1815 .func = bpf_skb_load_bytes_relative, 1816 .gpl_only = false, 1817 .ret_type = RET_INTEGER, 1818 .arg1_type = ARG_PTR_TO_CTX, 1819 .arg2_type = ARG_ANYTHING, 1820 .arg3_type = ARG_PTR_TO_UNINIT_MEM, 1821 .arg4_type = ARG_CONST_SIZE, 1822 .arg5_type = ARG_ANYTHING, 1823 }; 1824 1825 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len) 1826 { 1827 /* Idea is the following: should the needed direct read/write 1828 * test fail during runtime, we can pull in more data and redo 1829 * again, since implicitly, we invalidate previous checks here. 1830 * 1831 * Or, since we know how much we need to make read/writeable, 1832 * this can be done once at the program beginning for direct 1833 * access case. By this we overcome limitations of only current 1834 * headroom being accessible. 1835 */ 1836 return bpf_try_make_writable(skb, len ? : skb_headlen(skb)); 1837 } 1838 1839 static const struct bpf_func_proto bpf_skb_pull_data_proto = { 1840 .func = bpf_skb_pull_data, 1841 .gpl_only = false, 1842 .ret_type = RET_INTEGER, 1843 .arg1_type = ARG_PTR_TO_CTX, 1844 .arg2_type = ARG_ANYTHING, 1845 }; 1846 1847 BPF_CALL_1(bpf_sk_fullsock, struct sock *, sk) 1848 { 1849 return sk_fullsock(sk) ? (unsigned long)sk : (unsigned long)NULL; 1850 } 1851 1852 static const struct bpf_func_proto bpf_sk_fullsock_proto = { 1853 .func = bpf_sk_fullsock, 1854 .gpl_only = false, 1855 .ret_type = RET_PTR_TO_SOCKET_OR_NULL, 1856 .arg1_type = ARG_PTR_TO_SOCK_COMMON, 1857 }; 1858 1859 static inline int sk_skb_try_make_writable(struct sk_buff *skb, 1860 unsigned int write_len) 1861 { 1862 int err = __bpf_try_make_writable(skb, write_len); 1863 1864 bpf_compute_data_end_sk_skb(skb); 1865 return err; 1866 } 1867 1868 BPF_CALL_2(sk_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 sk_skb_try_make_writable(skb, len ? : skb_headlen(skb)); 1880 } 1881 1882 static const struct bpf_func_proto sk_skb_pull_data_proto = { 1883 .func = sk_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_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset, 1891 u64, from, u64, to, u64, flags) 1892 { 1893 __sum16 *ptr; 1894 1895 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK))) 1896 return -EINVAL; 1897 if (unlikely(offset > 0xffff || offset & 1)) 1898 return -EFAULT; 1899 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr)))) 1900 return -EFAULT; 1901 1902 ptr = (__sum16 *)(skb->data + offset); 1903 switch (flags & BPF_F_HDR_FIELD_MASK) { 1904 case 0: 1905 if (unlikely(from != 0)) 1906 return -EINVAL; 1907 1908 csum_replace_by_diff(ptr, to); 1909 break; 1910 case 2: 1911 csum_replace2(ptr, from, to); 1912 break; 1913 case 4: 1914 csum_replace4(ptr, from, to); 1915 break; 1916 default: 1917 return -EINVAL; 1918 } 1919 1920 return 0; 1921 } 1922 1923 static const struct bpf_func_proto bpf_l3_csum_replace_proto = { 1924 .func = bpf_l3_csum_replace, 1925 .gpl_only = false, 1926 .ret_type = RET_INTEGER, 1927 .arg1_type = ARG_PTR_TO_CTX, 1928 .arg2_type = ARG_ANYTHING, 1929 .arg3_type = ARG_ANYTHING, 1930 .arg4_type = ARG_ANYTHING, 1931 .arg5_type = ARG_ANYTHING, 1932 }; 1933 1934 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset, 1935 u64, from, u64, to, u64, flags) 1936 { 1937 bool is_pseudo = flags & BPF_F_PSEUDO_HDR; 1938 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0; 1939 bool do_mforce = flags & BPF_F_MARK_ENFORCE; 1940 __sum16 *ptr; 1941 1942 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE | 1943 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK))) 1944 return -EINVAL; 1945 if (unlikely(offset > 0xffff || offset & 1)) 1946 return -EFAULT; 1947 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr)))) 1948 return -EFAULT; 1949 1950 ptr = (__sum16 *)(skb->data + offset); 1951 if (is_mmzero && !do_mforce && !*ptr) 1952 return 0; 1953 1954 switch (flags & BPF_F_HDR_FIELD_MASK) { 1955 case 0: 1956 if (unlikely(from != 0)) 1957 return -EINVAL; 1958 1959 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo); 1960 break; 1961 case 2: 1962 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo); 1963 break; 1964 case 4: 1965 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo); 1966 break; 1967 default: 1968 return -EINVAL; 1969 } 1970 1971 if (is_mmzero && !*ptr) 1972 *ptr = CSUM_MANGLED_0; 1973 return 0; 1974 } 1975 1976 static const struct bpf_func_proto bpf_l4_csum_replace_proto = { 1977 .func = bpf_l4_csum_replace, 1978 .gpl_only = false, 1979 .ret_type = RET_INTEGER, 1980 .arg1_type = ARG_PTR_TO_CTX, 1981 .arg2_type = ARG_ANYTHING, 1982 .arg3_type = ARG_ANYTHING, 1983 .arg4_type = ARG_ANYTHING, 1984 .arg5_type = ARG_ANYTHING, 1985 }; 1986 1987 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size, 1988 __be32 *, to, u32, to_size, __wsum, seed) 1989 { 1990 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp); 1991 u32 diff_size = from_size + to_size; 1992 int i, j = 0; 1993 1994 /* This is quite flexible, some examples: 1995 * 1996 * from_size == 0, to_size > 0, seed := csum --> pushing data 1997 * from_size > 0, to_size == 0, seed := csum --> pulling data 1998 * from_size > 0, to_size > 0, seed := 0 --> diffing data 1999 * 2000 * Even for diffing, from_size and to_size don't need to be equal. 2001 */ 2002 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) || 2003 diff_size > sizeof(sp->diff))) 2004 return -EINVAL; 2005 2006 for (i = 0; i < from_size / sizeof(__be32); i++, j++) 2007 sp->diff[j] = ~from[i]; 2008 for (i = 0; i < to_size / sizeof(__be32); i++, j++) 2009 sp->diff[j] = to[i]; 2010 2011 return csum_partial(sp->diff, diff_size, seed); 2012 } 2013 2014 static const struct bpf_func_proto bpf_csum_diff_proto = { 2015 .func = bpf_csum_diff, 2016 .gpl_only = false, 2017 .pkt_access = true, 2018 .ret_type = RET_INTEGER, 2019 .arg1_type = ARG_PTR_TO_MEM_OR_NULL, 2020 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 2021 .arg3_type = ARG_PTR_TO_MEM_OR_NULL, 2022 .arg4_type = ARG_CONST_SIZE_OR_ZERO, 2023 .arg5_type = ARG_ANYTHING, 2024 }; 2025 2026 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum) 2027 { 2028 /* The interface is to be used in combination with bpf_csum_diff() 2029 * for direct packet writes. csum rotation for alignment as well 2030 * as emulating csum_sub() can be done from the eBPF program. 2031 */ 2032 if (skb->ip_summed == CHECKSUM_COMPLETE) 2033 return (skb->csum = csum_add(skb->csum, csum)); 2034 2035 return -ENOTSUPP; 2036 } 2037 2038 static const struct bpf_func_proto bpf_csum_update_proto = { 2039 .func = bpf_csum_update, 2040 .gpl_only = false, 2041 .ret_type = RET_INTEGER, 2042 .arg1_type = ARG_PTR_TO_CTX, 2043 .arg2_type = ARG_ANYTHING, 2044 }; 2045 2046 BPF_CALL_2(bpf_csum_level, struct sk_buff *, skb, u64, level) 2047 { 2048 /* The interface is to be used in combination with bpf_skb_adjust_room() 2049 * for encap/decap of packet headers when BPF_F_ADJ_ROOM_NO_CSUM_RESET 2050 * is passed as flags, for example. 2051 */ 2052 switch (level) { 2053 case BPF_CSUM_LEVEL_INC: 2054 __skb_incr_checksum_unnecessary(skb); 2055 break; 2056 case BPF_CSUM_LEVEL_DEC: 2057 __skb_decr_checksum_unnecessary(skb); 2058 break; 2059 case BPF_CSUM_LEVEL_RESET: 2060 __skb_reset_checksum_unnecessary(skb); 2061 break; 2062 case BPF_CSUM_LEVEL_QUERY: 2063 return skb->ip_summed == CHECKSUM_UNNECESSARY ? 2064 skb->csum_level : -EACCES; 2065 default: 2066 return -EINVAL; 2067 } 2068 2069 return 0; 2070 } 2071 2072 static const struct bpf_func_proto bpf_csum_level_proto = { 2073 .func = bpf_csum_level, 2074 .gpl_only = false, 2075 .ret_type = RET_INTEGER, 2076 .arg1_type = ARG_PTR_TO_CTX, 2077 .arg2_type = ARG_ANYTHING, 2078 }; 2079 2080 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb) 2081 { 2082 return dev_forward_skb(dev, skb); 2083 } 2084 2085 static inline int __bpf_rx_skb_no_mac(struct net_device *dev, 2086 struct sk_buff *skb) 2087 { 2088 int ret = ____dev_forward_skb(dev, skb); 2089 2090 if (likely(!ret)) { 2091 skb->dev = dev; 2092 ret = netif_rx(skb); 2093 } 2094 2095 return ret; 2096 } 2097 2098 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb) 2099 { 2100 int ret; 2101 2102 if (dev_xmit_recursion()) { 2103 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n"); 2104 kfree_skb(skb); 2105 return -ENETDOWN; 2106 } 2107 2108 skb->dev = dev; 2109 skb->tstamp = 0; 2110 2111 dev_xmit_recursion_inc(); 2112 ret = dev_queue_xmit(skb); 2113 dev_xmit_recursion_dec(); 2114 2115 return ret; 2116 } 2117 2118 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev, 2119 u32 flags) 2120 { 2121 unsigned int mlen = skb_network_offset(skb); 2122 2123 if (mlen) { 2124 __skb_pull(skb, mlen); 2125 2126 /* At ingress, the mac header has already been pulled once. 2127 * At egress, skb_pospull_rcsum has to be done in case that 2128 * the skb is originated from ingress (i.e. a forwarded skb) 2129 * to ensure that rcsum starts at net header. 2130 */ 2131 if (!skb_at_tc_ingress(skb)) 2132 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen); 2133 } 2134 skb_pop_mac_header(skb); 2135 skb_reset_mac_len(skb); 2136 return flags & BPF_F_INGRESS ? 2137 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb); 2138 } 2139 2140 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev, 2141 u32 flags) 2142 { 2143 /* Verify that a link layer header is carried */ 2144 if (unlikely(skb->mac_header >= skb->network_header)) { 2145 kfree_skb(skb); 2146 return -ERANGE; 2147 } 2148 2149 bpf_push_mac_rcsum(skb); 2150 return flags & BPF_F_INGRESS ? 2151 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb); 2152 } 2153 2154 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev, 2155 u32 flags) 2156 { 2157 if (dev_is_mac_header_xmit(dev)) 2158 return __bpf_redirect_common(skb, dev, flags); 2159 else 2160 return __bpf_redirect_no_mac(skb, dev, flags); 2161 } 2162 2163 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags) 2164 { 2165 struct net_device *dev; 2166 struct sk_buff *clone; 2167 int ret; 2168 2169 if (unlikely(flags & ~(BPF_F_INGRESS))) 2170 return -EINVAL; 2171 2172 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex); 2173 if (unlikely(!dev)) 2174 return -EINVAL; 2175 2176 clone = skb_clone(skb, GFP_ATOMIC); 2177 if (unlikely(!clone)) 2178 return -ENOMEM; 2179 2180 /* For direct write, we need to keep the invariant that the skbs 2181 * we're dealing with need to be uncloned. Should uncloning fail 2182 * here, we need to free the just generated clone to unclone once 2183 * again. 2184 */ 2185 ret = bpf_try_make_head_writable(skb); 2186 if (unlikely(ret)) { 2187 kfree_skb(clone); 2188 return -ENOMEM; 2189 } 2190 2191 return __bpf_redirect(clone, dev, flags); 2192 } 2193 2194 static const struct bpf_func_proto bpf_clone_redirect_proto = { 2195 .func = bpf_clone_redirect, 2196 .gpl_only = false, 2197 .ret_type = RET_INTEGER, 2198 .arg1_type = ARG_PTR_TO_CTX, 2199 .arg2_type = ARG_ANYTHING, 2200 .arg3_type = ARG_ANYTHING, 2201 }; 2202 2203 DEFINE_PER_CPU(struct bpf_redirect_info, bpf_redirect_info); 2204 EXPORT_PER_CPU_SYMBOL_GPL(bpf_redirect_info); 2205 2206 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags) 2207 { 2208 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 2209 2210 if (unlikely(flags & ~(BPF_F_INGRESS))) 2211 return TC_ACT_SHOT; 2212 2213 ri->flags = flags; 2214 ri->tgt_index = ifindex; 2215 2216 return TC_ACT_REDIRECT; 2217 } 2218 2219 int skb_do_redirect(struct sk_buff *skb) 2220 { 2221 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 2222 struct net_device *dev; 2223 2224 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->tgt_index); 2225 ri->tgt_index = 0; 2226 if (unlikely(!dev)) { 2227 kfree_skb(skb); 2228 return -EINVAL; 2229 } 2230 2231 return __bpf_redirect(skb, dev, ri->flags); 2232 } 2233 2234 static const struct bpf_func_proto bpf_redirect_proto = { 2235 .func = bpf_redirect, 2236 .gpl_only = false, 2237 .ret_type = RET_INTEGER, 2238 .arg1_type = ARG_ANYTHING, 2239 .arg2_type = ARG_ANYTHING, 2240 }; 2241 2242 BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg *, msg, u32, bytes) 2243 { 2244 msg->apply_bytes = bytes; 2245 return 0; 2246 } 2247 2248 static const struct bpf_func_proto bpf_msg_apply_bytes_proto = { 2249 .func = bpf_msg_apply_bytes, 2250 .gpl_only = false, 2251 .ret_type = RET_INTEGER, 2252 .arg1_type = ARG_PTR_TO_CTX, 2253 .arg2_type = ARG_ANYTHING, 2254 }; 2255 2256 BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg *, msg, u32, bytes) 2257 { 2258 msg->cork_bytes = bytes; 2259 return 0; 2260 } 2261 2262 static const struct bpf_func_proto bpf_msg_cork_bytes_proto = { 2263 .func = bpf_msg_cork_bytes, 2264 .gpl_only = false, 2265 .ret_type = RET_INTEGER, 2266 .arg1_type = ARG_PTR_TO_CTX, 2267 .arg2_type = ARG_ANYTHING, 2268 }; 2269 2270 BPF_CALL_4(bpf_msg_pull_data, struct sk_msg *, msg, u32, start, 2271 u32, end, u64, flags) 2272 { 2273 u32 len = 0, offset = 0, copy = 0, poffset = 0, bytes = end - start; 2274 u32 first_sge, last_sge, i, shift, bytes_sg_total; 2275 struct scatterlist *sge; 2276 u8 *raw, *to, *from; 2277 struct page *page; 2278 2279 if (unlikely(flags || end <= start)) 2280 return -EINVAL; 2281 2282 /* First find the starting scatterlist element */ 2283 i = msg->sg.start; 2284 do { 2285 offset += len; 2286 len = sk_msg_elem(msg, i)->length; 2287 if (start < offset + len) 2288 break; 2289 sk_msg_iter_var_next(i); 2290 } while (i != msg->sg.end); 2291 2292 if (unlikely(start >= offset + len)) 2293 return -EINVAL; 2294 2295 first_sge = i; 2296 /* The start may point into the sg element so we need to also 2297 * account for the headroom. 2298 */ 2299 bytes_sg_total = start - offset + bytes; 2300 if (!test_bit(i, &msg->sg.copy) && bytes_sg_total <= len) 2301 goto out; 2302 2303 /* At this point we need to linearize multiple scatterlist 2304 * elements or a single shared page. Either way we need to 2305 * copy into a linear buffer exclusively owned by BPF. Then 2306 * place the buffer in the scatterlist and fixup the original 2307 * entries by removing the entries now in the linear buffer 2308 * and shifting the remaining entries. For now we do not try 2309 * to copy partial entries to avoid complexity of running out 2310 * of sg_entry slots. The downside is reading a single byte 2311 * will copy the entire sg entry. 2312 */ 2313 do { 2314 copy += sk_msg_elem(msg, i)->length; 2315 sk_msg_iter_var_next(i); 2316 if (bytes_sg_total <= copy) 2317 break; 2318 } while (i != msg->sg.end); 2319 last_sge = i; 2320 2321 if (unlikely(bytes_sg_total > copy)) 2322 return -EINVAL; 2323 2324 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP, 2325 get_order(copy)); 2326 if (unlikely(!page)) 2327 return -ENOMEM; 2328 2329 raw = page_address(page); 2330 i = first_sge; 2331 do { 2332 sge = sk_msg_elem(msg, i); 2333 from = sg_virt(sge); 2334 len = sge->length; 2335 to = raw + poffset; 2336 2337 memcpy(to, from, len); 2338 poffset += len; 2339 sge->length = 0; 2340 put_page(sg_page(sge)); 2341 2342 sk_msg_iter_var_next(i); 2343 } while (i != last_sge); 2344 2345 sg_set_page(&msg->sg.data[first_sge], page, copy, 0); 2346 2347 /* To repair sg ring we need to shift entries. If we only 2348 * had a single entry though we can just replace it and 2349 * be done. Otherwise walk the ring and shift the entries. 2350 */ 2351 WARN_ON_ONCE(last_sge == first_sge); 2352 shift = last_sge > first_sge ? 2353 last_sge - first_sge - 1 : 2354 NR_MSG_FRAG_IDS - first_sge + last_sge - 1; 2355 if (!shift) 2356 goto out; 2357 2358 i = first_sge; 2359 sk_msg_iter_var_next(i); 2360 do { 2361 u32 move_from; 2362 2363 if (i + shift >= NR_MSG_FRAG_IDS) 2364 move_from = i + shift - NR_MSG_FRAG_IDS; 2365 else 2366 move_from = i + shift; 2367 if (move_from == msg->sg.end) 2368 break; 2369 2370 msg->sg.data[i] = msg->sg.data[move_from]; 2371 msg->sg.data[move_from].length = 0; 2372 msg->sg.data[move_from].page_link = 0; 2373 msg->sg.data[move_from].offset = 0; 2374 sk_msg_iter_var_next(i); 2375 } while (1); 2376 2377 msg->sg.end = msg->sg.end - shift > msg->sg.end ? 2378 msg->sg.end - shift + NR_MSG_FRAG_IDS : 2379 msg->sg.end - shift; 2380 out: 2381 msg->data = sg_virt(&msg->sg.data[first_sge]) + start - offset; 2382 msg->data_end = msg->data + bytes; 2383 return 0; 2384 } 2385 2386 static const struct bpf_func_proto bpf_msg_pull_data_proto = { 2387 .func = bpf_msg_pull_data, 2388 .gpl_only = false, 2389 .ret_type = RET_INTEGER, 2390 .arg1_type = ARG_PTR_TO_CTX, 2391 .arg2_type = ARG_ANYTHING, 2392 .arg3_type = ARG_ANYTHING, 2393 .arg4_type = ARG_ANYTHING, 2394 }; 2395 2396 BPF_CALL_4(bpf_msg_push_data, struct sk_msg *, msg, u32, start, 2397 u32, len, u64, flags) 2398 { 2399 struct scatterlist sge, nsge, nnsge, rsge = {0}, *psge; 2400 u32 new, i = 0, l = 0, space, copy = 0, offset = 0; 2401 u8 *raw, *to, *from; 2402 struct page *page; 2403 2404 if (unlikely(flags)) 2405 return -EINVAL; 2406 2407 /* First find the starting scatterlist element */ 2408 i = msg->sg.start; 2409 do { 2410 offset += l; 2411 l = sk_msg_elem(msg, i)->length; 2412 2413 if (start < offset + l) 2414 break; 2415 sk_msg_iter_var_next(i); 2416 } while (i != msg->sg.end); 2417 2418 if (start >= offset + l) 2419 return -EINVAL; 2420 2421 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg); 2422 2423 /* If no space available will fallback to copy, we need at 2424 * least one scatterlist elem available to push data into 2425 * when start aligns to the beginning of an element or two 2426 * when it falls inside an element. We handle the start equals 2427 * offset case because its the common case for inserting a 2428 * header. 2429 */ 2430 if (!space || (space == 1 && start != offset)) 2431 copy = msg->sg.data[i].length; 2432 2433 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP, 2434 get_order(copy + len)); 2435 if (unlikely(!page)) 2436 return -ENOMEM; 2437 2438 if (copy) { 2439 int front, back; 2440 2441 raw = page_address(page); 2442 2443 psge = sk_msg_elem(msg, i); 2444 front = start - offset; 2445 back = psge->length - front; 2446 from = sg_virt(psge); 2447 2448 if (front) 2449 memcpy(raw, from, front); 2450 2451 if (back) { 2452 from += front; 2453 to = raw + front + len; 2454 2455 memcpy(to, from, back); 2456 } 2457 2458 put_page(sg_page(psge)); 2459 } else if (start - offset) { 2460 psge = sk_msg_elem(msg, i); 2461 rsge = sk_msg_elem_cpy(msg, i); 2462 2463 psge->length = start - offset; 2464 rsge.length -= psge->length; 2465 rsge.offset += start; 2466 2467 sk_msg_iter_var_next(i); 2468 sg_unmark_end(psge); 2469 sg_unmark_end(&rsge); 2470 sk_msg_iter_next(msg, end); 2471 } 2472 2473 /* Slot(s) to place newly allocated data */ 2474 new = i; 2475 2476 /* Shift one or two slots as needed */ 2477 if (!copy) { 2478 sge = sk_msg_elem_cpy(msg, i); 2479 2480 sk_msg_iter_var_next(i); 2481 sg_unmark_end(&sge); 2482 sk_msg_iter_next(msg, end); 2483 2484 nsge = sk_msg_elem_cpy(msg, i); 2485 if (rsge.length) { 2486 sk_msg_iter_var_next(i); 2487 nnsge = sk_msg_elem_cpy(msg, i); 2488 } 2489 2490 while (i != msg->sg.end) { 2491 msg->sg.data[i] = sge; 2492 sge = nsge; 2493 sk_msg_iter_var_next(i); 2494 if (rsge.length) { 2495 nsge = nnsge; 2496 nnsge = sk_msg_elem_cpy(msg, i); 2497 } else { 2498 nsge = sk_msg_elem_cpy(msg, i); 2499 } 2500 } 2501 } 2502 2503 /* Place newly allocated data buffer */ 2504 sk_mem_charge(msg->sk, len); 2505 msg->sg.size += len; 2506 __clear_bit(new, &msg->sg.copy); 2507 sg_set_page(&msg->sg.data[new], page, len + copy, 0); 2508 if (rsge.length) { 2509 get_page(sg_page(&rsge)); 2510 sk_msg_iter_var_next(new); 2511 msg->sg.data[new] = rsge; 2512 } 2513 2514 sk_msg_compute_data_pointers(msg); 2515 return 0; 2516 } 2517 2518 static const struct bpf_func_proto bpf_msg_push_data_proto = { 2519 .func = bpf_msg_push_data, 2520 .gpl_only = false, 2521 .ret_type = RET_INTEGER, 2522 .arg1_type = ARG_PTR_TO_CTX, 2523 .arg2_type = ARG_ANYTHING, 2524 .arg3_type = ARG_ANYTHING, 2525 .arg4_type = ARG_ANYTHING, 2526 }; 2527 2528 static void sk_msg_shift_left(struct sk_msg *msg, int i) 2529 { 2530 int prev; 2531 2532 do { 2533 prev = i; 2534 sk_msg_iter_var_next(i); 2535 msg->sg.data[prev] = msg->sg.data[i]; 2536 } while (i != msg->sg.end); 2537 2538 sk_msg_iter_prev(msg, end); 2539 } 2540 2541 static void sk_msg_shift_right(struct sk_msg *msg, int i) 2542 { 2543 struct scatterlist tmp, sge; 2544 2545 sk_msg_iter_next(msg, end); 2546 sge = sk_msg_elem_cpy(msg, i); 2547 sk_msg_iter_var_next(i); 2548 tmp = sk_msg_elem_cpy(msg, i); 2549 2550 while (i != msg->sg.end) { 2551 msg->sg.data[i] = sge; 2552 sk_msg_iter_var_next(i); 2553 sge = tmp; 2554 tmp = sk_msg_elem_cpy(msg, i); 2555 } 2556 } 2557 2558 BPF_CALL_4(bpf_msg_pop_data, struct sk_msg *, msg, u32, start, 2559 u32, len, u64, flags) 2560 { 2561 u32 i = 0, l = 0, space, offset = 0; 2562 u64 last = start + len; 2563 int pop; 2564 2565 if (unlikely(flags)) 2566 return -EINVAL; 2567 2568 /* First find the starting scatterlist element */ 2569 i = msg->sg.start; 2570 do { 2571 offset += l; 2572 l = sk_msg_elem(msg, i)->length; 2573 2574 if (start < offset + l) 2575 break; 2576 sk_msg_iter_var_next(i); 2577 } while (i != msg->sg.end); 2578 2579 /* Bounds checks: start and pop must be inside message */ 2580 if (start >= offset + l || last >= msg->sg.size) 2581 return -EINVAL; 2582 2583 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg); 2584 2585 pop = len; 2586 /* --------------| offset 2587 * -| start |-------- len -------| 2588 * 2589 * |----- a ----|-------- pop -------|----- b ----| 2590 * |______________________________________________| length 2591 * 2592 * 2593 * a: region at front of scatter element to save 2594 * b: region at back of scatter element to save when length > A + pop 2595 * pop: region to pop from element, same as input 'pop' here will be 2596 * decremented below per iteration. 2597 * 2598 * Two top-level cases to handle when start != offset, first B is non 2599 * zero and second B is zero corresponding to when a pop includes more 2600 * than one element. 2601 * 2602 * Then if B is non-zero AND there is no space allocate space and 2603 * compact A, B regions into page. If there is space shift ring to 2604 * the rigth free'ing the next element in ring to place B, leaving 2605 * A untouched except to reduce length. 2606 */ 2607 if (start != offset) { 2608 struct scatterlist *nsge, *sge = sk_msg_elem(msg, i); 2609 int a = start; 2610 int b = sge->length - pop - a; 2611 2612 sk_msg_iter_var_next(i); 2613 2614 if (pop < sge->length - a) { 2615 if (space) { 2616 sge->length = a; 2617 sk_msg_shift_right(msg, i); 2618 nsge = sk_msg_elem(msg, i); 2619 get_page(sg_page(sge)); 2620 sg_set_page(nsge, 2621 sg_page(sge), 2622 b, sge->offset + pop + a); 2623 } else { 2624 struct page *page, *orig; 2625 u8 *to, *from; 2626 2627 page = alloc_pages(__GFP_NOWARN | 2628 __GFP_COMP | GFP_ATOMIC, 2629 get_order(a + b)); 2630 if (unlikely(!page)) 2631 return -ENOMEM; 2632 2633 sge->length = a; 2634 orig = sg_page(sge); 2635 from = sg_virt(sge); 2636 to = page_address(page); 2637 memcpy(to, from, a); 2638 memcpy(to + a, from + a + pop, b); 2639 sg_set_page(sge, page, a + b, 0); 2640 put_page(orig); 2641 } 2642 pop = 0; 2643 } else if (pop >= sge->length - a) { 2644 pop -= (sge->length - a); 2645 sge->length = a; 2646 } 2647 } 2648 2649 /* From above the current layout _must_ be as follows, 2650 * 2651 * -| offset 2652 * -| start 2653 * 2654 * |---- pop ---|---------------- b ------------| 2655 * |____________________________________________| length 2656 * 2657 * Offset and start of the current msg elem are equal because in the 2658 * previous case we handled offset != start and either consumed the 2659 * entire element and advanced to the next element OR pop == 0. 2660 * 2661 * Two cases to handle here are first pop is less than the length 2662 * leaving some remainder b above. Simply adjust the element's layout 2663 * in this case. Or pop >= length of the element so that b = 0. In this 2664 * case advance to next element decrementing pop. 2665 */ 2666 while (pop) { 2667 struct scatterlist *sge = sk_msg_elem(msg, i); 2668 2669 if (pop < sge->length) { 2670 sge->length -= pop; 2671 sge->offset += pop; 2672 pop = 0; 2673 } else { 2674 pop -= sge->length; 2675 sk_msg_shift_left(msg, i); 2676 } 2677 sk_msg_iter_var_next(i); 2678 } 2679 2680 sk_mem_uncharge(msg->sk, len - pop); 2681 msg->sg.size -= (len - pop); 2682 sk_msg_compute_data_pointers(msg); 2683 return 0; 2684 } 2685 2686 static const struct bpf_func_proto bpf_msg_pop_data_proto = { 2687 .func = bpf_msg_pop_data, 2688 .gpl_only = false, 2689 .ret_type = RET_INTEGER, 2690 .arg1_type = ARG_PTR_TO_CTX, 2691 .arg2_type = ARG_ANYTHING, 2692 .arg3_type = ARG_ANYTHING, 2693 .arg4_type = ARG_ANYTHING, 2694 }; 2695 2696 #ifdef CONFIG_CGROUP_NET_CLASSID 2697 BPF_CALL_0(bpf_get_cgroup_classid_curr) 2698 { 2699 return __task_get_classid(current); 2700 } 2701 2702 static const struct bpf_func_proto bpf_get_cgroup_classid_curr_proto = { 2703 .func = bpf_get_cgroup_classid_curr, 2704 .gpl_only = false, 2705 .ret_type = RET_INTEGER, 2706 }; 2707 #endif 2708 2709 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb) 2710 { 2711 return task_get_classid(skb); 2712 } 2713 2714 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = { 2715 .func = bpf_get_cgroup_classid, 2716 .gpl_only = false, 2717 .ret_type = RET_INTEGER, 2718 .arg1_type = ARG_PTR_TO_CTX, 2719 }; 2720 2721 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb) 2722 { 2723 return dst_tclassid(skb); 2724 } 2725 2726 static const struct bpf_func_proto bpf_get_route_realm_proto = { 2727 .func = bpf_get_route_realm, 2728 .gpl_only = false, 2729 .ret_type = RET_INTEGER, 2730 .arg1_type = ARG_PTR_TO_CTX, 2731 }; 2732 2733 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb) 2734 { 2735 /* If skb_clear_hash() was called due to mangling, we can 2736 * trigger SW recalculation here. Later access to hash 2737 * can then use the inline skb->hash via context directly 2738 * instead of calling this helper again. 2739 */ 2740 return skb_get_hash(skb); 2741 } 2742 2743 static const struct bpf_func_proto bpf_get_hash_recalc_proto = { 2744 .func = bpf_get_hash_recalc, 2745 .gpl_only = false, 2746 .ret_type = RET_INTEGER, 2747 .arg1_type = ARG_PTR_TO_CTX, 2748 }; 2749 2750 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb) 2751 { 2752 /* After all direct packet write, this can be used once for 2753 * triggering a lazy recalc on next skb_get_hash() invocation. 2754 */ 2755 skb_clear_hash(skb); 2756 return 0; 2757 } 2758 2759 static const struct bpf_func_proto bpf_set_hash_invalid_proto = { 2760 .func = bpf_set_hash_invalid, 2761 .gpl_only = false, 2762 .ret_type = RET_INTEGER, 2763 .arg1_type = ARG_PTR_TO_CTX, 2764 }; 2765 2766 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash) 2767 { 2768 /* Set user specified hash as L4(+), so that it gets returned 2769 * on skb_get_hash() call unless BPF prog later on triggers a 2770 * skb_clear_hash(). 2771 */ 2772 __skb_set_sw_hash(skb, hash, true); 2773 return 0; 2774 } 2775 2776 static const struct bpf_func_proto bpf_set_hash_proto = { 2777 .func = bpf_set_hash, 2778 .gpl_only = false, 2779 .ret_type = RET_INTEGER, 2780 .arg1_type = ARG_PTR_TO_CTX, 2781 .arg2_type = ARG_ANYTHING, 2782 }; 2783 2784 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto, 2785 u16, vlan_tci) 2786 { 2787 int ret; 2788 2789 if (unlikely(vlan_proto != htons(ETH_P_8021Q) && 2790 vlan_proto != htons(ETH_P_8021AD))) 2791 vlan_proto = htons(ETH_P_8021Q); 2792 2793 bpf_push_mac_rcsum(skb); 2794 ret = skb_vlan_push(skb, vlan_proto, vlan_tci); 2795 bpf_pull_mac_rcsum(skb); 2796 2797 bpf_compute_data_pointers(skb); 2798 return ret; 2799 } 2800 2801 static const struct bpf_func_proto bpf_skb_vlan_push_proto = { 2802 .func = bpf_skb_vlan_push, 2803 .gpl_only = false, 2804 .ret_type = RET_INTEGER, 2805 .arg1_type = ARG_PTR_TO_CTX, 2806 .arg2_type = ARG_ANYTHING, 2807 .arg3_type = ARG_ANYTHING, 2808 }; 2809 2810 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb) 2811 { 2812 int ret; 2813 2814 bpf_push_mac_rcsum(skb); 2815 ret = skb_vlan_pop(skb); 2816 bpf_pull_mac_rcsum(skb); 2817 2818 bpf_compute_data_pointers(skb); 2819 return ret; 2820 } 2821 2822 static const struct bpf_func_proto bpf_skb_vlan_pop_proto = { 2823 .func = bpf_skb_vlan_pop, 2824 .gpl_only = false, 2825 .ret_type = RET_INTEGER, 2826 .arg1_type = ARG_PTR_TO_CTX, 2827 }; 2828 2829 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len) 2830 { 2831 /* Caller already did skb_cow() with len as headroom, 2832 * so no need to do it here. 2833 */ 2834 skb_push(skb, len); 2835 memmove(skb->data, skb->data + len, off); 2836 memset(skb->data + off, 0, len); 2837 2838 /* No skb_postpush_rcsum(skb, skb->data + off, len) 2839 * needed here as it does not change the skb->csum 2840 * result for checksum complete when summing over 2841 * zeroed blocks. 2842 */ 2843 return 0; 2844 } 2845 2846 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len) 2847 { 2848 /* skb_ensure_writable() is not needed here, as we're 2849 * already working on an uncloned skb. 2850 */ 2851 if (unlikely(!pskb_may_pull(skb, off + len))) 2852 return -ENOMEM; 2853 2854 skb_postpull_rcsum(skb, skb->data + off, len); 2855 memmove(skb->data + len, skb->data, off); 2856 __skb_pull(skb, len); 2857 2858 return 0; 2859 } 2860 2861 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len) 2862 { 2863 bool trans_same = skb->transport_header == skb->network_header; 2864 int ret; 2865 2866 /* There's no need for __skb_push()/__skb_pull() pair to 2867 * get to the start of the mac header as we're guaranteed 2868 * to always start from here under eBPF. 2869 */ 2870 ret = bpf_skb_generic_push(skb, off, len); 2871 if (likely(!ret)) { 2872 skb->mac_header -= len; 2873 skb->network_header -= len; 2874 if (trans_same) 2875 skb->transport_header = skb->network_header; 2876 } 2877 2878 return ret; 2879 } 2880 2881 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len) 2882 { 2883 bool trans_same = skb->transport_header == skb->network_header; 2884 int ret; 2885 2886 /* Same here, __skb_push()/__skb_pull() pair not needed. */ 2887 ret = bpf_skb_generic_pop(skb, off, len); 2888 if (likely(!ret)) { 2889 skb->mac_header += len; 2890 skb->network_header += len; 2891 if (trans_same) 2892 skb->transport_header = skb->network_header; 2893 } 2894 2895 return ret; 2896 } 2897 2898 static int bpf_skb_proto_4_to_6(struct sk_buff *skb) 2899 { 2900 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr); 2901 u32 off = skb_mac_header_len(skb); 2902 int ret; 2903 2904 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) 2905 return -ENOTSUPP; 2906 2907 ret = skb_cow(skb, len_diff); 2908 if (unlikely(ret < 0)) 2909 return ret; 2910 2911 ret = bpf_skb_net_hdr_push(skb, off, len_diff); 2912 if (unlikely(ret < 0)) 2913 return ret; 2914 2915 if (skb_is_gso(skb)) { 2916 struct skb_shared_info *shinfo = skb_shinfo(skb); 2917 2918 /* SKB_GSO_TCPV4 needs to be changed into 2919 * SKB_GSO_TCPV6. 2920 */ 2921 if (shinfo->gso_type & SKB_GSO_TCPV4) { 2922 shinfo->gso_type &= ~SKB_GSO_TCPV4; 2923 shinfo->gso_type |= SKB_GSO_TCPV6; 2924 } 2925 2926 /* Due to IPv6 header, MSS needs to be downgraded. */ 2927 skb_decrease_gso_size(shinfo, len_diff); 2928 /* Header must be checked, and gso_segs recomputed. */ 2929 shinfo->gso_type |= SKB_GSO_DODGY; 2930 shinfo->gso_segs = 0; 2931 } 2932 2933 skb->protocol = htons(ETH_P_IPV6); 2934 skb_clear_hash(skb); 2935 2936 return 0; 2937 } 2938 2939 static int bpf_skb_proto_6_to_4(struct sk_buff *skb) 2940 { 2941 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr); 2942 u32 off = skb_mac_header_len(skb); 2943 int ret; 2944 2945 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) 2946 return -ENOTSUPP; 2947 2948 ret = skb_unclone(skb, GFP_ATOMIC); 2949 if (unlikely(ret < 0)) 2950 return ret; 2951 2952 ret = bpf_skb_net_hdr_pop(skb, off, len_diff); 2953 if (unlikely(ret < 0)) 2954 return ret; 2955 2956 if (skb_is_gso(skb)) { 2957 struct skb_shared_info *shinfo = skb_shinfo(skb); 2958 2959 /* SKB_GSO_TCPV6 needs to be changed into 2960 * SKB_GSO_TCPV4. 2961 */ 2962 if (shinfo->gso_type & SKB_GSO_TCPV6) { 2963 shinfo->gso_type &= ~SKB_GSO_TCPV6; 2964 shinfo->gso_type |= SKB_GSO_TCPV4; 2965 } 2966 2967 /* Due to IPv4 header, MSS can be upgraded. */ 2968 skb_increase_gso_size(shinfo, len_diff); 2969 /* Header must be checked, and gso_segs recomputed. */ 2970 shinfo->gso_type |= SKB_GSO_DODGY; 2971 shinfo->gso_segs = 0; 2972 } 2973 2974 skb->protocol = htons(ETH_P_IP); 2975 skb_clear_hash(skb); 2976 2977 return 0; 2978 } 2979 2980 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto) 2981 { 2982 __be16 from_proto = skb->protocol; 2983 2984 if (from_proto == htons(ETH_P_IP) && 2985 to_proto == htons(ETH_P_IPV6)) 2986 return bpf_skb_proto_4_to_6(skb); 2987 2988 if (from_proto == htons(ETH_P_IPV6) && 2989 to_proto == htons(ETH_P_IP)) 2990 return bpf_skb_proto_6_to_4(skb); 2991 2992 return -ENOTSUPP; 2993 } 2994 2995 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto, 2996 u64, flags) 2997 { 2998 int ret; 2999 3000 if (unlikely(flags)) 3001 return -EINVAL; 3002 3003 /* General idea is that this helper does the basic groundwork 3004 * needed for changing the protocol, and eBPF program fills the 3005 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace() 3006 * and other helpers, rather than passing a raw buffer here. 3007 * 3008 * The rationale is to keep this minimal and without a need to 3009 * deal with raw packet data. F.e. even if we would pass buffers 3010 * here, the program still needs to call the bpf_lX_csum_replace() 3011 * helpers anyway. Plus, this way we keep also separation of 3012 * concerns, since f.e. bpf_skb_store_bytes() should only take 3013 * care of stores. 3014 * 3015 * Currently, additional options and extension header space are 3016 * not supported, but flags register is reserved so we can adapt 3017 * that. For offloads, we mark packet as dodgy, so that headers 3018 * need to be verified first. 3019 */ 3020 ret = bpf_skb_proto_xlat(skb, proto); 3021 bpf_compute_data_pointers(skb); 3022 return ret; 3023 } 3024 3025 static const struct bpf_func_proto bpf_skb_change_proto_proto = { 3026 .func = bpf_skb_change_proto, 3027 .gpl_only = false, 3028 .ret_type = RET_INTEGER, 3029 .arg1_type = ARG_PTR_TO_CTX, 3030 .arg2_type = ARG_ANYTHING, 3031 .arg3_type = ARG_ANYTHING, 3032 }; 3033 3034 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type) 3035 { 3036 /* We only allow a restricted subset to be changed for now. */ 3037 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) || 3038 !skb_pkt_type_ok(pkt_type))) 3039 return -EINVAL; 3040 3041 skb->pkt_type = pkt_type; 3042 return 0; 3043 } 3044 3045 static const struct bpf_func_proto bpf_skb_change_type_proto = { 3046 .func = bpf_skb_change_type, 3047 .gpl_only = false, 3048 .ret_type = RET_INTEGER, 3049 .arg1_type = ARG_PTR_TO_CTX, 3050 .arg2_type = ARG_ANYTHING, 3051 }; 3052 3053 static u32 bpf_skb_net_base_len(const struct sk_buff *skb) 3054 { 3055 switch (skb->protocol) { 3056 case htons(ETH_P_IP): 3057 return sizeof(struct iphdr); 3058 case htons(ETH_P_IPV6): 3059 return sizeof(struct ipv6hdr); 3060 default: 3061 return ~0U; 3062 } 3063 } 3064 3065 #define BPF_F_ADJ_ROOM_ENCAP_L3_MASK (BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 | \ 3066 BPF_F_ADJ_ROOM_ENCAP_L3_IPV6) 3067 3068 #define BPF_F_ADJ_ROOM_MASK (BPF_F_ADJ_ROOM_FIXED_GSO | \ 3069 BPF_F_ADJ_ROOM_ENCAP_L3_MASK | \ 3070 BPF_F_ADJ_ROOM_ENCAP_L4_GRE | \ 3071 BPF_F_ADJ_ROOM_ENCAP_L4_UDP | \ 3072 BPF_F_ADJ_ROOM_ENCAP_L2( \ 3073 BPF_ADJ_ROOM_ENCAP_L2_MASK)) 3074 3075 static int bpf_skb_net_grow(struct sk_buff *skb, u32 off, u32 len_diff, 3076 u64 flags) 3077 { 3078 u8 inner_mac_len = flags >> BPF_ADJ_ROOM_ENCAP_L2_SHIFT; 3079 bool encap = flags & BPF_F_ADJ_ROOM_ENCAP_L3_MASK; 3080 u16 mac_len = 0, inner_net = 0, inner_trans = 0; 3081 unsigned int gso_type = SKB_GSO_DODGY; 3082 int ret; 3083 3084 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) { 3085 /* udp gso_size delineates datagrams, only allow if fixed */ 3086 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) || 3087 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO)) 3088 return -ENOTSUPP; 3089 } 3090 3091 ret = skb_cow_head(skb, len_diff); 3092 if (unlikely(ret < 0)) 3093 return ret; 3094 3095 if (encap) { 3096 if (skb->protocol != htons(ETH_P_IP) && 3097 skb->protocol != htons(ETH_P_IPV6)) 3098 return -ENOTSUPP; 3099 3100 if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 && 3101 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6) 3102 return -EINVAL; 3103 3104 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE && 3105 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP) 3106 return -EINVAL; 3107 3108 if (skb->encapsulation) 3109 return -EALREADY; 3110 3111 mac_len = skb->network_header - skb->mac_header; 3112 inner_net = skb->network_header; 3113 if (inner_mac_len > len_diff) 3114 return -EINVAL; 3115 inner_trans = skb->transport_header; 3116 } 3117 3118 ret = bpf_skb_net_hdr_push(skb, off, len_diff); 3119 if (unlikely(ret < 0)) 3120 return ret; 3121 3122 if (encap) { 3123 skb->inner_mac_header = inner_net - inner_mac_len; 3124 skb->inner_network_header = inner_net; 3125 skb->inner_transport_header = inner_trans; 3126 skb_set_inner_protocol(skb, skb->protocol); 3127 3128 skb->encapsulation = 1; 3129 skb_set_network_header(skb, mac_len); 3130 3131 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP) 3132 gso_type |= SKB_GSO_UDP_TUNNEL; 3133 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE) 3134 gso_type |= SKB_GSO_GRE; 3135 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6) 3136 gso_type |= SKB_GSO_IPXIP6; 3137 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4) 3138 gso_type |= SKB_GSO_IPXIP4; 3139 3140 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE || 3141 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP) { 3142 int nh_len = flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 ? 3143 sizeof(struct ipv6hdr) : 3144 sizeof(struct iphdr); 3145 3146 skb_set_transport_header(skb, mac_len + nh_len); 3147 } 3148 3149 /* Match skb->protocol to new outer l3 protocol */ 3150 if (skb->protocol == htons(ETH_P_IP) && 3151 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6) 3152 skb->protocol = htons(ETH_P_IPV6); 3153 else if (skb->protocol == htons(ETH_P_IPV6) && 3154 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4) 3155 skb->protocol = htons(ETH_P_IP); 3156 } 3157 3158 if (skb_is_gso(skb)) { 3159 struct skb_shared_info *shinfo = skb_shinfo(skb); 3160 3161 /* Due to header grow, MSS needs to be downgraded. */ 3162 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO)) 3163 skb_decrease_gso_size(shinfo, len_diff); 3164 3165 /* Header must be checked, and gso_segs recomputed. */ 3166 shinfo->gso_type |= gso_type; 3167 shinfo->gso_segs = 0; 3168 } 3169 3170 return 0; 3171 } 3172 3173 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 off, u32 len_diff, 3174 u64 flags) 3175 { 3176 int ret; 3177 3178 if (unlikely(flags & ~(BPF_F_ADJ_ROOM_FIXED_GSO | 3179 BPF_F_ADJ_ROOM_NO_CSUM_RESET))) 3180 return -EINVAL; 3181 3182 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) { 3183 /* udp gso_size delineates datagrams, only allow if fixed */ 3184 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) || 3185 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO)) 3186 return -ENOTSUPP; 3187 } 3188 3189 ret = skb_unclone(skb, GFP_ATOMIC); 3190 if (unlikely(ret < 0)) 3191 return ret; 3192 3193 ret = bpf_skb_net_hdr_pop(skb, off, len_diff); 3194 if (unlikely(ret < 0)) 3195 return ret; 3196 3197 if (skb_is_gso(skb)) { 3198 struct skb_shared_info *shinfo = skb_shinfo(skb); 3199 3200 /* Due to header shrink, MSS can be upgraded. */ 3201 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO)) 3202 skb_increase_gso_size(shinfo, len_diff); 3203 3204 /* Header must be checked, and gso_segs recomputed. */ 3205 shinfo->gso_type |= SKB_GSO_DODGY; 3206 shinfo->gso_segs = 0; 3207 } 3208 3209 return 0; 3210 } 3211 3212 static u32 __bpf_skb_max_len(const struct sk_buff *skb) 3213 { 3214 return skb->dev ? skb->dev->mtu + skb->dev->hard_header_len : 3215 SKB_MAX_ALLOC; 3216 } 3217 3218 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff, 3219 u32, mode, u64, flags) 3220 { 3221 u32 len_cur, len_diff_abs = abs(len_diff); 3222 u32 len_min = bpf_skb_net_base_len(skb); 3223 u32 len_max = __bpf_skb_max_len(skb); 3224 __be16 proto = skb->protocol; 3225 bool shrink = len_diff < 0; 3226 u32 off; 3227 int ret; 3228 3229 if (unlikely(flags & ~(BPF_F_ADJ_ROOM_MASK | 3230 BPF_F_ADJ_ROOM_NO_CSUM_RESET))) 3231 return -EINVAL; 3232 if (unlikely(len_diff_abs > 0xfffU)) 3233 return -EFAULT; 3234 if (unlikely(proto != htons(ETH_P_IP) && 3235 proto != htons(ETH_P_IPV6))) 3236 return -ENOTSUPP; 3237 3238 off = skb_mac_header_len(skb); 3239 switch (mode) { 3240 case BPF_ADJ_ROOM_NET: 3241 off += bpf_skb_net_base_len(skb); 3242 break; 3243 case BPF_ADJ_ROOM_MAC: 3244 break; 3245 default: 3246 return -ENOTSUPP; 3247 } 3248 3249 len_cur = skb->len - skb_network_offset(skb); 3250 if ((shrink && (len_diff_abs >= len_cur || 3251 len_cur - len_diff_abs < len_min)) || 3252 (!shrink && (skb->len + len_diff_abs > len_max && 3253 !skb_is_gso(skb)))) 3254 return -ENOTSUPP; 3255 3256 ret = shrink ? bpf_skb_net_shrink(skb, off, len_diff_abs, flags) : 3257 bpf_skb_net_grow(skb, off, len_diff_abs, flags); 3258 if (!ret && !(flags & BPF_F_ADJ_ROOM_NO_CSUM_RESET)) 3259 __skb_reset_checksum_unnecessary(skb); 3260 3261 bpf_compute_data_pointers(skb); 3262 return ret; 3263 } 3264 3265 static const struct bpf_func_proto bpf_skb_adjust_room_proto = { 3266 .func = bpf_skb_adjust_room, 3267 .gpl_only = false, 3268 .ret_type = RET_INTEGER, 3269 .arg1_type = ARG_PTR_TO_CTX, 3270 .arg2_type = ARG_ANYTHING, 3271 .arg3_type = ARG_ANYTHING, 3272 .arg4_type = ARG_ANYTHING, 3273 }; 3274 3275 static u32 __bpf_skb_min_len(const struct sk_buff *skb) 3276 { 3277 u32 min_len = skb_network_offset(skb); 3278 3279 if (skb_transport_header_was_set(skb)) 3280 min_len = skb_transport_offset(skb); 3281 if (skb->ip_summed == CHECKSUM_PARTIAL) 3282 min_len = skb_checksum_start_offset(skb) + 3283 skb->csum_offset + sizeof(__sum16); 3284 return min_len; 3285 } 3286 3287 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len) 3288 { 3289 unsigned int old_len = skb->len; 3290 int ret; 3291 3292 ret = __skb_grow_rcsum(skb, new_len); 3293 if (!ret) 3294 memset(skb->data + old_len, 0, new_len - old_len); 3295 return ret; 3296 } 3297 3298 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len) 3299 { 3300 return __skb_trim_rcsum(skb, new_len); 3301 } 3302 3303 static inline int __bpf_skb_change_tail(struct sk_buff *skb, u32 new_len, 3304 u64 flags) 3305 { 3306 u32 max_len = __bpf_skb_max_len(skb); 3307 u32 min_len = __bpf_skb_min_len(skb); 3308 int ret; 3309 3310 if (unlikely(flags || new_len > max_len || new_len < min_len)) 3311 return -EINVAL; 3312 if (skb->encapsulation) 3313 return -ENOTSUPP; 3314 3315 /* The basic idea of this helper is that it's performing the 3316 * needed work to either grow or trim an skb, and eBPF program 3317 * rewrites the rest via helpers like bpf_skb_store_bytes(), 3318 * bpf_lX_csum_replace() and others rather than passing a raw 3319 * buffer here. This one is a slow path helper and intended 3320 * for replies with control messages. 3321 * 3322 * Like in bpf_skb_change_proto(), we want to keep this rather 3323 * minimal and without protocol specifics so that we are able 3324 * to separate concerns as in bpf_skb_store_bytes() should only 3325 * be the one responsible for writing buffers. 3326 * 3327 * It's really expected to be a slow path operation here for 3328 * control message replies, so we're implicitly linearizing, 3329 * uncloning and drop offloads from the skb by this. 3330 */ 3331 ret = __bpf_try_make_writable(skb, skb->len); 3332 if (!ret) { 3333 if (new_len > skb->len) 3334 ret = bpf_skb_grow_rcsum(skb, new_len); 3335 else if (new_len < skb->len) 3336 ret = bpf_skb_trim_rcsum(skb, new_len); 3337 if (!ret && skb_is_gso(skb)) 3338 skb_gso_reset(skb); 3339 } 3340 return ret; 3341 } 3342 3343 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len, 3344 u64, flags) 3345 { 3346 int ret = __bpf_skb_change_tail(skb, new_len, flags); 3347 3348 bpf_compute_data_pointers(skb); 3349 return ret; 3350 } 3351 3352 static const struct bpf_func_proto bpf_skb_change_tail_proto = { 3353 .func = bpf_skb_change_tail, 3354 .gpl_only = false, 3355 .ret_type = RET_INTEGER, 3356 .arg1_type = ARG_PTR_TO_CTX, 3357 .arg2_type = ARG_ANYTHING, 3358 .arg3_type = ARG_ANYTHING, 3359 }; 3360 3361 BPF_CALL_3(sk_skb_change_tail, struct sk_buff *, skb, u32, new_len, 3362 u64, flags) 3363 { 3364 int ret = __bpf_skb_change_tail(skb, new_len, flags); 3365 3366 bpf_compute_data_end_sk_skb(skb); 3367 return ret; 3368 } 3369 3370 static const struct bpf_func_proto sk_skb_change_tail_proto = { 3371 .func = sk_skb_change_tail, 3372 .gpl_only = false, 3373 .ret_type = RET_INTEGER, 3374 .arg1_type = ARG_PTR_TO_CTX, 3375 .arg2_type = ARG_ANYTHING, 3376 .arg3_type = ARG_ANYTHING, 3377 }; 3378 3379 static inline int __bpf_skb_change_head(struct sk_buff *skb, u32 head_room, 3380 u64 flags) 3381 { 3382 u32 max_len = __bpf_skb_max_len(skb); 3383 u32 new_len = skb->len + head_room; 3384 int ret; 3385 3386 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) || 3387 new_len < skb->len)) 3388 return -EINVAL; 3389 3390 ret = skb_cow(skb, head_room); 3391 if (likely(!ret)) { 3392 /* Idea for this helper is that we currently only 3393 * allow to expand on mac header. This means that 3394 * skb->protocol network header, etc, stay as is. 3395 * Compared to bpf_skb_change_tail(), we're more 3396 * flexible due to not needing to linearize or 3397 * reset GSO. Intention for this helper is to be 3398 * used by an L3 skb that needs to push mac header 3399 * for redirection into L2 device. 3400 */ 3401 __skb_push(skb, head_room); 3402 memset(skb->data, 0, head_room); 3403 skb_reset_mac_header(skb); 3404 } 3405 3406 return ret; 3407 } 3408 3409 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room, 3410 u64, flags) 3411 { 3412 int ret = __bpf_skb_change_head(skb, head_room, flags); 3413 3414 bpf_compute_data_pointers(skb); 3415 return ret; 3416 } 3417 3418 static const struct bpf_func_proto bpf_skb_change_head_proto = { 3419 .func = bpf_skb_change_head, 3420 .gpl_only = false, 3421 .ret_type = RET_INTEGER, 3422 .arg1_type = ARG_PTR_TO_CTX, 3423 .arg2_type = ARG_ANYTHING, 3424 .arg3_type = ARG_ANYTHING, 3425 }; 3426 3427 BPF_CALL_3(sk_skb_change_head, struct sk_buff *, skb, u32, head_room, 3428 u64, flags) 3429 { 3430 int ret = __bpf_skb_change_head(skb, head_room, flags); 3431 3432 bpf_compute_data_end_sk_skb(skb); 3433 return ret; 3434 } 3435 3436 static const struct bpf_func_proto sk_skb_change_head_proto = { 3437 .func = sk_skb_change_head, 3438 .gpl_only = false, 3439 .ret_type = RET_INTEGER, 3440 .arg1_type = ARG_PTR_TO_CTX, 3441 .arg2_type = ARG_ANYTHING, 3442 .arg3_type = ARG_ANYTHING, 3443 }; 3444 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp) 3445 { 3446 return xdp_data_meta_unsupported(xdp) ? 0 : 3447 xdp->data - xdp->data_meta; 3448 } 3449 3450 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset) 3451 { 3452 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame); 3453 unsigned long metalen = xdp_get_metalen(xdp); 3454 void *data_start = xdp_frame_end + metalen; 3455 void *data = xdp->data + offset; 3456 3457 if (unlikely(data < data_start || 3458 data > xdp->data_end - ETH_HLEN)) 3459 return -EINVAL; 3460 3461 if (metalen) 3462 memmove(xdp->data_meta + offset, 3463 xdp->data_meta, metalen); 3464 xdp->data_meta += offset; 3465 xdp->data = data; 3466 3467 return 0; 3468 } 3469 3470 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = { 3471 .func = bpf_xdp_adjust_head, 3472 .gpl_only = false, 3473 .ret_type = RET_INTEGER, 3474 .arg1_type = ARG_PTR_TO_CTX, 3475 .arg2_type = ARG_ANYTHING, 3476 }; 3477 3478 BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset) 3479 { 3480 void *data_hard_end = xdp_data_hard_end(xdp); /* use xdp->frame_sz */ 3481 void *data_end = xdp->data_end + offset; 3482 3483 /* Notice that xdp_data_hard_end have reserved some tailroom */ 3484 if (unlikely(data_end > data_hard_end)) 3485 return -EINVAL; 3486 3487 /* ALL drivers MUST init xdp->frame_sz, chicken check below */ 3488 if (unlikely(xdp->frame_sz > PAGE_SIZE)) { 3489 WARN_ONCE(1, "Too BIG xdp->frame_sz = %d\n", xdp->frame_sz); 3490 return -EINVAL; 3491 } 3492 3493 if (unlikely(data_end < xdp->data + ETH_HLEN)) 3494 return -EINVAL; 3495 3496 /* Clear memory area on grow, can contain uninit kernel memory */ 3497 if (offset > 0) 3498 memset(xdp->data_end, 0, offset); 3499 3500 xdp->data_end = data_end; 3501 3502 return 0; 3503 } 3504 3505 static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = { 3506 .func = bpf_xdp_adjust_tail, 3507 .gpl_only = false, 3508 .ret_type = RET_INTEGER, 3509 .arg1_type = ARG_PTR_TO_CTX, 3510 .arg2_type = ARG_ANYTHING, 3511 }; 3512 3513 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset) 3514 { 3515 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame); 3516 void *meta = xdp->data_meta + offset; 3517 unsigned long metalen = xdp->data - meta; 3518 3519 if (xdp_data_meta_unsupported(xdp)) 3520 return -ENOTSUPP; 3521 if (unlikely(meta < xdp_frame_end || 3522 meta > xdp->data)) 3523 return -EINVAL; 3524 if (unlikely((metalen & (sizeof(__u32) - 1)) || 3525 (metalen > 32))) 3526 return -EACCES; 3527 3528 xdp->data_meta = meta; 3529 3530 return 0; 3531 } 3532 3533 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = { 3534 .func = bpf_xdp_adjust_meta, 3535 .gpl_only = false, 3536 .ret_type = RET_INTEGER, 3537 .arg1_type = ARG_PTR_TO_CTX, 3538 .arg2_type = ARG_ANYTHING, 3539 }; 3540 3541 static int __bpf_tx_xdp_map(struct net_device *dev_rx, void *fwd, 3542 struct bpf_map *map, struct xdp_buff *xdp) 3543 { 3544 switch (map->map_type) { 3545 case BPF_MAP_TYPE_DEVMAP: 3546 case BPF_MAP_TYPE_DEVMAP_HASH: 3547 return dev_map_enqueue(fwd, xdp, dev_rx); 3548 case BPF_MAP_TYPE_CPUMAP: 3549 return cpu_map_enqueue(fwd, xdp, dev_rx); 3550 case BPF_MAP_TYPE_XSKMAP: 3551 return __xsk_map_redirect(fwd, xdp); 3552 default: 3553 return -EBADRQC; 3554 } 3555 return 0; 3556 } 3557 3558 void xdp_do_flush(void) 3559 { 3560 __dev_flush(); 3561 __cpu_map_flush(); 3562 __xsk_map_flush(); 3563 } 3564 EXPORT_SYMBOL_GPL(xdp_do_flush); 3565 3566 static inline void *__xdp_map_lookup_elem(struct bpf_map *map, u32 index) 3567 { 3568 switch (map->map_type) { 3569 case BPF_MAP_TYPE_DEVMAP: 3570 return __dev_map_lookup_elem(map, index); 3571 case BPF_MAP_TYPE_DEVMAP_HASH: 3572 return __dev_map_hash_lookup_elem(map, index); 3573 case BPF_MAP_TYPE_CPUMAP: 3574 return __cpu_map_lookup_elem(map, index); 3575 case BPF_MAP_TYPE_XSKMAP: 3576 return __xsk_map_lookup_elem(map, index); 3577 default: 3578 return NULL; 3579 } 3580 } 3581 3582 void bpf_clear_redirect_map(struct bpf_map *map) 3583 { 3584 struct bpf_redirect_info *ri; 3585 int cpu; 3586 3587 for_each_possible_cpu(cpu) { 3588 ri = per_cpu_ptr(&bpf_redirect_info, cpu); 3589 /* Avoid polluting remote cacheline due to writes if 3590 * not needed. Once we pass this test, we need the 3591 * cmpxchg() to make sure it hasn't been changed in 3592 * the meantime by remote CPU. 3593 */ 3594 if (unlikely(READ_ONCE(ri->map) == map)) 3595 cmpxchg(&ri->map, map, NULL); 3596 } 3597 } 3598 3599 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp, 3600 struct bpf_prog *xdp_prog) 3601 { 3602 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 3603 struct bpf_map *map = READ_ONCE(ri->map); 3604 u32 index = ri->tgt_index; 3605 void *fwd = ri->tgt_value; 3606 int err; 3607 3608 ri->tgt_index = 0; 3609 ri->tgt_value = NULL; 3610 WRITE_ONCE(ri->map, NULL); 3611 3612 if (unlikely(!map)) { 3613 fwd = dev_get_by_index_rcu(dev_net(dev), index); 3614 if (unlikely(!fwd)) { 3615 err = -EINVAL; 3616 goto err; 3617 } 3618 3619 err = dev_xdp_enqueue(fwd, xdp, dev); 3620 } else { 3621 err = __bpf_tx_xdp_map(dev, fwd, map, xdp); 3622 } 3623 3624 if (unlikely(err)) 3625 goto err; 3626 3627 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index); 3628 return 0; 3629 err: 3630 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err); 3631 return err; 3632 } 3633 EXPORT_SYMBOL_GPL(xdp_do_redirect); 3634 3635 static int xdp_do_generic_redirect_map(struct net_device *dev, 3636 struct sk_buff *skb, 3637 struct xdp_buff *xdp, 3638 struct bpf_prog *xdp_prog, 3639 struct bpf_map *map) 3640 { 3641 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 3642 u32 index = ri->tgt_index; 3643 void *fwd = ri->tgt_value; 3644 int err = 0; 3645 3646 ri->tgt_index = 0; 3647 ri->tgt_value = NULL; 3648 WRITE_ONCE(ri->map, NULL); 3649 3650 if (map->map_type == BPF_MAP_TYPE_DEVMAP || 3651 map->map_type == BPF_MAP_TYPE_DEVMAP_HASH) { 3652 struct bpf_dtab_netdev *dst = fwd; 3653 3654 err = dev_map_generic_redirect(dst, skb, xdp_prog); 3655 if (unlikely(err)) 3656 goto err; 3657 } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { 3658 struct xdp_sock *xs = fwd; 3659 3660 err = xsk_generic_rcv(xs, xdp); 3661 if (err) 3662 goto err; 3663 consume_skb(skb); 3664 } else { 3665 /* TODO: Handle BPF_MAP_TYPE_CPUMAP */ 3666 err = -EBADRQC; 3667 goto err; 3668 } 3669 3670 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index); 3671 return 0; 3672 err: 3673 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err); 3674 return err; 3675 } 3676 3677 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb, 3678 struct xdp_buff *xdp, struct bpf_prog *xdp_prog) 3679 { 3680 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 3681 struct bpf_map *map = READ_ONCE(ri->map); 3682 u32 index = ri->tgt_index; 3683 struct net_device *fwd; 3684 int err = 0; 3685 3686 if (map) 3687 return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog, 3688 map); 3689 ri->tgt_index = 0; 3690 fwd = dev_get_by_index_rcu(dev_net(dev), index); 3691 if (unlikely(!fwd)) { 3692 err = -EINVAL; 3693 goto err; 3694 } 3695 3696 err = xdp_ok_fwd_dev(fwd, skb->len); 3697 if (unlikely(err)) 3698 goto err; 3699 3700 skb->dev = fwd; 3701 _trace_xdp_redirect(dev, xdp_prog, index); 3702 generic_xdp_tx(skb, xdp_prog); 3703 return 0; 3704 err: 3705 _trace_xdp_redirect_err(dev, xdp_prog, index, err); 3706 return err; 3707 } 3708 3709 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags) 3710 { 3711 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 3712 3713 if (unlikely(flags)) 3714 return XDP_ABORTED; 3715 3716 ri->flags = flags; 3717 ri->tgt_index = ifindex; 3718 ri->tgt_value = NULL; 3719 WRITE_ONCE(ri->map, NULL); 3720 3721 return XDP_REDIRECT; 3722 } 3723 3724 static const struct bpf_func_proto bpf_xdp_redirect_proto = { 3725 .func = bpf_xdp_redirect, 3726 .gpl_only = false, 3727 .ret_type = RET_INTEGER, 3728 .arg1_type = ARG_ANYTHING, 3729 .arg2_type = ARG_ANYTHING, 3730 }; 3731 3732 BPF_CALL_3(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex, 3733 u64, flags) 3734 { 3735 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 3736 3737 /* Lower bits of the flags are used as return code on lookup failure */ 3738 if (unlikely(flags > XDP_TX)) 3739 return XDP_ABORTED; 3740 3741 ri->tgt_value = __xdp_map_lookup_elem(map, ifindex); 3742 if (unlikely(!ri->tgt_value)) { 3743 /* If the lookup fails we want to clear out the state in the 3744 * redirect_info struct completely, so that if an eBPF program 3745 * performs multiple lookups, the last one always takes 3746 * precedence. 3747 */ 3748 WRITE_ONCE(ri->map, NULL); 3749 return flags; 3750 } 3751 3752 ri->flags = flags; 3753 ri->tgt_index = ifindex; 3754 WRITE_ONCE(ri->map, map); 3755 3756 return XDP_REDIRECT; 3757 } 3758 3759 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = { 3760 .func = bpf_xdp_redirect_map, 3761 .gpl_only = false, 3762 .ret_type = RET_INTEGER, 3763 .arg1_type = ARG_CONST_MAP_PTR, 3764 .arg2_type = ARG_ANYTHING, 3765 .arg3_type = ARG_ANYTHING, 3766 }; 3767 3768 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb, 3769 unsigned long off, unsigned long len) 3770 { 3771 void *ptr = skb_header_pointer(skb, off, len, dst_buff); 3772 3773 if (unlikely(!ptr)) 3774 return len; 3775 if (ptr != dst_buff) 3776 memcpy(dst_buff, ptr, len); 3777 3778 return 0; 3779 } 3780 3781 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map, 3782 u64, flags, void *, meta, u64, meta_size) 3783 { 3784 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32; 3785 3786 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK))) 3787 return -EINVAL; 3788 if (unlikely(!skb || skb_size > skb->len)) 3789 return -EFAULT; 3790 3791 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size, 3792 bpf_skb_copy); 3793 } 3794 3795 static const struct bpf_func_proto bpf_skb_event_output_proto = { 3796 .func = bpf_skb_event_output, 3797 .gpl_only = true, 3798 .ret_type = RET_INTEGER, 3799 .arg1_type = ARG_PTR_TO_CTX, 3800 .arg2_type = ARG_CONST_MAP_PTR, 3801 .arg3_type = ARG_ANYTHING, 3802 .arg4_type = ARG_PTR_TO_MEM, 3803 .arg5_type = ARG_CONST_SIZE_OR_ZERO, 3804 }; 3805 3806 BTF_ID_LIST(bpf_skb_output_btf_ids) 3807 BTF_ID(struct, sk_buff) 3808 3809 const struct bpf_func_proto bpf_skb_output_proto = { 3810 .func = bpf_skb_event_output, 3811 .gpl_only = true, 3812 .ret_type = RET_INTEGER, 3813 .arg1_type = ARG_PTR_TO_BTF_ID, 3814 .arg2_type = ARG_CONST_MAP_PTR, 3815 .arg3_type = ARG_ANYTHING, 3816 .arg4_type = ARG_PTR_TO_MEM, 3817 .arg5_type = ARG_CONST_SIZE_OR_ZERO, 3818 .btf_id = bpf_skb_output_btf_ids, 3819 }; 3820 3821 static unsigned short bpf_tunnel_key_af(u64 flags) 3822 { 3823 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET; 3824 } 3825 3826 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to, 3827 u32, size, u64, flags) 3828 { 3829 const struct ip_tunnel_info *info = skb_tunnel_info(skb); 3830 u8 compat[sizeof(struct bpf_tunnel_key)]; 3831 void *to_orig = to; 3832 int err; 3833 3834 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) { 3835 err = -EINVAL; 3836 goto err_clear; 3837 } 3838 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) { 3839 err = -EPROTO; 3840 goto err_clear; 3841 } 3842 if (unlikely(size != sizeof(struct bpf_tunnel_key))) { 3843 err = -EINVAL; 3844 switch (size) { 3845 case offsetof(struct bpf_tunnel_key, tunnel_label): 3846 case offsetof(struct bpf_tunnel_key, tunnel_ext): 3847 goto set_compat; 3848 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]): 3849 /* Fixup deprecated structure layouts here, so we have 3850 * a common path later on. 3851 */ 3852 if (ip_tunnel_info_af(info) != AF_INET) 3853 goto err_clear; 3854 set_compat: 3855 to = (struct bpf_tunnel_key *)compat; 3856 break; 3857 default: 3858 goto err_clear; 3859 } 3860 } 3861 3862 to->tunnel_id = be64_to_cpu(info->key.tun_id); 3863 to->tunnel_tos = info->key.tos; 3864 to->tunnel_ttl = info->key.ttl; 3865 to->tunnel_ext = 0; 3866 3867 if (flags & BPF_F_TUNINFO_IPV6) { 3868 memcpy(to->remote_ipv6, &info->key.u.ipv6.src, 3869 sizeof(to->remote_ipv6)); 3870 to->tunnel_label = be32_to_cpu(info->key.label); 3871 } else { 3872 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src); 3873 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3); 3874 to->tunnel_label = 0; 3875 } 3876 3877 if (unlikely(size != sizeof(struct bpf_tunnel_key))) 3878 memcpy(to_orig, to, size); 3879 3880 return 0; 3881 err_clear: 3882 memset(to_orig, 0, size); 3883 return err; 3884 } 3885 3886 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = { 3887 .func = bpf_skb_get_tunnel_key, 3888 .gpl_only = false, 3889 .ret_type = RET_INTEGER, 3890 .arg1_type = ARG_PTR_TO_CTX, 3891 .arg2_type = ARG_PTR_TO_UNINIT_MEM, 3892 .arg3_type = ARG_CONST_SIZE, 3893 .arg4_type = ARG_ANYTHING, 3894 }; 3895 3896 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size) 3897 { 3898 const struct ip_tunnel_info *info = skb_tunnel_info(skb); 3899 int err; 3900 3901 if (unlikely(!info || 3902 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) { 3903 err = -ENOENT; 3904 goto err_clear; 3905 } 3906 if (unlikely(size < info->options_len)) { 3907 err = -ENOMEM; 3908 goto err_clear; 3909 } 3910 3911 ip_tunnel_info_opts_get(to, info); 3912 if (size > info->options_len) 3913 memset(to + info->options_len, 0, size - info->options_len); 3914 3915 return info->options_len; 3916 err_clear: 3917 memset(to, 0, size); 3918 return err; 3919 } 3920 3921 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = { 3922 .func = bpf_skb_get_tunnel_opt, 3923 .gpl_only = false, 3924 .ret_type = RET_INTEGER, 3925 .arg1_type = ARG_PTR_TO_CTX, 3926 .arg2_type = ARG_PTR_TO_UNINIT_MEM, 3927 .arg3_type = ARG_CONST_SIZE, 3928 }; 3929 3930 static struct metadata_dst __percpu *md_dst; 3931 3932 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb, 3933 const struct bpf_tunnel_key *, from, u32, size, u64, flags) 3934 { 3935 struct metadata_dst *md = this_cpu_ptr(md_dst); 3936 u8 compat[sizeof(struct bpf_tunnel_key)]; 3937 struct ip_tunnel_info *info; 3938 3939 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX | 3940 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER))) 3941 return -EINVAL; 3942 if (unlikely(size != sizeof(struct bpf_tunnel_key))) { 3943 switch (size) { 3944 case offsetof(struct bpf_tunnel_key, tunnel_label): 3945 case offsetof(struct bpf_tunnel_key, tunnel_ext): 3946 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]): 3947 /* Fixup deprecated structure layouts here, so we have 3948 * a common path later on. 3949 */ 3950 memcpy(compat, from, size); 3951 memset(compat + size, 0, sizeof(compat) - size); 3952 from = (const struct bpf_tunnel_key *) compat; 3953 break; 3954 default: 3955 return -EINVAL; 3956 } 3957 } 3958 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) || 3959 from->tunnel_ext)) 3960 return -EINVAL; 3961 3962 skb_dst_drop(skb); 3963 dst_hold((struct dst_entry *) md); 3964 skb_dst_set(skb, (struct dst_entry *) md); 3965 3966 info = &md->u.tun_info; 3967 memset(info, 0, sizeof(*info)); 3968 info->mode = IP_TUNNEL_INFO_TX; 3969 3970 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE; 3971 if (flags & BPF_F_DONT_FRAGMENT) 3972 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT; 3973 if (flags & BPF_F_ZERO_CSUM_TX) 3974 info->key.tun_flags &= ~TUNNEL_CSUM; 3975 if (flags & BPF_F_SEQ_NUMBER) 3976 info->key.tun_flags |= TUNNEL_SEQ; 3977 3978 info->key.tun_id = cpu_to_be64(from->tunnel_id); 3979 info->key.tos = from->tunnel_tos; 3980 info->key.ttl = from->tunnel_ttl; 3981 3982 if (flags & BPF_F_TUNINFO_IPV6) { 3983 info->mode |= IP_TUNNEL_INFO_IPV6; 3984 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6, 3985 sizeof(from->remote_ipv6)); 3986 info->key.label = cpu_to_be32(from->tunnel_label) & 3987 IPV6_FLOWLABEL_MASK; 3988 } else { 3989 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4); 3990 } 3991 3992 return 0; 3993 } 3994 3995 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = { 3996 .func = bpf_skb_set_tunnel_key, 3997 .gpl_only = false, 3998 .ret_type = RET_INTEGER, 3999 .arg1_type = ARG_PTR_TO_CTX, 4000 .arg2_type = ARG_PTR_TO_MEM, 4001 .arg3_type = ARG_CONST_SIZE, 4002 .arg4_type = ARG_ANYTHING, 4003 }; 4004 4005 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb, 4006 const u8 *, from, u32, size) 4007 { 4008 struct ip_tunnel_info *info = skb_tunnel_info(skb); 4009 const struct metadata_dst *md = this_cpu_ptr(md_dst); 4010 4011 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1)))) 4012 return -EINVAL; 4013 if (unlikely(size > IP_TUNNEL_OPTS_MAX)) 4014 return -ENOMEM; 4015 4016 ip_tunnel_info_opts_set(info, from, size, TUNNEL_OPTIONS_PRESENT); 4017 4018 return 0; 4019 } 4020 4021 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = { 4022 .func = bpf_skb_set_tunnel_opt, 4023 .gpl_only = false, 4024 .ret_type = RET_INTEGER, 4025 .arg1_type = ARG_PTR_TO_CTX, 4026 .arg2_type = ARG_PTR_TO_MEM, 4027 .arg3_type = ARG_CONST_SIZE, 4028 }; 4029 4030 static const struct bpf_func_proto * 4031 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which) 4032 { 4033 if (!md_dst) { 4034 struct metadata_dst __percpu *tmp; 4035 4036 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX, 4037 METADATA_IP_TUNNEL, 4038 GFP_KERNEL); 4039 if (!tmp) 4040 return NULL; 4041 if (cmpxchg(&md_dst, NULL, tmp)) 4042 metadata_dst_free_percpu(tmp); 4043 } 4044 4045 switch (which) { 4046 case BPF_FUNC_skb_set_tunnel_key: 4047 return &bpf_skb_set_tunnel_key_proto; 4048 case BPF_FUNC_skb_set_tunnel_opt: 4049 return &bpf_skb_set_tunnel_opt_proto; 4050 default: 4051 return NULL; 4052 } 4053 } 4054 4055 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map, 4056 u32, idx) 4057 { 4058 struct bpf_array *array = container_of(map, struct bpf_array, map); 4059 struct cgroup *cgrp; 4060 struct sock *sk; 4061 4062 sk = skb_to_full_sk(skb); 4063 if (!sk || !sk_fullsock(sk)) 4064 return -ENOENT; 4065 if (unlikely(idx >= array->map.max_entries)) 4066 return -E2BIG; 4067 4068 cgrp = READ_ONCE(array->ptrs[idx]); 4069 if (unlikely(!cgrp)) 4070 return -EAGAIN; 4071 4072 return sk_under_cgroup_hierarchy(sk, cgrp); 4073 } 4074 4075 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = { 4076 .func = bpf_skb_under_cgroup, 4077 .gpl_only = false, 4078 .ret_type = RET_INTEGER, 4079 .arg1_type = ARG_PTR_TO_CTX, 4080 .arg2_type = ARG_CONST_MAP_PTR, 4081 .arg3_type = ARG_ANYTHING, 4082 }; 4083 4084 #ifdef CONFIG_SOCK_CGROUP_DATA 4085 static inline u64 __bpf_sk_cgroup_id(struct sock *sk) 4086 { 4087 struct cgroup *cgrp; 4088 4089 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); 4090 return cgroup_id(cgrp); 4091 } 4092 4093 BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb) 4094 { 4095 struct sock *sk = skb_to_full_sk(skb); 4096 4097 if (!sk || !sk_fullsock(sk)) 4098 return 0; 4099 4100 return __bpf_sk_cgroup_id(sk); 4101 } 4102 4103 static const struct bpf_func_proto bpf_skb_cgroup_id_proto = { 4104 .func = bpf_skb_cgroup_id, 4105 .gpl_only = false, 4106 .ret_type = RET_INTEGER, 4107 .arg1_type = ARG_PTR_TO_CTX, 4108 }; 4109 4110 static inline u64 __bpf_sk_ancestor_cgroup_id(struct sock *sk, 4111 int ancestor_level) 4112 { 4113 struct cgroup *ancestor; 4114 struct cgroup *cgrp; 4115 4116 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); 4117 ancestor = cgroup_ancestor(cgrp, ancestor_level); 4118 if (!ancestor) 4119 return 0; 4120 4121 return cgroup_id(ancestor); 4122 } 4123 4124 BPF_CALL_2(bpf_skb_ancestor_cgroup_id, const struct sk_buff *, skb, int, 4125 ancestor_level) 4126 { 4127 struct sock *sk = skb_to_full_sk(skb); 4128 4129 if (!sk || !sk_fullsock(sk)) 4130 return 0; 4131 4132 return __bpf_sk_ancestor_cgroup_id(sk, ancestor_level); 4133 } 4134 4135 static const struct bpf_func_proto bpf_skb_ancestor_cgroup_id_proto = { 4136 .func = bpf_skb_ancestor_cgroup_id, 4137 .gpl_only = false, 4138 .ret_type = RET_INTEGER, 4139 .arg1_type = ARG_PTR_TO_CTX, 4140 .arg2_type = ARG_ANYTHING, 4141 }; 4142 4143 BPF_CALL_1(bpf_sk_cgroup_id, struct sock *, sk) 4144 { 4145 return __bpf_sk_cgroup_id(sk); 4146 } 4147 4148 static const struct bpf_func_proto bpf_sk_cgroup_id_proto = { 4149 .func = bpf_sk_cgroup_id, 4150 .gpl_only = false, 4151 .ret_type = RET_INTEGER, 4152 .arg1_type = ARG_PTR_TO_SOCKET, 4153 }; 4154 4155 BPF_CALL_2(bpf_sk_ancestor_cgroup_id, struct sock *, sk, int, ancestor_level) 4156 { 4157 return __bpf_sk_ancestor_cgroup_id(sk, ancestor_level); 4158 } 4159 4160 static const struct bpf_func_proto bpf_sk_ancestor_cgroup_id_proto = { 4161 .func = bpf_sk_ancestor_cgroup_id, 4162 .gpl_only = false, 4163 .ret_type = RET_INTEGER, 4164 .arg1_type = ARG_PTR_TO_SOCKET, 4165 .arg2_type = ARG_ANYTHING, 4166 }; 4167 #endif 4168 4169 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff, 4170 unsigned long off, unsigned long len) 4171 { 4172 memcpy(dst_buff, src_buff + off, len); 4173 return 0; 4174 } 4175 4176 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map, 4177 u64, flags, void *, meta, u64, meta_size) 4178 { 4179 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32; 4180 4181 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK))) 4182 return -EINVAL; 4183 if (unlikely(!xdp || 4184 xdp_size > (unsigned long)(xdp->data_end - xdp->data))) 4185 return -EFAULT; 4186 4187 return bpf_event_output(map, flags, meta, meta_size, xdp->data, 4188 xdp_size, bpf_xdp_copy); 4189 } 4190 4191 static const struct bpf_func_proto bpf_xdp_event_output_proto = { 4192 .func = bpf_xdp_event_output, 4193 .gpl_only = true, 4194 .ret_type = RET_INTEGER, 4195 .arg1_type = ARG_PTR_TO_CTX, 4196 .arg2_type = ARG_CONST_MAP_PTR, 4197 .arg3_type = ARG_ANYTHING, 4198 .arg4_type = ARG_PTR_TO_MEM, 4199 .arg5_type = ARG_CONST_SIZE_OR_ZERO, 4200 }; 4201 4202 BTF_ID_LIST(bpf_xdp_output_btf_ids) 4203 BTF_ID(struct, xdp_buff) 4204 4205 const struct bpf_func_proto bpf_xdp_output_proto = { 4206 .func = bpf_xdp_event_output, 4207 .gpl_only = true, 4208 .ret_type = RET_INTEGER, 4209 .arg1_type = ARG_PTR_TO_BTF_ID, 4210 .arg2_type = ARG_CONST_MAP_PTR, 4211 .arg3_type = ARG_ANYTHING, 4212 .arg4_type = ARG_PTR_TO_MEM, 4213 .arg5_type = ARG_CONST_SIZE_OR_ZERO, 4214 .btf_id = bpf_xdp_output_btf_ids, 4215 }; 4216 4217 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb) 4218 { 4219 return skb->sk ? sock_gen_cookie(skb->sk) : 0; 4220 } 4221 4222 static const struct bpf_func_proto bpf_get_socket_cookie_proto = { 4223 .func = bpf_get_socket_cookie, 4224 .gpl_only = false, 4225 .ret_type = RET_INTEGER, 4226 .arg1_type = ARG_PTR_TO_CTX, 4227 }; 4228 4229 BPF_CALL_1(bpf_get_socket_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx) 4230 { 4231 return sock_gen_cookie(ctx->sk); 4232 } 4233 4234 static const struct bpf_func_proto bpf_get_socket_cookie_sock_addr_proto = { 4235 .func = bpf_get_socket_cookie_sock_addr, 4236 .gpl_only = false, 4237 .ret_type = RET_INTEGER, 4238 .arg1_type = ARG_PTR_TO_CTX, 4239 }; 4240 4241 BPF_CALL_1(bpf_get_socket_cookie_sock, struct sock *, ctx) 4242 { 4243 return sock_gen_cookie(ctx); 4244 } 4245 4246 static const struct bpf_func_proto bpf_get_socket_cookie_sock_proto = { 4247 .func = bpf_get_socket_cookie_sock, 4248 .gpl_only = false, 4249 .ret_type = RET_INTEGER, 4250 .arg1_type = ARG_PTR_TO_CTX, 4251 }; 4252 4253 BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx) 4254 { 4255 return sock_gen_cookie(ctx->sk); 4256 } 4257 4258 static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = { 4259 .func = bpf_get_socket_cookie_sock_ops, 4260 .gpl_only = false, 4261 .ret_type = RET_INTEGER, 4262 .arg1_type = ARG_PTR_TO_CTX, 4263 }; 4264 4265 static u64 __bpf_get_netns_cookie(struct sock *sk) 4266 { 4267 #ifdef CONFIG_NET_NS 4268 return net_gen_cookie(sk ? sk->sk_net.net : &init_net); 4269 #else 4270 return 0; 4271 #endif 4272 } 4273 4274 BPF_CALL_1(bpf_get_netns_cookie_sock, struct sock *, ctx) 4275 { 4276 return __bpf_get_netns_cookie(ctx); 4277 } 4278 4279 static const struct bpf_func_proto bpf_get_netns_cookie_sock_proto = { 4280 .func = bpf_get_netns_cookie_sock, 4281 .gpl_only = false, 4282 .ret_type = RET_INTEGER, 4283 .arg1_type = ARG_PTR_TO_CTX_OR_NULL, 4284 }; 4285 4286 BPF_CALL_1(bpf_get_netns_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx) 4287 { 4288 return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL); 4289 } 4290 4291 static const struct bpf_func_proto bpf_get_netns_cookie_sock_addr_proto = { 4292 .func = bpf_get_netns_cookie_sock_addr, 4293 .gpl_only = false, 4294 .ret_type = RET_INTEGER, 4295 .arg1_type = ARG_PTR_TO_CTX_OR_NULL, 4296 }; 4297 4298 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb) 4299 { 4300 struct sock *sk = sk_to_full_sk(skb->sk); 4301 kuid_t kuid; 4302 4303 if (!sk || !sk_fullsock(sk)) 4304 return overflowuid; 4305 kuid = sock_net_uid(sock_net(sk), sk); 4306 return from_kuid_munged(sock_net(sk)->user_ns, kuid); 4307 } 4308 4309 static const struct bpf_func_proto bpf_get_socket_uid_proto = { 4310 .func = bpf_get_socket_uid, 4311 .gpl_only = false, 4312 .ret_type = RET_INTEGER, 4313 .arg1_type = ARG_PTR_TO_CTX, 4314 }; 4315 4316 #define SOCKOPT_CC_REINIT (1 << 0) 4317 4318 static int _bpf_setsockopt(struct sock *sk, int level, int optname, 4319 char *optval, int optlen, u32 flags) 4320 { 4321 char devname[IFNAMSIZ]; 4322 int val, valbool; 4323 struct net *net; 4324 int ifindex; 4325 int ret = 0; 4326 4327 if (!sk_fullsock(sk)) 4328 return -EINVAL; 4329 4330 sock_owned_by_me(sk); 4331 4332 if (level == SOL_SOCKET) { 4333 if (optlen != sizeof(int) && optname != SO_BINDTODEVICE) 4334 return -EINVAL; 4335 val = *((int *)optval); 4336 valbool = val ? 1 : 0; 4337 4338 /* Only some socketops are supported */ 4339 switch (optname) { 4340 case SO_RCVBUF: 4341 val = min_t(u32, val, sysctl_rmem_max); 4342 sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 4343 WRITE_ONCE(sk->sk_rcvbuf, 4344 max_t(int, val * 2, SOCK_MIN_RCVBUF)); 4345 break; 4346 case SO_SNDBUF: 4347 val = min_t(u32, val, sysctl_wmem_max); 4348 sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 4349 WRITE_ONCE(sk->sk_sndbuf, 4350 max_t(int, val * 2, SOCK_MIN_SNDBUF)); 4351 break; 4352 case SO_MAX_PACING_RATE: /* 32bit version */ 4353 if (val != ~0U) 4354 cmpxchg(&sk->sk_pacing_status, 4355 SK_PACING_NONE, 4356 SK_PACING_NEEDED); 4357 sk->sk_max_pacing_rate = (val == ~0U) ? ~0UL : val; 4358 sk->sk_pacing_rate = min(sk->sk_pacing_rate, 4359 sk->sk_max_pacing_rate); 4360 break; 4361 case SO_PRIORITY: 4362 sk->sk_priority = val; 4363 break; 4364 case SO_RCVLOWAT: 4365 if (val < 0) 4366 val = INT_MAX; 4367 WRITE_ONCE(sk->sk_rcvlowat, val ? : 1); 4368 break; 4369 case SO_MARK: 4370 if (sk->sk_mark != val) { 4371 sk->sk_mark = val; 4372 sk_dst_reset(sk); 4373 } 4374 break; 4375 case SO_BINDTODEVICE: 4376 optlen = min_t(long, optlen, IFNAMSIZ - 1); 4377 strncpy(devname, optval, optlen); 4378 devname[optlen] = 0; 4379 4380 ifindex = 0; 4381 if (devname[0] != '\0') { 4382 struct net_device *dev; 4383 4384 ret = -ENODEV; 4385 4386 net = sock_net(sk); 4387 dev = dev_get_by_name(net, devname); 4388 if (!dev) 4389 break; 4390 ifindex = dev->ifindex; 4391 dev_put(dev); 4392 } 4393 ret = sock_bindtoindex(sk, ifindex, false); 4394 break; 4395 case SO_KEEPALIVE: 4396 if (sk->sk_prot->keepalive) 4397 sk->sk_prot->keepalive(sk, valbool); 4398 sock_valbool_flag(sk, SOCK_KEEPOPEN, valbool); 4399 break; 4400 default: 4401 ret = -EINVAL; 4402 } 4403 #ifdef CONFIG_INET 4404 } else if (level == SOL_IP) { 4405 if (optlen != sizeof(int) || sk->sk_family != AF_INET) 4406 return -EINVAL; 4407 4408 val = *((int *)optval); 4409 /* Only some options are supported */ 4410 switch (optname) { 4411 case IP_TOS: 4412 if (val < -1 || val > 0xff) { 4413 ret = -EINVAL; 4414 } else { 4415 struct inet_sock *inet = inet_sk(sk); 4416 4417 if (val == -1) 4418 val = 0; 4419 inet->tos = val; 4420 } 4421 break; 4422 default: 4423 ret = -EINVAL; 4424 } 4425 #if IS_ENABLED(CONFIG_IPV6) 4426 } else if (level == SOL_IPV6) { 4427 if (optlen != sizeof(int) || sk->sk_family != AF_INET6) 4428 return -EINVAL; 4429 4430 val = *((int *)optval); 4431 /* Only some options are supported */ 4432 switch (optname) { 4433 case IPV6_TCLASS: 4434 if (val < -1 || val > 0xff) { 4435 ret = -EINVAL; 4436 } else { 4437 struct ipv6_pinfo *np = inet6_sk(sk); 4438 4439 if (val == -1) 4440 val = 0; 4441 np->tclass = val; 4442 } 4443 break; 4444 default: 4445 ret = -EINVAL; 4446 } 4447 #endif 4448 } else if (level == SOL_TCP && 4449 sk->sk_prot->setsockopt == tcp_setsockopt) { 4450 if (optname == TCP_CONGESTION) { 4451 char name[TCP_CA_NAME_MAX]; 4452 bool reinit = flags & SOCKOPT_CC_REINIT; 4453 4454 strncpy(name, optval, min_t(long, optlen, 4455 TCP_CA_NAME_MAX-1)); 4456 name[TCP_CA_NAME_MAX-1] = 0; 4457 ret = tcp_set_congestion_control(sk, name, false, 4458 reinit, true); 4459 } else { 4460 struct inet_connection_sock *icsk = inet_csk(sk); 4461 struct tcp_sock *tp = tcp_sk(sk); 4462 4463 if (optlen != sizeof(int)) 4464 return -EINVAL; 4465 4466 val = *((int *)optval); 4467 /* Only some options are supported */ 4468 switch (optname) { 4469 case TCP_BPF_IW: 4470 if (val <= 0 || tp->data_segs_out > tp->syn_data) 4471 ret = -EINVAL; 4472 else 4473 tp->snd_cwnd = val; 4474 break; 4475 case TCP_BPF_SNDCWND_CLAMP: 4476 if (val <= 0) { 4477 ret = -EINVAL; 4478 } else { 4479 tp->snd_cwnd_clamp = val; 4480 tp->snd_ssthresh = val; 4481 } 4482 break; 4483 case TCP_SAVE_SYN: 4484 if (val < 0 || val > 1) 4485 ret = -EINVAL; 4486 else 4487 tp->save_syn = val; 4488 break; 4489 case TCP_KEEPIDLE: 4490 ret = tcp_sock_set_keepidle_locked(sk, val); 4491 break; 4492 case TCP_KEEPINTVL: 4493 if (val < 1 || val > MAX_TCP_KEEPINTVL) 4494 ret = -EINVAL; 4495 else 4496 tp->keepalive_intvl = val * HZ; 4497 break; 4498 case TCP_KEEPCNT: 4499 if (val < 1 || val > MAX_TCP_KEEPCNT) 4500 ret = -EINVAL; 4501 else 4502 tp->keepalive_probes = val; 4503 break; 4504 case TCP_SYNCNT: 4505 if (val < 1 || val > MAX_TCP_SYNCNT) 4506 ret = -EINVAL; 4507 else 4508 icsk->icsk_syn_retries = val; 4509 break; 4510 case TCP_USER_TIMEOUT: 4511 if (val < 0) 4512 ret = -EINVAL; 4513 else 4514 icsk->icsk_user_timeout = val; 4515 break; 4516 default: 4517 ret = -EINVAL; 4518 } 4519 } 4520 #endif 4521 } else { 4522 ret = -EINVAL; 4523 } 4524 return ret; 4525 } 4526 4527 static int _bpf_getsockopt(struct sock *sk, int level, int optname, 4528 char *optval, int optlen) 4529 { 4530 if (!sk_fullsock(sk)) 4531 goto err_clear; 4532 4533 sock_owned_by_me(sk); 4534 4535 #ifdef CONFIG_INET 4536 if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) { 4537 struct inet_connection_sock *icsk; 4538 struct tcp_sock *tp; 4539 4540 switch (optname) { 4541 case TCP_CONGESTION: 4542 icsk = inet_csk(sk); 4543 4544 if (!icsk->icsk_ca_ops || optlen <= 1) 4545 goto err_clear; 4546 strncpy(optval, icsk->icsk_ca_ops->name, optlen); 4547 optval[optlen - 1] = 0; 4548 break; 4549 case TCP_SAVED_SYN: 4550 tp = tcp_sk(sk); 4551 4552 if (optlen <= 0 || !tp->saved_syn || 4553 optlen > tp->saved_syn[0]) 4554 goto err_clear; 4555 memcpy(optval, tp->saved_syn + 1, optlen); 4556 break; 4557 default: 4558 goto err_clear; 4559 } 4560 } else if (level == SOL_IP) { 4561 struct inet_sock *inet = inet_sk(sk); 4562 4563 if (optlen != sizeof(int) || sk->sk_family != AF_INET) 4564 goto err_clear; 4565 4566 /* Only some options are supported */ 4567 switch (optname) { 4568 case IP_TOS: 4569 *((int *)optval) = (int)inet->tos; 4570 break; 4571 default: 4572 goto err_clear; 4573 } 4574 #if IS_ENABLED(CONFIG_IPV6) 4575 } else if (level == SOL_IPV6) { 4576 struct ipv6_pinfo *np = inet6_sk(sk); 4577 4578 if (optlen != sizeof(int) || sk->sk_family != AF_INET6) 4579 goto err_clear; 4580 4581 /* Only some options are supported */ 4582 switch (optname) { 4583 case IPV6_TCLASS: 4584 *((int *)optval) = (int)np->tclass; 4585 break; 4586 default: 4587 goto err_clear; 4588 } 4589 #endif 4590 } else { 4591 goto err_clear; 4592 } 4593 return 0; 4594 #endif 4595 err_clear: 4596 memset(optval, 0, optlen); 4597 return -EINVAL; 4598 } 4599 4600 BPF_CALL_5(bpf_sock_addr_setsockopt, struct bpf_sock_addr_kern *, ctx, 4601 int, level, int, optname, char *, optval, int, optlen) 4602 { 4603 u32 flags = 0; 4604 return _bpf_setsockopt(ctx->sk, level, optname, optval, optlen, 4605 flags); 4606 } 4607 4608 static const struct bpf_func_proto bpf_sock_addr_setsockopt_proto = { 4609 .func = bpf_sock_addr_setsockopt, 4610 .gpl_only = false, 4611 .ret_type = RET_INTEGER, 4612 .arg1_type = ARG_PTR_TO_CTX, 4613 .arg2_type = ARG_ANYTHING, 4614 .arg3_type = ARG_ANYTHING, 4615 .arg4_type = ARG_PTR_TO_MEM, 4616 .arg5_type = ARG_CONST_SIZE, 4617 }; 4618 4619 BPF_CALL_5(bpf_sock_addr_getsockopt, struct bpf_sock_addr_kern *, ctx, 4620 int, level, int, optname, char *, optval, int, optlen) 4621 { 4622 return _bpf_getsockopt(ctx->sk, level, optname, optval, optlen); 4623 } 4624 4625 static const struct bpf_func_proto bpf_sock_addr_getsockopt_proto = { 4626 .func = bpf_sock_addr_getsockopt, 4627 .gpl_only = false, 4628 .ret_type = RET_INTEGER, 4629 .arg1_type = ARG_PTR_TO_CTX, 4630 .arg2_type = ARG_ANYTHING, 4631 .arg3_type = ARG_ANYTHING, 4632 .arg4_type = ARG_PTR_TO_UNINIT_MEM, 4633 .arg5_type = ARG_CONST_SIZE, 4634 }; 4635 4636 BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock, 4637 int, level, int, optname, char *, optval, int, optlen) 4638 { 4639 u32 flags = 0; 4640 if (bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN) 4641 flags |= SOCKOPT_CC_REINIT; 4642 return _bpf_setsockopt(bpf_sock->sk, level, optname, optval, optlen, 4643 flags); 4644 } 4645 4646 static const struct bpf_func_proto bpf_sock_ops_setsockopt_proto = { 4647 .func = bpf_sock_ops_setsockopt, 4648 .gpl_only = false, 4649 .ret_type = RET_INTEGER, 4650 .arg1_type = ARG_PTR_TO_CTX, 4651 .arg2_type = ARG_ANYTHING, 4652 .arg3_type = ARG_ANYTHING, 4653 .arg4_type = ARG_PTR_TO_MEM, 4654 .arg5_type = ARG_CONST_SIZE, 4655 }; 4656 4657 BPF_CALL_5(bpf_sock_ops_getsockopt, struct bpf_sock_ops_kern *, bpf_sock, 4658 int, level, int, optname, char *, optval, int, optlen) 4659 { 4660 return _bpf_getsockopt(bpf_sock->sk, level, optname, optval, optlen); 4661 } 4662 4663 static const struct bpf_func_proto bpf_sock_ops_getsockopt_proto = { 4664 .func = bpf_sock_ops_getsockopt, 4665 .gpl_only = false, 4666 .ret_type = RET_INTEGER, 4667 .arg1_type = ARG_PTR_TO_CTX, 4668 .arg2_type = ARG_ANYTHING, 4669 .arg3_type = ARG_ANYTHING, 4670 .arg4_type = ARG_PTR_TO_UNINIT_MEM, 4671 .arg5_type = ARG_CONST_SIZE, 4672 }; 4673 4674 BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock, 4675 int, argval) 4676 { 4677 struct sock *sk = bpf_sock->sk; 4678 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS; 4679 4680 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk)) 4681 return -EINVAL; 4682 4683 tcp_sk(sk)->bpf_sock_ops_cb_flags = val; 4684 4685 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS); 4686 } 4687 4688 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = { 4689 .func = bpf_sock_ops_cb_flags_set, 4690 .gpl_only = false, 4691 .ret_type = RET_INTEGER, 4692 .arg1_type = ARG_PTR_TO_CTX, 4693 .arg2_type = ARG_ANYTHING, 4694 }; 4695 4696 const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly; 4697 EXPORT_SYMBOL_GPL(ipv6_bpf_stub); 4698 4699 BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr, 4700 int, addr_len) 4701 { 4702 #ifdef CONFIG_INET 4703 struct sock *sk = ctx->sk; 4704 u32 flags = BIND_FROM_BPF; 4705 int err; 4706 4707 err = -EINVAL; 4708 if (addr_len < offsetofend(struct sockaddr, sa_family)) 4709 return err; 4710 if (addr->sa_family == AF_INET) { 4711 if (addr_len < sizeof(struct sockaddr_in)) 4712 return err; 4713 if (((struct sockaddr_in *)addr)->sin_port == htons(0)) 4714 flags |= BIND_FORCE_ADDRESS_NO_PORT; 4715 return __inet_bind(sk, addr, addr_len, flags); 4716 #if IS_ENABLED(CONFIG_IPV6) 4717 } else if (addr->sa_family == AF_INET6) { 4718 if (addr_len < SIN6_LEN_RFC2133) 4719 return err; 4720 if (((struct sockaddr_in6 *)addr)->sin6_port == htons(0)) 4721 flags |= BIND_FORCE_ADDRESS_NO_PORT; 4722 /* ipv6_bpf_stub cannot be NULL, since it's called from 4723 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded 4724 */ 4725 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, flags); 4726 #endif /* CONFIG_IPV6 */ 4727 } 4728 #endif /* CONFIG_INET */ 4729 4730 return -EAFNOSUPPORT; 4731 } 4732 4733 static const struct bpf_func_proto bpf_bind_proto = { 4734 .func = bpf_bind, 4735 .gpl_only = false, 4736 .ret_type = RET_INTEGER, 4737 .arg1_type = ARG_PTR_TO_CTX, 4738 .arg2_type = ARG_PTR_TO_MEM, 4739 .arg3_type = ARG_CONST_SIZE, 4740 }; 4741 4742 #ifdef CONFIG_XFRM 4743 BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index, 4744 struct bpf_xfrm_state *, to, u32, size, u64, flags) 4745 { 4746 const struct sec_path *sp = skb_sec_path(skb); 4747 const struct xfrm_state *x; 4748 4749 if (!sp || unlikely(index >= sp->len || flags)) 4750 goto err_clear; 4751 4752 x = sp->xvec[index]; 4753 4754 if (unlikely(size != sizeof(struct bpf_xfrm_state))) 4755 goto err_clear; 4756 4757 to->reqid = x->props.reqid; 4758 to->spi = x->id.spi; 4759 to->family = x->props.family; 4760 to->ext = 0; 4761 4762 if (to->family == AF_INET6) { 4763 memcpy(to->remote_ipv6, x->props.saddr.a6, 4764 sizeof(to->remote_ipv6)); 4765 } else { 4766 to->remote_ipv4 = x->props.saddr.a4; 4767 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3); 4768 } 4769 4770 return 0; 4771 err_clear: 4772 memset(to, 0, size); 4773 return -EINVAL; 4774 } 4775 4776 static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = { 4777 .func = bpf_skb_get_xfrm_state, 4778 .gpl_only = false, 4779 .ret_type = RET_INTEGER, 4780 .arg1_type = ARG_PTR_TO_CTX, 4781 .arg2_type = ARG_ANYTHING, 4782 .arg3_type = ARG_PTR_TO_UNINIT_MEM, 4783 .arg4_type = ARG_CONST_SIZE, 4784 .arg5_type = ARG_ANYTHING, 4785 }; 4786 #endif 4787 4788 #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6) 4789 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params, 4790 const struct neighbour *neigh, 4791 const struct net_device *dev) 4792 { 4793 memcpy(params->dmac, neigh->ha, ETH_ALEN); 4794 memcpy(params->smac, dev->dev_addr, ETH_ALEN); 4795 params->h_vlan_TCI = 0; 4796 params->h_vlan_proto = 0; 4797 params->ifindex = dev->ifindex; 4798 4799 return 0; 4800 } 4801 #endif 4802 4803 #if IS_ENABLED(CONFIG_INET) 4804 static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params, 4805 u32 flags, bool check_mtu) 4806 { 4807 struct fib_nh_common *nhc; 4808 struct in_device *in_dev; 4809 struct neighbour *neigh; 4810 struct net_device *dev; 4811 struct fib_result res; 4812 struct flowi4 fl4; 4813 int err; 4814 u32 mtu; 4815 4816 dev = dev_get_by_index_rcu(net, params->ifindex); 4817 if (unlikely(!dev)) 4818 return -ENODEV; 4819 4820 /* verify forwarding is enabled on this interface */ 4821 in_dev = __in_dev_get_rcu(dev); 4822 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev))) 4823 return BPF_FIB_LKUP_RET_FWD_DISABLED; 4824 4825 if (flags & BPF_FIB_LOOKUP_OUTPUT) { 4826 fl4.flowi4_iif = 1; 4827 fl4.flowi4_oif = params->ifindex; 4828 } else { 4829 fl4.flowi4_iif = params->ifindex; 4830 fl4.flowi4_oif = 0; 4831 } 4832 fl4.flowi4_tos = params->tos & IPTOS_RT_MASK; 4833 fl4.flowi4_scope = RT_SCOPE_UNIVERSE; 4834 fl4.flowi4_flags = 0; 4835 4836 fl4.flowi4_proto = params->l4_protocol; 4837 fl4.daddr = params->ipv4_dst; 4838 fl4.saddr = params->ipv4_src; 4839 fl4.fl4_sport = params->sport; 4840 fl4.fl4_dport = params->dport; 4841 4842 if (flags & BPF_FIB_LOOKUP_DIRECT) { 4843 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN; 4844 struct fib_table *tb; 4845 4846 tb = fib_get_table(net, tbid); 4847 if (unlikely(!tb)) 4848 return BPF_FIB_LKUP_RET_NOT_FWDED; 4849 4850 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF); 4851 } else { 4852 fl4.flowi4_mark = 0; 4853 fl4.flowi4_secid = 0; 4854 fl4.flowi4_tun_key.tun_id = 0; 4855 fl4.flowi4_uid = sock_net_uid(net, NULL); 4856 4857 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF); 4858 } 4859 4860 if (err) { 4861 /* map fib lookup errors to RTN_ type */ 4862 if (err == -EINVAL) 4863 return BPF_FIB_LKUP_RET_BLACKHOLE; 4864 if (err == -EHOSTUNREACH) 4865 return BPF_FIB_LKUP_RET_UNREACHABLE; 4866 if (err == -EACCES) 4867 return BPF_FIB_LKUP_RET_PROHIBIT; 4868 4869 return BPF_FIB_LKUP_RET_NOT_FWDED; 4870 } 4871 4872 if (res.type != RTN_UNICAST) 4873 return BPF_FIB_LKUP_RET_NOT_FWDED; 4874 4875 if (fib_info_num_path(res.fi) > 1) 4876 fib_select_path(net, &res, &fl4, NULL); 4877 4878 if (check_mtu) { 4879 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst); 4880 if (params->tot_len > mtu) 4881 return BPF_FIB_LKUP_RET_FRAG_NEEDED; 4882 } 4883 4884 nhc = res.nhc; 4885 4886 /* do not handle lwt encaps right now */ 4887 if (nhc->nhc_lwtstate) 4888 return BPF_FIB_LKUP_RET_UNSUPP_LWT; 4889 4890 dev = nhc->nhc_dev; 4891 4892 params->rt_metric = res.fi->fib_priority; 4893 4894 /* xdp and cls_bpf programs are run in RCU-bh so 4895 * rcu_read_lock_bh is not needed here 4896 */ 4897 if (likely(nhc->nhc_gw_family != AF_INET6)) { 4898 if (nhc->nhc_gw_family) 4899 params->ipv4_dst = nhc->nhc_gw.ipv4; 4900 4901 neigh = __ipv4_neigh_lookup_noref(dev, 4902 (__force u32)params->ipv4_dst); 4903 } else { 4904 struct in6_addr *dst = (struct in6_addr *)params->ipv6_dst; 4905 4906 params->family = AF_INET6; 4907 *dst = nhc->nhc_gw.ipv6; 4908 neigh = __ipv6_neigh_lookup_noref_stub(dev, dst); 4909 } 4910 4911 if (!neigh) 4912 return BPF_FIB_LKUP_RET_NO_NEIGH; 4913 4914 return bpf_fib_set_fwd_params(params, neigh, dev); 4915 } 4916 #endif 4917 4918 #if IS_ENABLED(CONFIG_IPV6) 4919 static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params, 4920 u32 flags, bool check_mtu) 4921 { 4922 struct in6_addr *src = (struct in6_addr *) params->ipv6_src; 4923 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst; 4924 struct fib6_result res = {}; 4925 struct neighbour *neigh; 4926 struct net_device *dev; 4927 struct inet6_dev *idev; 4928 struct flowi6 fl6; 4929 int strict = 0; 4930 int oif, err; 4931 u32 mtu; 4932 4933 /* link local addresses are never forwarded */ 4934 if (rt6_need_strict(dst) || rt6_need_strict(src)) 4935 return BPF_FIB_LKUP_RET_NOT_FWDED; 4936 4937 dev = dev_get_by_index_rcu(net, params->ifindex); 4938 if (unlikely(!dev)) 4939 return -ENODEV; 4940 4941 idev = __in6_dev_get_safely(dev); 4942 if (unlikely(!idev || !idev->cnf.forwarding)) 4943 return BPF_FIB_LKUP_RET_FWD_DISABLED; 4944 4945 if (flags & BPF_FIB_LOOKUP_OUTPUT) { 4946 fl6.flowi6_iif = 1; 4947 oif = fl6.flowi6_oif = params->ifindex; 4948 } else { 4949 oif = fl6.flowi6_iif = params->ifindex; 4950 fl6.flowi6_oif = 0; 4951 strict = RT6_LOOKUP_F_HAS_SADDR; 4952 } 4953 fl6.flowlabel = params->flowinfo; 4954 fl6.flowi6_scope = 0; 4955 fl6.flowi6_flags = 0; 4956 fl6.mp_hash = 0; 4957 4958 fl6.flowi6_proto = params->l4_protocol; 4959 fl6.daddr = *dst; 4960 fl6.saddr = *src; 4961 fl6.fl6_sport = params->sport; 4962 fl6.fl6_dport = params->dport; 4963 4964 if (flags & BPF_FIB_LOOKUP_DIRECT) { 4965 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN; 4966 struct fib6_table *tb; 4967 4968 tb = ipv6_stub->fib6_get_table(net, tbid); 4969 if (unlikely(!tb)) 4970 return BPF_FIB_LKUP_RET_NOT_FWDED; 4971 4972 err = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, &res, 4973 strict); 4974 } else { 4975 fl6.flowi6_mark = 0; 4976 fl6.flowi6_secid = 0; 4977 fl6.flowi6_tun_key.tun_id = 0; 4978 fl6.flowi6_uid = sock_net_uid(net, NULL); 4979 4980 err = ipv6_stub->fib6_lookup(net, oif, &fl6, &res, strict); 4981 } 4982 4983 if (unlikely(err || IS_ERR_OR_NULL(res.f6i) || 4984 res.f6i == net->ipv6.fib6_null_entry)) 4985 return BPF_FIB_LKUP_RET_NOT_FWDED; 4986 4987 switch (res.fib6_type) { 4988 /* only unicast is forwarded */ 4989 case RTN_UNICAST: 4990 break; 4991 case RTN_BLACKHOLE: 4992 return BPF_FIB_LKUP_RET_BLACKHOLE; 4993 case RTN_UNREACHABLE: 4994 return BPF_FIB_LKUP_RET_UNREACHABLE; 4995 case RTN_PROHIBIT: 4996 return BPF_FIB_LKUP_RET_PROHIBIT; 4997 default: 4998 return BPF_FIB_LKUP_RET_NOT_FWDED; 4999 } 5000 5001 ipv6_stub->fib6_select_path(net, &res, &fl6, fl6.flowi6_oif, 5002 fl6.flowi6_oif != 0, NULL, strict); 5003 5004 if (check_mtu) { 5005 mtu = ipv6_stub->ip6_mtu_from_fib6(&res, dst, src); 5006 if (params->tot_len > mtu) 5007 return BPF_FIB_LKUP_RET_FRAG_NEEDED; 5008 } 5009 5010 if (res.nh->fib_nh_lws) 5011 return BPF_FIB_LKUP_RET_UNSUPP_LWT; 5012 5013 if (res.nh->fib_nh_gw_family) 5014 *dst = res.nh->fib_nh_gw6; 5015 5016 dev = res.nh->fib_nh_dev; 5017 params->rt_metric = res.f6i->fib6_metric; 5018 5019 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is 5020 * not needed here. 5021 */ 5022 neigh = __ipv6_neigh_lookup_noref_stub(dev, dst); 5023 if (!neigh) 5024 return BPF_FIB_LKUP_RET_NO_NEIGH; 5025 5026 return bpf_fib_set_fwd_params(params, neigh, dev); 5027 } 5028 #endif 5029 5030 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx, 5031 struct bpf_fib_lookup *, params, int, plen, u32, flags) 5032 { 5033 if (plen < sizeof(*params)) 5034 return -EINVAL; 5035 5036 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT)) 5037 return -EINVAL; 5038 5039 switch (params->family) { 5040 #if IS_ENABLED(CONFIG_INET) 5041 case AF_INET: 5042 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params, 5043 flags, true); 5044 #endif 5045 #if IS_ENABLED(CONFIG_IPV6) 5046 case AF_INET6: 5047 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params, 5048 flags, true); 5049 #endif 5050 } 5051 return -EAFNOSUPPORT; 5052 } 5053 5054 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = { 5055 .func = bpf_xdp_fib_lookup, 5056 .gpl_only = true, 5057 .ret_type = RET_INTEGER, 5058 .arg1_type = ARG_PTR_TO_CTX, 5059 .arg2_type = ARG_PTR_TO_MEM, 5060 .arg3_type = ARG_CONST_SIZE, 5061 .arg4_type = ARG_ANYTHING, 5062 }; 5063 5064 BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb, 5065 struct bpf_fib_lookup *, params, int, plen, u32, flags) 5066 { 5067 struct net *net = dev_net(skb->dev); 5068 int rc = -EAFNOSUPPORT; 5069 5070 if (plen < sizeof(*params)) 5071 return -EINVAL; 5072 5073 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT)) 5074 return -EINVAL; 5075 5076 switch (params->family) { 5077 #if IS_ENABLED(CONFIG_INET) 5078 case AF_INET: 5079 rc = bpf_ipv4_fib_lookup(net, params, flags, false); 5080 break; 5081 #endif 5082 #if IS_ENABLED(CONFIG_IPV6) 5083 case AF_INET6: 5084 rc = bpf_ipv6_fib_lookup(net, params, flags, false); 5085 break; 5086 #endif 5087 } 5088 5089 if (!rc) { 5090 struct net_device *dev; 5091 5092 dev = dev_get_by_index_rcu(net, params->ifindex); 5093 if (!is_skb_forwardable(dev, skb)) 5094 rc = BPF_FIB_LKUP_RET_FRAG_NEEDED; 5095 } 5096 5097 return rc; 5098 } 5099 5100 static const struct bpf_func_proto bpf_skb_fib_lookup_proto = { 5101 .func = bpf_skb_fib_lookup, 5102 .gpl_only = true, 5103 .ret_type = RET_INTEGER, 5104 .arg1_type = ARG_PTR_TO_CTX, 5105 .arg2_type = ARG_PTR_TO_MEM, 5106 .arg3_type = ARG_CONST_SIZE, 5107 .arg4_type = ARG_ANYTHING, 5108 }; 5109 5110 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF) 5111 static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len) 5112 { 5113 int err; 5114 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr; 5115 5116 if (!seg6_validate_srh(srh, len, false)) 5117 return -EINVAL; 5118 5119 switch (type) { 5120 case BPF_LWT_ENCAP_SEG6_INLINE: 5121 if (skb->protocol != htons(ETH_P_IPV6)) 5122 return -EBADMSG; 5123 5124 err = seg6_do_srh_inline(skb, srh); 5125 break; 5126 case BPF_LWT_ENCAP_SEG6: 5127 skb_reset_inner_headers(skb); 5128 skb->encapsulation = 1; 5129 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6); 5130 break; 5131 default: 5132 return -EINVAL; 5133 } 5134 5135 bpf_compute_data_pointers(skb); 5136 if (err) 5137 return err; 5138 5139 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); 5140 skb_set_transport_header(skb, sizeof(struct ipv6hdr)); 5141 5142 return seg6_lookup_nexthop(skb, NULL, 0); 5143 } 5144 #endif /* CONFIG_IPV6_SEG6_BPF */ 5145 5146 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF) 5147 static int bpf_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len, 5148 bool ingress) 5149 { 5150 return bpf_lwt_push_ip_encap(skb, hdr, len, ingress); 5151 } 5152 #endif 5153 5154 BPF_CALL_4(bpf_lwt_in_push_encap, struct sk_buff *, skb, u32, type, void *, hdr, 5155 u32, len) 5156 { 5157 switch (type) { 5158 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF) 5159 case BPF_LWT_ENCAP_SEG6: 5160 case BPF_LWT_ENCAP_SEG6_INLINE: 5161 return bpf_push_seg6_encap(skb, type, hdr, len); 5162 #endif 5163 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF) 5164 case BPF_LWT_ENCAP_IP: 5165 return bpf_push_ip_encap(skb, hdr, len, true /* ingress */); 5166 #endif 5167 default: 5168 return -EINVAL; 5169 } 5170 } 5171 5172 BPF_CALL_4(bpf_lwt_xmit_push_encap, struct sk_buff *, skb, u32, type, 5173 void *, hdr, u32, len) 5174 { 5175 switch (type) { 5176 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF) 5177 case BPF_LWT_ENCAP_IP: 5178 return bpf_push_ip_encap(skb, hdr, len, false /* egress */); 5179 #endif 5180 default: 5181 return -EINVAL; 5182 } 5183 } 5184 5185 static const struct bpf_func_proto bpf_lwt_in_push_encap_proto = { 5186 .func = bpf_lwt_in_push_encap, 5187 .gpl_only = false, 5188 .ret_type = RET_INTEGER, 5189 .arg1_type = ARG_PTR_TO_CTX, 5190 .arg2_type = ARG_ANYTHING, 5191 .arg3_type = ARG_PTR_TO_MEM, 5192 .arg4_type = ARG_CONST_SIZE 5193 }; 5194 5195 static const struct bpf_func_proto bpf_lwt_xmit_push_encap_proto = { 5196 .func = bpf_lwt_xmit_push_encap, 5197 .gpl_only = false, 5198 .ret_type = RET_INTEGER, 5199 .arg1_type = ARG_PTR_TO_CTX, 5200 .arg2_type = ARG_ANYTHING, 5201 .arg3_type = ARG_PTR_TO_MEM, 5202 .arg4_type = ARG_CONST_SIZE 5203 }; 5204 5205 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF) 5206 BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset, 5207 const void *, from, u32, len) 5208 { 5209 struct seg6_bpf_srh_state *srh_state = 5210 this_cpu_ptr(&seg6_bpf_srh_states); 5211 struct ipv6_sr_hdr *srh = srh_state->srh; 5212 void *srh_tlvs, *srh_end, *ptr; 5213 int srhoff = 0; 5214 5215 if (srh == NULL) 5216 return -EINVAL; 5217 5218 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4)); 5219 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen); 5220 5221 ptr = skb->data + offset; 5222 if (ptr >= srh_tlvs && ptr + len <= srh_end) 5223 srh_state->valid = false; 5224 else if (ptr < (void *)&srh->flags || 5225 ptr + len > (void *)&srh->segments) 5226 return -EFAULT; 5227 5228 if (unlikely(bpf_try_make_writable(skb, offset + len))) 5229 return -EFAULT; 5230 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) 5231 return -EINVAL; 5232 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); 5233 5234 memcpy(skb->data + offset, from, len); 5235 return 0; 5236 } 5237 5238 static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = { 5239 .func = bpf_lwt_seg6_store_bytes, 5240 .gpl_only = false, 5241 .ret_type = RET_INTEGER, 5242 .arg1_type = ARG_PTR_TO_CTX, 5243 .arg2_type = ARG_ANYTHING, 5244 .arg3_type = ARG_PTR_TO_MEM, 5245 .arg4_type = ARG_CONST_SIZE 5246 }; 5247 5248 static void bpf_update_srh_state(struct sk_buff *skb) 5249 { 5250 struct seg6_bpf_srh_state *srh_state = 5251 this_cpu_ptr(&seg6_bpf_srh_states); 5252 int srhoff = 0; 5253 5254 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) { 5255 srh_state->srh = NULL; 5256 } else { 5257 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); 5258 srh_state->hdrlen = srh_state->srh->hdrlen << 3; 5259 srh_state->valid = true; 5260 } 5261 } 5262 5263 BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb, 5264 u32, action, void *, param, u32, param_len) 5265 { 5266 struct seg6_bpf_srh_state *srh_state = 5267 this_cpu_ptr(&seg6_bpf_srh_states); 5268 int hdroff = 0; 5269 int err; 5270 5271 switch (action) { 5272 case SEG6_LOCAL_ACTION_END_X: 5273 if (!seg6_bpf_has_valid_srh(skb)) 5274 return -EBADMSG; 5275 if (param_len != sizeof(struct in6_addr)) 5276 return -EINVAL; 5277 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0); 5278 case SEG6_LOCAL_ACTION_END_T: 5279 if (!seg6_bpf_has_valid_srh(skb)) 5280 return -EBADMSG; 5281 if (param_len != sizeof(int)) 5282 return -EINVAL; 5283 return seg6_lookup_nexthop(skb, NULL, *(int *)param); 5284 case SEG6_LOCAL_ACTION_END_DT6: 5285 if (!seg6_bpf_has_valid_srh(skb)) 5286 return -EBADMSG; 5287 if (param_len != sizeof(int)) 5288 return -EINVAL; 5289 5290 if (ipv6_find_hdr(skb, &hdroff, IPPROTO_IPV6, NULL, NULL) < 0) 5291 return -EBADMSG; 5292 if (!pskb_pull(skb, hdroff)) 5293 return -EBADMSG; 5294 5295 skb_postpull_rcsum(skb, skb_network_header(skb), hdroff); 5296 skb_reset_network_header(skb); 5297 skb_reset_transport_header(skb); 5298 skb->encapsulation = 0; 5299 5300 bpf_compute_data_pointers(skb); 5301 bpf_update_srh_state(skb); 5302 return seg6_lookup_nexthop(skb, NULL, *(int *)param); 5303 case SEG6_LOCAL_ACTION_END_B6: 5304 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb)) 5305 return -EBADMSG; 5306 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE, 5307 param, param_len); 5308 if (!err) 5309 bpf_update_srh_state(skb); 5310 5311 return err; 5312 case SEG6_LOCAL_ACTION_END_B6_ENCAP: 5313 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb)) 5314 return -EBADMSG; 5315 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6, 5316 param, param_len); 5317 if (!err) 5318 bpf_update_srh_state(skb); 5319 5320 return err; 5321 default: 5322 return -EINVAL; 5323 } 5324 } 5325 5326 static const struct bpf_func_proto bpf_lwt_seg6_action_proto = { 5327 .func = bpf_lwt_seg6_action, 5328 .gpl_only = false, 5329 .ret_type = RET_INTEGER, 5330 .arg1_type = ARG_PTR_TO_CTX, 5331 .arg2_type = ARG_ANYTHING, 5332 .arg3_type = ARG_PTR_TO_MEM, 5333 .arg4_type = ARG_CONST_SIZE 5334 }; 5335 5336 BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset, 5337 s32, len) 5338 { 5339 struct seg6_bpf_srh_state *srh_state = 5340 this_cpu_ptr(&seg6_bpf_srh_states); 5341 struct ipv6_sr_hdr *srh = srh_state->srh; 5342 void *srh_end, *srh_tlvs, *ptr; 5343 struct ipv6hdr *hdr; 5344 int srhoff = 0; 5345 int ret; 5346 5347 if (unlikely(srh == NULL)) 5348 return -EINVAL; 5349 5350 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) + 5351 ((srh->first_segment + 1) << 4)); 5352 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) + 5353 srh_state->hdrlen); 5354 ptr = skb->data + offset; 5355 5356 if (unlikely(ptr < srh_tlvs || ptr > srh_end)) 5357 return -EFAULT; 5358 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end)) 5359 return -EFAULT; 5360 5361 if (len > 0) { 5362 ret = skb_cow_head(skb, len); 5363 if (unlikely(ret < 0)) 5364 return ret; 5365 5366 ret = bpf_skb_net_hdr_push(skb, offset, len); 5367 } else { 5368 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len); 5369 } 5370 5371 bpf_compute_data_pointers(skb); 5372 if (unlikely(ret < 0)) 5373 return ret; 5374 5375 hdr = (struct ipv6hdr *)skb->data; 5376 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); 5377 5378 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) 5379 return -EINVAL; 5380 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); 5381 srh_state->hdrlen += len; 5382 srh_state->valid = false; 5383 return 0; 5384 } 5385 5386 static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = { 5387 .func = bpf_lwt_seg6_adjust_srh, 5388 .gpl_only = false, 5389 .ret_type = RET_INTEGER, 5390 .arg1_type = ARG_PTR_TO_CTX, 5391 .arg2_type = ARG_ANYTHING, 5392 .arg3_type = ARG_ANYTHING, 5393 }; 5394 #endif /* CONFIG_IPV6_SEG6_BPF */ 5395 5396 #ifdef CONFIG_INET 5397 static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple, 5398 int dif, int sdif, u8 family, u8 proto) 5399 { 5400 bool refcounted = false; 5401 struct sock *sk = NULL; 5402 5403 if (family == AF_INET) { 5404 __be32 src4 = tuple->ipv4.saddr; 5405 __be32 dst4 = tuple->ipv4.daddr; 5406 5407 if (proto == IPPROTO_TCP) 5408 sk = __inet_lookup(net, &tcp_hashinfo, NULL, 0, 5409 src4, tuple->ipv4.sport, 5410 dst4, tuple->ipv4.dport, 5411 dif, sdif, &refcounted); 5412 else 5413 sk = __udp4_lib_lookup(net, src4, tuple->ipv4.sport, 5414 dst4, tuple->ipv4.dport, 5415 dif, sdif, &udp_table, NULL); 5416 #if IS_ENABLED(CONFIG_IPV6) 5417 } else { 5418 struct in6_addr *src6 = (struct in6_addr *)&tuple->ipv6.saddr; 5419 struct in6_addr *dst6 = (struct in6_addr *)&tuple->ipv6.daddr; 5420 5421 if (proto == IPPROTO_TCP) 5422 sk = __inet6_lookup(net, &tcp_hashinfo, NULL, 0, 5423 src6, tuple->ipv6.sport, 5424 dst6, ntohs(tuple->ipv6.dport), 5425 dif, sdif, &refcounted); 5426 else if (likely(ipv6_bpf_stub)) 5427 sk = ipv6_bpf_stub->udp6_lib_lookup(net, 5428 src6, tuple->ipv6.sport, 5429 dst6, tuple->ipv6.dport, 5430 dif, sdif, 5431 &udp_table, NULL); 5432 #endif 5433 } 5434 5435 if (unlikely(sk && !refcounted && !sock_flag(sk, SOCK_RCU_FREE))) { 5436 WARN_ONCE(1, "Found non-RCU, unreferenced socket!"); 5437 sk = NULL; 5438 } 5439 return sk; 5440 } 5441 5442 /* bpf_skc_lookup performs the core lookup for different types of sockets, 5443 * taking a reference on the socket if it doesn't have the flag SOCK_RCU_FREE. 5444 * Returns the socket as an 'unsigned long' to simplify the casting in the 5445 * callers to satisfy BPF_CALL declarations. 5446 */ 5447 static struct sock * 5448 __bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, 5449 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id, 5450 u64 flags) 5451 { 5452 struct sock *sk = NULL; 5453 u8 family = AF_UNSPEC; 5454 struct net *net; 5455 int sdif; 5456 5457 if (len == sizeof(tuple->ipv4)) 5458 family = AF_INET; 5459 else if (len == sizeof(tuple->ipv6)) 5460 family = AF_INET6; 5461 else 5462 return NULL; 5463 5464 if (unlikely(family == AF_UNSPEC || flags || 5465 !((s32)netns_id < 0 || netns_id <= S32_MAX))) 5466 goto out; 5467 5468 if (family == AF_INET) 5469 sdif = inet_sdif(skb); 5470 else 5471 sdif = inet6_sdif(skb); 5472 5473 if ((s32)netns_id < 0) { 5474 net = caller_net; 5475 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto); 5476 } else { 5477 net = get_net_ns_by_id(caller_net, netns_id); 5478 if (unlikely(!net)) 5479 goto out; 5480 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto); 5481 put_net(net); 5482 } 5483 5484 out: 5485 return sk; 5486 } 5487 5488 static struct sock * 5489 __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, 5490 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id, 5491 u64 flags) 5492 { 5493 struct sock *sk = __bpf_skc_lookup(skb, tuple, len, caller_net, 5494 ifindex, proto, netns_id, flags); 5495 5496 if (sk) { 5497 sk = sk_to_full_sk(sk); 5498 if (!sk_fullsock(sk)) { 5499 sock_gen_put(sk); 5500 return NULL; 5501 } 5502 } 5503 5504 return sk; 5505 } 5506 5507 static struct sock * 5508 bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, 5509 u8 proto, u64 netns_id, u64 flags) 5510 { 5511 struct net *caller_net; 5512 int ifindex; 5513 5514 if (skb->dev) { 5515 caller_net = dev_net(skb->dev); 5516 ifindex = skb->dev->ifindex; 5517 } else { 5518 caller_net = sock_net(skb->sk); 5519 ifindex = 0; 5520 } 5521 5522 return __bpf_skc_lookup(skb, tuple, len, caller_net, ifindex, proto, 5523 netns_id, flags); 5524 } 5525 5526 static struct sock * 5527 bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, 5528 u8 proto, u64 netns_id, u64 flags) 5529 { 5530 struct sock *sk = bpf_skc_lookup(skb, tuple, len, proto, netns_id, 5531 flags); 5532 5533 if (sk) { 5534 sk = sk_to_full_sk(sk); 5535 if (!sk_fullsock(sk)) { 5536 sock_gen_put(sk); 5537 return NULL; 5538 } 5539 } 5540 5541 return sk; 5542 } 5543 5544 BPF_CALL_5(bpf_skc_lookup_tcp, struct sk_buff *, skb, 5545 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) 5546 { 5547 return (unsigned long)bpf_skc_lookup(skb, tuple, len, IPPROTO_TCP, 5548 netns_id, flags); 5549 } 5550 5551 static const struct bpf_func_proto bpf_skc_lookup_tcp_proto = { 5552 .func = bpf_skc_lookup_tcp, 5553 .gpl_only = false, 5554 .pkt_access = true, 5555 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL, 5556 .arg1_type = ARG_PTR_TO_CTX, 5557 .arg2_type = ARG_PTR_TO_MEM, 5558 .arg3_type = ARG_CONST_SIZE, 5559 .arg4_type = ARG_ANYTHING, 5560 .arg5_type = ARG_ANYTHING, 5561 }; 5562 5563 BPF_CALL_5(bpf_sk_lookup_tcp, struct sk_buff *, skb, 5564 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) 5565 { 5566 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_TCP, 5567 netns_id, flags); 5568 } 5569 5570 static const struct bpf_func_proto bpf_sk_lookup_tcp_proto = { 5571 .func = bpf_sk_lookup_tcp, 5572 .gpl_only = false, 5573 .pkt_access = true, 5574 .ret_type = RET_PTR_TO_SOCKET_OR_NULL, 5575 .arg1_type = ARG_PTR_TO_CTX, 5576 .arg2_type = ARG_PTR_TO_MEM, 5577 .arg3_type = ARG_CONST_SIZE, 5578 .arg4_type = ARG_ANYTHING, 5579 .arg5_type = ARG_ANYTHING, 5580 }; 5581 5582 BPF_CALL_5(bpf_sk_lookup_udp, struct sk_buff *, skb, 5583 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) 5584 { 5585 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_UDP, 5586 netns_id, flags); 5587 } 5588 5589 static const struct bpf_func_proto bpf_sk_lookup_udp_proto = { 5590 .func = bpf_sk_lookup_udp, 5591 .gpl_only = false, 5592 .pkt_access = true, 5593 .ret_type = RET_PTR_TO_SOCKET_OR_NULL, 5594 .arg1_type = ARG_PTR_TO_CTX, 5595 .arg2_type = ARG_PTR_TO_MEM, 5596 .arg3_type = ARG_CONST_SIZE, 5597 .arg4_type = ARG_ANYTHING, 5598 .arg5_type = ARG_ANYTHING, 5599 }; 5600 5601 BPF_CALL_1(bpf_sk_release, struct sock *, sk) 5602 { 5603 if (sk_is_refcounted(sk)) 5604 sock_gen_put(sk); 5605 return 0; 5606 } 5607 5608 static const struct bpf_func_proto bpf_sk_release_proto = { 5609 .func = bpf_sk_release, 5610 .gpl_only = false, 5611 .ret_type = RET_INTEGER, 5612 .arg1_type = ARG_PTR_TO_SOCK_COMMON, 5613 }; 5614 5615 BPF_CALL_5(bpf_xdp_sk_lookup_udp, struct xdp_buff *, ctx, 5616 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags) 5617 { 5618 struct net *caller_net = dev_net(ctx->rxq->dev); 5619 int ifindex = ctx->rxq->dev->ifindex; 5620 5621 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net, 5622 ifindex, IPPROTO_UDP, netns_id, 5623 flags); 5624 } 5625 5626 static const struct bpf_func_proto bpf_xdp_sk_lookup_udp_proto = { 5627 .func = bpf_xdp_sk_lookup_udp, 5628 .gpl_only = false, 5629 .pkt_access = true, 5630 .ret_type = RET_PTR_TO_SOCKET_OR_NULL, 5631 .arg1_type = ARG_PTR_TO_CTX, 5632 .arg2_type = ARG_PTR_TO_MEM, 5633 .arg3_type = ARG_CONST_SIZE, 5634 .arg4_type = ARG_ANYTHING, 5635 .arg5_type = ARG_ANYTHING, 5636 }; 5637 5638 BPF_CALL_5(bpf_xdp_skc_lookup_tcp, struct xdp_buff *, ctx, 5639 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags) 5640 { 5641 struct net *caller_net = dev_net(ctx->rxq->dev); 5642 int ifindex = ctx->rxq->dev->ifindex; 5643 5644 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len, caller_net, 5645 ifindex, IPPROTO_TCP, netns_id, 5646 flags); 5647 } 5648 5649 static const struct bpf_func_proto bpf_xdp_skc_lookup_tcp_proto = { 5650 .func = bpf_xdp_skc_lookup_tcp, 5651 .gpl_only = false, 5652 .pkt_access = true, 5653 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL, 5654 .arg1_type = ARG_PTR_TO_CTX, 5655 .arg2_type = ARG_PTR_TO_MEM, 5656 .arg3_type = ARG_CONST_SIZE, 5657 .arg4_type = ARG_ANYTHING, 5658 .arg5_type = ARG_ANYTHING, 5659 }; 5660 5661 BPF_CALL_5(bpf_xdp_sk_lookup_tcp, struct xdp_buff *, ctx, 5662 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags) 5663 { 5664 struct net *caller_net = dev_net(ctx->rxq->dev); 5665 int ifindex = ctx->rxq->dev->ifindex; 5666 5667 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net, 5668 ifindex, IPPROTO_TCP, netns_id, 5669 flags); 5670 } 5671 5672 static const struct bpf_func_proto bpf_xdp_sk_lookup_tcp_proto = { 5673 .func = bpf_xdp_sk_lookup_tcp, 5674 .gpl_only = false, 5675 .pkt_access = true, 5676 .ret_type = RET_PTR_TO_SOCKET_OR_NULL, 5677 .arg1_type = ARG_PTR_TO_CTX, 5678 .arg2_type = ARG_PTR_TO_MEM, 5679 .arg3_type = ARG_CONST_SIZE, 5680 .arg4_type = ARG_ANYTHING, 5681 .arg5_type = ARG_ANYTHING, 5682 }; 5683 5684 BPF_CALL_5(bpf_sock_addr_skc_lookup_tcp, struct bpf_sock_addr_kern *, ctx, 5685 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) 5686 { 5687 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len, 5688 sock_net(ctx->sk), 0, 5689 IPPROTO_TCP, netns_id, flags); 5690 } 5691 5692 static const struct bpf_func_proto bpf_sock_addr_skc_lookup_tcp_proto = { 5693 .func = bpf_sock_addr_skc_lookup_tcp, 5694 .gpl_only = false, 5695 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL, 5696 .arg1_type = ARG_PTR_TO_CTX, 5697 .arg2_type = ARG_PTR_TO_MEM, 5698 .arg3_type = ARG_CONST_SIZE, 5699 .arg4_type = ARG_ANYTHING, 5700 .arg5_type = ARG_ANYTHING, 5701 }; 5702 5703 BPF_CALL_5(bpf_sock_addr_sk_lookup_tcp, struct bpf_sock_addr_kern *, ctx, 5704 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) 5705 { 5706 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, 5707 sock_net(ctx->sk), 0, IPPROTO_TCP, 5708 netns_id, flags); 5709 } 5710 5711 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_tcp_proto = { 5712 .func = bpf_sock_addr_sk_lookup_tcp, 5713 .gpl_only = false, 5714 .ret_type = RET_PTR_TO_SOCKET_OR_NULL, 5715 .arg1_type = ARG_PTR_TO_CTX, 5716 .arg2_type = ARG_PTR_TO_MEM, 5717 .arg3_type = ARG_CONST_SIZE, 5718 .arg4_type = ARG_ANYTHING, 5719 .arg5_type = ARG_ANYTHING, 5720 }; 5721 5722 BPF_CALL_5(bpf_sock_addr_sk_lookup_udp, struct bpf_sock_addr_kern *, ctx, 5723 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) 5724 { 5725 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, 5726 sock_net(ctx->sk), 0, IPPROTO_UDP, 5727 netns_id, flags); 5728 } 5729 5730 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_udp_proto = { 5731 .func = bpf_sock_addr_sk_lookup_udp, 5732 .gpl_only = false, 5733 .ret_type = RET_PTR_TO_SOCKET_OR_NULL, 5734 .arg1_type = ARG_PTR_TO_CTX, 5735 .arg2_type = ARG_PTR_TO_MEM, 5736 .arg3_type = ARG_CONST_SIZE, 5737 .arg4_type = ARG_ANYTHING, 5738 .arg5_type = ARG_ANYTHING, 5739 }; 5740 5741 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type, 5742 struct bpf_insn_access_aux *info) 5743 { 5744 if (off < 0 || off >= offsetofend(struct bpf_tcp_sock, 5745 icsk_retransmits)) 5746 return false; 5747 5748 if (off % size != 0) 5749 return false; 5750 5751 switch (off) { 5752 case offsetof(struct bpf_tcp_sock, bytes_received): 5753 case offsetof(struct bpf_tcp_sock, bytes_acked): 5754 return size == sizeof(__u64); 5755 default: 5756 return size == sizeof(__u32); 5757 } 5758 } 5759 5760 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type, 5761 const struct bpf_insn *si, 5762 struct bpf_insn *insn_buf, 5763 struct bpf_prog *prog, u32 *target_size) 5764 { 5765 struct bpf_insn *insn = insn_buf; 5766 5767 #define BPF_TCP_SOCK_GET_COMMON(FIELD) \ 5768 do { \ 5769 BUILD_BUG_ON(sizeof_field(struct tcp_sock, FIELD) > \ 5770 sizeof_field(struct bpf_tcp_sock, FIELD)); \ 5771 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_sock, FIELD),\ 5772 si->dst_reg, si->src_reg, \ 5773 offsetof(struct tcp_sock, FIELD)); \ 5774 } while (0) 5775 5776 #define BPF_INET_SOCK_GET_COMMON(FIELD) \ 5777 do { \ 5778 BUILD_BUG_ON(sizeof_field(struct inet_connection_sock, \ 5779 FIELD) > \ 5780 sizeof_field(struct bpf_tcp_sock, FIELD)); \ 5781 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ 5782 struct inet_connection_sock, \ 5783 FIELD), \ 5784 si->dst_reg, si->src_reg, \ 5785 offsetof( \ 5786 struct inet_connection_sock, \ 5787 FIELD)); \ 5788 } while (0) 5789 5790 if (insn > insn_buf) 5791 return insn - insn_buf; 5792 5793 switch (si->off) { 5794 case offsetof(struct bpf_tcp_sock, rtt_min): 5795 BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) != 5796 sizeof(struct minmax)); 5797 BUILD_BUG_ON(sizeof(struct minmax) < 5798 sizeof(struct minmax_sample)); 5799 5800 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 5801 offsetof(struct tcp_sock, rtt_min) + 5802 offsetof(struct minmax_sample, v)); 5803 break; 5804 case offsetof(struct bpf_tcp_sock, snd_cwnd): 5805 BPF_TCP_SOCK_GET_COMMON(snd_cwnd); 5806 break; 5807 case offsetof(struct bpf_tcp_sock, srtt_us): 5808 BPF_TCP_SOCK_GET_COMMON(srtt_us); 5809 break; 5810 case offsetof(struct bpf_tcp_sock, snd_ssthresh): 5811 BPF_TCP_SOCK_GET_COMMON(snd_ssthresh); 5812 break; 5813 case offsetof(struct bpf_tcp_sock, rcv_nxt): 5814 BPF_TCP_SOCK_GET_COMMON(rcv_nxt); 5815 break; 5816 case offsetof(struct bpf_tcp_sock, snd_nxt): 5817 BPF_TCP_SOCK_GET_COMMON(snd_nxt); 5818 break; 5819 case offsetof(struct bpf_tcp_sock, snd_una): 5820 BPF_TCP_SOCK_GET_COMMON(snd_una); 5821 break; 5822 case offsetof(struct bpf_tcp_sock, mss_cache): 5823 BPF_TCP_SOCK_GET_COMMON(mss_cache); 5824 break; 5825 case offsetof(struct bpf_tcp_sock, ecn_flags): 5826 BPF_TCP_SOCK_GET_COMMON(ecn_flags); 5827 break; 5828 case offsetof(struct bpf_tcp_sock, rate_delivered): 5829 BPF_TCP_SOCK_GET_COMMON(rate_delivered); 5830 break; 5831 case offsetof(struct bpf_tcp_sock, rate_interval_us): 5832 BPF_TCP_SOCK_GET_COMMON(rate_interval_us); 5833 break; 5834 case offsetof(struct bpf_tcp_sock, packets_out): 5835 BPF_TCP_SOCK_GET_COMMON(packets_out); 5836 break; 5837 case offsetof(struct bpf_tcp_sock, retrans_out): 5838 BPF_TCP_SOCK_GET_COMMON(retrans_out); 5839 break; 5840 case offsetof(struct bpf_tcp_sock, total_retrans): 5841 BPF_TCP_SOCK_GET_COMMON(total_retrans); 5842 break; 5843 case offsetof(struct bpf_tcp_sock, segs_in): 5844 BPF_TCP_SOCK_GET_COMMON(segs_in); 5845 break; 5846 case offsetof(struct bpf_tcp_sock, data_segs_in): 5847 BPF_TCP_SOCK_GET_COMMON(data_segs_in); 5848 break; 5849 case offsetof(struct bpf_tcp_sock, segs_out): 5850 BPF_TCP_SOCK_GET_COMMON(segs_out); 5851 break; 5852 case offsetof(struct bpf_tcp_sock, data_segs_out): 5853 BPF_TCP_SOCK_GET_COMMON(data_segs_out); 5854 break; 5855 case offsetof(struct bpf_tcp_sock, lost_out): 5856 BPF_TCP_SOCK_GET_COMMON(lost_out); 5857 break; 5858 case offsetof(struct bpf_tcp_sock, sacked_out): 5859 BPF_TCP_SOCK_GET_COMMON(sacked_out); 5860 break; 5861 case offsetof(struct bpf_tcp_sock, bytes_received): 5862 BPF_TCP_SOCK_GET_COMMON(bytes_received); 5863 break; 5864 case offsetof(struct bpf_tcp_sock, bytes_acked): 5865 BPF_TCP_SOCK_GET_COMMON(bytes_acked); 5866 break; 5867 case offsetof(struct bpf_tcp_sock, dsack_dups): 5868 BPF_TCP_SOCK_GET_COMMON(dsack_dups); 5869 break; 5870 case offsetof(struct bpf_tcp_sock, delivered): 5871 BPF_TCP_SOCK_GET_COMMON(delivered); 5872 break; 5873 case offsetof(struct bpf_tcp_sock, delivered_ce): 5874 BPF_TCP_SOCK_GET_COMMON(delivered_ce); 5875 break; 5876 case offsetof(struct bpf_tcp_sock, icsk_retransmits): 5877 BPF_INET_SOCK_GET_COMMON(icsk_retransmits); 5878 break; 5879 } 5880 5881 return insn - insn_buf; 5882 } 5883 5884 BPF_CALL_1(bpf_tcp_sock, struct sock *, sk) 5885 { 5886 if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP) 5887 return (unsigned long)sk; 5888 5889 return (unsigned long)NULL; 5890 } 5891 5892 const struct bpf_func_proto bpf_tcp_sock_proto = { 5893 .func = bpf_tcp_sock, 5894 .gpl_only = false, 5895 .ret_type = RET_PTR_TO_TCP_SOCK_OR_NULL, 5896 .arg1_type = ARG_PTR_TO_SOCK_COMMON, 5897 }; 5898 5899 BPF_CALL_1(bpf_get_listener_sock, struct sock *, sk) 5900 { 5901 sk = sk_to_full_sk(sk); 5902 5903 if (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_RCU_FREE)) 5904 return (unsigned long)sk; 5905 5906 return (unsigned long)NULL; 5907 } 5908 5909 static const struct bpf_func_proto bpf_get_listener_sock_proto = { 5910 .func = bpf_get_listener_sock, 5911 .gpl_only = false, 5912 .ret_type = RET_PTR_TO_SOCKET_OR_NULL, 5913 .arg1_type = ARG_PTR_TO_SOCK_COMMON, 5914 }; 5915 5916 BPF_CALL_1(bpf_skb_ecn_set_ce, struct sk_buff *, skb) 5917 { 5918 unsigned int iphdr_len; 5919 5920 switch (skb_protocol(skb, true)) { 5921 case cpu_to_be16(ETH_P_IP): 5922 iphdr_len = sizeof(struct iphdr); 5923 break; 5924 case cpu_to_be16(ETH_P_IPV6): 5925 iphdr_len = sizeof(struct ipv6hdr); 5926 break; 5927 default: 5928 return 0; 5929 } 5930 5931 if (skb_headlen(skb) < iphdr_len) 5932 return 0; 5933 5934 if (skb_cloned(skb) && !skb_clone_writable(skb, iphdr_len)) 5935 return 0; 5936 5937 return INET_ECN_set_ce(skb); 5938 } 5939 5940 bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type, 5941 struct bpf_insn_access_aux *info) 5942 { 5943 if (off < 0 || off >= offsetofend(struct bpf_xdp_sock, queue_id)) 5944 return false; 5945 5946 if (off % size != 0) 5947 return false; 5948 5949 switch (off) { 5950 default: 5951 return size == sizeof(__u32); 5952 } 5953 } 5954 5955 u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type, 5956 const struct bpf_insn *si, 5957 struct bpf_insn *insn_buf, 5958 struct bpf_prog *prog, u32 *target_size) 5959 { 5960 struct bpf_insn *insn = insn_buf; 5961 5962 #define BPF_XDP_SOCK_GET(FIELD) \ 5963 do { \ 5964 BUILD_BUG_ON(sizeof_field(struct xdp_sock, FIELD) > \ 5965 sizeof_field(struct bpf_xdp_sock, FIELD)); \ 5966 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_sock, FIELD),\ 5967 si->dst_reg, si->src_reg, \ 5968 offsetof(struct xdp_sock, FIELD)); \ 5969 } while (0) 5970 5971 switch (si->off) { 5972 case offsetof(struct bpf_xdp_sock, queue_id): 5973 BPF_XDP_SOCK_GET(queue_id); 5974 break; 5975 } 5976 5977 return insn - insn_buf; 5978 } 5979 5980 static const struct bpf_func_proto bpf_skb_ecn_set_ce_proto = { 5981 .func = bpf_skb_ecn_set_ce, 5982 .gpl_only = false, 5983 .ret_type = RET_INTEGER, 5984 .arg1_type = ARG_PTR_TO_CTX, 5985 }; 5986 5987 BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_len, 5988 struct tcphdr *, th, u32, th_len) 5989 { 5990 #ifdef CONFIG_SYN_COOKIES 5991 u32 cookie; 5992 int ret; 5993 5994 if (unlikely(th_len < sizeof(*th))) 5995 return -EINVAL; 5996 5997 /* sk_listener() allows TCP_NEW_SYN_RECV, which makes no sense here. */ 5998 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN) 5999 return -EINVAL; 6000 6001 if (!sock_net(sk)->ipv4.sysctl_tcp_syncookies) 6002 return -EINVAL; 6003 6004 if (!th->ack || th->rst || th->syn) 6005 return -ENOENT; 6006 6007 if (tcp_synq_no_recent_overflow(sk)) 6008 return -ENOENT; 6009 6010 cookie = ntohl(th->ack_seq) - 1; 6011 6012 switch (sk->sk_family) { 6013 case AF_INET: 6014 if (unlikely(iph_len < sizeof(struct iphdr))) 6015 return -EINVAL; 6016 6017 ret = __cookie_v4_check((struct iphdr *)iph, th, cookie); 6018 break; 6019 6020 #if IS_BUILTIN(CONFIG_IPV6) 6021 case AF_INET6: 6022 if (unlikely(iph_len < sizeof(struct ipv6hdr))) 6023 return -EINVAL; 6024 6025 ret = __cookie_v6_check((struct ipv6hdr *)iph, th, cookie); 6026 break; 6027 #endif /* CONFIG_IPV6 */ 6028 6029 default: 6030 return -EPROTONOSUPPORT; 6031 } 6032 6033 if (ret > 0) 6034 return 0; 6035 6036 return -ENOENT; 6037 #else 6038 return -ENOTSUPP; 6039 #endif 6040 } 6041 6042 static const struct bpf_func_proto bpf_tcp_check_syncookie_proto = { 6043 .func = bpf_tcp_check_syncookie, 6044 .gpl_only = true, 6045 .pkt_access = true, 6046 .ret_type = RET_INTEGER, 6047 .arg1_type = ARG_PTR_TO_SOCK_COMMON, 6048 .arg2_type = ARG_PTR_TO_MEM, 6049 .arg3_type = ARG_CONST_SIZE, 6050 .arg4_type = ARG_PTR_TO_MEM, 6051 .arg5_type = ARG_CONST_SIZE, 6052 }; 6053 6054 BPF_CALL_5(bpf_tcp_gen_syncookie, struct sock *, sk, void *, iph, u32, iph_len, 6055 struct tcphdr *, th, u32, th_len) 6056 { 6057 #ifdef CONFIG_SYN_COOKIES 6058 u32 cookie; 6059 u16 mss; 6060 6061 if (unlikely(th_len < sizeof(*th) || th_len != th->doff * 4)) 6062 return -EINVAL; 6063 6064 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN) 6065 return -EINVAL; 6066 6067 if (!sock_net(sk)->ipv4.sysctl_tcp_syncookies) 6068 return -ENOENT; 6069 6070 if (!th->syn || th->ack || th->fin || th->rst) 6071 return -EINVAL; 6072 6073 if (unlikely(iph_len < sizeof(struct iphdr))) 6074 return -EINVAL; 6075 6076 /* Both struct iphdr and struct ipv6hdr have the version field at the 6077 * same offset so we can cast to the shorter header (struct iphdr). 6078 */ 6079 switch (((struct iphdr *)iph)->version) { 6080 case 4: 6081 if (sk->sk_family == AF_INET6 && sk->sk_ipv6only) 6082 return -EINVAL; 6083 6084 mss = tcp_v4_get_syncookie(sk, iph, th, &cookie); 6085 break; 6086 6087 #if IS_BUILTIN(CONFIG_IPV6) 6088 case 6: 6089 if (unlikely(iph_len < sizeof(struct ipv6hdr))) 6090 return -EINVAL; 6091 6092 if (sk->sk_family != AF_INET6) 6093 return -EINVAL; 6094 6095 mss = tcp_v6_get_syncookie(sk, iph, th, &cookie); 6096 break; 6097 #endif /* CONFIG_IPV6 */ 6098 6099 default: 6100 return -EPROTONOSUPPORT; 6101 } 6102 if (mss == 0) 6103 return -ENOENT; 6104 6105 return cookie | ((u64)mss << 32); 6106 #else 6107 return -EOPNOTSUPP; 6108 #endif /* CONFIG_SYN_COOKIES */ 6109 } 6110 6111 static const struct bpf_func_proto bpf_tcp_gen_syncookie_proto = { 6112 .func = bpf_tcp_gen_syncookie, 6113 .gpl_only = true, /* __cookie_v*_init_sequence() is GPL */ 6114 .pkt_access = true, 6115 .ret_type = RET_INTEGER, 6116 .arg1_type = ARG_PTR_TO_SOCK_COMMON, 6117 .arg2_type = ARG_PTR_TO_MEM, 6118 .arg3_type = ARG_CONST_SIZE, 6119 .arg4_type = ARG_PTR_TO_MEM, 6120 .arg5_type = ARG_CONST_SIZE, 6121 }; 6122 6123 BPF_CALL_3(bpf_sk_assign, struct sk_buff *, skb, struct sock *, sk, u64, flags) 6124 { 6125 if (flags != 0) 6126 return -EINVAL; 6127 if (!skb_at_tc_ingress(skb)) 6128 return -EOPNOTSUPP; 6129 if (unlikely(dev_net(skb->dev) != sock_net(sk))) 6130 return -ENETUNREACH; 6131 if (unlikely(sk_fullsock(sk) && sk->sk_reuseport)) 6132 return -ESOCKTNOSUPPORT; 6133 if (sk_is_refcounted(sk) && 6134 unlikely(!refcount_inc_not_zero(&sk->sk_refcnt))) 6135 return -ENOENT; 6136 6137 skb_orphan(skb); 6138 skb->sk = sk; 6139 skb->destructor = sock_pfree; 6140 6141 return 0; 6142 } 6143 6144 static const struct bpf_func_proto bpf_sk_assign_proto = { 6145 .func = bpf_sk_assign, 6146 .gpl_only = false, 6147 .ret_type = RET_INTEGER, 6148 .arg1_type = ARG_PTR_TO_CTX, 6149 .arg2_type = ARG_PTR_TO_SOCK_COMMON, 6150 .arg3_type = ARG_ANYTHING, 6151 }; 6152 6153 #endif /* CONFIG_INET */ 6154 6155 bool bpf_helper_changes_pkt_data(void *func) 6156 { 6157 if (func == bpf_skb_vlan_push || 6158 func == bpf_skb_vlan_pop || 6159 func == bpf_skb_store_bytes || 6160 func == bpf_skb_change_proto || 6161 func == bpf_skb_change_head || 6162 func == sk_skb_change_head || 6163 func == bpf_skb_change_tail || 6164 func == sk_skb_change_tail || 6165 func == bpf_skb_adjust_room || 6166 func == bpf_skb_pull_data || 6167 func == sk_skb_pull_data || 6168 func == bpf_clone_redirect || 6169 func == bpf_l3_csum_replace || 6170 func == bpf_l4_csum_replace || 6171 func == bpf_xdp_adjust_head || 6172 func == bpf_xdp_adjust_meta || 6173 func == bpf_msg_pull_data || 6174 func == bpf_msg_push_data || 6175 func == bpf_msg_pop_data || 6176 func == bpf_xdp_adjust_tail || 6177 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF) 6178 func == bpf_lwt_seg6_store_bytes || 6179 func == bpf_lwt_seg6_adjust_srh || 6180 func == bpf_lwt_seg6_action || 6181 #endif 6182 func == bpf_lwt_in_push_encap || 6183 func == bpf_lwt_xmit_push_encap) 6184 return true; 6185 6186 return false; 6187 } 6188 6189 const struct bpf_func_proto bpf_event_output_data_proto __weak; 6190 const struct bpf_func_proto bpf_sk_storage_get_cg_sock_proto __weak; 6191 6192 static const struct bpf_func_proto * 6193 sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 6194 { 6195 switch (func_id) { 6196 /* inet and inet6 sockets are created in a process 6197 * context so there is always a valid uid/gid 6198 */ 6199 case BPF_FUNC_get_current_uid_gid: 6200 return &bpf_get_current_uid_gid_proto; 6201 case BPF_FUNC_get_local_storage: 6202 return &bpf_get_local_storage_proto; 6203 case BPF_FUNC_get_socket_cookie: 6204 return &bpf_get_socket_cookie_sock_proto; 6205 case BPF_FUNC_get_netns_cookie: 6206 return &bpf_get_netns_cookie_sock_proto; 6207 case BPF_FUNC_perf_event_output: 6208 return &bpf_event_output_data_proto; 6209 case BPF_FUNC_get_current_pid_tgid: 6210 return &bpf_get_current_pid_tgid_proto; 6211 case BPF_FUNC_get_current_comm: 6212 return &bpf_get_current_comm_proto; 6213 #ifdef CONFIG_CGROUPS 6214 case BPF_FUNC_get_current_cgroup_id: 6215 return &bpf_get_current_cgroup_id_proto; 6216 case BPF_FUNC_get_current_ancestor_cgroup_id: 6217 return &bpf_get_current_ancestor_cgroup_id_proto; 6218 #endif 6219 #ifdef CONFIG_CGROUP_NET_CLASSID 6220 case BPF_FUNC_get_cgroup_classid: 6221 return &bpf_get_cgroup_classid_curr_proto; 6222 #endif 6223 case BPF_FUNC_sk_storage_get: 6224 return &bpf_sk_storage_get_cg_sock_proto; 6225 default: 6226 return bpf_base_func_proto(func_id); 6227 } 6228 } 6229 6230 static const struct bpf_func_proto * 6231 sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 6232 { 6233 switch (func_id) { 6234 /* inet and inet6 sockets are created in a process 6235 * context so there is always a valid uid/gid 6236 */ 6237 case BPF_FUNC_get_current_uid_gid: 6238 return &bpf_get_current_uid_gid_proto; 6239 case BPF_FUNC_bind: 6240 switch (prog->expected_attach_type) { 6241 case BPF_CGROUP_INET4_CONNECT: 6242 case BPF_CGROUP_INET6_CONNECT: 6243 return &bpf_bind_proto; 6244 default: 6245 return NULL; 6246 } 6247 case BPF_FUNC_get_socket_cookie: 6248 return &bpf_get_socket_cookie_sock_addr_proto; 6249 case BPF_FUNC_get_netns_cookie: 6250 return &bpf_get_netns_cookie_sock_addr_proto; 6251 case BPF_FUNC_get_local_storage: 6252 return &bpf_get_local_storage_proto; 6253 case BPF_FUNC_perf_event_output: 6254 return &bpf_event_output_data_proto; 6255 case BPF_FUNC_get_current_pid_tgid: 6256 return &bpf_get_current_pid_tgid_proto; 6257 case BPF_FUNC_get_current_comm: 6258 return &bpf_get_current_comm_proto; 6259 #ifdef CONFIG_CGROUPS 6260 case BPF_FUNC_get_current_cgroup_id: 6261 return &bpf_get_current_cgroup_id_proto; 6262 case BPF_FUNC_get_current_ancestor_cgroup_id: 6263 return &bpf_get_current_ancestor_cgroup_id_proto; 6264 #endif 6265 #ifdef CONFIG_CGROUP_NET_CLASSID 6266 case BPF_FUNC_get_cgroup_classid: 6267 return &bpf_get_cgroup_classid_curr_proto; 6268 #endif 6269 #ifdef CONFIG_INET 6270 case BPF_FUNC_sk_lookup_tcp: 6271 return &bpf_sock_addr_sk_lookup_tcp_proto; 6272 case BPF_FUNC_sk_lookup_udp: 6273 return &bpf_sock_addr_sk_lookup_udp_proto; 6274 case BPF_FUNC_sk_release: 6275 return &bpf_sk_release_proto; 6276 case BPF_FUNC_skc_lookup_tcp: 6277 return &bpf_sock_addr_skc_lookup_tcp_proto; 6278 #endif /* CONFIG_INET */ 6279 case BPF_FUNC_sk_storage_get: 6280 return &bpf_sk_storage_get_proto; 6281 case BPF_FUNC_sk_storage_delete: 6282 return &bpf_sk_storage_delete_proto; 6283 case BPF_FUNC_setsockopt: 6284 switch (prog->expected_attach_type) { 6285 case BPF_CGROUP_INET4_CONNECT: 6286 case BPF_CGROUP_INET6_CONNECT: 6287 return &bpf_sock_addr_setsockopt_proto; 6288 default: 6289 return NULL; 6290 } 6291 case BPF_FUNC_getsockopt: 6292 switch (prog->expected_attach_type) { 6293 case BPF_CGROUP_INET4_CONNECT: 6294 case BPF_CGROUP_INET6_CONNECT: 6295 return &bpf_sock_addr_getsockopt_proto; 6296 default: 6297 return NULL; 6298 } 6299 default: 6300 return bpf_base_func_proto(func_id); 6301 } 6302 } 6303 6304 static const struct bpf_func_proto * 6305 sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 6306 { 6307 switch (func_id) { 6308 case BPF_FUNC_skb_load_bytes: 6309 return &bpf_skb_load_bytes_proto; 6310 case BPF_FUNC_skb_load_bytes_relative: 6311 return &bpf_skb_load_bytes_relative_proto; 6312 case BPF_FUNC_get_socket_cookie: 6313 return &bpf_get_socket_cookie_proto; 6314 case BPF_FUNC_get_socket_uid: 6315 return &bpf_get_socket_uid_proto; 6316 case BPF_FUNC_perf_event_output: 6317 return &bpf_skb_event_output_proto; 6318 default: 6319 return bpf_base_func_proto(func_id); 6320 } 6321 } 6322 6323 const struct bpf_func_proto bpf_sk_storage_get_proto __weak; 6324 const struct bpf_func_proto bpf_sk_storage_delete_proto __weak; 6325 6326 static const struct bpf_func_proto * 6327 cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 6328 { 6329 switch (func_id) { 6330 case BPF_FUNC_get_local_storage: 6331 return &bpf_get_local_storage_proto; 6332 case BPF_FUNC_sk_fullsock: 6333 return &bpf_sk_fullsock_proto; 6334 case BPF_FUNC_sk_storage_get: 6335 return &bpf_sk_storage_get_proto; 6336 case BPF_FUNC_sk_storage_delete: 6337 return &bpf_sk_storage_delete_proto; 6338 case BPF_FUNC_perf_event_output: 6339 return &bpf_skb_event_output_proto; 6340 #ifdef CONFIG_SOCK_CGROUP_DATA 6341 case BPF_FUNC_skb_cgroup_id: 6342 return &bpf_skb_cgroup_id_proto; 6343 case BPF_FUNC_skb_ancestor_cgroup_id: 6344 return &bpf_skb_ancestor_cgroup_id_proto; 6345 case BPF_FUNC_sk_cgroup_id: 6346 return &bpf_sk_cgroup_id_proto; 6347 case BPF_FUNC_sk_ancestor_cgroup_id: 6348 return &bpf_sk_ancestor_cgroup_id_proto; 6349 #endif 6350 #ifdef CONFIG_INET 6351 case BPF_FUNC_sk_lookup_tcp: 6352 return &bpf_sk_lookup_tcp_proto; 6353 case BPF_FUNC_sk_lookup_udp: 6354 return &bpf_sk_lookup_udp_proto; 6355 case BPF_FUNC_sk_release: 6356 return &bpf_sk_release_proto; 6357 case BPF_FUNC_skc_lookup_tcp: 6358 return &bpf_skc_lookup_tcp_proto; 6359 case BPF_FUNC_tcp_sock: 6360 return &bpf_tcp_sock_proto; 6361 case BPF_FUNC_get_listener_sock: 6362 return &bpf_get_listener_sock_proto; 6363 case BPF_FUNC_skb_ecn_set_ce: 6364 return &bpf_skb_ecn_set_ce_proto; 6365 #endif 6366 default: 6367 return sk_filter_func_proto(func_id, prog); 6368 } 6369 } 6370 6371 static const struct bpf_func_proto * 6372 tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 6373 { 6374 switch (func_id) { 6375 case BPF_FUNC_skb_store_bytes: 6376 return &bpf_skb_store_bytes_proto; 6377 case BPF_FUNC_skb_load_bytes: 6378 return &bpf_skb_load_bytes_proto; 6379 case BPF_FUNC_skb_load_bytes_relative: 6380 return &bpf_skb_load_bytes_relative_proto; 6381 case BPF_FUNC_skb_pull_data: 6382 return &bpf_skb_pull_data_proto; 6383 case BPF_FUNC_csum_diff: 6384 return &bpf_csum_diff_proto; 6385 case BPF_FUNC_csum_update: 6386 return &bpf_csum_update_proto; 6387 case BPF_FUNC_csum_level: 6388 return &bpf_csum_level_proto; 6389 case BPF_FUNC_l3_csum_replace: 6390 return &bpf_l3_csum_replace_proto; 6391 case BPF_FUNC_l4_csum_replace: 6392 return &bpf_l4_csum_replace_proto; 6393 case BPF_FUNC_clone_redirect: 6394 return &bpf_clone_redirect_proto; 6395 case BPF_FUNC_get_cgroup_classid: 6396 return &bpf_get_cgroup_classid_proto; 6397 case BPF_FUNC_skb_vlan_push: 6398 return &bpf_skb_vlan_push_proto; 6399 case BPF_FUNC_skb_vlan_pop: 6400 return &bpf_skb_vlan_pop_proto; 6401 case BPF_FUNC_skb_change_proto: 6402 return &bpf_skb_change_proto_proto; 6403 case BPF_FUNC_skb_change_type: 6404 return &bpf_skb_change_type_proto; 6405 case BPF_FUNC_skb_adjust_room: 6406 return &bpf_skb_adjust_room_proto; 6407 case BPF_FUNC_skb_change_tail: 6408 return &bpf_skb_change_tail_proto; 6409 case BPF_FUNC_skb_change_head: 6410 return &bpf_skb_change_head_proto; 6411 case BPF_FUNC_skb_get_tunnel_key: 6412 return &bpf_skb_get_tunnel_key_proto; 6413 case BPF_FUNC_skb_set_tunnel_key: 6414 return bpf_get_skb_set_tunnel_proto(func_id); 6415 case BPF_FUNC_skb_get_tunnel_opt: 6416 return &bpf_skb_get_tunnel_opt_proto; 6417 case BPF_FUNC_skb_set_tunnel_opt: 6418 return bpf_get_skb_set_tunnel_proto(func_id); 6419 case BPF_FUNC_redirect: 6420 return &bpf_redirect_proto; 6421 case BPF_FUNC_get_route_realm: 6422 return &bpf_get_route_realm_proto; 6423 case BPF_FUNC_get_hash_recalc: 6424 return &bpf_get_hash_recalc_proto; 6425 case BPF_FUNC_set_hash_invalid: 6426 return &bpf_set_hash_invalid_proto; 6427 case BPF_FUNC_set_hash: 6428 return &bpf_set_hash_proto; 6429 case BPF_FUNC_perf_event_output: 6430 return &bpf_skb_event_output_proto; 6431 case BPF_FUNC_get_smp_processor_id: 6432 return &bpf_get_smp_processor_id_proto; 6433 case BPF_FUNC_skb_under_cgroup: 6434 return &bpf_skb_under_cgroup_proto; 6435 case BPF_FUNC_get_socket_cookie: 6436 return &bpf_get_socket_cookie_proto; 6437 case BPF_FUNC_get_socket_uid: 6438 return &bpf_get_socket_uid_proto; 6439 case BPF_FUNC_fib_lookup: 6440 return &bpf_skb_fib_lookup_proto; 6441 case BPF_FUNC_sk_fullsock: 6442 return &bpf_sk_fullsock_proto; 6443 case BPF_FUNC_sk_storage_get: 6444 return &bpf_sk_storage_get_proto; 6445 case BPF_FUNC_sk_storage_delete: 6446 return &bpf_sk_storage_delete_proto; 6447 #ifdef CONFIG_XFRM 6448 case BPF_FUNC_skb_get_xfrm_state: 6449 return &bpf_skb_get_xfrm_state_proto; 6450 #endif 6451 #ifdef CONFIG_SOCK_CGROUP_DATA 6452 case BPF_FUNC_skb_cgroup_id: 6453 return &bpf_skb_cgroup_id_proto; 6454 case BPF_FUNC_skb_ancestor_cgroup_id: 6455 return &bpf_skb_ancestor_cgroup_id_proto; 6456 #endif 6457 #ifdef CONFIG_INET 6458 case BPF_FUNC_sk_lookup_tcp: 6459 return &bpf_sk_lookup_tcp_proto; 6460 case BPF_FUNC_sk_lookup_udp: 6461 return &bpf_sk_lookup_udp_proto; 6462 case BPF_FUNC_sk_release: 6463 return &bpf_sk_release_proto; 6464 case BPF_FUNC_tcp_sock: 6465 return &bpf_tcp_sock_proto; 6466 case BPF_FUNC_get_listener_sock: 6467 return &bpf_get_listener_sock_proto; 6468 case BPF_FUNC_skc_lookup_tcp: 6469 return &bpf_skc_lookup_tcp_proto; 6470 case BPF_FUNC_tcp_check_syncookie: 6471 return &bpf_tcp_check_syncookie_proto; 6472 case BPF_FUNC_skb_ecn_set_ce: 6473 return &bpf_skb_ecn_set_ce_proto; 6474 case BPF_FUNC_tcp_gen_syncookie: 6475 return &bpf_tcp_gen_syncookie_proto; 6476 case BPF_FUNC_sk_assign: 6477 return &bpf_sk_assign_proto; 6478 #endif 6479 default: 6480 return bpf_base_func_proto(func_id); 6481 } 6482 } 6483 6484 static const struct bpf_func_proto * 6485 xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 6486 { 6487 switch (func_id) { 6488 case BPF_FUNC_perf_event_output: 6489 return &bpf_xdp_event_output_proto; 6490 case BPF_FUNC_get_smp_processor_id: 6491 return &bpf_get_smp_processor_id_proto; 6492 case BPF_FUNC_csum_diff: 6493 return &bpf_csum_diff_proto; 6494 case BPF_FUNC_xdp_adjust_head: 6495 return &bpf_xdp_adjust_head_proto; 6496 case BPF_FUNC_xdp_adjust_meta: 6497 return &bpf_xdp_adjust_meta_proto; 6498 case BPF_FUNC_redirect: 6499 return &bpf_xdp_redirect_proto; 6500 case BPF_FUNC_redirect_map: 6501 return &bpf_xdp_redirect_map_proto; 6502 case BPF_FUNC_xdp_adjust_tail: 6503 return &bpf_xdp_adjust_tail_proto; 6504 case BPF_FUNC_fib_lookup: 6505 return &bpf_xdp_fib_lookup_proto; 6506 #ifdef CONFIG_INET 6507 case BPF_FUNC_sk_lookup_udp: 6508 return &bpf_xdp_sk_lookup_udp_proto; 6509 case BPF_FUNC_sk_lookup_tcp: 6510 return &bpf_xdp_sk_lookup_tcp_proto; 6511 case BPF_FUNC_sk_release: 6512 return &bpf_sk_release_proto; 6513 case BPF_FUNC_skc_lookup_tcp: 6514 return &bpf_xdp_skc_lookup_tcp_proto; 6515 case BPF_FUNC_tcp_check_syncookie: 6516 return &bpf_tcp_check_syncookie_proto; 6517 case BPF_FUNC_tcp_gen_syncookie: 6518 return &bpf_tcp_gen_syncookie_proto; 6519 #endif 6520 default: 6521 return bpf_base_func_proto(func_id); 6522 } 6523 } 6524 6525 const struct bpf_func_proto bpf_sock_map_update_proto __weak; 6526 const struct bpf_func_proto bpf_sock_hash_update_proto __weak; 6527 6528 static const struct bpf_func_proto * 6529 sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 6530 { 6531 switch (func_id) { 6532 case BPF_FUNC_setsockopt: 6533 return &bpf_sock_ops_setsockopt_proto; 6534 case BPF_FUNC_getsockopt: 6535 return &bpf_sock_ops_getsockopt_proto; 6536 case BPF_FUNC_sock_ops_cb_flags_set: 6537 return &bpf_sock_ops_cb_flags_set_proto; 6538 case BPF_FUNC_sock_map_update: 6539 return &bpf_sock_map_update_proto; 6540 case BPF_FUNC_sock_hash_update: 6541 return &bpf_sock_hash_update_proto; 6542 case BPF_FUNC_get_socket_cookie: 6543 return &bpf_get_socket_cookie_sock_ops_proto; 6544 case BPF_FUNC_get_local_storage: 6545 return &bpf_get_local_storage_proto; 6546 case BPF_FUNC_perf_event_output: 6547 return &bpf_event_output_data_proto; 6548 case BPF_FUNC_sk_storage_get: 6549 return &bpf_sk_storage_get_proto; 6550 case BPF_FUNC_sk_storage_delete: 6551 return &bpf_sk_storage_delete_proto; 6552 #ifdef CONFIG_INET 6553 case BPF_FUNC_tcp_sock: 6554 return &bpf_tcp_sock_proto; 6555 #endif /* CONFIG_INET */ 6556 default: 6557 return bpf_base_func_proto(func_id); 6558 } 6559 } 6560 6561 const struct bpf_func_proto bpf_msg_redirect_map_proto __weak; 6562 const struct bpf_func_proto bpf_msg_redirect_hash_proto __weak; 6563 6564 static const struct bpf_func_proto * 6565 sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 6566 { 6567 switch (func_id) { 6568 case BPF_FUNC_msg_redirect_map: 6569 return &bpf_msg_redirect_map_proto; 6570 case BPF_FUNC_msg_redirect_hash: 6571 return &bpf_msg_redirect_hash_proto; 6572 case BPF_FUNC_msg_apply_bytes: 6573 return &bpf_msg_apply_bytes_proto; 6574 case BPF_FUNC_msg_cork_bytes: 6575 return &bpf_msg_cork_bytes_proto; 6576 case BPF_FUNC_msg_pull_data: 6577 return &bpf_msg_pull_data_proto; 6578 case BPF_FUNC_msg_push_data: 6579 return &bpf_msg_push_data_proto; 6580 case BPF_FUNC_msg_pop_data: 6581 return &bpf_msg_pop_data_proto; 6582 case BPF_FUNC_perf_event_output: 6583 return &bpf_event_output_data_proto; 6584 case BPF_FUNC_get_current_uid_gid: 6585 return &bpf_get_current_uid_gid_proto; 6586 case BPF_FUNC_get_current_pid_tgid: 6587 return &bpf_get_current_pid_tgid_proto; 6588 case BPF_FUNC_sk_storage_get: 6589 return &bpf_sk_storage_get_proto; 6590 case BPF_FUNC_sk_storage_delete: 6591 return &bpf_sk_storage_delete_proto; 6592 #ifdef CONFIG_CGROUPS 6593 case BPF_FUNC_get_current_cgroup_id: 6594 return &bpf_get_current_cgroup_id_proto; 6595 case BPF_FUNC_get_current_ancestor_cgroup_id: 6596 return &bpf_get_current_ancestor_cgroup_id_proto; 6597 #endif 6598 #ifdef CONFIG_CGROUP_NET_CLASSID 6599 case BPF_FUNC_get_cgroup_classid: 6600 return &bpf_get_cgroup_classid_curr_proto; 6601 #endif 6602 default: 6603 return bpf_base_func_proto(func_id); 6604 } 6605 } 6606 6607 const struct bpf_func_proto bpf_sk_redirect_map_proto __weak; 6608 const struct bpf_func_proto bpf_sk_redirect_hash_proto __weak; 6609 6610 static const struct bpf_func_proto * 6611 sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 6612 { 6613 switch (func_id) { 6614 case BPF_FUNC_skb_store_bytes: 6615 return &bpf_skb_store_bytes_proto; 6616 case BPF_FUNC_skb_load_bytes: 6617 return &bpf_skb_load_bytes_proto; 6618 case BPF_FUNC_skb_pull_data: 6619 return &sk_skb_pull_data_proto; 6620 case BPF_FUNC_skb_change_tail: 6621 return &sk_skb_change_tail_proto; 6622 case BPF_FUNC_skb_change_head: 6623 return &sk_skb_change_head_proto; 6624 case BPF_FUNC_get_socket_cookie: 6625 return &bpf_get_socket_cookie_proto; 6626 case BPF_FUNC_get_socket_uid: 6627 return &bpf_get_socket_uid_proto; 6628 case BPF_FUNC_sk_redirect_map: 6629 return &bpf_sk_redirect_map_proto; 6630 case BPF_FUNC_sk_redirect_hash: 6631 return &bpf_sk_redirect_hash_proto; 6632 case BPF_FUNC_perf_event_output: 6633 return &bpf_skb_event_output_proto; 6634 #ifdef CONFIG_INET 6635 case BPF_FUNC_sk_lookup_tcp: 6636 return &bpf_sk_lookup_tcp_proto; 6637 case BPF_FUNC_sk_lookup_udp: 6638 return &bpf_sk_lookup_udp_proto; 6639 case BPF_FUNC_sk_release: 6640 return &bpf_sk_release_proto; 6641 case BPF_FUNC_skc_lookup_tcp: 6642 return &bpf_skc_lookup_tcp_proto; 6643 #endif 6644 default: 6645 return bpf_base_func_proto(func_id); 6646 } 6647 } 6648 6649 static const struct bpf_func_proto * 6650 flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 6651 { 6652 switch (func_id) { 6653 case BPF_FUNC_skb_load_bytes: 6654 return &bpf_flow_dissector_load_bytes_proto; 6655 default: 6656 return bpf_base_func_proto(func_id); 6657 } 6658 } 6659 6660 static const struct bpf_func_proto * 6661 lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 6662 { 6663 switch (func_id) { 6664 case BPF_FUNC_skb_load_bytes: 6665 return &bpf_skb_load_bytes_proto; 6666 case BPF_FUNC_skb_pull_data: 6667 return &bpf_skb_pull_data_proto; 6668 case BPF_FUNC_csum_diff: 6669 return &bpf_csum_diff_proto; 6670 case BPF_FUNC_get_cgroup_classid: 6671 return &bpf_get_cgroup_classid_proto; 6672 case BPF_FUNC_get_route_realm: 6673 return &bpf_get_route_realm_proto; 6674 case BPF_FUNC_get_hash_recalc: 6675 return &bpf_get_hash_recalc_proto; 6676 case BPF_FUNC_perf_event_output: 6677 return &bpf_skb_event_output_proto; 6678 case BPF_FUNC_get_smp_processor_id: 6679 return &bpf_get_smp_processor_id_proto; 6680 case BPF_FUNC_skb_under_cgroup: 6681 return &bpf_skb_under_cgroup_proto; 6682 default: 6683 return bpf_base_func_proto(func_id); 6684 } 6685 } 6686 6687 static const struct bpf_func_proto * 6688 lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 6689 { 6690 switch (func_id) { 6691 case BPF_FUNC_lwt_push_encap: 6692 return &bpf_lwt_in_push_encap_proto; 6693 default: 6694 return lwt_out_func_proto(func_id, prog); 6695 } 6696 } 6697 6698 static const struct bpf_func_proto * 6699 lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 6700 { 6701 switch (func_id) { 6702 case BPF_FUNC_skb_get_tunnel_key: 6703 return &bpf_skb_get_tunnel_key_proto; 6704 case BPF_FUNC_skb_set_tunnel_key: 6705 return bpf_get_skb_set_tunnel_proto(func_id); 6706 case BPF_FUNC_skb_get_tunnel_opt: 6707 return &bpf_skb_get_tunnel_opt_proto; 6708 case BPF_FUNC_skb_set_tunnel_opt: 6709 return bpf_get_skb_set_tunnel_proto(func_id); 6710 case BPF_FUNC_redirect: 6711 return &bpf_redirect_proto; 6712 case BPF_FUNC_clone_redirect: 6713 return &bpf_clone_redirect_proto; 6714 case BPF_FUNC_skb_change_tail: 6715 return &bpf_skb_change_tail_proto; 6716 case BPF_FUNC_skb_change_head: 6717 return &bpf_skb_change_head_proto; 6718 case BPF_FUNC_skb_store_bytes: 6719 return &bpf_skb_store_bytes_proto; 6720 case BPF_FUNC_csum_update: 6721 return &bpf_csum_update_proto; 6722 case BPF_FUNC_csum_level: 6723 return &bpf_csum_level_proto; 6724 case BPF_FUNC_l3_csum_replace: 6725 return &bpf_l3_csum_replace_proto; 6726 case BPF_FUNC_l4_csum_replace: 6727 return &bpf_l4_csum_replace_proto; 6728 case BPF_FUNC_set_hash_invalid: 6729 return &bpf_set_hash_invalid_proto; 6730 case BPF_FUNC_lwt_push_encap: 6731 return &bpf_lwt_xmit_push_encap_proto; 6732 default: 6733 return lwt_out_func_proto(func_id, prog); 6734 } 6735 } 6736 6737 static const struct bpf_func_proto * 6738 lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 6739 { 6740 switch (func_id) { 6741 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF) 6742 case BPF_FUNC_lwt_seg6_store_bytes: 6743 return &bpf_lwt_seg6_store_bytes_proto; 6744 case BPF_FUNC_lwt_seg6_action: 6745 return &bpf_lwt_seg6_action_proto; 6746 case BPF_FUNC_lwt_seg6_adjust_srh: 6747 return &bpf_lwt_seg6_adjust_srh_proto; 6748 #endif 6749 default: 6750 return lwt_out_func_proto(func_id, prog); 6751 } 6752 } 6753 6754 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type, 6755 const struct bpf_prog *prog, 6756 struct bpf_insn_access_aux *info) 6757 { 6758 const int size_default = sizeof(__u32); 6759 6760 if (off < 0 || off >= sizeof(struct __sk_buff)) 6761 return false; 6762 6763 /* The verifier guarantees that size > 0. */ 6764 if (off % size != 0) 6765 return false; 6766 6767 switch (off) { 6768 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]): 6769 if (off + size > offsetofend(struct __sk_buff, cb[4])) 6770 return false; 6771 break; 6772 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]): 6773 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]): 6774 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4): 6775 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4): 6776 case bpf_ctx_range(struct __sk_buff, data): 6777 case bpf_ctx_range(struct __sk_buff, data_meta): 6778 case bpf_ctx_range(struct __sk_buff, data_end): 6779 if (size != size_default) 6780 return false; 6781 break; 6782 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys): 6783 return false; 6784 case bpf_ctx_range(struct __sk_buff, tstamp): 6785 if (size != sizeof(__u64)) 6786 return false; 6787 break; 6788 case offsetof(struct __sk_buff, sk): 6789 if (type == BPF_WRITE || size != sizeof(__u64)) 6790 return false; 6791 info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL; 6792 break; 6793 default: 6794 /* Only narrow read access allowed for now. */ 6795 if (type == BPF_WRITE) { 6796 if (size != size_default) 6797 return false; 6798 } else { 6799 bpf_ctx_record_field_size(info, size_default); 6800 if (!bpf_ctx_narrow_access_ok(off, size, size_default)) 6801 return false; 6802 } 6803 } 6804 6805 return true; 6806 } 6807 6808 static bool sk_filter_is_valid_access(int off, int size, 6809 enum bpf_access_type type, 6810 const struct bpf_prog *prog, 6811 struct bpf_insn_access_aux *info) 6812 { 6813 switch (off) { 6814 case bpf_ctx_range(struct __sk_buff, tc_classid): 6815 case bpf_ctx_range(struct __sk_buff, data): 6816 case bpf_ctx_range(struct __sk_buff, data_meta): 6817 case bpf_ctx_range(struct __sk_buff, data_end): 6818 case bpf_ctx_range_till(struct __sk_buff, family, local_port): 6819 case bpf_ctx_range(struct __sk_buff, tstamp): 6820 case bpf_ctx_range(struct __sk_buff, wire_len): 6821 return false; 6822 } 6823 6824 if (type == BPF_WRITE) { 6825 switch (off) { 6826 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]): 6827 break; 6828 default: 6829 return false; 6830 } 6831 } 6832 6833 return bpf_skb_is_valid_access(off, size, type, prog, info); 6834 } 6835 6836 static bool cg_skb_is_valid_access(int off, int size, 6837 enum bpf_access_type type, 6838 const struct bpf_prog *prog, 6839 struct bpf_insn_access_aux *info) 6840 { 6841 switch (off) { 6842 case bpf_ctx_range(struct __sk_buff, tc_classid): 6843 case bpf_ctx_range(struct __sk_buff, data_meta): 6844 case bpf_ctx_range(struct __sk_buff, wire_len): 6845 return false; 6846 case bpf_ctx_range(struct __sk_buff, data): 6847 case bpf_ctx_range(struct __sk_buff, data_end): 6848 if (!bpf_capable()) 6849 return false; 6850 break; 6851 } 6852 6853 if (type == BPF_WRITE) { 6854 switch (off) { 6855 case bpf_ctx_range(struct __sk_buff, mark): 6856 case bpf_ctx_range(struct __sk_buff, priority): 6857 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]): 6858 break; 6859 case bpf_ctx_range(struct __sk_buff, tstamp): 6860 if (!bpf_capable()) 6861 return false; 6862 break; 6863 default: 6864 return false; 6865 } 6866 } 6867 6868 switch (off) { 6869 case bpf_ctx_range(struct __sk_buff, data): 6870 info->reg_type = PTR_TO_PACKET; 6871 break; 6872 case bpf_ctx_range(struct __sk_buff, data_end): 6873 info->reg_type = PTR_TO_PACKET_END; 6874 break; 6875 } 6876 6877 return bpf_skb_is_valid_access(off, size, type, prog, info); 6878 } 6879 6880 static bool lwt_is_valid_access(int off, int size, 6881 enum bpf_access_type type, 6882 const struct bpf_prog *prog, 6883 struct bpf_insn_access_aux *info) 6884 { 6885 switch (off) { 6886 case bpf_ctx_range(struct __sk_buff, tc_classid): 6887 case bpf_ctx_range_till(struct __sk_buff, family, local_port): 6888 case bpf_ctx_range(struct __sk_buff, data_meta): 6889 case bpf_ctx_range(struct __sk_buff, tstamp): 6890 case bpf_ctx_range(struct __sk_buff, wire_len): 6891 return false; 6892 } 6893 6894 if (type == BPF_WRITE) { 6895 switch (off) { 6896 case bpf_ctx_range(struct __sk_buff, mark): 6897 case bpf_ctx_range(struct __sk_buff, priority): 6898 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]): 6899 break; 6900 default: 6901 return false; 6902 } 6903 } 6904 6905 switch (off) { 6906 case bpf_ctx_range(struct __sk_buff, data): 6907 info->reg_type = PTR_TO_PACKET; 6908 break; 6909 case bpf_ctx_range(struct __sk_buff, data_end): 6910 info->reg_type = PTR_TO_PACKET_END; 6911 break; 6912 } 6913 6914 return bpf_skb_is_valid_access(off, size, type, prog, info); 6915 } 6916 6917 /* Attach type specific accesses */ 6918 static bool __sock_filter_check_attach_type(int off, 6919 enum bpf_access_type access_type, 6920 enum bpf_attach_type attach_type) 6921 { 6922 switch (off) { 6923 case offsetof(struct bpf_sock, bound_dev_if): 6924 case offsetof(struct bpf_sock, mark): 6925 case offsetof(struct bpf_sock, priority): 6926 switch (attach_type) { 6927 case BPF_CGROUP_INET_SOCK_CREATE: 6928 case BPF_CGROUP_INET_SOCK_RELEASE: 6929 goto full_access; 6930 default: 6931 return false; 6932 } 6933 case bpf_ctx_range(struct bpf_sock, src_ip4): 6934 switch (attach_type) { 6935 case BPF_CGROUP_INET4_POST_BIND: 6936 goto read_only; 6937 default: 6938 return false; 6939 } 6940 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]): 6941 switch (attach_type) { 6942 case BPF_CGROUP_INET6_POST_BIND: 6943 goto read_only; 6944 default: 6945 return false; 6946 } 6947 case bpf_ctx_range(struct bpf_sock, src_port): 6948 switch (attach_type) { 6949 case BPF_CGROUP_INET4_POST_BIND: 6950 case BPF_CGROUP_INET6_POST_BIND: 6951 goto read_only; 6952 default: 6953 return false; 6954 } 6955 } 6956 read_only: 6957 return access_type == BPF_READ; 6958 full_access: 6959 return true; 6960 } 6961 6962 bool bpf_sock_common_is_valid_access(int off, int size, 6963 enum bpf_access_type type, 6964 struct bpf_insn_access_aux *info) 6965 { 6966 switch (off) { 6967 case bpf_ctx_range_till(struct bpf_sock, type, priority): 6968 return false; 6969 default: 6970 return bpf_sock_is_valid_access(off, size, type, info); 6971 } 6972 } 6973 6974 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type, 6975 struct bpf_insn_access_aux *info) 6976 { 6977 const int size_default = sizeof(__u32); 6978 6979 if (off < 0 || off >= sizeof(struct bpf_sock)) 6980 return false; 6981 if (off % size != 0) 6982 return false; 6983 6984 switch (off) { 6985 case offsetof(struct bpf_sock, state): 6986 case offsetof(struct bpf_sock, family): 6987 case offsetof(struct bpf_sock, type): 6988 case offsetof(struct bpf_sock, protocol): 6989 case offsetof(struct bpf_sock, dst_port): 6990 case offsetof(struct bpf_sock, src_port): 6991 case offsetof(struct bpf_sock, rx_queue_mapping): 6992 case bpf_ctx_range(struct bpf_sock, src_ip4): 6993 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]): 6994 case bpf_ctx_range(struct bpf_sock, dst_ip4): 6995 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]): 6996 bpf_ctx_record_field_size(info, size_default); 6997 return bpf_ctx_narrow_access_ok(off, size, size_default); 6998 } 6999 7000 return size == size_default; 7001 } 7002 7003 static bool sock_filter_is_valid_access(int off, int size, 7004 enum bpf_access_type type, 7005 const struct bpf_prog *prog, 7006 struct bpf_insn_access_aux *info) 7007 { 7008 if (!bpf_sock_is_valid_access(off, size, type, info)) 7009 return false; 7010 return __sock_filter_check_attach_type(off, type, 7011 prog->expected_attach_type); 7012 } 7013 7014 static int bpf_noop_prologue(struct bpf_insn *insn_buf, bool direct_write, 7015 const struct bpf_prog *prog) 7016 { 7017 /* Neither direct read nor direct write requires any preliminary 7018 * action. 7019 */ 7020 return 0; 7021 } 7022 7023 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write, 7024 const struct bpf_prog *prog, int drop_verdict) 7025 { 7026 struct bpf_insn *insn = insn_buf; 7027 7028 if (!direct_write) 7029 return 0; 7030 7031 /* if (!skb->cloned) 7032 * goto start; 7033 * 7034 * (Fast-path, otherwise approximation that we might be 7035 * a clone, do the rest in helper.) 7036 */ 7037 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET()); 7038 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK); 7039 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7); 7040 7041 /* ret = bpf_skb_pull_data(skb, 0); */ 7042 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1); 7043 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2); 7044 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, 7045 BPF_FUNC_skb_pull_data); 7046 /* if (!ret) 7047 * goto restore; 7048 * return TC_ACT_SHOT; 7049 */ 7050 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2); 7051 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict); 7052 *insn++ = BPF_EXIT_INSN(); 7053 7054 /* restore: */ 7055 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6); 7056 /* start: */ 7057 *insn++ = prog->insnsi[0]; 7058 7059 return insn - insn_buf; 7060 } 7061 7062 static int bpf_gen_ld_abs(const struct bpf_insn *orig, 7063 struct bpf_insn *insn_buf) 7064 { 7065 bool indirect = BPF_MODE(orig->code) == BPF_IND; 7066 struct bpf_insn *insn = insn_buf; 7067 7068 /* We're guaranteed here that CTX is in R6. */ 7069 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX); 7070 if (!indirect) { 7071 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm); 7072 } else { 7073 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg); 7074 if (orig->imm) 7075 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm); 7076 } 7077 7078 switch (BPF_SIZE(orig->code)) { 7079 case BPF_B: 7080 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache); 7081 break; 7082 case BPF_H: 7083 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache); 7084 break; 7085 case BPF_W: 7086 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache); 7087 break; 7088 } 7089 7090 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2); 7091 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0); 7092 *insn++ = BPF_EXIT_INSN(); 7093 7094 return insn - insn_buf; 7095 } 7096 7097 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write, 7098 const struct bpf_prog *prog) 7099 { 7100 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT); 7101 } 7102 7103 static bool tc_cls_act_is_valid_access(int off, int size, 7104 enum bpf_access_type type, 7105 const struct bpf_prog *prog, 7106 struct bpf_insn_access_aux *info) 7107 { 7108 if (type == BPF_WRITE) { 7109 switch (off) { 7110 case bpf_ctx_range(struct __sk_buff, mark): 7111 case bpf_ctx_range(struct __sk_buff, tc_index): 7112 case bpf_ctx_range(struct __sk_buff, priority): 7113 case bpf_ctx_range(struct __sk_buff, tc_classid): 7114 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]): 7115 case bpf_ctx_range(struct __sk_buff, tstamp): 7116 case bpf_ctx_range(struct __sk_buff, queue_mapping): 7117 break; 7118 default: 7119 return false; 7120 } 7121 } 7122 7123 switch (off) { 7124 case bpf_ctx_range(struct __sk_buff, data): 7125 info->reg_type = PTR_TO_PACKET; 7126 break; 7127 case bpf_ctx_range(struct __sk_buff, data_meta): 7128 info->reg_type = PTR_TO_PACKET_META; 7129 break; 7130 case bpf_ctx_range(struct __sk_buff, data_end): 7131 info->reg_type = PTR_TO_PACKET_END; 7132 break; 7133 case bpf_ctx_range_till(struct __sk_buff, family, local_port): 7134 return false; 7135 } 7136 7137 return bpf_skb_is_valid_access(off, size, type, prog, info); 7138 } 7139 7140 static bool __is_valid_xdp_access(int off, int size) 7141 { 7142 if (off < 0 || off >= sizeof(struct xdp_md)) 7143 return false; 7144 if (off % size != 0) 7145 return false; 7146 if (size != sizeof(__u32)) 7147 return false; 7148 7149 return true; 7150 } 7151 7152 static bool xdp_is_valid_access(int off, int size, 7153 enum bpf_access_type type, 7154 const struct bpf_prog *prog, 7155 struct bpf_insn_access_aux *info) 7156 { 7157 if (prog->expected_attach_type != BPF_XDP_DEVMAP) { 7158 switch (off) { 7159 case offsetof(struct xdp_md, egress_ifindex): 7160 return false; 7161 } 7162 } 7163 7164 if (type == BPF_WRITE) { 7165 if (bpf_prog_is_dev_bound(prog->aux)) { 7166 switch (off) { 7167 case offsetof(struct xdp_md, rx_queue_index): 7168 return __is_valid_xdp_access(off, size); 7169 } 7170 } 7171 return false; 7172 } 7173 7174 switch (off) { 7175 case offsetof(struct xdp_md, data): 7176 info->reg_type = PTR_TO_PACKET; 7177 break; 7178 case offsetof(struct xdp_md, data_meta): 7179 info->reg_type = PTR_TO_PACKET_META; 7180 break; 7181 case offsetof(struct xdp_md, data_end): 7182 info->reg_type = PTR_TO_PACKET_END; 7183 break; 7184 } 7185 7186 return __is_valid_xdp_access(off, size); 7187 } 7188 7189 void bpf_warn_invalid_xdp_action(u32 act) 7190 { 7191 const u32 act_max = XDP_REDIRECT; 7192 7193 WARN_ONCE(1, "%s XDP return value %u, expect packet loss!\n", 7194 act > act_max ? "Illegal" : "Driver unsupported", 7195 act); 7196 } 7197 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action); 7198 7199 static bool sock_addr_is_valid_access(int off, int size, 7200 enum bpf_access_type type, 7201 const struct bpf_prog *prog, 7202 struct bpf_insn_access_aux *info) 7203 { 7204 const int size_default = sizeof(__u32); 7205 7206 if (off < 0 || off >= sizeof(struct bpf_sock_addr)) 7207 return false; 7208 if (off % size != 0) 7209 return false; 7210 7211 /* Disallow access to IPv6 fields from IPv4 contex and vise 7212 * versa. 7213 */ 7214 switch (off) { 7215 case bpf_ctx_range(struct bpf_sock_addr, user_ip4): 7216 switch (prog->expected_attach_type) { 7217 case BPF_CGROUP_INET4_BIND: 7218 case BPF_CGROUP_INET4_CONNECT: 7219 case BPF_CGROUP_INET4_GETPEERNAME: 7220 case BPF_CGROUP_INET4_GETSOCKNAME: 7221 case BPF_CGROUP_UDP4_SENDMSG: 7222 case BPF_CGROUP_UDP4_RECVMSG: 7223 break; 7224 default: 7225 return false; 7226 } 7227 break; 7228 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]): 7229 switch (prog->expected_attach_type) { 7230 case BPF_CGROUP_INET6_BIND: 7231 case BPF_CGROUP_INET6_CONNECT: 7232 case BPF_CGROUP_INET6_GETPEERNAME: 7233 case BPF_CGROUP_INET6_GETSOCKNAME: 7234 case BPF_CGROUP_UDP6_SENDMSG: 7235 case BPF_CGROUP_UDP6_RECVMSG: 7236 break; 7237 default: 7238 return false; 7239 } 7240 break; 7241 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4): 7242 switch (prog->expected_attach_type) { 7243 case BPF_CGROUP_UDP4_SENDMSG: 7244 break; 7245 default: 7246 return false; 7247 } 7248 break; 7249 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0], 7250 msg_src_ip6[3]): 7251 switch (prog->expected_attach_type) { 7252 case BPF_CGROUP_UDP6_SENDMSG: 7253 break; 7254 default: 7255 return false; 7256 } 7257 break; 7258 } 7259 7260 switch (off) { 7261 case bpf_ctx_range(struct bpf_sock_addr, user_ip4): 7262 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]): 7263 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4): 7264 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0], 7265 msg_src_ip6[3]): 7266 case bpf_ctx_range(struct bpf_sock_addr, user_port): 7267 if (type == BPF_READ) { 7268 bpf_ctx_record_field_size(info, size_default); 7269 7270 if (bpf_ctx_wide_access_ok(off, size, 7271 struct bpf_sock_addr, 7272 user_ip6)) 7273 return true; 7274 7275 if (bpf_ctx_wide_access_ok(off, size, 7276 struct bpf_sock_addr, 7277 msg_src_ip6)) 7278 return true; 7279 7280 if (!bpf_ctx_narrow_access_ok(off, size, size_default)) 7281 return false; 7282 } else { 7283 if (bpf_ctx_wide_access_ok(off, size, 7284 struct bpf_sock_addr, 7285 user_ip6)) 7286 return true; 7287 7288 if (bpf_ctx_wide_access_ok(off, size, 7289 struct bpf_sock_addr, 7290 msg_src_ip6)) 7291 return true; 7292 7293 if (size != size_default) 7294 return false; 7295 } 7296 break; 7297 case offsetof(struct bpf_sock_addr, sk): 7298 if (type != BPF_READ) 7299 return false; 7300 if (size != sizeof(__u64)) 7301 return false; 7302 info->reg_type = PTR_TO_SOCKET; 7303 break; 7304 default: 7305 if (type == BPF_READ) { 7306 if (size != size_default) 7307 return false; 7308 } else { 7309 return false; 7310 } 7311 } 7312 7313 return true; 7314 } 7315 7316 static bool sock_ops_is_valid_access(int off, int size, 7317 enum bpf_access_type type, 7318 const struct bpf_prog *prog, 7319 struct bpf_insn_access_aux *info) 7320 { 7321 const int size_default = sizeof(__u32); 7322 7323 if (off < 0 || off >= sizeof(struct bpf_sock_ops)) 7324 return false; 7325 7326 /* The verifier guarantees that size > 0. */ 7327 if (off % size != 0) 7328 return false; 7329 7330 if (type == BPF_WRITE) { 7331 switch (off) { 7332 case offsetof(struct bpf_sock_ops, reply): 7333 case offsetof(struct bpf_sock_ops, sk_txhash): 7334 if (size != size_default) 7335 return false; 7336 break; 7337 default: 7338 return false; 7339 } 7340 } else { 7341 switch (off) { 7342 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received, 7343 bytes_acked): 7344 if (size != sizeof(__u64)) 7345 return false; 7346 break; 7347 case offsetof(struct bpf_sock_ops, sk): 7348 if (size != sizeof(__u64)) 7349 return false; 7350 info->reg_type = PTR_TO_SOCKET_OR_NULL; 7351 break; 7352 default: 7353 if (size != size_default) 7354 return false; 7355 break; 7356 } 7357 } 7358 7359 return true; 7360 } 7361 7362 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write, 7363 const struct bpf_prog *prog) 7364 { 7365 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP); 7366 } 7367 7368 static bool sk_skb_is_valid_access(int off, int size, 7369 enum bpf_access_type type, 7370 const struct bpf_prog *prog, 7371 struct bpf_insn_access_aux *info) 7372 { 7373 switch (off) { 7374 case bpf_ctx_range(struct __sk_buff, tc_classid): 7375 case bpf_ctx_range(struct __sk_buff, data_meta): 7376 case bpf_ctx_range(struct __sk_buff, tstamp): 7377 case bpf_ctx_range(struct __sk_buff, wire_len): 7378 return false; 7379 } 7380 7381 if (type == BPF_WRITE) { 7382 switch (off) { 7383 case bpf_ctx_range(struct __sk_buff, tc_index): 7384 case bpf_ctx_range(struct __sk_buff, priority): 7385 break; 7386 default: 7387 return false; 7388 } 7389 } 7390 7391 switch (off) { 7392 case bpf_ctx_range(struct __sk_buff, mark): 7393 return false; 7394 case bpf_ctx_range(struct __sk_buff, data): 7395 info->reg_type = PTR_TO_PACKET; 7396 break; 7397 case bpf_ctx_range(struct __sk_buff, data_end): 7398 info->reg_type = PTR_TO_PACKET_END; 7399 break; 7400 } 7401 7402 return bpf_skb_is_valid_access(off, size, type, prog, info); 7403 } 7404 7405 static bool sk_msg_is_valid_access(int off, int size, 7406 enum bpf_access_type type, 7407 const struct bpf_prog *prog, 7408 struct bpf_insn_access_aux *info) 7409 { 7410 if (type == BPF_WRITE) 7411 return false; 7412 7413 if (off % size != 0) 7414 return false; 7415 7416 switch (off) { 7417 case offsetof(struct sk_msg_md, data): 7418 info->reg_type = PTR_TO_PACKET; 7419 if (size != sizeof(__u64)) 7420 return false; 7421 break; 7422 case offsetof(struct sk_msg_md, data_end): 7423 info->reg_type = PTR_TO_PACKET_END; 7424 if (size != sizeof(__u64)) 7425 return false; 7426 break; 7427 case offsetof(struct sk_msg_md, sk): 7428 if (size != sizeof(__u64)) 7429 return false; 7430 info->reg_type = PTR_TO_SOCKET; 7431 break; 7432 case bpf_ctx_range(struct sk_msg_md, family): 7433 case bpf_ctx_range(struct sk_msg_md, remote_ip4): 7434 case bpf_ctx_range(struct sk_msg_md, local_ip4): 7435 case bpf_ctx_range_till(struct sk_msg_md, remote_ip6[0], remote_ip6[3]): 7436 case bpf_ctx_range_till(struct sk_msg_md, local_ip6[0], local_ip6[3]): 7437 case bpf_ctx_range(struct sk_msg_md, remote_port): 7438 case bpf_ctx_range(struct sk_msg_md, local_port): 7439 case bpf_ctx_range(struct sk_msg_md, size): 7440 if (size != sizeof(__u32)) 7441 return false; 7442 break; 7443 default: 7444 return false; 7445 } 7446 return true; 7447 } 7448 7449 static bool flow_dissector_is_valid_access(int off, int size, 7450 enum bpf_access_type type, 7451 const struct bpf_prog *prog, 7452 struct bpf_insn_access_aux *info) 7453 { 7454 const int size_default = sizeof(__u32); 7455 7456 if (off < 0 || off >= sizeof(struct __sk_buff)) 7457 return false; 7458 7459 if (type == BPF_WRITE) 7460 return false; 7461 7462 switch (off) { 7463 case bpf_ctx_range(struct __sk_buff, data): 7464 if (size != size_default) 7465 return false; 7466 info->reg_type = PTR_TO_PACKET; 7467 return true; 7468 case bpf_ctx_range(struct __sk_buff, data_end): 7469 if (size != size_default) 7470 return false; 7471 info->reg_type = PTR_TO_PACKET_END; 7472 return true; 7473 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys): 7474 if (size != sizeof(__u64)) 7475 return false; 7476 info->reg_type = PTR_TO_FLOW_KEYS; 7477 return true; 7478 default: 7479 return false; 7480 } 7481 } 7482 7483 static u32 flow_dissector_convert_ctx_access(enum bpf_access_type type, 7484 const struct bpf_insn *si, 7485 struct bpf_insn *insn_buf, 7486 struct bpf_prog *prog, 7487 u32 *target_size) 7488 7489 { 7490 struct bpf_insn *insn = insn_buf; 7491 7492 switch (si->off) { 7493 case offsetof(struct __sk_buff, data): 7494 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data), 7495 si->dst_reg, si->src_reg, 7496 offsetof(struct bpf_flow_dissector, data)); 7497 break; 7498 7499 case offsetof(struct __sk_buff, data_end): 7500 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data_end), 7501 si->dst_reg, si->src_reg, 7502 offsetof(struct bpf_flow_dissector, data_end)); 7503 break; 7504 7505 case offsetof(struct __sk_buff, flow_keys): 7506 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, flow_keys), 7507 si->dst_reg, si->src_reg, 7508 offsetof(struct bpf_flow_dissector, flow_keys)); 7509 break; 7510 } 7511 7512 return insn - insn_buf; 7513 } 7514 7515 static struct bpf_insn *bpf_convert_shinfo_access(const struct bpf_insn *si, 7516 struct bpf_insn *insn) 7517 { 7518 /* si->dst_reg = skb_shinfo(SKB); */ 7519 #ifdef NET_SKBUFF_DATA_USES_OFFSET 7520 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end), 7521 BPF_REG_AX, si->src_reg, 7522 offsetof(struct sk_buff, end)); 7523 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, head), 7524 si->dst_reg, si->src_reg, 7525 offsetof(struct sk_buff, head)); 7526 *insn++ = BPF_ALU64_REG(BPF_ADD, si->dst_reg, BPF_REG_AX); 7527 #else 7528 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end), 7529 si->dst_reg, si->src_reg, 7530 offsetof(struct sk_buff, end)); 7531 #endif 7532 7533 return insn; 7534 } 7535 7536 static u32 bpf_convert_ctx_access(enum bpf_access_type type, 7537 const struct bpf_insn *si, 7538 struct bpf_insn *insn_buf, 7539 struct bpf_prog *prog, u32 *target_size) 7540 { 7541 struct bpf_insn *insn = insn_buf; 7542 int off; 7543 7544 switch (si->off) { 7545 case offsetof(struct __sk_buff, len): 7546 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 7547 bpf_target_off(struct sk_buff, len, 4, 7548 target_size)); 7549 break; 7550 7551 case offsetof(struct __sk_buff, protocol): 7552 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, 7553 bpf_target_off(struct sk_buff, protocol, 2, 7554 target_size)); 7555 break; 7556 7557 case offsetof(struct __sk_buff, vlan_proto): 7558 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, 7559 bpf_target_off(struct sk_buff, vlan_proto, 2, 7560 target_size)); 7561 break; 7562 7563 case offsetof(struct __sk_buff, priority): 7564 if (type == BPF_WRITE) 7565 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg, 7566 bpf_target_off(struct sk_buff, priority, 4, 7567 target_size)); 7568 else 7569 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 7570 bpf_target_off(struct sk_buff, priority, 4, 7571 target_size)); 7572 break; 7573 7574 case offsetof(struct __sk_buff, ingress_ifindex): 7575 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 7576 bpf_target_off(struct sk_buff, skb_iif, 4, 7577 target_size)); 7578 break; 7579 7580 case offsetof(struct __sk_buff, ifindex): 7581 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev), 7582 si->dst_reg, si->src_reg, 7583 offsetof(struct sk_buff, dev)); 7584 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1); 7585 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 7586 bpf_target_off(struct net_device, ifindex, 4, 7587 target_size)); 7588 break; 7589 7590 case offsetof(struct __sk_buff, hash): 7591 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 7592 bpf_target_off(struct sk_buff, hash, 4, 7593 target_size)); 7594 break; 7595 7596 case offsetof(struct __sk_buff, mark): 7597 if (type == BPF_WRITE) 7598 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg, 7599 bpf_target_off(struct sk_buff, mark, 4, 7600 target_size)); 7601 else 7602 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 7603 bpf_target_off(struct sk_buff, mark, 4, 7604 target_size)); 7605 break; 7606 7607 case offsetof(struct __sk_buff, pkt_type): 7608 *target_size = 1; 7609 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg, 7610 PKT_TYPE_OFFSET()); 7611 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX); 7612 #ifdef __BIG_ENDIAN_BITFIELD 7613 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5); 7614 #endif 7615 break; 7616 7617 case offsetof(struct __sk_buff, queue_mapping): 7618 if (type == BPF_WRITE) { 7619 *insn++ = BPF_JMP_IMM(BPF_JGE, si->src_reg, NO_QUEUE_MAPPING, 1); 7620 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg, 7621 bpf_target_off(struct sk_buff, 7622 queue_mapping, 7623 2, target_size)); 7624 } else { 7625 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, 7626 bpf_target_off(struct sk_buff, 7627 queue_mapping, 7628 2, target_size)); 7629 } 7630 break; 7631 7632 case offsetof(struct __sk_buff, vlan_present): 7633 *target_size = 1; 7634 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg, 7635 PKT_VLAN_PRESENT_OFFSET()); 7636 if (PKT_VLAN_PRESENT_BIT) 7637 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, PKT_VLAN_PRESENT_BIT); 7638 if (PKT_VLAN_PRESENT_BIT < 7) 7639 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1); 7640 break; 7641 7642 case offsetof(struct __sk_buff, vlan_tci): 7643 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, 7644 bpf_target_off(struct sk_buff, vlan_tci, 2, 7645 target_size)); 7646 break; 7647 7648 case offsetof(struct __sk_buff, cb[0]) ... 7649 offsetofend(struct __sk_buff, cb[4]) - 1: 7650 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, data) < 20); 7651 BUILD_BUG_ON((offsetof(struct sk_buff, cb) + 7652 offsetof(struct qdisc_skb_cb, data)) % 7653 sizeof(__u64)); 7654 7655 prog->cb_access = 1; 7656 off = si->off; 7657 off -= offsetof(struct __sk_buff, cb[0]); 7658 off += offsetof(struct sk_buff, cb); 7659 off += offsetof(struct qdisc_skb_cb, data); 7660 if (type == BPF_WRITE) 7661 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg, 7662 si->src_reg, off); 7663 else 7664 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg, 7665 si->src_reg, off); 7666 break; 7667 7668 case offsetof(struct __sk_buff, tc_classid): 7669 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, tc_classid) != 2); 7670 7671 off = si->off; 7672 off -= offsetof(struct __sk_buff, tc_classid); 7673 off += offsetof(struct sk_buff, cb); 7674 off += offsetof(struct qdisc_skb_cb, tc_classid); 7675 *target_size = 2; 7676 if (type == BPF_WRITE) 7677 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, 7678 si->src_reg, off); 7679 else 7680 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, 7681 si->src_reg, off); 7682 break; 7683 7684 case offsetof(struct __sk_buff, data): 7685 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data), 7686 si->dst_reg, si->src_reg, 7687 offsetof(struct sk_buff, data)); 7688 break; 7689 7690 case offsetof(struct __sk_buff, data_meta): 7691 off = si->off; 7692 off -= offsetof(struct __sk_buff, data_meta); 7693 off += offsetof(struct sk_buff, cb); 7694 off += offsetof(struct bpf_skb_data_end, data_meta); 7695 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, 7696 si->src_reg, off); 7697 break; 7698 7699 case offsetof(struct __sk_buff, data_end): 7700 off = si->off; 7701 off -= offsetof(struct __sk_buff, data_end); 7702 off += offsetof(struct sk_buff, cb); 7703 off += offsetof(struct bpf_skb_data_end, data_end); 7704 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, 7705 si->src_reg, off); 7706 break; 7707 7708 case offsetof(struct __sk_buff, tc_index): 7709 #ifdef CONFIG_NET_SCHED 7710 if (type == BPF_WRITE) 7711 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg, 7712 bpf_target_off(struct sk_buff, tc_index, 2, 7713 target_size)); 7714 else 7715 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, 7716 bpf_target_off(struct sk_buff, tc_index, 2, 7717 target_size)); 7718 #else 7719 *target_size = 2; 7720 if (type == BPF_WRITE) 7721 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg); 7722 else 7723 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0); 7724 #endif 7725 break; 7726 7727 case offsetof(struct __sk_buff, napi_id): 7728 #if defined(CONFIG_NET_RX_BUSY_POLL) 7729 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 7730 bpf_target_off(struct sk_buff, napi_id, 4, 7731 target_size)); 7732 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1); 7733 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0); 7734 #else 7735 *target_size = 4; 7736 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0); 7737 #endif 7738 break; 7739 case offsetof(struct __sk_buff, family): 7740 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2); 7741 7742 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), 7743 si->dst_reg, si->src_reg, 7744 offsetof(struct sk_buff, sk)); 7745 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 7746 bpf_target_off(struct sock_common, 7747 skc_family, 7748 2, target_size)); 7749 break; 7750 case offsetof(struct __sk_buff, remote_ip4): 7751 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4); 7752 7753 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), 7754 si->dst_reg, si->src_reg, 7755 offsetof(struct sk_buff, sk)); 7756 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 7757 bpf_target_off(struct sock_common, 7758 skc_daddr, 7759 4, target_size)); 7760 break; 7761 case offsetof(struct __sk_buff, local_ip4): 7762 BUILD_BUG_ON(sizeof_field(struct sock_common, 7763 skc_rcv_saddr) != 4); 7764 7765 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), 7766 si->dst_reg, si->src_reg, 7767 offsetof(struct sk_buff, sk)); 7768 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 7769 bpf_target_off(struct sock_common, 7770 skc_rcv_saddr, 7771 4, target_size)); 7772 break; 7773 case offsetof(struct __sk_buff, remote_ip6[0]) ... 7774 offsetof(struct __sk_buff, remote_ip6[3]): 7775 #if IS_ENABLED(CONFIG_IPV6) 7776 BUILD_BUG_ON(sizeof_field(struct sock_common, 7777 skc_v6_daddr.s6_addr32[0]) != 4); 7778 7779 off = si->off; 7780 off -= offsetof(struct __sk_buff, remote_ip6[0]); 7781 7782 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), 7783 si->dst_reg, si->src_reg, 7784 offsetof(struct sk_buff, sk)); 7785 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 7786 offsetof(struct sock_common, 7787 skc_v6_daddr.s6_addr32[0]) + 7788 off); 7789 #else 7790 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 7791 #endif 7792 break; 7793 case offsetof(struct __sk_buff, local_ip6[0]) ... 7794 offsetof(struct __sk_buff, local_ip6[3]): 7795 #if IS_ENABLED(CONFIG_IPV6) 7796 BUILD_BUG_ON(sizeof_field(struct sock_common, 7797 skc_v6_rcv_saddr.s6_addr32[0]) != 4); 7798 7799 off = si->off; 7800 off -= offsetof(struct __sk_buff, local_ip6[0]); 7801 7802 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), 7803 si->dst_reg, si->src_reg, 7804 offsetof(struct sk_buff, sk)); 7805 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 7806 offsetof(struct sock_common, 7807 skc_v6_rcv_saddr.s6_addr32[0]) + 7808 off); 7809 #else 7810 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 7811 #endif 7812 break; 7813 7814 case offsetof(struct __sk_buff, remote_port): 7815 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2); 7816 7817 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), 7818 si->dst_reg, si->src_reg, 7819 offsetof(struct sk_buff, sk)); 7820 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 7821 bpf_target_off(struct sock_common, 7822 skc_dport, 7823 2, target_size)); 7824 #ifndef __BIG_ENDIAN_BITFIELD 7825 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16); 7826 #endif 7827 break; 7828 7829 case offsetof(struct __sk_buff, local_port): 7830 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2); 7831 7832 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), 7833 si->dst_reg, si->src_reg, 7834 offsetof(struct sk_buff, sk)); 7835 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 7836 bpf_target_off(struct sock_common, 7837 skc_num, 2, target_size)); 7838 break; 7839 7840 case offsetof(struct __sk_buff, tstamp): 7841 BUILD_BUG_ON(sizeof_field(struct sk_buff, tstamp) != 8); 7842 7843 if (type == BPF_WRITE) 7844 *insn++ = BPF_STX_MEM(BPF_DW, 7845 si->dst_reg, si->src_reg, 7846 bpf_target_off(struct sk_buff, 7847 tstamp, 8, 7848 target_size)); 7849 else 7850 *insn++ = BPF_LDX_MEM(BPF_DW, 7851 si->dst_reg, si->src_reg, 7852 bpf_target_off(struct sk_buff, 7853 tstamp, 8, 7854 target_size)); 7855 break; 7856 7857 case offsetof(struct __sk_buff, gso_segs): 7858 insn = bpf_convert_shinfo_access(si, insn); 7859 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_segs), 7860 si->dst_reg, si->dst_reg, 7861 bpf_target_off(struct skb_shared_info, 7862 gso_segs, 2, 7863 target_size)); 7864 break; 7865 case offsetof(struct __sk_buff, gso_size): 7866 insn = bpf_convert_shinfo_access(si, insn); 7867 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_size), 7868 si->dst_reg, si->dst_reg, 7869 bpf_target_off(struct skb_shared_info, 7870 gso_size, 2, 7871 target_size)); 7872 break; 7873 case offsetof(struct __sk_buff, wire_len): 7874 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, pkt_len) != 4); 7875 7876 off = si->off; 7877 off -= offsetof(struct __sk_buff, wire_len); 7878 off += offsetof(struct sk_buff, cb); 7879 off += offsetof(struct qdisc_skb_cb, pkt_len); 7880 *target_size = 4; 7881 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, off); 7882 break; 7883 7884 case offsetof(struct __sk_buff, sk): 7885 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), 7886 si->dst_reg, si->src_reg, 7887 offsetof(struct sk_buff, sk)); 7888 break; 7889 } 7890 7891 return insn - insn_buf; 7892 } 7893 7894 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type, 7895 const struct bpf_insn *si, 7896 struct bpf_insn *insn_buf, 7897 struct bpf_prog *prog, u32 *target_size) 7898 { 7899 struct bpf_insn *insn = insn_buf; 7900 int off; 7901 7902 switch (si->off) { 7903 case offsetof(struct bpf_sock, bound_dev_if): 7904 BUILD_BUG_ON(sizeof_field(struct sock, sk_bound_dev_if) != 4); 7905 7906 if (type == BPF_WRITE) 7907 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg, 7908 offsetof(struct sock, sk_bound_dev_if)); 7909 else 7910 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 7911 offsetof(struct sock, sk_bound_dev_if)); 7912 break; 7913 7914 case offsetof(struct bpf_sock, mark): 7915 BUILD_BUG_ON(sizeof_field(struct sock, sk_mark) != 4); 7916 7917 if (type == BPF_WRITE) 7918 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg, 7919 offsetof(struct sock, sk_mark)); 7920 else 7921 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 7922 offsetof(struct sock, sk_mark)); 7923 break; 7924 7925 case offsetof(struct bpf_sock, priority): 7926 BUILD_BUG_ON(sizeof_field(struct sock, sk_priority) != 4); 7927 7928 if (type == BPF_WRITE) 7929 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg, 7930 offsetof(struct sock, sk_priority)); 7931 else 7932 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 7933 offsetof(struct sock, sk_priority)); 7934 break; 7935 7936 case offsetof(struct bpf_sock, family): 7937 *insn++ = BPF_LDX_MEM( 7938 BPF_FIELD_SIZEOF(struct sock_common, skc_family), 7939 si->dst_reg, si->src_reg, 7940 bpf_target_off(struct sock_common, 7941 skc_family, 7942 sizeof_field(struct sock_common, 7943 skc_family), 7944 target_size)); 7945 break; 7946 7947 case offsetof(struct bpf_sock, type): 7948 *insn++ = BPF_LDX_MEM( 7949 BPF_FIELD_SIZEOF(struct sock, sk_type), 7950 si->dst_reg, si->src_reg, 7951 bpf_target_off(struct sock, sk_type, 7952 sizeof_field(struct sock, sk_type), 7953 target_size)); 7954 break; 7955 7956 case offsetof(struct bpf_sock, protocol): 7957 *insn++ = BPF_LDX_MEM( 7958 BPF_FIELD_SIZEOF(struct sock, sk_protocol), 7959 si->dst_reg, si->src_reg, 7960 bpf_target_off(struct sock, sk_protocol, 7961 sizeof_field(struct sock, sk_protocol), 7962 target_size)); 7963 break; 7964 7965 case offsetof(struct bpf_sock, src_ip4): 7966 *insn++ = BPF_LDX_MEM( 7967 BPF_SIZE(si->code), si->dst_reg, si->src_reg, 7968 bpf_target_off(struct sock_common, skc_rcv_saddr, 7969 sizeof_field(struct sock_common, 7970 skc_rcv_saddr), 7971 target_size)); 7972 break; 7973 7974 case offsetof(struct bpf_sock, dst_ip4): 7975 *insn++ = BPF_LDX_MEM( 7976 BPF_SIZE(si->code), si->dst_reg, si->src_reg, 7977 bpf_target_off(struct sock_common, skc_daddr, 7978 sizeof_field(struct sock_common, 7979 skc_daddr), 7980 target_size)); 7981 break; 7982 7983 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]): 7984 #if IS_ENABLED(CONFIG_IPV6) 7985 off = si->off; 7986 off -= offsetof(struct bpf_sock, src_ip6[0]); 7987 *insn++ = BPF_LDX_MEM( 7988 BPF_SIZE(si->code), si->dst_reg, si->src_reg, 7989 bpf_target_off( 7990 struct sock_common, 7991 skc_v6_rcv_saddr.s6_addr32[0], 7992 sizeof_field(struct sock_common, 7993 skc_v6_rcv_saddr.s6_addr32[0]), 7994 target_size) + off); 7995 #else 7996 (void)off; 7997 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 7998 #endif 7999 break; 8000 8001 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]): 8002 #if IS_ENABLED(CONFIG_IPV6) 8003 off = si->off; 8004 off -= offsetof(struct bpf_sock, dst_ip6[0]); 8005 *insn++ = BPF_LDX_MEM( 8006 BPF_SIZE(si->code), si->dst_reg, si->src_reg, 8007 bpf_target_off(struct sock_common, 8008 skc_v6_daddr.s6_addr32[0], 8009 sizeof_field(struct sock_common, 8010 skc_v6_daddr.s6_addr32[0]), 8011 target_size) + off); 8012 #else 8013 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 8014 *target_size = 4; 8015 #endif 8016 break; 8017 8018 case offsetof(struct bpf_sock, src_port): 8019 *insn++ = BPF_LDX_MEM( 8020 BPF_FIELD_SIZEOF(struct sock_common, skc_num), 8021 si->dst_reg, si->src_reg, 8022 bpf_target_off(struct sock_common, skc_num, 8023 sizeof_field(struct sock_common, 8024 skc_num), 8025 target_size)); 8026 break; 8027 8028 case offsetof(struct bpf_sock, dst_port): 8029 *insn++ = BPF_LDX_MEM( 8030 BPF_FIELD_SIZEOF(struct sock_common, skc_dport), 8031 si->dst_reg, si->src_reg, 8032 bpf_target_off(struct sock_common, skc_dport, 8033 sizeof_field(struct sock_common, 8034 skc_dport), 8035 target_size)); 8036 break; 8037 8038 case offsetof(struct bpf_sock, state): 8039 *insn++ = BPF_LDX_MEM( 8040 BPF_FIELD_SIZEOF(struct sock_common, skc_state), 8041 si->dst_reg, si->src_reg, 8042 bpf_target_off(struct sock_common, skc_state, 8043 sizeof_field(struct sock_common, 8044 skc_state), 8045 target_size)); 8046 break; 8047 case offsetof(struct bpf_sock, rx_queue_mapping): 8048 #ifdef CONFIG_XPS 8049 *insn++ = BPF_LDX_MEM( 8050 BPF_FIELD_SIZEOF(struct sock, sk_rx_queue_mapping), 8051 si->dst_reg, si->src_reg, 8052 bpf_target_off(struct sock, sk_rx_queue_mapping, 8053 sizeof_field(struct sock, 8054 sk_rx_queue_mapping), 8055 target_size)); 8056 *insn++ = BPF_JMP_IMM(BPF_JNE, si->dst_reg, NO_QUEUE_MAPPING, 8057 1); 8058 *insn++ = BPF_MOV64_IMM(si->dst_reg, -1); 8059 #else 8060 *insn++ = BPF_MOV64_IMM(si->dst_reg, -1); 8061 *target_size = 2; 8062 #endif 8063 break; 8064 } 8065 8066 return insn - insn_buf; 8067 } 8068 8069 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type, 8070 const struct bpf_insn *si, 8071 struct bpf_insn *insn_buf, 8072 struct bpf_prog *prog, u32 *target_size) 8073 { 8074 struct bpf_insn *insn = insn_buf; 8075 8076 switch (si->off) { 8077 case offsetof(struct __sk_buff, ifindex): 8078 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev), 8079 si->dst_reg, si->src_reg, 8080 offsetof(struct sk_buff, dev)); 8081 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 8082 bpf_target_off(struct net_device, ifindex, 4, 8083 target_size)); 8084 break; 8085 default: 8086 return bpf_convert_ctx_access(type, si, insn_buf, prog, 8087 target_size); 8088 } 8089 8090 return insn - insn_buf; 8091 } 8092 8093 static u32 xdp_convert_ctx_access(enum bpf_access_type type, 8094 const struct bpf_insn *si, 8095 struct bpf_insn *insn_buf, 8096 struct bpf_prog *prog, u32 *target_size) 8097 { 8098 struct bpf_insn *insn = insn_buf; 8099 8100 switch (si->off) { 8101 case offsetof(struct xdp_md, data): 8102 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data), 8103 si->dst_reg, si->src_reg, 8104 offsetof(struct xdp_buff, data)); 8105 break; 8106 case offsetof(struct xdp_md, data_meta): 8107 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta), 8108 si->dst_reg, si->src_reg, 8109 offsetof(struct xdp_buff, data_meta)); 8110 break; 8111 case offsetof(struct xdp_md, data_end): 8112 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end), 8113 si->dst_reg, si->src_reg, 8114 offsetof(struct xdp_buff, data_end)); 8115 break; 8116 case offsetof(struct xdp_md, ingress_ifindex): 8117 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq), 8118 si->dst_reg, si->src_reg, 8119 offsetof(struct xdp_buff, rxq)); 8120 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev), 8121 si->dst_reg, si->dst_reg, 8122 offsetof(struct xdp_rxq_info, dev)); 8123 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 8124 offsetof(struct net_device, ifindex)); 8125 break; 8126 case offsetof(struct xdp_md, rx_queue_index): 8127 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq), 8128 si->dst_reg, si->src_reg, 8129 offsetof(struct xdp_buff, rxq)); 8130 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 8131 offsetof(struct xdp_rxq_info, 8132 queue_index)); 8133 break; 8134 case offsetof(struct xdp_md, egress_ifindex): 8135 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, txq), 8136 si->dst_reg, si->src_reg, 8137 offsetof(struct xdp_buff, txq)); 8138 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_txq_info, dev), 8139 si->dst_reg, si->dst_reg, 8140 offsetof(struct xdp_txq_info, dev)); 8141 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 8142 offsetof(struct net_device, ifindex)); 8143 break; 8144 } 8145 8146 return insn - insn_buf; 8147 } 8148 8149 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of 8150 * context Structure, F is Field in context structure that contains a pointer 8151 * to Nested Structure of type NS that has the field NF. 8152 * 8153 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make 8154 * sure that SIZE is not greater than actual size of S.F.NF. 8155 * 8156 * If offset OFF is provided, the load happens from that offset relative to 8157 * offset of NF. 8158 */ 8159 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \ 8160 do { \ 8161 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \ 8162 si->src_reg, offsetof(S, F)); \ 8163 *insn++ = BPF_LDX_MEM( \ 8164 SIZE, si->dst_reg, si->dst_reg, \ 8165 bpf_target_off(NS, NF, sizeof_field(NS, NF), \ 8166 target_size) \ 8167 + OFF); \ 8168 } while (0) 8169 8170 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \ 8171 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \ 8172 BPF_FIELD_SIZEOF(NS, NF), 0) 8173 8174 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to 8175 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation. 8176 * 8177 * In addition it uses Temporary Field TF (member of struct S) as the 3rd 8178 * "register" since two registers available in convert_ctx_access are not 8179 * enough: we can't override neither SRC, since it contains value to store, nor 8180 * DST since it contains pointer to context that may be used by later 8181 * instructions. But we need a temporary place to save pointer to nested 8182 * structure whose field we want to store to. 8183 */ 8184 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, OFF, TF) \ 8185 do { \ 8186 int tmp_reg = BPF_REG_9; \ 8187 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \ 8188 --tmp_reg; \ 8189 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \ 8190 --tmp_reg; \ 8191 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \ 8192 offsetof(S, TF)); \ 8193 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \ 8194 si->dst_reg, offsetof(S, F)); \ 8195 *insn++ = BPF_STX_MEM(SIZE, tmp_reg, si->src_reg, \ 8196 bpf_target_off(NS, NF, sizeof_field(NS, NF), \ 8197 target_size) \ 8198 + OFF); \ 8199 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \ 8200 offsetof(S, TF)); \ 8201 } while (0) 8202 8203 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \ 8204 TF) \ 8205 do { \ 8206 if (type == BPF_WRITE) { \ 8207 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, \ 8208 OFF, TF); \ 8209 } else { \ 8210 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \ 8211 S, NS, F, NF, SIZE, OFF); \ 8212 } \ 8213 } while (0) 8214 8215 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \ 8216 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \ 8217 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF) 8218 8219 static u32 sock_addr_convert_ctx_access(enum bpf_access_type type, 8220 const struct bpf_insn *si, 8221 struct bpf_insn *insn_buf, 8222 struct bpf_prog *prog, u32 *target_size) 8223 { 8224 int off, port_size = sizeof_field(struct sockaddr_in6, sin6_port); 8225 struct bpf_insn *insn = insn_buf; 8226 8227 switch (si->off) { 8228 case offsetof(struct bpf_sock_addr, user_family): 8229 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern, 8230 struct sockaddr, uaddr, sa_family); 8231 break; 8232 8233 case offsetof(struct bpf_sock_addr, user_ip4): 8234 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( 8235 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr, 8236 sin_addr, BPF_SIZE(si->code), 0, tmp_reg); 8237 break; 8238 8239 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]): 8240 off = si->off; 8241 off -= offsetof(struct bpf_sock_addr, user_ip6[0]); 8242 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( 8243 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr, 8244 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off, 8245 tmp_reg); 8246 break; 8247 8248 case offsetof(struct bpf_sock_addr, user_port): 8249 /* To get port we need to know sa_family first and then treat 8250 * sockaddr as either sockaddr_in or sockaddr_in6. 8251 * Though we can simplify since port field has same offset and 8252 * size in both structures. 8253 * Here we check this invariant and use just one of the 8254 * structures if it's true. 8255 */ 8256 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) != 8257 offsetof(struct sockaddr_in6, sin6_port)); 8258 BUILD_BUG_ON(sizeof_field(struct sockaddr_in, sin_port) != 8259 sizeof_field(struct sockaddr_in6, sin6_port)); 8260 /* Account for sin6_port being smaller than user_port. */ 8261 port_size = min(port_size, BPF_LDST_BYTES(si)); 8262 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( 8263 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr, 8264 sin6_port, bytes_to_bpf_size(port_size), 0, tmp_reg); 8265 break; 8266 8267 case offsetof(struct bpf_sock_addr, family): 8268 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern, 8269 struct sock, sk, sk_family); 8270 break; 8271 8272 case offsetof(struct bpf_sock_addr, type): 8273 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern, 8274 struct sock, sk, sk_type); 8275 break; 8276 8277 case offsetof(struct bpf_sock_addr, protocol): 8278 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern, 8279 struct sock, sk, sk_protocol); 8280 break; 8281 8282 case offsetof(struct bpf_sock_addr, msg_src_ip4): 8283 /* Treat t_ctx as struct in_addr for msg_src_ip4. */ 8284 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( 8285 struct bpf_sock_addr_kern, struct in_addr, t_ctx, 8286 s_addr, BPF_SIZE(si->code), 0, tmp_reg); 8287 break; 8288 8289 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0], 8290 msg_src_ip6[3]): 8291 off = si->off; 8292 off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]); 8293 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */ 8294 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( 8295 struct bpf_sock_addr_kern, struct in6_addr, t_ctx, 8296 s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg); 8297 break; 8298 case offsetof(struct bpf_sock_addr, sk): 8299 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_addr_kern, sk), 8300 si->dst_reg, si->src_reg, 8301 offsetof(struct bpf_sock_addr_kern, sk)); 8302 break; 8303 } 8304 8305 return insn - insn_buf; 8306 } 8307 8308 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type, 8309 const struct bpf_insn *si, 8310 struct bpf_insn *insn_buf, 8311 struct bpf_prog *prog, 8312 u32 *target_size) 8313 { 8314 struct bpf_insn *insn = insn_buf; 8315 int off; 8316 8317 /* Helper macro for adding read access to tcp_sock or sock fields. */ 8318 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \ 8319 do { \ 8320 int fullsock_reg = si->dst_reg, reg = BPF_REG_9, jmp = 2; \ 8321 BUILD_BUG_ON(sizeof_field(OBJ, OBJ_FIELD) > \ 8322 sizeof_field(struct bpf_sock_ops, BPF_FIELD)); \ 8323 if (si->dst_reg == reg || si->src_reg == reg) \ 8324 reg--; \ 8325 if (si->dst_reg == reg || si->src_reg == reg) \ 8326 reg--; \ 8327 if (si->dst_reg == si->src_reg) { \ 8328 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, \ 8329 offsetof(struct bpf_sock_ops_kern, \ 8330 temp)); \ 8331 fullsock_reg = reg; \ 8332 jmp += 2; \ 8333 } \ 8334 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ 8335 struct bpf_sock_ops_kern, \ 8336 is_fullsock), \ 8337 fullsock_reg, si->src_reg, \ 8338 offsetof(struct bpf_sock_ops_kern, \ 8339 is_fullsock)); \ 8340 *insn++ = BPF_JMP_IMM(BPF_JEQ, fullsock_reg, 0, jmp); \ 8341 if (si->dst_reg == si->src_reg) \ 8342 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \ 8343 offsetof(struct bpf_sock_ops_kern, \ 8344 temp)); \ 8345 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ 8346 struct bpf_sock_ops_kern, sk),\ 8347 si->dst_reg, si->src_reg, \ 8348 offsetof(struct bpf_sock_ops_kern, sk));\ 8349 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \ 8350 OBJ_FIELD), \ 8351 si->dst_reg, si->dst_reg, \ 8352 offsetof(OBJ, OBJ_FIELD)); \ 8353 if (si->dst_reg == si->src_reg) { \ 8354 *insn++ = BPF_JMP_A(1); \ 8355 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \ 8356 offsetof(struct bpf_sock_ops_kern, \ 8357 temp)); \ 8358 } \ 8359 } while (0) 8360 8361 #define SOCK_OPS_GET_SK() \ 8362 do { \ 8363 int fullsock_reg = si->dst_reg, reg = BPF_REG_9, jmp = 1; \ 8364 if (si->dst_reg == reg || si->src_reg == reg) \ 8365 reg--; \ 8366 if (si->dst_reg == reg || si->src_reg == reg) \ 8367 reg--; \ 8368 if (si->dst_reg == si->src_reg) { \ 8369 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, \ 8370 offsetof(struct bpf_sock_ops_kern, \ 8371 temp)); \ 8372 fullsock_reg = reg; \ 8373 jmp += 2; \ 8374 } \ 8375 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ 8376 struct bpf_sock_ops_kern, \ 8377 is_fullsock), \ 8378 fullsock_reg, si->src_reg, \ 8379 offsetof(struct bpf_sock_ops_kern, \ 8380 is_fullsock)); \ 8381 *insn++ = BPF_JMP_IMM(BPF_JEQ, fullsock_reg, 0, jmp); \ 8382 if (si->dst_reg == si->src_reg) \ 8383 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \ 8384 offsetof(struct bpf_sock_ops_kern, \ 8385 temp)); \ 8386 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ 8387 struct bpf_sock_ops_kern, sk),\ 8388 si->dst_reg, si->src_reg, \ 8389 offsetof(struct bpf_sock_ops_kern, sk));\ 8390 if (si->dst_reg == si->src_reg) { \ 8391 *insn++ = BPF_JMP_A(1); \ 8392 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \ 8393 offsetof(struct bpf_sock_ops_kern, \ 8394 temp)); \ 8395 } \ 8396 } while (0) 8397 8398 #define SOCK_OPS_GET_TCP_SOCK_FIELD(FIELD) \ 8399 SOCK_OPS_GET_FIELD(FIELD, FIELD, struct tcp_sock) 8400 8401 /* Helper macro for adding write access to tcp_sock or sock fields. 8402 * The macro is called with two registers, dst_reg which contains a pointer 8403 * to ctx (context) and src_reg which contains the value that should be 8404 * stored. However, we need an additional register since we cannot overwrite 8405 * dst_reg because it may be used later in the program. 8406 * Instead we "borrow" one of the other register. We first save its value 8407 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore 8408 * it at the end of the macro. 8409 */ 8410 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \ 8411 do { \ 8412 int reg = BPF_REG_9; \ 8413 BUILD_BUG_ON(sizeof_field(OBJ, OBJ_FIELD) > \ 8414 sizeof_field(struct bpf_sock_ops, BPF_FIELD)); \ 8415 if (si->dst_reg == reg || si->src_reg == reg) \ 8416 reg--; \ 8417 if (si->dst_reg == reg || si->src_reg == reg) \ 8418 reg--; \ 8419 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \ 8420 offsetof(struct bpf_sock_ops_kern, \ 8421 temp)); \ 8422 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ 8423 struct bpf_sock_ops_kern, \ 8424 is_fullsock), \ 8425 reg, si->dst_reg, \ 8426 offsetof(struct bpf_sock_ops_kern, \ 8427 is_fullsock)); \ 8428 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \ 8429 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ 8430 struct bpf_sock_ops_kern, sk),\ 8431 reg, si->dst_reg, \ 8432 offsetof(struct bpf_sock_ops_kern, sk));\ 8433 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \ 8434 reg, si->src_reg, \ 8435 offsetof(OBJ, OBJ_FIELD)); \ 8436 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \ 8437 offsetof(struct bpf_sock_ops_kern, \ 8438 temp)); \ 8439 } while (0) 8440 8441 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \ 8442 do { \ 8443 if (TYPE == BPF_WRITE) \ 8444 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \ 8445 else \ 8446 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \ 8447 } while (0) 8448 8449 if (insn > insn_buf) 8450 return insn - insn_buf; 8451 8452 switch (si->off) { 8453 case offsetof(struct bpf_sock_ops, op) ... 8454 offsetof(struct bpf_sock_ops, replylong[3]): 8455 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, op) != 8456 sizeof_field(struct bpf_sock_ops_kern, op)); 8457 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, reply) != 8458 sizeof_field(struct bpf_sock_ops_kern, reply)); 8459 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, replylong) != 8460 sizeof_field(struct bpf_sock_ops_kern, replylong)); 8461 off = si->off; 8462 off -= offsetof(struct bpf_sock_ops, op); 8463 off += offsetof(struct bpf_sock_ops_kern, op); 8464 if (type == BPF_WRITE) 8465 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg, 8466 off); 8467 else 8468 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 8469 off); 8470 break; 8471 8472 case offsetof(struct bpf_sock_ops, family): 8473 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2); 8474 8475 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 8476 struct bpf_sock_ops_kern, sk), 8477 si->dst_reg, si->src_reg, 8478 offsetof(struct bpf_sock_ops_kern, sk)); 8479 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 8480 offsetof(struct sock_common, skc_family)); 8481 break; 8482 8483 case offsetof(struct bpf_sock_ops, remote_ip4): 8484 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4); 8485 8486 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 8487 struct bpf_sock_ops_kern, sk), 8488 si->dst_reg, si->src_reg, 8489 offsetof(struct bpf_sock_ops_kern, sk)); 8490 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 8491 offsetof(struct sock_common, skc_daddr)); 8492 break; 8493 8494 case offsetof(struct bpf_sock_ops, local_ip4): 8495 BUILD_BUG_ON(sizeof_field(struct sock_common, 8496 skc_rcv_saddr) != 4); 8497 8498 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 8499 struct bpf_sock_ops_kern, sk), 8500 si->dst_reg, si->src_reg, 8501 offsetof(struct bpf_sock_ops_kern, sk)); 8502 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 8503 offsetof(struct sock_common, 8504 skc_rcv_saddr)); 8505 break; 8506 8507 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ... 8508 offsetof(struct bpf_sock_ops, remote_ip6[3]): 8509 #if IS_ENABLED(CONFIG_IPV6) 8510 BUILD_BUG_ON(sizeof_field(struct sock_common, 8511 skc_v6_daddr.s6_addr32[0]) != 4); 8512 8513 off = si->off; 8514 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]); 8515 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 8516 struct bpf_sock_ops_kern, sk), 8517 si->dst_reg, si->src_reg, 8518 offsetof(struct bpf_sock_ops_kern, sk)); 8519 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 8520 offsetof(struct sock_common, 8521 skc_v6_daddr.s6_addr32[0]) + 8522 off); 8523 #else 8524 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 8525 #endif 8526 break; 8527 8528 case offsetof(struct bpf_sock_ops, local_ip6[0]) ... 8529 offsetof(struct bpf_sock_ops, local_ip6[3]): 8530 #if IS_ENABLED(CONFIG_IPV6) 8531 BUILD_BUG_ON(sizeof_field(struct sock_common, 8532 skc_v6_rcv_saddr.s6_addr32[0]) != 4); 8533 8534 off = si->off; 8535 off -= offsetof(struct bpf_sock_ops, local_ip6[0]); 8536 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 8537 struct bpf_sock_ops_kern, sk), 8538 si->dst_reg, si->src_reg, 8539 offsetof(struct bpf_sock_ops_kern, sk)); 8540 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 8541 offsetof(struct sock_common, 8542 skc_v6_rcv_saddr.s6_addr32[0]) + 8543 off); 8544 #else 8545 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 8546 #endif 8547 break; 8548 8549 case offsetof(struct bpf_sock_ops, remote_port): 8550 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2); 8551 8552 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 8553 struct bpf_sock_ops_kern, sk), 8554 si->dst_reg, si->src_reg, 8555 offsetof(struct bpf_sock_ops_kern, sk)); 8556 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 8557 offsetof(struct sock_common, skc_dport)); 8558 #ifndef __BIG_ENDIAN_BITFIELD 8559 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16); 8560 #endif 8561 break; 8562 8563 case offsetof(struct bpf_sock_ops, local_port): 8564 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2); 8565 8566 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 8567 struct bpf_sock_ops_kern, sk), 8568 si->dst_reg, si->src_reg, 8569 offsetof(struct bpf_sock_ops_kern, sk)); 8570 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 8571 offsetof(struct sock_common, skc_num)); 8572 break; 8573 8574 case offsetof(struct bpf_sock_ops, is_fullsock): 8575 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 8576 struct bpf_sock_ops_kern, 8577 is_fullsock), 8578 si->dst_reg, si->src_reg, 8579 offsetof(struct bpf_sock_ops_kern, 8580 is_fullsock)); 8581 break; 8582 8583 case offsetof(struct bpf_sock_ops, state): 8584 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_state) != 1); 8585 8586 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 8587 struct bpf_sock_ops_kern, sk), 8588 si->dst_reg, si->src_reg, 8589 offsetof(struct bpf_sock_ops_kern, sk)); 8590 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg, 8591 offsetof(struct sock_common, skc_state)); 8592 break; 8593 8594 case offsetof(struct bpf_sock_ops, rtt_min): 8595 BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) != 8596 sizeof(struct minmax)); 8597 BUILD_BUG_ON(sizeof(struct minmax) < 8598 sizeof(struct minmax_sample)); 8599 8600 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 8601 struct bpf_sock_ops_kern, sk), 8602 si->dst_reg, si->src_reg, 8603 offsetof(struct bpf_sock_ops_kern, sk)); 8604 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 8605 offsetof(struct tcp_sock, rtt_min) + 8606 sizeof_field(struct minmax_sample, t)); 8607 break; 8608 8609 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags): 8610 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags, 8611 struct tcp_sock); 8612 break; 8613 8614 case offsetof(struct bpf_sock_ops, sk_txhash): 8615 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash, 8616 struct sock, type); 8617 break; 8618 case offsetof(struct bpf_sock_ops, snd_cwnd): 8619 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_cwnd); 8620 break; 8621 case offsetof(struct bpf_sock_ops, srtt_us): 8622 SOCK_OPS_GET_TCP_SOCK_FIELD(srtt_us); 8623 break; 8624 case offsetof(struct bpf_sock_ops, snd_ssthresh): 8625 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_ssthresh); 8626 break; 8627 case offsetof(struct bpf_sock_ops, rcv_nxt): 8628 SOCK_OPS_GET_TCP_SOCK_FIELD(rcv_nxt); 8629 break; 8630 case offsetof(struct bpf_sock_ops, snd_nxt): 8631 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_nxt); 8632 break; 8633 case offsetof(struct bpf_sock_ops, snd_una): 8634 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_una); 8635 break; 8636 case offsetof(struct bpf_sock_ops, mss_cache): 8637 SOCK_OPS_GET_TCP_SOCK_FIELD(mss_cache); 8638 break; 8639 case offsetof(struct bpf_sock_ops, ecn_flags): 8640 SOCK_OPS_GET_TCP_SOCK_FIELD(ecn_flags); 8641 break; 8642 case offsetof(struct bpf_sock_ops, rate_delivered): 8643 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_delivered); 8644 break; 8645 case offsetof(struct bpf_sock_ops, rate_interval_us): 8646 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_interval_us); 8647 break; 8648 case offsetof(struct bpf_sock_ops, packets_out): 8649 SOCK_OPS_GET_TCP_SOCK_FIELD(packets_out); 8650 break; 8651 case offsetof(struct bpf_sock_ops, retrans_out): 8652 SOCK_OPS_GET_TCP_SOCK_FIELD(retrans_out); 8653 break; 8654 case offsetof(struct bpf_sock_ops, total_retrans): 8655 SOCK_OPS_GET_TCP_SOCK_FIELD(total_retrans); 8656 break; 8657 case offsetof(struct bpf_sock_ops, segs_in): 8658 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_in); 8659 break; 8660 case offsetof(struct bpf_sock_ops, data_segs_in): 8661 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_in); 8662 break; 8663 case offsetof(struct bpf_sock_ops, segs_out): 8664 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_out); 8665 break; 8666 case offsetof(struct bpf_sock_ops, data_segs_out): 8667 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_out); 8668 break; 8669 case offsetof(struct bpf_sock_ops, lost_out): 8670 SOCK_OPS_GET_TCP_SOCK_FIELD(lost_out); 8671 break; 8672 case offsetof(struct bpf_sock_ops, sacked_out): 8673 SOCK_OPS_GET_TCP_SOCK_FIELD(sacked_out); 8674 break; 8675 case offsetof(struct bpf_sock_ops, bytes_received): 8676 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_received); 8677 break; 8678 case offsetof(struct bpf_sock_ops, bytes_acked): 8679 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_acked); 8680 break; 8681 case offsetof(struct bpf_sock_ops, sk): 8682 SOCK_OPS_GET_SK(); 8683 break; 8684 } 8685 return insn - insn_buf; 8686 } 8687 8688 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type, 8689 const struct bpf_insn *si, 8690 struct bpf_insn *insn_buf, 8691 struct bpf_prog *prog, u32 *target_size) 8692 { 8693 struct bpf_insn *insn = insn_buf; 8694 int off; 8695 8696 switch (si->off) { 8697 case offsetof(struct __sk_buff, data_end): 8698 off = si->off; 8699 off -= offsetof(struct __sk_buff, data_end); 8700 off += offsetof(struct sk_buff, cb); 8701 off += offsetof(struct tcp_skb_cb, bpf.data_end); 8702 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, 8703 si->src_reg, off); 8704 break; 8705 default: 8706 return bpf_convert_ctx_access(type, si, insn_buf, prog, 8707 target_size); 8708 } 8709 8710 return insn - insn_buf; 8711 } 8712 8713 static u32 sk_msg_convert_ctx_access(enum bpf_access_type type, 8714 const struct bpf_insn *si, 8715 struct bpf_insn *insn_buf, 8716 struct bpf_prog *prog, u32 *target_size) 8717 { 8718 struct bpf_insn *insn = insn_buf; 8719 #if IS_ENABLED(CONFIG_IPV6) 8720 int off; 8721 #endif 8722 8723 /* convert ctx uses the fact sg element is first in struct */ 8724 BUILD_BUG_ON(offsetof(struct sk_msg, sg) != 0); 8725 8726 switch (si->off) { 8727 case offsetof(struct sk_msg_md, data): 8728 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data), 8729 si->dst_reg, si->src_reg, 8730 offsetof(struct sk_msg, data)); 8731 break; 8732 case offsetof(struct sk_msg_md, data_end): 8733 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data_end), 8734 si->dst_reg, si->src_reg, 8735 offsetof(struct sk_msg, data_end)); 8736 break; 8737 case offsetof(struct sk_msg_md, family): 8738 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2); 8739 8740 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 8741 struct sk_msg, sk), 8742 si->dst_reg, si->src_reg, 8743 offsetof(struct sk_msg, sk)); 8744 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 8745 offsetof(struct sock_common, skc_family)); 8746 break; 8747 8748 case offsetof(struct sk_msg_md, remote_ip4): 8749 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4); 8750 8751 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 8752 struct sk_msg, sk), 8753 si->dst_reg, si->src_reg, 8754 offsetof(struct sk_msg, sk)); 8755 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 8756 offsetof(struct sock_common, skc_daddr)); 8757 break; 8758 8759 case offsetof(struct sk_msg_md, local_ip4): 8760 BUILD_BUG_ON(sizeof_field(struct sock_common, 8761 skc_rcv_saddr) != 4); 8762 8763 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 8764 struct sk_msg, sk), 8765 si->dst_reg, si->src_reg, 8766 offsetof(struct sk_msg, sk)); 8767 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 8768 offsetof(struct sock_common, 8769 skc_rcv_saddr)); 8770 break; 8771 8772 case offsetof(struct sk_msg_md, remote_ip6[0]) ... 8773 offsetof(struct sk_msg_md, remote_ip6[3]): 8774 #if IS_ENABLED(CONFIG_IPV6) 8775 BUILD_BUG_ON(sizeof_field(struct sock_common, 8776 skc_v6_daddr.s6_addr32[0]) != 4); 8777 8778 off = si->off; 8779 off -= offsetof(struct sk_msg_md, remote_ip6[0]); 8780 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 8781 struct sk_msg, sk), 8782 si->dst_reg, si->src_reg, 8783 offsetof(struct sk_msg, sk)); 8784 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 8785 offsetof(struct sock_common, 8786 skc_v6_daddr.s6_addr32[0]) + 8787 off); 8788 #else 8789 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 8790 #endif 8791 break; 8792 8793 case offsetof(struct sk_msg_md, local_ip6[0]) ... 8794 offsetof(struct sk_msg_md, local_ip6[3]): 8795 #if IS_ENABLED(CONFIG_IPV6) 8796 BUILD_BUG_ON(sizeof_field(struct sock_common, 8797 skc_v6_rcv_saddr.s6_addr32[0]) != 4); 8798 8799 off = si->off; 8800 off -= offsetof(struct sk_msg_md, local_ip6[0]); 8801 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 8802 struct sk_msg, sk), 8803 si->dst_reg, si->src_reg, 8804 offsetof(struct sk_msg, sk)); 8805 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, 8806 offsetof(struct sock_common, 8807 skc_v6_rcv_saddr.s6_addr32[0]) + 8808 off); 8809 #else 8810 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 8811 #endif 8812 break; 8813 8814 case offsetof(struct sk_msg_md, remote_port): 8815 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2); 8816 8817 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 8818 struct sk_msg, sk), 8819 si->dst_reg, si->src_reg, 8820 offsetof(struct sk_msg, sk)); 8821 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 8822 offsetof(struct sock_common, skc_dport)); 8823 #ifndef __BIG_ENDIAN_BITFIELD 8824 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16); 8825 #endif 8826 break; 8827 8828 case offsetof(struct sk_msg_md, local_port): 8829 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2); 8830 8831 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( 8832 struct sk_msg, sk), 8833 si->dst_reg, si->src_reg, 8834 offsetof(struct sk_msg, sk)); 8835 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, 8836 offsetof(struct sock_common, skc_num)); 8837 break; 8838 8839 case offsetof(struct sk_msg_md, size): 8840 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_sg, size), 8841 si->dst_reg, si->src_reg, 8842 offsetof(struct sk_msg_sg, size)); 8843 break; 8844 8845 case offsetof(struct sk_msg_md, sk): 8846 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, sk), 8847 si->dst_reg, si->src_reg, 8848 offsetof(struct sk_msg, sk)); 8849 break; 8850 } 8851 8852 return insn - insn_buf; 8853 } 8854 8855 const struct bpf_verifier_ops sk_filter_verifier_ops = { 8856 .get_func_proto = sk_filter_func_proto, 8857 .is_valid_access = sk_filter_is_valid_access, 8858 .convert_ctx_access = bpf_convert_ctx_access, 8859 .gen_ld_abs = bpf_gen_ld_abs, 8860 }; 8861 8862 const struct bpf_prog_ops sk_filter_prog_ops = { 8863 .test_run = bpf_prog_test_run_skb, 8864 }; 8865 8866 const struct bpf_verifier_ops tc_cls_act_verifier_ops = { 8867 .get_func_proto = tc_cls_act_func_proto, 8868 .is_valid_access = tc_cls_act_is_valid_access, 8869 .convert_ctx_access = tc_cls_act_convert_ctx_access, 8870 .gen_prologue = tc_cls_act_prologue, 8871 .gen_ld_abs = bpf_gen_ld_abs, 8872 }; 8873 8874 const struct bpf_prog_ops tc_cls_act_prog_ops = { 8875 .test_run = bpf_prog_test_run_skb, 8876 }; 8877 8878 const struct bpf_verifier_ops xdp_verifier_ops = { 8879 .get_func_proto = xdp_func_proto, 8880 .is_valid_access = xdp_is_valid_access, 8881 .convert_ctx_access = xdp_convert_ctx_access, 8882 .gen_prologue = bpf_noop_prologue, 8883 }; 8884 8885 const struct bpf_prog_ops xdp_prog_ops = { 8886 .test_run = bpf_prog_test_run_xdp, 8887 }; 8888 8889 const struct bpf_verifier_ops cg_skb_verifier_ops = { 8890 .get_func_proto = cg_skb_func_proto, 8891 .is_valid_access = cg_skb_is_valid_access, 8892 .convert_ctx_access = bpf_convert_ctx_access, 8893 }; 8894 8895 const struct bpf_prog_ops cg_skb_prog_ops = { 8896 .test_run = bpf_prog_test_run_skb, 8897 }; 8898 8899 const struct bpf_verifier_ops lwt_in_verifier_ops = { 8900 .get_func_proto = lwt_in_func_proto, 8901 .is_valid_access = lwt_is_valid_access, 8902 .convert_ctx_access = bpf_convert_ctx_access, 8903 }; 8904 8905 const struct bpf_prog_ops lwt_in_prog_ops = { 8906 .test_run = bpf_prog_test_run_skb, 8907 }; 8908 8909 const struct bpf_verifier_ops lwt_out_verifier_ops = { 8910 .get_func_proto = lwt_out_func_proto, 8911 .is_valid_access = lwt_is_valid_access, 8912 .convert_ctx_access = bpf_convert_ctx_access, 8913 }; 8914 8915 const struct bpf_prog_ops lwt_out_prog_ops = { 8916 .test_run = bpf_prog_test_run_skb, 8917 }; 8918 8919 const struct bpf_verifier_ops lwt_xmit_verifier_ops = { 8920 .get_func_proto = lwt_xmit_func_proto, 8921 .is_valid_access = lwt_is_valid_access, 8922 .convert_ctx_access = bpf_convert_ctx_access, 8923 .gen_prologue = tc_cls_act_prologue, 8924 }; 8925 8926 const struct bpf_prog_ops lwt_xmit_prog_ops = { 8927 .test_run = bpf_prog_test_run_skb, 8928 }; 8929 8930 const struct bpf_verifier_ops lwt_seg6local_verifier_ops = { 8931 .get_func_proto = lwt_seg6local_func_proto, 8932 .is_valid_access = lwt_is_valid_access, 8933 .convert_ctx_access = bpf_convert_ctx_access, 8934 }; 8935 8936 const struct bpf_prog_ops lwt_seg6local_prog_ops = { 8937 .test_run = bpf_prog_test_run_skb, 8938 }; 8939 8940 const struct bpf_verifier_ops cg_sock_verifier_ops = { 8941 .get_func_proto = sock_filter_func_proto, 8942 .is_valid_access = sock_filter_is_valid_access, 8943 .convert_ctx_access = bpf_sock_convert_ctx_access, 8944 }; 8945 8946 const struct bpf_prog_ops cg_sock_prog_ops = { 8947 }; 8948 8949 const struct bpf_verifier_ops cg_sock_addr_verifier_ops = { 8950 .get_func_proto = sock_addr_func_proto, 8951 .is_valid_access = sock_addr_is_valid_access, 8952 .convert_ctx_access = sock_addr_convert_ctx_access, 8953 }; 8954 8955 const struct bpf_prog_ops cg_sock_addr_prog_ops = { 8956 }; 8957 8958 const struct bpf_verifier_ops sock_ops_verifier_ops = { 8959 .get_func_proto = sock_ops_func_proto, 8960 .is_valid_access = sock_ops_is_valid_access, 8961 .convert_ctx_access = sock_ops_convert_ctx_access, 8962 }; 8963 8964 const struct bpf_prog_ops sock_ops_prog_ops = { 8965 }; 8966 8967 const struct bpf_verifier_ops sk_skb_verifier_ops = { 8968 .get_func_proto = sk_skb_func_proto, 8969 .is_valid_access = sk_skb_is_valid_access, 8970 .convert_ctx_access = sk_skb_convert_ctx_access, 8971 .gen_prologue = sk_skb_prologue, 8972 }; 8973 8974 const struct bpf_prog_ops sk_skb_prog_ops = { 8975 }; 8976 8977 const struct bpf_verifier_ops sk_msg_verifier_ops = { 8978 .get_func_proto = sk_msg_func_proto, 8979 .is_valid_access = sk_msg_is_valid_access, 8980 .convert_ctx_access = sk_msg_convert_ctx_access, 8981 .gen_prologue = bpf_noop_prologue, 8982 }; 8983 8984 const struct bpf_prog_ops sk_msg_prog_ops = { 8985 }; 8986 8987 const struct bpf_verifier_ops flow_dissector_verifier_ops = { 8988 .get_func_proto = flow_dissector_func_proto, 8989 .is_valid_access = flow_dissector_is_valid_access, 8990 .convert_ctx_access = flow_dissector_convert_ctx_access, 8991 }; 8992 8993 const struct bpf_prog_ops flow_dissector_prog_ops = { 8994 .test_run = bpf_prog_test_run_flow_dissector, 8995 }; 8996 8997 int sk_detach_filter(struct sock *sk) 8998 { 8999 int ret = -ENOENT; 9000 struct sk_filter *filter; 9001 9002 if (sock_flag(sk, SOCK_FILTER_LOCKED)) 9003 return -EPERM; 9004 9005 filter = rcu_dereference_protected(sk->sk_filter, 9006 lockdep_sock_is_held(sk)); 9007 if (filter) { 9008 RCU_INIT_POINTER(sk->sk_filter, NULL); 9009 sk_filter_uncharge(sk, filter); 9010 ret = 0; 9011 } 9012 9013 return ret; 9014 } 9015 EXPORT_SYMBOL_GPL(sk_detach_filter); 9016 9017 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf, 9018 unsigned int len) 9019 { 9020 struct sock_fprog_kern *fprog; 9021 struct sk_filter *filter; 9022 int ret = 0; 9023 9024 lock_sock(sk); 9025 filter = rcu_dereference_protected(sk->sk_filter, 9026 lockdep_sock_is_held(sk)); 9027 if (!filter) 9028 goto out; 9029 9030 /* We're copying the filter that has been originally attached, 9031 * so no conversion/decode needed anymore. eBPF programs that 9032 * have no original program cannot be dumped through this. 9033 */ 9034 ret = -EACCES; 9035 fprog = filter->prog->orig_prog; 9036 if (!fprog) 9037 goto out; 9038 9039 ret = fprog->len; 9040 if (!len) 9041 /* User space only enquires number of filter blocks. */ 9042 goto out; 9043 9044 ret = -EINVAL; 9045 if (len < fprog->len) 9046 goto out; 9047 9048 ret = -EFAULT; 9049 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog))) 9050 goto out; 9051 9052 /* Instead of bytes, the API requests to return the number 9053 * of filter blocks. 9054 */ 9055 ret = fprog->len; 9056 out: 9057 release_sock(sk); 9058 return ret; 9059 } 9060 9061 #ifdef CONFIG_INET 9062 static void bpf_init_reuseport_kern(struct sk_reuseport_kern *reuse_kern, 9063 struct sock_reuseport *reuse, 9064 struct sock *sk, struct sk_buff *skb, 9065 u32 hash) 9066 { 9067 reuse_kern->skb = skb; 9068 reuse_kern->sk = sk; 9069 reuse_kern->selected_sk = NULL; 9070 reuse_kern->data_end = skb->data + skb_headlen(skb); 9071 reuse_kern->hash = hash; 9072 reuse_kern->reuseport_id = reuse->reuseport_id; 9073 reuse_kern->bind_inany = reuse->bind_inany; 9074 } 9075 9076 struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk, 9077 struct bpf_prog *prog, struct sk_buff *skb, 9078 u32 hash) 9079 { 9080 struct sk_reuseport_kern reuse_kern; 9081 enum sk_action action; 9082 9083 bpf_init_reuseport_kern(&reuse_kern, reuse, sk, skb, hash); 9084 action = BPF_PROG_RUN(prog, &reuse_kern); 9085 9086 if (action == SK_PASS) 9087 return reuse_kern.selected_sk; 9088 else 9089 return ERR_PTR(-ECONNREFUSED); 9090 } 9091 9092 BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern, 9093 struct bpf_map *, map, void *, key, u32, flags) 9094 { 9095 bool is_sockarray = map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY; 9096 struct sock_reuseport *reuse; 9097 struct sock *selected_sk; 9098 9099 selected_sk = map->ops->map_lookup_elem(map, key); 9100 if (!selected_sk) 9101 return -ENOENT; 9102 9103 reuse = rcu_dereference(selected_sk->sk_reuseport_cb); 9104 if (!reuse) { 9105 /* Lookup in sock_map can return TCP ESTABLISHED sockets. */ 9106 if (sk_is_refcounted(selected_sk)) 9107 sock_put(selected_sk); 9108 9109 /* reuseport_array has only sk with non NULL sk_reuseport_cb. 9110 * The only (!reuse) case here is - the sk has already been 9111 * unhashed (e.g. by close()), so treat it as -ENOENT. 9112 * 9113 * Other maps (e.g. sock_map) do not provide this guarantee and 9114 * the sk may never be in the reuseport group to begin with. 9115 */ 9116 return is_sockarray ? -ENOENT : -EINVAL; 9117 } 9118 9119 if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) { 9120 struct sock *sk = reuse_kern->sk; 9121 9122 if (sk->sk_protocol != selected_sk->sk_protocol) 9123 return -EPROTOTYPE; 9124 else if (sk->sk_family != selected_sk->sk_family) 9125 return -EAFNOSUPPORT; 9126 9127 /* Catch all. Likely bound to a different sockaddr. */ 9128 return -EBADFD; 9129 } 9130 9131 reuse_kern->selected_sk = selected_sk; 9132 9133 return 0; 9134 } 9135 9136 static const struct bpf_func_proto sk_select_reuseport_proto = { 9137 .func = sk_select_reuseport, 9138 .gpl_only = false, 9139 .ret_type = RET_INTEGER, 9140 .arg1_type = ARG_PTR_TO_CTX, 9141 .arg2_type = ARG_CONST_MAP_PTR, 9142 .arg3_type = ARG_PTR_TO_MAP_KEY, 9143 .arg4_type = ARG_ANYTHING, 9144 }; 9145 9146 BPF_CALL_4(sk_reuseport_load_bytes, 9147 const struct sk_reuseport_kern *, reuse_kern, u32, offset, 9148 void *, to, u32, len) 9149 { 9150 return ____bpf_skb_load_bytes(reuse_kern->skb, offset, to, len); 9151 } 9152 9153 static const struct bpf_func_proto sk_reuseport_load_bytes_proto = { 9154 .func = sk_reuseport_load_bytes, 9155 .gpl_only = false, 9156 .ret_type = RET_INTEGER, 9157 .arg1_type = ARG_PTR_TO_CTX, 9158 .arg2_type = ARG_ANYTHING, 9159 .arg3_type = ARG_PTR_TO_UNINIT_MEM, 9160 .arg4_type = ARG_CONST_SIZE, 9161 }; 9162 9163 BPF_CALL_5(sk_reuseport_load_bytes_relative, 9164 const struct sk_reuseport_kern *, reuse_kern, u32, offset, 9165 void *, to, u32, len, u32, start_header) 9166 { 9167 return ____bpf_skb_load_bytes_relative(reuse_kern->skb, offset, to, 9168 len, start_header); 9169 } 9170 9171 static const struct bpf_func_proto sk_reuseport_load_bytes_relative_proto = { 9172 .func = sk_reuseport_load_bytes_relative, 9173 .gpl_only = false, 9174 .ret_type = RET_INTEGER, 9175 .arg1_type = ARG_PTR_TO_CTX, 9176 .arg2_type = ARG_ANYTHING, 9177 .arg3_type = ARG_PTR_TO_UNINIT_MEM, 9178 .arg4_type = ARG_CONST_SIZE, 9179 .arg5_type = ARG_ANYTHING, 9180 }; 9181 9182 static const struct bpf_func_proto * 9183 sk_reuseport_func_proto(enum bpf_func_id func_id, 9184 const struct bpf_prog *prog) 9185 { 9186 switch (func_id) { 9187 case BPF_FUNC_sk_select_reuseport: 9188 return &sk_select_reuseport_proto; 9189 case BPF_FUNC_skb_load_bytes: 9190 return &sk_reuseport_load_bytes_proto; 9191 case BPF_FUNC_skb_load_bytes_relative: 9192 return &sk_reuseport_load_bytes_relative_proto; 9193 default: 9194 return bpf_base_func_proto(func_id); 9195 } 9196 } 9197 9198 static bool 9199 sk_reuseport_is_valid_access(int off, int size, 9200 enum bpf_access_type type, 9201 const struct bpf_prog *prog, 9202 struct bpf_insn_access_aux *info) 9203 { 9204 const u32 size_default = sizeof(__u32); 9205 9206 if (off < 0 || off >= sizeof(struct sk_reuseport_md) || 9207 off % size || type != BPF_READ) 9208 return false; 9209 9210 switch (off) { 9211 case offsetof(struct sk_reuseport_md, data): 9212 info->reg_type = PTR_TO_PACKET; 9213 return size == sizeof(__u64); 9214 9215 case offsetof(struct sk_reuseport_md, data_end): 9216 info->reg_type = PTR_TO_PACKET_END; 9217 return size == sizeof(__u64); 9218 9219 case offsetof(struct sk_reuseport_md, hash): 9220 return size == size_default; 9221 9222 /* Fields that allow narrowing */ 9223 case bpf_ctx_range(struct sk_reuseport_md, eth_protocol): 9224 if (size < sizeof_field(struct sk_buff, protocol)) 9225 return false; 9226 fallthrough; 9227 case bpf_ctx_range(struct sk_reuseport_md, ip_protocol): 9228 case bpf_ctx_range(struct sk_reuseport_md, bind_inany): 9229 case bpf_ctx_range(struct sk_reuseport_md, len): 9230 bpf_ctx_record_field_size(info, size_default); 9231 return bpf_ctx_narrow_access_ok(off, size, size_default); 9232 9233 default: 9234 return false; 9235 } 9236 } 9237 9238 #define SK_REUSEPORT_LOAD_FIELD(F) ({ \ 9239 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_reuseport_kern, F), \ 9240 si->dst_reg, si->src_reg, \ 9241 bpf_target_off(struct sk_reuseport_kern, F, \ 9242 sizeof_field(struct sk_reuseport_kern, F), \ 9243 target_size)); \ 9244 }) 9245 9246 #define SK_REUSEPORT_LOAD_SKB_FIELD(SKB_FIELD) \ 9247 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \ 9248 struct sk_buff, \ 9249 skb, \ 9250 SKB_FIELD) 9251 9252 #define SK_REUSEPORT_LOAD_SK_FIELD(SK_FIELD) \ 9253 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \ 9254 struct sock, \ 9255 sk, \ 9256 SK_FIELD) 9257 9258 static u32 sk_reuseport_convert_ctx_access(enum bpf_access_type type, 9259 const struct bpf_insn *si, 9260 struct bpf_insn *insn_buf, 9261 struct bpf_prog *prog, 9262 u32 *target_size) 9263 { 9264 struct bpf_insn *insn = insn_buf; 9265 9266 switch (si->off) { 9267 case offsetof(struct sk_reuseport_md, data): 9268 SK_REUSEPORT_LOAD_SKB_FIELD(data); 9269 break; 9270 9271 case offsetof(struct sk_reuseport_md, len): 9272 SK_REUSEPORT_LOAD_SKB_FIELD(len); 9273 break; 9274 9275 case offsetof(struct sk_reuseport_md, eth_protocol): 9276 SK_REUSEPORT_LOAD_SKB_FIELD(protocol); 9277 break; 9278 9279 case offsetof(struct sk_reuseport_md, ip_protocol): 9280 SK_REUSEPORT_LOAD_SK_FIELD(sk_protocol); 9281 break; 9282 9283 case offsetof(struct sk_reuseport_md, data_end): 9284 SK_REUSEPORT_LOAD_FIELD(data_end); 9285 break; 9286 9287 case offsetof(struct sk_reuseport_md, hash): 9288 SK_REUSEPORT_LOAD_FIELD(hash); 9289 break; 9290 9291 case offsetof(struct sk_reuseport_md, bind_inany): 9292 SK_REUSEPORT_LOAD_FIELD(bind_inany); 9293 break; 9294 } 9295 9296 return insn - insn_buf; 9297 } 9298 9299 const struct bpf_verifier_ops sk_reuseport_verifier_ops = { 9300 .get_func_proto = sk_reuseport_func_proto, 9301 .is_valid_access = sk_reuseport_is_valid_access, 9302 .convert_ctx_access = sk_reuseport_convert_ctx_access, 9303 }; 9304 9305 const struct bpf_prog_ops sk_reuseport_prog_ops = { 9306 }; 9307 9308 DEFINE_STATIC_KEY_FALSE(bpf_sk_lookup_enabled); 9309 EXPORT_SYMBOL(bpf_sk_lookup_enabled); 9310 9311 BPF_CALL_3(bpf_sk_lookup_assign, struct bpf_sk_lookup_kern *, ctx, 9312 struct sock *, sk, u64, flags) 9313 { 9314 if (unlikely(flags & ~(BPF_SK_LOOKUP_F_REPLACE | 9315 BPF_SK_LOOKUP_F_NO_REUSEPORT))) 9316 return -EINVAL; 9317 if (unlikely(sk && sk_is_refcounted(sk))) 9318 return -ESOCKTNOSUPPORT; /* reject non-RCU freed sockets */ 9319 if (unlikely(sk && sk->sk_state == TCP_ESTABLISHED)) 9320 return -ESOCKTNOSUPPORT; /* reject connected sockets */ 9321 9322 /* Check if socket is suitable for packet L3/L4 protocol */ 9323 if (sk && sk->sk_protocol != ctx->protocol) 9324 return -EPROTOTYPE; 9325 if (sk && sk->sk_family != ctx->family && 9326 (sk->sk_family == AF_INET || ipv6_only_sock(sk))) 9327 return -EAFNOSUPPORT; 9328 9329 if (ctx->selected_sk && !(flags & BPF_SK_LOOKUP_F_REPLACE)) 9330 return -EEXIST; 9331 9332 /* Select socket as lookup result */ 9333 ctx->selected_sk = sk; 9334 ctx->no_reuseport = flags & BPF_SK_LOOKUP_F_NO_REUSEPORT; 9335 return 0; 9336 } 9337 9338 static const struct bpf_func_proto bpf_sk_lookup_assign_proto = { 9339 .func = bpf_sk_lookup_assign, 9340 .gpl_only = false, 9341 .ret_type = RET_INTEGER, 9342 .arg1_type = ARG_PTR_TO_CTX, 9343 .arg2_type = ARG_PTR_TO_SOCKET_OR_NULL, 9344 .arg3_type = ARG_ANYTHING, 9345 }; 9346 9347 static const struct bpf_func_proto * 9348 sk_lookup_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 9349 { 9350 switch (func_id) { 9351 case BPF_FUNC_perf_event_output: 9352 return &bpf_event_output_data_proto; 9353 case BPF_FUNC_sk_assign: 9354 return &bpf_sk_lookup_assign_proto; 9355 case BPF_FUNC_sk_release: 9356 return &bpf_sk_release_proto; 9357 default: 9358 return bpf_base_func_proto(func_id); 9359 } 9360 } 9361 9362 static bool sk_lookup_is_valid_access(int off, int size, 9363 enum bpf_access_type type, 9364 const struct bpf_prog *prog, 9365 struct bpf_insn_access_aux *info) 9366 { 9367 if (off < 0 || off >= sizeof(struct bpf_sk_lookup)) 9368 return false; 9369 if (off % size != 0) 9370 return false; 9371 if (type != BPF_READ) 9372 return false; 9373 9374 switch (off) { 9375 case offsetof(struct bpf_sk_lookup, sk): 9376 info->reg_type = PTR_TO_SOCKET_OR_NULL; 9377 return size == sizeof(__u64); 9378 9379 case bpf_ctx_range(struct bpf_sk_lookup, family): 9380 case bpf_ctx_range(struct bpf_sk_lookup, protocol): 9381 case bpf_ctx_range(struct bpf_sk_lookup, remote_ip4): 9382 case bpf_ctx_range(struct bpf_sk_lookup, local_ip4): 9383 case bpf_ctx_range_till(struct bpf_sk_lookup, remote_ip6[0], remote_ip6[3]): 9384 case bpf_ctx_range_till(struct bpf_sk_lookup, local_ip6[0], local_ip6[3]): 9385 case bpf_ctx_range(struct bpf_sk_lookup, remote_port): 9386 case bpf_ctx_range(struct bpf_sk_lookup, local_port): 9387 bpf_ctx_record_field_size(info, sizeof(__u32)); 9388 return bpf_ctx_narrow_access_ok(off, size, sizeof(__u32)); 9389 9390 default: 9391 return false; 9392 } 9393 } 9394 9395 static u32 sk_lookup_convert_ctx_access(enum bpf_access_type type, 9396 const struct bpf_insn *si, 9397 struct bpf_insn *insn_buf, 9398 struct bpf_prog *prog, 9399 u32 *target_size) 9400 { 9401 struct bpf_insn *insn = insn_buf; 9402 9403 switch (si->off) { 9404 case offsetof(struct bpf_sk_lookup, sk): 9405 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg, 9406 offsetof(struct bpf_sk_lookup_kern, selected_sk)); 9407 break; 9408 9409 case offsetof(struct bpf_sk_lookup, family): 9410 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, 9411 bpf_target_off(struct bpf_sk_lookup_kern, 9412 family, 2, target_size)); 9413 break; 9414 9415 case offsetof(struct bpf_sk_lookup, protocol): 9416 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, 9417 bpf_target_off(struct bpf_sk_lookup_kern, 9418 protocol, 2, target_size)); 9419 break; 9420 9421 case offsetof(struct bpf_sk_lookup, remote_ip4): 9422 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 9423 bpf_target_off(struct bpf_sk_lookup_kern, 9424 v4.saddr, 4, target_size)); 9425 break; 9426 9427 case offsetof(struct bpf_sk_lookup, local_ip4): 9428 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, 9429 bpf_target_off(struct bpf_sk_lookup_kern, 9430 v4.daddr, 4, target_size)); 9431 break; 9432 9433 case bpf_ctx_range_till(struct bpf_sk_lookup, 9434 remote_ip6[0], remote_ip6[3]): { 9435 #if IS_ENABLED(CONFIG_IPV6) 9436 int off = si->off; 9437 9438 off -= offsetof(struct bpf_sk_lookup, remote_ip6[0]); 9439 off += bpf_target_off(struct in6_addr, s6_addr32[0], 4, target_size); 9440 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg, 9441 offsetof(struct bpf_sk_lookup_kern, v6.saddr)); 9442 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1); 9443 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, off); 9444 #else 9445 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 9446 #endif 9447 break; 9448 } 9449 case bpf_ctx_range_till(struct bpf_sk_lookup, 9450 local_ip6[0], local_ip6[3]): { 9451 #if IS_ENABLED(CONFIG_IPV6) 9452 int off = si->off; 9453 9454 off -= offsetof(struct bpf_sk_lookup, local_ip6[0]); 9455 off += bpf_target_off(struct in6_addr, s6_addr32[0], 4, target_size); 9456 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg, 9457 offsetof(struct bpf_sk_lookup_kern, v6.daddr)); 9458 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1); 9459 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, off); 9460 #else 9461 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); 9462 #endif 9463 break; 9464 } 9465 case offsetof(struct bpf_sk_lookup, remote_port): 9466 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, 9467 bpf_target_off(struct bpf_sk_lookup_kern, 9468 sport, 2, target_size)); 9469 break; 9470 9471 case offsetof(struct bpf_sk_lookup, local_port): 9472 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, 9473 bpf_target_off(struct bpf_sk_lookup_kern, 9474 dport, 2, target_size)); 9475 break; 9476 } 9477 9478 return insn - insn_buf; 9479 } 9480 9481 const struct bpf_prog_ops sk_lookup_prog_ops = { 9482 }; 9483 9484 const struct bpf_verifier_ops sk_lookup_verifier_ops = { 9485 .get_func_proto = sk_lookup_func_proto, 9486 .is_valid_access = sk_lookup_is_valid_access, 9487 .convert_ctx_access = sk_lookup_convert_ctx_access, 9488 }; 9489 9490 #endif /* CONFIG_INET */ 9491 9492 DEFINE_BPF_DISPATCHER(xdp) 9493 9494 void bpf_prog_change_xdp(struct bpf_prog *prev_prog, struct bpf_prog *prog) 9495 { 9496 bpf_dispatcher_change_prog(BPF_DISPATCHER_PTR(xdp), prev_prog, prog); 9497 } 9498 9499 #ifdef CONFIG_DEBUG_INFO_BTF 9500 BTF_ID_LIST_GLOBAL(btf_sock_ids) 9501 #define BTF_SOCK_TYPE(name, type) BTF_ID(struct, type) 9502 BTF_SOCK_TYPE_xxx 9503 #undef BTF_SOCK_TYPE 9504 #else 9505 u32 btf_sock_ids[MAX_BTF_SOCK_TYPE]; 9506 #endif 9507 9508 static bool check_arg_btf_id(u32 btf_id, u32 arg) 9509 { 9510 int i; 9511 9512 /* only one argument, no need to check arg */ 9513 for (i = 0; i < MAX_BTF_SOCK_TYPE; i++) 9514 if (btf_sock_ids[i] == btf_id) 9515 return true; 9516 return false; 9517 } 9518 9519 BPF_CALL_1(bpf_skc_to_tcp6_sock, struct sock *, sk) 9520 { 9521 /* tcp6_sock type is not generated in dwarf and hence btf, 9522 * trigger an explicit type generation here. 9523 */ 9524 BTF_TYPE_EMIT(struct tcp6_sock); 9525 if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP && 9526 sk->sk_family == AF_INET6) 9527 return (unsigned long)sk; 9528 9529 return (unsigned long)NULL; 9530 } 9531 9532 const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto = { 9533 .func = bpf_skc_to_tcp6_sock, 9534 .gpl_only = false, 9535 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, 9536 .arg1_type = ARG_PTR_TO_BTF_ID, 9537 .check_btf_id = check_arg_btf_id, 9538 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP6], 9539 }; 9540 9541 BPF_CALL_1(bpf_skc_to_tcp_sock, struct sock *, sk) 9542 { 9543 if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP) 9544 return (unsigned long)sk; 9545 9546 return (unsigned long)NULL; 9547 } 9548 9549 const struct bpf_func_proto bpf_skc_to_tcp_sock_proto = { 9550 .func = bpf_skc_to_tcp_sock, 9551 .gpl_only = false, 9552 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, 9553 .arg1_type = ARG_PTR_TO_BTF_ID, 9554 .check_btf_id = check_arg_btf_id, 9555 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP], 9556 }; 9557 9558 BPF_CALL_1(bpf_skc_to_tcp_timewait_sock, struct sock *, sk) 9559 { 9560 #ifdef CONFIG_INET 9561 if (sk->sk_prot == &tcp_prot && sk->sk_state == TCP_TIME_WAIT) 9562 return (unsigned long)sk; 9563 #endif 9564 9565 #if IS_BUILTIN(CONFIG_IPV6) 9566 if (sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_TIME_WAIT) 9567 return (unsigned long)sk; 9568 #endif 9569 9570 return (unsigned long)NULL; 9571 } 9572 9573 const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto = { 9574 .func = bpf_skc_to_tcp_timewait_sock, 9575 .gpl_only = false, 9576 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, 9577 .arg1_type = ARG_PTR_TO_BTF_ID, 9578 .check_btf_id = check_arg_btf_id, 9579 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_TW], 9580 }; 9581 9582 BPF_CALL_1(bpf_skc_to_tcp_request_sock, struct sock *, sk) 9583 { 9584 #ifdef CONFIG_INET 9585 if (sk->sk_prot == &tcp_prot && sk->sk_state == TCP_NEW_SYN_RECV) 9586 return (unsigned long)sk; 9587 #endif 9588 9589 #if IS_BUILTIN(CONFIG_IPV6) 9590 if (sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_NEW_SYN_RECV) 9591 return (unsigned long)sk; 9592 #endif 9593 9594 return (unsigned long)NULL; 9595 } 9596 9597 const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto = { 9598 .func = bpf_skc_to_tcp_request_sock, 9599 .gpl_only = false, 9600 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, 9601 .arg1_type = ARG_PTR_TO_BTF_ID, 9602 .check_btf_id = check_arg_btf_id, 9603 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_REQ], 9604 }; 9605 9606 BPF_CALL_1(bpf_skc_to_udp6_sock, struct sock *, sk) 9607 { 9608 /* udp6_sock type is not generated in dwarf and hence btf, 9609 * trigger an explicit type generation here. 9610 */ 9611 BTF_TYPE_EMIT(struct udp6_sock); 9612 if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_UDP && 9613 sk->sk_type == SOCK_DGRAM && sk->sk_family == AF_INET6) 9614 return (unsigned long)sk; 9615 9616 return (unsigned long)NULL; 9617 } 9618 9619 const struct bpf_func_proto bpf_skc_to_udp6_sock_proto = { 9620 .func = bpf_skc_to_udp6_sock, 9621 .gpl_only = false, 9622 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, 9623 .arg1_type = ARG_PTR_TO_BTF_ID, 9624 .check_btf_id = check_arg_btf_id, 9625 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UDP6], 9626 }; 9627