Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/sparc-2.6
[linux/fpc-iii.git] / net / ipv6 / ipv6_sockglue.c
blobde6b91981b30611b0cd63ea8d14a304b34387c27
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/sched.h>
35 #include <linux/net.h>
36 #include <linux/in6.h>
37 #include <linux/netdevice.h>
38 #include <linux/if_arp.h>
39 #include <linux/init.h>
40 #include <linux/sysctl.h>
41 #include <linux/netfilter.h>
43 #include <net/sock.h>
44 #include <net/snmp.h>
45 #include <net/ipv6.h>
46 #include <net/ndisc.h>
47 #include <net/protocol.h>
48 #include <net/transp_v6.h>
49 #include <net/ip6_route.h>
50 #include <net/addrconf.h>
51 #include <net/inet_common.h>
52 #include <net/tcp.h>
53 #include <net/udp.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_TCP)
243 break;
245 if (sk->sk_state != TCP_ESTABLISHED) {
246 retv = -ENOTCONN;
247 break;
250 if (ipv6_only_sock(sk) ||
251 !(ipv6_addr_type(&np->daddr) & IPV6_ADDR_MAPPED)) {
252 retv = -EADDRNOTAVAIL;
253 break;
256 fl6_free_socklist(sk);
257 ipv6_sock_mc_close(sk);
260 * Sock is moving from IPv6 to IPv4 (sk_prot), so
261 * remove it from the refcnt debug socks count in the
262 * original family...
264 sk_refcnt_debug_dec(sk);
266 if (sk->sk_protocol == IPPROTO_TCP) {
267 struct inet_connection_sock *icsk = inet_csk(sk);
269 local_bh_disable();
270 sock_prot_dec_use(sk->sk_prot);
271 sock_prot_inc_use(&tcp_prot);
272 local_bh_enable();
273 sk->sk_prot = &tcp_prot;
274 icsk->icsk_af_ops = &ipv4_specific;
275 sk->sk_socket->ops = &inet_stream_ops;
276 sk->sk_family = PF_INET;
277 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
278 } else {
279 local_bh_disable();
280 sock_prot_dec_use(sk->sk_prot);
281 sock_prot_inc_use(&udp_prot);
282 local_bh_enable();
283 sk->sk_prot = &udp_prot;
284 sk->sk_socket->ops = &inet_dgram_ops;
285 sk->sk_family = PF_INET;
287 opt = xchg(&np->opt, NULL);
288 if (opt)
289 sock_kfree_s(sk, opt, opt->tot_len);
290 pktopt = xchg(&np->pktoptions, NULL);
291 if (pktopt)
292 kfree_skb(pktopt);
294 sk->sk_destruct = inet_sock_destruct;
296 * ... and add it to the refcnt debug socks count
297 * in the new family. -acme
299 sk_refcnt_debug_inc(sk);
300 module_put(THIS_MODULE);
301 retv = 0;
302 break;
304 goto e_inval;
306 case IPV6_V6ONLY:
307 if (inet_sk(sk)->num)
308 goto e_inval;
309 np->ipv6only = valbool;
310 retv = 0;
311 break;
313 case IPV6_RECVPKTINFO:
314 np->rxopt.bits.rxinfo = valbool;
315 retv = 0;
316 break;
318 case IPV6_2292PKTINFO:
319 np->rxopt.bits.rxoinfo = valbool;
320 retv = 0;
321 break;
323 case IPV6_RECVHOPLIMIT:
324 np->rxopt.bits.rxhlim = valbool;
325 retv = 0;
326 break;
328 case IPV6_2292HOPLIMIT:
329 np->rxopt.bits.rxohlim = valbool;
330 retv = 0;
331 break;
333 case IPV6_RECVRTHDR:
334 if (val < 0 || val > 2)
335 goto e_inval;
336 np->rxopt.bits.srcrt = val;
337 retv = 0;
338 break;
340 case IPV6_2292RTHDR:
341 if (val < 0 || val > 2)
342 goto e_inval;
343 np->rxopt.bits.osrcrt = val;
344 retv = 0;
345 break;
347 case IPV6_RECVHOPOPTS:
348 np->rxopt.bits.hopopts = valbool;
349 retv = 0;
350 break;
352 case IPV6_2292HOPOPTS:
353 np->rxopt.bits.ohopopts = valbool;
354 retv = 0;
355 break;
357 case IPV6_RECVDSTOPTS:
358 np->rxopt.bits.dstopts = valbool;
359 retv = 0;
360 break;
362 case IPV6_2292DSTOPTS:
363 np->rxopt.bits.odstopts = valbool;
364 retv = 0;
365 break;
367 case IPV6_TCLASS:
368 if (val < -1 || val > 0xff)
369 goto e_inval;
370 np->tclass = val;
371 retv = 0;
372 break;
374 case IPV6_RECVTCLASS:
375 np->rxopt.bits.rxtclass = valbool;
376 retv = 0;
377 break;
379 case IPV6_FLOWINFO:
380 np->rxopt.bits.rxflow = valbool;
381 retv = 0;
382 break;
384 case IPV6_HOPOPTS:
385 case IPV6_RTHDRDSTOPTS:
386 case IPV6_RTHDR:
387 case IPV6_DSTOPTS:
389 struct ipv6_txoptions *opt;
390 if (optlen == 0)
391 optval = NULL;
393 /* hop-by-hop / destination options are privileged option */
394 retv = -EPERM;
395 if (optname != IPV6_RTHDR && !capable(CAP_NET_RAW))
396 break;
398 retv = -EINVAL;
399 if (optlen & 0x7 || optlen > 8 * 255)
400 break;
402 opt = ipv6_renew_options(sk, np->opt, optname,
403 (struct ipv6_opt_hdr __user *)optval,
404 optlen);
405 if (IS_ERR(opt)) {
406 retv = PTR_ERR(opt);
407 break;
410 /* routing header option needs extra check */
411 if (optname == IPV6_RTHDR && opt->srcrt) {
412 struct ipv6_rt_hdr *rthdr = opt->srcrt;
413 switch (rthdr->type) {
414 case IPV6_SRCRT_TYPE_0:
415 #ifdef CONFIG_IPV6_MIP6
416 case IPV6_SRCRT_TYPE_2:
417 #endif
418 break;
419 default:
420 goto sticky_done;
423 if ((rthdr->hdrlen & 1) ||
424 (rthdr->hdrlen >> 1) != rthdr->segments_left)
425 goto sticky_done;
428 retv = 0;
429 if (inet_sk(sk)->is_icsk) {
430 if (opt) {
431 struct inet_connection_sock *icsk = inet_csk(sk);
432 if (!((1 << sk->sk_state) &
433 (TCPF_LISTEN | TCPF_CLOSE))
434 && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
435 icsk->icsk_ext_hdr_len =
436 opt->opt_flen + opt->opt_nflen;
437 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
440 opt = xchg(&np->opt, opt);
441 sk_dst_reset(sk);
442 } else {
443 write_lock(&sk->sk_dst_lock);
444 opt = xchg(&np->opt, opt);
445 write_unlock(&sk->sk_dst_lock);
446 sk_dst_reset(sk);
448 sticky_done:
449 if (opt)
450 sock_kfree_s(sk, opt, opt->tot_len);
451 break;
454 case IPV6_2292PKTOPTIONS:
456 struct ipv6_txoptions *opt = NULL;
457 struct msghdr msg;
458 struct flowi fl;
459 int junk;
461 fl.fl6_flowlabel = 0;
462 fl.oif = sk->sk_bound_dev_if;
464 if (optlen == 0)
465 goto update;
467 /* 1K is probably excessive
468 * 1K is surely not enough, 2K per standard header is 16K.
470 retv = -EINVAL;
471 if (optlen > 64*1024)
472 break;
474 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
475 retv = -ENOBUFS;
476 if (opt == NULL)
477 break;
479 memset(opt, 0, sizeof(*opt));
480 opt->tot_len = sizeof(*opt) + optlen;
481 retv = -EFAULT;
482 if (copy_from_user(opt+1, optval, optlen))
483 goto done;
485 msg.msg_controllen = optlen;
486 msg.msg_control = (void*)(opt+1);
488 retv = datagram_send_ctl(&msg, &fl, opt, &junk, &junk);
489 if (retv)
490 goto done;
491 update:
492 retv = 0;
493 if (inet_sk(sk)->is_icsk) {
494 if (opt) {
495 struct inet_connection_sock *icsk = inet_csk(sk);
496 if (!((1 << sk->sk_state) &
497 (TCPF_LISTEN | TCPF_CLOSE))
498 && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
499 icsk->icsk_ext_hdr_len =
500 opt->opt_flen + opt->opt_nflen;
501 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
504 opt = xchg(&np->opt, opt);
505 sk_dst_reset(sk);
506 } else {
507 write_lock(&sk->sk_dst_lock);
508 opt = xchg(&np->opt, opt);
509 write_unlock(&sk->sk_dst_lock);
510 sk_dst_reset(sk);
513 done:
514 if (opt)
515 sock_kfree_s(sk, opt, opt->tot_len);
516 break;
518 case IPV6_UNICAST_HOPS:
519 if (val > 255 || val < -1)
520 goto e_inval;
521 np->hop_limit = val;
522 retv = 0;
523 break;
525 case IPV6_MULTICAST_HOPS:
526 if (sk->sk_type == SOCK_STREAM)
527 goto e_inval;
528 if (val > 255 || val < -1)
529 goto e_inval;
530 np->mcast_hops = val;
531 retv = 0;
532 break;
534 case IPV6_MULTICAST_LOOP:
535 np->mc_loop = valbool;
536 retv = 0;
537 break;
539 case IPV6_MULTICAST_IF:
540 if (sk->sk_type == SOCK_STREAM)
541 goto e_inval;
542 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
543 goto e_inval;
545 if (__dev_get_by_index(val) == NULL) {
546 retv = -ENODEV;
547 break;
549 np->mcast_oif = val;
550 retv = 0;
551 break;
552 case IPV6_ADD_MEMBERSHIP:
553 case IPV6_DROP_MEMBERSHIP:
555 struct ipv6_mreq mreq;
557 retv = -EFAULT;
558 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
559 break;
561 if (optname == IPV6_ADD_MEMBERSHIP)
562 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
563 else
564 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
565 break;
567 case IPV6_JOIN_ANYCAST:
568 case IPV6_LEAVE_ANYCAST:
570 struct ipv6_mreq mreq;
572 if (optlen != sizeof(struct ipv6_mreq))
573 goto e_inval;
575 retv = -EFAULT;
576 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
577 break;
579 if (optname == IPV6_JOIN_ANYCAST)
580 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
581 else
582 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
583 break;
585 case MCAST_JOIN_GROUP:
586 case MCAST_LEAVE_GROUP:
588 struct group_req greq;
589 struct sockaddr_in6 *psin6;
591 retv = -EFAULT;
592 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
593 break;
594 if (greq.gr_group.ss_family != AF_INET6) {
595 retv = -EADDRNOTAVAIL;
596 break;
598 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
599 if (optname == MCAST_JOIN_GROUP)
600 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
601 &psin6->sin6_addr);
602 else
603 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
604 &psin6->sin6_addr);
605 break;
607 case MCAST_JOIN_SOURCE_GROUP:
608 case MCAST_LEAVE_SOURCE_GROUP:
609 case MCAST_BLOCK_SOURCE:
610 case MCAST_UNBLOCK_SOURCE:
612 struct group_source_req greqs;
613 int omode, add;
615 if (optlen != sizeof(struct group_source_req))
616 goto e_inval;
617 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
618 retv = -EFAULT;
619 break;
621 if (greqs.gsr_group.ss_family != AF_INET6 ||
622 greqs.gsr_source.ss_family != AF_INET6) {
623 retv = -EADDRNOTAVAIL;
624 break;
626 if (optname == MCAST_BLOCK_SOURCE) {
627 omode = MCAST_EXCLUDE;
628 add = 1;
629 } else if (optname == MCAST_UNBLOCK_SOURCE) {
630 omode = MCAST_EXCLUDE;
631 add = 0;
632 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
633 struct sockaddr_in6 *psin6;
635 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
636 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
637 &psin6->sin6_addr);
638 /* prior join w/ different source is ok */
639 if (retv && retv != -EADDRINUSE)
640 break;
641 omode = MCAST_INCLUDE;
642 add = 1;
643 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
644 omode = MCAST_INCLUDE;
645 add = 0;
647 retv = ip6_mc_source(add, omode, sk, &greqs);
648 break;
650 case MCAST_MSFILTER:
652 extern int sysctl_mld_max_msf;
653 struct group_filter *gsf;
655 if (optlen < GROUP_FILTER_SIZE(0))
656 goto e_inval;
657 if (optlen > sysctl_optmem_max) {
658 retv = -ENOBUFS;
659 break;
661 gsf = kmalloc(optlen,GFP_KERNEL);
662 if (gsf == 0) {
663 retv = -ENOBUFS;
664 break;
666 retv = -EFAULT;
667 if (copy_from_user(gsf, optval, optlen)) {
668 kfree(gsf);
669 break;
671 /* numsrc >= (4G-140)/128 overflow in 32 bits */
672 if (gsf->gf_numsrc >= 0x1ffffffU ||
673 gsf->gf_numsrc > sysctl_mld_max_msf) {
674 kfree(gsf);
675 retv = -ENOBUFS;
676 break;
678 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
679 kfree(gsf);
680 retv = -EINVAL;
681 break;
683 retv = ip6_mc_msfilter(sk, gsf);
684 kfree(gsf);
686 break;
688 case IPV6_ROUTER_ALERT:
689 retv = ip6_ra_control(sk, val, NULL);
690 break;
691 case IPV6_MTU_DISCOVER:
692 if (val<0 || val>2)
693 goto e_inval;
694 np->pmtudisc = val;
695 retv = 0;
696 break;
697 case IPV6_MTU:
698 if (val && val < IPV6_MIN_MTU)
699 goto e_inval;
700 np->frag_size = val;
701 retv = 0;
702 break;
703 case IPV6_RECVERR:
704 np->recverr = valbool;
705 if (!val)
706 skb_queue_purge(&sk->sk_error_queue);
707 retv = 0;
708 break;
709 case IPV6_FLOWINFO_SEND:
710 np->sndflow = valbool;
711 retv = 0;
712 break;
713 case IPV6_FLOWLABEL_MGR:
714 retv = ipv6_flowlabel_opt(sk, optval, optlen);
715 break;
716 case IPV6_IPSEC_POLICY:
717 case IPV6_XFRM_POLICY:
718 retv = -EPERM;
719 if (!capable(CAP_NET_ADMIN))
720 break;
721 retv = xfrm_user_policy(sk, optname, optval, optlen);
722 break;
725 release_sock(sk);
727 return retv;
729 e_inval:
730 release_sock(sk);
731 return -EINVAL;
734 int ipv6_setsockopt(struct sock *sk, int level, int optname,
735 char __user *optval, int optlen)
737 int err;
739 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
740 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
742 if (level != SOL_IPV6)
743 return -ENOPROTOOPT;
745 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
746 #ifdef CONFIG_NETFILTER
747 /* we need to exclude all possible ENOPROTOOPTs except default case */
748 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
749 optname != IPV6_XFRM_POLICY) {
750 lock_sock(sk);
751 err = nf_setsockopt(sk, PF_INET6, optname, optval,
752 optlen);
753 release_sock(sk);
755 #endif
756 return err;
760 #ifdef CONFIG_COMPAT
761 int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
762 char __user *optval, int optlen)
764 int err;
766 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
767 if (udp_prot.compat_setsockopt != NULL)
768 return udp_prot.compat_setsockopt(sk, level, optname,
769 optval, optlen);
770 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
773 if (level != SOL_IPV6)
774 return -ENOPROTOOPT;
776 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
777 #ifdef CONFIG_NETFILTER
778 /* we need to exclude all possible ENOPROTOOPTs except default case */
779 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
780 optname != IPV6_XFRM_POLICY) {
781 lock_sock(sk);
782 err = compat_nf_setsockopt(sk, PF_INET6, optname,
783 optval, optlen);
784 release_sock(sk);
786 #endif
787 return err;
790 EXPORT_SYMBOL(compat_ipv6_setsockopt);
791 #endif
793 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_opt_hdr *hdr,
794 char __user *optval, int len)
796 if (!hdr)
797 return 0;
798 len = min_t(int, len, ipv6_optlen(hdr));
799 if (copy_to_user(optval, hdr, ipv6_optlen(hdr)))
800 return -EFAULT;
801 return len;
804 static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
805 char __user *optval, int __user *optlen)
807 struct ipv6_pinfo *np = inet6_sk(sk);
808 int len;
809 int val;
811 if (get_user(len, optlen))
812 return -EFAULT;
813 switch (optname) {
814 case IPV6_ADDRFORM:
815 if (sk->sk_protocol != IPPROTO_UDP &&
816 sk->sk_protocol != IPPROTO_TCP)
817 return -EINVAL;
818 if (sk->sk_state != TCP_ESTABLISHED)
819 return -ENOTCONN;
820 val = sk->sk_family;
821 break;
822 case MCAST_MSFILTER:
824 struct group_filter gsf;
825 int err;
827 if (len < GROUP_FILTER_SIZE(0))
828 return -EINVAL;
829 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
830 return -EFAULT;
831 lock_sock(sk);
832 err = ip6_mc_msfget(sk, &gsf,
833 (struct group_filter __user *)optval, optlen);
834 release_sock(sk);
835 return err;
838 case IPV6_2292PKTOPTIONS:
840 struct msghdr msg;
841 struct sk_buff *skb;
843 if (sk->sk_type != SOCK_STREAM)
844 return -ENOPROTOOPT;
846 msg.msg_control = optval;
847 msg.msg_controllen = len;
848 msg.msg_flags = 0;
850 lock_sock(sk);
851 skb = np->pktoptions;
852 if (skb)
853 atomic_inc(&skb->users);
854 release_sock(sk);
856 if (skb) {
857 int err = datagram_recv_ctl(sk, &msg, skb);
858 kfree_skb(skb);
859 if (err)
860 return err;
861 } else {
862 if (np->rxopt.bits.rxinfo) {
863 struct in6_pktinfo src_info;
864 src_info.ipi6_ifindex = np->mcast_oif;
865 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
866 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
868 if (np->rxopt.bits.rxhlim) {
869 int hlim = np->mcast_hops;
870 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
872 if (np->rxopt.bits.rxoinfo) {
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_2292PKTINFO, sizeof(src_info), &src_info);
878 if (np->rxopt.bits.rxohlim) {
879 int hlim = np->mcast_hops;
880 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
883 len -= msg.msg_controllen;
884 return put_user(len, optlen);
886 case IPV6_MTU:
888 struct dst_entry *dst;
889 val = 0;
890 lock_sock(sk);
891 dst = sk_dst_get(sk);
892 if (dst) {
893 val = dst_mtu(dst);
894 dst_release(dst);
896 release_sock(sk);
897 if (!val)
898 return -ENOTCONN;
899 break;
902 case IPV6_V6ONLY:
903 val = np->ipv6only;
904 break;
906 case IPV6_RECVPKTINFO:
907 val = np->rxopt.bits.rxinfo;
908 break;
910 case IPV6_2292PKTINFO:
911 val = np->rxopt.bits.rxoinfo;
912 break;
914 case IPV6_RECVHOPLIMIT:
915 val = np->rxopt.bits.rxhlim;
916 break;
918 case IPV6_2292HOPLIMIT:
919 val = np->rxopt.bits.rxohlim;
920 break;
922 case IPV6_RECVRTHDR:
923 val = np->rxopt.bits.srcrt;
924 break;
926 case IPV6_2292RTHDR:
927 val = np->rxopt.bits.osrcrt;
928 break;
930 case IPV6_HOPOPTS:
931 case IPV6_RTHDRDSTOPTS:
932 case IPV6_RTHDR:
933 case IPV6_DSTOPTS:
936 lock_sock(sk);
937 len = ipv6_getsockopt_sticky(sk, np->opt->hopopt,
938 optval, len);
939 release_sock(sk);
940 return put_user(len, optlen);
943 case IPV6_RECVHOPOPTS:
944 val = np->rxopt.bits.hopopts;
945 break;
947 case IPV6_2292HOPOPTS:
948 val = np->rxopt.bits.ohopopts;
949 break;
951 case IPV6_RECVDSTOPTS:
952 val = np->rxopt.bits.dstopts;
953 break;
955 case IPV6_2292DSTOPTS:
956 val = np->rxopt.bits.odstopts;
957 break;
959 case IPV6_TCLASS:
960 val = np->tclass;
961 if (val < 0)
962 val = 0;
963 break;
965 case IPV6_RECVTCLASS:
966 val = np->rxopt.bits.rxtclass;
967 break;
969 case IPV6_FLOWINFO:
970 val = np->rxopt.bits.rxflow;
971 break;
973 case IPV6_UNICAST_HOPS:
974 val = np->hop_limit;
975 break;
977 case IPV6_MULTICAST_HOPS:
978 val = np->mcast_hops;
979 break;
981 case IPV6_MULTICAST_LOOP:
982 val = np->mc_loop;
983 break;
985 case IPV6_MULTICAST_IF:
986 val = np->mcast_oif;
987 break;
989 case IPV6_MTU_DISCOVER:
990 val = np->pmtudisc;
991 break;
993 case IPV6_RECVERR:
994 val = np->recverr;
995 break;
997 case IPV6_FLOWINFO_SEND:
998 val = np->sndflow;
999 break;
1001 default:
1002 return -EINVAL;
1004 len = min_t(unsigned int, sizeof(int), len);
1005 if(put_user(len, optlen))
1006 return -EFAULT;
1007 if(copy_to_user(optval,&val,len))
1008 return -EFAULT;
1009 return 0;
1012 int ipv6_getsockopt(struct sock *sk, int level, int optname,
1013 char __user *optval, int __user *optlen)
1015 int err;
1017 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1018 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1020 if(level != SOL_IPV6)
1021 return -ENOPROTOOPT;
1023 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1024 #ifdef CONFIG_NETFILTER
1025 /* we need to exclude all possible EINVALs except default case */
1026 if (err == -EINVAL && optname != IPV6_ADDRFORM &&
1027 optname != MCAST_MSFILTER) {
1028 int len;
1030 if (get_user(len, optlen))
1031 return -EFAULT;
1033 lock_sock(sk);
1034 err = nf_getsockopt(sk, PF_INET6, optname, optval,
1035 &len);
1036 release_sock(sk);
1037 if (err >= 0)
1038 err = put_user(len, optlen);
1040 #endif
1041 return err;
1044 #ifdef CONFIG_COMPAT
1045 int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
1046 char __user *optval, int __user *optlen)
1048 int err;
1050 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
1051 if (udp_prot.compat_getsockopt != NULL)
1052 return udp_prot.compat_getsockopt(sk, level, optname,
1053 optval, optlen);
1054 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1057 if (level != SOL_IPV6)
1058 return -ENOPROTOOPT;
1060 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1061 #ifdef CONFIG_NETFILTER
1062 /* we need to exclude all possible EINVALs except default case */
1063 if (err == -EINVAL && optname != IPV6_ADDRFORM &&
1064 optname != MCAST_MSFILTER) {
1065 int len;
1067 if (get_user(len, optlen))
1068 return -EFAULT;
1070 lock_sock(sk);
1071 err = compat_nf_getsockopt(sk, PF_INET6,
1072 optname, optval, &len);
1073 release_sock(sk);
1074 if (err >= 0)
1075 err = put_user(len, optlen);
1077 #endif
1078 return err;
1081 EXPORT_SYMBOL(compat_ipv6_getsockopt);
1082 #endif
1084 void __init ipv6_packet_init(void)
1086 dev_add_pack(&ipv6_packet_type);
1089 void ipv6_packet_cleanup(void)
1091 dev_remove_pack(&ipv6_packet_type);