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