blob: 3a180cfd7b48ee37116f9b78a1dc98081a9528d5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* net/atm/svc.c - ATM SVC sockets */
2
3/* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
4
5
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>
22
23#include "resources.h"
24#include "common.h" /* common for PVCs and SVCs */
25#include "signaling.h"
26#include "addr.h"
27
28
29#if 0
30#define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
31#else
32#define DPRINTK(format,args...)
33#endif
34
35
36static int svc_create(struct socket *sock,int protocol);
37
38
39/*
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.
43 */
44
45
46static int svc_shutdown(struct socket *sock,int how)
47{
48 return 0;
49}
50
51
52static void svc_disconnect(struct atm_vcc *vcc)
53{
54 DEFINE_WAIT(wait);
55 struct sk_buff *skb;
56 struct sock *sk = sk_atm(vcc);
57
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) {
63 schedule();
64 prepare_to_wait(sk->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
65 }
66 finish_wait(sk->sk_sleep, &wait);
67 }
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);
74 dev_kfree_skb(skb);
75 }
76 clear_bit(ATM_VF_REGIS, &vcc->flags);
77 /* ... may retry later */
78}
79
80
81static int svc_release(struct socket *sock)
82{
83 struct sock *sk = sock->sk;
84 struct atm_vcc *vcc;
85
86 if (sk) {
87 vcc = ATM_SD(sock);
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
92 are closed */
93 svc_disconnect(vcc);
94 vcc_release(sock);
95 }
96 return 0;
97}
98
99
100static int svc_bind(struct socket *sock,struct sockaddr *sockaddr,
101 int sockaddr_len)
102{
103 DEFINE_WAIT(wait);
104 struct sock *sk = sock->sk;
105 struct sockaddr_atmsvc *addr;
106 struct atm_vcc *vcc;
107 int error;
108
109 if (sockaddr_len != sizeof(struct sockaddr_atmsvc))
110 return -EINVAL;
111 lock_sock(sk);
112 if (sock->state == SS_CONNECTED) {
113 error = -EISCONN;
114 goto out;
115 }
116 if (sock->state != SS_UNCONNECTED) {
117 error = -EINVAL;
118 goto out;
119 }
120 vcc = ATM_SD(sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 addr = (struct sockaddr_atmsvc *) sockaddr;
122 if (addr->sas_family != AF_ATMSVC) {
123 error = -EAFNOSUPPORT;
124 goto out;
125 }
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)) {
130 error = -EBADFD;
131 goto out;
132 }
133 vcc->local = *addr;
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) {
138 schedule();
139 prepare_to_wait(sk->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
140 }
141 finish_wait(sk->sk_sleep, &wait);
142 clear_bit(ATM_VF_REGIS,&vcc->flags); /* doesn't count */
143 if (!sigd) {
144 error = -EUNATCH;
145 goto out;
146 }
147 if (!sk->sk_err)
148 set_bit(ATM_VF_BOUND,&vcc->flags);
149 error = -sk->sk_err;
150out:
151 release_sock(sk);
152 return error;
153}
154
155
156static int svc_connect(struct socket *sock,struct sockaddr *sockaddr,
157 int sockaddr_len,int flags)
158{
159 DEFINE_WAIT(wait);
160 struct sock *sk = sock->sk;
161 struct sockaddr_atmsvc *addr;
162 struct atm_vcc *vcc = ATM_SD(sock);
163 int error;
164
165 DPRINTK("svc_connect %p\n",vcc);
166 lock_sock(sk);
167 if (sockaddr_len != sizeof(struct sockaddr_atmsvc)) {
168 error = -EINVAL;
169 goto out;
170 }
171
172 switch (sock->state) {
173 default:
174 error = -EINVAL;
175 goto out;
176 case SS_CONNECTED:
177 error = -EISCONN;
178 goto out;
179 case SS_CONNECTING:
180 if (test_bit(ATM_VF_WAITING, &vcc->flags)) {
181 error = -EALREADY;
182 goto out;
183 }
184 sock->state = SS_UNCONNECTED;
185 if (sk->sk_err) {
186 error = -sk->sk_err;
187 goto out;
188 }
189 break;
190 case SS_UNCONNECTED:
191 addr = (struct sockaddr_atmsvc *) sockaddr;
192 if (addr->sas_family != AF_ATMSVC) {
193 error = -EAFNOSUPPORT;
194 goto out;
195 }
196 if (!test_bit(ATM_VF_HASQOS, &vcc->flags)) {
197 error = -EBADFD;
198 goto out;
199 }
200 if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
201 vcc->qos.rxtp.traffic_class == ATM_ANYCLASS) {
202 error = -EINVAL;
203 goto out;
204 }
205 if (!vcc->qos.txtp.traffic_class &&
206 !vcc->qos.rxtp.traffic_class) {
207 error = -EINVAL;
208 goto out;
209 }
210 vcc->remote = *addr;
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;
218 goto out;
219 }
220 error = 0;
221 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
222 schedule();
223 if (!signal_pending(current)) {
224 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
225 continue;
226 }
227 DPRINTK("*ABORT*\n");
228 /*
229 * This is tricky:
230 * Kernel ---close--> Demon
231 * Kernel <--close--- Demon
232 * or
233 * Kernel ---close--> Demon
234 * Kernel <--error--- Demon
235 * or
236 * Kernel ---close--> Demon
237 * Kernel <--okay---- Demon
238 * Kernel <--close--- Demon
239 */
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);
243 schedule();
244 }
245 if (!sk->sk_err)
246 while (!test_bit(ATM_VF_RELEASED,&vcc->flags)
247 && sigd) {
248 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
249 schedule();
250 }
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 */
255 error = -EINTR;
256 break;
257 }
258 finish_wait(sk->sk_sleep, &wait);
259 if (error)
260 goto out;
261 if (!sigd) {
262 error = -EUNATCH;
263 goto out;
264 }
265 if (sk->sk_err) {
266 error = -sk->sk_err;
267 goto out;
268 }
269 }
270/*
271 * Not supported yet
272 *
273 * #ifndef CONFIG_SINGLE_SIGITF
274 */
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;
278/*
279 * #endif
280 */
281 if (!(error = vcc_connect(sock, vcc->itf, vcc->vpi, vcc->vci)))
282 sock->state = SS_CONNECTED;
283 else
284 (void) svc_disconnect(vcc);
285out:
286 release_sock(sk);
287 return error;
288}
289
290
291static int svc_listen(struct socket *sock,int backlog)
292{
293 DEFINE_WAIT(wait);
294 struct sock *sk = sock->sk;
295 struct atm_vcc *vcc = ATM_SD(sock);
296 int error;
297
298 DPRINTK("svc_listen %p\n",vcc);
299 lock_sock(sk);
300 /* let server handle listen on unbound sockets */
301 if (test_bit(ATM_VF_SESSION,&vcc->flags)) {
302 error = -EINVAL;
303 goto out;
304 }
Chas Williams9301e322005-09-28 16:35:01 -0700305 vcc_insert_socket(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 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) {
310 schedule();
311 prepare_to_wait(sk->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
312 }
313 finish_wait(sk->sk_sleep, &wait);
314 if (!sigd) {
315 error = -EUNATCH;
316 goto out;
317 }
318 set_bit(ATM_VF_LISTEN,&vcc->flags);
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
326
327static int svc_accept(struct socket *sock,struct socket *newsock,int flags)
328{
329 struct sock *sk = sock->sk;
330 struct sk_buff *skb;
331 struct atmsvc_msg *msg;
332 struct atm_vcc *old_vcc = ATM_SD(sock);
333 struct atm_vcc *new_vcc;
334 int error;
335
336 lock_sock(sk);
337
338 error = svc_create(newsock,0);
339 if (error)
340 goto out;
341
342 new_vcc = ATM_SD(newsock);
343
344 DPRINTK("svc_accept %p -> %p\n",old_vcc,new_vcc);
345 while (1) {
346 DEFINE_WAIT(wait);
347
348 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
349 while (!(skb = skb_dequeue(&sk->sk_receive_queue)) &&
350 sigd) {
351 if (test_bit(ATM_VF_RELEASED,&old_vcc->flags)) 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->sk_sleep, &wait, TASK_INTERRUPTIBLE);
368 }
369 finish_wait(sk->sk_sleep, &wait);
370 if (error)
371 goto out;
372 if (!skb) {
373 error = -EUNATCH;
374 goto out;
375 }
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);
384 dev_kfree_skb(skb);
385 sk->sk_ack_backlog--;
386 if (error) {
387 sigd_enq2(NULL,as_reject,old_vcc,NULL,NULL,
388 &old_vcc->qos,error);
389 error = error == -EAGAIN ? -EBUSY : error;
390 goto out;
391 }
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) {
397 release_sock(sk);
398 schedule();
399 lock_sock(sk);
400 prepare_to_wait(sk_atm(new_vcc)->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
401 }
402 finish_wait(sk_atm(new_vcc)->sk_sleep, &wait);
403 if (!sigd) {
404 error = -EUNATCH;
405 goto out;
406 }
407 if (!sk_atm(new_vcc)->sk_err)
408 break;
409 if (sk_atm(new_vcc)->sk_err != ERESTARTSYS) {
410 error = -sk_atm(new_vcc)->sk_err;
411 goto out;
412 }
413 }
414 newsock->state = SS_CONNECTED;
415out:
416 release_sock(sk);
417 return error;
418}
419
420
421static int svc_getname(struct socket *sock,struct sockaddr *sockaddr,
422 int *sockaddr_len,int peer)
423{
424 struct sockaddr_atmsvc *addr;
425
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));
430 return 0;
431}
432
433
434int svc_change_qos(struct atm_vcc *vcc,struct atm_qos *qos)
435{
436 struct sock *sk = sk_atm(vcc);
437 DEFINE_WAIT(wait);
438
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) {
444 schedule();
445 prepare_to_wait(sk->sk_sleep, &wait, TASK_UNINTERRUPTIBLE);
446 }
447 finish_wait(sk->sk_sleep, &wait);
448 if (!sigd) return -EUNATCH;
449 return -sk->sk_err;
450}
451
452
453static int svc_setsockopt(struct socket *sock, int level, int optname,
454 char __user *optval, int optlen)
455{
456 struct sock *sk = sock->sk;
457 struct atm_vcc *vcc = ATM_SD(sock);
458 int value, error = 0;
459
460 lock_sock(sk);
461 switch (optname) {
462 case SO_ATMSAP:
463 if (level != SOL_ATM || optlen != sizeof(struct atm_sap)) {
464 error = -EINVAL;
465 goto out;
466 }
467 if (copy_from_user(&vcc->sap, optval, optlen)) {
468 error = -EFAULT;
469 goto out;
470 }
471 set_bit(ATM_VF_HASSAP, &vcc->flags);
472 break;
473 case SO_MULTIPOINT:
474 if (level != SOL_ATM || optlen != sizeof(int)) {
475 error = -EINVAL;
476 goto out;
477 }
478 if (get_user(value, (int __user *) optval)) {
479 error = -EFAULT;
480 goto out;
481 }
482 if (value == 1) {
483 set_bit(ATM_VF_SESSION, &vcc->flags);
484 } else if (value == 0) {
485 clear_bit(ATM_VF_SESSION, &vcc->flags);
486 } else {
487 error = -EINVAL;
488 }
489 break;
490 default:
491 error = vcc_setsockopt(sock, level, optname,
492 optval, optlen);
493 }
494
495out:
496 release_sock(sk);
497 return error;
498}
499
500
501static int svc_getsockopt(struct socket *sock,int level,int optname,
502 char __user *optval,int __user *optlen)
503{
504 struct sock *sk = sock->sk;
505 int error = 0, len;
506
507 lock_sock(sk);
508 if (!__SO_LEVEL_MATCH(optname, level) || optname != SO_ATMSAP) {
509 error = vcc_getsockopt(sock, level, optname, optval, optlen);
510 goto out;
511 }
512 if (get_user(len, optlen)) {
513 error = -EFAULT;
514 goto out;
515 }
516 if (len != sizeof(struct atm_sap)) {
517 error = -EINVAL;
518 goto out;
519 }
520 if (copy_to_user(optval, &ATM_SD(sock)->sap, sizeof(struct atm_sap))) {
521 error = -EFAULT;
522 goto out;
523 }
524out:
525 release_sock(sk);
526 return error;
527}
528
529
530static int svc_addparty(struct socket *sock, struct sockaddr *sockaddr,
531 int sockaddr_len, int flags)
532{
533 DEFINE_WAIT(wait);
534 struct sock *sk = sock->sk;
535 struct atm_vcc *vcc = ATM_SD(sock);
536 int error;
537
538 lock_sock(sk);
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;
546 goto out;
547 }
548 DPRINTK("svc_addparty added wait queue\n");
549 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
550 schedule();
551 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
552 }
553 finish_wait(sk->sk_sleep, &wait);
554 error = xchg(&sk->sk_err_soft, 0);
555out:
556 release_sock(sk);
557 return error;
558}
559
560
561static int svc_dropparty(struct socket *sock, int ep_ref)
562{
563 DEFINE_WAIT(wait);
564 struct sock *sk = sock->sk;
565 struct atm_vcc *vcc = ATM_SD(sock);
566 int error;
567
568 lock_sock(sk);
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) {
573 schedule();
574 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
575 }
576 finish_wait(sk->sk_sleep, &wait);
577 if (!sigd) {
578 error = -EUNATCH;
579 goto out;
580 }
581 error = xchg(&sk->sk_err_soft, 0);
582out:
583 release_sock(sk);
584 return error;
585}
586
587
588static int svc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
589{
590 int error, ep_ref;
591 struct sockaddr_atmsvc sa;
592 struct atm_vcc *vcc = ATM_SD(sock);
593
594 switch (cmd) {
595 case ATM_ADDPARTY:
596 if (!test_bit(ATM_VF_SESSION, &vcc->flags))
597 return -EINVAL;
598 if (copy_from_user(&sa, (void __user *) arg, sizeof(sa)))
599 return -EFAULT;
600 error = svc_addparty(sock, (struct sockaddr *) &sa, sizeof(sa), 0);
601 break;
602 case ATM_DROPPARTY:
603 if (!test_bit(ATM_VF_SESSION, &vcc->flags))
604 return -EINVAL;
605 if (copy_from_user(&ep_ref, (void __user *) arg, sizeof(int)))
606 return -EFAULT;
607 error = svc_dropparty(sock, ep_ref);
608 break;
609 default:
610 error = vcc_ioctl(sock, cmd, arg);
611 }
612
613 return error;
614}
615
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800616static const struct proto_ops svc_proto_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 .family = PF_ATMSVC,
618 .owner = THIS_MODULE,
619
620 .release = svc_release,
621 .bind = svc_bind,
622 .connect = svc_connect,
623 .socketpair = sock_no_socketpair,
624 .accept = svc_accept,
625 .getname = svc_getname,
626 .poll = vcc_poll,
627 .ioctl = svc_ioctl,
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,
636};
637
638
639static int svc_create(struct socket *sock,int protocol)
640{
641 int error;
642
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;
648 return 0;
649}
650
651
652static struct net_proto_family svc_family_ops = {
653 .family = PF_ATMSVC,
654 .create = svc_create,
655 .owner = THIS_MODULE,
656};
657
658
659/*
660 * Initialize the ATM SVC protocol family
661 */
662
663int __init atmsvc_init(void)
664{
665 return sock_register(&svc_family_ops);
666}
667
668void atmsvc_exit(void)
669{
670 sock_unregister(PF_ATMSVC);
671}