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