• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ip6_ra_control(struct sock * sk,int sel)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 
ipv6_update_options(struct sock * sk,struct ipv6_txoptions * opt)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 
setsockopt_needs_rtnl(int optname)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 
copy_group_source_from_sockptr(struct group_source_req * greqs,sockptr_t optval,int optlen)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 
do_ipv6_mcast_group_source(struct sock * sk,int optname,sockptr_t optval,int optlen)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 
ipv6_set_mcast_msfilter(struct sock * sk,sockptr_t optval,int optlen)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 
compat_ipv6_set_mcast_msfilter(struct sock * sk,sockptr_t optval,int optlen)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 
ipv6_mcast_join_leave(struct sock * sk,int optname,sockptr_t optval,int optlen)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 
compat_ipv6_mcast_join_leave(struct sock * sk,int optname,sockptr_t optval,int optlen)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 
ipv6_set_opt_hdr(struct sock * sk,int optname,sockptr_t optval,int optlen)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 
do_ipv6_setsockopt(struct sock * sk,int level,int optname,sockptr_t optval,unsigned int optlen)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 	/* Another thread has converted the socket into IPv4 with
421 	 * IPV6_ADDRFORM concurrently.
422 	 */
423 	if (unlikely(sk->sk_family != AF_INET6))
424 		goto unlock;
425 
426 	switch (optname) {
427 
428 	case IPV6_ADDRFORM:
429 		if (optlen < sizeof(int))
430 			goto e_inval;
431 		if (val == PF_INET) {
432 			struct ipv6_txoptions *opt;
433 			struct sk_buff *pktopt;
434 
435 			if (sk->sk_type == SOCK_RAW)
436 				break;
437 
438 			if (sk->sk_protocol == IPPROTO_UDP ||
439 			    sk->sk_protocol == IPPROTO_UDPLITE) {
440 				struct udp_sock *up = udp_sk(sk);
441 				if (up->pending == AF_INET6) {
442 					retv = -EBUSY;
443 					break;
444 				}
445 			} else if (sk->sk_protocol == IPPROTO_TCP) {
446 				if (sk->sk_prot != &tcpv6_prot) {
447 					retv = -EBUSY;
448 					break;
449 				}
450 			} else {
451 				break;
452 			}
453 
454 			if (sk->sk_state != TCP_ESTABLISHED) {
455 				retv = -ENOTCONN;
456 				break;
457 			}
458 
459 			if (ipv6_only_sock(sk) ||
460 			    !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
461 				retv = -EADDRNOTAVAIL;
462 				break;
463 			}
464 
465 			fl6_free_socklist(sk);
466 			__ipv6_sock_mc_close(sk);
467 			__ipv6_sock_ac_close(sk);
468 
469 			/*
470 			 * Sock is moving from IPv6 to IPv4 (sk_prot), so
471 			 * remove it from the refcnt debug socks count in the
472 			 * original family...
473 			 */
474 			sk_refcnt_debug_dec(sk);
475 
476 			if (sk->sk_protocol == IPPROTO_TCP) {
477 				struct inet_connection_sock *icsk = inet_csk(sk);
478 				local_bh_disable();
479 				sock_prot_inuse_add(net, sk->sk_prot, -1);
480 				sock_prot_inuse_add(net, &tcp_prot, 1);
481 				local_bh_enable();
482 				/* Paired with READ_ONCE(sk->sk_prot) in inet6_stream_ops */
483 				WRITE_ONCE(sk->sk_prot, &tcp_prot);
484 				/* Paired with READ_ONCE() in tcp_(get|set)sockopt() */
485 				WRITE_ONCE(icsk->icsk_af_ops, &ipv4_specific);
486 				sk->sk_socket->ops = &inet_stream_ops;
487 				sk->sk_family = PF_INET;
488 				tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
489 			} else {
490 				struct proto *prot = &udp_prot;
491 
492 				if (sk->sk_protocol == IPPROTO_UDPLITE)
493 					prot = &udplite_prot;
494 				local_bh_disable();
495 				sock_prot_inuse_add(net, sk->sk_prot, -1);
496 				sock_prot_inuse_add(net, prot, 1);
497 				local_bh_enable();
498 				/* Paired with READ_ONCE(sk->sk_prot) in inet6_dgram_ops */
499 				WRITE_ONCE(sk->sk_prot, prot);
500 				sk->sk_socket->ops = &inet_dgram_ops;
501 				sk->sk_family = PF_INET;
502 			}
503 			opt = xchg((__force struct ipv6_txoptions **)&np->opt,
504 				   NULL);
505 			if (opt) {
506 				atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
507 				txopt_put(opt);
508 			}
509 			pktopt = xchg(&np->pktoptions, NULL);
510 			kfree_skb(pktopt);
511 
512 			/*
513 			 * ... and add it to the refcnt debug socks count
514 			 * in the new family. -acme
515 			 */
516 			sk_refcnt_debug_inc(sk);
517 			module_put(THIS_MODULE);
518 			retv = 0;
519 			break;
520 		}
521 		goto e_inval;
522 
523 	case IPV6_V6ONLY:
524 		if (optlen < sizeof(int) ||
525 		    inet_sk(sk)->inet_num)
526 			goto e_inval;
527 		sk->sk_ipv6only = valbool;
528 		retv = 0;
529 		break;
530 
531 	case IPV6_RECVPKTINFO:
532 		if (optlen < sizeof(int))
533 			goto e_inval;
534 		np->rxopt.bits.rxinfo = valbool;
535 		retv = 0;
536 		break;
537 
538 	case IPV6_2292PKTINFO:
539 		if (optlen < sizeof(int))
540 			goto e_inval;
541 		np->rxopt.bits.rxoinfo = valbool;
542 		retv = 0;
543 		break;
544 
545 	case IPV6_RECVHOPLIMIT:
546 		if (optlen < sizeof(int))
547 			goto e_inval;
548 		np->rxopt.bits.rxhlim = valbool;
549 		retv = 0;
550 		break;
551 
552 	case IPV6_2292HOPLIMIT:
553 		if (optlen < sizeof(int))
554 			goto e_inval;
555 		np->rxopt.bits.rxohlim = valbool;
556 		retv = 0;
557 		break;
558 
559 	case IPV6_RECVRTHDR:
560 		if (optlen < sizeof(int))
561 			goto e_inval;
562 		np->rxopt.bits.srcrt = valbool;
563 		retv = 0;
564 		break;
565 
566 	case IPV6_2292RTHDR:
567 		if (optlen < sizeof(int))
568 			goto e_inval;
569 		np->rxopt.bits.osrcrt = valbool;
570 		retv = 0;
571 		break;
572 
573 	case IPV6_RECVHOPOPTS:
574 		if (optlen < sizeof(int))
575 			goto e_inval;
576 		np->rxopt.bits.hopopts = valbool;
577 		retv = 0;
578 		break;
579 
580 	case IPV6_2292HOPOPTS:
581 		if (optlen < sizeof(int))
582 			goto e_inval;
583 		np->rxopt.bits.ohopopts = valbool;
584 		retv = 0;
585 		break;
586 
587 	case IPV6_RECVDSTOPTS:
588 		if (optlen < sizeof(int))
589 			goto e_inval;
590 		np->rxopt.bits.dstopts = valbool;
591 		retv = 0;
592 		break;
593 
594 	case IPV6_2292DSTOPTS:
595 		if (optlen < sizeof(int))
596 			goto e_inval;
597 		np->rxopt.bits.odstopts = valbool;
598 		retv = 0;
599 		break;
600 
601 	case IPV6_TCLASS:
602 		if (optlen < sizeof(int))
603 			goto e_inval;
604 		if (val < -1 || val > 0xff)
605 			goto e_inval;
606 		/* RFC 3542, 6.5: default traffic class of 0x0 */
607 		if (val == -1)
608 			val = 0;
609 		np->tclass = val;
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 		np->min_hopcount = val;
963 		retv = 0;
964 		break;
965 	case IPV6_DONTFRAG:
966 		np->dontfrag = valbool;
967 		retv = 0;
968 		break;
969 	case IPV6_AUTOFLOWLABEL:
970 		np->autoflowlabel = valbool;
971 		np->autoflowlabel_set = 1;
972 		retv = 0;
973 		break;
974 	case IPV6_RECVFRAGSIZE:
975 		np->rxopt.bits.recvfragsize = valbool;
976 		retv = 0;
977 		break;
978 	case IPV6_RECVERR_RFC4884:
979 		if (optlen < sizeof(int))
980 			goto e_inval;
981 		if (val < 0 || val > 1)
982 			goto e_inval;
983 		np->recverr_rfc4884 = valbool;
984 		retv = 0;
985 		break;
986 	}
987 
988 unlock:
989 	release_sock(sk);
990 	if (needs_rtnl)
991 		rtnl_unlock();
992 
993 	return retv;
994 
995 e_inval:
996 	release_sock(sk);
997 	if (needs_rtnl)
998 		rtnl_unlock();
999 	return -EINVAL;
1000 }
1001 
ipv6_setsockopt(struct sock * sk,int level,int optname,sockptr_t optval,unsigned int optlen)1002 int ipv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
1003 		    unsigned int optlen)
1004 {
1005 	int err;
1006 
1007 	if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1008 		return udp_prot.setsockopt(sk, level, optname, optval, optlen);
1009 
1010 	if (level != SOL_IPV6)
1011 		return -ENOPROTOOPT;
1012 
1013 	err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
1014 #ifdef CONFIG_NETFILTER
1015 	/* we need to exclude all possible ENOPROTOOPTs except default case */
1016 	if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
1017 			optname != IPV6_XFRM_POLICY)
1018 		err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen);
1019 #endif
1020 	return err;
1021 }
1022 EXPORT_SYMBOL(ipv6_setsockopt);
1023 
ipv6_getsockopt_sticky(struct sock * sk,struct ipv6_txoptions * opt,int optname,char __user * optval,int len)1024 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
1025 				  int optname, char __user *optval, int len)
1026 {
1027 	struct ipv6_opt_hdr *hdr;
1028 
1029 	if (!opt)
1030 		return 0;
1031 
1032 	switch (optname) {
1033 	case IPV6_HOPOPTS:
1034 		hdr = opt->hopopt;
1035 		break;
1036 	case IPV6_RTHDRDSTOPTS:
1037 		hdr = opt->dst0opt;
1038 		break;
1039 	case IPV6_RTHDR:
1040 		hdr = (struct ipv6_opt_hdr *)opt->srcrt;
1041 		break;
1042 	case IPV6_DSTOPTS:
1043 		hdr = opt->dst1opt;
1044 		break;
1045 	default:
1046 		return -EINVAL;	/* should not happen */
1047 	}
1048 
1049 	if (!hdr)
1050 		return 0;
1051 
1052 	len = min_t(unsigned int, len, ipv6_optlen(hdr));
1053 	if (copy_to_user(optval, hdr, len))
1054 		return -EFAULT;
1055 	return len;
1056 }
1057 
ipv6_get_msfilter(struct sock * sk,void __user * optval,int __user * optlen,int len)1058 static int ipv6_get_msfilter(struct sock *sk, void __user *optval,
1059 		int __user *optlen, int len)
1060 {
1061 	const int size0 = offsetof(struct group_filter, gf_slist);
1062 	struct group_filter __user *p = optval;
1063 	struct group_filter gsf;
1064 	int num;
1065 	int err;
1066 
1067 	if (len < size0)
1068 		return -EINVAL;
1069 	if (copy_from_user(&gsf, p, size0))
1070 		return -EFAULT;
1071 	if (gsf.gf_group.ss_family != AF_INET6)
1072 		return -EADDRNOTAVAIL;
1073 	num = gsf.gf_numsrc;
1074 	lock_sock(sk);
1075 	err = ip6_mc_msfget(sk, &gsf, p->gf_slist);
1076 	if (!err) {
1077 		if (num > gsf.gf_numsrc)
1078 			num = gsf.gf_numsrc;
1079 		if (put_user(GROUP_FILTER_SIZE(num), optlen) ||
1080 		    copy_to_user(p, &gsf, size0))
1081 			err = -EFAULT;
1082 	}
1083 	release_sock(sk);
1084 	return err;
1085 }
1086 
compat_ipv6_get_msfilter(struct sock * sk,void __user * optval,int __user * optlen)1087 static int compat_ipv6_get_msfilter(struct sock *sk, void __user *optval,
1088 		int __user *optlen)
1089 {
1090 	const int size0 = offsetof(struct compat_group_filter, gf_slist);
1091 	struct compat_group_filter __user *p = optval;
1092 	struct compat_group_filter gf32;
1093 	struct group_filter gf;
1094 	int len, err;
1095 	int num;
1096 
1097 	if (get_user(len, optlen))
1098 		return -EFAULT;
1099 	if (len < size0)
1100 		return -EINVAL;
1101 
1102 	if (copy_from_user(&gf32, p, size0))
1103 		return -EFAULT;
1104 	gf.gf_interface = gf32.gf_interface;
1105 	gf.gf_fmode = gf32.gf_fmode;
1106 	num = gf.gf_numsrc = gf32.gf_numsrc;
1107 	gf.gf_group = gf32.gf_group;
1108 
1109 	if (gf.gf_group.ss_family != AF_INET6)
1110 		return -EADDRNOTAVAIL;
1111 
1112 	lock_sock(sk);
1113 	err = ip6_mc_msfget(sk, &gf, p->gf_slist);
1114 	release_sock(sk);
1115 	if (err)
1116 		return err;
1117 	if (num > gf.gf_numsrc)
1118 		num = gf.gf_numsrc;
1119 	len = GROUP_FILTER_SIZE(num) - (sizeof(gf)-sizeof(gf32));
1120 	if (put_user(len, optlen) ||
1121 	    put_user(gf.gf_fmode, &p->gf_fmode) ||
1122 	    put_user(gf.gf_numsrc, &p->gf_numsrc))
1123 		return -EFAULT;
1124 	return 0;
1125 }
1126 
do_ipv6_getsockopt(struct sock * sk,int level,int optname,char __user * optval,int __user * optlen,unsigned int flags)1127 static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
1128 		    char __user *optval, int __user *optlen, unsigned int flags)
1129 {
1130 	struct ipv6_pinfo *np = inet6_sk(sk);
1131 	int len;
1132 	int val;
1133 
1134 	if (ip6_mroute_opt(optname))
1135 		return ip6_mroute_getsockopt(sk, optname, optval, optlen);
1136 
1137 	if (get_user(len, optlen))
1138 		return -EFAULT;
1139 	switch (optname) {
1140 	case IPV6_ADDRFORM:
1141 		if (sk->sk_protocol != IPPROTO_UDP &&
1142 		    sk->sk_protocol != IPPROTO_UDPLITE &&
1143 		    sk->sk_protocol != IPPROTO_TCP)
1144 			return -ENOPROTOOPT;
1145 		if (sk->sk_state != TCP_ESTABLISHED)
1146 			return -ENOTCONN;
1147 		val = sk->sk_family;
1148 		break;
1149 	case MCAST_MSFILTER:
1150 		if (in_compat_syscall())
1151 			return compat_ipv6_get_msfilter(sk, optval, optlen);
1152 		return ipv6_get_msfilter(sk, optval, optlen, len);
1153 	case IPV6_2292PKTOPTIONS:
1154 	{
1155 		struct msghdr msg;
1156 		struct sk_buff *skb;
1157 
1158 		if (sk->sk_type != SOCK_STREAM)
1159 			return -ENOPROTOOPT;
1160 
1161 		msg.msg_control = optval;
1162 		msg.msg_controllen = len;
1163 		msg.msg_flags = flags;
1164 		msg.msg_control_is_user = true;
1165 
1166 		lock_sock(sk);
1167 		skb = np->pktoptions;
1168 		if (skb)
1169 			ip6_datagram_recv_ctl(sk, &msg, skb);
1170 		release_sock(sk);
1171 		if (!skb) {
1172 			if (np->rxopt.bits.rxinfo) {
1173 				struct in6_pktinfo src_info;
1174 				src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1175 					np->sticky_pktinfo.ipi6_ifindex;
1176 				src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
1177 				put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1178 			}
1179 			if (np->rxopt.bits.rxhlim) {
1180 				int hlim = np->mcast_hops;
1181 				put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1182 			}
1183 			if (np->rxopt.bits.rxtclass) {
1184 				int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1185 
1186 				put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1187 			}
1188 			if (np->rxopt.bits.rxoinfo) {
1189 				struct in6_pktinfo src_info;
1190 				src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1191 					np->sticky_pktinfo.ipi6_ifindex;
1192 				src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1193 								     np->sticky_pktinfo.ipi6_addr;
1194 				put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1195 			}
1196 			if (np->rxopt.bits.rxohlim) {
1197 				int hlim = np->mcast_hops;
1198 				put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1199 			}
1200 			if (np->rxopt.bits.rxflow) {
1201 				__be32 flowinfo = np->rcv_flowinfo;
1202 
1203 				put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1204 			}
1205 		}
1206 		len -= msg.msg_controllen;
1207 		return put_user(len, optlen);
1208 	}
1209 	case IPV6_MTU:
1210 	{
1211 		struct dst_entry *dst;
1212 
1213 		val = 0;
1214 		rcu_read_lock();
1215 		dst = __sk_dst_get(sk);
1216 		if (dst)
1217 			val = dst_mtu(dst);
1218 		rcu_read_unlock();
1219 		if (!val)
1220 			return -ENOTCONN;
1221 		break;
1222 	}
1223 
1224 	case IPV6_V6ONLY:
1225 		val = sk->sk_ipv6only;
1226 		break;
1227 
1228 	case IPV6_RECVPKTINFO:
1229 		val = np->rxopt.bits.rxinfo;
1230 		break;
1231 
1232 	case IPV6_2292PKTINFO:
1233 		val = np->rxopt.bits.rxoinfo;
1234 		break;
1235 
1236 	case IPV6_RECVHOPLIMIT:
1237 		val = np->rxopt.bits.rxhlim;
1238 		break;
1239 
1240 	case IPV6_2292HOPLIMIT:
1241 		val = np->rxopt.bits.rxohlim;
1242 		break;
1243 
1244 	case IPV6_RECVRTHDR:
1245 		val = np->rxopt.bits.srcrt;
1246 		break;
1247 
1248 	case IPV6_2292RTHDR:
1249 		val = np->rxopt.bits.osrcrt;
1250 		break;
1251 
1252 	case IPV6_HOPOPTS:
1253 	case IPV6_RTHDRDSTOPTS:
1254 	case IPV6_RTHDR:
1255 	case IPV6_DSTOPTS:
1256 	{
1257 		struct ipv6_txoptions *opt;
1258 
1259 		lock_sock(sk);
1260 		opt = rcu_dereference_protected(np->opt,
1261 						lockdep_sock_is_held(sk));
1262 		len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len);
1263 		release_sock(sk);
1264 		/* check if ipv6_getsockopt_sticky() returns err code */
1265 		if (len < 0)
1266 			return len;
1267 		return put_user(len, optlen);
1268 	}
1269 
1270 	case IPV6_RECVHOPOPTS:
1271 		val = np->rxopt.bits.hopopts;
1272 		break;
1273 
1274 	case IPV6_2292HOPOPTS:
1275 		val = np->rxopt.bits.ohopopts;
1276 		break;
1277 
1278 	case IPV6_RECVDSTOPTS:
1279 		val = np->rxopt.bits.dstopts;
1280 		break;
1281 
1282 	case IPV6_2292DSTOPTS:
1283 		val = np->rxopt.bits.odstopts;
1284 		break;
1285 
1286 	case IPV6_TCLASS:
1287 		val = np->tclass;
1288 		break;
1289 
1290 	case IPV6_RECVTCLASS:
1291 		val = np->rxopt.bits.rxtclass;
1292 		break;
1293 
1294 	case IPV6_FLOWINFO:
1295 		val = np->rxopt.bits.rxflow;
1296 		break;
1297 
1298 	case IPV6_RECVPATHMTU:
1299 		val = np->rxopt.bits.rxpmtu;
1300 		break;
1301 
1302 	case IPV6_PATHMTU:
1303 	{
1304 		struct dst_entry *dst;
1305 		struct ip6_mtuinfo mtuinfo;
1306 
1307 		if (len < sizeof(mtuinfo))
1308 			return -EINVAL;
1309 
1310 		len = sizeof(mtuinfo);
1311 		memset(&mtuinfo, 0, sizeof(mtuinfo));
1312 
1313 		rcu_read_lock();
1314 		dst = __sk_dst_get(sk);
1315 		if (dst)
1316 			mtuinfo.ip6m_mtu = dst_mtu(dst);
1317 		rcu_read_unlock();
1318 		if (!mtuinfo.ip6m_mtu)
1319 			return -ENOTCONN;
1320 
1321 		if (put_user(len, optlen))
1322 			return -EFAULT;
1323 		if (copy_to_user(optval, &mtuinfo, len))
1324 			return -EFAULT;
1325 
1326 		return 0;
1327 	}
1328 
1329 	case IPV6_TRANSPARENT:
1330 		val = inet_sk(sk)->transparent;
1331 		break;
1332 
1333 	case IPV6_FREEBIND:
1334 		val = inet_sk(sk)->freebind;
1335 		break;
1336 
1337 	case IPV6_RECVORIGDSTADDR:
1338 		val = np->rxopt.bits.rxorigdstaddr;
1339 		break;
1340 
1341 	case IPV6_UNICAST_HOPS:
1342 	case IPV6_MULTICAST_HOPS:
1343 	{
1344 		struct dst_entry *dst;
1345 
1346 		if (optname == IPV6_UNICAST_HOPS)
1347 			val = np->hop_limit;
1348 		else
1349 			val = np->mcast_hops;
1350 
1351 		if (val < 0) {
1352 			rcu_read_lock();
1353 			dst = __sk_dst_get(sk);
1354 			if (dst)
1355 				val = ip6_dst_hoplimit(dst);
1356 			rcu_read_unlock();
1357 		}
1358 
1359 		if (val < 0)
1360 			val = sock_net(sk)->ipv6.devconf_all->hop_limit;
1361 		break;
1362 	}
1363 
1364 	case IPV6_MULTICAST_LOOP:
1365 		val = np->mc_loop;
1366 		break;
1367 
1368 	case IPV6_MULTICAST_IF:
1369 		val = np->mcast_oif;
1370 		break;
1371 
1372 	case IPV6_MULTICAST_ALL:
1373 		val = np->mc_all;
1374 		break;
1375 
1376 	case IPV6_UNICAST_IF:
1377 		val = (__force int)htonl((__u32) np->ucast_oif);
1378 		break;
1379 
1380 	case IPV6_MTU_DISCOVER:
1381 		val = np->pmtudisc;
1382 		break;
1383 
1384 	case IPV6_RECVERR:
1385 		val = np->recverr;
1386 		break;
1387 
1388 	case IPV6_FLOWINFO_SEND:
1389 		val = np->sndflow;
1390 		break;
1391 
1392 	case IPV6_FLOWLABEL_MGR:
1393 	{
1394 		struct in6_flowlabel_req freq;
1395 		int flags;
1396 
1397 		if (len < sizeof(freq))
1398 			return -EINVAL;
1399 
1400 		if (copy_from_user(&freq, optval, sizeof(freq)))
1401 			return -EFAULT;
1402 
1403 		if (freq.flr_action != IPV6_FL_A_GET)
1404 			return -EINVAL;
1405 
1406 		len = sizeof(freq);
1407 		flags = freq.flr_flags;
1408 
1409 		memset(&freq, 0, sizeof(freq));
1410 
1411 		val = ipv6_flowlabel_opt_get(sk, &freq, flags);
1412 		if (val < 0)
1413 			return val;
1414 
1415 		if (put_user(len, optlen))
1416 			return -EFAULT;
1417 		if (copy_to_user(optval, &freq, len))
1418 			return -EFAULT;
1419 
1420 		return 0;
1421 	}
1422 
1423 	case IPV6_ADDR_PREFERENCES:
1424 		val = 0;
1425 
1426 		if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1427 			val |= IPV6_PREFER_SRC_TMP;
1428 		else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1429 			val |= IPV6_PREFER_SRC_PUBLIC;
1430 		else {
1431 			/* XXX: should we return system default? */
1432 			val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1433 		}
1434 
1435 		if (np->srcprefs & IPV6_PREFER_SRC_COA)
1436 			val |= IPV6_PREFER_SRC_COA;
1437 		else
1438 			val |= IPV6_PREFER_SRC_HOME;
1439 		break;
1440 
1441 	case IPV6_MINHOPCOUNT:
1442 		val = np->min_hopcount;
1443 		break;
1444 
1445 	case IPV6_DONTFRAG:
1446 		val = np->dontfrag;
1447 		break;
1448 
1449 	case IPV6_AUTOFLOWLABEL:
1450 		val = ip6_autoflowlabel(sock_net(sk), np);
1451 		break;
1452 
1453 	case IPV6_RECVFRAGSIZE:
1454 		val = np->rxopt.bits.recvfragsize;
1455 		break;
1456 
1457 	case IPV6_ROUTER_ALERT_ISOLATE:
1458 		val = np->rtalert_isolate;
1459 		break;
1460 
1461 	case IPV6_RECVERR_RFC4884:
1462 		val = np->recverr_rfc4884;
1463 		break;
1464 
1465 	default:
1466 		return -ENOPROTOOPT;
1467 	}
1468 	len = min_t(unsigned int, sizeof(int), len);
1469 	if (put_user(len, optlen))
1470 		return -EFAULT;
1471 	if (copy_to_user(optval, &val, len))
1472 		return -EFAULT;
1473 	return 0;
1474 }
1475 
ipv6_getsockopt(struct sock * sk,int level,int optname,char __user * optval,int __user * optlen)1476 int ipv6_getsockopt(struct sock *sk, int level, int optname,
1477 		    char __user *optval, int __user *optlen)
1478 {
1479 	int err;
1480 
1481 	if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1482 		return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1483 
1484 	if (level != SOL_IPV6)
1485 		return -ENOPROTOOPT;
1486 
1487 	err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
1488 #ifdef CONFIG_NETFILTER
1489 	/* we need to exclude all possible ENOPROTOOPTs except default case */
1490 	if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1491 		int len;
1492 
1493 		if (get_user(len, optlen))
1494 			return -EFAULT;
1495 
1496 		err = nf_getsockopt(sk, PF_INET6, optname, optval, &len);
1497 		if (err >= 0)
1498 			err = put_user(len, optlen);
1499 	}
1500 #endif
1501 	return err;
1502 }
1503 EXPORT_SYMBOL(ipv6_getsockopt);
1504