xref: /openbmc/linux/net/ipv6/ipv6_sockglue.c (revision d9565bf4)
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 > 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 > 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 && !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 static 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 	lock_sock(sk);
421 
422 	switch (optname) {
423 
424 	case IPV6_ADDRFORM:
425 		if (optlen < sizeof(int))
426 			goto e_inval;
427 		if (val == PF_INET) {
428 			struct ipv6_txoptions *opt;
429 			struct sk_buff *pktopt;
430 
431 			if (sk->sk_type == SOCK_RAW)
432 				break;
433 
434 			if (sk->sk_protocol == IPPROTO_UDP ||
435 			    sk->sk_protocol == IPPROTO_UDPLITE) {
436 				struct udp_sock *up = udp_sk(sk);
437 				if (up->pending == AF_INET6) {
438 					retv = -EBUSY;
439 					break;
440 				}
441 			} else if (sk->sk_protocol == IPPROTO_TCP) {
442 				if (sk->sk_prot != &tcpv6_prot) {
443 					retv = -EBUSY;
444 					break;
445 				}
446 			} else {
447 				break;
448 			}
449 
450 			if (sk->sk_state != TCP_ESTABLISHED) {
451 				retv = -ENOTCONN;
452 				break;
453 			}
454 
455 			if (ipv6_only_sock(sk) ||
456 			    !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
457 				retv = -EADDRNOTAVAIL;
458 				break;
459 			}
460 
461 			fl6_free_socklist(sk);
462 			__ipv6_sock_mc_close(sk);
463 			__ipv6_sock_ac_close(sk);
464 
465 			/*
466 			 * Sock is moving from IPv6 to IPv4 (sk_prot), so
467 			 * remove it from the refcnt debug socks count in the
468 			 * original family...
469 			 */
470 			sk_refcnt_debug_dec(sk);
471 
472 			if (sk->sk_protocol == IPPROTO_TCP) {
473 				struct inet_connection_sock *icsk = inet_csk(sk);
474 
475 				sock_prot_inuse_add(net, sk->sk_prot, -1);
476 				sock_prot_inuse_add(net, &tcp_prot, 1);
477 
478 				sk->sk_prot = &tcp_prot;
479 				icsk->icsk_af_ops = &ipv4_specific;
480 				sk->sk_socket->ops = &inet_stream_ops;
481 				sk->sk_family = PF_INET;
482 				tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
483 			} else {
484 				struct proto *prot = &udp_prot;
485 
486 				if (sk->sk_protocol == IPPROTO_UDPLITE)
487 					prot = &udplite_prot;
488 
489 				sock_prot_inuse_add(net, sk->sk_prot, -1);
490 				sock_prot_inuse_add(net, prot, 1);
491 
492 				sk->sk_prot = prot;
493 				sk->sk_socket->ops = &inet_dgram_ops;
494 				sk->sk_family = PF_INET;
495 			}
496 			opt = xchg((__force struct ipv6_txoptions **)&np->opt,
497 				   NULL);
498 			if (opt) {
499 				atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
500 				txopt_put(opt);
501 			}
502 			pktopt = xchg(&np->pktoptions, NULL);
503 			kfree_skb(pktopt);
504 
505 			/*
506 			 * ... and add it to the refcnt debug socks count
507 			 * in the new family. -acme
508 			 */
509 			sk_refcnt_debug_inc(sk);
510 			module_put(THIS_MODULE);
511 			retv = 0;
512 			break;
513 		}
514 		goto e_inval;
515 
516 	case IPV6_V6ONLY:
517 		if (optlen < sizeof(int) ||
518 		    inet_sk(sk)->inet_num)
519 			goto e_inval;
520 		sk->sk_ipv6only = valbool;
521 		retv = 0;
522 		break;
523 
524 	case IPV6_RECVPKTINFO:
525 		if (optlen < sizeof(int))
526 			goto e_inval;
527 		np->rxopt.bits.rxinfo = valbool;
528 		retv = 0;
529 		break;
530 
531 	case IPV6_2292PKTINFO:
532 		if (optlen < sizeof(int))
533 			goto e_inval;
534 		np->rxopt.bits.rxoinfo = valbool;
535 		retv = 0;
536 		break;
537 
538 	case IPV6_RECVHOPLIMIT:
539 		if (optlen < sizeof(int))
540 			goto e_inval;
541 		np->rxopt.bits.rxhlim = valbool;
542 		retv = 0;
543 		break;
544 
545 	case IPV6_2292HOPLIMIT:
546 		if (optlen < sizeof(int))
547 			goto e_inval;
548 		np->rxopt.bits.rxohlim = valbool;
549 		retv = 0;
550 		break;
551 
552 	case IPV6_RECVRTHDR:
553 		if (optlen < sizeof(int))
554 			goto e_inval;
555 		np->rxopt.bits.srcrt = valbool;
556 		retv = 0;
557 		break;
558 
559 	case IPV6_2292RTHDR:
560 		if (optlen < sizeof(int))
561 			goto e_inval;
562 		np->rxopt.bits.osrcrt = valbool;
563 		retv = 0;
564 		break;
565 
566 	case IPV6_RECVHOPOPTS:
567 		if (optlen < sizeof(int))
568 			goto e_inval;
569 		np->rxopt.bits.hopopts = valbool;
570 		retv = 0;
571 		break;
572 
573 	case IPV6_2292HOPOPTS:
574 		if (optlen < sizeof(int))
575 			goto e_inval;
576 		np->rxopt.bits.ohopopts = valbool;
577 		retv = 0;
578 		break;
579 
580 	case IPV6_RECVDSTOPTS:
581 		if (optlen < sizeof(int))
582 			goto e_inval;
583 		np->rxopt.bits.dstopts = valbool;
584 		retv = 0;
585 		break;
586 
587 	case IPV6_2292DSTOPTS:
588 		if (optlen < sizeof(int))
589 			goto e_inval;
590 		np->rxopt.bits.odstopts = valbool;
591 		retv = 0;
592 		break;
593 
594 	case IPV6_TCLASS:
595 		if (optlen < sizeof(int))
596 			goto e_inval;
597 		if (val < -1 || val > 0xff)
598 			goto e_inval;
599 		/* RFC 3542, 6.5: default traffic class of 0x0 */
600 		if (val == -1)
601 			val = 0;
602 		if (sk->sk_type == SOCK_STREAM) {
603 			val &= ~INET_ECN_MASK;
604 			val |= np->tclass & INET_ECN_MASK;
605 		}
606 		if (np->tclass != val) {
607 			np->tclass = val;
608 			sk_dst_reset(sk);
609 		}
610 		retv = 0;
611 		break;
612 
613 	case IPV6_RECVTCLASS:
614 		if (optlen < sizeof(int))
615 			goto e_inval;
616 		np->rxopt.bits.rxtclass = valbool;
617 		retv = 0;
618 		break;
619 
620 	case IPV6_FLOWINFO:
621 		if (optlen < sizeof(int))
622 			goto e_inval;
623 		np->rxopt.bits.rxflow = valbool;
624 		retv = 0;
625 		break;
626 
627 	case IPV6_RECVPATHMTU:
628 		if (optlen < sizeof(int))
629 			goto e_inval;
630 		np->rxopt.bits.rxpmtu = valbool;
631 		retv = 0;
632 		break;
633 
634 	case IPV6_TRANSPARENT:
635 		if (valbool && !ns_capable(net->user_ns, CAP_NET_RAW) &&
636 		    !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
637 			retv = -EPERM;
638 			break;
639 		}
640 		if (optlen < sizeof(int))
641 			goto e_inval;
642 		/* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
643 		inet_sk(sk)->transparent = valbool;
644 		retv = 0;
645 		break;
646 
647 	case IPV6_FREEBIND:
648 		if (optlen < sizeof(int))
649 			goto e_inval;
650 		/* we also don't have a separate freebind bit for IPV6 */
651 		inet_sk(sk)->freebind = valbool;
652 		retv = 0;
653 		break;
654 
655 	case IPV6_RECVORIGDSTADDR:
656 		if (optlen < sizeof(int))
657 			goto e_inval;
658 		np->rxopt.bits.rxorigdstaddr = valbool;
659 		retv = 0;
660 		break;
661 
662 	case IPV6_HOPOPTS:
663 	case IPV6_RTHDRDSTOPTS:
664 	case IPV6_RTHDR:
665 	case IPV6_DSTOPTS:
666 		retv = ipv6_set_opt_hdr(sk, optname, optval, optlen);
667 		break;
668 
669 	case IPV6_PKTINFO:
670 	{
671 		struct in6_pktinfo pkt;
672 
673 		if (optlen == 0)
674 			goto e_inval;
675 		else if (optlen < sizeof(struct in6_pktinfo) ||
676 			 sockptr_is_null(optval))
677 			goto e_inval;
678 
679 		if (copy_from_sockptr(&pkt, optval, sizeof(pkt))) {
680 			retv = -EFAULT;
681 			break;
682 		}
683 		if (!sk_dev_equal_l3scope(sk, pkt.ipi6_ifindex))
684 			goto e_inval;
685 
686 		np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
687 		np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
688 		retv = 0;
689 		break;
690 	}
691 
692 	case IPV6_2292PKTOPTIONS:
693 	{
694 		struct ipv6_txoptions *opt = NULL;
695 		struct msghdr msg;
696 		struct flowi6 fl6;
697 		struct ipcm6_cookie ipc6;
698 
699 		memset(&fl6, 0, sizeof(fl6));
700 		fl6.flowi6_oif = sk->sk_bound_dev_if;
701 		fl6.flowi6_mark = sk->sk_mark;
702 
703 		if (optlen == 0)
704 			goto update;
705 
706 		/* 1K is probably excessive
707 		 * 1K is surely not enough, 2K per standard header is 16K.
708 		 */
709 		retv = -EINVAL;
710 		if (optlen > 64*1024)
711 			break;
712 
713 		opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
714 		retv = -ENOBUFS;
715 		if (!opt)
716 			break;
717 
718 		memset(opt, 0, sizeof(*opt));
719 		refcount_set(&opt->refcnt, 1);
720 		opt->tot_len = sizeof(*opt) + optlen;
721 		retv = -EFAULT;
722 		if (copy_from_sockptr(opt + 1, optval, optlen))
723 			goto done;
724 
725 		msg.msg_controllen = optlen;
726 		msg.msg_control = (void *)(opt+1);
727 		ipc6.opt = opt;
728 
729 		retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6);
730 		if (retv)
731 			goto done;
732 update:
733 		retv = 0;
734 		opt = ipv6_update_options(sk, opt);
735 done:
736 		if (opt) {
737 			atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
738 			txopt_put(opt);
739 		}
740 		break;
741 	}
742 	case IPV6_UNICAST_HOPS:
743 		if (optlen < sizeof(int))
744 			goto e_inval;
745 		if (val > 255 || val < -1)
746 			goto e_inval;
747 		np->hop_limit = val;
748 		retv = 0;
749 		break;
750 
751 	case IPV6_MULTICAST_HOPS:
752 		if (sk->sk_type == SOCK_STREAM)
753 			break;
754 		if (optlen < sizeof(int))
755 			goto e_inval;
756 		if (val > 255 || val < -1)
757 			goto e_inval;
758 		np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
759 		retv = 0;
760 		break;
761 
762 	case IPV6_MULTICAST_LOOP:
763 		if (optlen < sizeof(int))
764 			goto e_inval;
765 		if (val != valbool)
766 			goto e_inval;
767 		np->mc_loop = valbool;
768 		retv = 0;
769 		break;
770 
771 	case IPV6_UNICAST_IF:
772 	{
773 		struct net_device *dev = NULL;
774 		int ifindex;
775 
776 		if (optlen != sizeof(int))
777 			goto e_inval;
778 
779 		ifindex = (__force int)ntohl((__force __be32)val);
780 		if (ifindex == 0) {
781 			np->ucast_oif = 0;
782 			retv = 0;
783 			break;
784 		}
785 
786 		dev = dev_get_by_index(net, ifindex);
787 		retv = -EADDRNOTAVAIL;
788 		if (!dev)
789 			break;
790 		dev_put(dev);
791 
792 		retv = -EINVAL;
793 		if (sk->sk_bound_dev_if)
794 			break;
795 
796 		np->ucast_oif = ifindex;
797 		retv = 0;
798 		break;
799 	}
800 
801 	case IPV6_MULTICAST_IF:
802 		if (sk->sk_type == SOCK_STREAM)
803 			break;
804 		if (optlen < sizeof(int))
805 			goto e_inval;
806 
807 		if (val) {
808 			struct net_device *dev;
809 			int midx;
810 
811 			rcu_read_lock();
812 
813 			dev = dev_get_by_index_rcu(net, val);
814 			if (!dev) {
815 				rcu_read_unlock();
816 				retv = -ENODEV;
817 				break;
818 			}
819 			midx = l3mdev_master_ifindex_rcu(dev);
820 
821 			rcu_read_unlock();
822 
823 			if (sk->sk_bound_dev_if &&
824 			    sk->sk_bound_dev_if != val &&
825 			    (!midx || midx != sk->sk_bound_dev_if))
826 				goto e_inval;
827 		}
828 		np->mcast_oif = val;
829 		retv = 0;
830 		break;
831 	case IPV6_ADD_MEMBERSHIP:
832 	case IPV6_DROP_MEMBERSHIP:
833 	{
834 		struct ipv6_mreq mreq;
835 
836 		if (optlen < sizeof(struct ipv6_mreq))
837 			goto e_inval;
838 
839 		retv = -EPROTO;
840 		if (inet_sk(sk)->is_icsk)
841 			break;
842 
843 		retv = -EFAULT;
844 		if (copy_from_sockptr(&mreq, optval, sizeof(struct ipv6_mreq)))
845 			break;
846 
847 		if (optname == IPV6_ADD_MEMBERSHIP)
848 			retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
849 		else
850 			retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
851 		break;
852 	}
853 	case IPV6_JOIN_ANYCAST:
854 	case IPV6_LEAVE_ANYCAST:
855 	{
856 		struct ipv6_mreq mreq;
857 
858 		if (optlen < sizeof(struct ipv6_mreq))
859 			goto e_inval;
860 
861 		retv = -EFAULT;
862 		if (copy_from_sockptr(&mreq, optval, sizeof(struct ipv6_mreq)))
863 			break;
864 
865 		if (optname == IPV6_JOIN_ANYCAST)
866 			retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
867 		else
868 			retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
869 		break;
870 	}
871 	case IPV6_MULTICAST_ALL:
872 		if (optlen < sizeof(int))
873 			goto e_inval;
874 		np->mc_all = valbool;
875 		retv = 0;
876 		break;
877 
878 	case MCAST_JOIN_GROUP:
879 	case MCAST_LEAVE_GROUP:
880 		if (in_compat_syscall())
881 			retv = compat_ipv6_mcast_join_leave(sk, optname, optval,
882 							    optlen);
883 		else
884 			retv = ipv6_mcast_join_leave(sk, optname, optval,
885 						     optlen);
886 		break;
887 	case MCAST_JOIN_SOURCE_GROUP:
888 	case MCAST_LEAVE_SOURCE_GROUP:
889 	case MCAST_BLOCK_SOURCE:
890 	case MCAST_UNBLOCK_SOURCE:
891 		retv = do_ipv6_mcast_group_source(sk, optname, optval, optlen);
892 		break;
893 	case MCAST_MSFILTER:
894 		if (in_compat_syscall())
895 			retv = compat_ipv6_set_mcast_msfilter(sk, optval,
896 							      optlen);
897 		else
898 			retv = ipv6_set_mcast_msfilter(sk, optval, optlen);
899 		break;
900 	case IPV6_ROUTER_ALERT:
901 		if (optlen < sizeof(int))
902 			goto e_inval;
903 		retv = ip6_ra_control(sk, val);
904 		break;
905 	case IPV6_ROUTER_ALERT_ISOLATE:
906 		if (optlen < sizeof(int))
907 			goto e_inval;
908 		np->rtalert_isolate = valbool;
909 		retv = 0;
910 		break;
911 	case IPV6_MTU_DISCOVER:
912 		if (optlen < sizeof(int))
913 			goto e_inval;
914 		if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
915 			goto e_inval;
916 		np->pmtudisc = val;
917 		retv = 0;
918 		break;
919 	case IPV6_MTU:
920 		if (optlen < sizeof(int))
921 			goto e_inval;
922 		if (val && val < IPV6_MIN_MTU)
923 			goto e_inval;
924 		np->frag_size = val;
925 		retv = 0;
926 		break;
927 	case IPV6_RECVERR:
928 		if (optlen < sizeof(int))
929 			goto e_inval;
930 		np->recverr = valbool;
931 		if (!val)
932 			skb_queue_purge(&sk->sk_error_queue);
933 		retv = 0;
934 		break;
935 	case IPV6_FLOWINFO_SEND:
936 		if (optlen < sizeof(int))
937 			goto e_inval;
938 		np->sndflow = valbool;
939 		retv = 0;
940 		break;
941 	case IPV6_FLOWLABEL_MGR:
942 		retv = ipv6_flowlabel_opt(sk, optval, optlen);
943 		break;
944 	case IPV6_IPSEC_POLICY:
945 	case IPV6_XFRM_POLICY:
946 		retv = -EPERM;
947 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
948 			break;
949 		retv = xfrm_user_policy(sk, optname, optval, optlen);
950 		break;
951 
952 	case IPV6_ADDR_PREFERENCES:
953 		if (optlen < sizeof(int))
954 			goto e_inval;
955 		retv = __ip6_sock_set_addr_preferences(sk, val);
956 		break;
957 	case IPV6_MINHOPCOUNT:
958 		if (optlen < sizeof(int))
959 			goto e_inval;
960 		if (val < 0 || val > 255)
961 			goto e_inval;
962 
963 		if (val)
964 			static_branch_enable(&ip6_min_hopcount);
965 
966 		/* tcp_v6_err() and tcp_v6_rcv() might read min_hopcount
967 		 * while we are changing it.
968 		 */
969 		WRITE_ONCE(np->min_hopcount, val);
970 		retv = 0;
971 		break;
972 	case IPV6_DONTFRAG:
973 		np->dontfrag = valbool;
974 		retv = 0;
975 		break;
976 	case IPV6_AUTOFLOWLABEL:
977 		np->autoflowlabel = valbool;
978 		np->autoflowlabel_set = 1;
979 		retv = 0;
980 		break;
981 	case IPV6_RECVFRAGSIZE:
982 		np->rxopt.bits.recvfragsize = valbool;
983 		retv = 0;
984 		break;
985 	case IPV6_RECVERR_RFC4884:
986 		if (optlen < sizeof(int))
987 			goto e_inval;
988 		if (val < 0 || val > 1)
989 			goto e_inval;
990 		np->recverr_rfc4884 = valbool;
991 		retv = 0;
992 		break;
993 	}
994 
995 	release_sock(sk);
996 	if (needs_rtnl)
997 		rtnl_unlock();
998 
999 	return retv;
1000 
1001 e_inval:
1002 	release_sock(sk);
1003 	if (needs_rtnl)
1004 		rtnl_unlock();
1005 	return -EINVAL;
1006 }
1007 
1008 int ipv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
1009 		    unsigned int optlen)
1010 {
1011 	int err;
1012 
1013 	if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1014 		return udp_prot.setsockopt(sk, level, optname, optval, optlen);
1015 
1016 	if (level != SOL_IPV6)
1017 		return -ENOPROTOOPT;
1018 
1019 	err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
1020 #ifdef CONFIG_NETFILTER
1021 	/* we need to exclude all possible ENOPROTOOPTs except default case */
1022 	if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
1023 			optname != IPV6_XFRM_POLICY)
1024 		err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen);
1025 #endif
1026 	return err;
1027 }
1028 EXPORT_SYMBOL(ipv6_setsockopt);
1029 
1030 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
1031 				  int optname, char __user *optval, int len)
1032 {
1033 	struct ipv6_opt_hdr *hdr;
1034 
1035 	if (!opt)
1036 		return 0;
1037 
1038 	switch (optname) {
1039 	case IPV6_HOPOPTS:
1040 		hdr = opt->hopopt;
1041 		break;
1042 	case IPV6_RTHDRDSTOPTS:
1043 		hdr = opt->dst0opt;
1044 		break;
1045 	case IPV6_RTHDR:
1046 		hdr = (struct ipv6_opt_hdr *)opt->srcrt;
1047 		break;
1048 	case IPV6_DSTOPTS:
1049 		hdr = opt->dst1opt;
1050 		break;
1051 	default:
1052 		return -EINVAL;	/* should not happen */
1053 	}
1054 
1055 	if (!hdr)
1056 		return 0;
1057 
1058 	len = min_t(unsigned int, len, ipv6_optlen(hdr));
1059 	if (copy_to_user(optval, hdr, len))
1060 		return -EFAULT;
1061 	return len;
1062 }
1063 
1064 static int ipv6_get_msfilter(struct sock *sk, void __user *optval,
1065 		int __user *optlen, int len)
1066 {
1067 	const int size0 = offsetof(struct group_filter, gf_slist_flex);
1068 	struct group_filter __user *p = optval;
1069 	struct group_filter gsf;
1070 	int num;
1071 	int err;
1072 
1073 	if (len < size0)
1074 		return -EINVAL;
1075 	if (copy_from_user(&gsf, p, size0))
1076 		return -EFAULT;
1077 	if (gsf.gf_group.ss_family != AF_INET6)
1078 		return -EADDRNOTAVAIL;
1079 	num = gsf.gf_numsrc;
1080 	lock_sock(sk);
1081 	err = ip6_mc_msfget(sk, &gsf, p->gf_slist_flex);
1082 	if (!err) {
1083 		if (num > gsf.gf_numsrc)
1084 			num = gsf.gf_numsrc;
1085 		if (put_user(GROUP_FILTER_SIZE(num), optlen) ||
1086 		    copy_to_user(p, &gsf, size0))
1087 			err = -EFAULT;
1088 	}
1089 	release_sock(sk);
1090 	return err;
1091 }
1092 
1093 static int compat_ipv6_get_msfilter(struct sock *sk, void __user *optval,
1094 		int __user *optlen)
1095 {
1096 	const int size0 = offsetof(struct compat_group_filter, gf_slist_flex);
1097 	struct compat_group_filter __user *p = optval;
1098 	struct compat_group_filter gf32;
1099 	struct group_filter gf;
1100 	int len, err;
1101 	int num;
1102 
1103 	if (get_user(len, optlen))
1104 		return -EFAULT;
1105 	if (len < size0)
1106 		return -EINVAL;
1107 
1108 	if (copy_from_user(&gf32, p, size0))
1109 		return -EFAULT;
1110 	gf.gf_interface = gf32.gf_interface;
1111 	gf.gf_fmode = gf32.gf_fmode;
1112 	num = gf.gf_numsrc = gf32.gf_numsrc;
1113 	gf.gf_group = gf32.gf_group;
1114 
1115 	if (gf.gf_group.ss_family != AF_INET6)
1116 		return -EADDRNOTAVAIL;
1117 
1118 	lock_sock(sk);
1119 	err = ip6_mc_msfget(sk, &gf, p->gf_slist_flex);
1120 	release_sock(sk);
1121 	if (err)
1122 		return err;
1123 	if (num > gf.gf_numsrc)
1124 		num = gf.gf_numsrc;
1125 	len = GROUP_FILTER_SIZE(num) - (sizeof(gf)-sizeof(gf32));
1126 	if (put_user(len, optlen) ||
1127 	    put_user(gf.gf_fmode, &p->gf_fmode) ||
1128 	    put_user(gf.gf_numsrc, &p->gf_numsrc))
1129 		return -EFAULT;
1130 	return 0;
1131 }
1132 
1133 static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
1134 		    char __user *optval, int __user *optlen, unsigned int flags)
1135 {
1136 	struct ipv6_pinfo *np = inet6_sk(sk);
1137 	int len;
1138 	int val;
1139 
1140 	if (ip6_mroute_opt(optname))
1141 		return ip6_mroute_getsockopt(sk, optname, optval, optlen);
1142 
1143 	if (get_user(len, optlen))
1144 		return -EFAULT;
1145 	switch (optname) {
1146 	case IPV6_ADDRFORM:
1147 		if (sk->sk_protocol != IPPROTO_UDP &&
1148 		    sk->sk_protocol != IPPROTO_UDPLITE &&
1149 		    sk->sk_protocol != IPPROTO_TCP)
1150 			return -ENOPROTOOPT;
1151 		if (sk->sk_state != TCP_ESTABLISHED)
1152 			return -ENOTCONN;
1153 		val = sk->sk_family;
1154 		break;
1155 	case MCAST_MSFILTER:
1156 		if (in_compat_syscall())
1157 			return compat_ipv6_get_msfilter(sk, optval, optlen);
1158 		return ipv6_get_msfilter(sk, optval, optlen, len);
1159 	case IPV6_2292PKTOPTIONS:
1160 	{
1161 		struct msghdr msg;
1162 		struct sk_buff *skb;
1163 
1164 		if (sk->sk_type != SOCK_STREAM)
1165 			return -ENOPROTOOPT;
1166 
1167 		msg.msg_control_user = optval;
1168 		msg.msg_controllen = len;
1169 		msg.msg_flags = flags;
1170 		msg.msg_control_is_user = true;
1171 
1172 		lock_sock(sk);
1173 		skb = np->pktoptions;
1174 		if (skb)
1175 			ip6_datagram_recv_ctl(sk, &msg, skb);
1176 		release_sock(sk);
1177 		if (!skb) {
1178 			if (np->rxopt.bits.rxinfo) {
1179 				struct in6_pktinfo src_info;
1180 				src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1181 					np->sticky_pktinfo.ipi6_ifindex;
1182 				src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
1183 				put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1184 			}
1185 			if (np->rxopt.bits.rxhlim) {
1186 				int hlim = np->mcast_hops;
1187 				put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1188 			}
1189 			if (np->rxopt.bits.rxtclass) {
1190 				int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1191 
1192 				put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1193 			}
1194 			if (np->rxopt.bits.rxoinfo) {
1195 				struct in6_pktinfo src_info;
1196 				src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1197 					np->sticky_pktinfo.ipi6_ifindex;
1198 				src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1199 								     np->sticky_pktinfo.ipi6_addr;
1200 				put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1201 			}
1202 			if (np->rxopt.bits.rxohlim) {
1203 				int hlim = np->mcast_hops;
1204 				put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1205 			}
1206 			if (np->rxopt.bits.rxflow) {
1207 				__be32 flowinfo = np->rcv_flowinfo;
1208 
1209 				put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1210 			}
1211 		}
1212 		len -= msg.msg_controllen;
1213 		return put_user(len, optlen);
1214 	}
1215 	case IPV6_MTU:
1216 	{
1217 		struct dst_entry *dst;
1218 
1219 		val = 0;
1220 		rcu_read_lock();
1221 		dst = __sk_dst_get(sk);
1222 		if (dst)
1223 			val = dst_mtu(dst);
1224 		rcu_read_unlock();
1225 		if (!val)
1226 			return -ENOTCONN;
1227 		break;
1228 	}
1229 
1230 	case IPV6_V6ONLY:
1231 		val = sk->sk_ipv6only;
1232 		break;
1233 
1234 	case IPV6_RECVPKTINFO:
1235 		val = np->rxopt.bits.rxinfo;
1236 		break;
1237 
1238 	case IPV6_2292PKTINFO:
1239 		val = np->rxopt.bits.rxoinfo;
1240 		break;
1241 
1242 	case IPV6_RECVHOPLIMIT:
1243 		val = np->rxopt.bits.rxhlim;
1244 		break;
1245 
1246 	case IPV6_2292HOPLIMIT:
1247 		val = np->rxopt.bits.rxohlim;
1248 		break;
1249 
1250 	case IPV6_RECVRTHDR:
1251 		val = np->rxopt.bits.srcrt;
1252 		break;
1253 
1254 	case IPV6_2292RTHDR:
1255 		val = np->rxopt.bits.osrcrt;
1256 		break;
1257 
1258 	case IPV6_HOPOPTS:
1259 	case IPV6_RTHDRDSTOPTS:
1260 	case IPV6_RTHDR:
1261 	case IPV6_DSTOPTS:
1262 	{
1263 		struct ipv6_txoptions *opt;
1264 
1265 		lock_sock(sk);
1266 		opt = rcu_dereference_protected(np->opt,
1267 						lockdep_sock_is_held(sk));
1268 		len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len);
1269 		release_sock(sk);
1270 		/* check if ipv6_getsockopt_sticky() returns err code */
1271 		if (len < 0)
1272 			return len;
1273 		return put_user(len, optlen);
1274 	}
1275 
1276 	case IPV6_RECVHOPOPTS:
1277 		val = np->rxopt.bits.hopopts;
1278 		break;
1279 
1280 	case IPV6_2292HOPOPTS:
1281 		val = np->rxopt.bits.ohopopts;
1282 		break;
1283 
1284 	case IPV6_RECVDSTOPTS:
1285 		val = np->rxopt.bits.dstopts;
1286 		break;
1287 
1288 	case IPV6_2292DSTOPTS:
1289 		val = np->rxopt.bits.odstopts;
1290 		break;
1291 
1292 	case IPV6_TCLASS:
1293 		val = np->tclass;
1294 		break;
1295 
1296 	case IPV6_RECVTCLASS:
1297 		val = np->rxopt.bits.rxtclass;
1298 		break;
1299 
1300 	case IPV6_FLOWINFO:
1301 		val = np->rxopt.bits.rxflow;
1302 		break;
1303 
1304 	case IPV6_RECVPATHMTU:
1305 		val = np->rxopt.bits.rxpmtu;
1306 		break;
1307 
1308 	case IPV6_PATHMTU:
1309 	{
1310 		struct dst_entry *dst;
1311 		struct ip6_mtuinfo mtuinfo;
1312 
1313 		if (len < sizeof(mtuinfo))
1314 			return -EINVAL;
1315 
1316 		len = sizeof(mtuinfo);
1317 		memset(&mtuinfo, 0, sizeof(mtuinfo));
1318 
1319 		rcu_read_lock();
1320 		dst = __sk_dst_get(sk);
1321 		if (dst)
1322 			mtuinfo.ip6m_mtu = dst_mtu(dst);
1323 		rcu_read_unlock();
1324 		if (!mtuinfo.ip6m_mtu)
1325 			return -ENOTCONN;
1326 
1327 		if (put_user(len, optlen))
1328 			return -EFAULT;
1329 		if (copy_to_user(optval, &mtuinfo, len))
1330 			return -EFAULT;
1331 
1332 		return 0;
1333 	}
1334 
1335 	case IPV6_TRANSPARENT:
1336 		val = inet_sk(sk)->transparent;
1337 		break;
1338 
1339 	case IPV6_FREEBIND:
1340 		val = inet_sk(sk)->freebind;
1341 		break;
1342 
1343 	case IPV6_RECVORIGDSTADDR:
1344 		val = np->rxopt.bits.rxorigdstaddr;
1345 		break;
1346 
1347 	case IPV6_UNICAST_HOPS:
1348 	case IPV6_MULTICAST_HOPS:
1349 	{
1350 		struct dst_entry *dst;
1351 
1352 		if (optname == IPV6_UNICAST_HOPS)
1353 			val = np->hop_limit;
1354 		else
1355 			val = np->mcast_hops;
1356 
1357 		if (val < 0) {
1358 			rcu_read_lock();
1359 			dst = __sk_dst_get(sk);
1360 			if (dst)
1361 				val = ip6_dst_hoplimit(dst);
1362 			rcu_read_unlock();
1363 		}
1364 
1365 		if (val < 0)
1366 			val = sock_net(sk)->ipv6.devconf_all->hop_limit;
1367 		break;
1368 	}
1369 
1370 	case IPV6_MULTICAST_LOOP:
1371 		val = np->mc_loop;
1372 		break;
1373 
1374 	case IPV6_MULTICAST_IF:
1375 		val = np->mcast_oif;
1376 		break;
1377 
1378 	case IPV6_MULTICAST_ALL:
1379 		val = np->mc_all;
1380 		break;
1381 
1382 	case IPV6_UNICAST_IF:
1383 		val = (__force int)htonl((__u32) np->ucast_oif);
1384 		break;
1385 
1386 	case IPV6_MTU_DISCOVER:
1387 		val = np->pmtudisc;
1388 		break;
1389 
1390 	case IPV6_RECVERR:
1391 		val = np->recverr;
1392 		break;
1393 
1394 	case IPV6_FLOWINFO_SEND:
1395 		val = np->sndflow;
1396 		break;
1397 
1398 	case IPV6_FLOWLABEL_MGR:
1399 	{
1400 		struct in6_flowlabel_req freq;
1401 		int flags;
1402 
1403 		if (len < sizeof(freq))
1404 			return -EINVAL;
1405 
1406 		if (copy_from_user(&freq, optval, sizeof(freq)))
1407 			return -EFAULT;
1408 
1409 		if (freq.flr_action != IPV6_FL_A_GET)
1410 			return -EINVAL;
1411 
1412 		len = sizeof(freq);
1413 		flags = freq.flr_flags;
1414 
1415 		memset(&freq, 0, sizeof(freq));
1416 
1417 		val = ipv6_flowlabel_opt_get(sk, &freq, flags);
1418 		if (val < 0)
1419 			return val;
1420 
1421 		if (put_user(len, optlen))
1422 			return -EFAULT;
1423 		if (copy_to_user(optval, &freq, len))
1424 			return -EFAULT;
1425 
1426 		return 0;
1427 	}
1428 
1429 	case IPV6_ADDR_PREFERENCES:
1430 		val = 0;
1431 
1432 		if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1433 			val |= IPV6_PREFER_SRC_TMP;
1434 		else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1435 			val |= IPV6_PREFER_SRC_PUBLIC;
1436 		else {
1437 			/* XXX: should we return system default? */
1438 			val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1439 		}
1440 
1441 		if (np->srcprefs & IPV6_PREFER_SRC_COA)
1442 			val |= IPV6_PREFER_SRC_COA;
1443 		else
1444 			val |= IPV6_PREFER_SRC_HOME;
1445 		break;
1446 
1447 	case IPV6_MINHOPCOUNT:
1448 		val = np->min_hopcount;
1449 		break;
1450 
1451 	case IPV6_DONTFRAG:
1452 		val = np->dontfrag;
1453 		break;
1454 
1455 	case IPV6_AUTOFLOWLABEL:
1456 		val = ip6_autoflowlabel(sock_net(sk), np);
1457 		break;
1458 
1459 	case IPV6_RECVFRAGSIZE:
1460 		val = np->rxopt.bits.recvfragsize;
1461 		break;
1462 
1463 	case IPV6_ROUTER_ALERT_ISOLATE:
1464 		val = np->rtalert_isolate;
1465 		break;
1466 
1467 	case IPV6_RECVERR_RFC4884:
1468 		val = np->recverr_rfc4884;
1469 		break;
1470 
1471 	default:
1472 		return -ENOPROTOOPT;
1473 	}
1474 	len = min_t(unsigned int, sizeof(int), len);
1475 	if (put_user(len, optlen))
1476 		return -EFAULT;
1477 	if (copy_to_user(optval, &val, len))
1478 		return -EFAULT;
1479 	return 0;
1480 }
1481 
1482 int ipv6_getsockopt(struct sock *sk, int level, int optname,
1483 		    char __user *optval, int __user *optlen)
1484 {
1485 	int err;
1486 
1487 	if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1488 		return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1489 
1490 	if (level != SOL_IPV6)
1491 		return -ENOPROTOOPT;
1492 
1493 	err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
1494 #ifdef CONFIG_NETFILTER
1495 	/* we need to exclude all possible ENOPROTOOPTs except default case */
1496 	if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1497 		int len;
1498 
1499 		if (get_user(len, optlen))
1500 			return -EFAULT;
1501 
1502 		err = nf_getsockopt(sk, PF_INET6, optname, optval, &len);
1503 		if (err >= 0)
1504 			err = put_user(len, optlen);
1505 	}
1506 #endif
1507 	return err;
1508 }
1509 EXPORT_SYMBOL(ipv6_getsockopt);
1510