Linux 2.6.34.3
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv6 / ipv6_sockglue.c
blob33f60fca7aa775e36082c65171a10e10a4ec49bb
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 DEFINE_SNMP_STAT(struct ipstats_mib, ipv6_statistics) __read_mostly;
60 struct ip6_ra_chain *ip6_ra_chain;
61 DEFINE_RWLOCK(ip6_ra_lock);
63 int ip6_ra_control(struct sock *sk, int sel)
65 struct ip6_ra_chain *ra, *new_ra, **rap;
67 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
68 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num != IPPROTO_RAW)
69 return -ENOPROTOOPT;
71 new_ra = (sel>=0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
73 write_lock_bh(&ip6_ra_lock);
74 for (rap = &ip6_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
75 if (ra->sk == sk) {
76 if (sel>=0) {
77 write_unlock_bh(&ip6_ra_lock);
78 kfree(new_ra);
79 return -EADDRINUSE;
82 *rap = ra->next;
83 write_unlock_bh(&ip6_ra_lock);
85 sock_put(sk);
86 kfree(ra);
87 return 0;
90 if (new_ra == NULL) {
91 write_unlock_bh(&ip6_ra_lock);
92 return -ENOBUFS;
94 new_ra->sk = sk;
95 new_ra->sel = sel;
96 new_ra->next = ra;
97 *rap = new_ra;
98 sock_hold(sk);
99 write_unlock_bh(&ip6_ra_lock);
100 return 0;
103 static
104 struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
105 struct ipv6_txoptions *opt)
107 if (inet_sk(sk)->is_icsk) {
108 if (opt &&
109 !((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
110 inet_sk(sk)->inet_daddr != LOOPBACK4_IPV6) {
111 struct inet_connection_sock *icsk = inet_csk(sk);
112 icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen;
113 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
115 opt = xchg(&inet6_sk(sk)->opt, opt);
116 } else {
117 write_lock(&sk->sk_dst_lock);
118 opt = xchg(&inet6_sk(sk)->opt, opt);
119 write_unlock(&sk->sk_dst_lock);
121 sk_dst_reset(sk);
123 return opt;
126 static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
127 char __user *optval, unsigned int optlen)
129 struct ipv6_pinfo *np = inet6_sk(sk);
130 struct net *net = sock_net(sk);
131 int val, valbool;
132 int retv = -ENOPROTOOPT;
134 if (optval == NULL)
135 val=0;
136 else {
137 if (optlen >= sizeof(int)) {
138 if (get_user(val, (int __user *) optval))
139 return -EFAULT;
140 } else
141 val = 0;
144 valbool = (val!=0);
146 if (ip6_mroute_opt(optname))
147 return ip6_mroute_setsockopt(sk, optname, optval, optlen);
149 lock_sock(sk);
151 switch (optname) {
153 case IPV6_ADDRFORM:
154 if (optlen < sizeof(int))
155 goto e_inval;
156 if (val == PF_INET) {
157 struct ipv6_txoptions *opt;
158 struct sk_buff *pktopt;
160 if (sk->sk_type == SOCK_RAW)
161 break;
163 if (sk->sk_protocol == IPPROTO_UDP ||
164 sk->sk_protocol == IPPROTO_UDPLITE) {
165 struct udp_sock *up = udp_sk(sk);
166 if (up->pending == AF_INET6) {
167 retv = -EBUSY;
168 break;
170 } else if (sk->sk_protocol != IPPROTO_TCP)
171 break;
173 if (sk->sk_state != TCP_ESTABLISHED) {
174 retv = -ENOTCONN;
175 break;
178 if (ipv6_only_sock(sk) ||
179 !ipv6_addr_v4mapped(&np->daddr)) {
180 retv = -EADDRNOTAVAIL;
181 break;
184 fl6_free_socklist(sk);
185 ipv6_sock_mc_close(sk);
188 * Sock is moving from IPv6 to IPv4 (sk_prot), so
189 * remove it from the refcnt debug socks count in the
190 * original family...
192 sk_refcnt_debug_dec(sk);
194 if (sk->sk_protocol == IPPROTO_TCP) {
195 struct inet_connection_sock *icsk = inet_csk(sk);
196 local_bh_disable();
197 sock_prot_inuse_add(net, sk->sk_prot, -1);
198 sock_prot_inuse_add(net, &tcp_prot, 1);
199 local_bh_enable();
200 sk->sk_prot = &tcp_prot;
201 icsk->icsk_af_ops = &ipv4_specific;
202 sk->sk_socket->ops = &inet_stream_ops;
203 sk->sk_family = PF_INET;
204 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
205 } else {
206 struct proto *prot = &udp_prot;
208 if (sk->sk_protocol == IPPROTO_UDPLITE)
209 prot = &udplite_prot;
210 local_bh_disable();
211 sock_prot_inuse_add(net, sk->sk_prot, -1);
212 sock_prot_inuse_add(net, prot, 1);
213 local_bh_enable();
214 sk->sk_prot = prot;
215 sk->sk_socket->ops = &inet_dgram_ops;
216 sk->sk_family = PF_INET;
218 opt = xchg(&np->opt, NULL);
219 if (opt)
220 sock_kfree_s(sk, opt, opt->tot_len);
221 pktopt = xchg(&np->pktoptions, NULL);
222 kfree_skb(pktopt);
224 sk->sk_destruct = inet_sock_destruct;
226 * ... and add it to the refcnt debug socks count
227 * in the new family. -acme
229 sk_refcnt_debug_inc(sk);
230 module_put(THIS_MODULE);
231 retv = 0;
232 break;
234 goto e_inval;
236 case IPV6_V6ONLY:
237 if (optlen < sizeof(int) ||
238 inet_sk(sk)->inet_num)
239 goto e_inval;
240 np->ipv6only = valbool;
241 retv = 0;
242 break;
244 case IPV6_RECVPKTINFO:
245 if (optlen < sizeof(int))
246 goto e_inval;
247 np->rxopt.bits.rxinfo = valbool;
248 retv = 0;
249 break;
251 case IPV6_2292PKTINFO:
252 if (optlen < sizeof(int))
253 goto e_inval;
254 np->rxopt.bits.rxoinfo = valbool;
255 retv = 0;
256 break;
258 case IPV6_RECVHOPLIMIT:
259 if (optlen < sizeof(int))
260 goto e_inval;
261 np->rxopt.bits.rxhlim = valbool;
262 retv = 0;
263 break;
265 case IPV6_2292HOPLIMIT:
266 if (optlen < sizeof(int))
267 goto e_inval;
268 np->rxopt.bits.rxohlim = valbool;
269 retv = 0;
270 break;
272 case IPV6_RECVRTHDR:
273 if (optlen < sizeof(int))
274 goto e_inval;
275 np->rxopt.bits.srcrt = valbool;
276 retv = 0;
277 break;
279 case IPV6_2292RTHDR:
280 if (optlen < sizeof(int))
281 goto e_inval;
282 np->rxopt.bits.osrcrt = valbool;
283 retv = 0;
284 break;
286 case IPV6_RECVHOPOPTS:
287 if (optlen < sizeof(int))
288 goto e_inval;
289 np->rxopt.bits.hopopts = valbool;
290 retv = 0;
291 break;
293 case IPV6_2292HOPOPTS:
294 if (optlen < sizeof(int))
295 goto e_inval;
296 np->rxopt.bits.ohopopts = valbool;
297 retv = 0;
298 break;
300 case IPV6_RECVDSTOPTS:
301 if (optlen < sizeof(int))
302 goto e_inval;
303 np->rxopt.bits.dstopts = valbool;
304 retv = 0;
305 break;
307 case IPV6_2292DSTOPTS:
308 if (optlen < sizeof(int))
309 goto e_inval;
310 np->rxopt.bits.odstopts = valbool;
311 retv = 0;
312 break;
314 case IPV6_TCLASS:
315 if (optlen < sizeof(int))
316 goto e_inval;
317 if (val < -1 || val > 0xff)
318 goto e_inval;
319 /* RFC 3542, 6.5: default traffic class of 0x0 */
320 if (val == -1)
321 val = 0;
322 np->tclass = val;
323 retv = 0;
324 break;
326 case IPV6_RECVTCLASS:
327 if (optlen < sizeof(int))
328 goto e_inval;
329 np->rxopt.bits.rxtclass = valbool;
330 retv = 0;
331 break;
333 case IPV6_FLOWINFO:
334 if (optlen < sizeof(int))
335 goto e_inval;
336 np->rxopt.bits.rxflow = valbool;
337 retv = 0;
338 break;
340 case IPV6_HOPOPTS:
341 case IPV6_RTHDRDSTOPTS:
342 case IPV6_RTHDR:
343 case IPV6_DSTOPTS:
345 struct ipv6_txoptions *opt;
347 /* remove any sticky options header with a zero option
348 * length, per RFC3542.
350 if (optlen == 0)
351 optval = NULL;
352 else if (optval == NULL)
353 goto e_inval;
354 else if (optlen < sizeof(struct ipv6_opt_hdr) ||
355 optlen & 0x7 || optlen > 8 * 255)
356 goto e_inval;
358 /* hop-by-hop / destination options are privileged option */
359 retv = -EPERM;
360 if (optname != IPV6_RTHDR && !capable(CAP_NET_RAW))
361 break;
363 opt = ipv6_renew_options(sk, np->opt, optname,
364 (struct ipv6_opt_hdr __user *)optval,
365 optlen);
366 if (IS_ERR(opt)) {
367 retv = PTR_ERR(opt);
368 break;
371 /* routing header option needs extra check */
372 retv = -EINVAL;
373 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
374 struct ipv6_rt_hdr *rthdr = opt->srcrt;
375 switch (rthdr->type) {
376 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
377 case IPV6_SRCRT_TYPE_2:
378 if (rthdr->hdrlen != 2 ||
379 rthdr->segments_left != 1)
380 goto sticky_done;
382 break;
383 #endif
384 default:
385 goto sticky_done;
389 retv = 0;
390 opt = ipv6_update_options(sk, opt);
391 sticky_done:
392 if (opt)
393 sock_kfree_s(sk, opt, opt->tot_len);
394 break;
397 case IPV6_PKTINFO:
399 struct in6_pktinfo pkt;
401 if (optlen == 0)
402 goto e_inval;
403 else if (optlen < sizeof(struct in6_pktinfo) || optval == NULL)
404 goto e_inval;
406 if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) {
407 retv = -EFAULT;
408 break;
410 if (sk->sk_bound_dev_if && pkt.ipi6_ifindex != sk->sk_bound_dev_if)
411 goto e_inval;
413 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
414 ipv6_addr_copy(&np->sticky_pktinfo.ipi6_addr, &pkt.ipi6_addr);
415 retv = 0;
416 break;
419 case IPV6_2292PKTOPTIONS:
421 struct ipv6_txoptions *opt = NULL;
422 struct msghdr msg;
423 struct flowi fl;
424 int junk;
426 fl.fl6_flowlabel = 0;
427 fl.oif = sk->sk_bound_dev_if;
428 fl.mark = sk->sk_mark;
430 if (optlen == 0)
431 goto update;
433 /* 1K is probably excessive
434 * 1K is surely not enough, 2K per standard header is 16K.
436 retv = -EINVAL;
437 if (optlen > 64*1024)
438 break;
440 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
441 retv = -ENOBUFS;
442 if (opt == NULL)
443 break;
445 memset(opt, 0, sizeof(*opt));
446 opt->tot_len = sizeof(*opt) + optlen;
447 retv = -EFAULT;
448 if (copy_from_user(opt+1, optval, optlen))
449 goto done;
451 msg.msg_controllen = optlen;
452 msg.msg_control = (void*)(opt+1);
454 retv = datagram_send_ctl(net, &msg, &fl, opt, &junk, &junk);
455 if (retv)
456 goto done;
457 update:
458 retv = 0;
459 opt = ipv6_update_options(sk, opt);
460 done:
461 if (opt)
462 sock_kfree_s(sk, opt, opt->tot_len);
463 break;
465 case IPV6_UNICAST_HOPS:
466 if (optlen < sizeof(int))
467 goto e_inval;
468 if (val > 255 || val < -1)
469 goto e_inval;
470 np->hop_limit = val;
471 retv = 0;
472 break;
474 case IPV6_MULTICAST_HOPS:
475 if (sk->sk_type == SOCK_STREAM)
476 break;
477 if (optlen < sizeof(int))
478 goto e_inval;
479 if (val > 255 || val < -1)
480 goto e_inval;
481 np->mcast_hops = val;
482 retv = 0;
483 break;
485 case IPV6_MULTICAST_LOOP:
486 if (optlen < sizeof(int))
487 goto e_inval;
488 if (val != valbool)
489 goto e_inval;
490 np->mc_loop = valbool;
491 retv = 0;
492 break;
494 case IPV6_MULTICAST_IF:
495 if (sk->sk_type == SOCK_STREAM)
496 break;
497 if (optlen < sizeof(int))
498 goto e_inval;
500 if (val) {
501 struct net_device *dev;
503 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
504 goto e_inval;
506 dev = dev_get_by_index(net, val);
507 if (!dev) {
508 retv = -ENODEV;
509 break;
511 dev_put(dev);
513 np->mcast_oif = val;
514 retv = 0;
515 break;
516 case IPV6_ADD_MEMBERSHIP:
517 case IPV6_DROP_MEMBERSHIP:
519 struct ipv6_mreq mreq;
521 if (optlen < sizeof(struct ipv6_mreq))
522 goto e_inval;
524 retv = -EPROTO;
525 if (inet_sk(sk)->is_icsk)
526 break;
528 retv = -EFAULT;
529 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
530 break;
532 if (optname == IPV6_ADD_MEMBERSHIP)
533 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
534 else
535 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
536 break;
538 case IPV6_JOIN_ANYCAST:
539 case IPV6_LEAVE_ANYCAST:
541 struct ipv6_mreq mreq;
543 if (optlen < sizeof(struct ipv6_mreq))
544 goto e_inval;
546 retv = -EFAULT;
547 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
548 break;
550 if (optname == IPV6_JOIN_ANYCAST)
551 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
552 else
553 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
554 break;
556 case MCAST_JOIN_GROUP:
557 case MCAST_LEAVE_GROUP:
559 struct group_req greq;
560 struct sockaddr_in6 *psin6;
562 if (optlen < sizeof(struct group_req))
563 goto e_inval;
565 retv = -EFAULT;
566 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
567 break;
568 if (greq.gr_group.ss_family != AF_INET6) {
569 retv = -EADDRNOTAVAIL;
570 break;
572 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
573 if (optname == MCAST_JOIN_GROUP)
574 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
575 &psin6->sin6_addr);
576 else
577 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
578 &psin6->sin6_addr);
579 break;
581 case MCAST_JOIN_SOURCE_GROUP:
582 case MCAST_LEAVE_SOURCE_GROUP:
583 case MCAST_BLOCK_SOURCE:
584 case MCAST_UNBLOCK_SOURCE:
586 struct group_source_req greqs;
587 int omode, add;
589 if (optlen < sizeof(struct group_source_req))
590 goto e_inval;
591 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
592 retv = -EFAULT;
593 break;
595 if (greqs.gsr_group.ss_family != AF_INET6 ||
596 greqs.gsr_source.ss_family != AF_INET6) {
597 retv = -EADDRNOTAVAIL;
598 break;
600 if (optname == MCAST_BLOCK_SOURCE) {
601 omode = MCAST_EXCLUDE;
602 add = 1;
603 } else if (optname == MCAST_UNBLOCK_SOURCE) {
604 omode = MCAST_EXCLUDE;
605 add = 0;
606 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
607 struct sockaddr_in6 *psin6;
609 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
610 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
611 &psin6->sin6_addr);
612 /* prior join w/ different source is ok */
613 if (retv && retv != -EADDRINUSE)
614 break;
615 omode = MCAST_INCLUDE;
616 add = 1;
617 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
618 omode = MCAST_INCLUDE;
619 add = 0;
621 retv = ip6_mc_source(add, omode, sk, &greqs);
622 break;
624 case MCAST_MSFILTER:
626 extern int sysctl_mld_max_msf;
627 struct group_filter *gsf;
629 if (optlen < GROUP_FILTER_SIZE(0))
630 goto e_inval;
631 if (optlen > sysctl_optmem_max) {
632 retv = -ENOBUFS;
633 break;
635 gsf = kmalloc(optlen,GFP_KERNEL);
636 if (!gsf) {
637 retv = -ENOBUFS;
638 break;
640 retv = -EFAULT;
641 if (copy_from_user(gsf, optval, optlen)) {
642 kfree(gsf);
643 break;
645 /* numsrc >= (4G-140)/128 overflow in 32 bits */
646 if (gsf->gf_numsrc >= 0x1ffffffU ||
647 gsf->gf_numsrc > sysctl_mld_max_msf) {
648 kfree(gsf);
649 retv = -ENOBUFS;
650 break;
652 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
653 kfree(gsf);
654 retv = -EINVAL;
655 break;
657 retv = ip6_mc_msfilter(sk, gsf);
658 kfree(gsf);
660 break;
662 case IPV6_ROUTER_ALERT:
663 if (optlen < sizeof(int))
664 goto e_inval;
665 retv = ip6_ra_control(sk, val);
666 break;
667 case IPV6_MTU_DISCOVER:
668 if (optlen < sizeof(int))
669 goto e_inval;
670 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_PROBE)
671 goto e_inval;
672 np->pmtudisc = val;
673 retv = 0;
674 break;
675 case IPV6_MTU:
676 if (optlen < sizeof(int))
677 goto e_inval;
678 if (val && val < IPV6_MIN_MTU)
679 goto e_inval;
680 np->frag_size = val;
681 retv = 0;
682 break;
683 case IPV6_RECVERR:
684 if (optlen < sizeof(int))
685 goto e_inval;
686 np->recverr = valbool;
687 if (!val)
688 skb_queue_purge(&sk->sk_error_queue);
689 retv = 0;
690 break;
691 case IPV6_FLOWINFO_SEND:
692 if (optlen < sizeof(int))
693 goto e_inval;
694 np->sndflow = valbool;
695 retv = 0;
696 break;
697 case IPV6_FLOWLABEL_MGR:
698 retv = ipv6_flowlabel_opt(sk, optval, optlen);
699 break;
700 case IPV6_IPSEC_POLICY:
701 case IPV6_XFRM_POLICY:
702 retv = -EPERM;
703 if (!capable(CAP_NET_ADMIN))
704 break;
705 retv = xfrm_user_policy(sk, optname, optval, optlen);
706 break;
708 case IPV6_ADDR_PREFERENCES:
710 unsigned int pref = 0;
711 unsigned int prefmask = ~0;
713 if (optlen < sizeof(int))
714 goto e_inval;
716 retv = -EINVAL;
718 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
719 switch (val & (IPV6_PREFER_SRC_PUBLIC|
720 IPV6_PREFER_SRC_TMP|
721 IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
722 case IPV6_PREFER_SRC_PUBLIC:
723 pref |= IPV6_PREFER_SRC_PUBLIC;
724 break;
725 case IPV6_PREFER_SRC_TMP:
726 pref |= IPV6_PREFER_SRC_TMP;
727 break;
728 case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
729 break;
730 case 0:
731 goto pref_skip_pubtmp;
732 default:
733 goto e_inval;
736 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
737 IPV6_PREFER_SRC_TMP);
738 pref_skip_pubtmp:
740 /* check HOME/COA conflicts */
741 switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
742 case IPV6_PREFER_SRC_HOME:
743 break;
744 case IPV6_PREFER_SRC_COA:
745 pref |= IPV6_PREFER_SRC_COA;
746 case 0:
747 goto pref_skip_coa;
748 default:
749 goto e_inval;
752 prefmask &= ~IPV6_PREFER_SRC_COA;
753 pref_skip_coa:
755 /* check CGA/NONCGA conflicts */
756 switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
757 case IPV6_PREFER_SRC_CGA:
758 case IPV6_PREFER_SRC_NONCGA:
759 case 0:
760 break;
761 default:
762 goto e_inval;
765 np->srcprefs = (np->srcprefs & prefmask) | pref;
766 retv = 0;
768 break;
772 release_sock(sk);
774 return retv;
776 e_inval:
777 release_sock(sk);
778 return -EINVAL;
781 int ipv6_setsockopt(struct sock *sk, int level, int optname,
782 char __user *optval, unsigned int optlen)
784 int err;
786 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
787 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
789 if (level != SOL_IPV6)
790 return -ENOPROTOOPT;
792 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
793 #ifdef CONFIG_NETFILTER
794 /* we need to exclude all possible ENOPROTOOPTs except default case */
795 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
796 optname != IPV6_XFRM_POLICY) {
797 lock_sock(sk);
798 err = nf_setsockopt(sk, PF_INET6, optname, optval,
799 optlen);
800 release_sock(sk);
802 #endif
803 return err;
806 EXPORT_SYMBOL(ipv6_setsockopt);
808 #ifdef CONFIG_COMPAT
809 int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
810 char __user *optval, unsigned int optlen)
812 int err;
814 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
815 if (udp_prot.compat_setsockopt != NULL)
816 return udp_prot.compat_setsockopt(sk, level, optname,
817 optval, optlen);
818 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
821 if (level != SOL_IPV6)
822 return -ENOPROTOOPT;
824 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
825 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
826 ipv6_setsockopt);
828 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
829 #ifdef CONFIG_NETFILTER
830 /* we need to exclude all possible ENOPROTOOPTs except default case */
831 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
832 optname != IPV6_XFRM_POLICY) {
833 lock_sock(sk);
834 err = compat_nf_setsockopt(sk, PF_INET6, optname,
835 optval, optlen);
836 release_sock(sk);
838 #endif
839 return err;
842 EXPORT_SYMBOL(compat_ipv6_setsockopt);
843 #endif
845 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
846 int optname, char __user *optval, int len)
848 struct ipv6_opt_hdr *hdr;
850 if (!opt)
851 return 0;
853 switch(optname) {
854 case IPV6_HOPOPTS:
855 hdr = opt->hopopt;
856 break;
857 case IPV6_RTHDRDSTOPTS:
858 hdr = opt->dst0opt;
859 break;
860 case IPV6_RTHDR:
861 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
862 break;
863 case IPV6_DSTOPTS:
864 hdr = opt->dst1opt;
865 break;
866 default:
867 return -EINVAL; /* should not happen */
870 if (!hdr)
871 return 0;
873 len = min_t(unsigned int, len, ipv6_optlen(hdr));
874 if (copy_to_user(optval, hdr, len))
875 return -EFAULT;
876 return len;
879 static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
880 char __user *optval, int __user *optlen)
882 struct ipv6_pinfo *np = inet6_sk(sk);
883 int len;
884 int val;
886 if (ip6_mroute_opt(optname))
887 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
889 if (get_user(len, optlen))
890 return -EFAULT;
891 switch (optname) {
892 case IPV6_ADDRFORM:
893 if (sk->sk_protocol != IPPROTO_UDP &&
894 sk->sk_protocol != IPPROTO_UDPLITE &&
895 sk->sk_protocol != IPPROTO_TCP)
896 return -ENOPROTOOPT;
897 if (sk->sk_state != TCP_ESTABLISHED)
898 return -ENOTCONN;
899 val = sk->sk_family;
900 break;
901 case MCAST_MSFILTER:
903 struct group_filter gsf;
904 int err;
906 if (len < GROUP_FILTER_SIZE(0))
907 return -EINVAL;
908 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
909 return -EFAULT;
910 if (gsf.gf_group.ss_family != AF_INET6)
911 return -EADDRNOTAVAIL;
912 lock_sock(sk);
913 err = ip6_mc_msfget(sk, &gsf,
914 (struct group_filter __user *)optval, optlen);
915 release_sock(sk);
916 return err;
919 case IPV6_2292PKTOPTIONS:
921 struct msghdr msg;
922 struct sk_buff *skb;
924 if (sk->sk_type != SOCK_STREAM)
925 return -ENOPROTOOPT;
927 msg.msg_control = optval;
928 msg.msg_controllen = len;
929 msg.msg_flags = 0;
931 lock_sock(sk);
932 skb = np->pktoptions;
933 if (skb)
934 atomic_inc(&skb->users);
935 release_sock(sk);
937 if (skb) {
938 int err = datagram_recv_ctl(sk, &msg, skb);
939 kfree_skb(skb);
940 if (err)
941 return err;
942 } else {
943 if (np->rxopt.bits.rxinfo) {
944 struct in6_pktinfo src_info;
945 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
946 np->sticky_pktinfo.ipi6_ifindex;
947 np->mcast_oif? ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr) :
948 ipv6_addr_copy(&src_info.ipi6_addr, &(np->sticky_pktinfo.ipi6_addr));
949 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
951 if (np->rxopt.bits.rxhlim) {
952 int hlim = np->mcast_hops;
953 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
955 if (np->rxopt.bits.rxoinfo) {
956 struct in6_pktinfo src_info;
957 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
958 np->sticky_pktinfo.ipi6_ifindex;
959 np->mcast_oif? ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr) :
960 ipv6_addr_copy(&src_info.ipi6_addr, &(np->sticky_pktinfo.ipi6_addr));
961 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
963 if (np->rxopt.bits.rxohlim) {
964 int hlim = np->mcast_hops;
965 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
968 len -= msg.msg_controllen;
969 return put_user(len, optlen);
971 case IPV6_MTU:
973 struct dst_entry *dst;
974 val = 0;
975 lock_sock(sk);
976 dst = sk_dst_get(sk);
977 if (dst) {
978 val = dst_mtu(dst);
979 dst_release(dst);
981 release_sock(sk);
982 if (!val)
983 return -ENOTCONN;
984 break;
987 case IPV6_V6ONLY:
988 val = np->ipv6only;
989 break;
991 case IPV6_RECVPKTINFO:
992 val = np->rxopt.bits.rxinfo;
993 break;
995 case IPV6_2292PKTINFO:
996 val = np->rxopt.bits.rxoinfo;
997 break;
999 case IPV6_RECVHOPLIMIT:
1000 val = np->rxopt.bits.rxhlim;
1001 break;
1003 case IPV6_2292HOPLIMIT:
1004 val = np->rxopt.bits.rxohlim;
1005 break;
1007 case IPV6_RECVRTHDR:
1008 val = np->rxopt.bits.srcrt;
1009 break;
1011 case IPV6_2292RTHDR:
1012 val = np->rxopt.bits.osrcrt;
1013 break;
1015 case IPV6_HOPOPTS:
1016 case IPV6_RTHDRDSTOPTS:
1017 case IPV6_RTHDR:
1018 case IPV6_DSTOPTS:
1021 lock_sock(sk);
1022 len = ipv6_getsockopt_sticky(sk, np->opt,
1023 optname, optval, len);
1024 release_sock(sk);
1025 /* check if ipv6_getsockopt_sticky() returns err code */
1026 if (len < 0)
1027 return len;
1028 return put_user(len, optlen);
1031 case IPV6_RECVHOPOPTS:
1032 val = np->rxopt.bits.hopopts;
1033 break;
1035 case IPV6_2292HOPOPTS:
1036 val = np->rxopt.bits.ohopopts;
1037 break;
1039 case IPV6_RECVDSTOPTS:
1040 val = np->rxopt.bits.dstopts;
1041 break;
1043 case IPV6_2292DSTOPTS:
1044 val = np->rxopt.bits.odstopts;
1045 break;
1047 case IPV6_TCLASS:
1048 val = np->tclass;
1049 break;
1051 case IPV6_RECVTCLASS:
1052 val = np->rxopt.bits.rxtclass;
1053 break;
1055 case IPV6_FLOWINFO:
1056 val = np->rxopt.bits.rxflow;
1057 break;
1059 case IPV6_UNICAST_HOPS:
1060 case IPV6_MULTICAST_HOPS:
1062 struct dst_entry *dst;
1064 if (optname == IPV6_UNICAST_HOPS)
1065 val = np->hop_limit;
1066 else
1067 val = np->mcast_hops;
1069 dst = sk_dst_get(sk);
1070 if (dst) {
1071 if (val < 0)
1072 val = ip6_dst_hoplimit(dst);
1073 dst_release(dst);
1075 if (val < 0)
1076 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
1077 break;
1080 case IPV6_MULTICAST_LOOP:
1081 val = np->mc_loop;
1082 break;
1084 case IPV6_MULTICAST_IF:
1085 val = np->mcast_oif;
1086 break;
1088 case IPV6_MTU_DISCOVER:
1089 val = np->pmtudisc;
1090 break;
1092 case IPV6_RECVERR:
1093 val = np->recverr;
1094 break;
1096 case IPV6_FLOWINFO_SEND:
1097 val = np->sndflow;
1098 break;
1100 case IPV6_ADDR_PREFERENCES:
1101 val = 0;
1103 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1104 val |= IPV6_PREFER_SRC_TMP;
1105 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1106 val |= IPV6_PREFER_SRC_PUBLIC;
1107 else {
1108 /* XXX: should we return system default? */
1109 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1112 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1113 val |= IPV6_PREFER_SRC_COA;
1114 else
1115 val |= IPV6_PREFER_SRC_HOME;
1116 break;
1118 default:
1119 return -ENOPROTOOPT;
1121 len = min_t(unsigned int, sizeof(int), len);
1122 if(put_user(len, optlen))
1123 return -EFAULT;
1124 if(copy_to_user(optval,&val,len))
1125 return -EFAULT;
1126 return 0;
1129 int ipv6_getsockopt(struct sock *sk, int level, int optname,
1130 char __user *optval, int __user *optlen)
1132 int err;
1134 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1135 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1137 if(level != SOL_IPV6)
1138 return -ENOPROTOOPT;
1140 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1141 #ifdef CONFIG_NETFILTER
1142 /* we need to exclude all possible ENOPROTOOPTs except default case */
1143 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1144 int len;
1146 if (get_user(len, optlen))
1147 return -EFAULT;
1149 lock_sock(sk);
1150 err = nf_getsockopt(sk, PF_INET6, optname, optval,
1151 &len);
1152 release_sock(sk);
1153 if (err >= 0)
1154 err = put_user(len, optlen);
1156 #endif
1157 return err;
1160 EXPORT_SYMBOL(ipv6_getsockopt);
1162 #ifdef CONFIG_COMPAT
1163 int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
1164 char __user *optval, int __user *optlen)
1166 int err;
1168 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
1169 if (udp_prot.compat_getsockopt != NULL)
1170 return udp_prot.compat_getsockopt(sk, level, optname,
1171 optval, optlen);
1172 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1175 if (level != SOL_IPV6)
1176 return -ENOPROTOOPT;
1178 if (optname == MCAST_MSFILTER)
1179 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1180 ipv6_getsockopt);
1182 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1183 #ifdef CONFIG_NETFILTER
1184 /* we need to exclude all possible ENOPROTOOPTs except default case */
1185 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1186 int len;
1188 if (get_user(len, optlen))
1189 return -EFAULT;
1191 lock_sock(sk);
1192 err = compat_nf_getsockopt(sk, PF_INET6,
1193 optname, optval, &len);
1194 release_sock(sk);
1195 if (err >= 0)
1196 err = put_user(len, optlen);
1198 #endif
1199 return err;
1202 EXPORT_SYMBOL(compat_ipv6_getsockopt);
1203 #endif