1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * SR-IPv6 implementation 4 * 5 * Authors: 6 * David Lebrun <david.lebrun@uclouvain.be> 7 * eBPF support: Mathieu Xhonneux <m.xhonneux@gmail.com> 8 */ 9 10 #include <linux/filter.h> 11 #include <linux/types.h> 12 #include <linux/skbuff.h> 13 #include <linux/net.h> 14 #include <linux/module.h> 15 #include <net/ip.h> 16 #include <net/lwtunnel.h> 17 #include <net/netevent.h> 18 #include <net/netns/generic.h> 19 #include <net/ip6_fib.h> 20 #include <net/route.h> 21 #include <net/seg6.h> 22 #include <linux/seg6.h> 23 #include <linux/seg6_local.h> 24 #include <net/addrconf.h> 25 #include <net/ip6_route.h> 26 #include <net/dst_cache.h> 27 #include <net/dst_metadata.h> 28 #include <net/ip_tunnels.h> 29 #ifdef CONFIG_IPV6_SEG6_HMAC 30 #include <net/seg6_hmac.h> 31 #endif 32 #include <net/seg6_local.h> 33 #include <linux/etherdevice.h> 34 #include <linux/bpf.h> 35 #include <linux/netfilter.h> 36 37 #define SEG6_F_ATTR(i) BIT(i) 38 39 struct seg6_local_lwt; 40 41 /* callbacks used for customizing the creation and destruction of a behavior */ 42 struct seg6_local_lwtunnel_ops { 43 int (*build_state)(struct seg6_local_lwt *slwt, const void *cfg, 44 struct netlink_ext_ack *extack); 45 void (*destroy_state)(struct seg6_local_lwt *slwt); 46 }; 47 48 struct seg6_action_desc { 49 int action; 50 unsigned long attrs; 51 52 /* The optattrs field is used for specifying all the optional 53 * attributes supported by a specific behavior. 54 * It means that if one of these attributes is not provided in the 55 * netlink message during the behavior creation, no errors will be 56 * returned to the userspace. 57 * 58 * Each attribute can be only of two types (mutually exclusive): 59 * 1) required or 2) optional. 60 * Every user MUST obey to this rule! If you set an attribute as 61 * required the same attribute CANNOT be set as optional and vice 62 * versa. 63 */ 64 unsigned long optattrs; 65 66 int (*input)(struct sk_buff *skb, struct seg6_local_lwt *slwt); 67 int static_headroom; 68 69 struct seg6_local_lwtunnel_ops slwt_ops; 70 }; 71 72 struct bpf_lwt_prog { 73 struct bpf_prog *prog; 74 char *name; 75 }; 76 77 /* default length values (expressed in bits) for both Locator-Block and 78 * Locator-Node Function. 79 * 80 * Both SEG6_LOCAL_LCBLOCK_DBITS and SEG6_LOCAL_LCNODE_FN_DBITS *must* be: 81 * i) greater than 0; 82 * ii) evenly divisible by 8. In other terms, the lengths of the 83 * Locator-Block and Locator-Node Function must be byte-aligned (we can 84 * relax this constraint in the future if really needed). 85 * 86 * Moreover, a third condition must hold: 87 * iii) SEG6_LOCAL_LCBLOCK_DBITS + SEG6_LOCAL_LCNODE_FN_DBITS <= 128. 88 * 89 * The correctness of SEG6_LOCAL_LCBLOCK_DBITS and SEG6_LOCAL_LCNODE_FN_DBITS 90 * values are checked during the kernel compilation. If the compilation stops, 91 * check the value of these parameters to see if they meet conditions (i), (ii) 92 * and (iii). 93 */ 94 #define SEG6_LOCAL_LCBLOCK_DBITS 32 95 #define SEG6_LOCAL_LCNODE_FN_DBITS 16 96 97 /* The following next_csid_chk_{cntr,lcblock,lcblock_fn}_bits macros can be 98 * used directly to check whether the lengths (in bits) of Locator-Block and 99 * Locator-Node Function are valid according to (i), (ii), (iii). 100 */ 101 #define next_csid_chk_cntr_bits(blen, flen) \ 102 ((blen) + (flen) > 128) 103 104 #define next_csid_chk_lcblock_bits(blen) \ 105 ({ \ 106 typeof(blen) __tmp = blen; \ 107 (!__tmp || __tmp > 120 || (__tmp & 0x07)); \ 108 }) 109 110 #define next_csid_chk_lcnode_fn_bits(flen) \ 111 next_csid_chk_lcblock_bits(flen) 112 113 /* flag indicating that flavors are set up for a given End* behavior */ 114 #define SEG6_F_LOCAL_FLAVORS SEG6_F_ATTR(SEG6_LOCAL_FLAVORS) 115 116 #define SEG6_F_LOCAL_FLV_OP(flvname) BIT(SEG6_LOCAL_FLV_OP_##flvname) 117 #define SEG6_F_LOCAL_FLV_NEXT_CSID SEG6_F_LOCAL_FLV_OP(NEXT_CSID) 118 #define SEG6_F_LOCAL_FLV_PSP SEG6_F_LOCAL_FLV_OP(PSP) 119 120 /* Supported RFC8986 Flavor operations are reported in this bitmask */ 121 #define SEG6_LOCAL_FLV8986_SUPP_OPS SEG6_F_LOCAL_FLV_PSP 122 123 #define SEG6_LOCAL_END_FLV_SUPP_OPS (SEG6_F_LOCAL_FLV_NEXT_CSID | \ 124 SEG6_LOCAL_FLV8986_SUPP_OPS) 125 #define SEG6_LOCAL_END_X_FLV_SUPP_OPS SEG6_F_LOCAL_FLV_NEXT_CSID 126 127 struct seg6_flavors_info { 128 /* Flavor operations */ 129 __u32 flv_ops; 130 131 /* Locator-Block length, expressed in bits */ 132 __u8 lcblock_bits; 133 /* Locator-Node Function length, expressed in bits*/ 134 __u8 lcnode_func_bits; 135 }; 136 137 enum seg6_end_dt_mode { 138 DT_INVALID_MODE = -EINVAL, 139 DT_LEGACY_MODE = 0, 140 DT_VRF_MODE = 1, 141 }; 142 143 struct seg6_end_dt_info { 144 enum seg6_end_dt_mode mode; 145 146 struct net *net; 147 /* VRF device associated to the routing table used by the SRv6 148 * End.DT4/DT6 behavior for routing IPv4/IPv6 packets. 149 */ 150 int vrf_ifindex; 151 int vrf_table; 152 153 /* tunneled packet family (IPv4 or IPv6) */ 154 u16 family; 155 }; 156 157 struct pcpu_seg6_local_counters { 158 u64_stats_t packets; 159 u64_stats_t bytes; 160 u64_stats_t errors; 161 162 struct u64_stats_sync syncp; 163 }; 164 165 /* This struct groups all the SRv6 Behavior counters supported so far. 166 * 167 * put_nla_counters() makes use of this data structure to collect all counter 168 * values after the per-CPU counter evaluation has been performed. 169 * Finally, each counter value (in seg6_local_counters) is stored in the 170 * corresponding netlink attribute and sent to user space. 171 * 172 * NB: we don't want to expose this structure to user space! 173 */ 174 struct seg6_local_counters { 175 __u64 packets; 176 __u64 bytes; 177 __u64 errors; 178 }; 179 180 #define seg6_local_alloc_pcpu_counters(__gfp) \ 181 __netdev_alloc_pcpu_stats(struct pcpu_seg6_local_counters, \ 182 ((__gfp) | __GFP_ZERO)) 183 184 #define SEG6_F_LOCAL_COUNTERS SEG6_F_ATTR(SEG6_LOCAL_COUNTERS) 185 186 struct seg6_local_lwt { 187 int action; 188 struct ipv6_sr_hdr *srh; 189 int table; 190 struct in_addr nh4; 191 struct in6_addr nh6; 192 int iif; 193 int oif; 194 struct bpf_lwt_prog bpf; 195 #ifdef CONFIG_NET_L3_MASTER_DEV 196 struct seg6_end_dt_info dt_info; 197 #endif 198 struct seg6_flavors_info flv_info; 199 200 struct pcpu_seg6_local_counters __percpu *pcpu_counters; 201 202 int headroom; 203 struct seg6_action_desc *desc; 204 /* unlike the required attrs, we have to track the optional attributes 205 * that have been effectively parsed. 206 */ 207 unsigned long parsed_optattrs; 208 }; 209 210 static struct seg6_local_lwt *seg6_local_lwtunnel(struct lwtunnel_state *lwt) 211 { 212 return (struct seg6_local_lwt *)lwt->data; 213 } 214 215 static bool seg6_local_skb_has_lwtstate(struct sk_buff *skb) 216 { 217 struct dst_entry *dst; 218 219 if (unlikely(!skb_valid_dst(skb))) 220 return false; 221 222 dst = skb_dst(skb); 223 if (unlikely(!dst->lwtstate || 224 dst->lwtstate->type != LWTUNNEL_ENCAP_SEG6_LOCAL)) 225 return false; 226 227 return true; 228 } 229 230 static struct ipv6_sr_hdr *get_and_validate_srh(struct sk_buff *skb) 231 { 232 struct ipv6_sr_hdr *srh; 233 234 srh = seg6_get_srh(skb, IP6_FH_F_SKIP_RH); 235 if (!srh) 236 return NULL; 237 238 #ifdef CONFIG_IPV6_SEG6_HMAC 239 if (!seg6_hmac_validate_skb(skb, srh)) 240 return NULL; 241 #endif 242 243 return srh; 244 } 245 246 static bool decap_and_validate(struct sk_buff *skb, int proto) 247 { 248 struct ipv6_sr_hdr *srh; 249 unsigned int off = 0; 250 251 srh = seg6_get_srh(skb, 0); 252 if (srh && srh->segments_left > 0) 253 return false; 254 255 #ifdef CONFIG_IPV6_SEG6_HMAC 256 if (srh && !seg6_hmac_validate_skb(skb, srh)) 257 return false; 258 #endif 259 260 if (ipv6_find_hdr(skb, &off, proto, NULL, NULL) < 0) 261 return false; 262 263 if (!pskb_pull(skb, off)) 264 return false; 265 266 skb_postpull_rcsum(skb, skb_network_header(skb), off); 267 268 skb_reset_network_header(skb); 269 skb_reset_transport_header(skb); 270 if (iptunnel_pull_offloads(skb)) 271 return false; 272 273 if (proto == IPPROTO_IPIP) { 274 bool l3slave = ipv6_l3mdev_skb(IP6CB(skb)->flags); 275 int iif = IP6CB(skb)->iif; 276 277 memset(IPCB(skb), 0, sizeof(*IPCB(skb))); 278 IPCB(skb)->iif = iif; 279 if (l3slave) 280 IPCB(skb)->flags |= IPSKB_L3SLAVE; 281 } else if (proto == IPPROTO_IPV6) { 282 bool l3slave = ipv6_l3mdev_skb(IP6CB(skb)->flags); 283 int iif = IP6CB(skb)->iif; 284 285 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb))); 286 IP6CB(skb)->iif = iif; 287 IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr); 288 if (l3slave) 289 IP6CB(skb)->flags |= IP6SKB_L3SLAVE; 290 } 291 292 return true; 293 } 294 295 /* advance the SRH to the next segment and set the IPv6 DA accordingly. 296 * skb pointers may change: after this call, the caller must evaluate again 297 * any pointer into the packet data. 298 * 299 * This function returns: 300 * - the SRH on success; 301 * - NULL when the skb cannot be made writable. In this case, the function 302 * sets the drop reason to SKB_DROP_REASON_NOMEM. 303 */ 304 static struct ipv6_sr_hdr *advance_nextseg(struct sk_buff *skb, 305 struct ipv6_sr_hdr *srh, 306 enum skb_drop_reason *reason) 307 { 308 struct in6_addr *addr; 309 int srhoff; 310 int wlen; 311 312 srhoff = (unsigned char *)srh - skb->data; 313 /* we write only the Segment Left field and the IPv6 DA. The segment 314 * list is only read, and seg6_get_srh() already pulled it into the 315 * linear area. 316 */ 317 wlen = srhoff + sizeof(*srh); 318 319 if (unlikely(skb_ensure_writable(skb, wlen))) { 320 *reason = SKB_DROP_REASON_NOMEM; 321 return NULL; 322 } 323 324 /* skb_ensure_writable() may change skb pointers; evaluate srh again */ 325 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); 326 327 srh->segments_left--; 328 addr = srh->segments + srh->segments_left; 329 ipv6_hdr(skb)->daddr = *addr; 330 331 return srh; 332 } 333 334 static int 335 seg6_lookup_any_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr, 336 u32 tbl_id, bool local_delivery, int oif) 337 { 338 struct net *net = dev_net(skb->dev); 339 struct ipv6hdr *hdr = ipv6_hdr(skb); 340 int flags = RT6_LOOKUP_F_HAS_SADDR; 341 struct dst_entry *dst = NULL; 342 struct rt6_info *rt; 343 struct flowi6 fl6; 344 int dev_flags = 0; 345 346 memset(&fl6, 0, sizeof(fl6)); 347 fl6.flowi6_iif = skb->dev->ifindex; 348 fl6.flowi6_oif = oif; 349 fl6.daddr = nhaddr ? *nhaddr : hdr->daddr; 350 fl6.saddr = hdr->saddr; 351 fl6.flowlabel = ip6_flowinfo(hdr); 352 fl6.flowi6_mark = skb->mark; 353 fl6.flowi6_proto = hdr->nexthdr; 354 355 if (nhaddr) 356 fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH; 357 358 if (!tbl_id && !oif) { 359 dst = ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags); 360 } else if (tbl_id) { 361 struct fib6_table *table; 362 363 table = fib6_get_table(net, tbl_id); 364 if (!table) 365 goto out; 366 367 rt = ip6_pol_route(net, table, oif, &fl6, skb, flags); 368 dst = &rt->dst; 369 } else { 370 dst = ip6_route_output(net, NULL, &fl6); 371 } 372 373 /* we want to discard traffic destined for local packet processing, 374 * if @local_delivery is set to false. 375 */ 376 if (!local_delivery) 377 dev_flags |= IFF_LOOPBACK; 378 379 if (dst && (dst_dev(dst)->flags & dev_flags) && !dst->error) { 380 dst_release(dst); 381 dst = NULL; 382 } 383 384 out: 385 if (!dst) { 386 rt = net->ipv6.ip6_blk_hole_entry; 387 dst = &rt->dst; 388 dst_hold(dst); 389 } 390 391 skb_dst_drop(skb); 392 skb_dst_set(skb, dst); 393 return dst->error; 394 } 395 396 int seg6_lookup_nexthop(struct sk_buff *skb, 397 struct in6_addr *nhaddr, u32 tbl_id) 398 { 399 return seg6_lookup_any_nexthop(skb, nhaddr, tbl_id, false, 0); 400 } 401 402 static __u8 seg6_flv_lcblock_octects(const struct seg6_flavors_info *finfo) 403 { 404 return finfo->lcblock_bits >> 3; 405 } 406 407 static __u8 seg6_flv_lcnode_func_octects(const struct seg6_flavors_info *finfo) 408 { 409 return finfo->lcnode_func_bits >> 3; 410 } 411 412 static bool seg6_next_csid_is_arg_zero(const struct in6_addr *addr, 413 const struct seg6_flavors_info *finfo) 414 { 415 __u8 fnc_octects = seg6_flv_lcnode_func_octects(finfo); 416 __u8 blk_octects = seg6_flv_lcblock_octects(finfo); 417 __u8 arg_octects; 418 int i; 419 420 arg_octects = 16 - blk_octects - fnc_octects; 421 for (i = 0; i < arg_octects; ++i) { 422 if (addr->s6_addr[blk_octects + fnc_octects + i] != 0x00) 423 return false; 424 } 425 426 return true; 427 } 428 429 /* assume that DA.Argument length > 0. 430 * skb pointers may change: after this call, the caller must evaluate again 431 * any pointer into the packet data. 432 * 433 * This function returns: 434 * - SKB_NOT_DROPPED_YET on success; 435 * - SKB_DROP_REASON_NOMEM when the skb cannot be made writable. 436 */ 437 static enum skb_drop_reason 438 seg6_next_csid_advance_arg(struct sk_buff *skb, 439 const struct seg6_flavors_info *finfo) 440 { 441 __u8 fnc_octects = seg6_flv_lcnode_func_octects(finfo); 442 __u8 blk_octects = seg6_flv_lcblock_octects(finfo); 443 struct in6_addr *addr; 444 445 if (unlikely(skb_ensure_writable(skb, sizeof(struct ipv6hdr)))) 446 return SKB_DROP_REASON_NOMEM; 447 448 addr = &ipv6_hdr(skb)->daddr; 449 450 /* advance DA.Argument */ 451 memmove(&addr->s6_addr[blk_octects], 452 &addr->s6_addr[blk_octects + fnc_octects], 453 16 - blk_octects - fnc_octects); 454 455 memset(&addr->s6_addr[16 - fnc_octects], 0x00, fnc_octects); 456 457 return SKB_NOT_DROPPED_YET; 458 } 459 460 static int input_action_end_finish(struct sk_buff *skb, 461 struct seg6_local_lwt *slwt) 462 { 463 seg6_lookup_nexthop(skb, NULL, 0); 464 465 return dst_input(skb); 466 } 467 468 static int input_action_end_core(struct sk_buff *skb, 469 struct seg6_local_lwt *slwt) 470 { 471 enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED; 472 struct ipv6_sr_hdr *srh; 473 int err = -EINVAL; 474 475 srh = get_and_validate_srh(skb); 476 if (!srh) 477 goto drop; 478 479 srh = advance_nextseg(skb, srh, &reason); 480 if (!srh) { 481 err = -ENOMEM; 482 goto drop; 483 } 484 485 return input_action_end_finish(skb, slwt); 486 487 drop: 488 kfree_skb_reason(skb, reason); 489 return err; 490 } 491 492 static int end_next_csid_core(struct sk_buff *skb, struct seg6_local_lwt *slwt) 493 { 494 const struct seg6_flavors_info *finfo = &slwt->flv_info; 495 struct in6_addr *daddr = &ipv6_hdr(skb)->daddr; 496 enum skb_drop_reason reason; 497 498 if (seg6_next_csid_is_arg_zero(daddr, finfo)) 499 return input_action_end_core(skb, slwt); 500 501 /* update DA */ 502 reason = seg6_next_csid_advance_arg(skb, finfo); 503 if (reason) { 504 kfree_skb_reason(skb, reason); 505 return -ENOMEM; 506 } 507 508 return input_action_end_finish(skb, slwt); 509 } 510 511 static int input_action_end_x_finish(struct sk_buff *skb, 512 struct seg6_local_lwt *slwt) 513 { 514 seg6_lookup_any_nexthop(skb, &slwt->nh6, 0, false, slwt->oif); 515 516 return dst_input(skb); 517 } 518 519 static int input_action_end_x_core(struct sk_buff *skb, 520 struct seg6_local_lwt *slwt) 521 { 522 enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED; 523 struct ipv6_sr_hdr *srh; 524 int err = -EINVAL; 525 526 srh = get_and_validate_srh(skb); 527 if (!srh) 528 goto drop; 529 530 srh = advance_nextseg(skb, srh, &reason); 531 if (!srh) { 532 err = -ENOMEM; 533 goto drop; 534 } 535 536 return input_action_end_x_finish(skb, slwt); 537 538 drop: 539 kfree_skb_reason(skb, reason); 540 return err; 541 } 542 543 static int end_x_next_csid_core(struct sk_buff *skb, 544 struct seg6_local_lwt *slwt) 545 { 546 const struct seg6_flavors_info *finfo = &slwt->flv_info; 547 struct in6_addr *daddr = &ipv6_hdr(skb)->daddr; 548 enum skb_drop_reason reason; 549 550 if (seg6_next_csid_is_arg_zero(daddr, finfo)) 551 return input_action_end_x_core(skb, slwt); 552 553 /* update DA */ 554 reason = seg6_next_csid_advance_arg(skb, finfo); 555 if (reason) { 556 kfree_skb_reason(skb, reason); 557 return -ENOMEM; 558 } 559 560 return input_action_end_x_finish(skb, slwt); 561 } 562 563 static bool seg6_next_csid_enabled(__u32 fops) 564 { 565 return fops & SEG6_F_LOCAL_FLV_NEXT_CSID; 566 } 567 568 /* Processing of SRv6 End, End.X, and End.T behaviors can be extended through 569 * the flavors framework. These behaviors must report the subset of (flavor) 570 * operations they currently implement. In this way, if a user specifies a 571 * flavor combination that is not supported by a given End* behavior, the 572 * kernel refuses to instantiate the tunnel reporting the error. 573 */ 574 static int seg6_flv_supp_ops_by_action(int action, __u32 *fops) 575 { 576 switch (action) { 577 case SEG6_LOCAL_ACTION_END: 578 *fops = SEG6_LOCAL_END_FLV_SUPP_OPS; 579 break; 580 case SEG6_LOCAL_ACTION_END_X: 581 *fops = SEG6_LOCAL_END_X_FLV_SUPP_OPS; 582 break; 583 default: 584 return -EOPNOTSUPP; 585 } 586 587 return 0; 588 } 589 590 /* We describe the packet state in relation to the absence/presence of the SRH 591 * and the Segment Left (SL) field. 592 * For our purposes, it is not necessary to record the exact value of the SL 593 * when the SID List consists of two or more segments. 594 */ 595 enum seg6_local_pktinfo { 596 /* the order really matters! */ 597 SEG6_LOCAL_PKTINFO_NOHDR = 0, 598 SEG6_LOCAL_PKTINFO_SL_ZERO, 599 SEG6_LOCAL_PKTINFO_SL_ONE, 600 SEG6_LOCAL_PKTINFO_SL_MORE, 601 __SEG6_LOCAL_PKTINFO_MAX, 602 }; 603 604 #define SEG6_LOCAL_PKTINFO_MAX (__SEG6_LOCAL_PKTINFO_MAX - 1) 605 606 static enum seg6_local_pktinfo seg6_get_srh_pktinfo(struct ipv6_sr_hdr *srh) 607 { 608 __u8 sgl; 609 610 if (!srh) 611 return SEG6_LOCAL_PKTINFO_NOHDR; 612 613 sgl = srh->segments_left; 614 if (sgl < 2) 615 return SEG6_LOCAL_PKTINFO_SL_ZERO + sgl; 616 617 return SEG6_LOCAL_PKTINFO_SL_MORE; 618 } 619 620 enum seg6_local_flv_action { 621 SEG6_LOCAL_FLV_ACT_UNSPEC = 0, 622 SEG6_LOCAL_FLV_ACT_END, 623 SEG6_LOCAL_FLV_ACT_PSP, 624 SEG6_LOCAL_FLV_ACT_USP, 625 SEG6_LOCAL_FLV_ACT_USD, 626 __SEG6_LOCAL_FLV_ACT_MAX 627 }; 628 629 #define SEG6_LOCAL_FLV_ACT_MAX (__SEG6_LOCAL_FLV_ACT_MAX - 1) 630 631 /* The action table for RFC8986 flavors (see the flv8986_act_tbl below) 632 * contains the actions (i.e. processing operations) to be applied on packets 633 * when flavors are configured for an End* behavior. 634 * By combining the pkinfo data and from the flavors mask, the macro 635 * computes the index used to access the elements (actions) stored in the 636 * action table. The index is structured as follows: 637 * 638 * index 639 * _______________/\________________ 640 * / \ 641 * +----------------+----------------+ 642 * | pf | afm | 643 * +----------------+----------------+ 644 * ph-1 ... p1 p0 fk-1 ... f1 f0 645 * MSB LSB 646 * 647 * where: 648 * - 'afm' (adjusted flavor mask) is the mask containing a combination of the 649 * RFC8986 flavors currently supported. 'afm' corresponds to the @fm 650 * argument of the macro whose value is righ-shifted by 1 bit. By doing so, 651 * we discard the SEG6_LOCAL_FLV_OP_UNSPEC flag (bit 0 in @fm) which is 652 * never used here; 653 * - 'pf' encodes the packet info (pktinfo) regarding the presence/absence of 654 * the SRH, SL = 0, etc. 'pf' is set with the value of @pf provided as 655 * argument to the macro. 656 */ 657 #define flv8986_act_tbl_idx(pf, fm) \ 658 ((((pf) << bits_per(SEG6_LOCAL_FLV8986_SUPP_OPS)) | \ 659 ((fm) & SEG6_LOCAL_FLV8986_SUPP_OPS)) >> SEG6_LOCAL_FLV_OP_PSP) 660 661 /* We compute the size of the action table by considering the RFC8986 flavors 662 * actually supported by the kernel. In this way, the size is automatically 663 * adjusted when new flavors are supported. 664 */ 665 #define FLV8986_ACT_TBL_SIZE \ 666 roundup_pow_of_two(flv8986_act_tbl_idx(SEG6_LOCAL_PKTINFO_MAX, \ 667 SEG6_LOCAL_FLV8986_SUPP_OPS)) 668 669 /* tbl_cfg(act, pf, fm) macro is used to easily configure the action 670 * table; it accepts 3 arguments: 671 * i) @act, the suffix from SEG6_LOCAL_FLV_ACT_{act} representing 672 * the action that should be applied on the packet; 673 * ii) @pf, the suffix from SEG6_LOCAL_PKTINFO_{pf} reporting the packet 674 * info about the lack/presence of SRH, SRH with SL = 0, etc; 675 * iii) @fm, the mask of flavors. 676 */ 677 #define tbl_cfg(act, pf, fm) \ 678 [flv8986_act_tbl_idx(SEG6_LOCAL_PKTINFO_##pf, \ 679 (fm))] = SEG6_LOCAL_FLV_ACT_##act 680 681 /* shorthand for improving readability */ 682 #define F_PSP SEG6_F_LOCAL_FLV_PSP 683 684 /* The table contains, for each combination of the pktinfo data and 685 * flavors, the action that should be taken on a packet (e.g. 686 * "standard" Endpoint processing, Penultimate Segment Pop, etc). 687 * 688 * By default, table entries not explicitly configured are initialized with the 689 * SEG6_LOCAL_FLV_ACT_UNSPEC action, which generally has the effect of 690 * discarding the processed packet. 691 */ 692 static const u8 flv8986_act_tbl[FLV8986_ACT_TBL_SIZE] = { 693 /* PSP variant for packet where SRH with SL = 1 */ 694 tbl_cfg(PSP, SL_ONE, F_PSP), 695 /* End for packet where the SRH with SL > 1*/ 696 tbl_cfg(END, SL_MORE, F_PSP), 697 }; 698 699 #undef F_PSP 700 #undef tbl_cfg 701 702 /* For each flavor defined in RFC8986 (or a combination of them) an action is 703 * performed on the packet. The specific action depends on: 704 * - info extracted from the packet (i.e. pktinfo data) regarding the 705 * lack/presence of the SRH, and if the SRH is available, on the value of 706 * Segment Left field; 707 * - the mask of flavors configured for the specific SRv6 End* behavior. 708 * 709 * The function combines both the pkinfo and the flavors mask to evaluate the 710 * corresponding action to be taken on the packet. 711 */ 712 static enum seg6_local_flv_action 713 seg6_local_flv8986_act_lookup(enum seg6_local_pktinfo pinfo, __u32 flvmask) 714 { 715 unsigned long index; 716 717 /* check if the provided mask of flavors is supported */ 718 if (unlikely(flvmask & ~SEG6_LOCAL_FLV8986_SUPP_OPS)) 719 return SEG6_LOCAL_FLV_ACT_UNSPEC; 720 721 index = flv8986_act_tbl_idx(pinfo, flvmask); 722 if (unlikely(index >= FLV8986_ACT_TBL_SIZE)) 723 return SEG6_LOCAL_FLV_ACT_UNSPEC; 724 725 return flv8986_act_tbl[index]; 726 } 727 728 /* skb->data must be aligned with skb->network_header */ 729 static bool seg6_pop_srh(struct sk_buff *skb, int srhoff) 730 { 731 struct ipv6_sr_hdr *srh; 732 struct ipv6hdr *iph; 733 __u8 srh_nexthdr; 734 int thoff = -1; 735 int srhlen; 736 int nhlen; 737 738 if (unlikely(srhoff < sizeof(*iph) || 739 !pskb_may_pull(skb, srhoff + sizeof(*srh)))) 740 return false; 741 742 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); 743 srhlen = ipv6_optlen(srh); 744 745 /* we are about to mangle the pkt, let's check if we can write on it */ 746 if (unlikely(skb_ensure_writable(skb, srhoff + srhlen))) 747 return false; 748 749 /* skb_ensure_writable() may change skb pointers; evaluate srh again */ 750 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); 751 srh_nexthdr = srh->nexthdr; 752 753 if (unlikely(!skb_transport_header_was_set(skb))) 754 goto pull; 755 756 nhlen = skb_network_header_len(skb); 757 /* we have to deal with the transport header: it could be set before 758 * the SRH, after the SRH, or within it (which is considered wrong, 759 * however). 760 */ 761 if (likely(nhlen <= srhoff)) 762 thoff = nhlen; 763 else if (nhlen >= srhoff + srhlen) 764 /* transport_header is set after the SRH */ 765 thoff = nhlen - srhlen; 766 else 767 /* transport_header falls inside the SRH; hence, we can't 768 * restore the transport_header pointer properly after 769 * SRH removing operation. 770 */ 771 return false; 772 pull: 773 /* we need to pop the SRH: 774 * 1) first of all, we pull out everything from IPv6 header up to SRH 775 * (included) evaluating also the rcsum; 776 * 2) we overwrite (and then remove) the SRH by properly moving the 777 * IPv6 along with any extension header that precedes the SRH; 778 * 3) At the end, we push back the pulled headers (except for SRH, 779 * obviously). 780 */ 781 skb_pull_rcsum(skb, srhoff + srhlen); 782 memmove(skb_network_header(skb) + srhlen, skb_network_header(skb), 783 srhoff); 784 skb_push(skb, srhoff); 785 786 skb_reset_network_header(skb); 787 skb_mac_header_rebuild(skb); 788 if (likely(thoff >= 0)) 789 skb_set_transport_header(skb, thoff); 790 791 iph = ipv6_hdr(skb); 792 if (iph->nexthdr == NEXTHDR_ROUTING) { 793 iph->nexthdr = srh_nexthdr; 794 } else { 795 /* we must look for the extension header (EXTH, for short) that 796 * immediately precedes the SRH we have just removed. 797 * Then, we update the value of the EXTH nexthdr with the one 798 * contained in the SRH nexthdr. 799 */ 800 unsigned int off = sizeof(*iph); 801 struct ipv6_opt_hdr *hp, _hdr; 802 __u8 nexthdr = iph->nexthdr; 803 804 for (;;) { 805 if (unlikely(!ipv6_ext_hdr(nexthdr) || 806 nexthdr == NEXTHDR_NONE)) 807 return false; 808 809 hp = skb_header_pointer(skb, off, sizeof(_hdr), &_hdr); 810 if (unlikely(!hp)) 811 return false; 812 813 if (hp->nexthdr == NEXTHDR_ROUTING) { 814 hp->nexthdr = srh_nexthdr; 815 break; 816 } 817 818 switch (nexthdr) { 819 case NEXTHDR_FRAGMENT: 820 fallthrough; 821 case NEXTHDR_AUTH: 822 /* we expect SRH before FRAG and AUTH */ 823 return false; 824 default: 825 off += ipv6_optlen(hp); 826 break; 827 } 828 829 nexthdr = hp->nexthdr; 830 } 831 } 832 833 iph->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); 834 835 skb_postpush_rcsum(skb, iph, srhoff); 836 837 return true; 838 } 839 840 /* process the packet on the basis of the RFC8986 flavors set for the given 841 * SRv6 End behavior instance. 842 */ 843 static int end_flv8986_core(struct sk_buff *skb, struct seg6_local_lwt *slwt) 844 { 845 enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED; 846 const struct seg6_flavors_info *finfo = &slwt->flv_info; 847 enum seg6_local_flv_action action; 848 enum seg6_local_pktinfo pinfo; 849 struct ipv6_sr_hdr *srh; 850 int err = -EINVAL; 851 __u32 flvmask; 852 int srhoff; 853 854 srh = seg6_get_srh(skb, 0); 855 srhoff = srh ? ((unsigned char *)srh - skb->data) : 0; 856 pinfo = seg6_get_srh_pktinfo(srh); 857 #ifdef CONFIG_IPV6_SEG6_HMAC 858 if (srh && !seg6_hmac_validate_skb(skb, srh)) 859 goto drop; 860 #endif 861 flvmask = finfo->flv_ops; 862 if (unlikely(flvmask & ~SEG6_LOCAL_FLV8986_SUPP_OPS)) { 863 pr_warn_once("seg6local: invalid RFC8986 flavors\n"); 864 goto drop; 865 } 866 867 /* retrieve the action triggered by the combination of pktinfo data and 868 * the flavors mask. 869 */ 870 action = seg6_local_flv8986_act_lookup(pinfo, flvmask); 871 switch (action) { 872 case SEG6_LOCAL_FLV_ACT_END: 873 /* process the packet as the "standard" End behavior */ 874 srh = advance_nextseg(skb, srh, &reason); 875 if (!srh) { 876 err = -ENOMEM; 877 goto drop; 878 } 879 break; 880 case SEG6_LOCAL_FLV_ACT_PSP: 881 srh = advance_nextseg(skb, srh, &reason); 882 if (!srh) { 883 err = -ENOMEM; 884 goto drop; 885 } 886 887 if (unlikely(!seg6_pop_srh(skb, srhoff))) 888 goto drop; 889 break; 890 case SEG6_LOCAL_FLV_ACT_UNSPEC: 891 fallthrough; 892 default: 893 /* by default, we drop the packet since we could not find a 894 * suitable action. 895 */ 896 goto drop; 897 } 898 899 return input_action_end_finish(skb, slwt); 900 901 drop: 902 kfree_skb_reason(skb, reason); 903 return err; 904 } 905 906 /* regular endpoint function */ 907 static int input_action_end(struct sk_buff *skb, struct seg6_local_lwt *slwt) 908 { 909 const struct seg6_flavors_info *finfo = &slwt->flv_info; 910 __u32 fops = finfo->flv_ops; 911 912 if (!fops) 913 return input_action_end_core(skb, slwt); 914 915 /* check for the presence of NEXT-C-SID since it applies first */ 916 if (seg6_next_csid_enabled(fops)) 917 return end_next_csid_core(skb, slwt); 918 919 /* the specific processing function to be performed on the packet 920 * depends on the combination of flavors defined in RFC8986 and some 921 * information extracted from the packet, e.g. presence/absence of SRH, 922 * Segment Left = 0, etc. 923 */ 924 return end_flv8986_core(skb, slwt); 925 } 926 927 /* regular endpoint, and forward to specified nexthop */ 928 static int input_action_end_x(struct sk_buff *skb, struct seg6_local_lwt *slwt) 929 { 930 const struct seg6_flavors_info *finfo = &slwt->flv_info; 931 __u32 fops = finfo->flv_ops; 932 933 /* check for the presence of NEXT-C-SID since it applies first */ 934 if (seg6_next_csid_enabled(fops)) 935 return end_x_next_csid_core(skb, slwt); 936 937 return input_action_end_x_core(skb, slwt); 938 } 939 940 static int input_action_end_t(struct sk_buff *skb, struct seg6_local_lwt *slwt) 941 { 942 enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED; 943 struct ipv6_sr_hdr *srh; 944 int err = -EINVAL; 945 946 srh = get_and_validate_srh(skb); 947 if (!srh) 948 goto drop; 949 950 srh = advance_nextseg(skb, srh, &reason); 951 if (!srh) { 952 err = -ENOMEM; 953 goto drop; 954 } 955 956 seg6_lookup_nexthop(skb, NULL, slwt->table); 957 958 return dst_input(skb); 959 960 drop: 961 kfree_skb_reason(skb, reason); 962 return err; 963 } 964 965 /* decapsulate and forward inner L2 frame on specified interface */ 966 static int input_action_end_dx2(struct sk_buff *skb, 967 struct seg6_local_lwt *slwt) 968 { 969 struct net *net = dev_net(skb->dev); 970 struct net_device *odev; 971 struct ethhdr *eth; 972 973 if (!decap_and_validate(skb, IPPROTO_ETHERNET)) 974 goto drop; 975 976 if (!pskb_may_pull(skb, ETH_HLEN)) 977 goto drop; 978 979 skb_reset_mac_header(skb); 980 eth = (struct ethhdr *)skb->data; 981 982 /* To determine the frame's protocol, we assume it is 802.3. This avoids 983 * a call to eth_type_trans(), which is not really relevant for our 984 * use case. 985 */ 986 if (!eth_proto_is_802_3(eth->h_proto)) 987 goto drop; 988 989 odev = dev_get_by_index_rcu(net, slwt->oif); 990 if (!odev) 991 goto drop; 992 993 /* As we accept Ethernet frames, make sure the egress device is of 994 * the correct type. 995 */ 996 if (odev->type != ARPHRD_ETHER) 997 goto drop; 998 999 if (!(odev->flags & IFF_UP) || !netif_carrier_ok(odev)) 1000 goto drop; 1001 1002 skb_orphan(skb); 1003 1004 if (skb_warn_if_lro(skb)) 1005 goto drop; 1006 1007 skb_forward_csum(skb); 1008 1009 if (skb->len - ETH_HLEN > odev->mtu) 1010 goto drop; 1011 1012 skb->dev = odev; 1013 skb->protocol = eth->h_proto; 1014 1015 return dev_queue_xmit(skb); 1016 1017 drop: 1018 kfree_skb(skb); 1019 return -EINVAL; 1020 } 1021 1022 static int input_action_end_dx6_finish(struct net *net, struct sock *sk, 1023 struct sk_buff *skb) 1024 { 1025 struct dst_entry *orig_dst = skb_dst(skb); 1026 struct in6_addr *nhaddr = NULL; 1027 struct seg6_local_lwt *slwt; 1028 1029 slwt = seg6_local_lwtunnel(orig_dst->lwtstate); 1030 1031 /* The inner packet is not associated to any local interface, 1032 * so we do not call netif_rx(). 1033 * 1034 * If slwt->nh6 is set to ::, then lookup the nexthop for the 1035 * inner packet's DA. Otherwise, use the specified nexthop. 1036 */ 1037 if (!ipv6_addr_any(&slwt->nh6)) 1038 nhaddr = &slwt->nh6; 1039 1040 seg6_lookup_nexthop(skb, nhaddr, 0); 1041 1042 return dst_input(skb); 1043 } 1044 1045 static int input_action_end_dx6_finish_nf(struct net *net, struct sock *sk, 1046 struct sk_buff *skb) 1047 { 1048 if (unlikely(!seg6_local_skb_has_lwtstate(skb))) { 1049 kfree_skb(skb); 1050 return -EINVAL; 1051 } 1052 1053 return input_action_end_dx6_finish(net, sk, skb); 1054 } 1055 1056 /* decapsulate and forward to specified nexthop */ 1057 static int input_action_end_dx6(struct sk_buff *skb, 1058 struct seg6_local_lwt *slwt) 1059 { 1060 /* this function accepts IPv6 encapsulated packets, with either 1061 * an SRH with SL=0, or no SRH. 1062 */ 1063 1064 if (!decap_and_validate(skb, IPPROTO_IPV6)) 1065 goto drop; 1066 1067 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) 1068 goto drop; 1069 1070 skb_set_transport_header(skb, sizeof(struct ipv6hdr)); 1071 nf_reset_ct(skb); 1072 1073 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled)) 1074 return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, 1075 dev_net(skb->dev), NULL, skb, skb->dev, 1076 NULL, input_action_end_dx6_finish_nf); 1077 1078 return input_action_end_dx6_finish(dev_net(skb->dev), NULL, skb); 1079 drop: 1080 kfree_skb(skb); 1081 return -EINVAL; 1082 } 1083 1084 /* Validate the length of the IPv4 header at skb->data (which must point to 1085 * the network header) and report that length, options included. 1086 * Only the version, the header length and the presence of the whole header 1087 * are checked. 1088 * The length is written only when SKB_NOT_DROPPED_YET is returned. 1089 * 1090 * skb pointers may change: after this call, the caller must evaluate again 1091 * any pointer into the packet data. 1092 */ 1093 static enum skb_drop_reason 1094 seg6_validate_iphdr_len(struct sk_buff *skb, int *hdrlen) 1095 { 1096 enum skb_drop_reason reason; 1097 const struct iphdr *iph; 1098 int len; 1099 1100 reason = pskb_may_pull_reason(skb, sizeof(struct iphdr)); 1101 if (reason) 1102 return reason; 1103 1104 iph = ip_hdr(skb); 1105 if (iph->version != 4 || iph->ihl < 5) 1106 return SKB_DROP_REASON_IP_INHDR; 1107 1108 len = ip_hdrlen(skb); 1109 1110 reason = pskb_may_pull_reason(skb, len); 1111 if (reason) 1112 return reason; 1113 1114 *hdrlen = len; 1115 return SKB_NOT_DROPPED_YET; 1116 } 1117 1118 static int input_action_end_dx4_finish(struct net *net, struct sock *sk, 1119 struct sk_buff *skb) 1120 { 1121 struct dst_entry *orig_dst = skb_dst(skb); 1122 enum skb_drop_reason reason; 1123 struct seg6_local_lwt *slwt; 1124 struct iphdr *iph; 1125 __be32 nhaddr; 1126 1127 slwt = seg6_local_lwtunnel(orig_dst->lwtstate); 1128 1129 iph = ip_hdr(skb); 1130 1131 nhaddr = slwt->nh4.s_addr ?: iph->daddr; 1132 1133 skb_dst_drop(skb); 1134 1135 reason = ip_route_input(skb, nhaddr, iph->saddr, 0, skb->dev); 1136 if (reason) { 1137 kfree_skb_reason(skb, reason); 1138 return -EINVAL; 1139 } 1140 1141 return dst_input(skb); 1142 } 1143 1144 static int input_action_end_dx4_finish_nf(struct net *net, struct sock *sk, 1145 struct sk_buff *skb) 1146 { 1147 if (unlikely(!seg6_local_skb_has_lwtstate(skb))) { 1148 kfree_skb(skb); 1149 return -EINVAL; 1150 } 1151 1152 return input_action_end_dx4_finish(net, sk, skb); 1153 } 1154 1155 static int input_action_end_dx4(struct sk_buff *skb, 1156 struct seg6_local_lwt *slwt) 1157 { 1158 SKB_DR(reason); 1159 int hdrlen; 1160 1161 if (!decap_and_validate(skb, IPPROTO_IPIP)) 1162 goto drop; 1163 1164 reason = seg6_validate_iphdr_len(skb, &hdrlen); 1165 if (reason) 1166 goto drop; 1167 1168 skb->protocol = htons(ETH_P_IP); 1169 skb_set_transport_header(skb, hdrlen); 1170 nf_reset_ct(skb); 1171 1172 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled)) 1173 return NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, 1174 dev_net(skb->dev), NULL, skb, skb->dev, 1175 NULL, input_action_end_dx4_finish_nf); 1176 1177 return input_action_end_dx4_finish(dev_net(skb->dev), NULL, skb); 1178 drop: 1179 kfree_skb_reason(skb, reason); 1180 return -EINVAL; 1181 } 1182 1183 #ifdef CONFIG_NET_L3_MASTER_DEV 1184 static struct net *fib6_config_get_net(const struct fib6_config *fib6_cfg) 1185 { 1186 const struct nl_info *nli = &fib6_cfg->fc_nlinfo; 1187 1188 return nli->nl_net; 1189 } 1190 1191 static int __seg6_end_dt_vrf_build(struct seg6_local_lwt *slwt, const void *cfg, 1192 u16 family, struct netlink_ext_ack *extack) 1193 { 1194 struct seg6_end_dt_info *info = &slwt->dt_info; 1195 int vrf_ifindex; 1196 struct net *net; 1197 1198 net = fib6_config_get_net(cfg); 1199 1200 /* note that vrf_table was already set by parse_nla_vrftable() */ 1201 vrf_ifindex = l3mdev_ifindex_lookup_by_table_id(L3MDEV_TYPE_VRF, net, 1202 info->vrf_table); 1203 if (vrf_ifindex < 0) { 1204 if (vrf_ifindex == -EPERM) { 1205 NL_SET_ERR_MSG(extack, 1206 "Strict mode for VRF is disabled"); 1207 } else if (vrf_ifindex == -ENODEV) { 1208 NL_SET_ERR_MSG(extack, 1209 "Table has no associated VRF device"); 1210 } else { 1211 pr_debug("seg6local: SRv6 End.DT* creation error=%d\n", 1212 vrf_ifindex); 1213 } 1214 1215 return vrf_ifindex; 1216 } 1217 1218 info->net = net; 1219 info->vrf_ifindex = vrf_ifindex; 1220 1221 info->family = family; 1222 info->mode = DT_VRF_MODE; 1223 1224 return 0; 1225 } 1226 1227 /* The SRv6 End.DT4/DT6 behavior extracts the inner (IPv4/IPv6) packet and 1228 * routes the IPv4/IPv6 packet by looking at the configured routing table. 1229 * 1230 * In the SRv6 End.DT4/DT6 use case, we can receive traffic (IPv6+Segment 1231 * Routing Header packets) from several interfaces and the outer IPv6 1232 * destination address (DA) is used for retrieving the specific instance of the 1233 * End.DT4/DT6 behavior that should process the packets. 1234 * 1235 * However, the inner IPv4/IPv6 packet is not really bound to any receiving 1236 * interface and thus the End.DT4/DT6 sets the VRF (associated with the 1237 * corresponding routing table) as the *receiving* interface. 1238 * In other words, the End.DT4/DT6 processes a packet as if it has been received 1239 * directly by the VRF (and not by one of its slave devices, if any). 1240 * In this way, the VRF interface is used for routing the IPv4/IPv6 packet in 1241 * according to the routing table configured by the End.DT4/DT6 instance. 1242 * 1243 * This design allows you to get some interesting features like: 1244 * 1) the statistics on rx packets; 1245 * 2) the possibility to install a packet sniffer on the receiving interface 1246 * (the VRF one) for looking at the incoming packets; 1247 * 3) the possibility to leverage the netfilter prerouting hook for the inner 1248 * IPv4 packet. 1249 * 1250 * This function returns: 1251 * - the sk_buff* when the VRF rcv handler has processed the packet correctly; 1252 * - NULL when the skb is consumed by the VRF rcv handler; 1253 * - a pointer which encodes a negative error number in case of error. 1254 * Note that in this case, the function takes care of freeing the skb. 1255 */ 1256 static struct sk_buff *end_dt_vrf_rcv(struct sk_buff *skb, u16 family, 1257 struct net_device *dev) 1258 { 1259 /* based on l3mdev_ip_rcv; we are only interested in the master */ 1260 if (unlikely(!netif_is_l3_master(dev) && !netif_has_l3_rx_handler(dev))) 1261 goto drop; 1262 1263 if (unlikely(!dev->l3mdev_ops->l3mdev_l3_rcv)) 1264 goto drop; 1265 1266 /* the decap packet IPv4/IPv6 does not come with any mac header info. 1267 * We must unset the mac header to allow the VRF device to rebuild it, 1268 * just in case there is a sniffer attached on the device. 1269 */ 1270 skb_unset_mac_header(skb); 1271 1272 skb = dev->l3mdev_ops->l3mdev_l3_rcv(dev, skb, family); 1273 if (!skb) 1274 /* the skb buffer was consumed by the handler */ 1275 return NULL; 1276 1277 /* when a packet is received by a VRF or by one of its slaves, the 1278 * master device reference is set into the skb. 1279 */ 1280 if (unlikely(skb->dev != dev || skb->skb_iif != dev->ifindex)) 1281 goto drop; 1282 1283 return skb; 1284 1285 drop: 1286 kfree_skb(skb); 1287 return ERR_PTR(-EINVAL); 1288 } 1289 1290 static struct net_device *end_dt_get_vrf_rcu(struct sk_buff *skb, 1291 struct seg6_end_dt_info *info) 1292 { 1293 int vrf_ifindex = info->vrf_ifindex; 1294 struct net *net = info->net; 1295 1296 if (unlikely(vrf_ifindex < 0)) 1297 goto error; 1298 1299 if (unlikely(!net_eq(dev_net(skb->dev), net))) 1300 goto error; 1301 1302 return dev_get_by_index_rcu(net, vrf_ifindex); 1303 1304 error: 1305 return NULL; 1306 } 1307 1308 static struct sk_buff *end_dt_vrf_core(struct sk_buff *skb, 1309 struct seg6_local_lwt *slwt, u16 family) 1310 { 1311 struct seg6_end_dt_info *info = &slwt->dt_info; 1312 struct net_device *vrf; 1313 __be16 protocol; 1314 SKB_DR(reason); 1315 int hdrlen; 1316 1317 vrf = end_dt_get_vrf_rcu(skb, info); 1318 if (unlikely(!vrf)) 1319 goto drop; 1320 1321 if (unlikely(info->family != AF_UNSPEC && info->family != family)) { 1322 pr_warn_once("seg6local: SRv6 End.DT* family mismatch"); 1323 goto drop; 1324 } 1325 1326 switch (family) { 1327 case AF_INET: 1328 protocol = htons(ETH_P_IP); 1329 reason = seg6_validate_iphdr_len(skb, &hdrlen); 1330 if (unlikely(reason)) 1331 goto drop; 1332 break; 1333 case AF_INET6: 1334 protocol = htons(ETH_P_IPV6); 1335 hdrlen = sizeof(struct ipv6hdr); 1336 break; 1337 case AF_UNSPEC: 1338 fallthrough; 1339 default: 1340 goto drop; 1341 } 1342 1343 skb->protocol = protocol; 1344 1345 skb_dst_drop(skb); 1346 1347 skb_set_transport_header(skb, hdrlen); 1348 nf_reset_ct(skb); 1349 1350 return end_dt_vrf_rcv(skb, family, vrf); 1351 1352 drop: 1353 kfree_skb_reason(skb, reason); 1354 return ERR_PTR(-EINVAL); 1355 } 1356 1357 static int input_action_end_dt4(struct sk_buff *skb, 1358 struct seg6_local_lwt *slwt) 1359 { 1360 enum skb_drop_reason reason; 1361 struct iphdr *iph; 1362 1363 if (!decap_and_validate(skb, IPPROTO_IPIP)) 1364 goto drop; 1365 1366 /* end_dt_vrf_core() validates the inner IPv4 header length */ 1367 skb = end_dt_vrf_core(skb, slwt, AF_INET); 1368 if (!skb) 1369 /* packet has been processed and consumed by the VRF */ 1370 return 0; 1371 1372 if (IS_ERR(skb)) 1373 return PTR_ERR(skb); 1374 1375 iph = ip_hdr(skb); 1376 1377 reason = ip_route_input(skb, iph->daddr, iph->saddr, 0, skb->dev); 1378 if (unlikely(reason)) 1379 goto drop; 1380 1381 return dst_input(skb); 1382 1383 drop: 1384 kfree_skb(skb); 1385 return -EINVAL; 1386 } 1387 1388 static int seg6_end_dt4_build(struct seg6_local_lwt *slwt, const void *cfg, 1389 struct netlink_ext_ack *extack) 1390 { 1391 return __seg6_end_dt_vrf_build(slwt, cfg, AF_INET, extack); 1392 } 1393 1394 static enum 1395 seg6_end_dt_mode seg6_end_dt6_parse_mode(struct seg6_local_lwt *slwt) 1396 { 1397 unsigned long parsed_optattrs = slwt->parsed_optattrs; 1398 bool legacy, vrfmode; 1399 1400 legacy = !!(parsed_optattrs & SEG6_F_ATTR(SEG6_LOCAL_TABLE)); 1401 vrfmode = !!(parsed_optattrs & SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE)); 1402 1403 if (!(legacy ^ vrfmode)) 1404 /* both are absent or present: invalid DT6 mode */ 1405 return DT_INVALID_MODE; 1406 1407 return legacy ? DT_LEGACY_MODE : DT_VRF_MODE; 1408 } 1409 1410 static enum seg6_end_dt_mode seg6_end_dt6_get_mode(struct seg6_local_lwt *slwt) 1411 { 1412 struct seg6_end_dt_info *info = &slwt->dt_info; 1413 1414 return info->mode; 1415 } 1416 1417 static int seg6_end_dt6_build(struct seg6_local_lwt *slwt, const void *cfg, 1418 struct netlink_ext_ack *extack) 1419 { 1420 enum seg6_end_dt_mode mode = seg6_end_dt6_parse_mode(slwt); 1421 struct seg6_end_dt_info *info = &slwt->dt_info; 1422 1423 switch (mode) { 1424 case DT_LEGACY_MODE: 1425 info->mode = DT_LEGACY_MODE; 1426 return 0; 1427 case DT_VRF_MODE: 1428 return __seg6_end_dt_vrf_build(slwt, cfg, AF_INET6, extack); 1429 default: 1430 NL_SET_ERR_MSG(extack, "table or vrftable must be specified"); 1431 return -EINVAL; 1432 } 1433 } 1434 #endif 1435 1436 static int input_action_end_dt6(struct sk_buff *skb, 1437 struct seg6_local_lwt *slwt) 1438 { 1439 if (!decap_and_validate(skb, IPPROTO_IPV6)) 1440 goto drop; 1441 1442 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) 1443 goto drop; 1444 1445 #ifdef CONFIG_NET_L3_MASTER_DEV 1446 if (seg6_end_dt6_get_mode(slwt) == DT_LEGACY_MODE) 1447 goto legacy_mode; 1448 1449 /* DT6_VRF_MODE */ 1450 skb = end_dt_vrf_core(skb, slwt, AF_INET6); 1451 if (!skb) 1452 /* packet has been processed and consumed by the VRF */ 1453 return 0; 1454 1455 if (IS_ERR(skb)) 1456 return PTR_ERR(skb); 1457 1458 /* note: this time we do not need to specify the table because the VRF 1459 * takes care of selecting the correct table. 1460 */ 1461 seg6_lookup_any_nexthop(skb, NULL, 0, true, 0); 1462 1463 return dst_input(skb); 1464 1465 legacy_mode: 1466 #endif 1467 skb_set_transport_header(skb, sizeof(struct ipv6hdr)); 1468 1469 seg6_lookup_any_nexthop(skb, NULL, slwt->table, true, 0); 1470 1471 return dst_input(skb); 1472 1473 drop: 1474 kfree_skb(skb); 1475 return -EINVAL; 1476 } 1477 1478 #ifdef CONFIG_NET_L3_MASTER_DEV 1479 static int seg6_end_dt46_build(struct seg6_local_lwt *slwt, const void *cfg, 1480 struct netlink_ext_ack *extack) 1481 { 1482 return __seg6_end_dt_vrf_build(slwt, cfg, AF_UNSPEC, extack); 1483 } 1484 1485 static int input_action_end_dt46(struct sk_buff *skb, 1486 struct seg6_local_lwt *slwt) 1487 { 1488 unsigned int off = 0; 1489 int nexthdr; 1490 1491 nexthdr = ipv6_find_hdr(skb, &off, -1, NULL, NULL); 1492 if (unlikely(nexthdr < 0)) 1493 goto drop; 1494 1495 switch (nexthdr) { 1496 case IPPROTO_IPIP: 1497 return input_action_end_dt4(skb, slwt); 1498 case IPPROTO_IPV6: 1499 return input_action_end_dt6(skb, slwt); 1500 } 1501 1502 drop: 1503 kfree_skb(skb); 1504 return -EINVAL; 1505 } 1506 #endif 1507 1508 /* push an SRH on top of the current one */ 1509 static int input_action_end_b6(struct sk_buff *skb, struct seg6_local_lwt *slwt) 1510 { 1511 struct ipv6_sr_hdr *srh; 1512 int err = -EINVAL; 1513 1514 srh = get_and_validate_srh(skb); 1515 if (!srh) 1516 goto drop; 1517 1518 err = seg6_do_srh_inline(skb, slwt->srh); 1519 if (err) 1520 goto drop; 1521 1522 skb_set_transport_header(skb, sizeof(struct ipv6hdr)); 1523 1524 seg6_lookup_nexthop(skb, NULL, 0); 1525 1526 return dst_input(skb); 1527 1528 drop: 1529 kfree_skb(skb); 1530 return err; 1531 } 1532 1533 /* encapsulate within an outer IPv6 header and a specified SRH */ 1534 static int input_action_end_b6_encap(struct sk_buff *skb, 1535 struct seg6_local_lwt *slwt) 1536 { 1537 enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED; 1538 struct ipv6_sr_hdr *srh; 1539 int err = -EINVAL; 1540 1541 srh = get_and_validate_srh(skb); 1542 if (!srh) 1543 goto drop; 1544 1545 srh = advance_nextseg(skb, srh, &reason); 1546 if (!srh) { 1547 err = -ENOMEM; 1548 goto drop; 1549 } 1550 1551 skb_reset_inner_headers(skb); 1552 skb->encapsulation = 1; 1553 1554 err = seg6_do_srh_encap(skb, slwt->srh, IPPROTO_IPV6); 1555 if (err) 1556 goto drop; 1557 1558 skb_set_transport_header(skb, sizeof(struct ipv6hdr)); 1559 1560 seg6_lookup_nexthop(skb, NULL, 0); 1561 1562 return dst_input(skb); 1563 1564 drop: 1565 kfree_skb_reason(skb, reason); 1566 return err; 1567 } 1568 1569 DEFINE_PER_CPU(struct seg6_bpf_srh_state, seg6_bpf_srh_states) = { 1570 .bh_lock = INIT_LOCAL_LOCK(bh_lock), 1571 }; 1572 1573 bool seg6_bpf_has_valid_srh(struct sk_buff *skb) 1574 { 1575 struct seg6_bpf_srh_state *srh_state = 1576 this_cpu_ptr(&seg6_bpf_srh_states); 1577 struct ipv6_sr_hdr *srh = srh_state->srh; 1578 1579 lockdep_assert_held(&srh_state->bh_lock); 1580 if (unlikely(srh == NULL)) 1581 return false; 1582 1583 if (unlikely(!srh_state->valid)) { 1584 if ((srh_state->hdrlen & 7) != 0) 1585 return false; 1586 1587 srh->hdrlen = (u8)(srh_state->hdrlen >> 3); 1588 if (!seg6_validate_srh(srh, (srh->hdrlen + 1) << 3, true)) 1589 return false; 1590 1591 srh_state->valid = true; 1592 } 1593 1594 return true; 1595 } 1596 1597 static int input_action_end_bpf(struct sk_buff *skb, 1598 struct seg6_local_lwt *slwt) 1599 { 1600 struct seg6_bpf_srh_state *srh_state; 1601 enum skb_drop_reason reason; 1602 struct ipv6_sr_hdr *srh; 1603 int ret; 1604 1605 srh = get_and_validate_srh(skb); 1606 if (!srh) { 1607 kfree_skb(skb); 1608 return -EINVAL; 1609 } 1610 srh = advance_nextseg(skb, srh, &reason); 1611 if (!srh) { 1612 kfree_skb_reason(skb, reason); 1613 return -ENOMEM; 1614 } 1615 1616 /* The access to the per-CPU buffer srh_state is protected by running 1617 * always in softirq context (with disabled BH). On PREEMPT_RT the 1618 * required locking is provided by the following local_lock_nested_bh() 1619 * statement. It is also accessed by the bpf_lwt_seg6_* helpers via 1620 * bpf_prog_run_save_cb(). 1621 */ 1622 local_lock_nested_bh(&seg6_bpf_srh_states.bh_lock); 1623 srh_state = this_cpu_ptr(&seg6_bpf_srh_states); 1624 srh_state->srh = srh; 1625 srh_state->hdrlen = srh->hdrlen << 3; 1626 srh_state->valid = true; 1627 1628 rcu_read_lock(); 1629 bpf_compute_data_pointers(skb); 1630 ret = bpf_prog_run_save_cb(slwt->bpf.prog, skb); 1631 rcu_read_unlock(); 1632 1633 switch (ret) { 1634 case BPF_OK: 1635 case BPF_REDIRECT: 1636 break; 1637 case BPF_DROP: 1638 goto drop; 1639 default: 1640 pr_warn_once("bpf-seg6local: Illegal return value %u\n", ret); 1641 goto drop; 1642 } 1643 1644 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb)) 1645 goto drop; 1646 local_unlock_nested_bh(&seg6_bpf_srh_states.bh_lock); 1647 1648 if (ret != BPF_REDIRECT) 1649 seg6_lookup_nexthop(skb, NULL, 0); 1650 1651 return dst_input(skb); 1652 1653 drop: 1654 local_unlock_nested_bh(&seg6_bpf_srh_states.bh_lock); 1655 kfree_skb(skb); 1656 return -EINVAL; 1657 } 1658 1659 static struct seg6_action_desc seg6_action_table[] = { 1660 { 1661 .action = SEG6_LOCAL_ACTION_END, 1662 .attrs = 0, 1663 .optattrs = SEG6_F_LOCAL_COUNTERS | 1664 SEG6_F_LOCAL_FLAVORS, 1665 .input = input_action_end, 1666 }, 1667 { 1668 .action = SEG6_LOCAL_ACTION_END_X, 1669 .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH6), 1670 .optattrs = SEG6_F_LOCAL_COUNTERS | 1671 SEG6_F_LOCAL_FLAVORS | 1672 SEG6_F_ATTR(SEG6_LOCAL_OIF), 1673 .input = input_action_end_x, 1674 }, 1675 { 1676 .action = SEG6_LOCAL_ACTION_END_T, 1677 .attrs = SEG6_F_ATTR(SEG6_LOCAL_TABLE), 1678 .optattrs = SEG6_F_LOCAL_COUNTERS, 1679 .input = input_action_end_t, 1680 }, 1681 { 1682 .action = SEG6_LOCAL_ACTION_END_DX2, 1683 .attrs = SEG6_F_ATTR(SEG6_LOCAL_OIF), 1684 .optattrs = SEG6_F_LOCAL_COUNTERS, 1685 .input = input_action_end_dx2, 1686 }, 1687 { 1688 .action = SEG6_LOCAL_ACTION_END_DX6, 1689 .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH6), 1690 .optattrs = SEG6_F_LOCAL_COUNTERS, 1691 .input = input_action_end_dx6, 1692 }, 1693 { 1694 .action = SEG6_LOCAL_ACTION_END_DX4, 1695 .attrs = SEG6_F_ATTR(SEG6_LOCAL_NH4), 1696 .optattrs = SEG6_F_LOCAL_COUNTERS, 1697 .input = input_action_end_dx4, 1698 }, 1699 { 1700 .action = SEG6_LOCAL_ACTION_END_DT4, 1701 .attrs = SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE), 1702 .optattrs = SEG6_F_LOCAL_COUNTERS, 1703 #ifdef CONFIG_NET_L3_MASTER_DEV 1704 .input = input_action_end_dt4, 1705 .slwt_ops = { 1706 .build_state = seg6_end_dt4_build, 1707 }, 1708 #endif 1709 }, 1710 { 1711 .action = SEG6_LOCAL_ACTION_END_DT6, 1712 #ifdef CONFIG_NET_L3_MASTER_DEV 1713 .attrs = 0, 1714 .optattrs = SEG6_F_LOCAL_COUNTERS | 1715 SEG6_F_ATTR(SEG6_LOCAL_TABLE) | 1716 SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE), 1717 .slwt_ops = { 1718 .build_state = seg6_end_dt6_build, 1719 }, 1720 #else 1721 .attrs = SEG6_F_ATTR(SEG6_LOCAL_TABLE), 1722 .optattrs = SEG6_F_LOCAL_COUNTERS, 1723 #endif 1724 .input = input_action_end_dt6, 1725 }, 1726 { 1727 .action = SEG6_LOCAL_ACTION_END_DT46, 1728 .attrs = SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE), 1729 .optattrs = SEG6_F_LOCAL_COUNTERS, 1730 #ifdef CONFIG_NET_L3_MASTER_DEV 1731 .input = input_action_end_dt46, 1732 .slwt_ops = { 1733 .build_state = seg6_end_dt46_build, 1734 }, 1735 #endif 1736 }, 1737 { 1738 .action = SEG6_LOCAL_ACTION_END_B6, 1739 .attrs = SEG6_F_ATTR(SEG6_LOCAL_SRH), 1740 .optattrs = SEG6_F_LOCAL_COUNTERS, 1741 .input = input_action_end_b6, 1742 }, 1743 { 1744 .action = SEG6_LOCAL_ACTION_END_B6_ENCAP, 1745 .attrs = SEG6_F_ATTR(SEG6_LOCAL_SRH), 1746 .optattrs = SEG6_F_LOCAL_COUNTERS, 1747 .input = input_action_end_b6_encap, 1748 .static_headroom = sizeof(struct ipv6hdr), 1749 }, 1750 { 1751 .action = SEG6_LOCAL_ACTION_END_BPF, 1752 .attrs = SEG6_F_ATTR(SEG6_LOCAL_BPF), 1753 .optattrs = SEG6_F_LOCAL_COUNTERS, 1754 .input = input_action_end_bpf, 1755 }, 1756 1757 }; 1758 1759 static struct seg6_action_desc *__get_action_desc(int action) 1760 { 1761 struct seg6_action_desc *desc; 1762 int i, count; 1763 1764 count = ARRAY_SIZE(seg6_action_table); 1765 for (i = 0; i < count; i++) { 1766 desc = &seg6_action_table[i]; 1767 if (desc->action == action) 1768 return desc; 1769 } 1770 1771 return NULL; 1772 } 1773 1774 static bool seg6_lwtunnel_counters_enabled(struct seg6_local_lwt *slwt) 1775 { 1776 return slwt->parsed_optattrs & SEG6_F_LOCAL_COUNTERS; 1777 } 1778 1779 static void seg6_local_update_counters(struct seg6_local_lwt *slwt, 1780 unsigned int len, int err) 1781 { 1782 struct pcpu_seg6_local_counters *pcounters; 1783 1784 pcounters = this_cpu_ptr(slwt->pcpu_counters); 1785 u64_stats_update_begin(&pcounters->syncp); 1786 1787 if (likely(!err)) { 1788 u64_stats_inc(&pcounters->packets); 1789 u64_stats_add(&pcounters->bytes, len); 1790 } else { 1791 u64_stats_inc(&pcounters->errors); 1792 } 1793 1794 u64_stats_update_end(&pcounters->syncp); 1795 } 1796 1797 static int seg6_local_input_core(struct net *net, struct sock *sk, 1798 struct sk_buff *skb) 1799 { 1800 struct dst_entry *orig_dst = skb_dst(skb); 1801 struct seg6_action_desc *desc; 1802 struct seg6_local_lwt *slwt; 1803 unsigned int len = skb->len; 1804 int rc; 1805 1806 slwt = seg6_local_lwtunnel(orig_dst->lwtstate); 1807 desc = slwt->desc; 1808 1809 rc = desc->input(skb, slwt); 1810 1811 if (!seg6_lwtunnel_counters_enabled(slwt)) 1812 return rc; 1813 1814 seg6_local_update_counters(slwt, len, rc); 1815 1816 return rc; 1817 } 1818 1819 static int seg6_local_input_core_nf(struct net *net, struct sock *sk, 1820 struct sk_buff *skb) 1821 { 1822 if (unlikely(!seg6_local_skb_has_lwtstate(skb))) { 1823 kfree_skb(skb); 1824 return -EINVAL; 1825 } 1826 1827 return seg6_local_input_core(net, sk, skb); 1828 } 1829 1830 static int seg6_local_input(struct sk_buff *skb) 1831 { 1832 if (skb->protocol != htons(ETH_P_IPV6)) { 1833 kfree_skb(skb); 1834 return -EINVAL; 1835 } 1836 1837 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled)) 1838 return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN, 1839 dev_net(skb->dev), NULL, skb, skb->dev, NULL, 1840 seg6_local_input_core_nf); 1841 1842 return seg6_local_input_core(dev_net(skb->dev), NULL, skb); 1843 } 1844 1845 static const struct nla_policy seg6_local_policy[SEG6_LOCAL_MAX + 1] = { 1846 [SEG6_LOCAL_ACTION] = { .type = NLA_U32 }, 1847 [SEG6_LOCAL_SRH] = { .type = NLA_BINARY }, 1848 [SEG6_LOCAL_TABLE] = { .type = NLA_U32 }, 1849 [SEG6_LOCAL_VRFTABLE] = { .type = NLA_U32 }, 1850 [SEG6_LOCAL_NH4] = NLA_POLICY_EXACT_LEN(sizeof(struct in_addr)), 1851 [SEG6_LOCAL_NH6] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)), 1852 [SEG6_LOCAL_IIF] = { .type = NLA_U32 }, 1853 [SEG6_LOCAL_OIF] = { .type = NLA_U32 }, 1854 [SEG6_LOCAL_BPF] = { .type = NLA_NESTED }, 1855 [SEG6_LOCAL_COUNTERS] = { .type = NLA_NESTED }, 1856 [SEG6_LOCAL_FLAVORS] = { .type = NLA_NESTED }, 1857 }; 1858 1859 static int parse_nla_srh(struct nlattr **attrs, struct seg6_local_lwt *slwt, 1860 struct netlink_ext_ack *extack) 1861 { 1862 struct ipv6_sr_hdr *srh; 1863 int len; 1864 1865 srh = nla_data(attrs[SEG6_LOCAL_SRH]); 1866 len = nla_len(attrs[SEG6_LOCAL_SRH]); 1867 1868 /* SRH must contain at least one segment */ 1869 if (len < sizeof(*srh) + sizeof(struct in6_addr)) 1870 return -EINVAL; 1871 1872 if (!seg6_validate_srh(srh, len, false)) 1873 return -EINVAL; 1874 1875 slwt->srh = kmemdup(srh, len, GFP_KERNEL); 1876 if (!slwt->srh) 1877 return -ENOMEM; 1878 1879 slwt->headroom += len; 1880 1881 return 0; 1882 } 1883 1884 static int put_nla_srh(struct sk_buff *skb, struct seg6_local_lwt *slwt) 1885 { 1886 struct ipv6_sr_hdr *srh; 1887 struct nlattr *nla; 1888 int len; 1889 1890 srh = slwt->srh; 1891 len = (srh->hdrlen + 1) << 3; 1892 1893 nla = nla_reserve(skb, SEG6_LOCAL_SRH, len); 1894 if (!nla) 1895 return -EMSGSIZE; 1896 1897 memcpy(nla_data(nla), srh, len); 1898 1899 return 0; 1900 } 1901 1902 static int cmp_nla_srh(struct seg6_local_lwt *a, struct seg6_local_lwt *b) 1903 { 1904 int len = (a->srh->hdrlen + 1) << 3; 1905 1906 if (len != ((b->srh->hdrlen + 1) << 3)) 1907 return 1; 1908 1909 return memcmp(a->srh, b->srh, len); 1910 } 1911 1912 static void destroy_attr_srh(struct seg6_local_lwt *slwt) 1913 { 1914 kfree(slwt->srh); 1915 } 1916 1917 static int parse_nla_table(struct nlattr **attrs, struct seg6_local_lwt *slwt, 1918 struct netlink_ext_ack *extack) 1919 { 1920 slwt->table = nla_get_u32(attrs[SEG6_LOCAL_TABLE]); 1921 1922 return 0; 1923 } 1924 1925 static int put_nla_table(struct sk_buff *skb, struct seg6_local_lwt *slwt) 1926 { 1927 if (nla_put_u32(skb, SEG6_LOCAL_TABLE, slwt->table)) 1928 return -EMSGSIZE; 1929 1930 return 0; 1931 } 1932 1933 static int cmp_nla_table(struct seg6_local_lwt *a, struct seg6_local_lwt *b) 1934 { 1935 if (a->table != b->table) 1936 return 1; 1937 1938 return 0; 1939 } 1940 1941 static struct 1942 seg6_end_dt_info *seg6_possible_end_dt_info(struct seg6_local_lwt *slwt) 1943 { 1944 #ifdef CONFIG_NET_L3_MASTER_DEV 1945 return &slwt->dt_info; 1946 #else 1947 return ERR_PTR(-EOPNOTSUPP); 1948 #endif 1949 } 1950 1951 static int parse_nla_vrftable(struct nlattr **attrs, 1952 struct seg6_local_lwt *slwt, 1953 struct netlink_ext_ack *extack) 1954 { 1955 struct seg6_end_dt_info *info = seg6_possible_end_dt_info(slwt); 1956 1957 if (IS_ERR(info)) 1958 return PTR_ERR(info); 1959 1960 info->vrf_table = nla_get_u32(attrs[SEG6_LOCAL_VRFTABLE]); 1961 1962 return 0; 1963 } 1964 1965 static int put_nla_vrftable(struct sk_buff *skb, struct seg6_local_lwt *slwt) 1966 { 1967 struct seg6_end_dt_info *info = seg6_possible_end_dt_info(slwt); 1968 1969 if (IS_ERR(info)) 1970 return PTR_ERR(info); 1971 1972 if (nla_put_u32(skb, SEG6_LOCAL_VRFTABLE, info->vrf_table)) 1973 return -EMSGSIZE; 1974 1975 return 0; 1976 } 1977 1978 static int cmp_nla_vrftable(struct seg6_local_lwt *a, struct seg6_local_lwt *b) 1979 { 1980 struct seg6_end_dt_info *info_a = seg6_possible_end_dt_info(a); 1981 struct seg6_end_dt_info *info_b = seg6_possible_end_dt_info(b); 1982 1983 if (info_a->vrf_table != info_b->vrf_table) 1984 return 1; 1985 1986 return 0; 1987 } 1988 1989 static int parse_nla_nh4(struct nlattr **attrs, struct seg6_local_lwt *slwt, 1990 struct netlink_ext_ack *extack) 1991 { 1992 memcpy(&slwt->nh4, nla_data(attrs[SEG6_LOCAL_NH4]), 1993 sizeof(struct in_addr)); 1994 1995 return 0; 1996 } 1997 1998 static int put_nla_nh4(struct sk_buff *skb, struct seg6_local_lwt *slwt) 1999 { 2000 struct nlattr *nla; 2001 2002 nla = nla_reserve(skb, SEG6_LOCAL_NH4, sizeof(struct in_addr)); 2003 if (!nla) 2004 return -EMSGSIZE; 2005 2006 memcpy(nla_data(nla), &slwt->nh4, sizeof(struct in_addr)); 2007 2008 return 0; 2009 } 2010 2011 static int cmp_nla_nh4(struct seg6_local_lwt *a, struct seg6_local_lwt *b) 2012 { 2013 return memcmp(&a->nh4, &b->nh4, sizeof(struct in_addr)); 2014 } 2015 2016 static int parse_nla_nh6(struct nlattr **attrs, struct seg6_local_lwt *slwt, 2017 struct netlink_ext_ack *extack) 2018 { 2019 memcpy(&slwt->nh6, nla_data(attrs[SEG6_LOCAL_NH6]), 2020 sizeof(struct in6_addr)); 2021 2022 return 0; 2023 } 2024 2025 static int put_nla_nh6(struct sk_buff *skb, struct seg6_local_lwt *slwt) 2026 { 2027 struct nlattr *nla; 2028 2029 nla = nla_reserve(skb, SEG6_LOCAL_NH6, sizeof(struct in6_addr)); 2030 if (!nla) 2031 return -EMSGSIZE; 2032 2033 memcpy(nla_data(nla), &slwt->nh6, sizeof(struct in6_addr)); 2034 2035 return 0; 2036 } 2037 2038 static int cmp_nla_nh6(struct seg6_local_lwt *a, struct seg6_local_lwt *b) 2039 { 2040 return memcmp(&a->nh6, &b->nh6, sizeof(struct in6_addr)); 2041 } 2042 2043 static int parse_nla_iif(struct nlattr **attrs, struct seg6_local_lwt *slwt, 2044 struct netlink_ext_ack *extack) 2045 { 2046 slwt->iif = nla_get_u32(attrs[SEG6_LOCAL_IIF]); 2047 2048 return 0; 2049 } 2050 2051 static int put_nla_iif(struct sk_buff *skb, struct seg6_local_lwt *slwt) 2052 { 2053 if (nla_put_u32(skb, SEG6_LOCAL_IIF, slwt->iif)) 2054 return -EMSGSIZE; 2055 2056 return 0; 2057 } 2058 2059 static int cmp_nla_iif(struct seg6_local_lwt *a, struct seg6_local_lwt *b) 2060 { 2061 if (a->iif != b->iif) 2062 return 1; 2063 2064 return 0; 2065 } 2066 2067 static int parse_nla_oif(struct nlattr **attrs, struct seg6_local_lwt *slwt, 2068 struct netlink_ext_ack *extack) 2069 { 2070 slwt->oif = nla_get_u32(attrs[SEG6_LOCAL_OIF]); 2071 2072 return 0; 2073 } 2074 2075 static int put_nla_oif(struct sk_buff *skb, struct seg6_local_lwt *slwt) 2076 { 2077 if (nla_put_u32(skb, SEG6_LOCAL_OIF, slwt->oif)) 2078 return -EMSGSIZE; 2079 2080 return 0; 2081 } 2082 2083 static int cmp_nla_oif(struct seg6_local_lwt *a, struct seg6_local_lwt *b) 2084 { 2085 if (a->oif != b->oif) 2086 return 1; 2087 2088 return 0; 2089 } 2090 2091 #define MAX_PROG_NAME 256 2092 static const struct nla_policy bpf_prog_policy[SEG6_LOCAL_BPF_PROG_MAX + 1] = { 2093 [SEG6_LOCAL_BPF_PROG] = { .type = NLA_U32, }, 2094 [SEG6_LOCAL_BPF_PROG_NAME] = { .type = NLA_NUL_STRING, 2095 .len = MAX_PROG_NAME }, 2096 }; 2097 2098 static int parse_nla_bpf(struct nlattr **attrs, struct seg6_local_lwt *slwt, 2099 struct netlink_ext_ack *extack) 2100 { 2101 struct nlattr *tb[SEG6_LOCAL_BPF_PROG_MAX + 1]; 2102 struct bpf_prog *p; 2103 int ret; 2104 u32 fd; 2105 2106 ret = nla_parse_nested_deprecated(tb, SEG6_LOCAL_BPF_PROG_MAX, 2107 attrs[SEG6_LOCAL_BPF], 2108 bpf_prog_policy, NULL); 2109 if (ret < 0) 2110 return ret; 2111 2112 if (!tb[SEG6_LOCAL_BPF_PROG] || !tb[SEG6_LOCAL_BPF_PROG_NAME]) 2113 return -EINVAL; 2114 2115 slwt->bpf.name = nla_memdup(tb[SEG6_LOCAL_BPF_PROG_NAME], GFP_KERNEL); 2116 if (!slwt->bpf.name) 2117 return -ENOMEM; 2118 2119 fd = nla_get_u32(tb[SEG6_LOCAL_BPF_PROG]); 2120 p = bpf_prog_get_type(fd, BPF_PROG_TYPE_LWT_SEG6LOCAL); 2121 if (IS_ERR(p)) { 2122 kfree(slwt->bpf.name); 2123 return PTR_ERR(p); 2124 } 2125 2126 slwt->bpf.prog = p; 2127 return 0; 2128 } 2129 2130 static int put_nla_bpf(struct sk_buff *skb, struct seg6_local_lwt *slwt) 2131 { 2132 struct nlattr *nest; 2133 2134 if (!slwt->bpf.prog) 2135 return 0; 2136 2137 nest = nla_nest_start_noflag(skb, SEG6_LOCAL_BPF); 2138 if (!nest) 2139 return -EMSGSIZE; 2140 2141 if (nla_put_u32(skb, SEG6_LOCAL_BPF_PROG, slwt->bpf.prog->aux->id)) 2142 return -EMSGSIZE; 2143 2144 if (slwt->bpf.name && 2145 nla_put_string(skb, SEG6_LOCAL_BPF_PROG_NAME, slwt->bpf.name)) 2146 return -EMSGSIZE; 2147 2148 return nla_nest_end(skb, nest); 2149 } 2150 2151 static int cmp_nla_bpf(struct seg6_local_lwt *a, struct seg6_local_lwt *b) 2152 { 2153 if (!a->bpf.name && !b->bpf.name) 2154 return 0; 2155 2156 if (!a->bpf.name || !b->bpf.name) 2157 return 1; 2158 2159 return strcmp(a->bpf.name, b->bpf.name); 2160 } 2161 2162 static void destroy_attr_bpf(struct seg6_local_lwt *slwt) 2163 { 2164 kfree(slwt->bpf.name); 2165 if (slwt->bpf.prog) 2166 bpf_prog_put(slwt->bpf.prog); 2167 } 2168 2169 static const struct 2170 nla_policy seg6_local_counters_policy[SEG6_LOCAL_CNT_MAX + 1] = { 2171 [SEG6_LOCAL_CNT_PACKETS] = { .type = NLA_U64 }, 2172 [SEG6_LOCAL_CNT_BYTES] = { .type = NLA_U64 }, 2173 [SEG6_LOCAL_CNT_ERRORS] = { .type = NLA_U64 }, 2174 }; 2175 2176 static int parse_nla_counters(struct nlattr **attrs, 2177 struct seg6_local_lwt *slwt, 2178 struct netlink_ext_ack *extack) 2179 { 2180 struct pcpu_seg6_local_counters __percpu *pcounters; 2181 struct nlattr *tb[SEG6_LOCAL_CNT_MAX + 1]; 2182 int ret; 2183 2184 ret = nla_parse_nested_deprecated(tb, SEG6_LOCAL_CNT_MAX, 2185 attrs[SEG6_LOCAL_COUNTERS], 2186 seg6_local_counters_policy, NULL); 2187 if (ret < 0) 2188 return ret; 2189 2190 /* basic support for SRv6 Behavior counters requires at least: 2191 * packets, bytes and errors. 2192 */ 2193 if (!tb[SEG6_LOCAL_CNT_PACKETS] || !tb[SEG6_LOCAL_CNT_BYTES] || 2194 !tb[SEG6_LOCAL_CNT_ERRORS]) 2195 return -EINVAL; 2196 2197 /* counters are always zero initialized */ 2198 pcounters = seg6_local_alloc_pcpu_counters(GFP_KERNEL); 2199 if (!pcounters) 2200 return -ENOMEM; 2201 2202 slwt->pcpu_counters = pcounters; 2203 2204 return 0; 2205 } 2206 2207 static int seg6_local_fill_nla_counters(struct sk_buff *skb, 2208 struct seg6_local_counters *counters) 2209 { 2210 if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_PACKETS, counters->packets, 2211 SEG6_LOCAL_CNT_PAD)) 2212 return -EMSGSIZE; 2213 2214 if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_BYTES, counters->bytes, 2215 SEG6_LOCAL_CNT_PAD)) 2216 return -EMSGSIZE; 2217 2218 if (nla_put_u64_64bit(skb, SEG6_LOCAL_CNT_ERRORS, counters->errors, 2219 SEG6_LOCAL_CNT_PAD)) 2220 return -EMSGSIZE; 2221 2222 return 0; 2223 } 2224 2225 static int put_nla_counters(struct sk_buff *skb, struct seg6_local_lwt *slwt) 2226 { 2227 struct seg6_local_counters counters = { 0, 0, 0 }; 2228 struct nlattr *nest; 2229 int rc, i; 2230 2231 nest = nla_nest_start(skb, SEG6_LOCAL_COUNTERS); 2232 if (!nest) 2233 return -EMSGSIZE; 2234 2235 for_each_possible_cpu(i) { 2236 struct pcpu_seg6_local_counters *pcounters; 2237 u64 packets, bytes, errors; 2238 unsigned int start; 2239 2240 pcounters = per_cpu_ptr(slwt->pcpu_counters, i); 2241 do { 2242 start = u64_stats_fetch_begin(&pcounters->syncp); 2243 2244 packets = u64_stats_read(&pcounters->packets); 2245 bytes = u64_stats_read(&pcounters->bytes); 2246 errors = u64_stats_read(&pcounters->errors); 2247 2248 } while (u64_stats_fetch_retry(&pcounters->syncp, start)); 2249 2250 counters.packets += packets; 2251 counters.bytes += bytes; 2252 counters.errors += errors; 2253 } 2254 2255 rc = seg6_local_fill_nla_counters(skb, &counters); 2256 if (rc < 0) { 2257 nla_nest_cancel(skb, nest); 2258 return rc; 2259 } 2260 2261 return nla_nest_end(skb, nest); 2262 } 2263 2264 static int cmp_nla_counters(struct seg6_local_lwt *a, struct seg6_local_lwt *b) 2265 { 2266 /* a and b are equal if both have pcpu_counters set or not */ 2267 return (!!((unsigned long)a->pcpu_counters)) ^ 2268 (!!((unsigned long)b->pcpu_counters)); 2269 } 2270 2271 static void destroy_attr_counters(struct seg6_local_lwt *slwt) 2272 { 2273 free_percpu(slwt->pcpu_counters); 2274 } 2275 2276 static const 2277 struct nla_policy seg6_local_flavors_policy[SEG6_LOCAL_FLV_MAX + 1] = { 2278 [SEG6_LOCAL_FLV_OPERATION] = { .type = NLA_U32 }, 2279 [SEG6_LOCAL_FLV_LCBLOCK_BITS] = { .type = NLA_U8 }, 2280 [SEG6_LOCAL_FLV_LCNODE_FN_BITS] = { .type = NLA_U8 }, 2281 }; 2282 2283 /* check whether the lengths of the Locator-Block and Locator-Node Function 2284 * are compatible with the dimension of a C-SID container. 2285 */ 2286 static int seg6_chk_next_csid_cfg(__u8 block_len, __u8 func_len) 2287 { 2288 /* Locator-Block and Locator-Node Function cannot exceed 128 bits 2289 * (i.e. C-SID container length). 2290 */ 2291 if (next_csid_chk_cntr_bits(block_len, func_len)) 2292 return -EINVAL; 2293 2294 /* Locator-Block length must be greater than zero and evenly divisible 2295 * by 8. There must be room for a Locator-Node Function, at least. 2296 */ 2297 if (next_csid_chk_lcblock_bits(block_len)) 2298 return -EINVAL; 2299 2300 /* Locator-Node Function length must be greater than zero and evenly 2301 * divisible by 8. There must be room for the Locator-Block. 2302 */ 2303 if (next_csid_chk_lcnode_fn_bits(func_len)) 2304 return -EINVAL; 2305 2306 return 0; 2307 } 2308 2309 static int seg6_parse_nla_next_csid_cfg(struct nlattr **tb, 2310 struct seg6_flavors_info *finfo, 2311 struct netlink_ext_ack *extack) 2312 { 2313 __u8 func_len = SEG6_LOCAL_LCNODE_FN_DBITS; 2314 __u8 block_len = SEG6_LOCAL_LCBLOCK_DBITS; 2315 int rc; 2316 2317 if (tb[SEG6_LOCAL_FLV_LCBLOCK_BITS]) 2318 block_len = nla_get_u8(tb[SEG6_LOCAL_FLV_LCBLOCK_BITS]); 2319 2320 if (tb[SEG6_LOCAL_FLV_LCNODE_FN_BITS]) 2321 func_len = nla_get_u8(tb[SEG6_LOCAL_FLV_LCNODE_FN_BITS]); 2322 2323 rc = seg6_chk_next_csid_cfg(block_len, func_len); 2324 if (rc < 0) { 2325 NL_SET_ERR_MSG(extack, 2326 "Invalid Locator Block/Node Function lengths"); 2327 return rc; 2328 } 2329 2330 finfo->lcblock_bits = block_len; 2331 finfo->lcnode_func_bits = func_len; 2332 2333 return 0; 2334 } 2335 2336 static int parse_nla_flavors(struct nlattr **attrs, struct seg6_local_lwt *slwt, 2337 struct netlink_ext_ack *extack) 2338 { 2339 struct seg6_flavors_info *finfo = &slwt->flv_info; 2340 struct nlattr *tb[SEG6_LOCAL_FLV_MAX + 1]; 2341 int action = slwt->action; 2342 __u32 fops, supp_fops; 2343 int rc; 2344 2345 rc = nla_parse_nested_deprecated(tb, SEG6_LOCAL_FLV_MAX, 2346 attrs[SEG6_LOCAL_FLAVORS], 2347 seg6_local_flavors_policy, NULL); 2348 if (rc < 0) 2349 return rc; 2350 2351 /* this attribute MUST always be present since it represents the Flavor 2352 * operation(s) to be carried out. 2353 */ 2354 if (!tb[SEG6_LOCAL_FLV_OPERATION]) 2355 return -EINVAL; 2356 2357 fops = nla_get_u32(tb[SEG6_LOCAL_FLV_OPERATION]); 2358 rc = seg6_flv_supp_ops_by_action(action, &supp_fops); 2359 if (rc < 0 || (fops & ~supp_fops)) { 2360 NL_SET_ERR_MSG(extack, "Unsupported Flavor operation(s)"); 2361 return -EOPNOTSUPP; 2362 } 2363 2364 finfo->flv_ops = fops; 2365 2366 if (seg6_next_csid_enabled(fops)) { 2367 /* Locator-Block and Locator-Node Function lengths can be 2368 * provided by the user space. Otherwise, default values are 2369 * applied. 2370 */ 2371 rc = seg6_parse_nla_next_csid_cfg(tb, finfo, extack); 2372 if (rc < 0) 2373 return rc; 2374 } 2375 2376 return 0; 2377 } 2378 2379 static int seg6_fill_nla_next_csid_cfg(struct sk_buff *skb, 2380 struct seg6_flavors_info *finfo) 2381 { 2382 if (nla_put_u8(skb, SEG6_LOCAL_FLV_LCBLOCK_BITS, finfo->lcblock_bits)) 2383 return -EMSGSIZE; 2384 2385 if (nla_put_u8(skb, SEG6_LOCAL_FLV_LCNODE_FN_BITS, 2386 finfo->lcnode_func_bits)) 2387 return -EMSGSIZE; 2388 2389 return 0; 2390 } 2391 2392 static int put_nla_flavors(struct sk_buff *skb, struct seg6_local_lwt *slwt) 2393 { 2394 struct seg6_flavors_info *finfo = &slwt->flv_info; 2395 __u32 fops = finfo->flv_ops; 2396 struct nlattr *nest; 2397 int rc; 2398 2399 nest = nla_nest_start(skb, SEG6_LOCAL_FLAVORS); 2400 if (!nest) 2401 return -EMSGSIZE; 2402 2403 if (nla_put_u32(skb, SEG6_LOCAL_FLV_OPERATION, fops)) { 2404 rc = -EMSGSIZE; 2405 goto err; 2406 } 2407 2408 if (seg6_next_csid_enabled(fops)) { 2409 rc = seg6_fill_nla_next_csid_cfg(skb, finfo); 2410 if (rc < 0) 2411 goto err; 2412 } 2413 2414 return nla_nest_end(skb, nest); 2415 2416 err: 2417 nla_nest_cancel(skb, nest); 2418 return rc; 2419 } 2420 2421 static int seg6_cmp_nla_next_csid_cfg(struct seg6_flavors_info *finfo_a, 2422 struct seg6_flavors_info *finfo_b) 2423 { 2424 if (finfo_a->lcblock_bits != finfo_b->lcblock_bits) 2425 return 1; 2426 2427 if (finfo_a->lcnode_func_bits != finfo_b->lcnode_func_bits) 2428 return 1; 2429 2430 return 0; 2431 } 2432 2433 static int cmp_nla_flavors(struct seg6_local_lwt *a, struct seg6_local_lwt *b) 2434 { 2435 struct seg6_flavors_info *finfo_a = &a->flv_info; 2436 struct seg6_flavors_info *finfo_b = &b->flv_info; 2437 2438 if (finfo_a->flv_ops != finfo_b->flv_ops) 2439 return 1; 2440 2441 if (seg6_next_csid_enabled(finfo_a->flv_ops)) { 2442 if (seg6_cmp_nla_next_csid_cfg(finfo_a, finfo_b)) 2443 return 1; 2444 } 2445 2446 return 0; 2447 } 2448 2449 static int encap_size_flavors(struct seg6_local_lwt *slwt) 2450 { 2451 struct seg6_flavors_info *finfo = &slwt->flv_info; 2452 int nlsize; 2453 2454 nlsize = nla_total_size(0) + /* nest SEG6_LOCAL_FLAVORS */ 2455 nla_total_size(4); /* SEG6_LOCAL_FLV_OPERATION */ 2456 2457 if (seg6_next_csid_enabled(finfo->flv_ops)) 2458 nlsize += nla_total_size(1) + /* SEG6_LOCAL_FLV_LCBLOCK_BITS */ 2459 nla_total_size(1); /* SEG6_LOCAL_FLV_LCNODE_FN_BITS */ 2460 2461 return nlsize; 2462 } 2463 2464 struct seg6_action_param { 2465 int (*parse)(struct nlattr **attrs, struct seg6_local_lwt *slwt, 2466 struct netlink_ext_ack *extack); 2467 int (*put)(struct sk_buff *skb, struct seg6_local_lwt *slwt); 2468 int (*cmp)(struct seg6_local_lwt *a, struct seg6_local_lwt *b); 2469 2470 /* optional destroy() callback useful for releasing resources which 2471 * have been previously acquired in the corresponding parse() 2472 * function. 2473 */ 2474 void (*destroy)(struct seg6_local_lwt *slwt); 2475 }; 2476 2477 static struct seg6_action_param seg6_action_params[SEG6_LOCAL_MAX + 1] = { 2478 [SEG6_LOCAL_SRH] = { .parse = parse_nla_srh, 2479 .put = put_nla_srh, 2480 .cmp = cmp_nla_srh, 2481 .destroy = destroy_attr_srh }, 2482 2483 [SEG6_LOCAL_TABLE] = { .parse = parse_nla_table, 2484 .put = put_nla_table, 2485 .cmp = cmp_nla_table }, 2486 2487 [SEG6_LOCAL_NH4] = { .parse = parse_nla_nh4, 2488 .put = put_nla_nh4, 2489 .cmp = cmp_nla_nh4 }, 2490 2491 [SEG6_LOCAL_NH6] = { .parse = parse_nla_nh6, 2492 .put = put_nla_nh6, 2493 .cmp = cmp_nla_nh6 }, 2494 2495 [SEG6_LOCAL_IIF] = { .parse = parse_nla_iif, 2496 .put = put_nla_iif, 2497 .cmp = cmp_nla_iif }, 2498 2499 [SEG6_LOCAL_OIF] = { .parse = parse_nla_oif, 2500 .put = put_nla_oif, 2501 .cmp = cmp_nla_oif }, 2502 2503 [SEG6_LOCAL_BPF] = { .parse = parse_nla_bpf, 2504 .put = put_nla_bpf, 2505 .cmp = cmp_nla_bpf, 2506 .destroy = destroy_attr_bpf }, 2507 2508 [SEG6_LOCAL_VRFTABLE] = { .parse = parse_nla_vrftable, 2509 .put = put_nla_vrftable, 2510 .cmp = cmp_nla_vrftable }, 2511 2512 [SEG6_LOCAL_COUNTERS] = { .parse = parse_nla_counters, 2513 .put = put_nla_counters, 2514 .cmp = cmp_nla_counters, 2515 .destroy = destroy_attr_counters }, 2516 2517 [SEG6_LOCAL_FLAVORS] = { .parse = parse_nla_flavors, 2518 .put = put_nla_flavors, 2519 .cmp = cmp_nla_flavors }, 2520 }; 2521 2522 /* call the destroy() callback (if available) for each set attribute in 2523 * @parsed_attrs, starting from the first attribute up to the @max_parsed 2524 * (excluded) attribute. 2525 */ 2526 static void __destroy_attrs(unsigned long parsed_attrs, int max_parsed, 2527 struct seg6_local_lwt *slwt) 2528 { 2529 struct seg6_action_param *param; 2530 int i; 2531 2532 /* Every required seg6local attribute is identified by an ID which is 2533 * encoded as a flag (i.e: 1 << ID) in the 'attrs' bitmask; 2534 * 2535 * We scan the 'parsed_attrs' bitmask, starting from the first attribute 2536 * up to the @max_parsed (excluded) attribute. 2537 * For each set attribute, we retrieve the corresponding destroy() 2538 * callback. If the callback is not available, then we skip to the next 2539 * attribute; otherwise, we call the destroy() callback. 2540 */ 2541 for (i = SEG6_LOCAL_SRH; i < max_parsed; ++i) { 2542 if (!(parsed_attrs & SEG6_F_ATTR(i))) 2543 continue; 2544 2545 param = &seg6_action_params[i]; 2546 2547 if (param->destroy) 2548 param->destroy(slwt); 2549 } 2550 } 2551 2552 /* release all the resources that may have been acquired during parsing 2553 * operations. 2554 */ 2555 static void destroy_attrs(struct seg6_local_lwt *slwt) 2556 { 2557 unsigned long attrs = slwt->desc->attrs | slwt->parsed_optattrs; 2558 2559 __destroy_attrs(attrs, SEG6_LOCAL_MAX + 1, slwt); 2560 } 2561 2562 static int parse_nla_optional_attrs(struct nlattr **attrs, 2563 struct seg6_local_lwt *slwt, 2564 struct netlink_ext_ack *extack) 2565 { 2566 struct seg6_action_desc *desc = slwt->desc; 2567 unsigned long parsed_optattrs = 0; 2568 struct seg6_action_param *param; 2569 int err, i; 2570 2571 for (i = SEG6_LOCAL_SRH; i < SEG6_LOCAL_MAX + 1; ++i) { 2572 if (!(desc->optattrs & SEG6_F_ATTR(i)) || !attrs[i]) 2573 continue; 2574 2575 /* once here, the i-th attribute is provided by the 2576 * userspace AND it is identified optional as well. 2577 */ 2578 param = &seg6_action_params[i]; 2579 2580 err = param->parse(attrs, slwt, extack); 2581 if (err < 0) 2582 goto parse_optattrs_err; 2583 2584 /* current attribute has been correctly parsed */ 2585 parsed_optattrs |= SEG6_F_ATTR(i); 2586 } 2587 2588 /* store in the tunnel state all the optional attributed successfully 2589 * parsed. 2590 */ 2591 slwt->parsed_optattrs = parsed_optattrs; 2592 2593 return 0; 2594 2595 parse_optattrs_err: 2596 __destroy_attrs(parsed_optattrs, i, slwt); 2597 2598 return err; 2599 } 2600 2601 /* call the custom constructor of the behavior during its initialization phase 2602 * and after that all its attributes have been parsed successfully. 2603 */ 2604 static int 2605 seg6_local_lwtunnel_build_state(struct seg6_local_lwt *slwt, const void *cfg, 2606 struct netlink_ext_ack *extack) 2607 { 2608 struct seg6_action_desc *desc = slwt->desc; 2609 struct seg6_local_lwtunnel_ops *ops; 2610 2611 ops = &desc->slwt_ops; 2612 if (!ops->build_state) 2613 return 0; 2614 2615 return ops->build_state(slwt, cfg, extack); 2616 } 2617 2618 /* call the custom destructor of the behavior which is invoked before the 2619 * tunnel is going to be destroyed. 2620 */ 2621 static void seg6_local_lwtunnel_destroy_state(struct seg6_local_lwt *slwt) 2622 { 2623 struct seg6_action_desc *desc = slwt->desc; 2624 struct seg6_local_lwtunnel_ops *ops; 2625 2626 ops = &desc->slwt_ops; 2627 if (!ops->destroy_state) 2628 return; 2629 2630 ops->destroy_state(slwt); 2631 } 2632 2633 static int parse_nla_action(struct nlattr **attrs, struct seg6_local_lwt *slwt, 2634 struct netlink_ext_ack *extack) 2635 { 2636 struct seg6_action_param *param; 2637 struct seg6_action_desc *desc; 2638 unsigned long invalid_attrs; 2639 int i, err; 2640 2641 desc = __get_action_desc(slwt->action); 2642 if (!desc) 2643 return -EINVAL; 2644 2645 if (!desc->input) 2646 return -EOPNOTSUPP; 2647 2648 slwt->desc = desc; 2649 slwt->headroom += desc->static_headroom; 2650 2651 /* Forcing the desc->optattrs *set* and the desc->attrs *set* to be 2652 * disjoined, this allow us to release acquired resources by optional 2653 * attributes and by required attributes independently from each other 2654 * without any interference. 2655 * In other terms, we are sure that we do not release some the acquired 2656 * resources twice. 2657 * 2658 * Note that if an attribute is configured both as required and as 2659 * optional, it means that the user has messed something up in the 2660 * seg6_action_table. Therefore, this check is required for SRv6 2661 * behaviors to work properly. 2662 */ 2663 invalid_attrs = desc->attrs & desc->optattrs; 2664 if (invalid_attrs) { 2665 WARN_ONCE(1, 2666 "An attribute cannot be both required AND optional"); 2667 return -EINVAL; 2668 } 2669 2670 /* parse the required attributes */ 2671 for (i = SEG6_LOCAL_SRH; i < SEG6_LOCAL_MAX + 1; i++) { 2672 if (desc->attrs & SEG6_F_ATTR(i)) { 2673 if (!attrs[i]) 2674 return -EINVAL; 2675 2676 param = &seg6_action_params[i]; 2677 2678 err = param->parse(attrs, slwt, extack); 2679 if (err < 0) 2680 goto parse_attrs_err; 2681 } 2682 } 2683 2684 /* parse the optional attributes, if any */ 2685 err = parse_nla_optional_attrs(attrs, slwt, extack); 2686 if (err < 0) 2687 goto parse_attrs_err; 2688 2689 return 0; 2690 2691 parse_attrs_err: 2692 /* release any resource that may have been acquired during the i-1 2693 * parse() operations. 2694 */ 2695 __destroy_attrs(desc->attrs, i, slwt); 2696 2697 return err; 2698 } 2699 2700 static int seg6_local_build_state(struct net *net, struct nlattr *nla, 2701 unsigned int family, const void *cfg, 2702 struct lwtunnel_state **ts, 2703 struct netlink_ext_ack *extack) 2704 { 2705 struct nlattr *tb[SEG6_LOCAL_MAX + 1]; 2706 struct lwtunnel_state *newts; 2707 struct seg6_local_lwt *slwt; 2708 int err; 2709 2710 if (family != AF_INET6) 2711 return -EINVAL; 2712 2713 err = nla_parse_nested_deprecated(tb, SEG6_LOCAL_MAX, nla, 2714 seg6_local_policy, extack); 2715 2716 if (err < 0) 2717 return err; 2718 2719 if (!tb[SEG6_LOCAL_ACTION]) 2720 return -EINVAL; 2721 2722 newts = lwtunnel_state_alloc(sizeof(*slwt)); 2723 if (!newts) 2724 return -ENOMEM; 2725 2726 slwt = seg6_local_lwtunnel(newts); 2727 slwt->action = nla_get_u32(tb[SEG6_LOCAL_ACTION]); 2728 2729 err = parse_nla_action(tb, slwt, extack); 2730 if (err < 0) 2731 goto out_free; 2732 2733 err = seg6_local_lwtunnel_build_state(slwt, cfg, extack); 2734 if (err < 0) 2735 goto out_destroy_attrs; 2736 2737 newts->type = LWTUNNEL_ENCAP_SEG6_LOCAL; 2738 newts->flags = LWTUNNEL_STATE_INPUT_REDIRECT; 2739 newts->headroom = slwt->headroom; 2740 2741 *ts = newts; 2742 2743 return 0; 2744 2745 out_destroy_attrs: 2746 destroy_attrs(slwt); 2747 out_free: 2748 kfree(newts); 2749 return err; 2750 } 2751 2752 static void seg6_local_destroy_state(struct lwtunnel_state *lwt) 2753 { 2754 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt); 2755 2756 seg6_local_lwtunnel_destroy_state(slwt); 2757 2758 destroy_attrs(slwt); 2759 2760 return; 2761 } 2762 2763 static int seg6_local_fill_encap(struct sk_buff *skb, 2764 struct lwtunnel_state *lwt) 2765 { 2766 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt); 2767 struct seg6_action_param *param; 2768 unsigned long attrs; 2769 int i, err; 2770 2771 if (nla_put_u32(skb, SEG6_LOCAL_ACTION, slwt->action)) 2772 return -EMSGSIZE; 2773 2774 attrs = slwt->desc->attrs | slwt->parsed_optattrs; 2775 2776 for (i = SEG6_LOCAL_SRH; i < SEG6_LOCAL_MAX + 1; i++) { 2777 if (attrs & SEG6_F_ATTR(i)) { 2778 param = &seg6_action_params[i]; 2779 err = param->put(skb, slwt); 2780 if (err < 0) 2781 return err; 2782 } 2783 } 2784 2785 return 0; 2786 } 2787 2788 static int seg6_local_get_encap_size(struct lwtunnel_state *lwt) 2789 { 2790 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt); 2791 unsigned long attrs; 2792 int nlsize; 2793 2794 nlsize = nla_total_size(4); /* action */ 2795 2796 attrs = slwt->desc->attrs | slwt->parsed_optattrs; 2797 2798 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_SRH)) 2799 nlsize += nla_total_size((slwt->srh->hdrlen + 1) << 3); 2800 2801 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_TABLE)) 2802 nlsize += nla_total_size(4); 2803 2804 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_NH4)) 2805 nlsize += nla_total_size(4); 2806 2807 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_NH6)) 2808 nlsize += nla_total_size(16); 2809 2810 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_IIF)) 2811 nlsize += nla_total_size(4); 2812 2813 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_OIF)) 2814 nlsize += nla_total_size(4); 2815 2816 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_BPF)) 2817 nlsize += nla_total_size(sizeof(struct nlattr)) + 2818 nla_total_size(MAX_PROG_NAME) + 2819 nla_total_size(4); 2820 2821 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_VRFTABLE)) 2822 nlsize += nla_total_size(4); 2823 2824 if (attrs & SEG6_F_LOCAL_COUNTERS) 2825 nlsize += nla_total_size(0) + /* nest SEG6_LOCAL_COUNTERS */ 2826 /* SEG6_LOCAL_CNT_PACKETS */ 2827 nla_total_size_64bit(sizeof(__u64)) + 2828 /* SEG6_LOCAL_CNT_BYTES */ 2829 nla_total_size_64bit(sizeof(__u64)) + 2830 /* SEG6_LOCAL_CNT_ERRORS */ 2831 nla_total_size_64bit(sizeof(__u64)); 2832 2833 if (attrs & SEG6_F_ATTR(SEG6_LOCAL_FLAVORS)) 2834 nlsize += encap_size_flavors(slwt); 2835 2836 return nlsize; 2837 } 2838 2839 static int seg6_local_cmp_encap(struct lwtunnel_state *a, 2840 struct lwtunnel_state *b) 2841 { 2842 struct seg6_local_lwt *slwt_a, *slwt_b; 2843 struct seg6_action_param *param; 2844 unsigned long attrs_a, attrs_b; 2845 int i; 2846 2847 slwt_a = seg6_local_lwtunnel(a); 2848 slwt_b = seg6_local_lwtunnel(b); 2849 2850 if (slwt_a->action != slwt_b->action) 2851 return 1; 2852 2853 attrs_a = slwt_a->desc->attrs | slwt_a->parsed_optattrs; 2854 attrs_b = slwt_b->desc->attrs | slwt_b->parsed_optattrs; 2855 2856 if (attrs_a != attrs_b) 2857 return 1; 2858 2859 for (i = SEG6_LOCAL_SRH; i < SEG6_LOCAL_MAX + 1; i++) { 2860 if (attrs_a & SEG6_F_ATTR(i)) { 2861 param = &seg6_action_params[i]; 2862 if (param->cmp(slwt_a, slwt_b)) 2863 return 1; 2864 } 2865 } 2866 2867 return 0; 2868 } 2869 2870 static const struct lwtunnel_encap_ops seg6_local_ops = { 2871 .build_state = seg6_local_build_state, 2872 .destroy_state = seg6_local_destroy_state, 2873 .input = seg6_local_input, 2874 .fill_encap = seg6_local_fill_encap, 2875 .get_encap_size = seg6_local_get_encap_size, 2876 .cmp_encap = seg6_local_cmp_encap, 2877 .owner = THIS_MODULE, 2878 }; 2879 2880 int __init seg6_local_init(void) 2881 { 2882 /* If the max total number of defined attributes is reached, then your 2883 * kernel build stops here. 2884 * 2885 * This check is required to avoid arithmetic overflows when processing 2886 * behavior attributes and the maximum number of defined attributes 2887 * exceeds the allowed value. 2888 */ 2889 BUILD_BUG_ON(SEG6_LOCAL_MAX + 1 > BITS_PER_TYPE(unsigned long)); 2890 2891 /* Check whether the number of defined flavors exceeds the maximum 2892 * allowed value. 2893 */ 2894 BUILD_BUG_ON(SEG6_LOCAL_FLV_OP_MAX + 1 > BITS_PER_TYPE(__u32)); 2895 2896 /* If the default NEXT-C-SID Locator-Block/Node Function lengths (in 2897 * bits) have been changed with invalid values, kernel build stops 2898 * here. 2899 */ 2900 BUILD_BUG_ON(next_csid_chk_cntr_bits(SEG6_LOCAL_LCBLOCK_DBITS, 2901 SEG6_LOCAL_LCNODE_FN_DBITS)); 2902 BUILD_BUG_ON(next_csid_chk_lcblock_bits(SEG6_LOCAL_LCBLOCK_DBITS)); 2903 BUILD_BUG_ON(next_csid_chk_lcnode_fn_bits(SEG6_LOCAL_LCNODE_FN_DBITS)); 2904 2905 /* To be memory efficient, we use 'u8' to represent the different 2906 * actions related to RFC8986 flavors. If the kernel build stops here, 2907 * it means that it is not possible to correctly encode these actions 2908 * with the data type chosen for the action table. 2909 */ 2910 BUILD_BUG_ON(SEG6_LOCAL_FLV_ACT_MAX > (typeof(flv8986_act_tbl[0]))~0U); 2911 2912 return lwtunnel_encap_add_ops(&seg6_local_ops, 2913 LWTUNNEL_ENCAP_SEG6_LOCAL); 2914 } 2915 2916 void seg6_local_exit(void) 2917 { 2918 lwtunnel_encap_del_ops(&seg6_local_ops, LWTUNNEL_ENCAP_SEG6_LOCAL); 2919 } 2920