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"
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)
43 static void svc_disconnect(struct atm_vcc *vcc)
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) {
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);
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;
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
91 static int svc_bind(struct socket *sock,struct sockaddr *sockaddr,
95 struct sock *sk = sock->sk;
96 struct sockaddr_atmsvc *addr;
100 if (sockaddr_len != sizeof(struct sockaddr_atmsvc))
103 if (sock->state == SS_CONNECTED) {
107 if (sock->state != SS_UNCONNECTED) {
112 addr = (struct sockaddr_atmsvc *) sockaddr;
113 if (addr->sas_family != AF_ATMSVC) {
114 error = -EAFNOSUPPORT;
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)) {
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) {
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 */
139 set_bit(ATM_VF_BOUND,&vcc->flags);
147 static int svc_connect(struct socket *sock,struct sockaddr *sockaddr,
148 int sockaddr_len,int flags)
151 struct sock *sk = sock->sk;
152 struct sockaddr_atmsvc *addr;
153 struct atm_vcc *vcc = ATM_SD(sock);
156 pr_debug("svc_connect %p\n",vcc);
158 if (sockaddr_len != sizeof(struct sockaddr_atmsvc)) {
163 switch (sock->state) {
171 if (test_bit(ATM_VF_WAITING, &vcc->flags)) {
175 sock->state = SS_UNCONNECTED;
182 addr = (struct sockaddr_atmsvc *) sockaddr;
183 if (addr->sas_family != AF_ATMSVC) {
184 error = -EAFNOSUPPORT;
187 if (!test_bit(ATM_VF_HASQOS, &vcc->flags)) {
191 if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
192 vcc->qos.rxtp.traffic_class == ATM_ANYCLASS) {
196 if (!vcc->qos.txtp.traffic_class &&
197 !vcc->qos.rxtp.traffic_class) {
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;
212 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
214 if (!signal_pending(current)) {
215 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
218 pr_debug("*ABORT*\n");
221 * Kernel ---close--> Demon
222 * Kernel <--close--- Demon
224 * Kernel ---close--> Demon
225 * Kernel <--error--- Demon
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);
237 while (!test_bit(ATM_VF_RELEASED,&vcc->flags)
239 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
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 */
249 finish_wait(sk->sk_sleep, &wait);
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;
272 if (!(error = vcc_connect(sock, vcc->itf, vcc->vpi, vcc->vci)))
273 sock->state = SS_CONNECTED;
275 (void) svc_disconnect(vcc);
282 static int svc_listen(struct socket *sock,int backlog)
285 struct sock *sk = sock->sk;
286 struct atm_vcc *vcc = ATM_SD(sock);
289 pr_debug("svc_listen %p\n",vcc);
291 /* let server handle listen on unbound sockets */
292 if (test_bit(ATM_VF_SESSION,&vcc->flags)) {
296 if (test_bit(ATM_VF_LISTEN, &vcc->flags)) {
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) {
305 prepare_to_wait(sk->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
307 finish_wait(sk->sk_sleep, &wait);
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;
322 static int svc_accept(struct socket *sock,struct socket *newsock,int flags)
324 struct sock *sk = sock->sk;
326 struct atmsvc_msg *msg;
327 struct atm_vcc *old_vcc = ATM_SD(sock);
328 struct atm_vcc *new_vcc;
333 error = svc_create(sock_net(sk), newsock,0);
337 new_vcc = ATM_SD(newsock);
339 pr_debug("svc_accept %p -> %p\n",old_vcc,new_vcc);
343 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
344 while (!(skb = skb_dequeue(&sk->sk_receive_queue)) &&
346 if (test_bit(ATM_VF_RELEASED,&old_vcc->flags)) break;
347 if (test_bit(ATM_VF_CLOSE,&old_vcc->flags)) {
351 if (flags & O_NONBLOCK) {
358 if (signal_pending(current)) {
359 error = -ERESTARTSYS;
362 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
364 finish_wait(sk->sk_sleep, &wait);
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);
380 sk->sk_ack_backlog--;
382 sigd_enq2(NULL,as_reject,old_vcc,NULL,NULL,
383 &old_vcc->qos,error);
384 error = error == -EAGAIN ? -EBUSY : error;
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) {
395 prepare_to_wait(sk_atm(new_vcc)->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
397 finish_wait(sk_atm(new_vcc)->sk_sleep, &wait);
402 if (!sk_atm(new_vcc)->sk_err)
404 if (sk_atm(new_vcc)->sk_err != ERESTARTSYS) {
405 error = -sk_atm(new_vcc)->sk_err;
409 newsock->state = SS_CONNECTED;
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));
429 int svc_change_qos(struct atm_vcc *vcc,struct atm_qos *qos)
431 struct sock *sk = sk_atm(vcc);
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) {
440 prepare_to_wait(sk->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
442 finish_wait(sk->sk_sleep, &wait);
443 if (!sigd) return -EUNATCH;
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;
458 if (level != SOL_ATM || optlen != sizeof(struct atm_sap)) {
462 if (copy_from_user(&vcc->sap, optval, optlen)) {
466 set_bit(ATM_VF_HASSAP, &vcc->flags);
469 if (level != SOL_ATM || optlen != sizeof(int)) {
473 if (get_user(value, (int __user *) optval)) {
478 set_bit(ATM_VF_SESSION, &vcc->flags);
479 } else if (value == 0) {
480 clear_bit(ATM_VF_SESSION, &vcc->flags);
486 error = vcc_setsockopt(sock, level, optname,
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;
503 if (!__SO_LEVEL_MATCH(optname, level) || optname != SO_ATMSAP) {
504 error = vcc_getsockopt(sock, level, optname, optval, optlen);
507 if (get_user(len, optlen)) {
511 if (len != sizeof(struct atm_sap)) {
515 if (copy_to_user(optval, &ATM_SD(sock)->sap, sizeof(struct atm_sap))) {
525 static int svc_addparty(struct socket *sock, struct sockaddr *sockaddr,
526 int sockaddr_len, int flags)
529 struct sock *sk = sock->sk;
530 struct atm_vcc *vcc = ATM_SD(sock);
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;
543 pr_debug("svc_addparty added wait queue\n");
544 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
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);
556 static int svc_dropparty(struct socket *sock, int ep_ref)
559 struct sock *sk = sock->sk;
560 struct atm_vcc *vcc = ATM_SD(sock);
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) {
569 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
571 finish_wait(sk->sk_sleep, &wait);
576 error = xchg(&sk->sk_err_soft, 0);
583 static int svc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
586 struct sockaddr_atmsvc sa;
587 struct atm_vcc *vcc = ATM_SD(sock);
591 if (!test_bit(ATM_VF_SESSION, &vcc->flags))
593 if (copy_from_user(&sa, (void __user *) arg, sizeof(sa)))
595 error = svc_addparty(sock, (struct sockaddr *) &sa, sizeof(sa), 0);
598 if (!test_bit(ATM_VF_SESSION, &vcc->flags))
600 if (copy_from_user(&ep_ref, (void __user *) arg, sizeof(int)))
602 error = svc_dropparty(sock, ep_ref);
605 error = vcc_ioctl(sock, cmd, arg);
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)
620 if (cmd == ATM_ADDPARTY || cmd == ATM_DROPPARTY)
621 return svc_ioctl(sock, cmd, arg);
623 return vcc_compat_ioctl(sock, cmd, arg);
625 #endif /* CONFIG_COMPAT */
627 static const struct proto_ops svc_proto_ops = {
629 .owner = THIS_MODULE,
631 .release = svc_release,
633 .connect = svc_connect,
634 .socketpair = sock_no_socketpair,
635 .accept = svc_accept,
636 .getname = svc_getname,
640 .compat_ioctl = svc_compat_ioctl,
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)
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;
669 static struct net_proto_family svc_family_ops = {
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);