Import 2.3.41pre2
[davej-history.git] / net / ipv6 / ipv6_sockglue.c
blob873d22c3de01a7076c6f32471bca882c248922ec
1 /*
2 * IPv6 BSD socket options interface
3 * Linux INET6 implementation
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * Based on linux/net/ipv4/ip_sockglue.c
10 * $Id: ipv6_sockglue.c,v 1.31 2000/01/16 05:11:38 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 #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>
39 #include <net/sock.h>
40 #include <net/snmp.h>
41 #include <net/ipv6.h>
42 #include <net/ndisc.h>
43 #include <net/protocol.h>
44 #include <net/transp_v6.h>
45 #include <net/ip6_route.h>
46 #include <net/addrconf.h>
47 #include <net/inet_common.h>
48 #include <net/tcp.h>
49 #include <net/udp.h>
51 #include <asm/uaccess.h>
53 struct ipv6_mib ipv6_statistics[NR_CPUS*2];
55 struct packet_type ipv6_packet_type =
57 __constant_htons(ETH_P_IPV6),
58 NULL, /* All devices */
59 ipv6_rcv,
60 (void*)1,
61 NULL
65 * addrconf module should be notifyed of a device going up
67 static struct notifier_block ipv6_dev_notf = {
68 addrconf_notify,
69 NULL,
73 struct ip6_ra_chain *ip6_ra_chain;
74 rwlock_t ip6_ra_lock = RW_LOCK_UNLOCKED;
76 int ip6_ra_control(struct sock *sk, int sel, void (*destructor)(struct sock *))
78 struct ip6_ra_chain *ra, *new_ra, **rap;
80 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
81 if (sk->type != SOCK_RAW || sk->num != IPPROTO_RAW)
82 return -EINVAL;
84 new_ra = (sel>=0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
86 write_lock_bh(&ip6_ra_lock);
87 for (rap = &ip6_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
88 if (ra->sk == sk) {
89 if (sel>=0) {
90 write_unlock_bh(&ip6_ra_lock);
91 if (new_ra)
92 kfree(new_ra);
93 return -EADDRINUSE;
96 *rap = ra->next;
97 write_unlock_bh(&ip6_ra_lock);
99 if (ra->destructor)
100 ra->destructor(sk);
101 sock_put(sk);
102 kfree(ra);
103 return 0;
106 if (new_ra == NULL) {
107 write_unlock_bh(&ip6_ra_lock);
108 return -ENOBUFS;
110 new_ra->sk = sk;
111 new_ra->sel = sel;
112 new_ra->destructor = destructor;
113 new_ra->next = ra;
114 *rap = new_ra;
115 sock_hold(sk);
116 write_unlock_bh(&ip6_ra_lock);
117 return 0;
121 int ipv6_setsockopt(struct sock *sk, int level, int optname, char *optval,
122 int optlen)
124 struct ipv6_pinfo *np = &sk->net_pinfo.af_inet6;
125 int val, valbool;
126 int retv = -ENOPROTOOPT;
128 if(level==SOL_IP && sk->type != SOCK_RAW)
129 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
131 if(level!=SOL_IPV6)
132 goto out;
134 if (optval == NULL)
135 val=0;
136 else if (get_user(val, (int *) optval))
137 return -EFAULT;
139 valbool = (val!=0);
141 lock_sock(sk);
143 switch (optname) {
145 case IPV6_ADDRFORM:
146 if (val == PF_INET) {
147 struct ipv6_txoptions *opt;
148 struct sk_buff *pktopt;
150 if (sk->protocol != IPPROTO_UDP &&
151 sk->protocol != IPPROTO_TCP)
152 break;
154 if (sk->state != TCP_ESTABLISHED) {
155 retv = -ENOTCONN;
156 break;
159 if (!(ipv6_addr_type(&np->daddr) & IPV6_ADDR_MAPPED)) {
160 retv = -EADDRNOTAVAIL;
161 break;
164 fl6_free_socklist(sk);
165 ipv6_sock_mc_close(sk);
167 if (sk->protocol == IPPROTO_TCP) {
168 struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
170 local_bh_disable();
171 sock_prot_dec_use(sk->prot);
172 sock_prot_inc_use(&tcp_prot);
173 local_bh_enable();
174 sk->prot = &tcp_prot;
175 tp->af_specific = &ipv4_specific;
176 sk->socket->ops = &inet_stream_ops;
177 sk->family = PF_INET;
178 tcp_sync_mss(sk, tp->pmtu_cookie);
179 } else {
180 local_bh_disable();
181 sock_prot_dec_use(sk->prot);
182 sock_prot_inc_use(&udp_prot);
183 local_bh_enable();
184 sk->prot = &udp_prot;
185 sk->socket->ops = &inet_dgram_ops;
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);
194 sk->destruct = inet_sock_destruct;
195 #ifdef INET_REFCNT_DEBUG
196 atomic_dec(&inet6_sock_nr);
197 #endif
198 MOD_DEC_USE_COUNT;
199 retv = 0;
200 break;
202 goto e_inval;
204 case IPV6_PKTINFO:
205 np->rxopt.bits.rxinfo = valbool;
206 retv = 0;
207 break;
209 case IPV6_HOPLIMIT:
210 np->rxopt.bits.rxhlim = valbool;
211 retv = 0;
212 break;
214 case IPV6_RTHDR:
215 if (val < 0 || val > 2)
216 goto e_inval;
217 np->rxopt.bits.srcrt = val;
218 retv = 0;
219 break;
221 case IPV6_HOPOPTS:
222 np->rxopt.bits.hopopts = valbool;
223 retv = 0;
224 break;
226 case IPV6_AUTHHDR:
227 np->rxopt.bits.authhdr = valbool;
228 retv = 0;
229 break;
231 case IPV6_DSTOPTS:
232 np->rxopt.bits.dstopts = valbool;
233 retv = 0;
234 break;
236 case IPV6_FLOWINFO:
237 np->rxopt.bits.rxflow = valbool;
238 retv = 0;
239 break;
241 case IPV6_PKTOPTIONS:
243 struct ipv6_txoptions *opt = NULL;
244 struct msghdr msg;
245 struct flowi fl;
246 int junk;
248 fl.fl6_flowlabel = 0;
249 fl.oif = sk->bound_dev_if;
251 if (optlen == 0)
252 goto update;
254 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
255 retv = -ENOBUFS;
256 if (opt == NULL)
257 break;
259 memset(opt, 0, sizeof(*opt));
260 opt->tot_len = sizeof(*opt) + optlen;
261 retv = -EFAULT;
262 if (copy_from_user(opt+1, optval, optlen))
263 goto done;
265 msg.msg_controllen = optlen;
266 msg.msg_control = (void*)(opt+1);
268 retv = datagram_send_ctl(&msg, &fl, opt, &junk);
269 if (retv)
270 goto done;
271 update:
272 retv = 0;
273 if (sk->type == SOCK_STREAM) {
274 if (opt) {
275 struct tcp_opt *tp = &sk->tp_pinfo.af_tcp;
276 if (!((1<<sk->state)&(TCPF_LISTEN|TCPF_CLOSE))
277 && sk->daddr != LOOPBACK4_IPV6) {
278 tp->ext_header_len = opt->opt_flen + opt->opt_nflen;
279 tcp_sync_mss(sk, tp->pmtu_cookie);
282 opt = xchg(&np->opt, opt);
283 sk_dst_reset(sk);
284 } else {
285 write_lock(&sk->dst_lock);
286 opt = xchg(&np->opt, opt);
287 write_unlock(&sk->dst_lock);
288 sk_dst_reset(sk);
291 done:
292 if (opt)
293 sock_kfree_s(sk, opt, opt->tot_len);
294 break;
296 case IPV6_UNICAST_HOPS:
297 if (val > 255 || val < -1)
298 goto e_inval;
299 np->hop_limit = val;
300 retv = 0;
301 break;
303 case IPV6_MULTICAST_HOPS:
304 if (sk->type == SOCK_STREAM)
305 goto e_inval;
306 if (val > 255 || val < -1)
307 goto e_inval;
308 np->mcast_hops = val;
309 retv = 0;
310 break;
312 case IPV6_MULTICAST_LOOP:
313 np->mc_loop = valbool;
314 retv = 0;
315 break;
317 case IPV6_MULTICAST_IF:
318 if (sk->type == SOCK_STREAM)
319 goto e_inval;
320 if (sk->bound_dev_if && sk->bound_dev_if != val)
321 goto e_inval;
323 if (__dev_get_by_index(val) == NULL) {
324 retv = -ENODEV;
325 break;
327 np->mcast_oif = val;
328 retv = 0;
329 break;
330 case IPV6_ADD_MEMBERSHIP:
331 case IPV6_DROP_MEMBERSHIP:
333 struct ipv6_mreq mreq;
335 retv = -EFAULT;
336 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
337 break;
339 if (optname == IPV6_ADD_MEMBERSHIP)
340 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
341 else
342 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
343 break;
345 case IPV6_ROUTER_ALERT:
346 retv = ip6_ra_control(sk, val, NULL);
347 break;
348 case IPV6_MTU_DISCOVER:
349 if (val<0 || val>2)
350 goto e_inval;
351 np->pmtudisc = val;
352 retv = 0;
353 break;
354 case IPV6_MTU:
355 if (val && val < IPV6_MIN_MTU)
356 goto e_inval;
357 np->frag_size = val;
358 retv = 0;
359 break;
360 case IPV6_RECVERR:
361 np->recverr = valbool;
362 if (!val)
363 skb_queue_purge(&sk->error_queue);
364 retv = 0;
365 break;
366 case IPV6_FLOWINFO_SEND:
367 np->sndflow = valbool;
368 retv = 0;
369 break;
370 case IPV6_FLOWLABEL_MGR:
371 retv = ipv6_flowlabel_opt(sk, optval, optlen);
372 break;
374 release_sock(sk);
376 out:
377 return retv;
379 e_inval:
380 release_sock(sk);
381 return -EINVAL;
384 int ipv6_getsockopt(struct sock *sk, int level, int optname, char *optval,
385 int *optlen)
387 struct ipv6_pinfo *np = &sk->net_pinfo.af_inet6;
388 int len;
389 int val;
391 if(level==SOL_IP && sk->type != SOCK_RAW)
392 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
393 if(level!=SOL_IPV6)
394 return -ENOPROTOOPT;
395 if (get_user(len, optlen))
396 return -EFAULT;
397 switch (optname) {
398 case IPV6_PKTOPTIONS:
400 struct msghdr msg;
401 struct sk_buff *skb;
403 if (sk->type != SOCK_STREAM)
404 return -ENOPROTOOPT;
406 msg.msg_control = optval;
407 msg.msg_controllen = len;
408 msg.msg_flags = 0;
410 lock_sock(sk);
411 skb = np->pktoptions;
412 if (skb)
413 atomic_inc(&skb->users);
414 release_sock(sk);
416 if (skb) {
417 int err = datagram_recv_ctl(sk, &msg, skb);
418 kfree_skb(skb);
419 if (err)
420 return err;
421 } else {
422 if (np->rxopt.bits.rxinfo) {
423 struct in6_pktinfo src_info;
424 src_info.ipi6_ifindex = np->mcast_oif;
425 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
426 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
428 if (np->rxopt.bits.rxhlim) {
429 int hlim = np->mcast_hops;
430 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
433 len -= msg.msg_controllen;
434 return put_user(len, optlen);
436 case IP_MTU:
438 struct dst_entry *dst;
439 val = 0;
440 lock_sock(sk);
441 dst = sk_dst_get(sk);
442 if (dst) {
443 val = dst->pmtu;
444 dst_release(dst);
446 release_sock(sk);
447 if (!val)
448 return -ENOTCONN;
449 break;
451 default:
452 return -EINVAL;
454 len=min(sizeof(int),len);
455 if(put_user(len, optlen))
456 return -EFAULT;
457 if(copy_to_user(optval,&val,len))
458 return -EFAULT;
459 return 0;
462 #if defined(MODULE) && defined(CONFIG_SYSCTL)
465 * sysctl registration functions defined in sysctl_net_ipv6.c
468 extern void ipv6_sysctl_register(void);
469 extern void ipv6_sysctl_unregister(void);
470 #endif
472 void __init ipv6_packet_init(void)
474 dev_add_pack(&ipv6_packet_type);
477 void __init ipv6_netdev_notif_init(void)
479 register_netdevice_notifier(&ipv6_dev_notf);
482 #ifdef MODULE
483 void ipv6_packet_cleanup(void)
485 dev_remove_pack(&ipv6_packet_type);
488 void ipv6_netdev_notif_cleanup(void)
490 unregister_netdevice_notifier(&ipv6_dev_notf);
492 #endif