2 * IPv6 BSD socket options interface
3 * Linux INET6 implementation
6 * Pedro Roque <pedro_m@yahoo.com>
8 * Based on linux/net/ipv4/ip_sockglue.c
10 * $Id: ipv6_sockglue.c,v 1.40 2001/09/18 22:29:10 davem Exp $
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.
17 * FIXME: Make the setsockopt code POSIX compliant: That is
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
24 * David L Stevens <dlstevens@us.ibm.com>:
25 * - added multicast source filtering API for MLDv2
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>
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>
55 #include <asm/uaccess.h>
57 struct ipv6_mib ipv6_statistics[NR_CPUS*2];
59 struct packet_type ipv6_packet_type =
61 __constant_htons(ETH_P_IPV6),
62 NULL, /* All devices */
69 * addrconf module should be notifyed of a device going up
71 static struct notifier_block ipv6_dev_notf = {
77 struct ip6_ra_chain *ip6_ra_chain;
78 rwlock_t ip6_ra_lock = RW_LOCK_UNLOCKED;
80 int ip6_ra_control(struct sock *sk, int sel, void (*destructor)(struct sock *))
82 struct ip6_ra_chain *ra, *new_ra, **rap;
84 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
85 if (sk->type != SOCK_RAW || sk->num != IPPROTO_RAW)
88 new_ra = (sel>=0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
90 write_lock_bh(&ip6_ra_lock);
91 for (rap = &ip6_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
94 write_unlock_bh(&ip6_ra_lock);
101 write_unlock_bh(&ip6_ra_lock);
110 if (new_ra == NULL) {
111 write_unlock_bh(&ip6_ra_lock);
116 new_ra->destructor = destructor;
120 write_unlock_bh(&ip6_ra_lock);
124 extern int ip6_mc_source(int add, int omode, struct sock *sk,
125 struct group_source_req *pgsr);
126 extern int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf);
127 extern int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
128 struct group_filter *optval, int *optlen);
131 int ipv6_setsockopt(struct sock *sk, int level, int optname, char *optval,
134 struct ipv6_pinfo *np = &sk->net_pinfo.af_inet6;
136 int retv = -ENOPROTOOPT;
138 if(level==SOL_IP && sk->type != SOCK_RAW)
139 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
146 else if (get_user(val, (int *) optval))
156 if (val == PF_INET) {
157 struct ipv6_txoptions *opt;
158 struct sk_buff *pktopt;
160 if (sk->protocol != IPPROTO_UDP &&
161 sk->protocol != IPPROTO_TCP)
164 if (sk->state != TCP_ESTABLISHED) {
169 if (ipv6_only_sock(sk) ||
170 !(ipv6_addr_type(&np->daddr) & IPV6_ADDR_MAPPED)) {
171 retv = -EADDRNOTAVAIL;
175 fl6_free_socklist(sk);
176 ipv6_sock_mc_close(sk);
178 if (sk->protocol == IPPROTO_TCP) {
179 struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
182 sock_prot_dec_use(sk->prot);
183 sock_prot_inc_use(&tcp_prot);
185 sk->prot = &tcp_prot;
186 tp->af_specific = &ipv4_specific;
187 sk->socket->ops = &inet_stream_ops;
188 sk->family = PF_INET;
189 tcp_sync_mss(sk, tp->pmtu_cookie);
192 sock_prot_dec_use(sk->prot);
193 sock_prot_inc_use(&udp_prot);
195 sk->prot = &udp_prot;
196 sk->socket->ops = &inet_dgram_ops;
197 sk->family = PF_INET;
199 opt = xchg(&np->opt, NULL);
201 sock_kfree_s(sk, opt, opt->tot_len);
202 pktopt = xchg(&np->pktoptions, NULL);
206 sk->destruct = inet_sock_destruct;
207 #ifdef INET_REFCNT_DEBUG
208 atomic_dec(&inet6_sock_nr);
219 np->ipv6only = valbool;
224 np->rxopt.bits.rxinfo = valbool;
229 np->rxopt.bits.rxhlim = valbool;
234 if (val < 0 || val > 2)
236 np->rxopt.bits.srcrt = val;
241 np->rxopt.bits.hopopts = valbool;
246 np->rxopt.bits.authhdr = valbool;
251 np->rxopt.bits.dstopts = valbool;
256 np->rxopt.bits.rxflow = valbool;
260 case IPV6_PKTOPTIONS:
262 struct ipv6_txoptions *opt = NULL;
267 fl.fl6_flowlabel = 0;
268 fl.oif = sk->bound_dev_if;
273 /* 1K is probably excessive
274 * 1K is surely not enough, 2K per standard header is 16K.
277 if (optlen > 64*1024)
280 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
285 memset(opt, 0, sizeof(*opt));
286 opt->tot_len = sizeof(*opt) + optlen;
288 if (copy_from_user(opt+1, optval, optlen))
291 msg.msg_controllen = optlen;
292 msg.msg_control = (void*)(opt+1);
294 retv = datagram_send_ctl(&msg, &fl, opt, &junk);
299 if (sk->type == SOCK_STREAM) {
301 struct tcp_opt *tp = &sk->tp_pinfo.af_tcp;
302 if (!((1<<sk->state)&(TCPF_LISTEN|TCPF_CLOSE))
303 && sk->daddr != LOOPBACK4_IPV6) {
304 tp->ext_header_len = opt->opt_flen + opt->opt_nflen;
305 tcp_sync_mss(sk, tp->pmtu_cookie);
308 opt = xchg(&np->opt, opt);
311 write_lock(&sk->dst_lock);
312 opt = xchg(&np->opt, opt);
313 write_unlock(&sk->dst_lock);
319 sock_kfree_s(sk, opt, opt->tot_len);
322 case IPV6_UNICAST_HOPS:
323 if (val > 255 || val < -1)
329 case IPV6_MULTICAST_HOPS:
330 if (sk->type == SOCK_STREAM)
332 if (val > 255 || val < -1)
334 np->mcast_hops = val;
338 case IPV6_MULTICAST_LOOP:
339 np->mc_loop = valbool;
343 case IPV6_MULTICAST_IF:
344 if (sk->type == SOCK_STREAM)
346 if (sk->bound_dev_if && sk->bound_dev_if != val)
349 if (__dev_get_by_index(val) == NULL) {
356 case IPV6_ADD_MEMBERSHIP:
357 case IPV6_DROP_MEMBERSHIP:
359 struct ipv6_mreq mreq;
362 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
365 if (optname == IPV6_ADD_MEMBERSHIP)
366 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
368 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
371 case IPV6_JOIN_ANYCAST:
372 case IPV6_LEAVE_ANYCAST:
374 struct ipv6_mreq mreq;
376 if (optlen != sizeof(struct ipv6_mreq))
380 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
383 if (optname == IPV6_JOIN_ANYCAST)
384 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
386 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
389 case MCAST_JOIN_GROUP:
390 case MCAST_LEAVE_GROUP:
392 struct group_req greq;
393 struct sockaddr_in6 *psin6;
396 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
398 if (greq.gr_group.ss_family != AF_INET6) {
399 retv = -EADDRNOTAVAIL;
402 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
403 if (optname == MCAST_JOIN_GROUP)
404 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
407 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
411 case MCAST_JOIN_SOURCE_GROUP:
412 case MCAST_LEAVE_SOURCE_GROUP:
413 case MCAST_BLOCK_SOURCE:
414 case MCAST_UNBLOCK_SOURCE:
416 struct group_source_req greqs;
419 if (optlen != sizeof(struct group_source_req))
421 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
425 if (greqs.gsr_group.ss_family != AF_INET6 ||
426 greqs.gsr_source.ss_family != AF_INET6) {
427 retv = -EADDRNOTAVAIL;
430 if (optname == MCAST_BLOCK_SOURCE) {
431 omode = MCAST_EXCLUDE;
433 } else if (optname == MCAST_UNBLOCK_SOURCE) {
434 omode = MCAST_EXCLUDE;
436 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
437 struct sockaddr_in6 *psin6;
439 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
440 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
444 omode = MCAST_INCLUDE;
446 } else /*IP_DROP_SOURCE_MEMBERSHIP */ {
447 omode = MCAST_INCLUDE;
450 retv = ip6_mc_source(add, omode, sk, &greqs);
455 extern int sysctl_optmem_max;
456 extern int sysctl_mld_max_msf;
457 struct group_filter *gsf;
459 if (optlen < GROUP_FILTER_SIZE(0))
461 if (optlen > sysctl_optmem_max) {
465 gsf = (struct group_filter *)kmalloc(optlen,GFP_KERNEL);
471 if (copy_from_user(gsf, optval, optlen)) {
475 /* numsrc >= (4G-140)/128 overflow in 32 bits */
476 if (gsf->gf_numsrc >= 0x1ffffffU ||
477 gsf->gf_numsrc > sysctl_mld_max_msf) {
482 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
487 retv = ip6_mc_msfilter(sk, gsf);
492 case IPV6_ROUTER_ALERT:
493 retv = ip6_ra_control(sk, val, NULL);
495 case IPV6_MTU_DISCOVER:
502 if (val && val < IPV6_MIN_MTU)
508 np->recverr = valbool;
510 skb_queue_purge(&sk->error_queue);
513 case IPV6_FLOWINFO_SEND:
514 np->sndflow = valbool;
517 case IPV6_FLOWLABEL_MGR:
518 retv = ipv6_flowlabel_opt(sk, optval, optlen);
521 #ifdef CONFIG_NETFILTER
523 retv = nf_setsockopt(sk, PF_INET6, optname, optval,
539 int ipv6_getsockopt(struct sock *sk, int level, int optname, char *optval,
542 struct ipv6_pinfo *np = &sk->net_pinfo.af_inet6;
546 if(level==SOL_IP && sk->type != SOCK_RAW)
547 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
550 if (get_user(len, optlen))
553 case IPV6_PKTOPTIONS:
558 if (sk->type != SOCK_STREAM)
561 msg.msg_control = optval;
562 msg.msg_controllen = len;
566 skb = np->pktoptions;
568 atomic_inc(&skb->users);
572 int err = datagram_recv_ctl(sk, &msg, skb);
577 if (np->rxopt.bits.rxinfo) {
578 struct in6_pktinfo src_info;
579 src_info.ipi6_ifindex = np->mcast_oif;
580 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
581 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
583 if (np->rxopt.bits.rxhlim) {
584 int hlim = np->mcast_hops;
585 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
588 len -= msg.msg_controllen;
589 return put_user(len, optlen);
593 struct dst_entry *dst;
596 dst = sk_dst_get(sk);
612 val = np->rxopt.bits.rxinfo;
616 struct group_filter gsf;
619 if (len < GROUP_FILTER_SIZE(0))
621 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
624 err = ip6_mc_msfget(sk, &gsf,
625 (struct group_filter *)optval, optlen);
631 val = np->rxopt.bits.rxhlim;
635 val = np->rxopt.bits.srcrt;
639 val = np->rxopt.bits.hopopts;
643 val = np->rxopt.bits.authhdr;
647 val = np->rxopt.bits.dstopts;
651 val = np->rxopt.bits.rxflow;
654 case IPV6_UNICAST_HOPS:
658 case IPV6_MULTICAST_HOPS:
659 val = np->mcast_hops;
662 case IPV6_MULTICAST_LOOP:
666 case IPV6_MULTICAST_IF:
670 case IPV6_MTU_DISCOVER:
678 case IPV6_FLOWINFO_SEND:
683 #ifdef CONFIG_NETFILTER
685 val = nf_getsockopt(sk, PF_INET6, optname, optval,
689 val = put_user(len, optlen);
695 len = min_t(unsigned int, sizeof(int), len);
696 if(put_user(len, optlen))
698 if(copy_to_user(optval,&val,len))
703 #if defined(MODULE) && defined(CONFIG_SYSCTL)
706 * sysctl registration functions defined in sysctl_net_ipv6.c
709 extern void ipv6_sysctl_register(void);
710 extern void ipv6_sysctl_unregister(void);
713 void __init ipv6_packet_init(void)
715 dev_add_pack(&ipv6_packet_type);
718 void __init ipv6_netdev_notif_init(void)
720 register_netdevice_notifier(&ipv6_dev_notf);
724 void ipv6_packet_cleanup(void)
726 dev_remove_pack(&ipv6_packet_type);
729 void ipv6_netdev_notif_cleanup(void)
731 unregister_netdevice_notifier(&ipv6_dev_notf);