1 /* 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 net_proto,
8 				   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 <asm/uaccess.h>
23 
24 #include "resources.h"
25 #include "common.h"		/* common for PVCs and SVCs */
26 #include "signaling.h"
27 #include "addr.h"
28 
29 
30 #if 0
31 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
32 #else
33 #define DPRINTK(format,args...)
34 #endif
35 
36 
37 static int svc_create(struct socket *sock,int protocol);
38 
39 
40 /*
41  * Note: since all this is still nicely synchronized with the signaling demon,
42  *       there's no need to protect sleep loops with clis. If signaling is
43  *       moved into the kernel, that would change.
44  */
45 
46 
svc_callback(struct atm_vcc * vcc)47 void svc_callback(struct atm_vcc *vcc)
48 {
49 	wake_up(&vcc->sleep);
50 }
51 
52 
53 
54 
svc_shutdown(struct socket * sock,int how)55 static int svc_shutdown(struct socket *sock,int how)
56 {
57 	return 0;
58 }
59 
60 
svc_disconnect(struct atm_vcc * vcc)61 static void svc_disconnect(struct atm_vcc *vcc)
62 {
63 	DECLARE_WAITQUEUE(wait,current);
64 	struct sk_buff *skb;
65 
66 	DPRINTK("svc_disconnect %p\n",vcc);
67 	if (test_bit(ATM_VF_REGIS,&vcc->flags)) {
68 		add_wait_queue(&vcc->sleep,&wait);
69 		sigd_enq(vcc,as_close,NULL,NULL,NULL);
70 		while (!test_bit(ATM_VF_RELEASED,&vcc->flags) && sigd) {
71 			set_current_state(TASK_UNINTERRUPTIBLE);
72 			schedule();
73 		}
74 		remove_wait_queue(&vcc->sleep,&wait);
75 	}
76 	/* beware - socket is still in use by atmsigd until the last
77 	   as_indicate has been answered */
78 	while ((skb = skb_dequeue(&vcc->sk->receive_queue))) {
79 		DPRINTK("LISTEN REL\n");
80 		sigd_enq2(NULL,as_reject,vcc,NULL,NULL,&vcc->qos,0);
81 		dev_kfree_skb(skb);
82 	}
83 	clear_bit(ATM_VF_REGIS,&vcc->flags);
84 	clear_bit(ATM_VF_RELEASED,&vcc->flags);
85 	clear_bit(ATM_VF_CLOSE,&vcc->flags);
86 	/* ... may retry later */
87 }
88 
89 
svc_release(struct socket * sock)90 static int svc_release(struct socket *sock)
91 {
92 	struct sock *sk = sock->sk;
93 	struct atm_vcc *vcc;
94 
95 	if (sk)  {
96 		vcc = ATM_SD(sock);
97 		DPRINTK("svc_release %p\n", vcc);
98 		clear_bit(ATM_VF_READY, &vcc->flags);
99 		/* VCC pointer is used as a reference, so we must not free it
100 		   (thereby subjecting it to re-use) before all pending connections
101 	           are closed */
102 		sock_hold(sk);
103 		vcc_release(sock);
104 		svc_disconnect(vcc);
105 		sock_put(sk);
106 	}
107 	return 0;
108 }
109 
110 
svc_bind(struct socket * sock,struct sockaddr * sockaddr,int sockaddr_len)111 static int svc_bind(struct socket *sock,struct sockaddr *sockaddr,
112     int sockaddr_len)
113 {
114 	DECLARE_WAITQUEUE(wait,current);
115 	struct sock *sk = sock->sk;
116 	struct sockaddr_atmsvc *addr;
117 	struct atm_vcc *vcc;
118 	int error;
119 
120 	if (sockaddr_len != sizeof(struct sockaddr_atmsvc))
121 		return -EINVAL;
122 	lock_sock(sk);
123 	if (sock->state == SS_CONNECTED) {
124 		error = -EISCONN;
125 		goto out;
126 	}
127 	if (sock->state != SS_UNCONNECTED) {
128 		error = -EINVAL;
129 		goto out;
130 	}
131 	vcc = ATM_SD(sock);
132 	if (test_bit(ATM_VF_SESSION, &vcc->flags)) {
133 		error = -EINVAL;
134 		goto out;
135 	}
136 	addr = (struct sockaddr_atmsvc *) sockaddr;
137 	if (addr->sas_family != AF_ATMSVC) {
138 		error = -EAFNOSUPPORT;
139 		goto out;
140 	}
141 	clear_bit(ATM_VF_BOUND,&vcc->flags);
142 	    /* failing rebind will kill old binding */
143 	/* @@@ check memory (de)allocation on rebind */
144 	if (!test_bit(ATM_VF_HASQOS,&vcc->flags)) {
145 		error = -EBADFD;
146 		goto out;
147 	}
148 	vcc->local = *addr;
149 	vcc->reply = WAITING;
150 	add_wait_queue(&vcc->sleep,&wait);
151 	sigd_enq(vcc,as_bind,NULL,NULL,&vcc->local);
152 	while (vcc->reply == WAITING && sigd) {
153 		set_current_state(TASK_UNINTERRUPTIBLE);
154 		schedule();
155 	}
156 	remove_wait_queue(&vcc->sleep,&wait);
157 	clear_bit(ATM_VF_REGIS,&vcc->flags); /* doesn't count */
158 	if (!sigd) {
159 		error = -EUNATCH;
160 		goto out;
161 	}
162         if (!vcc->reply)
163 		set_bit(ATM_VF_BOUND,&vcc->flags);
164 	error = vcc->reply;
165 out:
166 	release_sock(sk);
167 	return error;
168 }
169 
170 
svc_connect(struct socket * sock,struct sockaddr * sockaddr,int sockaddr_len,int flags)171 static int svc_connect(struct socket *sock,struct sockaddr *sockaddr,
172     int sockaddr_len,int flags)
173 {
174 	DECLARE_WAITQUEUE(wait,current);
175 	struct sock *sk = sock->sk;
176 	struct sockaddr_atmsvc *addr;
177 	struct atm_vcc *vcc = ATM_SD(sock);
178 	int error;
179 
180 	DPRINTK("svc_connect %p\n",vcc);
181 	lock_sock(sk);
182 	if (sockaddr_len != sizeof(struct sockaddr_atmsvc)) {
183 		error = -EINVAL;
184 		goto out;
185 	}
186 
187 	switch (sock->state) {
188 	default:
189 		error = -EINVAL;
190 		goto out;
191 	case SS_CONNECTED:
192 		error = -EISCONN;
193 		goto out;
194 	case SS_CONNECTING:
195 		if (vcc->reply == WAITING) {
196 			error = -EALREADY;
197 			goto out;
198 		}
199 		sock->state = SS_UNCONNECTED;
200 		if (vcc->reply) {
201 			error = vcc->reply;
202 			goto out;
203 		}
204 		break;
205 	case SS_UNCONNECTED:
206 		if (test_bit(ATM_VF_SESSION, &vcc->flags)) {
207 			error = -EINVAL;
208 			goto out;
209 		}
210 		addr = (struct sockaddr_atmsvc *) sockaddr;
211 		if (addr->sas_family != AF_ATMSVC) {
212 			error = -EAFNOSUPPORT;
213 			goto out;
214 		}
215 		if (!test_bit(ATM_VF_HASQOS, &vcc->flags)) {
216 			error = -EBADFD;
217 			goto out;
218 		}
219 		if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
220 		    vcc->qos.rxtp.traffic_class == ATM_ANYCLASS) {
221 			error = -EINVAL;
222 			goto out;
223 		}
224 		if (!vcc->qos.txtp.traffic_class &&
225 		    !vcc->qos.rxtp.traffic_class) {
226 			error = -EINVAL;
227 			goto out;
228 		}
229 		vcc->remote = *addr;
230 		vcc->reply = WAITING;
231 		add_wait_queue(&vcc->sleep,&wait);
232 		sigd_enq(vcc,as_connect,NULL,NULL,&vcc->remote);
233 		if (flags & O_NONBLOCK) {
234 			remove_wait_queue(&vcc->sleep,&wait);
235 			sock->state = SS_CONNECTING;
236 			error = -EINPROGRESS;
237 			goto out;
238 		}
239 		error = 0;
240 		while (vcc->reply == WAITING && sigd) {
241 			set_current_state(TASK_INTERRUPTIBLE);
242 			schedule();
243 			if (!signal_pending(current)) continue;
244 			DPRINTK("*ABORT*\n");
245 			/*
246 			 * This is tricky:
247 			 *   Kernel ---close--> Demon
248 			 *   Kernel <--close--- Demon
249 		         * or
250 			 *   Kernel ---close--> Demon
251 			 *   Kernel <--error--- Demon
252 			 * or
253 			 *   Kernel ---close--> Demon
254 			 *   Kernel <--okay---- Demon
255 			 *   Kernel <--close--- Demon
256 			 */
257 			sigd_enq(vcc,as_close,NULL,NULL,NULL);
258 			while (vcc->reply == WAITING && sigd) {
259 				set_current_state(TASK_UNINTERRUPTIBLE);
260 				schedule();
261 			}
262 			if (!vcc->reply)
263 				while (!test_bit(ATM_VF_RELEASED,&vcc->flags)
264 				    && sigd) {
265 					set_current_state(TASK_UNINTERRUPTIBLE);
266 					schedule();
267 				}
268 			clear_bit(ATM_VF_REGIS,&vcc->flags);
269 			clear_bit(ATM_VF_RELEASED,&vcc->flags);
270 			clear_bit(ATM_VF_CLOSE,&vcc->flags);
271 			    /* we're gone now but may connect later */
272 			error = -EINTR;
273 			break;
274 		}
275 		remove_wait_queue(&vcc->sleep,&wait);
276 		if (error)
277 			goto out;
278 		if (!sigd) {
279 			error = -EUNATCH;
280 			goto out;
281 		}
282 		if (vcc->reply) {
283 			error = vcc->reply;
284 			goto out;
285 		}
286 	}
287 /*
288  * Not supported yet
289  *
290  * #ifndef CONFIG_SINGLE_SIGITF
291  */
292 	vcc->qos.txtp.max_pcr = SELECT_TOP_PCR(vcc->qos.txtp);
293 	vcc->qos.txtp.pcr = 0;
294 	vcc->qos.txtp.min_pcr = 0;
295 /*
296  * #endif
297  */
298 	if (!(error = vcc_connect(sock, vcc->itf, vcc->vpi, vcc->vci)))
299 		sock->state = SS_CONNECTED;
300 	else (void) svc_disconnect(vcc);
301 out:
302 	release_sock(sk);
303 	return error;
304 }
305 
306 
svc_listen(struct socket * sock,int backlog)307 static int svc_listen(struct socket *sock,int backlog)
308 {
309 	DECLARE_WAITQUEUE(wait,current);
310 	struct sock *sk = sock->sk;
311 	struct atm_vcc *vcc = ATM_SD(sock);
312 	int error;
313 
314 	DPRINTK("svc_listen %p\n",vcc);
315 	lock_sock(sk);
316 	/* let server handle listen on unbound sockets */
317 	if (test_bit(ATM_VF_SESSION,&vcc->flags)) {
318 		error = -EINVAL;
319 		goto out;
320 	}
321 	if (test_bit(ATM_VF_LISTEN, &vcc->flags)) {
322 		error = -EADDRINUSE;
323 		goto out;
324 	}
325 	vcc->reply = WAITING;
326 	add_wait_queue(&vcc->sleep,&wait);
327 	sigd_enq(vcc,as_listen,NULL,NULL,&vcc->local);
328 	while (vcc->reply == WAITING && sigd) {
329 		set_current_state(TASK_UNINTERRUPTIBLE);
330 		schedule();
331 	}
332 	remove_wait_queue(&vcc->sleep,&wait);
333 	if (!sigd) {
334 		error = -EUNATCH;
335 		goto out;
336 	}
337 	set_bit(ATM_VF_LISTEN,&vcc->flags);
338 	vcc->sk->max_ack_backlog = backlog > 0 ? backlog : ATM_BACKLOG_DEFAULT;
339 	error = vcc->reply;
340 out:
341 	release_sock(sk);
342 	return error;
343 }
344 
345 
svc_accept(struct socket * sock,struct socket * newsock,int flags)346 static int svc_accept(struct socket *sock,struct socket *newsock,int flags)
347 {
348 	struct sock *sk = sock->sk;
349 	struct sk_buff *skb;
350 	struct atmsvc_msg *msg;
351 	struct atm_vcc *old_vcc = ATM_SD(sock);
352 	struct atm_vcc *new_vcc;
353 	int error;
354 
355 	lock_sock(sk);
356 
357 	error = svc_create(newsock,0);
358 	if (error)
359 		goto out;
360 
361 	new_vcc = ATM_SD(newsock);
362 
363 	DPRINTK("svc_accept %p -> %p\n",old_vcc,new_vcc);
364 	while (1) {
365 		DECLARE_WAITQUEUE(wait,current);
366 
367 		add_wait_queue(&old_vcc->sleep,&wait);
368 		while (!(skb = skb_dequeue(&old_vcc->sk->receive_queue)) && sigd) {
369 			if (test_bit(ATM_VF_RELEASED,&old_vcc->flags)) break;
370 			if (test_bit(ATM_VF_CLOSE,&old_vcc->flags)) {
371 				error = old_vcc->reply;
372 				break;
373 			}
374 			if (flags & O_NONBLOCK) {
375 				error = -EAGAIN;
376 				break;
377 			}
378 			release_sock(sk);
379 			schedule();
380 			lock_sock(sk);
381 			if (signal_pending(current)) {
382 				error = -ERESTARTSYS;
383 				break;
384 			}
385 		}
386 		remove_wait_queue(&old_vcc->sleep,&wait);
387 		if (error)
388 			goto out;
389 		if (!skb) {
390 			error = -EUNATCH;
391 			goto out;
392 		}
393 		msg = (struct atmsvc_msg *) skb->data;
394 		new_vcc->qos = msg->qos;
395 		set_bit(ATM_VF_HASQOS,&new_vcc->flags);
396 		new_vcc->remote = msg->svc;
397 		new_vcc->local = msg->local;
398 		new_vcc->sap = msg->sap;
399 		error = vcc_connect(newsock, msg->pvc.sap_addr.itf,
400 				    msg->pvc.sap_addr.vpi, msg->pvc.sap_addr.vci);
401 		dev_kfree_skb(skb);
402 		old_vcc->sk->ack_backlog--;
403 		if (error) {
404 			sigd_enq2(NULL,as_reject,old_vcc,NULL,NULL,
405 			    &old_vcc->qos,error);
406 			error = error == -EAGAIN ? -EBUSY : error;
407 			goto out;
408 		}
409 		/* wait should be short, so we ignore the non-blocking flag */
410 		new_vcc->reply = WAITING;
411 		add_wait_queue(&new_vcc->sleep,&wait);
412 		sigd_enq(new_vcc,as_accept,old_vcc,NULL,NULL);
413 		while (new_vcc->reply == WAITING && sigd) {
414 			set_current_state(TASK_UNINTERRUPTIBLE);
415 			release_sock(sk);
416 			schedule();
417 			lock_sock(sk);
418 		}
419 		remove_wait_queue(&new_vcc->sleep,&wait);
420 		if (!sigd) {
421 			error = -EUNATCH;
422 			goto out;
423 		}
424 		if (!new_vcc->reply) break;
425 		if (new_vcc->reply != -ERESTARTSYS) {
426 			error = new_vcc->reply;
427 			goto out;
428 		}
429 	}
430 	newsock->state = SS_CONNECTED;
431 out:
432 	release_sock(sk);
433 	return error;
434 }
435 
436 
svc_getname(struct socket * sock,struct sockaddr * sockaddr,int * sockaddr_len,int peer)437 static int svc_getname(struct socket *sock,struct sockaddr *sockaddr,
438     int *sockaddr_len,int peer)
439 {
440 	struct sockaddr_atmsvc *addr;
441 
442 	*sockaddr_len = sizeof(struct sockaddr_atmsvc);
443 	addr = (struct sockaddr_atmsvc *) sockaddr;
444 	memcpy(addr,peer ? &ATM_SD(sock)->remote : &ATM_SD(sock)->local,
445 	    sizeof(struct sockaddr_atmsvc));
446 	return 0;
447 }
448 
449 
svc_change_qos(struct atm_vcc * vcc,struct atm_qos * qos)450 int svc_change_qos(struct atm_vcc *vcc,struct atm_qos *qos)
451 {
452 	DECLARE_WAITQUEUE(wait,current);
453 
454 	vcc->reply = WAITING;
455 	add_wait_queue(&vcc->sleep,&wait);
456 	sigd_enq2(vcc,as_modify,NULL,NULL,&vcc->local,qos,0);
457 	while (vcc->reply == WAITING && !test_bit(ATM_VF_RELEASED,&vcc->flags)
458 	    && sigd) {
459 		set_current_state(TASK_UNINTERRUPTIBLE);
460 		schedule();
461 	}
462 	remove_wait_queue(&vcc->sleep,&wait);
463 	if (!sigd) return -EUNATCH;
464 	return vcc->reply;
465 }
466 
467 
svc_setsockopt(struct socket * sock,int level,int optname,char * optval,int optlen)468 static int svc_setsockopt(struct socket *sock,int level,int optname,
469     char *optval,int optlen)
470 {
471 	struct sock *sk = sock->sk;
472 	struct atm_vcc *vcc;
473 	int error = 0;
474 
475 	if (!__SO_LEVEL_MATCH(optname, level) || optname != SO_ATMSAP ||
476 	    optlen != sizeof(struct atm_sap)) {
477 		error = vcc_setsockopt(sock, level, optname, optval, optlen);
478 		goto out;
479 	}
480 	vcc = ATM_SD(sock);
481 	if (copy_from_user(&vcc->sap, optval, optlen)) {
482 		error = -EFAULT;
483 		goto out;
484 	}
485 	set_bit(ATM_VF_HASSAP, &vcc->flags);
486 out:
487 	release_sock(sk);
488 	return error;
489 }
490 
491 
svc_getsockopt(struct socket * sock,int level,int optname,char * optval,int * optlen)492 static int svc_getsockopt(struct socket *sock,int level,int optname,
493     char *optval,int *optlen)
494 {
495 	struct sock *sk = sock->sk;
496 	int error = 0, len;
497 
498 	lock_sock(sk);
499 	if (!__SO_LEVEL_MATCH(optname, level) || optname != SO_ATMSAP) {
500 		error = vcc_getsockopt(sock, level, optname, optval, optlen);
501 		goto out;
502 	}
503 	if (get_user(len, optlen)) {
504 		error = -EFAULT;
505 		goto out;
506 	}
507 	if (len != sizeof(struct atm_sap)) {
508 		error = -EINVAL;
509 		goto out;
510 	}
511 	if (copy_to_user(optval, &ATM_SD(sock)->sap, sizeof(struct atm_sap))) {
512 		error = -EFAULT;
513 		goto out;
514 	}
515 out:
516 	release_sock(sk);
517 	return error;
518 }
519 
520 
521 static struct proto_ops svc_proto_ops = {
522 	.family =	PF_ATMSVC,
523 
524 	.release =	svc_release,
525 	.bind =		svc_bind,
526 	.connect =	svc_connect,
527 	.socketpair =	sock_no_socketpair,
528 	.accept =	svc_accept,
529 	.getname =	svc_getname,
530 	.poll =		atm_poll,
531 	.ioctl =	vcc_ioctl,
532 	.listen =	svc_listen,
533 	.shutdown =	svc_shutdown,
534 	.setsockopt =	svc_setsockopt,
535 	.getsockopt =	svc_getsockopt,
536 	.sendmsg =	vcc_sendmsg,
537 	.recvmsg =	vcc_recvmsg,
538 	.mmap =		sock_no_mmap,
539 	.sendpage =	sock_no_sendpage,
540 };
541 
542 
svc_create(struct socket * sock,int protocol)543 static int svc_create(struct socket *sock,int protocol)
544 {
545 	int error;
546 
547 	sock->ops = &svc_proto_ops;
548 	error = vcc_create(sock, protocol, AF_ATMSVC);
549 	if (error) return error;
550 	ATM_SD(sock)->callback = svc_callback;
551 	ATM_SD(sock)->local.sas_family = AF_ATMSVC;
552 	ATM_SD(sock)->remote.sas_family = AF_ATMSVC;
553 	return 0;
554 }
555 
556 
557 static struct net_proto_family svc_family_ops = {
558 	PF_ATMSVC,
559 	svc_create,
560 	0,			/* no authentication */
561 	0,			/* no encryption */
562 	0			/* no encrypt_net */
563 };
564 
565 
566 /*
567  *	Initialize the ATM SVC protocol family
568  */
569 
atmsvc_init(void)570 int atmsvc_init(void)
571 {
572 	return sock_register(&svc_family_ops);
573 }
574 
atmsvc_exit(void)575 void atmsvc_exit(void)
576 {
577 	sock_unregister(PF_ATMSVC);
578 }
579