1 // SPDX-License-Identifier: GPL-2.0
2 /* net/atm/svc.c - ATM SVC sockets */
4 /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
6 #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
8 #include <linux/string.h>
9 #include <linux/net.h> /* struct socket, struct proto_ops */
10 #include <linux/errno.h> /* error codes */
11 #include <linux/kernel.h> /* printk */
12 #include <linux/skbuff.h>
13 #include <linux/wait.h>
14 #include <linux/sched/signal.h>
15 #include <linux/fcntl.h> /* O_NONBLOCK */
16 #include <linux/init.h>
17 #include <linux/atm.h> /* ATM stuff */
18 #include <linux/atmsap.h>
19 #include <linux/atmsvc.h>
20 #include <linux/atmdev.h>
21 #include <linux/bitops.h>
22 #include <net/sock.h> /* for sock_no_* */
23 #include <linux/uaccess.h>
24 #include <linux/export.h>
26 #include "resources.h"
27 #include "common.h" /* common for PVCs and SVCs */
28 #include "signaling.h"
31 static int svc_create(struct net
*net
, struct socket
*sock
, int protocol
,
35 * Note: since all this is still nicely synchronized with the signaling demon,
36 * there's no need to protect sleep loops with clis. If signaling is
37 * moved into the kernel, that would change.
41 static int svc_shutdown(struct socket
*sock
, int how
)
46 static void svc_disconnect(struct atm_vcc
*vcc
)
50 struct sock
*sk
= sk_atm(vcc
);
52 pr_debug("%p\n", vcc
);
53 if (test_bit(ATM_VF_REGIS
, &vcc
->flags
)) {
54 sigd_enq(vcc
, as_close
, NULL
, NULL
, NULL
);
56 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_UNINTERRUPTIBLE
);
57 if (test_bit(ATM_VF_RELEASED
, &vcc
->flags
) || !sigd
)
61 finish_wait(sk_sleep(sk
), &wait
);
63 /* beware - socket is still in use by atmsigd until the last
64 as_indicate has been answered */
65 while ((skb
= skb_dequeue(&sk
->sk_receive_queue
)) != NULL
) {
66 atm_return(vcc
, skb
->truesize
);
67 pr_debug("LISTEN REL\n");
68 sigd_enq2(NULL
, as_reject
, vcc
, NULL
, NULL
, &vcc
->qos
, 0);
71 clear_bit(ATM_VF_REGIS
, &vcc
->flags
);
72 /* ... may retry later */
75 static int svc_release(struct socket
*sock
)
77 struct sock
*sk
= sock
->sk
;
82 pr_debug("%p\n", vcc
);
83 clear_bit(ATM_VF_READY
, &vcc
->flags
);
85 * VCC pointer is used as a reference,
86 * so we must not free it (thereby subjecting it to re-use)
87 * before all pending connections are closed
95 static int svc_bind(struct socket
*sock
, struct sockaddr
*sockaddr
,
99 struct sock
*sk
= sock
->sk
;
100 struct sockaddr_atmsvc
*addr
;
104 if (sockaddr_len
!= sizeof(struct sockaddr_atmsvc
))
107 if (sock
->state
== SS_CONNECTED
) {
111 if (sock
->state
!= SS_UNCONNECTED
) {
116 addr
= (struct sockaddr_atmsvc
*) sockaddr
;
117 if (addr
->sas_family
!= AF_ATMSVC
) {
118 error
= -EAFNOSUPPORT
;
121 clear_bit(ATM_VF_BOUND
, &vcc
->flags
);
122 /* failing rebind will kill old binding */
123 /* @@@ check memory (de)allocation on rebind */
124 if (!test_bit(ATM_VF_HASQOS
, &vcc
->flags
)) {
129 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
130 sigd_enq(vcc
, as_bind
, NULL
, NULL
, &vcc
->local
);
132 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_UNINTERRUPTIBLE
);
133 if (!test_bit(ATM_VF_WAITING
, &vcc
->flags
) || !sigd
)
137 finish_wait(sk_sleep(sk
), &wait
);
138 clear_bit(ATM_VF_REGIS
, &vcc
->flags
); /* doesn't count */
144 set_bit(ATM_VF_BOUND
, &vcc
->flags
);
151 static int svc_connect(struct socket
*sock
, struct sockaddr
*sockaddr
,
152 int sockaddr_len
, int flags
)
155 struct sock
*sk
= sock
->sk
;
156 struct sockaddr_atmsvc
*addr
;
157 struct atm_vcc
*vcc
= ATM_SD(sock
);
160 pr_debug("%p\n", vcc
);
162 if (sockaddr_len
!= sizeof(struct sockaddr_atmsvc
)) {
167 switch (sock
->state
) {
175 if (test_bit(ATM_VF_WAITING
, &vcc
->flags
)) {
179 sock
->state
= SS_UNCONNECTED
;
186 addr
= (struct sockaddr_atmsvc
*) sockaddr
;
187 if (addr
->sas_family
!= AF_ATMSVC
) {
188 error
= -EAFNOSUPPORT
;
191 if (!test_bit(ATM_VF_HASQOS
, &vcc
->flags
)) {
195 if (vcc
->qos
.txtp
.traffic_class
== ATM_ANYCLASS
||
196 vcc
->qos
.rxtp
.traffic_class
== ATM_ANYCLASS
) {
200 if (!vcc
->qos
.txtp
.traffic_class
&&
201 !vcc
->qos
.rxtp
.traffic_class
) {
206 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
207 sigd_enq(vcc
, as_connect
, NULL
, NULL
, &vcc
->remote
);
208 if (flags
& O_NONBLOCK
) {
209 sock
->state
= SS_CONNECTING
;
210 error
= -EINPROGRESS
;
214 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
215 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) && sigd
) {
217 if (!signal_pending(current
)) {
218 prepare_to_wait(sk_sleep(sk
), &wait
,
222 pr_debug("*ABORT*\n");
225 * Kernel ---close--> Demon
226 * Kernel <--close--- Demon
228 * Kernel ---close--> Demon
229 * Kernel <--error--- Demon
231 * Kernel ---close--> Demon
232 * Kernel <--okay---- Demon
233 * Kernel <--close--- Demon
235 sigd_enq(vcc
, as_close
, NULL
, NULL
, NULL
);
236 while (test_bit(ATM_VF_WAITING
, &vcc
->flags
) && sigd
) {
237 prepare_to_wait(sk_sleep(sk
), &wait
,
242 while (!test_bit(ATM_VF_RELEASED
, &vcc
->flags
) &&
244 prepare_to_wait(sk_sleep(sk
), &wait
,
248 clear_bit(ATM_VF_REGIS
, &vcc
->flags
);
249 clear_bit(ATM_VF_RELEASED
, &vcc
->flags
);
250 clear_bit(ATM_VF_CLOSE
, &vcc
->flags
);
251 /* we're gone now but may connect later */
255 finish_wait(sk_sleep(sk
), &wait
);
268 vcc
->qos
.txtp
.max_pcr
= SELECT_TOP_PCR(vcc
->qos
.txtp
);
269 vcc
->qos
.txtp
.pcr
= 0;
270 vcc
->qos
.txtp
.min_pcr
= 0;
272 error
= vcc_connect(sock
, vcc
->itf
, vcc
->vpi
, vcc
->vci
);
274 sock
->state
= SS_CONNECTED
;
276 (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("%p\n", vcc
);
291 /* let server handle listen on unbound sockets */
292 if (test_bit(ATM_VF_SESSION
, &vcc
->flags
)) {
296 if (test_bit(ATM_VF_LISTEN
, &vcc
->flags
)) {
300 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
301 sigd_enq(vcc
, as_listen
, NULL
, NULL
, &vcc
->local
);
303 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_UNINTERRUPTIBLE
);
304 if (!test_bit(ATM_VF_WAITING
, &vcc
->flags
) || !sigd
)
308 finish_wait(sk_sleep(sk
), &wait
);
313 set_bit(ATM_VF_LISTEN
, &vcc
->flags
);
314 vcc_insert_socket(sk
);
315 sk
->sk_max_ack_backlog
= backlog
> 0 ? backlog
: ATM_BACKLOG_DEFAULT
;
322 static int svc_accept(struct socket
*sock
, struct socket
*newsock
, int flags
,
325 struct sock
*sk
= sock
->sk
;
327 struct atmsvc_msg
*msg
;
328 struct atm_vcc
*old_vcc
= ATM_SD(sock
);
329 struct atm_vcc
*new_vcc
;
334 error
= svc_create(sock_net(sk
), newsock
, 0, kern
);
338 new_vcc
= ATM_SD(newsock
);
340 pr_debug("%p -> %p\n", old_vcc
, new_vcc
);
344 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
345 while (!(skb
= skb_dequeue(&sk
->sk_receive_queue
)) &&
347 if (test_bit(ATM_VF_RELEASED
, &old_vcc
->flags
))
349 if (test_bit(ATM_VF_CLOSE
, &old_vcc
->flags
)) {
353 if (flags
& O_NONBLOCK
) {
360 if (signal_pending(current
)) {
361 error
= -ERESTARTSYS
;
364 prepare_to_wait(sk_sleep(sk
), &wait
,
367 finish_wait(sk_sleep(sk
), &wait
);
374 msg
= (struct atmsvc_msg
*)skb
->data
;
375 new_vcc
->qos
= msg
->qos
;
376 set_bit(ATM_VF_HASQOS
, &new_vcc
->flags
);
377 new_vcc
->remote
= msg
->svc
;
378 new_vcc
->local
= msg
->local
;
379 new_vcc
->sap
= msg
->sap
;
380 error
= vcc_connect(newsock
, msg
->pvc
.sap_addr
.itf
,
381 msg
->pvc
.sap_addr
.vpi
,
382 msg
->pvc
.sap_addr
.vci
);
384 sk
->sk_ack_backlog
--;
386 sigd_enq2(NULL
, as_reject
, old_vcc
, NULL
, NULL
,
387 &old_vcc
->qos
, error
);
388 error
= error
== -EAGAIN
? -EBUSY
: error
;
391 /* wait should be short, so we ignore the non-blocking flag */
392 set_bit(ATM_VF_WAITING
, &new_vcc
->flags
);
393 sigd_enq(new_vcc
, as_accept
, old_vcc
, NULL
, NULL
);
395 prepare_to_wait(sk_sleep(sk_atm(new_vcc
)), &wait
,
396 TASK_UNINTERRUPTIBLE
);
397 if (!test_bit(ATM_VF_WAITING
, &new_vcc
->flags
) || !sigd
)
403 finish_wait(sk_sleep(sk_atm(new_vcc
)), &wait
);
408 if (!sk_atm(new_vcc
)->sk_err
)
410 if (sk_atm(new_vcc
)->sk_err
!= ERESTARTSYS
) {
411 error
= -sk_atm(new_vcc
)->sk_err
;
415 newsock
->state
= SS_CONNECTED
;
421 static int svc_getname(struct socket
*sock
, struct sockaddr
*sockaddr
,
424 struct sockaddr_atmsvc
*addr
;
426 addr
= (struct sockaddr_atmsvc
*) sockaddr
;
427 memcpy(addr
, peer
? &ATM_SD(sock
)->remote
: &ATM_SD(sock
)->local
,
428 sizeof(struct sockaddr_atmsvc
));
429 return sizeof(struct sockaddr_atmsvc
);
432 int svc_change_qos(struct atm_vcc
*vcc
, struct atm_qos
*qos
)
434 struct sock
*sk
= sk_atm(vcc
);
437 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
438 sigd_enq2(vcc
, as_modify
, NULL
, NULL
, &vcc
->local
, qos
, 0);
440 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_UNINTERRUPTIBLE
);
441 if (!test_bit(ATM_VF_WAITING
, &vcc
->flags
) ||
442 test_bit(ATM_VF_RELEASED
, &vcc
->flags
) || !sigd
) {
447 finish_wait(sk_sleep(sk
), &wait
);
453 static int svc_setsockopt(struct socket
*sock
, int level
, int optname
,
454 char __user
*optval
, unsigned 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
);
485 clear_bit(ATM_VF_SESSION
, &vcc
->flags
);
490 error
= vcc_setsockopt(sock
, level
, optname
, optval
, optlen
);
498 static int svc_getsockopt(struct socket
*sock
, int level
, int optname
,
499 char __user
*optval
, int __user
*optlen
)
501 struct sock
*sk
= sock
->sk
;
505 if (!__SO_LEVEL_MATCH(optname
, level
) || optname
!= SO_ATMSAP
) {
506 error
= vcc_getsockopt(sock
, level
, optname
, optval
, optlen
);
509 if (get_user(len
, optlen
)) {
513 if (len
!= sizeof(struct atm_sap
)) {
517 if (copy_to_user(optval
, &ATM_SD(sock
)->sap
, sizeof(struct atm_sap
))) {
526 static int svc_addparty(struct socket
*sock
, struct sockaddr
*sockaddr
,
527 int sockaddr_len
, int flags
)
530 struct sock
*sk
= sock
->sk
;
531 struct atm_vcc
*vcc
= ATM_SD(sock
);
535 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
536 sigd_enq(vcc
, as_addparty
, NULL
, NULL
,
537 (struct sockaddr_atmsvc
*) sockaddr
);
538 if (flags
& O_NONBLOCK
) {
539 error
= -EINPROGRESS
;
542 pr_debug("added wait queue\n");
544 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
545 if (!test_bit(ATM_VF_WAITING
, &vcc
->flags
) || !sigd
)
549 finish_wait(sk_sleep(sk
), &wait
);
550 error
= -xchg(&sk
->sk_err_soft
, 0);
556 static int svc_dropparty(struct socket
*sock
, int ep_ref
)
559 struct sock
*sk
= sock
->sk
;
560 struct atm_vcc
*vcc
= ATM_SD(sock
);
564 set_bit(ATM_VF_WAITING
, &vcc
->flags
);
565 sigd_enq2(vcc
, as_dropparty
, NULL
, NULL
, NULL
, NULL
, ep_ref
);
567 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
568 if (!test_bit(ATM_VF_WAITING
, &vcc
->flags
) || !sigd
)
572 finish_wait(sk_sleep(sk
), &wait
);
577 error
= -xchg(&sk
->sk_err_soft
, 0);
583 static int svc_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
586 struct sockaddr_atmsvc sa
;
587 struct atm_vcc
*vcc
= ATM_SD(sock
);
591 if (!test_bit(ATM_VF_SESSION
, &vcc
->flags
))
593 if (copy_from_user(&sa
, (void __user
*) arg
, sizeof(sa
)))
595 error
= svc_addparty(sock
, (struct sockaddr
*)&sa
, sizeof(sa
),
599 if (!test_bit(ATM_VF_SESSION
, &vcc
->flags
))
601 if (copy_from_user(&ep_ref
, (void __user
*) arg
, sizeof(int)))
603 error
= svc_dropparty(sock
, ep_ref
);
606 error
= vcc_ioctl(sock
, cmd
, arg
);
613 static int svc_compat_ioctl(struct socket
*sock
, unsigned int cmd
,
616 /* The definition of ATM_ADDPARTY uses the size of struct atm_iobuf.
617 But actually it takes a struct sockaddr_atmsvc, which doesn't need
618 compat handling. So all we have to do is fix up cmd... */
619 if (cmd
== COMPAT_ATM_ADDPARTY
)
622 if (cmd
== ATM_ADDPARTY
|| cmd
== ATM_DROPPARTY
)
623 return svc_ioctl(sock
, cmd
, arg
);
625 return vcc_compat_ioctl(sock
, cmd
, arg
);
627 #endif /* CONFIG_COMPAT */
629 static const struct proto_ops svc_proto_ops
= {
631 .owner
= THIS_MODULE
,
633 .release
= svc_release
,
635 .connect
= svc_connect
,
636 .socketpair
= sock_no_socketpair
,
637 .accept
= svc_accept
,
638 .getname
= svc_getname
,
642 .compat_ioctl
= svc_compat_ioctl
,
644 .listen
= svc_listen
,
645 .shutdown
= svc_shutdown
,
646 .setsockopt
= svc_setsockopt
,
647 .getsockopt
= svc_getsockopt
,
648 .sendmsg
= vcc_sendmsg
,
649 .recvmsg
= vcc_recvmsg
,
650 .mmap
= sock_no_mmap
,
651 .sendpage
= sock_no_sendpage
,
655 static int svc_create(struct net
*net
, struct socket
*sock
, int protocol
,
660 if (!net_eq(net
, &init_net
))
661 return -EAFNOSUPPORT
;
663 sock
->ops
= &svc_proto_ops
;
664 error
= vcc_create(net
, sock
, protocol
, AF_ATMSVC
, kern
);
667 ATM_SD(sock
)->local
.sas_family
= AF_ATMSVC
;
668 ATM_SD(sock
)->remote
.sas_family
= AF_ATMSVC
;
672 static const struct net_proto_family svc_family_ops
= {
674 .create
= svc_create
,
675 .owner
= THIS_MODULE
,
680 * Initialize the ATM SVC protocol family
683 int __init
atmsvc_init(void)
685 return sock_register(&svc_family_ops
);
688 void atmsvc_exit(void)
690 sock_unregister(PF_ATMSVC
);