[PATCH] oom-kill: update comments to reflect current code
[pv_ops_mirror.git] / net / ipv6 / ipv6_sockglue.c
blob4f3bb7fcc8b509ac71615e7a8c0f543d95349854
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 (unlikely(skb_shinfo(skb)->gso_type &
127 ~(SKB_GSO_UDP |
128 SKB_GSO_DODGY |
129 SKB_GSO_TCP_ECN |
130 SKB_GSO_TCPV6 |
131 0)))
132 goto out;
134 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
135 goto out;
137 ipv6h = skb->nh.ipv6h;
138 __skb_pull(skb, sizeof(*ipv6h));
139 segs = ERR_PTR(-EPROTONOSUPPORT);
141 rcu_read_lock();
142 ops = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
143 if (likely(ops && ops->gso_segment)) {
144 skb->h.raw = skb->data;
145 segs = ops->gso_segment(skb, features);
147 rcu_read_unlock();
149 if (unlikely(IS_ERR(segs)))
150 goto out;
152 for (skb = segs; skb; skb = skb->next) {
153 ipv6h = skb->nh.ipv6h;
154 ipv6h->payload_len = htons(skb->len - skb->mac_len -
155 sizeof(*ipv6h));
158 out:
159 return segs;
162 static struct packet_type ipv6_packet_type = {
163 .type = __constant_htons(ETH_P_IPV6),
164 .func = ipv6_rcv,
165 .gso_send_check = ipv6_gso_send_check,
166 .gso_segment = ipv6_gso_segment,
169 struct ip6_ra_chain *ip6_ra_chain;
170 DEFINE_RWLOCK(ip6_ra_lock);
172 int ip6_ra_control(struct sock *sk, int sel, void (*destructor)(struct sock *))
174 struct ip6_ra_chain *ra, *new_ra, **rap;
176 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
177 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->num != IPPROTO_RAW)
178 return -EINVAL;
180 new_ra = (sel>=0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
182 write_lock_bh(&ip6_ra_lock);
183 for (rap = &ip6_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
184 if (ra->sk == sk) {
185 if (sel>=0) {
186 write_unlock_bh(&ip6_ra_lock);
187 kfree(new_ra);
188 return -EADDRINUSE;
191 *rap = ra->next;
192 write_unlock_bh(&ip6_ra_lock);
194 if (ra->destructor)
195 ra->destructor(sk);
196 sock_put(sk);
197 kfree(ra);
198 return 0;
201 if (new_ra == NULL) {
202 write_unlock_bh(&ip6_ra_lock);
203 return -ENOBUFS;
205 new_ra->sk = sk;
206 new_ra->sel = sel;
207 new_ra->destructor = destructor;
208 new_ra->next = ra;
209 *rap = new_ra;
210 sock_hold(sk);
211 write_unlock_bh(&ip6_ra_lock);
212 return 0;
215 static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
216 char __user *optval, int optlen)
218 struct ipv6_pinfo *np = inet6_sk(sk);
219 int val, valbool;
220 int retv = -ENOPROTOOPT;
222 if (optval == NULL)
223 val=0;
224 else if (get_user(val, (int __user *) optval))
225 return -EFAULT;
227 valbool = (val!=0);
229 lock_sock(sk);
231 switch (optname) {
233 case IPV6_ADDRFORM:
234 if (val == PF_INET) {
235 struct ipv6_txoptions *opt;
236 struct sk_buff *pktopt;
238 if (sk->sk_protocol != IPPROTO_UDP &&
239 sk->sk_protocol != IPPROTO_TCP)
240 break;
242 if (sk->sk_state != TCP_ESTABLISHED) {
243 retv = -ENOTCONN;
244 break;
247 if (ipv6_only_sock(sk) ||
248 !(ipv6_addr_type(&np->daddr) & IPV6_ADDR_MAPPED)) {
249 retv = -EADDRNOTAVAIL;
250 break;
253 fl6_free_socklist(sk);
254 ipv6_sock_mc_close(sk);
257 * Sock is moving from IPv6 to IPv4 (sk_prot), so
258 * remove it from the refcnt debug socks count in the
259 * original family...
261 sk_refcnt_debug_dec(sk);
263 if (sk->sk_protocol == IPPROTO_TCP) {
264 struct inet_connection_sock *icsk = inet_csk(sk);
266 local_bh_disable();
267 sock_prot_dec_use(sk->sk_prot);
268 sock_prot_inc_use(&tcp_prot);
269 local_bh_enable();
270 sk->sk_prot = &tcp_prot;
271 icsk->icsk_af_ops = &ipv4_specific;
272 sk->sk_socket->ops = &inet_stream_ops;
273 sk->sk_family = PF_INET;
274 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
275 } else {
276 local_bh_disable();
277 sock_prot_dec_use(sk->sk_prot);
278 sock_prot_inc_use(&udp_prot);
279 local_bh_enable();
280 sk->sk_prot = &udp_prot;
281 sk->sk_socket->ops = &inet_dgram_ops;
282 sk->sk_family = PF_INET;
284 opt = xchg(&np->opt, NULL);
285 if (opt)
286 sock_kfree_s(sk, opt, opt->tot_len);
287 pktopt = xchg(&np->pktoptions, NULL);
288 if (pktopt)
289 kfree_skb(pktopt);
291 sk->sk_destruct = inet_sock_destruct;
293 * ... and add it to the refcnt debug socks count
294 * in the new family. -acme
296 sk_refcnt_debug_inc(sk);
297 module_put(THIS_MODULE);
298 retv = 0;
299 break;
301 goto e_inval;
303 case IPV6_V6ONLY:
304 if (inet_sk(sk)->num)
305 goto e_inval;
306 np->ipv6only = valbool;
307 retv = 0;
308 break;
310 case IPV6_RECVPKTINFO:
311 np->rxopt.bits.rxinfo = valbool;
312 retv = 0;
313 break;
315 case IPV6_2292PKTINFO:
316 np->rxopt.bits.rxoinfo = valbool;
317 retv = 0;
318 break;
320 case IPV6_RECVHOPLIMIT:
321 np->rxopt.bits.rxhlim = valbool;
322 retv = 0;
323 break;
325 case IPV6_2292HOPLIMIT:
326 np->rxopt.bits.rxohlim = valbool;
327 retv = 0;
328 break;
330 case IPV6_RECVRTHDR:
331 if (val < 0 || val > 2)
332 goto e_inval;
333 np->rxopt.bits.srcrt = val;
334 retv = 0;
335 break;
337 case IPV6_2292RTHDR:
338 if (val < 0 || val > 2)
339 goto e_inval;
340 np->rxopt.bits.osrcrt = val;
341 retv = 0;
342 break;
344 case IPV6_RECVHOPOPTS:
345 np->rxopt.bits.hopopts = valbool;
346 retv = 0;
347 break;
349 case IPV6_2292HOPOPTS:
350 np->rxopt.bits.ohopopts = valbool;
351 retv = 0;
352 break;
354 case IPV6_RECVDSTOPTS:
355 np->rxopt.bits.dstopts = valbool;
356 retv = 0;
357 break;
359 case IPV6_2292DSTOPTS:
360 np->rxopt.bits.odstopts = valbool;
361 retv = 0;
362 break;
364 case IPV6_TCLASS:
365 if (val < -1 || val > 0xff)
366 goto e_inval;
367 np->tclass = val;
368 retv = 0;
369 break;
371 case IPV6_RECVTCLASS:
372 np->rxopt.bits.rxtclass = valbool;
373 retv = 0;
374 break;
376 case IPV6_FLOWINFO:
377 np->rxopt.bits.rxflow = valbool;
378 retv = 0;
379 break;
381 case IPV6_HOPOPTS:
382 case IPV6_RTHDRDSTOPTS:
383 case IPV6_RTHDR:
384 case IPV6_DSTOPTS:
386 struct ipv6_txoptions *opt;
387 if (optlen == 0)
388 optval = NULL;
390 /* hop-by-hop / destination options are privileged option */
391 retv = -EPERM;
392 if (optname != IPV6_RTHDR && !capable(CAP_NET_RAW))
393 break;
395 retv = -EINVAL;
396 if (optlen & 0x7 || optlen > 8 * 255)
397 break;
399 opt = ipv6_renew_options(sk, np->opt, optname,
400 (struct ipv6_opt_hdr __user *)optval,
401 optlen);
402 if (IS_ERR(opt)) {
403 retv = PTR_ERR(opt);
404 break;
407 /* routing header option needs extra check */
408 if (optname == IPV6_RTHDR && opt->srcrt) {
409 struct ipv6_rt_hdr *rthdr = opt->srcrt;
410 switch (rthdr->type) {
411 case IPV6_SRCRT_TYPE_0:
412 #ifdef CONFIG_IPV6_MIP6
413 case IPV6_SRCRT_TYPE_2:
414 #endif
415 break;
416 default:
417 goto sticky_done;
420 if ((rthdr->hdrlen & 1) ||
421 (rthdr->hdrlen >> 1) != rthdr->segments_left)
422 goto sticky_done;
425 retv = 0;
426 if (inet_sk(sk)->is_icsk) {
427 if (opt) {
428 struct inet_connection_sock *icsk = inet_csk(sk);
429 if (!((1 << sk->sk_state) &
430 (TCPF_LISTEN | TCPF_CLOSE))
431 && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
432 icsk->icsk_ext_hdr_len =
433 opt->opt_flen + opt->opt_nflen;
434 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
437 opt = xchg(&np->opt, opt);
438 sk_dst_reset(sk);
439 } else {
440 write_lock(&sk->sk_dst_lock);
441 opt = xchg(&np->opt, opt);
442 write_unlock(&sk->sk_dst_lock);
443 sk_dst_reset(sk);
445 sticky_done:
446 if (opt)
447 sock_kfree_s(sk, opt, opt->tot_len);
448 break;
451 case IPV6_2292PKTOPTIONS:
453 struct ipv6_txoptions *opt = NULL;
454 struct msghdr msg;
455 struct flowi fl;
456 int junk;
458 fl.fl6_flowlabel = 0;
459 fl.oif = sk->sk_bound_dev_if;
461 if (optlen == 0)
462 goto update;
464 /* 1K is probably excessive
465 * 1K is surely not enough, 2K per standard header is 16K.
467 retv = -EINVAL;
468 if (optlen > 64*1024)
469 break;
471 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
472 retv = -ENOBUFS;
473 if (opt == NULL)
474 break;
476 memset(opt, 0, sizeof(*opt));
477 opt->tot_len = sizeof(*opt) + optlen;
478 retv = -EFAULT;
479 if (copy_from_user(opt+1, optval, optlen))
480 goto done;
482 msg.msg_controllen = optlen;
483 msg.msg_control = (void*)(opt+1);
485 retv = datagram_send_ctl(&msg, &fl, opt, &junk, &junk);
486 if (retv)
487 goto done;
488 update:
489 retv = 0;
490 if (inet_sk(sk)->is_icsk) {
491 if (opt) {
492 struct inet_connection_sock *icsk = inet_csk(sk);
493 if (!((1 << sk->sk_state) &
494 (TCPF_LISTEN | TCPF_CLOSE))
495 && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
496 icsk->icsk_ext_hdr_len =
497 opt->opt_flen + opt->opt_nflen;
498 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
501 opt = xchg(&np->opt, opt);
502 sk_dst_reset(sk);
503 } else {
504 write_lock(&sk->sk_dst_lock);
505 opt = xchg(&np->opt, opt);
506 write_unlock(&sk->sk_dst_lock);
507 sk_dst_reset(sk);
510 done:
511 if (opt)
512 sock_kfree_s(sk, opt, opt->tot_len);
513 break;
515 case IPV6_UNICAST_HOPS:
516 if (val > 255 || val < -1)
517 goto e_inval;
518 np->hop_limit = val;
519 retv = 0;
520 break;
522 case IPV6_MULTICAST_HOPS:
523 if (sk->sk_type == SOCK_STREAM)
524 goto e_inval;
525 if (val > 255 || val < -1)
526 goto e_inval;
527 np->mcast_hops = val;
528 retv = 0;
529 break;
531 case IPV6_MULTICAST_LOOP:
532 np->mc_loop = valbool;
533 retv = 0;
534 break;
536 case IPV6_MULTICAST_IF:
537 if (sk->sk_type == SOCK_STREAM)
538 goto e_inval;
539 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
540 goto e_inval;
542 if (__dev_get_by_index(val) == NULL) {
543 retv = -ENODEV;
544 break;
546 np->mcast_oif = val;
547 retv = 0;
548 break;
549 case IPV6_ADD_MEMBERSHIP:
550 case IPV6_DROP_MEMBERSHIP:
552 struct ipv6_mreq mreq;
554 retv = -EFAULT;
555 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
556 break;
558 if (optname == IPV6_ADD_MEMBERSHIP)
559 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
560 else
561 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
562 break;
564 case IPV6_JOIN_ANYCAST:
565 case IPV6_LEAVE_ANYCAST:
567 struct ipv6_mreq mreq;
569 if (optlen != sizeof(struct ipv6_mreq))
570 goto e_inval;
572 retv = -EFAULT;
573 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
574 break;
576 if (optname == IPV6_JOIN_ANYCAST)
577 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
578 else
579 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
580 break;
582 case MCAST_JOIN_GROUP:
583 case MCAST_LEAVE_GROUP:
585 struct group_req greq;
586 struct sockaddr_in6 *psin6;
588 retv = -EFAULT;
589 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
590 break;
591 if (greq.gr_group.ss_family != AF_INET6) {
592 retv = -EADDRNOTAVAIL;
593 break;
595 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
596 if (optname == MCAST_JOIN_GROUP)
597 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
598 &psin6->sin6_addr);
599 else
600 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
601 &psin6->sin6_addr);
602 break;
604 case MCAST_JOIN_SOURCE_GROUP:
605 case MCAST_LEAVE_SOURCE_GROUP:
606 case MCAST_BLOCK_SOURCE:
607 case MCAST_UNBLOCK_SOURCE:
609 struct group_source_req greqs;
610 int omode, add;
612 if (optlen != sizeof(struct group_source_req))
613 goto e_inval;
614 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
615 retv = -EFAULT;
616 break;
618 if (greqs.gsr_group.ss_family != AF_INET6 ||
619 greqs.gsr_source.ss_family != AF_INET6) {
620 retv = -EADDRNOTAVAIL;
621 break;
623 if (optname == MCAST_BLOCK_SOURCE) {
624 omode = MCAST_EXCLUDE;
625 add = 1;
626 } else if (optname == MCAST_UNBLOCK_SOURCE) {
627 omode = MCAST_EXCLUDE;
628 add = 0;
629 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
630 struct sockaddr_in6 *psin6;
632 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
633 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
634 &psin6->sin6_addr);
635 /* prior join w/ different source is ok */
636 if (retv && retv != -EADDRINUSE)
637 break;
638 omode = MCAST_INCLUDE;
639 add = 1;
640 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
641 omode = MCAST_INCLUDE;
642 add = 0;
644 retv = ip6_mc_source(add, omode, sk, &greqs);
645 break;
647 case MCAST_MSFILTER:
649 extern int sysctl_mld_max_msf;
650 struct group_filter *gsf;
652 if (optlen < GROUP_FILTER_SIZE(0))
653 goto e_inval;
654 if (optlen > sysctl_optmem_max) {
655 retv = -ENOBUFS;
656 break;
658 gsf = kmalloc(optlen,GFP_KERNEL);
659 if (gsf == 0) {
660 retv = -ENOBUFS;
661 break;
663 retv = -EFAULT;
664 if (copy_from_user(gsf, optval, optlen)) {
665 kfree(gsf);
666 break;
668 /* numsrc >= (4G-140)/128 overflow in 32 bits */
669 if (gsf->gf_numsrc >= 0x1ffffffU ||
670 gsf->gf_numsrc > sysctl_mld_max_msf) {
671 kfree(gsf);
672 retv = -ENOBUFS;
673 break;
675 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
676 kfree(gsf);
677 retv = -EINVAL;
678 break;
680 retv = ip6_mc_msfilter(sk, gsf);
681 kfree(gsf);
683 break;
685 case IPV6_ROUTER_ALERT:
686 retv = ip6_ra_control(sk, val, NULL);
687 break;
688 case IPV6_MTU_DISCOVER:
689 if (val<0 || val>2)
690 goto e_inval;
691 np->pmtudisc = val;
692 retv = 0;
693 break;
694 case IPV6_MTU:
695 if (val && val < IPV6_MIN_MTU)
696 goto e_inval;
697 np->frag_size = val;
698 retv = 0;
699 break;
700 case IPV6_RECVERR:
701 np->recverr = valbool;
702 if (!val)
703 skb_queue_purge(&sk->sk_error_queue);
704 retv = 0;
705 break;
706 case IPV6_FLOWINFO_SEND:
707 np->sndflow = valbool;
708 retv = 0;
709 break;
710 case IPV6_FLOWLABEL_MGR:
711 retv = ipv6_flowlabel_opt(sk, optval, optlen);
712 break;
713 case IPV6_IPSEC_POLICY:
714 case IPV6_XFRM_POLICY:
715 retv = -EPERM;
716 if (!capable(CAP_NET_ADMIN))
717 break;
718 retv = xfrm_user_policy(sk, optname, optval, optlen);
719 break;
722 release_sock(sk);
724 return retv;
726 e_inval:
727 release_sock(sk);
728 return -EINVAL;
731 int ipv6_setsockopt(struct sock *sk, int level, int optname,
732 char __user *optval, int optlen)
734 int err;
736 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
737 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
739 if (level != SOL_IPV6)
740 return -ENOPROTOOPT;
742 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
743 #ifdef CONFIG_NETFILTER
744 /* we need to exclude all possible ENOPROTOOPTs except default case */
745 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
746 optname != IPV6_XFRM_POLICY) {
747 lock_sock(sk);
748 err = nf_setsockopt(sk, PF_INET6, optname, optval,
749 optlen);
750 release_sock(sk);
752 #endif
753 return err;
757 #ifdef CONFIG_COMPAT
758 int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
759 char __user *optval, int optlen)
761 int err;
763 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
764 if (udp_prot.compat_setsockopt != NULL)
765 return udp_prot.compat_setsockopt(sk, level, optname,
766 optval, optlen);
767 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
770 if (level != SOL_IPV6)
771 return -ENOPROTOOPT;
773 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
774 #ifdef CONFIG_NETFILTER
775 /* we need to exclude all possible ENOPROTOOPTs except default case */
776 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
777 optname != IPV6_XFRM_POLICY) {
778 lock_sock(sk);
779 err = compat_nf_setsockopt(sk, PF_INET6, optname,
780 optval, optlen);
781 release_sock(sk);
783 #endif
784 return err;
787 EXPORT_SYMBOL(compat_ipv6_setsockopt);
788 #endif
790 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_opt_hdr *hdr,
791 char __user *optval, int len)
793 if (!hdr)
794 return 0;
795 len = min_t(int, len, ipv6_optlen(hdr));
796 if (copy_to_user(optval, hdr, ipv6_optlen(hdr)))
797 return -EFAULT;
798 return len;
801 static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
802 char __user *optval, int __user *optlen)
804 struct ipv6_pinfo *np = inet6_sk(sk);
805 int len;
806 int val;
808 if (get_user(len, optlen))
809 return -EFAULT;
810 switch (optname) {
811 case IPV6_ADDRFORM:
812 if (sk->sk_protocol != IPPROTO_UDP &&
813 sk->sk_protocol != IPPROTO_TCP)
814 return -EINVAL;
815 if (sk->sk_state != TCP_ESTABLISHED)
816 return -ENOTCONN;
817 val = sk->sk_family;
818 break;
819 case MCAST_MSFILTER:
821 struct group_filter gsf;
822 int err;
824 if (len < GROUP_FILTER_SIZE(0))
825 return -EINVAL;
826 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
827 return -EFAULT;
828 lock_sock(sk);
829 err = ip6_mc_msfget(sk, &gsf,
830 (struct group_filter __user *)optval, optlen);
831 release_sock(sk);
832 return err;
835 case IPV6_2292PKTOPTIONS:
837 struct msghdr msg;
838 struct sk_buff *skb;
840 if (sk->sk_type != SOCK_STREAM)
841 return -ENOPROTOOPT;
843 msg.msg_control = optval;
844 msg.msg_controllen = len;
845 msg.msg_flags = 0;
847 lock_sock(sk);
848 skb = np->pktoptions;
849 if (skb)
850 atomic_inc(&skb->users);
851 release_sock(sk);
853 if (skb) {
854 int err = datagram_recv_ctl(sk, &msg, skb);
855 kfree_skb(skb);
856 if (err)
857 return err;
858 } else {
859 if (np->rxopt.bits.rxinfo) {
860 struct in6_pktinfo src_info;
861 src_info.ipi6_ifindex = np->mcast_oif;
862 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
863 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
865 if (np->rxopt.bits.rxhlim) {
866 int hlim = np->mcast_hops;
867 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
869 if (np->rxopt.bits.rxoinfo) {
870 struct in6_pktinfo src_info;
871 src_info.ipi6_ifindex = np->mcast_oif;
872 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
873 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
875 if (np->rxopt.bits.rxohlim) {
876 int hlim = np->mcast_hops;
877 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
880 len -= msg.msg_controllen;
881 return put_user(len, optlen);
883 case IPV6_MTU:
885 struct dst_entry *dst;
886 val = 0;
887 lock_sock(sk);
888 dst = sk_dst_get(sk);
889 if (dst) {
890 val = dst_mtu(dst);
891 dst_release(dst);
893 release_sock(sk);
894 if (!val)
895 return -ENOTCONN;
896 break;
899 case IPV6_V6ONLY:
900 val = np->ipv6only;
901 break;
903 case IPV6_RECVPKTINFO:
904 val = np->rxopt.bits.rxinfo;
905 break;
907 case IPV6_2292PKTINFO:
908 val = np->rxopt.bits.rxoinfo;
909 break;
911 case IPV6_RECVHOPLIMIT:
912 val = np->rxopt.bits.rxhlim;
913 break;
915 case IPV6_2292HOPLIMIT:
916 val = np->rxopt.bits.rxohlim;
917 break;
919 case IPV6_RECVRTHDR:
920 val = np->rxopt.bits.srcrt;
921 break;
923 case IPV6_2292RTHDR:
924 val = np->rxopt.bits.osrcrt;
925 break;
927 case IPV6_HOPOPTS:
928 case IPV6_RTHDRDSTOPTS:
929 case IPV6_RTHDR:
930 case IPV6_DSTOPTS:
933 lock_sock(sk);
934 len = ipv6_getsockopt_sticky(sk, np->opt->hopopt,
935 optval, len);
936 release_sock(sk);
937 return put_user(len, optlen);
940 case IPV6_RECVHOPOPTS:
941 val = np->rxopt.bits.hopopts;
942 break;
944 case IPV6_2292HOPOPTS:
945 val = np->rxopt.bits.ohopopts;
946 break;
948 case IPV6_RECVDSTOPTS:
949 val = np->rxopt.bits.dstopts;
950 break;
952 case IPV6_2292DSTOPTS:
953 val = np->rxopt.bits.odstopts;
954 break;
956 case IPV6_TCLASS:
957 val = np->tclass;
958 if (val < 0)
959 val = 0;
960 break;
962 case IPV6_RECVTCLASS:
963 val = np->rxopt.bits.rxtclass;
964 break;
966 case IPV6_FLOWINFO:
967 val = np->rxopt.bits.rxflow;
968 break;
970 case IPV6_UNICAST_HOPS:
971 val = np->hop_limit;
972 break;
974 case IPV6_MULTICAST_HOPS:
975 val = np->mcast_hops;
976 break;
978 case IPV6_MULTICAST_LOOP:
979 val = np->mc_loop;
980 break;
982 case IPV6_MULTICAST_IF:
983 val = np->mcast_oif;
984 break;
986 case IPV6_MTU_DISCOVER:
987 val = np->pmtudisc;
988 break;
990 case IPV6_RECVERR:
991 val = np->recverr;
992 break;
994 case IPV6_FLOWINFO_SEND:
995 val = np->sndflow;
996 break;
998 default:
999 return -EINVAL;
1001 len = min_t(unsigned int, sizeof(int), len);
1002 if(put_user(len, optlen))
1003 return -EFAULT;
1004 if(copy_to_user(optval,&val,len))
1005 return -EFAULT;
1006 return 0;
1009 int ipv6_getsockopt(struct sock *sk, int level, int optname,
1010 char __user *optval, int __user *optlen)
1012 int err;
1014 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1015 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1017 if(level != SOL_IPV6)
1018 return -ENOPROTOOPT;
1020 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1021 #ifdef CONFIG_NETFILTER
1022 /* we need to exclude all possible EINVALs except default case */
1023 if (err == -EINVAL && optname != IPV6_ADDRFORM &&
1024 optname != MCAST_MSFILTER) {
1025 int len;
1027 if (get_user(len, optlen))
1028 return -EFAULT;
1030 lock_sock(sk);
1031 err = nf_getsockopt(sk, PF_INET6, optname, optval,
1032 &len);
1033 release_sock(sk);
1034 if (err >= 0)
1035 err = put_user(len, optlen);
1037 #endif
1038 return err;
1041 #ifdef CONFIG_COMPAT
1042 int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
1043 char __user *optval, int __user *optlen)
1045 int err;
1047 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
1048 if (udp_prot.compat_getsockopt != NULL)
1049 return udp_prot.compat_getsockopt(sk, level, optname,
1050 optval, optlen);
1051 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1054 if (level != SOL_IPV6)
1055 return -ENOPROTOOPT;
1057 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1058 #ifdef CONFIG_NETFILTER
1059 /* we need to exclude all possible EINVALs except default case */
1060 if (err == -EINVAL && optname != IPV6_ADDRFORM &&
1061 optname != MCAST_MSFILTER) {
1062 int len;
1064 if (get_user(len, optlen))
1065 return -EFAULT;
1067 lock_sock(sk);
1068 err = compat_nf_getsockopt(sk, PF_INET6,
1069 optname, optval, &len);
1070 release_sock(sk);
1071 if (err >= 0)
1072 err = put_user(len, optlen);
1074 #endif
1075 return err;
1078 EXPORT_SYMBOL(compat_ipv6_getsockopt);
1079 #endif
1081 void __init ipv6_packet_init(void)
1083 dev_add_pack(&ipv6_packet_type);
1086 void ipv6_packet_cleanup(void)
1088 dev_remove_pack(&ipv6_packet_type);