• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ip6_tlvopt_unknown(struct sk_buff * skb,int optoff,bool disallow_unknowns)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(skb, ICMPV6_UNK_OPTION, optoff);
94 		return false;
95 	}
96 
97 drop:
98 	kfree_skb(skb);
99 	return false;
100 }
101 
102 static bool ipv6_hop_ra(struct sk_buff *skb, int optoff);
103 static bool ipv6_hop_ioam(struct sk_buff *skb, int optoff);
104 static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff);
105 static bool ipv6_hop_calipso(struct sk_buff *skb, int optoff);
106 #if IS_ENABLED(CONFIG_IPV6_MIP6)
107 static bool ipv6_dest_hao(struct sk_buff *skb, int optoff);
108 #endif
109 
110 /* Parse tlv encoded option header (hop-by-hop or destination) */
111 
ip6_parse_tlv(bool hopbyhop,struct sk_buff * skb,int max_count)112 static bool ip6_parse_tlv(bool hopbyhop,
113 			  struct sk_buff *skb,
114 			  int max_count)
115 {
116 	int len = (skb_transport_header(skb)[1] + 1) << 3;
117 	const unsigned char *nh = skb_network_header(skb);
118 	int off = skb_network_header_len(skb);
119 	bool disallow_unknowns = false;
120 	int tlv_count = 0;
121 	int padlen = 0;
122 
123 	if (unlikely(max_count < 0)) {
124 		disallow_unknowns = true;
125 		max_count = -max_count;
126 	}
127 
128 	if (skb_transport_offset(skb) + len > skb_headlen(skb))
129 		goto bad;
130 
131 	off += 2;
132 	len -= 2;
133 
134 	while (len > 0) {
135 		int optlen, i;
136 
137 		if (nh[off] == IPV6_TLV_PAD1) {
138 			padlen++;
139 			if (padlen > 7)
140 				goto bad;
141 			off++;
142 			len--;
143 			continue;
144 		}
145 		if (len < 2)
146 			goto bad;
147 		optlen = nh[off + 1] + 2;
148 		if (optlen > len)
149 			goto bad;
150 
151 		if (nh[off] == IPV6_TLV_PADN) {
152 			/* RFC 2460 states that the purpose of PadN is
153 			 * to align the containing header to multiples
154 			 * of 8. 7 is therefore the highest valid value.
155 			 * See also RFC 4942, Section 2.1.9.5.
156 			 */
157 			padlen += optlen;
158 			if (padlen > 7)
159 				goto bad;
160 			/* RFC 4942 recommends receiving hosts to
161 			 * actively check PadN payload to contain
162 			 * only zeroes.
163 			 */
164 			for (i = 2; i < optlen; i++) {
165 				if (nh[off + i] != 0)
166 					goto bad;
167 			}
168 		} else {
169 			tlv_count++;
170 			if (tlv_count > max_count)
171 				goto bad;
172 
173 			if (hopbyhop) {
174 				switch (nh[off]) {
175 				case IPV6_TLV_ROUTERALERT:
176 					if (!ipv6_hop_ra(skb, off))
177 						return false;
178 					break;
179 				case IPV6_TLV_IOAM:
180 					if (!ipv6_hop_ioam(skb, off))
181 						return false;
182 					break;
183 				case IPV6_TLV_JUMBO:
184 					if (!ipv6_hop_jumbo(skb, off))
185 						return false;
186 					break;
187 				case IPV6_TLV_CALIPSO:
188 					if (!ipv6_hop_calipso(skb, off))
189 						return false;
190 					break;
191 				default:
192 					if (!ip6_tlvopt_unknown(skb, off,
193 								disallow_unknowns))
194 						return false;
195 					break;
196 				}
197 			} else {
198 				switch (nh[off]) {
199 #if IS_ENABLED(CONFIG_IPV6_MIP6)
200 				case IPV6_TLV_HAO:
201 					if (!ipv6_dest_hao(skb, off))
202 						return false;
203 					break;
204 #endif
205 				default:
206 					if (!ip6_tlvopt_unknown(skb, off,
207 								disallow_unknowns))
208 						return false;
209 					break;
210 				}
211 			}
212 			padlen = 0;
213 		}
214 		off += optlen;
215 		len -= optlen;
216 	}
217 
218 	if (len == 0)
219 		return true;
220 bad:
221 	kfree_skb(skb);
222 	return false;
223 }
224 
225 /*****************************
226   Destination options header.
227  *****************************/
228 
229 #if IS_ENABLED(CONFIG_IPV6_MIP6)
ipv6_dest_hao(struct sk_buff * skb,int optoff)230 static bool ipv6_dest_hao(struct sk_buff *skb, int optoff)
231 {
232 	struct ipv6_destopt_hao *hao;
233 	struct inet6_skb_parm *opt = IP6CB(skb);
234 	struct ipv6hdr *ipv6h = ipv6_hdr(skb);
235 	int ret;
236 
237 	if (opt->dsthao) {
238 		net_dbg_ratelimited("hao duplicated\n");
239 		goto discard;
240 	}
241 	opt->dsthao = opt->dst1;
242 	opt->dst1 = 0;
243 
244 	hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + optoff);
245 
246 	if (hao->length != 16) {
247 		net_dbg_ratelimited("hao invalid option length = %d\n",
248 				    hao->length);
249 		goto discard;
250 	}
251 
252 	if (!(ipv6_addr_type(&hao->addr) & IPV6_ADDR_UNICAST)) {
253 		net_dbg_ratelimited("hao is not an unicast addr: %pI6\n",
254 				    &hao->addr);
255 		goto discard;
256 	}
257 
258 	ret = xfrm6_input_addr(skb, (xfrm_address_t *)&ipv6h->daddr,
259 			       (xfrm_address_t *)&hao->addr, IPPROTO_DSTOPTS);
260 	if (unlikely(ret < 0))
261 		goto discard;
262 
263 	if (skb_cloned(skb)) {
264 		if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
265 			goto discard;
266 
267 		/* update all variable using below by copied skbuff */
268 		hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) +
269 						  optoff);
270 		ipv6h = ipv6_hdr(skb);
271 	}
272 
273 	if (skb->ip_summed == CHECKSUM_COMPLETE)
274 		skb->ip_summed = CHECKSUM_NONE;
275 
276 	swap(ipv6h->saddr, hao->addr);
277 
278 	if (skb->tstamp == 0)
279 		__net_timestamp(skb);
280 
281 	return true;
282 
283  discard:
284 	kfree_skb(skb);
285 	return false;
286 }
287 #endif
288 
ipv6_destopt_rcv(struct sk_buff * skb)289 static int ipv6_destopt_rcv(struct sk_buff *skb)
290 {
291 	struct inet6_dev *idev = __in6_dev_get(skb->dev);
292 	struct inet6_skb_parm *opt = IP6CB(skb);
293 #if IS_ENABLED(CONFIG_IPV6_MIP6)
294 	__u16 dstbuf;
295 #endif
296 	struct dst_entry *dst = skb_dst(skb);
297 	struct net *net = dev_net(skb->dev);
298 	int extlen;
299 
300 	if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
301 	    !pskb_may_pull(skb, (skb_transport_offset(skb) +
302 				 ((skb_transport_header(skb)[1] + 1) << 3)))) {
303 		__IP6_INC_STATS(dev_net(dst->dev), idev,
304 				IPSTATS_MIB_INHDRERRORS);
305 fail_and_free:
306 		kfree_skb(skb);
307 		return -1;
308 	}
309 
310 	extlen = (skb_transport_header(skb)[1] + 1) << 3;
311 	if (extlen > net->ipv6.sysctl.max_dst_opts_len)
312 		goto fail_and_free;
313 
314 	opt->lastopt = opt->dst1 = skb_network_header_len(skb);
315 #if IS_ENABLED(CONFIG_IPV6_MIP6)
316 	dstbuf = opt->dst1;
317 #endif
318 
319 	if (ip6_parse_tlv(false, skb, net->ipv6.sysctl.max_dst_opts_cnt)) {
320 		skb->transport_header += extlen;
321 		opt = IP6CB(skb);
322 #if IS_ENABLED(CONFIG_IPV6_MIP6)
323 		opt->nhoff = dstbuf;
324 #else
325 		opt->nhoff = opt->dst1;
326 #endif
327 		return 1;
328 	}
329 
330 	__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
331 	return -1;
332 }
333 
seg6_update_csum(struct sk_buff * skb)334 static void seg6_update_csum(struct sk_buff *skb)
335 {
336 	struct ipv6_sr_hdr *hdr;
337 	struct in6_addr *addr;
338 	__be32 from, to;
339 
340 	/* srh is at transport offset and seg_left is already decremented
341 	 * but daddr is not yet updated with next segment
342 	 */
343 
344 	hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb);
345 	addr = hdr->segments + hdr->segments_left;
346 
347 	hdr->segments_left++;
348 	from = *(__be32 *)hdr;
349 
350 	hdr->segments_left--;
351 	to = *(__be32 *)hdr;
352 
353 	/* update skb csum with diff resulting from seg_left decrement */
354 
355 	update_csum_diff4(skb, from, to);
356 
357 	/* compute csum diff between current and next segment and update */
358 
359 	update_csum_diff16(skb, (__be32 *)(&ipv6_hdr(skb)->daddr),
360 			   (__be32 *)addr);
361 }
362 
ipv6_srh_rcv(struct sk_buff * skb)363 static int ipv6_srh_rcv(struct sk_buff *skb)
364 {
365 	struct inet6_skb_parm *opt = IP6CB(skb);
366 	struct net *net = dev_net(skb->dev);
367 	struct ipv6_sr_hdr *hdr;
368 	struct inet6_dev *idev;
369 	struct in6_addr *addr;
370 	int accept_seg6;
371 
372 	hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb);
373 
374 	idev = __in6_dev_get(skb->dev);
375 
376 	accept_seg6 = net->ipv6.devconf_all->seg6_enabled;
377 	if (accept_seg6 > idev->cnf.seg6_enabled)
378 		accept_seg6 = idev->cnf.seg6_enabled;
379 
380 	if (!accept_seg6) {
381 		kfree_skb(skb);
382 		return -1;
383 	}
384 
385 #ifdef CONFIG_IPV6_SEG6_HMAC
386 	if (!seg6_hmac_validate_skb(skb)) {
387 		kfree_skb(skb);
388 		return -1;
389 	}
390 #endif
391 
392 looped_back:
393 	if (hdr->segments_left == 0) {
394 		if (hdr->nexthdr == NEXTHDR_IPV6 || hdr->nexthdr == NEXTHDR_IPV4) {
395 			int offset = (hdr->hdrlen + 1) << 3;
396 
397 			skb_postpull_rcsum(skb, skb_network_header(skb),
398 					   skb_network_header_len(skb));
399 
400 			if (!pskb_pull(skb, offset)) {
401 				kfree_skb(skb);
402 				return -1;
403 			}
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 
443 	hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb);
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 	skb_dst_drop(skb);
456 
457 	ip6_route_input(skb);
458 
459 	if (skb_dst(skb)->error) {
460 		dst_input(skb);
461 		return -1;
462 	}
463 
464 	if (skb_dst(skb)->dev->flags & IFF_LOOPBACK) {
465 		if (ipv6_hdr(skb)->hop_limit <= 1) {
466 			__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
467 			icmpv6_send(skb, ICMPV6_TIME_EXCEED,
468 				    ICMPV6_EXC_HOPLIMIT, 0);
469 			kfree_skb(skb);
470 			return -1;
471 		}
472 		ipv6_hdr(skb)->hop_limit--;
473 
474 		skb_pull(skb, sizeof(struct ipv6hdr));
475 		goto looped_back;
476 	}
477 
478 	dst_input(skb);
479 
480 	return -1;
481 }
482 
ipv6_rpl_srh_rcv(struct sk_buff * skb)483 static int ipv6_rpl_srh_rcv(struct sk_buff *skb)
484 {
485 	struct ipv6_rpl_sr_hdr *hdr, *ohdr, *chdr;
486 	struct inet6_skb_parm *opt = IP6CB(skb);
487 	struct net *net = dev_net(skb->dev);
488 	struct inet6_dev *idev;
489 	struct ipv6hdr *oldhdr;
490 	struct in6_addr addr;
491 	unsigned char *buf;
492 	int accept_rpl_seg;
493 	int i, err;
494 	u64 n = 0;
495 	u32 r;
496 
497 	idev = __in6_dev_get(skb->dev);
498 
499 	accept_rpl_seg = net->ipv6.devconf_all->rpl_seg_enabled;
500 	if (accept_rpl_seg > idev->cnf.rpl_seg_enabled)
501 		accept_rpl_seg = idev->cnf.rpl_seg_enabled;
502 
503 	if (!accept_rpl_seg) {
504 		kfree_skb(skb);
505 		return -1;
506 	}
507 
508 looped_back:
509 	hdr = (struct ipv6_rpl_sr_hdr *)skb_transport_header(skb);
510 
511 	if (hdr->segments_left == 0) {
512 		if (hdr->nexthdr == NEXTHDR_IPV6) {
513 			int offset = (hdr->hdrlen + 1) << 3;
514 
515 			skb_postpull_rcsum(skb, skb_network_header(skb),
516 					   skb_network_header_len(skb));
517 
518 			if (!pskb_pull(skb, offset)) {
519 				kfree_skb(skb);
520 				return -1;
521 			}
522 			skb_postpull_rcsum(skb, skb_transport_header(skb),
523 					   offset);
524 
525 			skb_reset_network_header(skb);
526 			skb_reset_transport_header(skb);
527 			skb->encapsulation = 0;
528 
529 			__skb_tunnel_rx(skb, skb->dev, net);
530 
531 			netif_rx(skb);
532 			return -1;
533 		}
534 
535 		opt->srcrt = skb_network_header_len(skb);
536 		opt->lastopt = opt->srcrt;
537 		skb->transport_header += (hdr->hdrlen + 1) << 3;
538 		opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
539 
540 		return 1;
541 	}
542 
543 	if (!pskb_may_pull(skb, sizeof(*hdr))) {
544 		kfree_skb(skb);
545 		return -1;
546 	}
547 
548 	n = (hdr->hdrlen << 3) - hdr->pad - (16 - hdr->cmpre);
549 	r = do_div(n, (16 - hdr->cmpri));
550 	/* checks if calculation was without remainder and n fits into
551 	 * unsigned char which is segments_left field. Should not be
552 	 * higher than that.
553 	 */
554 	if (r || (n + 1) > 255) {
555 		kfree_skb(skb);
556 		return -1;
557 	}
558 
559 	if (hdr->segments_left > n + 1) {
560 		__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
561 		icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
562 				  ((&hdr->segments_left) -
563 				   skb_network_header(skb)));
564 		return -1;
565 	}
566 
567 	if (!pskb_may_pull(skb, ipv6_rpl_srh_size(n, hdr->cmpri,
568 						  hdr->cmpre))) {
569 		kfree_skb(skb);
570 		return -1;
571 	}
572 
573 	hdr->segments_left--;
574 	i = n - hdr->segments_left;
575 
576 	buf = kcalloc(struct_size(hdr, segments.addr, n + 2), 2, GFP_ATOMIC);
577 	if (unlikely(!buf)) {
578 		kfree_skb(skb);
579 		return -1;
580 	}
581 
582 	ohdr = (struct ipv6_rpl_sr_hdr *)buf;
583 	ipv6_rpl_srh_decompress(ohdr, hdr, &ipv6_hdr(skb)->daddr, n);
584 	chdr = (struct ipv6_rpl_sr_hdr *)(buf + ((ohdr->hdrlen + 1) << 3));
585 
586 	if ((ipv6_addr_type(&ipv6_hdr(skb)->daddr) & IPV6_ADDR_MULTICAST) ||
587 	    (ipv6_addr_type(&ohdr->rpl_segaddr[i]) & IPV6_ADDR_MULTICAST)) {
588 		kfree_skb(skb);
589 		kfree(buf);
590 		return -1;
591 	}
592 
593 	err = ipv6_chk_rpl_srh_loop(net, ohdr->rpl_segaddr, n + 1);
594 	if (err) {
595 		icmpv6_send(skb, ICMPV6_PARAMPROB, 0, 0);
596 		kfree_skb(skb);
597 		kfree(buf);
598 		return -1;
599 	}
600 
601 	addr = ipv6_hdr(skb)->daddr;
602 	ipv6_hdr(skb)->daddr = ohdr->rpl_segaddr[i];
603 	ohdr->rpl_segaddr[i] = addr;
604 
605 	ipv6_rpl_srh_compress(chdr, ohdr, &ipv6_hdr(skb)->daddr, n);
606 
607 	oldhdr = ipv6_hdr(skb);
608 
609 	skb_pull(skb, ((hdr->hdrlen + 1) << 3));
610 	skb_postpull_rcsum(skb, oldhdr,
611 			   sizeof(struct ipv6hdr) + ((hdr->hdrlen + 1) << 3));
612 	if (unlikely(!hdr->segments_left)) {
613 		if (pskb_expand_head(skb, sizeof(struct ipv6hdr) + ((chdr->hdrlen + 1) << 3), 0,
614 				     GFP_ATOMIC)) {
615 			__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_OUTDISCARDS);
616 			kfree_skb(skb);
617 			kfree(buf);
618 			return -1;
619 		}
620 
621 		oldhdr = ipv6_hdr(skb);
622 	}
623 	skb_push(skb, ((chdr->hdrlen + 1) << 3) + sizeof(struct ipv6hdr));
624 	skb_reset_network_header(skb);
625 	skb_mac_header_rebuild(skb);
626 	skb_set_transport_header(skb, sizeof(struct ipv6hdr));
627 
628 	memmove(ipv6_hdr(skb), oldhdr, sizeof(struct ipv6hdr));
629 	memcpy(skb_transport_header(skb), chdr, (chdr->hdrlen + 1) << 3);
630 
631 	ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
632 	skb_postpush_rcsum(skb, ipv6_hdr(skb),
633 			   sizeof(struct ipv6hdr) + ((chdr->hdrlen + 1) << 3));
634 
635 	kfree(buf);
636 
637 	skb_dst_drop(skb);
638 
639 	ip6_route_input(skb);
640 
641 	if (skb_dst(skb)->error) {
642 		dst_input(skb);
643 		return -1;
644 	}
645 
646 	if (skb_dst(skb)->dev->flags & IFF_LOOPBACK) {
647 		if (ipv6_hdr(skb)->hop_limit <= 1) {
648 			__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
649 			icmpv6_send(skb, ICMPV6_TIME_EXCEED,
650 				    ICMPV6_EXC_HOPLIMIT, 0);
651 			kfree_skb(skb);
652 			return -1;
653 		}
654 		ipv6_hdr(skb)->hop_limit--;
655 
656 		skb_pull(skb, sizeof(struct ipv6hdr));
657 		goto looped_back;
658 	}
659 
660 	dst_input(skb);
661 
662 	return -1;
663 }
664 
665 /********************************
666   Routing header.
667  ********************************/
668 
669 /* called with rcu_read_lock() */
ipv6_rthdr_rcv(struct sk_buff * skb)670 static int ipv6_rthdr_rcv(struct sk_buff *skb)
671 {
672 	struct inet6_dev *idev = __in6_dev_get(skb->dev);
673 	struct inet6_skb_parm *opt = IP6CB(skb);
674 	struct in6_addr *addr = NULL;
675 	struct in6_addr daddr;
676 	int n, i;
677 	struct ipv6_rt_hdr *hdr;
678 	struct rt0_hdr *rthdr;
679 	struct net *net = dev_net(skb->dev);
680 	int accept_source_route = net->ipv6.devconf_all->accept_source_route;
681 
682 	idev = __in6_dev_get(skb->dev);
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 	skb_dst_drop(skb);
829 	ip6_route_input(skb);
830 	if (skb_dst(skb)->error) {
831 		skb_push(skb, skb->data - skb_network_header(skb));
832 		dst_input(skb);
833 		return -1;
834 	}
835 
836 	if (skb_dst(skb)->dev->flags&IFF_LOOPBACK) {
837 		if (ipv6_hdr(skb)->hop_limit <= 1) {
838 			__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
839 			icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
840 				    0);
841 			kfree_skb(skb);
842 			return -1;
843 		}
844 		ipv6_hdr(skb)->hop_limit--;
845 		goto looped_back;
846 	}
847 
848 	skb_push(skb, skb->data - skb_network_header(skb));
849 	dst_input(skb);
850 	return -1;
851 
852 unknown_rh:
853 	__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
854 	icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
855 			  (&hdr->type) - skb_network_header(skb));
856 	return -1;
857 }
858 
859 static const struct inet6_protocol rthdr_protocol = {
860 	.handler	=	ipv6_rthdr_rcv,
861 	.flags		=	INET6_PROTO_NOPOLICY,
862 };
863 
864 static const struct inet6_protocol destopt_protocol = {
865 	.handler	=	ipv6_destopt_rcv,
866 	.flags		=	INET6_PROTO_NOPOLICY,
867 };
868 
869 static const struct inet6_protocol nodata_protocol = {
870 	.handler	=	dst_discard,
871 	.flags		=	INET6_PROTO_NOPOLICY,
872 };
873 
ipv6_exthdrs_init(void)874 int __init ipv6_exthdrs_init(void)
875 {
876 	int ret;
877 
878 	ret = inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING);
879 	if (ret)
880 		goto out;
881 
882 	ret = inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
883 	if (ret)
884 		goto out_rthdr;
885 
886 	ret = inet6_add_protocol(&nodata_protocol, IPPROTO_NONE);
887 	if (ret)
888 		goto out_destopt;
889 
890 out:
891 	return ret;
892 out_destopt:
893 	inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
894 out_rthdr:
895 	inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
896 	goto out;
897 };
898 
ipv6_exthdrs_exit(void)899 void ipv6_exthdrs_exit(void)
900 {
901 	inet6_del_protocol(&nodata_protocol, IPPROTO_NONE);
902 	inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
903 	inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
904 }
905 
906 /**********************************
907   Hop-by-hop options.
908  **********************************/
909 
910 /*
911  * Note: we cannot rely on skb_dst(skb) before we assign it in ip6_route_input().
912  */
ipv6_skb_net(struct sk_buff * skb)913 static inline struct net *ipv6_skb_net(struct sk_buff *skb)
914 {
915 	return skb_dst(skb) ? dev_net(skb_dst(skb)->dev) : dev_net(skb->dev);
916 }
917 
918 /* Router Alert as of RFC 2711 */
919 
ipv6_hop_ra(struct sk_buff * skb,int optoff)920 static bool ipv6_hop_ra(struct sk_buff *skb, int optoff)
921 {
922 	const unsigned char *nh = skb_network_header(skb);
923 
924 	if (nh[optoff + 1] == 2) {
925 		IP6CB(skb)->flags |= IP6SKB_ROUTERALERT;
926 		memcpy(&IP6CB(skb)->ra, nh + optoff + 2, sizeof(IP6CB(skb)->ra));
927 		return true;
928 	}
929 	net_dbg_ratelimited("ipv6_hop_ra: wrong RA length %d\n",
930 			    nh[optoff + 1]);
931 	kfree_skb(skb);
932 	return false;
933 }
934 
935 /* IOAM */
936 
ipv6_hop_ioam(struct sk_buff * skb,int optoff)937 static bool ipv6_hop_ioam(struct sk_buff *skb, int optoff)
938 {
939 	struct ioam6_trace_hdr *trace;
940 	struct ioam6_namespace *ns;
941 	struct ioam6_hdr *hdr;
942 
943 	/* Bad alignment (must be 4n-aligned) */
944 	if (optoff & 3)
945 		goto drop;
946 
947 	/* Ignore if IOAM is not enabled on ingress */
948 	if (!__in6_dev_get(skb->dev)->cnf.ioam6_enabled)
949 		goto ignore;
950 
951 	/* Truncated Option header */
952 	hdr = (struct ioam6_hdr *)(skb_network_header(skb) + optoff);
953 	if (hdr->opt_len < 2)
954 		goto drop;
955 
956 	switch (hdr->type) {
957 	case IOAM6_TYPE_PREALLOC:
958 		/* Truncated Pre-allocated Trace header */
959 		if (hdr->opt_len < 2 + sizeof(*trace))
960 			goto drop;
961 
962 		/* Malformed Pre-allocated Trace header */
963 		trace = (struct ioam6_trace_hdr *)((u8 *)hdr + sizeof(*hdr));
964 		if (hdr->opt_len < 2 + sizeof(*trace) + trace->remlen * 4)
965 			goto drop;
966 
967 		/* Ignore if the IOAM namespace is unknown */
968 		ns = ioam6_namespace(ipv6_skb_net(skb), trace->namespace_id);
969 		if (!ns)
970 			goto ignore;
971 
972 		if (!skb_valid_dst(skb))
973 			ip6_route_input(skb);
974 
975 		ioam6_fill_trace_data(skb, ns, trace);
976 		break;
977 	default:
978 		break;
979 	}
980 
981 ignore:
982 	return true;
983 
984 drop:
985 	kfree_skb(skb);
986 	return false;
987 }
988 
989 /* Jumbo payload */
990 
ipv6_hop_jumbo(struct sk_buff * skb,int optoff)991 static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff)
992 {
993 	const unsigned char *nh = skb_network_header(skb);
994 	struct inet6_dev *idev = __in6_dev_get_safely(skb->dev);
995 	struct net *net = ipv6_skb_net(skb);
996 	u32 pkt_len;
997 
998 	if (nh[optoff + 1] != 4 || (optoff & 3) != 2) {
999 		net_dbg_ratelimited("ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n",
1000 				    nh[optoff+1]);
1001 		__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
1002 		goto drop;
1003 	}
1004 
1005 	pkt_len = ntohl(*(__be32 *)(nh + optoff + 2));
1006 	if (pkt_len <= IPV6_MAXPLEN) {
1007 		__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
1008 		icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff+2);
1009 		return false;
1010 	}
1011 	if (ipv6_hdr(skb)->payload_len) {
1012 		__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
1013 		icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff);
1014 		return false;
1015 	}
1016 
1017 	if (pkt_len > skb->len - sizeof(struct ipv6hdr)) {
1018 		__IP6_INC_STATS(net, idev, IPSTATS_MIB_INTRUNCATEDPKTS);
1019 		goto drop;
1020 	}
1021 
1022 	if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
1023 		goto drop;
1024 
1025 	IP6CB(skb)->flags |= IP6SKB_JUMBOGRAM;
1026 	return true;
1027 
1028 drop:
1029 	kfree_skb(skb);
1030 	return false;
1031 }
1032 
1033 /* CALIPSO RFC 5570 */
1034 
ipv6_hop_calipso(struct sk_buff * skb,int optoff)1035 static bool ipv6_hop_calipso(struct sk_buff *skb, int optoff)
1036 {
1037 	const unsigned char *nh = skb_network_header(skb);
1038 
1039 	if (nh[optoff + 1] < 8)
1040 		goto drop;
1041 
1042 	if (nh[optoff + 6] * 4 + 8 > nh[optoff + 1])
1043 		goto drop;
1044 
1045 	if (!calipso_validate(skb, nh + optoff))
1046 		goto drop;
1047 
1048 	return true;
1049 
1050 drop:
1051 	kfree_skb(skb);
1052 	return false;
1053 }
1054 
ipv6_parse_hopopts(struct sk_buff * skb)1055 int ipv6_parse_hopopts(struct sk_buff *skb)
1056 {
1057 	struct inet6_skb_parm *opt = IP6CB(skb);
1058 	struct net *net = dev_net(skb->dev);
1059 	int extlen;
1060 
1061 	/*
1062 	 * skb_network_header(skb) is equal to skb->data, and
1063 	 * skb_network_header_len(skb) is always equal to
1064 	 * sizeof(struct ipv6hdr) by definition of
1065 	 * hop-by-hop options.
1066 	 */
1067 	if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) ||
1068 	    !pskb_may_pull(skb, (sizeof(struct ipv6hdr) +
1069 				 ((skb_transport_header(skb)[1] + 1) << 3)))) {
1070 fail_and_free:
1071 		kfree_skb(skb);
1072 		return -1;
1073 	}
1074 
1075 	extlen = (skb_transport_header(skb)[1] + 1) << 3;
1076 	if (extlen > net->ipv6.sysctl.max_hbh_opts_len)
1077 		goto fail_and_free;
1078 
1079 	opt->flags |= IP6SKB_HOPBYHOP;
1080 	if (ip6_parse_tlv(true, skb, 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 
ipv6_push_rthdr0(struct sk_buff * skb,u8 * proto,struct ipv6_rt_hdr * opt,struct in6_addr ** addr_p,struct in6_addr * saddr)1099 static void 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 	*proto = NEXTHDR_ROUTING;
1122 }
1123 
ipv6_push_rthdr4(struct sk_buff * skb,u8 * proto,struct ipv6_rt_hdr * opt,struct in6_addr ** addr_p,struct in6_addr * saddr)1124 static void 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 	*proto = NEXTHDR_ROUTING;
1171 }
1172 
ipv6_push_rthdr(struct sk_buff * skb,u8 * proto,struct ipv6_rt_hdr * opt,struct in6_addr ** addr_p,struct in6_addr * saddr)1173 static void 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 		ipv6_push_rthdr0(skb, proto, opt, addr_p, saddr);
1182 		break;
1183 	case IPV6_SRCRT_TYPE_4:
1184 		ipv6_push_rthdr4(skb, proto, opt, addr_p, saddr);
1185 		break;
1186 	default:
1187 		break;
1188 	}
1189 }
1190 
ipv6_push_exthdr(struct sk_buff * skb,u8 * proto,u8 type,struct ipv6_opt_hdr * opt)1191 static void ipv6_push_exthdr(struct sk_buff *skb, u8 *proto, u8 type, struct ipv6_opt_hdr *opt)
1192 {
1193 	struct ipv6_opt_hdr *h = skb_push(skb, ipv6_optlen(opt));
1194 
1195 	memcpy(h, opt, ipv6_optlen(opt));
1196 	h->nexthdr = *proto;
1197 	*proto = type;
1198 }
1199 
ipv6_push_nfrag_opts(struct sk_buff * skb,struct ipv6_txoptions * opt,u8 * proto,struct in6_addr ** daddr,struct in6_addr * saddr)1200 void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
1201 			  u8 *proto,
1202 			  struct in6_addr **daddr, struct in6_addr *saddr)
1203 {
1204 	if (opt->srcrt) {
1205 		ipv6_push_rthdr(skb, proto, opt->srcrt, daddr, saddr);
1206 		/*
1207 		 * IPV6_RTHDRDSTOPTS is ignored
1208 		 * unless IPV6_RTHDR is set (RFC3542).
1209 		 */
1210 		if (opt->dst0opt)
1211 			ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst0opt);
1212 	}
1213 	if (opt->hopopt)
1214 		ipv6_push_exthdr(skb, proto, NEXTHDR_HOP, opt->hopopt);
1215 }
1216 
ipv6_push_frag_opts(struct sk_buff * skb,struct ipv6_txoptions * opt,u8 * proto)1217 void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, u8 *proto)
1218 {
1219 	if (opt->dst1opt)
1220 		ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst1opt);
1221 }
1222 EXPORT_SYMBOL(ipv6_push_frag_opts);
1223 
1224 struct ipv6_txoptions *
ipv6_dup_options(struct sock * sk,struct ipv6_txoptions * opt)1225 ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt)
1226 {
1227 	struct ipv6_txoptions *opt2;
1228 
1229 	opt2 = sock_kmalloc(sk, opt->tot_len, GFP_ATOMIC);
1230 	if (opt2) {
1231 		long dif = (char *)opt2 - (char *)opt;
1232 		memcpy(opt2, opt, opt->tot_len);
1233 		if (opt2->hopopt)
1234 			*((char **)&opt2->hopopt) += dif;
1235 		if (opt2->dst0opt)
1236 			*((char **)&opt2->dst0opt) += dif;
1237 		if (opt2->dst1opt)
1238 			*((char **)&opt2->dst1opt) += dif;
1239 		if (opt2->srcrt)
1240 			*((char **)&opt2->srcrt) += dif;
1241 		refcount_set(&opt2->refcnt, 1);
1242 	}
1243 	return opt2;
1244 }
1245 EXPORT_SYMBOL_GPL(ipv6_dup_options);
1246 
ipv6_renew_option(int renewtype,struct ipv6_opt_hdr ** dest,struct ipv6_opt_hdr * old,struct ipv6_opt_hdr * new,int newtype,char ** p)1247 static void ipv6_renew_option(int renewtype,
1248 			      struct ipv6_opt_hdr **dest,
1249 			      struct ipv6_opt_hdr *old,
1250 			      struct ipv6_opt_hdr *new,
1251 			      int newtype, char **p)
1252 {
1253 	struct ipv6_opt_hdr *src;
1254 
1255 	src = (renewtype == newtype ? new : old);
1256 	if (!src)
1257 		return;
1258 
1259 	memcpy(*p, src, ipv6_optlen(src));
1260 	*dest = (struct ipv6_opt_hdr *)*p;
1261 	*p += CMSG_ALIGN(ipv6_optlen(*dest));
1262 }
1263 
1264 /**
1265  * ipv6_renew_options - replace a specific ext hdr with a new one.
1266  *
1267  * @sk: sock from which to allocate memory
1268  * @opt: original options
1269  * @newtype: option type to replace in @opt
1270  * @newopt: new option of type @newtype to replace (user-mem)
1271  *
1272  * Returns a new set of options which is a copy of @opt with the
1273  * option type @newtype replaced with @newopt.
1274  *
1275  * @opt may be NULL, in which case a new set of options is returned
1276  * containing just @newopt.
1277  *
1278  * @newopt may be NULL, in which case the specified option type is
1279  * not copied into the new set of options.
1280  *
1281  * The new set of options is allocated from the socket option memory
1282  * buffer of @sk.
1283  */
1284 struct ipv6_txoptions *
ipv6_renew_options(struct sock * sk,struct ipv6_txoptions * opt,int newtype,struct ipv6_opt_hdr * newopt)1285 ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt,
1286 		   int newtype, struct ipv6_opt_hdr *newopt)
1287 {
1288 	int tot_len = 0;
1289 	char *p;
1290 	struct ipv6_txoptions *opt2;
1291 
1292 	if (opt) {
1293 		if (newtype != IPV6_HOPOPTS && opt->hopopt)
1294 			tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt));
1295 		if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt)
1296 			tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt));
1297 		if (newtype != IPV6_RTHDR && opt->srcrt)
1298 			tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt));
1299 		if (newtype != IPV6_DSTOPTS && opt->dst1opt)
1300 			tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt));
1301 	}
1302 
1303 	if (newopt)
1304 		tot_len += CMSG_ALIGN(ipv6_optlen(newopt));
1305 
1306 	if (!tot_len)
1307 		return NULL;
1308 
1309 	tot_len += sizeof(*opt2);
1310 	opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC);
1311 	if (!opt2)
1312 		return ERR_PTR(-ENOBUFS);
1313 
1314 	memset(opt2, 0, tot_len);
1315 	refcount_set(&opt2->refcnt, 1);
1316 	opt2->tot_len = tot_len;
1317 	p = (char *)(opt2 + 1);
1318 
1319 	ipv6_renew_option(IPV6_HOPOPTS, &opt2->hopopt,
1320 			  (opt ? opt->hopopt : NULL),
1321 			  newopt, newtype, &p);
1322 	ipv6_renew_option(IPV6_RTHDRDSTOPTS, &opt2->dst0opt,
1323 			  (opt ? opt->dst0opt : NULL),
1324 			  newopt, newtype, &p);
1325 	ipv6_renew_option(IPV6_RTHDR,
1326 			  (struct ipv6_opt_hdr **)&opt2->srcrt,
1327 			  (opt ? (struct ipv6_opt_hdr *)opt->srcrt : NULL),
1328 			  newopt, newtype, &p);
1329 	ipv6_renew_option(IPV6_DSTOPTS, &opt2->dst1opt,
1330 			  (opt ? opt->dst1opt : NULL),
1331 			  newopt, newtype, &p);
1332 
1333 	opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) +
1334 			  (opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) +
1335 			  (opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0);
1336 	opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0);
1337 
1338 	return opt2;
1339 }
1340 
ipv6_fixup_options(struct ipv6_txoptions * opt_space,struct ipv6_txoptions * opt)1341 struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space,
1342 					  struct ipv6_txoptions *opt)
1343 {
1344 	/*
1345 	 * ignore the dest before srcrt unless srcrt is being included.
1346 	 * --yoshfuji
1347 	 */
1348 	if (opt && opt->dst0opt && !opt->srcrt) {
1349 		if (opt_space != opt) {
1350 			memcpy(opt_space, opt, sizeof(*opt_space));
1351 			opt = opt_space;
1352 		}
1353 		opt->opt_nflen -= ipv6_optlen(opt->dst0opt);
1354 		opt->dst0opt = NULL;
1355 	}
1356 
1357 	return opt;
1358 }
1359 EXPORT_SYMBOL_GPL(ipv6_fixup_options);
1360 
1361 /**
1362  * fl6_update_dst - update flowi destination address with info given
1363  *                  by srcrt option, if any.
1364  *
1365  * @fl6: flowi6 for which daddr is to be updated
1366  * @opt: struct ipv6_txoptions in which to look for srcrt opt
1367  * @orig: copy of original daddr address if modified
1368  *
1369  * Returns NULL if no txoptions or no srcrt, otherwise returns orig
1370  * and initial value of fl6->daddr set in orig
1371  */
fl6_update_dst(struct flowi6 * fl6,const struct ipv6_txoptions * opt,struct in6_addr * orig)1372 struct in6_addr *fl6_update_dst(struct flowi6 *fl6,
1373 				const struct ipv6_txoptions *opt,
1374 				struct in6_addr *orig)
1375 {
1376 	if (!opt || !opt->srcrt)
1377 		return NULL;
1378 
1379 	*orig = fl6->daddr;
1380 
1381 	switch (opt->srcrt->type) {
1382 	case IPV6_SRCRT_TYPE_0:
1383 	case IPV6_SRCRT_STRICT:
1384 	case IPV6_SRCRT_TYPE_2:
1385 		fl6->daddr = *((struct rt0_hdr *)opt->srcrt)->addr;
1386 		break;
1387 	case IPV6_SRCRT_TYPE_4:
1388 	{
1389 		struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)opt->srcrt;
1390 
1391 		fl6->daddr = srh->segments[srh->segments_left];
1392 		break;
1393 	}
1394 	default:
1395 		return NULL;
1396 	}
1397 
1398 	return orig;
1399 }
1400 EXPORT_SYMBOL_GPL(fl6_update_dst);
1401