Loading...
1/* net/atm/svc.c - ATM SVC sockets */
2
3/* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
4
5#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
6
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.h> /* jiffies and HZ */
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>
24
25#include "resources.h"
26#include "common.h" /* common for PVCs and SVCs */
27#include "signaling.h"
28#include "addr.h"
29
30static int svc_create(struct net *net, struct socket *sock, int protocol,
31 int kern);
32
33/*
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.
37 */
38
39
40static int svc_shutdown(struct socket *sock, int how)
41{
42 return 0;
43}
44
45static void svc_disconnect(struct atm_vcc *vcc)
46{
47 DEFINE_WAIT(wait);
48 struct sk_buff *skb;
49 struct sock *sk = sk_atm(vcc);
50
51 pr_debug("%p\n", vcc);
52 if (test_bit(ATM_VF_REGIS, &vcc->flags)) {
53 sigd_enq(vcc, as_close, NULL, NULL, NULL);
54 for (;;) {
55 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
56 if (test_bit(ATM_VF_RELEASED, &vcc->flags) || !sigd)
57 break;
58 schedule();
59 }
60 finish_wait(sk_sleep(sk), &wait);
61 }
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);
68 dev_kfree_skb(skb);
69 }
70 clear_bit(ATM_VF_REGIS, &vcc->flags);
71 /* ... may retry later */
72}
73
74static int svc_release(struct socket *sock)
75{
76 struct sock *sk = sock->sk;
77 struct atm_vcc *vcc;
78
79 if (sk) {
80 vcc = ATM_SD(sock);
81 pr_debug("%p\n", vcc);
82 clear_bit(ATM_VF_READY, &vcc->flags);
83 /*
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
87 */
88 svc_disconnect(vcc);
89 vcc_release(sock);
90 }
91 return 0;
92}
93
94static int svc_bind(struct socket *sock, struct sockaddr *sockaddr,
95 int sockaddr_len)
96{
97 DEFINE_WAIT(wait);
98 struct sock *sk = sock->sk;
99 struct sockaddr_atmsvc *addr;
100 struct atm_vcc *vcc;
101 int error;
102
103 if (sockaddr_len != sizeof(struct sockaddr_atmsvc))
104 return -EINVAL;
105 lock_sock(sk);
106 if (sock->state == SS_CONNECTED) {
107 error = -EISCONN;
108 goto out;
109 }
110 if (sock->state != SS_UNCONNECTED) {
111 error = -EINVAL;
112 goto out;
113 }
114 vcc = ATM_SD(sock);
115 addr = (struct sockaddr_atmsvc *) sockaddr;
116 if (addr->sas_family != AF_ATMSVC) {
117 error = -EAFNOSUPPORT;
118 goto out;
119 }
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)) {
124 error = -EBADFD;
125 goto out;
126 }
127 vcc->local = *addr;
128 set_bit(ATM_VF_WAITING, &vcc->flags);
129 sigd_enq(vcc, as_bind, NULL, NULL, &vcc->local);
130 for (;;) {
131 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
132 if (!test_bit(ATM_VF_WAITING, &vcc->flags) || !sigd)
133 break;
134 schedule();
135 }
136 finish_wait(sk_sleep(sk), &wait);
137 clear_bit(ATM_VF_REGIS, &vcc->flags); /* doesn't count */
138 if (!sigd) {
139 error = -EUNATCH;
140 goto out;
141 }
142 if (!sk->sk_err)
143 set_bit(ATM_VF_BOUND, &vcc->flags);
144 error = -sk->sk_err;
145out:
146 release_sock(sk);
147 return error;
148}
149
150static int svc_connect(struct socket *sock, struct sockaddr *sockaddr,
151 int sockaddr_len, int flags)
152{
153 DEFINE_WAIT(wait);
154 struct sock *sk = sock->sk;
155 struct sockaddr_atmsvc *addr;
156 struct atm_vcc *vcc = ATM_SD(sock);
157 int error;
158
159 pr_debug("%p\n", vcc);
160 lock_sock(sk);
161 if (sockaddr_len != sizeof(struct sockaddr_atmsvc)) {
162 error = -EINVAL;
163 goto out;
164 }
165
166 switch (sock->state) {
167 default:
168 error = -EINVAL;
169 goto out;
170 case SS_CONNECTED:
171 error = -EISCONN;
172 goto out;
173 case SS_CONNECTING:
174 if (test_bit(ATM_VF_WAITING, &vcc->flags)) {
175 error = -EALREADY;
176 goto out;
177 }
178 sock->state = SS_UNCONNECTED;
179 if (sk->sk_err) {
180 error = -sk->sk_err;
181 goto out;
182 }
183 break;
184 case SS_UNCONNECTED:
185 addr = (struct sockaddr_atmsvc *) sockaddr;
186 if (addr->sas_family != AF_ATMSVC) {
187 error = -EAFNOSUPPORT;
188 goto out;
189 }
190 if (!test_bit(ATM_VF_HASQOS, &vcc->flags)) {
191 error = -EBADFD;
192 goto out;
193 }
194 if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
195 vcc->qos.rxtp.traffic_class == ATM_ANYCLASS) {
196 error = -EINVAL;
197 goto out;
198 }
199 if (!vcc->qos.txtp.traffic_class &&
200 !vcc->qos.rxtp.traffic_class) {
201 error = -EINVAL;
202 goto out;
203 }
204 vcc->remote = *addr;
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;
210 goto out;
211 }
212 error = 0;
213 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
214 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
215 schedule();
216 if (!signal_pending(current)) {
217 prepare_to_wait(sk_sleep(sk), &wait,
218 TASK_INTERRUPTIBLE);
219 continue;
220 }
221 pr_debug("*ABORT*\n");
222 /*
223 * This is tricky:
224 * Kernel ---close--> Demon
225 * Kernel <--close--- Demon
226 * or
227 * Kernel ---close--> Demon
228 * Kernel <--error--- Demon
229 * or
230 * Kernel ---close--> Demon
231 * Kernel <--okay---- Demon
232 * Kernel <--close--- Demon
233 */
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,
237 TASK_INTERRUPTIBLE);
238 schedule();
239 }
240 if (!sk->sk_err)
241 while (!test_bit(ATM_VF_RELEASED, &vcc->flags) &&
242 sigd) {
243 prepare_to_wait(sk_sleep(sk), &wait,
244 TASK_INTERRUPTIBLE);
245 schedule();
246 }
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 */
251 error = -EINTR;
252 break;
253 }
254 finish_wait(sk_sleep(sk), &wait);
255 if (error)
256 goto out;
257 if (!sigd) {
258 error = -EUNATCH;
259 goto out;
260 }
261 if (sk->sk_err) {
262 error = -sk->sk_err;
263 goto out;
264 }
265 }
266
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;
270
271 error = vcc_connect(sock, vcc->itf, vcc->vpi, vcc->vci);
272 if (!error)
273 sock->state = SS_CONNECTED;
274 else
275 (void)svc_disconnect(vcc);
276out:
277 release_sock(sk);
278 return error;
279}
280
281static int svc_listen(struct socket *sock, int backlog)
282{
283 DEFINE_WAIT(wait);
284 struct sock *sk = sock->sk;
285 struct atm_vcc *vcc = ATM_SD(sock);
286 int error;
287
288 pr_debug("%p\n", vcc);
289 lock_sock(sk);
290 /* let server handle listen on unbound sockets */
291 if (test_bit(ATM_VF_SESSION, &vcc->flags)) {
292 error = -EINVAL;
293 goto out;
294 }
295 if (test_bit(ATM_VF_LISTEN, &vcc->flags)) {
296 error = -EADDRINUSE;
297 goto out;
298 }
299 set_bit(ATM_VF_WAITING, &vcc->flags);
300 sigd_enq(vcc, as_listen, NULL, NULL, &vcc->local);
301 for (;;) {
302 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
303 if (!test_bit(ATM_VF_WAITING, &vcc->flags) || !sigd)
304 break;
305 schedule();
306 }
307 finish_wait(sk_sleep(sk), &wait);
308 if (!sigd) {
309 error = -EUNATCH;
310 goto out;
311 }
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;
315 error = -sk->sk_err;
316out:
317 release_sock(sk);
318 return error;
319}
320
321static int svc_accept(struct socket *sock, struct socket *newsock, int flags)
322{
323 struct sock *sk = sock->sk;
324 struct sk_buff *skb;
325 struct atmsvc_msg *msg;
326 struct atm_vcc *old_vcc = ATM_SD(sock);
327 struct atm_vcc *new_vcc;
328 int error;
329
330 lock_sock(sk);
331
332 error = svc_create(sock_net(sk), newsock, 0, 0);
333 if (error)
334 goto out;
335
336 new_vcc = ATM_SD(newsock);
337
338 pr_debug("%p -> %p\n", old_vcc, new_vcc);
339 while (1) {
340 DEFINE_WAIT(wait);
341
342 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
343 while (!(skb = skb_dequeue(&sk->sk_receive_queue)) &&
344 sigd) {
345 if (test_bit(ATM_VF_RELEASED, &old_vcc->flags))
346 break;
347 if (test_bit(ATM_VF_CLOSE, &old_vcc->flags)) {
348 error = -sk->sk_err;
349 break;
350 }
351 if (flags & O_NONBLOCK) {
352 error = -EAGAIN;
353 break;
354 }
355 release_sock(sk);
356 schedule();
357 lock_sock(sk);
358 if (signal_pending(current)) {
359 error = -ERESTARTSYS;
360 break;
361 }
362 prepare_to_wait(sk_sleep(sk), &wait,
363 TASK_INTERRUPTIBLE);
364 }
365 finish_wait(sk_sleep(sk), &wait);
366 if (error)
367 goto out;
368 if (!skb) {
369 error = -EUNATCH;
370 goto out;
371 }
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);
381 dev_kfree_skb(skb);
382 sk->sk_ack_backlog--;
383 if (error) {
384 sigd_enq2(NULL, as_reject, old_vcc, NULL, NULL,
385 &old_vcc->qos, error);
386 error = error == -EAGAIN ? -EBUSY : error;
387 goto out;
388 }
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);
392 for (;;) {
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)
396 break;
397 release_sock(sk);
398 schedule();
399 lock_sock(sk);
400 }
401 finish_wait(sk_sleep(sk_atm(new_vcc)), &wait);
402 if (!sigd) {
403 error = -EUNATCH;
404 goto out;
405 }
406 if (!sk_atm(new_vcc)->sk_err)
407 break;
408 if (sk_atm(new_vcc)->sk_err != ERESTARTSYS) {
409 error = -sk_atm(new_vcc)->sk_err;
410 goto out;
411 }
412 }
413 newsock->state = SS_CONNECTED;
414out:
415 release_sock(sk);
416 return error;
417}
418
419static int svc_getname(struct socket *sock, struct sockaddr *sockaddr,
420 int *sockaddr_len, int peer)
421{
422 struct sockaddr_atmsvc *addr;
423
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));
428 return 0;
429}
430
431int svc_change_qos(struct atm_vcc *vcc, struct atm_qos *qos)
432{
433 struct sock *sk = sk_atm(vcc);
434 DEFINE_WAIT(wait);
435
436 set_bit(ATM_VF_WAITING, &vcc->flags);
437 sigd_enq2(vcc, as_modify, NULL, NULL, &vcc->local, qos, 0);
438 for (;;) {
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) {
442 break;
443 }
444 schedule();
445 }
446 finish_wait(sk_sleep(sk), &wait);
447 if (!sigd)
448 return -EUNATCH;
449 return -sk->sk_err;
450}
451
452static int svc_setsockopt(struct socket *sock, int level, int optname,
453 char __user *optval, unsigned int optlen)
454{
455 struct sock *sk = sock->sk;
456 struct atm_vcc *vcc = ATM_SD(sock);
457 int value, error = 0;
458
459 lock_sock(sk);
460 switch (optname) {
461 case SO_ATMSAP:
462 if (level != SOL_ATM || optlen != sizeof(struct atm_sap)) {
463 error = -EINVAL;
464 goto out;
465 }
466 if (copy_from_user(&vcc->sap, optval, optlen)) {
467 error = -EFAULT;
468 goto out;
469 }
470 set_bit(ATM_VF_HASSAP, &vcc->flags);
471 break;
472 case SO_MULTIPOINT:
473 if (level != SOL_ATM || optlen != sizeof(int)) {
474 error = -EINVAL;
475 goto out;
476 }
477 if (get_user(value, (int __user *)optval)) {
478 error = -EFAULT;
479 goto out;
480 }
481 if (value == 1)
482 set_bit(ATM_VF_SESSION, &vcc->flags);
483 else if (value == 0)
484 clear_bit(ATM_VF_SESSION, &vcc->flags);
485 else
486 error = -EINVAL;
487 break;
488 default:
489 error = vcc_setsockopt(sock, level, optname, optval, optlen);
490 }
491
492out:
493 release_sock(sk);
494 return error;
495}
496
497static int svc_getsockopt(struct socket *sock, int level, int optname,
498 char __user *optval, int __user *optlen)
499{
500 struct sock *sk = sock->sk;
501 int error = 0, len;
502
503 lock_sock(sk);
504 if (!__SO_LEVEL_MATCH(optname, level) || optname != SO_ATMSAP) {
505 error = vcc_getsockopt(sock, level, optname, optval, optlen);
506 goto out;
507 }
508 if (get_user(len, optlen)) {
509 error = -EFAULT;
510 goto out;
511 }
512 if (len != sizeof(struct atm_sap)) {
513 error = -EINVAL;
514 goto out;
515 }
516 if (copy_to_user(optval, &ATM_SD(sock)->sap, sizeof(struct atm_sap))) {
517 error = -EFAULT;
518 goto out;
519 }
520out:
521 release_sock(sk);
522 return error;
523}
524
525static int svc_addparty(struct socket *sock, struct sockaddr *sockaddr,
526 int sockaddr_len, int flags)
527{
528 DEFINE_WAIT(wait);
529 struct sock *sk = sock->sk;
530 struct atm_vcc *vcc = ATM_SD(sock);
531 int error;
532
533 lock_sock(sk);
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;
539 goto out;
540 }
541 pr_debug("added wait queue\n");
542 for (;;) {
543 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
544 if (!test_bit(ATM_VF_WAITING, &vcc->flags) || !sigd)
545 break;
546 schedule();
547 }
548 finish_wait(sk_sleep(sk), &wait);
549 error = xchg(&sk->sk_err_soft, 0);
550out:
551 release_sock(sk);
552 return error;
553}
554
555static int svc_dropparty(struct socket *sock, int ep_ref)
556{
557 DEFINE_WAIT(wait);
558 struct sock *sk = sock->sk;
559 struct atm_vcc *vcc = ATM_SD(sock);
560 int error;
561
562 lock_sock(sk);
563 set_bit(ATM_VF_WAITING, &vcc->flags);
564 sigd_enq2(vcc, as_dropparty, NULL, NULL, NULL, NULL, ep_ref);
565 for (;;) {
566 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
567 if (!test_bit(ATM_VF_WAITING, &vcc->flags) || !sigd)
568 break;
569 schedule();
570 }
571 finish_wait(sk_sleep(sk), &wait);
572 if (!sigd) {
573 error = -EUNATCH;
574 goto out;
575 }
576 error = xchg(&sk->sk_err_soft, 0);
577out:
578 release_sock(sk);
579 return error;
580}
581
582static int svc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
583{
584 int error, ep_ref;
585 struct sockaddr_atmsvc sa;
586 struct atm_vcc *vcc = ATM_SD(sock);
587
588 switch (cmd) {
589 case ATM_ADDPARTY:
590 if (!test_bit(ATM_VF_SESSION, &vcc->flags))
591 return -EINVAL;
592 if (copy_from_user(&sa, (void __user *) arg, sizeof(sa)))
593 return -EFAULT;
594 error = svc_addparty(sock, (struct sockaddr *)&sa, sizeof(sa),
595 0);
596 break;
597 case ATM_DROPPARTY:
598 if (!test_bit(ATM_VF_SESSION, &vcc->flags))
599 return -EINVAL;
600 if (copy_from_user(&ep_ref, (void __user *) arg, sizeof(int)))
601 return -EFAULT;
602 error = svc_dropparty(sock, ep_ref);
603 break;
604 default:
605 error = vcc_ioctl(sock, cmd, arg);
606 }
607
608 return error;
609}
610
611#ifdef CONFIG_COMPAT
612static int svc_compat_ioctl(struct socket *sock, unsigned int cmd,
613 unsigned long arg)
614{
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)
619 cmd = ATM_ADDPARTY;
620
621 if (cmd == ATM_ADDPARTY || cmd == ATM_DROPPARTY)
622 return svc_ioctl(sock, cmd, arg);
623 else
624 return vcc_compat_ioctl(sock, cmd, arg);
625}
626#endif /* CONFIG_COMPAT */
627
628static const struct proto_ops svc_proto_ops = {
629 .family = PF_ATMSVC,
630 .owner = THIS_MODULE,
631
632 .release = svc_release,
633 .bind = svc_bind,
634 .connect = svc_connect,
635 .socketpair = sock_no_socketpair,
636 .accept = svc_accept,
637 .getname = svc_getname,
638 .poll = vcc_poll,
639 .ioctl = svc_ioctl,
640#ifdef CONFIG_COMPAT
641 .compat_ioctl = svc_compat_ioctl,
642#endif
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,
651};
652
653
654static int svc_create(struct net *net, struct socket *sock, int protocol,
655 int kern)
656{
657 int error;
658
659 if (!net_eq(net, &init_net))
660 return -EAFNOSUPPORT;
661
662 sock->ops = &svc_proto_ops;
663 error = vcc_create(net, sock, protocol, AF_ATMSVC, kern);
664 if (error)
665 return error;
666 ATM_SD(sock)->local.sas_family = AF_ATMSVC;
667 ATM_SD(sock)->remote.sas_family = AF_ATMSVC;
668 return 0;
669}
670
671static const struct net_proto_family svc_family_ops = {
672 .family = PF_ATMSVC,
673 .create = svc_create,
674 .owner = THIS_MODULE,
675};
676
677
678/*
679 * Initialize the ATM SVC protocol family
680 */
681
682int __init atmsvc_init(void)
683{
684 return sock_register(&svc_family_ops);
685}
686
687void atmsvc_exit(void)
688{
689 sock_unregister(PF_ATMSVC);
690}
1/* net/atm/svc.c - ATM SVC sockets */
2
3/* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
4
5#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
6
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.h> /* jiffies and HZ */
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>
24
25#include "resources.h"
26#include "common.h" /* common for PVCs and SVCs */
27#include "signaling.h"
28#include "addr.h"
29
30static int svc_create(struct net *net, struct socket *sock, int protocol,
31 int kern);
32
33/*
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.
37 */
38
39
40static int svc_shutdown(struct socket *sock, int how)
41{
42 return 0;
43}
44
45static void svc_disconnect(struct atm_vcc *vcc)
46{
47 DEFINE_WAIT(wait);
48 struct sk_buff *skb;
49 struct sock *sk = sk_atm(vcc);
50
51 pr_debug("%p\n", vcc);
52 if (test_bit(ATM_VF_REGIS, &vcc->flags)) {
53 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
54 sigd_enq(vcc, as_close, NULL, NULL, NULL);
55 while (!test_bit(ATM_VF_RELEASED, &vcc->flags) && sigd) {
56 schedule();
57 prepare_to_wait(sk_sleep(sk), &wait,
58 TASK_UNINTERRUPTIBLE);
59 }
60 finish_wait(sk_sleep(sk), &wait);
61 }
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);
68 dev_kfree_skb(skb);
69 }
70 clear_bit(ATM_VF_REGIS, &vcc->flags);
71 /* ... may retry later */
72}
73
74static int svc_release(struct socket *sock)
75{
76 struct sock *sk = sock->sk;
77 struct atm_vcc *vcc;
78
79 if (sk) {
80 vcc = ATM_SD(sock);
81 pr_debug("%p\n", vcc);
82 clear_bit(ATM_VF_READY, &vcc->flags);
83 /*
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
87 */
88 svc_disconnect(vcc);
89 vcc_release(sock);
90 }
91 return 0;
92}
93
94static int svc_bind(struct socket *sock, struct sockaddr *sockaddr,
95 int sockaddr_len)
96{
97 DEFINE_WAIT(wait);
98 struct sock *sk = sock->sk;
99 struct sockaddr_atmsvc *addr;
100 struct atm_vcc *vcc;
101 int error;
102
103 if (sockaddr_len != sizeof(struct sockaddr_atmsvc))
104 return -EINVAL;
105 lock_sock(sk);
106 if (sock->state == SS_CONNECTED) {
107 error = -EISCONN;
108 goto out;
109 }
110 if (sock->state != SS_UNCONNECTED) {
111 error = -EINVAL;
112 goto out;
113 }
114 vcc = ATM_SD(sock);
115 addr = (struct sockaddr_atmsvc *) sockaddr;
116 if (addr->sas_family != AF_ATMSVC) {
117 error = -EAFNOSUPPORT;
118 goto out;
119 }
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)) {
124 error = -EBADFD;
125 goto out;
126 }
127 vcc->local = *addr;
128 set_bit(ATM_VF_WAITING, &vcc->flags);
129 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
130 sigd_enq(vcc, as_bind, NULL, NULL, &vcc->local);
131 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
132 schedule();
133 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
134 }
135 finish_wait(sk_sleep(sk), &wait);
136 clear_bit(ATM_VF_REGIS, &vcc->flags); /* doesn't count */
137 if (!sigd) {
138 error = -EUNATCH;
139 goto out;
140 }
141 if (!sk->sk_err)
142 set_bit(ATM_VF_BOUND, &vcc->flags);
143 error = -sk->sk_err;
144out:
145 release_sock(sk);
146 return error;
147}
148
149static int svc_connect(struct socket *sock, struct sockaddr *sockaddr,
150 int sockaddr_len, int flags)
151{
152 DEFINE_WAIT(wait);
153 struct sock *sk = sock->sk;
154 struct sockaddr_atmsvc *addr;
155 struct atm_vcc *vcc = ATM_SD(sock);
156 int error;
157
158 pr_debug("%p\n", vcc);
159 lock_sock(sk);
160 if (sockaddr_len != sizeof(struct sockaddr_atmsvc)) {
161 error = -EINVAL;
162 goto out;
163 }
164
165 switch (sock->state) {
166 default:
167 error = -EINVAL;
168 goto out;
169 case SS_CONNECTED:
170 error = -EISCONN;
171 goto out;
172 case SS_CONNECTING:
173 if (test_bit(ATM_VF_WAITING, &vcc->flags)) {
174 error = -EALREADY;
175 goto out;
176 }
177 sock->state = SS_UNCONNECTED;
178 if (sk->sk_err) {
179 error = -sk->sk_err;
180 goto out;
181 }
182 break;
183 case SS_UNCONNECTED:
184 addr = (struct sockaddr_atmsvc *) sockaddr;
185 if (addr->sas_family != AF_ATMSVC) {
186 error = -EAFNOSUPPORT;
187 goto out;
188 }
189 if (!test_bit(ATM_VF_HASQOS, &vcc->flags)) {
190 error = -EBADFD;
191 goto out;
192 }
193 if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
194 vcc->qos.rxtp.traffic_class == ATM_ANYCLASS) {
195 error = -EINVAL;
196 goto out;
197 }
198 if (!vcc->qos.txtp.traffic_class &&
199 !vcc->qos.rxtp.traffic_class) {
200 error = -EINVAL;
201 goto out;
202 }
203 vcc->remote = *addr;
204 set_bit(ATM_VF_WAITING, &vcc->flags);
205 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
206 sigd_enq(vcc, as_connect, NULL, NULL, &vcc->remote);
207 if (flags & O_NONBLOCK) {
208 finish_wait(sk_sleep(sk), &wait);
209 sock->state = SS_CONNECTING;
210 error = -EINPROGRESS;
211 goto out;
212 }
213 error = 0;
214 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
215 schedule();
216 if (!signal_pending(current)) {
217 prepare_to_wait(sk_sleep(sk), &wait,
218 TASK_INTERRUPTIBLE);
219 continue;
220 }
221 pr_debug("*ABORT*\n");
222 /*
223 * This is tricky:
224 * Kernel ---close--> Demon
225 * Kernel <--close--- Demon
226 * or
227 * Kernel ---close--> Demon
228 * Kernel <--error--- Demon
229 * or
230 * Kernel ---close--> Demon
231 * Kernel <--okay---- Demon
232 * Kernel <--close--- Demon
233 */
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,
237 TASK_INTERRUPTIBLE);
238 schedule();
239 }
240 if (!sk->sk_err)
241 while (!test_bit(ATM_VF_RELEASED, &vcc->flags) &&
242 sigd) {
243 prepare_to_wait(sk_sleep(sk), &wait,
244 TASK_INTERRUPTIBLE);
245 schedule();
246 }
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 */
251 error = -EINTR;
252 break;
253 }
254 finish_wait(sk_sleep(sk), &wait);
255 if (error)
256 goto out;
257 if (!sigd) {
258 error = -EUNATCH;
259 goto out;
260 }
261 if (sk->sk_err) {
262 error = -sk->sk_err;
263 goto out;
264 }
265 }
266/*
267 * Not supported yet
268 *
269 * #ifndef CONFIG_SINGLE_SIGITF
270 */
271 vcc->qos.txtp.max_pcr = SELECT_TOP_PCR(vcc->qos.txtp);
272 vcc->qos.txtp.pcr = 0;
273 vcc->qos.txtp.min_pcr = 0;
274/*
275 * #endif
276 */
277 error = vcc_connect(sock, vcc->itf, vcc->vpi, vcc->vci);
278 if (!error)
279 sock->state = SS_CONNECTED;
280 else
281 (void)svc_disconnect(vcc);
282out:
283 release_sock(sk);
284 return error;
285}
286
287static int svc_listen(struct socket *sock, int backlog)
288{
289 DEFINE_WAIT(wait);
290 struct sock *sk = sock->sk;
291 struct atm_vcc *vcc = ATM_SD(sock);
292 int error;
293
294 pr_debug("%p\n", vcc);
295 lock_sock(sk);
296 /* let server handle listen on unbound sockets */
297 if (test_bit(ATM_VF_SESSION, &vcc->flags)) {
298 error = -EINVAL;
299 goto out;
300 }
301 if (test_bit(ATM_VF_LISTEN, &vcc->flags)) {
302 error = -EADDRINUSE;
303 goto out;
304 }
305 set_bit(ATM_VF_WAITING, &vcc->flags);
306 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
307 sigd_enq(vcc, as_listen, NULL, NULL, &vcc->local);
308 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
309 schedule();
310 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
311 }
312 finish_wait(sk_sleep(sk), &wait);
313 if (!sigd) {
314 error = -EUNATCH;
315 goto out;
316 }
317 set_bit(ATM_VF_LISTEN, &vcc->flags);
318 vcc_insert_socket(sk);
319 sk->sk_max_ack_backlog = backlog > 0 ? backlog : ATM_BACKLOG_DEFAULT;
320 error = -sk->sk_err;
321out:
322 release_sock(sk);
323 return error;
324}
325
326static int svc_accept(struct socket *sock, struct socket *newsock, int flags)
327{
328 struct sock *sk = sock->sk;
329 struct sk_buff *skb;
330 struct atmsvc_msg *msg;
331 struct atm_vcc *old_vcc = ATM_SD(sock);
332 struct atm_vcc *new_vcc;
333 int error;
334
335 lock_sock(sk);
336
337 error = svc_create(sock_net(sk), newsock, 0, 0);
338 if (error)
339 goto out;
340
341 new_vcc = ATM_SD(newsock);
342
343 pr_debug("%p -> %p\n", old_vcc, new_vcc);
344 while (1) {
345 DEFINE_WAIT(wait);
346
347 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
348 while (!(skb = skb_dequeue(&sk->sk_receive_queue)) &&
349 sigd) {
350 if (test_bit(ATM_VF_RELEASED, &old_vcc->flags))
351 break;
352 if (test_bit(ATM_VF_CLOSE, &old_vcc->flags)) {
353 error = -sk->sk_err;
354 break;
355 }
356 if (flags & O_NONBLOCK) {
357 error = -EAGAIN;
358 break;
359 }
360 release_sock(sk);
361 schedule();
362 lock_sock(sk);
363 if (signal_pending(current)) {
364 error = -ERESTARTSYS;
365 break;
366 }
367 prepare_to_wait(sk_sleep(sk), &wait,
368 TASK_INTERRUPTIBLE);
369 }
370 finish_wait(sk_sleep(sk), &wait);
371 if (error)
372 goto out;
373 if (!skb) {
374 error = -EUNATCH;
375 goto out;
376 }
377 msg = (struct atmsvc_msg *)skb->data;
378 new_vcc->qos = msg->qos;
379 set_bit(ATM_VF_HASQOS, &new_vcc->flags);
380 new_vcc->remote = msg->svc;
381 new_vcc->local = msg->local;
382 new_vcc->sap = msg->sap;
383 error = vcc_connect(newsock, msg->pvc.sap_addr.itf,
384 msg->pvc.sap_addr.vpi,
385 msg->pvc.sap_addr.vci);
386 dev_kfree_skb(skb);
387 sk->sk_ack_backlog--;
388 if (error) {
389 sigd_enq2(NULL, as_reject, old_vcc, NULL, NULL,
390 &old_vcc->qos, error);
391 error = error == -EAGAIN ? -EBUSY : error;
392 goto out;
393 }
394 /* wait should be short, so we ignore the non-blocking flag */
395 set_bit(ATM_VF_WAITING, &new_vcc->flags);
396 prepare_to_wait(sk_sleep(sk_atm(new_vcc)), &wait,
397 TASK_UNINTERRUPTIBLE);
398 sigd_enq(new_vcc, as_accept, old_vcc, NULL, NULL);
399 while (test_bit(ATM_VF_WAITING, &new_vcc->flags) && sigd) {
400 release_sock(sk);
401 schedule();
402 lock_sock(sk);
403 prepare_to_wait(sk_sleep(sk_atm(new_vcc)), &wait,
404 TASK_UNINTERRUPTIBLE);
405 }
406 finish_wait(sk_sleep(sk_atm(new_vcc)), &wait);
407 if (!sigd) {
408 error = -EUNATCH;
409 goto out;
410 }
411 if (!sk_atm(new_vcc)->sk_err)
412 break;
413 if (sk_atm(new_vcc)->sk_err != ERESTARTSYS) {
414 error = -sk_atm(new_vcc)->sk_err;
415 goto out;
416 }
417 }
418 newsock->state = SS_CONNECTED;
419out:
420 release_sock(sk);
421 return error;
422}
423
424static int svc_getname(struct socket *sock, struct sockaddr *sockaddr,
425 int *sockaddr_len, int peer)
426{
427 struct sockaddr_atmsvc *addr;
428
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));
433 return 0;
434}
435
436int svc_change_qos(struct atm_vcc *vcc, struct atm_qos *qos)
437{
438 struct sock *sk = sk_atm(vcc);
439 DEFINE_WAIT(wait);
440
441 set_bit(ATM_VF_WAITING, &vcc->flags);
442 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
443 sigd_enq2(vcc, as_modify, NULL, NULL, &vcc->local, qos, 0);
444 while (test_bit(ATM_VF_WAITING, &vcc->flags) &&
445 !test_bit(ATM_VF_RELEASED, &vcc->flags) && sigd) {
446 schedule();
447 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
448 }
449 finish_wait(sk_sleep(sk), &wait);
450 if (!sigd)
451 return -EUNATCH;
452 return -sk->sk_err;
453}
454
455static int svc_setsockopt(struct socket *sock, int level, int optname,
456 char __user *optval, unsigned int optlen)
457{
458 struct sock *sk = sock->sk;
459 struct atm_vcc *vcc = ATM_SD(sock);
460 int value, error = 0;
461
462 lock_sock(sk);
463 switch (optname) {
464 case SO_ATMSAP:
465 if (level != SOL_ATM || optlen != sizeof(struct atm_sap)) {
466 error = -EINVAL;
467 goto out;
468 }
469 if (copy_from_user(&vcc->sap, optval, optlen)) {
470 error = -EFAULT;
471 goto out;
472 }
473 set_bit(ATM_VF_HASSAP, &vcc->flags);
474 break;
475 case SO_MULTIPOINT:
476 if (level != SOL_ATM || optlen != sizeof(int)) {
477 error = -EINVAL;
478 goto out;
479 }
480 if (get_user(value, (int __user *)optval)) {
481 error = -EFAULT;
482 goto out;
483 }
484 if (value == 1)
485 set_bit(ATM_VF_SESSION, &vcc->flags);
486 else if (value == 0)
487 clear_bit(ATM_VF_SESSION, &vcc->flags);
488 else
489 error = -EINVAL;
490 break;
491 default:
492 error = vcc_setsockopt(sock, level, optname, optval, optlen);
493 }
494
495out:
496 release_sock(sk);
497 return error;
498}
499
500static int svc_getsockopt(struct socket *sock, int level, int optname,
501 char __user *optval, int __user *optlen)
502{
503 struct sock *sk = sock->sk;
504 int error = 0, len;
505
506 lock_sock(sk);
507 if (!__SO_LEVEL_MATCH(optname, level) || optname != SO_ATMSAP) {
508 error = vcc_getsockopt(sock, level, optname, optval, optlen);
509 goto out;
510 }
511 if (get_user(len, optlen)) {
512 error = -EFAULT;
513 goto out;
514 }
515 if (len != sizeof(struct atm_sap)) {
516 error = -EINVAL;
517 goto out;
518 }
519 if (copy_to_user(optval, &ATM_SD(sock)->sap, sizeof(struct atm_sap))) {
520 error = -EFAULT;
521 goto out;
522 }
523out:
524 release_sock(sk);
525 return error;
526}
527
528static int svc_addparty(struct socket *sock, struct sockaddr *sockaddr,
529 int sockaddr_len, int flags)
530{
531 DEFINE_WAIT(wait);
532 struct sock *sk = sock->sk;
533 struct atm_vcc *vcc = ATM_SD(sock);
534 int error;
535
536 lock_sock(sk);
537 set_bit(ATM_VF_WAITING, &vcc->flags);
538 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
539 sigd_enq(vcc, as_addparty, NULL, NULL,
540 (struct sockaddr_atmsvc *) sockaddr);
541 if (flags & O_NONBLOCK) {
542 finish_wait(sk_sleep(sk), &wait);
543 error = -EINPROGRESS;
544 goto out;
545 }
546 pr_debug("added wait queue\n");
547 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
548 schedule();
549 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
550 }
551 finish_wait(sk_sleep(sk), &wait);
552 error = xchg(&sk->sk_err_soft, 0);
553out:
554 release_sock(sk);
555 return error;
556}
557
558static int svc_dropparty(struct socket *sock, int ep_ref)
559{
560 DEFINE_WAIT(wait);
561 struct sock *sk = sock->sk;
562 struct atm_vcc *vcc = ATM_SD(sock);
563 int error;
564
565 lock_sock(sk);
566 set_bit(ATM_VF_WAITING, &vcc->flags);
567 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
568 sigd_enq2(vcc, as_dropparty, NULL, NULL, NULL, NULL, ep_ref);
569 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
570 schedule();
571 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
572 }
573 finish_wait(sk_sleep(sk), &wait);
574 if (!sigd) {
575 error = -EUNATCH;
576 goto out;
577 }
578 error = xchg(&sk->sk_err_soft, 0);
579out:
580 release_sock(sk);
581 return error;
582}
583
584static int svc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
585{
586 int error, ep_ref;
587 struct sockaddr_atmsvc sa;
588 struct atm_vcc *vcc = ATM_SD(sock);
589
590 switch (cmd) {
591 case ATM_ADDPARTY:
592 if (!test_bit(ATM_VF_SESSION, &vcc->flags))
593 return -EINVAL;
594 if (copy_from_user(&sa, (void __user *) arg, sizeof(sa)))
595 return -EFAULT;
596 error = svc_addparty(sock, (struct sockaddr *)&sa, sizeof(sa),
597 0);
598 break;
599 case ATM_DROPPARTY:
600 if (!test_bit(ATM_VF_SESSION, &vcc->flags))
601 return -EINVAL;
602 if (copy_from_user(&ep_ref, (void __user *) arg, sizeof(int)))
603 return -EFAULT;
604 error = svc_dropparty(sock, ep_ref);
605 break;
606 default:
607 error = vcc_ioctl(sock, cmd, arg);
608 }
609
610 return error;
611}
612
613#ifdef CONFIG_COMPAT
614static int svc_compat_ioctl(struct socket *sock, unsigned int cmd,
615 unsigned long arg)
616{
617 /* The definition of ATM_ADDPARTY uses the size of struct atm_iobuf.
618 But actually it takes a struct sockaddr_atmsvc, which doesn't need
619 compat handling. So all we have to do is fix up cmd... */
620 if (cmd == COMPAT_ATM_ADDPARTY)
621 cmd = ATM_ADDPARTY;
622
623 if (cmd == ATM_ADDPARTY || cmd == ATM_DROPPARTY)
624 return svc_ioctl(sock, cmd, arg);
625 else
626 return vcc_compat_ioctl(sock, cmd, arg);
627}
628#endif /* CONFIG_COMPAT */
629
630static const struct proto_ops svc_proto_ops = {
631 .family = PF_ATMSVC,
632 .owner = THIS_MODULE,
633
634 .release = svc_release,
635 .bind = svc_bind,
636 .connect = svc_connect,
637 .socketpair = sock_no_socketpair,
638 .accept = svc_accept,
639 .getname = svc_getname,
640 .poll = vcc_poll,
641 .ioctl = svc_ioctl,
642#ifdef CONFIG_COMPAT
643 .compat_ioctl = svc_compat_ioctl,
644#endif
645 .listen = svc_listen,
646 .shutdown = svc_shutdown,
647 .setsockopt = svc_setsockopt,
648 .getsockopt = svc_getsockopt,
649 .sendmsg = vcc_sendmsg,
650 .recvmsg = vcc_recvmsg,
651 .mmap = sock_no_mmap,
652 .sendpage = sock_no_sendpage,
653};
654
655
656static int svc_create(struct net *net, struct socket *sock, int protocol,
657 int kern)
658{
659 int error;
660
661 if (!net_eq(net, &init_net))
662 return -EAFNOSUPPORT;
663
664 sock->ops = &svc_proto_ops;
665 error = vcc_create(net, sock, protocol, AF_ATMSVC);
666 if (error)
667 return error;
668 ATM_SD(sock)->local.sas_family = AF_ATMSVC;
669 ATM_SD(sock)->remote.sas_family = AF_ATMSVC;
670 return 0;
671}
672
673static const struct net_proto_family svc_family_ops = {
674 .family = PF_ATMSVC,
675 .create = svc_create,
676 .owner = THIS_MODULE,
677};
678
679
680/*
681 * Initialize the ATM SVC protocol family
682 */
683
684int __init atmsvc_init(void)
685{
686 return sock_register(&svc_family_ops);
687}
688
689void atmsvc_exit(void)
690{
691 sock_unregister(PF_ATMSVC);
692}