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(&np->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 np->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 < IP_PMTUDISC_DONT || val > IP_PMTUDISC_PROBE)
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 }
849
850 release_sock(sk);
851
852 return retv;
853
854 e_inval:
855 release_sock(sk);
856 return -EINVAL;
857 }
858
ipv6_setsockopt(struct sock * sk,int level,int optname,char __user * optval,unsigned int optlen)859 int ipv6_setsockopt(struct sock *sk, int level, int optname,
860 char __user *optval, unsigned int optlen)
861 {
862 int err;
863
864 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
865 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
866
867 if (level != SOL_IPV6)
868 return -ENOPROTOOPT;
869
870 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
871 #ifdef CONFIG_NETFILTER
872 /* we need to exclude all possible ENOPROTOOPTs except default case */
873 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
874 optname != IPV6_XFRM_POLICY) {
875 lock_sock(sk);
876 err = nf_setsockopt(sk, PF_INET6, optname, optval,
877 optlen);
878 release_sock(sk);
879 }
880 #endif
881 return err;
882 }
883
884 EXPORT_SYMBOL(ipv6_setsockopt);
885
886 #ifdef CONFIG_COMPAT
compat_ipv6_setsockopt(struct sock * sk,int level,int optname,char __user * optval,unsigned int optlen)887 int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
888 char __user *optval, unsigned int optlen)
889 {
890 int err;
891
892 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
893 if (udp_prot.compat_setsockopt != NULL)
894 return udp_prot.compat_setsockopt(sk, level, optname,
895 optval, optlen);
896 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
897 }
898
899 if (level != SOL_IPV6)
900 return -ENOPROTOOPT;
901
902 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
903 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
904 ipv6_setsockopt);
905
906 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
907 #ifdef CONFIG_NETFILTER
908 /* we need to exclude all possible ENOPROTOOPTs except default case */
909 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
910 optname != IPV6_XFRM_POLICY) {
911 lock_sock(sk);
912 err = compat_nf_setsockopt(sk, PF_INET6, optname,
913 optval, optlen);
914 release_sock(sk);
915 }
916 #endif
917 return err;
918 }
919
920 EXPORT_SYMBOL(compat_ipv6_setsockopt);
921 #endif
922
ipv6_getsockopt_sticky(struct sock * sk,struct ipv6_txoptions * opt,int optname,char __user * optval,int len)923 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
924 int optname, char __user *optval, int len)
925 {
926 struct ipv6_opt_hdr *hdr;
927
928 if (!opt)
929 return 0;
930
931 switch(optname) {
932 case IPV6_HOPOPTS:
933 hdr = opt->hopopt;
934 break;
935 case IPV6_RTHDRDSTOPTS:
936 hdr = opt->dst0opt;
937 break;
938 case IPV6_RTHDR:
939 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
940 break;
941 case IPV6_DSTOPTS:
942 hdr = opt->dst1opt;
943 break;
944 default:
945 return -EINVAL; /* should not happen */
946 }
947
948 if (!hdr)
949 return 0;
950
951 len = min_t(unsigned int, len, ipv6_optlen(hdr));
952 if (copy_to_user(optval, hdr, len))
953 return -EFAULT;
954 return len;
955 }
956
do_ipv6_getsockopt(struct sock * sk,int level,int optname,char __user * optval,int __user * optlen,unsigned int flags)957 static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
958 char __user *optval, int __user *optlen, unsigned int flags)
959 {
960 struct ipv6_pinfo *np = inet6_sk(sk);
961 int len;
962 int val;
963
964 if (ip6_mroute_opt(optname))
965 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
966
967 if (get_user(len, optlen))
968 return -EFAULT;
969 switch (optname) {
970 case IPV6_ADDRFORM:
971 if (sk->sk_protocol != IPPROTO_UDP &&
972 sk->sk_protocol != IPPROTO_UDPLITE &&
973 sk->sk_protocol != IPPROTO_TCP)
974 return -ENOPROTOOPT;
975 if (sk->sk_state != TCP_ESTABLISHED)
976 return -ENOTCONN;
977 val = sk->sk_family;
978 break;
979 case MCAST_MSFILTER:
980 {
981 struct group_filter gsf;
982 int err;
983
984 if (len < GROUP_FILTER_SIZE(0))
985 return -EINVAL;
986 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
987 return -EFAULT;
988 if (gsf.gf_group.ss_family != AF_INET6)
989 return -EADDRNOTAVAIL;
990 lock_sock(sk);
991 err = ip6_mc_msfget(sk, &gsf,
992 (struct group_filter __user *)optval, optlen);
993 release_sock(sk);
994 return err;
995 }
996
997 case IPV6_2292PKTOPTIONS:
998 {
999 struct msghdr msg;
1000 struct sk_buff *skb;
1001
1002 if (sk->sk_type != SOCK_STREAM)
1003 return -ENOPROTOOPT;
1004
1005 msg.msg_control = optval;
1006 msg.msg_controllen = len;
1007 msg.msg_flags = flags;
1008
1009 lock_sock(sk);
1010 skb = np->pktoptions;
1011 if (skb)
1012 atomic_inc(&skb->users);
1013 release_sock(sk);
1014
1015 if (skb) {
1016 int err = ip6_datagram_recv_ctl(sk, &msg, skb);
1017 kfree_skb(skb);
1018 if (err)
1019 return err;
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 ? np->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 = np->rcv_tclass;
1034 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1035 }
1036 if (np->rxopt.bits.rxoinfo) {
1037 struct in6_pktinfo src_info;
1038 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1039 np->sticky_pktinfo.ipi6_ifindex;
1040 src_info.ipi6_addr = np->mcast_oif ? np->daddr : np->sticky_pktinfo.ipi6_addr;
1041 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1042 }
1043 if (np->rxopt.bits.rxohlim) {
1044 int hlim = np->mcast_hops;
1045 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1046 }
1047 }
1048 len -= msg.msg_controllen;
1049 return put_user(len, optlen);
1050 }
1051 case IPV6_MTU:
1052 {
1053 struct dst_entry *dst;
1054
1055 val = 0;
1056 rcu_read_lock();
1057 dst = __sk_dst_get(sk);
1058 if (dst)
1059 val = dst_mtu(dst);
1060 rcu_read_unlock();
1061 if (!val)
1062 return -ENOTCONN;
1063 break;
1064 }
1065
1066 case IPV6_V6ONLY:
1067 val = np->ipv6only;
1068 break;
1069
1070 case IPV6_RECVPKTINFO:
1071 val = np->rxopt.bits.rxinfo;
1072 break;
1073
1074 case IPV6_2292PKTINFO:
1075 val = np->rxopt.bits.rxoinfo;
1076 break;
1077
1078 case IPV6_RECVHOPLIMIT:
1079 val = np->rxopt.bits.rxhlim;
1080 break;
1081
1082 case IPV6_2292HOPLIMIT:
1083 val = np->rxopt.bits.rxohlim;
1084 break;
1085
1086 case IPV6_RECVRTHDR:
1087 val = np->rxopt.bits.srcrt;
1088 break;
1089
1090 case IPV6_2292RTHDR:
1091 val = np->rxopt.bits.osrcrt;
1092 break;
1093
1094 case IPV6_HOPOPTS:
1095 case IPV6_RTHDRDSTOPTS:
1096 case IPV6_RTHDR:
1097 case IPV6_DSTOPTS:
1098 {
1099 struct ipv6_txoptions *opt;
1100
1101 lock_sock(sk);
1102 opt = rcu_dereference_protected(np->opt, sock_owned_by_user(sk));
1103 len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len);
1104 release_sock(sk);
1105 /* check if ipv6_getsockopt_sticky() returns err code */
1106 if (len < 0)
1107 return len;
1108 return put_user(len, optlen);
1109 }
1110
1111 case IPV6_RECVHOPOPTS:
1112 val = np->rxopt.bits.hopopts;
1113 break;
1114
1115 case IPV6_2292HOPOPTS:
1116 val = np->rxopt.bits.ohopopts;
1117 break;
1118
1119 case IPV6_RECVDSTOPTS:
1120 val = np->rxopt.bits.dstopts;
1121 break;
1122
1123 case IPV6_2292DSTOPTS:
1124 val = np->rxopt.bits.odstopts;
1125 break;
1126
1127 case IPV6_TCLASS:
1128 val = np->tclass;
1129 break;
1130
1131 case IPV6_RECVTCLASS:
1132 val = np->rxopt.bits.rxtclass;
1133 break;
1134
1135 case IPV6_FLOWINFO:
1136 val = np->rxopt.bits.rxflow;
1137 break;
1138
1139 case IPV6_RECVPATHMTU:
1140 val = np->rxopt.bits.rxpmtu;
1141 break;
1142
1143 case IPV6_PATHMTU:
1144 {
1145 struct dst_entry *dst;
1146 struct ip6_mtuinfo mtuinfo;
1147
1148 if (len < sizeof(mtuinfo))
1149 return -EINVAL;
1150
1151 len = sizeof(mtuinfo);
1152 memset(&mtuinfo, 0, sizeof(mtuinfo));
1153
1154 rcu_read_lock();
1155 dst = __sk_dst_get(sk);
1156 if (dst)
1157 mtuinfo.ip6m_mtu = dst_mtu(dst);
1158 rcu_read_unlock();
1159 if (!mtuinfo.ip6m_mtu)
1160 return -ENOTCONN;
1161
1162 if (put_user(len, optlen))
1163 return -EFAULT;
1164 if (copy_to_user(optval, &mtuinfo, len))
1165 return -EFAULT;
1166
1167 return 0;
1168 break;
1169 }
1170
1171 case IPV6_TRANSPARENT:
1172 val = inet_sk(sk)->transparent;
1173 break;
1174
1175 case IPV6_RECVORIGDSTADDR:
1176 val = np->rxopt.bits.rxorigdstaddr;
1177 break;
1178
1179 case IPV6_UNICAST_HOPS:
1180 case IPV6_MULTICAST_HOPS:
1181 {
1182 struct dst_entry *dst;
1183
1184 if (optname == IPV6_UNICAST_HOPS)
1185 val = np->hop_limit;
1186 else
1187 val = np->mcast_hops;
1188
1189 if (val < 0) {
1190 rcu_read_lock();
1191 dst = __sk_dst_get(sk);
1192 if (dst)
1193 val = ip6_dst_hoplimit(dst);
1194 rcu_read_unlock();
1195 }
1196
1197 if (val < 0)
1198 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
1199 break;
1200 }
1201
1202 case IPV6_MULTICAST_LOOP:
1203 val = np->mc_loop;
1204 break;
1205
1206 case IPV6_MULTICAST_IF:
1207 val = np->mcast_oif;
1208 break;
1209
1210 case IPV6_UNICAST_IF:
1211 val = (__force int)htonl((__u32) np->ucast_oif);
1212 break;
1213
1214 case IPV6_MTU_DISCOVER:
1215 val = np->pmtudisc;
1216 break;
1217
1218 case IPV6_RECVERR:
1219 val = np->recverr;
1220 break;
1221
1222 case IPV6_FLOWINFO_SEND:
1223 val = np->sndflow;
1224 break;
1225
1226 case IPV6_ADDR_PREFERENCES:
1227 val = 0;
1228
1229 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1230 val |= IPV6_PREFER_SRC_TMP;
1231 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1232 val |= IPV6_PREFER_SRC_PUBLIC;
1233 else {
1234 /* XXX: should we return system default? */
1235 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1236 }
1237
1238 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1239 val |= IPV6_PREFER_SRC_COA;
1240 else
1241 val |= IPV6_PREFER_SRC_HOME;
1242 break;
1243
1244 case IPV6_MINHOPCOUNT:
1245 val = np->min_hopcount;
1246 break;
1247
1248 case IPV6_DONTFRAG:
1249 val = np->dontfrag;
1250 break;
1251
1252 default:
1253 return -ENOPROTOOPT;
1254 }
1255 len = min_t(unsigned int, sizeof(int), len);
1256 if(put_user(len, optlen))
1257 return -EFAULT;
1258 if(copy_to_user(optval,&val,len))
1259 return -EFAULT;
1260 return 0;
1261 }
1262
ipv6_getsockopt(struct sock * sk,int level,int optname,char __user * optval,int __user * optlen)1263 int ipv6_getsockopt(struct sock *sk, int level, int optname,
1264 char __user *optval, int __user *optlen)
1265 {
1266 int err;
1267
1268 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1269 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1270
1271 if(level != SOL_IPV6)
1272 return -ENOPROTOOPT;
1273
1274 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
1275 #ifdef CONFIG_NETFILTER
1276 /* we need to exclude all possible ENOPROTOOPTs except default case */
1277 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1278 int len;
1279
1280 if (get_user(len, optlen))
1281 return -EFAULT;
1282
1283 lock_sock(sk);
1284 err = nf_getsockopt(sk, PF_INET6, optname, optval,
1285 &len);
1286 release_sock(sk);
1287 if (err >= 0)
1288 err = put_user(len, optlen);
1289 }
1290 #endif
1291 return err;
1292 }
1293
1294 EXPORT_SYMBOL(ipv6_getsockopt);
1295
1296 #ifdef CONFIG_COMPAT
compat_ipv6_getsockopt(struct sock * sk,int level,int optname,char __user * optval,int __user * optlen)1297 int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
1298 char __user *optval, int __user *optlen)
1299 {
1300 int err;
1301
1302 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
1303 if (udp_prot.compat_getsockopt != NULL)
1304 return udp_prot.compat_getsockopt(sk, level, optname,
1305 optval, optlen);
1306 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1307 }
1308
1309 if (level != SOL_IPV6)
1310 return -ENOPROTOOPT;
1311
1312 if (optname == MCAST_MSFILTER)
1313 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1314 ipv6_getsockopt);
1315
1316 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen,
1317 MSG_CMSG_COMPAT);
1318 #ifdef CONFIG_NETFILTER
1319 /* we need to exclude all possible ENOPROTOOPTs except default case */
1320 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1321 int len;
1322
1323 if (get_user(len, optlen))
1324 return -EFAULT;
1325
1326 lock_sock(sk);
1327 err = compat_nf_getsockopt(sk, PF_INET6,
1328 optname, optval, &len);
1329 release_sock(sk);
1330 if (err >= 0)
1331 err = put_user(len, optlen);
1332 }
1333 #endif
1334 return err;
1335 }
1336
1337 EXPORT_SYMBOL(compat_ipv6_getsockopt);
1338 #endif
1339
1340