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