xref: /openbmc/linux/net/ipv6/ipv6_sockglue.c (revision 9b12f050)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	IPv6 BSD socket options interface
4  *	Linux INET6 implementation
5  *
6  *	Authors:
7  *	Pedro Roque		<roque@di.fc.ul.pt>
8  *
9  *	Based on linux/net/ipv4/ip_sockglue.c
10  *
11  *	FIXME: Make the setsockopt code POSIX compliant: That is
12  *
13  *	o	Truncate getsockopt returns
14  *	o	Return an optlen of the truncated length if need be
15  *
16  *	Changes:
17  *	David L Stevens <dlstevens@us.ibm.com>:
18  *		- added multicast source filtering API for MLDv2
19  */
20 
21 #include <linux/module.h>
22 #include <linux/capability.h>
23 #include <linux/errno.h>
24 #include <linux/types.h>
25 #include <linux/socket.h>
26 #include <linux/sockios.h>
27 #include <linux/net.h>
28 #include <linux/in6.h>
29 #include <linux/mroute6.h>
30 #include <linux/netdevice.h>
31 #include <linux/if_arp.h>
32 #include <linux/init.h>
33 #include <linux/sysctl.h>
34 #include <linux/netfilter.h>
35 #include <linux/slab.h>
36 
37 #include <net/sock.h>
38 #include <net/snmp.h>
39 #include <net/ipv6.h>
40 #include <net/ndisc.h>
41 #include <net/protocol.h>
42 #include <net/transp_v6.h>
43 #include <net/ip6_route.h>
44 #include <net/addrconf.h>
45 #include <net/inet_common.h>
46 #include <net/tcp.h>
47 #include <net/udp.h>
48 #include <net/udplite.h>
49 #include <net/xfrm.h>
50 #include <net/compat.h>
51 #include <net/seg6.h>
52 
53 #include <linux/uaccess.h>
54 
55 struct ip6_ra_chain *ip6_ra_chain;
56 DEFINE_RWLOCK(ip6_ra_lock);
57 
58 DEFINE_STATIC_KEY_FALSE(ip6_min_hopcount);
59 
60 int ip6_ra_control(struct sock *sk, int sel)
61 {
62 	struct ip6_ra_chain *ra, *new_ra, **rap;
63 
64 	/* RA packet may be delivered ONLY to IPPROTO_RAW socket */
65 	if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num != IPPROTO_RAW)
66 		return -ENOPROTOOPT;
67 
68 	new_ra = (sel >= 0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
69 	if (sel >= 0 && !new_ra)
70 		return -ENOMEM;
71 
72 	write_lock_bh(&ip6_ra_lock);
73 	for (rap = &ip6_ra_chain; (ra = *rap) != NULL; rap = &ra->next) {
74 		if (ra->sk == sk) {
75 			if (sel >= 0) {
76 				write_unlock_bh(&ip6_ra_lock);
77 				kfree(new_ra);
78 				return -EADDRINUSE;
79 			}
80 
81 			*rap = ra->next;
82 			write_unlock_bh(&ip6_ra_lock);
83 
84 			sock_put(sk);
85 			kfree(ra);
86 			return 0;
87 		}
88 	}
89 	if (!new_ra) {
90 		write_unlock_bh(&ip6_ra_lock);
91 		return -ENOBUFS;
92 	}
93 	new_ra->sk = sk;
94 	new_ra->sel = sel;
95 	new_ra->next = ra;
96 	*rap = new_ra;
97 	sock_hold(sk);
98 	write_unlock_bh(&ip6_ra_lock);
99 	return 0;
100 }
101 
102 struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
103 					   struct ipv6_txoptions *opt)
104 {
105 	if (inet_sk(sk)->is_icsk) {
106 		if (opt &&
107 		    !((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
108 		    inet_sk(sk)->inet_daddr != LOOPBACK4_IPV6) {
109 			struct inet_connection_sock *icsk = inet_csk(sk);
110 			icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen;
111 			icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
112 		}
113 	}
114 	opt = xchg((__force struct ipv6_txoptions **)&inet6_sk(sk)->opt,
115 		   opt);
116 	sk_dst_reset(sk);
117 
118 	return opt;
119 }
120 
121 static bool setsockopt_needs_rtnl(int optname)
122 {
123 	switch (optname) {
124 	case IPV6_ADDRFORM:
125 	case IPV6_ADD_MEMBERSHIP:
126 	case IPV6_DROP_MEMBERSHIP:
127 	case IPV6_JOIN_ANYCAST:
128 	case IPV6_LEAVE_ANYCAST:
129 	case MCAST_JOIN_GROUP:
130 	case MCAST_LEAVE_GROUP:
131 	case MCAST_JOIN_SOURCE_GROUP:
132 	case MCAST_LEAVE_SOURCE_GROUP:
133 	case MCAST_BLOCK_SOURCE:
134 	case MCAST_UNBLOCK_SOURCE:
135 	case MCAST_MSFILTER:
136 		return true;
137 	}
138 	return false;
139 }
140 
141 static int copy_group_source_from_sockptr(struct group_source_req *greqs,
142 		sockptr_t optval, int optlen)
143 {
144 	if (in_compat_syscall()) {
145 		struct compat_group_source_req gr32;
146 
147 		if (optlen < sizeof(gr32))
148 			return -EINVAL;
149 		if (copy_from_sockptr(&gr32, optval, sizeof(gr32)))
150 			return -EFAULT;
151 		greqs->gsr_interface = gr32.gsr_interface;
152 		greqs->gsr_group = gr32.gsr_group;
153 		greqs->gsr_source = gr32.gsr_source;
154 	} else {
155 		if (optlen < sizeof(*greqs))
156 			return -EINVAL;
157 		if (copy_from_sockptr(greqs, optval, sizeof(*greqs)))
158 			return -EFAULT;
159 	}
160 
161 	return 0;
162 }
163 
164 static int do_ipv6_mcast_group_source(struct sock *sk, int optname,
165 		sockptr_t optval, int optlen)
166 {
167 	struct group_source_req greqs;
168 	int omode, add;
169 	int ret;
170 
171 	ret = copy_group_source_from_sockptr(&greqs, optval, optlen);
172 	if (ret)
173 		return ret;
174 
175 	if (greqs.gsr_group.ss_family != AF_INET6 ||
176 	    greqs.gsr_source.ss_family != AF_INET6)
177 		return -EADDRNOTAVAIL;
178 
179 	if (optname == MCAST_BLOCK_SOURCE) {
180 		omode = MCAST_EXCLUDE;
181 		add = 1;
182 	} else if (optname == MCAST_UNBLOCK_SOURCE) {
183 		omode = MCAST_EXCLUDE;
184 		add = 0;
185 	} else if (optname == MCAST_JOIN_SOURCE_GROUP) {
186 		struct sockaddr_in6 *psin6;
187 		int retv;
188 
189 		psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
190 		retv = ipv6_sock_mc_join_ssm(sk, greqs.gsr_interface,
191 					     &psin6->sin6_addr,
192 					     MCAST_INCLUDE);
193 		/* prior join w/ different source is ok */
194 		if (retv && retv != -EADDRINUSE)
195 			return retv;
196 		omode = MCAST_INCLUDE;
197 		add = 1;
198 	} else /* MCAST_LEAVE_SOURCE_GROUP */ {
199 		omode = MCAST_INCLUDE;
200 		add = 0;
201 	}
202 	return ip6_mc_source(add, omode, sk, &greqs);
203 }
204 
205 static int ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval,
206 		int optlen)
207 {
208 	struct group_filter *gsf;
209 	int ret;
210 
211 	if (optlen < GROUP_FILTER_SIZE(0))
212 		return -EINVAL;
213 	if (optlen > READ_ONCE(sysctl_optmem_max))
214 		return -ENOBUFS;
215 
216 	gsf = memdup_sockptr(optval, optlen);
217 	if (IS_ERR(gsf))
218 		return PTR_ERR(gsf);
219 
220 	/* numsrc >= (4G-140)/128 overflow in 32 bits */
221 	ret = -ENOBUFS;
222 	if (gsf->gf_numsrc >= 0x1ffffffU ||
223 	    gsf->gf_numsrc > sysctl_mld_max_msf)
224 		goto out_free_gsf;
225 
226 	ret = -EINVAL;
227 	if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen)
228 		goto out_free_gsf;
229 
230 	ret = ip6_mc_msfilter(sk, gsf, gsf->gf_slist_flex);
231 out_free_gsf:
232 	kfree(gsf);
233 	return ret;
234 }
235 
236 static int compat_ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval,
237 		int optlen)
238 {
239 	const int size0 = offsetof(struct compat_group_filter, gf_slist_flex);
240 	struct compat_group_filter *gf32;
241 	void *p;
242 	int ret;
243 	int n;
244 
245 	if (optlen < size0)
246 		return -EINVAL;
247 	if (optlen > READ_ONCE(sysctl_optmem_max) - 4)
248 		return -ENOBUFS;
249 
250 	p = kmalloc(optlen + 4, GFP_KERNEL);
251 	if (!p)
252 		return -ENOMEM;
253 
254 	gf32 = p + 4; /* we want ->gf_group and ->gf_slist_flex aligned */
255 	ret = -EFAULT;
256 	if (copy_from_sockptr(gf32, optval, optlen))
257 		goto out_free_p;
258 
259 	/* numsrc >= (4G-140)/128 overflow in 32 bits */
260 	ret = -ENOBUFS;
261 	n = gf32->gf_numsrc;
262 	if (n >= 0x1ffffffU || n > sysctl_mld_max_msf)
263 		goto out_free_p;
264 
265 	ret = -EINVAL;
266 	if (offsetof(struct compat_group_filter, gf_slist_flex[n]) > optlen)
267 		goto out_free_p;
268 
269 	ret = ip6_mc_msfilter(sk, &(struct group_filter){
270 			.gf_interface = gf32->gf_interface,
271 			.gf_group = gf32->gf_group,
272 			.gf_fmode = gf32->gf_fmode,
273 			.gf_numsrc = gf32->gf_numsrc}, gf32->gf_slist_flex);
274 
275 out_free_p:
276 	kfree(p);
277 	return ret;
278 }
279 
280 static int ipv6_mcast_join_leave(struct sock *sk, int optname,
281 		sockptr_t optval, int optlen)
282 {
283 	struct sockaddr_in6 *psin6;
284 	struct group_req greq;
285 
286 	if (optlen < sizeof(greq))
287 		return -EINVAL;
288 	if (copy_from_sockptr(&greq, optval, sizeof(greq)))
289 		return -EFAULT;
290 
291 	if (greq.gr_group.ss_family != AF_INET6)
292 		return -EADDRNOTAVAIL;
293 	psin6 = (struct sockaddr_in6 *)&greq.gr_group;
294 	if (optname == MCAST_JOIN_GROUP)
295 		return ipv6_sock_mc_join(sk, greq.gr_interface,
296 					 &psin6->sin6_addr);
297 	return ipv6_sock_mc_drop(sk, greq.gr_interface, &psin6->sin6_addr);
298 }
299 
300 static int compat_ipv6_mcast_join_leave(struct sock *sk, int optname,
301 		sockptr_t optval, int optlen)
302 {
303 	struct compat_group_req gr32;
304 	struct sockaddr_in6 *psin6;
305 
306 	if (optlen < sizeof(gr32))
307 		return -EINVAL;
308 	if (copy_from_sockptr(&gr32, optval, sizeof(gr32)))
309 		return -EFAULT;
310 
311 	if (gr32.gr_group.ss_family != AF_INET6)
312 		return -EADDRNOTAVAIL;
313 	psin6 = (struct sockaddr_in6 *)&gr32.gr_group;
314 	if (optname == MCAST_JOIN_GROUP)
315 		return ipv6_sock_mc_join(sk, gr32.gr_interface,
316 					&psin6->sin6_addr);
317 	return ipv6_sock_mc_drop(sk, gr32.gr_interface, &psin6->sin6_addr);
318 }
319 
320 static int ipv6_set_opt_hdr(struct sock *sk, int optname, sockptr_t optval,
321 		int optlen)
322 {
323 	struct ipv6_pinfo *np = inet6_sk(sk);
324 	struct ipv6_opt_hdr *new = NULL;
325 	struct net *net = sock_net(sk);
326 	struct ipv6_txoptions *opt;
327 	int err;
328 
329 	/* hop-by-hop / destination options are privileged option */
330 	if (optname != IPV6_RTHDR && !sockopt_ns_capable(net->user_ns, CAP_NET_RAW))
331 		return -EPERM;
332 
333 	/* remove any sticky options header with a zero option
334 	 * length, per RFC3542.
335 	 */
336 	if (optlen > 0) {
337 		if (sockptr_is_null(optval))
338 			return -EINVAL;
339 		if (optlen < sizeof(struct ipv6_opt_hdr) ||
340 		    optlen & 0x7 ||
341 		    optlen > 8 * 255)
342 			return -EINVAL;
343 
344 		new = memdup_sockptr(optval, optlen);
345 		if (IS_ERR(new))
346 			return PTR_ERR(new);
347 		if (unlikely(ipv6_optlen(new) > optlen)) {
348 			kfree(new);
349 			return -EINVAL;
350 		}
351 	}
352 
353 	opt = rcu_dereference_protected(np->opt, lockdep_sock_is_held(sk));
354 	opt = ipv6_renew_options(sk, opt, optname, new);
355 	kfree(new);
356 	if (IS_ERR(opt))
357 		return PTR_ERR(opt);
358 
359 	/* routing header option needs extra check */
360 	err = -EINVAL;
361 	if (optname == IPV6_RTHDR && opt && opt->srcrt) {
362 		struct ipv6_rt_hdr *rthdr = opt->srcrt;
363 		switch (rthdr->type) {
364 #if IS_ENABLED(CONFIG_IPV6_MIP6)
365 		case IPV6_SRCRT_TYPE_2:
366 			if (rthdr->hdrlen != 2 || rthdr->segments_left != 1)
367 				goto sticky_done;
368 			break;
369 #endif
370 		case IPV6_SRCRT_TYPE_4:
371 		{
372 			struct ipv6_sr_hdr *srh =
373 				(struct ipv6_sr_hdr *)opt->srcrt;
374 
375 			if (!seg6_validate_srh(srh, optlen, false))
376 				goto sticky_done;
377 			break;
378 		}
379 		default:
380 			goto sticky_done;
381 		}
382 	}
383 
384 	err = 0;
385 	opt = ipv6_update_options(sk, opt);
386 sticky_done:
387 	if (opt) {
388 		atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
389 		txopt_put(opt);
390 	}
391 	return err;
392 }
393 
394 int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
395 		       sockptr_t optval, unsigned int optlen)
396 {
397 	struct ipv6_pinfo *np = inet6_sk(sk);
398 	struct net *net = sock_net(sk);
399 	int val, valbool;
400 	int retv = -ENOPROTOOPT;
401 	bool needs_rtnl = setsockopt_needs_rtnl(optname);
402 
403 	if (sockptr_is_null(optval))
404 		val = 0;
405 	else {
406 		if (optlen >= sizeof(int)) {
407 			if (copy_from_sockptr(&val, optval, sizeof(val)))
408 				return -EFAULT;
409 		} else
410 			val = 0;
411 	}
412 
413 	valbool = (val != 0);
414 
415 	if (ip6_mroute_opt(optname))
416 		return ip6_mroute_setsockopt(sk, optname, optval, optlen);
417 
418 	if (needs_rtnl)
419 		rtnl_lock();
420 	sockopt_lock_sock(sk);
421 
422 	/* Another thread has converted the socket into IPv4 with
423 	 * IPV6_ADDRFORM concurrently.
424 	 */
425 	if (unlikely(sk->sk_family != AF_INET6))
426 		goto unlock;
427 
428 	switch (optname) {
429 
430 	case IPV6_ADDRFORM:
431 		if (optlen < sizeof(int))
432 			goto e_inval;
433 		if (val == PF_INET) {
434 			if (sk->sk_type == SOCK_RAW)
435 				break;
436 
437 			if (sk->sk_protocol == IPPROTO_UDP ||
438 			    sk->sk_protocol == IPPROTO_UDPLITE) {
439 				struct udp_sock *up = udp_sk(sk);
440 				if (up->pending == AF_INET6) {
441 					retv = -EBUSY;
442 					break;
443 				}
444 			} else if (sk->sk_protocol == IPPROTO_TCP) {
445 				if (sk->sk_prot != &tcpv6_prot) {
446 					retv = -EBUSY;
447 					break;
448 				}
449 			} else {
450 				break;
451 			}
452 
453 			if (sk->sk_state != TCP_ESTABLISHED) {
454 				retv = -ENOTCONN;
455 				break;
456 			}
457 
458 			if (ipv6_only_sock(sk) ||
459 			    !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
460 				retv = -EADDRNOTAVAIL;
461 				break;
462 			}
463 
464 			__ipv6_sock_mc_close(sk);
465 			__ipv6_sock_ac_close(sk);
466 
467 			if (sk->sk_protocol == IPPROTO_TCP) {
468 				struct inet_connection_sock *icsk = inet_csk(sk);
469 
470 				sock_prot_inuse_add(net, sk->sk_prot, -1);
471 				sock_prot_inuse_add(net, &tcp_prot, 1);
472 
473 				/* Paired with READ_ONCE(sk->sk_prot) in inet6_stream_ops */
474 				WRITE_ONCE(sk->sk_prot, &tcp_prot);
475 				/* Paired with READ_ONCE() in tcp_(get|set)sockopt() */
476 				WRITE_ONCE(icsk->icsk_af_ops, &ipv4_specific);
477 				sk->sk_socket->ops = &inet_stream_ops;
478 				sk->sk_family = PF_INET;
479 				tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
480 			} else {
481 				struct proto *prot = &udp_prot;
482 
483 				if (sk->sk_protocol == IPPROTO_UDPLITE)
484 					prot = &udplite_prot;
485 
486 				sock_prot_inuse_add(net, sk->sk_prot, -1);
487 				sock_prot_inuse_add(net, prot, 1);
488 
489 				/* Paired with READ_ONCE(sk->sk_prot) in inet6_dgram_ops */
490 				WRITE_ONCE(sk->sk_prot, prot);
491 				sk->sk_socket->ops = &inet_dgram_ops;
492 				sk->sk_family = PF_INET;
493 			}
494 
495 			/* Disable all options not to allocate memory anymore,
496 			 * but there is still a race.  See the lockless path
497 			 * in udpv6_sendmsg() and ipv6_local_rxpmtu().
498 			 */
499 			np->rxopt.all = 0;
500 
501 			inet6_cleanup_sock(sk);
502 
503 			module_put(THIS_MODULE);
504 			retv = 0;
505 			break;
506 		}
507 		goto e_inval;
508 
509 	case IPV6_V6ONLY:
510 		if (optlen < sizeof(int) ||
511 		    inet_sk(sk)->inet_num)
512 			goto e_inval;
513 		sk->sk_ipv6only = valbool;
514 		retv = 0;
515 		break;
516 
517 	case IPV6_RECVPKTINFO:
518 		if (optlen < sizeof(int))
519 			goto e_inval;
520 		np->rxopt.bits.rxinfo = valbool;
521 		retv = 0;
522 		break;
523 
524 	case IPV6_2292PKTINFO:
525 		if (optlen < sizeof(int))
526 			goto e_inval;
527 		np->rxopt.bits.rxoinfo = valbool;
528 		retv = 0;
529 		break;
530 
531 	case IPV6_RECVHOPLIMIT:
532 		if (optlen < sizeof(int))
533 			goto e_inval;
534 		np->rxopt.bits.rxhlim = valbool;
535 		retv = 0;
536 		break;
537 
538 	case IPV6_2292HOPLIMIT:
539 		if (optlen < sizeof(int))
540 			goto e_inval;
541 		np->rxopt.bits.rxohlim = valbool;
542 		retv = 0;
543 		break;
544 
545 	case IPV6_RECVRTHDR:
546 		if (optlen < sizeof(int))
547 			goto e_inval;
548 		np->rxopt.bits.srcrt = valbool;
549 		retv = 0;
550 		break;
551 
552 	case IPV6_2292RTHDR:
553 		if (optlen < sizeof(int))
554 			goto e_inval;
555 		np->rxopt.bits.osrcrt = valbool;
556 		retv = 0;
557 		break;
558 
559 	case IPV6_RECVHOPOPTS:
560 		if (optlen < sizeof(int))
561 			goto e_inval;
562 		np->rxopt.bits.hopopts = valbool;
563 		retv = 0;
564 		break;
565 
566 	case IPV6_2292HOPOPTS:
567 		if (optlen < sizeof(int))
568 			goto e_inval;
569 		np->rxopt.bits.ohopopts = valbool;
570 		retv = 0;
571 		break;
572 
573 	case IPV6_RECVDSTOPTS:
574 		if (optlen < sizeof(int))
575 			goto e_inval;
576 		np->rxopt.bits.dstopts = valbool;
577 		retv = 0;
578 		break;
579 
580 	case IPV6_2292DSTOPTS:
581 		if (optlen < sizeof(int))
582 			goto e_inval;
583 		np->rxopt.bits.odstopts = valbool;
584 		retv = 0;
585 		break;
586 
587 	case IPV6_TCLASS:
588 		if (optlen < sizeof(int))
589 			goto e_inval;
590 		if (val < -1 || val > 0xff)
591 			goto e_inval;
592 		/* RFC 3542, 6.5: default traffic class of 0x0 */
593 		if (val == -1)
594 			val = 0;
595 		if (sk->sk_type == SOCK_STREAM) {
596 			val &= ~INET_ECN_MASK;
597 			val |= np->tclass & INET_ECN_MASK;
598 		}
599 		if (np->tclass != val) {
600 			np->tclass = val;
601 			sk_dst_reset(sk);
602 		}
603 		retv = 0;
604 		break;
605 
606 	case IPV6_RECVTCLASS:
607 		if (optlen < sizeof(int))
608 			goto e_inval;
609 		np->rxopt.bits.rxtclass = valbool;
610 		retv = 0;
611 		break;
612 
613 	case IPV6_FLOWINFO:
614 		if (optlen < sizeof(int))
615 			goto e_inval;
616 		np->rxopt.bits.rxflow = valbool;
617 		retv = 0;
618 		break;
619 
620 	case IPV6_RECVPATHMTU:
621 		if (optlen < sizeof(int))
622 			goto e_inval;
623 		np->rxopt.bits.rxpmtu = valbool;
624 		retv = 0;
625 		break;
626 
627 	case IPV6_TRANSPARENT:
628 		if (valbool && !sockopt_ns_capable(net->user_ns, CAP_NET_RAW) &&
629 		    !sockopt_ns_capable(net->user_ns, CAP_NET_ADMIN)) {
630 			retv = -EPERM;
631 			break;
632 		}
633 		if (optlen < sizeof(int))
634 			goto e_inval;
635 		/* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
636 		inet_sk(sk)->transparent = valbool;
637 		retv = 0;
638 		break;
639 
640 	case IPV6_FREEBIND:
641 		if (optlen < sizeof(int))
642 			goto e_inval;
643 		/* we also don't have a separate freebind bit for IPV6 */
644 		inet_sk(sk)->freebind = valbool;
645 		retv = 0;
646 		break;
647 
648 	case IPV6_RECVORIGDSTADDR:
649 		if (optlen < sizeof(int))
650 			goto e_inval;
651 		np->rxopt.bits.rxorigdstaddr = valbool;
652 		retv = 0;
653 		break;
654 
655 	case IPV6_HOPOPTS:
656 	case IPV6_RTHDRDSTOPTS:
657 	case IPV6_RTHDR:
658 	case IPV6_DSTOPTS:
659 		retv = ipv6_set_opt_hdr(sk, optname, optval, optlen);
660 		break;
661 
662 	case IPV6_PKTINFO:
663 	{
664 		struct in6_pktinfo pkt;
665 
666 		if (optlen == 0)
667 			goto e_inval;
668 		else if (optlen < sizeof(struct in6_pktinfo) ||
669 			 sockptr_is_null(optval))
670 			goto e_inval;
671 
672 		if (copy_from_sockptr(&pkt, optval, sizeof(pkt))) {
673 			retv = -EFAULT;
674 			break;
675 		}
676 		if (!sk_dev_equal_l3scope(sk, pkt.ipi6_ifindex))
677 			goto e_inval;
678 
679 		np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
680 		np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
681 		retv = 0;
682 		break;
683 	}
684 
685 	case IPV6_2292PKTOPTIONS:
686 	{
687 		struct ipv6_txoptions *opt = NULL;
688 		struct msghdr msg;
689 		struct flowi6 fl6;
690 		struct ipcm6_cookie ipc6;
691 
692 		memset(&fl6, 0, sizeof(fl6));
693 		fl6.flowi6_oif = sk->sk_bound_dev_if;
694 		fl6.flowi6_mark = sk->sk_mark;
695 
696 		if (optlen == 0)
697 			goto update;
698 
699 		/* 1K is probably excessive
700 		 * 1K is surely not enough, 2K per standard header is 16K.
701 		 */
702 		retv = -EINVAL;
703 		if (optlen > 64*1024)
704 			break;
705 
706 		opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
707 		retv = -ENOBUFS;
708 		if (!opt)
709 			break;
710 
711 		memset(opt, 0, sizeof(*opt));
712 		refcount_set(&opt->refcnt, 1);
713 		opt->tot_len = sizeof(*opt) + optlen;
714 		retv = -EFAULT;
715 		if (copy_from_sockptr(opt + 1, optval, optlen))
716 			goto done;
717 
718 		msg.msg_controllen = optlen;
719 		msg.msg_control = (void *)(opt+1);
720 		ipc6.opt = opt;
721 
722 		retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6);
723 		if (retv)
724 			goto done;
725 update:
726 		retv = 0;
727 		opt = ipv6_update_options(sk, opt);
728 done:
729 		if (opt) {
730 			atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
731 			txopt_put(opt);
732 		}
733 		break;
734 	}
735 	case IPV6_UNICAST_HOPS:
736 		if (optlen < sizeof(int))
737 			goto e_inval;
738 		if (val > 255 || val < -1)
739 			goto e_inval;
740 		np->hop_limit = val;
741 		retv = 0;
742 		break;
743 
744 	case IPV6_MULTICAST_HOPS:
745 		if (sk->sk_type == SOCK_STREAM)
746 			break;
747 		if (optlen < sizeof(int))
748 			goto e_inval;
749 		if (val > 255 || val < -1)
750 			goto e_inval;
751 		np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
752 		retv = 0;
753 		break;
754 
755 	case IPV6_MULTICAST_LOOP:
756 		if (optlen < sizeof(int))
757 			goto e_inval;
758 		if (val != valbool)
759 			goto e_inval;
760 		np->mc_loop = valbool;
761 		retv = 0;
762 		break;
763 
764 	case IPV6_UNICAST_IF:
765 	{
766 		struct net_device *dev = NULL;
767 		int ifindex;
768 
769 		if (optlen != sizeof(int))
770 			goto e_inval;
771 
772 		ifindex = (__force int)ntohl((__force __be32)val);
773 		if (ifindex == 0) {
774 			np->ucast_oif = 0;
775 			retv = 0;
776 			break;
777 		}
778 
779 		dev = dev_get_by_index(net, ifindex);
780 		retv = -EADDRNOTAVAIL;
781 		if (!dev)
782 			break;
783 		dev_put(dev);
784 
785 		retv = -EINVAL;
786 		if (sk->sk_bound_dev_if)
787 			break;
788 
789 		np->ucast_oif = ifindex;
790 		retv = 0;
791 		break;
792 	}
793 
794 	case IPV6_MULTICAST_IF:
795 		if (sk->sk_type == SOCK_STREAM)
796 			break;
797 		if (optlen < sizeof(int))
798 			goto e_inval;
799 
800 		if (val) {
801 			struct net_device *dev;
802 			int midx;
803 
804 			rcu_read_lock();
805 
806 			dev = dev_get_by_index_rcu(net, val);
807 			if (!dev) {
808 				rcu_read_unlock();
809 				retv = -ENODEV;
810 				break;
811 			}
812 			midx = l3mdev_master_ifindex_rcu(dev);
813 
814 			rcu_read_unlock();
815 
816 			if (sk->sk_bound_dev_if &&
817 			    sk->sk_bound_dev_if != val &&
818 			    (!midx || midx != sk->sk_bound_dev_if))
819 				goto e_inval;
820 		}
821 		np->mcast_oif = val;
822 		retv = 0;
823 		break;
824 	case IPV6_ADD_MEMBERSHIP:
825 	case IPV6_DROP_MEMBERSHIP:
826 	{
827 		struct ipv6_mreq mreq;
828 
829 		if (optlen < sizeof(struct ipv6_mreq))
830 			goto e_inval;
831 
832 		retv = -EPROTO;
833 		if (inet_sk(sk)->is_icsk)
834 			break;
835 
836 		retv = -EFAULT;
837 		if (copy_from_sockptr(&mreq, optval, sizeof(struct ipv6_mreq)))
838 			break;
839 
840 		if (optname == IPV6_ADD_MEMBERSHIP)
841 			retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
842 		else
843 			retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
844 		break;
845 	}
846 	case IPV6_JOIN_ANYCAST:
847 	case IPV6_LEAVE_ANYCAST:
848 	{
849 		struct ipv6_mreq mreq;
850 
851 		if (optlen < sizeof(struct ipv6_mreq))
852 			goto e_inval;
853 
854 		retv = -EFAULT;
855 		if (copy_from_sockptr(&mreq, optval, sizeof(struct ipv6_mreq)))
856 			break;
857 
858 		if (optname == IPV6_JOIN_ANYCAST)
859 			retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
860 		else
861 			retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
862 		break;
863 	}
864 	case IPV6_MULTICAST_ALL:
865 		if (optlen < sizeof(int))
866 			goto e_inval;
867 		np->mc_all = valbool;
868 		retv = 0;
869 		break;
870 
871 	case MCAST_JOIN_GROUP:
872 	case MCAST_LEAVE_GROUP:
873 		if (in_compat_syscall())
874 			retv = compat_ipv6_mcast_join_leave(sk, optname, optval,
875 							    optlen);
876 		else
877 			retv = ipv6_mcast_join_leave(sk, optname, optval,
878 						     optlen);
879 		break;
880 	case MCAST_JOIN_SOURCE_GROUP:
881 	case MCAST_LEAVE_SOURCE_GROUP:
882 	case MCAST_BLOCK_SOURCE:
883 	case MCAST_UNBLOCK_SOURCE:
884 		retv = do_ipv6_mcast_group_source(sk, optname, optval, optlen);
885 		break;
886 	case MCAST_MSFILTER:
887 		if (in_compat_syscall())
888 			retv = compat_ipv6_set_mcast_msfilter(sk, optval,
889 							      optlen);
890 		else
891 			retv = ipv6_set_mcast_msfilter(sk, optval, optlen);
892 		break;
893 	case IPV6_ROUTER_ALERT:
894 		if (optlen < sizeof(int))
895 			goto e_inval;
896 		retv = ip6_ra_control(sk, val);
897 		break;
898 	case IPV6_ROUTER_ALERT_ISOLATE:
899 		if (optlen < sizeof(int))
900 			goto e_inval;
901 		np->rtalert_isolate = valbool;
902 		retv = 0;
903 		break;
904 	case IPV6_MTU_DISCOVER:
905 		if (optlen < sizeof(int))
906 			goto e_inval;
907 		if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
908 			goto e_inval;
909 		np->pmtudisc = val;
910 		retv = 0;
911 		break;
912 	case IPV6_MTU:
913 		if (optlen < sizeof(int))
914 			goto e_inval;
915 		if (val && val < IPV6_MIN_MTU)
916 			goto e_inval;
917 		np->frag_size = val;
918 		retv = 0;
919 		break;
920 	case IPV6_RECVERR:
921 		if (optlen < sizeof(int))
922 			goto e_inval;
923 		np->recverr = valbool;
924 		if (!val)
925 			skb_queue_purge(&sk->sk_error_queue);
926 		retv = 0;
927 		break;
928 	case IPV6_FLOWINFO_SEND:
929 		if (optlen < sizeof(int))
930 			goto e_inval;
931 		np->sndflow = valbool;
932 		retv = 0;
933 		break;
934 	case IPV6_FLOWLABEL_MGR:
935 		retv = ipv6_flowlabel_opt(sk, optval, optlen);
936 		break;
937 	case IPV6_IPSEC_POLICY:
938 	case IPV6_XFRM_POLICY:
939 		retv = -EPERM;
940 		if (!sockopt_ns_capable(net->user_ns, CAP_NET_ADMIN))
941 			break;
942 		retv = xfrm_user_policy(sk, optname, optval, optlen);
943 		break;
944 
945 	case IPV6_ADDR_PREFERENCES:
946 		if (optlen < sizeof(int))
947 			goto e_inval;
948 		retv = __ip6_sock_set_addr_preferences(sk, val);
949 		break;
950 	case IPV6_MINHOPCOUNT:
951 		if (optlen < sizeof(int))
952 			goto e_inval;
953 		if (val < 0 || val > 255)
954 			goto e_inval;
955 
956 		if (val)
957 			static_branch_enable(&ip6_min_hopcount);
958 
959 		/* tcp_v6_err() and tcp_v6_rcv() might read min_hopcount
960 		 * while we are changing it.
961 		 */
962 		WRITE_ONCE(np->min_hopcount, val);
963 		retv = 0;
964 		break;
965 	case IPV6_DONTFRAG:
966 		np->dontfrag = valbool;
967 		retv = 0;
968 		break;
969 	case IPV6_AUTOFLOWLABEL:
970 		np->autoflowlabel = valbool;
971 		np->autoflowlabel_set = 1;
972 		retv = 0;
973 		break;
974 	case IPV6_RECVFRAGSIZE:
975 		np->rxopt.bits.recvfragsize = valbool;
976 		retv = 0;
977 		break;
978 	case IPV6_RECVERR_RFC4884:
979 		if (optlen < sizeof(int))
980 			goto e_inval;
981 		if (val < 0 || val > 1)
982 			goto e_inval;
983 		np->recverr_rfc4884 = valbool;
984 		retv = 0;
985 		break;
986 	}
987 
988 unlock:
989 	sockopt_release_sock(sk);
990 	if (needs_rtnl)
991 		rtnl_unlock();
992 
993 	return retv;
994 
995 e_inval:
996 	retv = -EINVAL;
997 	goto unlock;
998 }
999 
1000 int ipv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
1001 		    unsigned int optlen)
1002 {
1003 	int err;
1004 
1005 	if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1006 		return udp_prot.setsockopt(sk, level, optname, optval, optlen);
1007 
1008 	if (level != SOL_IPV6)
1009 		return -ENOPROTOOPT;
1010 
1011 	err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
1012 #ifdef CONFIG_NETFILTER
1013 	/* we need to exclude all possible ENOPROTOOPTs except default case */
1014 	if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
1015 			optname != IPV6_XFRM_POLICY)
1016 		err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen);
1017 #endif
1018 	return err;
1019 }
1020 EXPORT_SYMBOL(ipv6_setsockopt);
1021 
1022 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
1023 				  int optname, sockptr_t optval, int len)
1024 {
1025 	struct ipv6_opt_hdr *hdr;
1026 
1027 	if (!opt)
1028 		return 0;
1029 
1030 	switch (optname) {
1031 	case IPV6_HOPOPTS:
1032 		hdr = opt->hopopt;
1033 		break;
1034 	case IPV6_RTHDRDSTOPTS:
1035 		hdr = opt->dst0opt;
1036 		break;
1037 	case IPV6_RTHDR:
1038 		hdr = (struct ipv6_opt_hdr *)opt->srcrt;
1039 		break;
1040 	case IPV6_DSTOPTS:
1041 		hdr = opt->dst1opt;
1042 		break;
1043 	default:
1044 		return -EINVAL;	/* should not happen */
1045 	}
1046 
1047 	if (!hdr)
1048 		return 0;
1049 
1050 	len = min_t(unsigned int, len, ipv6_optlen(hdr));
1051 	if (copy_to_sockptr(optval, hdr, len))
1052 		return -EFAULT;
1053 	return len;
1054 }
1055 
1056 static int ipv6_get_msfilter(struct sock *sk, sockptr_t optval,
1057 			     sockptr_t optlen, int len)
1058 {
1059 	const int size0 = offsetof(struct group_filter, gf_slist_flex);
1060 	struct group_filter gsf;
1061 	int num;
1062 	int err;
1063 
1064 	if (len < size0)
1065 		return -EINVAL;
1066 	if (copy_from_sockptr(&gsf, optval, size0))
1067 		return -EFAULT;
1068 	if (gsf.gf_group.ss_family != AF_INET6)
1069 		return -EADDRNOTAVAIL;
1070 	num = gsf.gf_numsrc;
1071 	sockopt_lock_sock(sk);
1072 	err = ip6_mc_msfget(sk, &gsf, optval, size0);
1073 	if (!err) {
1074 		if (num > gsf.gf_numsrc)
1075 			num = gsf.gf_numsrc;
1076 		len = GROUP_FILTER_SIZE(num);
1077 		if (copy_to_sockptr(optlen, &len, sizeof(int)) ||
1078 		    copy_to_sockptr(optval, &gsf, size0))
1079 			err = -EFAULT;
1080 	}
1081 	sockopt_release_sock(sk);
1082 	return err;
1083 }
1084 
1085 static int compat_ipv6_get_msfilter(struct sock *sk, sockptr_t optval,
1086 				    sockptr_t optlen, int len)
1087 {
1088 	const int size0 = offsetof(struct compat_group_filter, gf_slist_flex);
1089 	struct compat_group_filter gf32;
1090 	struct group_filter gf;
1091 	int err;
1092 	int num;
1093 
1094 	if (len < size0)
1095 		return -EINVAL;
1096 
1097 	if (copy_from_sockptr(&gf32, optval, size0))
1098 		return -EFAULT;
1099 	gf.gf_interface = gf32.gf_interface;
1100 	gf.gf_fmode = gf32.gf_fmode;
1101 	num = gf.gf_numsrc = gf32.gf_numsrc;
1102 	gf.gf_group = gf32.gf_group;
1103 
1104 	if (gf.gf_group.ss_family != AF_INET6)
1105 		return -EADDRNOTAVAIL;
1106 
1107 	sockopt_lock_sock(sk);
1108 	err = ip6_mc_msfget(sk, &gf, optval, size0);
1109 	sockopt_release_sock(sk);
1110 	if (err)
1111 		return err;
1112 	if (num > gf.gf_numsrc)
1113 		num = gf.gf_numsrc;
1114 	len = GROUP_FILTER_SIZE(num) - (sizeof(gf)-sizeof(gf32));
1115 	if (copy_to_sockptr(optlen, &len, sizeof(int)) ||
1116 	    copy_to_sockptr_offset(optval, offsetof(struct compat_group_filter, gf_fmode),
1117 				   &gf.gf_fmode, sizeof(gf32.gf_fmode)) ||
1118 	    copy_to_sockptr_offset(optval, offsetof(struct compat_group_filter, gf_numsrc),
1119 				   &gf.gf_numsrc, sizeof(gf32.gf_numsrc)))
1120 		return -EFAULT;
1121 	return 0;
1122 }
1123 
1124 int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
1125 		       sockptr_t optval, sockptr_t optlen)
1126 {
1127 	struct ipv6_pinfo *np = inet6_sk(sk);
1128 	int len;
1129 	int val;
1130 
1131 	if (ip6_mroute_opt(optname))
1132 		return ip6_mroute_getsockopt(sk, optname, optval, optlen);
1133 
1134 	if (copy_from_sockptr(&len, optlen, sizeof(int)))
1135 		return -EFAULT;
1136 	switch (optname) {
1137 	case IPV6_ADDRFORM:
1138 		if (sk->sk_protocol != IPPROTO_UDP &&
1139 		    sk->sk_protocol != IPPROTO_UDPLITE &&
1140 		    sk->sk_protocol != IPPROTO_TCP)
1141 			return -ENOPROTOOPT;
1142 		if (sk->sk_state != TCP_ESTABLISHED)
1143 			return -ENOTCONN;
1144 		val = sk->sk_family;
1145 		break;
1146 	case MCAST_MSFILTER:
1147 		if (in_compat_syscall())
1148 			return compat_ipv6_get_msfilter(sk, optval, optlen, len);
1149 		return ipv6_get_msfilter(sk, optval, optlen, len);
1150 	case IPV6_2292PKTOPTIONS:
1151 	{
1152 		struct msghdr msg;
1153 		struct sk_buff *skb;
1154 
1155 		if (sk->sk_type != SOCK_STREAM)
1156 			return -ENOPROTOOPT;
1157 
1158 		if (optval.is_kernel) {
1159 			msg.msg_control_is_user = false;
1160 			msg.msg_control = optval.kernel;
1161 		} else {
1162 			msg.msg_control_is_user = true;
1163 			msg.msg_control_user = optval.user;
1164 		}
1165 		msg.msg_controllen = len;
1166 		msg.msg_flags = 0;
1167 
1168 		sockopt_lock_sock(sk);
1169 		skb = np->pktoptions;
1170 		if (skb)
1171 			ip6_datagram_recv_ctl(sk, &msg, skb);
1172 		sockopt_release_sock(sk);
1173 		if (!skb) {
1174 			if (np->rxopt.bits.rxinfo) {
1175 				struct in6_pktinfo src_info;
1176 				src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1177 					np->sticky_pktinfo.ipi6_ifindex;
1178 				src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
1179 				put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1180 			}
1181 			if (np->rxopt.bits.rxhlim) {
1182 				int hlim = np->mcast_hops;
1183 				put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1184 			}
1185 			if (np->rxopt.bits.rxtclass) {
1186 				int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1187 
1188 				put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1189 			}
1190 			if (np->rxopt.bits.rxoinfo) {
1191 				struct in6_pktinfo src_info;
1192 				src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1193 					np->sticky_pktinfo.ipi6_ifindex;
1194 				src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1195 								     np->sticky_pktinfo.ipi6_addr;
1196 				put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1197 			}
1198 			if (np->rxopt.bits.rxohlim) {
1199 				int hlim = np->mcast_hops;
1200 				put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1201 			}
1202 			if (np->rxopt.bits.rxflow) {
1203 				__be32 flowinfo = np->rcv_flowinfo;
1204 
1205 				put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1206 			}
1207 		}
1208 		len -= msg.msg_controllen;
1209 		return copy_to_sockptr(optlen, &len, sizeof(int));
1210 	}
1211 	case IPV6_MTU:
1212 	{
1213 		struct dst_entry *dst;
1214 
1215 		val = 0;
1216 		rcu_read_lock();
1217 		dst = __sk_dst_get(sk);
1218 		if (dst)
1219 			val = dst_mtu(dst);
1220 		rcu_read_unlock();
1221 		if (!val)
1222 			return -ENOTCONN;
1223 		break;
1224 	}
1225 
1226 	case IPV6_V6ONLY:
1227 		val = sk->sk_ipv6only;
1228 		break;
1229 
1230 	case IPV6_RECVPKTINFO:
1231 		val = np->rxopt.bits.rxinfo;
1232 		break;
1233 
1234 	case IPV6_2292PKTINFO:
1235 		val = np->rxopt.bits.rxoinfo;
1236 		break;
1237 
1238 	case IPV6_RECVHOPLIMIT:
1239 		val = np->rxopt.bits.rxhlim;
1240 		break;
1241 
1242 	case IPV6_2292HOPLIMIT:
1243 		val = np->rxopt.bits.rxohlim;
1244 		break;
1245 
1246 	case IPV6_RECVRTHDR:
1247 		val = np->rxopt.bits.srcrt;
1248 		break;
1249 
1250 	case IPV6_2292RTHDR:
1251 		val = np->rxopt.bits.osrcrt;
1252 		break;
1253 
1254 	case IPV6_HOPOPTS:
1255 	case IPV6_RTHDRDSTOPTS:
1256 	case IPV6_RTHDR:
1257 	case IPV6_DSTOPTS:
1258 	{
1259 		struct ipv6_txoptions *opt;
1260 
1261 		sockopt_lock_sock(sk);
1262 		opt = rcu_dereference_protected(np->opt,
1263 						lockdep_sock_is_held(sk));
1264 		len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len);
1265 		sockopt_release_sock(sk);
1266 		/* check if ipv6_getsockopt_sticky() returns err code */
1267 		if (len < 0)
1268 			return len;
1269 		return copy_to_sockptr(optlen, &len, sizeof(int));
1270 	}
1271 
1272 	case IPV6_RECVHOPOPTS:
1273 		val = np->rxopt.bits.hopopts;
1274 		break;
1275 
1276 	case IPV6_2292HOPOPTS:
1277 		val = np->rxopt.bits.ohopopts;
1278 		break;
1279 
1280 	case IPV6_RECVDSTOPTS:
1281 		val = np->rxopt.bits.dstopts;
1282 		break;
1283 
1284 	case IPV6_2292DSTOPTS:
1285 		val = np->rxopt.bits.odstopts;
1286 		break;
1287 
1288 	case IPV6_TCLASS:
1289 		val = np->tclass;
1290 		break;
1291 
1292 	case IPV6_RECVTCLASS:
1293 		val = np->rxopt.bits.rxtclass;
1294 		break;
1295 
1296 	case IPV6_FLOWINFO:
1297 		val = np->rxopt.bits.rxflow;
1298 		break;
1299 
1300 	case IPV6_RECVPATHMTU:
1301 		val = np->rxopt.bits.rxpmtu;
1302 		break;
1303 
1304 	case IPV6_PATHMTU:
1305 	{
1306 		struct dst_entry *dst;
1307 		struct ip6_mtuinfo mtuinfo;
1308 
1309 		if (len < sizeof(mtuinfo))
1310 			return -EINVAL;
1311 
1312 		len = sizeof(mtuinfo);
1313 		memset(&mtuinfo, 0, sizeof(mtuinfo));
1314 
1315 		rcu_read_lock();
1316 		dst = __sk_dst_get(sk);
1317 		if (dst)
1318 			mtuinfo.ip6m_mtu = dst_mtu(dst);
1319 		rcu_read_unlock();
1320 		if (!mtuinfo.ip6m_mtu)
1321 			return -ENOTCONN;
1322 
1323 		if (copy_to_sockptr(optlen, &len, sizeof(int)))
1324 			return -EFAULT;
1325 		if (copy_to_sockptr(optval, &mtuinfo, len))
1326 			return -EFAULT;
1327 
1328 		return 0;
1329 	}
1330 
1331 	case IPV6_TRANSPARENT:
1332 		val = inet_sk(sk)->transparent;
1333 		break;
1334 
1335 	case IPV6_FREEBIND:
1336 		val = inet_sk(sk)->freebind;
1337 		break;
1338 
1339 	case IPV6_RECVORIGDSTADDR:
1340 		val = np->rxopt.bits.rxorigdstaddr;
1341 		break;
1342 
1343 	case IPV6_UNICAST_HOPS:
1344 	case IPV6_MULTICAST_HOPS:
1345 	{
1346 		struct dst_entry *dst;
1347 
1348 		if (optname == IPV6_UNICAST_HOPS)
1349 			val = np->hop_limit;
1350 		else
1351 			val = np->mcast_hops;
1352 
1353 		if (val < 0) {
1354 			rcu_read_lock();
1355 			dst = __sk_dst_get(sk);
1356 			if (dst)
1357 				val = ip6_dst_hoplimit(dst);
1358 			rcu_read_unlock();
1359 		}
1360 
1361 		if (val < 0)
1362 			val = sock_net(sk)->ipv6.devconf_all->hop_limit;
1363 		break;
1364 	}
1365 
1366 	case IPV6_MULTICAST_LOOP:
1367 		val = np->mc_loop;
1368 		break;
1369 
1370 	case IPV6_MULTICAST_IF:
1371 		val = np->mcast_oif;
1372 		break;
1373 
1374 	case IPV6_MULTICAST_ALL:
1375 		val = np->mc_all;
1376 		break;
1377 
1378 	case IPV6_UNICAST_IF:
1379 		val = (__force int)htonl((__u32) np->ucast_oif);
1380 		break;
1381 
1382 	case IPV6_MTU_DISCOVER:
1383 		val = np->pmtudisc;
1384 		break;
1385 
1386 	case IPV6_RECVERR:
1387 		val = np->recverr;
1388 		break;
1389 
1390 	case IPV6_FLOWINFO_SEND:
1391 		val = np->sndflow;
1392 		break;
1393 
1394 	case IPV6_FLOWLABEL_MGR:
1395 	{
1396 		struct in6_flowlabel_req freq;
1397 		int flags;
1398 
1399 		if (len < sizeof(freq))
1400 			return -EINVAL;
1401 
1402 		if (copy_from_sockptr(&freq, optval, sizeof(freq)))
1403 			return -EFAULT;
1404 
1405 		if (freq.flr_action != IPV6_FL_A_GET)
1406 			return -EINVAL;
1407 
1408 		len = sizeof(freq);
1409 		flags = freq.flr_flags;
1410 
1411 		memset(&freq, 0, sizeof(freq));
1412 
1413 		val = ipv6_flowlabel_opt_get(sk, &freq, flags);
1414 		if (val < 0)
1415 			return val;
1416 
1417 		if (copy_to_sockptr(optlen, &len, sizeof(int)))
1418 			return -EFAULT;
1419 		if (copy_to_sockptr(optval, &freq, len))
1420 			return -EFAULT;
1421 
1422 		return 0;
1423 	}
1424 
1425 	case IPV6_ADDR_PREFERENCES:
1426 		val = 0;
1427 
1428 		if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1429 			val |= IPV6_PREFER_SRC_TMP;
1430 		else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1431 			val |= IPV6_PREFER_SRC_PUBLIC;
1432 		else {
1433 			/* XXX: should we return system default? */
1434 			val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1435 		}
1436 
1437 		if (np->srcprefs & IPV6_PREFER_SRC_COA)
1438 			val |= IPV6_PREFER_SRC_COA;
1439 		else
1440 			val |= IPV6_PREFER_SRC_HOME;
1441 		break;
1442 
1443 	case IPV6_MINHOPCOUNT:
1444 		val = np->min_hopcount;
1445 		break;
1446 
1447 	case IPV6_DONTFRAG:
1448 		val = np->dontfrag;
1449 		break;
1450 
1451 	case IPV6_AUTOFLOWLABEL:
1452 		val = ip6_autoflowlabel(sock_net(sk), np);
1453 		break;
1454 
1455 	case IPV6_RECVFRAGSIZE:
1456 		val = np->rxopt.bits.recvfragsize;
1457 		break;
1458 
1459 	case IPV6_ROUTER_ALERT_ISOLATE:
1460 		val = np->rtalert_isolate;
1461 		break;
1462 
1463 	case IPV6_RECVERR_RFC4884:
1464 		val = np->recverr_rfc4884;
1465 		break;
1466 
1467 	default:
1468 		return -ENOPROTOOPT;
1469 	}
1470 	len = min_t(unsigned int, sizeof(int), len);
1471 	if (copy_to_sockptr(optlen, &len, sizeof(int)))
1472 		return -EFAULT;
1473 	if (copy_to_sockptr(optval, &val, len))
1474 		return -EFAULT;
1475 	return 0;
1476 }
1477 
1478 int ipv6_getsockopt(struct sock *sk, int level, int optname,
1479 		    char __user *optval, int __user *optlen)
1480 {
1481 	int err;
1482 
1483 	if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1484 		return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1485 
1486 	if (level != SOL_IPV6)
1487 		return -ENOPROTOOPT;
1488 
1489 	err = do_ipv6_getsockopt(sk, level, optname,
1490 				 USER_SOCKPTR(optval), USER_SOCKPTR(optlen));
1491 #ifdef CONFIG_NETFILTER
1492 	/* we need to exclude all possible ENOPROTOOPTs except default case */
1493 	if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1494 		int len;
1495 
1496 		if (get_user(len, optlen))
1497 			return -EFAULT;
1498 
1499 		err = nf_getsockopt(sk, PF_INET6, optname, optval, &len);
1500 		if (err >= 0)
1501 			err = put_user(len, optlen);
1502 	}
1503 #endif
1504 	return err;
1505 }
1506 EXPORT_SYMBOL(ipv6_getsockopt);
1507