ia64/linux-2.6.18-xen.hg

view net/ipv6/ipv6_sockglue.c @ 673:3161879fdf22

[IA64] xencomm: support XENMEM_add_to_physmap and XENMEM_remove_from_phsymap

support XENMEM_add_to_physmap and XENMEM_remove_from_phsymap.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
author Isaku Yamahata <yamahata@valinux.co.jp>
date Tue Sep 16 21:26:15 2008 +0900 (2008-09-16)
parents 3e8752eb6d9c
children
line source
1 /*
2 * IPv6 BSD socket options interface
3 * Linux INET6 implementation
4 *
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 *
8 * Based on linux/net/ipv4/ip_sockglue.c
9 *
10 * $Id: ipv6_sockglue.c,v 1.41 2002/02/01 22:01:04 davem Exp $
11 *
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.
16 *
17 * FIXME: Make the setsockopt code POSIX compliant: That is
18 *
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
22 *
23 * Changes:
24 * David L Stevens <dlstevens@us.ibm.com>:
25 * - added multicast source filtering API for MLDv2
26 */
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)
62 {
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;
77 }
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);
90 }
92 return ops;
93 }
95 static int ipv6_gso_send_check(struct sk_buff *skb)
96 {
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);
113 }
114 rcu_read_unlock();
116 out:
117 return err;
118 }
120 static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb, int features)
121 {
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);
149 }
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));
159 }
161 out:
162 return segs;
163 }
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,
170 };
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 *))
176 {
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;
192 }
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;
202 }
203 }
204 if (new_ra == NULL) {
205 write_unlock_bh(&ip6_ra_lock);
206 return -ENOBUFS;
207 }
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;
216 }
218 static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
219 char __user *optval, int optlen)
220 {
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;
248 }
250 if (ipv6_only_sock(sk) ||
251 !(ipv6_addr_type(&np->daddr) & IPV6_ADDR_MAPPED)) {
252 retv = -EADDRNOTAVAIL;
253 break;
254 }
256 fl6_free_socklist(sk);
257 ipv6_sock_mc_close(sk);
259 /*
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...
263 */
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;
286 }
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;
295 /*
296 * ... and add it to the refcnt debug socks count
297 * in the new family. -acme
298 */
299 sk_refcnt_debug_inc(sk);
300 module_put(THIS_MODULE);
301 retv = 0;
302 break;
303 }
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:
388 {
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;
408 }
410 /* routing header option needs extra check */
411 if (optname == IPV6_RTHDR && opt->srcrt) {
412 struct ipv6_rt_hdr *rthdr = opt->srcrt;
413 if (rthdr->type)
414 goto sticky_done;
415 if ((rthdr->hdrlen & 1) ||
416 (rthdr->hdrlen >> 1) != rthdr->segments_left)
417 goto sticky_done;
418 }
420 retv = 0;
421 if (inet_sk(sk)->is_icsk) {
422 if (opt) {
423 struct inet_connection_sock *icsk = inet_csk(sk);
424 if (!((1 << sk->sk_state) &
425 (TCPF_LISTEN | TCPF_CLOSE))
426 && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
427 icsk->icsk_ext_hdr_len =
428 opt->opt_flen + opt->opt_nflen;
429 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
430 }
431 }
432 opt = xchg(&np->opt, opt);
433 sk_dst_reset(sk);
434 } else {
435 write_lock(&sk->sk_dst_lock);
436 opt = xchg(&np->opt, opt);
437 write_unlock(&sk->sk_dst_lock);
438 sk_dst_reset(sk);
439 }
440 sticky_done:
441 if (opt)
442 sock_kfree_s(sk, opt, opt->tot_len);
443 break;
444 }
446 case IPV6_2292PKTOPTIONS:
447 {
448 struct ipv6_txoptions *opt = NULL;
449 struct msghdr msg;
450 struct flowi fl;
451 int junk;
453 fl.fl6_flowlabel = 0;
454 fl.oif = sk->sk_bound_dev_if;
456 if (optlen == 0)
457 goto update;
459 /* 1K is probably excessive
460 * 1K is surely not enough, 2K per standard header is 16K.
461 */
462 retv = -EINVAL;
463 if (optlen > 64*1024)
464 break;
466 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
467 retv = -ENOBUFS;
468 if (opt == NULL)
469 break;
471 memset(opt, 0, sizeof(*opt));
472 opt->tot_len = sizeof(*opt) + optlen;
473 retv = -EFAULT;
474 if (copy_from_user(opt+1, optval, optlen))
475 goto done;
477 msg.msg_controllen = optlen;
478 msg.msg_control = (void*)(opt+1);
480 retv = datagram_send_ctl(&msg, &fl, opt, &junk, &junk);
481 if (retv)
482 goto done;
483 update:
484 retv = 0;
485 if (inet_sk(sk)->is_icsk) {
486 if (opt) {
487 struct inet_connection_sock *icsk = inet_csk(sk);
488 if (!((1 << sk->sk_state) &
489 (TCPF_LISTEN | TCPF_CLOSE))
490 && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
491 icsk->icsk_ext_hdr_len =
492 opt->opt_flen + opt->opt_nflen;
493 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
494 }
495 }
496 opt = xchg(&np->opt, opt);
497 sk_dst_reset(sk);
498 } else {
499 write_lock(&sk->sk_dst_lock);
500 opt = xchg(&np->opt, opt);
501 write_unlock(&sk->sk_dst_lock);
502 sk_dst_reset(sk);
503 }
505 done:
506 if (opt)
507 sock_kfree_s(sk, opt, opt->tot_len);
508 break;
509 }
510 case IPV6_UNICAST_HOPS:
511 if (val > 255 || val < -1)
512 goto e_inval;
513 np->hop_limit = val;
514 retv = 0;
515 break;
517 case IPV6_MULTICAST_HOPS:
518 if (sk->sk_type == SOCK_STREAM)
519 goto e_inval;
520 if (val > 255 || val < -1)
521 goto e_inval;
522 np->mcast_hops = val;
523 retv = 0;
524 break;
526 case IPV6_MULTICAST_LOOP:
527 np->mc_loop = valbool;
528 retv = 0;
529 break;
531 case IPV6_MULTICAST_IF:
532 if (sk->sk_type == SOCK_STREAM)
533 goto e_inval;
534 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
535 goto e_inval;
537 if (__dev_get_by_index(val) == NULL) {
538 retv = -ENODEV;
539 break;
540 }
541 np->mcast_oif = val;
542 retv = 0;
543 break;
544 case IPV6_ADD_MEMBERSHIP:
545 case IPV6_DROP_MEMBERSHIP:
546 {
547 struct ipv6_mreq mreq;
549 retv = -EFAULT;
550 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
551 break;
553 if (optname == IPV6_ADD_MEMBERSHIP)
554 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
555 else
556 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
557 break;
558 }
559 case IPV6_JOIN_ANYCAST:
560 case IPV6_LEAVE_ANYCAST:
561 {
562 struct ipv6_mreq mreq;
564 if (optlen != sizeof(struct ipv6_mreq))
565 goto e_inval;
567 retv = -EFAULT;
568 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
569 break;
571 if (optname == IPV6_JOIN_ANYCAST)
572 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
573 else
574 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
575 break;
576 }
577 case MCAST_JOIN_GROUP:
578 case MCAST_LEAVE_GROUP:
579 {
580 struct group_req greq;
581 struct sockaddr_in6 *psin6;
583 retv = -EFAULT;
584 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
585 break;
586 if (greq.gr_group.ss_family != AF_INET6) {
587 retv = -EADDRNOTAVAIL;
588 break;
589 }
590 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
591 if (optname == MCAST_JOIN_GROUP)
592 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
593 &psin6->sin6_addr);
594 else
595 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
596 &psin6->sin6_addr);
597 break;
598 }
599 case MCAST_JOIN_SOURCE_GROUP:
600 case MCAST_LEAVE_SOURCE_GROUP:
601 case MCAST_BLOCK_SOURCE:
602 case MCAST_UNBLOCK_SOURCE:
603 {
604 struct group_source_req greqs;
605 int omode, add;
607 if (optlen != sizeof(struct group_source_req))
608 goto e_inval;
609 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
610 retv = -EFAULT;
611 break;
612 }
613 if (greqs.gsr_group.ss_family != AF_INET6 ||
614 greqs.gsr_source.ss_family != AF_INET6) {
615 retv = -EADDRNOTAVAIL;
616 break;
617 }
618 if (optname == MCAST_BLOCK_SOURCE) {
619 omode = MCAST_EXCLUDE;
620 add = 1;
621 } else if (optname == MCAST_UNBLOCK_SOURCE) {
622 omode = MCAST_EXCLUDE;
623 add = 0;
624 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
625 struct sockaddr_in6 *psin6;
627 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
628 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
629 &psin6->sin6_addr);
630 /* prior join w/ different source is ok */
631 if (retv && retv != -EADDRINUSE)
632 break;
633 omode = MCAST_INCLUDE;
634 add = 1;
635 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
636 omode = MCAST_INCLUDE;
637 add = 0;
638 }
639 retv = ip6_mc_source(add, omode, sk, &greqs);
640 break;
641 }
642 case MCAST_MSFILTER:
643 {
644 extern int sysctl_mld_max_msf;
645 struct group_filter *gsf;
647 if (optlen < GROUP_FILTER_SIZE(0))
648 goto e_inval;
649 if (optlen > sysctl_optmem_max) {
650 retv = -ENOBUFS;
651 break;
652 }
653 gsf = kmalloc(optlen,GFP_KERNEL);
654 if (gsf == 0) {
655 retv = -ENOBUFS;
656 break;
657 }
658 retv = -EFAULT;
659 if (copy_from_user(gsf, optval, optlen)) {
660 kfree(gsf);
661 break;
662 }
663 /* numsrc >= (4G-140)/128 overflow in 32 bits */
664 if (gsf->gf_numsrc >= 0x1ffffffU ||
665 gsf->gf_numsrc > sysctl_mld_max_msf) {
666 kfree(gsf);
667 retv = -ENOBUFS;
668 break;
669 }
670 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
671 kfree(gsf);
672 retv = -EINVAL;
673 break;
674 }
675 retv = ip6_mc_msfilter(sk, gsf);
676 kfree(gsf);
678 break;
679 }
680 case IPV6_ROUTER_ALERT:
681 retv = ip6_ra_control(sk, val, NULL);
682 break;
683 case IPV6_MTU_DISCOVER:
684 if (val<0 || val>2)
685 goto e_inval;
686 np->pmtudisc = val;
687 retv = 0;
688 break;
689 case IPV6_MTU:
690 if (val && val < IPV6_MIN_MTU)
691 goto e_inval;
692 np->frag_size = val;
693 retv = 0;
694 break;
695 case IPV6_RECVERR:
696 np->recverr = valbool;
697 if (!val)
698 skb_queue_purge(&sk->sk_error_queue);
699 retv = 0;
700 break;
701 case IPV6_FLOWINFO_SEND:
702 np->sndflow = valbool;
703 retv = 0;
704 break;
705 case IPV6_FLOWLABEL_MGR:
706 retv = ipv6_flowlabel_opt(sk, optval, optlen);
707 break;
708 case IPV6_IPSEC_POLICY:
709 case IPV6_XFRM_POLICY:
710 retv = -EPERM;
711 if (!capable(CAP_NET_ADMIN))
712 break;
713 retv = xfrm_user_policy(sk, optname, optval, optlen);
714 break;
716 }
717 release_sock(sk);
719 return retv;
721 e_inval:
722 release_sock(sk);
723 return -EINVAL;
724 }
726 int ipv6_setsockopt(struct sock *sk, int level, int optname,
727 char __user *optval, int optlen)
728 {
729 int err;
731 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
732 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
734 if (level != SOL_IPV6)
735 return -ENOPROTOOPT;
737 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
738 #ifdef CONFIG_NETFILTER
739 /* we need to exclude all possible ENOPROTOOPTs except default case */
740 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
741 optname != IPV6_XFRM_POLICY) {
742 lock_sock(sk);
743 err = nf_setsockopt(sk, PF_INET6, optname, optval,
744 optlen);
745 release_sock(sk);
746 }
747 #endif
748 return err;
749 }
752 #ifdef CONFIG_COMPAT
753 int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
754 char __user *optval, int optlen)
755 {
756 int err;
758 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
759 if (udp_prot.compat_setsockopt != NULL)
760 return udp_prot.compat_setsockopt(sk, level, optname,
761 optval, optlen);
762 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
763 }
765 if (level != SOL_IPV6)
766 return -ENOPROTOOPT;
768 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
769 #ifdef CONFIG_NETFILTER
770 /* we need to exclude all possible ENOPROTOOPTs except default case */
771 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
772 optname != IPV6_XFRM_POLICY) {
773 lock_sock(sk);
774 err = compat_nf_setsockopt(sk, PF_INET6, optname,
775 optval, optlen);
776 release_sock(sk);
777 }
778 #endif
779 return err;
780 }
782 EXPORT_SYMBOL(compat_ipv6_setsockopt);
783 #endif
785 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_opt_hdr *hdr,
786 char __user *optval, int len)
787 {
788 if (!hdr)
789 return 0;
790 len = min_t(int, len, ipv6_optlen(hdr));
791 if (copy_to_user(optval, hdr, ipv6_optlen(hdr)))
792 return -EFAULT;
793 return len;
794 }
796 static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
797 char __user *optval, int __user *optlen)
798 {
799 struct ipv6_pinfo *np = inet6_sk(sk);
800 int len;
801 int val;
803 if (get_user(len, optlen))
804 return -EFAULT;
805 switch (optname) {
806 case IPV6_ADDRFORM:
807 if (sk->sk_protocol != IPPROTO_UDP &&
808 sk->sk_protocol != IPPROTO_TCP)
809 return -EINVAL;
810 if (sk->sk_state != TCP_ESTABLISHED)
811 return -ENOTCONN;
812 val = sk->sk_family;
813 break;
814 case MCAST_MSFILTER:
815 {
816 struct group_filter gsf;
817 int err;
819 if (len < GROUP_FILTER_SIZE(0))
820 return -EINVAL;
821 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
822 return -EFAULT;
823 lock_sock(sk);
824 err = ip6_mc_msfget(sk, &gsf,
825 (struct group_filter __user *)optval, optlen);
826 release_sock(sk);
827 return err;
828 }
830 case IPV6_2292PKTOPTIONS:
831 {
832 struct msghdr msg;
833 struct sk_buff *skb;
835 if (sk->sk_type != SOCK_STREAM)
836 return -ENOPROTOOPT;
838 msg.msg_control = optval;
839 msg.msg_controllen = len;
840 msg.msg_flags = 0;
842 lock_sock(sk);
843 skb = np->pktoptions;
844 if (skb)
845 atomic_inc(&skb->users);
846 release_sock(sk);
848 if (skb) {
849 int err = datagram_recv_ctl(sk, &msg, skb);
850 kfree_skb(skb);
851 if (err)
852 return err;
853 } else {
854 if (np->rxopt.bits.rxinfo) {
855 struct in6_pktinfo src_info;
856 src_info.ipi6_ifindex = np->mcast_oif;
857 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
858 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
859 }
860 if (np->rxopt.bits.rxhlim) {
861 int hlim = np->mcast_hops;
862 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
863 }
864 if (np->rxopt.bits.rxoinfo) {
865 struct in6_pktinfo src_info;
866 src_info.ipi6_ifindex = np->mcast_oif;
867 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
868 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
869 }
870 if (np->rxopt.bits.rxohlim) {
871 int hlim = np->mcast_hops;
872 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
873 }
874 }
875 len -= msg.msg_controllen;
876 return put_user(len, optlen);
877 }
878 case IPV6_MTU:
879 {
880 struct dst_entry *dst;
881 val = 0;
882 lock_sock(sk);
883 dst = sk_dst_get(sk);
884 if (dst) {
885 val = dst_mtu(dst);
886 dst_release(dst);
887 }
888 release_sock(sk);
889 if (!val)
890 return -ENOTCONN;
891 break;
892 }
894 case IPV6_V6ONLY:
895 val = np->ipv6only;
896 break;
898 case IPV6_RECVPKTINFO:
899 val = np->rxopt.bits.rxinfo;
900 break;
902 case IPV6_2292PKTINFO:
903 val = np->rxopt.bits.rxoinfo;
904 break;
906 case IPV6_RECVHOPLIMIT:
907 val = np->rxopt.bits.rxhlim;
908 break;
910 case IPV6_2292HOPLIMIT:
911 val = np->rxopt.bits.rxohlim;
912 break;
914 case IPV6_RECVRTHDR:
915 val = np->rxopt.bits.srcrt;
916 break;
918 case IPV6_2292RTHDR:
919 val = np->rxopt.bits.osrcrt;
920 break;
922 case IPV6_HOPOPTS:
923 case IPV6_RTHDRDSTOPTS:
924 case IPV6_RTHDR:
925 case IPV6_DSTOPTS:
926 {
928 lock_sock(sk);
929 len = ipv6_getsockopt_sticky(sk, np->opt->hopopt,
930 optval, len);
931 release_sock(sk);
932 return put_user(len, optlen);
933 }
935 case IPV6_RECVHOPOPTS:
936 val = np->rxopt.bits.hopopts;
937 break;
939 case IPV6_2292HOPOPTS:
940 val = np->rxopt.bits.ohopopts;
941 break;
943 case IPV6_RECVDSTOPTS:
944 val = np->rxopt.bits.dstopts;
945 break;
947 case IPV6_2292DSTOPTS:
948 val = np->rxopt.bits.odstopts;
949 break;
951 case IPV6_TCLASS:
952 val = np->tclass;
953 if (val < 0)
954 val = 0;
955 break;
957 case IPV6_RECVTCLASS:
958 val = np->rxopt.bits.rxtclass;
959 break;
961 case IPV6_FLOWINFO:
962 val = np->rxopt.bits.rxflow;
963 break;
965 case IPV6_UNICAST_HOPS:
966 val = np->hop_limit;
967 break;
969 case IPV6_MULTICAST_HOPS:
970 val = np->mcast_hops;
971 break;
973 case IPV6_MULTICAST_LOOP:
974 val = np->mc_loop;
975 break;
977 case IPV6_MULTICAST_IF:
978 val = np->mcast_oif;
979 break;
981 case IPV6_MTU_DISCOVER:
982 val = np->pmtudisc;
983 break;
985 case IPV6_RECVERR:
986 val = np->recverr;
987 break;
989 case IPV6_FLOWINFO_SEND:
990 val = np->sndflow;
991 break;
993 default:
994 return -EINVAL;
995 }
996 len = min_t(unsigned int, sizeof(int), len);
997 if(put_user(len, optlen))
998 return -EFAULT;
999 if(copy_to_user(optval,&val,len))
1000 return -EFAULT;
1001 return 0;
1004 int ipv6_getsockopt(struct sock *sk, int level, int optname,
1005 char __user *optval, int __user *optlen)
1007 int err;
1009 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1010 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1012 if(level != SOL_IPV6)
1013 return -ENOPROTOOPT;
1015 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1016 #ifdef CONFIG_NETFILTER
1017 /* we need to exclude all possible EINVALs except default case */
1018 if (err == -EINVAL && optname != IPV6_ADDRFORM &&
1019 optname != MCAST_MSFILTER) {
1020 int len;
1022 if (get_user(len, optlen))
1023 return -EFAULT;
1025 lock_sock(sk);
1026 err = nf_getsockopt(sk, PF_INET6, optname, optval,
1027 &len);
1028 release_sock(sk);
1029 if (err >= 0)
1030 err = put_user(len, optlen);
1032 #endif
1033 return err;
1036 #ifdef CONFIG_COMPAT
1037 int compat_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 if (udp_prot.compat_getsockopt != NULL)
1044 return udp_prot.compat_getsockopt(sk, level, optname,
1045 optval, optlen);
1046 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1049 if (level != SOL_IPV6)
1050 return -ENOPROTOOPT;
1052 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1053 #ifdef CONFIG_NETFILTER
1054 /* we need to exclude all possible EINVALs except default case */
1055 if (err == -EINVAL && optname != IPV6_ADDRFORM &&
1056 optname != MCAST_MSFILTER) {
1057 int len;
1059 if (get_user(len, optlen))
1060 return -EFAULT;
1062 lock_sock(sk);
1063 err = compat_nf_getsockopt(sk, PF_INET6,
1064 optname, optval, &len);
1065 release_sock(sk);
1066 if (err >= 0)
1067 err = put_user(len, optlen);
1069 #endif
1070 return err;
1073 EXPORT_SYMBOL(compat_ipv6_getsockopt);
1074 #endif
1076 void __init ipv6_packet_init(void)
1078 dev_add_pack(&ipv6_packet_type);
1081 void ipv6_packet_cleanup(void)
1083 dev_remove_pack(&ipv6_packet_type);