1 /* 2 * SR-IPv6 implementation 3 * 4 * Author: 5 * David Lebrun <david.lebrun@uclouvain.be> 6 * 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License 10 * as published by the Free Software Foundation; either version 11 * 2 of the License, or (at your option) any later version. 12 */ 13 14 #include <linux/types.h> 15 #include <linux/skbuff.h> 16 #include <linux/net.h> 17 #include <linux/module.h> 18 #include <net/ip.h> 19 #include <net/lwtunnel.h> 20 #include <net/netevent.h> 21 #include <net/netns/generic.h> 22 #include <net/ip6_fib.h> 23 #include <net/route.h> 24 #include <net/seg6.h> 25 #include <linux/seg6.h> 26 #include <linux/seg6_local.h> 27 #include <net/addrconf.h> 28 #include <net/ip6_route.h> 29 #include <net/dst_cache.h> 30 #ifdef CONFIG_IPV6_SEG6_HMAC 31 #include <net/seg6_hmac.h> 32 #endif 33 #include <linux/etherdevice.h> 34 35 struct seg6_local_lwt; 36 37 struct seg6_action_desc { 38 int action; 39 unsigned long attrs; 40 int (*input)(struct sk_buff *skb, struct seg6_local_lwt *slwt); 41 int static_headroom; 42 }; 43 44 struct seg6_local_lwt { 45 int action; 46 struct ipv6_sr_hdr *srh; 47 int table; 48 struct in_addr nh4; 49 struct in6_addr nh6; 50 int iif; 51 int oif; 52 53 int headroom; 54 struct seg6_action_desc *desc; 55 }; 56 57 static struct seg6_local_lwt *seg6_local_lwtunnel(struct lwtunnel_state *lwt) 58 { 59 return (struct seg6_local_lwt *)lwt->data; 60 } 61 62 static struct ipv6_sr_hdr *get_srh(struct sk_buff *skb) 63 { 64 struct ipv6_sr_hdr *srh; 65 int len, srhoff = 0; 66 67 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) 68 return NULL; 69 70 if (!pskb_may_pull(skb, srhoff + sizeof(*srh))) 71 return NULL; 72 73 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); 74 75 /* make sure it's a Segment Routing header (Routing Type 4) */ 76 if (srh->type != IPV6_SRCRT_TYPE_4) 77 return NULL; 78 79 len = (srh->hdrlen + 1) << 3; 80 81 if (!pskb_may_pull(skb, srhoff + len)) 82 return NULL; 83 84 if (!seg6_validate_srh(srh, len)) 85 return NULL; 86 87 return srh; 88 } 89 90 static struct ipv6_sr_hdr *get_and_validate_srh(struct sk_buff *skb) 91 { 92 struct ipv6_sr_hdr *srh; 93 94 srh = get_srh(skb); 95 if (!srh) 96 return NULL; 97 98 if (srh->segments_left == 0) 99 return NULL; 100 101 #ifdef CONFIG_IPV6_SEG6_HMAC 102 if (!seg6_hmac_validate_skb(skb)) 103 return NULL; 104 #endif 105 106 return srh; 107 } 108 109 static bool decap_and_validate(struct sk_buff *skb, int proto) 110 { 111 struct ipv6_sr_hdr *srh; 112 unsigned int off = 0; 113 114 srh = get_srh(skb); 115 if (srh && srh->segments_left > 0) 116 return false; 117 118 #ifdef CONFIG_IPV6_SEG6_HMAC 119 if (srh && !seg6_hmac_validate_skb(skb)) 120 return false; 121 #endif 122 123 if (ipv6_find_hdr(skb, &off, proto, NULL, NULL) < 0) 124 return false; 125 126 if (!pskb_pull(skb, off)) 127 return false; 128 129 skb_postpull_rcsum(skb, skb_network_header(skb), off); 130 131 skb_reset_network_header(skb); 132 skb_reset_transport_header(skb); 133 skb->encapsulation = 0; 134 135 return true; 136 } 137 138 static void advance_nextseg(struct ipv6_sr_hdr *srh, struct in6_addr *daddr) 139 { 140 struct in6_addr *addr; 141 142 srh->segments_left--; 143 addr = srh->segments + srh->segments_left; 144 *daddr = *addr; 145 } 146 147 static void lookup_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr, 148 u32 tbl_id) 149 { 150 struct net *net = dev_net(skb->dev); 151 struct ipv6hdr *hdr = ipv6_hdr(skb); 152 int flags = RT6_LOOKUP_F_HAS_SADDR; 153 struct dst_entry *dst = NULL; 154 struct rt6_info *rt; 155 struct flowi6 fl6; 156 157 fl6.flowi6_iif = skb->dev->ifindex; 158 fl6.daddr = nhaddr ? *nhaddr : hdr->daddr; 159 fl6.saddr = hdr->saddr; 160 fl6.flowlabel = ip6_flowinfo(hdr); 161 fl6.flowi6_mark = skb->mark; 162 fl6.flowi6_proto = hdr->nexthdr; 163 164 if (nhaddr) 165 fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH; 166 167 if (!tbl_id) { 168 dst = ip6_route_input_lookup(net, skb->dev, &fl6, flags); 169 } else { 170 struct fib6_table *table; 171 172 table = fib6_get_table(net, tbl_id); 173 if (!table) 174 goto out; 175 176 rt = ip6_pol_route(net, table, 0, &fl6, flags); 177 dst = &rt->dst; 178 } 179 180 if (dst && dst->dev->flags & IFF_LOOPBACK && !dst->error) { 181 dst_release(dst); 182 dst = NULL; 183 } 184 185 out: 186 if (!dst) { 187 rt = net->ipv6.ip6_blk_hole_entry; 188 dst = &rt->dst; 189 dst_hold(dst); 190 } 191 192 skb_dst_drop(skb); 193 skb_dst_set(skb, dst); 194 } 195 196 /* regular endpoint function */ 197 static int input_action_end(struct sk_buff *skb, struct seg6_local_lwt *slwt) 198 { 199 struct ipv6_sr_hdr *srh; 200 201 srh = get_and_validate_srh(skb); 202 if (!srh) 203 goto drop; 204 205 advance_nextseg(srh, &ipv6_hdr(skb)->daddr); 206 207 lookup_nexthop(skb, NULL, 0); 208 209 return dst_input(skb); 210 211 drop: 212 kfree_skb(skb); 213 return -EINVAL; 214 } 215 216 /* regular endpoint, and forward to specified nexthop */ 217 static int input_action_end_x(struct sk_buff *skb, struct seg6_local_lwt *slwt) 218 { 219 struct ipv6_sr_hdr *srh; 220 221 srh = get_and_validate_srh(skb); 222 if (!srh) 223 goto drop; 224 225 advance_nextseg(srh, &ipv6_hdr(skb)->daddr); 226 227 lookup_nexthop(skb, &slwt->nh6, 0); 228 229 return dst_input(skb); 230 231 drop: 232 kfree_skb(skb); 233 return -EINVAL; 234 } 235 236 static int input_action_end_t(struct sk_buff *skb, struct seg6_local_lwt *slwt) 237 { 238 struct ipv6_sr_hdr *srh; 239 240 srh = get_and_validate_srh(skb); 241 if (!srh) 242 goto drop; 243 244 advance_nextseg(srh, &ipv6_hdr(skb)->daddr); 245 246 lookup_nexthop(skb, NULL, slwt->table); 247 248 return dst_input(skb); 249 250 drop: 251 kfree_skb(skb); 252 return -EINVAL; 253 } 254 255 /* decapsulate and forward inner L2 frame on specified interface */ 256 static int input_action_end_dx2(struct sk_buff *skb, 257 struct seg6_local_lwt *slwt) 258 { 259 struct net *net = dev_net(skb->dev); 260 struct net_device *odev; 261 struct ethhdr *eth; 262 263 if (!decap_and_validate(skb, NEXTHDR_NONE)) 264 goto drop; 265 266 if (!pskb_may_pull(skb, ETH_HLEN)) 267 goto drop; 268 269 skb_reset_mac_header(skb); 270 eth = (struct ethhdr *)skb->data; 271 272 /* To determine the frame's protocol, we assume it is 802.3. This avoids 273 * a call to eth_type_trans(), which is not really relevant for our 274 * use case. 275 */ 276 if (!eth_proto_is_802_3(eth->h_proto)) 277 goto drop; 278 279 odev = dev_get_by_index_rcu(net, slwt->oif); 280 if (!odev) 281 goto drop; 282 283 /* As we accept Ethernet frames, make sure the egress device is of 284 * the correct type. 285 */ 286 if (odev->type != ARPHRD_ETHER) 287 goto drop; 288 289 if (!(odev->flags & IFF_UP) || !netif_carrier_ok(odev)) 290 goto drop; 291 292 skb_orphan(skb); 293 294 if (skb_warn_if_lro(skb)) 295 goto drop; 296 297 skb_forward_csum(skb); 298 299 if (skb->len - ETH_HLEN > odev->mtu) 300 goto drop; 301 302 skb->dev = odev; 303 skb->protocol = eth->h_proto; 304 305 return dev_queue_xmit(skb); 306 307 drop: 308 kfree_skb(skb); 309 return -EINVAL; 310 } 311 312 /* decapsulate and forward to specified nexthop */ 313 static int input_action_end_dx6(struct sk_buff *skb, 314 struct seg6_local_lwt *slwt) 315 { 316 struct in6_addr *nhaddr = NULL; 317 318 /* this function accepts IPv6 encapsulated packets, with either 319 * an SRH with SL=0, or no SRH. 320 */ 321 322 if (!decap_and_validate(skb, IPPROTO_IPV6)) 323 goto drop; 324 325 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) 326 goto drop; 327 328 /* The inner packet is not associated to any local interface, 329 * so we do not call netif_rx(). 330 * 331 * If slwt->nh6 is set to ::, then lookup the nexthop for the 332 * inner packet's DA. Otherwise, use the specified nexthop. 333 */ 334 335 if (!ipv6_addr_any(&slwt->nh6)) 336 nhaddr = &slwt->nh6; 337 338 lookup_nexthop(skb, nhaddr, 0); 339 340 return dst_input(skb); 341 drop: 342 kfree_skb(skb); 343 return -EINVAL; 344 } 345 346 static int input_action_end_dx4(struct sk_buff *skb, 347 struct seg6_local_lwt *slwt) 348 { 349 struct iphdr *iph; 350 __be32 nhaddr; 351 int err; 352 353 if (!decap_and_validate(skb, IPPROTO_IPIP)) 354 goto drop; 355 356 if (!pskb_may_pull(skb, sizeof(struct iphdr))) 357 goto drop; 358 359 skb->protocol = htons(ETH_P_IP); 360 361 iph = ip_hdr(skb); 362 363 nhaddr = slwt->nh4.s_addr ?: iph->daddr; 364 365 skb_dst_drop(skb); 366 367 err = ip_route_input(skb, nhaddr, iph->saddr, 0, skb->dev); 368 if (err) 369 goto drop; 370 371 return dst_input(skb); 372 373 drop: 374 kfree_skb(skb); 375 return -EINVAL; 376 } 377 378 static int input_action_end_dt6(struct sk_buff *skb, 379 struct seg6_local_lwt *slwt) 380 { 381 if (!decap_and_validate(skb, IPPROTO_IPV6)) 382 goto drop; 383 384 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) 385 goto drop; 386 387 lookup_nexthop(skb, NULL, slwt->table); 388 389 return dst_input(skb); 390 391 drop: 392 kfree_skb(skb); 393 return -EINVAL; 394 } 395 396 /* push an SRH on top of the current one */ 397 static int input_action_end_b6(struct sk_buff *skb, struct seg6_local_lwt *slwt) 398 { 399 struct ipv6_sr_hdr *srh; 400 int err = -EINVAL; 401 402 srh = get_and_validate_srh(skb); 403 if (!srh) 404 goto drop; 405 406 err = seg6_do_srh_inline(skb, slwt->srh); 407 if (err) 408 goto drop; 409 410 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); 411 skb_set_transport_header(skb, sizeof(struct ipv6hdr)); 412 413 lookup_nexthop(skb, NULL, 0); 414 415 return dst_input(skb); 416 417 drop: 418 kfree_skb(skb); 419 return err; 420 } 421 422 /* encapsulate within an outer IPv6 header and a specified SRH */ 423 static int input_action_end_b6_encap(struct sk_buff *skb, 424 struct seg6_local_lwt *slwt) 425 { 426 struct ipv6_sr_hdr *srh; 427 int err = -EINVAL; 428 429 srh = get_and_validate_srh(skb); 430 if (!srh) 431 goto drop; 432 433 advance_nextseg(srh, &ipv6_hdr(skb)->daddr); 434 435 skb_reset_inner_headers(skb); 436 skb->encapsulation = 1; 437 438 err = seg6_do_srh_encap(skb, slwt->srh, IPPROTO_IPV6); 439 if (err) 440 goto drop; 441 442 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); 443 skb_set_transport_header(skb, sizeof(struct ipv6hdr)); 444 445 lookup_nexthop(skb, NULL, 0); 446 447 return dst_input(skb); 448 449 drop: 450 kfree_skb(skb); 451 return err; 452 } 453 454 static struct seg6_action_desc seg6_action_table[] = { 455 { 456 .action = SEG6_LOCAL_ACTION_END, 457 .attrs = 0, 458 .input = input_action_end, 459 }, 460 { 461 .action = SEG6_LOCAL_ACTION_END_X, 462 .attrs = (1 << SEG6_LOCAL_NH6), 463 .input = input_action_end_x, 464 }, 465 { 466 .action = SEG6_LOCAL_ACTION_END_T, 467 .attrs = (1 << SEG6_LOCAL_TABLE), 468 .input = input_action_end_t, 469 }, 470 { 471 .action = SEG6_LOCAL_ACTION_END_DX2, 472 .attrs = (1 << SEG6_LOCAL_OIF), 473 .input = input_action_end_dx2, 474 }, 475 { 476 .action = SEG6_LOCAL_ACTION_END_DX6, 477 .attrs = (1 << SEG6_LOCAL_NH6), 478 .input = input_action_end_dx6, 479 }, 480 { 481 .action = SEG6_LOCAL_ACTION_END_DX4, 482 .attrs = (1 << SEG6_LOCAL_NH4), 483 .input = input_action_end_dx4, 484 }, 485 { 486 .action = SEG6_LOCAL_ACTION_END_DT6, 487 .attrs = (1 << SEG6_LOCAL_TABLE), 488 .input = input_action_end_dt6, 489 }, 490 { 491 .action = SEG6_LOCAL_ACTION_END_B6, 492 .attrs = (1 << SEG6_LOCAL_SRH), 493 .input = input_action_end_b6, 494 }, 495 { 496 .action = SEG6_LOCAL_ACTION_END_B6_ENCAP, 497 .attrs = (1 << SEG6_LOCAL_SRH), 498 .input = input_action_end_b6_encap, 499 .static_headroom = sizeof(struct ipv6hdr), 500 } 501 }; 502 503 static struct seg6_action_desc *__get_action_desc(int action) 504 { 505 struct seg6_action_desc *desc; 506 int i, count; 507 508 count = sizeof(seg6_action_table) / sizeof(struct seg6_action_desc); 509 for (i = 0; i < count; i++) { 510 desc = &seg6_action_table[i]; 511 if (desc->action == action) 512 return desc; 513 } 514 515 return NULL; 516 } 517 518 static int seg6_local_input(struct sk_buff *skb) 519 { 520 struct dst_entry *orig_dst = skb_dst(skb); 521 struct seg6_action_desc *desc; 522 struct seg6_local_lwt *slwt; 523 524 if (skb->protocol != htons(ETH_P_IPV6)) { 525 kfree_skb(skb); 526 return -EINVAL; 527 } 528 529 slwt = seg6_local_lwtunnel(orig_dst->lwtstate); 530 desc = slwt->desc; 531 532 return desc->input(skb, slwt); 533 } 534 535 static const struct nla_policy seg6_local_policy[SEG6_LOCAL_MAX + 1] = { 536 [SEG6_LOCAL_ACTION] = { .type = NLA_U32 }, 537 [SEG6_LOCAL_SRH] = { .type = NLA_BINARY }, 538 [SEG6_LOCAL_TABLE] = { .type = NLA_U32 }, 539 [SEG6_LOCAL_NH4] = { .type = NLA_BINARY, 540 .len = sizeof(struct in_addr) }, 541 [SEG6_LOCAL_NH6] = { .type = NLA_BINARY, 542 .len = sizeof(struct in6_addr) }, 543 [SEG6_LOCAL_IIF] = { .type = NLA_U32 }, 544 [SEG6_LOCAL_OIF] = { .type = NLA_U32 }, 545 }; 546 547 static int parse_nla_srh(struct nlattr **attrs, struct seg6_local_lwt *slwt) 548 { 549 struct ipv6_sr_hdr *srh; 550 int len; 551 552 srh = nla_data(attrs[SEG6_LOCAL_SRH]); 553 len = nla_len(attrs[SEG6_LOCAL_SRH]); 554 555 /* SRH must contain at least one segment */ 556 if (len < sizeof(*srh) + sizeof(struct in6_addr)) 557 return -EINVAL; 558 559 if (!seg6_validate_srh(srh, len)) 560 return -EINVAL; 561 562 slwt->srh = kmalloc(len, GFP_KERNEL); 563 if (!slwt->srh) 564 return -ENOMEM; 565 566 memcpy(slwt->srh, srh, len); 567 568 slwt->headroom += len; 569 570 return 0; 571 } 572 573 static int put_nla_srh(struct sk_buff *skb, struct seg6_local_lwt *slwt) 574 { 575 struct ipv6_sr_hdr *srh; 576 struct nlattr *nla; 577 int len; 578 579 srh = slwt->srh; 580 len = (srh->hdrlen + 1) << 3; 581 582 nla = nla_reserve(skb, SEG6_LOCAL_SRH, len); 583 if (!nla) 584 return -EMSGSIZE; 585 586 memcpy(nla_data(nla), srh, len); 587 588 return 0; 589 } 590 591 static int cmp_nla_srh(struct seg6_local_lwt *a, struct seg6_local_lwt *b) 592 { 593 int len = (a->srh->hdrlen + 1) << 3; 594 595 if (len != ((b->srh->hdrlen + 1) << 3)) 596 return 1; 597 598 return memcmp(a->srh, b->srh, len); 599 } 600 601 static int parse_nla_table(struct nlattr **attrs, struct seg6_local_lwt *slwt) 602 { 603 slwt->table = nla_get_u32(attrs[SEG6_LOCAL_TABLE]); 604 605 return 0; 606 } 607 608 static int put_nla_table(struct sk_buff *skb, struct seg6_local_lwt *slwt) 609 { 610 if (nla_put_u32(skb, SEG6_LOCAL_TABLE, slwt->table)) 611 return -EMSGSIZE; 612 613 return 0; 614 } 615 616 static int cmp_nla_table(struct seg6_local_lwt *a, struct seg6_local_lwt *b) 617 { 618 if (a->table != b->table) 619 return 1; 620 621 return 0; 622 } 623 624 static int parse_nla_nh4(struct nlattr **attrs, struct seg6_local_lwt *slwt) 625 { 626 memcpy(&slwt->nh4, nla_data(attrs[SEG6_LOCAL_NH4]), 627 sizeof(struct in_addr)); 628 629 return 0; 630 } 631 632 static int put_nla_nh4(struct sk_buff *skb, struct seg6_local_lwt *slwt) 633 { 634 struct nlattr *nla; 635 636 nla = nla_reserve(skb, SEG6_LOCAL_NH4, sizeof(struct in_addr)); 637 if (!nla) 638 return -EMSGSIZE; 639 640 memcpy(nla_data(nla), &slwt->nh4, sizeof(struct in_addr)); 641 642 return 0; 643 } 644 645 static int cmp_nla_nh4(struct seg6_local_lwt *a, struct seg6_local_lwt *b) 646 { 647 return memcmp(&a->nh4, &b->nh4, sizeof(struct in_addr)); 648 } 649 650 static int parse_nla_nh6(struct nlattr **attrs, struct seg6_local_lwt *slwt) 651 { 652 memcpy(&slwt->nh6, nla_data(attrs[SEG6_LOCAL_NH6]), 653 sizeof(struct in6_addr)); 654 655 return 0; 656 } 657 658 static int put_nla_nh6(struct sk_buff *skb, struct seg6_local_lwt *slwt) 659 { 660 struct nlattr *nla; 661 662 nla = nla_reserve(skb, SEG6_LOCAL_NH6, sizeof(struct in6_addr)); 663 if (!nla) 664 return -EMSGSIZE; 665 666 memcpy(nla_data(nla), &slwt->nh6, sizeof(struct in6_addr)); 667 668 return 0; 669 } 670 671 static int cmp_nla_nh6(struct seg6_local_lwt *a, struct seg6_local_lwt *b) 672 { 673 return memcmp(&a->nh6, &b->nh6, sizeof(struct in6_addr)); 674 } 675 676 static int parse_nla_iif(struct nlattr **attrs, struct seg6_local_lwt *slwt) 677 { 678 slwt->iif = nla_get_u32(attrs[SEG6_LOCAL_IIF]); 679 680 return 0; 681 } 682 683 static int put_nla_iif(struct sk_buff *skb, struct seg6_local_lwt *slwt) 684 { 685 if (nla_put_u32(skb, SEG6_LOCAL_IIF, slwt->iif)) 686 return -EMSGSIZE; 687 688 return 0; 689 } 690 691 static int cmp_nla_iif(struct seg6_local_lwt *a, struct seg6_local_lwt *b) 692 { 693 if (a->iif != b->iif) 694 return 1; 695 696 return 0; 697 } 698 699 static int parse_nla_oif(struct nlattr **attrs, struct seg6_local_lwt *slwt) 700 { 701 slwt->oif = nla_get_u32(attrs[SEG6_LOCAL_OIF]); 702 703 return 0; 704 } 705 706 static int put_nla_oif(struct sk_buff *skb, struct seg6_local_lwt *slwt) 707 { 708 if (nla_put_u32(skb, SEG6_LOCAL_OIF, slwt->oif)) 709 return -EMSGSIZE; 710 711 return 0; 712 } 713 714 static int cmp_nla_oif(struct seg6_local_lwt *a, struct seg6_local_lwt *b) 715 { 716 if (a->oif != b->oif) 717 return 1; 718 719 return 0; 720 } 721 722 struct seg6_action_param { 723 int (*parse)(struct nlattr **attrs, struct seg6_local_lwt *slwt); 724 int (*put)(struct sk_buff *skb, struct seg6_local_lwt *slwt); 725 int (*cmp)(struct seg6_local_lwt *a, struct seg6_local_lwt *b); 726 }; 727 728 static struct seg6_action_param seg6_action_params[SEG6_LOCAL_MAX + 1] = { 729 [SEG6_LOCAL_SRH] = { .parse = parse_nla_srh, 730 .put = put_nla_srh, 731 .cmp = cmp_nla_srh }, 732 733 [SEG6_LOCAL_TABLE] = { .parse = parse_nla_table, 734 .put = put_nla_table, 735 .cmp = cmp_nla_table }, 736 737 [SEG6_LOCAL_NH4] = { .parse = parse_nla_nh4, 738 .put = put_nla_nh4, 739 .cmp = cmp_nla_nh4 }, 740 741 [SEG6_LOCAL_NH6] = { .parse = parse_nla_nh6, 742 .put = put_nla_nh6, 743 .cmp = cmp_nla_nh6 }, 744 745 [SEG6_LOCAL_IIF] = { .parse = parse_nla_iif, 746 .put = put_nla_iif, 747 .cmp = cmp_nla_iif }, 748 749 [SEG6_LOCAL_OIF] = { .parse = parse_nla_oif, 750 .put = put_nla_oif, 751 .cmp = cmp_nla_oif }, 752 }; 753 754 static int parse_nla_action(struct nlattr **attrs, struct seg6_local_lwt *slwt) 755 { 756 struct seg6_action_param *param; 757 struct seg6_action_desc *desc; 758 int i, err; 759 760 desc = __get_action_desc(slwt->action); 761 if (!desc) 762 return -EINVAL; 763 764 if (!desc->input) 765 return -EOPNOTSUPP; 766 767 slwt->desc = desc; 768 slwt->headroom += desc->static_headroom; 769 770 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) { 771 if (desc->attrs & (1 << i)) { 772 if (!attrs[i]) 773 return -EINVAL; 774 775 param = &seg6_action_params[i]; 776 777 err = param->parse(attrs, slwt); 778 if (err < 0) 779 return err; 780 } 781 } 782 783 return 0; 784 } 785 786 static int seg6_local_build_state(struct nlattr *nla, unsigned int family, 787 const void *cfg, struct lwtunnel_state **ts, 788 struct netlink_ext_ack *extack) 789 { 790 struct nlattr *tb[SEG6_LOCAL_MAX + 1]; 791 struct lwtunnel_state *newts; 792 struct seg6_local_lwt *slwt; 793 int err; 794 795 if (family != AF_INET6) 796 return -EINVAL; 797 798 err = nla_parse_nested(tb, SEG6_LOCAL_MAX, nla, seg6_local_policy, 799 extack); 800 801 if (err < 0) 802 return err; 803 804 if (!tb[SEG6_LOCAL_ACTION]) 805 return -EINVAL; 806 807 newts = lwtunnel_state_alloc(sizeof(*slwt)); 808 if (!newts) 809 return -ENOMEM; 810 811 slwt = seg6_local_lwtunnel(newts); 812 slwt->action = nla_get_u32(tb[SEG6_LOCAL_ACTION]); 813 814 err = parse_nla_action(tb, slwt); 815 if (err < 0) 816 goto out_free; 817 818 newts->type = LWTUNNEL_ENCAP_SEG6_LOCAL; 819 newts->flags = LWTUNNEL_STATE_INPUT_REDIRECT; 820 newts->headroom = slwt->headroom; 821 822 *ts = newts; 823 824 return 0; 825 826 out_free: 827 kfree(slwt->srh); 828 kfree(newts); 829 return err; 830 } 831 832 static void seg6_local_destroy_state(struct lwtunnel_state *lwt) 833 { 834 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt); 835 836 kfree(slwt->srh); 837 } 838 839 static int seg6_local_fill_encap(struct sk_buff *skb, 840 struct lwtunnel_state *lwt) 841 { 842 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt); 843 struct seg6_action_param *param; 844 int i, err; 845 846 if (nla_put_u32(skb, SEG6_LOCAL_ACTION, slwt->action)) 847 return -EMSGSIZE; 848 849 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) { 850 if (slwt->desc->attrs & (1 << i)) { 851 param = &seg6_action_params[i]; 852 err = param->put(skb, slwt); 853 if (err < 0) 854 return err; 855 } 856 } 857 858 return 0; 859 } 860 861 static int seg6_local_get_encap_size(struct lwtunnel_state *lwt) 862 { 863 struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt); 864 unsigned long attrs; 865 int nlsize; 866 867 nlsize = nla_total_size(4); /* action */ 868 869 attrs = slwt->desc->attrs; 870 871 if (attrs & (1 << SEG6_LOCAL_SRH)) 872 nlsize += nla_total_size((slwt->srh->hdrlen + 1) << 3); 873 874 if (attrs & (1 << SEG6_LOCAL_TABLE)) 875 nlsize += nla_total_size(4); 876 877 if (attrs & (1 << SEG6_LOCAL_NH4)) 878 nlsize += nla_total_size(4); 879 880 if (attrs & (1 << SEG6_LOCAL_NH6)) 881 nlsize += nla_total_size(16); 882 883 if (attrs & (1 << SEG6_LOCAL_IIF)) 884 nlsize += nla_total_size(4); 885 886 if (attrs & (1 << SEG6_LOCAL_OIF)) 887 nlsize += nla_total_size(4); 888 889 return nlsize; 890 } 891 892 static int seg6_local_cmp_encap(struct lwtunnel_state *a, 893 struct lwtunnel_state *b) 894 { 895 struct seg6_local_lwt *slwt_a, *slwt_b; 896 struct seg6_action_param *param; 897 int i; 898 899 slwt_a = seg6_local_lwtunnel(a); 900 slwt_b = seg6_local_lwtunnel(b); 901 902 if (slwt_a->action != slwt_b->action) 903 return 1; 904 905 if (slwt_a->desc->attrs != slwt_b->desc->attrs) 906 return 1; 907 908 for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) { 909 if (slwt_a->desc->attrs & (1 << i)) { 910 param = &seg6_action_params[i]; 911 if (param->cmp(slwt_a, slwt_b)) 912 return 1; 913 } 914 } 915 916 return 0; 917 } 918 919 static const struct lwtunnel_encap_ops seg6_local_ops = { 920 .build_state = seg6_local_build_state, 921 .destroy_state = seg6_local_destroy_state, 922 .input = seg6_local_input, 923 .fill_encap = seg6_local_fill_encap, 924 .get_encap_size = seg6_local_get_encap_size, 925 .cmp_encap = seg6_local_cmp_encap, 926 .owner = THIS_MODULE, 927 }; 928 929 int __init seg6_local_init(void) 930 { 931 return lwtunnel_encap_add_ops(&seg6_local_ops, 932 LWTUNNEL_ENCAP_SEG6_LOCAL); 933 } 934 935 void seg6_local_exit(void) 936 { 937 lwtunnel_encap_del_ops(&seg6_local_ops, LWTUNNEL_ENCAP_SEG6_LOCAL); 938 } 939