1 /* net/atm/common.c - ATM sockets (common part for PVC and SVC) */
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/module.h>
8 #include <linux/kmod.h>
9 #include <linux/net.h>		/* struct socket, struct proto_ops */
10 #include <linux/atm.h>		/* ATM stuff */
11 #include <linux/atmdev.h>
12 #include <linux/socket.h>	/* SOL_SOCKET */
13 #include <linux/errno.h>	/* error codes */
14 #include <linux/capability.h>
15 #include <linux/mm.h>
16 #include <linux/sched.h>
17 #include <linux/time.h>		/* struct timeval */
18 #include <linux/skbuff.h>
19 #include <linux/bitops.h>
20 #include <linux/init.h>
21 #include <linux/slab.h>
22 #include <net/sock.h>		/* struct sock */
23 #include <linux/uaccess.h>
24 #include <linux/poll.h>
25 
26 #include <asm/atomic.h>
27 
28 #include "resources.h"		/* atm_find_dev */
29 #include "common.h"		/* prototypes */
30 #include "protocols.h"		/* atm_init_<transport> */
31 #include "addr.h"		/* address registry */
32 #include "signaling.h"		/* for WAITING and sigd_attach */
33 
34 struct hlist_head vcc_hash[VCC_HTABLE_SIZE];
35 EXPORT_SYMBOL(vcc_hash);
36 
37 DEFINE_RWLOCK(vcc_sklist_lock);
38 EXPORT_SYMBOL(vcc_sklist_lock);
39 
40 static ATOMIC_NOTIFIER_HEAD(atm_dev_notify_chain);
41 
__vcc_insert_socket(struct sock * sk)42 static void __vcc_insert_socket(struct sock *sk)
43 {
44 	struct atm_vcc *vcc = atm_sk(sk);
45 	struct hlist_head *head = &vcc_hash[vcc->vci & (VCC_HTABLE_SIZE - 1)];
46 	sk->sk_hash = vcc->vci & (VCC_HTABLE_SIZE - 1);
47 	sk_add_node(sk, head);
48 }
49 
vcc_insert_socket(struct sock * sk)50 void vcc_insert_socket(struct sock *sk)
51 {
52 	write_lock_irq(&vcc_sklist_lock);
53 	__vcc_insert_socket(sk);
54 	write_unlock_irq(&vcc_sklist_lock);
55 }
56 EXPORT_SYMBOL(vcc_insert_socket);
57 
vcc_remove_socket(struct sock * sk)58 static void vcc_remove_socket(struct sock *sk)
59 {
60 	write_lock_irq(&vcc_sklist_lock);
61 	sk_del_node_init(sk);
62 	write_unlock_irq(&vcc_sklist_lock);
63 }
64 
alloc_tx(struct atm_vcc * vcc,unsigned int size)65 static struct sk_buff *alloc_tx(struct atm_vcc *vcc, unsigned int size)
66 {
67 	struct sk_buff *skb;
68 	struct sock *sk = sk_atm(vcc);
69 
70 	if (sk_wmem_alloc_get(sk) && !atm_may_send(vcc, size)) {
71 		pr_debug("Sorry: wmem_alloc = %d, size = %d, sndbuf = %d\n",
72 			 sk_wmem_alloc_get(sk), size, sk->sk_sndbuf);
73 		return NULL;
74 	}
75 	while (!(skb = alloc_skb(size, GFP_KERNEL)))
76 		schedule();
77 	pr_debug("%d += %d\n", sk_wmem_alloc_get(sk), skb->truesize);
78 	atomic_add(skb->truesize, &sk->sk_wmem_alloc);
79 	return skb;
80 }
81 
vcc_sock_destruct(struct sock * sk)82 static void vcc_sock_destruct(struct sock *sk)
83 {
84 	if (atomic_read(&sk->sk_rmem_alloc))
85 		printk(KERN_DEBUG "%s: rmem leakage (%d bytes) detected.\n",
86 		       __func__, atomic_read(&sk->sk_rmem_alloc));
87 
88 	if (atomic_read(&sk->sk_wmem_alloc))
89 		printk(KERN_DEBUG "%s: wmem leakage (%d bytes) detected.\n",
90 		       __func__, atomic_read(&sk->sk_wmem_alloc));
91 }
92 
vcc_def_wakeup(struct sock * sk)93 static void vcc_def_wakeup(struct sock *sk)
94 {
95 	struct socket_wq *wq;
96 
97 	rcu_read_lock();
98 	wq = rcu_dereference(sk->sk_wq);
99 	if (wq_has_sleeper(wq))
100 		wake_up(&wq->wait);
101 	rcu_read_unlock();
102 }
103 
vcc_writable(struct sock * sk)104 static inline int vcc_writable(struct sock *sk)
105 {
106 	struct atm_vcc *vcc = atm_sk(sk);
107 
108 	return (vcc->qos.txtp.max_sdu +
109 		atomic_read(&sk->sk_wmem_alloc)) <= sk->sk_sndbuf;
110 }
111 
vcc_write_space(struct sock * sk)112 static void vcc_write_space(struct sock *sk)
113 {
114 	struct socket_wq *wq;
115 
116 	rcu_read_lock();
117 
118 	if (vcc_writable(sk)) {
119 		wq = rcu_dereference(sk->sk_wq);
120 		if (wq_has_sleeper(wq))
121 			wake_up_interruptible(&wq->wait);
122 
123 		sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
124 	}
125 
126 	rcu_read_unlock();
127 }
128 
129 static struct proto vcc_proto = {
130 	.name	  = "VCC",
131 	.owner	  = THIS_MODULE,
132 	.obj_size = sizeof(struct atm_vcc),
133 };
134 
vcc_create(struct net * net,struct socket * sock,int protocol,int family)135 int vcc_create(struct net *net, struct socket *sock, int protocol, int family)
136 {
137 	struct sock *sk;
138 	struct atm_vcc *vcc;
139 
140 	sock->sk = NULL;
141 	if (sock->type == SOCK_STREAM)
142 		return -EINVAL;
143 	sk = sk_alloc(net, family, GFP_KERNEL, &vcc_proto);
144 	if (!sk)
145 		return -ENOMEM;
146 	sock_init_data(sock, sk);
147 	sk->sk_state_change = vcc_def_wakeup;
148 	sk->sk_write_space = vcc_write_space;
149 
150 	vcc = atm_sk(sk);
151 	vcc->dev = NULL;
152 	memset(&vcc->local, 0, sizeof(struct sockaddr_atmsvc));
153 	memset(&vcc->remote, 0, sizeof(struct sockaddr_atmsvc));
154 	vcc->qos.txtp.max_sdu = 1 << 16; /* for meta VCs */
155 	atomic_set(&sk->sk_wmem_alloc, 1);
156 	atomic_set(&sk->sk_rmem_alloc, 0);
157 	vcc->push = NULL;
158 	vcc->pop = NULL;
159 	vcc->push_oam = NULL;
160 	vcc->vpi = vcc->vci = 0; /* no VCI/VPI yet */
161 	vcc->atm_options = vcc->aal_options = 0;
162 	sk->sk_destruct = vcc_sock_destruct;
163 	return 0;
164 }
165 
vcc_destroy_socket(struct sock * sk)166 static void vcc_destroy_socket(struct sock *sk)
167 {
168 	struct atm_vcc *vcc = atm_sk(sk);
169 	struct sk_buff *skb;
170 
171 	set_bit(ATM_VF_CLOSE, &vcc->flags);
172 	clear_bit(ATM_VF_READY, &vcc->flags);
173 	if (vcc->dev) {
174 		if (vcc->dev->ops->close)
175 			vcc->dev->ops->close(vcc);
176 		if (vcc->push)
177 			vcc->push(vcc, NULL); /* atmarpd has no push */
178 
179 		while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
180 			atm_return(vcc, skb->truesize);
181 			kfree_skb(skb);
182 		}
183 
184 		module_put(vcc->dev->ops->owner);
185 		atm_dev_put(vcc->dev);
186 	}
187 
188 	vcc_remove_socket(sk);
189 }
190 
vcc_release(struct socket * sock)191 int vcc_release(struct socket *sock)
192 {
193 	struct sock *sk = sock->sk;
194 
195 	if (sk) {
196 		lock_sock(sk);
197 		vcc_destroy_socket(sock->sk);
198 		release_sock(sk);
199 		sock_put(sk);
200 	}
201 
202 	return 0;
203 }
204 
vcc_release_async(struct atm_vcc * vcc,int reply)205 void vcc_release_async(struct atm_vcc *vcc, int reply)
206 {
207 	struct sock *sk = sk_atm(vcc);
208 
209 	set_bit(ATM_VF_CLOSE, &vcc->flags);
210 	sk->sk_shutdown |= RCV_SHUTDOWN;
211 	sk->sk_err = -reply;
212 	clear_bit(ATM_VF_WAITING, &vcc->flags);
213 	sk->sk_state_change(sk);
214 }
215 EXPORT_SYMBOL(vcc_release_async);
216 
atm_dev_signal_change(struct atm_dev * dev,char signal)217 void atm_dev_signal_change(struct atm_dev *dev, char signal)
218 {
219 	pr_debug("%s signal=%d dev=%p number=%d dev->signal=%d\n",
220 		__func__, signal, dev, dev->number, dev->signal);
221 
222 	/* atm driver sending invalid signal */
223 	WARN_ON(signal < ATM_PHY_SIG_LOST || signal > ATM_PHY_SIG_FOUND);
224 
225 	if (dev->signal == signal)
226 		return; /* no change */
227 
228 	dev->signal = signal;
229 
230 	atomic_notifier_call_chain(&atm_dev_notify_chain, signal, dev);
231 }
232 EXPORT_SYMBOL(atm_dev_signal_change);
233 
atm_dev_release_vccs(struct atm_dev * dev)234 void atm_dev_release_vccs(struct atm_dev *dev)
235 {
236 	int i;
237 
238 	write_lock_irq(&vcc_sklist_lock);
239 	for (i = 0; i < VCC_HTABLE_SIZE; i++) {
240 		struct hlist_head *head = &vcc_hash[i];
241 		struct hlist_node *node, *tmp;
242 		struct sock *s;
243 		struct atm_vcc *vcc;
244 
245 		sk_for_each_safe(s, node, tmp, head) {
246 			vcc = atm_sk(s);
247 			if (vcc->dev == dev) {
248 				vcc_release_async(vcc, -EPIPE);
249 				sk_del_node_init(s);
250 			}
251 		}
252 	}
253 	write_unlock_irq(&vcc_sklist_lock);
254 }
255 EXPORT_SYMBOL(atm_dev_release_vccs);
256 
adjust_tp(struct atm_trafprm * tp,unsigned char aal)257 static int adjust_tp(struct atm_trafprm *tp, unsigned char aal)
258 {
259 	int max_sdu;
260 
261 	if (!tp->traffic_class)
262 		return 0;
263 	switch (aal) {
264 	case ATM_AAL0:
265 		max_sdu = ATM_CELL_SIZE-1;
266 		break;
267 	case ATM_AAL34:
268 		max_sdu = ATM_MAX_AAL34_PDU;
269 		break;
270 	default:
271 		pr_warning("AAL problems ... (%d)\n", aal);
272 		/* fall through */
273 	case ATM_AAL5:
274 		max_sdu = ATM_MAX_AAL5_PDU;
275 	}
276 	if (!tp->max_sdu)
277 		tp->max_sdu = max_sdu;
278 	else if (tp->max_sdu > max_sdu)
279 		return -EINVAL;
280 	if (!tp->max_cdv)
281 		tp->max_cdv = ATM_MAX_CDV;
282 	return 0;
283 }
284 
check_ci(const struct atm_vcc * vcc,short vpi,int vci)285 static int check_ci(const struct atm_vcc *vcc, short vpi, int vci)
286 {
287 	struct hlist_head *head = &vcc_hash[vci & (VCC_HTABLE_SIZE - 1)];
288 	struct hlist_node *node;
289 	struct sock *s;
290 	struct atm_vcc *walk;
291 
292 	sk_for_each(s, node, head) {
293 		walk = atm_sk(s);
294 		if (walk->dev != vcc->dev)
295 			continue;
296 		if (test_bit(ATM_VF_ADDR, &walk->flags) && walk->vpi == vpi &&
297 		    walk->vci == vci && ((walk->qos.txtp.traffic_class !=
298 		    ATM_NONE && vcc->qos.txtp.traffic_class != ATM_NONE) ||
299 		    (walk->qos.rxtp.traffic_class != ATM_NONE &&
300 		    vcc->qos.rxtp.traffic_class != ATM_NONE)))
301 			return -EADDRINUSE;
302 	}
303 
304 	/* allow VCCs with same VPI/VCI iff they don't collide on
305 	   TX/RX (but we may refuse such sharing for other reasons,
306 	   e.g. if protocol requires to have both channels) */
307 
308 	return 0;
309 }
310 
find_ci(const struct atm_vcc * vcc,short * vpi,int * vci)311 static int find_ci(const struct atm_vcc *vcc, short *vpi, int *vci)
312 {
313 	static short p;        /* poor man's per-device cache */
314 	static int c;
315 	short old_p;
316 	int old_c;
317 	int err;
318 
319 	if (*vpi != ATM_VPI_ANY && *vci != ATM_VCI_ANY) {
320 		err = check_ci(vcc, *vpi, *vci);
321 		return err;
322 	}
323 	/* last scan may have left values out of bounds for current device */
324 	if (*vpi != ATM_VPI_ANY)
325 		p = *vpi;
326 	else if (p >= 1 << vcc->dev->ci_range.vpi_bits)
327 		p = 0;
328 	if (*vci != ATM_VCI_ANY)
329 		c = *vci;
330 	else if (c < ATM_NOT_RSV_VCI || c >= 1 << vcc->dev->ci_range.vci_bits)
331 			c = ATM_NOT_RSV_VCI;
332 	old_p = p;
333 	old_c = c;
334 	do {
335 		if (!check_ci(vcc, p, c)) {
336 			*vpi = p;
337 			*vci = c;
338 			return 0;
339 		}
340 		if (*vci == ATM_VCI_ANY) {
341 			c++;
342 			if (c >= 1 << vcc->dev->ci_range.vci_bits)
343 				c = ATM_NOT_RSV_VCI;
344 		}
345 		if ((c == ATM_NOT_RSV_VCI || *vci != ATM_VCI_ANY) &&
346 		    *vpi == ATM_VPI_ANY) {
347 			p++;
348 			if (p >= 1 << vcc->dev->ci_range.vpi_bits)
349 				p = 0;
350 		}
351 	} while (old_p != p || old_c != c);
352 	return -EADDRINUSE;
353 }
354 
__vcc_connect(struct atm_vcc * vcc,struct atm_dev * dev,short vpi,int vci)355 static int __vcc_connect(struct atm_vcc *vcc, struct atm_dev *dev, short vpi,
356 			 int vci)
357 {
358 	struct sock *sk = sk_atm(vcc);
359 	int error;
360 
361 	if ((vpi != ATM_VPI_UNSPEC && vpi != ATM_VPI_ANY &&
362 	    vpi >> dev->ci_range.vpi_bits) || (vci != ATM_VCI_UNSPEC &&
363 	    vci != ATM_VCI_ANY && vci >> dev->ci_range.vci_bits))
364 		return -EINVAL;
365 	if (vci > 0 && vci < ATM_NOT_RSV_VCI && !capable(CAP_NET_BIND_SERVICE))
366 		return -EPERM;
367 	error = -ENODEV;
368 	if (!try_module_get(dev->ops->owner))
369 		return error;
370 	vcc->dev = dev;
371 	write_lock_irq(&vcc_sklist_lock);
372 	if (test_bit(ATM_DF_REMOVED, &dev->flags) ||
373 	    (error = find_ci(vcc, &vpi, &vci))) {
374 		write_unlock_irq(&vcc_sklist_lock);
375 		goto fail_module_put;
376 	}
377 	vcc->vpi = vpi;
378 	vcc->vci = vci;
379 	__vcc_insert_socket(sk);
380 	write_unlock_irq(&vcc_sklist_lock);
381 	switch (vcc->qos.aal) {
382 	case ATM_AAL0:
383 		error = atm_init_aal0(vcc);
384 		vcc->stats = &dev->stats.aal0;
385 		break;
386 	case ATM_AAL34:
387 		error = atm_init_aal34(vcc);
388 		vcc->stats = &dev->stats.aal34;
389 		break;
390 	case ATM_NO_AAL:
391 		/* ATM_AAL5 is also used in the "0 for default" case */
392 		vcc->qos.aal = ATM_AAL5;
393 		/* fall through */
394 	case ATM_AAL5:
395 		error = atm_init_aal5(vcc);
396 		vcc->stats = &dev->stats.aal5;
397 		break;
398 	default:
399 		error = -EPROTOTYPE;
400 	}
401 	if (!error)
402 		error = adjust_tp(&vcc->qos.txtp, vcc->qos.aal);
403 	if (!error)
404 		error = adjust_tp(&vcc->qos.rxtp, vcc->qos.aal);
405 	if (error)
406 		goto fail;
407 	pr_debug("VCC %d.%d, AAL %d\n", vpi, vci, vcc->qos.aal);
408 	pr_debug("  TX: %d, PCR %d..%d, SDU %d\n",
409 		 vcc->qos.txtp.traffic_class,
410 		 vcc->qos.txtp.min_pcr,
411 		 vcc->qos.txtp.max_pcr,
412 		 vcc->qos.txtp.max_sdu);
413 	pr_debug("  RX: %d, PCR %d..%d, SDU %d\n",
414 		 vcc->qos.rxtp.traffic_class,
415 		 vcc->qos.rxtp.min_pcr,
416 		 vcc->qos.rxtp.max_pcr,
417 		 vcc->qos.rxtp.max_sdu);
418 
419 	if (dev->ops->open) {
420 		error = dev->ops->open(vcc);
421 		if (error)
422 			goto fail;
423 	}
424 	return 0;
425 
426 fail:
427 	vcc_remove_socket(sk);
428 fail_module_put:
429 	module_put(dev->ops->owner);
430 	/* ensure we get dev module ref count correct */
431 	vcc->dev = NULL;
432 	return error;
433 }
434 
vcc_connect(struct socket * sock,int itf,short vpi,int vci)435 int vcc_connect(struct socket *sock, int itf, short vpi, int vci)
436 {
437 	struct atm_dev *dev;
438 	struct atm_vcc *vcc = ATM_SD(sock);
439 	int error;
440 
441 	pr_debug("(vpi %d, vci %d)\n", vpi, vci);
442 	if (sock->state == SS_CONNECTED)
443 		return -EISCONN;
444 	if (sock->state != SS_UNCONNECTED)
445 		return -EINVAL;
446 	if (!(vpi || vci))
447 		return -EINVAL;
448 
449 	if (vpi != ATM_VPI_UNSPEC && vci != ATM_VCI_UNSPEC)
450 		clear_bit(ATM_VF_PARTIAL, &vcc->flags);
451 	else
452 		if (test_bit(ATM_VF_PARTIAL, &vcc->flags))
453 			return -EINVAL;
454 	pr_debug("(TX: cl %d,bw %d-%d,sdu %d; "
455 		 "RX: cl %d,bw %d-%d,sdu %d,AAL %s%d)\n",
456 		 vcc->qos.txtp.traffic_class, vcc->qos.txtp.min_pcr,
457 		 vcc->qos.txtp.max_pcr, vcc->qos.txtp.max_sdu,
458 		 vcc->qos.rxtp.traffic_class, vcc->qos.rxtp.min_pcr,
459 		 vcc->qos.rxtp.max_pcr, vcc->qos.rxtp.max_sdu,
460 		 vcc->qos.aal == ATM_AAL5 ? "" :
461 		 vcc->qos.aal == ATM_AAL0 ? "" : " ??? code ",
462 		 vcc->qos.aal == ATM_AAL0 ? 0 : vcc->qos.aal);
463 	if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
464 		return -EBADFD;
465 	if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
466 	    vcc->qos.rxtp.traffic_class == ATM_ANYCLASS)
467 		return -EINVAL;
468 	if (likely(itf != ATM_ITF_ANY)) {
469 		dev = try_then_request_module(atm_dev_lookup(itf),
470 					      "atm-device-%d", itf);
471 	} else {
472 		dev = NULL;
473 		mutex_lock(&atm_dev_mutex);
474 		if (!list_empty(&atm_devs)) {
475 			dev = list_entry(atm_devs.next,
476 					 struct atm_dev, dev_list);
477 			atm_dev_hold(dev);
478 		}
479 		mutex_unlock(&atm_dev_mutex);
480 	}
481 	if (!dev)
482 		return -ENODEV;
483 	error = __vcc_connect(vcc, dev, vpi, vci);
484 	if (error) {
485 		atm_dev_put(dev);
486 		return error;
487 	}
488 	if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC)
489 		set_bit(ATM_VF_PARTIAL, &vcc->flags);
490 	if (test_bit(ATM_VF_READY, &ATM_SD(sock)->flags))
491 		sock->state = SS_CONNECTED;
492 	return 0;
493 }
494 
vcc_recvmsg(struct kiocb * iocb,struct socket * sock,struct msghdr * msg,size_t size,int flags)495 int vcc_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
496 		size_t size, int flags)
497 {
498 	struct sock *sk = sock->sk;
499 	struct atm_vcc *vcc;
500 	struct sk_buff *skb;
501 	int copied, error = -EINVAL;
502 
503 	if (sock->state != SS_CONNECTED)
504 		return -ENOTCONN;
505 	if (flags & ~MSG_DONTWAIT)		/* only handle MSG_DONTWAIT */
506 		return -EOPNOTSUPP;
507 	vcc = ATM_SD(sock);
508 	if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
509 	    test_bit(ATM_VF_CLOSE, &vcc->flags) ||
510 	    !test_bit(ATM_VF_READY, &vcc->flags))
511 		return 0;
512 
513 	skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &error);
514 	if (!skb)
515 		return error;
516 
517 	copied = skb->len;
518 	if (copied > size) {
519 		copied = size;
520 		msg->msg_flags |= MSG_TRUNC;
521 	}
522 
523 	error = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
524 	if (error)
525 		return error;
526 	sock_recv_ts_and_drops(msg, sk, skb);
527 	pr_debug("%d -= %d\n", atomic_read(&sk->sk_rmem_alloc), skb->truesize);
528 	atm_return(vcc, skb->truesize);
529 	skb_free_datagram(sk, skb);
530 	return copied;
531 }
532 
vcc_sendmsg(struct kiocb * iocb,struct socket * sock,struct msghdr * m,size_t total_len)533 int vcc_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
534 		size_t total_len)
535 {
536 	struct sock *sk = sock->sk;
537 	DEFINE_WAIT(wait);
538 	struct atm_vcc *vcc;
539 	struct sk_buff *skb;
540 	int eff, error;
541 	const void __user *buff;
542 	int size;
543 
544 	lock_sock(sk);
545 	if (sock->state != SS_CONNECTED) {
546 		error = -ENOTCONN;
547 		goto out;
548 	}
549 	if (m->msg_name) {
550 		error = -EISCONN;
551 		goto out;
552 	}
553 	if (m->msg_iovlen != 1) {
554 		error = -ENOSYS; /* fix this later @@@ */
555 		goto out;
556 	}
557 	buff = m->msg_iov->iov_base;
558 	size = m->msg_iov->iov_len;
559 	vcc = ATM_SD(sock);
560 	if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
561 	    test_bit(ATM_VF_CLOSE, &vcc->flags) ||
562 	    !test_bit(ATM_VF_READY, &vcc->flags)) {
563 		error = -EPIPE;
564 		send_sig(SIGPIPE, current, 0);
565 		goto out;
566 	}
567 	if (!size) {
568 		error = 0;
569 		goto out;
570 	}
571 	if (size < 0 || size > vcc->qos.txtp.max_sdu) {
572 		error = -EMSGSIZE;
573 		goto out;
574 	}
575 
576 	eff = (size+3) & ~3; /* align to word boundary */
577 	prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
578 	error = 0;
579 	while (!(skb = alloc_tx(vcc, eff))) {
580 		if (m->msg_flags & MSG_DONTWAIT) {
581 			error = -EAGAIN;
582 			break;
583 		}
584 		schedule();
585 		if (signal_pending(current)) {
586 			error = -ERESTARTSYS;
587 			break;
588 		}
589 		if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
590 		    test_bit(ATM_VF_CLOSE, &vcc->flags) ||
591 		    !test_bit(ATM_VF_READY, &vcc->flags)) {
592 			error = -EPIPE;
593 			send_sig(SIGPIPE, current, 0);
594 			break;
595 		}
596 		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
597 	}
598 	finish_wait(sk_sleep(sk), &wait);
599 	if (error)
600 		goto out;
601 	skb->dev = NULL; /* for paths shared with net_device interfaces */
602 	ATM_SKB(skb)->atm_options = vcc->atm_options;
603 	if (copy_from_user(skb_put(skb, size), buff, size)) {
604 		kfree_skb(skb);
605 		error = -EFAULT;
606 		goto out;
607 	}
608 	if (eff != size)
609 		memset(skb->data + size, 0, eff-size);
610 	error = vcc->dev->ops->send(vcc, skb);
611 	error = error ? error : size;
612 out:
613 	release_sock(sk);
614 	return error;
615 }
616 
vcc_poll(struct file * file,struct socket * sock,poll_table * wait)617 unsigned int vcc_poll(struct file *file, struct socket *sock, poll_table *wait)
618 {
619 	struct sock *sk = sock->sk;
620 	struct atm_vcc *vcc;
621 	unsigned int mask;
622 
623 	sock_poll_wait(file, sk_sleep(sk), wait);
624 	mask = 0;
625 
626 	vcc = ATM_SD(sock);
627 
628 	/* exceptional events */
629 	if (sk->sk_err)
630 		mask = POLLERR;
631 
632 	if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
633 	    test_bit(ATM_VF_CLOSE, &vcc->flags))
634 		mask |= POLLHUP;
635 
636 	/* readable? */
637 	if (!skb_queue_empty(&sk->sk_receive_queue))
638 		mask |= POLLIN | POLLRDNORM;
639 
640 	/* writable? */
641 	if (sock->state == SS_CONNECTING &&
642 	    test_bit(ATM_VF_WAITING, &vcc->flags))
643 		return mask;
644 
645 	if (vcc->qos.txtp.traffic_class != ATM_NONE &&
646 	    vcc_writable(sk))
647 		mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
648 
649 	return mask;
650 }
651 
atm_change_qos(struct atm_vcc * vcc,struct atm_qos * qos)652 static int atm_change_qos(struct atm_vcc *vcc, struct atm_qos *qos)
653 {
654 	int error;
655 
656 	/*
657 	 * Don't let the QoS change the already connected AAL type nor the
658 	 * traffic class.
659 	 */
660 	if (qos->aal != vcc->qos.aal ||
661 	    qos->rxtp.traffic_class != vcc->qos.rxtp.traffic_class ||
662 	    qos->txtp.traffic_class != vcc->qos.txtp.traffic_class)
663 		return -EINVAL;
664 	error = adjust_tp(&qos->txtp, qos->aal);
665 	if (!error)
666 		error = adjust_tp(&qos->rxtp, qos->aal);
667 	if (error)
668 		return error;
669 	if (!vcc->dev->ops->change_qos)
670 		return -EOPNOTSUPP;
671 	if (sk_atm(vcc)->sk_family == AF_ATMPVC)
672 		return vcc->dev->ops->change_qos(vcc, qos, ATM_MF_SET);
673 	return svc_change_qos(vcc, qos);
674 }
675 
check_tp(const struct atm_trafprm * tp)676 static int check_tp(const struct atm_trafprm *tp)
677 {
678 	/* @@@ Should be merged with adjust_tp */
679 	if (!tp->traffic_class || tp->traffic_class == ATM_ANYCLASS)
680 		return 0;
681 	if (tp->traffic_class != ATM_UBR && !tp->min_pcr && !tp->pcr &&
682 	    !tp->max_pcr)
683 		return -EINVAL;
684 	if (tp->min_pcr == ATM_MAX_PCR)
685 		return -EINVAL;
686 	if (tp->min_pcr && tp->max_pcr && tp->max_pcr != ATM_MAX_PCR &&
687 	    tp->min_pcr > tp->max_pcr)
688 		return -EINVAL;
689 	/*
690 	 * We allow pcr to be outside [min_pcr,max_pcr], because later
691 	 * adjustment may still push it in the valid range.
692 	 */
693 	return 0;
694 }
695 
check_qos(const struct atm_qos * qos)696 static int check_qos(const struct atm_qos *qos)
697 {
698 	int error;
699 
700 	if (!qos->txtp.traffic_class && !qos->rxtp.traffic_class)
701 		return -EINVAL;
702 	if (qos->txtp.traffic_class != qos->rxtp.traffic_class &&
703 	    qos->txtp.traffic_class && qos->rxtp.traffic_class &&
704 	    qos->txtp.traffic_class != ATM_ANYCLASS &&
705 	    qos->rxtp.traffic_class != ATM_ANYCLASS)
706 		return -EINVAL;
707 	error = check_tp(&qos->txtp);
708 	if (error)
709 		return error;
710 	return check_tp(&qos->rxtp);
711 }
712 
vcc_setsockopt(struct socket * sock,int level,int optname,char __user * optval,unsigned int optlen)713 int vcc_setsockopt(struct socket *sock, int level, int optname,
714 		   char __user *optval, unsigned int optlen)
715 {
716 	struct atm_vcc *vcc;
717 	unsigned long value;
718 	int error;
719 
720 	if (__SO_LEVEL_MATCH(optname, level) && optlen != __SO_SIZE(optname))
721 		return -EINVAL;
722 
723 	vcc = ATM_SD(sock);
724 	switch (optname) {
725 	case SO_ATMQOS:
726 	{
727 		struct atm_qos qos;
728 
729 		if (copy_from_user(&qos, optval, sizeof(qos)))
730 			return -EFAULT;
731 		error = check_qos(&qos);
732 		if (error)
733 			return error;
734 		if (sock->state == SS_CONNECTED)
735 			return atm_change_qos(vcc, &qos);
736 		if (sock->state != SS_UNCONNECTED)
737 			return -EBADFD;
738 		vcc->qos = qos;
739 		set_bit(ATM_VF_HASQOS, &vcc->flags);
740 		return 0;
741 	}
742 	case SO_SETCLP:
743 		if (get_user(value, (unsigned long __user *)optval))
744 			return -EFAULT;
745 		if (value)
746 			vcc->atm_options |= ATM_ATMOPT_CLP;
747 		else
748 			vcc->atm_options &= ~ATM_ATMOPT_CLP;
749 		return 0;
750 	default:
751 		if (level == SOL_SOCKET)
752 			return -EINVAL;
753 		break;
754 	}
755 	if (!vcc->dev || !vcc->dev->ops->setsockopt)
756 		return -EINVAL;
757 	return vcc->dev->ops->setsockopt(vcc, level, optname, optval, optlen);
758 }
759 
vcc_getsockopt(struct socket * sock,int level,int optname,char __user * optval,int __user * optlen)760 int vcc_getsockopt(struct socket *sock, int level, int optname,
761 		   char __user *optval, int __user *optlen)
762 {
763 	struct atm_vcc *vcc;
764 	int len;
765 
766 	if (get_user(len, optlen))
767 		return -EFAULT;
768 	if (__SO_LEVEL_MATCH(optname, level) && len != __SO_SIZE(optname))
769 		return -EINVAL;
770 
771 	vcc = ATM_SD(sock);
772 	switch (optname) {
773 	case SO_ATMQOS:
774 		if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
775 			return -EINVAL;
776 		return copy_to_user(optval, &vcc->qos, sizeof(vcc->qos))
777 			? -EFAULT : 0;
778 	case SO_SETCLP:
779 		return put_user(vcc->atm_options & ATM_ATMOPT_CLP ? 1 : 0,
780 				(unsigned long __user *)optval) ? -EFAULT : 0;
781 	case SO_ATMPVC:
782 	{
783 		struct sockaddr_atmpvc pvc;
784 
785 		if (!vcc->dev || !test_bit(ATM_VF_ADDR, &vcc->flags))
786 			return -ENOTCONN;
787 		pvc.sap_family = AF_ATMPVC;
788 		pvc.sap_addr.itf = vcc->dev->number;
789 		pvc.sap_addr.vpi = vcc->vpi;
790 		pvc.sap_addr.vci = vcc->vci;
791 		return copy_to_user(optval, &pvc, sizeof(pvc)) ? -EFAULT : 0;
792 	}
793 	default:
794 		if (level == SOL_SOCKET)
795 			return -EINVAL;
796 		break;
797 	}
798 	if (!vcc->dev || !vcc->dev->ops->getsockopt)
799 		return -EINVAL;
800 	return vcc->dev->ops->getsockopt(vcc, level, optname, optval, len);
801 }
802 
register_atmdevice_notifier(struct notifier_block * nb)803 int register_atmdevice_notifier(struct notifier_block *nb)
804 {
805 	return atomic_notifier_chain_register(&atm_dev_notify_chain, nb);
806 }
807 EXPORT_SYMBOL_GPL(register_atmdevice_notifier);
808 
unregister_atmdevice_notifier(struct notifier_block * nb)809 void unregister_atmdevice_notifier(struct notifier_block *nb)
810 {
811 	atomic_notifier_chain_unregister(&atm_dev_notify_chain, nb);
812 }
813 EXPORT_SYMBOL_GPL(unregister_atmdevice_notifier);
814 
atm_init(void)815 static int __init atm_init(void)
816 {
817 	int error;
818 
819 	error = proto_register(&vcc_proto, 0);
820 	if (error < 0)
821 		goto out;
822 	error = atmpvc_init();
823 	if (error < 0) {
824 		pr_err("atmpvc_init() failed with %d\n", error);
825 		goto out_unregister_vcc_proto;
826 	}
827 	error = atmsvc_init();
828 	if (error < 0) {
829 		pr_err("atmsvc_init() failed with %d\n", error);
830 		goto out_atmpvc_exit;
831 	}
832 	error = atm_proc_init();
833 	if (error < 0) {
834 		pr_err("atm_proc_init() failed with %d\n", error);
835 		goto out_atmsvc_exit;
836 	}
837 	error = atm_sysfs_init();
838 	if (error < 0) {
839 		pr_err("atm_sysfs_init() failed with %d\n", error);
840 		goto out_atmproc_exit;
841 	}
842 out:
843 	return error;
844 out_atmproc_exit:
845 	atm_proc_exit();
846 out_atmsvc_exit:
847 	atmsvc_exit();
848 out_atmpvc_exit:
849 	atmsvc_exit();
850 out_unregister_vcc_proto:
851 	proto_unregister(&vcc_proto);
852 	goto out;
853 }
854 
atm_exit(void)855 static void __exit atm_exit(void)
856 {
857 	atm_proc_exit();
858 	atm_sysfs_exit();
859 	atmsvc_exit();
860 	atmpvc_exit();
861 	proto_unregister(&vcc_proto);
862 }
863 
864 subsys_initcall(atm_init);
865 
866 module_exit(atm_exit);
867 
868 MODULE_LICENSE("GPL");
869 MODULE_ALIAS_NETPROTO(PF_ATMPVC);
870 MODULE_ALIAS_NETPROTO(PF_ATMSVC);
871