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

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

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ 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.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

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

kernel.org | git.kernel.org | LWN.net | Project Home | 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.

sflogo.php