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