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