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.1.1.1 2005/04/11 02:51:13 jack 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 #define __NO_VERSION__
25 #include <linux/module.h>
26 #include <linux/config.h>
27 #include <linux/errno.h>
28 #include <linux/types.h>
29 #include <linux/socket.h>
30 #include <linux/sockios.h>
31 #include <linux/sched.h>
32 #include <linux/net.h>
33 #include <linux/in6.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>
43 #include <net/ndisc.h>
44 #include <net/protocol.h>
45 #include <net/transp_v6.h>
46 #include <net/ip6_route.h>
47 #include <net/addrconf.h>
48 #include <net/inet_common.h>
52 #include <asm/uaccess.h>
54 struct ipv6_mib ipv6_statistics[NR_CPUS*2];
56 struct packet_type ipv6_packet_type =
58 __constant_htons(ETH_P_IPV6),
59 NULL, /* All devices */
66 * addrconf module should be notifyed of a device going up
68 static struct notifier_block ipv6_dev_notf = {
74 struct ip6_ra_chain *ip6_ra_chain;
75 rwlock_t ip6_ra_lock = RW_LOCK_UNLOCKED;
77 int ip6_ra_control(struct sock *sk, int sel, void (*destructor)(struct sock *))
79 struct ip6_ra_chain *ra, *new_ra, **rap;
81 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
82 if (sk->type != SOCK_RAW || sk->num != IPPROTO_RAW)
85 new_ra = (sel>=0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
87 write_lock_bh(&ip6_ra_lock);
88 for (rap = &ip6_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
91 write_unlock_bh(&ip6_ra_lock);
98 write_unlock_bh(&ip6_ra_lock);
107 if (new_ra == NULL) {
108 write_unlock_bh(&ip6_ra_lock);
113 new_ra->destructor = destructor;
117 write_unlock_bh(&ip6_ra_lock);
122 int ipv6_setsockopt(struct sock *sk, int level, int optname, char *optval,
125 struct ipv6_pinfo *np = &sk->net_pinfo.af_inet6;
127 int retv = -ENOPROTOOPT;
129 if(level==SOL_IP && sk->type != SOCK_RAW)
130 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
137 else if (get_user(val, (int *) optval))
147 if (val == PF_INET) {
148 struct ipv6_txoptions *opt;
149 struct sk_buff *pktopt;
151 if (sk->protocol != IPPROTO_UDP &&
152 sk->protocol != IPPROTO_TCP)
155 if (sk->state != TCP_ESTABLISHED) {
160 if (ipv6_only_sock(sk) ||
161 !(ipv6_addr_type(&np->daddr) & IPV6_ADDR_MAPPED)) {
162 retv = -EADDRNOTAVAIL;
166 fl6_free_socklist(sk);
167 ipv6_sock_mc_close(sk);
169 if (sk->protocol == IPPROTO_TCP) {
170 struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
173 sock_prot_dec_use(sk->prot);
174 sock_prot_inc_use(&tcp_prot);
176 sk->prot = &tcp_prot;
177 tp->af_specific = &ipv4_specific;
178 sk->socket->ops = &inet_stream_ops;
179 sk->family = PF_INET;
180 tcp_sync_mss(sk, tp->pmtu_cookie);
183 sock_prot_dec_use(sk->prot);
184 sock_prot_inc_use(&udp_prot);
186 sk->prot = &udp_prot;
187 sk->socket->ops = &inet_dgram_ops;
188 sk->family = PF_INET;
190 opt = xchg(&np->opt, NULL);
192 sock_kfree_s(sk, opt, opt->tot_len);
193 pktopt = xchg(&np->pktoptions, NULL);
197 sk->destruct = inet_sock_destruct;
198 #ifdef INET_REFCNT_DEBUG
199 atomic_dec(&inet6_sock_nr);
210 np->ipv6only = valbool;
215 np->rxopt.bits.rxinfo = valbool;
220 np->rxopt.bits.rxhlim = valbool;
225 if (val < 0 || val > 2)
227 np->rxopt.bits.srcrt = val;
232 np->rxopt.bits.hopopts = valbool;
237 np->rxopt.bits.authhdr = valbool;
242 np->rxopt.bits.dstopts = valbool;
247 np->rxopt.bits.rxflow = valbool;
251 case IPV6_PKTOPTIONS:
253 struct ipv6_txoptions *opt = NULL;
258 fl.fl6_flowlabel = 0;
259 fl.oif = sk->bound_dev_if;
264 /* 1K is probably excessive
265 * 1K is surely not enough, 2K per standard header is 16K.
268 if (optlen > 64*1024)
271 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
276 memset(opt, 0, sizeof(*opt));
277 opt->tot_len = sizeof(*opt) + optlen;
279 if (copy_from_user(opt+1, optval, optlen))
282 msg.msg_controllen = optlen;
283 msg.msg_control = (void*)(opt+1);
285 retv = datagram_send_ctl(&msg, &fl, opt, &junk);
290 if (sk->type == SOCK_STREAM) {
292 struct tcp_opt *tp = &sk->tp_pinfo.af_tcp;
293 if (!((1<<sk->state)&(TCPF_LISTEN|TCPF_CLOSE))
294 && sk->daddr != LOOPBACK4_IPV6) {
295 tp->ext_header_len = opt->opt_flen + opt->opt_nflen;
296 tcp_sync_mss(sk, tp->pmtu_cookie);
299 opt = xchg(&np->opt, opt);
302 write_lock(&sk->dst_lock);
303 opt = xchg(&np->opt, opt);
304 write_unlock(&sk->dst_lock);
310 sock_kfree_s(sk, opt, opt->tot_len);
313 case IPV6_UNICAST_HOPS:
314 if (val > 255 || val < -1)
320 case IPV6_MULTICAST_HOPS:
321 if (sk->type == SOCK_STREAM)
323 if (val > 255 || val < -1)
325 np->mcast_hops = val;
329 case IPV6_MULTICAST_LOOP:
330 np->mc_loop = valbool;
334 case IPV6_MULTICAST_IF:
335 if (sk->type == SOCK_STREAM)
337 if (sk->bound_dev_if && sk->bound_dev_if != val)
340 if (__dev_get_by_index(val) == NULL) {
347 case IPV6_ADD_MEMBERSHIP:
348 case IPV6_DROP_MEMBERSHIP:
350 struct ipv6_mreq mreq;
353 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
356 if (optname == IPV6_ADD_MEMBERSHIP)
357 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
359 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
362 case IPV6_ROUTER_ALERT:
363 retv = ip6_ra_control(sk, val, NULL);
365 case IPV6_MTU_DISCOVER:
372 if (val && val < IPV6_MIN_MTU)
378 np->recverr = valbool;
380 skb_queue_purge(&sk->error_queue);
383 case IPV6_FLOWINFO_SEND:
384 np->sndflow = valbool;
387 case IPV6_FLOWLABEL_MGR:
388 retv = ipv6_flowlabel_opt(sk, optval, optlen);
391 #ifdef CONFIG_NETFILTER
393 retv = nf_setsockopt(sk, PF_INET6, optname, optval,
409 int ipv6_getsockopt(struct sock *sk, int level, int optname, char *optval,
412 struct ipv6_pinfo *np = &sk->net_pinfo.af_inet6;
416 if(level==SOL_IP && sk->type != SOCK_RAW)
417 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
420 if (get_user(len, optlen))
423 case IPV6_PKTOPTIONS:
428 if (sk->type != SOCK_STREAM)
431 msg.msg_control = optval;
432 msg.msg_controllen = len;
436 skb = np->pktoptions;
438 atomic_inc(&skb->users);
442 int err = datagram_recv_ctl(sk, &msg, skb);
447 if (np->rxopt.bits.rxinfo) {
448 struct in6_pktinfo src_info;
449 src_info.ipi6_ifindex = np->mcast_oif;
450 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
451 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
453 if (np->rxopt.bits.rxhlim) {
454 int hlim = np->mcast_hops;
455 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
458 len -= msg.msg_controllen;
459 return put_user(len, optlen);
463 struct dst_entry *dst;
466 dst = sk_dst_get(sk);
482 val = np->rxopt.bits.rxinfo;
486 val = np->rxopt.bits.rxhlim;
490 val = np->rxopt.bits.srcrt;
494 val = np->rxopt.bits.hopopts;
498 val = np->rxopt.bits.authhdr;
502 val = np->rxopt.bits.dstopts;
506 val = np->rxopt.bits.rxflow;
509 case IPV6_UNICAST_HOPS:
513 case IPV6_MULTICAST_HOPS:
514 val = np->mcast_hops;
517 case IPV6_MULTICAST_LOOP:
521 case IPV6_MULTICAST_IF:
525 case IPV6_MTU_DISCOVER:
533 case IPV6_FLOWINFO_SEND:
538 #ifdef CONFIG_NETFILTER
540 val = nf_getsockopt(sk, PF_INET6, optname, optval,
544 val = put_user(len, optlen);
550 len = min_t(unsigned int, sizeof(int), len);
551 if(put_user(len, optlen))
553 if(copy_to_user(optval,&val,len))
558 #if defined(MODULE) && defined(CONFIG_SYSCTL)
561 * sysctl registration functions defined in sysctl_net_ipv6.c
564 extern void ipv6_sysctl_register(void);
565 extern void ipv6_sysctl_unregister(void);
568 void __init ipv6_packet_init(void)
570 dev_add_pack(&ipv6_packet_type);
573 void __init ipv6_netdev_notif_init(void)
575 register_netdevice_notifier(&ipv6_dev_notf);
579 void ipv6_packet_cleanup(void)
581 dev_remove_pack(&ipv6_packet_type);
584 void ipv6_netdev_notif_cleanup(void)
586 unregister_netdevice_notifier(&ipv6_dev_notf);