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