[PATCH] choose_new_parent: remove unused arg, sanitize exit_state check
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / atm / svc.c
blob3a180cfd7b48ee37116f9b78a1dc98081a9528d5
1 /* net/atm/svc.c - ATM SVC sockets */
3 /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
6 #include <linux/string.h>
7 #include <linux/net.h> /* struct socket, struct proto_ops */
8 #include <linux/errno.h> /* error codes */
9 #include <linux/kernel.h> /* printk */
10 #include <linux/skbuff.h>
11 #include <linux/wait.h>
12 #include <linux/sched.h> /* jiffies and HZ */
13 #include <linux/fcntl.h> /* O_NONBLOCK */
14 #include <linux/init.h>
15 #include <linux/atm.h> /* ATM stuff */
16 #include <linux/atmsap.h>
17 #include <linux/atmsvc.h>
18 #include <linux/atmdev.h>
19 #include <linux/bitops.h>
20 #include <net/sock.h> /* for sock_no_* */
21 #include <asm/uaccess.h>
23 #include "resources.h"
24 #include "common.h" /* common for PVCs and SVCs */
25 #include "signaling.h"
26 #include "addr.h"
29 #if 0
30 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
31 #else
32 #define DPRINTK(format,args...)
33 #endif
36 static int svc_create(struct socket *sock,int protocol);
40 * Note: since all this is still nicely synchronized with the signaling demon,
41 * there's no need to protect sleep loops with clis. If signaling is
42 * moved into the kernel, that would change.
46 static int svc_shutdown(struct socket *sock,int how)
48 return 0;
52 static void svc_disconnect(struct atm_vcc *vcc)
54 DEFINE_WAIT(wait);
55 struct sk_buff *skb;
56 struct sock *sk = sk_atm(vcc);
58 DPRINTK("svc_disconnect %p\n",vcc);
59 if (test_bit(ATM_VF_REGIS,&vcc->flags)) {
60 prepare_to_wait(sk->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
61 sigd_enq(vcc,as_close,NULL,NULL,NULL);
62 while (!test_bit(ATM_VF_RELEASED,&vcc->flags) && sigd) {
63 schedule();
64 prepare_to_wait(sk->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
66 finish_wait(sk->sk_sleep, &wait);
68 /* beware - socket is still in use by atmsigd until the last
69 as_indicate has been answered */
70 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
71 atm_return(vcc, skb->truesize);
72 DPRINTK("LISTEN REL\n");
73 sigd_enq2(NULL,as_reject,vcc,NULL,NULL,&vcc->qos,0);
74 dev_kfree_skb(skb);
76 clear_bit(ATM_VF_REGIS, &vcc->flags);
77 /* ... may retry later */
81 static int svc_release(struct socket *sock)
83 struct sock *sk = sock->sk;
84 struct atm_vcc *vcc;
86 if (sk) {
87 vcc = ATM_SD(sock);
88 DPRINTK("svc_release %p\n", vcc);
89 clear_bit(ATM_VF_READY, &vcc->flags);
90 /* VCC pointer is used as a reference, so we must not free it
91 (thereby subjecting it to re-use) before all pending connections
92 are closed */
93 svc_disconnect(vcc);
94 vcc_release(sock);
96 return 0;
100 static int svc_bind(struct socket *sock,struct sockaddr *sockaddr,
101 int sockaddr_len)
103 DEFINE_WAIT(wait);
104 struct sock *sk = sock->sk;
105 struct sockaddr_atmsvc *addr;
106 struct atm_vcc *vcc;
107 int error;
109 if (sockaddr_len != sizeof(struct sockaddr_atmsvc))
110 return -EINVAL;
111 lock_sock(sk);
112 if (sock->state == SS_CONNECTED) {
113 error = -EISCONN;
114 goto out;
116 if (sock->state != SS_UNCONNECTED) {
117 error = -EINVAL;
118 goto out;
120 vcc = ATM_SD(sock);
121 addr = (struct sockaddr_atmsvc *) sockaddr;
122 if (addr->sas_family != AF_ATMSVC) {
123 error = -EAFNOSUPPORT;
124 goto out;
126 clear_bit(ATM_VF_BOUND,&vcc->flags);
127 /* failing rebind will kill old binding */
128 /* @@@ check memory (de)allocation on rebind */
129 if (!test_bit(ATM_VF_HASQOS,&vcc->flags)) {
130 error = -EBADFD;
131 goto out;
133 vcc->local = *addr;
134 set_bit(ATM_VF_WAITING, &vcc->flags);
135 prepare_to_wait(sk->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
136 sigd_enq(vcc,as_bind,NULL,NULL,&vcc->local);
137 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
138 schedule();
139 prepare_to_wait(sk->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
141 finish_wait(sk->sk_sleep, &wait);
142 clear_bit(ATM_VF_REGIS,&vcc->flags); /* doesn't count */
143 if (!sigd) {
144 error = -EUNATCH;
145 goto out;
147 if (!sk->sk_err)
148 set_bit(ATM_VF_BOUND,&vcc->flags);
149 error = -sk->sk_err;
150 out:
151 release_sock(sk);
152 return error;
156 static int svc_connect(struct socket *sock,struct sockaddr *sockaddr,
157 int sockaddr_len,int flags)
159 DEFINE_WAIT(wait);
160 struct sock *sk = sock->sk;
161 struct sockaddr_atmsvc *addr;
162 struct atm_vcc *vcc = ATM_SD(sock);
163 int error;
165 DPRINTK("svc_connect %p\n",vcc);
166 lock_sock(sk);
167 if (sockaddr_len != sizeof(struct sockaddr_atmsvc)) {
168 error = -EINVAL;
169 goto out;
172 switch (sock->state) {
173 default:
174 error = -EINVAL;
175 goto out;
176 case SS_CONNECTED:
177 error = -EISCONN;
178 goto out;
179 case SS_CONNECTING:
180 if (test_bit(ATM_VF_WAITING, &vcc->flags)) {
181 error = -EALREADY;
182 goto out;
184 sock->state = SS_UNCONNECTED;
185 if (sk->sk_err) {
186 error = -sk->sk_err;
187 goto out;
189 break;
190 case SS_UNCONNECTED:
191 addr = (struct sockaddr_atmsvc *) sockaddr;
192 if (addr->sas_family != AF_ATMSVC) {
193 error = -EAFNOSUPPORT;
194 goto out;
196 if (!test_bit(ATM_VF_HASQOS, &vcc->flags)) {
197 error = -EBADFD;
198 goto out;
200 if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
201 vcc->qos.rxtp.traffic_class == ATM_ANYCLASS) {
202 error = -EINVAL;
203 goto out;
205 if (!vcc->qos.txtp.traffic_class &&
206 !vcc->qos.rxtp.traffic_class) {
207 error = -EINVAL;
208 goto out;
210 vcc->remote = *addr;
211 set_bit(ATM_VF_WAITING, &vcc->flags);
212 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
213 sigd_enq(vcc,as_connect,NULL,NULL,&vcc->remote);
214 if (flags & O_NONBLOCK) {
215 finish_wait(sk->sk_sleep, &wait);
216 sock->state = SS_CONNECTING;
217 error = -EINPROGRESS;
218 goto out;
220 error = 0;
221 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
222 schedule();
223 if (!signal_pending(current)) {
224 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
225 continue;
227 DPRINTK("*ABORT*\n");
229 * This is tricky:
230 * Kernel ---close--> Demon
231 * Kernel <--close--- Demon
232 * or
233 * Kernel ---close--> Demon
234 * Kernel <--error--- Demon
235 * or
236 * Kernel ---close--> Demon
237 * Kernel <--okay---- Demon
238 * Kernel <--close--- Demon
240 sigd_enq(vcc,as_close,NULL,NULL,NULL);
241 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
242 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
243 schedule();
245 if (!sk->sk_err)
246 while (!test_bit(ATM_VF_RELEASED,&vcc->flags)
247 && sigd) {
248 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
249 schedule();
251 clear_bit(ATM_VF_REGIS,&vcc->flags);
252 clear_bit(ATM_VF_RELEASED,&vcc->flags);
253 clear_bit(ATM_VF_CLOSE,&vcc->flags);
254 /* we're gone now but may connect later */
255 error = -EINTR;
256 break;
258 finish_wait(sk->sk_sleep, &wait);
259 if (error)
260 goto out;
261 if (!sigd) {
262 error = -EUNATCH;
263 goto out;
265 if (sk->sk_err) {
266 error = -sk->sk_err;
267 goto out;
271 * Not supported yet
273 * #ifndef CONFIG_SINGLE_SIGITF
275 vcc->qos.txtp.max_pcr = SELECT_TOP_PCR(vcc->qos.txtp);
276 vcc->qos.txtp.pcr = 0;
277 vcc->qos.txtp.min_pcr = 0;
279 * #endif
281 if (!(error = vcc_connect(sock, vcc->itf, vcc->vpi, vcc->vci)))
282 sock->state = SS_CONNECTED;
283 else
284 (void) svc_disconnect(vcc);
285 out:
286 release_sock(sk);
287 return error;
291 static int svc_listen(struct socket *sock,int backlog)
293 DEFINE_WAIT(wait);
294 struct sock *sk = sock->sk;
295 struct atm_vcc *vcc = ATM_SD(sock);
296 int error;
298 DPRINTK("svc_listen %p\n",vcc);
299 lock_sock(sk);
300 /* let server handle listen on unbound sockets */
301 if (test_bit(ATM_VF_SESSION,&vcc->flags)) {
302 error = -EINVAL;
303 goto out;
305 vcc_insert_socket(sk);
306 set_bit(ATM_VF_WAITING, &vcc->flags);
307 prepare_to_wait(sk->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
308 sigd_enq(vcc,as_listen,NULL,NULL,&vcc->local);
309 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
310 schedule();
311 prepare_to_wait(sk->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
313 finish_wait(sk->sk_sleep, &wait);
314 if (!sigd) {
315 error = -EUNATCH;
316 goto out;
318 set_bit(ATM_VF_LISTEN,&vcc->flags);
319 sk->sk_max_ack_backlog = backlog > 0 ? backlog : ATM_BACKLOG_DEFAULT;
320 error = -sk->sk_err;
321 out:
322 release_sock(sk);
323 return error;
327 static int svc_accept(struct socket *sock,struct socket *newsock,int flags)
329 struct sock *sk = sock->sk;
330 struct sk_buff *skb;
331 struct atmsvc_msg *msg;
332 struct atm_vcc *old_vcc = ATM_SD(sock);
333 struct atm_vcc *new_vcc;
334 int error;
336 lock_sock(sk);
338 error = svc_create(newsock,0);
339 if (error)
340 goto out;
342 new_vcc = ATM_SD(newsock);
344 DPRINTK("svc_accept %p -> %p\n",old_vcc,new_vcc);
345 while (1) {
346 DEFINE_WAIT(wait);
348 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
349 while (!(skb = skb_dequeue(&sk->sk_receive_queue)) &&
350 sigd) {
351 if (test_bit(ATM_VF_RELEASED,&old_vcc->flags)) break;
352 if (test_bit(ATM_VF_CLOSE,&old_vcc->flags)) {
353 error = -sk->sk_err;
354 break;
356 if (flags & O_NONBLOCK) {
357 error = -EAGAIN;
358 break;
360 release_sock(sk);
361 schedule();
362 lock_sock(sk);
363 if (signal_pending(current)) {
364 error = -ERESTARTSYS;
365 break;
367 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
369 finish_wait(sk->sk_sleep, &wait);
370 if (error)
371 goto out;
372 if (!skb) {
373 error = -EUNATCH;
374 goto out;
376 msg = (struct atmsvc_msg *) skb->data;
377 new_vcc->qos = msg->qos;
378 set_bit(ATM_VF_HASQOS,&new_vcc->flags);
379 new_vcc->remote = msg->svc;
380 new_vcc->local = msg->local;
381 new_vcc->sap = msg->sap;
382 error = vcc_connect(newsock, msg->pvc.sap_addr.itf,
383 msg->pvc.sap_addr.vpi, msg->pvc.sap_addr.vci);
384 dev_kfree_skb(skb);
385 sk->sk_ack_backlog--;
386 if (error) {
387 sigd_enq2(NULL,as_reject,old_vcc,NULL,NULL,
388 &old_vcc->qos,error);
389 error = error == -EAGAIN ? -EBUSY : error;
390 goto out;
392 /* wait should be short, so we ignore the non-blocking flag */
393 set_bit(ATM_VF_WAITING, &new_vcc->flags);
394 prepare_to_wait(sk_atm(new_vcc)->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
395 sigd_enq(new_vcc,as_accept,old_vcc,NULL,NULL);
396 while (test_bit(ATM_VF_WAITING, &new_vcc->flags) && sigd) {
397 release_sock(sk);
398 schedule();
399 lock_sock(sk);
400 prepare_to_wait(sk_atm(new_vcc)->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
402 finish_wait(sk_atm(new_vcc)->sk_sleep, &wait);
403 if (!sigd) {
404 error = -EUNATCH;
405 goto out;
407 if (!sk_atm(new_vcc)->sk_err)
408 break;
409 if (sk_atm(new_vcc)->sk_err != ERESTARTSYS) {
410 error = -sk_atm(new_vcc)->sk_err;
411 goto out;
414 newsock->state = SS_CONNECTED;
415 out:
416 release_sock(sk);
417 return error;
421 static int svc_getname(struct socket *sock,struct sockaddr *sockaddr,
422 int *sockaddr_len,int peer)
424 struct sockaddr_atmsvc *addr;
426 *sockaddr_len = sizeof(struct sockaddr_atmsvc);
427 addr = (struct sockaddr_atmsvc *) sockaddr;
428 memcpy(addr,peer ? &ATM_SD(sock)->remote : &ATM_SD(sock)->local,
429 sizeof(struct sockaddr_atmsvc));
430 return 0;
434 int svc_change_qos(struct atm_vcc *vcc,struct atm_qos *qos)
436 struct sock *sk = sk_atm(vcc);
437 DEFINE_WAIT(wait);
439 set_bit(ATM_VF_WAITING, &vcc->flags);
440 prepare_to_wait(sk->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
441 sigd_enq2(vcc,as_modify,NULL,NULL,&vcc->local,qos,0);
442 while (test_bit(ATM_VF_WAITING, &vcc->flags) &&
443 !test_bit(ATM_VF_RELEASED, &vcc->flags) && sigd) {
444 schedule();
445 prepare_to_wait(sk->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
447 finish_wait(sk->sk_sleep, &wait);
448 if (!sigd) return -EUNATCH;
449 return -sk->sk_err;
453 static int svc_setsockopt(struct socket *sock, int level, int optname,
454 char __user *optval, int optlen)
456 struct sock *sk = sock->sk;
457 struct atm_vcc *vcc = ATM_SD(sock);
458 int value, error = 0;
460 lock_sock(sk);
461 switch (optname) {
462 case SO_ATMSAP:
463 if (level != SOL_ATM || optlen != sizeof(struct atm_sap)) {
464 error = -EINVAL;
465 goto out;
467 if (copy_from_user(&vcc->sap, optval, optlen)) {
468 error = -EFAULT;
469 goto out;
471 set_bit(ATM_VF_HASSAP, &vcc->flags);
472 break;
473 case SO_MULTIPOINT:
474 if (level != SOL_ATM || optlen != sizeof(int)) {
475 error = -EINVAL;
476 goto out;
478 if (get_user(value, (int __user *) optval)) {
479 error = -EFAULT;
480 goto out;
482 if (value == 1) {
483 set_bit(ATM_VF_SESSION, &vcc->flags);
484 } else if (value == 0) {
485 clear_bit(ATM_VF_SESSION, &vcc->flags);
486 } else {
487 error = -EINVAL;
489 break;
490 default:
491 error = vcc_setsockopt(sock, level, optname,
492 optval, optlen);
495 out:
496 release_sock(sk);
497 return error;
501 static int svc_getsockopt(struct socket *sock,int level,int optname,
502 char __user *optval,int __user *optlen)
504 struct sock *sk = sock->sk;
505 int error = 0, len;
507 lock_sock(sk);
508 if (!__SO_LEVEL_MATCH(optname, level) || optname != SO_ATMSAP) {
509 error = vcc_getsockopt(sock, level, optname, optval, optlen);
510 goto out;
512 if (get_user(len, optlen)) {
513 error = -EFAULT;
514 goto out;
516 if (len != sizeof(struct atm_sap)) {
517 error = -EINVAL;
518 goto out;
520 if (copy_to_user(optval, &ATM_SD(sock)->sap, sizeof(struct atm_sap))) {
521 error = -EFAULT;
522 goto out;
524 out:
525 release_sock(sk);
526 return error;
530 static int svc_addparty(struct socket *sock, struct sockaddr *sockaddr,
531 int sockaddr_len, int flags)
533 DEFINE_WAIT(wait);
534 struct sock *sk = sock->sk;
535 struct atm_vcc *vcc = ATM_SD(sock);
536 int error;
538 lock_sock(sk);
539 set_bit(ATM_VF_WAITING, &vcc->flags);
540 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
541 sigd_enq(vcc, as_addparty, NULL, NULL,
542 (struct sockaddr_atmsvc *) sockaddr);
543 if (flags & O_NONBLOCK) {
544 finish_wait(sk->sk_sleep, &wait);
545 error = -EINPROGRESS;
546 goto out;
548 DPRINTK("svc_addparty added wait queue\n");
549 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
550 schedule();
551 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
553 finish_wait(sk->sk_sleep, &wait);
554 error = xchg(&sk->sk_err_soft, 0);
555 out:
556 release_sock(sk);
557 return error;
561 static int svc_dropparty(struct socket *sock, int ep_ref)
563 DEFINE_WAIT(wait);
564 struct sock *sk = sock->sk;
565 struct atm_vcc *vcc = ATM_SD(sock);
566 int error;
568 lock_sock(sk);
569 set_bit(ATM_VF_WAITING, &vcc->flags);
570 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
571 sigd_enq2(vcc, as_dropparty, NULL, NULL, NULL, NULL, ep_ref);
572 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
573 schedule();
574 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
576 finish_wait(sk->sk_sleep, &wait);
577 if (!sigd) {
578 error = -EUNATCH;
579 goto out;
581 error = xchg(&sk->sk_err_soft, 0);
582 out:
583 release_sock(sk);
584 return error;
588 static int svc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
590 int error, ep_ref;
591 struct sockaddr_atmsvc sa;
592 struct atm_vcc *vcc = ATM_SD(sock);
594 switch (cmd) {
595 case ATM_ADDPARTY:
596 if (!test_bit(ATM_VF_SESSION, &vcc->flags))
597 return -EINVAL;
598 if (copy_from_user(&sa, (void __user *) arg, sizeof(sa)))
599 return -EFAULT;
600 error = svc_addparty(sock, (struct sockaddr *) &sa, sizeof(sa), 0);
601 break;
602 case ATM_DROPPARTY:
603 if (!test_bit(ATM_VF_SESSION, &vcc->flags))
604 return -EINVAL;
605 if (copy_from_user(&ep_ref, (void __user *) arg, sizeof(int)))
606 return -EFAULT;
607 error = svc_dropparty(sock, ep_ref);
608 break;
609 default:
610 error = vcc_ioctl(sock, cmd, arg);
613 return error;
616 static const struct proto_ops svc_proto_ops = {
617 .family = PF_ATMSVC,
618 .owner = THIS_MODULE,
620 .release = svc_release,
621 .bind = svc_bind,
622 .connect = svc_connect,
623 .socketpair = sock_no_socketpair,
624 .accept = svc_accept,
625 .getname = svc_getname,
626 .poll = vcc_poll,
627 .ioctl = svc_ioctl,
628 .listen = svc_listen,
629 .shutdown = svc_shutdown,
630 .setsockopt = svc_setsockopt,
631 .getsockopt = svc_getsockopt,
632 .sendmsg = vcc_sendmsg,
633 .recvmsg = vcc_recvmsg,
634 .mmap = sock_no_mmap,
635 .sendpage = sock_no_sendpage,
639 static int svc_create(struct socket *sock,int protocol)
641 int error;
643 sock->ops = &svc_proto_ops;
644 error = vcc_create(sock, protocol, AF_ATMSVC);
645 if (error) return error;
646 ATM_SD(sock)->local.sas_family = AF_ATMSVC;
647 ATM_SD(sock)->remote.sas_family = AF_ATMSVC;
648 return 0;
652 static struct net_proto_family svc_family_ops = {
653 .family = PF_ATMSVC,
654 .create = svc_create,
655 .owner = THIS_MODULE,
660 * Initialize the ATM SVC protocol family
663 int __init atmsvc_init(void)
665 return sock_register(&svc_family_ops);
668 void atmsvc_exit(void)
670 sock_unregister(PF_ATMSVC);