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