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