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