xref: /openbmc/linux/net/ipv6/exthdrs.c (revision aded0023)
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 <net/ioam6.h>
54 #include <net/dst_metadata.h>
55 
56 #include <linux/uaccess.h>
57 
58 /*********************
59   Generic functions
60  *********************/
61 
62 /* An unknown option is detected, decide what to do */
63 
64 static bool ip6_tlvopt_unknown(struct sk_buff *skb, int optoff,
65 			       bool disallow_unknowns)
66 {
67 	if (disallow_unknowns) {
68 		/* If unknown TLVs are disallowed by configuration
69 		 * then always silently drop packet. Note this also
70 		 * means no ICMP parameter problem is sent which
71 		 * could be a good property to mitigate a reflection DOS
72 		 * attack.
73 		 */
74 
75 		goto drop;
76 	}
77 
78 	switch ((skb_network_header(skb)[optoff] & 0xC0) >> 6) {
79 	case 0: /* ignore */
80 		return true;
81 
82 	case 1: /* drop packet */
83 		break;
84 
85 	case 3: /* Send ICMP if not a multicast address and drop packet */
86 		/* Actually, it is redundant check. icmp_send
87 		   will recheck in any case.
88 		 */
89 		if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr))
90 			break;
91 		fallthrough;
92 	case 2: /* send ICMP PARM PROB regardless and drop packet */
93 		icmpv6_param_prob_reason(skb, ICMPV6_UNK_OPTION, optoff,
94 					 SKB_DROP_REASON_UNHANDLED_PROTO);
95 		return false;
96 	}
97 
98 drop:
99 	kfree_skb_reason(skb, SKB_DROP_REASON_UNHANDLED_PROTO);
100 	return false;
101 }
102 
103 static bool ipv6_hop_ra(struct sk_buff *skb, int optoff);
104 static bool ipv6_hop_ioam(struct sk_buff *skb, int optoff);
105 static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff);
106 static bool ipv6_hop_calipso(struct sk_buff *skb, int optoff);
107 #if IS_ENABLED(CONFIG_IPV6_MIP6)
108 static bool ipv6_dest_hao(struct sk_buff *skb, int optoff);
109 #endif
110 
111 /* Parse tlv encoded option header (hop-by-hop or destination) */
112 
113 static bool ip6_parse_tlv(bool hopbyhop,
114 			  struct sk_buff *skb,
115 			  int max_count)
116 {
117 	int len = (skb_transport_header(skb)[1] + 1) << 3;
118 	const unsigned char *nh = skb_network_header(skb);
119 	int off = skb_network_header_len(skb);
120 	bool disallow_unknowns = false;
121 	int tlv_count = 0;
122 	int padlen = 0;
123 
124 	if (unlikely(max_count < 0)) {
125 		disallow_unknowns = true;
126 		max_count = -max_count;
127 	}
128 
129 	off += 2;
130 	len -= 2;
131 
132 	while (len > 0) {
133 		int optlen, i;
134 
135 		if (nh[off] == IPV6_TLV_PAD1) {
136 			padlen++;
137 			if (padlen > 7)
138 				goto bad;
139 			off++;
140 			len--;
141 			continue;
142 		}
143 		if (len < 2)
144 			goto bad;
145 		optlen = nh[off + 1] + 2;
146 		if (optlen > len)
147 			goto bad;
148 
149 		if (nh[off] == IPV6_TLV_PADN) {
150 			/* RFC 2460 states that the purpose of PadN is
151 			 * to align the containing header to multiples
152 			 * of 8. 7 is therefore the highest valid value.
153 			 * See also RFC 4942, Section 2.1.9.5.
154 			 */
155 			padlen += optlen;
156 			if (padlen > 7)
157 				goto bad;
158 			/* RFC 4942 recommends receiving hosts to
159 			 * actively check PadN payload to contain
160 			 * only zeroes.
161 			 */
162 			for (i = 2; i < optlen; i++) {
163 				if (nh[off + i] != 0)
164 					goto bad;
165 			}
166 		} else {
167 			tlv_count++;
168 			if (tlv_count > max_count)
169 				goto bad;
170 
171 			if (hopbyhop) {
172 				switch (nh[off]) {
173 				case IPV6_TLV_ROUTERALERT:
174 					if (!ipv6_hop_ra(skb, off))
175 						return false;
176 					break;
177 				case IPV6_TLV_IOAM:
178 					if (!ipv6_hop_ioam(skb, off))
179 						return false;
180 					break;
181 				case IPV6_TLV_JUMBO:
182 					if (!ipv6_hop_jumbo(skb, off))
183 						return false;
184 					break;
185 				case IPV6_TLV_CALIPSO:
186 					if (!ipv6_hop_calipso(skb, off))
187 						return false;
188 					break;
189 				default:
190 					if (!ip6_tlvopt_unknown(skb, off,
191 								disallow_unknowns))
192 						return false;
193 					break;
194 				}
195 			} else {
196 				switch (nh[off]) {
197 #if IS_ENABLED(CONFIG_IPV6_MIP6)
198 				case IPV6_TLV_HAO:
199 					if (!ipv6_dest_hao(skb, off))
200 						return false;
201 					break;
202 #endif
203 				default:
204 					if (!ip6_tlvopt_unknown(skb, off,
205 								disallow_unknowns))
206 						return false;
207 					break;
208 				}
209 			}
210 			padlen = 0;
211 		}
212 		off += optlen;
213 		len -= optlen;
214 	}
215 
216 	if (len == 0)
217 		return true;
218 bad:
219 	kfree_skb_reason(skb, SKB_DROP_REASON_IP_INHDR);
220 	return false;
221 }
222 
223 /*****************************
224   Destination options header.
225  *****************************/
226 
227 #if IS_ENABLED(CONFIG_IPV6_MIP6)
228 static bool ipv6_dest_hao(struct sk_buff *skb, int optoff)
229 {
230 	struct ipv6_destopt_hao *hao;
231 	struct inet6_skb_parm *opt = IP6CB(skb);
232 	struct ipv6hdr *ipv6h = ipv6_hdr(skb);
233 	SKB_DR(reason);
234 	int ret;
235 
236 	if (opt->dsthao) {
237 		net_dbg_ratelimited("hao duplicated\n");
238 		goto discard;
239 	}
240 	opt->dsthao = opt->dst1;
241 	opt->dst1 = 0;
242 
243 	hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + optoff);
244 
245 	if (hao->length != 16) {
246 		net_dbg_ratelimited("hao invalid option length = %d\n",
247 				    hao->length);
248 		SKB_DR_SET(reason, IP_INHDR);
249 		goto discard;
250 	}
251 
252 	if (!(ipv6_addr_type(&hao->addr) & IPV6_ADDR_UNICAST)) {
253 		net_dbg_ratelimited("hao is not an unicast addr: %pI6\n",
254 				    &hao->addr);
255 		SKB_DR_SET(reason, INVALID_PROTO);
256 		goto discard;
257 	}
258 
259 	ret = xfrm6_input_addr(skb, (xfrm_address_t *)&ipv6h->daddr,
260 			       (xfrm_address_t *)&hao->addr, IPPROTO_DSTOPTS);
261 	if (unlikely(ret < 0)) {
262 		SKB_DR_SET(reason, XFRM_POLICY);
263 		goto discard;
264 	}
265 
266 	if (skb_cloned(skb)) {
267 		if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
268 			goto discard;
269 
270 		/* update all variable using below by copied skbuff */
271 		hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) +
272 						  optoff);
273 		ipv6h = ipv6_hdr(skb);
274 	}
275 
276 	if (skb->ip_summed == CHECKSUM_COMPLETE)
277 		skb->ip_summed = CHECKSUM_NONE;
278 
279 	swap(ipv6h->saddr, hao->addr);
280 
281 	if (skb->tstamp == 0)
282 		__net_timestamp(skb);
283 
284 	return true;
285 
286  discard:
287 	kfree_skb_reason(skb, reason);
288 	return false;
289 }
290 #endif
291 
292 static int ipv6_destopt_rcv(struct sk_buff *skb)
293 {
294 	struct inet6_dev *idev = __in6_dev_get(skb->dev);
295 	struct inet6_skb_parm *opt = IP6CB(skb);
296 #if IS_ENABLED(CONFIG_IPV6_MIP6)
297 	__u16 dstbuf;
298 #endif
299 	struct dst_entry *dst = skb_dst(skb);
300 	struct net *net = dev_net(skb->dev);
301 	int extlen;
302 
303 	if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
304 	    !pskb_may_pull(skb, (skb_transport_offset(skb) +
305 				 ((skb_transport_header(skb)[1] + 1) << 3)))) {
306 		__IP6_INC_STATS(dev_net(dst->dev), idev,
307 				IPSTATS_MIB_INHDRERRORS);
308 fail_and_free:
309 		kfree_skb(skb);
310 		return -1;
311 	}
312 
313 	extlen = (skb_transport_header(skb)[1] + 1) << 3;
314 	if (extlen > net->ipv6.sysctl.max_dst_opts_len)
315 		goto fail_and_free;
316 
317 	opt->lastopt = opt->dst1 = skb_network_header_len(skb);
318 #if IS_ENABLED(CONFIG_IPV6_MIP6)
319 	dstbuf = opt->dst1;
320 #endif
321 
322 	if (ip6_parse_tlv(false, skb, net->ipv6.sysctl.max_dst_opts_cnt)) {
323 		skb->transport_header += extlen;
324 		opt = IP6CB(skb);
325 #if IS_ENABLED(CONFIG_IPV6_MIP6)
326 		opt->nhoff = dstbuf;
327 #else
328 		opt->nhoff = opt->dst1;
329 #endif
330 		return 1;
331 	}
332 
333 	__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
334 	return -1;
335 }
336 
337 static void seg6_update_csum(struct sk_buff *skb)
338 {
339 	struct ipv6_sr_hdr *hdr;
340 	struct in6_addr *addr;
341 	__be32 from, to;
342 
343 	/* srh is at transport offset and seg_left is already decremented
344 	 * but daddr is not yet updated with next segment
345 	 */
346 
347 	hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb);
348 	addr = hdr->segments + hdr->segments_left;
349 
350 	hdr->segments_left++;
351 	from = *(__be32 *)hdr;
352 
353 	hdr->segments_left--;
354 	to = *(__be32 *)hdr;
355 
356 	/* update skb csum with diff resulting from seg_left decrement */
357 
358 	update_csum_diff4(skb, from, to);
359 
360 	/* compute csum diff between current and next segment and update */
361 
362 	update_csum_diff16(skb, (__be32 *)(&ipv6_hdr(skb)->daddr),
363 			   (__be32 *)addr);
364 }
365 
366 static int ipv6_srh_rcv(struct sk_buff *skb)
367 {
368 	struct inet6_skb_parm *opt = IP6CB(skb);
369 	struct net *net = dev_net(skb->dev);
370 	struct ipv6_sr_hdr *hdr;
371 	struct inet6_dev *idev;
372 	struct in6_addr *addr;
373 	int accept_seg6;
374 
375 	hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb);
376 
377 	idev = __in6_dev_get(skb->dev);
378 
379 	accept_seg6 = net->ipv6.devconf_all->seg6_enabled;
380 	if (accept_seg6 > idev->cnf.seg6_enabled)
381 		accept_seg6 = idev->cnf.seg6_enabled;
382 
383 	if (!accept_seg6) {
384 		kfree_skb(skb);
385 		return -1;
386 	}
387 
388 #ifdef CONFIG_IPV6_SEG6_HMAC
389 	if (!seg6_hmac_validate_skb(skb)) {
390 		kfree_skb(skb);
391 		return -1;
392 	}
393 #endif
394 
395 looped_back:
396 	if (hdr->segments_left == 0) {
397 		if (hdr->nexthdr == NEXTHDR_IPV6 || hdr->nexthdr == NEXTHDR_IPV4) {
398 			int offset = (hdr->hdrlen + 1) << 3;
399 
400 			skb_postpull_rcsum(skb, skb_network_header(skb),
401 					   skb_network_header_len(skb));
402 			skb_pull(skb, offset);
403 			skb_postpull_rcsum(skb, skb_transport_header(skb),
404 					   offset);
405 
406 			skb_reset_network_header(skb);
407 			skb_reset_transport_header(skb);
408 			skb->encapsulation = 0;
409 			if (hdr->nexthdr == NEXTHDR_IPV4)
410 				skb->protocol = htons(ETH_P_IP);
411 			__skb_tunnel_rx(skb, skb->dev, net);
412 
413 			netif_rx(skb);
414 			return -1;
415 		}
416 
417 		opt->srcrt = skb_network_header_len(skb);
418 		opt->lastopt = opt->srcrt;
419 		skb->transport_header += (hdr->hdrlen + 1) << 3;
420 		opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
421 
422 		return 1;
423 	}
424 
425 	if (hdr->segments_left >= (hdr->hdrlen >> 1)) {
426 		__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
427 		icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
428 				  ((&hdr->segments_left) -
429 				   skb_network_header(skb)));
430 		return -1;
431 	}
432 
433 	if (skb_cloned(skb)) {
434 		if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
435 			__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
436 					IPSTATS_MIB_OUTDISCARDS);
437 			kfree_skb(skb);
438 			return -1;
439 		}
440 
441 		hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb);
442 	}
443 
444 	hdr->segments_left--;
445 	addr = hdr->segments + hdr->segments_left;
446 
447 	skb_push(skb, sizeof(struct ipv6hdr));
448 
449 	if (skb->ip_summed == CHECKSUM_COMPLETE)
450 		seg6_update_csum(skb);
451 
452 	ipv6_hdr(skb)->daddr = *addr;
453 
454 	ip6_route_input(skb);
455 
456 	if (skb_dst(skb)->error) {
457 		dst_input(skb);
458 		return -1;
459 	}
460 
461 	if (skb_dst(skb)->dev->flags & IFF_LOOPBACK) {
462 		if (ipv6_hdr(skb)->hop_limit <= 1) {
463 			__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
464 			icmpv6_send(skb, ICMPV6_TIME_EXCEED,
465 				    ICMPV6_EXC_HOPLIMIT, 0);
466 			kfree_skb(skb);
467 			return -1;
468 		}
469 		ipv6_hdr(skb)->hop_limit--;
470 
471 		skb_pull(skb, sizeof(struct ipv6hdr));
472 		goto looped_back;
473 	}
474 
475 	dst_input(skb);
476 
477 	return -1;
478 }
479 
480 static int ipv6_rpl_srh_rcv(struct sk_buff *skb)
481 {
482 	struct ipv6_rpl_sr_hdr *hdr, *ohdr, *chdr;
483 	struct inet6_skb_parm *opt = IP6CB(skb);
484 	struct net *net = dev_net(skb->dev);
485 	struct inet6_dev *idev;
486 	struct ipv6hdr *oldhdr;
487 	unsigned char *buf;
488 	int accept_rpl_seg;
489 	int i, err;
490 	u64 n = 0;
491 	u32 r;
492 
493 	idev = __in6_dev_get(skb->dev);
494 
495 	accept_rpl_seg = net->ipv6.devconf_all->rpl_seg_enabled;
496 	if (accept_rpl_seg > idev->cnf.rpl_seg_enabled)
497 		accept_rpl_seg = idev->cnf.rpl_seg_enabled;
498 
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 	skb_dst_drop(skb);
616 
617 	ip6_route_input(skb);
618 
619 	if (skb_dst(skb)->error) {
620 		dst_input(skb);
621 		return -1;
622 	}
623 
624 	if (skb_dst(skb)->dev->flags & IFF_LOOPBACK) {
625 		if (ipv6_hdr(skb)->hop_limit <= 1) {
626 			__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
627 			icmpv6_send(skb, ICMPV6_TIME_EXCEED,
628 				    ICMPV6_EXC_HOPLIMIT, 0);
629 			kfree_skb(skb);
630 			return -1;
631 		}
632 		ipv6_hdr(skb)->hop_limit--;
633 
634 		skb_pull(skb, sizeof(struct ipv6hdr));
635 		goto looped_back;
636 	}
637 
638 	dst_input(skb);
639 
640 	return -1;
641 }
642 
643 /********************************
644   Routing header.
645  ********************************/
646 
647 /* called with rcu_read_lock() */
648 static int ipv6_rthdr_rcv(struct sk_buff *skb)
649 {
650 	struct inet6_dev *idev = __in6_dev_get(skb->dev);
651 	struct inet6_skb_parm *opt = IP6CB(skb);
652 	struct in6_addr *addr = NULL;
653 	struct in6_addr daddr;
654 	int n, i;
655 	struct ipv6_rt_hdr *hdr;
656 	struct rt0_hdr *rthdr;
657 	struct net *net = dev_net(skb->dev);
658 	int accept_source_route = net->ipv6.devconf_all->accept_source_route;
659 
660 	if (idev && accept_source_route > idev->cnf.accept_source_route)
661 		accept_source_route = 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(dev_net(skb_dst(skb)->dev), 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 	daddr = *addr;
802 	*addr = ipv6_hdr(skb)->daddr;
803 	ipv6_hdr(skb)->daddr = daddr;
804 
805 	ip6_route_input(skb);
806 	if (skb_dst(skb)->error) {
807 		skb_push(skb, skb->data - skb_network_header(skb));
808 		dst_input(skb);
809 		return -1;
810 	}
811 
812 	if (skb_dst(skb)->dev->flags&IFF_LOOPBACK) {
813 		if (ipv6_hdr(skb)->hop_limit <= 1) {
814 			__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
815 			icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
816 				    0);
817 			kfree_skb(skb);
818 			return -1;
819 		}
820 		ipv6_hdr(skb)->hop_limit--;
821 		goto looped_back;
822 	}
823 
824 	skb_push(skb, skb->data - skb_network_header(skb));
825 	dst_input(skb);
826 	return -1;
827 
828 unknown_rh:
829 	__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
830 	icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
831 			  (&hdr->type) - skb_network_header(skb));
832 	return -1;
833 }
834 
835 static const struct inet6_protocol rthdr_protocol = {
836 	.handler	=	ipv6_rthdr_rcv,
837 	.flags		=	INET6_PROTO_NOPOLICY,
838 };
839 
840 static const struct inet6_protocol destopt_protocol = {
841 	.handler	=	ipv6_destopt_rcv,
842 	.flags		=	INET6_PROTO_NOPOLICY,
843 };
844 
845 static const struct inet6_protocol nodata_protocol = {
846 	.handler	=	dst_discard,
847 	.flags		=	INET6_PROTO_NOPOLICY,
848 };
849 
850 int __init ipv6_exthdrs_init(void)
851 {
852 	int ret;
853 
854 	ret = inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING);
855 	if (ret)
856 		goto out;
857 
858 	ret = inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
859 	if (ret)
860 		goto out_rthdr;
861 
862 	ret = inet6_add_protocol(&nodata_protocol, IPPROTO_NONE);
863 	if (ret)
864 		goto out_destopt;
865 
866 out:
867 	return ret;
868 out_destopt:
869 	inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
870 out_rthdr:
871 	inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
872 	goto out;
873 };
874 
875 void ipv6_exthdrs_exit(void)
876 {
877 	inet6_del_protocol(&nodata_protocol, IPPROTO_NONE);
878 	inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
879 	inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
880 }
881 
882 /**********************************
883   Hop-by-hop options.
884  **********************************/
885 
886 /*
887  * Note: we cannot rely on skb_dst(skb) before we assign it in ip6_route_input().
888  */
889 static inline struct net *ipv6_skb_net(struct sk_buff *skb)
890 {
891 	return skb_dst(skb) ? dev_net(skb_dst(skb)->dev) : dev_net(skb->dev);
892 }
893 
894 /* Router Alert as of RFC 2711 */
895 
896 static bool ipv6_hop_ra(struct sk_buff *skb, int optoff)
897 {
898 	const unsigned char *nh = skb_network_header(skb);
899 
900 	if (nh[optoff + 1] == 2) {
901 		IP6CB(skb)->flags |= IP6SKB_ROUTERALERT;
902 		memcpy(&IP6CB(skb)->ra, nh + optoff + 2, sizeof(IP6CB(skb)->ra));
903 		return true;
904 	}
905 	net_dbg_ratelimited("ipv6_hop_ra: wrong RA length %d\n",
906 			    nh[optoff + 1]);
907 	kfree_skb_reason(skb, SKB_DROP_REASON_IP_INHDR);
908 	return false;
909 }
910 
911 /* IOAM */
912 
913 static bool ipv6_hop_ioam(struct sk_buff *skb, int optoff)
914 {
915 	struct ioam6_trace_hdr *trace;
916 	struct ioam6_namespace *ns;
917 	struct ioam6_hdr *hdr;
918 
919 	/* Bad alignment (must be 4n-aligned) */
920 	if (optoff & 3)
921 		goto drop;
922 
923 	/* Ignore if IOAM is not enabled on ingress */
924 	if (!__in6_dev_get(skb->dev)->cnf.ioam6_enabled)
925 		goto ignore;
926 
927 	/* Truncated Option header */
928 	hdr = (struct ioam6_hdr *)(skb_network_header(skb) + optoff);
929 	if (hdr->opt_len < 2)
930 		goto drop;
931 
932 	switch (hdr->type) {
933 	case IOAM6_TYPE_PREALLOC:
934 		/* Truncated Pre-allocated Trace header */
935 		if (hdr->opt_len < 2 + sizeof(*trace))
936 			goto drop;
937 
938 		/* Malformed Pre-allocated Trace header */
939 		trace = (struct ioam6_trace_hdr *)((u8 *)hdr + sizeof(*hdr));
940 		if (hdr->opt_len < 2 + sizeof(*trace) + trace->remlen * 4)
941 			goto drop;
942 
943 		/* Ignore if the IOAM namespace is unknown */
944 		ns = ioam6_namespace(ipv6_skb_net(skb), trace->namespace_id);
945 		if (!ns)
946 			goto ignore;
947 
948 		if (!skb_valid_dst(skb))
949 			ip6_route_input(skb);
950 
951 		ioam6_fill_trace_data(skb, ns, trace, true);
952 		break;
953 	default:
954 		break;
955 	}
956 
957 ignore:
958 	return true;
959 
960 drop:
961 	kfree_skb_reason(skb, SKB_DROP_REASON_IP_INHDR);
962 	return false;
963 }
964 
965 /* Jumbo payload */
966 
967 static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff)
968 {
969 	const unsigned char *nh = skb_network_header(skb);
970 	SKB_DR(reason);
971 	u32 pkt_len;
972 
973 	if (nh[optoff + 1] != 4 || (optoff & 3) != 2) {
974 		net_dbg_ratelimited("ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n",
975 				    nh[optoff+1]);
976 		SKB_DR_SET(reason, IP_INHDR);
977 		goto drop;
978 	}
979 
980 	pkt_len = ntohl(*(__be32 *)(nh + optoff + 2));
981 	if (pkt_len <= IPV6_MAXPLEN) {
982 		icmpv6_param_prob_reason(skb, ICMPV6_HDR_FIELD, optoff + 2,
983 					 SKB_DROP_REASON_IP_INHDR);
984 		return false;
985 	}
986 	if (ipv6_hdr(skb)->payload_len) {
987 		icmpv6_param_prob_reason(skb, ICMPV6_HDR_FIELD, optoff,
988 					 SKB_DROP_REASON_IP_INHDR);
989 		return false;
990 	}
991 
992 	if (pkt_len > skb->len - sizeof(struct ipv6hdr)) {
993 		SKB_DR_SET(reason, PKT_TOO_SMALL);
994 		goto drop;
995 	}
996 
997 	if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
998 		goto drop;
999 
1000 	IP6CB(skb)->flags |= IP6SKB_JUMBOGRAM;
1001 	return true;
1002 
1003 drop:
1004 	kfree_skb_reason(skb, reason);
1005 	return false;
1006 }
1007 
1008 /* CALIPSO RFC 5570 */
1009 
1010 static bool ipv6_hop_calipso(struct sk_buff *skb, int optoff)
1011 {
1012 	const unsigned char *nh = skb_network_header(skb);
1013 
1014 	if (nh[optoff + 1] < 8)
1015 		goto drop;
1016 
1017 	if (nh[optoff + 6] * 4 + 8 > nh[optoff + 1])
1018 		goto drop;
1019 
1020 	if (!calipso_validate(skb, nh + optoff))
1021 		goto drop;
1022 
1023 	return true;
1024 
1025 drop:
1026 	kfree_skb_reason(skb, SKB_DROP_REASON_IP_INHDR);
1027 	return false;
1028 }
1029 
1030 int ipv6_parse_hopopts(struct sk_buff *skb)
1031 {
1032 	struct inet6_skb_parm *opt = IP6CB(skb);
1033 	struct net *net = dev_net(skb->dev);
1034 	int extlen;
1035 
1036 	/*
1037 	 * skb_network_header(skb) is equal to skb->data, and
1038 	 * skb_network_header_len(skb) is always equal to
1039 	 * sizeof(struct ipv6hdr) by definition of
1040 	 * hop-by-hop options.
1041 	 */
1042 	if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) ||
1043 	    !pskb_may_pull(skb, (sizeof(struct ipv6hdr) +
1044 				 ((skb_transport_header(skb)[1] + 1) << 3)))) {
1045 fail_and_free:
1046 		kfree_skb(skb);
1047 		return -1;
1048 	}
1049 
1050 	extlen = (skb_transport_header(skb)[1] + 1) << 3;
1051 	if (extlen > net->ipv6.sysctl.max_hbh_opts_len)
1052 		goto fail_and_free;
1053 
1054 	opt->flags |= IP6SKB_HOPBYHOP;
1055 	if (ip6_parse_tlv(true, skb, net->ipv6.sysctl.max_hbh_opts_cnt)) {
1056 		skb->transport_header += extlen;
1057 		opt = IP6CB(skb);
1058 		opt->nhoff = sizeof(struct ipv6hdr);
1059 		return 1;
1060 	}
1061 	return -1;
1062 }
1063 
1064 /*
1065  *	Creating outbound headers.
1066  *
1067  *	"build" functions work when skb is filled from head to tail (datagram)
1068  *	"push"	functions work when headers are added from tail to head (tcp)
1069  *
1070  *	In both cases we assume, that caller reserved enough room
1071  *	for headers.
1072  */
1073 
1074 static void ipv6_push_rthdr0(struct sk_buff *skb, u8 *proto,
1075 			     struct ipv6_rt_hdr *opt,
1076 			     struct in6_addr **addr_p, struct in6_addr *saddr)
1077 {
1078 	struct rt0_hdr *phdr, *ihdr;
1079 	int hops;
1080 
1081 	ihdr = (struct rt0_hdr *) opt;
1082 
1083 	phdr = skb_push(skb, (ihdr->rt_hdr.hdrlen + 1) << 3);
1084 	memcpy(phdr, ihdr, sizeof(struct rt0_hdr));
1085 
1086 	hops = ihdr->rt_hdr.hdrlen >> 1;
1087 
1088 	if (hops > 1)
1089 		memcpy(phdr->addr, ihdr->addr + 1,
1090 		       (hops - 1) * sizeof(struct in6_addr));
1091 
1092 	phdr->addr[hops - 1] = **addr_p;
1093 	*addr_p = ihdr->addr;
1094 
1095 	phdr->rt_hdr.nexthdr = *proto;
1096 	*proto = NEXTHDR_ROUTING;
1097 }
1098 
1099 static void ipv6_push_rthdr4(struct sk_buff *skb, u8 *proto,
1100 			     struct ipv6_rt_hdr *opt,
1101 			     struct in6_addr **addr_p, struct in6_addr *saddr)
1102 {
1103 	struct ipv6_sr_hdr *sr_phdr, *sr_ihdr;
1104 	int plen, hops;
1105 
1106 	sr_ihdr = (struct ipv6_sr_hdr *)opt;
1107 	plen = (sr_ihdr->hdrlen + 1) << 3;
1108 
1109 	sr_phdr = skb_push(skb, plen);
1110 	memcpy(sr_phdr, sr_ihdr, sizeof(struct ipv6_sr_hdr));
1111 
1112 	hops = sr_ihdr->first_segment + 1;
1113 	memcpy(sr_phdr->segments + 1, sr_ihdr->segments + 1,
1114 	       (hops - 1) * sizeof(struct in6_addr));
1115 
1116 	sr_phdr->segments[0] = **addr_p;
1117 	*addr_p = &sr_ihdr->segments[sr_ihdr->segments_left];
1118 
1119 	if (sr_ihdr->hdrlen > hops * 2) {
1120 		int tlvs_offset, tlvs_length;
1121 
1122 		tlvs_offset = (1 + hops * 2) << 3;
1123 		tlvs_length = (sr_ihdr->hdrlen - hops * 2) << 3;
1124 		memcpy((char *)sr_phdr + tlvs_offset,
1125 		       (char *)sr_ihdr + tlvs_offset, tlvs_length);
1126 	}
1127 
1128 #ifdef CONFIG_IPV6_SEG6_HMAC
1129 	if (sr_has_hmac(sr_phdr)) {
1130 		struct net *net = NULL;
1131 
1132 		if (skb->dev)
1133 			net = dev_net(skb->dev);
1134 		else if (skb->sk)
1135 			net = sock_net(skb->sk);
1136 
1137 		WARN_ON(!net);
1138 
1139 		if (net)
1140 			seg6_push_hmac(net, saddr, sr_phdr);
1141 	}
1142 #endif
1143 
1144 	sr_phdr->nexthdr = *proto;
1145 	*proto = NEXTHDR_ROUTING;
1146 }
1147 
1148 static void ipv6_push_rthdr(struct sk_buff *skb, u8 *proto,
1149 			    struct ipv6_rt_hdr *opt,
1150 			    struct in6_addr **addr_p, struct in6_addr *saddr)
1151 {
1152 	switch (opt->type) {
1153 	case IPV6_SRCRT_TYPE_0:
1154 	case IPV6_SRCRT_STRICT:
1155 	case IPV6_SRCRT_TYPE_2:
1156 		ipv6_push_rthdr0(skb, proto, opt, addr_p, saddr);
1157 		break;
1158 	case IPV6_SRCRT_TYPE_4:
1159 		ipv6_push_rthdr4(skb, proto, opt, addr_p, saddr);
1160 		break;
1161 	default:
1162 		break;
1163 	}
1164 }
1165 
1166 static void ipv6_push_exthdr(struct sk_buff *skb, u8 *proto, u8 type, struct ipv6_opt_hdr *opt)
1167 {
1168 	struct ipv6_opt_hdr *h = skb_push(skb, ipv6_optlen(opt));
1169 
1170 	memcpy(h, opt, ipv6_optlen(opt));
1171 	h->nexthdr = *proto;
1172 	*proto = type;
1173 }
1174 
1175 void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
1176 			  u8 *proto,
1177 			  struct in6_addr **daddr, struct in6_addr *saddr)
1178 {
1179 	if (opt->srcrt) {
1180 		ipv6_push_rthdr(skb, proto, opt->srcrt, daddr, saddr);
1181 		/*
1182 		 * IPV6_RTHDRDSTOPTS is ignored
1183 		 * unless IPV6_RTHDR is set (RFC3542).
1184 		 */
1185 		if (opt->dst0opt)
1186 			ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst0opt);
1187 	}
1188 	if (opt->hopopt)
1189 		ipv6_push_exthdr(skb, proto, NEXTHDR_HOP, opt->hopopt);
1190 }
1191 
1192 void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, u8 *proto)
1193 {
1194 	if (opt->dst1opt)
1195 		ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst1opt);
1196 }
1197 EXPORT_SYMBOL(ipv6_push_frag_opts);
1198 
1199 struct ipv6_txoptions *
1200 ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt)
1201 {
1202 	struct ipv6_txoptions *opt2;
1203 
1204 	opt2 = sock_kmalloc(sk, opt->tot_len, GFP_ATOMIC);
1205 	if (opt2) {
1206 		long dif = (char *)opt2 - (char *)opt;
1207 		memcpy(opt2, opt, opt->tot_len);
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 
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 *
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 
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  */
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