~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv6/ipv6_sockglue.c

Version: ~ [ linux-5.8-rc4 ] ~ [ linux-5.7.7 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.50 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.131 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.187 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.229 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.229 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 #include <linux/slab.h>
 40 
 41 #include <net/sock.h>
 42 #include <net/snmp.h>
 43 #include <net/ipv6.h>
 44 #include <net/ndisc.h>
 45 #include <net/protocol.h>
 46 #include <net/transp_v6.h>
 47 #include <net/ip6_route.h>
 48 #include <net/addrconf.h>
 49 #include <net/inet_common.h>
 50 #include <net/tcp.h>
 51 #include <net/udp.h>
 52 #include <net/udplite.h>
 53 #include <net/xfrm.h>
 54 #include <net/compat.h>
 55 #include <net/seg6.h>
 56 
 57 #include <linux/uaccess.h>
 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)->inet_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) {
 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 struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
103                                            struct ipv6_txoptions *opt)
104 {
105         if (inet_sk(sk)->is_icsk) {
106                 if (opt &&
107                     !((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
108                     inet_sk(sk)->inet_daddr != LOOPBACK4_IPV6) {
109                         struct inet_connection_sock *icsk = inet_csk(sk);
110                         icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen;
111                         icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
112                 }
113         }
114         opt = xchg((__force struct ipv6_txoptions **)&inet6_sk(sk)->opt,
115                    opt);
116         sk_dst_reset(sk);
117 
118         return opt;
119 }
120 
121 static bool setsockopt_needs_rtnl(int optname)
122 {
123         switch (optname) {
124         case IPV6_ADDRFORM:
125         case IPV6_ADD_MEMBERSHIP:
126         case IPV6_DROP_MEMBERSHIP:
127         case IPV6_JOIN_ANYCAST:
128         case IPV6_LEAVE_ANYCAST:
129         case MCAST_JOIN_GROUP:
130         case MCAST_LEAVE_GROUP:
131         case MCAST_JOIN_SOURCE_GROUP:
132         case MCAST_LEAVE_SOURCE_GROUP:
133         case MCAST_BLOCK_SOURCE:
134         case MCAST_UNBLOCK_SOURCE:
135         case MCAST_MSFILTER:
136                 return true;
137         }
138         return false;
139 }
140 
141 static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
142                     char __user *optval, unsigned int optlen)
143 {
144         struct ipv6_pinfo *np = inet6_sk(sk);
145         struct net *net = sock_net(sk);
146         int val, valbool;
147         int retv = -ENOPROTOOPT;
148         bool needs_rtnl = setsockopt_needs_rtnl(optname);
149 
150         if (!optval)
151                 val = 0;
152         else {
153                 if (optlen >= sizeof(int)) {
154                         if (get_user(val, (int __user *) optval))
155                                 return -EFAULT;
156                 } else
157                         val = 0;
158         }
159 
160         valbool = (val != 0);
161 
162         if (ip6_mroute_opt(optname))
163                 return ip6_mroute_setsockopt(sk, optname, optval, optlen);
164 
165         if (needs_rtnl)
166                 rtnl_lock();
167         lock_sock(sk);
168 
169         switch (optname) {
170 
171         case IPV6_ADDRFORM:
172                 if (optlen < sizeof(int))
173                         goto e_inval;
174                 if (val == PF_INET) {
175                         struct ipv6_txoptions *opt;
176                         struct sk_buff *pktopt;
177 
178                         if (sk->sk_type == SOCK_RAW)
179                                 break;
180 
181                         if (sk->sk_protocol == IPPROTO_UDP ||
182                             sk->sk_protocol == IPPROTO_UDPLITE) {
183                                 struct udp_sock *up = udp_sk(sk);
184                                 if (up->pending == AF_INET6) {
185                                         retv = -EBUSY;
186                                         break;
187                                 }
188                         } else if (sk->sk_protocol != IPPROTO_TCP)
189                                 break;
190 
191                         if (sk->sk_state != TCP_ESTABLISHED) {
192                                 retv = -ENOTCONN;
193                                 break;
194                         }
195 
196                         if (ipv6_only_sock(sk) ||
197                             !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
198                                 retv = -EADDRNOTAVAIL;
199                                 break;
200                         }
201 
202                         fl6_free_socklist(sk);
203                         __ipv6_sock_mc_close(sk);
204 
205                         /*
206                          * Sock is moving from IPv6 to IPv4 (sk_prot), so
207                          * remove it from the refcnt debug socks count in the
208                          * original family...
209                          */
210                         sk_refcnt_debug_dec(sk);
211 
212                         if (sk->sk_protocol == IPPROTO_TCP) {
213                                 struct inet_connection_sock *icsk = inet_csk(sk);
214                                 local_bh_disable();
215                                 sock_prot_inuse_add(net, sk->sk_prot, -1);
216                                 sock_prot_inuse_add(net, &tcp_prot, 1);
217                                 local_bh_enable();
218                                 sk->sk_prot = &tcp_prot;
219                                 icsk->icsk_af_ops = &ipv4_specific;
220                                 sk->sk_socket->ops = &inet_stream_ops;
221                                 sk->sk_family = PF_INET;
222                                 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
223                         } else {
224                                 struct proto *prot = &udp_prot;
225 
226                                 if (sk->sk_protocol == IPPROTO_UDPLITE)
227                                         prot = &udplite_prot;
228                                 local_bh_disable();
229                                 sock_prot_inuse_add(net, sk->sk_prot, -1);
230                                 sock_prot_inuse_add(net, prot, 1);
231                                 local_bh_enable();
232                                 sk->sk_prot = prot;
233                                 sk->sk_socket->ops = &inet_dgram_ops;
234                                 sk->sk_family = PF_INET;
235                         }
236                         opt = xchg((__force struct ipv6_txoptions **)&np->opt,
237                                    NULL);
238                         if (opt) {
239                                 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
240                                 txopt_put(opt);
241                         }
242                         pktopt = xchg(&np->pktoptions, NULL);
243                         kfree_skb(pktopt);
244 
245                         /*
246                          * ... and add it to the refcnt debug socks count
247                          * in the new family. -acme
248                          */
249                         sk_refcnt_debug_inc(sk);
250                         module_put(THIS_MODULE);
251                         retv = 0;
252                         break;
253                 }
254                 goto e_inval;
255 
256         case IPV6_V6ONLY:
257                 if (optlen < sizeof(int) ||
258                     inet_sk(sk)->inet_num)
259                         goto e_inval;
260                 sk->sk_ipv6only = valbool;
261                 retv = 0;
262                 break;
263 
264         case IPV6_RECVPKTINFO:
265                 if (optlen < sizeof(int))
266                         goto e_inval;
267                 np->rxopt.bits.rxinfo = valbool;
268                 retv = 0;
269                 break;
270 
271         case IPV6_2292PKTINFO:
272                 if (optlen < sizeof(int))
273                         goto e_inval;
274                 np->rxopt.bits.rxoinfo = valbool;
275                 retv = 0;
276                 break;
277 
278         case IPV6_RECVHOPLIMIT:
279                 if (optlen < sizeof(int))
280                         goto e_inval;
281                 np->rxopt.bits.rxhlim = valbool;
282                 retv = 0;
283                 break;
284 
285         case IPV6_2292HOPLIMIT:
286                 if (optlen < sizeof(int))
287                         goto e_inval;
288                 np->rxopt.bits.rxohlim = valbool;
289                 retv = 0;
290                 break;
291 
292         case IPV6_RECVRTHDR:
293                 if (optlen < sizeof(int))
294                         goto e_inval;
295                 np->rxopt.bits.srcrt = valbool;
296                 retv = 0;
297                 break;
298 
299         case IPV6_2292RTHDR:
300                 if (optlen < sizeof(int))
301                         goto e_inval;
302                 np->rxopt.bits.osrcrt = valbool;
303                 retv = 0;
304                 break;
305 
306         case IPV6_RECVHOPOPTS:
307                 if (optlen < sizeof(int))
308                         goto e_inval;
309                 np->rxopt.bits.hopopts = valbool;
310                 retv = 0;
311                 break;
312 
313         case IPV6_2292HOPOPTS:
314                 if (optlen < sizeof(int))
315                         goto e_inval;
316                 np->rxopt.bits.ohopopts = valbool;
317                 retv = 0;
318                 break;
319 
320         case IPV6_RECVDSTOPTS:
321                 if (optlen < sizeof(int))
322                         goto e_inval;
323                 np->rxopt.bits.dstopts = valbool;
324                 retv = 0;
325                 break;
326 
327         case IPV6_2292DSTOPTS:
328                 if (optlen < sizeof(int))
329                         goto e_inval;
330                 np->rxopt.bits.odstopts = valbool;
331                 retv = 0;
332                 break;
333 
334         case IPV6_TCLASS:
335                 if (optlen < sizeof(int))
336                         goto e_inval;
337                 if (val < -1 || val > 0xff)
338                         goto e_inval;
339                 /* RFC 3542, 6.5: default traffic class of 0x0 */
340                 if (val == -1)
341                         val = 0;
342                 np->tclass = val;
343                 retv = 0;
344                 break;
345 
346         case IPV6_RECVTCLASS:
347                 if (optlen < sizeof(int))
348                         goto e_inval;
349                 np->rxopt.bits.rxtclass = valbool;
350                 retv = 0;
351                 break;
352 
353         case IPV6_FLOWINFO:
354                 if (optlen < sizeof(int))
355                         goto e_inval;
356                 np->rxopt.bits.rxflow = valbool;
357                 retv = 0;
358                 break;
359 
360         case IPV6_RECVPATHMTU:
361                 if (optlen < sizeof(int))
362                         goto e_inval;
363                 np->rxopt.bits.rxpmtu = valbool;
364                 retv = 0;
365                 break;
366 
367         case IPV6_TRANSPARENT:
368                 if (valbool && !ns_capable(net->user_ns, CAP_NET_ADMIN) &&
369                     !ns_capable(net->user_ns, CAP_NET_RAW)) {
370                         retv = -EPERM;
371                         break;
372                 }
373                 if (optlen < sizeof(int))
374                         goto e_inval;
375                 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
376                 inet_sk(sk)->transparent = valbool;
377                 retv = 0;
378                 break;
379 
380         case IPV6_FREEBIND:
381                 if (optlen < sizeof(int))
382                         goto e_inval;
383                 /* we also don't have a separate freebind bit for IPV6 */
384                 inet_sk(sk)->freebind = valbool;
385                 retv = 0;
386                 break;
387 
388         case IPV6_RECVORIGDSTADDR:
389                 if (optlen < sizeof(int))
390                         goto e_inval;
391                 np->rxopt.bits.rxorigdstaddr = valbool;
392                 retv = 0;
393                 break;
394 
395         case IPV6_HOPOPTS:
396         case IPV6_RTHDRDSTOPTS:
397         case IPV6_RTHDR:
398         case IPV6_DSTOPTS:
399         {
400                 struct ipv6_txoptions *opt;
401                 struct ipv6_opt_hdr *new = NULL;
402 
403                 /* hop-by-hop / destination options are privileged option */
404                 retv = -EPERM;
405                 if (optname != IPV6_RTHDR && !ns_capable(net->user_ns, CAP_NET_RAW))
406                         break;
407 
408                 /* remove any sticky options header with a zero option
409                  * length, per RFC3542.
410                  */
411                 if (optlen == 0)
412                         optval = NULL;
413                 else if (!optval)
414                         goto e_inval;
415                 else if (optlen < sizeof(struct ipv6_opt_hdr) ||
416                          optlen & 0x7 || optlen > 8 * 255)
417                         goto e_inval;
418                 else {
419                         new = memdup_user(optval, optlen);
420                         if (IS_ERR(new)) {
421                                 retv = PTR_ERR(new);
422                                 break;
423                         }
424                         if (unlikely(ipv6_optlen(new) > optlen)) {
425                                 kfree(new);
426                                 goto e_inval;
427                         }
428                 }
429 
430                 opt = rcu_dereference_protected(np->opt,
431                                                 lockdep_sock_is_held(sk));
432                 opt = ipv6_renew_options(sk, opt, optname, new);
433                 kfree(new);
434                 if (IS_ERR(opt)) {
435                         retv = PTR_ERR(opt);
436                         break;
437                 }
438 
439                 /* routing header option needs extra check */
440                 retv = -EINVAL;
441                 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
442                         struct ipv6_rt_hdr *rthdr = opt->srcrt;
443                         switch (rthdr->type) {
444 #if IS_ENABLED(CONFIG_IPV6_MIP6)
445                         case IPV6_SRCRT_TYPE_2:
446                                 if (rthdr->hdrlen != 2 ||
447                                     rthdr->segments_left != 1)
448                                         goto sticky_done;
449 
450                                 break;
451 #endif
452                         case IPV6_SRCRT_TYPE_4:
453                         {
454                                 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)
455                                                           opt->srcrt;
456 
457                                 if (!seg6_validate_srh(srh, optlen))
458                                         goto sticky_done;
459                                 break;
460                         }
461                         default:
462                                 goto sticky_done;
463                         }
464                 }
465 
466                 retv = 0;
467                 opt = ipv6_update_options(sk, opt);
468 sticky_done:
469                 if (opt) {
470                         atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
471                         txopt_put(opt);
472                 }
473                 break;
474         }
475 
476         case IPV6_PKTINFO:
477         {
478                 struct in6_pktinfo pkt;
479 
480                 if (optlen == 0)
481                         goto e_inval;
482                 else if (optlen < sizeof(struct in6_pktinfo) || !optval)
483                         goto e_inval;
484 
485                 if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) {
486                                 retv = -EFAULT;
487                                 break;
488                 }
489                 if (sk->sk_bound_dev_if && pkt.ipi6_ifindex != sk->sk_bound_dev_if)
490                         goto e_inval;
491 
492                 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
493                 np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
494                 retv = 0;
495                 break;
496         }
497 
498         case IPV6_2292PKTOPTIONS:
499         {
500                 struct ipv6_txoptions *opt = NULL;
501                 struct msghdr msg;
502                 struct flowi6 fl6;
503                 struct sockcm_cookie sockc_junk;
504                 struct ipcm6_cookie ipc6;
505 
506                 memset(&fl6, 0, sizeof(fl6));
507                 fl6.flowi6_oif = sk->sk_bound_dev_if;
508                 fl6.flowi6_mark = sk->sk_mark;
509 
510                 if (optlen == 0)
511                         goto update;
512 
513                 /* 1K is probably excessive
514                  * 1K is surely not enough, 2K per standard header is 16K.
515                  */
516                 retv = -EINVAL;
517                 if (optlen > 64*1024)
518                         break;
519 
520                 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
521                 retv = -ENOBUFS;
522                 if (!opt)
523                         break;
524 
525                 memset(opt, 0, sizeof(*opt));
526                 refcount_set(&opt->refcnt, 1);
527                 opt->tot_len = sizeof(*opt) + optlen;
528                 retv = -EFAULT;
529                 if (copy_from_user(opt+1, optval, optlen))
530                         goto done;
531 
532                 msg.msg_controllen = optlen;
533                 msg.msg_control = (void *)(opt+1);
534                 ipc6.opt = opt;
535 
536                 retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6, &sockc_junk);
537                 if (retv)
538                         goto done;
539 update:
540                 retv = 0;
541                 opt = ipv6_update_options(sk, opt);
542 done:
543                 if (opt) {
544                         atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
545                         txopt_put(opt);
546                 }
547                 break;
548         }
549         case IPV6_UNICAST_HOPS:
550                 if (optlen < sizeof(int))
551                         goto e_inval;
552                 if (val > 255 || val < -1)
553                         goto e_inval;
554                 np->hop_limit = val;
555                 retv = 0;
556                 break;
557 
558         case IPV6_MULTICAST_HOPS:
559                 if (sk->sk_type == SOCK_STREAM)
560                         break;
561                 if (optlen < sizeof(int))
562                         goto e_inval;
563                 if (val > 255 || val < -1)
564                         goto e_inval;
565                 np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
566                 retv = 0;
567                 break;
568 
569         case IPV6_MULTICAST_LOOP:
570                 if (optlen < sizeof(int))
571                         goto e_inval;
572                 if (val != valbool)
573                         goto e_inval;
574                 np->mc_loop = valbool;
575                 retv = 0;
576                 break;
577 
578         case IPV6_UNICAST_IF:
579         {
580                 struct net_device *dev = NULL;
581                 int ifindex;
582 
583                 if (optlen != sizeof(int))
584                         goto e_inval;
585 
586                 ifindex = (__force int)ntohl((__force __be32)val);
587                 if (ifindex == 0) {
588                         np->ucast_oif = 0;
589                         retv = 0;
590                         break;
591                 }
592 
593                 dev = dev_get_by_index(net, ifindex);
594                 retv = -EADDRNOTAVAIL;
595                 if (!dev)
596                         break;
597                 dev_put(dev);
598 
599                 retv = -EINVAL;
600                 if (sk->sk_bound_dev_if)
601                         break;
602 
603                 np->ucast_oif = ifindex;
604                 retv = 0;
605                 break;
606         }
607 
608         case IPV6_MULTICAST_IF:
609                 if (sk->sk_type == SOCK_STREAM)
610                         break;
611                 if (optlen < sizeof(int))
612                         goto e_inval;
613 
614                 if (val) {
615                         struct net_device *dev;
616                         int midx;
617 
618                         rcu_read_lock();
619 
620                         dev = dev_get_by_index_rcu(net, val);
621                         if (!dev) {
622                                 rcu_read_unlock();
623                                 retv = -ENODEV;
624                                 break;
625                         }
626                         midx = l3mdev_master_ifindex_rcu(dev);
627 
628                         rcu_read_unlock();
629 
630                         if (sk->sk_bound_dev_if &&
631                             sk->sk_bound_dev_if != val &&
632                             (!midx || midx != sk->sk_bound_dev_if))
633                                 goto e_inval;
634                 }
635                 np->mcast_oif = val;
636                 retv = 0;
637                 break;
638         case IPV6_ADD_MEMBERSHIP:
639         case IPV6_DROP_MEMBERSHIP:
640         {
641                 struct ipv6_mreq mreq;
642 
643                 if (optlen < sizeof(struct ipv6_mreq))
644                         goto e_inval;
645 
646                 retv = -EPROTO;
647                 if (inet_sk(sk)->is_icsk)
648                         break;
649 
650                 retv = -EFAULT;
651                 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
652                         break;
653 
654                 if (optname == IPV6_ADD_MEMBERSHIP)
655                         retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
656                 else
657                         retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
658                 break;
659         }
660         case IPV6_JOIN_ANYCAST:
661         case IPV6_LEAVE_ANYCAST:
662         {
663                 struct ipv6_mreq mreq;
664 
665                 if (optlen < sizeof(struct ipv6_mreq))
666                         goto e_inval;
667 
668                 retv = -EFAULT;
669                 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
670                         break;
671 
672                 if (optname == IPV6_JOIN_ANYCAST)
673                         retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
674                 else
675                         retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
676                 break;
677         }
678         case MCAST_JOIN_GROUP:
679         case MCAST_LEAVE_GROUP:
680         {
681                 struct group_req greq;
682                 struct sockaddr_in6 *psin6;
683 
684                 if (optlen < sizeof(struct group_req))
685                         goto e_inval;
686 
687                 retv = -EFAULT;
688                 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
689                         break;
690                 if (greq.gr_group.ss_family != AF_INET6) {
691                         retv = -EADDRNOTAVAIL;
692                         break;
693                 }
694                 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
695                 if (optname == MCAST_JOIN_GROUP)
696                         retv = ipv6_sock_mc_join(sk, greq.gr_interface,
697                                                  &psin6->sin6_addr);
698                 else
699                         retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
700                                                  &psin6->sin6_addr);
701                 break;
702         }
703         case MCAST_JOIN_SOURCE_GROUP:
704         case MCAST_LEAVE_SOURCE_GROUP:
705         case MCAST_BLOCK_SOURCE:
706         case MCAST_UNBLOCK_SOURCE:
707         {
708                 struct group_source_req greqs;
709                 int omode, add;
710 
711                 if (optlen < sizeof(struct group_source_req))
712                         goto e_inval;
713                 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
714                         retv = -EFAULT;
715                         break;
716                 }
717                 if (greqs.gsr_group.ss_family != AF_INET6 ||
718                     greqs.gsr_source.ss_family != AF_INET6) {
719                         retv = -EADDRNOTAVAIL;
720                         break;
721                 }
722                 if (optname == MCAST_BLOCK_SOURCE) {
723                         omode = MCAST_EXCLUDE;
724                         add = 1;
725                 } else if (optname == MCAST_UNBLOCK_SOURCE) {
726                         omode = MCAST_EXCLUDE;
727                         add = 0;
728                 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
729                         struct sockaddr_in6 *psin6;
730 
731                         psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
732                         retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
733                                                  &psin6->sin6_addr);
734                         /* prior join w/ different source is ok */
735                         if (retv && retv != -EADDRINUSE)
736                                 break;
737                         omode = MCAST_INCLUDE;
738                         add = 1;
739                 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
740                         omode = MCAST_INCLUDE;
741                         add = 0;
742                 }
743                 retv = ip6_mc_source(add, omode, sk, &greqs);
744                 break;
745         }
746         case MCAST_MSFILTER:
747         {
748                 struct group_filter *gsf;
749 
750                 if (optlen < GROUP_FILTER_SIZE(0))
751                         goto e_inval;
752                 if (optlen > sysctl_optmem_max) {
753                         retv = -ENOBUFS;
754                         break;
755                 }
756                 gsf = memdup_user(optval, optlen);
757                 if (IS_ERR(gsf)) {
758                         retv = PTR_ERR(gsf);
759                         break;
760                 }
761                 /* numsrc >= (4G-140)/128 overflow in 32 bits */
762                 if (gsf->gf_numsrc >= 0x1ffffffU ||
763                     gsf->gf_numsrc > sysctl_mld_max_msf) {
764                         kfree(gsf);
765                         retv = -ENOBUFS;
766                         break;
767                 }
768                 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
769                         kfree(gsf);
770                         retv = -EINVAL;
771                         break;
772                 }
773                 retv = ip6_mc_msfilter(sk, gsf);
774                 kfree(gsf);
775 
776                 break;
777         }
778         case IPV6_ROUTER_ALERT:
779                 if (optlen < sizeof(int))
780                         goto e_inval;
781                 retv = ip6_ra_control(sk, val);
782                 break;
783         case IPV6_MTU_DISCOVER:
784                 if (optlen < sizeof(int))
785                         goto e_inval;
786                 if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
787                         goto e_inval;
788                 np->pmtudisc = val;
789                 retv = 0;
790                 break;
791         case IPV6_MTU:
792                 if (optlen < sizeof(int))
793                         goto e_inval;
794                 if (val && val < IPV6_MIN_MTU)
795                         goto e_inval;
796                 np->frag_size = val;
797                 retv = 0;
798                 break;
799         case IPV6_RECVERR:
800                 if (optlen < sizeof(int))
801                         goto e_inval;
802                 np->recverr = valbool;
803                 if (!val)
804                         skb_queue_purge(&sk->sk_error_queue);
805                 retv = 0;
806                 break;
807         case IPV6_FLOWINFO_SEND:
808                 if (optlen < sizeof(int))
809                         goto e_inval;
810                 np->sndflow = valbool;
811                 retv = 0;
812                 break;
813         case IPV6_FLOWLABEL_MGR:
814                 retv = ipv6_flowlabel_opt(sk, optval, optlen);
815                 break;
816         case IPV6_IPSEC_POLICY:
817         case IPV6_XFRM_POLICY:
818                 retv = -EPERM;
819                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
820                         break;
821                 retv = xfrm_user_policy(sk, optname, optval, optlen);
822                 break;
823 
824         case IPV6_ADDR_PREFERENCES:
825             {
826                 unsigned int pref = 0;
827                 unsigned int prefmask = ~0;
828 
829                 if (optlen < sizeof(int))
830                         goto e_inval;
831 
832                 retv = -EINVAL;
833 
834                 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
835                 switch (val & (IPV6_PREFER_SRC_PUBLIC|
836                                IPV6_PREFER_SRC_TMP|
837                                IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
838                 case IPV6_PREFER_SRC_PUBLIC:
839                         pref |= IPV6_PREFER_SRC_PUBLIC;
840                         break;
841                 case IPV6_PREFER_SRC_TMP:
842                         pref |= IPV6_PREFER_SRC_TMP;
843                         break;
844                 case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
845                         break;
846                 case 0:
847                         goto pref_skip_pubtmp;
848                 default:
849                         goto e_inval;
850                 }
851 
852                 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
853                               IPV6_PREFER_SRC_TMP);
854 pref_skip_pubtmp:
855 
856                 /* check HOME/COA conflicts */
857                 switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
858                 case IPV6_PREFER_SRC_HOME:
859                         break;
860                 case IPV6_PREFER_SRC_COA:
861                         pref |= IPV6_PREFER_SRC_COA;
862                 case 0:
863                         goto pref_skip_coa;
864                 default:
865                         goto e_inval;
866                 }
867 
868                 prefmask &= ~IPV6_PREFER_SRC_COA;
869 pref_skip_coa:
870 
871                 /* check CGA/NONCGA conflicts */
872                 switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
873                 case IPV6_PREFER_SRC_CGA:
874                 case IPV6_PREFER_SRC_NONCGA:
875                 case 0:
876                         break;
877                 default:
878                         goto e_inval;
879                 }
880 
881                 np->srcprefs = (np->srcprefs & prefmask) | pref;
882                 retv = 0;
883 
884                 break;
885             }
886         case IPV6_MINHOPCOUNT:
887                 if (optlen < sizeof(int))
888                         goto e_inval;
889                 if (val < 0 || val > 255)
890                         goto e_inval;
891                 np->min_hopcount = val;
892                 retv = 0;
893                 break;
894         case IPV6_DONTFRAG:
895                 np->dontfrag = valbool;
896                 retv = 0;
897                 break;
898         case IPV6_AUTOFLOWLABEL:
899                 np->autoflowlabel = valbool;
900                 np->autoflowlabel_set = 1;
901                 retv = 0;
902                 break;
903         case IPV6_RECVFRAGSIZE:
904                 np->rxopt.bits.recvfragsize = valbool;
905                 retv = 0;
906                 break;
907         }
908 
909         release_sock(sk);
910         if (needs_rtnl)
911                 rtnl_unlock();
912 
913         return retv;
914 
915 e_inval:
916         release_sock(sk);
917         if (needs_rtnl)
918                 rtnl_unlock();
919         return -EINVAL;
920 }
921 
922 int ipv6_setsockopt(struct sock *sk, int level, int optname,
923                     char __user *optval, unsigned int optlen)
924 {
925         int err;
926 
927         if (level == SOL_IP && sk->sk_type != SOCK_RAW)
928                 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
929 
930         if (level != SOL_IPV6)
931                 return -ENOPROTOOPT;
932 
933         err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
934 #ifdef CONFIG_NETFILTER
935         /* we need to exclude all possible ENOPROTOOPTs except default case */
936         if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
937                         optname != IPV6_XFRM_POLICY)
938                 err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen);
939 #endif
940         return err;
941 }
942 EXPORT_SYMBOL(ipv6_setsockopt);
943 
944 #ifdef CONFIG_COMPAT
945 int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
946                            char __user *optval, unsigned int optlen)
947 {
948         int err;
949 
950         if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
951                 if (udp_prot.compat_setsockopt != NULL)
952                         return udp_prot.compat_setsockopt(sk, level, optname,
953                                                           optval, optlen);
954                 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
955         }
956 
957         if (level != SOL_IPV6)
958                 return -ENOPROTOOPT;
959 
960         if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
961                 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
962                         ipv6_setsockopt);
963 
964         err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
965 #ifdef CONFIG_NETFILTER
966         /* we need to exclude all possible ENOPROTOOPTs except default case */
967         if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
968             optname != IPV6_XFRM_POLICY)
969                 err = compat_nf_setsockopt(sk, PF_INET6, optname, optval,
970                                            optlen);
971 #endif
972         return err;
973 }
974 EXPORT_SYMBOL(compat_ipv6_setsockopt);
975 #endif
976 
977 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
978                                   int optname, char __user *optval, int len)
979 {
980         struct ipv6_opt_hdr *hdr;
981 
982         if (!opt)
983                 return 0;
984 
985         switch (optname) {
986         case IPV6_HOPOPTS:
987                 hdr = opt->hopopt;
988                 break;
989         case IPV6_RTHDRDSTOPTS:
990                 hdr = opt->dst0opt;
991                 break;
992         case IPV6_RTHDR:
993                 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
994                 break;
995         case IPV6_DSTOPTS:
996                 hdr = opt->dst1opt;
997                 break;
998         default:
999                 return -EINVAL; /* should not happen */
1000         }
1001 
1002         if (!hdr)
1003                 return 0;
1004 
1005         len = min_t(unsigned int, len, ipv6_optlen(hdr));
1006         if (copy_to_user(optval, hdr, len))
1007                 return -EFAULT;
1008         return len;
1009 }
1010 
1011 static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
1012                     char __user *optval, int __user *optlen, unsigned int flags)
1013 {
1014         struct ipv6_pinfo *np = inet6_sk(sk);
1015         int len;
1016         int val;
1017 
1018         if (ip6_mroute_opt(optname))
1019                 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
1020 
1021         if (get_user(len, optlen))
1022                 return -EFAULT;
1023         switch (optname) {
1024         case IPV6_ADDRFORM:
1025                 if (sk->sk_protocol != IPPROTO_UDP &&
1026                     sk->sk_protocol != IPPROTO_UDPLITE &&
1027                     sk->sk_protocol != IPPROTO_TCP)
1028                         return -ENOPROTOOPT;
1029                 if (sk->sk_state != TCP_ESTABLISHED)
1030                         return -ENOTCONN;
1031                 val = sk->sk_family;
1032                 break;
1033         case MCAST_MSFILTER:
1034         {
1035                 struct group_filter gsf;
1036                 int err;
1037 
1038                 if (len < GROUP_FILTER_SIZE(0))
1039                         return -EINVAL;
1040                 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
1041                         return -EFAULT;
1042                 if (gsf.gf_group.ss_family != AF_INET6)
1043                         return -EADDRNOTAVAIL;
1044                 lock_sock(sk);
1045                 err = ip6_mc_msfget(sk, &gsf,
1046                         (struct group_filter __user *)optval, optlen);
1047                 release_sock(sk);
1048                 return err;
1049         }
1050 
1051         case IPV6_2292PKTOPTIONS:
1052         {
1053                 struct msghdr msg;
1054                 struct sk_buff *skb;
1055 
1056                 if (sk->sk_type != SOCK_STREAM)
1057                         return -ENOPROTOOPT;
1058 
1059                 msg.msg_control = optval;
1060                 msg.msg_controllen = len;
1061                 msg.msg_flags = flags;
1062 
1063                 lock_sock(sk);
1064                 skb = np->pktoptions;
1065                 if (skb)
1066                         ip6_datagram_recv_ctl(sk, &msg, skb);
1067                 release_sock(sk);
1068                 if (!skb) {
1069                         if (np->rxopt.bits.rxinfo) {
1070                                 struct in6_pktinfo src_info;
1071                                 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1072                                         np->sticky_pktinfo.ipi6_ifindex;
1073                                 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
1074                                 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1075                         }
1076                         if (np->rxopt.bits.rxhlim) {
1077                                 int hlim = np->mcast_hops;
1078                                 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1079                         }
1080                         if (np->rxopt.bits.rxtclass) {
1081                                 int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1082 
1083                                 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1084                         }
1085                         if (np->rxopt.bits.rxoinfo) {
1086                                 struct in6_pktinfo src_info;
1087                                 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1088                                         np->sticky_pktinfo.ipi6_ifindex;
1089                                 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1090                                                                      np->sticky_pktinfo.ipi6_addr;
1091                                 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1092                         }
1093                         if (np->rxopt.bits.rxohlim) {
1094                                 int hlim = np->mcast_hops;
1095                                 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1096                         }
1097                         if (np->rxopt.bits.rxflow) {
1098                                 __be32 flowinfo = np->rcv_flowinfo;
1099 
1100                                 put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1101                         }
1102                 }
1103                 len -= msg.msg_controllen;
1104                 return put_user(len, optlen);
1105         }
1106         case IPV6_MTU:
1107         {
1108                 struct dst_entry *dst;
1109 
1110                 val = 0;
1111                 rcu_read_lock();
1112                 dst = __sk_dst_get(sk);
1113                 if (dst)
1114                         val = dst_mtu(dst);
1115                 rcu_read_unlock();
1116                 if (!val)
1117                         return -ENOTCONN;
1118                 break;
1119         }
1120 
1121         case IPV6_V6ONLY:
1122                 val = sk->sk_ipv6only;
1123                 break;
1124 
1125         case IPV6_RECVPKTINFO:
1126                 val = np->rxopt.bits.rxinfo;
1127                 break;
1128 
1129         case IPV6_2292PKTINFO:
1130                 val = np->rxopt.bits.rxoinfo;
1131                 break;
1132 
1133         case IPV6_RECVHOPLIMIT:
1134                 val = np->rxopt.bits.rxhlim;
1135                 break;
1136 
1137         case IPV6_2292HOPLIMIT:
1138                 val = np->rxopt.bits.rxohlim;
1139                 break;
1140 
1141         case IPV6_RECVRTHDR:
1142                 val = np->rxopt.bits.srcrt;
1143                 break;
1144 
1145         case IPV6_2292RTHDR:
1146                 val = np->rxopt.bits.osrcrt;
1147                 break;
1148 
1149         case IPV6_HOPOPTS:
1150         case IPV6_RTHDRDSTOPTS:
1151         case IPV6_RTHDR:
1152         case IPV6_DSTOPTS:
1153         {
1154                 struct ipv6_txoptions *opt;
1155 
1156                 lock_sock(sk);
1157                 opt = rcu_dereference_protected(np->opt,
1158                                                 lockdep_sock_is_held(sk));
1159                 len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len);
1160                 release_sock(sk);
1161                 /* check if ipv6_getsockopt_sticky() returns err code */
1162                 if (len < 0)
1163                         return len;
1164                 return put_user(len, optlen);
1165         }
1166 
1167         case IPV6_RECVHOPOPTS:
1168                 val = np->rxopt.bits.hopopts;
1169                 break;
1170 
1171         case IPV6_2292HOPOPTS:
1172                 val = np->rxopt.bits.ohopopts;
1173                 break;
1174 
1175         case IPV6_RECVDSTOPTS:
1176                 val = np->rxopt.bits.dstopts;
1177                 break;
1178 
1179         case IPV6_2292DSTOPTS:
1180                 val = np->rxopt.bits.odstopts;
1181                 break;
1182 
1183         case IPV6_TCLASS:
1184                 val = np->tclass;
1185                 break;
1186 
1187         case IPV6_RECVTCLASS:
1188                 val = np->rxopt.bits.rxtclass;
1189                 break;
1190 
1191         case IPV6_FLOWINFO:
1192                 val = np->rxopt.bits.rxflow;
1193                 break;
1194 
1195         case IPV6_RECVPATHMTU:
1196                 val = np->rxopt.bits.rxpmtu;
1197                 break;
1198 
1199         case IPV6_PATHMTU:
1200         {
1201                 struct dst_entry *dst;
1202                 struct ip6_mtuinfo mtuinfo;
1203 
1204                 if (len < sizeof(mtuinfo))
1205                         return -EINVAL;
1206 
1207                 len = sizeof(mtuinfo);
1208                 memset(&mtuinfo, 0, sizeof(mtuinfo));
1209 
1210                 rcu_read_lock();
1211                 dst = __sk_dst_get(sk);
1212                 if (dst)
1213                         mtuinfo.ip6m_mtu = dst_mtu(dst);
1214                 rcu_read_unlock();
1215                 if (!mtuinfo.ip6m_mtu)
1216                         return -ENOTCONN;
1217 
1218                 if (put_user(len, optlen))
1219                         return -EFAULT;
1220                 if (copy_to_user(optval, &mtuinfo, len))
1221                         return -EFAULT;
1222 
1223                 return 0;
1224         }
1225 
1226         case IPV6_TRANSPARENT:
1227                 val = inet_sk(sk)->transparent;
1228                 break;
1229 
1230         case IPV6_FREEBIND:
1231                 val = inet_sk(sk)->freebind;
1232                 break;
1233 
1234         case IPV6_RECVORIGDSTADDR:
1235                 val = np->rxopt.bits.rxorigdstaddr;
1236                 break;
1237 
1238         case IPV6_UNICAST_HOPS:
1239         case IPV6_MULTICAST_HOPS:
1240         {
1241                 struct dst_entry *dst;
1242 
1243                 if (optname == IPV6_UNICAST_HOPS)
1244                         val = np->hop_limit;
1245                 else
1246                         val = np->mcast_hops;
1247 
1248                 if (val < 0) {
1249                         rcu_read_lock();
1250                         dst = __sk_dst_get(sk);
1251                         if (dst)
1252                                 val = ip6_dst_hoplimit(dst);
1253                         rcu_read_unlock();
1254                 }
1255 
1256                 if (val < 0)
1257                         val = sock_net(sk)->ipv6.devconf_all->hop_limit;
1258                 break;
1259         }
1260 
1261         case IPV6_MULTICAST_LOOP:
1262                 val = np->mc_loop;
1263                 break;
1264 
1265         case IPV6_MULTICAST_IF:
1266                 val = np->mcast_oif;
1267                 break;
1268 
1269         case IPV6_UNICAST_IF:
1270                 val = (__force int)htonl((__u32) np->ucast_oif);
1271                 break;
1272 
1273         case IPV6_MTU_DISCOVER:
1274                 val = np->pmtudisc;
1275                 break;
1276 
1277         case IPV6_RECVERR:
1278                 val = np->recverr;
1279                 break;
1280 
1281         case IPV6_FLOWINFO_SEND:
1282                 val = np->sndflow;
1283                 break;
1284 
1285         case IPV6_FLOWLABEL_MGR:
1286         {
1287                 struct in6_flowlabel_req freq;
1288                 int flags;
1289 
1290                 if (len < sizeof(freq))
1291                         return -EINVAL;
1292 
1293                 if (copy_from_user(&freq, optval, sizeof(freq)))
1294                         return -EFAULT;
1295 
1296                 if (freq.flr_action != IPV6_FL_A_GET)
1297                         return -EINVAL;
1298 
1299                 len = sizeof(freq);
1300                 flags = freq.flr_flags;
1301 
1302                 memset(&freq, 0, sizeof(freq));
1303 
1304                 val = ipv6_flowlabel_opt_get(sk, &freq, flags);
1305                 if (val < 0)
1306                         return val;
1307 
1308                 if (put_user(len, optlen))
1309                         return -EFAULT;
1310                 if (copy_to_user(optval, &freq, len))
1311                         return -EFAULT;
1312 
1313                 return 0;
1314         }
1315 
1316         case IPV6_ADDR_PREFERENCES:
1317                 val = 0;
1318 
1319                 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1320                         val |= IPV6_PREFER_SRC_TMP;
1321                 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1322                         val |= IPV6_PREFER_SRC_PUBLIC;
1323                 else {
1324                         /* XXX: should we return system default? */
1325                         val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1326                 }
1327 
1328                 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1329                         val |= IPV6_PREFER_SRC_COA;
1330                 else
1331                         val |= IPV6_PREFER_SRC_HOME;
1332                 break;
1333 
1334         case IPV6_MINHOPCOUNT:
1335                 val = np->min_hopcount;
1336                 break;
1337 
1338         case IPV6_DONTFRAG:
1339                 val = np->dontfrag;
1340                 break;
1341 
1342         case IPV6_AUTOFLOWLABEL:
1343                 val = ip6_autoflowlabel(sock_net(sk), np);
1344                 break;
1345 
1346         case IPV6_RECVFRAGSIZE:
1347                 val = np->rxopt.bits.recvfragsize;
1348                 break;
1349 
1350         default:
1351                 return -ENOPROTOOPT;
1352         }
1353         len = min_t(unsigned int, sizeof(int), len);
1354         if (put_user(len, optlen))
1355                 return -EFAULT;
1356         if (copy_to_user(optval, &val, len))
1357                 return -EFAULT;
1358         return 0;
1359 }
1360 
1361 int ipv6_getsockopt(struct sock *sk, int level, int optname,
1362                     char __user *optval, int __user *optlen)
1363 {
1364         int err;
1365 
1366         if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1367                 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1368 
1369         if (level != SOL_IPV6)
1370                 return -ENOPROTOOPT;
1371 
1372         err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
1373 #ifdef CONFIG_NETFILTER
1374         /* we need to exclude all possible ENOPROTOOPTs except default case */
1375         if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1376                 int len;
1377 
1378                 if (get_user(len, optlen))
1379                         return -EFAULT;
1380 
1381                 err = nf_getsockopt(sk, PF_INET6, optname, optval, &len);
1382                 if (err >= 0)
1383                         err = put_user(len, optlen);
1384         }
1385 #endif
1386         return err;
1387 }
1388 EXPORT_SYMBOL(ipv6_getsockopt);
1389 
1390 #ifdef CONFIG_COMPAT
1391 int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
1392                            char __user *optval, int __user *optlen)
1393 {
1394         int err;
1395 
1396         if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
1397                 if (udp_prot.compat_getsockopt != NULL)
1398                         return udp_prot.compat_getsockopt(sk, level, optname,
1399                                                           optval, optlen);
1400                 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1401         }
1402 
1403         if (level != SOL_IPV6)
1404                 return -ENOPROTOOPT;
1405 
1406         if (optname == MCAST_MSFILTER)
1407                 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1408                         ipv6_getsockopt);
1409 
1410         err = do_ipv6_getsockopt(sk, level, optname, optval, optlen,
1411                                  MSG_CMSG_COMPAT);
1412 #ifdef CONFIG_NETFILTER
1413         /* we need to exclude all possible ENOPROTOOPTs except default case */
1414         if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1415                 int len;
1416 
1417                 if (get_user(len, optlen))
1418                         return -EFAULT;
1419 
1420                 err = compat_nf_getsockopt(sk, PF_INET6, optname, optval, &len);
1421                 if (err >= 0)
1422                         err = put_user(len, optlen);
1423         }
1424 #endif
1425         return err;
1426 }
1427 EXPORT_SYMBOL(compat_ipv6_getsockopt);
1428 #endif
1429 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp