Merge branch 'drm-patches' of ssh://master.kernel.org/pub/scm/linux/kernel/git/airlie...
[linux-2.6] / net / ipv6 / ipv6_sockglue.c
1 /*
2  *      IPv6 BSD socket options interface
3  *      Linux INET6 implementation
4  *
5  *      Authors:
6  *      Pedro Roque             <roque@di.fc.ul.pt>
7  *
8  *      Based on linux/net/ipv4/ip_sockglue.c
9  *
10  *      $Id: ipv6_sockglue.c,v 1.41 2002/02/01 22:01:04 davem Exp $
11  *
12  *      This program is free software; you can redistribute it and/or
13  *      modify it under the terms of the GNU General Public License
14  *      as published by the Free Software Foundation; either version
15  *      2 of the License, or (at your option) any later version.
16  *
17  *      FIXME: Make the setsockopt code POSIX compliant: That is
18  *
19  *      o       Return -EINVAL for setsockopt of short lengths
20  *      o       Truncate getsockopt returns
21  *      o       Return an optlen of the truncated length if need be
22  *
23  *      Changes:
24  *      David L Stevens <dlstevens@us.ibm.com>:
25  *              - added multicast source filtering API for MLDv2
26  */
27
28 #include <linux/module.h>
29 #include <linux/capability.h>
30 #include <linux/errno.h>
31 #include <linux/types.h>
32 #include <linux/socket.h>
33 #include <linux/sockios.h>
34 #include <linux/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                             sk->sk_protocol != IPPROTO_UDPLITE &&
243                             sk->sk_protocol != IPPROTO_TCP)
244                                 break;
245
246                         if (sk->sk_state != TCP_ESTABLISHED) {
247                                 retv = -ENOTCONN;
248                                 break;
249                         }
250
251                         if (ipv6_only_sock(sk) ||
252                             !(ipv6_addr_type(&np->daddr) & IPV6_ADDR_MAPPED)) {
253                                 retv = -EADDRNOTAVAIL;
254                                 break;
255                         }
256
257                         fl6_free_socklist(sk);
258                         ipv6_sock_mc_close(sk);
259
260                         /*
261                          * Sock is moving from IPv6 to IPv4 (sk_prot), so
262                          * remove it from the refcnt debug socks count in the
263                          * original family...
264                          */
265                         sk_refcnt_debug_dec(sk);
266
267                         if (sk->sk_protocol == IPPROTO_TCP) {
268                                 struct inet_connection_sock *icsk = inet_csk(sk);
269
270                                 local_bh_disable();
271                                 sock_prot_dec_use(sk->sk_prot);
272                                 sock_prot_inc_use(&tcp_prot);
273                                 local_bh_enable();
274                                 sk->sk_prot = &tcp_prot;
275                                 icsk->icsk_af_ops = &ipv4_specific;
276                                 sk->sk_socket->ops = &inet_stream_ops;
277                                 sk->sk_family = PF_INET;
278                                 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
279                         } else {
280                                 struct proto *prot = &udp_prot;
281
282                                 if (sk->sk_protocol == IPPROTO_UDPLITE)
283                                         prot = &udplite_prot;
284                                 local_bh_disable();
285                                 sock_prot_dec_use(sk->sk_prot);
286                                 sock_prot_inc_use(prot);
287                                 local_bh_enable();
288                                 sk->sk_prot = prot;
289                                 sk->sk_socket->ops = &inet_dgram_ops;
290                                 sk->sk_family = PF_INET;
291                         }
292                         opt = xchg(&np->opt, NULL);
293                         if (opt)
294                                 sock_kfree_s(sk, opt, opt->tot_len);
295                         pktopt = xchg(&np->pktoptions, NULL);
296                         if (pktopt)
297                                 kfree_skb(pktopt);
298
299                         sk->sk_destruct = inet_sock_destruct;
300                         /*
301                          * ... and add it to the refcnt debug socks count
302                          * in the new family. -acme
303                          */
304                         sk_refcnt_debug_inc(sk);
305                         module_put(THIS_MODULE);
306                         retv = 0;
307                         break;
308                 }
309                 goto e_inval;
310
311         case IPV6_V6ONLY:
312                 if (inet_sk(sk)->num)
313                         goto e_inval;
314                 np->ipv6only = valbool;
315                 retv = 0;
316                 break;
317
318         case IPV6_RECVPKTINFO:
319                 np->rxopt.bits.rxinfo = valbool;
320                 retv = 0;
321                 break;
322
323         case IPV6_2292PKTINFO:
324                 np->rxopt.bits.rxoinfo = valbool;
325                 retv = 0;
326                 break;
327
328         case IPV6_RECVHOPLIMIT:
329                 np->rxopt.bits.rxhlim = valbool;
330                 retv = 0;
331                 break;
332
333         case IPV6_2292HOPLIMIT:
334                 np->rxopt.bits.rxohlim = valbool;
335                 retv = 0;
336                 break;
337
338         case IPV6_RECVRTHDR:
339                 np->rxopt.bits.srcrt = valbool;
340                 retv = 0;
341                 break;
342
343         case IPV6_2292RTHDR:
344                 np->rxopt.bits.osrcrt = valbool;
345                 retv = 0;
346                 break;
347
348         case IPV6_RECVHOPOPTS:
349                 np->rxopt.bits.hopopts = valbool;
350                 retv = 0;
351                 break;
352
353         case IPV6_2292HOPOPTS:
354                 np->rxopt.bits.ohopopts = valbool;
355                 retv = 0;
356                 break;
357
358         case IPV6_RECVDSTOPTS:
359                 np->rxopt.bits.dstopts = valbool;
360                 retv = 0;
361                 break;
362
363         case IPV6_2292DSTOPTS:
364                 np->rxopt.bits.odstopts = valbool;
365                 retv = 0;
366                 break;
367
368         case IPV6_TCLASS:
369                 if (val < -1 || val > 0xff)
370                         goto e_inval;
371                 np->tclass = val;
372                 retv = 0;
373                 break;
374
375         case IPV6_RECVTCLASS:
376                 np->rxopt.bits.rxtclass = valbool;
377                 retv = 0;
378                 break;
379
380         case IPV6_FLOWINFO:
381                 np->rxopt.bits.rxflow = valbool;
382                 retv = 0;
383                 break;
384
385         case IPV6_HOPOPTS:
386         case IPV6_RTHDRDSTOPTS:
387         case IPV6_RTHDR:
388         case IPV6_DSTOPTS:
389         {
390                 struct ipv6_txoptions *opt;
391                 if (optlen == 0)
392                         optval = NULL;
393
394                 /* hop-by-hop / destination options are privileged option */
395                 retv = -EPERM;
396                 if (optname != IPV6_RTHDR && !capable(CAP_NET_RAW))
397                         break;
398
399                 retv = -EINVAL;
400                 if (optlen & 0x7 || optlen > 8 * 255)
401                         break;
402
403                 opt = ipv6_renew_options(sk, np->opt, optname,
404                                          (struct ipv6_opt_hdr __user *)optval,
405                                          optlen);
406                 if (IS_ERR(opt)) {
407                         retv = PTR_ERR(opt);
408                         break;
409                 }
410
411                 /* routing header option needs extra check */
412                 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
413                         struct ipv6_rt_hdr *rthdr = opt->srcrt;
414                         switch (rthdr->type) {
415 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
416                         case IPV6_SRCRT_TYPE_2:
417                                 break;
418 #endif
419                         default:
420                                 goto sticky_done;
421                         }
422
423                         if ((rthdr->hdrlen & 1) ||
424                             (rthdr->hdrlen >> 1) != rthdr->segments_left)
425                                 goto sticky_done;
426                 }
427
428                 retv = 0;
429                 if (inet_sk(sk)->is_icsk) {
430                         if (opt) {
431                                 struct inet_connection_sock *icsk = inet_csk(sk);
432                                 if (!((1 << sk->sk_state) &
433                                       (TCPF_LISTEN | TCPF_CLOSE))
434                                     && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
435                                         icsk->icsk_ext_hdr_len =
436                                                 opt->opt_flen + opt->opt_nflen;
437                                         icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
438                                 }
439                         }
440                         opt = xchg(&np->opt, opt);
441                         sk_dst_reset(sk);
442                 } else {
443                         write_lock(&sk->sk_dst_lock);
444                         opt = xchg(&np->opt, opt);
445                         write_unlock(&sk->sk_dst_lock);
446                         sk_dst_reset(sk);
447                 }
448 sticky_done:
449                 if (opt)
450                         sock_kfree_s(sk, opt, opt->tot_len);
451                 break;
452         }
453
454         case IPV6_2292PKTOPTIONS:
455         {
456                 struct ipv6_txoptions *opt = NULL;
457                 struct msghdr msg;
458                 struct flowi fl;
459                 int junk;
460
461                 fl.fl6_flowlabel = 0;
462                 fl.oif = sk->sk_bound_dev_if;
463
464                 if (optlen == 0)
465                         goto update;
466
467                 /* 1K is probably excessive
468                  * 1K is surely not enough, 2K per standard header is 16K.
469                  */
470                 retv = -EINVAL;
471                 if (optlen > 64*1024)
472                         break;
473
474                 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
475                 retv = -ENOBUFS;
476                 if (opt == NULL)
477                         break;
478
479                 memset(opt, 0, sizeof(*opt));
480                 opt->tot_len = sizeof(*opt) + optlen;
481                 retv = -EFAULT;
482                 if (copy_from_user(opt+1, optval, optlen))
483                         goto done;
484
485                 msg.msg_controllen = optlen;
486                 msg.msg_control = (void*)(opt+1);
487
488                 retv = datagram_send_ctl(&msg, &fl, opt, &junk, &junk);
489                 if (retv)
490                         goto done;
491 update:
492                 retv = 0;
493                 if (inet_sk(sk)->is_icsk) {
494                         if (opt) {
495                                 struct inet_connection_sock *icsk = inet_csk(sk);
496                                 if (!((1 << sk->sk_state) &
497                                       (TCPF_LISTEN | TCPF_CLOSE))
498                                     && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
499                                         icsk->icsk_ext_hdr_len =
500                                                 opt->opt_flen + opt->opt_nflen;
501                                         icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
502                                 }
503                         }
504                         opt = xchg(&np->opt, opt);
505                         sk_dst_reset(sk);
506                 } else {
507                         write_lock(&sk->sk_dst_lock);
508                         opt = xchg(&np->opt, opt);
509                         write_unlock(&sk->sk_dst_lock);
510                         sk_dst_reset(sk);
511                 }
512
513 done:
514                 if (opt)
515                         sock_kfree_s(sk, opt, opt->tot_len);
516                 break;
517         }
518         case IPV6_UNICAST_HOPS:
519                 if (val > 255 || val < -1)
520                         goto e_inval;
521                 np->hop_limit = val;
522                 retv = 0;
523                 break;
524
525         case IPV6_MULTICAST_HOPS:
526                 if (sk->sk_type == SOCK_STREAM)
527                         goto e_inval;
528                 if (val > 255 || val < -1)
529                         goto e_inval;
530                 np->mcast_hops = val;
531                 retv = 0;
532                 break;
533
534         case IPV6_MULTICAST_LOOP:
535                 np->mc_loop = valbool;
536                 retv = 0;
537                 break;
538
539         case IPV6_MULTICAST_IF:
540                 if (sk->sk_type == SOCK_STREAM)
541                         goto e_inval;
542                 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
543                         goto e_inval;
544
545                 if (__dev_get_by_index(val) == NULL) {
546                         retv = -ENODEV;
547                         break;
548                 }
549                 np->mcast_oif = val;
550                 retv = 0;
551                 break;
552         case IPV6_ADD_MEMBERSHIP:
553         case IPV6_DROP_MEMBERSHIP:
554         {
555                 struct ipv6_mreq mreq;
556
557                 retv = -EFAULT;
558                 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
559                         break;
560
561                 if (optname == IPV6_ADD_MEMBERSHIP)
562                         retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
563                 else
564                         retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
565                 break;
566         }
567         case IPV6_JOIN_ANYCAST:
568         case IPV6_LEAVE_ANYCAST:
569         {
570                 struct ipv6_mreq mreq;
571
572                 if (optlen != sizeof(struct ipv6_mreq))
573                         goto e_inval;
574
575                 retv = -EFAULT;
576                 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
577                         break;
578
579                 if (optname == IPV6_JOIN_ANYCAST)
580                         retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
581                 else
582                         retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
583                 break;
584         }
585         case MCAST_JOIN_GROUP:
586         case MCAST_LEAVE_GROUP:
587         {
588                 struct group_req greq;
589                 struct sockaddr_in6 *psin6;
590
591                 retv = -EFAULT;
592                 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
593                         break;
594                 if (greq.gr_group.ss_family != AF_INET6) {
595                         retv = -EADDRNOTAVAIL;
596                         break;
597                 }
598                 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
599                 if (optname == MCAST_JOIN_GROUP)
600                         retv = ipv6_sock_mc_join(sk, greq.gr_interface,
601                                 &psin6->sin6_addr);
602                 else
603                         retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
604                                 &psin6->sin6_addr);
605                 break;
606         }
607         case MCAST_JOIN_SOURCE_GROUP:
608         case MCAST_LEAVE_SOURCE_GROUP:
609         case MCAST_BLOCK_SOURCE:
610         case MCAST_UNBLOCK_SOURCE:
611         {
612                 struct group_source_req greqs;
613                 int omode, add;
614
615                 if (optlen != sizeof(struct group_source_req))
616                         goto e_inval;
617                 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
618                         retv = -EFAULT;
619                         break;
620                 }
621                 if (greqs.gsr_group.ss_family != AF_INET6 ||
622                     greqs.gsr_source.ss_family != AF_INET6) {
623                         retv = -EADDRNOTAVAIL;
624                         break;
625                 }
626                 if (optname == MCAST_BLOCK_SOURCE) {
627                         omode = MCAST_EXCLUDE;
628                         add = 1;
629                 } else if (optname == MCAST_UNBLOCK_SOURCE) {
630                         omode = MCAST_EXCLUDE;
631                         add = 0;
632                 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
633                         struct sockaddr_in6 *psin6;
634
635                         psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
636                         retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
637                                 &psin6->sin6_addr);
638                         /* prior join w/ different source is ok */
639                         if (retv && retv != -EADDRINUSE)
640                                 break;
641                         omode = MCAST_INCLUDE;
642                         add = 1;
643                 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
644                         omode = MCAST_INCLUDE;
645                         add = 0;
646                 }
647                 retv = ip6_mc_source(add, omode, sk, &greqs);
648                 break;
649         }
650         case MCAST_MSFILTER:
651         {
652                 extern int sysctl_mld_max_msf;
653                 struct group_filter *gsf;
654
655                 if (optlen < GROUP_FILTER_SIZE(0))
656                         goto e_inval;
657                 if (optlen > sysctl_optmem_max) {
658                         retv = -ENOBUFS;
659                         break;
660                 }
661                 gsf = kmalloc(optlen,GFP_KERNEL);
662                 if (gsf == 0) {
663                         retv = -ENOBUFS;
664                         break;
665                 }
666                 retv = -EFAULT;
667                 if (copy_from_user(gsf, optval, optlen)) {
668                         kfree(gsf);
669                         break;
670                 }
671                 /* numsrc >= (4G-140)/128 overflow in 32 bits */
672                 if (gsf->gf_numsrc >= 0x1ffffffU ||
673                     gsf->gf_numsrc > sysctl_mld_max_msf) {
674                         kfree(gsf);
675                         retv = -ENOBUFS;
676                         break;
677                 }
678                 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
679                         kfree(gsf);
680                         retv = -EINVAL;
681                         break;
682                 }
683                 retv = ip6_mc_msfilter(sk, gsf);
684                 kfree(gsf);
685
686                 break;
687         }
688         case IPV6_ROUTER_ALERT:
689                 retv = ip6_ra_control(sk, val, NULL);
690                 break;
691         case IPV6_MTU_DISCOVER:
692                 if (val<0 || val>3)
693                         goto e_inval;
694                 np->pmtudisc = val;
695                 retv = 0;
696                 break;
697         case IPV6_MTU:
698                 if (val && val < IPV6_MIN_MTU)
699                         goto e_inval;
700                 np->frag_size = val;
701                 retv = 0;
702                 break;
703         case IPV6_RECVERR:
704                 np->recverr = valbool;
705                 if (!val)
706                         skb_queue_purge(&sk->sk_error_queue);
707                 retv = 0;
708                 break;
709         case IPV6_FLOWINFO_SEND:
710                 np->sndflow = valbool;
711                 retv = 0;
712                 break;
713         case IPV6_FLOWLABEL_MGR:
714                 retv = ipv6_flowlabel_opt(sk, optval, optlen);
715                 break;
716         case IPV6_IPSEC_POLICY:
717         case IPV6_XFRM_POLICY:
718                 retv = -EPERM;
719                 if (!capable(CAP_NET_ADMIN))
720                         break;
721                 retv = xfrm_user_policy(sk, optname, optval, optlen);
722                 break;
723
724         }
725         release_sock(sk);
726
727         return retv;
728
729 e_inval:
730         release_sock(sk);
731         return -EINVAL;
732 }
733
734 int ipv6_setsockopt(struct sock *sk, int level, int optname,
735                     char __user *optval, int optlen)
736 {
737         int err;
738
739         if (level == SOL_IP && sk->sk_type != SOCK_RAW)
740                 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
741
742         if (level != SOL_IPV6)
743                 return -ENOPROTOOPT;
744
745         err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
746 #ifdef CONFIG_NETFILTER
747         /* we need to exclude all possible ENOPROTOOPTs except default case */
748         if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
749                         optname != IPV6_XFRM_POLICY) {
750                 lock_sock(sk);
751                 err = nf_setsockopt(sk, PF_INET6, optname, optval,
752                                 optlen);
753                 release_sock(sk);
754         }
755 #endif
756         return err;
757 }
758
759 EXPORT_SYMBOL(ipv6_setsockopt);
760
761 #ifdef CONFIG_COMPAT
762 int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
763                            char __user *optval, int optlen)
764 {
765         int err;
766
767         if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
768                 if (udp_prot.compat_setsockopt != NULL)
769                         return udp_prot.compat_setsockopt(sk, level, optname,
770                                                           optval, optlen);
771                 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
772         }
773
774         if (level != SOL_IPV6)
775                 return -ENOPROTOOPT;
776
777         err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
778 #ifdef CONFIG_NETFILTER
779         /* we need to exclude all possible ENOPROTOOPTs except default case */
780         if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
781             optname != IPV6_XFRM_POLICY) {
782                 lock_sock(sk);
783                 err = compat_nf_setsockopt(sk, PF_INET6, optname,
784                                            optval, optlen);
785                 release_sock(sk);
786         }
787 #endif
788         return err;
789 }
790
791 EXPORT_SYMBOL(compat_ipv6_setsockopt);
792 #endif
793
794 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
795                                   int optname, char __user *optval, int len)
796 {
797         struct ipv6_opt_hdr *hdr;
798
799         if (!opt)
800                 return 0;
801
802         switch(optname) {
803         case IPV6_HOPOPTS:
804                 hdr = opt->hopopt;
805                 break;
806         case IPV6_RTHDRDSTOPTS:
807                 hdr = opt->dst0opt;
808                 break;
809         case IPV6_RTHDR:
810                 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
811                 break;
812         case IPV6_DSTOPTS:
813                 hdr = opt->dst1opt;
814                 break;
815         default:
816                 return -EINVAL; /* should not happen */
817         }
818
819         if (!hdr)
820                 return 0;
821
822         len = min_t(unsigned int, len, ipv6_optlen(hdr));
823         if (copy_to_user(optval, hdr, len));
824                 return -EFAULT;
825         return ipv6_optlen(hdr);
826 }
827
828 static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
829                     char __user *optval, int __user *optlen)
830 {
831         struct ipv6_pinfo *np = inet6_sk(sk);
832         int len;
833         int val;
834
835         if (get_user(len, optlen))
836                 return -EFAULT;
837         switch (optname) {
838         case IPV6_ADDRFORM:
839                 if (sk->sk_protocol != IPPROTO_UDP &&
840                     sk->sk_protocol != IPPROTO_UDPLITE &&
841                     sk->sk_protocol != IPPROTO_TCP)
842                         return -EINVAL;
843                 if (sk->sk_state != TCP_ESTABLISHED)
844                         return -ENOTCONN;
845                 val = sk->sk_family;
846                 break;
847         case MCAST_MSFILTER:
848         {
849                 struct group_filter gsf;
850                 int err;
851
852                 if (len < GROUP_FILTER_SIZE(0))
853                         return -EINVAL;
854                 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
855                         return -EFAULT;
856                 lock_sock(sk);
857                 err = ip6_mc_msfget(sk, &gsf,
858                         (struct group_filter __user *)optval, optlen);
859                 release_sock(sk);
860                 return err;
861         }
862
863         case IPV6_2292PKTOPTIONS:
864         {
865                 struct msghdr msg;
866                 struct sk_buff *skb;
867
868                 if (sk->sk_type != SOCK_STREAM)
869                         return -ENOPROTOOPT;
870
871                 msg.msg_control = optval;
872                 msg.msg_controllen = len;
873                 msg.msg_flags = 0;
874
875                 lock_sock(sk);
876                 skb = np->pktoptions;
877                 if (skb)
878                         atomic_inc(&skb->users);
879                 release_sock(sk);
880
881                 if (skb) {
882                         int err = datagram_recv_ctl(sk, &msg, skb);
883                         kfree_skb(skb);
884                         if (err)
885                                 return err;
886                 } else {
887                         if (np->rxopt.bits.rxinfo) {
888                                 struct in6_pktinfo src_info;
889                                 src_info.ipi6_ifindex = np->mcast_oif;
890                                 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
891                                 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
892                         }
893                         if (np->rxopt.bits.rxhlim) {
894                                 int hlim = np->mcast_hops;
895                                 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
896                         }
897                         if (np->rxopt.bits.rxoinfo) {
898                                 struct in6_pktinfo src_info;
899                                 src_info.ipi6_ifindex = np->mcast_oif;
900                                 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
901                                 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
902                         }
903                         if (np->rxopt.bits.rxohlim) {
904                                 int hlim = np->mcast_hops;
905                                 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
906                         }
907                 }
908                 len -= msg.msg_controllen;
909                 return put_user(len, optlen);
910         }
911         case IPV6_MTU:
912         {
913                 struct dst_entry *dst;
914                 val = 0;
915                 lock_sock(sk);
916                 dst = sk_dst_get(sk);
917                 if (dst) {
918                         val = dst_mtu(dst);
919                         dst_release(dst);
920                 }
921                 release_sock(sk);
922                 if (!val)
923                         return -ENOTCONN;
924                 break;
925         }
926
927         case IPV6_V6ONLY:
928                 val = np->ipv6only;
929                 break;
930
931         case IPV6_RECVPKTINFO:
932                 val = np->rxopt.bits.rxinfo;
933                 break;
934
935         case IPV6_2292PKTINFO:
936                 val = np->rxopt.bits.rxoinfo;
937                 break;
938
939         case IPV6_RECVHOPLIMIT:
940                 val = np->rxopt.bits.rxhlim;
941                 break;
942
943         case IPV6_2292HOPLIMIT:
944                 val = np->rxopt.bits.rxohlim;
945                 break;
946
947         case IPV6_RECVRTHDR:
948                 val = np->rxopt.bits.srcrt;
949                 break;
950
951         case IPV6_2292RTHDR:
952                 val = np->rxopt.bits.osrcrt;
953                 break;
954
955         case IPV6_HOPOPTS:
956         case IPV6_RTHDRDSTOPTS:
957         case IPV6_RTHDR:
958         case IPV6_DSTOPTS:
959         {
960
961                 lock_sock(sk);
962                 len = ipv6_getsockopt_sticky(sk, np->opt,
963                                              optname, optval, len);
964                 release_sock(sk);
965                 return put_user(len, optlen);
966         }
967
968         case IPV6_RECVHOPOPTS:
969                 val = np->rxopt.bits.hopopts;
970                 break;
971
972         case IPV6_2292HOPOPTS:
973                 val = np->rxopt.bits.ohopopts;
974                 break;
975
976         case IPV6_RECVDSTOPTS:
977                 val = np->rxopt.bits.dstopts;
978                 break;
979
980         case IPV6_2292DSTOPTS:
981                 val = np->rxopt.bits.odstopts;
982                 break;
983
984         case IPV6_TCLASS:
985                 val = np->tclass;
986                 if (val < 0)
987                         val = 0;
988                 break;
989
990         case IPV6_RECVTCLASS:
991                 val = np->rxopt.bits.rxtclass;
992                 break;
993
994         case IPV6_FLOWINFO:
995                 val = np->rxopt.bits.rxflow;
996                 break;
997
998         case IPV6_UNICAST_HOPS:
999         case IPV6_MULTICAST_HOPS:
1000         {
1001                 struct dst_entry *dst;
1002
1003                 if (optname == IPV6_UNICAST_HOPS)
1004                         val = np->hop_limit;
1005                 else
1006                         val = np->mcast_hops;
1007
1008                 dst = sk_dst_get(sk);
1009                 if (dst) {
1010                         if (val < 0)
1011                                 val = dst_metric(dst, RTAX_HOPLIMIT);
1012                         if (val < 0)
1013                                 val = ipv6_get_hoplimit(dst->dev);
1014                         dst_release(dst);
1015                 }
1016                 if (val < 0)
1017                         val = ipv6_devconf.hop_limit;
1018                 break;
1019         }
1020
1021         case IPV6_MULTICAST_LOOP:
1022                 val = np->mc_loop;
1023                 break;
1024
1025         case IPV6_MULTICAST_IF:
1026                 val = np->mcast_oif;
1027                 break;
1028
1029         case IPV6_MTU_DISCOVER:
1030                 val = np->pmtudisc;
1031                 break;
1032
1033         case IPV6_RECVERR:
1034                 val = np->recverr;
1035                 break;
1036
1037         case IPV6_FLOWINFO_SEND:
1038                 val = np->sndflow;
1039                 break;
1040
1041         default:
1042                 return -EINVAL;
1043         }
1044         len = min_t(unsigned int, sizeof(int), len);
1045         if(put_user(len, optlen))
1046                 return -EFAULT;
1047         if(copy_to_user(optval,&val,len))
1048                 return -EFAULT;
1049         return 0;
1050 }
1051
1052 int 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                 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1059
1060         if(level != SOL_IPV6)
1061                 return -ENOPROTOOPT;
1062
1063         err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1064 #ifdef CONFIG_NETFILTER
1065         /* we need to exclude all possible EINVALs except default case */
1066         if (err == -EINVAL && optname != IPV6_ADDRFORM &&
1067                         optname != MCAST_MSFILTER) {
1068                 int len;
1069
1070                 if (get_user(len, optlen))
1071                         return -EFAULT;
1072
1073                 lock_sock(sk);
1074                 err = nf_getsockopt(sk, PF_INET6, optname, optval,
1075                                 &len);
1076                 release_sock(sk);
1077                 if (err >= 0)
1078                         err = put_user(len, optlen);
1079         }
1080 #endif
1081         return err;
1082 }
1083
1084 EXPORT_SYMBOL(ipv6_getsockopt);
1085
1086 #ifdef CONFIG_COMPAT
1087 int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
1088                            char __user *optval, int __user *optlen)
1089 {
1090         int err;
1091
1092         if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
1093                 if (udp_prot.compat_getsockopt != NULL)
1094                         return udp_prot.compat_getsockopt(sk, level, optname,
1095                                                           optval, optlen);
1096                 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1097         }
1098
1099         if (level != SOL_IPV6)
1100                 return -ENOPROTOOPT;
1101
1102         err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1103 #ifdef CONFIG_NETFILTER
1104         /* we need to exclude all possible EINVALs except default case */
1105         if (err == -EINVAL && optname != IPV6_ADDRFORM &&
1106                         optname != MCAST_MSFILTER) {
1107                 int len;
1108
1109                 if (get_user(len, optlen))
1110                         return -EFAULT;
1111
1112                 lock_sock(sk);
1113                 err = compat_nf_getsockopt(sk, PF_INET6,
1114                                            optname, optval, &len);
1115                 release_sock(sk);
1116                 if (err >= 0)
1117                         err = put_user(len, optlen);
1118         }
1119 #endif
1120         return err;
1121 }
1122
1123 EXPORT_SYMBOL(compat_ipv6_getsockopt);
1124 #endif
1125
1126 void __init ipv6_packet_init(void)
1127 {
1128         dev_add_pack(&ipv6_packet_type);
1129 }
1130
1131 void ipv6_packet_cleanup(void)
1132 {
1133         dev_remove_pack(&ipv6_packet_type);
1134 }