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 addr
= (struct sockaddr_atmsvc
*) sockaddr
;
122 if (addr
->sas_family
!= AF_ATMSVC
) {
123 error
= -EAFNOSUPPORT
;
126 clear_bit(ATM_VF_BOUND
,&vcc
->flags
);
127 /* failing rebind will kill old binding */
128 /* @@@ check memory (de)allocation on rebind */
129 if (!test_bit(ATM_VF_HASQOS
,&vcc
->flags
)) {
134 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
135 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_UNINTERRUPTIBLE
);
136 sigd_enq(vcc
,as_bind
,NULL
,NULL
,&vcc
->local
);
137 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) && sigd
) {
139 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_UNINTERRUPTIBLE
);
141 finish_wait(sk
->sk_sleep
, &wait
);
142 clear_bit(ATM_VF_REGIS
,&vcc
->flags
); /* doesn't count */
148 set_bit(ATM_VF_BOUND
,&vcc
->flags
);
156 static int svc_connect(struct socket
*sock
,struct sockaddr
*sockaddr
,
157 int sockaddr_len
,int flags
)
160 struct sock
*sk
= sock
->sk
;
161 struct sockaddr_atmsvc
*addr
;
162 struct atm_vcc
*vcc
= ATM_SD(sock
);
165 DPRINTK("svc_connect %p\n",vcc
);
167 if (sockaddr_len
!= sizeof(struct sockaddr_atmsvc
)) {
172 switch (sock
->state
) {
180 if (test_bit(ATM_VF_WAITING
, &vcc
->flags
)) {
184 sock
->state
= SS_UNCONNECTED
;
191 addr
= (struct sockaddr_atmsvc
*) sockaddr
;
192 if (addr
->sas_family
!= AF_ATMSVC
) {
193 error
= -EAFNOSUPPORT
;
196 if (!test_bit(ATM_VF_HASQOS
, &vcc
->flags
)) {
200 if (vcc
->qos
.txtp
.traffic_class
== ATM_ANYCLASS
||
201 vcc
->qos
.rxtp
.traffic_class
== ATM_ANYCLASS
) {
205 if (!vcc
->qos
.txtp
.traffic_class
&&
206 !vcc
->qos
.rxtp
.traffic_class
) {
211 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
212 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
213 sigd_enq(vcc
,as_connect
,NULL
,NULL
,&vcc
->remote
);
214 if (flags
& O_NONBLOCK
) {
215 finish_wait(sk
->sk_sleep
, &wait
);
216 sock
->state
= SS_CONNECTING
;
217 error
= -EINPROGRESS
;
221 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) && sigd
) {
223 if (!signal_pending(current
)) {
224 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
227 DPRINTK("*ABORT*\n");
230 * Kernel ---close--> Demon
231 * Kernel <--close--- Demon
233 * Kernel ---close--> Demon
234 * Kernel <--error--- Demon
236 * Kernel ---close--> Demon
237 * Kernel <--okay---- Demon
238 * Kernel <--close--- Demon
240 sigd_enq(vcc
,as_close
,NULL
,NULL
,NULL
);
241 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) && sigd
) {
242 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
246 while (!test_bit(ATM_VF_RELEASED
,&vcc
->flags
)
248 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
251 clear_bit(ATM_VF_REGIS
,&vcc
->flags
);
252 clear_bit(ATM_VF_RELEASED
,&vcc
->flags
);
253 clear_bit(ATM_VF_CLOSE
,&vcc
->flags
);
254 /* we're gone now but may connect later */
258 finish_wait(sk
->sk_sleep
, &wait
);
273 * #ifndef CONFIG_SINGLE_SIGITF
275 vcc
->qos
.txtp
.max_pcr
= SELECT_TOP_PCR(vcc
->qos
.txtp
);
276 vcc
->qos
.txtp
.pcr
= 0;
277 vcc
->qos
.txtp
.min_pcr
= 0;
281 if (!(error
= vcc_connect(sock
, vcc
->itf
, vcc
->vpi
, vcc
->vci
)))
282 sock
->state
= SS_CONNECTED
;
284 (void) svc_disconnect(vcc
);
291 static int svc_listen(struct socket
*sock
,int backlog
)
294 struct sock
*sk
= sock
->sk
;
295 struct atm_vcc
*vcc
= ATM_SD(sock
);
298 DPRINTK("svc_listen %p\n",vcc
);
300 /* let server handle listen on unbound sockets */
301 if (test_bit(ATM_VF_SESSION
,&vcc
->flags
)) {
305 vcc_insert_socket(sk
);
306 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
307 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_UNINTERRUPTIBLE
);
308 sigd_enq(vcc
,as_listen
,NULL
,NULL
,&vcc
->local
);
309 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) && sigd
) {
311 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_UNINTERRUPTIBLE
);
313 finish_wait(sk
->sk_sleep
, &wait
);
318 set_bit(ATM_VF_LISTEN
,&vcc
->flags
);
319 sk
->sk_max_ack_backlog
= backlog
> 0 ? backlog
: ATM_BACKLOG_DEFAULT
;
327 static int svc_accept(struct socket
*sock
,struct socket
*newsock
,int flags
)
329 struct sock
*sk
= sock
->sk
;
331 struct atmsvc_msg
*msg
;
332 struct atm_vcc
*old_vcc
= ATM_SD(sock
);
333 struct atm_vcc
*new_vcc
;
338 error
= svc_create(newsock
,0);
342 new_vcc
= ATM_SD(newsock
);
344 DPRINTK("svc_accept %p -> %p\n",old_vcc
,new_vcc
);
348 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
349 while (!(skb
= skb_dequeue(&sk
->sk_receive_queue
)) &&
351 if (test_bit(ATM_VF_RELEASED
,&old_vcc
->flags
)) break;
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
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
369 finish_wait(sk
->sk_sleep
, &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
, msg
->pvc
.sap_addr
.vci
);
385 sk
->sk_ack_backlog
--;
387 sigd_enq2(NULL
,as_reject
,old_vcc
,NULL
,NULL
,
388 &old_vcc
->qos
,error
);
389 error
= error
== -EAGAIN
? -EBUSY
: error
;
392 /* wait should be short, so we ignore the non-blocking flag */
393 set_bit(ATM_VF_WAITING
, &new_vcc
->flags
);
394 prepare_to_wait(sk_atm(new_vcc
)->sk_sleep
, &wait
, TASK_UNINTERRUPTIBLE
);
395 sigd_enq(new_vcc
,as_accept
,old_vcc
,NULL
,NULL
);
396 while (test_bit(ATM_VF_WAITING
, &new_vcc
->flags
) && sigd
) {
400 prepare_to_wait(sk_atm(new_vcc
)->sk_sleep
, &wait
, TASK_UNINTERRUPTIBLE
);
402 finish_wait(sk_atm(new_vcc
)->sk_sleep
, &wait
);
407 if (!sk_atm(new_vcc
)->sk_err
)
409 if (sk_atm(new_vcc
)->sk_err
!= ERESTARTSYS
) {
410 error
= -sk_atm(new_vcc
)->sk_err
;
414 newsock
->state
= SS_CONNECTED
;
421 static int svc_getname(struct socket
*sock
,struct sockaddr
*sockaddr
,
422 int *sockaddr_len
,int peer
)
424 struct sockaddr_atmsvc
*addr
;
426 *sockaddr_len
= sizeof(struct sockaddr_atmsvc
);
427 addr
= (struct sockaddr_atmsvc
*) sockaddr
;
428 memcpy(addr
,peer
? &ATM_SD(sock
)->remote
: &ATM_SD(sock
)->local
,
429 sizeof(struct sockaddr_atmsvc
));
434 int svc_change_qos(struct atm_vcc
*vcc
,struct atm_qos
*qos
)
436 struct sock
*sk
= sk_atm(vcc
);
439 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
440 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_UNINTERRUPTIBLE
);
441 sigd_enq2(vcc
,as_modify
,NULL
,NULL
,&vcc
->local
,qos
,0);
442 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) &&
443 !test_bit(ATM_VF_RELEASED
, &vcc
->flags
) && sigd
) {
445 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_UNINTERRUPTIBLE
);
447 finish_wait(sk
->sk_sleep
, &wait
);
448 if (!sigd
) return -EUNATCH
;
453 static int svc_setsockopt(struct socket
*sock
, int level
, int optname
,
454 char __user
*optval
, int optlen
)
456 struct sock
*sk
= sock
->sk
;
457 struct atm_vcc
*vcc
= ATM_SD(sock
);
458 int value
, error
= 0;
463 if (level
!= SOL_ATM
|| optlen
!= sizeof(struct atm_sap
)) {
467 if (copy_from_user(&vcc
->sap
, optval
, optlen
)) {
471 set_bit(ATM_VF_HASSAP
, &vcc
->flags
);
474 if (level
!= SOL_ATM
|| optlen
!= sizeof(int)) {
478 if (get_user(value
, (int __user
*) optval
)) {
483 set_bit(ATM_VF_SESSION
, &vcc
->flags
);
484 } else if (value
== 0) {
485 clear_bit(ATM_VF_SESSION
, &vcc
->flags
);
491 error
= vcc_setsockopt(sock
, level
, optname
,
501 static int svc_getsockopt(struct socket
*sock
,int level
,int optname
,
502 char __user
*optval
,int __user
*optlen
)
504 struct sock
*sk
= sock
->sk
;
508 if (!__SO_LEVEL_MATCH(optname
, level
) || optname
!= SO_ATMSAP
) {
509 error
= vcc_getsockopt(sock
, level
, optname
, optval
, optlen
);
512 if (get_user(len
, optlen
)) {
516 if (len
!= sizeof(struct atm_sap
)) {
520 if (copy_to_user(optval
, &ATM_SD(sock
)->sap
, sizeof(struct atm_sap
))) {
530 static int svc_addparty(struct socket
*sock
, struct sockaddr
*sockaddr
,
531 int sockaddr_len
, int flags
)
534 struct sock
*sk
= sock
->sk
;
535 struct atm_vcc
*vcc
= ATM_SD(sock
);
539 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
540 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
541 sigd_enq(vcc
, as_addparty
, NULL
, NULL
,
542 (struct sockaddr_atmsvc
*) sockaddr
);
543 if (flags
& O_NONBLOCK
) {
544 finish_wait(sk
->sk_sleep
, &wait
);
545 error
= -EINPROGRESS
;
548 DPRINTK("svc_addparty added wait queue\n");
549 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) && sigd
) {
551 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
553 finish_wait(sk
->sk_sleep
, &wait
);
554 error
= xchg(&sk
->sk_err_soft
, 0);
561 static int svc_dropparty(struct socket
*sock
, int ep_ref
)
564 struct sock
*sk
= sock
->sk
;
565 struct atm_vcc
*vcc
= ATM_SD(sock
);
569 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
570 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
571 sigd_enq2(vcc
, as_dropparty
, NULL
, NULL
, NULL
, NULL
, ep_ref
);
572 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) && sigd
) {
574 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
576 finish_wait(sk
->sk_sleep
, &wait
);
581 error
= xchg(&sk
->sk_err_soft
, 0);
588 static int svc_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
591 struct sockaddr_atmsvc sa
;
592 struct atm_vcc
*vcc
= ATM_SD(sock
);
596 if (!test_bit(ATM_VF_SESSION
, &vcc
->flags
))
598 if (copy_from_user(&sa
, (void __user
*) arg
, sizeof(sa
)))
600 error
= svc_addparty(sock
, (struct sockaddr
*) &sa
, sizeof(sa
), 0);
603 if (!test_bit(ATM_VF_SESSION
, &vcc
->flags
))
605 if (copy_from_user(&ep_ref
, (void __user
*) arg
, sizeof(int)))
607 error
= svc_dropparty(sock
, ep_ref
);
610 error
= vcc_ioctl(sock
, cmd
, arg
);
616 static const struct proto_ops svc_proto_ops
= {
618 .owner
= THIS_MODULE
,
620 .release
= svc_release
,
622 .connect
= svc_connect
,
623 .socketpair
= sock_no_socketpair
,
624 .accept
= svc_accept
,
625 .getname
= svc_getname
,
628 .listen
= svc_listen
,
629 .shutdown
= svc_shutdown
,
630 .setsockopt
= svc_setsockopt
,
631 .getsockopt
= svc_getsockopt
,
632 .sendmsg
= vcc_sendmsg
,
633 .recvmsg
= vcc_recvmsg
,
634 .mmap
= sock_no_mmap
,
635 .sendpage
= sock_no_sendpage
,
639 static int svc_create(struct socket
*sock
,int protocol
)
643 sock
->ops
= &svc_proto_ops
;
644 error
= vcc_create(sock
, protocol
, AF_ATMSVC
);
645 if (error
) return error
;
646 ATM_SD(sock
)->local
.sas_family
= AF_ATMSVC
;
647 ATM_SD(sock
)->remote
.sas_family
= AF_ATMSVC
;
652 static struct net_proto_family svc_family_ops
= {
654 .create
= svc_create
,
655 .owner
= THIS_MODULE
,
660 * Initialize the ATM SVC protocol family
663 int __init
atmsvc_init(void)
665 return sock_register(&svc_family_ops
);
668 void atmsvc_exit(void)
670 sock_unregister(PF_ATMSVC
);