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