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