xref: /openbmc/linux/net/ipv6/datagram.c (revision bc000245)
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 <asm/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 int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
44 {
45 	struct sockaddr_in6	*usin = (struct sockaddr_in6 *) uaddr;
46 	struct inet_sock      	*inet = inet_sk(sk);
47 	struct ipv6_pinfo      	*np = inet6_sk(sk);
48 	struct in6_addr		*daddr, *final_p, final;
49 	struct dst_entry	*dst;
50 	struct flowi6		fl6;
51 	struct ip6_flowlabel	*flowlabel = NULL;
52 	struct ipv6_txoptions   *opt;
53 	int			addr_type;
54 	int			err;
55 
56 	if (usin->sin6_family == AF_INET) {
57 		if (__ipv6_only_sock(sk))
58 			return -EAFNOSUPPORT;
59 		err = ip4_datagram_connect(sk, uaddr, addr_len);
60 		goto ipv4_connected;
61 	}
62 
63 	if (addr_len < SIN6_LEN_RFC2133)
64 		return -EINVAL;
65 
66 	if (usin->sin6_family != AF_INET6)
67 		return -EAFNOSUPPORT;
68 
69 	memset(&fl6, 0, sizeof(fl6));
70 	if (np->sndflow) {
71 		fl6.flowlabel = usin->sin6_flowinfo&IPV6_FLOWINFO_MASK;
72 		if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
73 			flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
74 			if (flowlabel == NULL)
75 				return -EINVAL;
76 		}
77 	}
78 
79 	addr_type = ipv6_addr_type(&usin->sin6_addr);
80 
81 	if (addr_type == IPV6_ADDR_ANY) {
82 		/*
83 		 *	connect to self
84 		 */
85 		usin->sin6_addr.s6_addr[15] = 0x01;
86 	}
87 
88 	daddr = &usin->sin6_addr;
89 
90 	if (addr_type == IPV6_ADDR_MAPPED) {
91 		struct sockaddr_in sin;
92 
93 		if (__ipv6_only_sock(sk)) {
94 			err = -ENETUNREACH;
95 			goto out;
96 		}
97 		sin.sin_family = AF_INET;
98 		sin.sin_addr.s_addr = daddr->s6_addr32[3];
99 		sin.sin_port = usin->sin6_port;
100 
101 		err = ip4_datagram_connect(sk,
102 					   (struct sockaddr *) &sin,
103 					   sizeof(sin));
104 
105 ipv4_connected:
106 		if (err)
107 			goto out;
108 
109 		ipv6_addr_set_v4mapped(inet->inet_daddr, &sk->sk_v6_daddr);
110 
111 		if (ipv6_addr_any(&np->saddr) ||
112 		    ipv6_mapped_addr_any(&np->saddr))
113 			ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
114 
115 		if (ipv6_addr_any(&sk->sk_v6_rcv_saddr) ||
116 		    ipv6_mapped_addr_any(&sk->sk_v6_rcv_saddr)) {
117 			ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
118 					       &sk->sk_v6_rcv_saddr);
119 			if (sk->sk_prot->rehash)
120 				sk->sk_prot->rehash(sk);
121 		}
122 
123 		goto out;
124 	}
125 
126 	if (__ipv6_addr_needs_scope_id(addr_type)) {
127 		if (addr_len >= sizeof(struct sockaddr_in6) &&
128 		    usin->sin6_scope_id) {
129 			if (sk->sk_bound_dev_if &&
130 			    sk->sk_bound_dev_if != usin->sin6_scope_id) {
131 				err = -EINVAL;
132 				goto out;
133 			}
134 			sk->sk_bound_dev_if = usin->sin6_scope_id;
135 		}
136 
137 		if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST))
138 			sk->sk_bound_dev_if = np->mcast_oif;
139 
140 		/* Connect to link-local address requires an interface */
141 		if (!sk->sk_bound_dev_if) {
142 			err = -EINVAL;
143 			goto out;
144 		}
145 	}
146 
147 	sk->sk_v6_daddr = *daddr;
148 	np->flow_label = fl6.flowlabel;
149 
150 	inet->inet_dport = usin->sin6_port;
151 
152 	/*
153 	 *	Check for a route to destination an obtain the
154 	 *	destination cache for it.
155 	 */
156 
157 	fl6.flowi6_proto = sk->sk_protocol;
158 	fl6.daddr = sk->sk_v6_daddr;
159 	fl6.saddr = np->saddr;
160 	fl6.flowi6_oif = sk->sk_bound_dev_if;
161 	fl6.flowi6_mark = sk->sk_mark;
162 	fl6.fl6_dport = inet->inet_dport;
163 	fl6.fl6_sport = inet->inet_sport;
164 
165 	if (!fl6.flowi6_oif && (addr_type&IPV6_ADDR_MULTICAST))
166 		fl6.flowi6_oif = np->mcast_oif;
167 
168 	security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
169 
170 	opt = flowlabel ? flowlabel->opt : np->opt;
171 	final_p = fl6_update_dst(&fl6, opt, &final);
172 
173 	dst = ip6_dst_lookup_flow(sk, &fl6, final_p, true);
174 	err = 0;
175 	if (IS_ERR(dst)) {
176 		err = PTR_ERR(dst);
177 		goto out;
178 	}
179 
180 	/* source address lookup done in ip6_dst_lookup */
181 
182 	if (ipv6_addr_any(&np->saddr))
183 		np->saddr = fl6.saddr;
184 
185 	if (ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
186 		sk->sk_v6_rcv_saddr = fl6.saddr;
187 		inet->inet_rcv_saddr = LOOPBACK4_IPV6;
188 		if (sk->sk_prot->rehash)
189 			sk->sk_prot->rehash(sk);
190 	}
191 
192 	ip6_dst_store(sk, dst,
193 		      ipv6_addr_equal(&fl6.daddr, &sk->sk_v6_daddr) ?
194 		      &sk->sk_v6_daddr : NULL,
195 #ifdef CONFIG_IPV6_SUBTREES
196 		      ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
197 		      &np->saddr :
198 #endif
199 		      NULL);
200 
201 	sk->sk_state = TCP_ESTABLISHED;
202 out:
203 	fl6_sock_release(flowlabel);
204 	return err;
205 }
206 EXPORT_SYMBOL_GPL(ip6_datagram_connect);
207 
208 void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
209 		     __be16 port, u32 info, u8 *payload)
210 {
211 	struct ipv6_pinfo *np  = inet6_sk(sk);
212 	struct icmp6hdr *icmph = icmp6_hdr(skb);
213 	struct sock_exterr_skb *serr;
214 
215 	if (!np->recverr)
216 		return;
217 
218 	skb = skb_clone(skb, GFP_ATOMIC);
219 	if (!skb)
220 		return;
221 
222 	skb->protocol = htons(ETH_P_IPV6);
223 
224 	serr = SKB_EXT_ERR(skb);
225 	serr->ee.ee_errno = err;
226 	serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
227 	serr->ee.ee_type = icmph->icmp6_type;
228 	serr->ee.ee_code = icmph->icmp6_code;
229 	serr->ee.ee_pad = 0;
230 	serr->ee.ee_info = info;
231 	serr->ee.ee_data = 0;
232 	serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
233 				  skb_network_header(skb);
234 	serr->port = port;
235 
236 	__skb_pull(skb, payload - skb->data);
237 	skb_reset_transport_header(skb);
238 
239 	if (sock_queue_err_skb(sk, skb))
240 		kfree_skb(skb);
241 }
242 
243 void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info)
244 {
245 	struct ipv6_pinfo *np = inet6_sk(sk);
246 	struct sock_exterr_skb *serr;
247 	struct ipv6hdr *iph;
248 	struct sk_buff *skb;
249 
250 	if (!np->recverr)
251 		return;
252 
253 	skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
254 	if (!skb)
255 		return;
256 
257 	skb->protocol = htons(ETH_P_IPV6);
258 
259 	skb_put(skb, sizeof(struct ipv6hdr));
260 	skb_reset_network_header(skb);
261 	iph = ipv6_hdr(skb);
262 	iph->daddr = fl6->daddr;
263 
264 	serr = SKB_EXT_ERR(skb);
265 	serr->ee.ee_errno = err;
266 	serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
267 	serr->ee.ee_type = 0;
268 	serr->ee.ee_code = 0;
269 	serr->ee.ee_pad = 0;
270 	serr->ee.ee_info = info;
271 	serr->ee.ee_data = 0;
272 	serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
273 	serr->port = fl6->fl6_dport;
274 
275 	__skb_pull(skb, skb_tail_pointer(skb) - skb->data);
276 	skb_reset_transport_header(skb);
277 
278 	if (sock_queue_err_skb(sk, skb))
279 		kfree_skb(skb);
280 }
281 
282 void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu)
283 {
284 	struct ipv6_pinfo *np = inet6_sk(sk);
285 	struct ipv6hdr *iph;
286 	struct sk_buff *skb;
287 	struct ip6_mtuinfo *mtu_info;
288 
289 	if (!np->rxopt.bits.rxpmtu)
290 		return;
291 
292 	skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
293 	if (!skb)
294 		return;
295 
296 	skb_put(skb, sizeof(struct ipv6hdr));
297 	skb_reset_network_header(skb);
298 	iph = ipv6_hdr(skb);
299 	iph->daddr = fl6->daddr;
300 
301 	mtu_info = IP6CBMTU(skb);
302 
303 	mtu_info->ip6m_mtu = mtu;
304 	mtu_info->ip6m_addr.sin6_family = AF_INET6;
305 	mtu_info->ip6m_addr.sin6_port = 0;
306 	mtu_info->ip6m_addr.sin6_flowinfo = 0;
307 	mtu_info->ip6m_addr.sin6_scope_id = fl6->flowi6_oif;
308 	mtu_info->ip6m_addr.sin6_addr = ipv6_hdr(skb)->daddr;
309 
310 	__skb_pull(skb, skb_tail_pointer(skb) - skb->data);
311 	skb_reset_transport_header(skb);
312 
313 	skb = xchg(&np->rxpmtu, skb);
314 	kfree_skb(skb);
315 }
316 
317 /*
318  *	Handle MSG_ERRQUEUE
319  */
320 int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
321 {
322 	struct ipv6_pinfo *np = inet6_sk(sk);
323 	struct sock_exterr_skb *serr;
324 	struct sk_buff *skb, *skb2;
325 	struct sockaddr_in6 *sin;
326 	struct {
327 		struct sock_extended_err ee;
328 		struct sockaddr_in6	 offender;
329 	} errhdr;
330 	int err;
331 	int copied;
332 
333 	err = -EAGAIN;
334 	skb = skb_dequeue(&sk->sk_error_queue);
335 	if (skb == NULL)
336 		goto out;
337 
338 	copied = skb->len;
339 	if (copied > len) {
340 		msg->msg_flags |= MSG_TRUNC;
341 		copied = len;
342 	}
343 	err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
344 	if (err)
345 		goto out_free_skb;
346 
347 	sock_recv_timestamp(msg, sk, skb);
348 
349 	serr = SKB_EXT_ERR(skb);
350 
351 	sin = (struct sockaddr_in6 *)msg->msg_name;
352 	if (sin) {
353 		const unsigned char *nh = skb_network_header(skb);
354 		sin->sin6_family = AF_INET6;
355 		sin->sin6_flowinfo = 0;
356 		sin->sin6_port = serr->port;
357 		if (skb->protocol == htons(ETH_P_IPV6)) {
358 			const struct ipv6hdr *ip6h = container_of((struct in6_addr *)(nh + serr->addr_offset),
359 								  struct ipv6hdr, daddr);
360 			sin->sin6_addr = ip6h->daddr;
361 			if (np->sndflow)
362 				sin->sin6_flowinfo = ip6_flowinfo(ip6h);
363 			sin->sin6_scope_id =
364 				ipv6_iface_scope_id(&sin->sin6_addr,
365 						    IP6CB(skb)->iif);
366 		} else {
367 			ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset),
368 					       &sin->sin6_addr);
369 			sin->sin6_scope_id = 0;
370 		}
371 		*addr_len = sizeof(*sin);
372 	}
373 
374 	memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
375 	sin = &errhdr.offender;
376 	sin->sin6_family = AF_UNSPEC;
377 	if (serr->ee.ee_origin != SO_EE_ORIGIN_LOCAL) {
378 		sin->sin6_family = AF_INET6;
379 		sin->sin6_flowinfo = 0;
380 		sin->sin6_port = 0;
381 		if (skb->protocol == htons(ETH_P_IPV6)) {
382 			sin->sin6_addr = ipv6_hdr(skb)->saddr;
383 			if (np->rxopt.all)
384 				ip6_datagram_recv_ctl(sk, msg, skb);
385 			sin->sin6_scope_id =
386 				ipv6_iface_scope_id(&sin->sin6_addr,
387 						    IP6CB(skb)->iif);
388 		} else {
389 			struct inet_sock *inet = inet_sk(sk);
390 
391 			ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
392 					       &sin->sin6_addr);
393 			sin->sin6_scope_id = 0;
394 			if (inet->cmsg_flags)
395 				ip_cmsg_recv(msg, skb);
396 		}
397 	}
398 
399 	put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
400 
401 	/* Now we could try to dump offended packet options */
402 
403 	msg->msg_flags |= MSG_ERRQUEUE;
404 	err = copied;
405 
406 	/* Reset and regenerate socket error */
407 	spin_lock_bh(&sk->sk_error_queue.lock);
408 	sk->sk_err = 0;
409 	if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
410 		sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
411 		spin_unlock_bh(&sk->sk_error_queue.lock);
412 		sk->sk_error_report(sk);
413 	} else {
414 		spin_unlock_bh(&sk->sk_error_queue.lock);
415 	}
416 
417 out_free_skb:
418 	kfree_skb(skb);
419 out:
420 	return err;
421 }
422 EXPORT_SYMBOL_GPL(ipv6_recv_error);
423 
424 /*
425  *	Handle IPV6_RECVPATHMTU
426  */
427 int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len,
428 		     int *addr_len)
429 {
430 	struct ipv6_pinfo *np = inet6_sk(sk);
431 	struct sk_buff *skb;
432 	struct sockaddr_in6 *sin;
433 	struct ip6_mtuinfo mtu_info;
434 	int err;
435 	int copied;
436 
437 	err = -EAGAIN;
438 	skb = xchg(&np->rxpmtu, NULL);
439 	if (skb == NULL)
440 		goto out;
441 
442 	copied = skb->len;
443 	if (copied > len) {
444 		msg->msg_flags |= MSG_TRUNC;
445 		copied = len;
446 	}
447 	err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
448 	if (err)
449 		goto out_free_skb;
450 
451 	sock_recv_timestamp(msg, sk, skb);
452 
453 	memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info));
454 
455 	sin = (struct sockaddr_in6 *)msg->msg_name;
456 	if (sin) {
457 		sin->sin6_family = AF_INET6;
458 		sin->sin6_flowinfo = 0;
459 		sin->sin6_port = 0;
460 		sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id;
461 		sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr;
462 		*addr_len = sizeof(*sin);
463 	}
464 
465 	put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info);
466 
467 	err = copied;
468 
469 out_free_skb:
470 	kfree_skb(skb);
471 out:
472 	return err;
473 }
474 
475 
476 int ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
477 			  struct sk_buff *skb)
478 {
479 	struct ipv6_pinfo *np = inet6_sk(sk);
480 	struct inet6_skb_parm *opt = IP6CB(skb);
481 	unsigned char *nh = skb_network_header(skb);
482 
483 	if (np->rxopt.bits.rxinfo) {
484 		struct in6_pktinfo src_info;
485 
486 		src_info.ipi6_ifindex = opt->iif;
487 		src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
488 		put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
489 	}
490 
491 	if (np->rxopt.bits.rxhlim) {
492 		int hlim = ipv6_hdr(skb)->hop_limit;
493 		put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
494 	}
495 
496 	if (np->rxopt.bits.rxtclass) {
497 		int tclass = ipv6_get_dsfield(ipv6_hdr(skb));
498 		put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
499 	}
500 
501 	if (np->rxopt.bits.rxflow) {
502 		__be32 flowinfo = ip6_flowinfo((struct ipv6hdr *)nh);
503 		if (flowinfo)
504 			put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
505 	}
506 
507 	/* HbH is allowed only once */
508 	if (np->rxopt.bits.hopopts && opt->hop) {
509 		u8 *ptr = nh + opt->hop;
510 		put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
511 	}
512 
513 	if (opt->lastopt &&
514 	    (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
515 		/*
516 		 * Silly enough, but we need to reparse in order to
517 		 * report extension headers (except for HbH)
518 		 * in order.
519 		 *
520 		 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
521 		 * (and WILL NOT be) defined because
522 		 * IPV6_RECVDSTOPTS is more generic. --yoshfuji
523 		 */
524 		unsigned int off = sizeof(struct ipv6hdr);
525 		u8 nexthdr = ipv6_hdr(skb)->nexthdr;
526 
527 		while (off <= opt->lastopt) {
528 			unsigned int len;
529 			u8 *ptr = nh + off;
530 
531 			switch (nexthdr) {
532 			case IPPROTO_DSTOPTS:
533 				nexthdr = ptr[0];
534 				len = (ptr[1] + 1) << 3;
535 				if (np->rxopt.bits.dstopts)
536 					put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
537 				break;
538 			case IPPROTO_ROUTING:
539 				nexthdr = ptr[0];
540 				len = (ptr[1] + 1) << 3;
541 				if (np->rxopt.bits.srcrt)
542 					put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
543 				break;
544 			case IPPROTO_AH:
545 				nexthdr = ptr[0];
546 				len = (ptr[1] + 2) << 2;
547 				break;
548 			default:
549 				nexthdr = ptr[0];
550 				len = (ptr[1] + 1) << 3;
551 				break;
552 			}
553 
554 			off += len;
555 		}
556 	}
557 
558 	/* socket options in old style */
559 	if (np->rxopt.bits.rxoinfo) {
560 		struct in6_pktinfo src_info;
561 
562 		src_info.ipi6_ifindex = opt->iif;
563 		src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
564 		put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
565 	}
566 	if (np->rxopt.bits.rxohlim) {
567 		int hlim = ipv6_hdr(skb)->hop_limit;
568 		put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
569 	}
570 	if (np->rxopt.bits.ohopopts && opt->hop) {
571 		u8 *ptr = nh + opt->hop;
572 		put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
573 	}
574 	if (np->rxopt.bits.odstopts && opt->dst0) {
575 		u8 *ptr = nh + opt->dst0;
576 		put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
577 	}
578 	if (np->rxopt.bits.osrcrt && opt->srcrt) {
579 		struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
580 		put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
581 	}
582 	if (np->rxopt.bits.odstopts && opt->dst1) {
583 		u8 *ptr = nh + opt->dst1;
584 		put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
585 	}
586 	if (np->rxopt.bits.rxorigdstaddr) {
587 		struct sockaddr_in6 sin6;
588 		__be16 *ports = (__be16 *) skb_transport_header(skb);
589 
590 		if (skb_transport_offset(skb) + 4 <= skb->len) {
591 			/* All current transport protocols have the port numbers in the
592 			 * first four bytes of the transport header and this function is
593 			 * written with this assumption in mind.
594 			 */
595 
596 			sin6.sin6_family = AF_INET6;
597 			sin6.sin6_addr = ipv6_hdr(skb)->daddr;
598 			sin6.sin6_port = ports[1];
599 			sin6.sin6_flowinfo = 0;
600 			sin6.sin6_scope_id =
601 				ipv6_iface_scope_id(&ipv6_hdr(skb)->daddr,
602 						    opt->iif);
603 
604 			put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
605 		}
606 	}
607 	return 0;
608 }
609 EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl);
610 
611 int ip6_datagram_send_ctl(struct net *net, struct sock *sk,
612 			  struct msghdr *msg, struct flowi6 *fl6,
613 			  struct ipv6_txoptions *opt,
614 			  int *hlimit, int *tclass, int *dontfrag)
615 {
616 	struct in6_pktinfo *src_info;
617 	struct cmsghdr *cmsg;
618 	struct ipv6_rt_hdr *rthdr;
619 	struct ipv6_opt_hdr *hdr;
620 	int len;
621 	int err = 0;
622 
623 	for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
624 		int addr_type;
625 
626 		if (!CMSG_OK(msg, cmsg)) {
627 			err = -EINVAL;
628 			goto exit_f;
629 		}
630 
631 		if (cmsg->cmsg_level != SOL_IPV6)
632 			continue;
633 
634 		switch (cmsg->cmsg_type) {
635 		case IPV6_PKTINFO:
636 		case IPV6_2292PKTINFO:
637 		    {
638 			struct net_device *dev = NULL;
639 
640 			if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
641 				err = -EINVAL;
642 				goto exit_f;
643 			}
644 
645 			src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
646 
647 			if (src_info->ipi6_ifindex) {
648 				if (fl6->flowi6_oif &&
649 				    src_info->ipi6_ifindex != fl6->flowi6_oif)
650 					return -EINVAL;
651 				fl6->flowi6_oif = src_info->ipi6_ifindex;
652 			}
653 
654 			addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
655 
656 			rcu_read_lock();
657 			if (fl6->flowi6_oif) {
658 				dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
659 				if (!dev) {
660 					rcu_read_unlock();
661 					return -ENODEV;
662 				}
663 			} else if (addr_type & IPV6_ADDR_LINKLOCAL) {
664 				rcu_read_unlock();
665 				return -EINVAL;
666 			}
667 
668 			if (addr_type != IPV6_ADDR_ANY) {
669 				int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
670 				if (!(inet_sk(sk)->freebind || inet_sk(sk)->transparent) &&
671 				    !ipv6_chk_addr(net, &src_info->ipi6_addr,
672 						   strict ? dev : NULL, 0))
673 					err = -EINVAL;
674 				else
675 					fl6->saddr = src_info->ipi6_addr;
676 			}
677 
678 			rcu_read_unlock();
679 
680 			if (err)
681 				goto exit_f;
682 
683 			break;
684 		    }
685 
686 		case IPV6_FLOWINFO:
687 			if (cmsg->cmsg_len < CMSG_LEN(4)) {
688 				err = -EINVAL;
689 				goto exit_f;
690 			}
691 
692 			if (fl6->flowlabel&IPV6_FLOWINFO_MASK) {
693 				if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
694 					err = -EINVAL;
695 					goto exit_f;
696 				}
697 			}
698 			fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
699 			break;
700 
701 		case IPV6_2292HOPOPTS:
702 		case IPV6_HOPOPTS:
703 			if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
704 				err = -EINVAL;
705 				goto exit_f;
706 			}
707 
708 			hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
709 			len = ((hdr->hdrlen + 1) << 3);
710 			if (cmsg->cmsg_len < CMSG_LEN(len)) {
711 				err = -EINVAL;
712 				goto exit_f;
713 			}
714 			if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
715 				err = -EPERM;
716 				goto exit_f;
717 			}
718 			opt->opt_nflen += len;
719 			opt->hopopt = hdr;
720 			break;
721 
722 		case IPV6_2292DSTOPTS:
723 			if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
724 				err = -EINVAL;
725 				goto exit_f;
726 			}
727 
728 			hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
729 			len = ((hdr->hdrlen + 1) << 3);
730 			if (cmsg->cmsg_len < CMSG_LEN(len)) {
731 				err = -EINVAL;
732 				goto exit_f;
733 			}
734 			if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
735 				err = -EPERM;
736 				goto exit_f;
737 			}
738 			if (opt->dst1opt) {
739 				err = -EINVAL;
740 				goto exit_f;
741 			}
742 			opt->opt_flen += len;
743 			opt->dst1opt = hdr;
744 			break;
745 
746 		case IPV6_DSTOPTS:
747 		case IPV6_RTHDRDSTOPTS:
748 			if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
749 				err = -EINVAL;
750 				goto exit_f;
751 			}
752 
753 			hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
754 			len = ((hdr->hdrlen + 1) << 3);
755 			if (cmsg->cmsg_len < CMSG_LEN(len)) {
756 				err = -EINVAL;
757 				goto exit_f;
758 			}
759 			if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
760 				err = -EPERM;
761 				goto exit_f;
762 			}
763 			if (cmsg->cmsg_type == IPV6_DSTOPTS) {
764 				opt->opt_flen += len;
765 				opt->dst1opt = hdr;
766 			} else {
767 				opt->opt_nflen += len;
768 				opt->dst0opt = hdr;
769 			}
770 			break;
771 
772 		case IPV6_2292RTHDR:
773 		case IPV6_RTHDR:
774 			if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
775 				err = -EINVAL;
776 				goto exit_f;
777 			}
778 
779 			rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
780 
781 			switch (rthdr->type) {
782 #if IS_ENABLED(CONFIG_IPV6_MIP6)
783 			case IPV6_SRCRT_TYPE_2:
784 				if (rthdr->hdrlen != 2 ||
785 				    rthdr->segments_left != 1) {
786 					err = -EINVAL;
787 					goto exit_f;
788 				}
789 				break;
790 #endif
791 			default:
792 				err = -EINVAL;
793 				goto exit_f;
794 			}
795 
796 			len = ((rthdr->hdrlen + 1) << 3);
797 
798 			if (cmsg->cmsg_len < CMSG_LEN(len)) {
799 				err = -EINVAL;
800 				goto exit_f;
801 			}
802 
803 			/* segments left must also match */
804 			if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
805 				err = -EINVAL;
806 				goto exit_f;
807 			}
808 
809 			opt->opt_nflen += len;
810 			opt->srcrt = rthdr;
811 
812 			if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
813 				int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
814 
815 				opt->opt_nflen += dsthdrlen;
816 				opt->dst0opt = opt->dst1opt;
817 				opt->dst1opt = NULL;
818 				opt->opt_flen -= dsthdrlen;
819 			}
820 
821 			break;
822 
823 		case IPV6_2292HOPLIMIT:
824 		case IPV6_HOPLIMIT:
825 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
826 				err = -EINVAL;
827 				goto exit_f;
828 			}
829 
830 			*hlimit = *(int *)CMSG_DATA(cmsg);
831 			if (*hlimit < -1 || *hlimit > 0xff) {
832 				err = -EINVAL;
833 				goto exit_f;
834 			}
835 
836 			break;
837 
838 		case IPV6_TCLASS:
839 		    {
840 			int tc;
841 
842 			err = -EINVAL;
843 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
844 				goto exit_f;
845 
846 			tc = *(int *)CMSG_DATA(cmsg);
847 			if (tc < -1 || tc > 0xff)
848 				goto exit_f;
849 
850 			err = 0;
851 			*tclass = tc;
852 
853 			break;
854 		    }
855 
856 		case IPV6_DONTFRAG:
857 		    {
858 			int df;
859 
860 			err = -EINVAL;
861 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
862 				goto exit_f;
863 
864 			df = *(int *)CMSG_DATA(cmsg);
865 			if (df < 0 || df > 1)
866 				goto exit_f;
867 
868 			err = 0;
869 			*dontfrag = df;
870 
871 			break;
872 		    }
873 		default:
874 			LIMIT_NETDEBUG(KERN_DEBUG "invalid cmsg type: %d\n",
875 				       cmsg->cmsg_type);
876 			err = -EINVAL;
877 			goto exit_f;
878 		}
879 	}
880 
881 exit_f:
882 	return err;
883 }
884 EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl);
885 
886 void ip6_dgram_sock_seq_show(struct seq_file *seq, struct sock *sp,
887 			     __u16 srcp, __u16 destp, int bucket)
888 {
889 	const struct in6_addr *dest, *src;
890 
891 	dest  = &sp->sk_v6_daddr;
892 	src   = &sp->sk_v6_rcv_saddr;
893 	seq_printf(seq,
894 		   "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
895 		   "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d\n",
896 		   bucket,
897 		   src->s6_addr32[0], src->s6_addr32[1],
898 		   src->s6_addr32[2], src->s6_addr32[3], srcp,
899 		   dest->s6_addr32[0], dest->s6_addr32[1],
900 		   dest->s6_addr32[2], dest->s6_addr32[3], destp,
901 		   sp->sk_state,
902 		   sk_wmem_alloc_get(sp),
903 		   sk_rmem_alloc_get(sp),
904 		   0, 0L, 0,
905 		   from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
906 		   0,
907 		   sock_i_ino(sp),
908 		   atomic_read(&sp->sk_refcnt), sp,
909 		   atomic_read(&sp->sk_drops));
910 }
911