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/signal.h>
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 sigd_enq(vcc
, as_close
, NULL
, NULL
, NULL
);
55 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_UNINTERRUPTIBLE
);
56 if (test_bit(ATM_VF_RELEASED
, &vcc
->flags
) || !sigd
)
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 sigd_enq(vcc
, as_bind
, NULL
, NULL
, &vcc
->local
);
131 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_UNINTERRUPTIBLE
);
132 if (!test_bit(ATM_VF_WAITING
, &vcc
->flags
) || !sigd
)
136 finish_wait(sk_sleep(sk
), &wait
);
137 clear_bit(ATM_VF_REGIS
, &vcc
->flags
); /* doesn't count */
143 set_bit(ATM_VF_BOUND
, &vcc
->flags
);
150 static int svc_connect(struct socket
*sock
, struct sockaddr
*sockaddr
,
151 int sockaddr_len
, int flags
)
154 struct sock
*sk
= sock
->sk
;
155 struct sockaddr_atmsvc
*addr
;
156 struct atm_vcc
*vcc
= ATM_SD(sock
);
159 pr_debug("%p\n", vcc
);
161 if (sockaddr_len
!= sizeof(struct sockaddr_atmsvc
)) {
166 switch (sock
->state
) {
174 if (test_bit(ATM_VF_WAITING
, &vcc
->flags
)) {
178 sock
->state
= SS_UNCONNECTED
;
185 addr
= (struct sockaddr_atmsvc
*) sockaddr
;
186 if (addr
->sas_family
!= AF_ATMSVC
) {
187 error
= -EAFNOSUPPORT
;
190 if (!test_bit(ATM_VF_HASQOS
, &vcc
->flags
)) {
194 if (vcc
->qos
.txtp
.traffic_class
== ATM_ANYCLASS
||
195 vcc
->qos
.rxtp
.traffic_class
== ATM_ANYCLASS
) {
199 if (!vcc
->qos
.txtp
.traffic_class
&&
200 !vcc
->qos
.rxtp
.traffic_class
) {
205 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
206 sigd_enq(vcc
, as_connect
, NULL
, NULL
, &vcc
->remote
);
207 if (flags
& O_NONBLOCK
) {
208 sock
->state
= SS_CONNECTING
;
209 error
= -EINPROGRESS
;
213 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
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
);
267 vcc
->qos
.txtp
.max_pcr
= SELECT_TOP_PCR(vcc
->qos
.txtp
);
268 vcc
->qos
.txtp
.pcr
= 0;
269 vcc
->qos
.txtp
.min_pcr
= 0;
271 error
= vcc_connect(sock
, vcc
->itf
, vcc
->vpi
, vcc
->vci
);
273 sock
->state
= SS_CONNECTED
;
275 (void)svc_disconnect(vcc
);
281 static int svc_listen(struct socket
*sock
, int backlog
)
284 struct sock
*sk
= sock
->sk
;
285 struct atm_vcc
*vcc
= ATM_SD(sock
);
288 pr_debug("%p\n", vcc
);
290 /* let server handle listen on unbound sockets */
291 if (test_bit(ATM_VF_SESSION
, &vcc
->flags
)) {
295 if (test_bit(ATM_VF_LISTEN
, &vcc
->flags
)) {
299 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
300 sigd_enq(vcc
, as_listen
, NULL
, NULL
, &vcc
->local
);
302 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_UNINTERRUPTIBLE
);
303 if (!test_bit(ATM_VF_WAITING
, &vcc
->flags
) || !sigd
)
307 finish_wait(sk_sleep(sk
), &wait
);
312 set_bit(ATM_VF_LISTEN
, &vcc
->flags
);
313 vcc_insert_socket(sk
);
314 sk
->sk_max_ack_backlog
= backlog
> 0 ? backlog
: ATM_BACKLOG_DEFAULT
;
321 static int svc_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
323 struct sock
*sk
= sock
->sk
;
325 struct atmsvc_msg
*msg
;
326 struct atm_vcc
*old_vcc
= ATM_SD(sock
);
327 struct atm_vcc
*new_vcc
;
332 error
= svc_create(sock_net(sk
), newsock
, 0, 0);
336 new_vcc
= ATM_SD(newsock
);
338 pr_debug("%p -> %p\n", old_vcc
, new_vcc
);
342 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
343 while (!(skb
= skb_dequeue(&sk
->sk_receive_queue
)) &&
345 if (test_bit(ATM_VF_RELEASED
, &old_vcc
->flags
))
347 if (test_bit(ATM_VF_CLOSE
, &old_vcc
->flags
)) {
351 if (flags
& O_NONBLOCK
) {
358 if (signal_pending(current
)) {
359 error
= -ERESTARTSYS
;
362 prepare_to_wait(sk_sleep(sk
), &wait
,
365 finish_wait(sk_sleep(sk
), &wait
);
372 msg
= (struct atmsvc_msg
*)skb
->data
;
373 new_vcc
->qos
= msg
->qos
;
374 set_bit(ATM_VF_HASQOS
, &new_vcc
->flags
);
375 new_vcc
->remote
= msg
->svc
;
376 new_vcc
->local
= msg
->local
;
377 new_vcc
->sap
= msg
->sap
;
378 error
= vcc_connect(newsock
, msg
->pvc
.sap_addr
.itf
,
379 msg
->pvc
.sap_addr
.vpi
,
380 msg
->pvc
.sap_addr
.vci
);
382 sk
->sk_ack_backlog
--;
384 sigd_enq2(NULL
, as_reject
, old_vcc
, NULL
, NULL
,
385 &old_vcc
->qos
, error
);
386 error
= error
== -EAGAIN
? -EBUSY
: error
;
389 /* wait should be short, so we ignore the non-blocking flag */
390 set_bit(ATM_VF_WAITING
, &new_vcc
->flags
);
391 sigd_enq(new_vcc
, as_accept
, old_vcc
, NULL
, NULL
);
393 prepare_to_wait(sk_sleep(sk_atm(new_vcc
)), &wait
,
394 TASK_UNINTERRUPTIBLE
);
395 if (!test_bit(ATM_VF_WAITING
, &new_vcc
->flags
) || !sigd
)
401 finish_wait(sk_sleep(sk_atm(new_vcc
)), &wait
);
406 if (!sk_atm(new_vcc
)->sk_err
)
408 if (sk_atm(new_vcc
)->sk_err
!= ERESTARTSYS
) {
409 error
= -sk_atm(new_vcc
)->sk_err
;
413 newsock
->state
= SS_CONNECTED
;
419 static int svc_getname(struct socket
*sock
, struct sockaddr
*sockaddr
,
420 int *sockaddr_len
, int peer
)
422 struct sockaddr_atmsvc
*addr
;
424 *sockaddr_len
= sizeof(struct sockaddr_atmsvc
);
425 addr
= (struct sockaddr_atmsvc
*) sockaddr
;
426 memcpy(addr
, peer
? &ATM_SD(sock
)->remote
: &ATM_SD(sock
)->local
,
427 sizeof(struct sockaddr_atmsvc
));
431 int svc_change_qos(struct atm_vcc
*vcc
, struct atm_qos
*qos
)
433 struct sock
*sk
= sk_atm(vcc
);
436 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
437 sigd_enq2(vcc
, as_modify
, NULL
, NULL
, &vcc
->local
, qos
, 0);
439 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_UNINTERRUPTIBLE
);
440 if (!test_bit(ATM_VF_WAITING
, &vcc
->flags
) ||
441 test_bit(ATM_VF_RELEASED
, &vcc
->flags
) || !sigd
) {
446 finish_wait(sk_sleep(sk
), &wait
);
452 static int svc_setsockopt(struct socket
*sock
, int level
, int optname
,
453 char __user
*optval
, unsigned int optlen
)
455 struct sock
*sk
= sock
->sk
;
456 struct atm_vcc
*vcc
= ATM_SD(sock
);
457 int value
, error
= 0;
462 if (level
!= SOL_ATM
|| optlen
!= sizeof(struct atm_sap
)) {
466 if (copy_from_user(&vcc
->sap
, optval
, optlen
)) {
470 set_bit(ATM_VF_HASSAP
, &vcc
->flags
);
473 if (level
!= SOL_ATM
|| optlen
!= sizeof(int)) {
477 if (get_user(value
, (int __user
*)optval
)) {
482 set_bit(ATM_VF_SESSION
, &vcc
->flags
);
484 clear_bit(ATM_VF_SESSION
, &vcc
->flags
);
489 error
= vcc_setsockopt(sock
, level
, optname
, optval
, optlen
);
497 static int svc_getsockopt(struct socket
*sock
, int level
, int optname
,
498 char __user
*optval
, int __user
*optlen
)
500 struct sock
*sk
= sock
->sk
;
504 if (!__SO_LEVEL_MATCH(optname
, level
) || optname
!= SO_ATMSAP
) {
505 error
= vcc_getsockopt(sock
, level
, optname
, optval
, optlen
);
508 if (get_user(len
, optlen
)) {
512 if (len
!= sizeof(struct atm_sap
)) {
516 if (copy_to_user(optval
, &ATM_SD(sock
)->sap
, sizeof(struct atm_sap
))) {
525 static int svc_addparty(struct socket
*sock
, struct sockaddr
*sockaddr
,
526 int sockaddr_len
, int flags
)
529 struct sock
*sk
= sock
->sk
;
530 struct atm_vcc
*vcc
= ATM_SD(sock
);
534 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
535 sigd_enq(vcc
, as_addparty
, NULL
, NULL
,
536 (struct sockaddr_atmsvc
*) sockaddr
);
537 if (flags
& O_NONBLOCK
) {
538 error
= -EINPROGRESS
;
541 pr_debug("added wait queue\n");
543 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
544 if (!test_bit(ATM_VF_WAITING
, &vcc
->flags
) || !sigd
)
548 finish_wait(sk_sleep(sk
), &wait
);
549 error
= -xchg(&sk
->sk_err_soft
, 0);
555 static int svc_dropparty(struct socket
*sock
, int ep_ref
)
558 struct sock
*sk
= sock
->sk
;
559 struct atm_vcc
*vcc
= ATM_SD(sock
);
563 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
564 sigd_enq2(vcc
, as_dropparty
, NULL
, NULL
, NULL
, NULL
, ep_ref
);
566 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
567 if (!test_bit(ATM_VF_WAITING
, &vcc
->flags
) || !sigd
)
571 finish_wait(sk_sleep(sk
), &wait
);
576 error
= -xchg(&sk
->sk_err_soft
, 0);
582 static int svc_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
585 struct sockaddr_atmsvc sa
;
586 struct atm_vcc
*vcc
= ATM_SD(sock
);
590 if (!test_bit(ATM_VF_SESSION
, &vcc
->flags
))
592 if (copy_from_user(&sa
, (void __user
*) arg
, sizeof(sa
)))
594 error
= svc_addparty(sock
, (struct sockaddr
*)&sa
, sizeof(sa
),
598 if (!test_bit(ATM_VF_SESSION
, &vcc
->flags
))
600 if (copy_from_user(&ep_ref
, (void __user
*) arg
, sizeof(int)))
602 error
= svc_dropparty(sock
, ep_ref
);
605 error
= vcc_ioctl(sock
, cmd
, arg
);
612 static int svc_compat_ioctl(struct socket
*sock
, unsigned int cmd
,
615 /* The definition of ATM_ADDPARTY uses the size of struct atm_iobuf.
616 But actually it takes a struct sockaddr_atmsvc, which doesn't need
617 compat handling. So all we have to do is fix up cmd... */
618 if (cmd
== COMPAT_ATM_ADDPARTY
)
621 if (cmd
== ATM_ADDPARTY
|| cmd
== ATM_DROPPARTY
)
622 return svc_ioctl(sock
, cmd
, arg
);
624 return vcc_compat_ioctl(sock
, cmd
, arg
);
626 #endif /* CONFIG_COMPAT */
628 static const struct proto_ops svc_proto_ops
= {
630 .owner
= THIS_MODULE
,
632 .release
= svc_release
,
634 .connect
= svc_connect
,
635 .socketpair
= sock_no_socketpair
,
636 .accept
= svc_accept
,
637 .getname
= svc_getname
,
641 .compat_ioctl
= svc_compat_ioctl
,
643 .listen
= svc_listen
,
644 .shutdown
= svc_shutdown
,
645 .setsockopt
= svc_setsockopt
,
646 .getsockopt
= svc_getsockopt
,
647 .sendmsg
= vcc_sendmsg
,
648 .recvmsg
= vcc_recvmsg
,
649 .mmap
= sock_no_mmap
,
650 .sendpage
= sock_no_sendpage
,
654 static int svc_create(struct net
*net
, struct socket
*sock
, int protocol
,
659 if (!net_eq(net
, &init_net
))
660 return -EAFNOSUPPORT
;
662 sock
->ops
= &svc_proto_ops
;
663 error
= vcc_create(net
, sock
, protocol
, AF_ATMSVC
, kern
);
666 ATM_SD(sock
)->local
.sas_family
= AF_ATMSVC
;
667 ATM_SD(sock
)->remote
.sas_family
= AF_ATMSVC
;
671 static const struct net_proto_family svc_family_ops
= {
673 .create
= svc_create
,
674 .owner
= THIS_MODULE
,
679 * Initialize the ATM SVC protocol family
682 int __init
atmsvc_init(void)
684 return sock_register(&svc_family_ops
);
687 void atmsvc_exit(void)
689 sock_unregister(PF_ATMSVC
);