powerpc/powernv: Report size of OPAL memcons log
[linux/fpc-iii.git] / net / ipv6 / ipv6_sockglue.c
blobee97c44e2aa0074df4bf22790aaf759e3a1390b6
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);
245 sk->sk_destruct = inet_sock_destruct;
247 * ... and add it to the refcnt debug socks count
248 * in the new family. -acme
250 sk_refcnt_debug_inc(sk);
251 module_put(THIS_MODULE);
252 retv = 0;
253 break;
255 goto e_inval;
257 case IPV6_V6ONLY:
258 if (optlen < sizeof(int) ||
259 inet_sk(sk)->inet_num)
260 goto e_inval;
261 sk->sk_ipv6only = valbool;
262 retv = 0;
263 break;
265 case IPV6_RECVPKTINFO:
266 if (optlen < sizeof(int))
267 goto e_inval;
268 np->rxopt.bits.rxinfo = valbool;
269 retv = 0;
270 break;
272 case IPV6_2292PKTINFO:
273 if (optlen < sizeof(int))
274 goto e_inval;
275 np->rxopt.bits.rxoinfo = valbool;
276 retv = 0;
277 break;
279 case IPV6_RECVHOPLIMIT:
280 if (optlen < sizeof(int))
281 goto e_inval;
282 np->rxopt.bits.rxhlim = valbool;
283 retv = 0;
284 break;
286 case IPV6_2292HOPLIMIT:
287 if (optlen < sizeof(int))
288 goto e_inval;
289 np->rxopt.bits.rxohlim = valbool;
290 retv = 0;
291 break;
293 case IPV6_RECVRTHDR:
294 if (optlen < sizeof(int))
295 goto e_inval;
296 np->rxopt.bits.srcrt = valbool;
297 retv = 0;
298 break;
300 case IPV6_2292RTHDR:
301 if (optlen < sizeof(int))
302 goto e_inval;
303 np->rxopt.bits.osrcrt = valbool;
304 retv = 0;
305 break;
307 case IPV6_RECVHOPOPTS:
308 if (optlen < sizeof(int))
309 goto e_inval;
310 np->rxopt.bits.hopopts = valbool;
311 retv = 0;
312 break;
314 case IPV6_2292HOPOPTS:
315 if (optlen < sizeof(int))
316 goto e_inval;
317 np->rxopt.bits.ohopopts = valbool;
318 retv = 0;
319 break;
321 case IPV6_RECVDSTOPTS:
322 if (optlen < sizeof(int))
323 goto e_inval;
324 np->rxopt.bits.dstopts = valbool;
325 retv = 0;
326 break;
328 case IPV6_2292DSTOPTS:
329 if (optlen < sizeof(int))
330 goto e_inval;
331 np->rxopt.bits.odstopts = valbool;
332 retv = 0;
333 break;
335 case IPV6_TCLASS:
336 if (optlen < sizeof(int))
337 goto e_inval;
338 if (val < -1 || val > 0xff)
339 goto e_inval;
340 /* RFC 3542, 6.5: default traffic class of 0x0 */
341 if (val == -1)
342 val = 0;
343 np->tclass = val;
344 retv = 0;
345 break;
347 case IPV6_RECVTCLASS:
348 if (optlen < sizeof(int))
349 goto e_inval;
350 np->rxopt.bits.rxtclass = valbool;
351 retv = 0;
352 break;
354 case IPV6_FLOWINFO:
355 if (optlen < sizeof(int))
356 goto e_inval;
357 np->rxopt.bits.rxflow = valbool;
358 retv = 0;
359 break;
361 case IPV6_RECVPATHMTU:
362 if (optlen < sizeof(int))
363 goto e_inval;
364 np->rxopt.bits.rxpmtu = valbool;
365 retv = 0;
366 break;
368 case IPV6_TRANSPARENT:
369 if (valbool && !ns_capable(net->user_ns, CAP_NET_ADMIN) &&
370 !ns_capable(net->user_ns, CAP_NET_RAW)) {
371 retv = -EPERM;
372 break;
374 if (optlen < sizeof(int))
375 goto e_inval;
376 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
377 inet_sk(sk)->transparent = valbool;
378 retv = 0;
379 break;
381 case IPV6_RECVORIGDSTADDR:
382 if (optlen < sizeof(int))
383 goto e_inval;
384 np->rxopt.bits.rxorigdstaddr = valbool;
385 retv = 0;
386 break;
388 case IPV6_HOPOPTS:
389 case IPV6_RTHDRDSTOPTS:
390 case IPV6_RTHDR:
391 case IPV6_DSTOPTS:
393 struct ipv6_txoptions *opt;
395 /* remove any sticky options header with a zero option
396 * length, per RFC3542.
398 if (optlen == 0)
399 optval = NULL;
400 else if (!optval)
401 goto e_inval;
402 else if (optlen < sizeof(struct ipv6_opt_hdr) ||
403 optlen & 0x7 || optlen > 8 * 255)
404 goto e_inval;
406 /* hop-by-hop / destination options are privileged option */
407 retv = -EPERM;
408 if (optname != IPV6_RTHDR && !ns_capable(net->user_ns, CAP_NET_RAW))
409 break;
411 opt = rcu_dereference_protected(np->opt,
412 lockdep_sock_is_held(sk));
413 opt = ipv6_renew_options(sk, opt, optname,
414 (struct ipv6_opt_hdr __user *)optval,
415 optlen);
416 if (IS_ERR(opt)) {
417 retv = PTR_ERR(opt);
418 break;
421 /* routing header option needs extra check */
422 retv = -EINVAL;
423 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
424 struct ipv6_rt_hdr *rthdr = opt->srcrt;
425 switch (rthdr->type) {
426 #if IS_ENABLED(CONFIG_IPV6_MIP6)
427 case IPV6_SRCRT_TYPE_2:
428 if (rthdr->hdrlen != 2 ||
429 rthdr->segments_left != 1)
430 goto sticky_done;
432 break;
433 #endif
434 case IPV6_SRCRT_TYPE_4:
436 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)
437 opt->srcrt;
439 if (!seg6_validate_srh(srh, optlen))
440 goto sticky_done;
441 break;
443 default:
444 goto sticky_done;
448 retv = 0;
449 opt = ipv6_update_options(sk, opt);
450 sticky_done:
451 if (opt) {
452 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
453 txopt_put(opt);
455 break;
458 case IPV6_PKTINFO:
460 struct in6_pktinfo pkt;
462 if (optlen == 0)
463 goto e_inval;
464 else if (optlen < sizeof(struct in6_pktinfo) || !optval)
465 goto e_inval;
467 if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) {
468 retv = -EFAULT;
469 break;
471 if (sk->sk_bound_dev_if && pkt.ipi6_ifindex != sk->sk_bound_dev_if)
472 goto e_inval;
474 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
475 np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
476 retv = 0;
477 break;
480 case IPV6_2292PKTOPTIONS:
482 struct ipv6_txoptions *opt = NULL;
483 struct msghdr msg;
484 struct flowi6 fl6;
485 struct sockcm_cookie sockc_junk;
486 struct ipcm6_cookie ipc6;
488 memset(&fl6, 0, sizeof(fl6));
489 fl6.flowi6_oif = sk->sk_bound_dev_if;
490 fl6.flowi6_mark = sk->sk_mark;
492 if (optlen == 0)
493 goto update;
495 /* 1K is probably excessive
496 * 1K is surely not enough, 2K per standard header is 16K.
498 retv = -EINVAL;
499 if (optlen > 64*1024)
500 break;
502 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
503 retv = -ENOBUFS;
504 if (!opt)
505 break;
507 memset(opt, 0, sizeof(*opt));
508 atomic_set(&opt->refcnt, 1);
509 opt->tot_len = sizeof(*opt) + optlen;
510 retv = -EFAULT;
511 if (copy_from_user(opt+1, optval, optlen))
512 goto done;
514 msg.msg_controllen = optlen;
515 msg.msg_control = (void *)(opt+1);
516 ipc6.opt = opt;
518 retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6, &sockc_junk);
519 if (retv)
520 goto done;
521 update:
522 retv = 0;
523 opt = ipv6_update_options(sk, opt);
524 done:
525 if (opt) {
526 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
527 txopt_put(opt);
529 break;
531 case IPV6_UNICAST_HOPS:
532 if (optlen < sizeof(int))
533 goto e_inval;
534 if (val > 255 || val < -1)
535 goto e_inval;
536 np->hop_limit = val;
537 retv = 0;
538 break;
540 case IPV6_MULTICAST_HOPS:
541 if (sk->sk_type == SOCK_STREAM)
542 break;
543 if (optlen < sizeof(int))
544 goto e_inval;
545 if (val > 255 || val < -1)
546 goto e_inval;
547 np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
548 retv = 0;
549 break;
551 case IPV6_MULTICAST_LOOP:
552 if (optlen < sizeof(int))
553 goto e_inval;
554 if (val != valbool)
555 goto e_inval;
556 np->mc_loop = valbool;
557 retv = 0;
558 break;
560 case IPV6_UNICAST_IF:
562 struct net_device *dev = NULL;
563 int ifindex;
565 if (optlen != sizeof(int))
566 goto e_inval;
568 ifindex = (__force int)ntohl((__force __be32)val);
569 if (ifindex == 0) {
570 np->ucast_oif = 0;
571 retv = 0;
572 break;
575 dev = dev_get_by_index(net, ifindex);
576 retv = -EADDRNOTAVAIL;
577 if (!dev)
578 break;
579 dev_put(dev);
581 retv = -EINVAL;
582 if (sk->sk_bound_dev_if)
583 break;
585 np->ucast_oif = ifindex;
586 retv = 0;
587 break;
590 case IPV6_MULTICAST_IF:
591 if (sk->sk_type == SOCK_STREAM)
592 break;
593 if (optlen < sizeof(int))
594 goto e_inval;
596 if (val) {
597 struct net_device *dev;
599 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
600 goto e_inval;
602 dev = dev_get_by_index(net, val);
603 if (!dev) {
604 retv = -ENODEV;
605 break;
607 dev_put(dev);
609 np->mcast_oif = val;
610 retv = 0;
611 break;
612 case IPV6_ADD_MEMBERSHIP:
613 case IPV6_DROP_MEMBERSHIP:
615 struct ipv6_mreq mreq;
617 if (optlen < sizeof(struct ipv6_mreq))
618 goto e_inval;
620 retv = -EPROTO;
621 if (inet_sk(sk)->is_icsk)
622 break;
624 retv = -EFAULT;
625 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
626 break;
628 if (optname == IPV6_ADD_MEMBERSHIP)
629 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
630 else
631 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
632 break;
634 case IPV6_JOIN_ANYCAST:
635 case IPV6_LEAVE_ANYCAST:
637 struct ipv6_mreq mreq;
639 if (optlen < sizeof(struct ipv6_mreq))
640 goto e_inval;
642 retv = -EFAULT;
643 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
644 break;
646 if (optname == IPV6_JOIN_ANYCAST)
647 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
648 else
649 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
650 break;
652 case MCAST_JOIN_GROUP:
653 case MCAST_LEAVE_GROUP:
655 struct group_req greq;
656 struct sockaddr_in6 *psin6;
658 if (optlen < sizeof(struct group_req))
659 goto e_inval;
661 retv = -EFAULT;
662 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
663 break;
664 if (greq.gr_group.ss_family != AF_INET6) {
665 retv = -EADDRNOTAVAIL;
666 break;
668 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
669 if (optname == MCAST_JOIN_GROUP)
670 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
671 &psin6->sin6_addr);
672 else
673 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
674 &psin6->sin6_addr);
675 break;
677 case MCAST_JOIN_SOURCE_GROUP:
678 case MCAST_LEAVE_SOURCE_GROUP:
679 case MCAST_BLOCK_SOURCE:
680 case MCAST_UNBLOCK_SOURCE:
682 struct group_source_req greqs;
683 int omode, add;
685 if (optlen < sizeof(struct group_source_req))
686 goto e_inval;
687 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
688 retv = -EFAULT;
689 break;
691 if (greqs.gsr_group.ss_family != AF_INET6 ||
692 greqs.gsr_source.ss_family != AF_INET6) {
693 retv = -EADDRNOTAVAIL;
694 break;
696 if (optname == MCAST_BLOCK_SOURCE) {
697 omode = MCAST_EXCLUDE;
698 add = 1;
699 } else if (optname == MCAST_UNBLOCK_SOURCE) {
700 omode = MCAST_EXCLUDE;
701 add = 0;
702 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
703 struct sockaddr_in6 *psin6;
705 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
706 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
707 &psin6->sin6_addr);
708 /* prior join w/ different source is ok */
709 if (retv && retv != -EADDRINUSE)
710 break;
711 omode = MCAST_INCLUDE;
712 add = 1;
713 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
714 omode = MCAST_INCLUDE;
715 add = 0;
717 retv = ip6_mc_source(add, omode, sk, &greqs);
718 break;
720 case MCAST_MSFILTER:
722 struct group_filter *gsf;
724 if (optlen < GROUP_FILTER_SIZE(0))
725 goto e_inval;
726 if (optlen > sysctl_optmem_max) {
727 retv = -ENOBUFS;
728 break;
730 gsf = kmalloc(optlen, GFP_KERNEL);
731 if (!gsf) {
732 retv = -ENOBUFS;
733 break;
735 retv = -EFAULT;
736 if (copy_from_user(gsf, optval, optlen)) {
737 kfree(gsf);
738 break;
740 /* numsrc >= (4G-140)/128 overflow in 32 bits */
741 if (gsf->gf_numsrc >= 0x1ffffffU ||
742 gsf->gf_numsrc > sysctl_mld_max_msf) {
743 kfree(gsf);
744 retv = -ENOBUFS;
745 break;
747 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
748 kfree(gsf);
749 retv = -EINVAL;
750 break;
752 retv = ip6_mc_msfilter(sk, gsf);
753 kfree(gsf);
755 break;
757 case IPV6_ROUTER_ALERT:
758 if (optlen < sizeof(int))
759 goto e_inval;
760 retv = ip6_ra_control(sk, val);
761 break;
762 case IPV6_MTU_DISCOVER:
763 if (optlen < sizeof(int))
764 goto e_inval;
765 if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
766 goto e_inval;
767 np->pmtudisc = val;
768 retv = 0;
769 break;
770 case IPV6_MTU:
771 if (optlen < sizeof(int))
772 goto e_inval;
773 if (val && val < IPV6_MIN_MTU)
774 goto e_inval;
775 np->frag_size = val;
776 retv = 0;
777 break;
778 case IPV6_RECVERR:
779 if (optlen < sizeof(int))
780 goto e_inval;
781 np->recverr = valbool;
782 if (!val)
783 skb_queue_purge(&sk->sk_error_queue);
784 retv = 0;
785 break;
786 case IPV6_FLOWINFO_SEND:
787 if (optlen < sizeof(int))
788 goto e_inval;
789 np->sndflow = valbool;
790 retv = 0;
791 break;
792 case IPV6_FLOWLABEL_MGR:
793 retv = ipv6_flowlabel_opt(sk, optval, optlen);
794 break;
795 case IPV6_IPSEC_POLICY:
796 case IPV6_XFRM_POLICY:
797 retv = -EPERM;
798 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
799 break;
800 retv = xfrm_user_policy(sk, optname, optval, optlen);
801 break;
803 case IPV6_ADDR_PREFERENCES:
805 unsigned int pref = 0;
806 unsigned int prefmask = ~0;
808 if (optlen < sizeof(int))
809 goto e_inval;
811 retv = -EINVAL;
813 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
814 switch (val & (IPV6_PREFER_SRC_PUBLIC|
815 IPV6_PREFER_SRC_TMP|
816 IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
817 case IPV6_PREFER_SRC_PUBLIC:
818 pref |= IPV6_PREFER_SRC_PUBLIC;
819 break;
820 case IPV6_PREFER_SRC_TMP:
821 pref |= IPV6_PREFER_SRC_TMP;
822 break;
823 case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
824 break;
825 case 0:
826 goto pref_skip_pubtmp;
827 default:
828 goto e_inval;
831 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
832 IPV6_PREFER_SRC_TMP);
833 pref_skip_pubtmp:
835 /* check HOME/COA conflicts */
836 switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
837 case IPV6_PREFER_SRC_HOME:
838 break;
839 case IPV6_PREFER_SRC_COA:
840 pref |= IPV6_PREFER_SRC_COA;
841 case 0:
842 goto pref_skip_coa;
843 default:
844 goto e_inval;
847 prefmask &= ~IPV6_PREFER_SRC_COA;
848 pref_skip_coa:
850 /* check CGA/NONCGA conflicts */
851 switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
852 case IPV6_PREFER_SRC_CGA:
853 case IPV6_PREFER_SRC_NONCGA:
854 case 0:
855 break;
856 default:
857 goto e_inval;
860 np->srcprefs = (np->srcprefs & prefmask) | pref;
861 retv = 0;
863 break;
865 case IPV6_MINHOPCOUNT:
866 if (optlen < sizeof(int))
867 goto e_inval;
868 if (val < 0 || val > 255)
869 goto e_inval;
870 np->min_hopcount = val;
871 retv = 0;
872 break;
873 case IPV6_DONTFRAG:
874 np->dontfrag = valbool;
875 retv = 0;
876 break;
877 case IPV6_AUTOFLOWLABEL:
878 np->autoflowlabel = valbool;
879 retv = 0;
880 break;
881 case IPV6_RECVFRAGSIZE:
882 np->rxopt.bits.recvfragsize = valbool;
883 retv = 0;
884 break;
887 release_sock(sk);
888 if (needs_rtnl)
889 rtnl_unlock();
891 return retv;
893 e_inval:
894 release_sock(sk);
895 if (needs_rtnl)
896 rtnl_unlock();
897 return -EINVAL;
900 int ipv6_setsockopt(struct sock *sk, int level, int optname,
901 char __user *optval, unsigned int optlen)
903 int err;
905 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
906 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
908 if (level != SOL_IPV6)
909 return -ENOPROTOOPT;
911 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
912 #ifdef CONFIG_NETFILTER
913 /* we need to exclude all possible ENOPROTOOPTs except default case */
914 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
915 optname != IPV6_XFRM_POLICY) {
916 lock_sock(sk);
917 err = nf_setsockopt(sk, PF_INET6, optname, optval,
918 optlen);
919 release_sock(sk);
921 #endif
922 return err;
924 EXPORT_SYMBOL(ipv6_setsockopt);
926 #ifdef CONFIG_COMPAT
927 int compat_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 if (udp_prot.compat_setsockopt != NULL)
934 return udp_prot.compat_setsockopt(sk, level, optname,
935 optval, optlen);
936 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
939 if (level != SOL_IPV6)
940 return -ENOPROTOOPT;
942 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
943 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
944 ipv6_setsockopt);
946 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
947 #ifdef CONFIG_NETFILTER
948 /* we need to exclude all possible ENOPROTOOPTs except default case */
949 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
950 optname != IPV6_XFRM_POLICY) {
951 lock_sock(sk);
952 err = compat_nf_setsockopt(sk, PF_INET6, optname,
953 optval, optlen);
954 release_sock(sk);
956 #endif
957 return err;
959 EXPORT_SYMBOL(compat_ipv6_setsockopt);
960 #endif
962 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
963 int optname, char __user *optval, int len)
965 struct ipv6_opt_hdr *hdr;
967 if (!opt)
968 return 0;
970 switch (optname) {
971 case IPV6_HOPOPTS:
972 hdr = opt->hopopt;
973 break;
974 case IPV6_RTHDRDSTOPTS:
975 hdr = opt->dst0opt;
976 break;
977 case IPV6_RTHDR:
978 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
979 break;
980 case IPV6_DSTOPTS:
981 hdr = opt->dst1opt;
982 break;
983 default:
984 return -EINVAL; /* should not happen */
987 if (!hdr)
988 return 0;
990 len = min_t(unsigned int, len, ipv6_optlen(hdr));
991 if (copy_to_user(optval, hdr, len))
992 return -EFAULT;
993 return len;
996 static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
997 char __user *optval, int __user *optlen, unsigned int flags)
999 struct ipv6_pinfo *np = inet6_sk(sk);
1000 int len;
1001 int val;
1003 if (ip6_mroute_opt(optname))
1004 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
1006 if (get_user(len, optlen))
1007 return -EFAULT;
1008 switch (optname) {
1009 case IPV6_ADDRFORM:
1010 if (sk->sk_protocol != IPPROTO_UDP &&
1011 sk->sk_protocol != IPPROTO_UDPLITE &&
1012 sk->sk_protocol != IPPROTO_TCP)
1013 return -ENOPROTOOPT;
1014 if (sk->sk_state != TCP_ESTABLISHED)
1015 return -ENOTCONN;
1016 val = sk->sk_family;
1017 break;
1018 case MCAST_MSFILTER:
1020 struct group_filter gsf;
1021 int err;
1023 if (len < GROUP_FILTER_SIZE(0))
1024 return -EINVAL;
1025 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
1026 return -EFAULT;
1027 if (gsf.gf_group.ss_family != AF_INET6)
1028 return -EADDRNOTAVAIL;
1029 lock_sock(sk);
1030 err = ip6_mc_msfget(sk, &gsf,
1031 (struct group_filter __user *)optval, optlen);
1032 release_sock(sk);
1033 return err;
1036 case IPV6_2292PKTOPTIONS:
1038 struct msghdr msg;
1039 struct sk_buff *skb;
1041 if (sk->sk_type != SOCK_STREAM)
1042 return -ENOPROTOOPT;
1044 msg.msg_control = optval;
1045 msg.msg_controllen = len;
1046 msg.msg_flags = flags;
1048 lock_sock(sk);
1049 skb = np->pktoptions;
1050 if (skb)
1051 ip6_datagram_recv_ctl(sk, &msg, skb);
1052 release_sock(sk);
1053 if (!skb) {
1054 if (np->rxopt.bits.rxinfo) {
1055 struct in6_pktinfo src_info;
1056 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1057 np->sticky_pktinfo.ipi6_ifindex;
1058 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
1059 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1061 if (np->rxopt.bits.rxhlim) {
1062 int hlim = np->mcast_hops;
1063 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1065 if (np->rxopt.bits.rxtclass) {
1066 int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1068 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1070 if (np->rxopt.bits.rxoinfo) {
1071 struct in6_pktinfo src_info;
1072 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1073 np->sticky_pktinfo.ipi6_ifindex;
1074 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1075 np->sticky_pktinfo.ipi6_addr;
1076 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1078 if (np->rxopt.bits.rxohlim) {
1079 int hlim = np->mcast_hops;
1080 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1082 if (np->rxopt.bits.rxflow) {
1083 __be32 flowinfo = np->rcv_flowinfo;
1085 put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1088 len -= msg.msg_controllen;
1089 return put_user(len, optlen);
1091 case IPV6_MTU:
1093 struct dst_entry *dst;
1095 val = 0;
1096 rcu_read_lock();
1097 dst = __sk_dst_get(sk);
1098 if (dst)
1099 val = dst_mtu(dst);
1100 rcu_read_unlock();
1101 if (!val)
1102 return -ENOTCONN;
1103 break;
1106 case IPV6_V6ONLY:
1107 val = sk->sk_ipv6only;
1108 break;
1110 case IPV6_RECVPKTINFO:
1111 val = np->rxopt.bits.rxinfo;
1112 break;
1114 case IPV6_2292PKTINFO:
1115 val = np->rxopt.bits.rxoinfo;
1116 break;
1118 case IPV6_RECVHOPLIMIT:
1119 val = np->rxopt.bits.rxhlim;
1120 break;
1122 case IPV6_2292HOPLIMIT:
1123 val = np->rxopt.bits.rxohlim;
1124 break;
1126 case IPV6_RECVRTHDR:
1127 val = np->rxopt.bits.srcrt;
1128 break;
1130 case IPV6_2292RTHDR:
1131 val = np->rxopt.bits.osrcrt;
1132 break;
1134 case IPV6_HOPOPTS:
1135 case IPV6_RTHDRDSTOPTS:
1136 case IPV6_RTHDR:
1137 case IPV6_DSTOPTS:
1139 struct ipv6_txoptions *opt;
1141 lock_sock(sk);
1142 opt = rcu_dereference_protected(np->opt,
1143 lockdep_sock_is_held(sk));
1144 len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len);
1145 release_sock(sk);
1146 /* check if ipv6_getsockopt_sticky() returns err code */
1147 if (len < 0)
1148 return len;
1149 return put_user(len, optlen);
1152 case IPV6_RECVHOPOPTS:
1153 val = np->rxopt.bits.hopopts;
1154 break;
1156 case IPV6_2292HOPOPTS:
1157 val = np->rxopt.bits.ohopopts;
1158 break;
1160 case IPV6_RECVDSTOPTS:
1161 val = np->rxopt.bits.dstopts;
1162 break;
1164 case IPV6_2292DSTOPTS:
1165 val = np->rxopt.bits.odstopts;
1166 break;
1168 case IPV6_TCLASS:
1169 val = np->tclass;
1170 break;
1172 case IPV6_RECVTCLASS:
1173 val = np->rxopt.bits.rxtclass;
1174 break;
1176 case IPV6_FLOWINFO:
1177 val = np->rxopt.bits.rxflow;
1178 break;
1180 case IPV6_RECVPATHMTU:
1181 val = np->rxopt.bits.rxpmtu;
1182 break;
1184 case IPV6_PATHMTU:
1186 struct dst_entry *dst;
1187 struct ip6_mtuinfo mtuinfo;
1189 if (len < sizeof(mtuinfo))
1190 return -EINVAL;
1192 len = sizeof(mtuinfo);
1193 memset(&mtuinfo, 0, sizeof(mtuinfo));
1195 rcu_read_lock();
1196 dst = __sk_dst_get(sk);
1197 if (dst)
1198 mtuinfo.ip6m_mtu = dst_mtu(dst);
1199 rcu_read_unlock();
1200 if (!mtuinfo.ip6m_mtu)
1201 return -ENOTCONN;
1203 if (put_user(len, optlen))
1204 return -EFAULT;
1205 if (copy_to_user(optval, &mtuinfo, len))
1206 return -EFAULT;
1208 return 0;
1211 case IPV6_TRANSPARENT:
1212 val = inet_sk(sk)->transparent;
1213 break;
1215 case IPV6_RECVORIGDSTADDR:
1216 val = np->rxopt.bits.rxorigdstaddr;
1217 break;
1219 case IPV6_UNICAST_HOPS:
1220 case IPV6_MULTICAST_HOPS:
1222 struct dst_entry *dst;
1224 if (optname == IPV6_UNICAST_HOPS)
1225 val = np->hop_limit;
1226 else
1227 val = np->mcast_hops;
1229 if (val < 0) {
1230 rcu_read_lock();
1231 dst = __sk_dst_get(sk);
1232 if (dst)
1233 val = ip6_dst_hoplimit(dst);
1234 rcu_read_unlock();
1237 if (val < 0)
1238 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
1239 break;
1242 case IPV6_MULTICAST_LOOP:
1243 val = np->mc_loop;
1244 break;
1246 case IPV6_MULTICAST_IF:
1247 val = np->mcast_oif;
1248 break;
1250 case IPV6_UNICAST_IF:
1251 val = (__force int)htonl((__u32) np->ucast_oif);
1252 break;
1254 case IPV6_MTU_DISCOVER:
1255 val = np->pmtudisc;
1256 break;
1258 case IPV6_RECVERR:
1259 val = np->recverr;
1260 break;
1262 case IPV6_FLOWINFO_SEND:
1263 val = np->sndflow;
1264 break;
1266 case IPV6_FLOWLABEL_MGR:
1268 struct in6_flowlabel_req freq;
1269 int flags;
1271 if (len < sizeof(freq))
1272 return -EINVAL;
1274 if (copy_from_user(&freq, optval, sizeof(freq)))
1275 return -EFAULT;
1277 if (freq.flr_action != IPV6_FL_A_GET)
1278 return -EINVAL;
1280 len = sizeof(freq);
1281 flags = freq.flr_flags;
1283 memset(&freq, 0, sizeof(freq));
1285 val = ipv6_flowlabel_opt_get(sk, &freq, flags);
1286 if (val < 0)
1287 return val;
1289 if (put_user(len, optlen))
1290 return -EFAULT;
1291 if (copy_to_user(optval, &freq, len))
1292 return -EFAULT;
1294 return 0;
1297 case IPV6_ADDR_PREFERENCES:
1298 val = 0;
1300 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1301 val |= IPV6_PREFER_SRC_TMP;
1302 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1303 val |= IPV6_PREFER_SRC_PUBLIC;
1304 else {
1305 /* XXX: should we return system default? */
1306 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1309 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1310 val |= IPV6_PREFER_SRC_COA;
1311 else
1312 val |= IPV6_PREFER_SRC_HOME;
1313 break;
1315 case IPV6_MINHOPCOUNT:
1316 val = np->min_hopcount;
1317 break;
1319 case IPV6_DONTFRAG:
1320 val = np->dontfrag;
1321 break;
1323 case IPV6_AUTOFLOWLABEL:
1324 val = np->autoflowlabel;
1325 break;
1327 case IPV6_RECVFRAGSIZE:
1328 val = np->rxopt.bits.recvfragsize;
1329 break;
1331 default:
1332 return -ENOPROTOOPT;
1334 len = min_t(unsigned int, sizeof(int), len);
1335 if (put_user(len, optlen))
1336 return -EFAULT;
1337 if (copy_to_user(optval, &val, len))
1338 return -EFAULT;
1339 return 0;
1342 int ipv6_getsockopt(struct sock *sk, int level, int optname,
1343 char __user *optval, int __user *optlen)
1345 int err;
1347 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1348 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1350 if (level != SOL_IPV6)
1351 return -ENOPROTOOPT;
1353 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
1354 #ifdef CONFIG_NETFILTER
1355 /* we need to exclude all possible ENOPROTOOPTs except default case */
1356 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1357 int len;
1359 if (get_user(len, optlen))
1360 return -EFAULT;
1362 lock_sock(sk);
1363 err = nf_getsockopt(sk, PF_INET6, optname, optval,
1364 &len);
1365 release_sock(sk);
1366 if (err >= 0)
1367 err = put_user(len, optlen);
1369 #endif
1370 return err;
1372 EXPORT_SYMBOL(ipv6_getsockopt);
1374 #ifdef CONFIG_COMPAT
1375 int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
1376 char __user *optval, int __user *optlen)
1378 int err;
1380 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
1381 if (udp_prot.compat_getsockopt != NULL)
1382 return udp_prot.compat_getsockopt(sk, level, optname,
1383 optval, optlen);
1384 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1387 if (level != SOL_IPV6)
1388 return -ENOPROTOOPT;
1390 if (optname == MCAST_MSFILTER)
1391 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1392 ipv6_getsockopt);
1394 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen,
1395 MSG_CMSG_COMPAT);
1396 #ifdef CONFIG_NETFILTER
1397 /* we need to exclude all possible ENOPROTOOPTs except default case */
1398 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1399 int len;
1401 if (get_user(len, optlen))
1402 return -EFAULT;
1404 lock_sock(sk);
1405 err = compat_nf_getsockopt(sk, PF_INET6,
1406 optname, optval, &len);
1407 release_sock(sk);
1408 if (err >= 0)
1409 err = put_user(len, optlen);
1411 #endif
1412 return err;
1414 EXPORT_SYMBOL(compat_ipv6_getsockopt);
1415 #endif