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