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