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"
30 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
32 #define DPRINTK(format,args...)
36 static int svc_create(struct socket
*sock
,int protocol
);
40 * Note: since all this is still nicely synchronized with the signaling demon,
41 * there's no need to protect sleep loops with clis. If signaling is
42 * moved into the kernel, that would change.
46 static int svc_shutdown(struct socket
*sock
,int how
)
52 static void svc_disconnect(struct atm_vcc
*vcc
)
56 struct sock
*sk
= sk_atm(vcc
);
58 DPRINTK("svc_disconnect %p\n",vcc
);
59 if (test_bit(ATM_VF_REGIS
,&vcc
->flags
)) {
60 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_UNINTERRUPTIBLE
);
61 sigd_enq(vcc
,as_close
,NULL
,NULL
,NULL
);
62 while (!test_bit(ATM_VF_RELEASED
,&vcc
->flags
) && sigd
) {
64 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_UNINTERRUPTIBLE
);
66 finish_wait(sk
->sk_sleep
, &wait
);
68 /* beware - socket is still in use by atmsigd until the last
69 as_indicate has been answered */
70 while ((skb
= skb_dequeue(&sk
->sk_receive_queue
)) != NULL
) {
71 atm_return(vcc
, skb
->truesize
);
72 DPRINTK("LISTEN REL\n");
73 sigd_enq2(NULL
,as_reject
,vcc
,NULL
,NULL
,&vcc
->qos
,0);
76 clear_bit(ATM_VF_REGIS
, &vcc
->flags
);
77 /* ... may retry later */
81 static int svc_release(struct socket
*sock
)
83 struct sock
*sk
= sock
->sk
;
88 DPRINTK("svc_release %p\n", vcc
);
89 clear_bit(ATM_VF_READY
, &vcc
->flags
);
90 /* VCC pointer is used as a reference, so we must not free it
91 (thereby subjecting it to re-use) before all pending connections
100 static int svc_bind(struct socket
*sock
,struct sockaddr
*sockaddr
,
104 struct sock
*sk
= sock
->sk
;
105 struct sockaddr_atmsvc
*addr
;
109 if (sockaddr_len
!= sizeof(struct sockaddr_atmsvc
))
112 if (sock
->state
== SS_CONNECTED
) {
116 if (sock
->state
!= SS_UNCONNECTED
) {
121 if (test_bit(ATM_VF_SESSION
, &vcc
->flags
)) {
125 addr
= (struct sockaddr_atmsvc
*) sockaddr
;
126 if (addr
->sas_family
!= AF_ATMSVC
) {
127 error
= -EAFNOSUPPORT
;
130 clear_bit(ATM_VF_BOUND
,&vcc
->flags
);
131 /* failing rebind will kill old binding */
132 /* @@@ check memory (de)allocation on rebind */
133 if (!test_bit(ATM_VF_HASQOS
,&vcc
->flags
)) {
138 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
139 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_UNINTERRUPTIBLE
);
140 sigd_enq(vcc
,as_bind
,NULL
,NULL
,&vcc
->local
);
141 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) && sigd
) {
143 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_UNINTERRUPTIBLE
);
145 finish_wait(sk
->sk_sleep
, &wait
);
146 clear_bit(ATM_VF_REGIS
,&vcc
->flags
); /* doesn't count */
152 set_bit(ATM_VF_BOUND
,&vcc
->flags
);
160 static int svc_connect(struct socket
*sock
,struct sockaddr
*sockaddr
,
161 int sockaddr_len
,int flags
)
164 struct sock
*sk
= sock
->sk
;
165 struct sockaddr_atmsvc
*addr
;
166 struct atm_vcc
*vcc
= ATM_SD(sock
);
169 DPRINTK("svc_connect %p\n",vcc
);
171 if (sockaddr_len
!= sizeof(struct sockaddr_atmsvc
)) {
176 switch (sock
->state
) {
184 if (test_bit(ATM_VF_WAITING
, &vcc
->flags
)) {
188 sock
->state
= SS_UNCONNECTED
;
195 addr
= (struct sockaddr_atmsvc
*) sockaddr
;
196 if (addr
->sas_family
!= AF_ATMSVC
) {
197 error
= -EAFNOSUPPORT
;
200 if (!test_bit(ATM_VF_HASQOS
, &vcc
->flags
)) {
204 if (vcc
->qos
.txtp
.traffic_class
== ATM_ANYCLASS
||
205 vcc
->qos
.rxtp
.traffic_class
== ATM_ANYCLASS
) {
209 if (!vcc
->qos
.txtp
.traffic_class
&&
210 !vcc
->qos
.rxtp
.traffic_class
) {
215 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
216 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
217 sigd_enq(vcc
,as_connect
,NULL
,NULL
,&vcc
->remote
);
218 if (flags
& O_NONBLOCK
) {
219 finish_wait(sk
->sk_sleep
, &wait
);
220 sock
->state
= SS_CONNECTING
;
221 error
= -EINPROGRESS
;
225 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) && sigd
) {
227 if (!signal_pending(current
)) {
228 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
231 DPRINTK("*ABORT*\n");
234 * Kernel ---close--> Demon
235 * Kernel <--close--- Demon
237 * Kernel ---close--> Demon
238 * Kernel <--error--- Demon
240 * Kernel ---close--> Demon
241 * Kernel <--okay---- Demon
242 * Kernel <--close--- Demon
244 sigd_enq(vcc
,as_close
,NULL
,NULL
,NULL
);
245 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) && sigd
) {
246 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
250 while (!test_bit(ATM_VF_RELEASED
,&vcc
->flags
)
252 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
255 clear_bit(ATM_VF_REGIS
,&vcc
->flags
);
256 clear_bit(ATM_VF_RELEASED
,&vcc
->flags
);
257 clear_bit(ATM_VF_CLOSE
,&vcc
->flags
);
258 /* we're gone now but may connect later */
262 finish_wait(sk
->sk_sleep
, &wait
);
277 * #ifndef CONFIG_SINGLE_SIGITF
279 vcc
->qos
.txtp
.max_pcr
= SELECT_TOP_PCR(vcc
->qos
.txtp
);
280 vcc
->qos
.txtp
.pcr
= 0;
281 vcc
->qos
.txtp
.min_pcr
= 0;
285 if (!(error
= vcc_connect(sock
, vcc
->itf
, vcc
->vpi
, vcc
->vci
)))
286 sock
->state
= SS_CONNECTED
;
288 (void) svc_disconnect(vcc
);
295 static int svc_listen(struct socket
*sock
,int backlog
)
298 struct sock
*sk
= sock
->sk
;
299 struct atm_vcc
*vcc
= ATM_SD(sock
);
302 DPRINTK("svc_listen %p\n",vcc
);
304 /* let server handle listen on unbound sockets */
305 if (test_bit(ATM_VF_SESSION
,&vcc
->flags
)) {
309 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
310 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_UNINTERRUPTIBLE
);
311 sigd_enq(vcc
,as_listen
,NULL
,NULL
,&vcc
->local
);
312 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) && sigd
) {
314 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_UNINTERRUPTIBLE
);
316 finish_wait(sk
->sk_sleep
, &wait
);
321 set_bit(ATM_VF_LISTEN
,&vcc
->flags
);
322 sk
->sk_max_ack_backlog
= backlog
> 0 ? backlog
: ATM_BACKLOG_DEFAULT
;
330 static int svc_accept(struct socket
*sock
,struct socket
*newsock
,int flags
)
332 struct sock
*sk
= sock
->sk
;
334 struct atmsvc_msg
*msg
;
335 struct atm_vcc
*old_vcc
= ATM_SD(sock
);
336 struct atm_vcc
*new_vcc
;
341 error
= svc_create(newsock
,0);
345 new_vcc
= ATM_SD(newsock
);
347 DPRINTK("svc_accept %p -> %p\n",old_vcc
,new_vcc
);
351 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
352 while (!(skb
= skb_dequeue(&sk
->sk_receive_queue
)) &&
354 if (test_bit(ATM_VF_RELEASED
,&old_vcc
->flags
)) break;
355 if (test_bit(ATM_VF_CLOSE
,&old_vcc
->flags
)) {
359 if (flags
& O_NONBLOCK
) {
366 if (signal_pending(current
)) {
367 error
= -ERESTARTSYS
;
370 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
372 finish_wait(sk
->sk_sleep
, &wait
);
379 msg
= (struct atmsvc_msg
*) skb
->data
;
380 new_vcc
->qos
= msg
->qos
;
381 set_bit(ATM_VF_HASQOS
,&new_vcc
->flags
);
382 new_vcc
->remote
= msg
->svc
;
383 new_vcc
->local
= msg
->local
;
384 new_vcc
->sap
= msg
->sap
;
385 error
= vcc_connect(newsock
, msg
->pvc
.sap_addr
.itf
,
386 msg
->pvc
.sap_addr
.vpi
, msg
->pvc
.sap_addr
.vci
);
388 sk
->sk_ack_backlog
--;
390 sigd_enq2(NULL
,as_reject
,old_vcc
,NULL
,NULL
,
391 &old_vcc
->qos
,error
);
392 error
= error
== -EAGAIN
? -EBUSY
: error
;
395 /* wait should be short, so we ignore the non-blocking flag */
396 set_bit(ATM_VF_WAITING
, &new_vcc
->flags
);
397 prepare_to_wait(sk_atm(new_vcc
)->sk_sleep
, &wait
, 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_atm(new_vcc
)->sk_sleep
, &wait
, TASK_UNINTERRUPTIBLE
);
405 finish_wait(sk_atm(new_vcc
)->sk_sleep
, &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
;
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
));
437 int svc_change_qos(struct atm_vcc
*vcc
,struct atm_qos
*qos
)
439 struct sock
*sk
= sk_atm(vcc
);
442 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
443 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_UNINTERRUPTIBLE
);
444 sigd_enq2(vcc
,as_modify
,NULL
,NULL
,&vcc
->local
,qos
,0);
445 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) &&
446 !test_bit(ATM_VF_RELEASED
, &vcc
->flags
) && sigd
) {
448 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_UNINTERRUPTIBLE
);
450 finish_wait(sk
->sk_sleep
, &wait
);
451 if (!sigd
) return -EUNATCH
;
456 static int svc_setsockopt(struct socket
*sock
, int level
, int optname
,
457 char __user
*optval
, int optlen
)
459 struct sock
*sk
= sock
->sk
;
460 struct atm_vcc
*vcc
= ATM_SD(sock
);
461 int value
, error
= 0;
466 if (level
!= SOL_ATM
|| optlen
!= sizeof(struct atm_sap
)) {
470 if (copy_from_user(&vcc
->sap
, optval
, optlen
)) {
474 set_bit(ATM_VF_HASSAP
, &vcc
->flags
);
477 if (level
!= SOL_ATM
|| optlen
!= sizeof(int)) {
481 if (get_user(value
, (int __user
*) optval
)) {
486 set_bit(ATM_VF_SESSION
, &vcc
->flags
);
487 } else if (value
== 0) {
488 clear_bit(ATM_VF_SESSION
, &vcc
->flags
);
494 error
= vcc_setsockopt(sock
, level
, optname
,
504 static int svc_getsockopt(struct socket
*sock
,int level
,int optname
,
505 char __user
*optval
,int __user
*optlen
)
507 struct sock
*sk
= sock
->sk
;
511 if (!__SO_LEVEL_MATCH(optname
, level
) || optname
!= SO_ATMSAP
) {
512 error
= vcc_getsockopt(sock
, level
, optname
, optval
, optlen
);
515 if (get_user(len
, optlen
)) {
519 if (len
!= sizeof(struct atm_sap
)) {
523 if (copy_to_user(optval
, &ATM_SD(sock
)->sap
, sizeof(struct atm_sap
))) {
533 static int svc_addparty(struct socket
*sock
, struct sockaddr
*sockaddr
,
534 int sockaddr_len
, int flags
)
537 struct sock
*sk
= sock
->sk
;
538 struct atm_vcc
*vcc
= ATM_SD(sock
);
542 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
543 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
544 sigd_enq(vcc
, as_addparty
, NULL
, NULL
,
545 (struct sockaddr_atmsvc
*) sockaddr
);
546 if (flags
& O_NONBLOCK
) {
547 finish_wait(sk
->sk_sleep
, &wait
);
548 error
= -EINPROGRESS
;
551 DPRINTK("svc_addparty added wait queue\n");
552 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) && sigd
) {
554 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
556 finish_wait(sk
->sk_sleep
, &wait
);
557 error
= xchg(&sk
->sk_err_soft
, 0);
564 static int svc_dropparty(struct socket
*sock
, int ep_ref
)
567 struct sock
*sk
= sock
->sk
;
568 struct atm_vcc
*vcc
= ATM_SD(sock
);
572 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
573 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
574 sigd_enq2(vcc
, as_dropparty
, NULL
, NULL
, NULL
, NULL
, ep_ref
);
575 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) && sigd
) {
577 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
579 finish_wait(sk
->sk_sleep
, &wait
);
584 error
= xchg(&sk
->sk_err_soft
, 0);
591 static int svc_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
594 struct sockaddr_atmsvc sa
;
595 struct atm_vcc
*vcc
= ATM_SD(sock
);
599 if (!test_bit(ATM_VF_SESSION
, &vcc
->flags
))
601 if (copy_from_user(&sa
, (void __user
*) arg
, sizeof(sa
)))
603 error
= svc_addparty(sock
, (struct sockaddr
*) &sa
, sizeof(sa
), 0);
606 if (!test_bit(ATM_VF_SESSION
, &vcc
->flags
))
608 if (copy_from_user(&ep_ref
, (void __user
*) arg
, sizeof(int)))
610 error
= svc_dropparty(sock
, ep_ref
);
613 error
= vcc_ioctl(sock
, cmd
, arg
);
619 static struct proto_ops svc_proto_ops
= {
621 .owner
= THIS_MODULE
,
623 .release
= svc_release
,
625 .connect
= svc_connect
,
626 .socketpair
= sock_no_socketpair
,
627 .accept
= svc_accept
,
628 .getname
= svc_getname
,
631 .listen
= svc_listen
,
632 .shutdown
= svc_shutdown
,
633 .setsockopt
= svc_setsockopt
,
634 .getsockopt
= svc_getsockopt
,
635 .sendmsg
= vcc_sendmsg
,
636 .recvmsg
= vcc_recvmsg
,
637 .mmap
= sock_no_mmap
,
638 .sendpage
= sock_no_sendpage
,
642 static int svc_create(struct socket
*sock
,int protocol
)
646 sock
->ops
= &svc_proto_ops
;
647 error
= vcc_create(sock
, protocol
, AF_ATMSVC
);
648 if (error
) return error
;
649 ATM_SD(sock
)->local
.sas_family
= AF_ATMSVC
;
650 ATM_SD(sock
)->remote
.sas_family
= AF_ATMSVC
;
655 static struct net_proto_family svc_family_ops
= {
657 .create
= svc_create
,
658 .owner
= THIS_MODULE
,
663 * Initialize the ATM SVC protocol family
666 int __init
atmsvc_init(void)
668 return sock_register(&svc_family_ops
);
671 void atmsvc_exit(void)
673 sock_unregister(PF_ATMSVC
);