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