1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Extension Header handling for IPv6 4 * Linux INET6 implementation 5 * 6 * Authors: 7 * Pedro Roque <roque@di.fc.ul.pt> 8 * Andi Kleen <ak@muc.de> 9 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> 10 */ 11 12 /* Changes: 13 * yoshfuji : ensure not to overrun while parsing 14 * tlv options. 15 * Mitsuru KANDA @USAGI and: Remove ipv6_parse_exthdrs(). 16 * YOSHIFUJI Hideaki @USAGI Register inbound extension header 17 * handlers as inet6_protocol{}. 18 */ 19 20 #include <linux/errno.h> 21 #include <linux/types.h> 22 #include <linux/socket.h> 23 #include <linux/sockios.h> 24 #include <linux/net.h> 25 #include <linux/netdevice.h> 26 #include <linux/in6.h> 27 #include <linux/icmpv6.h> 28 #include <linux/slab.h> 29 #include <linux/export.h> 30 31 #include <net/dst.h> 32 #include <net/sock.h> 33 #include <net/snmp.h> 34 35 #include <net/ipv6.h> 36 #include <net/protocol.h> 37 #include <net/transp_v6.h> 38 #include <net/rawv6.h> 39 #include <net/ndisc.h> 40 #include <net/ip6_route.h> 41 #include <net/addrconf.h> 42 #include <net/calipso.h> 43 #if IS_ENABLED(CONFIG_IPV6_MIP6) 44 #include <net/xfrm.h> 45 #endif 46 #include <linux/seg6.h> 47 #include <net/seg6.h> 48 #ifdef CONFIG_IPV6_SEG6_HMAC 49 #include <net/seg6_hmac.h> 50 #endif 51 #include <net/rpl.h> 52 #include <linux/ioam6.h> 53 #include <linux/ioam6_genl.h> 54 #include <net/ioam6.h> 55 #include <net/dst_metadata.h> 56 57 #include <linux/uaccess.h> 58 59 /********************* 60 Generic functions 61 *********************/ 62 63 /* An unknown option is detected, decide what to do */ 64 65 static bool ip6_tlvopt_unknown(struct sk_buff *skb, int optoff, 66 bool disallow_unknowns) 67 { 68 if (disallow_unknowns) { 69 /* If unknown TLVs are disallowed by configuration 70 * then always silently drop packet. Note this also 71 * means no ICMP parameter problem is sent which 72 * could be a good property to mitigate a reflection DOS 73 * attack. 74 */ 75 76 goto drop; 77 } 78 79 switch ((skb_network_header(skb)[optoff] & 0xC0) >> 6) { 80 case 0: /* ignore */ 81 return true; 82 83 case 1: /* drop packet */ 84 break; 85 86 case 3: /* Send ICMP if not a multicast address and drop packet */ 87 /* Actually, it is redundant check. icmp_send 88 will recheck in any case. 89 */ 90 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) 91 break; 92 fallthrough; 93 case 2: /* send ICMP PARM PROB regardless and drop packet */ 94 icmpv6_param_prob_reason(skb, ICMPV6_UNK_OPTION, optoff, 95 SKB_DROP_REASON_UNHANDLED_PROTO); 96 return false; 97 } 98 99 drop: 100 kfree_skb_reason(skb, SKB_DROP_REASON_UNHANDLED_PROTO); 101 return false; 102 } 103 104 static bool ipv6_hop_ra(struct sk_buff *skb, int optoff); 105 static bool ipv6_hop_ioam(struct sk_buff *skb, int optoff); 106 static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff); 107 static bool ipv6_hop_calipso(struct sk_buff *skb, int optoff); 108 #if IS_ENABLED(CONFIG_IPV6_MIP6) 109 static bool ipv6_dest_hao(struct sk_buff *skb, int optoff); 110 #endif 111 112 /* Parse tlv encoded option header (hop-by-hop or destination) */ 113 114 static bool ip6_parse_tlv(bool hopbyhop, 115 struct sk_buff *skb, 116 int max_count) 117 { 118 int len = (skb_transport_header(skb)[1] + 1) << 3; 119 const unsigned char *nh = skb_network_header(skb); 120 int off = skb_network_header_len(skb); 121 bool disallow_unknowns = false; 122 int tlv_count = 0; 123 int padlen = 0; 124 125 if (unlikely(max_count < 0)) { 126 disallow_unknowns = true; 127 max_count = -max_count; 128 } 129 130 off += 2; 131 len -= 2; 132 133 while (len > 0) { 134 int optlen, i; 135 136 if (nh[off] == IPV6_TLV_PAD1) { 137 padlen++; 138 if (padlen > 7) 139 goto bad; 140 off++; 141 len--; 142 continue; 143 } 144 if (len < 2) 145 goto bad; 146 optlen = nh[off + 1] + 2; 147 if (optlen > len) 148 goto bad; 149 150 if (nh[off] == IPV6_TLV_PADN) { 151 /* RFC 2460 states that the purpose of PadN is 152 * to align the containing header to multiples 153 * of 8. 7 is therefore the highest valid value. 154 * See also RFC 4942, Section 2.1.9.5. 155 */ 156 padlen += optlen; 157 if (padlen > 7) 158 goto bad; 159 /* RFC 4942 recommends receiving hosts to 160 * actively check PadN payload to contain 161 * only zeroes. 162 */ 163 for (i = 2; i < optlen; i++) { 164 if (nh[off + i] != 0) 165 goto bad; 166 } 167 } else { 168 tlv_count++; 169 if (tlv_count > max_count) 170 goto bad; 171 172 if (hopbyhop) { 173 switch (nh[off]) { 174 case IPV6_TLV_ROUTERALERT: 175 if (!ipv6_hop_ra(skb, off)) 176 return false; 177 break; 178 case IPV6_TLV_IOAM: 179 if (!ipv6_hop_ioam(skb, off)) 180 return false; 181 182 nh = skb_network_header(skb); 183 break; 184 case IPV6_TLV_JUMBO: 185 if (!ipv6_hop_jumbo(skb, off)) 186 return false; 187 188 nh = skb_network_header(skb); 189 break; 190 case IPV6_TLV_CALIPSO: 191 if (!ipv6_hop_calipso(skb, off)) 192 return false; 193 break; 194 default: 195 if (!ip6_tlvopt_unknown(skb, off, 196 disallow_unknowns)) 197 return false; 198 break; 199 } 200 } else { 201 switch (nh[off]) { 202 #if IS_ENABLED(CONFIG_IPV6_MIP6) 203 case IPV6_TLV_HAO: 204 if (!ipv6_dest_hao(skb, off)) 205 return false; 206 207 nh = skb_network_header(skb); 208 break; 209 #endif 210 default: 211 if (!ip6_tlvopt_unknown(skb, off, 212 disallow_unknowns)) 213 return false; 214 break; 215 } 216 } 217 padlen = 0; 218 } 219 off += optlen; 220 len -= optlen; 221 } 222 223 if (len == 0) 224 return true; 225 bad: 226 kfree_skb_reason(skb, SKB_DROP_REASON_IP_INHDR); 227 return false; 228 } 229 230 /***************************** 231 Destination options header. 232 *****************************/ 233 234 #if IS_ENABLED(CONFIG_IPV6_MIP6) 235 static bool ipv6_dest_hao(struct sk_buff *skb, int optoff) 236 { 237 struct ipv6_destopt_hao *hao; 238 struct inet6_skb_parm *opt = IP6CB(skb); 239 struct ipv6hdr *ipv6h = ipv6_hdr(skb); 240 SKB_DR(reason); 241 int ret; 242 243 if (opt->dsthao) { 244 net_dbg_ratelimited("hao duplicated\n"); 245 goto discard; 246 } 247 opt->dsthao = opt->dst1; 248 opt->dst1 = 0; 249 250 hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + optoff); 251 252 if (hao->length != 16) { 253 net_dbg_ratelimited("hao invalid option length = %d\n", 254 hao->length); 255 SKB_DR_SET(reason, IP_INHDR); 256 goto discard; 257 } 258 259 if (!(ipv6_addr_type(&hao->addr) & IPV6_ADDR_UNICAST)) { 260 net_dbg_ratelimited("hao is not an unicast addr: %pI6\n", 261 &hao->addr); 262 SKB_DR_SET(reason, INVALID_PROTO); 263 goto discard; 264 } 265 266 ret = xfrm6_input_addr(skb, (xfrm_address_t *)&ipv6h->daddr, 267 (xfrm_address_t *)&hao->addr, IPPROTO_DSTOPTS); 268 if (unlikely(ret < 0)) { 269 SKB_DR_SET(reason, XFRM_POLICY); 270 goto discard; 271 } 272 273 if (skb_cloned(skb)) { 274 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) 275 goto discard; 276 277 /* update all variable using below by copied skbuff */ 278 hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + 279 optoff); 280 ipv6h = ipv6_hdr(skb); 281 } 282 283 if (skb->ip_summed == CHECKSUM_COMPLETE) 284 skb->ip_summed = CHECKSUM_NONE; 285 286 swap(ipv6h->saddr, hao->addr); 287 288 if (skb->tstamp == 0) 289 __net_timestamp(skb); 290 291 return true; 292 293 discard: 294 kfree_skb_reason(skb, reason); 295 return false; 296 } 297 #endif 298 299 static int ipv6_destopt_rcv(struct sk_buff *skb) 300 { 301 struct inet6_dev *idev = __in6_dev_get(skb->dev); 302 struct inet6_skb_parm *opt = IP6CB(skb); 303 #if IS_ENABLED(CONFIG_IPV6_MIP6) 304 __u16 dstbuf; 305 #endif 306 struct dst_entry *dst = skb_dst(skb); 307 struct net *net = dev_net(skb->dev); 308 int extlen; 309 310 if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) || 311 !pskb_may_pull(skb, (skb_transport_offset(skb) + 312 ((skb_transport_header(skb)[1] + 1) << 3)))) { 313 __IP6_INC_STATS(dev_net(dst_dev(dst)), idev, 314 IPSTATS_MIB_INHDRERRORS); 315 fail_and_free: 316 kfree_skb(skb); 317 return -1; 318 } 319 320 extlen = (skb_transport_header(skb)[1] + 1) << 3; 321 if (extlen > READ_ONCE(net->ipv6.sysctl.max_dst_opts_len)) 322 goto fail_and_free; 323 324 opt->lastopt = opt->dst1 = skb_network_header_len(skb); 325 #if IS_ENABLED(CONFIG_IPV6_MIP6) 326 dstbuf = opt->dst1; 327 #endif 328 329 if (ip6_parse_tlv(false, skb, 330 READ_ONCE(net->ipv6.sysctl.max_dst_opts_cnt))) { 331 skb->transport_header += extlen; 332 opt = IP6CB(skb); 333 #if IS_ENABLED(CONFIG_IPV6_MIP6) 334 opt->nhoff = dstbuf; 335 #else 336 opt->nhoff = opt->dst1; 337 #endif 338 return 1; 339 } 340 341 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 342 return -1; 343 } 344 345 static void seg6_update_csum(struct sk_buff *skb) 346 { 347 struct ipv6_sr_hdr *hdr; 348 struct in6_addr *addr; 349 __be32 from, to; 350 351 /* srh is at transport offset and seg_left is already decremented 352 * but daddr is not yet updated with next segment 353 */ 354 355 hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb); 356 addr = hdr->segments + hdr->segments_left; 357 358 hdr->segments_left++; 359 from = *(__be32 *)hdr; 360 361 hdr->segments_left--; 362 to = *(__be32 *)hdr; 363 364 /* update skb csum with diff resulting from seg_left decrement */ 365 366 update_csum_diff4(skb, from, to); 367 368 /* compute csum diff between current and next segment and update */ 369 370 update_csum_diff16(skb, (__be32 *)(&ipv6_hdr(skb)->daddr), 371 (__be32 *)addr); 372 } 373 374 static int ipv6_srh_rcv(struct sk_buff *skb) 375 { 376 struct inet6_skb_parm *opt = IP6CB(skb); 377 struct net *net = dev_net(skb->dev); 378 struct ipv6_sr_hdr *hdr; 379 struct inet6_dev *idev; 380 struct in6_addr *addr; 381 int accept_seg6; 382 383 hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb); 384 385 idev = __in6_dev_get(skb->dev); 386 if (!idev) { 387 kfree_skb(skb); 388 return -1; 389 } 390 391 accept_seg6 = min(READ_ONCE(net->ipv6.devconf_all->seg6_enabled), 392 READ_ONCE(idev->cnf.seg6_enabled)); 393 394 if (!accept_seg6) { 395 kfree_skb(skb); 396 return -1; 397 } 398 399 #ifdef CONFIG_IPV6_SEG6_HMAC 400 if (!seg6_hmac_validate_skb(skb)) { 401 kfree_skb(skb); 402 return -1; 403 } 404 #endif 405 406 looped_back: 407 if (hdr->segments_left == 0) { 408 if (hdr->nexthdr == NEXTHDR_IPV6 || hdr->nexthdr == NEXTHDR_IPV4) { 409 int offset = (hdr->hdrlen + 1) << 3; 410 411 skb_postpull_rcsum(skb, skb_network_header(skb), 412 skb_network_header_len(skb)); 413 skb_pull(skb, offset); 414 skb_postpull_rcsum(skb, skb_transport_header(skb), 415 offset); 416 417 skb_reset_network_header(skb); 418 skb_reset_transport_header(skb); 419 skb->encapsulation = 0; 420 if (hdr->nexthdr == NEXTHDR_IPV4) 421 skb->protocol = htons(ETH_P_IP); 422 __skb_tunnel_rx(skb, skb->dev, net); 423 424 netif_rx(skb); 425 return -1; 426 } 427 428 opt->srcrt = skb_network_header_len(skb); 429 opt->lastopt = opt->srcrt; 430 skb->transport_header += (hdr->hdrlen + 1) << 3; 431 opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb); 432 433 return 1; 434 } 435 436 if (hdr->segments_left >= (hdr->hdrlen >> 1)) { 437 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 438 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, 439 ((&hdr->segments_left) - 440 skb_network_header(skb))); 441 return -1; 442 } 443 444 if (skb_cloned(skb)) { 445 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) { 446 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), 447 IPSTATS_MIB_OUTDISCARDS); 448 kfree_skb(skb); 449 return -1; 450 } 451 452 hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb); 453 } 454 455 hdr->segments_left--; 456 addr = hdr->segments + hdr->segments_left; 457 458 skb_push(skb, sizeof(struct ipv6hdr)); 459 460 if (skb->ip_summed == CHECKSUM_COMPLETE) 461 seg6_update_csum(skb); 462 463 ipv6_hdr(skb)->daddr = *addr; 464 465 ip6_route_input(skb); 466 467 if (skb_dst(skb)->error) { 468 dst_input(skb); 469 return -1; 470 } 471 472 if (skb_dst_dev(skb)->flags & IFF_LOOPBACK) { 473 if (ipv6_hdr(skb)->hop_limit <= 1) { 474 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 475 icmpv6_send(skb, ICMPV6_TIME_EXCEED, 476 ICMPV6_EXC_HOPLIMIT, 0); 477 kfree_skb(skb); 478 return -1; 479 } 480 ipv6_hdr(skb)->hop_limit--; 481 482 skb_pull(skb, sizeof(struct ipv6hdr)); 483 goto looped_back; 484 } 485 486 dst_input(skb); 487 488 return -1; 489 } 490 491 static int ipv6_rpl_srh_rcv(struct sk_buff *skb) 492 { 493 struct ipv6_rpl_sr_hdr *hdr, *ohdr, *chdr; 494 struct inet6_skb_parm *opt = IP6CB(skb); 495 struct net *net = dev_net(skb->dev); 496 struct inet6_dev *idev; 497 struct ipv6hdr *oldhdr; 498 unsigned int chdr_len; 499 unsigned char *buf; 500 int accept_rpl_seg; 501 int i, err; 502 u64 n = 0; 503 u32 r; 504 505 idev = __in6_dev_get(skb->dev); 506 507 accept_rpl_seg = min(READ_ONCE(net->ipv6.devconf_all->rpl_seg_enabled), 508 READ_ONCE(idev->cnf.rpl_seg_enabled)); 509 if (!accept_rpl_seg) { 510 kfree_skb(skb); 511 return -1; 512 } 513 514 looped_back: 515 hdr = (struct ipv6_rpl_sr_hdr *)skb_transport_header(skb); 516 517 if (hdr->segments_left == 0) { 518 if (hdr->nexthdr == NEXTHDR_IPV6) { 519 int offset = (hdr->hdrlen + 1) << 3; 520 521 skb_postpull_rcsum(skb, skb_network_header(skb), 522 skb_network_header_len(skb)); 523 skb_pull(skb, offset); 524 skb_postpull_rcsum(skb, skb_transport_header(skb), 525 offset); 526 527 skb_reset_network_header(skb); 528 skb_reset_transport_header(skb); 529 skb->encapsulation = 0; 530 531 __skb_tunnel_rx(skb, skb->dev, net); 532 533 netif_rx(skb); 534 return -1; 535 } 536 537 opt->srcrt = skb_network_header_len(skb); 538 opt->lastopt = opt->srcrt; 539 skb->transport_header += (hdr->hdrlen + 1) << 3; 540 opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb); 541 542 return 1; 543 } 544 545 n = (hdr->hdrlen << 3) - hdr->pad - (16 - hdr->cmpre); 546 r = do_div(n, (16 - hdr->cmpri)); 547 /* checks if calculation was without remainder and n fits into 548 * unsigned char which is segments_left field. Should not be 549 * higher than that. 550 */ 551 if (r || (n + 1) > 127) { 552 kfree_skb(skb); 553 return -1; 554 } 555 556 if (hdr->segments_left > n + 1) { 557 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 558 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, 559 ((&hdr->segments_left) - 560 skb_network_header(skb))); 561 return -1; 562 } 563 564 hdr->segments_left--; 565 i = n - hdr->segments_left; 566 567 buf = kcalloc(struct_size(hdr, segments.addr, n + 2), 2, GFP_ATOMIC); 568 if (unlikely(!buf)) { 569 kfree_skb(skb); 570 return -1; 571 } 572 573 ohdr = (struct ipv6_rpl_sr_hdr *)buf; 574 ipv6_rpl_srh_decompress(ohdr, hdr, &ipv6_hdr(skb)->daddr, n); 575 chdr = (struct ipv6_rpl_sr_hdr *)(buf + ((ohdr->hdrlen + 1) << 3)); 576 577 if (ipv6_addr_is_multicast(&ohdr->rpl_segaddr[i])) { 578 kfree_skb(skb); 579 kfree(buf); 580 return -1; 581 } 582 583 err = ipv6_chk_rpl_srh_loop(net, ohdr->rpl_segaddr, n + 1); 584 if (err) { 585 icmpv6_send(skb, ICMPV6_PARAMPROB, 0, 0); 586 kfree_skb(skb); 587 kfree(buf); 588 return -1; 589 } 590 591 swap(ipv6_hdr(skb)->daddr, ohdr->rpl_segaddr[i]); 592 593 ipv6_rpl_srh_compress(chdr, ohdr, &ipv6_hdr(skb)->daddr, n); 594 595 oldhdr = ipv6_hdr(skb); 596 597 skb_pull(skb, ((hdr->hdrlen + 1) << 3)); 598 skb_postpull_rcsum(skb, oldhdr, 599 sizeof(struct ipv6hdr) + ((hdr->hdrlen + 1) << 3)); 600 chdr_len = sizeof(struct ipv6hdr) + ((chdr->hdrlen + 1) << 3); 601 if (unlikely(!hdr->segments_left || 602 skb_headroom(skb) < chdr_len + skb->mac_len)) { 603 if (pskb_expand_head(skb, chdr_len + skb->mac_len, 0, 604 GFP_ATOMIC)) { 605 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_OUTDISCARDS); 606 kfree_skb(skb); 607 kfree(buf); 608 return -1; 609 } 610 611 oldhdr = ipv6_hdr(skb); 612 } 613 skb_push(skb, chdr_len); 614 skb_reset_network_header(skb); 615 skb_mac_header_rebuild(skb); 616 skb_set_transport_header(skb, sizeof(struct ipv6hdr)); 617 618 memmove(ipv6_hdr(skb), oldhdr, sizeof(struct ipv6hdr)); 619 memcpy(skb_transport_header(skb), chdr, (chdr->hdrlen + 1) << 3); 620 621 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); 622 skb_postpush_rcsum(skb, ipv6_hdr(skb), 623 sizeof(struct ipv6hdr) + ((chdr->hdrlen + 1) << 3)); 624 625 kfree(buf); 626 627 ip6_route_input(skb); 628 629 if (skb_dst(skb)->error) { 630 dst_input(skb); 631 return -1; 632 } 633 634 if (skb_dst_dev(skb)->flags & IFF_LOOPBACK) { 635 if (ipv6_hdr(skb)->hop_limit <= 1) { 636 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 637 icmpv6_send(skb, ICMPV6_TIME_EXCEED, 638 ICMPV6_EXC_HOPLIMIT, 0); 639 kfree_skb(skb); 640 return -1; 641 } 642 ipv6_hdr(skb)->hop_limit--; 643 644 skb_pull(skb, sizeof(struct ipv6hdr)); 645 goto looped_back; 646 } 647 648 dst_input(skb); 649 650 return -1; 651 } 652 653 /******************************** 654 Routing header. 655 ********************************/ 656 657 /* called with rcu_read_lock() */ 658 static int ipv6_rthdr_rcv(struct sk_buff *skb) 659 { 660 struct inet6_dev *idev = __in6_dev_get(skb->dev); 661 struct inet6_skb_parm *opt = IP6CB(skb); 662 struct in6_addr *addr = NULL; 663 int n, i; 664 struct ipv6_rt_hdr *hdr; 665 struct rt0_hdr *rthdr; 666 struct net *net = dev_net(skb->dev); 667 int accept_source_route; 668 669 accept_source_route = READ_ONCE(net->ipv6.devconf_all->accept_source_route); 670 671 if (idev) 672 accept_source_route = min(accept_source_route, 673 READ_ONCE(idev->cnf.accept_source_route)); 674 675 if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) || 676 !pskb_may_pull(skb, (skb_transport_offset(skb) + 677 ((skb_transport_header(skb)[1] + 1) << 3)))) { 678 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 679 kfree_skb(skb); 680 return -1; 681 } 682 683 hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb); 684 685 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) || 686 skb->pkt_type != PACKET_HOST) { 687 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS); 688 kfree_skb(skb); 689 return -1; 690 } 691 692 switch (hdr->type) { 693 case IPV6_SRCRT_TYPE_4: 694 /* segment routing */ 695 return ipv6_srh_rcv(skb); 696 case IPV6_SRCRT_TYPE_3: 697 /* rpl segment routing */ 698 return ipv6_rpl_srh_rcv(skb); 699 default: 700 break; 701 } 702 703 looped_back: 704 if (hdr->segments_left == 0) { 705 switch (hdr->type) { 706 #if IS_ENABLED(CONFIG_IPV6_MIP6) 707 case IPV6_SRCRT_TYPE_2: 708 /* Silently discard type 2 header unless it was 709 * processed by own 710 */ 711 if (!addr) { 712 __IP6_INC_STATS(net, idev, 713 IPSTATS_MIB_INADDRERRORS); 714 kfree_skb(skb); 715 return -1; 716 } 717 break; 718 #endif 719 default: 720 break; 721 } 722 723 opt->lastopt = opt->srcrt = skb_network_header_len(skb); 724 skb->transport_header += (hdr->hdrlen + 1) << 3; 725 opt->dst0 = opt->dst1; 726 opt->dst1 = 0; 727 opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb); 728 return 1; 729 } 730 731 switch (hdr->type) { 732 #if IS_ENABLED(CONFIG_IPV6_MIP6) 733 case IPV6_SRCRT_TYPE_2: 734 if (accept_source_route < 0) 735 goto unknown_rh; 736 /* Silently discard invalid RTH type 2 */ 737 if (hdr->hdrlen != 2 || hdr->segments_left != 1) { 738 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 739 kfree_skb(skb); 740 return -1; 741 } 742 break; 743 #endif 744 default: 745 goto unknown_rh; 746 } 747 748 /* 749 * This is the routing header forwarding algorithm from 750 * RFC 2460, page 16. 751 */ 752 753 n = hdr->hdrlen >> 1; 754 755 if (hdr->segments_left > n) { 756 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 757 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, 758 ((&hdr->segments_left) - 759 skb_network_header(skb))); 760 return -1; 761 } 762 763 /* We are about to mangle packet header. Be careful! 764 Do not damage packets queued somewhere. 765 */ 766 if (skb_cloned(skb)) { 767 /* the copy is a forwarded packet */ 768 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) { 769 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), 770 IPSTATS_MIB_OUTDISCARDS); 771 kfree_skb(skb); 772 return -1; 773 } 774 hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb); 775 } 776 777 if (skb->ip_summed == CHECKSUM_COMPLETE) 778 skb->ip_summed = CHECKSUM_NONE; 779 780 i = n - --hdr->segments_left; 781 782 rthdr = (struct rt0_hdr *) hdr; 783 addr = rthdr->addr; 784 addr += i - 1; 785 786 switch (hdr->type) { 787 #if IS_ENABLED(CONFIG_IPV6_MIP6) 788 case IPV6_SRCRT_TYPE_2: 789 if (xfrm6_input_addr(skb, (xfrm_address_t *)addr, 790 (xfrm_address_t *)&ipv6_hdr(skb)->saddr, 791 IPPROTO_ROUTING) < 0) { 792 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS); 793 kfree_skb(skb); 794 return -1; 795 } 796 if (!ipv6_chk_home_addr(skb_dst_dev_net(skb), addr)) { 797 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS); 798 kfree_skb(skb); 799 return -1; 800 } 801 break; 802 #endif 803 default: 804 break; 805 } 806 807 if (ipv6_addr_is_multicast(addr)) { 808 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS); 809 kfree_skb(skb); 810 return -1; 811 } 812 813 swap(*addr, ipv6_hdr(skb)->daddr); 814 815 ip6_route_input(skb); 816 if (skb_dst(skb)->error) { 817 skb_push(skb, -skb_network_offset(skb)); 818 dst_input(skb); 819 return -1; 820 } 821 822 if (skb_dst_dev(skb)->flags & IFF_LOOPBACK) { 823 if (ipv6_hdr(skb)->hop_limit <= 1) { 824 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 825 icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, 826 0); 827 kfree_skb(skb); 828 return -1; 829 } 830 ipv6_hdr(skb)->hop_limit--; 831 goto looped_back; 832 } 833 834 skb_push(skb, -skb_network_offset(skb)); 835 dst_input(skb); 836 return -1; 837 838 unknown_rh: 839 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 840 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, 841 (&hdr->type) - skb_network_header(skb)); 842 return -1; 843 } 844 845 static const struct inet6_protocol rthdr_protocol = { 846 .handler = ipv6_rthdr_rcv, 847 .flags = INET6_PROTO_NOPOLICY, 848 }; 849 850 static const struct inet6_protocol destopt_protocol = { 851 .handler = ipv6_destopt_rcv, 852 .flags = INET6_PROTO_NOPOLICY, 853 }; 854 855 static const struct inet6_protocol nodata_protocol = { 856 .handler = dst_discard, 857 .flags = INET6_PROTO_NOPOLICY, 858 }; 859 860 int __init ipv6_exthdrs_init(void) 861 { 862 int ret; 863 864 ret = inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING); 865 if (ret) 866 goto out; 867 868 ret = inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS); 869 if (ret) 870 goto out_rthdr; 871 872 ret = inet6_add_protocol(&nodata_protocol, IPPROTO_NONE); 873 if (ret) 874 goto out_destopt; 875 876 out: 877 return ret; 878 out_destopt: 879 inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS); 880 out_rthdr: 881 inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING); 882 goto out; 883 }; 884 885 void ipv6_exthdrs_exit(void) 886 { 887 inet6_del_protocol(&nodata_protocol, IPPROTO_NONE); 888 inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS); 889 inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING); 890 } 891 892 /********************************** 893 Hop-by-hop options. 894 **********************************/ 895 896 /* Router Alert as of RFC 2711 */ 897 898 static bool ipv6_hop_ra(struct sk_buff *skb, int optoff) 899 { 900 const unsigned char *nh = skb_network_header(skb); 901 902 if (nh[optoff + 1] == 2) { 903 IP6CB(skb)->flags |= IP6SKB_ROUTERALERT; 904 memcpy(&IP6CB(skb)->ra, nh + optoff + 2, sizeof(IP6CB(skb)->ra)); 905 return true; 906 } 907 net_dbg_ratelimited("ipv6_hop_ra: wrong RA length %d\n", 908 nh[optoff + 1]); 909 kfree_skb_reason(skb, SKB_DROP_REASON_IP_INHDR); 910 return false; 911 } 912 913 /* IOAM */ 914 915 static bool ipv6_hop_ioam(struct sk_buff *skb, int optoff) 916 { 917 enum skb_drop_reason drop_reason; 918 struct ioam6_trace_hdr *trace; 919 struct ioam6_namespace *ns; 920 struct inet6_dev *idev; 921 struct ioam6_hdr *hdr; 922 923 drop_reason = SKB_DROP_REASON_IP_INHDR; 924 925 /* Bad alignment (must be 4n-aligned) */ 926 if (optoff & 3) 927 goto drop; 928 929 /* Does the device still have IPv6 configuration? */ 930 idev = __in6_dev_get(skb->dev); 931 if (!idev) { 932 drop_reason = SKB_DROP_REASON_IPV6DISABLED; 933 goto drop; 934 } 935 936 /* Ignore if IOAM is not enabled on ingress */ 937 if (!READ_ONCE(idev->cnf.ioam6_enabled)) 938 goto ignore; 939 940 /* Truncated Option header */ 941 hdr = (struct ioam6_hdr *)(skb_network_header(skb) + optoff); 942 if (hdr->opt_len < 2) 943 goto drop; 944 945 switch (hdr->type) { 946 case IOAM6_TYPE_PREALLOC: 947 /* Truncated Pre-allocated Trace header */ 948 if (hdr->opt_len < 2 + sizeof(*trace)) 949 goto drop; 950 951 /* Malformed Pre-allocated Trace header */ 952 trace = (struct ioam6_trace_hdr *)((u8 *)hdr + sizeof(*hdr)); 953 if (hdr->opt_len < 2 + sizeof(*trace) + trace->remlen * 4) 954 goto drop; 955 956 /* Inconsistent Pre-allocated Trace header */ 957 if (trace->nodelen != 958 ioam6_trace_compute_nodelen(be32_to_cpu(trace->type_be32))) 959 goto drop; 960 961 /* Ignore if the IOAM namespace is unknown */ 962 ns = ioam6_namespace(dev_net(skb->dev), trace->namespace_id); 963 if (!ns) 964 goto ignore; 965 966 if (!skb_valid_dst(skb)) 967 ip6_route_input(skb); 968 969 /* About to mangle packet header */ 970 if (skb_ensure_writable(skb, optoff + 2 + hdr->opt_len)) 971 goto drop; 972 973 /* Trace and hdr pointers may have changed */ 974 hdr = (struct ioam6_hdr *)(skb_network_header(skb) + optoff); 975 trace = (struct ioam6_trace_hdr *)((u8 *)hdr + sizeof(*hdr)); 976 977 ioam6_fill_trace_data(skb, ns, trace, true); 978 979 ioam6_event(IOAM6_EVENT_TRACE, dev_net(skb->dev), 980 GFP_ATOMIC, (void *)trace, hdr->opt_len - 2); 981 break; 982 default: 983 break; 984 } 985 986 ignore: 987 return true; 988 989 drop: 990 kfree_skb_reason(skb, drop_reason); 991 return false; 992 } 993 994 /* Jumbo payload */ 995 996 static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff) 997 { 998 const unsigned char *nh = skb_network_header(skb); 999 SKB_DR(reason); 1000 u32 pkt_len; 1001 1002 if (nh[optoff + 1] != 4 || (optoff & 3) != 2) { 1003 net_dbg_ratelimited("ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n", 1004 nh[optoff+1]); 1005 SKB_DR_SET(reason, IP_INHDR); 1006 goto drop; 1007 } 1008 1009 pkt_len = ntohl(*(__be32 *)(nh + optoff + 2)); 1010 if (pkt_len <= IPV6_MAXPLEN) { 1011 icmpv6_param_prob_reason(skb, ICMPV6_HDR_FIELD, optoff + 2, 1012 SKB_DROP_REASON_IP_INHDR); 1013 return false; 1014 } 1015 if (ipv6_hdr(skb)->payload_len) { 1016 icmpv6_param_prob_reason(skb, ICMPV6_HDR_FIELD, optoff, 1017 SKB_DROP_REASON_IP_INHDR); 1018 return false; 1019 } 1020 1021 if (pkt_len > skb->len - sizeof(struct ipv6hdr)) { 1022 SKB_DR_SET(reason, PKT_TOO_SMALL); 1023 goto drop; 1024 } 1025 1026 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) 1027 goto drop; 1028 1029 IP6CB(skb)->flags |= IP6SKB_JUMBOGRAM; 1030 return true; 1031 1032 drop: 1033 kfree_skb_reason(skb, reason); 1034 return false; 1035 } 1036 1037 /* CALIPSO RFC 5570 */ 1038 1039 static bool ipv6_hop_calipso(struct sk_buff *skb, int optoff) 1040 { 1041 const unsigned char *nh = skb_network_header(skb); 1042 1043 if (nh[optoff + 1] < 8) 1044 goto drop; 1045 1046 if (nh[optoff + 6] * 4 + 8 > nh[optoff + 1]) 1047 goto drop; 1048 1049 if (!calipso_validate(skb, nh + optoff)) 1050 goto drop; 1051 1052 return true; 1053 1054 drop: 1055 kfree_skb_reason(skb, SKB_DROP_REASON_IP_INHDR); 1056 return false; 1057 } 1058 1059 int ipv6_parse_hopopts(struct sk_buff *skb) 1060 { 1061 struct inet6_skb_parm *opt = IP6CB(skb); 1062 struct net *net = dev_net(skb->dev); 1063 int extlen; 1064 1065 /* 1066 * skb_network_header(skb) is equal to skb->data, and 1067 * skb_network_header_len(skb) is always equal to 1068 * sizeof(struct ipv6hdr) by definition of 1069 * hop-by-hop options. 1070 */ 1071 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) || 1072 !pskb_may_pull(skb, (sizeof(struct ipv6hdr) + 1073 ((skb_transport_header(skb)[1] + 1) << 3)))) { 1074 fail_and_free: 1075 kfree_skb(skb); 1076 return -1; 1077 } 1078 1079 extlen = (skb_transport_header(skb)[1] + 1) << 3; 1080 if (extlen > READ_ONCE(net->ipv6.sysctl.max_hbh_opts_len)) 1081 goto fail_and_free; 1082 1083 opt->flags |= IP6SKB_HOPBYHOP; 1084 if (ip6_parse_tlv(true, skb, 1085 READ_ONCE(net->ipv6.sysctl.max_hbh_opts_cnt))) { 1086 skb->transport_header += extlen; 1087 opt = IP6CB(skb); 1088 opt->nhoff = sizeof(struct ipv6hdr); 1089 return 1; 1090 } 1091 return -1; 1092 } 1093 1094 /* 1095 * Creating outbound headers. 1096 * 1097 * "build" functions work when skb is filled from head to tail (datagram) 1098 * "push" functions work when headers are added from tail to head (tcp) 1099 * 1100 * In both cases we assume, that caller reserved enough room 1101 * for headers. 1102 */ 1103 1104 static u8 ipv6_push_rthdr0(struct sk_buff *skb, u8 proto, 1105 struct ipv6_rt_hdr *opt, 1106 struct in6_addr **addr_p, struct in6_addr *saddr) 1107 { 1108 struct rt0_hdr *phdr, *ihdr; 1109 int hops; 1110 1111 ihdr = (struct rt0_hdr *) opt; 1112 1113 phdr = skb_push(skb, (ihdr->rt_hdr.hdrlen + 1) << 3); 1114 memcpy(phdr, ihdr, sizeof(struct rt0_hdr)); 1115 1116 hops = ihdr->rt_hdr.hdrlen >> 1; 1117 1118 if (hops > 1) 1119 memcpy(phdr->addr, ihdr->addr + 1, 1120 (hops - 1) * sizeof(struct in6_addr)); 1121 1122 phdr->addr[hops - 1] = **addr_p; 1123 *addr_p = ihdr->addr; 1124 1125 phdr->rt_hdr.nexthdr = proto; 1126 return NEXTHDR_ROUTING; 1127 } 1128 1129 static u8 ipv6_push_rthdr4(struct sk_buff *skb, u8 proto, 1130 struct ipv6_rt_hdr *opt, 1131 struct in6_addr **addr_p, struct in6_addr *saddr) 1132 { 1133 struct ipv6_sr_hdr *sr_phdr, *sr_ihdr; 1134 int plen, hops; 1135 1136 sr_ihdr = (struct ipv6_sr_hdr *)opt; 1137 plen = (sr_ihdr->hdrlen + 1) << 3; 1138 1139 sr_phdr = skb_push(skb, plen); 1140 memcpy(sr_phdr, sr_ihdr, sizeof(struct ipv6_sr_hdr)); 1141 1142 hops = sr_ihdr->first_segment + 1; 1143 memcpy(sr_phdr->segments + 1, sr_ihdr->segments + 1, 1144 (hops - 1) * sizeof(struct in6_addr)); 1145 1146 sr_phdr->segments[0] = **addr_p; 1147 *addr_p = &sr_ihdr->segments[sr_ihdr->segments_left]; 1148 1149 if (sr_ihdr->hdrlen > hops * 2) { 1150 int tlvs_offset, tlvs_length; 1151 1152 tlvs_offset = (1 + hops * 2) << 3; 1153 tlvs_length = (sr_ihdr->hdrlen - hops * 2) << 3; 1154 memcpy((char *)sr_phdr + tlvs_offset, 1155 (char *)sr_ihdr + tlvs_offset, tlvs_length); 1156 } 1157 1158 #ifdef CONFIG_IPV6_SEG6_HMAC 1159 if (sr_has_hmac(sr_phdr)) { 1160 struct net *net = NULL; 1161 1162 if (skb->dev) 1163 net = dev_net(skb->dev); 1164 else if (skb->sk) 1165 net = sock_net(skb->sk); 1166 1167 WARN_ON(!net); 1168 1169 if (net) 1170 seg6_push_hmac(net, saddr, sr_phdr); 1171 } 1172 #endif 1173 1174 sr_phdr->nexthdr = proto; 1175 return NEXTHDR_ROUTING; 1176 } 1177 1178 static u8 ipv6_push_rthdr(struct sk_buff *skb, u8 proto, 1179 struct ipv6_rt_hdr *opt, 1180 struct in6_addr **addr_p, struct in6_addr *saddr) 1181 { 1182 switch (opt->type) { 1183 case IPV6_SRCRT_TYPE_0: 1184 case IPV6_SRCRT_STRICT: 1185 case IPV6_SRCRT_TYPE_2: 1186 proto = ipv6_push_rthdr0(skb, proto, opt, addr_p, saddr); 1187 break; 1188 case IPV6_SRCRT_TYPE_4: 1189 proto = ipv6_push_rthdr4(skb, proto, opt, addr_p, saddr); 1190 break; 1191 default: 1192 break; 1193 } 1194 return proto; 1195 } 1196 1197 static u8 ipv6_push_exthdr(struct sk_buff *skb, u8 proto, u8 type, struct ipv6_opt_hdr *opt) 1198 { 1199 struct ipv6_opt_hdr *h = skb_push(skb, ipv6_optlen(opt)); 1200 1201 memcpy(h, opt, ipv6_optlen(opt)); 1202 h->nexthdr = proto; 1203 return type; 1204 } 1205 1206 u8 ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, 1207 u8 proto, 1208 struct in6_addr **daddr, struct in6_addr *saddr) 1209 { 1210 if (opt->srcrt) { 1211 proto = ipv6_push_rthdr(skb, proto, opt->srcrt, daddr, saddr); 1212 /* 1213 * IPV6_RTHDRDSTOPTS is ignored 1214 * unless IPV6_RTHDR is set (RFC3542). 1215 */ 1216 if (opt->dst0opt) 1217 proto = ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst0opt); 1218 } 1219 if (opt->hopopt) 1220 proto = ipv6_push_exthdr(skb, proto, NEXTHDR_HOP, opt->hopopt); 1221 return proto; 1222 } 1223 1224 u8 ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, u8 proto) 1225 { 1226 if (opt->dst1opt) 1227 proto = ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst1opt); 1228 return proto; 1229 } 1230 EXPORT_SYMBOL(ipv6_push_frag_opts); 1231 1232 struct ipv6_txoptions * 1233 ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt) 1234 { 1235 struct ipv6_txoptions *opt2; 1236 1237 opt2 = sock_kmemdup(sk, opt, opt->tot_len, GFP_ATOMIC); 1238 if (opt2) { 1239 long dif = (char *)opt2 - (char *)opt; 1240 if (opt2->hopopt) 1241 *((char **)&opt2->hopopt) += dif; 1242 if (opt2->dst0opt) 1243 *((char **)&opt2->dst0opt) += dif; 1244 if (opt2->dst1opt) 1245 *((char **)&opt2->dst1opt) += dif; 1246 if (opt2->srcrt) 1247 *((char **)&opt2->srcrt) += dif; 1248 refcount_set(&opt2->refcnt, 1); 1249 } 1250 return opt2; 1251 } 1252 EXPORT_SYMBOL_GPL(ipv6_dup_options); 1253 1254 static void ipv6_renew_option(int renewtype, 1255 struct ipv6_opt_hdr **dest, 1256 struct ipv6_opt_hdr *old, 1257 struct ipv6_opt_hdr *new, 1258 int newtype, char **p) 1259 { 1260 struct ipv6_opt_hdr *src; 1261 1262 src = (renewtype == newtype ? new : old); 1263 if (!src) 1264 return; 1265 1266 memcpy(*p, src, ipv6_optlen(src)); 1267 *dest = (struct ipv6_opt_hdr *)*p; 1268 *p += CMSG_ALIGN(ipv6_optlen(*dest)); 1269 } 1270 1271 /** 1272 * ipv6_renew_options - replace a specific ext hdr with a new one. 1273 * 1274 * @sk: sock from which to allocate memory 1275 * @opt: original options 1276 * @newtype: option type to replace in @opt 1277 * @newopt: new option of type @newtype to replace (user-mem) 1278 * 1279 * Returns a new set of options which is a copy of @opt with the 1280 * option type @newtype replaced with @newopt. 1281 * 1282 * @opt may be NULL, in which case a new set of options is returned 1283 * containing just @newopt. 1284 * 1285 * @newopt may be NULL, in which case the specified option type is 1286 * not copied into the new set of options. 1287 * 1288 * The new set of options is allocated from the socket option memory 1289 * buffer of @sk. 1290 */ 1291 struct ipv6_txoptions * 1292 ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt, 1293 int newtype, struct ipv6_opt_hdr *newopt) 1294 { 1295 int tot_len = 0; 1296 char *p; 1297 struct ipv6_txoptions *opt2; 1298 1299 if (opt) { 1300 if (newtype != IPV6_HOPOPTS && opt->hopopt) 1301 tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt)); 1302 if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt) 1303 tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt)); 1304 if (newtype != IPV6_RTHDR && opt->srcrt) 1305 tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt)); 1306 if (newtype != IPV6_DSTOPTS && opt->dst1opt) 1307 tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt)); 1308 } 1309 1310 if (newopt) 1311 tot_len += CMSG_ALIGN(ipv6_optlen(newopt)); 1312 1313 if (!tot_len) 1314 return NULL; 1315 1316 tot_len += sizeof(*opt2); 1317 opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC); 1318 if (!opt2) 1319 return ERR_PTR(-ENOBUFS); 1320 1321 memset(opt2, 0, tot_len); 1322 refcount_set(&opt2->refcnt, 1); 1323 opt2->tot_len = tot_len; 1324 p = (char *)(opt2 + 1); 1325 1326 ipv6_renew_option(IPV6_HOPOPTS, &opt2->hopopt, 1327 (opt ? opt->hopopt : NULL), 1328 newopt, newtype, &p); 1329 ipv6_renew_option(IPV6_RTHDRDSTOPTS, &opt2->dst0opt, 1330 (opt ? opt->dst0opt : NULL), 1331 newopt, newtype, &p); 1332 ipv6_renew_option(IPV6_RTHDR, 1333 (struct ipv6_opt_hdr **)&opt2->srcrt, 1334 (opt ? (struct ipv6_opt_hdr *)opt->srcrt : NULL), 1335 newopt, newtype, &p); 1336 ipv6_renew_option(IPV6_DSTOPTS, &opt2->dst1opt, 1337 (opt ? opt->dst1opt : NULL), 1338 newopt, newtype, &p); 1339 1340 opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) + 1341 (opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) + 1342 (opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0); 1343 opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0); 1344 1345 return opt2; 1346 } 1347 1348 struct ipv6_txoptions *__ipv6_fixup_options(struct ipv6_txoptions *opt_space, 1349 struct ipv6_txoptions *opt) 1350 { 1351 /* 1352 * ignore the dest before srcrt unless srcrt is being included. 1353 * --yoshfuji 1354 */ 1355 if (opt->dst0opt && !opt->srcrt) { 1356 if (opt_space != opt) { 1357 memcpy(opt_space, opt, sizeof(*opt_space)); 1358 opt = opt_space; 1359 } 1360 opt->opt_nflen -= ipv6_optlen(opt->dst0opt); 1361 opt->dst0opt = NULL; 1362 } 1363 1364 return opt; 1365 } 1366 EXPORT_SYMBOL_GPL(__ipv6_fixup_options); 1367 1368 /** 1369 * __fl6_update_dst - update flowi destination address with info given 1370 * by srcrt option, if any. 1371 * 1372 * @fl6: flowi6 for which daddr is to be updated 1373 * @opt: struct ipv6_txoptions in which to look for srcrt opt 1374 * @orig: copy of original daddr address if modified 1375 * 1376 * Return: NULL if no srcrt or invalid srcrt type, otherwise returns orig 1377 * and initial value of fl6->daddr set in orig 1378 */ 1379 struct in6_addr *__fl6_update_dst(struct flowi6 *fl6, 1380 const struct ipv6_txoptions *opt, 1381 struct in6_addr *orig) 1382 { 1383 if (!opt->srcrt) 1384 return NULL; 1385 1386 *orig = fl6->daddr; 1387 1388 switch (opt->srcrt->type) { 1389 case IPV6_SRCRT_TYPE_0: 1390 case IPV6_SRCRT_STRICT: 1391 case IPV6_SRCRT_TYPE_2: 1392 fl6->daddr = *((struct rt0_hdr *)opt->srcrt)->addr; 1393 break; 1394 case IPV6_SRCRT_TYPE_4: 1395 { 1396 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)opt->srcrt; 1397 1398 fl6->daddr = srh->segments[srh->segments_left]; 1399 break; 1400 } 1401 default: 1402 return NULL; 1403 } 1404 1405 return orig; 1406 } 1407 EXPORT_SYMBOL_GPL(__fl6_update_dst); 1408