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"
32 /* It actually takes struct sockaddr_atmsvc, not struct atm_iobuf */
33 #define COMPAT_ATM_ADDPARTY _IOW('a', ATMIOC_SPECIAL + 4, struct compat_atm_iobuf)
36 static int svc_create(struct net *net, 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)
51 static void svc_disconnect(struct atm_vcc *vcc)
55 struct sock *sk = sk_atm(vcc);
57 pr_debug("%p\n", vcc);
58 if (test_bit(ATM_VF_REGIS, &vcc->flags)) {
59 sigd_enq(vcc, as_close, NULL, NULL, NULL);
61 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
62 if (test_bit(ATM_VF_RELEASED, &vcc->flags) || !sigd)
66 finish_wait(sk_sleep(sk), &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 pr_debug("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 */
80 static int svc_release(struct socket *sock)
82 struct sock *sk = sock->sk;
87 pr_debug("%p\n", vcc);
88 clear_bit(ATM_VF_READY, &vcc->flags);
90 * VCC pointer is used as a reference,
91 * so we must not free it (thereby subjecting it to re-use)
92 * before all pending connections are closed
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 sigd_enq(vcc, as_bind, NULL, NULL, &vcc->local);
137 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
138 if (!test_bit(ATM_VF_WAITING, &vcc->flags) || !sigd)
142 finish_wait(sk_sleep(sk), &wait);
143 clear_bit(ATM_VF_REGIS, &vcc->flags); /* doesn't count */
149 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 pr_debug("%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 sigd_enq(vcc, as_connect, NULL, NULL, &vcc->remote);
213 if (flags & O_NONBLOCK) {
214 sock->state = SS_CONNECTING;
215 error = -EINPROGRESS;
219 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
220 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
222 if (!signal_pending(current)) {
223 prepare_to_wait(sk_sleep(sk), &wait,
227 pr_debug("*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_sleep(sk), &wait,
247 while (!test_bit(ATM_VF_RELEASED, &vcc->flags) &&
249 prepare_to_wait(sk_sleep(sk), &wait,
253 clear_bit(ATM_VF_REGIS, &vcc->flags);
254 clear_bit(ATM_VF_RELEASED, &vcc->flags);
255 clear_bit(ATM_VF_CLOSE, &vcc->flags);
256 /* we're gone now but may connect later */
260 finish_wait(sk_sleep(sk), &wait);
273 vcc->qos.txtp.max_pcr = SELECT_TOP_PCR(vcc->qos.txtp);
274 vcc->qos.txtp.pcr = 0;
275 vcc->qos.txtp.min_pcr = 0;
277 error = vcc_connect(sock, vcc->itf, vcc->vpi, vcc->vci);
279 sock->state = SS_CONNECTED;
281 (void)svc_disconnect(vcc);
287 static int svc_listen(struct socket *sock, int backlog)
290 struct sock *sk = sock->sk;
291 struct atm_vcc *vcc = ATM_SD(sock);
294 pr_debug("%p\n", vcc);
296 /* let server handle listen on unbound sockets */
297 if (test_bit(ATM_VF_SESSION, &vcc->flags)) {
301 if (test_bit(ATM_VF_LISTEN, &vcc->flags)) {
305 set_bit(ATM_VF_WAITING, &vcc->flags);
306 sigd_enq(vcc, as_listen, NULL, NULL, &vcc->local);
308 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
309 if (!test_bit(ATM_VF_WAITING, &vcc->flags) || !sigd)
313 finish_wait(sk_sleep(sk), &wait);
318 set_bit(ATM_VF_LISTEN, &vcc->flags);
319 vcc_insert_socket(sk);
320 sk->sk_max_ack_backlog = backlog > 0 ? backlog : ATM_BACKLOG_DEFAULT;
327 static int svc_accept(struct socket *sock, struct socket *newsock, int flags,
330 struct sock *sk = sock->sk;
332 struct atmsvc_msg *msg;
333 struct atm_vcc *old_vcc = ATM_SD(sock);
334 struct atm_vcc *new_vcc;
339 error = svc_create(sock_net(sk), newsock, 0, kern);
343 new_vcc = ATM_SD(newsock);
345 pr_debug("%p -> %p\n", old_vcc, new_vcc);
349 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
350 while (!(skb = skb_dequeue(&sk->sk_receive_queue)) &&
352 if (test_bit(ATM_VF_RELEASED, &old_vcc->flags))
354 if (test_bit(ATM_VF_CLOSE, &old_vcc->flags)) {
358 if (flags & O_NONBLOCK) {
365 if (signal_pending(current)) {
366 error = -ERESTARTSYS;
369 prepare_to_wait(sk_sleep(sk), &wait,
372 finish_wait(sk_sleep(sk), &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,
387 msg->pvc.sap_addr.vci);
389 sk_acceptq_removed(sk);
391 sigd_enq2(NULL, as_reject, old_vcc, NULL, NULL,
392 &old_vcc->qos, error);
393 error = error == -EAGAIN ? -EBUSY : error;
396 /* wait should be short, so we ignore the non-blocking flag */
397 set_bit(ATM_VF_WAITING, &new_vcc->flags);
398 sigd_enq(new_vcc, as_accept, old_vcc, NULL, NULL);
400 prepare_to_wait(sk_sleep(sk_atm(new_vcc)), &wait,
401 TASK_UNINTERRUPTIBLE);
402 if (!test_bit(ATM_VF_WAITING, &new_vcc->flags) || !sigd)
408 finish_wait(sk_sleep(sk_atm(new_vcc)), &wait);
413 if (!sk_atm(new_vcc)->sk_err)
415 if (sk_atm(new_vcc)->sk_err != ERESTARTSYS) {
416 error = -sk_atm(new_vcc)->sk_err;
420 newsock->state = SS_CONNECTED;
426 static int svc_getname(struct socket *sock, struct sockaddr *sockaddr,
429 struct sockaddr_atmsvc *addr;
431 addr = (struct sockaddr_atmsvc *) sockaddr;
432 memcpy(addr, peer ? &ATM_SD(sock)->remote : &ATM_SD(sock)->local,
433 sizeof(struct sockaddr_atmsvc));
434 return 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 sigd_enq2(vcc, as_modify, NULL, NULL, &vcc->local, qos, 0);
445 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
446 if (!test_bit(ATM_VF_WAITING, &vcc->flags) ||
447 test_bit(ATM_VF_RELEASED, &vcc->flags) || !sigd) {
452 finish_wait(sk_sleep(sk), &wait);
458 static int svc_setsockopt(struct socket *sock, int level, int optname,
459 sockptr_t optval, unsigned int optlen)
461 struct sock *sk = sock->sk;
462 struct atm_vcc *vcc = ATM_SD(sock);
463 int value, error = 0;
468 if (level != SOL_ATM || optlen != sizeof(struct atm_sap)) {
472 if (copy_from_sockptr(&vcc->sap, optval, optlen)) {
476 set_bit(ATM_VF_HASSAP, &vcc->flags);
479 if (level != SOL_ATM || optlen != sizeof(int)) {
483 if (copy_from_sockptr(&value, optval, sizeof(int))) {
488 set_bit(ATM_VF_SESSION, &vcc->flags);
490 clear_bit(ATM_VF_SESSION, &vcc->flags);
495 error = vcc_setsockopt(sock, level, optname, optval, optlen);
503 static int svc_getsockopt(struct socket *sock, int level, int optname,
504 char __user *optval, int __user *optlen)
506 struct sock *sk = sock->sk;
510 if (!__SO_LEVEL_MATCH(optname, level) || optname != SO_ATMSAP) {
511 error = vcc_getsockopt(sock, level, optname, optval, optlen);
514 if (get_user(len, optlen)) {
518 if (len != sizeof(struct atm_sap)) {
522 if (copy_to_user(optval, &ATM_SD(sock)->sap, sizeof(struct atm_sap))) {
531 static int svc_addparty(struct socket *sock, struct sockaddr *sockaddr,
532 int sockaddr_len, int flags)
535 struct sock *sk = sock->sk;
536 struct atm_vcc *vcc = ATM_SD(sock);
540 set_bit(ATM_VF_WAITING, &vcc->flags);
541 sigd_enq(vcc, as_addparty, NULL, NULL,
542 (struct sockaddr_atmsvc *) sockaddr);
543 if (flags & O_NONBLOCK) {
544 error = -EINPROGRESS;
547 pr_debug("added wait queue\n");
549 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
550 if (!test_bit(ATM_VF_WAITING, &vcc->flags) || !sigd)
554 finish_wait(sk_sleep(sk), &wait);
555 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 sigd_enq2(vcc, as_dropparty, NULL, NULL, NULL, NULL, ep_ref);
572 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
573 if (!test_bit(ATM_VF_WAITING, &vcc->flags) || !sigd)
577 finish_wait(sk_sleep(sk), &wait);
582 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),
604 if (!test_bit(ATM_VF_SESSION, &vcc->flags))
606 if (copy_from_user(&ep_ref, (void __user *) arg, sizeof(int)))
608 error = svc_dropparty(sock, ep_ref);
611 error = vcc_ioctl(sock, cmd, arg);
618 static int svc_compat_ioctl(struct socket *sock, unsigned int cmd,
621 /* The definition of ATM_ADDPARTY uses the size of struct atm_iobuf.
622 But actually it takes a struct sockaddr_atmsvc, which doesn't need
623 compat handling. So all we have to do is fix up cmd... */
624 if (cmd == COMPAT_ATM_ADDPARTY)
627 if (cmd == ATM_ADDPARTY || cmd == ATM_DROPPARTY)
628 return svc_ioctl(sock, cmd, arg);
630 return vcc_compat_ioctl(sock, cmd, arg);
632 #endif /* CONFIG_COMPAT */
634 static const struct proto_ops svc_proto_ops = {
636 .owner = THIS_MODULE,
638 .release = svc_release,
640 .connect = svc_connect,
641 .socketpair = sock_no_socketpair,
642 .accept = svc_accept,
643 .getname = svc_getname,
647 .compat_ioctl = svc_compat_ioctl,
649 .gettstamp = sock_gettstamp,
650 .listen = svc_listen,
651 .shutdown = svc_shutdown,
652 .setsockopt = svc_setsockopt,
653 .getsockopt = svc_getsockopt,
654 .sendmsg = vcc_sendmsg,
655 .recvmsg = vcc_recvmsg,
656 .mmap = sock_no_mmap,
660 static int svc_create(struct net *net, struct socket *sock, int protocol,
665 if (!net_eq(net, &init_net))
666 return -EAFNOSUPPORT;
668 sock->ops = &svc_proto_ops;
669 error = vcc_create(net, sock, protocol, AF_ATMSVC, kern);
672 ATM_SD(sock)->local.sas_family = AF_ATMSVC;
673 ATM_SD(sock)->remote.sas_family = AF_ATMSVC;
677 static const struct net_proto_family svc_family_ops = {
679 .create = svc_create,
680 .owner = THIS_MODULE,
685 * Initialize the ATM SVC protocol family
688 int __init atmsvc_init(void)
690 return sock_register(&svc_family_ops);
693 void atmsvc_exit(void)
695 sock_unregister(PF_ATMSVC);