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