Merge branch 'devel' of master.kernel.org:/home/rmk/linux-2.6-arm
[linux-2.6] / net / ipv6 / ipv6_sockglue.c
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  */
27
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>
42
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>
55
56 #include <asm/uaccess.h>
57
58 DEFINE_SNMP_STAT(struct ipstats_mib, ipv6_statistics) __read_mostly;
59
60 static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb, int features)
61 {
62         struct sk_buff *segs = ERR_PTR(-EINVAL);
63         struct ipv6hdr *ipv6h;
64         struct inet6_protocol *ops;
65         int proto;
66
67         if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
68                 goto out;
69
70         ipv6h = skb->nh.ipv6h;
71         proto = ipv6h->nexthdr;
72         __skb_pull(skb, sizeof(*ipv6h));
73
74         rcu_read_lock();
75         for (;;) {
76                 struct ipv6_opt_hdr *opth;
77                 int len;
78
79                 if (proto != NEXTHDR_HOP) {
80                         ops = rcu_dereference(inet6_protos[proto]);
81
82                         if (unlikely(!ops))
83                                 goto unlock;
84
85                         if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
86                                 break;
87                 }
88
89                 if (unlikely(!pskb_may_pull(skb, 8)))
90                         goto unlock;
91
92                 opth = (void *)skb->data;
93                 len = opth->hdrlen * 8 + 8;
94
95                 if (unlikely(!pskb_may_pull(skb, len)))
96                         goto unlock;
97
98                 proto = opth->nexthdr;
99                 __skb_pull(skb, len);
100         }
101
102         skb->h.raw = skb->data;
103         if (likely(ops->gso_segment))
104                 segs = ops->gso_segment(skb, features);
105
106 unlock:
107         rcu_read_unlock();
108
109         if (unlikely(IS_ERR(segs)))
110                 goto out;
111
112         for (skb = segs; skb; skb = skb->next) {
113                 ipv6h = skb->nh.ipv6h;
114                 ipv6h->payload_len = htons(skb->len - skb->mac_len);
115         }
116
117 out:
118         return segs;
119 }
120
121 static struct packet_type ipv6_packet_type = {
122         .type = __constant_htons(ETH_P_IPV6), 
123         .func = ipv6_rcv,
124         .gso_segment = ipv6_gso_segment,
125 };
126
127 struct ip6_ra_chain *ip6_ra_chain;
128 DEFINE_RWLOCK(ip6_ra_lock);
129
130 int ip6_ra_control(struct sock *sk, int sel, void (*destructor)(struct sock *))
131 {
132         struct ip6_ra_chain *ra, *new_ra, **rap;
133
134         /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
135         if (sk->sk_type != SOCK_RAW || inet_sk(sk)->num != IPPROTO_RAW)
136                 return -EINVAL;
137
138         new_ra = (sel>=0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
139
140         write_lock_bh(&ip6_ra_lock);
141         for (rap = &ip6_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
142                 if (ra->sk == sk) {
143                         if (sel>=0) {
144                                 write_unlock_bh(&ip6_ra_lock);
145                                 kfree(new_ra);
146                                 return -EADDRINUSE;
147                         }
148
149                         *rap = ra->next;
150                         write_unlock_bh(&ip6_ra_lock);
151
152                         if (ra->destructor)
153                                 ra->destructor(sk);
154                         sock_put(sk);
155                         kfree(ra);
156                         return 0;
157                 }
158         }
159         if (new_ra == NULL) {
160                 write_unlock_bh(&ip6_ra_lock);
161                 return -ENOBUFS;
162         }
163         new_ra->sk = sk;
164         new_ra->sel = sel;
165         new_ra->destructor = destructor;
166         new_ra->next = ra;
167         *rap = new_ra;
168         sock_hold(sk);
169         write_unlock_bh(&ip6_ra_lock);
170         return 0;
171 }
172
173 static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
174                     char __user *optval, int optlen)
175 {
176         struct ipv6_pinfo *np = inet6_sk(sk);
177         int val, valbool;
178         int retv = -ENOPROTOOPT;
179
180         if (optval == NULL)
181                 val=0;
182         else if (get_user(val, (int __user *) optval))
183                 return -EFAULT;
184
185         valbool = (val!=0);
186
187         lock_sock(sk);
188
189         switch (optname) {
190
191         case IPV6_ADDRFORM:
192                 if (val == PF_INET) {
193                         struct ipv6_txoptions *opt;
194                         struct sk_buff *pktopt;
195
196                         if (sk->sk_protocol != IPPROTO_UDP &&
197                             sk->sk_protocol != IPPROTO_TCP)
198                                 break;
199
200                         if (sk->sk_state != TCP_ESTABLISHED) {
201                                 retv = -ENOTCONN;
202                                 break;
203                         }
204
205                         if (ipv6_only_sock(sk) ||
206                             !(ipv6_addr_type(&np->daddr) & IPV6_ADDR_MAPPED)) {
207                                 retv = -EADDRNOTAVAIL;
208                                 break;
209                         }
210
211                         fl6_free_socklist(sk);
212                         ipv6_sock_mc_close(sk);
213
214                         /*
215                          * Sock is moving from IPv6 to IPv4 (sk_prot), so
216                          * remove it from the refcnt debug socks count in the
217                          * original family...
218                          */
219                         sk_refcnt_debug_dec(sk);
220
221                         if (sk->sk_protocol == IPPROTO_TCP) {
222                                 struct inet_connection_sock *icsk = inet_csk(sk);
223
224                                 local_bh_disable();
225                                 sock_prot_dec_use(sk->sk_prot);
226                                 sock_prot_inc_use(&tcp_prot);
227                                 local_bh_enable();
228                                 sk->sk_prot = &tcp_prot;
229                                 icsk->icsk_af_ops = &ipv4_specific;
230                                 sk->sk_socket->ops = &inet_stream_ops;
231                                 sk->sk_family = PF_INET;
232                                 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
233                         } else {
234                                 local_bh_disable();
235                                 sock_prot_dec_use(sk->sk_prot);
236                                 sock_prot_inc_use(&udp_prot);
237                                 local_bh_enable();
238                                 sk->sk_prot = &udp_prot;
239                                 sk->sk_socket->ops = &inet_dgram_ops;
240                                 sk->sk_family = PF_INET;
241                         }
242                         opt = xchg(&np->opt, NULL);
243                         if (opt)
244                                 sock_kfree_s(sk, opt, opt->tot_len);
245                         pktopt = xchg(&np->pktoptions, NULL);
246                         if (pktopt)
247                                 kfree_skb(pktopt);
248
249                         sk->sk_destruct = inet_sock_destruct;
250                         /*
251                          * ... and add it to the refcnt debug socks count
252                          * in the new family. -acme
253                          */
254                         sk_refcnt_debug_inc(sk);
255                         module_put(THIS_MODULE);
256                         retv = 0;
257                         break;
258                 }
259                 goto e_inval;
260
261         case IPV6_V6ONLY:
262                 if (inet_sk(sk)->num)
263                         goto e_inval;
264                 np->ipv6only = valbool;
265                 retv = 0;
266                 break;
267
268         case IPV6_RECVPKTINFO:
269                 np->rxopt.bits.rxinfo = valbool;
270                 retv = 0;
271                 break;
272                 
273         case IPV6_2292PKTINFO:
274                 np->rxopt.bits.rxoinfo = valbool;
275                 retv = 0;
276                 break;
277
278         case IPV6_RECVHOPLIMIT:
279                 np->rxopt.bits.rxhlim = valbool;
280                 retv = 0;
281                 break;
282
283         case IPV6_2292HOPLIMIT:
284                 np->rxopt.bits.rxohlim = valbool;
285                 retv = 0;
286                 break;
287
288         case IPV6_RECVRTHDR:
289                 if (val < 0 || val > 2)
290                         goto e_inval;
291                 np->rxopt.bits.srcrt = val;
292                 retv = 0;
293                 break;
294
295         case IPV6_2292RTHDR:
296                 if (val < 0 || val > 2)
297                         goto e_inval;
298                 np->rxopt.bits.osrcrt = val;
299                 retv = 0;
300                 break;
301
302         case IPV6_RECVHOPOPTS:
303                 np->rxopt.bits.hopopts = valbool;
304                 retv = 0;
305                 break;
306
307         case IPV6_2292HOPOPTS:
308                 np->rxopt.bits.ohopopts = valbool;
309                 retv = 0;
310                 break;
311
312         case IPV6_RECVDSTOPTS:
313                 np->rxopt.bits.dstopts = valbool;
314                 retv = 0;
315                 break;
316
317         case IPV6_2292DSTOPTS:
318                 np->rxopt.bits.odstopts = valbool;
319                 retv = 0;
320                 break;
321
322         case IPV6_TCLASS:
323                 if (val < 0 || val > 0xff)
324                         goto e_inval;
325                 np->tclass = val;
326                 retv = 0;
327                 break;
328                 
329         case IPV6_RECVTCLASS:
330                 np->rxopt.bits.rxtclass = valbool;
331                 retv = 0;
332                 break;
333
334         case IPV6_FLOWINFO:
335                 np->rxopt.bits.rxflow = valbool;
336                 retv = 0;
337                 break;
338
339         case IPV6_HOPOPTS:
340         case IPV6_RTHDRDSTOPTS:
341         case IPV6_RTHDR:
342         case IPV6_DSTOPTS:
343         {
344                 struct ipv6_txoptions *opt;
345                 if (optlen == 0)
346                         optval = NULL;
347
348                 /* hop-by-hop / destination options are privileged option */
349                 retv = -EPERM;
350                 if (optname != IPV6_RTHDR && !capable(CAP_NET_RAW))
351                         break;
352
353                 retv = -EINVAL;
354                 if (optlen & 0x7 || optlen > 8 * 255)
355                         break;
356
357                 opt = ipv6_renew_options(sk, np->opt, optname,
358                                          (struct ipv6_opt_hdr __user *)optval,
359                                          optlen);
360                 if (IS_ERR(opt)) {
361                         retv = PTR_ERR(opt);
362                         break;
363                 }
364
365                 /* routing header option needs extra check */
366                 if (optname == IPV6_RTHDR && opt->srcrt) {
367                         struct ipv6_rt_hdr *rthdr = opt->srcrt;
368                         if (rthdr->type)
369                                 goto sticky_done;
370                         if ((rthdr->hdrlen & 1) ||
371                             (rthdr->hdrlen >> 1) != rthdr->segments_left)
372                                 goto sticky_done;
373                 }
374
375                 retv = 0;
376                 if (inet_sk(sk)->is_icsk) {
377                         if (opt) {
378                                 struct inet_connection_sock *icsk = inet_csk(sk);
379                                 if (!((1 << sk->sk_state) &
380                                       (TCPF_LISTEN | TCPF_CLOSE))
381                                     && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
382                                         icsk->icsk_ext_hdr_len =
383                                                 opt->opt_flen + opt->opt_nflen;
384                                         icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
385                                 }
386                         }
387                         opt = xchg(&np->opt, opt);
388                         sk_dst_reset(sk);
389                 } else {
390                         write_lock(&sk->sk_dst_lock);
391                         opt = xchg(&np->opt, opt);
392                         write_unlock(&sk->sk_dst_lock);
393                         sk_dst_reset(sk);
394                 }
395 sticky_done:
396                 if (opt)
397                         sock_kfree_s(sk, opt, opt->tot_len);
398                 break;
399         }
400
401         case IPV6_2292PKTOPTIONS:
402         {
403                 struct ipv6_txoptions *opt = NULL;
404                 struct msghdr msg;
405                 struct flowi fl;
406                 int junk;
407
408                 fl.fl6_flowlabel = 0;
409                 fl.oif = sk->sk_bound_dev_if;
410
411                 if (optlen == 0)
412                         goto update;
413
414                 /* 1K is probably excessive
415                  * 1K is surely not enough, 2K per standard header is 16K.
416                  */
417                 retv = -EINVAL;
418                 if (optlen > 64*1024)
419                         break;
420
421                 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
422                 retv = -ENOBUFS;
423                 if (opt == NULL)
424                         break;
425
426                 memset(opt, 0, sizeof(*opt));
427                 opt->tot_len = sizeof(*opt) + optlen;
428                 retv = -EFAULT;
429                 if (copy_from_user(opt+1, optval, optlen))
430                         goto done;
431
432                 msg.msg_controllen = optlen;
433                 msg.msg_control = (void*)(opt+1);
434
435                 retv = datagram_send_ctl(&msg, &fl, opt, &junk, &junk);
436                 if (retv)
437                         goto done;
438 update:
439                 retv = 0;
440                 if (inet_sk(sk)->is_icsk) {
441                         if (opt) {
442                                 struct inet_connection_sock *icsk = inet_csk(sk);
443                                 if (!((1 << sk->sk_state) &
444                                       (TCPF_LISTEN | TCPF_CLOSE))
445                                     && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
446                                         icsk->icsk_ext_hdr_len =
447                                                 opt->opt_flen + opt->opt_nflen;
448                                         icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
449                                 }
450                         }
451                         opt = xchg(&np->opt, opt);
452                         sk_dst_reset(sk);
453                 } else {
454                         write_lock(&sk->sk_dst_lock);
455                         opt = xchg(&np->opt, opt);
456                         write_unlock(&sk->sk_dst_lock);
457                         sk_dst_reset(sk);
458                 }
459
460 done:
461                 if (opt)
462                         sock_kfree_s(sk, opt, opt->tot_len);
463                 break;
464         }
465         case IPV6_UNICAST_HOPS:
466                 if (val > 255 || val < -1)
467                         goto e_inval;
468                 np->hop_limit = val;
469                 retv = 0;
470                 break;
471
472         case IPV6_MULTICAST_HOPS:
473                 if (sk->sk_type == SOCK_STREAM)
474                         goto e_inval;
475                 if (val > 255 || val < -1)
476                         goto e_inval;
477                 np->mcast_hops = val;
478                 retv = 0;
479                 break;
480
481         case IPV6_MULTICAST_LOOP:
482                 np->mc_loop = valbool;
483                 retv = 0;
484                 break;
485
486         case IPV6_MULTICAST_IF:
487                 if (sk->sk_type == SOCK_STREAM)
488                         goto e_inval;
489                 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
490                         goto e_inval;
491
492                 if (__dev_get_by_index(val) == NULL) {
493                         retv = -ENODEV;
494                         break;
495                 }
496                 np->mcast_oif = val;
497                 retv = 0;
498                 break;
499         case IPV6_ADD_MEMBERSHIP:
500         case IPV6_DROP_MEMBERSHIP:
501         {
502                 struct ipv6_mreq mreq;
503
504                 retv = -EFAULT;
505                 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
506                         break;
507
508                 if (optname == IPV6_ADD_MEMBERSHIP)
509                         retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
510                 else
511                         retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
512                 break;
513         }
514         case IPV6_JOIN_ANYCAST:
515         case IPV6_LEAVE_ANYCAST:
516         {
517                 struct ipv6_mreq mreq;
518
519                 if (optlen != sizeof(struct ipv6_mreq))
520                         goto e_inval;
521
522                 retv = -EFAULT;
523                 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
524                         break;
525
526                 if (optname == IPV6_JOIN_ANYCAST)
527                         retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
528                 else
529                         retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
530                 break;
531         }
532         case MCAST_JOIN_GROUP:
533         case MCAST_LEAVE_GROUP:
534         {
535                 struct group_req greq;
536                 struct sockaddr_in6 *psin6;
537
538                 retv = -EFAULT;
539                 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
540                         break;
541                 if (greq.gr_group.ss_family != AF_INET6) {
542                         retv = -EADDRNOTAVAIL;
543                         break;
544                 }
545                 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
546                 if (optname == MCAST_JOIN_GROUP)
547                         retv = ipv6_sock_mc_join(sk, greq.gr_interface,
548                                 &psin6->sin6_addr);
549                 else
550                         retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
551                                 &psin6->sin6_addr);
552                 break;
553         }
554         case MCAST_JOIN_SOURCE_GROUP:
555         case MCAST_LEAVE_SOURCE_GROUP:
556         case MCAST_BLOCK_SOURCE:
557         case MCAST_UNBLOCK_SOURCE:
558         {
559                 struct group_source_req greqs;
560                 int omode, add;
561
562                 if (optlen != sizeof(struct group_source_req))
563                         goto e_inval;
564                 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
565                         retv = -EFAULT;
566                         break;
567                 }
568                 if (greqs.gsr_group.ss_family != AF_INET6 ||
569                     greqs.gsr_source.ss_family != AF_INET6) {
570                         retv = -EADDRNOTAVAIL;
571                         break;
572                 }
573                 if (optname == MCAST_BLOCK_SOURCE) {
574                         omode = MCAST_EXCLUDE;
575                         add = 1;
576                 } else if (optname == MCAST_UNBLOCK_SOURCE) {
577                         omode = MCAST_EXCLUDE;
578                         add = 0;
579                 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
580                         struct sockaddr_in6 *psin6;
581
582                         psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
583                         retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
584                                 &psin6->sin6_addr);
585                         /* prior join w/ different source is ok */
586                         if (retv && retv != -EADDRINUSE)
587                                 break;
588                         omode = MCAST_INCLUDE;
589                         add = 1;
590                 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
591                         omode = MCAST_INCLUDE;
592                         add = 0;
593                 }
594                 retv = ip6_mc_source(add, omode, sk, &greqs);
595                 break;
596         }
597         case MCAST_MSFILTER:
598         {
599                 extern int sysctl_mld_max_msf;
600                 struct group_filter *gsf;
601
602                 if (optlen < GROUP_FILTER_SIZE(0))
603                         goto e_inval;
604                 if (optlen > sysctl_optmem_max) {
605                         retv = -ENOBUFS;
606                         break;
607                 }
608                 gsf = kmalloc(optlen,GFP_KERNEL);
609                 if (gsf == 0) {
610                         retv = -ENOBUFS;
611                         break;
612                 }
613                 retv = -EFAULT;
614                 if (copy_from_user(gsf, optval, optlen)) {
615                         kfree(gsf);
616                         break;
617                 }
618                 /* numsrc >= (4G-140)/128 overflow in 32 bits */
619                 if (gsf->gf_numsrc >= 0x1ffffffU ||
620                     gsf->gf_numsrc > sysctl_mld_max_msf) {
621                         kfree(gsf);
622                         retv = -ENOBUFS;
623                         break;
624                 }
625                 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
626                         kfree(gsf);
627                         retv = -EINVAL;
628                         break;
629                 }
630                 retv = ip6_mc_msfilter(sk, gsf);
631                 kfree(gsf);
632
633                 break;
634         }
635         case IPV6_ROUTER_ALERT:
636                 retv = ip6_ra_control(sk, val, NULL);
637                 break;
638         case IPV6_MTU_DISCOVER:
639                 if (val<0 || val>2)
640                         goto e_inval;
641                 np->pmtudisc = val;
642                 retv = 0;
643                 break;
644         case IPV6_MTU:
645                 if (val && val < IPV6_MIN_MTU)
646                         goto e_inval;
647                 np->frag_size = val;
648                 retv = 0;
649                 break;
650         case IPV6_RECVERR:
651                 np->recverr = valbool;
652                 if (!val)
653                         skb_queue_purge(&sk->sk_error_queue);
654                 retv = 0;
655                 break;
656         case IPV6_FLOWINFO_SEND:
657                 np->sndflow = valbool;
658                 retv = 0;
659                 break;
660         case IPV6_FLOWLABEL_MGR:
661                 retv = ipv6_flowlabel_opt(sk, optval, optlen);
662                 break;
663         case IPV6_IPSEC_POLICY:
664         case IPV6_XFRM_POLICY:
665                 retv = -EPERM;
666                 if (!capable(CAP_NET_ADMIN))
667                         break;
668                 retv = xfrm_user_policy(sk, optname, optval, optlen);
669                 break;
670
671         }
672         release_sock(sk);
673
674         return retv;
675
676 e_inval:
677         release_sock(sk);
678         return -EINVAL;
679 }
680
681 int ipv6_setsockopt(struct sock *sk, int level, int optname,
682                     char __user *optval, int optlen)
683 {
684         int err;
685
686         if (level == SOL_IP && sk->sk_type != SOCK_RAW)
687                 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
688
689         if (level != SOL_IPV6)
690                 return -ENOPROTOOPT;
691
692         err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
693 #ifdef CONFIG_NETFILTER
694         /* we need to exclude all possible ENOPROTOOPTs except default case */
695         if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
696                         optname != IPV6_XFRM_POLICY) {
697                 lock_sock(sk);
698                 err = nf_setsockopt(sk, PF_INET6, optname, optval,
699                                 optlen);
700                 release_sock(sk);
701         }
702 #endif
703         return err;
704 }
705
706
707 #ifdef CONFIG_COMPAT
708 int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
709                            char __user *optval, int optlen)
710 {
711         int err;
712
713         if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
714                 if (udp_prot.compat_setsockopt != NULL)
715                         return udp_prot.compat_setsockopt(sk, level, optname,
716                                                           optval, optlen);
717                 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
718         }
719
720         if (level != SOL_IPV6)
721                 return -ENOPROTOOPT;
722
723         err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
724 #ifdef CONFIG_NETFILTER
725         /* we need to exclude all possible ENOPROTOOPTs except default case */
726         if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
727             optname != IPV6_XFRM_POLICY) {
728                 lock_sock(sk);
729                 err = compat_nf_setsockopt(sk, PF_INET6, optname,
730                                            optval, optlen);
731                 release_sock(sk);
732         }
733 #endif
734         return err;
735 }
736
737 EXPORT_SYMBOL(compat_ipv6_setsockopt);
738 #endif
739
740 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_opt_hdr *hdr,
741                                   char __user *optval, int len)
742 {
743         if (!hdr)
744                 return 0;
745         len = min_t(int, len, ipv6_optlen(hdr));
746         if (copy_to_user(optval, hdr, ipv6_optlen(hdr)))
747                 return -EFAULT;
748         return len;
749 }
750
751 static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
752                     char __user *optval, int __user *optlen)
753 {
754         struct ipv6_pinfo *np = inet6_sk(sk);
755         int len;
756         int val;
757
758         if (get_user(len, optlen))
759                 return -EFAULT;
760         switch (optname) {
761         case IPV6_ADDRFORM:
762                 if (sk->sk_protocol != IPPROTO_UDP &&
763                     sk->sk_protocol != IPPROTO_TCP)
764                         return -EINVAL;
765                 if (sk->sk_state != TCP_ESTABLISHED)
766                         return -ENOTCONN;
767                 val = sk->sk_family;
768                 break;
769         case MCAST_MSFILTER:
770         {
771                 struct group_filter gsf;
772                 int err;
773
774                 if (len < GROUP_FILTER_SIZE(0))
775                         return -EINVAL;
776                 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
777                         return -EFAULT;
778                 lock_sock(sk);
779                 err = ip6_mc_msfget(sk, &gsf,
780                         (struct group_filter __user *)optval, optlen);
781                 release_sock(sk);
782                 return err;
783         }
784
785         case IPV6_2292PKTOPTIONS:
786         {
787                 struct msghdr msg;
788                 struct sk_buff *skb;
789
790                 if (sk->sk_type != SOCK_STREAM)
791                         return -ENOPROTOOPT;
792
793                 msg.msg_control = optval;
794                 msg.msg_controllen = len;
795                 msg.msg_flags = 0;
796
797                 lock_sock(sk);
798                 skb = np->pktoptions;
799                 if (skb)
800                         atomic_inc(&skb->users);
801                 release_sock(sk);
802
803                 if (skb) {
804                         int err = datagram_recv_ctl(sk, &msg, skb);
805                         kfree_skb(skb);
806                         if (err)
807                                 return err;
808                 } else {
809                         if (np->rxopt.bits.rxinfo) {
810                                 struct in6_pktinfo src_info;
811                                 src_info.ipi6_ifindex = np->mcast_oif;
812                                 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
813                                 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
814                         }
815                         if (np->rxopt.bits.rxhlim) {
816                                 int hlim = np->mcast_hops;
817                                 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
818                         }
819                         if (np->rxopt.bits.rxoinfo) {
820                                 struct in6_pktinfo src_info;
821                                 src_info.ipi6_ifindex = np->mcast_oif;
822                                 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
823                                 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
824                         }
825                         if (np->rxopt.bits.rxohlim) {
826                                 int hlim = np->mcast_hops;
827                                 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
828                         }
829                 }
830                 len -= msg.msg_controllen;
831                 return put_user(len, optlen);
832         }
833         case IPV6_MTU:
834         {
835                 struct dst_entry *dst;
836                 val = 0;        
837                 lock_sock(sk);
838                 dst = sk_dst_get(sk);
839                 if (dst) {
840                         val = dst_mtu(dst);
841                         dst_release(dst);
842                 }
843                 release_sock(sk);
844                 if (!val)
845                         return -ENOTCONN;
846                 break;
847         }
848
849         case IPV6_V6ONLY:
850                 val = np->ipv6only;
851                 break;
852
853         case IPV6_RECVPKTINFO:
854                 val = np->rxopt.bits.rxinfo;
855                 break;
856
857         case IPV6_2292PKTINFO:
858                 val = np->rxopt.bits.rxoinfo;
859                 break;
860
861         case IPV6_RECVHOPLIMIT:
862                 val = np->rxopt.bits.rxhlim;
863                 break;
864
865         case IPV6_2292HOPLIMIT:
866                 val = np->rxopt.bits.rxohlim;
867                 break;
868
869         case IPV6_RECVRTHDR:
870                 val = np->rxopt.bits.srcrt;
871                 break;
872
873         case IPV6_2292RTHDR:
874                 val = np->rxopt.bits.osrcrt;
875                 break;
876
877         case IPV6_HOPOPTS:
878         case IPV6_RTHDRDSTOPTS:
879         case IPV6_RTHDR:
880         case IPV6_DSTOPTS:
881         {
882
883                 lock_sock(sk);
884                 len = ipv6_getsockopt_sticky(sk, np->opt->hopopt,
885                                              optval, len);
886                 release_sock(sk);
887                 return put_user(len, optlen);
888         }
889
890         case IPV6_RECVHOPOPTS:
891                 val = np->rxopt.bits.hopopts;
892                 break;
893
894         case IPV6_2292HOPOPTS:
895                 val = np->rxopt.bits.ohopopts;
896                 break;
897
898         case IPV6_RECVDSTOPTS:
899                 val = np->rxopt.bits.dstopts;
900                 break;
901
902         case IPV6_2292DSTOPTS:
903                 val = np->rxopt.bits.odstopts;
904                 break;
905
906         case IPV6_TCLASS:
907                 val = np->tclass;
908                 break;
909
910         case IPV6_RECVTCLASS:
911                 val = np->rxopt.bits.rxtclass;
912                 break;
913
914         case IPV6_FLOWINFO:
915                 val = np->rxopt.bits.rxflow;
916                 break;
917
918         case IPV6_UNICAST_HOPS:
919                 val = np->hop_limit;
920                 break;
921
922         case IPV6_MULTICAST_HOPS:
923                 val = np->mcast_hops;
924                 break;
925
926         case IPV6_MULTICAST_LOOP:
927                 val = np->mc_loop;
928                 break;
929
930         case IPV6_MULTICAST_IF:
931                 val = np->mcast_oif;
932                 break;
933
934         case IPV6_MTU_DISCOVER:
935                 val = np->pmtudisc;
936                 break;
937
938         case IPV6_RECVERR:
939                 val = np->recverr;
940                 break;
941
942         case IPV6_FLOWINFO_SEND:
943                 val = np->sndflow;
944                 break;
945
946         default:
947                 return -EINVAL;
948         }
949         len = min_t(unsigned int, sizeof(int), len);
950         if(put_user(len, optlen))
951                 return -EFAULT;
952         if(copy_to_user(optval,&val,len))
953                 return -EFAULT;
954         return 0;
955 }
956
957 int ipv6_getsockopt(struct sock *sk, int level, int optname,
958                     char __user *optval, int __user *optlen)
959 {
960         int err;
961
962         if (level == SOL_IP && sk->sk_type != SOCK_RAW)
963                 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
964
965         if(level != SOL_IPV6)
966                 return -ENOPROTOOPT;
967
968         err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
969 #ifdef CONFIG_NETFILTER
970         /* we need to exclude all possible EINVALs except default case */
971         if (err == -EINVAL && optname != IPV6_ADDRFORM &&
972                         optname != MCAST_MSFILTER) {
973                 int len;
974
975                 if (get_user(len, optlen))
976                         return -EFAULT;
977
978                 lock_sock(sk);
979                 err = nf_getsockopt(sk, PF_INET6, optname, optval,
980                                 &len);
981                 release_sock(sk);
982                 if (err >= 0)
983                         err = put_user(len, optlen);
984         }
985 #endif
986         return err;
987 }
988
989 #ifdef CONFIG_COMPAT
990 int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
991                            char __user *optval, int __user *optlen)
992 {
993         int err;
994
995         if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
996                 if (udp_prot.compat_getsockopt != NULL)
997                         return udp_prot.compat_getsockopt(sk, level, optname,
998                                                           optval, optlen);
999                 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1000         }
1001
1002         if (level != SOL_IPV6)
1003                 return -ENOPROTOOPT;
1004
1005         err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1006 #ifdef CONFIG_NETFILTER
1007         /* we need to exclude all possible EINVALs except default case */
1008         if (err == -EINVAL && optname != IPV6_ADDRFORM &&
1009                         optname != MCAST_MSFILTER) {
1010                 int len;
1011
1012                 if (get_user(len, optlen))
1013                         return -EFAULT;
1014
1015                 lock_sock(sk);
1016                 err = compat_nf_getsockopt(sk, PF_INET6,
1017                                            optname, optval, &len);
1018                 release_sock(sk);
1019                 if (err >= 0)
1020                         err = put_user(len, optlen);
1021         }
1022 #endif
1023         return err;
1024 }
1025
1026 EXPORT_SYMBOL(compat_ipv6_getsockopt);
1027 #endif
1028
1029 void __init ipv6_packet_init(void)
1030 {
1031         dev_add_pack(&ipv6_packet_type);
1032 }
1033
1034 void ipv6_packet_cleanup(void)
1035 {
1036         dev_remove_pack(&ipv6_packet_type);
1037 }