xref: /openbmc/linux/net/ipv6/datagram.c (revision 239480ab)
1 /*
2  *	common UDP/RAW code
3  *	Linux INET6 implementation
4  *
5  *	Authors:
6  *	Pedro Roque		<roque@di.fc.ul.pt>
7  *
8  *	This program is free software; you can redistribute it and/or
9  *      modify it under the terms of the GNU General Public License
10  *      as published by the Free Software Foundation; either version
11  *      2 of the License, or (at your option) any later version.
12  */
13 
14 #include <linux/capability.h>
15 #include <linux/errno.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/interrupt.h>
19 #include <linux/socket.h>
20 #include <linux/sockios.h>
21 #include <linux/in6.h>
22 #include <linux/ipv6.h>
23 #include <linux/route.h>
24 #include <linux/slab.h>
25 #include <linux/export.h>
26 
27 #include <net/ipv6.h>
28 #include <net/ndisc.h>
29 #include <net/addrconf.h>
30 #include <net/transp_v6.h>
31 #include <net/ip6_route.h>
32 #include <net/tcp_states.h>
33 #include <net/dsfield.h>
34 
35 #include <linux/errqueue.h>
36 #include <linux/uaccess.h>
37 
38 static bool ipv6_mapped_addr_any(const struct in6_addr *a)
39 {
40 	return ipv6_addr_v4mapped(a) && (a->s6_addr32[3] == 0);
41 }
42 
43 static void ip6_datagram_flow_key_init(struct flowi6 *fl6, struct sock *sk)
44 {
45 	struct inet_sock *inet = inet_sk(sk);
46 	struct ipv6_pinfo *np = inet6_sk(sk);
47 
48 	memset(fl6, 0, sizeof(*fl6));
49 	fl6->flowi6_proto = sk->sk_protocol;
50 	fl6->daddr = sk->sk_v6_daddr;
51 	fl6->saddr = np->saddr;
52 	fl6->flowi6_oif = sk->sk_bound_dev_if;
53 	fl6->flowi6_mark = sk->sk_mark;
54 	fl6->fl6_dport = inet->inet_dport;
55 	fl6->fl6_sport = inet->inet_sport;
56 	fl6->flowlabel = np->flow_label;
57 	fl6->flowi6_uid = sk->sk_uid;
58 
59 	if (!fl6->flowi6_oif)
60 		fl6->flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
61 
62 	if (!fl6->flowi6_oif && ipv6_addr_is_multicast(&fl6->daddr))
63 		fl6->flowi6_oif = np->mcast_oif;
64 
65 	security_sk_classify_flow(sk, flowi6_to_flowi(fl6));
66 }
67 
68 int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr)
69 {
70 	struct ip6_flowlabel *flowlabel = NULL;
71 	struct in6_addr *final_p, final;
72 	struct ipv6_txoptions *opt;
73 	struct dst_entry *dst;
74 	struct inet_sock *inet = inet_sk(sk);
75 	struct ipv6_pinfo *np = inet6_sk(sk);
76 	struct flowi6 fl6;
77 	int err = 0;
78 
79 	if (np->sndflow && (np->flow_label & IPV6_FLOWLABEL_MASK)) {
80 		flowlabel = fl6_sock_lookup(sk, np->flow_label);
81 		if (!flowlabel)
82 			return -EINVAL;
83 	}
84 	ip6_datagram_flow_key_init(&fl6, sk);
85 
86 	rcu_read_lock();
87 	opt = flowlabel ? flowlabel->opt : rcu_dereference(np->opt);
88 	final_p = fl6_update_dst(&fl6, opt, &final);
89 	rcu_read_unlock();
90 
91 	dst = ip6_dst_lookup_flow(sk, &fl6, final_p);
92 	if (IS_ERR(dst)) {
93 		err = PTR_ERR(dst);
94 		goto out;
95 	}
96 
97 	if (fix_sk_saddr) {
98 		if (ipv6_addr_any(&np->saddr))
99 			np->saddr = fl6.saddr;
100 
101 		if (ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
102 			sk->sk_v6_rcv_saddr = fl6.saddr;
103 			inet->inet_rcv_saddr = LOOPBACK4_IPV6;
104 			if (sk->sk_prot->rehash)
105 				sk->sk_prot->rehash(sk);
106 		}
107 	}
108 
109 	ip6_dst_store(sk, dst,
110 		      ipv6_addr_equal(&fl6.daddr, &sk->sk_v6_daddr) ?
111 		      &sk->sk_v6_daddr : NULL,
112 #ifdef CONFIG_IPV6_SUBTREES
113 		      ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
114 		      &np->saddr :
115 #endif
116 		      NULL);
117 
118 out:
119 	fl6_sock_release(flowlabel);
120 	return err;
121 }
122 
123 void ip6_datagram_release_cb(struct sock *sk)
124 {
125 	struct dst_entry *dst;
126 
127 	if (ipv6_addr_v4mapped(&sk->sk_v6_daddr))
128 		return;
129 
130 	rcu_read_lock();
131 	dst = __sk_dst_get(sk);
132 	if (!dst || !dst->obsolete ||
133 	    dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) {
134 		rcu_read_unlock();
135 		return;
136 	}
137 	rcu_read_unlock();
138 
139 	ip6_datagram_dst_update(sk, false);
140 }
141 EXPORT_SYMBOL_GPL(ip6_datagram_release_cb);
142 
143 int __ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr,
144 			   int addr_len)
145 {
146 	struct sockaddr_in6	*usin = (struct sockaddr_in6 *) uaddr;
147 	struct inet_sock	*inet = inet_sk(sk);
148 	struct ipv6_pinfo	*np = inet6_sk(sk);
149 	struct in6_addr		*daddr;
150 	int			addr_type;
151 	int			err;
152 	__be32			fl6_flowlabel = 0;
153 
154 	if (usin->sin6_family == AF_INET) {
155 		if (__ipv6_only_sock(sk))
156 			return -EAFNOSUPPORT;
157 		err = __ip4_datagram_connect(sk, uaddr, addr_len);
158 		goto ipv4_connected;
159 	}
160 
161 	if (addr_len < SIN6_LEN_RFC2133)
162 		return -EINVAL;
163 
164 	if (usin->sin6_family != AF_INET6)
165 		return -EAFNOSUPPORT;
166 
167 	if (np->sndflow)
168 		fl6_flowlabel = usin->sin6_flowinfo & IPV6_FLOWINFO_MASK;
169 
170 	if (ipv6_addr_any(&usin->sin6_addr)) {
171 		/*
172 		 *	connect to self
173 		 */
174 		if (ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr))
175 			ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK),
176 					       &usin->sin6_addr);
177 		else
178 			usin->sin6_addr = in6addr_loopback;
179 	}
180 
181 	addr_type = ipv6_addr_type(&usin->sin6_addr);
182 
183 	daddr = &usin->sin6_addr;
184 
185 	if (addr_type & IPV6_ADDR_MAPPED) {
186 		struct sockaddr_in sin;
187 
188 		if (__ipv6_only_sock(sk)) {
189 			err = -ENETUNREACH;
190 			goto out;
191 		}
192 		sin.sin_family = AF_INET;
193 		sin.sin_addr.s_addr = daddr->s6_addr32[3];
194 		sin.sin_port = usin->sin6_port;
195 
196 		err = __ip4_datagram_connect(sk,
197 					     (struct sockaddr *) &sin,
198 					     sizeof(sin));
199 
200 ipv4_connected:
201 		if (err)
202 			goto out;
203 
204 		ipv6_addr_set_v4mapped(inet->inet_daddr, &sk->sk_v6_daddr);
205 
206 		if (ipv6_addr_any(&np->saddr) ||
207 		    ipv6_mapped_addr_any(&np->saddr))
208 			ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
209 
210 		if (ipv6_addr_any(&sk->sk_v6_rcv_saddr) ||
211 		    ipv6_mapped_addr_any(&sk->sk_v6_rcv_saddr)) {
212 			ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
213 					       &sk->sk_v6_rcv_saddr);
214 			if (sk->sk_prot->rehash)
215 				sk->sk_prot->rehash(sk);
216 		}
217 
218 		goto out;
219 	}
220 
221 	if (__ipv6_addr_needs_scope_id(addr_type)) {
222 		if (addr_len >= sizeof(struct sockaddr_in6) &&
223 		    usin->sin6_scope_id) {
224 			if (sk->sk_bound_dev_if &&
225 			    sk->sk_bound_dev_if != usin->sin6_scope_id) {
226 				err = -EINVAL;
227 				goto out;
228 			}
229 			sk->sk_bound_dev_if = usin->sin6_scope_id;
230 		}
231 
232 		if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST))
233 			sk->sk_bound_dev_if = np->mcast_oif;
234 
235 		/* Connect to link-local address requires an interface */
236 		if (!sk->sk_bound_dev_if) {
237 			err = -EINVAL;
238 			goto out;
239 		}
240 	}
241 
242 	sk->sk_v6_daddr = *daddr;
243 	np->flow_label = fl6_flowlabel;
244 
245 	inet->inet_dport = usin->sin6_port;
246 
247 	/*
248 	 *	Check for a route to destination an obtain the
249 	 *	destination cache for it.
250 	 */
251 
252 	err = ip6_datagram_dst_update(sk, true);
253 	if (err)
254 		goto out;
255 
256 	sk->sk_state = TCP_ESTABLISHED;
257 	sk_set_txhash(sk);
258 out:
259 	return err;
260 }
261 EXPORT_SYMBOL_GPL(__ip6_datagram_connect);
262 
263 int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
264 {
265 	int res;
266 
267 	lock_sock(sk);
268 	res = __ip6_datagram_connect(sk, uaddr, addr_len);
269 	release_sock(sk);
270 	return res;
271 }
272 EXPORT_SYMBOL_GPL(ip6_datagram_connect);
273 
274 int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr *uaddr,
275 				 int addr_len)
276 {
277 	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, uaddr);
278 	if (sin6->sin6_family != AF_INET6)
279 		return -EAFNOSUPPORT;
280 	return ip6_datagram_connect(sk, uaddr, addr_len);
281 }
282 EXPORT_SYMBOL_GPL(ip6_datagram_connect_v6_only);
283 
284 void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
285 		     __be16 port, u32 info, u8 *payload)
286 {
287 	struct ipv6_pinfo *np  = inet6_sk(sk);
288 	struct icmp6hdr *icmph = icmp6_hdr(skb);
289 	struct sock_exterr_skb *serr;
290 
291 	if (!np->recverr)
292 		return;
293 
294 	skb = skb_clone(skb, GFP_ATOMIC);
295 	if (!skb)
296 		return;
297 
298 	skb->protocol = htons(ETH_P_IPV6);
299 
300 	serr = SKB_EXT_ERR(skb);
301 	serr->ee.ee_errno = err;
302 	serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
303 	serr->ee.ee_type = icmph->icmp6_type;
304 	serr->ee.ee_code = icmph->icmp6_code;
305 	serr->ee.ee_pad = 0;
306 	serr->ee.ee_info = info;
307 	serr->ee.ee_data = 0;
308 	serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
309 				  skb_network_header(skb);
310 	serr->port = port;
311 
312 	__skb_pull(skb, payload - skb->data);
313 	skb_reset_transport_header(skb);
314 
315 	if (sock_queue_err_skb(sk, skb))
316 		kfree_skb(skb);
317 }
318 
319 void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info)
320 {
321 	const struct ipv6_pinfo *np = inet6_sk(sk);
322 	struct sock_exterr_skb *serr;
323 	struct ipv6hdr *iph;
324 	struct sk_buff *skb;
325 
326 	if (!np->recverr)
327 		return;
328 
329 	skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
330 	if (!skb)
331 		return;
332 
333 	skb->protocol = htons(ETH_P_IPV6);
334 
335 	skb_put(skb, sizeof(struct ipv6hdr));
336 	skb_reset_network_header(skb);
337 	iph = ipv6_hdr(skb);
338 	iph->daddr = fl6->daddr;
339 
340 	serr = SKB_EXT_ERR(skb);
341 	serr->ee.ee_errno = err;
342 	serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
343 	serr->ee.ee_type = 0;
344 	serr->ee.ee_code = 0;
345 	serr->ee.ee_pad = 0;
346 	serr->ee.ee_info = info;
347 	serr->ee.ee_data = 0;
348 	serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
349 	serr->port = fl6->fl6_dport;
350 
351 	__skb_pull(skb, skb_tail_pointer(skb) - skb->data);
352 	skb_reset_transport_header(skb);
353 
354 	if (sock_queue_err_skb(sk, skb))
355 		kfree_skb(skb);
356 }
357 
358 void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu)
359 {
360 	struct ipv6_pinfo *np = inet6_sk(sk);
361 	struct ipv6hdr *iph;
362 	struct sk_buff *skb;
363 	struct ip6_mtuinfo *mtu_info;
364 
365 	if (!np->rxopt.bits.rxpmtu)
366 		return;
367 
368 	skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
369 	if (!skb)
370 		return;
371 
372 	skb_put(skb, sizeof(struct ipv6hdr));
373 	skb_reset_network_header(skb);
374 	iph = ipv6_hdr(skb);
375 	iph->daddr = fl6->daddr;
376 
377 	mtu_info = IP6CBMTU(skb);
378 
379 	mtu_info->ip6m_mtu = mtu;
380 	mtu_info->ip6m_addr.sin6_family = AF_INET6;
381 	mtu_info->ip6m_addr.sin6_port = 0;
382 	mtu_info->ip6m_addr.sin6_flowinfo = 0;
383 	mtu_info->ip6m_addr.sin6_scope_id = fl6->flowi6_oif;
384 	mtu_info->ip6m_addr.sin6_addr = ipv6_hdr(skb)->daddr;
385 
386 	__skb_pull(skb, skb_tail_pointer(skb) - skb->data);
387 	skb_reset_transport_header(skb);
388 
389 	skb = xchg(&np->rxpmtu, skb);
390 	kfree_skb(skb);
391 }
392 
393 /* For some errors we have valid addr_offset even with zero payload and
394  * zero port. Also, addr_offset should be supported if port is set.
395  */
396 static inline bool ipv6_datagram_support_addr(struct sock_exterr_skb *serr)
397 {
398 	return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6 ||
399 	       serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
400 	       serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
401 }
402 
403 /* IPv6 supports cmsg on all origins aside from SO_EE_ORIGIN_LOCAL.
404  *
405  * At one point, excluding local errors was a quick test to identify icmp/icmp6
406  * errors. This is no longer true, but the test remained, so the v6 stack,
407  * unlike v4, also honors cmsg requests on all wifi and timestamp errors.
408  */
409 static bool ip6_datagram_support_cmsg(struct sk_buff *skb,
410 				      struct sock_exterr_skb *serr)
411 {
412 	if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
413 	    serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6)
414 		return true;
415 
416 	if (serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL)
417 		return false;
418 
419 	if (!IP6CB(skb)->iif)
420 		return false;
421 
422 	return true;
423 }
424 
425 /*
426  *	Handle MSG_ERRQUEUE
427  */
428 int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
429 {
430 	struct ipv6_pinfo *np = inet6_sk(sk);
431 	struct sock_exterr_skb *serr;
432 	struct sk_buff *skb;
433 	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
434 	struct {
435 		struct sock_extended_err ee;
436 		struct sockaddr_in6	 offender;
437 	} errhdr;
438 	int err;
439 	int copied;
440 
441 	err = -EAGAIN;
442 	skb = sock_dequeue_err_skb(sk);
443 	if (!skb)
444 		goto out;
445 
446 	copied = skb->len;
447 	if (copied > len) {
448 		msg->msg_flags |= MSG_TRUNC;
449 		copied = len;
450 	}
451 	err = skb_copy_datagram_msg(skb, 0, msg, copied);
452 	if (unlikely(err)) {
453 		kfree_skb(skb);
454 		return err;
455 	}
456 	sock_recv_timestamp(msg, sk, skb);
457 
458 	serr = SKB_EXT_ERR(skb);
459 
460 	if (sin && ipv6_datagram_support_addr(serr)) {
461 		const unsigned char *nh = skb_network_header(skb);
462 		sin->sin6_family = AF_INET6;
463 		sin->sin6_flowinfo = 0;
464 		sin->sin6_port = serr->port;
465 		if (skb->protocol == htons(ETH_P_IPV6)) {
466 			const struct ipv6hdr *ip6h = container_of((struct in6_addr *)(nh + serr->addr_offset),
467 								  struct ipv6hdr, daddr);
468 			sin->sin6_addr = ip6h->daddr;
469 			if (np->sndflow)
470 				sin->sin6_flowinfo = ip6_flowinfo(ip6h);
471 			sin->sin6_scope_id =
472 				ipv6_iface_scope_id(&sin->sin6_addr,
473 						    IP6CB(skb)->iif);
474 		} else {
475 			ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset),
476 					       &sin->sin6_addr);
477 			sin->sin6_scope_id = 0;
478 		}
479 		*addr_len = sizeof(*sin);
480 	}
481 
482 	memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
483 	sin = &errhdr.offender;
484 	memset(sin, 0, sizeof(*sin));
485 
486 	if (ip6_datagram_support_cmsg(skb, serr)) {
487 		sin->sin6_family = AF_INET6;
488 		if (np->rxopt.all)
489 			ip6_datagram_recv_common_ctl(sk, msg, skb);
490 		if (skb->protocol == htons(ETH_P_IPV6)) {
491 			sin->sin6_addr = ipv6_hdr(skb)->saddr;
492 			if (np->rxopt.all)
493 				ip6_datagram_recv_specific_ctl(sk, msg, skb);
494 			sin->sin6_scope_id =
495 				ipv6_iface_scope_id(&sin->sin6_addr,
496 						    IP6CB(skb)->iif);
497 		} else {
498 			ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
499 					       &sin->sin6_addr);
500 			if (inet_sk(sk)->cmsg_flags)
501 				ip_cmsg_recv(msg, skb);
502 		}
503 	}
504 
505 	put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
506 
507 	/* Now we could try to dump offended packet options */
508 
509 	msg->msg_flags |= MSG_ERRQUEUE;
510 	err = copied;
511 
512 	consume_skb(skb);
513 out:
514 	return err;
515 }
516 EXPORT_SYMBOL_GPL(ipv6_recv_error);
517 
518 /*
519  *	Handle IPV6_RECVPATHMTU
520  */
521 int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len,
522 		     int *addr_len)
523 {
524 	struct ipv6_pinfo *np = inet6_sk(sk);
525 	struct sk_buff *skb;
526 	struct ip6_mtuinfo mtu_info;
527 	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
528 	int err;
529 	int copied;
530 
531 	err = -EAGAIN;
532 	skb = xchg(&np->rxpmtu, NULL);
533 	if (!skb)
534 		goto out;
535 
536 	copied = skb->len;
537 	if (copied > len) {
538 		msg->msg_flags |= MSG_TRUNC;
539 		copied = len;
540 	}
541 	err = skb_copy_datagram_msg(skb, 0, msg, copied);
542 	if (err)
543 		goto out_free_skb;
544 
545 	sock_recv_timestamp(msg, sk, skb);
546 
547 	memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info));
548 
549 	if (sin) {
550 		sin->sin6_family = AF_INET6;
551 		sin->sin6_flowinfo = 0;
552 		sin->sin6_port = 0;
553 		sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id;
554 		sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr;
555 		*addr_len = sizeof(*sin);
556 	}
557 
558 	put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info);
559 
560 	err = copied;
561 
562 out_free_skb:
563 	kfree_skb(skb);
564 out:
565 	return err;
566 }
567 
568 
569 void ip6_datagram_recv_common_ctl(struct sock *sk, struct msghdr *msg,
570 				 struct sk_buff *skb)
571 {
572 	struct ipv6_pinfo *np = inet6_sk(sk);
573 	bool is_ipv6 = skb->protocol == htons(ETH_P_IPV6);
574 
575 	if (np->rxopt.bits.rxinfo) {
576 		struct in6_pktinfo src_info;
577 
578 		if (is_ipv6) {
579 			src_info.ipi6_ifindex = IP6CB(skb)->iif;
580 			src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
581 		} else {
582 			src_info.ipi6_ifindex =
583 				PKTINFO_SKB_CB(skb)->ipi_ifindex;
584 			ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
585 					       &src_info.ipi6_addr);
586 		}
587 
588 		if (src_info.ipi6_ifindex >= 0)
589 			put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO,
590 				 sizeof(src_info), &src_info);
591 	}
592 }
593 
594 void ip6_datagram_recv_specific_ctl(struct sock *sk, struct msghdr *msg,
595 				    struct sk_buff *skb)
596 {
597 	struct ipv6_pinfo *np = inet6_sk(sk);
598 	struct inet6_skb_parm *opt = IP6CB(skb);
599 	unsigned char *nh = skb_network_header(skb);
600 
601 	if (np->rxopt.bits.rxhlim) {
602 		int hlim = ipv6_hdr(skb)->hop_limit;
603 		put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
604 	}
605 
606 	if (np->rxopt.bits.rxtclass) {
607 		int tclass = ipv6_get_dsfield(ipv6_hdr(skb));
608 		put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
609 	}
610 
611 	if (np->rxopt.bits.rxflow) {
612 		__be32 flowinfo = ip6_flowinfo((struct ipv6hdr *)nh);
613 		if (flowinfo)
614 			put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
615 	}
616 
617 	/* HbH is allowed only once */
618 	if (np->rxopt.bits.hopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
619 		u8 *ptr = nh + sizeof(struct ipv6hdr);
620 		put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
621 	}
622 
623 	if (opt->lastopt &&
624 	    (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
625 		/*
626 		 * Silly enough, but we need to reparse in order to
627 		 * report extension headers (except for HbH)
628 		 * in order.
629 		 *
630 		 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
631 		 * (and WILL NOT be) defined because
632 		 * IPV6_RECVDSTOPTS is more generic. --yoshfuji
633 		 */
634 		unsigned int off = sizeof(struct ipv6hdr);
635 		u8 nexthdr = ipv6_hdr(skb)->nexthdr;
636 
637 		while (off <= opt->lastopt) {
638 			unsigned int len;
639 			u8 *ptr = nh + off;
640 
641 			switch (nexthdr) {
642 			case IPPROTO_DSTOPTS:
643 				nexthdr = ptr[0];
644 				len = (ptr[1] + 1) << 3;
645 				if (np->rxopt.bits.dstopts)
646 					put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
647 				break;
648 			case IPPROTO_ROUTING:
649 				nexthdr = ptr[0];
650 				len = (ptr[1] + 1) << 3;
651 				if (np->rxopt.bits.srcrt)
652 					put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
653 				break;
654 			case IPPROTO_AH:
655 				nexthdr = ptr[0];
656 				len = (ptr[1] + 2) << 2;
657 				break;
658 			default:
659 				nexthdr = ptr[0];
660 				len = (ptr[1] + 1) << 3;
661 				break;
662 			}
663 
664 			off += len;
665 		}
666 	}
667 
668 	/* socket options in old style */
669 	if (np->rxopt.bits.rxoinfo) {
670 		struct in6_pktinfo src_info;
671 
672 		src_info.ipi6_ifindex = opt->iif;
673 		src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
674 		put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
675 	}
676 	if (np->rxopt.bits.rxohlim) {
677 		int hlim = ipv6_hdr(skb)->hop_limit;
678 		put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
679 	}
680 	if (np->rxopt.bits.ohopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
681 		u8 *ptr = nh + sizeof(struct ipv6hdr);
682 		put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
683 	}
684 	if (np->rxopt.bits.odstopts && opt->dst0) {
685 		u8 *ptr = nh + opt->dst0;
686 		put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
687 	}
688 	if (np->rxopt.bits.osrcrt && opt->srcrt) {
689 		struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
690 		put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
691 	}
692 	if (np->rxopt.bits.odstopts && opt->dst1) {
693 		u8 *ptr = nh + opt->dst1;
694 		put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
695 	}
696 	if (np->rxopt.bits.rxorigdstaddr) {
697 		struct sockaddr_in6 sin6;
698 		__be16 *ports = (__be16 *) skb_transport_header(skb);
699 
700 		if (skb_transport_offset(skb) + 4 <= (int)skb->len) {
701 			/* All current transport protocols have the port numbers in the
702 			 * first four bytes of the transport header and this function is
703 			 * written with this assumption in mind.
704 			 */
705 
706 			sin6.sin6_family = AF_INET6;
707 			sin6.sin6_addr = ipv6_hdr(skb)->daddr;
708 			sin6.sin6_port = ports[1];
709 			sin6.sin6_flowinfo = 0;
710 			sin6.sin6_scope_id =
711 				ipv6_iface_scope_id(&ipv6_hdr(skb)->daddr,
712 						    opt->iif);
713 
714 			put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
715 		}
716 	}
717 	if (np->rxopt.bits.recvfragsize && opt->frag_max_size) {
718 		int val = opt->frag_max_size;
719 
720 		put_cmsg(msg, SOL_IPV6, IPV6_RECVFRAGSIZE, sizeof(val), &val);
721 	}
722 }
723 
724 void ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
725 			  struct sk_buff *skb)
726 {
727 	ip6_datagram_recv_common_ctl(sk, msg, skb);
728 	ip6_datagram_recv_specific_ctl(sk, msg, skb);
729 }
730 EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl);
731 
732 int ip6_datagram_send_ctl(struct net *net, struct sock *sk,
733 			  struct msghdr *msg, struct flowi6 *fl6,
734 			  struct ipcm6_cookie *ipc6, struct sockcm_cookie *sockc)
735 {
736 	struct in6_pktinfo *src_info;
737 	struct cmsghdr *cmsg;
738 	struct ipv6_rt_hdr *rthdr;
739 	struct ipv6_opt_hdr *hdr;
740 	struct ipv6_txoptions *opt = ipc6->opt;
741 	int len;
742 	int err = 0;
743 
744 	for_each_cmsghdr(cmsg, msg) {
745 		int addr_type;
746 
747 		if (!CMSG_OK(msg, cmsg)) {
748 			err = -EINVAL;
749 			goto exit_f;
750 		}
751 
752 		if (cmsg->cmsg_level == SOL_SOCKET) {
753 			err = __sock_cmsg_send(sk, msg, cmsg, sockc);
754 			if (err)
755 				return err;
756 			continue;
757 		}
758 
759 		if (cmsg->cmsg_level != SOL_IPV6)
760 			continue;
761 
762 		switch (cmsg->cmsg_type) {
763 		case IPV6_PKTINFO:
764 		case IPV6_2292PKTINFO:
765 		    {
766 			struct net_device *dev = NULL;
767 
768 			if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
769 				err = -EINVAL;
770 				goto exit_f;
771 			}
772 
773 			src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
774 
775 			if (src_info->ipi6_ifindex) {
776 				if (fl6->flowi6_oif &&
777 				    src_info->ipi6_ifindex != fl6->flowi6_oif)
778 					return -EINVAL;
779 				fl6->flowi6_oif = src_info->ipi6_ifindex;
780 			}
781 
782 			addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
783 
784 			rcu_read_lock();
785 			if (fl6->flowi6_oif) {
786 				dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
787 				if (!dev) {
788 					rcu_read_unlock();
789 					return -ENODEV;
790 				}
791 			} else if (addr_type & IPV6_ADDR_LINKLOCAL) {
792 				rcu_read_unlock();
793 				return -EINVAL;
794 			}
795 
796 			if (addr_type != IPV6_ADDR_ANY) {
797 				int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
798 				if (!(inet_sk(sk)->freebind || inet_sk(sk)->transparent) &&
799 				    !ipv6_chk_addr(net, &src_info->ipi6_addr,
800 						   strict ? dev : NULL, 0) &&
801 				    !ipv6_chk_acast_addr_src(net, dev,
802 							     &src_info->ipi6_addr))
803 					err = -EINVAL;
804 				else
805 					fl6->saddr = src_info->ipi6_addr;
806 			}
807 
808 			rcu_read_unlock();
809 
810 			if (err)
811 				goto exit_f;
812 
813 			break;
814 		    }
815 
816 		case IPV6_FLOWINFO:
817 			if (cmsg->cmsg_len < CMSG_LEN(4)) {
818 				err = -EINVAL;
819 				goto exit_f;
820 			}
821 
822 			if (fl6->flowlabel&IPV6_FLOWINFO_MASK) {
823 				if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
824 					err = -EINVAL;
825 					goto exit_f;
826 				}
827 			}
828 			fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
829 			break;
830 
831 		case IPV6_2292HOPOPTS:
832 		case IPV6_HOPOPTS:
833 			if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
834 				err = -EINVAL;
835 				goto exit_f;
836 			}
837 
838 			hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
839 			len = ((hdr->hdrlen + 1) << 3);
840 			if (cmsg->cmsg_len < CMSG_LEN(len)) {
841 				err = -EINVAL;
842 				goto exit_f;
843 			}
844 			if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
845 				err = -EPERM;
846 				goto exit_f;
847 			}
848 			opt->opt_nflen += len;
849 			opt->hopopt = hdr;
850 			break;
851 
852 		case IPV6_2292DSTOPTS:
853 			if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
854 				err = -EINVAL;
855 				goto exit_f;
856 			}
857 
858 			hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
859 			len = ((hdr->hdrlen + 1) << 3);
860 			if (cmsg->cmsg_len < CMSG_LEN(len)) {
861 				err = -EINVAL;
862 				goto exit_f;
863 			}
864 			if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
865 				err = -EPERM;
866 				goto exit_f;
867 			}
868 			if (opt->dst1opt) {
869 				err = -EINVAL;
870 				goto exit_f;
871 			}
872 			opt->opt_flen += len;
873 			opt->dst1opt = hdr;
874 			break;
875 
876 		case IPV6_DSTOPTS:
877 		case IPV6_RTHDRDSTOPTS:
878 			if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
879 				err = -EINVAL;
880 				goto exit_f;
881 			}
882 
883 			hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
884 			len = ((hdr->hdrlen + 1) << 3);
885 			if (cmsg->cmsg_len < CMSG_LEN(len)) {
886 				err = -EINVAL;
887 				goto exit_f;
888 			}
889 			if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
890 				err = -EPERM;
891 				goto exit_f;
892 			}
893 			if (cmsg->cmsg_type == IPV6_DSTOPTS) {
894 				opt->opt_flen += len;
895 				opt->dst1opt = hdr;
896 			} else {
897 				opt->opt_nflen += len;
898 				opt->dst0opt = hdr;
899 			}
900 			break;
901 
902 		case IPV6_2292RTHDR:
903 		case IPV6_RTHDR:
904 			if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
905 				err = -EINVAL;
906 				goto exit_f;
907 			}
908 
909 			rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
910 
911 			switch (rthdr->type) {
912 #if IS_ENABLED(CONFIG_IPV6_MIP6)
913 			case IPV6_SRCRT_TYPE_2:
914 				if (rthdr->hdrlen != 2 ||
915 				    rthdr->segments_left != 1) {
916 					err = -EINVAL;
917 					goto exit_f;
918 				}
919 				break;
920 #endif
921 			default:
922 				err = -EINVAL;
923 				goto exit_f;
924 			}
925 
926 			len = ((rthdr->hdrlen + 1) << 3);
927 
928 			if (cmsg->cmsg_len < CMSG_LEN(len)) {
929 				err = -EINVAL;
930 				goto exit_f;
931 			}
932 
933 			/* segments left must also match */
934 			if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
935 				err = -EINVAL;
936 				goto exit_f;
937 			}
938 
939 			opt->opt_nflen += len;
940 			opt->srcrt = rthdr;
941 
942 			if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
943 				int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
944 
945 				opt->opt_nflen += dsthdrlen;
946 				opt->dst0opt = opt->dst1opt;
947 				opt->dst1opt = NULL;
948 				opt->opt_flen -= dsthdrlen;
949 			}
950 
951 			break;
952 
953 		case IPV6_2292HOPLIMIT:
954 		case IPV6_HOPLIMIT:
955 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
956 				err = -EINVAL;
957 				goto exit_f;
958 			}
959 
960 			ipc6->hlimit = *(int *)CMSG_DATA(cmsg);
961 			if (ipc6->hlimit < -1 || ipc6->hlimit > 0xff) {
962 				err = -EINVAL;
963 				goto exit_f;
964 			}
965 
966 			break;
967 
968 		case IPV6_TCLASS:
969 		    {
970 			int tc;
971 
972 			err = -EINVAL;
973 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
974 				goto exit_f;
975 
976 			tc = *(int *)CMSG_DATA(cmsg);
977 			if (tc < -1 || tc > 0xff)
978 				goto exit_f;
979 
980 			err = 0;
981 			ipc6->tclass = tc;
982 
983 			break;
984 		    }
985 
986 		case IPV6_DONTFRAG:
987 		    {
988 			int df;
989 
990 			err = -EINVAL;
991 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
992 				goto exit_f;
993 
994 			df = *(int *)CMSG_DATA(cmsg);
995 			if (df < 0 || df > 1)
996 				goto exit_f;
997 
998 			err = 0;
999 			ipc6->dontfrag = df;
1000 
1001 			break;
1002 		    }
1003 		default:
1004 			net_dbg_ratelimited("invalid cmsg type: %d\n",
1005 					    cmsg->cmsg_type);
1006 			err = -EINVAL;
1007 			goto exit_f;
1008 		}
1009 	}
1010 
1011 exit_f:
1012 	return err;
1013 }
1014 EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl);
1015 
1016 void ip6_dgram_sock_seq_show(struct seq_file *seq, struct sock *sp,
1017 			     __u16 srcp, __u16 destp, int bucket)
1018 {
1019 	const struct in6_addr *dest, *src;
1020 
1021 	dest  = &sp->sk_v6_daddr;
1022 	src   = &sp->sk_v6_rcv_saddr;
1023 	seq_printf(seq,
1024 		   "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1025 		   "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d\n",
1026 		   bucket,
1027 		   src->s6_addr32[0], src->s6_addr32[1],
1028 		   src->s6_addr32[2], src->s6_addr32[3], srcp,
1029 		   dest->s6_addr32[0], dest->s6_addr32[1],
1030 		   dest->s6_addr32[2], dest->s6_addr32[3], destp,
1031 		   sp->sk_state,
1032 		   sk_wmem_alloc_get(sp),
1033 		   sk_rmem_alloc_get(sp),
1034 		   0, 0L, 0,
1035 		   from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
1036 		   0,
1037 		   sock_i_ino(sp),
1038 		   atomic_read(&sp->sk_refcnt), sp,
1039 		   atomic_read(&sp->sk_drops));
1040 }
1041