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