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>
23 #include <linux/export.h>
25 #include "resources.h"
26 #include "common.h" /* common for PVCs and SVCs */
27 #include "signaling.h"
30 static int svc_create(struct net
*net
, struct socket
*sock
, int protocol
,
34 * Note: since all this is still nicely synchronized with the signaling demon,
35 * there's no need to protect sleep loops with clis. If signaling is
36 * moved into the kernel, that would change.
40 static int svc_shutdown(struct socket
*sock
, int how
)
45 static void svc_disconnect(struct atm_vcc
*vcc
)
49 struct sock
*sk
= sk_atm(vcc
);
51 pr_debug("%p\n", vcc
);
52 if (test_bit(ATM_VF_REGIS
, &vcc
->flags
)) {
53 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_UNINTERRUPTIBLE
);
54 sigd_enq(vcc
, as_close
, NULL
, NULL
, NULL
);
55 while (!test_bit(ATM_VF_RELEASED
, &vcc
->flags
) && sigd
) {
57 prepare_to_wait(sk_sleep(sk
), &wait
,
58 TASK_UNINTERRUPTIBLE
);
60 finish_wait(sk_sleep(sk
), &wait
);
62 /* beware - socket is still in use by atmsigd until the last
63 as_indicate has been answered */
64 while ((skb
= skb_dequeue(&sk
->sk_receive_queue
)) != NULL
) {
65 atm_return(vcc
, skb
->truesize
);
66 pr_debug("LISTEN REL\n");
67 sigd_enq2(NULL
, as_reject
, vcc
, NULL
, NULL
, &vcc
->qos
, 0);
70 clear_bit(ATM_VF_REGIS
, &vcc
->flags
);
71 /* ... may retry later */
74 static int svc_release(struct socket
*sock
)
76 struct sock
*sk
= sock
->sk
;
81 pr_debug("%p\n", vcc
);
82 clear_bit(ATM_VF_READY
, &vcc
->flags
);
84 * VCC pointer is used as a reference,
85 * so we must not free it (thereby subjecting it to re-use)
86 * before all pending connections are closed
94 static int svc_bind(struct socket
*sock
, struct sockaddr
*sockaddr
,
98 struct sock
*sk
= sock
->sk
;
99 struct sockaddr_atmsvc
*addr
;
103 if (sockaddr_len
!= sizeof(struct sockaddr_atmsvc
))
106 if (sock
->state
== SS_CONNECTED
) {
110 if (sock
->state
!= SS_UNCONNECTED
) {
115 addr
= (struct sockaddr_atmsvc
*) sockaddr
;
116 if (addr
->sas_family
!= AF_ATMSVC
) {
117 error
= -EAFNOSUPPORT
;
120 clear_bit(ATM_VF_BOUND
, &vcc
->flags
);
121 /* failing rebind will kill old binding */
122 /* @@@ check memory (de)allocation on rebind */
123 if (!test_bit(ATM_VF_HASQOS
, &vcc
->flags
)) {
128 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
129 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_UNINTERRUPTIBLE
);
130 sigd_enq(vcc
, as_bind
, NULL
, NULL
, &vcc
->local
);
131 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) && sigd
) {
133 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_UNINTERRUPTIBLE
);
135 finish_wait(sk_sleep(sk
), &wait
);
136 clear_bit(ATM_VF_REGIS
, &vcc
->flags
); /* doesn't count */
142 set_bit(ATM_VF_BOUND
, &vcc
->flags
);
149 static int svc_connect(struct socket
*sock
, struct sockaddr
*sockaddr
,
150 int sockaddr_len
, int flags
)
153 struct sock
*sk
= sock
->sk
;
154 struct sockaddr_atmsvc
*addr
;
155 struct atm_vcc
*vcc
= ATM_SD(sock
);
158 pr_debug("%p\n", vcc
);
160 if (sockaddr_len
!= sizeof(struct sockaddr_atmsvc
)) {
165 switch (sock
->state
) {
173 if (test_bit(ATM_VF_WAITING
, &vcc
->flags
)) {
177 sock
->state
= SS_UNCONNECTED
;
184 addr
= (struct sockaddr_atmsvc
*) sockaddr
;
185 if (addr
->sas_family
!= AF_ATMSVC
) {
186 error
= -EAFNOSUPPORT
;
189 if (!test_bit(ATM_VF_HASQOS
, &vcc
->flags
)) {
193 if (vcc
->qos
.txtp
.traffic_class
== ATM_ANYCLASS
||
194 vcc
->qos
.rxtp
.traffic_class
== ATM_ANYCLASS
) {
198 if (!vcc
->qos
.txtp
.traffic_class
&&
199 !vcc
->qos
.rxtp
.traffic_class
) {
204 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
205 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
206 sigd_enq(vcc
, as_connect
, NULL
, NULL
, &vcc
->remote
);
207 if (flags
& O_NONBLOCK
) {
208 finish_wait(sk_sleep(sk
), &wait
);
209 sock
->state
= SS_CONNECTING
;
210 error
= -EINPROGRESS
;
214 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) && sigd
) {
216 if (!signal_pending(current
)) {
217 prepare_to_wait(sk_sleep(sk
), &wait
,
221 pr_debug("*ABORT*\n");
224 * Kernel ---close--> Demon
225 * Kernel <--close--- Demon
227 * Kernel ---close--> Demon
228 * Kernel <--error--- Demon
230 * Kernel ---close--> Demon
231 * Kernel <--okay---- Demon
232 * Kernel <--close--- Demon
234 sigd_enq(vcc
, as_close
, NULL
, NULL
, NULL
);
235 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) && sigd
) {
236 prepare_to_wait(sk_sleep(sk
), &wait
,
241 while (!test_bit(ATM_VF_RELEASED
, &vcc
->flags
) &&
243 prepare_to_wait(sk_sleep(sk
), &wait
,
247 clear_bit(ATM_VF_REGIS
, &vcc
->flags
);
248 clear_bit(ATM_VF_RELEASED
, &vcc
->flags
);
249 clear_bit(ATM_VF_CLOSE
, &vcc
->flags
);
250 /* we're gone now but may connect later */
254 finish_wait(sk_sleep(sk
), &wait
);
269 * #ifndef CONFIG_SINGLE_SIGITF
271 vcc
->qos
.txtp
.max_pcr
= SELECT_TOP_PCR(vcc
->qos
.txtp
);
272 vcc
->qos
.txtp
.pcr
= 0;
273 vcc
->qos
.txtp
.min_pcr
= 0;
277 error
= vcc_connect(sock
, vcc
->itf
, vcc
->vpi
, vcc
->vci
);
279 sock
->state
= SS_CONNECTED
;
281 (void)svc_disconnect(vcc
);
287 static int svc_listen(struct socket
*sock
, int backlog
)
290 struct sock
*sk
= sock
->sk
;
291 struct atm_vcc
*vcc
= ATM_SD(sock
);
294 pr_debug("%p\n", vcc
);
296 /* let server handle listen on unbound sockets */
297 if (test_bit(ATM_VF_SESSION
, &vcc
->flags
)) {
301 if (test_bit(ATM_VF_LISTEN
, &vcc
->flags
)) {
305 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
306 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_UNINTERRUPTIBLE
);
307 sigd_enq(vcc
, as_listen
, NULL
, NULL
, &vcc
->local
);
308 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) && sigd
) {
310 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_UNINTERRUPTIBLE
);
312 finish_wait(sk_sleep(sk
), &wait
);
317 set_bit(ATM_VF_LISTEN
, &vcc
->flags
);
318 vcc_insert_socket(sk
);
319 sk
->sk_max_ack_backlog
= backlog
> 0 ? backlog
: ATM_BACKLOG_DEFAULT
;
326 static int svc_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
328 struct sock
*sk
= sock
->sk
;
330 struct atmsvc_msg
*msg
;
331 struct atm_vcc
*old_vcc
= ATM_SD(sock
);
332 struct atm_vcc
*new_vcc
;
337 error
= svc_create(sock_net(sk
), newsock
, 0, 0);
341 new_vcc
= ATM_SD(newsock
);
343 pr_debug("%p -> %p\n", old_vcc
, new_vcc
);
347 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
348 while (!(skb
= skb_dequeue(&sk
->sk_receive_queue
)) &&
350 if (test_bit(ATM_VF_RELEASED
, &old_vcc
->flags
))
352 if (test_bit(ATM_VF_CLOSE
, &old_vcc
->flags
)) {
356 if (flags
& O_NONBLOCK
) {
363 if (signal_pending(current
)) {
364 error
= -ERESTARTSYS
;
367 prepare_to_wait(sk_sleep(sk
), &wait
,
370 finish_wait(sk_sleep(sk
), &wait
);
377 msg
= (struct atmsvc_msg
*)skb
->data
;
378 new_vcc
->qos
= msg
->qos
;
379 set_bit(ATM_VF_HASQOS
, &new_vcc
->flags
);
380 new_vcc
->remote
= msg
->svc
;
381 new_vcc
->local
= msg
->local
;
382 new_vcc
->sap
= msg
->sap
;
383 error
= vcc_connect(newsock
, msg
->pvc
.sap_addr
.itf
,
384 msg
->pvc
.sap_addr
.vpi
,
385 msg
->pvc
.sap_addr
.vci
);
387 sk
->sk_ack_backlog
--;
389 sigd_enq2(NULL
, as_reject
, old_vcc
, NULL
, NULL
,
390 &old_vcc
->qos
, error
);
391 error
= error
== -EAGAIN
? -EBUSY
: error
;
394 /* wait should be short, so we ignore the non-blocking flag */
395 set_bit(ATM_VF_WAITING
, &new_vcc
->flags
);
396 prepare_to_wait(sk_sleep(sk_atm(new_vcc
)), &wait
,
397 TASK_UNINTERRUPTIBLE
);
398 sigd_enq(new_vcc
, as_accept
, old_vcc
, NULL
, NULL
);
399 while (test_bit(ATM_VF_WAITING
, &new_vcc
->flags
) && sigd
) {
403 prepare_to_wait(sk_sleep(sk_atm(new_vcc
)), &wait
,
404 TASK_UNINTERRUPTIBLE
);
406 finish_wait(sk_sleep(sk_atm(new_vcc
)), &wait
);
411 if (!sk_atm(new_vcc
)->sk_err
)
413 if (sk_atm(new_vcc
)->sk_err
!= ERESTARTSYS
) {
414 error
= -sk_atm(new_vcc
)->sk_err
;
418 newsock
->state
= SS_CONNECTED
;
424 static int svc_getname(struct socket
*sock
, struct sockaddr
*sockaddr
,
425 int *sockaddr_len
, int peer
)
427 struct sockaddr_atmsvc
*addr
;
429 *sockaddr_len
= sizeof(struct sockaddr_atmsvc
);
430 addr
= (struct sockaddr_atmsvc
*) sockaddr
;
431 memcpy(addr
, peer
? &ATM_SD(sock
)->remote
: &ATM_SD(sock
)->local
,
432 sizeof(struct sockaddr_atmsvc
));
436 int svc_change_qos(struct atm_vcc
*vcc
, struct atm_qos
*qos
)
438 struct sock
*sk
= sk_atm(vcc
);
441 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
442 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_UNINTERRUPTIBLE
);
443 sigd_enq2(vcc
, as_modify
, NULL
, NULL
, &vcc
->local
, qos
, 0);
444 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) &&
445 !test_bit(ATM_VF_RELEASED
, &vcc
->flags
) && sigd
) {
447 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_UNINTERRUPTIBLE
);
449 finish_wait(sk_sleep(sk
), &wait
);
455 static int svc_setsockopt(struct socket
*sock
, int level
, int optname
,
456 char __user
*optval
, unsigned int optlen
)
458 struct sock
*sk
= sock
->sk
;
459 struct atm_vcc
*vcc
= ATM_SD(sock
);
460 int value
, error
= 0;
465 if (level
!= SOL_ATM
|| optlen
!= sizeof(struct atm_sap
)) {
469 if (copy_from_user(&vcc
->sap
, optval
, optlen
)) {
473 set_bit(ATM_VF_HASSAP
, &vcc
->flags
);
476 if (level
!= SOL_ATM
|| optlen
!= sizeof(int)) {
480 if (get_user(value
, (int __user
*)optval
)) {
485 set_bit(ATM_VF_SESSION
, &vcc
->flags
);
487 clear_bit(ATM_VF_SESSION
, &vcc
->flags
);
492 error
= vcc_setsockopt(sock
, level
, optname
, optval
, optlen
);
500 static int svc_getsockopt(struct socket
*sock
, int level
, int optname
,
501 char __user
*optval
, int __user
*optlen
)
503 struct sock
*sk
= sock
->sk
;
507 if (!__SO_LEVEL_MATCH(optname
, level
) || optname
!= SO_ATMSAP
) {
508 error
= vcc_getsockopt(sock
, level
, optname
, optval
, optlen
);
511 if (get_user(len
, optlen
)) {
515 if (len
!= sizeof(struct atm_sap
)) {
519 if (copy_to_user(optval
, &ATM_SD(sock
)->sap
, sizeof(struct atm_sap
))) {
528 static int svc_addparty(struct socket
*sock
, struct sockaddr
*sockaddr
,
529 int sockaddr_len
, int flags
)
532 struct sock
*sk
= sock
->sk
;
533 struct atm_vcc
*vcc
= ATM_SD(sock
);
537 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
538 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
539 sigd_enq(vcc
, as_addparty
, NULL
, NULL
,
540 (struct sockaddr_atmsvc
*) sockaddr
);
541 if (flags
& O_NONBLOCK
) {
542 finish_wait(sk_sleep(sk
), &wait
);
543 error
= -EINPROGRESS
;
546 pr_debug("added wait queue\n");
547 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) && sigd
) {
549 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
551 finish_wait(sk_sleep(sk
), &wait
);
552 error
= xchg(&sk
->sk_err_soft
, 0);
558 static int svc_dropparty(struct socket
*sock
, int ep_ref
)
561 struct sock
*sk
= sock
->sk
;
562 struct atm_vcc
*vcc
= ATM_SD(sock
);
566 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
567 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
568 sigd_enq2(vcc
, as_dropparty
, NULL
, NULL
, NULL
, NULL
, ep_ref
);
569 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) && sigd
) {
571 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
573 finish_wait(sk_sleep(sk
), &wait
);
578 error
= xchg(&sk
->sk_err_soft
, 0);
584 static int svc_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
587 struct sockaddr_atmsvc sa
;
588 struct atm_vcc
*vcc
= ATM_SD(sock
);
592 if (!test_bit(ATM_VF_SESSION
, &vcc
->flags
))
594 if (copy_from_user(&sa
, (void __user
*) arg
, sizeof(sa
)))
596 error
= svc_addparty(sock
, (struct sockaddr
*)&sa
, sizeof(sa
),
600 if (!test_bit(ATM_VF_SESSION
, &vcc
->flags
))
602 if (copy_from_user(&ep_ref
, (void __user
*) arg
, sizeof(int)))
604 error
= svc_dropparty(sock
, ep_ref
);
607 error
= vcc_ioctl(sock
, cmd
, arg
);
614 static int svc_compat_ioctl(struct socket
*sock
, unsigned int cmd
,
617 /* The definition of ATM_ADDPARTY uses the size of struct atm_iobuf.
618 But actually it takes a struct sockaddr_atmsvc, which doesn't need
619 compat handling. So all we have to do is fix up cmd... */
620 if (cmd
== COMPAT_ATM_ADDPARTY
)
623 if (cmd
== ATM_ADDPARTY
|| cmd
== ATM_DROPPARTY
)
624 return svc_ioctl(sock
, cmd
, arg
);
626 return vcc_compat_ioctl(sock
, cmd
, arg
);
628 #endif /* CONFIG_COMPAT */
630 static const struct proto_ops svc_proto_ops
= {
632 .owner
= THIS_MODULE
,
634 .release
= svc_release
,
636 .connect
= svc_connect
,
637 .socketpair
= sock_no_socketpair
,
638 .accept
= svc_accept
,
639 .getname
= svc_getname
,
643 .compat_ioctl
= svc_compat_ioctl
,
645 .listen
= svc_listen
,
646 .shutdown
= svc_shutdown
,
647 .setsockopt
= svc_setsockopt
,
648 .getsockopt
= svc_getsockopt
,
649 .sendmsg
= vcc_sendmsg
,
650 .recvmsg
= vcc_recvmsg
,
651 .mmap
= sock_no_mmap
,
652 .sendpage
= sock_no_sendpage
,
656 static int svc_create(struct net
*net
, struct socket
*sock
, int protocol
,
661 if (!net_eq(net
, &init_net
))
662 return -EAFNOSUPPORT
;
664 sock
->ops
= &svc_proto_ops
;
665 error
= vcc_create(net
, sock
, protocol
, AF_ATMSVC
);
668 ATM_SD(sock
)->local
.sas_family
= AF_ATMSVC
;
669 ATM_SD(sock
)->remote
.sas_family
= AF_ATMSVC
;
673 static const struct net_proto_family svc_family_ops
= {
675 .create
= svc_create
,
676 .owner
= THIS_MODULE
,
681 * Initialize the ATM SVC protocol family
684 int __init
atmsvc_init(void)
686 return sock_register(&svc_family_ops
);
689 void atmsvc_exit(void)
691 sock_unregister(PF_ATMSVC
);