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