blob: f3ef4c38d315fbdff7786fd04b03d435cff9270c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 * $Id: ipv6_sockglue.c,v 1.41 2002/02/01 22:01:04 davem Exp $
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 *
17 * FIXME: Make the setsockopt code POSIX compliant: That is
18 *
19 * o Return -EINVAL for setsockopt of short lengths
20 * o Truncate getsockopt returns
21 * o Return an optlen of the truncated length if need be
22 *
23 * Changes:
24 * David L Stevens <dlstevens@us.ibm.com>:
25 * - added multicast source filtering API for MLDv2
26 */
27
28#include <linux/module.h>
29#include <linux/config.h>
30#include <linux/errno.h>
31#include <linux/types.h>
32#include <linux/socket.h>
33#include <linux/sockios.h>
34#include <linux/sched.h>
35#include <linux/net.h>
36#include <linux/in6.h>
37#include <linux/netdevice.h>
38#include <linux/if_arp.h>
39#include <linux/init.h>
40#include <linux/sysctl.h>
41#include <linux/netfilter.h>
42
43#include <net/sock.h>
44#include <net/snmp.h>
45#include <net/ipv6.h>
46#include <net/ndisc.h>
47#include <net/protocol.h>
48#include <net/transp_v6.h>
49#include <net/ip6_route.h>
50#include <net/addrconf.h>
51#include <net/inet_common.h>
52#include <net/tcp.h>
53#include <net/udp.h>
54#include <net/xfrm.h>
55
56#include <asm/uaccess.h>
57
58DEFINE_SNMP_STAT(struct ipstats_mib, ipv6_statistics);
59
60static struct packet_type ipv6_packet_type = {
61 .type = __constant_htons(ETH_P_IPV6),
62 .func = ipv6_rcv,
63};
64
65struct ip6_ra_chain *ip6_ra_chain;
66DEFINE_RWLOCK(ip6_ra_lock);
67
68int ip6_ra_control(struct sock *sk, int sel, void (*destructor)(struct sock *))
69{
70 struct ip6_ra_chain *ra, *new_ra, **rap;
71
72 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
73 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->num != IPPROTO_RAW)
74 return -EINVAL;
75
76 new_ra = (sel>=0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
77
78 write_lock_bh(&ip6_ra_lock);
79 for (rap = &ip6_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
80 if (ra->sk == sk) {
81 if (sel>=0) {
82 write_unlock_bh(&ip6_ra_lock);
83 if (new_ra)
84 kfree(new_ra);
85 return -EADDRINUSE;
86 }
87
88 *rap = ra->next;
89 write_unlock_bh(&ip6_ra_lock);
90
91 if (ra->destructor)
92 ra->destructor(sk);
93 sock_put(sk);
94 kfree(ra);
95 return 0;
96 }
97 }
98 if (new_ra == NULL) {
99 write_unlock_bh(&ip6_ra_lock);
100 return -ENOBUFS;
101 }
102 new_ra->sk = sk;
103 new_ra->sel = sel;
104 new_ra->destructor = destructor;
105 new_ra->next = ra;
106 *rap = new_ra;
107 sock_hold(sk);
108 write_unlock_bh(&ip6_ra_lock);
109 return 0;
110}
111
112extern int ip6_mc_source(int add, int omode, struct sock *sk,
113 struct group_source_req *pgsr);
114extern int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf);
115extern int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
116 struct group_filter __user *optval, int __user *optlen);
117
118
119int ipv6_setsockopt(struct sock *sk, int level, int optname,
120 char __user *optval, int optlen)
121{
122 struct ipv6_pinfo *np = inet6_sk(sk);
123 int val, valbool;
124 int retv = -ENOPROTOOPT;
125
126 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
127 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
128
129 if(level!=SOL_IPV6)
130 goto out;
131
132 if (optval == NULL)
133 val=0;
134 else if (get_user(val, (int __user *) optval))
135 return -EFAULT;
136
137 valbool = (val!=0);
138
139 lock_sock(sk);
140
141 switch (optname) {
142
143 case IPV6_ADDRFORM:
144 if (val == PF_INET) {
145 struct ipv6_txoptions *opt;
146 struct sk_buff *pktopt;
147
148 if (sk->sk_protocol != IPPROTO_UDP &&
149 sk->sk_protocol != IPPROTO_TCP)
150 break;
151
152 if (sk->sk_state != TCP_ESTABLISHED) {
153 retv = -ENOTCONN;
154 break;
155 }
156
157 if (ipv6_only_sock(sk) ||
158 !(ipv6_addr_type(&np->daddr) & IPV6_ADDR_MAPPED)) {
159 retv = -EADDRNOTAVAIL;
160 break;
161 }
162
163 fl6_free_socklist(sk);
164 ipv6_sock_mc_close(sk);
165
166 if (sk->sk_protocol == IPPROTO_TCP) {
167 struct tcp_sock *tp = tcp_sk(sk);
168
169 local_bh_disable();
170 sock_prot_dec_use(sk->sk_prot);
171 sock_prot_inc_use(&tcp_prot);
172 local_bh_enable();
173 sk->sk_prot = &tcp_prot;
174 tp->af_specific = &ipv4_specific;
175 sk->sk_socket->ops = &inet_stream_ops;
176 sk->sk_family = PF_INET;
177 tcp_sync_mss(sk, tp->pmtu_cookie);
178 } else {
179 local_bh_disable();
180 sock_prot_dec_use(sk->sk_prot);
181 sock_prot_inc_use(&udp_prot);
182 local_bh_enable();
183 sk->sk_prot = &udp_prot;
184 sk->sk_socket->ops = &inet_dgram_ops;
185 sk->sk_family = PF_INET;
186 }
187 opt = xchg(&np->opt, NULL);
188 if (opt)
189 sock_kfree_s(sk, opt, opt->tot_len);
190 pktopt = xchg(&np->pktoptions, NULL);
191 if (pktopt)
192 kfree_skb(pktopt);
193
194 sk->sk_destruct = inet_sock_destruct;
195#ifdef INET_REFCNT_DEBUG
196 atomic_dec(&inet6_sock_nr);
197#endif
198 module_put(THIS_MODULE);
199 retv = 0;
200 break;
201 }
202 goto e_inval;
203
204 case IPV6_V6ONLY:
205 if (inet_sk(sk)->num)
206 goto e_inval;
207 np->ipv6only = valbool;
208 retv = 0;
209 break;
210
211 case IPV6_PKTINFO:
212 np->rxopt.bits.rxinfo = valbool;
213 retv = 0;
214 break;
215
216 case IPV6_HOPLIMIT:
217 np->rxopt.bits.rxhlim = valbool;
218 retv = 0;
219 break;
220
221 case IPV6_RTHDR:
222 if (val < 0 || val > 2)
223 goto e_inval;
224 np->rxopt.bits.srcrt = val;
225 retv = 0;
226 break;
227
228 case IPV6_HOPOPTS:
229 np->rxopt.bits.hopopts = valbool;
230 retv = 0;
231 break;
232
233 case IPV6_DSTOPTS:
234 np->rxopt.bits.dstopts = valbool;
235 retv = 0;
236 break;
237
238 case IPV6_FLOWINFO:
239 np->rxopt.bits.rxflow = valbool;
240 retv = 0;
241 break;
242
243 case IPV6_PKTOPTIONS:
244 {
245 struct ipv6_txoptions *opt = NULL;
246 struct msghdr msg;
247 struct flowi fl;
248 int junk;
249
250 fl.fl6_flowlabel = 0;
251 fl.oif = sk->sk_bound_dev_if;
252
253 if (optlen == 0)
254 goto update;
255
256 /* 1K is probably excessive
257 * 1K is surely not enough, 2K per standard header is 16K.
258 */
259 retv = -EINVAL;
260 if (optlen > 64*1024)
261 break;
262
263 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
264 retv = -ENOBUFS;
265 if (opt == NULL)
266 break;
267
268 memset(opt, 0, sizeof(*opt));
269 opt->tot_len = sizeof(*opt) + optlen;
270 retv = -EFAULT;
271 if (copy_from_user(opt+1, optval, optlen))
272 goto done;
273
274 msg.msg_controllen = optlen;
275 msg.msg_control = (void*)(opt+1);
276
277 retv = datagram_send_ctl(&msg, &fl, opt, &junk);
278 if (retv)
279 goto done;
280update:
281 retv = 0;
282 if (sk->sk_type == SOCK_STREAM) {
283 if (opt) {
284 struct tcp_sock *tp = tcp_sk(sk);
285 if (!((1 << sk->sk_state) &
286 (TCPF_LISTEN | TCPF_CLOSE))
287 && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
288 tp->ext_header_len = opt->opt_flen + opt->opt_nflen;
289 tcp_sync_mss(sk, tp->pmtu_cookie);
290 }
291 }
292 opt = xchg(&np->opt, opt);
293 sk_dst_reset(sk);
294 } else {
295 write_lock(&sk->sk_dst_lock);
296 opt = xchg(&np->opt, opt);
297 write_unlock(&sk->sk_dst_lock);
298 sk_dst_reset(sk);
299 }
300
301done:
302 if (opt)
303 sock_kfree_s(sk, opt, opt->tot_len);
304 break;
305 }
306 case IPV6_UNICAST_HOPS:
307 if (val > 255 || val < -1)
308 goto e_inval;
309 np->hop_limit = val;
310 retv = 0;
311 break;
312
313 case IPV6_MULTICAST_HOPS:
314 if (sk->sk_type == SOCK_STREAM)
315 goto e_inval;
316 if (val > 255 || val < -1)
317 goto e_inval;
318 np->mcast_hops = val;
319 retv = 0;
320 break;
321
322 case IPV6_MULTICAST_LOOP:
323 np->mc_loop = valbool;
324 retv = 0;
325 break;
326
327 case IPV6_MULTICAST_IF:
328 if (sk->sk_type == SOCK_STREAM)
329 goto e_inval;
330 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
331 goto e_inval;
332
333 if (__dev_get_by_index(val) == NULL) {
334 retv = -ENODEV;
335 break;
336 }
337 np->mcast_oif = val;
338 retv = 0;
339 break;
340 case IPV6_ADD_MEMBERSHIP:
341 case IPV6_DROP_MEMBERSHIP:
342 {
343 struct ipv6_mreq mreq;
344
345 retv = -EFAULT;
346 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
347 break;
348
349 if (optname == IPV6_ADD_MEMBERSHIP)
350 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
351 else
352 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
353 break;
354 }
355 case IPV6_JOIN_ANYCAST:
356 case IPV6_LEAVE_ANYCAST:
357 {
358 struct ipv6_mreq mreq;
359
360 if (optlen != sizeof(struct ipv6_mreq))
361 goto e_inval;
362
363 retv = -EFAULT;
364 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
365 break;
366
367 if (optname == IPV6_JOIN_ANYCAST)
368 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
369 else
370 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
371 break;
372 }
373 case MCAST_JOIN_GROUP:
374 case MCAST_LEAVE_GROUP:
375 {
376 struct group_req greq;
377 struct sockaddr_in6 *psin6;
378
379 retv = -EFAULT;
380 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
381 break;
382 if (greq.gr_group.ss_family != AF_INET6) {
383 retv = -EADDRNOTAVAIL;
384 break;
385 }
386 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
387 if (optname == MCAST_JOIN_GROUP)
388 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
389 &psin6->sin6_addr);
390 else
391 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
392 &psin6->sin6_addr);
393 break;
394 }
395 case MCAST_JOIN_SOURCE_GROUP:
396 case MCAST_LEAVE_SOURCE_GROUP:
397 case MCAST_BLOCK_SOURCE:
398 case MCAST_UNBLOCK_SOURCE:
399 {
400 struct group_source_req greqs;
401 int omode, add;
402
403 if (optlen != sizeof(struct group_source_req))
404 goto e_inval;
405 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
406 retv = -EFAULT;
407 break;
408 }
409 if (greqs.gsr_group.ss_family != AF_INET6 ||
410 greqs.gsr_source.ss_family != AF_INET6) {
411 retv = -EADDRNOTAVAIL;
412 break;
413 }
414 if (optname == MCAST_BLOCK_SOURCE) {
415 omode = MCAST_EXCLUDE;
416 add = 1;
417 } else if (optname == MCAST_UNBLOCK_SOURCE) {
418 omode = MCAST_EXCLUDE;
419 add = 0;
420 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
421 struct sockaddr_in6 *psin6;
422
423 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
424 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
425 &psin6->sin6_addr);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700426 /* prior join w/ different source is ok */
427 if (retv && retv != -EADDRINUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 break;
429 omode = MCAST_INCLUDE;
430 add = 1;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700431 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 omode = MCAST_INCLUDE;
433 add = 0;
434 }
435 retv = ip6_mc_source(add, omode, sk, &greqs);
436 break;
437 }
438 case MCAST_MSFILTER:
439 {
440 extern int sysctl_optmem_max;
441 extern int sysctl_mld_max_msf;
442 struct group_filter *gsf;
443
444 if (optlen < GROUP_FILTER_SIZE(0))
445 goto e_inval;
446 if (optlen > sysctl_optmem_max) {
447 retv = -ENOBUFS;
448 break;
449 }
450 gsf = (struct group_filter *)kmalloc(optlen,GFP_KERNEL);
451 if (gsf == 0) {
452 retv = -ENOBUFS;
453 break;
454 }
455 retv = -EFAULT;
456 if (copy_from_user(gsf, optval, optlen)) {
457 kfree(gsf);
458 break;
459 }
460 /* numsrc >= (4G-140)/128 overflow in 32 bits */
461 if (gsf->gf_numsrc >= 0x1ffffffU ||
462 gsf->gf_numsrc > sysctl_mld_max_msf) {
463 kfree(gsf);
464 retv = -ENOBUFS;
465 break;
466 }
467 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
468 kfree(gsf);
469 retv = -EINVAL;
470 break;
471 }
472 retv = ip6_mc_msfilter(sk, gsf);
473 kfree(gsf);
474
475 break;
476 }
477 case IPV6_ROUTER_ALERT:
478 retv = ip6_ra_control(sk, val, NULL);
479 break;
480 case IPV6_MTU_DISCOVER:
481 if (val<0 || val>2)
482 goto e_inval;
483 np->pmtudisc = val;
484 retv = 0;
485 break;
486 case IPV6_MTU:
487 if (val && val < IPV6_MIN_MTU)
488 goto e_inval;
489 np->frag_size = val;
490 retv = 0;
491 break;
492 case IPV6_RECVERR:
493 np->recverr = valbool;
494 if (!val)
495 skb_queue_purge(&sk->sk_error_queue);
496 retv = 0;
497 break;
498 case IPV6_FLOWINFO_SEND:
499 np->sndflow = valbool;
500 retv = 0;
501 break;
502 case IPV6_FLOWLABEL_MGR:
503 retv = ipv6_flowlabel_opt(sk, optval, optlen);
504 break;
505 case IPV6_IPSEC_POLICY:
506 case IPV6_XFRM_POLICY:
507 retv = xfrm_user_policy(sk, optname, optval, optlen);
508 break;
509
510#ifdef CONFIG_NETFILTER
511 default:
512 retv = nf_setsockopt(sk, PF_INET6, optname, optval,
513 optlen);
514 break;
515#endif
516
517 }
518 release_sock(sk);
519
520out:
521 return retv;
522
523e_inval:
524 release_sock(sk);
525 return -EINVAL;
526}
527
528int ipv6_getsockopt(struct sock *sk, int level, int optname,
529 char __user *optval, int __user *optlen)
530{
531 struct ipv6_pinfo *np = inet6_sk(sk);
532 int len;
533 int val;
534
535 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
536 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
537 if(level!=SOL_IPV6)
538 return -ENOPROTOOPT;
539 if (get_user(len, optlen))
540 return -EFAULT;
541 switch (optname) {
542 case IPV6_ADDRFORM:
543 if (sk->sk_protocol != IPPROTO_UDP &&
544 sk->sk_protocol != IPPROTO_TCP)
545 return -EINVAL;
546 if (sk->sk_state != TCP_ESTABLISHED)
547 return -ENOTCONN;
548 val = sk->sk_family;
549 break;
550 case MCAST_MSFILTER:
551 {
552 struct group_filter gsf;
553 int err;
554
555 if (len < GROUP_FILTER_SIZE(0))
556 return -EINVAL;
557 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
558 return -EFAULT;
559 lock_sock(sk);
560 err = ip6_mc_msfget(sk, &gsf,
561 (struct group_filter __user *)optval, optlen);
562 release_sock(sk);
563 return err;
564 }
565
566 case IPV6_PKTOPTIONS:
567 {
568 struct msghdr msg;
569 struct sk_buff *skb;
570
571 if (sk->sk_type != SOCK_STREAM)
572 return -ENOPROTOOPT;
573
574 msg.msg_control = optval;
575 msg.msg_controllen = len;
576 msg.msg_flags = 0;
577
578 lock_sock(sk);
579 skb = np->pktoptions;
580 if (skb)
581 atomic_inc(&skb->users);
582 release_sock(sk);
583
584 if (skb) {
585 int err = datagram_recv_ctl(sk, &msg, skb);
586 kfree_skb(skb);
587 if (err)
588 return err;
589 } else {
590 if (np->rxopt.bits.rxinfo) {
591 struct in6_pktinfo src_info;
592 src_info.ipi6_ifindex = np->mcast_oif;
593 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
594 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
595 }
596 if (np->rxopt.bits.rxhlim) {
597 int hlim = np->mcast_hops;
598 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
599 }
600 }
601 len -= msg.msg_controllen;
602 return put_user(len, optlen);
603 }
604 case IPV6_MTU:
605 {
606 struct dst_entry *dst;
607 val = 0;
608 lock_sock(sk);
609 dst = sk_dst_get(sk);
610 if (dst) {
611 val = dst_mtu(dst);
612 dst_release(dst);
613 }
614 release_sock(sk);
615 if (!val)
616 return -ENOTCONN;
617 break;
618 }
619
620 case IPV6_V6ONLY:
621 val = np->ipv6only;
622 break;
623
624 case IPV6_PKTINFO:
625 val = np->rxopt.bits.rxinfo;
626 break;
627
628 case IPV6_HOPLIMIT:
629 val = np->rxopt.bits.rxhlim;
630 break;
631
632 case IPV6_RTHDR:
633 val = np->rxopt.bits.srcrt;
634 break;
635
636 case IPV6_HOPOPTS:
637 val = np->rxopt.bits.hopopts;
638 break;
639
640 case IPV6_DSTOPTS:
641 val = np->rxopt.bits.dstopts;
642 break;
643
644 case IPV6_FLOWINFO:
645 val = np->rxopt.bits.rxflow;
646 break;
647
648 case IPV6_UNICAST_HOPS:
649 val = np->hop_limit;
650 break;
651
652 case IPV6_MULTICAST_HOPS:
653 val = np->mcast_hops;
654 break;
655
656 case IPV6_MULTICAST_LOOP:
657 val = np->mc_loop;
658 break;
659
660 case IPV6_MULTICAST_IF:
661 val = np->mcast_oif;
662 break;
663
664 case IPV6_MTU_DISCOVER:
665 val = np->pmtudisc;
666 break;
667
668 case IPV6_RECVERR:
669 val = np->recverr;
670 break;
671
672 case IPV6_FLOWINFO_SEND:
673 val = np->sndflow;
674 break;
675
676 default:
677#ifdef CONFIG_NETFILTER
678 lock_sock(sk);
679 val = nf_getsockopt(sk, PF_INET6, optname, optval,
680 &len);
681 release_sock(sk);
682 if (val >= 0)
683 val = put_user(len, optlen);
684 return val;
685#else
686 return -EINVAL;
687#endif
688 }
689 len = min_t(unsigned int, sizeof(int), len);
690 if(put_user(len, optlen))
691 return -EFAULT;
692 if(copy_to_user(optval,&val,len))
693 return -EFAULT;
694 return 0;
695}
696
697void __init ipv6_packet_init(void)
698{
699 dev_add_pack(&ipv6_packet_type);
700}
701
702void ipv6_packet_cleanup(void)
703{
704 dev_remove_pack(&ipv6_packet_type);
705}