usb: dwc3: pci: Enable extcon driver for Intel Merrifield
[linux/fpc-iii.git] / net / ipv6 / ipv6_sockglue.c
blob231c489128e47a9d03e7b445e2087778b6c70183
1 /*
2 * IPv6 BSD socket options interface
3 * Linux INET6 implementation
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * Based on linux/net/ipv4/ip_sockglue.c
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
15 * FIXME: Make the setsockopt code POSIX compliant: That is
17 * o Truncate getsockopt returns
18 * o Return an optlen of the truncated length if need be
20 * Changes:
21 * David L Stevens <dlstevens@us.ibm.com>:
22 * - added multicast source filtering API for MLDv2
25 #include <linux/module.h>
26 #include <linux/capability.h>
27 #include <linux/errno.h>
28 #include <linux/types.h>
29 #include <linux/socket.h>
30 #include <linux/sockios.h>
31 #include <linux/net.h>
32 #include <linux/in6.h>
33 #include <linux/mroute6.h>
34 #include <linux/netdevice.h>
35 #include <linux/if_arp.h>
36 #include <linux/init.h>
37 #include <linux/sysctl.h>
38 #include <linux/netfilter.h>
39 #include <linux/slab.h>
41 #include <net/sock.h>
42 #include <net/snmp.h>
43 #include <net/ipv6.h>
44 #include <net/ndisc.h>
45 #include <net/protocol.h>
46 #include <net/transp_v6.h>
47 #include <net/ip6_route.h>
48 #include <net/addrconf.h>
49 #include <net/inet_common.h>
50 #include <net/tcp.h>
51 #include <net/udp.h>
52 #include <net/udplite.h>
53 #include <net/xfrm.h>
54 #include <net/compat.h>
55 #include <net/seg6.h>
57 #include <linux/uaccess.h>
59 struct ip6_ra_chain *ip6_ra_chain;
60 DEFINE_RWLOCK(ip6_ra_lock);
62 int ip6_ra_control(struct sock *sk, int sel)
64 struct ip6_ra_chain *ra, *new_ra, **rap;
66 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
67 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num != IPPROTO_RAW)
68 return -ENOPROTOOPT;
70 new_ra = (sel >= 0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
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;
81 *rap = ra->next;
82 write_unlock_bh(&ip6_ra_lock);
84 sock_put(sk);
85 kfree(ra);
86 return 0;
89 if (!new_ra) {
90 write_unlock_bh(&ip6_ra_lock);
91 return -ENOBUFS;
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;
102 struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
103 struct ipv6_txoptions *opt)
105 if (inet_sk(sk)->is_icsk) {
106 if (opt &&
107 !((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
108 inet_sk(sk)->inet_daddr != LOOPBACK4_IPV6) {
109 struct inet_connection_sock *icsk = inet_csk(sk);
110 icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen;
111 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
114 opt = xchg((__force struct ipv6_txoptions **)&inet6_sk(sk)->opt,
115 opt);
116 sk_dst_reset(sk);
118 return opt;
121 static bool setsockopt_needs_rtnl(int optname)
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;
138 return false;
141 static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
142 char __user *optval, unsigned int optlen)
144 struct ipv6_pinfo *np = inet6_sk(sk);
145 struct net *net = sock_net(sk);
146 int val, valbool;
147 int retv = -ENOPROTOOPT;
148 bool needs_rtnl = setsockopt_needs_rtnl(optname);
150 if (!optval)
151 val = 0;
152 else {
153 if (optlen >= sizeof(int)) {
154 if (get_user(val, (int __user *) optval))
155 return -EFAULT;
156 } else
157 val = 0;
160 valbool = (val != 0);
162 if (ip6_mroute_opt(optname))
163 return ip6_mroute_setsockopt(sk, optname, optval, optlen);
165 if (needs_rtnl)
166 rtnl_lock();
167 lock_sock(sk);
169 switch (optname) {
171 case IPV6_ADDRFORM:
172 if (optlen < sizeof(int))
173 goto e_inval;
174 if (val == PF_INET) {
175 struct ipv6_txoptions *opt;
176 struct sk_buff *pktopt;
178 if (sk->sk_type == SOCK_RAW)
179 break;
181 if (sk->sk_protocol == IPPROTO_UDP ||
182 sk->sk_protocol == IPPROTO_UDPLITE) {
183 struct udp_sock *up = udp_sk(sk);
184 if (up->pending == AF_INET6) {
185 retv = -EBUSY;
186 break;
189 if (sk->sk_protocol == IPPROTO_TCP &&
190 sk->sk_prot != &tcpv6_prot) {
191 retv = -EBUSY;
192 break;
194 if (sk->sk_protocol != IPPROTO_TCP)
195 break;
196 if (sk->sk_state != TCP_ESTABLISHED) {
197 retv = -ENOTCONN;
198 break;
201 if (ipv6_only_sock(sk) ||
202 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
203 retv = -EADDRNOTAVAIL;
204 break;
207 fl6_free_socklist(sk);
208 __ipv6_sock_mc_close(sk);
211 * Sock is moving from IPv6 to IPv4 (sk_prot), so
212 * remove it from the refcnt debug socks count in the
213 * original family...
215 sk_refcnt_debug_dec(sk);
217 if (sk->sk_protocol == IPPROTO_TCP) {
218 struct inet_connection_sock *icsk = inet_csk(sk);
219 local_bh_disable();
220 sock_prot_inuse_add(net, sk->sk_prot, -1);
221 sock_prot_inuse_add(net, &tcp_prot, 1);
222 local_bh_enable();
223 sk->sk_prot = &tcp_prot;
224 icsk->icsk_af_ops = &ipv4_specific;
225 sk->sk_socket->ops = &inet_stream_ops;
226 sk->sk_family = PF_INET;
227 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
228 } else {
229 struct proto *prot = &udp_prot;
231 if (sk->sk_protocol == IPPROTO_UDPLITE)
232 prot = &udplite_prot;
233 local_bh_disable();
234 sock_prot_inuse_add(net, sk->sk_prot, -1);
235 sock_prot_inuse_add(net, prot, 1);
236 local_bh_enable();
237 sk->sk_prot = prot;
238 sk->sk_socket->ops = &inet_dgram_ops;
239 sk->sk_family = PF_INET;
241 opt = xchg((__force struct ipv6_txoptions **)&np->opt,
242 NULL);
243 if (opt) {
244 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
245 txopt_put(opt);
247 pktopt = xchg(&np->pktoptions, NULL);
248 kfree_skb(pktopt);
251 * ... and add it to the refcnt debug socks count
252 * in the new family. -acme
254 sk_refcnt_debug_inc(sk);
255 module_put(THIS_MODULE);
256 retv = 0;
257 break;
259 goto e_inval;
261 case IPV6_V6ONLY:
262 if (optlen < sizeof(int) ||
263 inet_sk(sk)->inet_num)
264 goto e_inval;
265 sk->sk_ipv6only = valbool;
266 retv = 0;
267 break;
269 case IPV6_RECVPKTINFO:
270 if (optlen < sizeof(int))
271 goto e_inval;
272 np->rxopt.bits.rxinfo = valbool;
273 retv = 0;
274 break;
276 case IPV6_2292PKTINFO:
277 if (optlen < sizeof(int))
278 goto e_inval;
279 np->rxopt.bits.rxoinfo = valbool;
280 retv = 0;
281 break;
283 case IPV6_RECVHOPLIMIT:
284 if (optlen < sizeof(int))
285 goto e_inval;
286 np->rxopt.bits.rxhlim = valbool;
287 retv = 0;
288 break;
290 case IPV6_2292HOPLIMIT:
291 if (optlen < sizeof(int))
292 goto e_inval;
293 np->rxopt.bits.rxohlim = valbool;
294 retv = 0;
295 break;
297 case IPV6_RECVRTHDR:
298 if (optlen < sizeof(int))
299 goto e_inval;
300 np->rxopt.bits.srcrt = valbool;
301 retv = 0;
302 break;
304 case IPV6_2292RTHDR:
305 if (optlen < sizeof(int))
306 goto e_inval;
307 np->rxopt.bits.osrcrt = valbool;
308 retv = 0;
309 break;
311 case IPV6_RECVHOPOPTS:
312 if (optlen < sizeof(int))
313 goto e_inval;
314 np->rxopt.bits.hopopts = valbool;
315 retv = 0;
316 break;
318 case IPV6_2292HOPOPTS:
319 if (optlen < sizeof(int))
320 goto e_inval;
321 np->rxopt.bits.ohopopts = valbool;
322 retv = 0;
323 break;
325 case IPV6_RECVDSTOPTS:
326 if (optlen < sizeof(int))
327 goto e_inval;
328 np->rxopt.bits.dstopts = valbool;
329 retv = 0;
330 break;
332 case IPV6_2292DSTOPTS:
333 if (optlen < sizeof(int))
334 goto e_inval;
335 np->rxopt.bits.odstopts = valbool;
336 retv = 0;
337 break;
339 case IPV6_TCLASS:
340 if (optlen < sizeof(int))
341 goto e_inval;
342 if (val < -1 || val > 0xff)
343 goto e_inval;
344 /* RFC 3542, 6.5: default traffic class of 0x0 */
345 if (val == -1)
346 val = 0;
347 np->tclass = val;
348 retv = 0;
349 break;
351 case IPV6_RECVTCLASS:
352 if (optlen < sizeof(int))
353 goto e_inval;
354 np->rxopt.bits.rxtclass = valbool;
355 retv = 0;
356 break;
358 case IPV6_FLOWINFO:
359 if (optlen < sizeof(int))
360 goto e_inval;
361 np->rxopt.bits.rxflow = valbool;
362 retv = 0;
363 break;
365 case IPV6_RECVPATHMTU:
366 if (optlen < sizeof(int))
367 goto e_inval;
368 np->rxopt.bits.rxpmtu = valbool;
369 retv = 0;
370 break;
372 case IPV6_TRANSPARENT:
373 if (valbool && !ns_capable(net->user_ns, CAP_NET_ADMIN) &&
374 !ns_capable(net->user_ns, CAP_NET_RAW)) {
375 retv = -EPERM;
376 break;
378 if (optlen < sizeof(int))
379 goto e_inval;
380 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
381 inet_sk(sk)->transparent = valbool;
382 retv = 0;
383 break;
385 case IPV6_FREEBIND:
386 if (optlen < sizeof(int))
387 goto e_inval;
388 /* we also don't have a separate freebind bit for IPV6 */
389 inet_sk(sk)->freebind = valbool;
390 retv = 0;
391 break;
393 case IPV6_RECVORIGDSTADDR:
394 if (optlen < sizeof(int))
395 goto e_inval;
396 np->rxopt.bits.rxorigdstaddr = valbool;
397 retv = 0;
398 break;
400 case IPV6_HOPOPTS:
401 case IPV6_RTHDRDSTOPTS:
402 case IPV6_RTHDR:
403 case IPV6_DSTOPTS:
405 struct ipv6_txoptions *opt;
406 struct ipv6_opt_hdr *new = NULL;
408 /* hop-by-hop / destination options are privileged option */
409 retv = -EPERM;
410 if (optname != IPV6_RTHDR && !ns_capable(net->user_ns, CAP_NET_RAW))
411 break;
413 /* remove any sticky options header with a zero option
414 * length, per RFC3542.
416 if (optlen == 0)
417 optval = NULL;
418 else if (!optval)
419 goto e_inval;
420 else if (optlen < sizeof(struct ipv6_opt_hdr) ||
421 optlen & 0x7 || optlen > 8 * 255)
422 goto e_inval;
423 else {
424 new = memdup_user(optval, optlen);
425 if (IS_ERR(new)) {
426 retv = PTR_ERR(new);
427 break;
429 if (unlikely(ipv6_optlen(new) > optlen)) {
430 kfree(new);
431 goto e_inval;
435 opt = rcu_dereference_protected(np->opt,
436 lockdep_sock_is_held(sk));
437 opt = ipv6_renew_options(sk, opt, optname, new);
438 kfree(new);
439 if (IS_ERR(opt)) {
440 retv = PTR_ERR(opt);
441 break;
444 /* routing header option needs extra check */
445 retv = -EINVAL;
446 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
447 struct ipv6_rt_hdr *rthdr = opt->srcrt;
448 switch (rthdr->type) {
449 #if IS_ENABLED(CONFIG_IPV6_MIP6)
450 case IPV6_SRCRT_TYPE_2:
451 if (rthdr->hdrlen != 2 ||
452 rthdr->segments_left != 1)
453 goto sticky_done;
455 break;
456 #endif
457 case IPV6_SRCRT_TYPE_4:
459 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)
460 opt->srcrt;
462 if (!seg6_validate_srh(srh, optlen))
463 goto sticky_done;
464 break;
466 default:
467 goto sticky_done;
471 retv = 0;
472 opt = ipv6_update_options(sk, opt);
473 sticky_done:
474 if (opt) {
475 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
476 txopt_put(opt);
478 break;
481 case IPV6_PKTINFO:
483 struct in6_pktinfo pkt;
485 if (optlen == 0)
486 goto e_inval;
487 else if (optlen < sizeof(struct in6_pktinfo) || !optval)
488 goto e_inval;
490 if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) {
491 retv = -EFAULT;
492 break;
494 if (sk->sk_bound_dev_if && pkt.ipi6_ifindex != sk->sk_bound_dev_if)
495 goto e_inval;
497 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
498 np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
499 retv = 0;
500 break;
503 case IPV6_2292PKTOPTIONS:
505 struct ipv6_txoptions *opt = NULL;
506 struct msghdr msg;
507 struct flowi6 fl6;
508 struct ipcm6_cookie ipc6;
510 memset(&fl6, 0, sizeof(fl6));
511 fl6.flowi6_oif = sk->sk_bound_dev_if;
512 fl6.flowi6_mark = sk->sk_mark;
514 if (optlen == 0)
515 goto update;
517 /* 1K is probably excessive
518 * 1K is surely not enough, 2K per standard header is 16K.
520 retv = -EINVAL;
521 if (optlen > 64*1024)
522 break;
524 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
525 retv = -ENOBUFS;
526 if (!opt)
527 break;
529 memset(opt, 0, sizeof(*opt));
530 refcount_set(&opt->refcnt, 1);
531 opt->tot_len = sizeof(*opt) + optlen;
532 retv = -EFAULT;
533 if (copy_from_user(opt+1, optval, optlen))
534 goto done;
536 msg.msg_controllen = optlen;
537 msg.msg_control = (void *)(opt+1);
538 ipc6.opt = opt;
540 retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6);
541 if (retv)
542 goto done;
543 update:
544 retv = 0;
545 opt = ipv6_update_options(sk, opt);
546 done:
547 if (opt) {
548 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
549 txopt_put(opt);
551 break;
553 case IPV6_UNICAST_HOPS:
554 if (optlen < sizeof(int))
555 goto e_inval;
556 if (val > 255 || val < -1)
557 goto e_inval;
558 np->hop_limit = val;
559 retv = 0;
560 break;
562 case IPV6_MULTICAST_HOPS:
563 if (sk->sk_type == SOCK_STREAM)
564 break;
565 if (optlen < sizeof(int))
566 goto e_inval;
567 if (val > 255 || val < -1)
568 goto e_inval;
569 np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
570 retv = 0;
571 break;
573 case IPV6_MULTICAST_LOOP:
574 if (optlen < sizeof(int))
575 goto e_inval;
576 if (val != valbool)
577 goto e_inval;
578 np->mc_loop = valbool;
579 retv = 0;
580 break;
582 case IPV6_UNICAST_IF:
584 struct net_device *dev = NULL;
585 int ifindex;
587 if (optlen != sizeof(int))
588 goto e_inval;
590 ifindex = (__force int)ntohl((__force __be32)val);
591 if (ifindex == 0) {
592 np->ucast_oif = 0;
593 retv = 0;
594 break;
597 dev = dev_get_by_index(net, ifindex);
598 retv = -EADDRNOTAVAIL;
599 if (!dev)
600 break;
601 dev_put(dev);
603 retv = -EINVAL;
604 if (sk->sk_bound_dev_if)
605 break;
607 np->ucast_oif = ifindex;
608 retv = 0;
609 break;
612 case IPV6_MULTICAST_IF:
613 if (sk->sk_type == SOCK_STREAM)
614 break;
615 if (optlen < sizeof(int))
616 goto e_inval;
618 if (val) {
619 struct net_device *dev;
620 int midx;
622 rcu_read_lock();
624 dev = dev_get_by_index_rcu(net, val);
625 if (!dev) {
626 rcu_read_unlock();
627 retv = -ENODEV;
628 break;
630 midx = l3mdev_master_ifindex_rcu(dev);
632 rcu_read_unlock();
634 if (sk->sk_bound_dev_if &&
635 sk->sk_bound_dev_if != val &&
636 (!midx || midx != sk->sk_bound_dev_if))
637 goto e_inval;
639 np->mcast_oif = val;
640 retv = 0;
641 break;
642 case IPV6_ADD_MEMBERSHIP:
643 case IPV6_DROP_MEMBERSHIP:
645 struct ipv6_mreq mreq;
647 if (optlen < sizeof(struct ipv6_mreq))
648 goto e_inval;
650 retv = -EPROTO;
651 if (inet_sk(sk)->is_icsk)
652 break;
654 retv = -EFAULT;
655 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
656 break;
658 if (optname == IPV6_ADD_MEMBERSHIP)
659 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
660 else
661 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
662 break;
664 case IPV6_JOIN_ANYCAST:
665 case IPV6_LEAVE_ANYCAST:
667 struct ipv6_mreq mreq;
669 if (optlen < sizeof(struct ipv6_mreq))
670 goto e_inval;
672 retv = -EFAULT;
673 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
674 break;
676 if (optname == IPV6_JOIN_ANYCAST)
677 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
678 else
679 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
680 break;
682 case MCAST_JOIN_GROUP:
683 case MCAST_LEAVE_GROUP:
685 struct group_req greq;
686 struct sockaddr_in6 *psin6;
688 if (optlen < sizeof(struct group_req))
689 goto e_inval;
691 retv = -EFAULT;
692 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
693 break;
694 if (greq.gr_group.ss_family != AF_INET6) {
695 retv = -EADDRNOTAVAIL;
696 break;
698 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
699 if (optname == MCAST_JOIN_GROUP)
700 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
701 &psin6->sin6_addr);
702 else
703 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
704 &psin6->sin6_addr);
705 break;
707 case MCAST_JOIN_SOURCE_GROUP:
708 case MCAST_LEAVE_SOURCE_GROUP:
709 case MCAST_BLOCK_SOURCE:
710 case MCAST_UNBLOCK_SOURCE:
712 struct group_source_req greqs;
713 int omode, add;
715 if (optlen < sizeof(struct group_source_req))
716 goto e_inval;
717 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
718 retv = -EFAULT;
719 break;
721 if (greqs.gsr_group.ss_family != AF_INET6 ||
722 greqs.gsr_source.ss_family != AF_INET6) {
723 retv = -EADDRNOTAVAIL;
724 break;
726 if (optname == MCAST_BLOCK_SOURCE) {
727 omode = MCAST_EXCLUDE;
728 add = 1;
729 } else if (optname == MCAST_UNBLOCK_SOURCE) {
730 omode = MCAST_EXCLUDE;
731 add = 0;
732 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
733 struct sockaddr_in6 *psin6;
735 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
736 retv = ipv6_sock_mc_join_ssm(sk, greqs.gsr_interface,
737 &psin6->sin6_addr,
738 MCAST_INCLUDE);
739 /* prior join w/ different source is ok */
740 if (retv && retv != -EADDRINUSE)
741 break;
742 omode = MCAST_INCLUDE;
743 add = 1;
744 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
745 omode = MCAST_INCLUDE;
746 add = 0;
748 retv = ip6_mc_source(add, omode, sk, &greqs);
749 break;
751 case MCAST_MSFILTER:
753 struct group_filter *gsf;
755 if (optlen < GROUP_FILTER_SIZE(0))
756 goto e_inval;
757 if (optlen > sysctl_optmem_max) {
758 retv = -ENOBUFS;
759 break;
761 gsf = memdup_user(optval, optlen);
762 if (IS_ERR(gsf)) {
763 retv = PTR_ERR(gsf);
764 break;
766 /* numsrc >= (4G-140)/128 overflow in 32 bits */
767 if (gsf->gf_numsrc >= 0x1ffffffU ||
768 gsf->gf_numsrc > sysctl_mld_max_msf) {
769 kfree(gsf);
770 retv = -ENOBUFS;
771 break;
773 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
774 kfree(gsf);
775 retv = -EINVAL;
776 break;
778 retv = ip6_mc_msfilter(sk, gsf);
779 kfree(gsf);
781 break;
783 case IPV6_ROUTER_ALERT:
784 if (optlen < sizeof(int))
785 goto e_inval;
786 retv = ip6_ra_control(sk, val);
787 break;
788 case IPV6_MTU_DISCOVER:
789 if (optlen < sizeof(int))
790 goto e_inval;
791 if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
792 goto e_inval;
793 np->pmtudisc = val;
794 retv = 0;
795 break;
796 case IPV6_MTU:
797 if (optlen < sizeof(int))
798 goto e_inval;
799 if (val && val < IPV6_MIN_MTU)
800 goto e_inval;
801 np->frag_size = val;
802 retv = 0;
803 break;
804 case IPV6_RECVERR:
805 if (optlen < sizeof(int))
806 goto e_inval;
807 np->recverr = valbool;
808 if (!val)
809 skb_queue_purge(&sk->sk_error_queue);
810 retv = 0;
811 break;
812 case IPV6_FLOWINFO_SEND:
813 if (optlen < sizeof(int))
814 goto e_inval;
815 np->sndflow = valbool;
816 retv = 0;
817 break;
818 case IPV6_FLOWLABEL_MGR:
819 retv = ipv6_flowlabel_opt(sk, optval, optlen);
820 break;
821 case IPV6_IPSEC_POLICY:
822 case IPV6_XFRM_POLICY:
823 retv = -EPERM;
824 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
825 break;
826 retv = xfrm_user_policy(sk, optname, optval, optlen);
827 break;
829 case IPV6_ADDR_PREFERENCES:
831 unsigned int pref = 0;
832 unsigned int prefmask = ~0;
834 if (optlen < sizeof(int))
835 goto e_inval;
837 retv = -EINVAL;
839 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
840 switch (val & (IPV6_PREFER_SRC_PUBLIC|
841 IPV6_PREFER_SRC_TMP|
842 IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
843 case IPV6_PREFER_SRC_PUBLIC:
844 pref |= IPV6_PREFER_SRC_PUBLIC;
845 break;
846 case IPV6_PREFER_SRC_TMP:
847 pref |= IPV6_PREFER_SRC_TMP;
848 break;
849 case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
850 break;
851 case 0:
852 goto pref_skip_pubtmp;
853 default:
854 goto e_inval;
857 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
858 IPV6_PREFER_SRC_TMP);
859 pref_skip_pubtmp:
861 /* check HOME/COA conflicts */
862 switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
863 case IPV6_PREFER_SRC_HOME:
864 break;
865 case IPV6_PREFER_SRC_COA:
866 pref |= IPV6_PREFER_SRC_COA;
867 case 0:
868 goto pref_skip_coa;
869 default:
870 goto e_inval;
873 prefmask &= ~IPV6_PREFER_SRC_COA;
874 pref_skip_coa:
876 /* check CGA/NONCGA conflicts */
877 switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
878 case IPV6_PREFER_SRC_CGA:
879 case IPV6_PREFER_SRC_NONCGA:
880 case 0:
881 break;
882 default:
883 goto e_inval;
886 np->srcprefs = (np->srcprefs & prefmask) | pref;
887 retv = 0;
889 break;
891 case IPV6_MINHOPCOUNT:
892 if (optlen < sizeof(int))
893 goto e_inval;
894 if (val < 0 || val > 255)
895 goto e_inval;
896 np->min_hopcount = val;
897 retv = 0;
898 break;
899 case IPV6_DONTFRAG:
900 np->dontfrag = valbool;
901 retv = 0;
902 break;
903 case IPV6_AUTOFLOWLABEL:
904 np->autoflowlabel = valbool;
905 np->autoflowlabel_set = 1;
906 retv = 0;
907 break;
908 case IPV6_RECVFRAGSIZE:
909 np->rxopt.bits.recvfragsize = valbool;
910 retv = 0;
911 break;
914 release_sock(sk);
915 if (needs_rtnl)
916 rtnl_unlock();
918 return retv;
920 e_inval:
921 release_sock(sk);
922 if (needs_rtnl)
923 rtnl_unlock();
924 return -EINVAL;
927 int ipv6_setsockopt(struct sock *sk, int level, int optname,
928 char __user *optval, unsigned int optlen)
930 int err;
932 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
933 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
935 if (level != SOL_IPV6)
936 return -ENOPROTOOPT;
938 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
939 #ifdef CONFIG_NETFILTER
940 /* we need to exclude all possible ENOPROTOOPTs except default case */
941 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
942 optname != IPV6_XFRM_POLICY)
943 err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen);
944 #endif
945 return err;
947 EXPORT_SYMBOL(ipv6_setsockopt);
949 #ifdef CONFIG_COMPAT
950 int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
951 char __user *optval, unsigned int optlen)
953 int err;
955 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
956 if (udp_prot.compat_setsockopt != NULL)
957 return udp_prot.compat_setsockopt(sk, level, optname,
958 optval, optlen);
959 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
962 if (level != SOL_IPV6)
963 return -ENOPROTOOPT;
965 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
966 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
967 ipv6_setsockopt);
969 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
970 #ifdef CONFIG_NETFILTER
971 /* we need to exclude all possible ENOPROTOOPTs except default case */
972 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
973 optname != IPV6_XFRM_POLICY)
974 err = compat_nf_setsockopt(sk, PF_INET6, optname, optval,
975 optlen);
976 #endif
977 return err;
979 EXPORT_SYMBOL(compat_ipv6_setsockopt);
980 #endif
982 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
983 int optname, char __user *optval, int len)
985 struct ipv6_opt_hdr *hdr;
987 if (!opt)
988 return 0;
990 switch (optname) {
991 case IPV6_HOPOPTS:
992 hdr = opt->hopopt;
993 break;
994 case IPV6_RTHDRDSTOPTS:
995 hdr = opt->dst0opt;
996 break;
997 case IPV6_RTHDR:
998 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
999 break;
1000 case IPV6_DSTOPTS:
1001 hdr = opt->dst1opt;
1002 break;
1003 default:
1004 return -EINVAL; /* should not happen */
1007 if (!hdr)
1008 return 0;
1010 len = min_t(unsigned int, len, ipv6_optlen(hdr));
1011 if (copy_to_user(optval, hdr, len))
1012 return -EFAULT;
1013 return len;
1016 static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
1017 char __user *optval, int __user *optlen, unsigned int flags)
1019 struct ipv6_pinfo *np = inet6_sk(sk);
1020 int len;
1021 int val;
1023 if (ip6_mroute_opt(optname))
1024 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
1026 if (get_user(len, optlen))
1027 return -EFAULT;
1028 switch (optname) {
1029 case IPV6_ADDRFORM:
1030 if (sk->sk_protocol != IPPROTO_UDP &&
1031 sk->sk_protocol != IPPROTO_UDPLITE &&
1032 sk->sk_protocol != IPPROTO_TCP)
1033 return -ENOPROTOOPT;
1034 if (sk->sk_state != TCP_ESTABLISHED)
1035 return -ENOTCONN;
1036 val = sk->sk_family;
1037 break;
1038 case MCAST_MSFILTER:
1040 struct group_filter gsf;
1041 int err;
1043 if (len < GROUP_FILTER_SIZE(0))
1044 return -EINVAL;
1045 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
1046 return -EFAULT;
1047 if (gsf.gf_group.ss_family != AF_INET6)
1048 return -EADDRNOTAVAIL;
1049 lock_sock(sk);
1050 err = ip6_mc_msfget(sk, &gsf,
1051 (struct group_filter __user *)optval, optlen);
1052 release_sock(sk);
1053 return err;
1056 case IPV6_2292PKTOPTIONS:
1058 struct msghdr msg;
1059 struct sk_buff *skb;
1061 if (sk->sk_type != SOCK_STREAM)
1062 return -ENOPROTOOPT;
1064 msg.msg_control = optval;
1065 msg.msg_controllen = len;
1066 msg.msg_flags = flags;
1068 lock_sock(sk);
1069 skb = np->pktoptions;
1070 if (skb)
1071 ip6_datagram_recv_ctl(sk, &msg, skb);
1072 release_sock(sk);
1073 if (!skb) {
1074 if (np->rxopt.bits.rxinfo) {
1075 struct in6_pktinfo src_info;
1076 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1077 np->sticky_pktinfo.ipi6_ifindex;
1078 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
1079 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1081 if (np->rxopt.bits.rxhlim) {
1082 int hlim = np->mcast_hops;
1083 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1085 if (np->rxopt.bits.rxtclass) {
1086 int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1088 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1090 if (np->rxopt.bits.rxoinfo) {
1091 struct in6_pktinfo src_info;
1092 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1093 np->sticky_pktinfo.ipi6_ifindex;
1094 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1095 np->sticky_pktinfo.ipi6_addr;
1096 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1098 if (np->rxopt.bits.rxohlim) {
1099 int hlim = np->mcast_hops;
1100 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1102 if (np->rxopt.bits.rxflow) {
1103 __be32 flowinfo = np->rcv_flowinfo;
1105 put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1108 len -= msg.msg_controllen;
1109 return put_user(len, optlen);
1111 case IPV6_MTU:
1113 struct dst_entry *dst;
1115 val = 0;
1116 rcu_read_lock();
1117 dst = __sk_dst_get(sk);
1118 if (dst)
1119 val = dst_mtu(dst);
1120 rcu_read_unlock();
1121 if (!val)
1122 return -ENOTCONN;
1123 break;
1126 case IPV6_V6ONLY:
1127 val = sk->sk_ipv6only;
1128 break;
1130 case IPV6_RECVPKTINFO:
1131 val = np->rxopt.bits.rxinfo;
1132 break;
1134 case IPV6_2292PKTINFO:
1135 val = np->rxopt.bits.rxoinfo;
1136 break;
1138 case IPV6_RECVHOPLIMIT:
1139 val = np->rxopt.bits.rxhlim;
1140 break;
1142 case IPV6_2292HOPLIMIT:
1143 val = np->rxopt.bits.rxohlim;
1144 break;
1146 case IPV6_RECVRTHDR:
1147 val = np->rxopt.bits.srcrt;
1148 break;
1150 case IPV6_2292RTHDR:
1151 val = np->rxopt.bits.osrcrt;
1152 break;
1154 case IPV6_HOPOPTS:
1155 case IPV6_RTHDRDSTOPTS:
1156 case IPV6_RTHDR:
1157 case IPV6_DSTOPTS:
1159 struct ipv6_txoptions *opt;
1161 lock_sock(sk);
1162 opt = rcu_dereference_protected(np->opt,
1163 lockdep_sock_is_held(sk));
1164 len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len);
1165 release_sock(sk);
1166 /* check if ipv6_getsockopt_sticky() returns err code */
1167 if (len < 0)
1168 return len;
1169 return put_user(len, optlen);
1172 case IPV6_RECVHOPOPTS:
1173 val = np->rxopt.bits.hopopts;
1174 break;
1176 case IPV6_2292HOPOPTS:
1177 val = np->rxopt.bits.ohopopts;
1178 break;
1180 case IPV6_RECVDSTOPTS:
1181 val = np->rxopt.bits.dstopts;
1182 break;
1184 case IPV6_2292DSTOPTS:
1185 val = np->rxopt.bits.odstopts;
1186 break;
1188 case IPV6_TCLASS:
1189 val = np->tclass;
1190 break;
1192 case IPV6_RECVTCLASS:
1193 val = np->rxopt.bits.rxtclass;
1194 break;
1196 case IPV6_FLOWINFO:
1197 val = np->rxopt.bits.rxflow;
1198 break;
1200 case IPV6_RECVPATHMTU:
1201 val = np->rxopt.bits.rxpmtu;
1202 break;
1204 case IPV6_PATHMTU:
1206 struct dst_entry *dst;
1207 struct ip6_mtuinfo mtuinfo;
1209 if (len < sizeof(mtuinfo))
1210 return -EINVAL;
1212 len = sizeof(mtuinfo);
1213 memset(&mtuinfo, 0, sizeof(mtuinfo));
1215 rcu_read_lock();
1216 dst = __sk_dst_get(sk);
1217 if (dst)
1218 mtuinfo.ip6m_mtu = dst_mtu(dst);
1219 rcu_read_unlock();
1220 if (!mtuinfo.ip6m_mtu)
1221 return -ENOTCONN;
1223 if (put_user(len, optlen))
1224 return -EFAULT;
1225 if (copy_to_user(optval, &mtuinfo, len))
1226 return -EFAULT;
1228 return 0;
1231 case IPV6_TRANSPARENT:
1232 val = inet_sk(sk)->transparent;
1233 break;
1235 case IPV6_FREEBIND:
1236 val = inet_sk(sk)->freebind;
1237 break;
1239 case IPV6_RECVORIGDSTADDR:
1240 val = np->rxopt.bits.rxorigdstaddr;
1241 break;
1243 case IPV6_UNICAST_HOPS:
1244 case IPV6_MULTICAST_HOPS:
1246 struct dst_entry *dst;
1248 if (optname == IPV6_UNICAST_HOPS)
1249 val = np->hop_limit;
1250 else
1251 val = np->mcast_hops;
1253 if (val < 0) {
1254 rcu_read_lock();
1255 dst = __sk_dst_get(sk);
1256 if (dst)
1257 val = ip6_dst_hoplimit(dst);
1258 rcu_read_unlock();
1261 if (val < 0)
1262 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
1263 break;
1266 case IPV6_MULTICAST_LOOP:
1267 val = np->mc_loop;
1268 break;
1270 case IPV6_MULTICAST_IF:
1271 val = np->mcast_oif;
1272 break;
1274 case IPV6_UNICAST_IF:
1275 val = (__force int)htonl((__u32) np->ucast_oif);
1276 break;
1278 case IPV6_MTU_DISCOVER:
1279 val = np->pmtudisc;
1280 break;
1282 case IPV6_RECVERR:
1283 val = np->recverr;
1284 break;
1286 case IPV6_FLOWINFO_SEND:
1287 val = np->sndflow;
1288 break;
1290 case IPV6_FLOWLABEL_MGR:
1292 struct in6_flowlabel_req freq;
1293 int flags;
1295 if (len < sizeof(freq))
1296 return -EINVAL;
1298 if (copy_from_user(&freq, optval, sizeof(freq)))
1299 return -EFAULT;
1301 if (freq.flr_action != IPV6_FL_A_GET)
1302 return -EINVAL;
1304 len = sizeof(freq);
1305 flags = freq.flr_flags;
1307 memset(&freq, 0, sizeof(freq));
1309 val = ipv6_flowlabel_opt_get(sk, &freq, flags);
1310 if (val < 0)
1311 return val;
1313 if (put_user(len, optlen))
1314 return -EFAULT;
1315 if (copy_to_user(optval, &freq, len))
1316 return -EFAULT;
1318 return 0;
1321 case IPV6_ADDR_PREFERENCES:
1322 val = 0;
1324 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1325 val |= IPV6_PREFER_SRC_TMP;
1326 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1327 val |= IPV6_PREFER_SRC_PUBLIC;
1328 else {
1329 /* XXX: should we return system default? */
1330 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1333 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1334 val |= IPV6_PREFER_SRC_COA;
1335 else
1336 val |= IPV6_PREFER_SRC_HOME;
1337 break;
1339 case IPV6_MINHOPCOUNT:
1340 val = np->min_hopcount;
1341 break;
1343 case IPV6_DONTFRAG:
1344 val = np->dontfrag;
1345 break;
1347 case IPV6_AUTOFLOWLABEL:
1348 val = ip6_autoflowlabel(sock_net(sk), np);
1349 break;
1351 case IPV6_RECVFRAGSIZE:
1352 val = np->rxopt.bits.recvfragsize;
1353 break;
1355 default:
1356 return -ENOPROTOOPT;
1358 len = min_t(unsigned int, sizeof(int), len);
1359 if (put_user(len, optlen))
1360 return -EFAULT;
1361 if (copy_to_user(optval, &val, len))
1362 return -EFAULT;
1363 return 0;
1366 int ipv6_getsockopt(struct sock *sk, int level, int optname,
1367 char __user *optval, int __user *optlen)
1369 int err;
1371 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1372 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1374 if (level != SOL_IPV6)
1375 return -ENOPROTOOPT;
1377 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
1378 #ifdef CONFIG_NETFILTER
1379 /* we need to exclude all possible ENOPROTOOPTs except default case */
1380 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1381 int len;
1383 if (get_user(len, optlen))
1384 return -EFAULT;
1386 err = nf_getsockopt(sk, PF_INET6, optname, optval, &len);
1387 if (err >= 0)
1388 err = put_user(len, optlen);
1390 #endif
1391 return err;
1393 EXPORT_SYMBOL(ipv6_getsockopt);
1395 #ifdef CONFIG_COMPAT
1396 int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
1397 char __user *optval, int __user *optlen)
1399 int err;
1401 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
1402 if (udp_prot.compat_getsockopt != NULL)
1403 return udp_prot.compat_getsockopt(sk, level, optname,
1404 optval, optlen);
1405 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1408 if (level != SOL_IPV6)
1409 return -ENOPROTOOPT;
1411 if (optname == MCAST_MSFILTER)
1412 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1413 ipv6_getsockopt);
1415 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen,
1416 MSG_CMSG_COMPAT);
1417 #ifdef CONFIG_NETFILTER
1418 /* we need to exclude all possible ENOPROTOOPTs except default case */
1419 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1420 int len;
1422 if (get_user(len, optlen))
1423 return -EFAULT;
1425 err = compat_nf_getsockopt(sk, PF_INET6, optname, optval, &len);
1426 if (err >= 0)
1427 err = put_user(len, optlen);
1429 #endif
1430 return err;
1432 EXPORT_SYMBOL(compat_ipv6_getsockopt);
1433 #endif