ptrace: do not use task->ptrace directly in core kernel
[linux-2.6/mini2440.git] / net / atm / svc.c
blob7b831b526d0bce4fccb9204a4c9ee7adc3c3bafc
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"
28 static int svc_create(struct net *net, struct socket *sock,int protocol);
31 * Note: since all this is still nicely synchronized with the signaling demon,
32 * there's no need to protect sleep loops with clis. If signaling is
33 * moved into the kernel, that would change.
37 static int svc_shutdown(struct socket *sock,int how)
39 return 0;
43 static void svc_disconnect(struct atm_vcc *vcc)
45 DEFINE_WAIT(wait);
46 struct sk_buff *skb;
47 struct sock *sk = sk_atm(vcc);
49 pr_debug("svc_disconnect %p\n",vcc);
50 if (test_bit(ATM_VF_REGIS,&vcc->flags)) {
51 prepare_to_wait(sk->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
52 sigd_enq(vcc,as_close,NULL,NULL,NULL);
53 while (!test_bit(ATM_VF_RELEASED,&vcc->flags) && sigd) {
54 schedule();
55 prepare_to_wait(sk->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
57 finish_wait(sk->sk_sleep, &wait);
59 /* beware - socket is still in use by atmsigd until the last
60 as_indicate has been answered */
61 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
62 atm_return(vcc, skb->truesize);
63 pr_debug("LISTEN REL\n");
64 sigd_enq2(NULL,as_reject,vcc,NULL,NULL,&vcc->qos,0);
65 dev_kfree_skb(skb);
67 clear_bit(ATM_VF_REGIS, &vcc->flags);
68 /* ... may retry later */
72 static int svc_release(struct socket *sock)
74 struct sock *sk = sock->sk;
75 struct atm_vcc *vcc;
77 if (sk) {
78 vcc = ATM_SD(sock);
79 pr_debug("svc_release %p\n", vcc);
80 clear_bit(ATM_VF_READY, &vcc->flags);
81 /* VCC pointer is used as a reference, so we must not free it
82 (thereby subjecting it to re-use) before all pending connections
83 are closed */
84 svc_disconnect(vcc);
85 vcc_release(sock);
87 return 0;
91 static int svc_bind(struct socket *sock,struct sockaddr *sockaddr,
92 int sockaddr_len)
94 DEFINE_WAIT(wait);
95 struct sock *sk = sock->sk;
96 struct sockaddr_atmsvc *addr;
97 struct atm_vcc *vcc;
98 int error;
100 if (sockaddr_len != sizeof(struct sockaddr_atmsvc))
101 return -EINVAL;
102 lock_sock(sk);
103 if (sock->state == SS_CONNECTED) {
104 error = -EISCONN;
105 goto out;
107 if (sock->state != SS_UNCONNECTED) {
108 error = -EINVAL;
109 goto out;
111 vcc = ATM_SD(sock);
112 addr = (struct sockaddr_atmsvc *) sockaddr;
113 if (addr->sas_family != AF_ATMSVC) {
114 error = -EAFNOSUPPORT;
115 goto out;
117 clear_bit(ATM_VF_BOUND,&vcc->flags);
118 /* failing rebind will kill old binding */
119 /* @@@ check memory (de)allocation on rebind */
120 if (!test_bit(ATM_VF_HASQOS,&vcc->flags)) {
121 error = -EBADFD;
122 goto out;
124 vcc->local = *addr;
125 set_bit(ATM_VF_WAITING, &vcc->flags);
126 prepare_to_wait(sk->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
127 sigd_enq(vcc,as_bind,NULL,NULL,&vcc->local);
128 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
129 schedule();
130 prepare_to_wait(sk->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
132 finish_wait(sk->sk_sleep, &wait);
133 clear_bit(ATM_VF_REGIS,&vcc->flags); /* doesn't count */
134 if (!sigd) {
135 error = -EUNATCH;
136 goto out;
138 if (!sk->sk_err)
139 set_bit(ATM_VF_BOUND,&vcc->flags);
140 error = -sk->sk_err;
141 out:
142 release_sock(sk);
143 return error;
147 static int svc_connect(struct socket *sock,struct sockaddr *sockaddr,
148 int sockaddr_len,int flags)
150 DEFINE_WAIT(wait);
151 struct sock *sk = sock->sk;
152 struct sockaddr_atmsvc *addr;
153 struct atm_vcc *vcc = ATM_SD(sock);
154 int error;
156 pr_debug("svc_connect %p\n",vcc);
157 lock_sock(sk);
158 if (sockaddr_len != sizeof(struct sockaddr_atmsvc)) {
159 error = -EINVAL;
160 goto out;
163 switch (sock->state) {
164 default:
165 error = -EINVAL;
166 goto out;
167 case SS_CONNECTED:
168 error = -EISCONN;
169 goto out;
170 case SS_CONNECTING:
171 if (test_bit(ATM_VF_WAITING, &vcc->flags)) {
172 error = -EALREADY;
173 goto out;
175 sock->state = SS_UNCONNECTED;
176 if (sk->sk_err) {
177 error = -sk->sk_err;
178 goto out;
180 break;
181 case SS_UNCONNECTED:
182 addr = (struct sockaddr_atmsvc *) sockaddr;
183 if (addr->sas_family != AF_ATMSVC) {
184 error = -EAFNOSUPPORT;
185 goto out;
187 if (!test_bit(ATM_VF_HASQOS, &vcc->flags)) {
188 error = -EBADFD;
189 goto out;
191 if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
192 vcc->qos.rxtp.traffic_class == ATM_ANYCLASS) {
193 error = -EINVAL;
194 goto out;
196 if (!vcc->qos.txtp.traffic_class &&
197 !vcc->qos.rxtp.traffic_class) {
198 error = -EINVAL;
199 goto out;
201 vcc->remote = *addr;
202 set_bit(ATM_VF_WAITING, &vcc->flags);
203 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
204 sigd_enq(vcc,as_connect,NULL,NULL,&vcc->remote);
205 if (flags & O_NONBLOCK) {
206 finish_wait(sk->sk_sleep, &wait);
207 sock->state = SS_CONNECTING;
208 error = -EINPROGRESS;
209 goto out;
211 error = 0;
212 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
213 schedule();
214 if (!signal_pending(current)) {
215 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
216 continue;
218 pr_debug("*ABORT*\n");
220 * This is tricky:
221 * Kernel ---close--> Demon
222 * Kernel <--close--- Demon
223 * or
224 * Kernel ---close--> Demon
225 * Kernel <--error--- Demon
226 * or
227 * Kernel ---close--> Demon
228 * Kernel <--okay---- Demon
229 * Kernel <--close--- Demon
231 sigd_enq(vcc,as_close,NULL,NULL,NULL);
232 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
233 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
234 schedule();
236 if (!sk->sk_err)
237 while (!test_bit(ATM_VF_RELEASED,&vcc->flags)
238 && sigd) {
239 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
240 schedule();
242 clear_bit(ATM_VF_REGIS,&vcc->flags);
243 clear_bit(ATM_VF_RELEASED,&vcc->flags);
244 clear_bit(ATM_VF_CLOSE,&vcc->flags);
245 /* we're gone now but may connect later */
246 error = -EINTR;
247 break;
249 finish_wait(sk->sk_sleep, &wait);
250 if (error)
251 goto out;
252 if (!sigd) {
253 error = -EUNATCH;
254 goto out;
256 if (sk->sk_err) {
257 error = -sk->sk_err;
258 goto out;
262 * Not supported yet
264 * #ifndef CONFIG_SINGLE_SIGITF
266 vcc->qos.txtp.max_pcr = SELECT_TOP_PCR(vcc->qos.txtp);
267 vcc->qos.txtp.pcr = 0;
268 vcc->qos.txtp.min_pcr = 0;
270 * #endif
272 if (!(error = vcc_connect(sock, vcc->itf, vcc->vpi, vcc->vci)))
273 sock->state = SS_CONNECTED;
274 else
275 (void) svc_disconnect(vcc);
276 out:
277 release_sock(sk);
278 return error;
282 static int svc_listen(struct socket *sock,int backlog)
284 DEFINE_WAIT(wait);
285 struct sock *sk = sock->sk;
286 struct atm_vcc *vcc = ATM_SD(sock);
287 int error;
289 pr_debug("svc_listen %p\n",vcc);
290 lock_sock(sk);
291 /* let server handle listen on unbound sockets */
292 if (test_bit(ATM_VF_SESSION,&vcc->flags)) {
293 error = -EINVAL;
294 goto out;
296 if (test_bit(ATM_VF_LISTEN, &vcc->flags)) {
297 error = -EADDRINUSE;
298 goto out;
300 set_bit(ATM_VF_WAITING, &vcc->flags);
301 prepare_to_wait(sk->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
302 sigd_enq(vcc,as_listen,NULL,NULL,&vcc->local);
303 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
304 schedule();
305 prepare_to_wait(sk->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
307 finish_wait(sk->sk_sleep, &wait);
308 if (!sigd) {
309 error = -EUNATCH;
310 goto out;
312 set_bit(ATM_VF_LISTEN,&vcc->flags);
313 vcc_insert_socket(sk);
314 sk->sk_max_ack_backlog = backlog > 0 ? backlog : ATM_BACKLOG_DEFAULT;
315 error = -sk->sk_err;
316 out:
317 release_sock(sk);
318 return error;
322 static int svc_accept(struct socket *sock,struct socket *newsock,int flags)
324 struct sock *sk = sock->sk;
325 struct sk_buff *skb;
326 struct atmsvc_msg *msg;
327 struct atm_vcc *old_vcc = ATM_SD(sock);
328 struct atm_vcc *new_vcc;
329 int error;
331 lock_sock(sk);
333 error = svc_create(sock_net(sk), newsock,0);
334 if (error)
335 goto out;
337 new_vcc = ATM_SD(newsock);
339 pr_debug("svc_accept %p -> %p\n",old_vcc,new_vcc);
340 while (1) {
341 DEFINE_WAIT(wait);
343 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
344 while (!(skb = skb_dequeue(&sk->sk_receive_queue)) &&
345 sigd) {
346 if (test_bit(ATM_VF_RELEASED,&old_vcc->flags)) break;
347 if (test_bit(ATM_VF_CLOSE,&old_vcc->flags)) {
348 error = -sk->sk_err;
349 break;
351 if (flags & O_NONBLOCK) {
352 error = -EAGAIN;
353 break;
355 release_sock(sk);
356 schedule();
357 lock_sock(sk);
358 if (signal_pending(current)) {
359 error = -ERESTARTSYS;
360 break;
362 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
364 finish_wait(sk->sk_sleep, &wait);
365 if (error)
366 goto out;
367 if (!skb) {
368 error = -EUNATCH;
369 goto out;
371 msg = (struct atmsvc_msg *) skb->data;
372 new_vcc->qos = msg->qos;
373 set_bit(ATM_VF_HASQOS,&new_vcc->flags);
374 new_vcc->remote = msg->svc;
375 new_vcc->local = msg->local;
376 new_vcc->sap = msg->sap;
377 error = vcc_connect(newsock, msg->pvc.sap_addr.itf,
378 msg->pvc.sap_addr.vpi, msg->pvc.sap_addr.vci);
379 dev_kfree_skb(skb);
380 sk->sk_ack_backlog--;
381 if (error) {
382 sigd_enq2(NULL,as_reject,old_vcc,NULL,NULL,
383 &old_vcc->qos,error);
384 error = error == -EAGAIN ? -EBUSY : error;
385 goto out;
387 /* wait should be short, so we ignore the non-blocking flag */
388 set_bit(ATM_VF_WAITING, &new_vcc->flags);
389 prepare_to_wait(sk_atm(new_vcc)->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
390 sigd_enq(new_vcc,as_accept,old_vcc,NULL,NULL);
391 while (test_bit(ATM_VF_WAITING, &new_vcc->flags) && sigd) {
392 release_sock(sk);
393 schedule();
394 lock_sock(sk);
395 prepare_to_wait(sk_atm(new_vcc)->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
397 finish_wait(sk_atm(new_vcc)->sk_sleep, &wait);
398 if (!sigd) {
399 error = -EUNATCH;
400 goto out;
402 if (!sk_atm(new_vcc)->sk_err)
403 break;
404 if (sk_atm(new_vcc)->sk_err != ERESTARTSYS) {
405 error = -sk_atm(new_vcc)->sk_err;
406 goto out;
409 newsock->state = SS_CONNECTED;
410 out:
411 release_sock(sk);
412 return error;
416 static int svc_getname(struct socket *sock,struct sockaddr *sockaddr,
417 int *sockaddr_len,int peer)
419 struct sockaddr_atmsvc *addr;
421 *sockaddr_len = sizeof(struct sockaddr_atmsvc);
422 addr = (struct sockaddr_atmsvc *) sockaddr;
423 memcpy(addr,peer ? &ATM_SD(sock)->remote : &ATM_SD(sock)->local,
424 sizeof(struct sockaddr_atmsvc));
425 return 0;
429 int svc_change_qos(struct atm_vcc *vcc,struct atm_qos *qos)
431 struct sock *sk = sk_atm(vcc);
432 DEFINE_WAIT(wait);
434 set_bit(ATM_VF_WAITING, &vcc->flags);
435 prepare_to_wait(sk->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
436 sigd_enq2(vcc,as_modify,NULL,NULL,&vcc->local,qos,0);
437 while (test_bit(ATM_VF_WAITING, &vcc->flags) &&
438 !test_bit(ATM_VF_RELEASED, &vcc->flags) && sigd) {
439 schedule();
440 prepare_to_wait(sk->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
442 finish_wait(sk->sk_sleep, &wait);
443 if (!sigd) return -EUNATCH;
444 return -sk->sk_err;
448 static int svc_setsockopt(struct socket *sock, int level, int optname,
449 char __user *optval, int optlen)
451 struct sock *sk = sock->sk;
452 struct atm_vcc *vcc = ATM_SD(sock);
453 int value, error = 0;
455 lock_sock(sk);
456 switch (optname) {
457 case SO_ATMSAP:
458 if (level != SOL_ATM || optlen != sizeof(struct atm_sap)) {
459 error = -EINVAL;
460 goto out;
462 if (copy_from_user(&vcc->sap, optval, optlen)) {
463 error = -EFAULT;
464 goto out;
466 set_bit(ATM_VF_HASSAP, &vcc->flags);
467 break;
468 case SO_MULTIPOINT:
469 if (level != SOL_ATM || optlen != sizeof(int)) {
470 error = -EINVAL;
471 goto out;
473 if (get_user(value, (int __user *) optval)) {
474 error = -EFAULT;
475 goto out;
477 if (value == 1) {
478 set_bit(ATM_VF_SESSION, &vcc->flags);
479 } else if (value == 0) {
480 clear_bit(ATM_VF_SESSION, &vcc->flags);
481 } else {
482 error = -EINVAL;
484 break;
485 default:
486 error = vcc_setsockopt(sock, level, optname,
487 optval, optlen);
490 out:
491 release_sock(sk);
492 return error;
496 static int svc_getsockopt(struct socket *sock,int level,int optname,
497 char __user *optval,int __user *optlen)
499 struct sock *sk = sock->sk;
500 int error = 0, len;
502 lock_sock(sk);
503 if (!__SO_LEVEL_MATCH(optname, level) || optname != SO_ATMSAP) {
504 error = vcc_getsockopt(sock, level, optname, optval, optlen);
505 goto out;
507 if (get_user(len, optlen)) {
508 error = -EFAULT;
509 goto out;
511 if (len != sizeof(struct atm_sap)) {
512 error = -EINVAL;
513 goto out;
515 if (copy_to_user(optval, &ATM_SD(sock)->sap, sizeof(struct atm_sap))) {
516 error = -EFAULT;
517 goto out;
519 out:
520 release_sock(sk);
521 return error;
525 static int svc_addparty(struct socket *sock, struct sockaddr *sockaddr,
526 int sockaddr_len, int flags)
528 DEFINE_WAIT(wait);
529 struct sock *sk = sock->sk;
530 struct atm_vcc *vcc = ATM_SD(sock);
531 int error;
533 lock_sock(sk);
534 set_bit(ATM_VF_WAITING, &vcc->flags);
535 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
536 sigd_enq(vcc, as_addparty, NULL, NULL,
537 (struct sockaddr_atmsvc *) sockaddr);
538 if (flags & O_NONBLOCK) {
539 finish_wait(sk->sk_sleep, &wait);
540 error = -EINPROGRESS;
541 goto out;
543 pr_debug("svc_addparty added wait queue\n");
544 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
545 schedule();
546 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
548 finish_wait(sk->sk_sleep, &wait);
549 error = xchg(&sk->sk_err_soft, 0);
550 out:
551 release_sock(sk);
552 return error;
556 static int svc_dropparty(struct socket *sock, int ep_ref)
558 DEFINE_WAIT(wait);
559 struct sock *sk = sock->sk;
560 struct atm_vcc *vcc = ATM_SD(sock);
561 int error;
563 lock_sock(sk);
564 set_bit(ATM_VF_WAITING, &vcc->flags);
565 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
566 sigd_enq2(vcc, as_dropparty, NULL, NULL, NULL, NULL, ep_ref);
567 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
568 schedule();
569 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
571 finish_wait(sk->sk_sleep, &wait);
572 if (!sigd) {
573 error = -EUNATCH;
574 goto out;
576 error = xchg(&sk->sk_err_soft, 0);
577 out:
578 release_sock(sk);
579 return error;
583 static int svc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
585 int error, ep_ref;
586 struct sockaddr_atmsvc sa;
587 struct atm_vcc *vcc = ATM_SD(sock);
589 switch (cmd) {
590 case ATM_ADDPARTY:
591 if (!test_bit(ATM_VF_SESSION, &vcc->flags))
592 return -EINVAL;
593 if (copy_from_user(&sa, (void __user *) arg, sizeof(sa)))
594 return -EFAULT;
595 error = svc_addparty(sock, (struct sockaddr *) &sa, sizeof(sa), 0);
596 break;
597 case ATM_DROPPARTY:
598 if (!test_bit(ATM_VF_SESSION, &vcc->flags))
599 return -EINVAL;
600 if (copy_from_user(&ep_ref, (void __user *) arg, sizeof(int)))
601 return -EFAULT;
602 error = svc_dropparty(sock, ep_ref);
603 break;
604 default:
605 error = vcc_ioctl(sock, cmd, arg);
608 return error;
611 #ifdef CONFIG_COMPAT
612 static int svc_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
614 /* The definition of ATM_ADDPARTY uses the size of struct atm_iobuf.
615 But actually it takes a struct sockaddr_atmsvc, which doesn't need
616 compat handling. So all we have to do is fix up cmd... */
617 if (cmd == COMPAT_ATM_ADDPARTY)
618 cmd = ATM_ADDPARTY;
620 if (cmd == ATM_ADDPARTY || cmd == ATM_DROPPARTY)
621 return svc_ioctl(sock, cmd, arg);
622 else
623 return vcc_compat_ioctl(sock, cmd, arg);
625 #endif /* CONFIG_COMPAT */
627 static const struct proto_ops svc_proto_ops = {
628 .family = PF_ATMSVC,
629 .owner = THIS_MODULE,
631 .release = svc_release,
632 .bind = svc_bind,
633 .connect = svc_connect,
634 .socketpair = sock_no_socketpair,
635 .accept = svc_accept,
636 .getname = svc_getname,
637 .poll = vcc_poll,
638 .ioctl = svc_ioctl,
639 #ifdef CONFIG_COMPAT
640 .compat_ioctl = svc_compat_ioctl,
641 #endif
642 .listen = svc_listen,
643 .shutdown = svc_shutdown,
644 .setsockopt = svc_setsockopt,
645 .getsockopt = svc_getsockopt,
646 .sendmsg = vcc_sendmsg,
647 .recvmsg = vcc_recvmsg,
648 .mmap = sock_no_mmap,
649 .sendpage = sock_no_sendpage,
653 static int svc_create(struct net *net, struct socket *sock,int protocol)
655 int error;
657 if (net != &init_net)
658 return -EAFNOSUPPORT;
660 sock->ops = &svc_proto_ops;
661 error = vcc_create(net, sock, protocol, AF_ATMSVC);
662 if (error) return error;
663 ATM_SD(sock)->local.sas_family = AF_ATMSVC;
664 ATM_SD(sock)->remote.sas_family = AF_ATMSVC;
665 return 0;
669 static struct net_proto_family svc_family_ops = {
670 .family = PF_ATMSVC,
671 .create = svc_create,
672 .owner = THIS_MODULE,
677 * Initialize the ATM SVC protocol family
680 int __init atmsvc_init(void)
682 return sock_register(&svc_family_ops);
685 void atmsvc_exit(void)
687 sock_unregister(PF_ATMSVC);