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