Linux 2.6.21.1
[linux/fpc-iii.git] / net / ipv6 / ipv6_sockglue.c
blobf5f9582a8d39554c120ed147ddc9f71105d6a889
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 * $Id: ipv6_sockglue.c,v 1.41 2002/02/01 22:01:04 davem Exp $
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
17 * FIXME: Make the setsockopt code POSIX compliant: That is
19 * o Return -EINVAL for setsockopt of short lengths
20 * o Truncate getsockopt returns
21 * o Return an optlen of the truncated length if need be
23 * Changes:
24 * David L Stevens <dlstevens@us.ibm.com>:
25 * - added multicast source filtering API for MLDv2
28 #include <linux/module.h>
29 #include <linux/capability.h>
30 #include <linux/errno.h>
31 #include <linux/types.h>
32 #include <linux/socket.h>
33 #include <linux/sockios.h>
34 #include <linux/net.h>
35 #include <linux/in6.h>
36 #include <linux/netdevice.h>
37 #include <linux/if_arp.h>
38 #include <linux/init.h>
39 #include <linux/sysctl.h>
40 #include <linux/netfilter.h>
42 #include <net/sock.h>
43 #include <net/snmp.h>
44 #include <net/ipv6.h>
45 #include <net/ndisc.h>
46 #include <net/protocol.h>
47 #include <net/transp_v6.h>
48 #include <net/ip6_route.h>
49 #include <net/addrconf.h>
50 #include <net/inet_common.h>
51 #include <net/tcp.h>
52 #include <net/udp.h>
53 #include <net/udplite.h>
54 #include <net/xfrm.h>
56 #include <asm/uaccess.h>
58 DEFINE_SNMP_STAT(struct ipstats_mib, ipv6_statistics) __read_mostly;
60 static struct inet6_protocol *ipv6_gso_pull_exthdrs(struct sk_buff *skb,
61 int proto)
63 struct inet6_protocol *ops = NULL;
65 for (;;) {
66 struct ipv6_opt_hdr *opth;
67 int len;
69 if (proto != NEXTHDR_HOP) {
70 ops = rcu_dereference(inet6_protos[proto]);
72 if (unlikely(!ops))
73 break;
75 if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
76 break;
79 if (unlikely(!pskb_may_pull(skb, 8)))
80 break;
82 opth = (void *)skb->data;
83 len = opth->hdrlen * 8 + 8;
85 if (unlikely(!pskb_may_pull(skb, len)))
86 break;
88 proto = opth->nexthdr;
89 __skb_pull(skb, len);
92 return ops;
95 static int ipv6_gso_send_check(struct sk_buff *skb)
97 struct ipv6hdr *ipv6h;
98 struct inet6_protocol *ops;
99 int err = -EINVAL;
101 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
102 goto out;
104 ipv6h = skb->nh.ipv6h;
105 __skb_pull(skb, sizeof(*ipv6h));
106 err = -EPROTONOSUPPORT;
108 rcu_read_lock();
109 ops = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
110 if (likely(ops && ops->gso_send_check)) {
111 skb->h.raw = skb->data;
112 err = ops->gso_send_check(skb);
114 rcu_read_unlock();
116 out:
117 return err;
120 static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb, int features)
122 struct sk_buff *segs = ERR_PTR(-EINVAL);
123 struct ipv6hdr *ipv6h;
124 struct inet6_protocol *ops;
126 if (!(features & NETIF_F_HW_CSUM))
127 features &= ~NETIF_F_SG;
129 if (unlikely(skb_shinfo(skb)->gso_type &
130 ~(SKB_GSO_UDP |
131 SKB_GSO_DODGY |
132 SKB_GSO_TCP_ECN |
133 SKB_GSO_TCPV6 |
134 0)))
135 goto out;
137 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
138 goto out;
140 ipv6h = skb->nh.ipv6h;
141 __skb_pull(skb, sizeof(*ipv6h));
142 segs = ERR_PTR(-EPROTONOSUPPORT);
144 rcu_read_lock();
145 ops = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
146 if (likely(ops && ops->gso_segment)) {
147 skb->h.raw = skb->data;
148 segs = ops->gso_segment(skb, features);
150 rcu_read_unlock();
152 if (unlikely(IS_ERR(segs)))
153 goto out;
155 for (skb = segs; skb; skb = skb->next) {
156 ipv6h = skb->nh.ipv6h;
157 ipv6h->payload_len = htons(skb->len - skb->mac_len -
158 sizeof(*ipv6h));
161 out:
162 return segs;
165 static struct packet_type ipv6_packet_type = {
166 .type = __constant_htons(ETH_P_IPV6),
167 .func = ipv6_rcv,
168 .gso_send_check = ipv6_gso_send_check,
169 .gso_segment = ipv6_gso_segment,
172 struct ip6_ra_chain *ip6_ra_chain;
173 DEFINE_RWLOCK(ip6_ra_lock);
175 int ip6_ra_control(struct sock *sk, int sel, void (*destructor)(struct sock *))
177 struct ip6_ra_chain *ra, *new_ra, **rap;
179 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
180 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->num != IPPROTO_RAW)
181 return -EINVAL;
183 new_ra = (sel>=0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
185 write_lock_bh(&ip6_ra_lock);
186 for (rap = &ip6_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
187 if (ra->sk == sk) {
188 if (sel>=0) {
189 write_unlock_bh(&ip6_ra_lock);
190 kfree(new_ra);
191 return -EADDRINUSE;
194 *rap = ra->next;
195 write_unlock_bh(&ip6_ra_lock);
197 if (ra->destructor)
198 ra->destructor(sk);
199 sock_put(sk);
200 kfree(ra);
201 return 0;
204 if (new_ra == NULL) {
205 write_unlock_bh(&ip6_ra_lock);
206 return -ENOBUFS;
208 new_ra->sk = sk;
209 new_ra->sel = sel;
210 new_ra->destructor = destructor;
211 new_ra->next = ra;
212 *rap = new_ra;
213 sock_hold(sk);
214 write_unlock_bh(&ip6_ra_lock);
215 return 0;
218 static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
219 char __user *optval, int optlen)
221 struct ipv6_pinfo *np = inet6_sk(sk);
222 int val, valbool;
223 int retv = -ENOPROTOOPT;
225 if (optval == NULL)
226 val=0;
227 else if (get_user(val, (int __user *) optval))
228 return -EFAULT;
230 valbool = (val!=0);
232 lock_sock(sk);
234 switch (optname) {
236 case IPV6_ADDRFORM:
237 if (val == PF_INET) {
238 struct ipv6_txoptions *opt;
239 struct sk_buff *pktopt;
241 if (sk->sk_protocol != IPPROTO_UDP &&
242 sk->sk_protocol != IPPROTO_UDPLITE &&
243 sk->sk_protocol != IPPROTO_TCP)
244 break;
246 if (sk->sk_state != TCP_ESTABLISHED) {
247 retv = -ENOTCONN;
248 break;
251 if (ipv6_only_sock(sk) ||
252 !(ipv6_addr_type(&np->daddr) & IPV6_ADDR_MAPPED)) {
253 retv = -EADDRNOTAVAIL;
254 break;
257 fl6_free_socklist(sk);
258 ipv6_sock_mc_close(sk);
261 * Sock is moving from IPv6 to IPv4 (sk_prot), so
262 * remove it from the refcnt debug socks count in the
263 * original family...
265 sk_refcnt_debug_dec(sk);
267 if (sk->sk_protocol == IPPROTO_TCP) {
268 struct inet_connection_sock *icsk = inet_csk(sk);
270 local_bh_disable();
271 sock_prot_dec_use(sk->sk_prot);
272 sock_prot_inc_use(&tcp_prot);
273 local_bh_enable();
274 sk->sk_prot = &tcp_prot;
275 icsk->icsk_af_ops = &ipv4_specific;
276 sk->sk_socket->ops = &inet_stream_ops;
277 sk->sk_family = PF_INET;
278 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
279 } else {
280 struct proto *prot = &udp_prot;
282 if (sk->sk_protocol == IPPROTO_UDPLITE)
283 prot = &udplite_prot;
284 local_bh_disable();
285 sock_prot_dec_use(sk->sk_prot);
286 sock_prot_inc_use(prot);
287 local_bh_enable();
288 sk->sk_prot = prot;
289 sk->sk_socket->ops = &inet_dgram_ops;
290 sk->sk_family = PF_INET;
292 opt = xchg(&np->opt, NULL);
293 if (opt)
294 sock_kfree_s(sk, opt, opt->tot_len);
295 pktopt = xchg(&np->pktoptions, NULL);
296 if (pktopt)
297 kfree_skb(pktopt);
299 sk->sk_destruct = inet_sock_destruct;
301 * ... and add it to the refcnt debug socks count
302 * in the new family. -acme
304 sk_refcnt_debug_inc(sk);
305 module_put(THIS_MODULE);
306 retv = 0;
307 break;
309 goto e_inval;
311 case IPV6_V6ONLY:
312 if (inet_sk(sk)->num)
313 goto e_inval;
314 np->ipv6only = valbool;
315 retv = 0;
316 break;
318 case IPV6_RECVPKTINFO:
319 np->rxopt.bits.rxinfo = valbool;
320 retv = 0;
321 break;
323 case IPV6_2292PKTINFO:
324 np->rxopt.bits.rxoinfo = valbool;
325 retv = 0;
326 break;
328 case IPV6_RECVHOPLIMIT:
329 np->rxopt.bits.rxhlim = valbool;
330 retv = 0;
331 break;
333 case IPV6_2292HOPLIMIT:
334 np->rxopt.bits.rxohlim = valbool;
335 retv = 0;
336 break;
338 case IPV6_RECVRTHDR:
339 if (val < 0 || val > 2)
340 goto e_inval;
341 np->rxopt.bits.srcrt = val;
342 retv = 0;
343 break;
345 case IPV6_2292RTHDR:
346 if (val < 0 || val > 2)
347 goto e_inval;
348 np->rxopt.bits.osrcrt = val;
349 retv = 0;
350 break;
352 case IPV6_RECVHOPOPTS:
353 np->rxopt.bits.hopopts = valbool;
354 retv = 0;
355 break;
357 case IPV6_2292HOPOPTS:
358 np->rxopt.bits.ohopopts = valbool;
359 retv = 0;
360 break;
362 case IPV6_RECVDSTOPTS:
363 np->rxopt.bits.dstopts = valbool;
364 retv = 0;
365 break;
367 case IPV6_2292DSTOPTS:
368 np->rxopt.bits.odstopts = valbool;
369 retv = 0;
370 break;
372 case IPV6_TCLASS:
373 if (val < -1 || val > 0xff)
374 goto e_inval;
375 np->tclass = val;
376 retv = 0;
377 break;
379 case IPV6_RECVTCLASS:
380 np->rxopt.bits.rxtclass = valbool;
381 retv = 0;
382 break;
384 case IPV6_FLOWINFO:
385 np->rxopt.bits.rxflow = valbool;
386 retv = 0;
387 break;
389 case IPV6_HOPOPTS:
390 case IPV6_RTHDRDSTOPTS:
391 case IPV6_RTHDR:
392 case IPV6_DSTOPTS:
394 struct ipv6_txoptions *opt;
395 if (optlen == 0)
396 optval = NULL;
398 /* hop-by-hop / destination options are privileged option */
399 retv = -EPERM;
400 if (optname != IPV6_RTHDR && !capable(CAP_NET_RAW))
401 break;
403 retv = -EINVAL;
404 if (optlen & 0x7 || optlen > 8 * 255)
405 break;
407 opt = ipv6_renew_options(sk, np->opt, optname,
408 (struct ipv6_opt_hdr __user *)optval,
409 optlen);
410 if (IS_ERR(opt)) {
411 retv = PTR_ERR(opt);
412 break;
415 /* routing header option needs extra check */
416 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
417 struct ipv6_rt_hdr *rthdr = opt->srcrt;
418 switch (rthdr->type) {
419 case IPV6_SRCRT_TYPE_0:
420 #ifdef CONFIG_IPV6_MIP6
421 case IPV6_SRCRT_TYPE_2:
422 #endif
423 break;
424 default:
425 goto sticky_done;
428 if ((rthdr->hdrlen & 1) ||
429 (rthdr->hdrlen >> 1) != rthdr->segments_left)
430 goto sticky_done;
433 retv = 0;
434 if (inet_sk(sk)->is_icsk) {
435 if (opt) {
436 struct inet_connection_sock *icsk = inet_csk(sk);
437 if (!((1 << sk->sk_state) &
438 (TCPF_LISTEN | TCPF_CLOSE))
439 && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
440 icsk->icsk_ext_hdr_len =
441 opt->opt_flen + opt->opt_nflen;
442 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
445 opt = xchg(&np->opt, opt);
446 sk_dst_reset(sk);
447 } else {
448 write_lock(&sk->sk_dst_lock);
449 opt = xchg(&np->opt, opt);
450 write_unlock(&sk->sk_dst_lock);
451 sk_dst_reset(sk);
453 sticky_done:
454 if (opt)
455 sock_kfree_s(sk, opt, opt->tot_len);
456 break;
459 case IPV6_2292PKTOPTIONS:
461 struct ipv6_txoptions *opt = NULL;
462 struct msghdr msg;
463 struct flowi fl;
464 int junk;
466 fl.fl6_flowlabel = 0;
467 fl.oif = sk->sk_bound_dev_if;
469 if (optlen == 0)
470 goto update;
472 /* 1K is probably excessive
473 * 1K is surely not enough, 2K per standard header is 16K.
475 retv = -EINVAL;
476 if (optlen > 64*1024)
477 break;
479 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
480 retv = -ENOBUFS;
481 if (opt == NULL)
482 break;
484 memset(opt, 0, sizeof(*opt));
485 opt->tot_len = sizeof(*opt) + optlen;
486 retv = -EFAULT;
487 if (copy_from_user(opt+1, optval, optlen))
488 goto done;
490 msg.msg_controllen = optlen;
491 msg.msg_control = (void*)(opt+1);
493 retv = datagram_send_ctl(&msg, &fl, opt, &junk, &junk);
494 if (retv)
495 goto done;
496 update:
497 retv = 0;
498 if (inet_sk(sk)->is_icsk) {
499 if (opt) {
500 struct inet_connection_sock *icsk = inet_csk(sk);
501 if (!((1 << sk->sk_state) &
502 (TCPF_LISTEN | TCPF_CLOSE))
503 && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
504 icsk->icsk_ext_hdr_len =
505 opt->opt_flen + opt->opt_nflen;
506 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
509 opt = xchg(&np->opt, opt);
510 sk_dst_reset(sk);
511 } else {
512 write_lock(&sk->sk_dst_lock);
513 opt = xchg(&np->opt, opt);
514 write_unlock(&sk->sk_dst_lock);
515 sk_dst_reset(sk);
518 done:
519 if (opt)
520 sock_kfree_s(sk, opt, opt->tot_len);
521 break;
523 case IPV6_UNICAST_HOPS:
524 if (val > 255 || val < -1)
525 goto e_inval;
526 np->hop_limit = val;
527 retv = 0;
528 break;
530 case IPV6_MULTICAST_HOPS:
531 if (sk->sk_type == SOCK_STREAM)
532 goto e_inval;
533 if (val > 255 || val < -1)
534 goto e_inval;
535 np->mcast_hops = val;
536 retv = 0;
537 break;
539 case IPV6_MULTICAST_LOOP:
540 np->mc_loop = valbool;
541 retv = 0;
542 break;
544 case IPV6_MULTICAST_IF:
545 if (sk->sk_type == SOCK_STREAM)
546 goto e_inval;
547 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
548 goto e_inval;
550 if (__dev_get_by_index(val) == NULL) {
551 retv = -ENODEV;
552 break;
554 np->mcast_oif = val;
555 retv = 0;
556 break;
557 case IPV6_ADD_MEMBERSHIP:
558 case IPV6_DROP_MEMBERSHIP:
560 struct ipv6_mreq mreq;
562 retv = -EFAULT;
563 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
564 break;
566 if (optname == IPV6_ADD_MEMBERSHIP)
567 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
568 else
569 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
570 break;
572 case IPV6_JOIN_ANYCAST:
573 case IPV6_LEAVE_ANYCAST:
575 struct ipv6_mreq mreq;
577 if (optlen != sizeof(struct ipv6_mreq))
578 goto e_inval;
580 retv = -EFAULT;
581 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
582 break;
584 if (optname == IPV6_JOIN_ANYCAST)
585 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
586 else
587 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
588 break;
590 case MCAST_JOIN_GROUP:
591 case MCAST_LEAVE_GROUP:
593 struct group_req greq;
594 struct sockaddr_in6 *psin6;
596 retv = -EFAULT;
597 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
598 break;
599 if (greq.gr_group.ss_family != AF_INET6) {
600 retv = -EADDRNOTAVAIL;
601 break;
603 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
604 if (optname == MCAST_JOIN_GROUP)
605 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
606 &psin6->sin6_addr);
607 else
608 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
609 &psin6->sin6_addr);
610 break;
612 case MCAST_JOIN_SOURCE_GROUP:
613 case MCAST_LEAVE_SOURCE_GROUP:
614 case MCAST_BLOCK_SOURCE:
615 case MCAST_UNBLOCK_SOURCE:
617 struct group_source_req greqs;
618 int omode, add;
620 if (optlen != sizeof(struct group_source_req))
621 goto e_inval;
622 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
623 retv = -EFAULT;
624 break;
626 if (greqs.gsr_group.ss_family != AF_INET6 ||
627 greqs.gsr_source.ss_family != AF_INET6) {
628 retv = -EADDRNOTAVAIL;
629 break;
631 if (optname == MCAST_BLOCK_SOURCE) {
632 omode = MCAST_EXCLUDE;
633 add = 1;
634 } else if (optname == MCAST_UNBLOCK_SOURCE) {
635 omode = MCAST_EXCLUDE;
636 add = 0;
637 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
638 struct sockaddr_in6 *psin6;
640 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
641 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
642 &psin6->sin6_addr);
643 /* prior join w/ different source is ok */
644 if (retv && retv != -EADDRINUSE)
645 break;
646 omode = MCAST_INCLUDE;
647 add = 1;
648 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
649 omode = MCAST_INCLUDE;
650 add = 0;
652 retv = ip6_mc_source(add, omode, sk, &greqs);
653 break;
655 case MCAST_MSFILTER:
657 extern int sysctl_mld_max_msf;
658 struct group_filter *gsf;
660 if (optlen < GROUP_FILTER_SIZE(0))
661 goto e_inval;
662 if (optlen > sysctl_optmem_max) {
663 retv = -ENOBUFS;
664 break;
666 gsf = kmalloc(optlen,GFP_KERNEL);
667 if (gsf == 0) {
668 retv = -ENOBUFS;
669 break;
671 retv = -EFAULT;
672 if (copy_from_user(gsf, optval, optlen)) {
673 kfree(gsf);
674 break;
676 /* numsrc >= (4G-140)/128 overflow in 32 bits */
677 if (gsf->gf_numsrc >= 0x1ffffffU ||
678 gsf->gf_numsrc > sysctl_mld_max_msf) {
679 kfree(gsf);
680 retv = -ENOBUFS;
681 break;
683 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
684 kfree(gsf);
685 retv = -EINVAL;
686 break;
688 retv = ip6_mc_msfilter(sk, gsf);
689 kfree(gsf);
691 break;
693 case IPV6_ROUTER_ALERT:
694 retv = ip6_ra_control(sk, val, NULL);
695 break;
696 case IPV6_MTU_DISCOVER:
697 if (val<0 || val>2)
698 goto e_inval;
699 np->pmtudisc = val;
700 retv = 0;
701 break;
702 case IPV6_MTU:
703 if (val && val < IPV6_MIN_MTU)
704 goto e_inval;
705 np->frag_size = val;
706 retv = 0;
707 break;
708 case IPV6_RECVERR:
709 np->recverr = valbool;
710 if (!val)
711 skb_queue_purge(&sk->sk_error_queue);
712 retv = 0;
713 break;
714 case IPV6_FLOWINFO_SEND:
715 np->sndflow = valbool;
716 retv = 0;
717 break;
718 case IPV6_FLOWLABEL_MGR:
719 retv = ipv6_flowlabel_opt(sk, optval, optlen);
720 break;
721 case IPV6_IPSEC_POLICY:
722 case IPV6_XFRM_POLICY:
723 retv = -EPERM;
724 if (!capable(CAP_NET_ADMIN))
725 break;
726 retv = xfrm_user_policy(sk, optname, optval, optlen);
727 break;
730 release_sock(sk);
732 return retv;
734 e_inval:
735 release_sock(sk);
736 return -EINVAL;
739 int ipv6_setsockopt(struct sock *sk, int level, int optname,
740 char __user *optval, int optlen)
742 int err;
744 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
745 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
747 if (level != SOL_IPV6)
748 return -ENOPROTOOPT;
750 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
751 #ifdef CONFIG_NETFILTER
752 /* we need to exclude all possible ENOPROTOOPTs except default case */
753 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
754 optname != IPV6_XFRM_POLICY) {
755 lock_sock(sk);
756 err = nf_setsockopt(sk, PF_INET6, optname, optval,
757 optlen);
758 release_sock(sk);
760 #endif
761 return err;
765 #ifdef CONFIG_COMPAT
766 int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
767 char __user *optval, int optlen)
769 int err;
771 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
772 if (udp_prot.compat_setsockopt != NULL)
773 return udp_prot.compat_setsockopt(sk, level, optname,
774 optval, optlen);
775 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
778 if (level != SOL_IPV6)
779 return -ENOPROTOOPT;
781 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
782 #ifdef CONFIG_NETFILTER
783 /* we need to exclude all possible ENOPROTOOPTs except default case */
784 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
785 optname != IPV6_XFRM_POLICY) {
786 lock_sock(sk);
787 err = compat_nf_setsockopt(sk, PF_INET6, optname,
788 optval, optlen);
789 release_sock(sk);
791 #endif
792 return err;
795 EXPORT_SYMBOL(compat_ipv6_setsockopt);
796 #endif
798 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
799 char __user *optval, int len)
801 struct ipv6_opt_hdr *hdr;
803 if (!opt || !opt->hopopt)
804 return 0;
805 hdr = opt->hopopt;
807 len = min_t(unsigned int, len, ipv6_optlen(hdr));
808 if (copy_to_user(optval, hdr, ipv6_optlen(hdr)))
809 return -EFAULT;
810 return len;
813 static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
814 char __user *optval, int __user *optlen)
816 struct ipv6_pinfo *np = inet6_sk(sk);
817 int len;
818 int val;
820 if (get_user(len, optlen))
821 return -EFAULT;
822 switch (optname) {
823 case IPV6_ADDRFORM:
824 if (sk->sk_protocol != IPPROTO_UDP &&
825 sk->sk_protocol != IPPROTO_UDPLITE &&
826 sk->sk_protocol != IPPROTO_TCP)
827 return -EINVAL;
828 if (sk->sk_state != TCP_ESTABLISHED)
829 return -ENOTCONN;
830 val = sk->sk_family;
831 break;
832 case MCAST_MSFILTER:
834 struct group_filter gsf;
835 int err;
837 if (len < GROUP_FILTER_SIZE(0))
838 return -EINVAL;
839 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
840 return -EFAULT;
841 lock_sock(sk);
842 err = ip6_mc_msfget(sk, &gsf,
843 (struct group_filter __user *)optval, optlen);
844 release_sock(sk);
845 return err;
848 case IPV6_2292PKTOPTIONS:
850 struct msghdr msg;
851 struct sk_buff *skb;
853 if (sk->sk_type != SOCK_STREAM)
854 return -ENOPROTOOPT;
856 msg.msg_control = optval;
857 msg.msg_controllen = len;
858 msg.msg_flags = 0;
860 lock_sock(sk);
861 skb = np->pktoptions;
862 if (skb)
863 atomic_inc(&skb->users);
864 release_sock(sk);
866 if (skb) {
867 int err = datagram_recv_ctl(sk, &msg, skb);
868 kfree_skb(skb);
869 if (err)
870 return err;
871 } else {
872 if (np->rxopt.bits.rxinfo) {
873 struct in6_pktinfo src_info;
874 src_info.ipi6_ifindex = np->mcast_oif;
875 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
876 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
878 if (np->rxopt.bits.rxhlim) {
879 int hlim = np->mcast_hops;
880 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
882 if (np->rxopt.bits.rxoinfo) {
883 struct in6_pktinfo src_info;
884 src_info.ipi6_ifindex = np->mcast_oif;
885 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
886 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
888 if (np->rxopt.bits.rxohlim) {
889 int hlim = np->mcast_hops;
890 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
893 len -= msg.msg_controllen;
894 return put_user(len, optlen);
896 case IPV6_MTU:
898 struct dst_entry *dst;
899 val = 0;
900 lock_sock(sk);
901 dst = sk_dst_get(sk);
902 if (dst) {
903 val = dst_mtu(dst);
904 dst_release(dst);
906 release_sock(sk);
907 if (!val)
908 return -ENOTCONN;
909 break;
912 case IPV6_V6ONLY:
913 val = np->ipv6only;
914 break;
916 case IPV6_RECVPKTINFO:
917 val = np->rxopt.bits.rxinfo;
918 break;
920 case IPV6_2292PKTINFO:
921 val = np->rxopt.bits.rxoinfo;
922 break;
924 case IPV6_RECVHOPLIMIT:
925 val = np->rxopt.bits.rxhlim;
926 break;
928 case IPV6_2292HOPLIMIT:
929 val = np->rxopt.bits.rxohlim;
930 break;
932 case IPV6_RECVRTHDR:
933 val = np->rxopt.bits.srcrt;
934 break;
936 case IPV6_2292RTHDR:
937 val = np->rxopt.bits.osrcrt;
938 break;
940 case IPV6_HOPOPTS:
941 case IPV6_RTHDRDSTOPTS:
942 case IPV6_RTHDR:
943 case IPV6_DSTOPTS:
946 lock_sock(sk);
947 len = ipv6_getsockopt_sticky(sk, np->opt,
948 optval, len);
949 release_sock(sk);
950 return put_user(len, optlen);
953 case IPV6_RECVHOPOPTS:
954 val = np->rxopt.bits.hopopts;
955 break;
957 case IPV6_2292HOPOPTS:
958 val = np->rxopt.bits.ohopopts;
959 break;
961 case IPV6_RECVDSTOPTS:
962 val = np->rxopt.bits.dstopts;
963 break;
965 case IPV6_2292DSTOPTS:
966 val = np->rxopt.bits.odstopts;
967 break;
969 case IPV6_TCLASS:
970 val = np->tclass;
971 if (val < 0)
972 val = 0;
973 break;
975 case IPV6_RECVTCLASS:
976 val = np->rxopt.bits.rxtclass;
977 break;
979 case IPV6_FLOWINFO:
980 val = np->rxopt.bits.rxflow;
981 break;
983 case IPV6_UNICAST_HOPS:
984 case IPV6_MULTICAST_HOPS:
986 struct dst_entry *dst;
988 if (optname == IPV6_UNICAST_HOPS)
989 val = np->hop_limit;
990 else
991 val = np->mcast_hops;
993 dst = sk_dst_get(sk);
994 if (dst) {
995 if (val < 0)
996 val = dst_metric(dst, RTAX_HOPLIMIT);
997 if (val < 0)
998 val = ipv6_get_hoplimit(dst->dev);
999 dst_release(dst);
1001 if (val < 0)
1002 val = ipv6_devconf.hop_limit;
1003 break;
1006 case IPV6_MULTICAST_LOOP:
1007 val = np->mc_loop;
1008 break;
1010 case IPV6_MULTICAST_IF:
1011 val = np->mcast_oif;
1012 break;
1014 case IPV6_MTU_DISCOVER:
1015 val = np->pmtudisc;
1016 break;
1018 case IPV6_RECVERR:
1019 val = np->recverr;
1020 break;
1022 case IPV6_FLOWINFO_SEND:
1023 val = np->sndflow;
1024 break;
1026 default:
1027 return -EINVAL;
1029 len = min_t(unsigned int, sizeof(int), len);
1030 if(put_user(len, optlen))
1031 return -EFAULT;
1032 if(copy_to_user(optval,&val,len))
1033 return -EFAULT;
1034 return 0;
1037 int ipv6_getsockopt(struct sock *sk, int level, int optname,
1038 char __user *optval, int __user *optlen)
1040 int err;
1042 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1043 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1045 if(level != SOL_IPV6)
1046 return -ENOPROTOOPT;
1048 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1049 #ifdef CONFIG_NETFILTER
1050 /* we need to exclude all possible EINVALs except default case */
1051 if (err == -EINVAL && optname != IPV6_ADDRFORM &&
1052 optname != MCAST_MSFILTER) {
1053 int len;
1055 if (get_user(len, optlen))
1056 return -EFAULT;
1058 lock_sock(sk);
1059 err = nf_getsockopt(sk, PF_INET6, optname, optval,
1060 &len);
1061 release_sock(sk);
1062 if (err >= 0)
1063 err = put_user(len, optlen);
1065 #endif
1066 return err;
1069 #ifdef CONFIG_COMPAT
1070 int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
1071 char __user *optval, int __user *optlen)
1073 int err;
1075 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
1076 if (udp_prot.compat_getsockopt != NULL)
1077 return udp_prot.compat_getsockopt(sk, level, optname,
1078 optval, optlen);
1079 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1082 if (level != SOL_IPV6)
1083 return -ENOPROTOOPT;
1085 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1086 #ifdef CONFIG_NETFILTER
1087 /* we need to exclude all possible EINVALs except default case */
1088 if (err == -EINVAL && optname != IPV6_ADDRFORM &&
1089 optname != MCAST_MSFILTER) {
1090 int len;
1092 if (get_user(len, optlen))
1093 return -EFAULT;
1095 lock_sock(sk);
1096 err = compat_nf_getsockopt(sk, PF_INET6,
1097 optname, optval, &len);
1098 release_sock(sk);
1099 if (err >= 0)
1100 err = put_user(len, optlen);
1102 #endif
1103 return err;
1106 EXPORT_SYMBOL(compat_ipv6_getsockopt);
1107 #endif
1109 void __init ipv6_packet_init(void)
1111 dev_add_pack(&ipv6_packet_type);
1114 void ipv6_packet_cleanup(void)
1116 dev_remove_pack(&ipv6_packet_type);