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

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

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

~ [ 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