x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / net / ipv6 / ipv6_sockglue.c
blob5c91b05c8d8feb3207741a4d9b821df7e5a24cc6
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;
188 } else if (sk->sk_protocol != IPPROTO_TCP)
189 break;
191 if (sk->sk_state != TCP_ESTABLISHED) {
192 retv = -ENOTCONN;
193 break;
196 if (ipv6_only_sock(sk) ||
197 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
198 retv = -EADDRNOTAVAIL;
199 break;
202 fl6_free_socklist(sk);
203 __ipv6_sock_mc_close(sk);
206 * Sock is moving from IPv6 to IPv4 (sk_prot), so
207 * remove it from the refcnt debug socks count in the
208 * original family...
210 sk_refcnt_debug_dec(sk);
212 if (sk->sk_protocol == IPPROTO_TCP) {
213 struct inet_connection_sock *icsk = inet_csk(sk);
214 local_bh_disable();
215 sock_prot_inuse_add(net, sk->sk_prot, -1);
216 sock_prot_inuse_add(net, &tcp_prot, 1);
217 local_bh_enable();
218 sk->sk_prot = &tcp_prot;
219 icsk->icsk_af_ops = &ipv4_specific;
220 sk->sk_socket->ops = &inet_stream_ops;
221 sk->sk_family = PF_INET;
222 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
223 } else {
224 struct proto *prot = &udp_prot;
226 if (sk->sk_protocol == IPPROTO_UDPLITE)
227 prot = &udplite_prot;
228 local_bh_disable();
229 sock_prot_inuse_add(net, sk->sk_prot, -1);
230 sock_prot_inuse_add(net, prot, 1);
231 local_bh_enable();
232 sk->sk_prot = prot;
233 sk->sk_socket->ops = &inet_dgram_ops;
234 sk->sk_family = PF_INET;
236 opt = xchg((__force struct ipv6_txoptions **)&np->opt,
237 NULL);
238 if (opt) {
239 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
240 txopt_put(opt);
242 pktopt = xchg(&np->pktoptions, NULL);
243 kfree_skb(pktopt);
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;
393 struct ipv6_opt_hdr *new = NULL;
395 /* hop-by-hop / destination options are privileged option */
396 retv = -EPERM;
397 if (optname != IPV6_RTHDR && !ns_capable(net->user_ns, CAP_NET_RAW))
398 break;
400 /* remove any sticky options header with a zero option
401 * length, per RFC3542.
403 if (optlen == 0)
404 optval = NULL;
405 else if (!optval)
406 goto e_inval;
407 else if (optlen < sizeof(struct ipv6_opt_hdr) ||
408 optlen & 0x7 || optlen > 8 * 255)
409 goto e_inval;
410 else {
411 new = memdup_user(optval, optlen);
412 if (IS_ERR(new)) {
413 retv = PTR_ERR(new);
414 break;
416 if (unlikely(ipv6_optlen(new) > optlen)) {
417 kfree(new);
418 goto e_inval;
422 opt = rcu_dereference_protected(np->opt,
423 lockdep_sock_is_held(sk));
424 opt = ipv6_renew_options(sk, opt, optname, new);
425 kfree(new);
426 if (IS_ERR(opt)) {
427 retv = PTR_ERR(opt);
428 break;
431 /* routing header option needs extra check */
432 retv = -EINVAL;
433 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
434 struct ipv6_rt_hdr *rthdr = opt->srcrt;
435 switch (rthdr->type) {
436 #if IS_ENABLED(CONFIG_IPV6_MIP6)
437 case IPV6_SRCRT_TYPE_2:
438 if (rthdr->hdrlen != 2 ||
439 rthdr->segments_left != 1)
440 goto sticky_done;
442 break;
443 #endif
444 case IPV6_SRCRT_TYPE_4:
446 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)
447 opt->srcrt;
449 if (!seg6_validate_srh(srh, optlen))
450 goto sticky_done;
451 break;
453 default:
454 goto sticky_done;
458 retv = 0;
459 opt = ipv6_update_options(sk, opt);
460 sticky_done:
461 if (opt) {
462 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
463 txopt_put(opt);
465 break;
468 case IPV6_PKTINFO:
470 struct in6_pktinfo pkt;
472 if (optlen == 0)
473 goto e_inval;
474 else if (optlen < sizeof(struct in6_pktinfo) || !optval)
475 goto e_inval;
477 if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) {
478 retv = -EFAULT;
479 break;
481 if (sk->sk_bound_dev_if && pkt.ipi6_ifindex != sk->sk_bound_dev_if)
482 goto e_inval;
484 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
485 np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
486 retv = 0;
487 break;
490 case IPV6_2292PKTOPTIONS:
492 struct ipv6_txoptions *opt = NULL;
493 struct msghdr msg;
494 struct flowi6 fl6;
495 struct sockcm_cookie sockc_junk;
496 struct ipcm6_cookie ipc6;
498 memset(&fl6, 0, sizeof(fl6));
499 fl6.flowi6_oif = sk->sk_bound_dev_if;
500 fl6.flowi6_mark = sk->sk_mark;
502 if (optlen == 0)
503 goto update;
505 /* 1K is probably excessive
506 * 1K is surely not enough, 2K per standard header is 16K.
508 retv = -EINVAL;
509 if (optlen > 64*1024)
510 break;
512 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
513 retv = -ENOBUFS;
514 if (!opt)
515 break;
517 memset(opt, 0, sizeof(*opt));
518 refcount_set(&opt->refcnt, 1);
519 opt->tot_len = sizeof(*opt) + optlen;
520 retv = -EFAULT;
521 if (copy_from_user(opt+1, optval, optlen))
522 goto done;
524 msg.msg_controllen = optlen;
525 msg.msg_control = (void *)(opt+1);
526 ipc6.opt = opt;
528 retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6, &sockc_junk);
529 if (retv)
530 goto done;
531 update:
532 retv = 0;
533 opt = ipv6_update_options(sk, opt);
534 done:
535 if (opt) {
536 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
537 txopt_put(opt);
539 break;
541 case IPV6_UNICAST_HOPS:
542 if (optlen < sizeof(int))
543 goto e_inval;
544 if (val > 255 || val < -1)
545 goto e_inval;
546 np->hop_limit = val;
547 retv = 0;
548 break;
550 case IPV6_MULTICAST_HOPS:
551 if (sk->sk_type == SOCK_STREAM)
552 break;
553 if (optlen < sizeof(int))
554 goto e_inval;
555 if (val > 255 || val < -1)
556 goto e_inval;
557 np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
558 retv = 0;
559 break;
561 case IPV6_MULTICAST_LOOP:
562 if (optlen < sizeof(int))
563 goto e_inval;
564 if (val != valbool)
565 goto e_inval;
566 np->mc_loop = valbool;
567 retv = 0;
568 break;
570 case IPV6_UNICAST_IF:
572 struct net_device *dev = NULL;
573 int ifindex;
575 if (optlen != sizeof(int))
576 goto e_inval;
578 ifindex = (__force int)ntohl((__force __be32)val);
579 if (ifindex == 0) {
580 np->ucast_oif = 0;
581 retv = 0;
582 break;
585 dev = dev_get_by_index(net, ifindex);
586 retv = -EADDRNOTAVAIL;
587 if (!dev)
588 break;
589 dev_put(dev);
591 retv = -EINVAL;
592 if (sk->sk_bound_dev_if)
593 break;
595 np->ucast_oif = ifindex;
596 retv = 0;
597 break;
600 case IPV6_MULTICAST_IF:
601 if (sk->sk_type == SOCK_STREAM)
602 break;
603 if (optlen < sizeof(int))
604 goto e_inval;
606 if (val) {
607 struct net_device *dev;
608 int midx;
610 rcu_read_lock();
612 dev = dev_get_by_index_rcu(net, val);
613 if (!dev) {
614 rcu_read_unlock();
615 retv = -ENODEV;
616 break;
618 midx = l3mdev_master_ifindex_rcu(dev);
620 rcu_read_unlock();
622 if (sk->sk_bound_dev_if &&
623 sk->sk_bound_dev_if != val &&
624 (!midx || midx != sk->sk_bound_dev_if))
625 goto e_inval;
627 np->mcast_oif = val;
628 retv = 0;
629 break;
630 case IPV6_ADD_MEMBERSHIP:
631 case IPV6_DROP_MEMBERSHIP:
633 struct ipv6_mreq mreq;
635 if (optlen < sizeof(struct ipv6_mreq))
636 goto e_inval;
638 retv = -EPROTO;
639 if (inet_sk(sk)->is_icsk)
640 break;
642 retv = -EFAULT;
643 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
644 break;
646 if (optname == IPV6_ADD_MEMBERSHIP)
647 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
648 else
649 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
650 break;
652 case IPV6_JOIN_ANYCAST:
653 case IPV6_LEAVE_ANYCAST:
655 struct ipv6_mreq mreq;
657 if (optlen < sizeof(struct ipv6_mreq))
658 goto e_inval;
660 retv = -EFAULT;
661 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
662 break;
664 if (optname == IPV6_JOIN_ANYCAST)
665 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
666 else
667 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
668 break;
670 case MCAST_JOIN_GROUP:
671 case MCAST_LEAVE_GROUP:
673 struct group_req greq;
674 struct sockaddr_in6 *psin6;
676 if (optlen < sizeof(struct group_req))
677 goto e_inval;
679 retv = -EFAULT;
680 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
681 break;
682 if (greq.gr_group.ss_family != AF_INET6) {
683 retv = -EADDRNOTAVAIL;
684 break;
686 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
687 if (optname == MCAST_JOIN_GROUP)
688 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
689 &psin6->sin6_addr);
690 else
691 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
692 &psin6->sin6_addr);
693 break;
695 case MCAST_JOIN_SOURCE_GROUP:
696 case MCAST_LEAVE_SOURCE_GROUP:
697 case MCAST_BLOCK_SOURCE:
698 case MCAST_UNBLOCK_SOURCE:
700 struct group_source_req greqs;
701 int omode, add;
703 if (optlen < sizeof(struct group_source_req))
704 goto e_inval;
705 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
706 retv = -EFAULT;
707 break;
709 if (greqs.gsr_group.ss_family != AF_INET6 ||
710 greqs.gsr_source.ss_family != AF_INET6) {
711 retv = -EADDRNOTAVAIL;
712 break;
714 if (optname == MCAST_BLOCK_SOURCE) {
715 omode = MCAST_EXCLUDE;
716 add = 1;
717 } else if (optname == MCAST_UNBLOCK_SOURCE) {
718 omode = MCAST_EXCLUDE;
719 add = 0;
720 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
721 struct sockaddr_in6 *psin6;
723 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
724 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
725 &psin6->sin6_addr);
726 /* prior join w/ different source is ok */
727 if (retv && retv != -EADDRINUSE)
728 break;
729 omode = MCAST_INCLUDE;
730 add = 1;
731 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
732 omode = MCAST_INCLUDE;
733 add = 0;
735 retv = ip6_mc_source(add, omode, sk, &greqs);
736 break;
738 case MCAST_MSFILTER:
740 struct group_filter *gsf;
742 if (optlen < GROUP_FILTER_SIZE(0))
743 goto e_inval;
744 if (optlen > sysctl_optmem_max) {
745 retv = -ENOBUFS;
746 break;
748 gsf = memdup_user(optval, optlen);
749 if (IS_ERR(gsf)) {
750 retv = PTR_ERR(gsf);
751 break;
753 /* numsrc >= (4G-140)/128 overflow in 32 bits */
754 if (gsf->gf_numsrc >= 0x1ffffffU ||
755 gsf->gf_numsrc > sysctl_mld_max_msf) {
756 kfree(gsf);
757 retv = -ENOBUFS;
758 break;
760 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
761 kfree(gsf);
762 retv = -EINVAL;
763 break;
765 retv = ip6_mc_msfilter(sk, gsf);
766 kfree(gsf);
768 break;
770 case IPV6_ROUTER_ALERT:
771 if (optlen < sizeof(int))
772 goto e_inval;
773 retv = ip6_ra_control(sk, val);
774 break;
775 case IPV6_MTU_DISCOVER:
776 if (optlen < sizeof(int))
777 goto e_inval;
778 if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
779 goto e_inval;
780 np->pmtudisc = val;
781 retv = 0;
782 break;
783 case IPV6_MTU:
784 if (optlen < sizeof(int))
785 goto e_inval;
786 if (val && val < IPV6_MIN_MTU)
787 goto e_inval;
788 np->frag_size = val;
789 retv = 0;
790 break;
791 case IPV6_RECVERR:
792 if (optlen < sizeof(int))
793 goto e_inval;
794 np->recverr = valbool;
795 if (!val)
796 skb_queue_purge(&sk->sk_error_queue);
797 retv = 0;
798 break;
799 case IPV6_FLOWINFO_SEND:
800 if (optlen < sizeof(int))
801 goto e_inval;
802 np->sndflow = valbool;
803 retv = 0;
804 break;
805 case IPV6_FLOWLABEL_MGR:
806 retv = ipv6_flowlabel_opt(sk, optval, optlen);
807 break;
808 case IPV6_IPSEC_POLICY:
809 case IPV6_XFRM_POLICY:
810 retv = -EPERM;
811 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
812 break;
813 retv = xfrm_user_policy(sk, optname, optval, optlen);
814 break;
816 case IPV6_ADDR_PREFERENCES:
818 unsigned int pref = 0;
819 unsigned int prefmask = ~0;
821 if (optlen < sizeof(int))
822 goto e_inval;
824 retv = -EINVAL;
826 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
827 switch (val & (IPV6_PREFER_SRC_PUBLIC|
828 IPV6_PREFER_SRC_TMP|
829 IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
830 case IPV6_PREFER_SRC_PUBLIC:
831 pref |= IPV6_PREFER_SRC_PUBLIC;
832 break;
833 case IPV6_PREFER_SRC_TMP:
834 pref |= IPV6_PREFER_SRC_TMP;
835 break;
836 case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
837 break;
838 case 0:
839 goto pref_skip_pubtmp;
840 default:
841 goto e_inval;
844 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
845 IPV6_PREFER_SRC_TMP);
846 pref_skip_pubtmp:
848 /* check HOME/COA conflicts */
849 switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
850 case IPV6_PREFER_SRC_HOME:
851 break;
852 case IPV6_PREFER_SRC_COA:
853 pref |= IPV6_PREFER_SRC_COA;
854 case 0:
855 goto pref_skip_coa;
856 default:
857 goto e_inval;
860 prefmask &= ~IPV6_PREFER_SRC_COA;
861 pref_skip_coa:
863 /* check CGA/NONCGA conflicts */
864 switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
865 case IPV6_PREFER_SRC_CGA:
866 case IPV6_PREFER_SRC_NONCGA:
867 case 0:
868 break;
869 default:
870 goto e_inval;
873 np->srcprefs = (np->srcprefs & prefmask) | pref;
874 retv = 0;
876 break;
878 case IPV6_MINHOPCOUNT:
879 if (optlen < sizeof(int))
880 goto e_inval;
881 if (val < 0 || val > 255)
882 goto e_inval;
883 np->min_hopcount = val;
884 retv = 0;
885 break;
886 case IPV6_DONTFRAG:
887 np->dontfrag = valbool;
888 retv = 0;
889 break;
890 case IPV6_AUTOFLOWLABEL:
891 np->autoflowlabel = valbool;
892 np->autoflowlabel_set = 1;
893 retv = 0;
894 break;
895 case IPV6_RECVFRAGSIZE:
896 np->rxopt.bits.recvfragsize = valbool;
897 retv = 0;
898 break;
901 release_sock(sk);
902 if (needs_rtnl)
903 rtnl_unlock();
905 return retv;
907 e_inval:
908 release_sock(sk);
909 if (needs_rtnl)
910 rtnl_unlock();
911 return -EINVAL;
914 int ipv6_setsockopt(struct sock *sk, int level, int optname,
915 char __user *optval, unsigned int optlen)
917 int err;
919 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
920 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
922 if (level != SOL_IPV6)
923 return -ENOPROTOOPT;
925 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
926 #ifdef CONFIG_NETFILTER
927 /* we need to exclude all possible ENOPROTOOPTs except default case */
928 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
929 optname != IPV6_XFRM_POLICY)
930 err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen);
931 #endif
932 return err;
934 EXPORT_SYMBOL(ipv6_setsockopt);
936 #ifdef CONFIG_COMPAT
937 int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
938 char __user *optval, unsigned int optlen)
940 int err;
942 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
943 if (udp_prot.compat_setsockopt != NULL)
944 return udp_prot.compat_setsockopt(sk, level, optname,
945 optval, optlen);
946 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
949 if (level != SOL_IPV6)
950 return -ENOPROTOOPT;
952 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
953 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
954 ipv6_setsockopt);
956 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
957 #ifdef CONFIG_NETFILTER
958 /* we need to exclude all possible ENOPROTOOPTs except default case */
959 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
960 optname != IPV6_XFRM_POLICY)
961 err = compat_nf_setsockopt(sk, PF_INET6, optname, optval,
962 optlen);
963 #endif
964 return err;
966 EXPORT_SYMBOL(compat_ipv6_setsockopt);
967 #endif
969 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
970 int optname, char __user *optval, int len)
972 struct ipv6_opt_hdr *hdr;
974 if (!opt)
975 return 0;
977 switch (optname) {
978 case IPV6_HOPOPTS:
979 hdr = opt->hopopt;
980 break;
981 case IPV6_RTHDRDSTOPTS:
982 hdr = opt->dst0opt;
983 break;
984 case IPV6_RTHDR:
985 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
986 break;
987 case IPV6_DSTOPTS:
988 hdr = opt->dst1opt;
989 break;
990 default:
991 return -EINVAL; /* should not happen */
994 if (!hdr)
995 return 0;
997 len = min_t(unsigned int, len, ipv6_optlen(hdr));
998 if (copy_to_user(optval, hdr, len))
999 return -EFAULT;
1000 return len;
1003 static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
1004 char __user *optval, int __user *optlen, unsigned int flags)
1006 struct ipv6_pinfo *np = inet6_sk(sk);
1007 int len;
1008 int val;
1010 if (ip6_mroute_opt(optname))
1011 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
1013 if (get_user(len, optlen))
1014 return -EFAULT;
1015 switch (optname) {
1016 case IPV6_ADDRFORM:
1017 if (sk->sk_protocol != IPPROTO_UDP &&
1018 sk->sk_protocol != IPPROTO_UDPLITE &&
1019 sk->sk_protocol != IPPROTO_TCP)
1020 return -ENOPROTOOPT;
1021 if (sk->sk_state != TCP_ESTABLISHED)
1022 return -ENOTCONN;
1023 val = sk->sk_family;
1024 break;
1025 case MCAST_MSFILTER:
1027 struct group_filter gsf;
1028 int err;
1030 if (len < GROUP_FILTER_SIZE(0))
1031 return -EINVAL;
1032 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
1033 return -EFAULT;
1034 if (gsf.gf_group.ss_family != AF_INET6)
1035 return -EADDRNOTAVAIL;
1036 lock_sock(sk);
1037 err = ip6_mc_msfget(sk, &gsf,
1038 (struct group_filter __user *)optval, optlen);
1039 release_sock(sk);
1040 return err;
1043 case IPV6_2292PKTOPTIONS:
1045 struct msghdr msg;
1046 struct sk_buff *skb;
1048 if (sk->sk_type != SOCK_STREAM)
1049 return -ENOPROTOOPT;
1051 msg.msg_control = optval;
1052 msg.msg_controllen = len;
1053 msg.msg_flags = flags;
1055 lock_sock(sk);
1056 skb = np->pktoptions;
1057 if (skb)
1058 ip6_datagram_recv_ctl(sk, &msg, skb);
1059 release_sock(sk);
1060 if (!skb) {
1061 if (np->rxopt.bits.rxinfo) {
1062 struct in6_pktinfo src_info;
1063 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1064 np->sticky_pktinfo.ipi6_ifindex;
1065 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
1066 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1068 if (np->rxopt.bits.rxhlim) {
1069 int hlim = np->mcast_hops;
1070 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1072 if (np->rxopt.bits.rxtclass) {
1073 int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1075 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1077 if (np->rxopt.bits.rxoinfo) {
1078 struct in6_pktinfo src_info;
1079 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1080 np->sticky_pktinfo.ipi6_ifindex;
1081 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1082 np->sticky_pktinfo.ipi6_addr;
1083 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1085 if (np->rxopt.bits.rxohlim) {
1086 int hlim = np->mcast_hops;
1087 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1089 if (np->rxopt.bits.rxflow) {
1090 __be32 flowinfo = np->rcv_flowinfo;
1092 put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1095 len -= msg.msg_controllen;
1096 return put_user(len, optlen);
1098 case IPV6_MTU:
1100 struct dst_entry *dst;
1102 val = 0;
1103 rcu_read_lock();
1104 dst = __sk_dst_get(sk);
1105 if (dst)
1106 val = dst_mtu(dst);
1107 rcu_read_unlock();
1108 if (!val)
1109 return -ENOTCONN;
1110 break;
1113 case IPV6_V6ONLY:
1114 val = sk->sk_ipv6only;
1115 break;
1117 case IPV6_RECVPKTINFO:
1118 val = np->rxopt.bits.rxinfo;
1119 break;
1121 case IPV6_2292PKTINFO:
1122 val = np->rxopt.bits.rxoinfo;
1123 break;
1125 case IPV6_RECVHOPLIMIT:
1126 val = np->rxopt.bits.rxhlim;
1127 break;
1129 case IPV6_2292HOPLIMIT:
1130 val = np->rxopt.bits.rxohlim;
1131 break;
1133 case IPV6_RECVRTHDR:
1134 val = np->rxopt.bits.srcrt;
1135 break;
1137 case IPV6_2292RTHDR:
1138 val = np->rxopt.bits.osrcrt;
1139 break;
1141 case IPV6_HOPOPTS:
1142 case IPV6_RTHDRDSTOPTS:
1143 case IPV6_RTHDR:
1144 case IPV6_DSTOPTS:
1146 struct ipv6_txoptions *opt;
1148 lock_sock(sk);
1149 opt = rcu_dereference_protected(np->opt,
1150 lockdep_sock_is_held(sk));
1151 len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len);
1152 release_sock(sk);
1153 /* check if ipv6_getsockopt_sticky() returns err code */
1154 if (len < 0)
1155 return len;
1156 return put_user(len, optlen);
1159 case IPV6_RECVHOPOPTS:
1160 val = np->rxopt.bits.hopopts;
1161 break;
1163 case IPV6_2292HOPOPTS:
1164 val = np->rxopt.bits.ohopopts;
1165 break;
1167 case IPV6_RECVDSTOPTS:
1168 val = np->rxopt.bits.dstopts;
1169 break;
1171 case IPV6_2292DSTOPTS:
1172 val = np->rxopt.bits.odstopts;
1173 break;
1175 case IPV6_TCLASS:
1176 val = np->tclass;
1177 break;
1179 case IPV6_RECVTCLASS:
1180 val = np->rxopt.bits.rxtclass;
1181 break;
1183 case IPV6_FLOWINFO:
1184 val = np->rxopt.bits.rxflow;
1185 break;
1187 case IPV6_RECVPATHMTU:
1188 val = np->rxopt.bits.rxpmtu;
1189 break;
1191 case IPV6_PATHMTU:
1193 struct dst_entry *dst;
1194 struct ip6_mtuinfo mtuinfo;
1196 if (len < sizeof(mtuinfo))
1197 return -EINVAL;
1199 len = sizeof(mtuinfo);
1200 memset(&mtuinfo, 0, sizeof(mtuinfo));
1202 rcu_read_lock();
1203 dst = __sk_dst_get(sk);
1204 if (dst)
1205 mtuinfo.ip6m_mtu = dst_mtu(dst);
1206 rcu_read_unlock();
1207 if (!mtuinfo.ip6m_mtu)
1208 return -ENOTCONN;
1210 if (put_user(len, optlen))
1211 return -EFAULT;
1212 if (copy_to_user(optval, &mtuinfo, len))
1213 return -EFAULT;
1215 return 0;
1218 case IPV6_TRANSPARENT:
1219 val = inet_sk(sk)->transparent;
1220 break;
1222 case IPV6_RECVORIGDSTADDR:
1223 val = np->rxopt.bits.rxorigdstaddr;
1224 break;
1226 case IPV6_UNICAST_HOPS:
1227 case IPV6_MULTICAST_HOPS:
1229 struct dst_entry *dst;
1231 if (optname == IPV6_UNICAST_HOPS)
1232 val = np->hop_limit;
1233 else
1234 val = np->mcast_hops;
1236 if (val < 0) {
1237 rcu_read_lock();
1238 dst = __sk_dst_get(sk);
1239 if (dst)
1240 val = ip6_dst_hoplimit(dst);
1241 rcu_read_unlock();
1244 if (val < 0)
1245 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
1246 break;
1249 case IPV6_MULTICAST_LOOP:
1250 val = np->mc_loop;
1251 break;
1253 case IPV6_MULTICAST_IF:
1254 val = np->mcast_oif;
1255 break;
1257 case IPV6_UNICAST_IF:
1258 val = (__force int)htonl((__u32) np->ucast_oif);
1259 break;
1261 case IPV6_MTU_DISCOVER:
1262 val = np->pmtudisc;
1263 break;
1265 case IPV6_RECVERR:
1266 val = np->recverr;
1267 break;
1269 case IPV6_FLOWINFO_SEND:
1270 val = np->sndflow;
1271 break;
1273 case IPV6_FLOWLABEL_MGR:
1275 struct in6_flowlabel_req freq;
1276 int flags;
1278 if (len < sizeof(freq))
1279 return -EINVAL;
1281 if (copy_from_user(&freq, optval, sizeof(freq)))
1282 return -EFAULT;
1284 if (freq.flr_action != IPV6_FL_A_GET)
1285 return -EINVAL;
1287 len = sizeof(freq);
1288 flags = freq.flr_flags;
1290 memset(&freq, 0, sizeof(freq));
1292 val = ipv6_flowlabel_opt_get(sk, &freq, flags);
1293 if (val < 0)
1294 return val;
1296 if (put_user(len, optlen))
1297 return -EFAULT;
1298 if (copy_to_user(optval, &freq, len))
1299 return -EFAULT;
1301 return 0;
1304 case IPV6_ADDR_PREFERENCES:
1305 val = 0;
1307 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1308 val |= IPV6_PREFER_SRC_TMP;
1309 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1310 val |= IPV6_PREFER_SRC_PUBLIC;
1311 else {
1312 /* XXX: should we return system default? */
1313 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1316 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1317 val |= IPV6_PREFER_SRC_COA;
1318 else
1319 val |= IPV6_PREFER_SRC_HOME;
1320 break;
1322 case IPV6_MINHOPCOUNT:
1323 val = np->min_hopcount;
1324 break;
1326 case IPV6_DONTFRAG:
1327 val = np->dontfrag;
1328 break;
1330 case IPV6_AUTOFLOWLABEL:
1331 val = ip6_autoflowlabel(sock_net(sk), np);
1332 break;
1334 case IPV6_RECVFRAGSIZE:
1335 val = np->rxopt.bits.recvfragsize;
1336 break;
1338 default:
1339 return -ENOPROTOOPT;
1341 len = min_t(unsigned int, sizeof(int), len);
1342 if (put_user(len, optlen))
1343 return -EFAULT;
1344 if (copy_to_user(optval, &val, len))
1345 return -EFAULT;
1346 return 0;
1349 int ipv6_getsockopt(struct sock *sk, int level, int optname,
1350 char __user *optval, int __user *optlen)
1352 int err;
1354 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1355 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1357 if (level != SOL_IPV6)
1358 return -ENOPROTOOPT;
1360 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
1361 #ifdef CONFIG_NETFILTER
1362 /* we need to exclude all possible ENOPROTOOPTs except default case */
1363 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1364 int len;
1366 if (get_user(len, optlen))
1367 return -EFAULT;
1369 err = nf_getsockopt(sk, PF_INET6, optname, optval, &len);
1370 if (err >= 0)
1371 err = put_user(len, optlen);
1373 #endif
1374 return err;
1376 EXPORT_SYMBOL(ipv6_getsockopt);
1378 #ifdef CONFIG_COMPAT
1379 int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
1380 char __user *optval, int __user *optlen)
1382 int err;
1384 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
1385 if (udp_prot.compat_getsockopt != NULL)
1386 return udp_prot.compat_getsockopt(sk, level, optname,
1387 optval, optlen);
1388 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1391 if (level != SOL_IPV6)
1392 return -ENOPROTOOPT;
1394 if (optname == MCAST_MSFILTER)
1395 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1396 ipv6_getsockopt);
1398 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen,
1399 MSG_CMSG_COMPAT);
1400 #ifdef CONFIG_NETFILTER
1401 /* we need to exclude all possible ENOPROTOOPTs except default case */
1402 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1403 int len;
1405 if (get_user(len, optlen))
1406 return -EFAULT;
1408 err = compat_nf_getsockopt(sk, PF_INET6, optname, optval, &len);
1409 if (err >= 0)
1410 err = put_user(len, optlen);
1412 #endif
1413 return err;
1415 EXPORT_SYMBOL(compat_ipv6_getsockopt);
1416 #endif