xref: /linux/net/ipv6/datagram.c (revision dfecb0c5af3b07ebfa84be63a7a21bfc9e29a872)
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_uid(sk);
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 = READ_ONCE(np->mcast_oif);
64 		else
65 			oif = READ_ONCE(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 inet_sock *inet = inet_sk(sk);
76 	struct ipv6_pinfo *np = inet6_sk(sk);
77 	struct ipv6_txoptions *opt;
78 	struct in6_addr *final_p;
79 	struct dst_entry *dst;
80 	struct flowi6 *fl6;
81 	int err = 0;
82 
83 	if (inet6_test_bit(SNDFLOW, sk) &&
84 	    (np->flow_label & IPV6_FLOWLABEL_MASK)) {
85 		flowlabel = fl6_sock_lookup(sk, np->flow_label);
86 		if (IS_ERR(flowlabel))
87 			return -EINVAL;
88 	}
89 	fl6 = &inet_sk(sk)->cork.fl.u.ip6;
90 	ip6_datagram_flow_key_init(fl6, sk);
91 
92 	rcu_read_lock();
93 	opt = flowlabel ? flowlabel->opt : rcu_dereference(np->opt);
94 	final_p = fl6_update_dst(fl6, opt, &np->final);
95 	rcu_read_unlock();
96 
97 	dst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_p);
98 	if (IS_ERR(dst)) {
99 		err = PTR_ERR(dst);
100 		goto out;
101 	}
102 
103 	if (fix_sk_saddr) {
104 		if (ipv6_addr_any(&np->saddr))
105 			np->saddr = fl6->saddr;
106 
107 		if (ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
108 			sk->sk_v6_rcv_saddr = fl6->saddr;
109 			inet->inet_rcv_saddr = LOOPBACK4_IPV6;
110 			if (sk->sk_prot->rehash)
111 				sk->sk_prot->rehash(sk);
112 		}
113 	}
114 
115 	ip6_sk_dst_store_flow(sk, dst, fl6);
116 
117 out:
118 	fl6_sock_release(flowlabel);
119 	return err;
120 }
121 
122 void ip6_datagram_release_cb(struct sock *sk)
123 {
124 	struct dst_entry *dst;
125 
126 	if (ipv6_addr_v4mapped(&sk->sk_v6_daddr))
127 		return;
128 
129 	rcu_read_lock();
130 	dst = __sk_dst_get(sk);
131 	if (!dst || !READ_ONCE(dst->obsolete) ||
132 	    dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) {
133 		rcu_read_unlock();
134 		return;
135 	}
136 	rcu_read_unlock();
137 
138 	ip6_datagram_dst_update(sk, false);
139 }
140 EXPORT_SYMBOL_GPL(ip6_datagram_release_cb);
141 
142 int __ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
143 			   int addr_len)
144 {
145 	struct sockaddr_in6	*usin = (struct sockaddr_in6 *) uaddr;
146 	struct inet_sock	*inet = inet_sk(sk);
147 	struct ipv6_pinfo	*np = inet6_sk(sk);
148 	struct in6_addr		*daddr, old_daddr;
149 	__be32			fl6_flowlabel = 0;
150 	__be32			old_fl6_flowlabel;
151 	__be16			old_dport;
152 	int			addr_type;
153 	int			err;
154 
155 	if (usin->sin6_family == AF_INET) {
156 		if (ipv6_only_sock(sk))
157 			return -EAFNOSUPPORT;
158 		err = __ip4_datagram_connect(sk, uaddr, addr_len);
159 		goto ipv4_connected;
160 	}
161 
162 	if (addr_len < SIN6_LEN_RFC2133)
163 		return -EINVAL;
164 
165 	if (usin->sin6_family != AF_INET6)
166 		return -EAFNOSUPPORT;
167 
168 	if (inet6_test_bit(SNDFLOW, sk))
169 		fl6_flowlabel = usin->sin6_flowinfo & IPV6_FLOWINFO_MASK;
170 
171 	if (ipv6_addr_any(&usin->sin6_addr)) {
172 		/*
173 		 *	connect to self
174 		 */
175 		if (ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr))
176 			ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK),
177 					       &usin->sin6_addr);
178 		else
179 			usin->sin6_addr = in6addr_loopback;
180 	}
181 
182 	addr_type = ipv6_addr_type(&usin->sin6_addr);
183 
184 	daddr = &usin->sin6_addr;
185 
186 	if (addr_type & IPV6_ADDR_MAPPED) {
187 		struct sockaddr_in sin;
188 
189 		if (ipv6_only_sock(sk)) {
190 			err = -ENETUNREACH;
191 			goto out;
192 		}
193 		sin.sin_family = AF_INET;
194 		sin.sin_addr.s_addr = daddr->s6_addr32[3];
195 		sin.sin_port = usin->sin6_port;
196 
197 		err = __ip4_datagram_connect(sk,
198 					     (struct sockaddr_unsized *)&sin,
199 					     sizeof(sin));
200 
201 ipv4_connected:
202 		if (err)
203 			goto out;
204 
205 		ipv6_addr_set_v4mapped(inet->inet_daddr, &sk->sk_v6_daddr);
206 
207 		if (ipv6_addr_any(&np->saddr) ||
208 		    ipv6_mapped_addr_any(&np->saddr))
209 			ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
210 
211 		if (ipv6_addr_any(&sk->sk_v6_rcv_saddr) ||
212 		    ipv6_mapped_addr_any(&sk->sk_v6_rcv_saddr)) {
213 			ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
214 					       &sk->sk_v6_rcv_saddr);
215 			if (sk->sk_prot->rehash)
216 				sk->sk_prot->rehash(sk);
217 		}
218 
219 		goto out;
220 	}
221 
222 	if (__ipv6_addr_needs_scope_id(addr_type)) {
223 		if (addr_len >= sizeof(struct sockaddr_in6) &&
224 		    usin->sin6_scope_id) {
225 			if (!sk_dev_equal_l3scope(sk, usin->sin6_scope_id)) {
226 				err = -EINVAL;
227 				goto out;
228 			}
229 			WRITE_ONCE(sk->sk_bound_dev_if, usin->sin6_scope_id);
230 		}
231 
232 		if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST))
233 			WRITE_ONCE(sk->sk_bound_dev_if, READ_ONCE(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 	/* save the current peer information before updating it */
243 	old_daddr = sk->sk_v6_daddr;
244 	old_fl6_flowlabel = np->flow_label;
245 	old_dport = inet->inet_dport;
246 
247 	sk->sk_v6_daddr = *daddr;
248 	np->flow_label = fl6_flowlabel;
249 	inet->inet_dport = usin->sin6_port;
250 
251 	/*
252 	 *	Check for a route to destination an obtain the
253 	 *	destination cache for it.
254 	 */
255 
256 	err = ip6_datagram_dst_update(sk, true);
257 	if (err) {
258 		/* Restore the socket peer info, to keep it consistent with
259 		 * the old socket state
260 		 */
261 		sk->sk_v6_daddr = old_daddr;
262 		np->flow_label = old_fl6_flowlabel;
263 		inet->inet_dport = old_dport;
264 		goto out;
265 	}
266 
267 	reuseport_has_conns_set(sk);
268 	sk->sk_state = TCP_ESTABLISHED;
269 	sk_set_txhash(sk);
270 out:
271 	return err;
272 }
273 EXPORT_SYMBOL_GPL(__ip6_datagram_connect);
274 
275 int ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len)
276 {
277 	int res;
278 
279 	lock_sock(sk);
280 	res = __ip6_datagram_connect(sk, uaddr, addr_len);
281 	release_sock(sk);
282 	return res;
283 }
284 EXPORT_SYMBOL_GPL(ip6_datagram_connect);
285 
286 int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr_unsized *uaddr,
287 				 int addr_len)
288 {
289 	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, uaddr);
290 	if (sin6->sin6_family != AF_INET6)
291 		return -EAFNOSUPPORT;
292 	return ip6_datagram_connect(sk, uaddr, addr_len);
293 }
294 EXPORT_SYMBOL_GPL(ip6_datagram_connect_v6_only);
295 
296 static void ipv6_icmp_error_rfc4884(const struct sk_buff *skb,
297 				    struct sock_ee_data_rfc4884 *out)
298 {
299 	switch (icmp6_hdr(skb)->icmp6_type) {
300 	case ICMPV6_TIME_EXCEED:
301 	case ICMPV6_DEST_UNREACH:
302 		ip_icmp_error_rfc4884(skb, out, sizeof(struct icmp6hdr),
303 				      icmp6_hdr(skb)->icmp6_datagram_len * 8);
304 	}
305 }
306 
307 void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
308 		     __be16 port, u32 info, u8 *payload)
309 {
310 	struct icmp6hdr *icmph = icmp6_hdr(skb);
311 	struct sock_exterr_skb *serr;
312 
313 	if (!inet6_test_bit(RECVERR6, sk))
314 		return;
315 
316 	skb = skb_clone(skb, GFP_ATOMIC);
317 	if (!skb)
318 		return;
319 
320 	skb->protocol = htons(ETH_P_IPV6);
321 
322 	serr = SKB_EXT_ERR(skb);
323 	serr->ee.ee_errno = err;
324 	serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
325 	serr->ee.ee_type = icmph->icmp6_type;
326 	serr->ee.ee_code = icmph->icmp6_code;
327 	serr->ee.ee_pad = 0;
328 	serr->ee.ee_info = info;
329 	serr->ee.ee_data = 0;
330 	serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
331 				  skb_network_header(skb);
332 	serr->port = port;
333 
334 	__skb_pull(skb, payload - skb->data);
335 
336 	if (inet6_test_bit(RECVERR6_RFC4884, sk))
337 		ipv6_icmp_error_rfc4884(skb, &serr->ee.ee_rfc4884);
338 
339 	skb_reset_transport_header(skb);
340 
341 	if (sock_queue_err_skb(sk, skb))
342 		kfree_skb(skb);
343 }
344 EXPORT_SYMBOL_GPL(ipv6_icmp_error);
345 
346 void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info)
347 {
348 	struct sock_exterr_skb *serr;
349 	struct ipv6hdr *iph;
350 	struct sk_buff *skb;
351 
352 	if (!inet6_test_bit(RECVERR6, sk))
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)
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 (inet6_test_bit(SNDFLOW, sk))
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 		msg->msg_namelen = 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 {
550 	struct ipv6_pinfo *np = inet6_sk(sk);
551 	struct sk_buff *skb;
552 	struct ip6_mtuinfo mtu_info;
553 	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
554 	int err;
555 	int copied;
556 
557 	err = -EAGAIN;
558 	skb = xchg(&np->rxpmtu, NULL);
559 	if (!skb)
560 		goto out;
561 
562 	copied = skb->len;
563 	if (copied > len) {
564 		msg->msg_flags |= MSG_TRUNC;
565 		copied = len;
566 	}
567 	err = skb_copy_datagram_msg(skb, 0, msg, copied);
568 	if (err)
569 		goto out_free_skb;
570 
571 	sock_recv_timestamp(msg, sk, skb);
572 
573 	memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info));
574 
575 	if (sin) {
576 		sin->sin6_family = AF_INET6;
577 		sin->sin6_flowinfo = 0;
578 		sin->sin6_port = 0;
579 		sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id;
580 		sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr;
581 		msg->msg_namelen = sizeof(*sin);
582 	}
583 
584 	put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info);
585 
586 	err = copied;
587 
588 out_free_skb:
589 	kfree_skb(skb);
590 out:
591 	return err;
592 }
593 
594 
595 void ip6_datagram_recv_common_ctl(struct sock *sk, struct msghdr *msg,
596 				 struct sk_buff *skb)
597 {
598 	struct ipv6_pinfo *np = inet6_sk(sk);
599 	bool is_ipv6 = skb->protocol == htons(ETH_P_IPV6);
600 
601 	if (np->rxopt.bits.rxinfo) {
602 		struct in6_pktinfo src_info;
603 
604 		if (is_ipv6) {
605 			src_info.ipi6_ifindex = IP6CB(skb)->iif;
606 			src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
607 		} else {
608 			src_info.ipi6_ifindex =
609 				PKTINFO_SKB_CB(skb)->ipi_ifindex;
610 			ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
611 					       &src_info.ipi6_addr);
612 		}
613 
614 		if (src_info.ipi6_ifindex >= 0)
615 			put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO,
616 				 sizeof(src_info), &src_info);
617 	}
618 }
619 
620 void ip6_datagram_recv_specific_ctl(struct sock *sk, struct msghdr *msg,
621 				    struct sk_buff *skb)
622 {
623 	struct ipv6_pinfo *np = inet6_sk(sk);
624 	struct inet6_skb_parm *opt = IP6CB(skb);
625 	unsigned char *nh = skb_network_header(skb);
626 
627 	if (np->rxopt.bits.rxhlim) {
628 		int hlim = ipv6_hdr(skb)->hop_limit;
629 		put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
630 	}
631 
632 	if (np->rxopt.bits.rxtclass) {
633 		int tclass = ipv6_get_dsfield(ipv6_hdr(skb));
634 		put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
635 	}
636 
637 	if (np->rxopt.bits.rxflow) {
638 		__be32 flowinfo = ip6_flowinfo((struct ipv6hdr *)nh);
639 		if (flowinfo)
640 			put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
641 	}
642 
643 	/* HbH is allowed only once */
644 	if (np->rxopt.bits.hopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
645 		u8 *ptr = nh + sizeof(struct ipv6hdr);
646 		put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
647 	}
648 
649 	if (opt->lastopt &&
650 	    (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
651 		/*
652 		 * Silly enough, but we need to reparse in order to
653 		 * report extension headers (except for HbH)
654 		 * in order.
655 		 *
656 		 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
657 		 * (and WILL NOT be) defined because
658 		 * IPV6_RECVDSTOPTS is more generic. --yoshfuji
659 		 */
660 		unsigned int off = sizeof(struct ipv6hdr);
661 		u8 nexthdr = ipv6_hdr(skb)->nexthdr;
662 
663 		while (off <= opt->lastopt) {
664 			unsigned int len;
665 			u8 *ptr = nh + off;
666 
667 			switch (nexthdr) {
668 			case IPPROTO_DSTOPTS:
669 				nexthdr = ptr[0];
670 				len = (ptr[1] + 1) << 3;
671 				if (np->rxopt.bits.dstopts)
672 					put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
673 				break;
674 			case IPPROTO_ROUTING:
675 				nexthdr = ptr[0];
676 				len = (ptr[1] + 1) << 3;
677 				if (np->rxopt.bits.srcrt)
678 					put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
679 				break;
680 			case IPPROTO_AH:
681 				nexthdr = ptr[0];
682 				len = (ptr[1] + 2) << 2;
683 				break;
684 			default:
685 				nexthdr = ptr[0];
686 				len = (ptr[1] + 1) << 3;
687 				break;
688 			}
689 
690 			off += len;
691 		}
692 	}
693 
694 	/* socket options in old style */
695 	if (np->rxopt.bits.rxoinfo) {
696 		struct in6_pktinfo src_info;
697 
698 		src_info.ipi6_ifindex = opt->iif;
699 		src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
700 		put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
701 	}
702 	if (np->rxopt.bits.rxohlim) {
703 		int hlim = ipv6_hdr(skb)->hop_limit;
704 		put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
705 	}
706 	if (np->rxopt.bits.ohopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
707 		u8 *ptr = nh + sizeof(struct ipv6hdr);
708 		put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
709 	}
710 	if (np->rxopt.bits.odstopts && opt->dst0) {
711 		u8 *ptr = nh + opt->dst0;
712 		put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
713 	}
714 	if (np->rxopt.bits.osrcrt && opt->srcrt) {
715 		struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
716 		put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
717 	}
718 	if (np->rxopt.bits.odstopts && opt->dst1) {
719 		u8 *ptr = nh + opt->dst1;
720 		put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
721 	}
722 	if (np->rxopt.bits.rxorigdstaddr) {
723 		struct sockaddr_in6 sin6;
724 		__be16 _ports[2], *ports;
725 
726 		ports = skb_header_pointer(skb, skb_transport_offset(skb),
727 					   sizeof(_ports), &_ports);
728 		if (ports) {
729 			/* All current transport protocols have the port numbers in the
730 			 * first four bytes of the transport header and this function is
731 			 * written with this assumption in mind.
732 			 */
733 			sin6.sin6_family = AF_INET6;
734 			sin6.sin6_addr = ipv6_hdr(skb)->daddr;
735 			sin6.sin6_port = ports[1];
736 			sin6.sin6_flowinfo = 0;
737 			sin6.sin6_scope_id =
738 				ipv6_iface_scope_id(&ipv6_hdr(skb)->daddr,
739 						    opt->iif);
740 
741 			put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
742 		}
743 	}
744 	if (np->rxopt.bits.recvfragsize && opt->frag_max_size) {
745 		int val = opt->frag_max_size;
746 
747 		put_cmsg(msg, SOL_IPV6, IPV6_RECVFRAGSIZE, sizeof(val), &val);
748 	}
749 }
750 
751 void ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
752 			  struct sk_buff *skb)
753 {
754 	ip6_datagram_recv_common_ctl(sk, msg, skb);
755 	ip6_datagram_recv_specific_ctl(sk, msg, skb);
756 }
757 EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl);
758 
759 int ip6_datagram_send_ctl(struct net *net, struct sock *sk,
760 			  struct msghdr *msg, struct flowi6 *fl6,
761 			  struct ipcm6_cookie *ipc6)
762 {
763 	struct in6_pktinfo *src_info;
764 	struct cmsghdr *cmsg;
765 	struct ipv6_rt_hdr *orthdr;
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 				if (opt->dst1opt)
928 					opt->opt_flen -= ipv6_optlen(opt->dst1opt);
929 				opt->opt_flen += len;
930 				opt->dst1opt = hdr;
931 			} else {
932 				if (opt->dst0opt)
933 					opt->opt_nflen -= ipv6_optlen(opt->dst0opt);
934 				opt->opt_nflen += len;
935 				opt->dst0opt = hdr;
936 			}
937 			break;
938 
939 		case IPV6_2292RTHDR:
940 		case IPV6_RTHDR:
941 			if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
942 				err = -EINVAL;
943 				goto exit_f;
944 			}
945 
946 			rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
947 
948 			switch (rthdr->type) {
949 #if IS_ENABLED(CONFIG_IPV6_MIP6)
950 			case IPV6_SRCRT_TYPE_2:
951 				if (rthdr->hdrlen != 2 ||
952 				    rthdr->segments_left != 1) {
953 					err = -EINVAL;
954 					goto exit_f;
955 				}
956 				break;
957 #endif
958 			default:
959 				err = -EINVAL;
960 				goto exit_f;
961 			}
962 
963 			len = ((rthdr->hdrlen + 1) << 3);
964 
965 			if (cmsg->cmsg_len < CMSG_LEN(len)) {
966 				err = -EINVAL;
967 				goto exit_f;
968 			}
969 
970 			/* segments left must also match */
971 			if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
972 				err = -EINVAL;
973 				goto exit_f;
974 			}
975 
976 			orthdr = opt->srcrt;
977 			if (orthdr)
978 				opt->opt_nflen -= ((orthdr->hdrlen + 1) << 3);
979 			opt->opt_nflen += len;
980 			opt->srcrt = rthdr;
981 
982 			if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
983 				int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
984 
985 				if (opt->dst0opt)
986 					opt->opt_nflen -= ipv6_optlen(opt->dst0opt);
987 				opt->opt_nflen += dsthdrlen;
988 				opt->dst0opt = opt->dst1opt;
989 				opt->dst1opt = NULL;
990 				opt->opt_flen -= dsthdrlen;
991 			}
992 
993 			break;
994 
995 		case IPV6_2292HOPLIMIT:
996 		case IPV6_HOPLIMIT:
997 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
998 				err = -EINVAL;
999 				goto exit_f;
1000 			}
1001 
1002 			ipc6->hlimit = *(int *)CMSG_DATA(cmsg);
1003 			if (ipc6->hlimit < -1 || ipc6->hlimit > 0xff) {
1004 				err = -EINVAL;
1005 				goto exit_f;
1006 			}
1007 
1008 			break;
1009 
1010 		case IPV6_TCLASS:
1011 		    {
1012 			int tc;
1013 
1014 			err = -EINVAL;
1015 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
1016 				goto exit_f;
1017 
1018 			tc = *(int *)CMSG_DATA(cmsg);
1019 			if (tc < -1 || tc > 0xff)
1020 				goto exit_f;
1021 
1022 			err = 0;
1023 			ipc6->tclass = tc;
1024 
1025 			break;
1026 		    }
1027 
1028 		case IPV6_DONTFRAG:
1029 		    {
1030 			int df;
1031 
1032 			err = -EINVAL;
1033 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
1034 				goto exit_f;
1035 
1036 			df = *(int *)CMSG_DATA(cmsg);
1037 			if (df < 0 || df > 1)
1038 				goto exit_f;
1039 
1040 			err = 0;
1041 			ipc6->dontfrag = df;
1042 
1043 			break;
1044 		    }
1045 		default:
1046 			net_dbg_ratelimited("invalid cmsg type: %d\n",
1047 					    cmsg->cmsg_type);
1048 			err = -EINVAL;
1049 			goto exit_f;
1050 		}
1051 	}
1052 
1053 exit_f:
1054 	return err;
1055 }
1056 EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl);
1057 
1058 void __ip6_dgram_sock_seq_show(struct seq_file *seq, struct sock *sp,
1059 			       __u16 srcp, __u16 destp, int rqueue, int bucket)
1060 {
1061 	const struct in6_addr *dest, *src;
1062 
1063 	dest  = &sp->sk_v6_daddr;
1064 	src   = &sp->sk_v6_rcv_saddr;
1065 	seq_printf(seq,
1066 		   "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1067 		   "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %u\n",
1068 		   bucket,
1069 		   src->s6_addr32[0], src->s6_addr32[1],
1070 		   src->s6_addr32[2], src->s6_addr32[3], srcp,
1071 		   dest->s6_addr32[0], dest->s6_addr32[1],
1072 		   dest->s6_addr32[2], dest->s6_addr32[3], destp,
1073 		   sp->sk_state,
1074 		   sk_wmem_alloc_get(sp),
1075 		   rqueue,
1076 		   0, 0L, 0,
1077 		   from_kuid_munged(seq_user_ns(seq), sk_uid(sp)),
1078 		   0,
1079 		   sock_i_ino(sp),
1080 		   refcount_read(&sp->sk_refcnt), sp,
1081 		   sk_drops_read(sp));
1082 }
1083