dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / net / ipv6 / ipv6_sockglue.c
blob8d11a034ca3f6217bce4a83ff64ead7ef4c22a4b
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>
56 #include <asm/uaccess.h>
58 struct ip6_ra_chain *ip6_ra_chain;
59 DEFINE_RWLOCK(ip6_ra_lock);
61 int ip6_ra_control(struct sock *sk, int sel)
63 struct ip6_ra_chain *ra, *new_ra, **rap;
65 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
66 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num != IPPROTO_RAW)
67 return -ENOPROTOOPT;
69 new_ra = (sel >= 0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
71 write_lock_bh(&ip6_ra_lock);
72 for (rap = &ip6_ra_chain; (ra = *rap) != NULL; rap = &ra->next) {
73 if (ra->sk == sk) {
74 if (sel >= 0) {
75 write_unlock_bh(&ip6_ra_lock);
76 kfree(new_ra);
77 return -EADDRINUSE;
80 *rap = ra->next;
81 write_unlock_bh(&ip6_ra_lock);
83 sock_put(sk);
84 kfree(ra);
85 return 0;
88 if (!new_ra) {
89 write_unlock_bh(&ip6_ra_lock);
90 return -ENOBUFS;
92 new_ra->sk = sk;
93 new_ra->sel = sel;
94 new_ra->next = ra;
95 *rap = new_ra;
96 sock_hold(sk);
97 write_unlock_bh(&ip6_ra_lock);
98 return 0;
101 static
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_ADD_MEMBERSHIP:
125 case IPV6_DROP_MEMBERSHIP:
126 case IPV6_JOIN_ANYCAST:
127 case IPV6_LEAVE_ANYCAST:
128 case MCAST_JOIN_GROUP:
129 case MCAST_LEAVE_GROUP:
130 case MCAST_JOIN_SOURCE_GROUP:
131 case MCAST_LEAVE_SOURCE_GROUP:
132 case MCAST_BLOCK_SOURCE:
133 case MCAST_UNBLOCK_SOURCE:
134 case MCAST_MSFILTER:
135 return true;
137 return false;
140 static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
141 char __user *optval, unsigned int optlen)
143 struct ipv6_pinfo *np = inet6_sk(sk);
144 struct net *net = sock_net(sk);
145 int val, valbool;
146 int retv = -ENOPROTOOPT;
147 bool needs_rtnl = setsockopt_needs_rtnl(optname);
149 if (!optval)
150 val = 0;
151 else {
152 if (optlen >= sizeof(int)) {
153 if (get_user(val, (int __user *) optval))
154 return -EFAULT;
155 } else
156 val = 0;
159 valbool = (val != 0);
161 if (ip6_mroute_opt(optname))
162 return ip6_mroute_setsockopt(sk, optname, optval, optlen);
164 if (needs_rtnl)
165 rtnl_lock();
166 lock_sock(sk);
168 switch (optname) {
170 case IPV6_ADDRFORM:
171 if (optlen < sizeof(int))
172 goto e_inval;
173 if (val == PF_INET) {
174 struct ipv6_txoptions *opt;
175 struct sk_buff *pktopt;
177 if (sk->sk_type == SOCK_RAW)
178 break;
180 if (sk->sk_protocol == IPPROTO_UDP ||
181 sk->sk_protocol == IPPROTO_UDPLITE) {
182 struct udp_sock *up = udp_sk(sk);
183 if (up->pending == AF_INET6) {
184 retv = -EBUSY;
185 break;
187 } else if (sk->sk_protocol != IPPROTO_TCP)
188 break;
190 if (sk->sk_state != TCP_ESTABLISHED) {
191 retv = -ENOTCONN;
192 break;
195 if (ipv6_only_sock(sk) ||
196 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
197 retv = -EADDRNOTAVAIL;
198 break;
201 fl6_free_socklist(sk);
202 ipv6_sock_mc_close(sk);
205 * Sock is moving from IPv6 to IPv4 (sk_prot), so
206 * remove it from the refcnt debug socks count in the
207 * original family...
209 sk_refcnt_debug_dec(sk);
211 if (sk->sk_protocol == IPPROTO_TCP) {
212 struct inet_connection_sock *icsk = inet_csk(sk);
213 local_bh_disable();
214 sock_prot_inuse_add(net, sk->sk_prot, -1);
215 sock_prot_inuse_add(net, &tcp_prot, 1);
216 local_bh_enable();
217 sk->sk_prot = &tcp_prot;
218 icsk->icsk_af_ops = &ipv4_specific;
219 sk->sk_socket->ops = &inet_stream_ops;
220 sk->sk_family = PF_INET;
221 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
222 } else {
223 struct proto *prot = &udp_prot;
225 if (sk->sk_protocol == IPPROTO_UDPLITE)
226 prot = &udplite_prot;
227 local_bh_disable();
228 sock_prot_inuse_add(net, sk->sk_prot, -1);
229 sock_prot_inuse_add(net, prot, 1);
230 local_bh_enable();
231 sk->sk_prot = prot;
232 sk->sk_socket->ops = &inet_dgram_ops;
233 sk->sk_family = PF_INET;
235 opt = xchg((__force struct ipv6_txoptions **)&np->opt,
236 NULL);
237 if (opt) {
238 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
239 txopt_put(opt);
241 pktopt = xchg(&np->pktoptions, NULL);
242 kfree_skb(pktopt);
244 sk->sk_destruct = inet_sock_destruct;
246 * ... and add it to the refcnt debug socks count
247 * in the new family. -acme
249 sk_refcnt_debug_inc(sk);
250 module_put(THIS_MODULE);
251 retv = 0;
252 break;
254 goto e_inval;
256 case IPV6_V6ONLY:
257 if (optlen < sizeof(int) ||
258 inet_sk(sk)->inet_num)
259 goto e_inval;
260 sk->sk_ipv6only = valbool;
261 retv = 0;
262 break;
264 case IPV6_RECVPKTINFO:
265 if (optlen < sizeof(int))
266 goto e_inval;
267 np->rxopt.bits.rxinfo = valbool;
268 retv = 0;
269 break;
271 case IPV6_2292PKTINFO:
272 if (optlen < sizeof(int))
273 goto e_inval;
274 np->rxopt.bits.rxoinfo = valbool;
275 retv = 0;
276 break;
278 case IPV6_RECVHOPLIMIT:
279 if (optlen < sizeof(int))
280 goto e_inval;
281 np->rxopt.bits.rxhlim = valbool;
282 retv = 0;
283 break;
285 case IPV6_2292HOPLIMIT:
286 if (optlen < sizeof(int))
287 goto e_inval;
288 np->rxopt.bits.rxohlim = valbool;
289 retv = 0;
290 break;
292 case IPV6_RECVRTHDR:
293 if (optlen < sizeof(int))
294 goto e_inval;
295 np->rxopt.bits.srcrt = valbool;
296 retv = 0;
297 break;
299 case IPV6_2292RTHDR:
300 if (optlen < sizeof(int))
301 goto e_inval;
302 np->rxopt.bits.osrcrt = valbool;
303 retv = 0;
304 break;
306 case IPV6_RECVHOPOPTS:
307 if (optlen < sizeof(int))
308 goto e_inval;
309 np->rxopt.bits.hopopts = valbool;
310 retv = 0;
311 break;
313 case IPV6_2292HOPOPTS:
314 if (optlen < sizeof(int))
315 goto e_inval;
316 np->rxopt.bits.ohopopts = valbool;
317 retv = 0;
318 break;
320 case IPV6_RECVDSTOPTS:
321 if (optlen < sizeof(int))
322 goto e_inval;
323 np->rxopt.bits.dstopts = valbool;
324 retv = 0;
325 break;
327 case IPV6_2292DSTOPTS:
328 if (optlen < sizeof(int))
329 goto e_inval;
330 np->rxopt.bits.odstopts = valbool;
331 retv = 0;
332 break;
334 case IPV6_TCLASS:
335 if (optlen < sizeof(int))
336 goto e_inval;
337 if (val < -1 || val > 0xff)
338 goto e_inval;
339 /* RFC 3542, 6.5: default traffic class of 0x0 */
340 if (val == -1)
341 val = 0;
342 np->tclass = val;
343 retv = 0;
344 break;
346 case IPV6_RECVTCLASS:
347 if (optlen < sizeof(int))
348 goto e_inval;
349 np->rxopt.bits.rxtclass = valbool;
350 retv = 0;
351 break;
353 case IPV6_FLOWINFO:
354 if (optlen < sizeof(int))
355 goto e_inval;
356 np->rxopt.bits.rxflow = valbool;
357 retv = 0;
358 break;
360 case IPV6_RECVPATHMTU:
361 if (optlen < sizeof(int))
362 goto e_inval;
363 np->rxopt.bits.rxpmtu = valbool;
364 retv = 0;
365 break;
367 case IPV6_TRANSPARENT:
368 if (valbool && !ns_capable(net->user_ns, CAP_NET_ADMIN) &&
369 !ns_capable(net->user_ns, CAP_NET_RAW)) {
370 retv = -EPERM;
371 break;
373 if (optlen < sizeof(int))
374 goto e_inval;
375 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
376 inet_sk(sk)->transparent = valbool;
377 retv = 0;
378 break;
380 case IPV6_RECVORIGDSTADDR:
381 if (optlen < sizeof(int))
382 goto e_inval;
383 np->rxopt.bits.rxorigdstaddr = valbool;
384 retv = 0;
385 break;
387 case IPV6_HOPOPTS:
388 case IPV6_RTHDRDSTOPTS:
389 case IPV6_RTHDR:
390 case IPV6_DSTOPTS:
392 struct ipv6_txoptions *opt;
394 /* remove any sticky options header with a zero option
395 * length, per RFC3542.
397 if (optlen == 0)
398 optval = NULL;
399 else if (!optval)
400 goto e_inval;
401 else if (optlen < sizeof(struct ipv6_opt_hdr) ||
402 optlen & 0x7 || optlen > 8 * 255)
403 goto e_inval;
405 /* hop-by-hop / destination options are privileged option */
406 retv = -EPERM;
407 if (optname != IPV6_RTHDR && !ns_capable(net->user_ns, CAP_NET_RAW))
408 break;
410 opt = rcu_dereference_protected(np->opt, sock_owned_by_user(sk));
411 opt = ipv6_renew_options(sk, opt, optname,
412 (struct ipv6_opt_hdr __user *)optval,
413 optlen);
414 if (IS_ERR(opt)) {
415 retv = PTR_ERR(opt);
416 break;
419 /* routing header option needs extra check */
420 retv = -EINVAL;
421 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
422 struct ipv6_rt_hdr *rthdr = opt->srcrt;
423 switch (rthdr->type) {
424 #if IS_ENABLED(CONFIG_IPV6_MIP6)
425 case IPV6_SRCRT_TYPE_2:
426 if (rthdr->hdrlen != 2 ||
427 rthdr->segments_left != 1)
428 goto sticky_done;
430 break;
431 #endif
432 default:
433 goto sticky_done;
437 retv = 0;
438 opt = ipv6_update_options(sk, opt);
439 sticky_done:
440 if (opt) {
441 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
442 txopt_put(opt);
444 break;
447 case IPV6_PKTINFO:
449 struct in6_pktinfo pkt;
451 if (optlen == 0)
452 goto e_inval;
453 else if (optlen < sizeof(struct in6_pktinfo) || !optval)
454 goto e_inval;
456 if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) {
457 retv = -EFAULT;
458 break;
460 if (sk->sk_bound_dev_if && pkt.ipi6_ifindex != sk->sk_bound_dev_if)
461 goto e_inval;
463 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
464 np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
465 retv = 0;
466 break;
469 case IPV6_2292PKTOPTIONS:
471 struct ipv6_txoptions *opt = NULL;
472 struct msghdr msg;
473 struct flowi6 fl6;
474 int junk;
476 memset(&fl6, 0, sizeof(fl6));
477 fl6.flowi6_oif = sk->sk_bound_dev_if;
478 fl6.flowi6_mark = sk->sk_mark;
480 if (optlen == 0)
481 goto update;
483 /* 1K is probably excessive
484 * 1K is surely not enough, 2K per standard header is 16K.
486 retv = -EINVAL;
487 if (optlen > 64*1024)
488 break;
490 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
491 retv = -ENOBUFS;
492 if (!opt)
493 break;
495 memset(opt, 0, sizeof(*opt));
496 atomic_set(&opt->refcnt, 1);
497 opt->tot_len = sizeof(*opt) + optlen;
498 retv = -EFAULT;
499 if (copy_from_user(opt+1, optval, optlen))
500 goto done;
502 msg.msg_controllen = optlen;
503 msg.msg_control = (void *)(opt+1);
505 retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, opt, &junk,
506 &junk, &junk);
507 if (retv)
508 goto done;
509 update:
510 retv = 0;
511 opt = ipv6_update_options(sk, opt);
512 done:
513 if (opt) {
514 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
515 txopt_put(opt);
517 break;
519 case IPV6_UNICAST_HOPS:
520 if (optlen < sizeof(int))
521 goto e_inval;
522 if (val > 255 || val < -1)
523 goto e_inval;
524 np->hop_limit = val;
525 retv = 0;
526 break;
528 case IPV6_MULTICAST_HOPS:
529 if (sk->sk_type == SOCK_STREAM)
530 break;
531 if (optlen < sizeof(int))
532 goto e_inval;
533 if (val > 255 || val < -1)
534 goto e_inval;
535 np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
536 retv = 0;
537 break;
539 case IPV6_MULTICAST_LOOP:
540 if (optlen < sizeof(int))
541 goto e_inval;
542 if (val != valbool)
543 goto e_inval;
544 np->mc_loop = valbool;
545 retv = 0;
546 break;
548 case IPV6_UNICAST_IF:
550 struct net_device *dev = NULL;
551 int ifindex;
553 if (optlen != sizeof(int))
554 goto e_inval;
556 ifindex = (__force int)ntohl((__force __be32)val);
557 if (ifindex == 0) {
558 np->ucast_oif = 0;
559 retv = 0;
560 break;
563 dev = dev_get_by_index(net, ifindex);
564 retv = -EADDRNOTAVAIL;
565 if (!dev)
566 break;
567 dev_put(dev);
569 retv = -EINVAL;
570 if (sk->sk_bound_dev_if)
571 break;
573 np->ucast_oif = ifindex;
574 retv = 0;
575 break;
578 case IPV6_MULTICAST_IF:
579 if (sk->sk_type == SOCK_STREAM)
580 break;
581 if (optlen < sizeof(int))
582 goto e_inval;
584 if (val) {
585 struct net_device *dev;
586 int midx;
588 rcu_read_lock();
590 dev = dev_get_by_index_rcu(net, val);
591 if (!dev) {
592 rcu_read_unlock();
593 retv = -ENODEV;
594 break;
596 midx = l3mdev_master_ifindex_rcu(dev);
598 rcu_read_unlock();
600 if (sk->sk_bound_dev_if &&
601 sk->sk_bound_dev_if != val &&
602 (!midx || midx != sk->sk_bound_dev_if))
603 goto e_inval;
605 np->mcast_oif = val;
606 retv = 0;
607 break;
608 case IPV6_ADD_MEMBERSHIP:
609 case IPV6_DROP_MEMBERSHIP:
611 struct ipv6_mreq mreq;
613 if (optlen < sizeof(struct ipv6_mreq))
614 goto e_inval;
616 retv = -EPROTO;
617 if (inet_sk(sk)->is_icsk)
618 break;
620 retv = -EFAULT;
621 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
622 break;
624 if (optname == IPV6_ADD_MEMBERSHIP)
625 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
626 else
627 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
628 break;
630 case IPV6_JOIN_ANYCAST:
631 case IPV6_LEAVE_ANYCAST:
633 struct ipv6_mreq mreq;
635 if (optlen < sizeof(struct ipv6_mreq))
636 goto e_inval;
638 retv = -EFAULT;
639 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
640 break;
642 if (optname == IPV6_JOIN_ANYCAST)
643 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
644 else
645 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
646 break;
648 case MCAST_JOIN_GROUP:
649 case MCAST_LEAVE_GROUP:
651 struct group_req greq;
652 struct sockaddr_in6 *psin6;
654 if (optlen < sizeof(struct group_req))
655 goto e_inval;
657 retv = -EFAULT;
658 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
659 break;
660 if (greq.gr_group.ss_family != AF_INET6) {
661 retv = -EADDRNOTAVAIL;
662 break;
664 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
665 if (optname == MCAST_JOIN_GROUP)
666 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
667 &psin6->sin6_addr);
668 else
669 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
670 &psin6->sin6_addr);
671 break;
673 case MCAST_JOIN_SOURCE_GROUP:
674 case MCAST_LEAVE_SOURCE_GROUP:
675 case MCAST_BLOCK_SOURCE:
676 case MCAST_UNBLOCK_SOURCE:
678 struct group_source_req greqs;
679 int omode, add;
681 if (optlen < sizeof(struct group_source_req))
682 goto e_inval;
683 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
684 retv = -EFAULT;
685 break;
687 if (greqs.gsr_group.ss_family != AF_INET6 ||
688 greqs.gsr_source.ss_family != AF_INET6) {
689 retv = -EADDRNOTAVAIL;
690 break;
692 if (optname == MCAST_BLOCK_SOURCE) {
693 omode = MCAST_EXCLUDE;
694 add = 1;
695 } else if (optname == MCAST_UNBLOCK_SOURCE) {
696 omode = MCAST_EXCLUDE;
697 add = 0;
698 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
699 struct sockaddr_in6 *psin6;
701 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
702 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
703 &psin6->sin6_addr);
704 /* prior join w/ different source is ok */
705 if (retv && retv != -EADDRINUSE)
706 break;
707 omode = MCAST_INCLUDE;
708 add = 1;
709 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
710 omode = MCAST_INCLUDE;
711 add = 0;
713 retv = ip6_mc_source(add, omode, sk, &greqs);
714 break;
716 case MCAST_MSFILTER:
718 struct group_filter *gsf;
720 if (optlen < GROUP_FILTER_SIZE(0))
721 goto e_inval;
722 if (optlen > sysctl_optmem_max) {
723 retv = -ENOBUFS;
724 break;
726 gsf = kmalloc(optlen, GFP_KERNEL);
727 if (!gsf) {
728 retv = -ENOBUFS;
729 break;
731 retv = -EFAULT;
732 if (copy_from_user(gsf, optval, optlen)) {
733 kfree(gsf);
734 break;
736 /* numsrc >= (4G-140)/128 overflow in 32 bits */
737 if (gsf->gf_numsrc >= 0x1ffffffU ||
738 gsf->gf_numsrc > sysctl_mld_max_msf) {
739 kfree(gsf);
740 retv = -ENOBUFS;
741 break;
743 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
744 kfree(gsf);
745 retv = -EINVAL;
746 break;
748 retv = ip6_mc_msfilter(sk, gsf);
749 kfree(gsf);
751 break;
753 case IPV6_ROUTER_ALERT:
754 if (optlen < sizeof(int))
755 goto e_inval;
756 retv = ip6_ra_control(sk, val);
757 break;
758 case IPV6_MTU_DISCOVER:
759 if (optlen < sizeof(int))
760 goto e_inval;
761 if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
762 goto e_inval;
763 np->pmtudisc = val;
764 retv = 0;
765 break;
766 case IPV6_MTU:
767 if (optlen < sizeof(int))
768 goto e_inval;
769 if (val && val < IPV6_MIN_MTU)
770 goto e_inval;
771 np->frag_size = val;
772 retv = 0;
773 break;
774 case IPV6_RECVERR:
775 if (optlen < sizeof(int))
776 goto e_inval;
777 np->recverr = valbool;
778 if (!val)
779 skb_queue_purge(&sk->sk_error_queue);
780 retv = 0;
781 break;
782 case IPV6_FLOWINFO_SEND:
783 if (optlen < sizeof(int))
784 goto e_inval;
785 np->sndflow = valbool;
786 retv = 0;
787 break;
788 case IPV6_FLOWLABEL_MGR:
789 retv = ipv6_flowlabel_opt(sk, optval, optlen);
790 break;
791 case IPV6_IPSEC_POLICY:
792 case IPV6_XFRM_POLICY:
793 retv = -EPERM;
794 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
795 break;
796 retv = xfrm_user_policy(sk, optname, optval, optlen);
797 break;
799 case IPV6_ADDR_PREFERENCES:
801 unsigned int pref = 0;
802 unsigned int prefmask = ~0;
804 if (optlen < sizeof(int))
805 goto e_inval;
807 retv = -EINVAL;
809 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
810 switch (val & (IPV6_PREFER_SRC_PUBLIC|
811 IPV6_PREFER_SRC_TMP|
812 IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
813 case IPV6_PREFER_SRC_PUBLIC:
814 pref |= IPV6_PREFER_SRC_PUBLIC;
815 break;
816 case IPV6_PREFER_SRC_TMP:
817 pref |= IPV6_PREFER_SRC_TMP;
818 break;
819 case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
820 break;
821 case 0:
822 goto pref_skip_pubtmp;
823 default:
824 goto e_inval;
827 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
828 IPV6_PREFER_SRC_TMP);
829 pref_skip_pubtmp:
831 /* check HOME/COA conflicts */
832 switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
833 case IPV6_PREFER_SRC_HOME:
834 break;
835 case IPV6_PREFER_SRC_COA:
836 pref |= IPV6_PREFER_SRC_COA;
837 case 0:
838 goto pref_skip_coa;
839 default:
840 goto e_inval;
843 prefmask &= ~IPV6_PREFER_SRC_COA;
844 pref_skip_coa:
846 /* check CGA/NONCGA conflicts */
847 switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
848 case IPV6_PREFER_SRC_CGA:
849 case IPV6_PREFER_SRC_NONCGA:
850 case 0:
851 break;
852 default:
853 goto e_inval;
856 np->srcprefs = (np->srcprefs & prefmask) | pref;
857 retv = 0;
859 break;
861 case IPV6_MINHOPCOUNT:
862 if (optlen < sizeof(int))
863 goto e_inval;
864 if (val < 0 || val > 255)
865 goto e_inval;
866 np->min_hopcount = val;
867 retv = 0;
868 break;
869 case IPV6_DONTFRAG:
870 np->dontfrag = valbool;
871 retv = 0;
872 break;
873 case IPV6_AUTOFLOWLABEL:
874 np->autoflowlabel = valbool;
875 np->autoflowlabel_set = 1;
876 retv = 0;
877 break;
880 release_sock(sk);
881 if (needs_rtnl)
882 rtnl_unlock();
884 return retv;
886 e_inval:
887 release_sock(sk);
888 if (needs_rtnl)
889 rtnl_unlock();
890 return -EINVAL;
893 int ipv6_setsockopt(struct sock *sk, int level, int optname,
894 char __user *optval, unsigned int optlen)
896 int err;
898 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
899 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
901 if (level != SOL_IPV6)
902 return -ENOPROTOOPT;
904 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
905 #ifdef CONFIG_NETFILTER
906 /* we need to exclude all possible ENOPROTOOPTs except default case */
907 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
908 optname != IPV6_XFRM_POLICY)
909 err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen);
910 #endif
911 return err;
913 EXPORT_SYMBOL(ipv6_setsockopt);
915 #ifdef CONFIG_COMPAT
916 int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
917 char __user *optval, unsigned int optlen)
919 int err;
921 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
922 if (udp_prot.compat_setsockopt != NULL)
923 return udp_prot.compat_setsockopt(sk, level, optname,
924 optval, optlen);
925 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
928 if (level != SOL_IPV6)
929 return -ENOPROTOOPT;
931 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
932 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
933 ipv6_setsockopt);
935 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
936 #ifdef CONFIG_NETFILTER
937 /* we need to exclude all possible ENOPROTOOPTs except default case */
938 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
939 optname != IPV6_XFRM_POLICY)
940 err = compat_nf_setsockopt(sk, PF_INET6, optname, optval,
941 optlen);
942 #endif
943 return err;
945 EXPORT_SYMBOL(compat_ipv6_setsockopt);
946 #endif
948 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
949 int optname, char __user *optval, int len)
951 struct ipv6_opt_hdr *hdr;
953 if (!opt)
954 return 0;
956 switch (optname) {
957 case IPV6_HOPOPTS:
958 hdr = opt->hopopt;
959 break;
960 case IPV6_RTHDRDSTOPTS:
961 hdr = opt->dst0opt;
962 break;
963 case IPV6_RTHDR:
964 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
965 break;
966 case IPV6_DSTOPTS:
967 hdr = opt->dst1opt;
968 break;
969 default:
970 return -EINVAL; /* should not happen */
973 if (!hdr)
974 return 0;
976 len = min_t(unsigned int, len, ipv6_optlen(hdr));
977 if (copy_to_user(optval, hdr, len))
978 return -EFAULT;
979 return len;
982 static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
983 char __user *optval, int __user *optlen, unsigned int flags)
985 struct ipv6_pinfo *np = inet6_sk(sk);
986 int len;
987 int val;
989 if (ip6_mroute_opt(optname))
990 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
992 if (get_user(len, optlen))
993 return -EFAULT;
994 switch (optname) {
995 case IPV6_ADDRFORM:
996 if (sk->sk_protocol != IPPROTO_UDP &&
997 sk->sk_protocol != IPPROTO_UDPLITE &&
998 sk->sk_protocol != IPPROTO_TCP)
999 return -ENOPROTOOPT;
1000 if (sk->sk_state != TCP_ESTABLISHED)
1001 return -ENOTCONN;
1002 val = sk->sk_family;
1003 break;
1004 case MCAST_MSFILTER:
1006 struct group_filter gsf;
1007 int err;
1009 if (len < GROUP_FILTER_SIZE(0))
1010 return -EINVAL;
1011 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
1012 return -EFAULT;
1013 if (gsf.gf_group.ss_family != AF_INET6)
1014 return -EADDRNOTAVAIL;
1015 lock_sock(sk);
1016 err = ip6_mc_msfget(sk, &gsf,
1017 (struct group_filter __user *)optval, optlen);
1018 release_sock(sk);
1019 return err;
1022 case IPV6_2292PKTOPTIONS:
1024 struct msghdr msg;
1025 struct sk_buff *skb;
1027 if (sk->sk_type != SOCK_STREAM)
1028 return -ENOPROTOOPT;
1030 msg.msg_control = optval;
1031 msg.msg_controllen = len;
1032 msg.msg_flags = flags;
1034 lock_sock(sk);
1035 skb = np->pktoptions;
1036 if (skb)
1037 ip6_datagram_recv_ctl(sk, &msg, skb);
1038 release_sock(sk);
1039 if (!skb) {
1040 if (np->rxopt.bits.rxinfo) {
1041 struct in6_pktinfo src_info;
1042 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1043 np->sticky_pktinfo.ipi6_ifindex;
1044 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
1045 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1047 if (np->rxopt.bits.rxhlim) {
1048 int hlim = np->mcast_hops;
1049 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1051 if (np->rxopt.bits.rxtclass) {
1052 int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1054 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1056 if (np->rxopt.bits.rxoinfo) {
1057 struct in6_pktinfo src_info;
1058 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1059 np->sticky_pktinfo.ipi6_ifindex;
1060 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1061 np->sticky_pktinfo.ipi6_addr;
1062 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1064 if (np->rxopt.bits.rxohlim) {
1065 int hlim = np->mcast_hops;
1066 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1068 if (np->rxopt.bits.rxflow) {
1069 __be32 flowinfo = np->rcv_flowinfo;
1071 put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1074 len -= msg.msg_controllen;
1075 return put_user(len, optlen);
1077 case IPV6_MTU:
1079 struct dst_entry *dst;
1081 val = 0;
1082 rcu_read_lock();
1083 dst = __sk_dst_get(sk);
1084 if (dst)
1085 val = dst_mtu(dst);
1086 rcu_read_unlock();
1087 if (!val)
1088 return -ENOTCONN;
1089 break;
1092 case IPV6_V6ONLY:
1093 val = sk->sk_ipv6only;
1094 break;
1096 case IPV6_RECVPKTINFO:
1097 val = np->rxopt.bits.rxinfo;
1098 break;
1100 case IPV6_2292PKTINFO:
1101 val = np->rxopt.bits.rxoinfo;
1102 break;
1104 case IPV6_RECVHOPLIMIT:
1105 val = np->rxopt.bits.rxhlim;
1106 break;
1108 case IPV6_2292HOPLIMIT:
1109 val = np->rxopt.bits.rxohlim;
1110 break;
1112 case IPV6_RECVRTHDR:
1113 val = np->rxopt.bits.srcrt;
1114 break;
1116 case IPV6_2292RTHDR:
1117 val = np->rxopt.bits.osrcrt;
1118 break;
1120 case IPV6_HOPOPTS:
1121 case IPV6_RTHDRDSTOPTS:
1122 case IPV6_RTHDR:
1123 case IPV6_DSTOPTS:
1125 struct ipv6_txoptions *opt;
1127 lock_sock(sk);
1128 opt = rcu_dereference_protected(np->opt, sock_owned_by_user(sk));
1129 len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len);
1130 release_sock(sk);
1131 /* check if ipv6_getsockopt_sticky() returns err code */
1132 if (len < 0)
1133 return len;
1134 return put_user(len, optlen);
1137 case IPV6_RECVHOPOPTS:
1138 val = np->rxopt.bits.hopopts;
1139 break;
1141 case IPV6_2292HOPOPTS:
1142 val = np->rxopt.bits.ohopopts;
1143 break;
1145 case IPV6_RECVDSTOPTS:
1146 val = np->rxopt.bits.dstopts;
1147 break;
1149 case IPV6_2292DSTOPTS:
1150 val = np->rxopt.bits.odstopts;
1151 break;
1153 case IPV6_TCLASS:
1154 val = np->tclass;
1155 break;
1157 case IPV6_RECVTCLASS:
1158 val = np->rxopt.bits.rxtclass;
1159 break;
1161 case IPV6_FLOWINFO:
1162 val = np->rxopt.bits.rxflow;
1163 break;
1165 case IPV6_RECVPATHMTU:
1166 val = np->rxopt.bits.rxpmtu;
1167 break;
1169 case IPV6_PATHMTU:
1171 struct dst_entry *dst;
1172 struct ip6_mtuinfo mtuinfo;
1174 if (len < sizeof(mtuinfo))
1175 return -EINVAL;
1177 len = sizeof(mtuinfo);
1178 memset(&mtuinfo, 0, sizeof(mtuinfo));
1180 rcu_read_lock();
1181 dst = __sk_dst_get(sk);
1182 if (dst)
1183 mtuinfo.ip6m_mtu = dst_mtu(dst);
1184 rcu_read_unlock();
1185 if (!mtuinfo.ip6m_mtu)
1186 return -ENOTCONN;
1188 if (put_user(len, optlen))
1189 return -EFAULT;
1190 if (copy_to_user(optval, &mtuinfo, len))
1191 return -EFAULT;
1193 return 0;
1196 case IPV6_TRANSPARENT:
1197 val = inet_sk(sk)->transparent;
1198 break;
1200 case IPV6_RECVORIGDSTADDR:
1201 val = np->rxopt.bits.rxorigdstaddr;
1202 break;
1204 case IPV6_UNICAST_HOPS:
1205 case IPV6_MULTICAST_HOPS:
1207 struct dst_entry *dst;
1209 if (optname == IPV6_UNICAST_HOPS)
1210 val = np->hop_limit;
1211 else
1212 val = np->mcast_hops;
1214 if (val < 0) {
1215 rcu_read_lock();
1216 dst = __sk_dst_get(sk);
1217 if (dst)
1218 val = ip6_dst_hoplimit(dst);
1219 rcu_read_unlock();
1222 if (val < 0)
1223 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
1224 break;
1227 case IPV6_MULTICAST_LOOP:
1228 val = np->mc_loop;
1229 break;
1231 case IPV6_MULTICAST_IF:
1232 val = np->mcast_oif;
1233 break;
1235 case IPV6_UNICAST_IF:
1236 val = (__force int)htonl((__u32) np->ucast_oif);
1237 break;
1239 case IPV6_MTU_DISCOVER:
1240 val = np->pmtudisc;
1241 break;
1243 case IPV6_RECVERR:
1244 val = np->recverr;
1245 break;
1247 case IPV6_FLOWINFO_SEND:
1248 val = np->sndflow;
1249 break;
1251 case IPV6_FLOWLABEL_MGR:
1253 struct in6_flowlabel_req freq;
1254 int flags;
1256 if (len < sizeof(freq))
1257 return -EINVAL;
1259 if (copy_from_user(&freq, optval, sizeof(freq)))
1260 return -EFAULT;
1262 if (freq.flr_action != IPV6_FL_A_GET)
1263 return -EINVAL;
1265 len = sizeof(freq);
1266 flags = freq.flr_flags;
1268 memset(&freq, 0, sizeof(freq));
1270 val = ipv6_flowlabel_opt_get(sk, &freq, flags);
1271 if (val < 0)
1272 return val;
1274 if (put_user(len, optlen))
1275 return -EFAULT;
1276 if (copy_to_user(optval, &freq, len))
1277 return -EFAULT;
1279 return 0;
1282 case IPV6_ADDR_PREFERENCES:
1283 val = 0;
1285 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1286 val |= IPV6_PREFER_SRC_TMP;
1287 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1288 val |= IPV6_PREFER_SRC_PUBLIC;
1289 else {
1290 /* XXX: should we return system default? */
1291 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1294 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1295 val |= IPV6_PREFER_SRC_COA;
1296 else
1297 val |= IPV6_PREFER_SRC_HOME;
1298 break;
1300 case IPV6_MINHOPCOUNT:
1301 val = np->min_hopcount;
1302 break;
1304 case IPV6_DONTFRAG:
1305 val = np->dontfrag;
1306 break;
1308 case IPV6_AUTOFLOWLABEL:
1309 val = ip6_autoflowlabel(sock_net(sk), np);
1310 break;
1312 default:
1313 return -ENOPROTOOPT;
1315 len = min_t(unsigned int, sizeof(int), len);
1316 if (put_user(len, optlen))
1317 return -EFAULT;
1318 if (copy_to_user(optval, &val, len))
1319 return -EFAULT;
1320 return 0;
1323 int ipv6_getsockopt(struct sock *sk, int level, int optname,
1324 char __user *optval, int __user *optlen)
1326 int err;
1328 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1329 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1331 if (level != SOL_IPV6)
1332 return -ENOPROTOOPT;
1334 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
1335 #ifdef CONFIG_NETFILTER
1336 /* we need to exclude all possible ENOPROTOOPTs except default case */
1337 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1338 int len;
1340 if (get_user(len, optlen))
1341 return -EFAULT;
1343 err = nf_getsockopt(sk, PF_INET6, optname, optval, &len);
1344 if (err >= 0)
1345 err = put_user(len, optlen);
1347 #endif
1348 return err;
1350 EXPORT_SYMBOL(ipv6_getsockopt);
1352 #ifdef CONFIG_COMPAT
1353 int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
1354 char __user *optval, int __user *optlen)
1356 int err;
1358 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
1359 if (udp_prot.compat_getsockopt != NULL)
1360 return udp_prot.compat_getsockopt(sk, level, optname,
1361 optval, optlen);
1362 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1365 if (level != SOL_IPV6)
1366 return -ENOPROTOOPT;
1368 if (optname == MCAST_MSFILTER)
1369 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1370 ipv6_getsockopt);
1372 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen,
1373 MSG_CMSG_COMPAT);
1374 #ifdef CONFIG_NETFILTER
1375 /* we need to exclude all possible ENOPROTOOPTs except default case */
1376 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1377 int len;
1379 if (get_user(len, optlen))
1380 return -EFAULT;
1382 err = compat_nf_getsockopt(sk, PF_INET6, optname, optval, &len);
1383 if (err >= 0)
1384 err = put_user(len, optlen);
1386 #endif
1387 return err;
1389 EXPORT_SYMBOL(compat_ipv6_getsockopt);
1390 #endif