[NET]: Fix sparse warnings
[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       Return -EINVAL for setsockopt of short lengths
20  *      o       Truncate getsockopt returns
21  *      o       Return an optlen of the truncated length if need be
22  *
23  *      Changes:
24  *      David L Stevens <dlstevens@us.ibm.com>:
25  *              - added multicast source filtering API for MLDv2
26  */
27
28 #include <linux/module.h>
29 #include <linux/config.h>
30 #include <linux/errno.h>
31 #include <linux/types.h>
32 #include <linux/socket.h>
33 #include <linux/sockios.h>
34 #include <linux/sched.h>
35 #include <linux/net.h>
36 #include <linux/in6.h>
37 #include <linux/netdevice.h>
38 #include <linux/if_arp.h>
39 #include <linux/init.h>
40 #include <linux/sysctl.h>
41 #include <linux/netfilter.h>
42
43 #include <net/sock.h>
44 #include <net/snmp.h>
45 #include <net/ipv6.h>
46 #include <net/ndisc.h>
47 #include <net/protocol.h>
48 #include <net/transp_v6.h>
49 #include <net/ip6_route.h>
50 #include <net/addrconf.h>
51 #include <net/inet_common.h>
52 #include <net/tcp.h>
53 #include <net/udp.h>
54 #include <net/xfrm.h>
55
56 #include <asm/uaccess.h>
57
58 DEFINE_SNMP_STAT(struct ipstats_mib, ipv6_statistics);
59
60 static struct packet_type ipv6_packet_type = {
61         .type = __constant_htons(ETH_P_IPV6), 
62         .func = ipv6_rcv,
63 };
64
65 struct ip6_ra_chain *ip6_ra_chain;
66 DEFINE_RWLOCK(ip6_ra_lock);
67
68 int ip6_ra_control(struct sock *sk, int sel, void (*destructor)(struct sock *))
69 {
70         struct ip6_ra_chain *ra, *new_ra, **rap;
71
72         /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
73         if (sk->sk_type != SOCK_RAW || inet_sk(sk)->num != IPPROTO_RAW)
74                 return -EINVAL;
75
76         new_ra = (sel>=0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
77
78         write_lock_bh(&ip6_ra_lock);
79         for (rap = &ip6_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
80                 if (ra->sk == sk) {
81                         if (sel>=0) {
82                                 write_unlock_bh(&ip6_ra_lock);
83                                 if (new_ra)
84                                         kfree(new_ra);
85                                 return -EADDRINUSE;
86                         }
87
88                         *rap = ra->next;
89                         write_unlock_bh(&ip6_ra_lock);
90
91                         if (ra->destructor)
92                                 ra->destructor(sk);
93                         sock_put(sk);
94                         kfree(ra);
95                         return 0;
96                 }
97         }
98         if (new_ra == NULL) {
99                 write_unlock_bh(&ip6_ra_lock);
100                 return -ENOBUFS;
101         }
102         new_ra->sk = sk;
103         new_ra->sel = sel;
104         new_ra->destructor = destructor;
105         new_ra->next = ra;
106         *rap = new_ra;
107         sock_hold(sk);
108         write_unlock_bh(&ip6_ra_lock);
109         return 0;
110 }
111
112 int ipv6_setsockopt(struct sock *sk, int level, int optname,
113                     char __user *optval, int optlen)
114 {
115         struct ipv6_pinfo *np = inet6_sk(sk);
116         int val, valbool;
117         int retv = -ENOPROTOOPT;
118
119         if (level == SOL_IP && sk->sk_type != SOCK_RAW)
120                 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
121
122         if(level!=SOL_IPV6)
123                 goto out;
124
125         if (optval == NULL)
126                 val=0;
127         else if (get_user(val, (int __user *) optval))
128                 return -EFAULT;
129
130         valbool = (val!=0);
131
132         lock_sock(sk);
133
134         switch (optname) {
135
136         case IPV6_ADDRFORM:
137                 if (val == PF_INET) {
138                         struct ipv6_txoptions *opt;
139                         struct sk_buff *pktopt;
140
141                         if (sk->sk_protocol != IPPROTO_UDP &&
142                             sk->sk_protocol != IPPROTO_TCP)
143                                 break;
144
145                         if (sk->sk_state != TCP_ESTABLISHED) {
146                                 retv = -ENOTCONN;
147                                 break;
148                         }
149
150                         if (ipv6_only_sock(sk) ||
151                             !(ipv6_addr_type(&np->daddr) & IPV6_ADDR_MAPPED)) {
152                                 retv = -EADDRNOTAVAIL;
153                                 break;
154                         }
155
156                         fl6_free_socklist(sk);
157                         ipv6_sock_mc_close(sk);
158
159                         /*
160                          * Sock is moving from IPv6 to IPv4 (sk_prot), so
161                          * remove it from the refcnt debug socks count in the
162                          * original family...
163                          */
164                         sk_refcnt_debug_dec(sk);
165
166                         if (sk->sk_protocol == IPPROTO_TCP) {
167                                 struct tcp_sock *tp = tcp_sk(sk);
168
169                                 local_bh_disable();
170                                 sock_prot_dec_use(sk->sk_prot);
171                                 sock_prot_inc_use(&tcp_prot);
172                                 local_bh_enable();
173                                 sk->sk_prot = &tcp_prot;
174                                 tp->af_specific = &ipv4_specific;
175                                 sk->sk_socket->ops = &inet_stream_ops;
176                                 sk->sk_family = PF_INET;
177                                 tcp_sync_mss(sk, tp->pmtu_cookie);
178                         } else {
179                                 local_bh_disable();
180                                 sock_prot_dec_use(sk->sk_prot);
181                                 sock_prot_inc_use(&udp_prot);
182                                 local_bh_enable();
183                                 sk->sk_prot = &udp_prot;
184                                 sk->sk_socket->ops = &inet_dgram_ops;
185                                 sk->sk_family = PF_INET;
186                         }
187                         opt = xchg(&np->opt, NULL);
188                         if (opt)
189                                 sock_kfree_s(sk, opt, opt->tot_len);
190                         pktopt = xchg(&np->pktoptions, NULL);
191                         if (pktopt)
192                                 kfree_skb(pktopt);
193
194                         sk->sk_destruct = inet_sock_destruct;
195                         /*
196                          * ... and add it to the refcnt debug socks count
197                          * in the new family. -acme
198                          */
199                         sk_refcnt_debug_inc(sk);
200                         module_put(THIS_MODULE);
201                         retv = 0;
202                         break;
203                 }
204                 goto e_inval;
205
206         case IPV6_V6ONLY:
207                 if (inet_sk(sk)->num)
208                         goto e_inval;
209                 np->ipv6only = valbool;
210                 retv = 0;
211                 break;
212
213         case IPV6_PKTINFO:
214                 np->rxopt.bits.rxinfo = valbool;
215                 retv = 0;
216                 break;
217
218         case IPV6_HOPLIMIT:
219                 np->rxopt.bits.rxhlim = valbool;
220                 retv = 0;
221                 break;
222
223         case IPV6_RTHDR:
224                 if (val < 0 || val > 2)
225                         goto e_inval;
226                 np->rxopt.bits.srcrt = val;
227                 retv = 0;
228                 break;
229
230         case IPV6_HOPOPTS:
231                 np->rxopt.bits.hopopts = valbool;
232                 retv = 0;
233                 break;
234
235         case IPV6_DSTOPTS:
236                 np->rxopt.bits.dstopts = valbool;
237                 retv = 0;
238                 break;
239
240         case IPV6_FLOWINFO:
241                 np->rxopt.bits.rxflow = valbool;
242                 retv = 0;
243                 break;
244
245         case IPV6_PKTOPTIONS:
246         {
247                 struct ipv6_txoptions *opt = NULL;
248                 struct msghdr msg;
249                 struct flowi fl;
250                 int junk;
251
252                 fl.fl6_flowlabel = 0;
253                 fl.oif = sk->sk_bound_dev_if;
254
255                 if (optlen == 0)
256                         goto update;
257
258                 /* 1K is probably excessive
259                  * 1K is surely not enough, 2K per standard header is 16K.
260                  */
261                 retv = -EINVAL;
262                 if (optlen > 64*1024)
263                         break;
264
265                 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
266                 retv = -ENOBUFS;
267                 if (opt == NULL)
268                         break;
269
270                 memset(opt, 0, sizeof(*opt));
271                 opt->tot_len = sizeof(*opt) + optlen;
272                 retv = -EFAULT;
273                 if (copy_from_user(opt+1, optval, optlen))
274                         goto done;
275
276                 msg.msg_controllen = optlen;
277                 msg.msg_control = (void*)(opt+1);
278
279                 retv = datagram_send_ctl(&msg, &fl, opt, &junk);
280                 if (retv)
281                         goto done;
282 update:
283                 retv = 0;
284                 if (sk->sk_type == SOCK_STREAM) {
285                         if (opt) {
286                                 struct tcp_sock *tp = tcp_sk(sk);
287                                 if (!((1 << sk->sk_state) &
288                                       (TCPF_LISTEN | TCPF_CLOSE))
289                                     && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
290                                         tp->ext_header_len = opt->opt_flen + opt->opt_nflen;
291                                         tcp_sync_mss(sk, tp->pmtu_cookie);
292                                 }
293                         }
294                         opt = xchg(&np->opt, opt);
295                         sk_dst_reset(sk);
296                 } else {
297                         write_lock(&sk->sk_dst_lock);
298                         opt = xchg(&np->opt, opt);
299                         write_unlock(&sk->sk_dst_lock);
300                         sk_dst_reset(sk);
301                 }
302
303 done:
304                 if (opt)
305                         sock_kfree_s(sk, opt, opt->tot_len);
306                 break;
307         }
308         case IPV6_UNICAST_HOPS:
309                 if (val > 255 || val < -1)
310                         goto e_inval;
311                 np->hop_limit = val;
312                 retv = 0;
313                 break;
314
315         case IPV6_MULTICAST_HOPS:
316                 if (sk->sk_type == SOCK_STREAM)
317                         goto e_inval;
318                 if (val > 255 || val < -1)
319                         goto e_inval;
320                 np->mcast_hops = val;
321                 retv = 0;
322                 break;
323
324         case IPV6_MULTICAST_LOOP:
325                 np->mc_loop = valbool;
326                 retv = 0;
327                 break;
328
329         case IPV6_MULTICAST_IF:
330                 if (sk->sk_type == SOCK_STREAM)
331                         goto e_inval;
332                 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
333                         goto e_inval;
334
335                 if (__dev_get_by_index(val) == NULL) {
336                         retv = -ENODEV;
337                         break;
338                 }
339                 np->mcast_oif = val;
340                 retv = 0;
341                 break;
342         case IPV6_ADD_MEMBERSHIP:
343         case IPV6_DROP_MEMBERSHIP:
344         {
345                 struct ipv6_mreq mreq;
346
347                 retv = -EFAULT;
348                 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
349                         break;
350
351                 if (optname == IPV6_ADD_MEMBERSHIP)
352                         retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
353                 else
354                         retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
355                 break;
356         }
357         case IPV6_JOIN_ANYCAST:
358         case IPV6_LEAVE_ANYCAST:
359         {
360                 struct ipv6_mreq mreq;
361
362                 if (optlen != sizeof(struct ipv6_mreq))
363                         goto e_inval;
364
365                 retv = -EFAULT;
366                 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
367                         break;
368
369                 if (optname == IPV6_JOIN_ANYCAST)
370                         retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
371                 else
372                         retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
373                 break;
374         }
375         case MCAST_JOIN_GROUP:
376         case MCAST_LEAVE_GROUP:
377         {
378                 struct group_req greq;
379                 struct sockaddr_in6 *psin6;
380
381                 retv = -EFAULT;
382                 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
383                         break;
384                 if (greq.gr_group.ss_family != AF_INET6) {
385                         retv = -EADDRNOTAVAIL;
386                         break;
387                 }
388                 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
389                 if (optname == MCAST_JOIN_GROUP)
390                         retv = ipv6_sock_mc_join(sk, greq.gr_interface,
391                                 &psin6->sin6_addr);
392                 else
393                         retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
394                                 &psin6->sin6_addr);
395                 break;
396         }
397         case MCAST_JOIN_SOURCE_GROUP:
398         case MCAST_LEAVE_SOURCE_GROUP:
399         case MCAST_BLOCK_SOURCE:
400         case MCAST_UNBLOCK_SOURCE:
401         {
402                 struct group_source_req greqs;
403                 int omode, add;
404
405                 if (optlen != sizeof(struct group_source_req))
406                         goto e_inval;
407                 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
408                         retv = -EFAULT;
409                         break;
410                 }
411                 if (greqs.gsr_group.ss_family != AF_INET6 ||
412                     greqs.gsr_source.ss_family != AF_INET6) {
413                         retv = -EADDRNOTAVAIL;
414                         break;
415                 }
416                 if (optname == MCAST_BLOCK_SOURCE) {
417                         omode = MCAST_EXCLUDE;
418                         add = 1;
419                 } else if (optname == MCAST_UNBLOCK_SOURCE) {
420                         omode = MCAST_EXCLUDE;
421                         add = 0;
422                 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
423                         struct sockaddr_in6 *psin6;
424
425                         psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
426                         retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
427                                 &psin6->sin6_addr);
428                         /* prior join w/ different source is ok */
429                         if (retv && retv != -EADDRINUSE)
430                                 break;
431                         omode = MCAST_INCLUDE;
432                         add = 1;
433                 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
434                         omode = MCAST_INCLUDE;
435                         add = 0;
436                 }
437                 retv = ip6_mc_source(add, omode, sk, &greqs);
438                 break;
439         }
440         case MCAST_MSFILTER:
441         {
442                 extern int sysctl_mld_max_msf;
443                 struct group_filter *gsf;
444
445                 if (optlen < GROUP_FILTER_SIZE(0))
446                         goto e_inval;
447                 if (optlen > sysctl_optmem_max) {
448                         retv = -ENOBUFS;
449                         break;
450                 }
451                 gsf = (struct group_filter *)kmalloc(optlen,GFP_KERNEL);
452                 if (gsf == 0) {
453                         retv = -ENOBUFS;
454                         break;
455                 }
456                 retv = -EFAULT;
457                 if (copy_from_user(gsf, optval, optlen)) {
458                         kfree(gsf);
459                         break;
460                 }
461                 /* numsrc >= (4G-140)/128 overflow in 32 bits */
462                 if (gsf->gf_numsrc >= 0x1ffffffU ||
463                     gsf->gf_numsrc > sysctl_mld_max_msf) {
464                         kfree(gsf);
465                         retv = -ENOBUFS;
466                         break;
467                 }
468                 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
469                         kfree(gsf);
470                         retv = -EINVAL;
471                         break;
472                 }
473                 retv = ip6_mc_msfilter(sk, gsf);
474                 kfree(gsf);
475
476                 break;
477         }
478         case IPV6_ROUTER_ALERT:
479                 retv = ip6_ra_control(sk, val, NULL);
480                 break;
481         case IPV6_MTU_DISCOVER:
482                 if (val<0 || val>2)
483                         goto e_inval;
484                 np->pmtudisc = val;
485                 retv = 0;
486                 break;
487         case IPV6_MTU:
488                 if (val && val < IPV6_MIN_MTU)
489                         goto e_inval;
490                 np->frag_size = val;
491                 retv = 0;
492                 break;
493         case IPV6_RECVERR:
494                 np->recverr = valbool;
495                 if (!val)
496                         skb_queue_purge(&sk->sk_error_queue);
497                 retv = 0;
498                 break;
499         case IPV6_FLOWINFO_SEND:
500                 np->sndflow = valbool;
501                 retv = 0;
502                 break;
503         case IPV6_FLOWLABEL_MGR:
504                 retv = ipv6_flowlabel_opt(sk, optval, optlen);
505                 break;
506         case IPV6_IPSEC_POLICY:
507         case IPV6_XFRM_POLICY:
508                 retv = -EPERM;
509                 if (!capable(CAP_NET_ADMIN))
510                         break;
511                 retv = xfrm_user_policy(sk, optname, optval, optlen);
512                 break;
513
514 #ifdef CONFIG_NETFILTER
515         default:
516                 retv = nf_setsockopt(sk, PF_INET6, optname, optval, 
517                                             optlen);
518                 break;
519 #endif
520
521         }
522         release_sock(sk);
523
524 out:
525         return retv;
526
527 e_inval:
528         release_sock(sk);
529         return -EINVAL;
530 }
531
532 int ipv6_getsockopt(struct sock *sk, int level, int optname,
533                     char __user *optval, int __user *optlen)
534 {
535         struct ipv6_pinfo *np = inet6_sk(sk);
536         int len;
537         int val;
538
539         if (level == SOL_IP && sk->sk_type != SOCK_RAW)
540                 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
541         if(level!=SOL_IPV6)
542                 return -ENOPROTOOPT;
543         if (get_user(len, optlen))
544                 return -EFAULT;
545         switch (optname) {
546         case IPV6_ADDRFORM:
547                 if (sk->sk_protocol != IPPROTO_UDP &&
548                     sk->sk_protocol != IPPROTO_TCP)
549                         return -EINVAL;
550                 if (sk->sk_state != TCP_ESTABLISHED)
551                         return -ENOTCONN;
552                 val = sk->sk_family;
553                 break;
554         case MCAST_MSFILTER:
555         {
556                 struct group_filter gsf;
557                 int err;
558
559                 if (len < GROUP_FILTER_SIZE(0))
560                         return -EINVAL;
561                 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
562                         return -EFAULT;
563                 lock_sock(sk);
564                 err = ip6_mc_msfget(sk, &gsf,
565                         (struct group_filter __user *)optval, optlen);
566                 release_sock(sk);
567                 return err;
568         }
569
570         case IPV6_PKTOPTIONS:
571         {
572                 struct msghdr msg;
573                 struct sk_buff *skb;
574
575                 if (sk->sk_type != SOCK_STREAM)
576                         return -ENOPROTOOPT;
577
578                 msg.msg_control = optval;
579                 msg.msg_controllen = len;
580                 msg.msg_flags = 0;
581
582                 lock_sock(sk);
583                 skb = np->pktoptions;
584                 if (skb)
585                         atomic_inc(&skb->users);
586                 release_sock(sk);
587
588                 if (skb) {
589                         int err = datagram_recv_ctl(sk, &msg, skb);
590                         kfree_skb(skb);
591                         if (err)
592                                 return err;
593                 } else {
594                         if (np->rxopt.bits.rxinfo) {
595                                 struct in6_pktinfo src_info;
596                                 src_info.ipi6_ifindex = np->mcast_oif;
597                                 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
598                                 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
599                         }
600                         if (np->rxopt.bits.rxhlim) {
601                                 int hlim = np->mcast_hops;
602                                 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
603                         }
604                 }
605                 len -= msg.msg_controllen;
606                 return put_user(len, optlen);
607         }
608         case IPV6_MTU:
609         {
610                 struct dst_entry *dst;
611                 val = 0;        
612                 lock_sock(sk);
613                 dst = sk_dst_get(sk);
614                 if (dst) {
615                         val = dst_mtu(dst);
616                         dst_release(dst);
617                 }
618                 release_sock(sk);
619                 if (!val)
620                         return -ENOTCONN;
621                 break;
622         }
623
624         case IPV6_V6ONLY:
625                 val = np->ipv6only;
626                 break;
627
628         case IPV6_PKTINFO:
629                 val = np->rxopt.bits.rxinfo;
630                 break;
631
632         case IPV6_HOPLIMIT:
633                 val = np->rxopt.bits.rxhlim;
634                 break;
635
636         case IPV6_RTHDR:
637                 val = np->rxopt.bits.srcrt;
638                 break;
639
640         case IPV6_HOPOPTS:
641                 val = np->rxopt.bits.hopopts;
642                 break;
643
644         case IPV6_DSTOPTS:
645                 val = np->rxopt.bits.dstopts;
646                 break;
647
648         case IPV6_FLOWINFO:
649                 val = np->rxopt.bits.rxflow;
650                 break;
651
652         case IPV6_UNICAST_HOPS:
653                 val = np->hop_limit;
654                 break;
655
656         case IPV6_MULTICAST_HOPS:
657                 val = np->mcast_hops;
658                 break;
659
660         case IPV6_MULTICAST_LOOP:
661                 val = np->mc_loop;
662                 break;
663
664         case IPV6_MULTICAST_IF:
665                 val = np->mcast_oif;
666                 break;
667
668         case IPV6_MTU_DISCOVER:
669                 val = np->pmtudisc;
670                 break;
671
672         case IPV6_RECVERR:
673                 val = np->recverr;
674                 break;
675
676         case IPV6_FLOWINFO_SEND:
677                 val = np->sndflow;
678                 break;
679
680         default:
681 #ifdef CONFIG_NETFILTER
682                 lock_sock(sk);
683                 val = nf_getsockopt(sk, PF_INET6, optname, optval, 
684                                     &len);
685                 release_sock(sk);
686                 if (val >= 0)
687                         val = put_user(len, optlen);
688                 return val;
689 #else
690                 return -EINVAL;
691 #endif
692         }
693         len = min_t(unsigned int, sizeof(int), len);
694         if(put_user(len, optlen))
695                 return -EFAULT;
696         if(copy_to_user(optval,&val,len))
697                 return -EFAULT;
698         return 0;
699 }
700
701 void __init ipv6_packet_init(void)
702 {
703         dev_add_pack(&ipv6_packet_type);
704 }
705
706 void ipv6_packet_cleanup(void)
707 {
708         dev_remove_pack(&ipv6_packet_type);
709 }