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