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