xref: /linux/net/ipv6/exthdrs.c (revision 995231c820e3bd3633cb38bf4ea6f2541e1da331)
1 /*
2  *	Extension Header handling for IPv6
3  *	Linux INET6 implementation
4  *
5  *	Authors:
6  *	Pedro Roque		<roque@di.fc.ul.pt>
7  *	Andi Kleen		<ak@muc.de>
8  *	Alexey Kuznetsov	<kuznet@ms2.inr.ac.ru>
9  *
10  *	This program is free software; you can redistribute it and/or
11  *      modify it under the terms of the GNU General Public License
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
14  */
15 
16 /* Changes:
17  *	yoshfuji		: ensure not to overrun while parsing
18  *				  tlv options.
19  *	Mitsuru KANDA @USAGI and: Remove ipv6_parse_exthdrs().
20  *	YOSHIFUJI Hideaki @USAGI  Register inbound extension header
21  *				  handlers as inet6_protocol{}.
22  */
23 
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/socket.h>
27 #include <linux/sockios.h>
28 #include <linux/net.h>
29 #include <linux/netdevice.h>
30 #include <linux/in6.h>
31 #include <linux/icmpv6.h>
32 #include <linux/slab.h>
33 #include <linux/export.h>
34 
35 #include <net/dst.h>
36 #include <net/sock.h>
37 #include <net/snmp.h>
38 
39 #include <net/ipv6.h>
40 #include <net/protocol.h>
41 #include <net/transp_v6.h>
42 #include <net/rawv6.h>
43 #include <net/ndisc.h>
44 #include <net/ip6_route.h>
45 #include <net/addrconf.h>
46 #include <net/calipso.h>
47 #if IS_ENABLED(CONFIG_IPV6_MIP6)
48 #include <net/xfrm.h>
49 #endif
50 #include <linux/seg6.h>
51 #include <net/seg6.h>
52 #ifdef CONFIG_IPV6_SEG6_HMAC
53 #include <net/seg6_hmac.h>
54 #endif
55 
56 #include <linux/uaccess.h>
57 
58 /*
59  *	Parsing tlv encoded headers.
60  *
61  *	Parsing function "func" returns true, if parsing succeed
62  *	and false, if it failed.
63  *	It MUST NOT touch skb->h.
64  */
65 
66 struct tlvtype_proc {
67 	int	type;
68 	bool	(*func)(struct sk_buff *skb, int offset);
69 };
70 
71 /*********************
72   Generic functions
73  *********************/
74 
75 /* An unknown option is detected, decide what to do */
76 
77 static bool ip6_tlvopt_unknown(struct sk_buff *skb, int optoff)
78 {
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 		/* fall through */
93 	case 2: /* send ICMP PARM PROB regardless and drop packet */
94 		icmpv6_param_prob(skb, ICMPV6_UNK_OPTION, optoff);
95 		return false;
96 	}
97 
98 	kfree_skb(skb);
99 	return false;
100 }
101 
102 /* Parse tlv encoded option header (hop-by-hop or destination) */
103 
104 static bool ip6_parse_tlv(const struct tlvtype_proc *procs, struct sk_buff *skb)
105 {
106 	const struct tlvtype_proc *curr;
107 	const unsigned char *nh = skb_network_header(skb);
108 	int off = skb_network_header_len(skb);
109 	int len = (skb_transport_header(skb)[1] + 1) << 3;
110 	int padlen = 0;
111 
112 	if (skb_transport_offset(skb) + len > skb_headlen(skb))
113 		goto bad;
114 
115 	off += 2;
116 	len -= 2;
117 
118 	while (len > 0) {
119 		int optlen = nh[off + 1] + 2;
120 		int i;
121 
122 		switch (nh[off]) {
123 		case IPV6_TLV_PAD1:
124 			optlen = 1;
125 			padlen++;
126 			if (padlen > 7)
127 				goto bad;
128 			break;
129 
130 		case IPV6_TLV_PADN:
131 			/* RFC 2460 states that the purpose of PadN is
132 			 * to align the containing header to multiples
133 			 * of 8. 7 is therefore the highest valid value.
134 			 * See also RFC 4942, Section 2.1.9.5.
135 			 */
136 			padlen += optlen;
137 			if (padlen > 7)
138 				goto bad;
139 			/* RFC 4942 recommends receiving hosts to
140 			 * actively check PadN payload to contain
141 			 * only zeroes.
142 			 */
143 			for (i = 2; i < optlen; i++) {
144 				if (nh[off + i] != 0)
145 					goto bad;
146 			}
147 			break;
148 
149 		default: /* Other TLV code so scan list */
150 			if (optlen > len)
151 				goto bad;
152 			for (curr = procs; curr->type >= 0; curr++) {
153 				if (curr->type == nh[off]) {
154 					/* type specific length/alignment
155 					   checks will be performed in the
156 					   func(). */
157 					if (curr->func(skb, off) == false)
158 						return false;
159 					break;
160 				}
161 			}
162 			if (curr->type < 0) {
163 				if (ip6_tlvopt_unknown(skb, off) == 0)
164 					return false;
165 			}
166 			padlen = 0;
167 			break;
168 		}
169 		off += optlen;
170 		len -= optlen;
171 	}
172 
173 	if (len == 0)
174 		return true;
175 bad:
176 	kfree_skb(skb);
177 	return false;
178 }
179 
180 /*****************************
181   Destination options header.
182  *****************************/
183 
184 #if IS_ENABLED(CONFIG_IPV6_MIP6)
185 static bool ipv6_dest_hao(struct sk_buff *skb, int optoff)
186 {
187 	struct ipv6_destopt_hao *hao;
188 	struct inet6_skb_parm *opt = IP6CB(skb);
189 	struct ipv6hdr *ipv6h = ipv6_hdr(skb);
190 	int ret;
191 
192 	if (opt->dsthao) {
193 		net_dbg_ratelimited("hao duplicated\n");
194 		goto discard;
195 	}
196 	opt->dsthao = opt->dst1;
197 	opt->dst1 = 0;
198 
199 	hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + optoff);
200 
201 	if (hao->length != 16) {
202 		net_dbg_ratelimited("hao invalid option length = %d\n",
203 				    hao->length);
204 		goto discard;
205 	}
206 
207 	if (!(ipv6_addr_type(&hao->addr) & IPV6_ADDR_UNICAST)) {
208 		net_dbg_ratelimited("hao is not an unicast addr: %pI6\n",
209 				    &hao->addr);
210 		goto discard;
211 	}
212 
213 	ret = xfrm6_input_addr(skb, (xfrm_address_t *)&ipv6h->daddr,
214 			       (xfrm_address_t *)&hao->addr, IPPROTO_DSTOPTS);
215 	if (unlikely(ret < 0))
216 		goto discard;
217 
218 	if (skb_cloned(skb)) {
219 		if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
220 			goto discard;
221 
222 		/* update all variable using below by copied skbuff */
223 		hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) +
224 						  optoff);
225 		ipv6h = ipv6_hdr(skb);
226 	}
227 
228 	if (skb->ip_summed == CHECKSUM_COMPLETE)
229 		skb->ip_summed = CHECKSUM_NONE;
230 
231 	swap(ipv6h->saddr, hao->addr);
232 
233 	if (skb->tstamp == 0)
234 		__net_timestamp(skb);
235 
236 	return true;
237 
238  discard:
239 	kfree_skb(skb);
240 	return false;
241 }
242 #endif
243 
244 static const struct tlvtype_proc tlvprocdestopt_lst[] = {
245 #if IS_ENABLED(CONFIG_IPV6_MIP6)
246 	{
247 		.type	= IPV6_TLV_HAO,
248 		.func	= ipv6_dest_hao,
249 	},
250 #endif
251 	{-1,			NULL}
252 };
253 
254 static int ipv6_destopt_rcv(struct sk_buff *skb)
255 {
256 	struct inet6_skb_parm *opt = IP6CB(skb);
257 #if IS_ENABLED(CONFIG_IPV6_MIP6)
258 	__u16 dstbuf;
259 #endif
260 	struct dst_entry *dst = skb_dst(skb);
261 
262 	if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
263 	    !pskb_may_pull(skb, (skb_transport_offset(skb) +
264 				 ((skb_transport_header(skb)[1] + 1) << 3)))) {
265 		__IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
266 				IPSTATS_MIB_INHDRERRORS);
267 		kfree_skb(skb);
268 		return -1;
269 	}
270 
271 	opt->lastopt = opt->dst1 = skb_network_header_len(skb);
272 #if IS_ENABLED(CONFIG_IPV6_MIP6)
273 	dstbuf = opt->dst1;
274 #endif
275 
276 	if (ip6_parse_tlv(tlvprocdestopt_lst, skb)) {
277 		skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
278 		opt = IP6CB(skb);
279 #if IS_ENABLED(CONFIG_IPV6_MIP6)
280 		opt->nhoff = dstbuf;
281 #else
282 		opt->nhoff = opt->dst1;
283 #endif
284 		return 1;
285 	}
286 
287 	__IP6_INC_STATS(dev_net(dst->dev),
288 			ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS);
289 	return -1;
290 }
291 
292 static void seg6_update_csum(struct sk_buff *skb)
293 {
294 	struct ipv6_sr_hdr *hdr;
295 	struct in6_addr *addr;
296 	__be32 from, to;
297 
298 	/* srh is at transport offset and seg_left is already decremented
299 	 * but daddr is not yet updated with next segment
300 	 */
301 
302 	hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb);
303 	addr = hdr->segments + hdr->segments_left;
304 
305 	hdr->segments_left++;
306 	from = *(__be32 *)hdr;
307 
308 	hdr->segments_left--;
309 	to = *(__be32 *)hdr;
310 
311 	/* update skb csum with diff resulting from seg_left decrement */
312 
313 	update_csum_diff4(skb, from, to);
314 
315 	/* compute csum diff between current and next segment and update */
316 
317 	update_csum_diff16(skb, (__be32 *)(&ipv6_hdr(skb)->daddr),
318 			   (__be32 *)addr);
319 }
320 
321 static int ipv6_srh_rcv(struct sk_buff *skb)
322 {
323 	struct inet6_skb_parm *opt = IP6CB(skb);
324 	struct net *net = dev_net(skb->dev);
325 	struct ipv6_sr_hdr *hdr;
326 	struct inet6_dev *idev;
327 	struct in6_addr *addr;
328 	int accept_seg6;
329 
330 	hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb);
331 
332 	idev = __in6_dev_get(skb->dev);
333 
334 	accept_seg6 = net->ipv6.devconf_all->seg6_enabled;
335 	if (accept_seg6 > idev->cnf.seg6_enabled)
336 		accept_seg6 = idev->cnf.seg6_enabled;
337 
338 	if (!accept_seg6) {
339 		kfree_skb(skb);
340 		return -1;
341 	}
342 
343 #ifdef CONFIG_IPV6_SEG6_HMAC
344 	if (!seg6_hmac_validate_skb(skb)) {
345 		kfree_skb(skb);
346 		return -1;
347 	}
348 #endif
349 
350 looped_back:
351 	if (hdr->segments_left == 0) {
352 		if (hdr->nexthdr == NEXTHDR_IPV6) {
353 			int offset = (hdr->hdrlen + 1) << 3;
354 
355 			skb_postpull_rcsum(skb, skb_network_header(skb),
356 					   skb_network_header_len(skb));
357 
358 			if (!pskb_pull(skb, offset)) {
359 				kfree_skb(skb);
360 				return -1;
361 			}
362 			skb_postpull_rcsum(skb, skb_transport_header(skb),
363 					   offset);
364 
365 			skb_reset_network_header(skb);
366 			skb_reset_transport_header(skb);
367 			skb->encapsulation = 0;
368 
369 			__skb_tunnel_rx(skb, skb->dev, net);
370 
371 			netif_rx(skb);
372 			return -1;
373 		}
374 
375 		opt->srcrt = skb_network_header_len(skb);
376 		opt->lastopt = opt->srcrt;
377 		skb->transport_header += (hdr->hdrlen + 1) << 3;
378 		opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
379 
380 		return 1;
381 	}
382 
383 	if (hdr->segments_left >= (hdr->hdrlen >> 1)) {
384 		__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
385 				IPSTATS_MIB_INHDRERRORS);
386 		icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
387 				  ((&hdr->segments_left) -
388 				   skb_network_header(skb)));
389 		return -1;
390 	}
391 
392 	if (skb_cloned(skb)) {
393 		if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
394 			__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
395 					IPSTATS_MIB_OUTDISCARDS);
396 			kfree_skb(skb);
397 			return -1;
398 		}
399 	}
400 
401 	hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb);
402 
403 	hdr->segments_left--;
404 	addr = hdr->segments + hdr->segments_left;
405 
406 	skb_push(skb, sizeof(struct ipv6hdr));
407 
408 	if (skb->ip_summed == CHECKSUM_COMPLETE)
409 		seg6_update_csum(skb);
410 
411 	ipv6_hdr(skb)->daddr = *addr;
412 
413 	skb_dst_drop(skb);
414 
415 	ip6_route_input(skb);
416 
417 	if (skb_dst(skb)->error) {
418 		dst_input(skb);
419 		return -1;
420 	}
421 
422 	if (skb_dst(skb)->dev->flags & IFF_LOOPBACK) {
423 		if (ipv6_hdr(skb)->hop_limit <= 1) {
424 			__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
425 					IPSTATS_MIB_INHDRERRORS);
426 			icmpv6_send(skb, ICMPV6_TIME_EXCEED,
427 				    ICMPV6_EXC_HOPLIMIT, 0);
428 			kfree_skb(skb);
429 			return -1;
430 		}
431 		ipv6_hdr(skb)->hop_limit--;
432 
433 		skb_pull(skb, sizeof(struct ipv6hdr));
434 		goto looped_back;
435 	}
436 
437 	dst_input(skb);
438 
439 	return -1;
440 }
441 
442 /********************************
443   Routing header.
444  ********************************/
445 
446 /* called with rcu_read_lock() */
447 static int ipv6_rthdr_rcv(struct sk_buff *skb)
448 {
449 	struct inet6_skb_parm *opt = IP6CB(skb);
450 	struct in6_addr *addr = NULL;
451 	struct in6_addr daddr;
452 	struct inet6_dev *idev;
453 	int n, i;
454 	struct ipv6_rt_hdr *hdr;
455 	struct rt0_hdr *rthdr;
456 	struct net *net = dev_net(skb->dev);
457 	int accept_source_route = net->ipv6.devconf_all->accept_source_route;
458 
459 	idev = __in6_dev_get(skb->dev);
460 	if (idev && accept_source_route > idev->cnf.accept_source_route)
461 		accept_source_route = idev->cnf.accept_source_route;
462 
463 	if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
464 	    !pskb_may_pull(skb, (skb_transport_offset(skb) +
465 				 ((skb_transport_header(skb)[1] + 1) << 3)))) {
466 		__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
467 				IPSTATS_MIB_INHDRERRORS);
468 		kfree_skb(skb);
469 		return -1;
470 	}
471 
472 	hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
473 
474 	if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ||
475 	    skb->pkt_type != PACKET_HOST) {
476 		__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
477 				IPSTATS_MIB_INADDRERRORS);
478 		kfree_skb(skb);
479 		return -1;
480 	}
481 
482 	/* segment routing */
483 	if (hdr->type == IPV6_SRCRT_TYPE_4)
484 		return ipv6_srh_rcv(skb);
485 
486 looped_back:
487 	if (hdr->segments_left == 0) {
488 		switch (hdr->type) {
489 #if IS_ENABLED(CONFIG_IPV6_MIP6)
490 		case IPV6_SRCRT_TYPE_2:
491 			/* Silently discard type 2 header unless it was
492 			 * processed by own
493 			 */
494 			if (!addr) {
495 				__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
496 						IPSTATS_MIB_INADDRERRORS);
497 				kfree_skb(skb);
498 				return -1;
499 			}
500 			break;
501 #endif
502 		default:
503 			break;
504 		}
505 
506 		opt->lastopt = opt->srcrt = skb_network_header_len(skb);
507 		skb->transport_header += (hdr->hdrlen + 1) << 3;
508 		opt->dst0 = opt->dst1;
509 		opt->dst1 = 0;
510 		opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
511 		return 1;
512 	}
513 
514 	switch (hdr->type) {
515 #if IS_ENABLED(CONFIG_IPV6_MIP6)
516 	case IPV6_SRCRT_TYPE_2:
517 		if (accept_source_route < 0)
518 			goto unknown_rh;
519 		/* Silently discard invalid RTH type 2 */
520 		if (hdr->hdrlen != 2 || hdr->segments_left != 1) {
521 			__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
522 					IPSTATS_MIB_INHDRERRORS);
523 			kfree_skb(skb);
524 			return -1;
525 		}
526 		break;
527 #endif
528 	default:
529 		goto unknown_rh;
530 	}
531 
532 	/*
533 	 *	This is the routing header forwarding algorithm from
534 	 *	RFC 2460, page 16.
535 	 */
536 
537 	n = hdr->hdrlen >> 1;
538 
539 	if (hdr->segments_left > n) {
540 		__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
541 				IPSTATS_MIB_INHDRERRORS);
542 		icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
543 				  ((&hdr->segments_left) -
544 				   skb_network_header(skb)));
545 		return -1;
546 	}
547 
548 	/* We are about to mangle packet header. Be careful!
549 	   Do not damage packets queued somewhere.
550 	 */
551 	if (skb_cloned(skb)) {
552 		/* the copy is a forwarded packet */
553 		if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
554 			__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
555 					IPSTATS_MIB_OUTDISCARDS);
556 			kfree_skb(skb);
557 			return -1;
558 		}
559 		hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
560 	}
561 
562 	if (skb->ip_summed == CHECKSUM_COMPLETE)
563 		skb->ip_summed = CHECKSUM_NONE;
564 
565 	i = n - --hdr->segments_left;
566 
567 	rthdr = (struct rt0_hdr *) hdr;
568 	addr = rthdr->addr;
569 	addr += i - 1;
570 
571 	switch (hdr->type) {
572 #if IS_ENABLED(CONFIG_IPV6_MIP6)
573 	case IPV6_SRCRT_TYPE_2:
574 		if (xfrm6_input_addr(skb, (xfrm_address_t *)addr,
575 				     (xfrm_address_t *)&ipv6_hdr(skb)->saddr,
576 				     IPPROTO_ROUTING) < 0) {
577 			__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
578 					IPSTATS_MIB_INADDRERRORS);
579 			kfree_skb(skb);
580 			return -1;
581 		}
582 		if (!ipv6_chk_home_addr(dev_net(skb_dst(skb)->dev), addr)) {
583 			__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
584 					IPSTATS_MIB_INADDRERRORS);
585 			kfree_skb(skb);
586 			return -1;
587 		}
588 		break;
589 #endif
590 	default:
591 		break;
592 	}
593 
594 	if (ipv6_addr_is_multicast(addr)) {
595 		__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
596 				IPSTATS_MIB_INADDRERRORS);
597 		kfree_skb(skb);
598 		return -1;
599 	}
600 
601 	daddr = *addr;
602 	*addr = ipv6_hdr(skb)->daddr;
603 	ipv6_hdr(skb)->daddr = daddr;
604 
605 	skb_dst_drop(skb);
606 	ip6_route_input(skb);
607 	if (skb_dst(skb)->error) {
608 		skb_push(skb, skb->data - skb_network_header(skb));
609 		dst_input(skb);
610 		return -1;
611 	}
612 
613 	if (skb_dst(skb)->dev->flags&IFF_LOOPBACK) {
614 		if (ipv6_hdr(skb)->hop_limit <= 1) {
615 			__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
616 					IPSTATS_MIB_INHDRERRORS);
617 			icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
618 				    0);
619 			kfree_skb(skb);
620 			return -1;
621 		}
622 		ipv6_hdr(skb)->hop_limit--;
623 		goto looped_back;
624 	}
625 
626 	skb_push(skb, skb->data - skb_network_header(skb));
627 	dst_input(skb);
628 	return -1;
629 
630 unknown_rh:
631 	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INHDRERRORS);
632 	icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
633 			  (&hdr->type) - skb_network_header(skb));
634 	return -1;
635 }
636 
637 static const struct inet6_protocol rthdr_protocol = {
638 	.handler	=	ipv6_rthdr_rcv,
639 	.flags		=	INET6_PROTO_NOPOLICY,
640 };
641 
642 static const struct inet6_protocol destopt_protocol = {
643 	.handler	=	ipv6_destopt_rcv,
644 	.flags		=	INET6_PROTO_NOPOLICY,
645 };
646 
647 static const struct inet6_protocol nodata_protocol = {
648 	.handler	=	dst_discard,
649 	.flags		=	INET6_PROTO_NOPOLICY,
650 };
651 
652 int __init ipv6_exthdrs_init(void)
653 {
654 	int ret;
655 
656 	ret = inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING);
657 	if (ret)
658 		goto out;
659 
660 	ret = inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
661 	if (ret)
662 		goto out_rthdr;
663 
664 	ret = inet6_add_protocol(&nodata_protocol, IPPROTO_NONE);
665 	if (ret)
666 		goto out_destopt;
667 
668 out:
669 	return ret;
670 out_destopt:
671 	inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
672 out_rthdr:
673 	inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
674 	goto out;
675 };
676 
677 void ipv6_exthdrs_exit(void)
678 {
679 	inet6_del_protocol(&nodata_protocol, IPPROTO_NONE);
680 	inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
681 	inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
682 }
683 
684 /**********************************
685   Hop-by-hop options.
686  **********************************/
687 
688 /*
689  * Note: we cannot rely on skb_dst(skb) before we assign it in ip6_route_input().
690  */
691 static inline struct inet6_dev *ipv6_skb_idev(struct sk_buff *skb)
692 {
693 	return skb_dst(skb) ? ip6_dst_idev(skb_dst(skb)) : __in6_dev_get(skb->dev);
694 }
695 
696 static inline struct net *ipv6_skb_net(struct sk_buff *skb)
697 {
698 	return skb_dst(skb) ? dev_net(skb_dst(skb)->dev) : dev_net(skb->dev);
699 }
700 
701 /* Router Alert as of RFC 2711 */
702 
703 static bool ipv6_hop_ra(struct sk_buff *skb, int optoff)
704 {
705 	const unsigned char *nh = skb_network_header(skb);
706 
707 	if (nh[optoff + 1] == 2) {
708 		IP6CB(skb)->flags |= IP6SKB_ROUTERALERT;
709 		memcpy(&IP6CB(skb)->ra, nh + optoff + 2, sizeof(IP6CB(skb)->ra));
710 		return true;
711 	}
712 	net_dbg_ratelimited("ipv6_hop_ra: wrong RA length %d\n",
713 			    nh[optoff + 1]);
714 	kfree_skb(skb);
715 	return false;
716 }
717 
718 /* Jumbo payload */
719 
720 static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff)
721 {
722 	const unsigned char *nh = skb_network_header(skb);
723 	struct net *net = ipv6_skb_net(skb);
724 	u32 pkt_len;
725 
726 	if (nh[optoff + 1] != 4 || (optoff & 3) != 2) {
727 		net_dbg_ratelimited("ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n",
728 				    nh[optoff+1]);
729 		__IP6_INC_STATS(net, ipv6_skb_idev(skb),
730 				IPSTATS_MIB_INHDRERRORS);
731 		goto drop;
732 	}
733 
734 	pkt_len = ntohl(*(__be32 *)(nh + optoff + 2));
735 	if (pkt_len <= IPV6_MAXPLEN) {
736 		__IP6_INC_STATS(net, ipv6_skb_idev(skb),
737 				IPSTATS_MIB_INHDRERRORS);
738 		icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff+2);
739 		return false;
740 	}
741 	if (ipv6_hdr(skb)->payload_len) {
742 		__IP6_INC_STATS(net, ipv6_skb_idev(skb),
743 				IPSTATS_MIB_INHDRERRORS);
744 		icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff);
745 		return false;
746 	}
747 
748 	if (pkt_len > skb->len - sizeof(struct ipv6hdr)) {
749 		__IP6_INC_STATS(net, ipv6_skb_idev(skb),
750 				IPSTATS_MIB_INTRUNCATEDPKTS);
751 		goto drop;
752 	}
753 
754 	if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
755 		goto drop;
756 
757 	IP6CB(skb)->flags |= IP6SKB_JUMBOGRAM;
758 	return true;
759 
760 drop:
761 	kfree_skb(skb);
762 	return false;
763 }
764 
765 /* CALIPSO RFC 5570 */
766 
767 static bool ipv6_hop_calipso(struct sk_buff *skb, int optoff)
768 {
769 	const unsigned char *nh = skb_network_header(skb);
770 
771 	if (nh[optoff + 1] < 8)
772 		goto drop;
773 
774 	if (nh[optoff + 6] * 4 + 8 > nh[optoff + 1])
775 		goto drop;
776 
777 	if (!calipso_validate(skb, nh + optoff))
778 		goto drop;
779 
780 	return true;
781 
782 drop:
783 	kfree_skb(skb);
784 	return false;
785 }
786 
787 static const struct tlvtype_proc tlvprochopopt_lst[] = {
788 	{
789 		.type	= IPV6_TLV_ROUTERALERT,
790 		.func	= ipv6_hop_ra,
791 	},
792 	{
793 		.type	= IPV6_TLV_JUMBO,
794 		.func	= ipv6_hop_jumbo,
795 	},
796 	{
797 		.type	= IPV6_TLV_CALIPSO,
798 		.func	= ipv6_hop_calipso,
799 	},
800 	{ -1, }
801 };
802 
803 int ipv6_parse_hopopts(struct sk_buff *skb)
804 {
805 	struct inet6_skb_parm *opt = IP6CB(skb);
806 
807 	/*
808 	 * skb_network_header(skb) is equal to skb->data, and
809 	 * skb_network_header_len(skb) is always equal to
810 	 * sizeof(struct ipv6hdr) by definition of
811 	 * hop-by-hop options.
812 	 */
813 	if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) ||
814 	    !pskb_may_pull(skb, (sizeof(struct ipv6hdr) +
815 				 ((skb_transport_header(skb)[1] + 1) << 3)))) {
816 		kfree_skb(skb);
817 		return -1;
818 	}
819 
820 	opt->flags |= IP6SKB_HOPBYHOP;
821 	if (ip6_parse_tlv(tlvprochopopt_lst, skb)) {
822 		skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
823 		opt = IP6CB(skb);
824 		opt->nhoff = sizeof(struct ipv6hdr);
825 		return 1;
826 	}
827 	return -1;
828 }
829 
830 /*
831  *	Creating outbound headers.
832  *
833  *	"build" functions work when skb is filled from head to tail (datagram)
834  *	"push"	functions work when headers are added from tail to head (tcp)
835  *
836  *	In both cases we assume, that caller reserved enough room
837  *	for headers.
838  */
839 
840 static void ipv6_push_rthdr0(struct sk_buff *skb, u8 *proto,
841 			     struct ipv6_rt_hdr *opt,
842 			     struct in6_addr **addr_p, struct in6_addr *saddr)
843 {
844 	struct rt0_hdr *phdr, *ihdr;
845 	int hops;
846 
847 	ihdr = (struct rt0_hdr *) opt;
848 
849 	phdr = skb_push(skb, (ihdr->rt_hdr.hdrlen + 1) << 3);
850 	memcpy(phdr, ihdr, sizeof(struct rt0_hdr));
851 
852 	hops = ihdr->rt_hdr.hdrlen >> 1;
853 
854 	if (hops > 1)
855 		memcpy(phdr->addr, ihdr->addr + 1,
856 		       (hops - 1) * sizeof(struct in6_addr));
857 
858 	phdr->addr[hops - 1] = **addr_p;
859 	*addr_p = ihdr->addr;
860 
861 	phdr->rt_hdr.nexthdr = *proto;
862 	*proto = NEXTHDR_ROUTING;
863 }
864 
865 static void ipv6_push_rthdr4(struct sk_buff *skb, u8 *proto,
866 			     struct ipv6_rt_hdr *opt,
867 			     struct in6_addr **addr_p, struct in6_addr *saddr)
868 {
869 	struct ipv6_sr_hdr *sr_phdr, *sr_ihdr;
870 	int plen, hops;
871 
872 	sr_ihdr = (struct ipv6_sr_hdr *)opt;
873 	plen = (sr_ihdr->hdrlen + 1) << 3;
874 
875 	sr_phdr = skb_push(skb, plen);
876 	memcpy(sr_phdr, sr_ihdr, sizeof(struct ipv6_sr_hdr));
877 
878 	hops = sr_ihdr->first_segment + 1;
879 	memcpy(sr_phdr->segments + 1, sr_ihdr->segments + 1,
880 	       (hops - 1) * sizeof(struct in6_addr));
881 
882 	sr_phdr->segments[0] = **addr_p;
883 	*addr_p = &sr_ihdr->segments[sr_ihdr->segments_left];
884 
885 #ifdef CONFIG_IPV6_SEG6_HMAC
886 	if (sr_has_hmac(sr_phdr)) {
887 		struct net *net = NULL;
888 
889 		if (skb->dev)
890 			net = dev_net(skb->dev);
891 		else if (skb->sk)
892 			net = sock_net(skb->sk);
893 
894 		WARN_ON(!net);
895 
896 		if (net)
897 			seg6_push_hmac(net, saddr, sr_phdr);
898 	}
899 #endif
900 
901 	sr_phdr->nexthdr = *proto;
902 	*proto = NEXTHDR_ROUTING;
903 }
904 
905 static void ipv6_push_rthdr(struct sk_buff *skb, u8 *proto,
906 			    struct ipv6_rt_hdr *opt,
907 			    struct in6_addr **addr_p, struct in6_addr *saddr)
908 {
909 	switch (opt->type) {
910 	case IPV6_SRCRT_TYPE_0:
911 	case IPV6_SRCRT_STRICT:
912 	case IPV6_SRCRT_TYPE_2:
913 		ipv6_push_rthdr0(skb, proto, opt, addr_p, saddr);
914 		break;
915 	case IPV6_SRCRT_TYPE_4:
916 		ipv6_push_rthdr4(skb, proto, opt, addr_p, saddr);
917 		break;
918 	default:
919 		break;
920 	}
921 }
922 
923 static void ipv6_push_exthdr(struct sk_buff *skb, u8 *proto, u8 type, struct ipv6_opt_hdr *opt)
924 {
925 	struct ipv6_opt_hdr *h = skb_push(skb, ipv6_optlen(opt));
926 
927 	memcpy(h, opt, ipv6_optlen(opt));
928 	h->nexthdr = *proto;
929 	*proto = type;
930 }
931 
932 void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
933 			  u8 *proto,
934 			  struct in6_addr **daddr, struct in6_addr *saddr)
935 {
936 	if (opt->srcrt) {
937 		ipv6_push_rthdr(skb, proto, opt->srcrt, daddr, saddr);
938 		/*
939 		 * IPV6_RTHDRDSTOPTS is ignored
940 		 * unless IPV6_RTHDR is set (RFC3542).
941 		 */
942 		if (opt->dst0opt)
943 			ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst0opt);
944 	}
945 	if (opt->hopopt)
946 		ipv6_push_exthdr(skb, proto, NEXTHDR_HOP, opt->hopopt);
947 }
948 
949 void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, u8 *proto)
950 {
951 	if (opt->dst1opt)
952 		ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst1opt);
953 }
954 EXPORT_SYMBOL(ipv6_push_frag_opts);
955 
956 struct ipv6_txoptions *
957 ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt)
958 {
959 	struct ipv6_txoptions *opt2;
960 
961 	opt2 = sock_kmalloc(sk, opt->tot_len, GFP_ATOMIC);
962 	if (opt2) {
963 		long dif = (char *)opt2 - (char *)opt;
964 		memcpy(opt2, opt, opt->tot_len);
965 		if (opt2->hopopt)
966 			*((char **)&opt2->hopopt) += dif;
967 		if (opt2->dst0opt)
968 			*((char **)&opt2->dst0opt) += dif;
969 		if (opt2->dst1opt)
970 			*((char **)&opt2->dst1opt) += dif;
971 		if (opt2->srcrt)
972 			*((char **)&opt2->srcrt) += dif;
973 		refcount_set(&opt2->refcnt, 1);
974 	}
975 	return opt2;
976 }
977 EXPORT_SYMBOL_GPL(ipv6_dup_options);
978 
979 static int ipv6_renew_option(void *ohdr,
980 			     struct ipv6_opt_hdr __user *newopt, int newoptlen,
981 			     int inherit,
982 			     struct ipv6_opt_hdr **hdr,
983 			     char **p)
984 {
985 	if (inherit) {
986 		if (ohdr) {
987 			memcpy(*p, ohdr, ipv6_optlen((struct ipv6_opt_hdr *)ohdr));
988 			*hdr = (struct ipv6_opt_hdr *)*p;
989 			*p += CMSG_ALIGN(ipv6_optlen(*hdr));
990 		}
991 	} else {
992 		if (newopt) {
993 			if (copy_from_user(*p, newopt, newoptlen))
994 				return -EFAULT;
995 			*hdr = (struct ipv6_opt_hdr *)*p;
996 			if (ipv6_optlen(*hdr) > newoptlen)
997 				return -EINVAL;
998 			*p += CMSG_ALIGN(newoptlen);
999 		}
1000 	}
1001 	return 0;
1002 }
1003 
1004 /**
1005  * ipv6_renew_options - replace a specific ext hdr with a new one.
1006  *
1007  * @sk: sock from which to allocate memory
1008  * @opt: original options
1009  * @newtype: option type to replace in @opt
1010  * @newopt: new option of type @newtype to replace (user-mem)
1011  * @newoptlen: length of @newopt
1012  *
1013  * Returns a new set of options which is a copy of @opt with the
1014  * option type @newtype replaced with @newopt.
1015  *
1016  * @opt may be NULL, in which case a new set of options is returned
1017  * containing just @newopt.
1018  *
1019  * @newopt may be NULL, in which case the specified option type is
1020  * not copied into the new set of options.
1021  *
1022  * The new set of options is allocated from the socket option memory
1023  * buffer of @sk.
1024  */
1025 struct ipv6_txoptions *
1026 ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt,
1027 		   int newtype,
1028 		   struct ipv6_opt_hdr __user *newopt, int newoptlen)
1029 {
1030 	int tot_len = 0;
1031 	char *p;
1032 	struct ipv6_txoptions *opt2;
1033 	int err;
1034 
1035 	if (opt) {
1036 		if (newtype != IPV6_HOPOPTS && opt->hopopt)
1037 			tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt));
1038 		if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt)
1039 			tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt));
1040 		if (newtype != IPV6_RTHDR && opt->srcrt)
1041 			tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt));
1042 		if (newtype != IPV6_DSTOPTS && opt->dst1opt)
1043 			tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt));
1044 	}
1045 
1046 	if (newopt && newoptlen)
1047 		tot_len += CMSG_ALIGN(newoptlen);
1048 
1049 	if (!tot_len)
1050 		return NULL;
1051 
1052 	tot_len += sizeof(*opt2);
1053 	opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC);
1054 	if (!opt2)
1055 		return ERR_PTR(-ENOBUFS);
1056 
1057 	memset(opt2, 0, tot_len);
1058 	refcount_set(&opt2->refcnt, 1);
1059 	opt2->tot_len = tot_len;
1060 	p = (char *)(opt2 + 1);
1061 
1062 	err = ipv6_renew_option(opt ? opt->hopopt : NULL, newopt, newoptlen,
1063 				newtype != IPV6_HOPOPTS,
1064 				&opt2->hopopt, &p);
1065 	if (err)
1066 		goto out;
1067 
1068 	err = ipv6_renew_option(opt ? opt->dst0opt : NULL, newopt, newoptlen,
1069 				newtype != IPV6_RTHDRDSTOPTS,
1070 				&opt2->dst0opt, &p);
1071 	if (err)
1072 		goto out;
1073 
1074 	err = ipv6_renew_option(opt ? opt->srcrt : NULL, newopt, newoptlen,
1075 				newtype != IPV6_RTHDR,
1076 				(struct ipv6_opt_hdr **)&opt2->srcrt, &p);
1077 	if (err)
1078 		goto out;
1079 
1080 	err = ipv6_renew_option(opt ? opt->dst1opt : NULL, newopt, newoptlen,
1081 				newtype != IPV6_DSTOPTS,
1082 				&opt2->dst1opt, &p);
1083 	if (err)
1084 		goto out;
1085 
1086 	opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) +
1087 			  (opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) +
1088 			  (opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0);
1089 	opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0);
1090 
1091 	return opt2;
1092 out:
1093 	sock_kfree_s(sk, opt2, opt2->tot_len);
1094 	return ERR_PTR(err);
1095 }
1096 
1097 /**
1098  * ipv6_renew_options_kern - replace a specific ext hdr with a new one.
1099  *
1100  * @sk: sock from which to allocate memory
1101  * @opt: original options
1102  * @newtype: option type to replace in @opt
1103  * @newopt: new option of type @newtype to replace (kernel-mem)
1104  * @newoptlen: length of @newopt
1105  *
1106  * See ipv6_renew_options().  The difference is that @newopt is
1107  * kernel memory, rather than user memory.
1108  */
1109 struct ipv6_txoptions *
1110 ipv6_renew_options_kern(struct sock *sk, struct ipv6_txoptions *opt,
1111 			int newtype, struct ipv6_opt_hdr *newopt,
1112 			int newoptlen)
1113 {
1114 	struct ipv6_txoptions *ret_val;
1115 	const mm_segment_t old_fs = get_fs();
1116 
1117 	set_fs(KERNEL_DS);
1118 	ret_val = ipv6_renew_options(sk, opt, newtype,
1119 				     (struct ipv6_opt_hdr __user *)newopt,
1120 				     newoptlen);
1121 	set_fs(old_fs);
1122 	return ret_val;
1123 }
1124 
1125 struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space,
1126 					  struct ipv6_txoptions *opt)
1127 {
1128 	/*
1129 	 * ignore the dest before srcrt unless srcrt is being included.
1130 	 * --yoshfuji
1131 	 */
1132 	if (opt && opt->dst0opt && !opt->srcrt) {
1133 		if (opt_space != opt) {
1134 			memcpy(opt_space, opt, sizeof(*opt_space));
1135 			opt = opt_space;
1136 		}
1137 		opt->opt_nflen -= ipv6_optlen(opt->dst0opt);
1138 		opt->dst0opt = NULL;
1139 	}
1140 
1141 	return opt;
1142 }
1143 EXPORT_SYMBOL_GPL(ipv6_fixup_options);
1144 
1145 /**
1146  * fl6_update_dst - update flowi destination address with info given
1147  *                  by srcrt option, if any.
1148  *
1149  * @fl6: flowi6 for which daddr is to be updated
1150  * @opt: struct ipv6_txoptions in which to look for srcrt opt
1151  * @orig: copy of original daddr address if modified
1152  *
1153  * Returns NULL if no txoptions or no srcrt, otherwise returns orig
1154  * and initial value of fl6->daddr set in orig
1155  */
1156 struct in6_addr *fl6_update_dst(struct flowi6 *fl6,
1157 				const struct ipv6_txoptions *opt,
1158 				struct in6_addr *orig)
1159 {
1160 	if (!opt || !opt->srcrt)
1161 		return NULL;
1162 
1163 	*orig = fl6->daddr;
1164 
1165 	switch (opt->srcrt->type) {
1166 	case IPV6_SRCRT_TYPE_0:
1167 	case IPV6_SRCRT_STRICT:
1168 	case IPV6_SRCRT_TYPE_2:
1169 		fl6->daddr = *((struct rt0_hdr *)opt->srcrt)->addr;
1170 		break;
1171 	case IPV6_SRCRT_TYPE_4:
1172 	{
1173 		struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)opt->srcrt;
1174 
1175 		fl6->daddr = srh->segments[srh->segments_left];
1176 		break;
1177 	}
1178 	default:
1179 		return NULL;
1180 	}
1181 
1182 	return orig;
1183 }
1184 EXPORT_SYMBOL_GPL(fl6_update_dst);
1185