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