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