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