2 * IPv6 BSD socket options interface
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * Based on linux/net/ipv4/ip_sockglue.c
10 * $Id: ipv6_sockglue.c,v 1.41 2002/02/01 22:01:04 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>
56 #include <asm/uaccess.h>
58 DEFINE_SNMP_STAT(struct ipstats_mib, ipv6_statistics);
60 static struct packet_type ipv6_packet_type = {
61 .type = __constant_htons(ETH_P_IPV6),
65 struct ip6_ra_chain *ip6_ra_chain;
66 DEFINE_RWLOCK(ip6_ra_lock);
68 int ip6_ra_control(struct sock *sk, int sel, void (*destructor)(struct sock *))
70 struct ip6_ra_chain *ra, *new_ra, **rap;
72 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
73 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->num != IPPROTO_RAW)
76 new_ra = (sel>=0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
78 write_lock_bh(&ip6_ra_lock);
79 for (rap = &ip6_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
82 write_unlock_bh(&ip6_ra_lock);
89 write_unlock_bh(&ip6_ra_lock);
99 write_unlock_bh(&ip6_ra_lock);
104 new_ra->destructor = destructor;
108 write_unlock_bh(&ip6_ra_lock);
112 extern int ip6_mc_source(int add, int omode, struct sock *sk,
113 struct group_source_req *pgsr);
114 extern int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf);
115 extern int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
116 struct group_filter __user *optval, int __user *optlen);
119 int ipv6_setsockopt(struct sock *sk, int level, int optname,
120 char __user *optval, int optlen)
122 struct ipv6_pinfo *np = inet6_sk(sk);
124 int retv = -ENOPROTOOPT;
126 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
127 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
134 else if (get_user(val, (int __user *) optval))
144 if (val == PF_INET) {
145 struct ipv6_txoptions *opt;
146 struct sk_buff *pktopt;
148 if (sk->sk_protocol != IPPROTO_UDP &&
149 sk->sk_protocol != IPPROTO_TCP)
152 if (sk->sk_state != TCP_ESTABLISHED) {
157 if (ipv6_only_sock(sk) ||
158 !(ipv6_addr_type(&np->daddr) & IPV6_ADDR_MAPPED)) {
159 retv = -EADDRNOTAVAIL;
163 fl6_free_socklist(sk);
164 ipv6_sock_mc_close(sk);
166 if (sk->sk_protocol == IPPROTO_TCP) {
167 struct tcp_sock *tp = tcp_sk(sk);
170 sock_prot_dec_use(sk->sk_prot);
171 sock_prot_inc_use(&tcp_prot);
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);
180 sock_prot_dec_use(sk->sk_prot);
181 sock_prot_inc_use(&udp_prot);
183 sk->sk_prot = &udp_prot;
184 sk->sk_socket->ops = &inet_dgram_ops;
185 sk->sk_family = PF_INET;
187 opt = xchg(&np->opt, NULL);
189 sock_kfree_s(sk, opt, opt->tot_len);
190 pktopt = xchg(&np->pktoptions, NULL);
194 sk->sk_destruct = inet_sock_destruct;
195 #ifdef INET_REFCNT_DEBUG
196 atomic_dec(&inet6_sock_nr);
198 module_put(THIS_MODULE);
205 if (inet_sk(sk)->num)
207 np->ipv6only = valbool;
212 np->rxopt.bits.rxinfo = valbool;
217 np->rxopt.bits.rxhlim = valbool;
222 if (val < 0 || val > 2)
224 np->rxopt.bits.srcrt = val;
229 np->rxopt.bits.hopopts = valbool;
234 np->rxopt.bits.dstopts = valbool;
239 np->rxopt.bits.rxflow = valbool;
243 case IPV6_PKTOPTIONS:
245 struct ipv6_txoptions *opt = NULL;
250 fl.fl6_flowlabel = 0;
251 fl.oif = sk->sk_bound_dev_if;
256 /* 1K is probably excessive
257 * 1K is surely not enough, 2K per standard header is 16K.
260 if (optlen > 64*1024)
263 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
268 memset(opt, 0, sizeof(*opt));
269 opt->tot_len = sizeof(*opt) + optlen;
271 if (copy_from_user(opt+1, optval, optlen))
274 msg.msg_controllen = optlen;
275 msg.msg_control = (void*)(opt+1);
277 retv = datagram_send_ctl(&msg, &fl, opt, &junk);
282 if (sk->sk_type == SOCK_STREAM) {
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);
292 opt = xchg(&np->opt, opt);
295 write_lock(&sk->sk_dst_lock);
296 opt = xchg(&np->opt, opt);
297 write_unlock(&sk->sk_dst_lock);
303 sock_kfree_s(sk, opt, opt->tot_len);
306 case IPV6_UNICAST_HOPS:
307 if (val > 255 || val < -1)
313 case IPV6_MULTICAST_HOPS:
314 if (sk->sk_type == SOCK_STREAM)
316 if (val > 255 || val < -1)
318 np->mcast_hops = val;
322 case IPV6_MULTICAST_LOOP:
323 np->mc_loop = valbool;
327 case IPV6_MULTICAST_IF:
328 if (sk->sk_type == SOCK_STREAM)
330 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
333 if (__dev_get_by_index(val) == NULL) {
340 case IPV6_ADD_MEMBERSHIP:
341 case IPV6_DROP_MEMBERSHIP:
343 struct ipv6_mreq mreq;
346 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
349 if (optname == IPV6_ADD_MEMBERSHIP)
350 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
352 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
355 case IPV6_JOIN_ANYCAST:
356 case IPV6_LEAVE_ANYCAST:
358 struct ipv6_mreq mreq;
360 if (optlen != sizeof(struct ipv6_mreq))
364 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
367 if (optname == IPV6_JOIN_ANYCAST)
368 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
370 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
373 case MCAST_JOIN_GROUP:
374 case MCAST_LEAVE_GROUP:
376 struct group_req greq;
377 struct sockaddr_in6 *psin6;
380 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
382 if (greq.gr_group.ss_family != AF_INET6) {
383 retv = -EADDRNOTAVAIL;
386 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
387 if (optname == MCAST_JOIN_GROUP)
388 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
391 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
395 case MCAST_JOIN_SOURCE_GROUP:
396 case MCAST_LEAVE_SOURCE_GROUP:
397 case MCAST_BLOCK_SOURCE:
398 case MCAST_UNBLOCK_SOURCE:
400 struct group_source_req greqs;
403 if (optlen != sizeof(struct group_source_req))
405 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
409 if (greqs.gsr_group.ss_family != AF_INET6 ||
410 greqs.gsr_source.ss_family != AF_INET6) {
411 retv = -EADDRNOTAVAIL;
414 if (optname == MCAST_BLOCK_SOURCE) {
415 omode = MCAST_EXCLUDE;
417 } else if (optname == MCAST_UNBLOCK_SOURCE) {
418 omode = MCAST_EXCLUDE;
420 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
421 struct sockaddr_in6 *psin6;
423 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
424 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
428 omode = MCAST_INCLUDE;
430 } else /*IP_DROP_SOURCE_MEMBERSHIP */ {
431 omode = MCAST_INCLUDE;
434 retv = ip6_mc_source(add, omode, sk, &greqs);
439 extern int sysctl_optmem_max;
440 extern int sysctl_mld_max_msf;
441 struct group_filter *gsf;
443 if (optlen < GROUP_FILTER_SIZE(0))
445 if (optlen > sysctl_optmem_max) {
449 gsf = (struct group_filter *)kmalloc(optlen,GFP_KERNEL);
455 if (copy_from_user(gsf, optval, optlen)) {
459 /* numsrc >= (4G-140)/128 overflow in 32 bits */
460 if (gsf->gf_numsrc >= 0x1ffffffU ||
461 gsf->gf_numsrc > sysctl_mld_max_msf) {
466 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
471 retv = ip6_mc_msfilter(sk, gsf);
476 case IPV6_ROUTER_ALERT:
477 retv = ip6_ra_control(sk, val, NULL);
479 case IPV6_MTU_DISCOVER:
486 if (val && val < IPV6_MIN_MTU)
492 np->recverr = valbool;
494 skb_queue_purge(&sk->sk_error_queue);
497 case IPV6_FLOWINFO_SEND:
498 np->sndflow = valbool;
501 case IPV6_FLOWLABEL_MGR:
502 retv = ipv6_flowlabel_opt(sk, optval, optlen);
504 case IPV6_IPSEC_POLICY:
505 case IPV6_XFRM_POLICY:
506 retv = xfrm_user_policy(sk, optname, optval, optlen);
509 #ifdef CONFIG_NETFILTER
511 retv = nf_setsockopt(sk, PF_INET6, optname, optval,
527 int ipv6_getsockopt(struct sock *sk, int level, int optname,
528 char __user *optval, int __user *optlen)
530 struct ipv6_pinfo *np = inet6_sk(sk);
534 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
535 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
538 if (get_user(len, optlen))
542 if (sk->sk_protocol != IPPROTO_UDP &&
543 sk->sk_protocol != IPPROTO_TCP)
545 if (sk->sk_state != TCP_ESTABLISHED)
551 struct group_filter gsf;
554 if (len < GROUP_FILTER_SIZE(0))
556 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
559 err = ip6_mc_msfget(sk, &gsf,
560 (struct group_filter __user *)optval, optlen);
565 case IPV6_PKTOPTIONS:
570 if (sk->sk_type != SOCK_STREAM)
573 msg.msg_control = optval;
574 msg.msg_controllen = len;
578 skb = np->pktoptions;
580 atomic_inc(&skb->users);
584 int err = datagram_recv_ctl(sk, &msg, skb);
589 if (np->rxopt.bits.rxinfo) {
590 struct in6_pktinfo src_info;
591 src_info.ipi6_ifindex = np->mcast_oif;
592 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
593 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
595 if (np->rxopt.bits.rxhlim) {
596 int hlim = np->mcast_hops;
597 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
600 len -= msg.msg_controllen;
601 return put_user(len, optlen);
605 struct dst_entry *dst;
608 dst = sk_dst_get(sk);
624 val = np->rxopt.bits.rxinfo;
628 val = np->rxopt.bits.rxhlim;
632 val = np->rxopt.bits.srcrt;
636 val = np->rxopt.bits.hopopts;
640 val = np->rxopt.bits.dstopts;
644 val = np->rxopt.bits.rxflow;
647 case IPV6_UNICAST_HOPS:
651 case IPV6_MULTICAST_HOPS:
652 val = np->mcast_hops;
655 case IPV6_MULTICAST_LOOP:
659 case IPV6_MULTICAST_IF:
663 case IPV6_MTU_DISCOVER:
671 case IPV6_FLOWINFO_SEND:
676 #ifdef CONFIG_NETFILTER
678 val = nf_getsockopt(sk, PF_INET6, optname, optval,
682 val = put_user(len, optlen);
688 len = min_t(unsigned int, sizeof(int), len);
689 if(put_user(len, optlen))
691 if(copy_to_user(optval,&val,len))
696 void __init ipv6_packet_init(void)
698 dev_add_pack(&ipv6_packet_type);
701 void ipv6_packet_cleanup(void)
703 dev_remove_pack(&ipv6_packet_type);