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 vcc_insert_socket(sk
);
297 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
298 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_UNINTERRUPTIBLE
);
299 sigd_enq(vcc
,as_listen
,NULL
,NULL
,&vcc
->local
);
300 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) && sigd
) {
302 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_UNINTERRUPTIBLE
);
304 finish_wait(sk
->sk_sleep
, &wait
);
309 set_bit(ATM_VF_LISTEN
,&vcc
->flags
);
310 sk
->sk_max_ack_backlog
= backlog
> 0 ? backlog
: ATM_BACKLOG_DEFAULT
;
318 static int svc_accept(struct socket
*sock
,struct socket
*newsock
,int flags
)
320 struct sock
*sk
= sock
->sk
;
322 struct atmsvc_msg
*msg
;
323 struct atm_vcc
*old_vcc
= ATM_SD(sock
);
324 struct atm_vcc
*new_vcc
;
329 error
= svc_create(sock_net(sk
), newsock
,0);
333 new_vcc
= ATM_SD(newsock
);
335 pr_debug("svc_accept %p -> %p\n",old_vcc
,new_vcc
);
339 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
340 while (!(skb
= skb_dequeue(&sk
->sk_receive_queue
)) &&
342 if (test_bit(ATM_VF_RELEASED
,&old_vcc
->flags
)) break;
343 if (test_bit(ATM_VF_CLOSE
,&old_vcc
->flags
)) {
347 if (flags
& O_NONBLOCK
) {
354 if (signal_pending(current
)) {
355 error
= -ERESTARTSYS
;
358 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
360 finish_wait(sk
->sk_sleep
, &wait
);
367 msg
= (struct atmsvc_msg
*) skb
->data
;
368 new_vcc
->qos
= msg
->qos
;
369 set_bit(ATM_VF_HASQOS
,&new_vcc
->flags
);
370 new_vcc
->remote
= msg
->svc
;
371 new_vcc
->local
= msg
->local
;
372 new_vcc
->sap
= msg
->sap
;
373 error
= vcc_connect(newsock
, msg
->pvc
.sap_addr
.itf
,
374 msg
->pvc
.sap_addr
.vpi
, msg
->pvc
.sap_addr
.vci
);
376 sk
->sk_ack_backlog
--;
378 sigd_enq2(NULL
,as_reject
,old_vcc
,NULL
,NULL
,
379 &old_vcc
->qos
,error
);
380 error
= error
== -EAGAIN
? -EBUSY
: error
;
383 /* wait should be short, so we ignore the non-blocking flag */
384 set_bit(ATM_VF_WAITING
, &new_vcc
->flags
);
385 prepare_to_wait(sk_atm(new_vcc
)->sk_sleep
, &wait
, TASK_UNINTERRUPTIBLE
);
386 sigd_enq(new_vcc
,as_accept
,old_vcc
,NULL
,NULL
);
387 while (test_bit(ATM_VF_WAITING
, &new_vcc
->flags
) && sigd
) {
391 prepare_to_wait(sk_atm(new_vcc
)->sk_sleep
, &wait
, TASK_UNINTERRUPTIBLE
);
393 finish_wait(sk_atm(new_vcc
)->sk_sleep
, &wait
);
398 if (!sk_atm(new_vcc
)->sk_err
)
400 if (sk_atm(new_vcc
)->sk_err
!= ERESTARTSYS
) {
401 error
= -sk_atm(new_vcc
)->sk_err
;
405 newsock
->state
= SS_CONNECTED
;
412 static int svc_getname(struct socket
*sock
,struct sockaddr
*sockaddr
,
413 int *sockaddr_len
,int peer
)
415 struct sockaddr_atmsvc
*addr
;
417 *sockaddr_len
= sizeof(struct sockaddr_atmsvc
);
418 addr
= (struct sockaddr_atmsvc
*) sockaddr
;
419 memcpy(addr
,peer
? &ATM_SD(sock
)->remote
: &ATM_SD(sock
)->local
,
420 sizeof(struct sockaddr_atmsvc
));
425 int svc_change_qos(struct atm_vcc
*vcc
,struct atm_qos
*qos
)
427 struct sock
*sk
= sk_atm(vcc
);
430 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
431 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_UNINTERRUPTIBLE
);
432 sigd_enq2(vcc
,as_modify
,NULL
,NULL
,&vcc
->local
,qos
,0);
433 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) &&
434 !test_bit(ATM_VF_RELEASED
, &vcc
->flags
) && sigd
) {
436 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_UNINTERRUPTIBLE
);
438 finish_wait(sk
->sk_sleep
, &wait
);
439 if (!sigd
) return -EUNATCH
;
444 static int svc_setsockopt(struct socket
*sock
, int level
, int optname
,
445 char __user
*optval
, int optlen
)
447 struct sock
*sk
= sock
->sk
;
448 struct atm_vcc
*vcc
= ATM_SD(sock
);
449 int value
, error
= 0;
454 if (level
!= SOL_ATM
|| optlen
!= sizeof(struct atm_sap
)) {
458 if (copy_from_user(&vcc
->sap
, optval
, optlen
)) {
462 set_bit(ATM_VF_HASSAP
, &vcc
->flags
);
465 if (level
!= SOL_ATM
|| optlen
!= sizeof(int)) {
469 if (get_user(value
, (int __user
*) optval
)) {
474 set_bit(ATM_VF_SESSION
, &vcc
->flags
);
475 } else if (value
== 0) {
476 clear_bit(ATM_VF_SESSION
, &vcc
->flags
);
482 error
= vcc_setsockopt(sock
, level
, optname
,
492 static int svc_getsockopt(struct socket
*sock
,int level
,int optname
,
493 char __user
*optval
,int __user
*optlen
)
495 struct sock
*sk
= sock
->sk
;
499 if (!__SO_LEVEL_MATCH(optname
, level
) || optname
!= SO_ATMSAP
) {
500 error
= vcc_getsockopt(sock
, level
, optname
, optval
, optlen
);
503 if (get_user(len
, optlen
)) {
507 if (len
!= sizeof(struct atm_sap
)) {
511 if (copy_to_user(optval
, &ATM_SD(sock
)->sap
, sizeof(struct atm_sap
))) {
521 static int svc_addparty(struct socket
*sock
, struct sockaddr
*sockaddr
,
522 int sockaddr_len
, int flags
)
525 struct sock
*sk
= sock
->sk
;
526 struct atm_vcc
*vcc
= ATM_SD(sock
);
530 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
531 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
532 sigd_enq(vcc
, as_addparty
, NULL
, NULL
,
533 (struct sockaddr_atmsvc
*) sockaddr
);
534 if (flags
& O_NONBLOCK
) {
535 finish_wait(sk
->sk_sleep
, &wait
);
536 error
= -EINPROGRESS
;
539 pr_debug("svc_addparty added wait queue\n");
540 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) && sigd
) {
542 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
544 finish_wait(sk
->sk_sleep
, &wait
);
545 error
= xchg(&sk
->sk_err_soft
, 0);
552 static int svc_dropparty(struct socket
*sock
, int ep_ref
)
555 struct sock
*sk
= sock
->sk
;
556 struct atm_vcc
*vcc
= ATM_SD(sock
);
560 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
561 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
562 sigd_enq2(vcc
, as_dropparty
, NULL
, NULL
, NULL
, NULL
, ep_ref
);
563 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) && sigd
) {
565 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
567 finish_wait(sk
->sk_sleep
, &wait
);
572 error
= xchg(&sk
->sk_err_soft
, 0);
579 static int svc_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
582 struct sockaddr_atmsvc sa
;
583 struct atm_vcc
*vcc
= ATM_SD(sock
);
587 if (!test_bit(ATM_VF_SESSION
, &vcc
->flags
))
589 if (copy_from_user(&sa
, (void __user
*) arg
, sizeof(sa
)))
591 error
= svc_addparty(sock
, (struct sockaddr
*) &sa
, sizeof(sa
), 0);
594 if (!test_bit(ATM_VF_SESSION
, &vcc
->flags
))
596 if (copy_from_user(&ep_ref
, (void __user
*) arg
, sizeof(int)))
598 error
= svc_dropparty(sock
, ep_ref
);
601 error
= vcc_ioctl(sock
, cmd
, arg
);
607 static const struct proto_ops svc_proto_ops
= {
609 .owner
= THIS_MODULE
,
611 .release
= svc_release
,
613 .connect
= svc_connect
,
614 .socketpair
= sock_no_socketpair
,
615 .accept
= svc_accept
,
616 .getname
= svc_getname
,
619 .listen
= svc_listen
,
620 .shutdown
= svc_shutdown
,
621 .setsockopt
= svc_setsockopt
,
622 .getsockopt
= svc_getsockopt
,
623 .sendmsg
= vcc_sendmsg
,
624 .recvmsg
= vcc_recvmsg
,
625 .mmap
= sock_no_mmap
,
626 .sendpage
= sock_no_sendpage
,
630 static int svc_create(struct net
*net
, struct socket
*sock
,int protocol
)
634 if (net
!= &init_net
)
635 return -EAFNOSUPPORT
;
637 sock
->ops
= &svc_proto_ops
;
638 error
= vcc_create(net
, sock
, protocol
, AF_ATMSVC
);
639 if (error
) return error
;
640 ATM_SD(sock
)->local
.sas_family
= AF_ATMSVC
;
641 ATM_SD(sock
)->remote
.sas_family
= AF_ATMSVC
;
646 static struct net_proto_family svc_family_ops
= {
648 .create
= svc_create
,
649 .owner
= THIS_MODULE
,
654 * Initialize the ATM SVC protocol family
657 int __init
atmsvc_init(void)
659 return sock_register(&svc_family_ops
);
662 void atmsvc_exit(void)
664 sock_unregister(PF_ATMSVC
);