1 /* net/atm/svc.c - ATM SVC sockets */
3 /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
5 #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
7 #include <linux/string.h>
8 #include <linux/net.h> /* struct socket, struct proto_ops */
9 #include <linux/errno.h> /* error codes */
10 #include <linux/kernel.h> /* printk */
11 #include <linux/skbuff.h>
12 #include <linux/wait.h>
13 #include <linux/sched.h> /* jiffies and HZ */
14 #include <linux/fcntl.h> /* O_NONBLOCK */
15 #include <linux/init.h>
16 #include <linux/atm.h> /* ATM stuff */
17 #include <linux/atmsap.h>
18 #include <linux/atmsvc.h>
19 #include <linux/atmdev.h>
20 #include <linux/bitops.h>
21 #include <net/sock.h> /* for sock_no_* */
22 #include <linux/uaccess.h>
24 #include "resources.h"
25 #include "common.h" /* common for PVCs and SVCs */
26 #include "signaling.h"
29 static int svc_create(struct net *net, struct socket *sock, int protocol,
33 * Note: since all this is still nicely synchronized with the signaling demon,
34 * there's no need to protect sleep loops with clis. If signaling is
35 * moved into the kernel, that would change.
39 static int svc_shutdown(struct socket *sock, int how)
44 static void svc_disconnect(struct atm_vcc *vcc)
48 struct sock *sk = sk_atm(vcc);
50 pr_debug("%p\n", vcc);
51 if (test_bit(ATM_VF_REGIS, &vcc->flags)) {
52 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
53 sigd_enq(vcc, as_close, NULL, NULL, NULL);
54 while (!test_bit(ATM_VF_RELEASED, &vcc->flags) && sigd) {
56 prepare_to_wait(sk_sleep(sk), &wait,
57 TASK_UNINTERRUPTIBLE);
59 finish_wait(sk_sleep(sk), &wait);
61 /* beware - socket is still in use by atmsigd until the last
62 as_indicate has been answered */
63 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
64 atm_return(vcc, skb->truesize);
65 pr_debug("LISTEN REL\n");
66 sigd_enq2(NULL, as_reject, vcc, NULL, NULL, &vcc->qos, 0);
69 clear_bit(ATM_VF_REGIS, &vcc->flags);
70 /* ... may retry later */
73 static int svc_release(struct socket *sock)
75 struct sock *sk = sock->sk;
80 pr_debug("%p\n", vcc);
81 clear_bit(ATM_VF_READY, &vcc->flags);
83 * VCC pointer is used as a reference,
84 * so we must not free it (thereby subjecting it to re-use)
85 * before all pending connections are closed
93 static int svc_bind(struct socket *sock, struct sockaddr *sockaddr,
97 struct sock *sk = sock->sk;
98 struct sockaddr_atmsvc *addr;
102 if (sockaddr_len != sizeof(struct sockaddr_atmsvc))
105 if (sock->state == SS_CONNECTED) {
109 if (sock->state != SS_UNCONNECTED) {
114 addr = (struct sockaddr_atmsvc *) sockaddr;
115 if (addr->sas_family != AF_ATMSVC) {
116 error = -EAFNOSUPPORT;
119 clear_bit(ATM_VF_BOUND, &vcc->flags);
120 /* failing rebind will kill old binding */
121 /* @@@ check memory (de)allocation on rebind */
122 if (!test_bit(ATM_VF_HASQOS, &vcc->flags)) {
127 set_bit(ATM_VF_WAITING, &vcc->flags);
128 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
129 sigd_enq(vcc, as_bind, NULL, NULL, &vcc->local);
130 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
132 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
134 finish_wait(sk_sleep(sk), &wait);
135 clear_bit(ATM_VF_REGIS, &vcc->flags); /* doesn't count */
141 set_bit(ATM_VF_BOUND, &vcc->flags);
148 static int svc_connect(struct socket *sock, struct sockaddr *sockaddr,
149 int sockaddr_len, int flags)
152 struct sock *sk = sock->sk;
153 struct sockaddr_atmsvc *addr;
154 struct atm_vcc *vcc = ATM_SD(sock);
157 pr_debug("%p\n", vcc);
159 if (sockaddr_len != sizeof(struct sockaddr_atmsvc)) {
164 switch (sock->state) {
172 if (test_bit(ATM_VF_WAITING, &vcc->flags)) {
176 sock->state = SS_UNCONNECTED;
183 addr = (struct sockaddr_atmsvc *) sockaddr;
184 if (addr->sas_family != AF_ATMSVC) {
185 error = -EAFNOSUPPORT;
188 if (!test_bit(ATM_VF_HASQOS, &vcc->flags)) {
192 if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
193 vcc->qos.rxtp.traffic_class == ATM_ANYCLASS) {
197 if (!vcc->qos.txtp.traffic_class &&
198 !vcc->qos.rxtp.traffic_class) {
203 set_bit(ATM_VF_WAITING, &vcc->flags);
204 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
205 sigd_enq(vcc, as_connect, NULL, NULL, &vcc->remote);
206 if (flags & O_NONBLOCK) {
207 finish_wait(sk_sleep(sk), &wait);
208 sock->state = SS_CONNECTING;
209 error = -EINPROGRESS;
213 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
215 if (!signal_pending(current)) {
216 prepare_to_wait(sk_sleep(sk), &wait,
220 pr_debug("*ABORT*\n");
223 * Kernel ---close--> Demon
224 * Kernel <--close--- Demon
226 * Kernel ---close--> Demon
227 * Kernel <--error--- Demon
229 * Kernel ---close--> Demon
230 * Kernel <--okay---- Demon
231 * Kernel <--close--- Demon
233 sigd_enq(vcc, as_close, NULL, NULL, NULL);
234 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
235 prepare_to_wait(sk_sleep(sk), &wait,
240 while (!test_bit(ATM_VF_RELEASED, &vcc->flags) &&
242 prepare_to_wait(sk_sleep(sk), &wait,
246 clear_bit(ATM_VF_REGIS, &vcc->flags);
247 clear_bit(ATM_VF_RELEASED, &vcc->flags);
248 clear_bit(ATM_VF_CLOSE, &vcc->flags);
249 /* we're gone now but may connect later */
253 finish_wait(sk_sleep(sk), &wait);
268 * #ifndef CONFIG_SINGLE_SIGITF
270 vcc->qos.txtp.max_pcr = SELECT_TOP_PCR(vcc->qos.txtp);
271 vcc->qos.txtp.pcr = 0;
272 vcc->qos.txtp.min_pcr = 0;
276 error = vcc_connect(sock, vcc->itf, vcc->vpi, vcc->vci);
278 sock->state = SS_CONNECTED;
280 (void)svc_disconnect(vcc);
286 static int svc_listen(struct socket *sock, int backlog)
289 struct sock *sk = sock->sk;
290 struct atm_vcc *vcc = ATM_SD(sock);
293 pr_debug("%p\n", vcc);
295 /* let server handle listen on unbound sockets */
296 if (test_bit(ATM_VF_SESSION, &vcc->flags)) {
300 if (test_bit(ATM_VF_LISTEN, &vcc->flags)) {
304 set_bit(ATM_VF_WAITING, &vcc->flags);
305 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
306 sigd_enq(vcc, as_listen, NULL, NULL, &vcc->local);
307 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
309 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
311 finish_wait(sk_sleep(sk), &wait);
316 set_bit(ATM_VF_LISTEN, &vcc->flags);
317 vcc_insert_socket(sk);
318 sk->sk_max_ack_backlog = backlog > 0 ? backlog : ATM_BACKLOG_DEFAULT;
325 static int svc_accept(struct socket *sock, struct socket *newsock, int flags)
327 struct sock *sk = sock->sk;
329 struct atmsvc_msg *msg;
330 struct atm_vcc *old_vcc = ATM_SD(sock);
331 struct atm_vcc *new_vcc;
336 error = svc_create(sock_net(sk), newsock, 0, 0);
340 new_vcc = ATM_SD(newsock);
342 pr_debug("%p -> %p\n", old_vcc, new_vcc);
346 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
347 while (!(skb = skb_dequeue(&sk->sk_receive_queue)) &&
349 if (test_bit(ATM_VF_RELEASED, &old_vcc->flags))
351 if (test_bit(ATM_VF_CLOSE, &old_vcc->flags)) {
355 if (flags & O_NONBLOCK) {
362 if (signal_pending(current)) {
363 error = -ERESTARTSYS;
366 prepare_to_wait(sk_sleep(sk), &wait,
369 finish_wait(sk_sleep(sk), &wait);
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,
384 msg->pvc.sap_addr.vci);
386 sk->sk_ack_backlog--;
388 sigd_enq2(NULL, as_reject, old_vcc, NULL, NULL,
389 &old_vcc->qos, error);
390 error = error == -EAGAIN ? -EBUSY : error;
393 /* wait should be short, so we ignore the non-blocking flag */
394 set_bit(ATM_VF_WAITING, &new_vcc->flags);
395 prepare_to_wait(sk_sleep(sk_atm(new_vcc)), &wait,
396 TASK_UNINTERRUPTIBLE);
397 sigd_enq(new_vcc, as_accept, old_vcc, NULL, NULL);
398 while (test_bit(ATM_VF_WAITING, &new_vcc->flags) && sigd) {
402 prepare_to_wait(sk_sleep(sk_atm(new_vcc)), &wait,
403 TASK_UNINTERRUPTIBLE);
405 finish_wait(sk_sleep(sk_atm(new_vcc)), &wait);
410 if (!sk_atm(new_vcc)->sk_err)
412 if (sk_atm(new_vcc)->sk_err != ERESTARTSYS) {
413 error = -sk_atm(new_vcc)->sk_err;
417 newsock->state = SS_CONNECTED;
423 static int svc_getname(struct socket *sock, struct sockaddr *sockaddr,
424 int *sockaddr_len, int peer)
426 struct sockaddr_atmsvc *addr;
428 *sockaddr_len = sizeof(struct sockaddr_atmsvc);
429 addr = (struct sockaddr_atmsvc *) sockaddr;
430 memcpy(addr, peer ? &ATM_SD(sock)->remote : &ATM_SD(sock)->local,
431 sizeof(struct sockaddr_atmsvc));
435 int svc_change_qos(struct atm_vcc *vcc, struct atm_qos *qos)
437 struct sock *sk = sk_atm(vcc);
440 set_bit(ATM_VF_WAITING, &vcc->flags);
441 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
442 sigd_enq2(vcc, as_modify, NULL, NULL, &vcc->local, qos, 0);
443 while (test_bit(ATM_VF_WAITING, &vcc->flags) &&
444 !test_bit(ATM_VF_RELEASED, &vcc->flags) && sigd) {
446 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
448 finish_wait(sk_sleep(sk), &wait);
454 static int svc_setsockopt(struct socket *sock, int level, int optname,
455 char __user *optval, unsigned int optlen)
457 struct sock *sk = sock->sk;
458 struct atm_vcc *vcc = ATM_SD(sock);
459 int value, error = 0;
464 if (level != SOL_ATM || optlen != sizeof(struct atm_sap)) {
468 if (copy_from_user(&vcc->sap, optval, optlen)) {
472 set_bit(ATM_VF_HASSAP, &vcc->flags);
475 if (level != SOL_ATM || optlen != sizeof(int)) {
479 if (get_user(value, (int __user *)optval)) {
484 set_bit(ATM_VF_SESSION, &vcc->flags);
486 clear_bit(ATM_VF_SESSION, &vcc->flags);
491 error = vcc_setsockopt(sock, level, optname, optval, optlen);
499 static int svc_getsockopt(struct socket *sock, int level, int optname,
500 char __user *optval, int __user *optlen)
502 struct sock *sk = sock->sk;
506 if (!__SO_LEVEL_MATCH(optname, level) || optname != SO_ATMSAP) {
507 error = vcc_getsockopt(sock, level, optname, optval, optlen);
510 if (get_user(len, optlen)) {
514 if (len != sizeof(struct atm_sap)) {
518 if (copy_to_user(optval, &ATM_SD(sock)->sap, sizeof(struct atm_sap))) {
527 static int svc_addparty(struct socket *sock, struct sockaddr *sockaddr,
528 int sockaddr_len, int flags)
531 struct sock *sk = sock->sk;
532 struct atm_vcc *vcc = ATM_SD(sock);
536 set_bit(ATM_VF_WAITING, &vcc->flags);
537 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
538 sigd_enq(vcc, as_addparty, NULL, NULL,
539 (struct sockaddr_atmsvc *) sockaddr);
540 if (flags & O_NONBLOCK) {
541 finish_wait(sk_sleep(sk), &wait);
542 error = -EINPROGRESS;
545 pr_debug("added wait queue\n");
546 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
548 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
550 finish_wait(sk_sleep(sk), &wait);
551 error = xchg(&sk->sk_err_soft, 0);
557 static int svc_dropparty(struct socket *sock, int ep_ref)
560 struct sock *sk = sock->sk;
561 struct atm_vcc *vcc = ATM_SD(sock);
565 set_bit(ATM_VF_WAITING, &vcc->flags);
566 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
567 sigd_enq2(vcc, as_dropparty, NULL, NULL, NULL, NULL, ep_ref);
568 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
570 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
572 finish_wait(sk_sleep(sk), &wait);
577 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),
599 if (!test_bit(ATM_VF_SESSION, &vcc->flags))
601 if (copy_from_user(&ep_ref, (void __user *) arg, sizeof(int)))
603 error = svc_dropparty(sock, ep_ref);
606 error = vcc_ioctl(sock, cmd, arg);
613 static int svc_compat_ioctl(struct socket *sock, unsigned int cmd,
616 /* The definition of ATM_ADDPARTY uses the size of struct atm_iobuf.
617 But actually it takes a struct sockaddr_atmsvc, which doesn't need
618 compat handling. So all we have to do is fix up cmd... */
619 if (cmd == COMPAT_ATM_ADDPARTY)
622 if (cmd == ATM_ADDPARTY || cmd == ATM_DROPPARTY)
623 return svc_ioctl(sock, cmd, arg);
625 return vcc_compat_ioctl(sock, cmd, arg);
627 #endif /* CONFIG_COMPAT */
629 static const struct proto_ops svc_proto_ops = {
631 .owner = THIS_MODULE,
633 .release = svc_release,
635 .connect = svc_connect,
636 .socketpair = sock_no_socketpair,
637 .accept = svc_accept,
638 .getname = svc_getname,
642 .compat_ioctl = svc_compat_ioctl,
644 .listen = svc_listen,
645 .shutdown = svc_shutdown,
646 .setsockopt = svc_setsockopt,
647 .getsockopt = svc_getsockopt,
648 .sendmsg = vcc_sendmsg,
649 .recvmsg = vcc_recvmsg,
650 .mmap = sock_no_mmap,
651 .sendpage = sock_no_sendpage,
655 static int svc_create(struct net *net, struct socket *sock, int protocol,
660 if (!net_eq(net, &init_net))
661 return -EAFNOSUPPORT;
663 sock->ops = &svc_proto_ops;
664 error = vcc_create(net, sock, protocol, AF_ATMSVC);
667 ATM_SD(sock)->local.sas_family = AF_ATMSVC;
668 ATM_SD(sock)->remote.sas_family = AF_ATMSVC;
672 static const struct net_proto_family svc_family_ops = {
674 .create = svc_create,
675 .owner = THIS_MODULE,
680 * Initialize the ATM SVC protocol family
683 int __init atmsvc_init(void)
685 return sock_register(&svc_family_ops);
688 void atmsvc_exit(void)
690 sock_unregister(PF_ATMSVC);