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