Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Extension Header handling for IPv6
0004  *  Linux INET6 implementation
0005  *
0006  *  Authors:
0007  *  Pedro Roque     <roque@di.fc.ul.pt>
0008  *  Andi Kleen      <ak@muc.de>
0009  *  Alexey Kuznetsov    <kuznet@ms2.inr.ac.ru>
0010  */
0011 
0012 /* Changes:
0013  *  yoshfuji        : ensure not to overrun while parsing
0014  *                tlv options.
0015  *  Mitsuru KANDA @USAGI and: Remove ipv6_parse_exthdrs().
0016  *  YOSHIFUJI Hideaki @USAGI  Register inbound extension header
0017  *                handlers as inet6_protocol{}.
0018  */
0019 
0020 #include <linux/errno.h>
0021 #include <linux/types.h>
0022 #include <linux/socket.h>
0023 #include <linux/sockios.h>
0024 #include <linux/net.h>
0025 #include <linux/netdevice.h>
0026 #include <linux/in6.h>
0027 #include <linux/icmpv6.h>
0028 #include <linux/slab.h>
0029 #include <linux/export.h>
0030 
0031 #include <net/dst.h>
0032 #include <net/sock.h>
0033 #include <net/snmp.h>
0034 
0035 #include <net/ipv6.h>
0036 #include <net/protocol.h>
0037 #include <net/transp_v6.h>
0038 #include <net/rawv6.h>
0039 #include <net/ndisc.h>
0040 #include <net/ip6_route.h>
0041 #include <net/addrconf.h>
0042 #include <net/calipso.h>
0043 #if IS_ENABLED(CONFIG_IPV6_MIP6)
0044 #include <net/xfrm.h>
0045 #endif
0046 #include <linux/seg6.h>
0047 #include <net/seg6.h>
0048 #ifdef CONFIG_IPV6_SEG6_HMAC
0049 #include <net/seg6_hmac.h>
0050 #endif
0051 #include <net/rpl.h>
0052 #include <linux/ioam6.h>
0053 #include <net/ioam6.h>
0054 #include <net/dst_metadata.h>
0055 
0056 #include <linux/uaccess.h>
0057 
0058 /*********************
0059   Generic functions
0060  *********************/
0061 
0062 /* An unknown option is detected, decide what to do */
0063 
0064 static bool ip6_tlvopt_unknown(struct sk_buff *skb, int optoff,
0065                    bool disallow_unknowns)
0066 {
0067     if (disallow_unknowns) {
0068         /* If unknown TLVs are disallowed by configuration
0069          * then always silently drop packet. Note this also
0070          * means no ICMP parameter problem is sent which
0071          * could be a good property to mitigate a reflection DOS
0072          * attack.
0073          */
0074 
0075         goto drop;
0076     }
0077 
0078     switch ((skb_network_header(skb)[optoff] & 0xC0) >> 6) {
0079     case 0: /* ignore */
0080         return true;
0081 
0082     case 1: /* drop packet */
0083         break;
0084 
0085     case 3: /* Send ICMP if not a multicast address and drop packet */
0086         /* Actually, it is redundant check. icmp_send
0087            will recheck in any case.
0088          */
0089         if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr))
0090             break;
0091         fallthrough;
0092     case 2: /* send ICMP PARM PROB regardless and drop packet */
0093         icmpv6_param_prob_reason(skb, ICMPV6_UNK_OPTION, optoff,
0094                      SKB_DROP_REASON_UNHANDLED_PROTO);
0095         return false;
0096     }
0097 
0098 drop:
0099     kfree_skb_reason(skb, SKB_DROP_REASON_UNHANDLED_PROTO);
0100     return false;
0101 }
0102 
0103 static bool ipv6_hop_ra(struct sk_buff *skb, int optoff);
0104 static bool ipv6_hop_ioam(struct sk_buff *skb, int optoff);
0105 static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff);
0106 static bool ipv6_hop_calipso(struct sk_buff *skb, int optoff);
0107 #if IS_ENABLED(CONFIG_IPV6_MIP6)
0108 static bool ipv6_dest_hao(struct sk_buff *skb, int optoff);
0109 #endif
0110 
0111 /* Parse tlv encoded option header (hop-by-hop or destination) */
0112 
0113 static bool ip6_parse_tlv(bool hopbyhop,
0114               struct sk_buff *skb,
0115               int max_count)
0116 {
0117     int len = (skb_transport_header(skb)[1] + 1) << 3;
0118     const unsigned char *nh = skb_network_header(skb);
0119     int off = skb_network_header_len(skb);
0120     bool disallow_unknowns = false;
0121     int tlv_count = 0;
0122     int padlen = 0;
0123 
0124     if (unlikely(max_count < 0)) {
0125         disallow_unknowns = true;
0126         max_count = -max_count;
0127     }
0128 
0129     if (skb_transport_offset(skb) + len > skb_headlen(skb))
0130         goto bad;
0131 
0132     off += 2;
0133     len -= 2;
0134 
0135     while (len > 0) {
0136         int optlen, i;
0137 
0138         if (nh[off] == IPV6_TLV_PAD1) {
0139             padlen++;
0140             if (padlen > 7)
0141                 goto bad;
0142             off++;
0143             len--;
0144             continue;
0145         }
0146         if (len < 2)
0147             goto bad;
0148         optlen = nh[off + 1] + 2;
0149         if (optlen > len)
0150             goto bad;
0151 
0152         if (nh[off] == IPV6_TLV_PADN) {
0153             /* RFC 2460 states that the purpose of PadN is
0154              * to align the containing header to multiples
0155              * of 8. 7 is therefore the highest valid value.
0156              * See also RFC 4942, Section 2.1.9.5.
0157              */
0158             padlen += optlen;
0159             if (padlen > 7)
0160                 goto bad;
0161             /* RFC 4942 recommends receiving hosts to
0162              * actively check PadN payload to contain
0163              * only zeroes.
0164              */
0165             for (i = 2; i < optlen; i++) {
0166                 if (nh[off + i] != 0)
0167                     goto bad;
0168             }
0169         } else {
0170             tlv_count++;
0171             if (tlv_count > max_count)
0172                 goto bad;
0173 
0174             if (hopbyhop) {
0175                 switch (nh[off]) {
0176                 case IPV6_TLV_ROUTERALERT:
0177                     if (!ipv6_hop_ra(skb, off))
0178                         return false;
0179                     break;
0180                 case IPV6_TLV_IOAM:
0181                     if (!ipv6_hop_ioam(skb, off))
0182                         return false;
0183                     break;
0184                 case IPV6_TLV_JUMBO:
0185                     if (!ipv6_hop_jumbo(skb, off))
0186                         return false;
0187                     break;
0188                 case IPV6_TLV_CALIPSO:
0189                     if (!ipv6_hop_calipso(skb, off))
0190                         return false;
0191                     break;
0192                 default:
0193                     if (!ip6_tlvopt_unknown(skb, off,
0194                                 disallow_unknowns))
0195                         return false;
0196                     break;
0197                 }
0198             } else {
0199                 switch (nh[off]) {
0200 #if IS_ENABLED(CONFIG_IPV6_MIP6)
0201                 case IPV6_TLV_HAO:
0202                     if (!ipv6_dest_hao(skb, off))
0203                         return false;
0204                     break;
0205 #endif
0206                 default:
0207                     if (!ip6_tlvopt_unknown(skb, off,
0208                                 disallow_unknowns))
0209                         return false;
0210                     break;
0211                 }
0212             }
0213             padlen = 0;
0214         }
0215         off += optlen;
0216         len -= optlen;
0217     }
0218 
0219     if (len == 0)
0220         return true;
0221 bad:
0222     kfree_skb_reason(skb, SKB_DROP_REASON_IP_INHDR);
0223     return false;
0224 }
0225 
0226 /*****************************
0227   Destination options header.
0228  *****************************/
0229 
0230 #if IS_ENABLED(CONFIG_IPV6_MIP6)
0231 static bool ipv6_dest_hao(struct sk_buff *skb, int optoff)
0232 {
0233     struct ipv6_destopt_hao *hao;
0234     struct inet6_skb_parm *opt = IP6CB(skb);
0235     struct ipv6hdr *ipv6h = ipv6_hdr(skb);
0236     SKB_DR(reason);
0237     int ret;
0238 
0239     if (opt->dsthao) {
0240         net_dbg_ratelimited("hao duplicated\n");
0241         goto discard;
0242     }
0243     opt->dsthao = opt->dst1;
0244     opt->dst1 = 0;
0245 
0246     hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + optoff);
0247 
0248     if (hao->length != 16) {
0249         net_dbg_ratelimited("hao invalid option length = %d\n",
0250                     hao->length);
0251         SKB_DR_SET(reason, IP_INHDR);
0252         goto discard;
0253     }
0254 
0255     if (!(ipv6_addr_type(&hao->addr) & IPV6_ADDR_UNICAST)) {
0256         net_dbg_ratelimited("hao is not an unicast addr: %pI6\n",
0257                     &hao->addr);
0258         SKB_DR_SET(reason, INVALID_PROTO);
0259         goto discard;
0260     }
0261 
0262     ret = xfrm6_input_addr(skb, (xfrm_address_t *)&ipv6h->daddr,
0263                    (xfrm_address_t *)&hao->addr, IPPROTO_DSTOPTS);
0264     if (unlikely(ret < 0)) {
0265         SKB_DR_SET(reason, XFRM_POLICY);
0266         goto discard;
0267     }
0268 
0269     if (skb_cloned(skb)) {
0270         if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
0271             goto discard;
0272 
0273         /* update all variable using below by copied skbuff */
0274         hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) +
0275                           optoff);
0276         ipv6h = ipv6_hdr(skb);
0277     }
0278 
0279     if (skb->ip_summed == CHECKSUM_COMPLETE)
0280         skb->ip_summed = CHECKSUM_NONE;
0281 
0282     swap(ipv6h->saddr, hao->addr);
0283 
0284     if (skb->tstamp == 0)
0285         __net_timestamp(skb);
0286 
0287     return true;
0288 
0289  discard:
0290     kfree_skb_reason(skb, reason);
0291     return false;
0292 }
0293 #endif
0294 
0295 static int ipv6_destopt_rcv(struct sk_buff *skb)
0296 {
0297     struct inet6_dev *idev = __in6_dev_get(skb->dev);
0298     struct inet6_skb_parm *opt = IP6CB(skb);
0299 #if IS_ENABLED(CONFIG_IPV6_MIP6)
0300     __u16 dstbuf;
0301 #endif
0302     struct dst_entry *dst = skb_dst(skb);
0303     struct net *net = dev_net(skb->dev);
0304     int extlen;
0305 
0306     if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
0307         !pskb_may_pull(skb, (skb_transport_offset(skb) +
0308                  ((skb_transport_header(skb)[1] + 1) << 3)))) {
0309         __IP6_INC_STATS(dev_net(dst->dev), idev,
0310                 IPSTATS_MIB_INHDRERRORS);
0311 fail_and_free:
0312         kfree_skb(skb);
0313         return -1;
0314     }
0315 
0316     extlen = (skb_transport_header(skb)[1] + 1) << 3;
0317     if (extlen > net->ipv6.sysctl.max_dst_opts_len)
0318         goto fail_and_free;
0319 
0320     opt->lastopt = opt->dst1 = skb_network_header_len(skb);
0321 #if IS_ENABLED(CONFIG_IPV6_MIP6)
0322     dstbuf = opt->dst1;
0323 #endif
0324 
0325     if (ip6_parse_tlv(false, skb, net->ipv6.sysctl.max_dst_opts_cnt)) {
0326         skb->transport_header += extlen;
0327         opt = IP6CB(skb);
0328 #if IS_ENABLED(CONFIG_IPV6_MIP6)
0329         opt->nhoff = dstbuf;
0330 #else
0331         opt->nhoff = opt->dst1;
0332 #endif
0333         return 1;
0334     }
0335 
0336     __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
0337     return -1;
0338 }
0339 
0340 static void seg6_update_csum(struct sk_buff *skb)
0341 {
0342     struct ipv6_sr_hdr *hdr;
0343     struct in6_addr *addr;
0344     __be32 from, to;
0345 
0346     /* srh is at transport offset and seg_left is already decremented
0347      * but daddr is not yet updated with next segment
0348      */
0349 
0350     hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb);
0351     addr = hdr->segments + hdr->segments_left;
0352 
0353     hdr->segments_left++;
0354     from = *(__be32 *)hdr;
0355 
0356     hdr->segments_left--;
0357     to = *(__be32 *)hdr;
0358 
0359     /* update skb csum with diff resulting from seg_left decrement */
0360 
0361     update_csum_diff4(skb, from, to);
0362 
0363     /* compute csum diff between current and next segment and update */
0364 
0365     update_csum_diff16(skb, (__be32 *)(&ipv6_hdr(skb)->daddr),
0366                (__be32 *)addr);
0367 }
0368 
0369 static int ipv6_srh_rcv(struct sk_buff *skb)
0370 {
0371     struct inet6_skb_parm *opt = IP6CB(skb);
0372     struct net *net = dev_net(skb->dev);
0373     struct ipv6_sr_hdr *hdr;
0374     struct inet6_dev *idev;
0375     struct in6_addr *addr;
0376     int accept_seg6;
0377 
0378     hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb);
0379 
0380     idev = __in6_dev_get(skb->dev);
0381 
0382     accept_seg6 = net->ipv6.devconf_all->seg6_enabled;
0383     if (accept_seg6 > idev->cnf.seg6_enabled)
0384         accept_seg6 = idev->cnf.seg6_enabled;
0385 
0386     if (!accept_seg6) {
0387         kfree_skb(skb);
0388         return -1;
0389     }
0390 
0391 #ifdef CONFIG_IPV6_SEG6_HMAC
0392     if (!seg6_hmac_validate_skb(skb)) {
0393         kfree_skb(skb);
0394         return -1;
0395     }
0396 #endif
0397 
0398 looped_back:
0399     if (hdr->segments_left == 0) {
0400         if (hdr->nexthdr == NEXTHDR_IPV6 || hdr->nexthdr == NEXTHDR_IPV4) {
0401             int offset = (hdr->hdrlen + 1) << 3;
0402 
0403             skb_postpull_rcsum(skb, skb_network_header(skb),
0404                        skb_network_header_len(skb));
0405 
0406             if (!pskb_pull(skb, offset)) {
0407                 kfree_skb(skb);
0408                 return -1;
0409             }
0410             skb_postpull_rcsum(skb, skb_transport_header(skb),
0411                        offset);
0412 
0413             skb_reset_network_header(skb);
0414             skb_reset_transport_header(skb);
0415             skb->encapsulation = 0;
0416             if (hdr->nexthdr == NEXTHDR_IPV4)
0417                 skb->protocol = htons(ETH_P_IP);
0418             __skb_tunnel_rx(skb, skb->dev, net);
0419 
0420             netif_rx(skb);
0421             return -1;
0422         }
0423 
0424         opt->srcrt = skb_network_header_len(skb);
0425         opt->lastopt = opt->srcrt;
0426         skb->transport_header += (hdr->hdrlen + 1) << 3;
0427         opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
0428 
0429         return 1;
0430     }
0431 
0432     if (hdr->segments_left >= (hdr->hdrlen >> 1)) {
0433         __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
0434         icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
0435                   ((&hdr->segments_left) -
0436                    skb_network_header(skb)));
0437         return -1;
0438     }
0439 
0440     if (skb_cloned(skb)) {
0441         if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
0442             __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
0443                     IPSTATS_MIB_OUTDISCARDS);
0444             kfree_skb(skb);
0445             return -1;
0446         }
0447     }
0448 
0449     hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb);
0450 
0451     hdr->segments_left--;
0452     addr = hdr->segments + hdr->segments_left;
0453 
0454     skb_push(skb, sizeof(struct ipv6hdr));
0455 
0456     if (skb->ip_summed == CHECKSUM_COMPLETE)
0457         seg6_update_csum(skb);
0458 
0459     ipv6_hdr(skb)->daddr = *addr;
0460 
0461     skb_dst_drop(skb);
0462 
0463     ip6_route_input(skb);
0464 
0465     if (skb_dst(skb)->error) {
0466         dst_input(skb);
0467         return -1;
0468     }
0469 
0470     if (skb_dst(skb)->dev->flags & IFF_LOOPBACK) {
0471         if (ipv6_hdr(skb)->hop_limit <= 1) {
0472             __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
0473             icmpv6_send(skb, ICMPV6_TIME_EXCEED,
0474                     ICMPV6_EXC_HOPLIMIT, 0);
0475             kfree_skb(skb);
0476             return -1;
0477         }
0478         ipv6_hdr(skb)->hop_limit--;
0479 
0480         skb_pull(skb, sizeof(struct ipv6hdr));
0481         goto looped_back;
0482     }
0483 
0484     dst_input(skb);
0485 
0486     return -1;
0487 }
0488 
0489 static int ipv6_rpl_srh_rcv(struct sk_buff *skb)
0490 {
0491     struct ipv6_rpl_sr_hdr *hdr, *ohdr, *chdr;
0492     struct inet6_skb_parm *opt = IP6CB(skb);
0493     struct net *net = dev_net(skb->dev);
0494     struct inet6_dev *idev;
0495     struct ipv6hdr *oldhdr;
0496     unsigned char *buf;
0497     int accept_rpl_seg;
0498     int i, err;
0499     u64 n = 0;
0500     u32 r;
0501 
0502     idev = __in6_dev_get(skb->dev);
0503 
0504     accept_rpl_seg = net->ipv6.devconf_all->rpl_seg_enabled;
0505     if (accept_rpl_seg > idev->cnf.rpl_seg_enabled)
0506         accept_rpl_seg = idev->cnf.rpl_seg_enabled;
0507 
0508     if (!accept_rpl_seg) {
0509         kfree_skb(skb);
0510         return -1;
0511     }
0512 
0513 looped_back:
0514     hdr = (struct ipv6_rpl_sr_hdr *)skb_transport_header(skb);
0515 
0516     if (hdr->segments_left == 0) {
0517         if (hdr->nexthdr == NEXTHDR_IPV6) {
0518             int offset = (hdr->hdrlen + 1) << 3;
0519 
0520             skb_postpull_rcsum(skb, skb_network_header(skb),
0521                        skb_network_header_len(skb));
0522 
0523             if (!pskb_pull(skb, offset)) {
0524                 kfree_skb(skb);
0525                 return -1;
0526             }
0527             skb_postpull_rcsum(skb, skb_transport_header(skb),
0528                        offset);
0529 
0530             skb_reset_network_header(skb);
0531             skb_reset_transport_header(skb);
0532             skb->encapsulation = 0;
0533 
0534             __skb_tunnel_rx(skb, skb->dev, net);
0535 
0536             netif_rx(skb);
0537             return -1;
0538         }
0539 
0540         opt->srcrt = skb_network_header_len(skb);
0541         opt->lastopt = opt->srcrt;
0542         skb->transport_header += (hdr->hdrlen + 1) << 3;
0543         opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
0544 
0545         return 1;
0546     }
0547 
0548     if (!pskb_may_pull(skb, sizeof(*hdr))) {
0549         kfree_skb(skb);
0550         return -1;
0551     }
0552 
0553     n = (hdr->hdrlen << 3) - hdr->pad - (16 - hdr->cmpre);
0554     r = do_div(n, (16 - hdr->cmpri));
0555     /* checks if calculation was without remainder and n fits into
0556      * unsigned char which is segments_left field. Should not be
0557      * higher than that.
0558      */
0559     if (r || (n + 1) > 255) {
0560         kfree_skb(skb);
0561         return -1;
0562     }
0563 
0564     if (hdr->segments_left > n + 1) {
0565         __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
0566         icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
0567                   ((&hdr->segments_left) -
0568                    skb_network_header(skb)));
0569         return -1;
0570     }
0571 
0572     if (skb_cloned(skb)) {
0573         if (pskb_expand_head(skb, IPV6_RPL_SRH_WORST_SWAP_SIZE, 0,
0574                      GFP_ATOMIC)) {
0575             __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
0576                     IPSTATS_MIB_OUTDISCARDS);
0577             kfree_skb(skb);
0578             return -1;
0579         }
0580     } else {
0581         err = skb_cow_head(skb, IPV6_RPL_SRH_WORST_SWAP_SIZE);
0582         if (unlikely(err)) {
0583             kfree_skb(skb);
0584             return -1;
0585         }
0586     }
0587 
0588     hdr = (struct ipv6_rpl_sr_hdr *)skb_transport_header(skb);
0589 
0590     if (!pskb_may_pull(skb, ipv6_rpl_srh_size(n, hdr->cmpri,
0591                           hdr->cmpre))) {
0592         kfree_skb(skb);
0593         return -1;
0594     }
0595 
0596     hdr->segments_left--;
0597     i = n - hdr->segments_left;
0598 
0599     buf = kcalloc(struct_size(hdr, segments.addr, n + 2), 2, GFP_ATOMIC);
0600     if (unlikely(!buf)) {
0601         kfree_skb(skb);
0602         return -1;
0603     }
0604 
0605     ohdr = (struct ipv6_rpl_sr_hdr *)buf;
0606     ipv6_rpl_srh_decompress(ohdr, hdr, &ipv6_hdr(skb)->daddr, n);
0607     chdr = (struct ipv6_rpl_sr_hdr *)(buf + ((ohdr->hdrlen + 1) << 3));
0608 
0609     if ((ipv6_addr_type(&ipv6_hdr(skb)->daddr) & IPV6_ADDR_MULTICAST) ||
0610         (ipv6_addr_type(&ohdr->rpl_segaddr[i]) & IPV6_ADDR_MULTICAST)) {
0611         kfree_skb(skb);
0612         kfree(buf);
0613         return -1;
0614     }
0615 
0616     err = ipv6_chk_rpl_srh_loop(net, ohdr->rpl_segaddr, n + 1);
0617     if (err) {
0618         icmpv6_send(skb, ICMPV6_PARAMPROB, 0, 0);
0619         kfree_skb(skb);
0620         kfree(buf);
0621         return -1;
0622     }
0623 
0624     swap(ipv6_hdr(skb)->daddr, ohdr->rpl_segaddr[i]);
0625 
0626     ipv6_rpl_srh_compress(chdr, ohdr, &ipv6_hdr(skb)->daddr, n);
0627 
0628     oldhdr = ipv6_hdr(skb);
0629 
0630     skb_pull(skb, ((hdr->hdrlen + 1) << 3));
0631     skb_postpull_rcsum(skb, oldhdr,
0632                sizeof(struct ipv6hdr) + ((hdr->hdrlen + 1) << 3));
0633     skb_push(skb, ((chdr->hdrlen + 1) << 3) + sizeof(struct ipv6hdr));
0634     skb_reset_network_header(skb);
0635     skb_mac_header_rebuild(skb);
0636     skb_set_transport_header(skb, sizeof(struct ipv6hdr));
0637 
0638     memmove(ipv6_hdr(skb), oldhdr, sizeof(struct ipv6hdr));
0639     memcpy(skb_transport_header(skb), chdr, (chdr->hdrlen + 1) << 3);
0640 
0641     ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
0642     skb_postpush_rcsum(skb, ipv6_hdr(skb),
0643                sizeof(struct ipv6hdr) + ((chdr->hdrlen + 1) << 3));
0644 
0645     kfree(buf);
0646 
0647     skb_dst_drop(skb);
0648 
0649     ip6_route_input(skb);
0650 
0651     if (skb_dst(skb)->error) {
0652         dst_input(skb);
0653         return -1;
0654     }
0655 
0656     if (skb_dst(skb)->dev->flags & IFF_LOOPBACK) {
0657         if (ipv6_hdr(skb)->hop_limit <= 1) {
0658             __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
0659             icmpv6_send(skb, ICMPV6_TIME_EXCEED,
0660                     ICMPV6_EXC_HOPLIMIT, 0);
0661             kfree_skb(skb);
0662             return -1;
0663         }
0664         ipv6_hdr(skb)->hop_limit--;
0665 
0666         skb_pull(skb, sizeof(struct ipv6hdr));
0667         goto looped_back;
0668     }
0669 
0670     dst_input(skb);
0671 
0672     return -1;
0673 }
0674 
0675 /********************************
0676   Routing header.
0677  ********************************/
0678 
0679 /* called with rcu_read_lock() */
0680 static int ipv6_rthdr_rcv(struct sk_buff *skb)
0681 {
0682     struct inet6_dev *idev = __in6_dev_get(skb->dev);
0683     struct inet6_skb_parm *opt = IP6CB(skb);
0684     struct in6_addr *addr = NULL;
0685     struct in6_addr daddr;
0686     int n, i;
0687     struct ipv6_rt_hdr *hdr;
0688     struct rt0_hdr *rthdr;
0689     struct net *net = dev_net(skb->dev);
0690     int accept_source_route = net->ipv6.devconf_all->accept_source_route;
0691 
0692     if (idev && accept_source_route > idev->cnf.accept_source_route)
0693         accept_source_route = idev->cnf.accept_source_route;
0694 
0695     if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
0696         !pskb_may_pull(skb, (skb_transport_offset(skb) +
0697                  ((skb_transport_header(skb)[1] + 1) << 3)))) {
0698         __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
0699         kfree_skb(skb);
0700         return -1;
0701     }
0702 
0703     hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
0704 
0705     if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ||
0706         skb->pkt_type != PACKET_HOST) {
0707         __IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
0708         kfree_skb(skb);
0709         return -1;
0710     }
0711 
0712     switch (hdr->type) {
0713     case IPV6_SRCRT_TYPE_4:
0714         /* segment routing */
0715         return ipv6_srh_rcv(skb);
0716     case IPV6_SRCRT_TYPE_3:
0717         /* rpl segment routing */
0718         return ipv6_rpl_srh_rcv(skb);
0719     default:
0720         break;
0721     }
0722 
0723 looped_back:
0724     if (hdr->segments_left == 0) {
0725         switch (hdr->type) {
0726 #if IS_ENABLED(CONFIG_IPV6_MIP6)
0727         case IPV6_SRCRT_TYPE_2:
0728             /* Silently discard type 2 header unless it was
0729              * processed by own
0730              */
0731             if (!addr) {
0732                 __IP6_INC_STATS(net, idev,
0733                         IPSTATS_MIB_INADDRERRORS);
0734                 kfree_skb(skb);
0735                 return -1;
0736             }
0737             break;
0738 #endif
0739         default:
0740             break;
0741         }
0742 
0743         opt->lastopt = opt->srcrt = skb_network_header_len(skb);
0744         skb->transport_header += (hdr->hdrlen + 1) << 3;
0745         opt->dst0 = opt->dst1;
0746         opt->dst1 = 0;
0747         opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
0748         return 1;
0749     }
0750 
0751     switch (hdr->type) {
0752 #if IS_ENABLED(CONFIG_IPV6_MIP6)
0753     case IPV6_SRCRT_TYPE_2:
0754         if (accept_source_route < 0)
0755             goto unknown_rh;
0756         /* Silently discard invalid RTH type 2 */
0757         if (hdr->hdrlen != 2 || hdr->segments_left != 1) {
0758             __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
0759             kfree_skb(skb);
0760             return -1;
0761         }
0762         break;
0763 #endif
0764     default:
0765         goto unknown_rh;
0766     }
0767 
0768     /*
0769      *  This is the routing header forwarding algorithm from
0770      *  RFC 2460, page 16.
0771      */
0772 
0773     n = hdr->hdrlen >> 1;
0774 
0775     if (hdr->segments_left > n) {
0776         __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
0777         icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
0778                   ((&hdr->segments_left) -
0779                    skb_network_header(skb)));
0780         return -1;
0781     }
0782 
0783     /* We are about to mangle packet header. Be careful!
0784        Do not damage packets queued somewhere.
0785      */
0786     if (skb_cloned(skb)) {
0787         /* the copy is a forwarded packet */
0788         if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
0789             __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
0790                     IPSTATS_MIB_OUTDISCARDS);
0791             kfree_skb(skb);
0792             return -1;
0793         }
0794         hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
0795     }
0796 
0797     if (skb->ip_summed == CHECKSUM_COMPLETE)
0798         skb->ip_summed = CHECKSUM_NONE;
0799 
0800     i = n - --hdr->segments_left;
0801 
0802     rthdr = (struct rt0_hdr *) hdr;
0803     addr = rthdr->addr;
0804     addr += i - 1;
0805 
0806     switch (hdr->type) {
0807 #if IS_ENABLED(CONFIG_IPV6_MIP6)
0808     case IPV6_SRCRT_TYPE_2:
0809         if (xfrm6_input_addr(skb, (xfrm_address_t *)addr,
0810                      (xfrm_address_t *)&ipv6_hdr(skb)->saddr,
0811                      IPPROTO_ROUTING) < 0) {
0812             __IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
0813             kfree_skb(skb);
0814             return -1;
0815         }
0816         if (!ipv6_chk_home_addr(dev_net(skb_dst(skb)->dev), addr)) {
0817             __IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
0818             kfree_skb(skb);
0819             return -1;
0820         }
0821         break;
0822 #endif
0823     default:
0824         break;
0825     }
0826 
0827     if (ipv6_addr_is_multicast(addr)) {
0828         __IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
0829         kfree_skb(skb);
0830         return -1;
0831     }
0832 
0833     daddr = *addr;
0834     *addr = ipv6_hdr(skb)->daddr;
0835     ipv6_hdr(skb)->daddr = daddr;
0836 
0837     skb_dst_drop(skb);
0838     ip6_route_input(skb);
0839     if (skb_dst(skb)->error) {
0840         skb_push(skb, skb->data - skb_network_header(skb));
0841         dst_input(skb);
0842         return -1;
0843     }
0844 
0845     if (skb_dst(skb)->dev->flags&IFF_LOOPBACK) {
0846         if (ipv6_hdr(skb)->hop_limit <= 1) {
0847             __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
0848             icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
0849                     0);
0850             kfree_skb(skb);
0851             return -1;
0852         }
0853         ipv6_hdr(skb)->hop_limit--;
0854         goto looped_back;
0855     }
0856 
0857     skb_push(skb, skb->data - skb_network_header(skb));
0858     dst_input(skb);
0859     return -1;
0860 
0861 unknown_rh:
0862     __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
0863     icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
0864               (&hdr->type) - skb_network_header(skb));
0865     return -1;
0866 }
0867 
0868 static const struct inet6_protocol rthdr_protocol = {
0869     .handler    =   ipv6_rthdr_rcv,
0870     .flags      =   INET6_PROTO_NOPOLICY,
0871 };
0872 
0873 static const struct inet6_protocol destopt_protocol = {
0874     .handler    =   ipv6_destopt_rcv,
0875     .flags      =   INET6_PROTO_NOPOLICY,
0876 };
0877 
0878 static const struct inet6_protocol nodata_protocol = {
0879     .handler    =   dst_discard,
0880     .flags      =   INET6_PROTO_NOPOLICY,
0881 };
0882 
0883 int __init ipv6_exthdrs_init(void)
0884 {
0885     int ret;
0886 
0887     ret = inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING);
0888     if (ret)
0889         goto out;
0890 
0891     ret = inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
0892     if (ret)
0893         goto out_rthdr;
0894 
0895     ret = inet6_add_protocol(&nodata_protocol, IPPROTO_NONE);
0896     if (ret)
0897         goto out_destopt;
0898 
0899 out:
0900     return ret;
0901 out_destopt:
0902     inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
0903 out_rthdr:
0904     inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
0905     goto out;
0906 };
0907 
0908 void ipv6_exthdrs_exit(void)
0909 {
0910     inet6_del_protocol(&nodata_protocol, IPPROTO_NONE);
0911     inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
0912     inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
0913 }
0914 
0915 /**********************************
0916   Hop-by-hop options.
0917  **********************************/
0918 
0919 /*
0920  * Note: we cannot rely on skb_dst(skb) before we assign it in ip6_route_input().
0921  */
0922 static inline struct net *ipv6_skb_net(struct sk_buff *skb)
0923 {
0924     return skb_dst(skb) ? dev_net(skb_dst(skb)->dev) : dev_net(skb->dev);
0925 }
0926 
0927 /* Router Alert as of RFC 2711 */
0928 
0929 static bool ipv6_hop_ra(struct sk_buff *skb, int optoff)
0930 {
0931     const unsigned char *nh = skb_network_header(skb);
0932 
0933     if (nh[optoff + 1] == 2) {
0934         IP6CB(skb)->flags |= IP6SKB_ROUTERALERT;
0935         memcpy(&IP6CB(skb)->ra, nh + optoff + 2, sizeof(IP6CB(skb)->ra));
0936         return true;
0937     }
0938     net_dbg_ratelimited("ipv6_hop_ra: wrong RA length %d\n",
0939                 nh[optoff + 1]);
0940     kfree_skb_reason(skb, SKB_DROP_REASON_IP_INHDR);
0941     return false;
0942 }
0943 
0944 /* IOAM */
0945 
0946 static bool ipv6_hop_ioam(struct sk_buff *skb, int optoff)
0947 {
0948     struct ioam6_trace_hdr *trace;
0949     struct ioam6_namespace *ns;
0950     struct ioam6_hdr *hdr;
0951 
0952     /* Bad alignment (must be 4n-aligned) */
0953     if (optoff & 3)
0954         goto drop;
0955 
0956     /* Ignore if IOAM is not enabled on ingress */
0957     if (!__in6_dev_get(skb->dev)->cnf.ioam6_enabled)
0958         goto ignore;
0959 
0960     /* Truncated Option header */
0961     hdr = (struct ioam6_hdr *)(skb_network_header(skb) + optoff);
0962     if (hdr->opt_len < 2)
0963         goto drop;
0964 
0965     switch (hdr->type) {
0966     case IOAM6_TYPE_PREALLOC:
0967         /* Truncated Pre-allocated Trace header */
0968         if (hdr->opt_len < 2 + sizeof(*trace))
0969             goto drop;
0970 
0971         /* Malformed Pre-allocated Trace header */
0972         trace = (struct ioam6_trace_hdr *)((u8 *)hdr + sizeof(*hdr));
0973         if (hdr->opt_len < 2 + sizeof(*trace) + trace->remlen * 4)
0974             goto drop;
0975 
0976         /* Ignore if the IOAM namespace is unknown */
0977         ns = ioam6_namespace(ipv6_skb_net(skb), trace->namespace_id);
0978         if (!ns)
0979             goto ignore;
0980 
0981         if (!skb_valid_dst(skb))
0982             ip6_route_input(skb);
0983 
0984         ioam6_fill_trace_data(skb, ns, trace, true);
0985         break;
0986     default:
0987         break;
0988     }
0989 
0990 ignore:
0991     return true;
0992 
0993 drop:
0994     kfree_skb_reason(skb, SKB_DROP_REASON_IP_INHDR);
0995     return false;
0996 }
0997 
0998 /* Jumbo payload */
0999 
1000 static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff)
1001 {
1002     const unsigned char *nh = skb_network_header(skb);
1003     SKB_DR(reason);
1004     u32 pkt_len;
1005 
1006     if (nh[optoff + 1] != 4 || (optoff & 3) != 2) {
1007         net_dbg_ratelimited("ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n",
1008                     nh[optoff+1]);
1009         SKB_DR_SET(reason, IP_INHDR);
1010         goto drop;
1011     }
1012 
1013     pkt_len = ntohl(*(__be32 *)(nh + optoff + 2));
1014     if (pkt_len <= IPV6_MAXPLEN) {
1015         icmpv6_param_prob_reason(skb, ICMPV6_HDR_FIELD, optoff + 2,
1016                      SKB_DROP_REASON_IP_INHDR);
1017         return false;
1018     }
1019     if (ipv6_hdr(skb)->payload_len) {
1020         icmpv6_param_prob_reason(skb, ICMPV6_HDR_FIELD, optoff,
1021                      SKB_DROP_REASON_IP_INHDR);
1022         return false;
1023     }
1024 
1025     if (pkt_len > skb->len - sizeof(struct ipv6hdr)) {
1026         SKB_DR_SET(reason, PKT_TOO_SMALL);
1027         goto drop;
1028     }
1029 
1030     if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
1031         goto drop;
1032 
1033     IP6CB(skb)->flags |= IP6SKB_JUMBOGRAM;
1034     return true;
1035 
1036 drop:
1037     kfree_skb_reason(skb, reason);
1038     return false;
1039 }
1040 
1041 /* CALIPSO RFC 5570 */
1042 
1043 static bool ipv6_hop_calipso(struct sk_buff *skb, int optoff)
1044 {
1045     const unsigned char *nh = skb_network_header(skb);
1046 
1047     if (nh[optoff + 1] < 8)
1048         goto drop;
1049 
1050     if (nh[optoff + 6] * 4 + 8 > nh[optoff + 1])
1051         goto drop;
1052 
1053     if (!calipso_validate(skb, nh + optoff))
1054         goto drop;
1055 
1056     return true;
1057 
1058 drop:
1059     kfree_skb_reason(skb, SKB_DROP_REASON_IP_INHDR);
1060     return false;
1061 }
1062 
1063 int ipv6_parse_hopopts(struct sk_buff *skb)
1064 {
1065     struct inet6_skb_parm *opt = IP6CB(skb);
1066     struct net *net = dev_net(skb->dev);
1067     int extlen;
1068 
1069     /*
1070      * skb_network_header(skb) is equal to skb->data, and
1071      * skb_network_header_len(skb) is always equal to
1072      * sizeof(struct ipv6hdr) by definition of
1073      * hop-by-hop options.
1074      */
1075     if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) ||
1076         !pskb_may_pull(skb, (sizeof(struct ipv6hdr) +
1077                  ((skb_transport_header(skb)[1] + 1) << 3)))) {
1078 fail_and_free:
1079         kfree_skb(skb);
1080         return -1;
1081     }
1082 
1083     extlen = (skb_transport_header(skb)[1] + 1) << 3;
1084     if (extlen > net->ipv6.sysctl.max_hbh_opts_len)
1085         goto fail_and_free;
1086 
1087     opt->flags |= IP6SKB_HOPBYHOP;
1088     if (ip6_parse_tlv(true, skb, net->ipv6.sysctl.max_hbh_opts_cnt)) {
1089         skb->transport_header += extlen;
1090         opt = IP6CB(skb);
1091         opt->nhoff = sizeof(struct ipv6hdr);
1092         return 1;
1093     }
1094     return -1;
1095 }
1096 
1097 /*
1098  *  Creating outbound headers.
1099  *
1100  *  "build" functions work when skb is filled from head to tail (datagram)
1101  *  "push"  functions work when headers are added from tail to head (tcp)
1102  *
1103  *  In both cases we assume, that caller reserved enough room
1104  *  for headers.
1105  */
1106 
1107 static void ipv6_push_rthdr0(struct sk_buff *skb, u8 *proto,
1108                  struct ipv6_rt_hdr *opt,
1109                  struct in6_addr **addr_p, struct in6_addr *saddr)
1110 {
1111     struct rt0_hdr *phdr, *ihdr;
1112     int hops;
1113 
1114     ihdr = (struct rt0_hdr *) opt;
1115 
1116     phdr = skb_push(skb, (ihdr->rt_hdr.hdrlen + 1) << 3);
1117     memcpy(phdr, ihdr, sizeof(struct rt0_hdr));
1118 
1119     hops = ihdr->rt_hdr.hdrlen >> 1;
1120 
1121     if (hops > 1)
1122         memcpy(phdr->addr, ihdr->addr + 1,
1123                (hops - 1) * sizeof(struct in6_addr));
1124 
1125     phdr->addr[hops - 1] = **addr_p;
1126     *addr_p = ihdr->addr;
1127 
1128     phdr->rt_hdr.nexthdr = *proto;
1129     *proto = NEXTHDR_ROUTING;
1130 }
1131 
1132 static void ipv6_push_rthdr4(struct sk_buff *skb, u8 *proto,
1133                  struct ipv6_rt_hdr *opt,
1134                  struct in6_addr **addr_p, struct in6_addr *saddr)
1135 {
1136     struct ipv6_sr_hdr *sr_phdr, *sr_ihdr;
1137     int plen, hops;
1138 
1139     sr_ihdr = (struct ipv6_sr_hdr *)opt;
1140     plen = (sr_ihdr->hdrlen + 1) << 3;
1141 
1142     sr_phdr = skb_push(skb, plen);
1143     memcpy(sr_phdr, sr_ihdr, sizeof(struct ipv6_sr_hdr));
1144 
1145     hops = sr_ihdr->first_segment + 1;
1146     memcpy(sr_phdr->segments + 1, sr_ihdr->segments + 1,
1147            (hops - 1) * sizeof(struct in6_addr));
1148 
1149     sr_phdr->segments[0] = **addr_p;
1150     *addr_p = &sr_ihdr->segments[sr_ihdr->segments_left];
1151 
1152     if (sr_ihdr->hdrlen > hops * 2) {
1153         int tlvs_offset, tlvs_length;
1154 
1155         tlvs_offset = (1 + hops * 2) << 3;
1156         tlvs_length = (sr_ihdr->hdrlen - hops * 2) << 3;
1157         memcpy((char *)sr_phdr + tlvs_offset,
1158                (char *)sr_ihdr + tlvs_offset, tlvs_length);
1159     }
1160 
1161 #ifdef CONFIG_IPV6_SEG6_HMAC
1162     if (sr_has_hmac(sr_phdr)) {
1163         struct net *net = NULL;
1164 
1165         if (skb->dev)
1166             net = dev_net(skb->dev);
1167         else if (skb->sk)
1168             net = sock_net(skb->sk);
1169 
1170         WARN_ON(!net);
1171 
1172         if (net)
1173             seg6_push_hmac(net, saddr, sr_phdr);
1174     }
1175 #endif
1176 
1177     sr_phdr->nexthdr = *proto;
1178     *proto = NEXTHDR_ROUTING;
1179 }
1180 
1181 static void ipv6_push_rthdr(struct sk_buff *skb, u8 *proto,
1182                 struct ipv6_rt_hdr *opt,
1183                 struct in6_addr **addr_p, struct in6_addr *saddr)
1184 {
1185     switch (opt->type) {
1186     case IPV6_SRCRT_TYPE_0:
1187     case IPV6_SRCRT_STRICT:
1188     case IPV6_SRCRT_TYPE_2:
1189         ipv6_push_rthdr0(skb, proto, opt, addr_p, saddr);
1190         break;
1191     case IPV6_SRCRT_TYPE_4:
1192         ipv6_push_rthdr4(skb, proto, opt, addr_p, saddr);
1193         break;
1194     default:
1195         break;
1196     }
1197 }
1198 
1199 static void ipv6_push_exthdr(struct sk_buff *skb, u8 *proto, u8 type, struct ipv6_opt_hdr *opt)
1200 {
1201     struct ipv6_opt_hdr *h = skb_push(skb, ipv6_optlen(opt));
1202 
1203     memcpy(h, opt, ipv6_optlen(opt));
1204     h->nexthdr = *proto;
1205     *proto = type;
1206 }
1207 
1208 void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
1209               u8 *proto,
1210               struct in6_addr **daddr, struct in6_addr *saddr)
1211 {
1212     if (opt->srcrt) {
1213         ipv6_push_rthdr(skb, proto, opt->srcrt, daddr, saddr);
1214         /*
1215          * IPV6_RTHDRDSTOPTS is ignored
1216          * unless IPV6_RTHDR is set (RFC3542).
1217          */
1218         if (opt->dst0opt)
1219             ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst0opt);
1220     }
1221     if (opt->hopopt)
1222         ipv6_push_exthdr(skb, proto, NEXTHDR_HOP, opt->hopopt);
1223 }
1224 
1225 void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, u8 *proto)
1226 {
1227     if (opt->dst1opt)
1228         ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst1opt);
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_kmalloc(sk, opt->tot_len, GFP_ATOMIC);
1238     if (opt2) {
1239         long dif = (char *)opt2 - (char *)opt;
1240         memcpy(opt2, opt, opt->tot_len);
1241         if (opt2->hopopt)
1242             *((char **)&opt2->hopopt) += dif;
1243         if (opt2->dst0opt)
1244             *((char **)&opt2->dst0opt) += dif;
1245         if (opt2->dst1opt)
1246             *((char **)&opt2->dst1opt) += dif;
1247         if (opt2->srcrt)
1248             *((char **)&opt2->srcrt) += dif;
1249         refcount_set(&opt2->refcnt, 1);
1250     }
1251     return opt2;
1252 }
1253 EXPORT_SYMBOL_GPL(ipv6_dup_options);
1254 
1255 static void ipv6_renew_option(int renewtype,
1256                   struct ipv6_opt_hdr **dest,
1257                   struct ipv6_opt_hdr *old,
1258                   struct ipv6_opt_hdr *new,
1259                   int newtype, char **p)
1260 {
1261     struct ipv6_opt_hdr *src;
1262 
1263     src = (renewtype == newtype ? new : old);
1264     if (!src)
1265         return;
1266 
1267     memcpy(*p, src, ipv6_optlen(src));
1268     *dest = (struct ipv6_opt_hdr *)*p;
1269     *p += CMSG_ALIGN(ipv6_optlen(*dest));
1270 }
1271 
1272 /**
1273  * ipv6_renew_options - replace a specific ext hdr with a new one.
1274  *
1275  * @sk: sock from which to allocate memory
1276  * @opt: original options
1277  * @newtype: option type to replace in @opt
1278  * @newopt: new option of type @newtype to replace (user-mem)
1279  *
1280  * Returns a new set of options which is a copy of @opt with the
1281  * option type @newtype replaced with @newopt.
1282  *
1283  * @opt may be NULL, in which case a new set of options is returned
1284  * containing just @newopt.
1285  *
1286  * @newopt may be NULL, in which case the specified option type is
1287  * not copied into the new set of options.
1288  *
1289  * The new set of options is allocated from the socket option memory
1290  * buffer of @sk.
1291  */
1292 struct ipv6_txoptions *
1293 ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt,
1294            int newtype, struct ipv6_opt_hdr *newopt)
1295 {
1296     int tot_len = 0;
1297     char *p;
1298     struct ipv6_txoptions *opt2;
1299 
1300     if (opt) {
1301         if (newtype != IPV6_HOPOPTS && opt->hopopt)
1302             tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt));
1303         if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt)
1304             tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt));
1305         if (newtype != IPV6_RTHDR && opt->srcrt)
1306             tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt));
1307         if (newtype != IPV6_DSTOPTS && opt->dst1opt)
1308             tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt));
1309     }
1310 
1311     if (newopt)
1312         tot_len += CMSG_ALIGN(ipv6_optlen(newopt));
1313 
1314     if (!tot_len)
1315         return NULL;
1316 
1317     tot_len += sizeof(*opt2);
1318     opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC);
1319     if (!opt2)
1320         return ERR_PTR(-ENOBUFS);
1321 
1322     memset(opt2, 0, tot_len);
1323     refcount_set(&opt2->refcnt, 1);
1324     opt2->tot_len = tot_len;
1325     p = (char *)(opt2 + 1);
1326 
1327     ipv6_renew_option(IPV6_HOPOPTS, &opt2->hopopt,
1328               (opt ? opt->hopopt : NULL),
1329               newopt, newtype, &p);
1330     ipv6_renew_option(IPV6_RTHDRDSTOPTS, &opt2->dst0opt,
1331               (opt ? opt->dst0opt : NULL),
1332               newopt, newtype, &p);
1333     ipv6_renew_option(IPV6_RTHDR,
1334               (struct ipv6_opt_hdr **)&opt2->srcrt,
1335               (opt ? (struct ipv6_opt_hdr *)opt->srcrt : NULL),
1336               newopt, newtype, &p);
1337     ipv6_renew_option(IPV6_DSTOPTS, &opt2->dst1opt,
1338               (opt ? opt->dst1opt : NULL),
1339               newopt, newtype, &p);
1340 
1341     opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) +
1342               (opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) +
1343               (opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0);
1344     opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0);
1345 
1346     return opt2;
1347 }
1348 
1349 struct ipv6_txoptions *__ipv6_fixup_options(struct ipv6_txoptions *opt_space,
1350                         struct ipv6_txoptions *opt)
1351 {
1352     /*
1353      * ignore the dest before srcrt unless srcrt is being included.
1354      * --yoshfuji
1355      */
1356     if (opt->dst0opt && !opt->srcrt) {
1357         if (opt_space != opt) {
1358             memcpy(opt_space, opt, sizeof(*opt_space));
1359             opt = opt_space;
1360         }
1361         opt->opt_nflen -= ipv6_optlen(opt->dst0opt);
1362         opt->dst0opt = NULL;
1363     }
1364 
1365     return opt;
1366 }
1367 EXPORT_SYMBOL_GPL(__ipv6_fixup_options);
1368 
1369 /**
1370  * fl6_update_dst - update flowi destination address with info given
1371  *                  by srcrt option, if any.
1372  *
1373  * @fl6: flowi6 for which daddr is to be updated
1374  * @opt: struct ipv6_txoptions in which to look for srcrt opt
1375  * @orig: copy of original daddr address if modified
1376  *
1377  * Returns NULL if no txoptions or no srcrt, otherwise returns orig
1378  * and initial value of fl6->daddr set in orig
1379  */
1380 struct in6_addr *fl6_update_dst(struct flowi6 *fl6,
1381                 const struct ipv6_txoptions *opt,
1382                 struct in6_addr *orig)
1383 {
1384     if (!opt || !opt->srcrt)
1385         return NULL;
1386 
1387     *orig = fl6->daddr;
1388 
1389     switch (opt->srcrt->type) {
1390     case IPV6_SRCRT_TYPE_0:
1391     case IPV6_SRCRT_STRICT:
1392     case IPV6_SRCRT_TYPE_2:
1393         fl6->daddr = *((struct rt0_hdr *)opt->srcrt)->addr;
1394         break;
1395     case IPV6_SRCRT_TYPE_4:
1396     {
1397         struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)opt->srcrt;
1398 
1399         fl6->daddr = srh->segments[srh->segments_left];
1400         break;
1401     }
1402     default:
1403         return NULL;
1404     }
1405 
1406     return orig;
1407 }
1408 EXPORT_SYMBOL_GPL(fl6_update_dst);