xref: /openbmc/linux/net/atm/common.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /* net/atm/common.c - ATM sockets (common part for PVC and SVC) */
31da177e4SLinus Torvalds 
41da177e4SLinus Torvalds /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
51da177e4SLinus Torvalds 
699824461SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include <linux/module.h>
91da177e4SLinus Torvalds #include <linux/kmod.h>
101da177e4SLinus Torvalds #include <linux/net.h>		/* struct socket, struct proto_ops */
111da177e4SLinus Torvalds #include <linux/atm.h>		/* ATM stuff */
121da177e4SLinus Torvalds #include <linux/atmdev.h>
131da177e4SLinus Torvalds #include <linux/socket.h>	/* SOL_SOCKET */
141da177e4SLinus Torvalds #include <linux/errno.h>	/* error codes */
151da177e4SLinus Torvalds #include <linux/capability.h>
16e49332bdSJesper Juhl #include <linux/mm.h>
173f07c014SIngo Molnar #include <linux/sched/signal.h>
18d750dbdcSTina Ruchandani #include <linux/time64.h>	/* 64-bit time for seconds */
191da177e4SLinus Torvalds #include <linux/skbuff.h>
201da177e4SLinus Torvalds #include <linux/bitops.h>
211da177e4SLinus Torvalds #include <linux/init.h>
225a0e3ad6STejun Heo #include <linux/slab.h>
231da177e4SLinus Torvalds #include <net/sock.h>		/* struct sock */
24a8147d73SJoe Perches #include <linux/uaccess.h>
25a8147d73SJoe Perches #include <linux/poll.h>
261da177e4SLinus Torvalds 
2760063497SArun Sharma #include <linux/atomic.h>
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #include "resources.h"		/* atm_find_dev */
301da177e4SLinus Torvalds #include "common.h"		/* prototypes */
311da177e4SLinus Torvalds #include "protocols.h"		/* atm_init_<transport> */
321da177e4SLinus Torvalds #include "addr.h"		/* address registry */
331da177e4SLinus Torvalds #include "signaling.h"		/* for WAITING and sigd_attach */
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds struct hlist_head vcc_hash[VCC_HTABLE_SIZE];
36a8147d73SJoe Perches EXPORT_SYMBOL(vcc_hash);
37a8147d73SJoe Perches 
381da177e4SLinus Torvalds DEFINE_RWLOCK(vcc_sklist_lock);
39a8147d73SJoe Perches EXPORT_SYMBOL(vcc_sklist_lock);
401da177e4SLinus Torvalds 
417313bb8fSKarl Hiramoto static ATOMIC_NOTIFIER_HEAD(atm_dev_notify_chain);
427313bb8fSKarl Hiramoto 
__vcc_insert_socket(struct sock * sk)431da177e4SLinus Torvalds static void __vcc_insert_socket(struct sock *sk)
441da177e4SLinus Torvalds {
451da177e4SLinus Torvalds 	struct atm_vcc *vcc = atm_sk(sk);
46a8147d73SJoe Perches 	struct hlist_head *head = &vcc_hash[vcc->vci & (VCC_HTABLE_SIZE - 1)];
4781c3d547SEric Dumazet 	sk->sk_hash = vcc->vci & (VCC_HTABLE_SIZE - 1);
481da177e4SLinus Torvalds 	sk_add_node(sk, head);
491da177e4SLinus Torvalds }
501da177e4SLinus Torvalds 
vcc_insert_socket(struct sock * sk)511da177e4SLinus Torvalds void vcc_insert_socket(struct sock *sk)
521da177e4SLinus Torvalds {
531da177e4SLinus Torvalds 	write_lock_irq(&vcc_sklist_lock);
541da177e4SLinus Torvalds 	__vcc_insert_socket(sk);
551da177e4SLinus Torvalds 	write_unlock_irq(&vcc_sklist_lock);
561da177e4SLinus Torvalds }
57a8147d73SJoe Perches EXPORT_SYMBOL(vcc_insert_socket);
581da177e4SLinus Torvalds 
vcc_remove_socket(struct sock * sk)591da177e4SLinus Torvalds static void vcc_remove_socket(struct sock *sk)
601da177e4SLinus Torvalds {
611da177e4SLinus Torvalds 	write_lock_irq(&vcc_sklist_lock);
621da177e4SLinus Torvalds 	sk_del_node_init(sk);
631da177e4SLinus Torvalds 	write_unlock_irq(&vcc_sklist_lock);
641da177e4SLinus Torvalds }
651da177e4SLinus Torvalds 
vcc_tx_ready(struct atm_vcc * vcc,unsigned int size)66c55fa3ccSFrancois Romieu static bool vcc_tx_ready(struct atm_vcc *vcc, unsigned int size)
671da177e4SLinus Torvalds {
681da177e4SLinus Torvalds 	struct sock *sk = sk_atm(vcc);
691da177e4SLinus Torvalds 
7081e2a3d5SEric Dumazet 	if (sk_wmem_alloc_get(sk) && !atm_may_send(vcc, size)) {
7152240062SStephen Hemminger 		pr_debug("Sorry: wmem_alloc = %d, size = %d, sndbuf = %d\n",
7299824461SJoe Perches 			 sk_wmem_alloc_get(sk), size, sk->sk_sndbuf);
73c55fa3ccSFrancois Romieu 		return false;
741da177e4SLinus Torvalds 	}
75c55fa3ccSFrancois Romieu 	return true;
761da177e4SLinus Torvalds }
771da177e4SLinus Torvalds 
vcc_sock_destruct(struct sock * sk)781da177e4SLinus Torvalds static void vcc_sock_destruct(struct sock *sk)
791da177e4SLinus Torvalds {
801da177e4SLinus Torvalds 	if (atomic_read(&sk->sk_rmem_alloc))
81a8147d73SJoe Perches 		printk(KERN_DEBUG "%s: rmem leakage (%d bytes) detected.\n",
82a8147d73SJoe Perches 		       __func__, atomic_read(&sk->sk_rmem_alloc));
831da177e4SLinus Torvalds 
8414afee4bSReshetova, Elena 	if (refcount_read(&sk->sk_wmem_alloc))
85a8147d73SJoe Perches 		printk(KERN_DEBUG "%s: wmem leakage (%d bytes) detected.\n",
8614afee4bSReshetova, Elena 		       __func__, refcount_read(&sk->sk_wmem_alloc));
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds 
vcc_def_wakeup(struct sock * sk)891da177e4SLinus Torvalds static void vcc_def_wakeup(struct sock *sk)
901da177e4SLinus Torvalds {
9143815482SEric Dumazet 	struct socket_wq *wq;
9243815482SEric Dumazet 
9343815482SEric Dumazet 	rcu_read_lock();
9443815482SEric Dumazet 	wq = rcu_dereference(sk->sk_wq);
951ce0bf50SHerbert Xu 	if (skwq_has_sleeper(wq))
9643815482SEric Dumazet 		wake_up(&wq->wait);
9743815482SEric Dumazet 	rcu_read_unlock();
981da177e4SLinus Torvalds }
991da177e4SLinus Torvalds 
vcc_writable(struct sock * sk)1001da177e4SLinus Torvalds static inline int vcc_writable(struct sock *sk)
1011da177e4SLinus Torvalds {
1021da177e4SLinus Torvalds 	struct atm_vcc *vcc = atm_sk(sk);
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 	return (vcc->qos.txtp.max_sdu +
10514afee4bSReshetova, Elena 		refcount_read(&sk->sk_wmem_alloc)) <= sk->sk_sndbuf;
1061da177e4SLinus Torvalds }
1071da177e4SLinus Torvalds 
vcc_write_space(struct sock * sk)1081da177e4SLinus Torvalds static void vcc_write_space(struct sock *sk)
1091da177e4SLinus Torvalds {
11043815482SEric Dumazet 	struct socket_wq *wq;
11143815482SEric Dumazet 
11243815482SEric Dumazet 	rcu_read_lock();
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds 	if (vcc_writable(sk)) {
11543815482SEric Dumazet 		wq = rcu_dereference(sk->sk_wq);
1161ce0bf50SHerbert Xu 		if (skwq_has_sleeper(wq))
11743815482SEric Dumazet 			wake_up_interruptible(&wq->wait);
1181da177e4SLinus Torvalds 
1198d8ad9d7SPavel Emelyanov 		sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
1201da177e4SLinus Torvalds 	}
1211da177e4SLinus Torvalds 
12243815482SEric Dumazet 	rcu_read_unlock();
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds 
vcc_release_cb(struct sock * sk)125c971f08cSDavid Woodhouse static void vcc_release_cb(struct sock *sk)
126c971f08cSDavid Woodhouse {
127c971f08cSDavid Woodhouse 	struct atm_vcc *vcc = atm_sk(sk);
128c971f08cSDavid Woodhouse 
129c971f08cSDavid Woodhouse 	if (vcc->release_cb)
130c971f08cSDavid Woodhouse 		vcc->release_cb(vcc);
131c971f08cSDavid Woodhouse }
132c971f08cSDavid Woodhouse 
1331da177e4SLinus Torvalds static struct proto vcc_proto = {
1341da177e4SLinus Torvalds 	.name	  = "VCC",
1351da177e4SLinus Torvalds 	.owner	  = THIS_MODULE,
1361da177e4SLinus Torvalds 	.obj_size = sizeof(struct atm_vcc),
137c971f08cSDavid Woodhouse 	.release_cb = vcc_release_cb,
1381da177e4SLinus Torvalds };
1391da177e4SLinus Torvalds 
vcc_create(struct net * net,struct socket * sock,int protocol,int family,int kern)14011aa9c28SEric W. Biederman int vcc_create(struct net *net, struct socket *sock, int protocol, int family, int kern)
1411da177e4SLinus Torvalds {
1421da177e4SLinus Torvalds 	struct sock *sk;
1431da177e4SLinus Torvalds 	struct atm_vcc *vcc;
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	sock->sk = NULL;
1461da177e4SLinus Torvalds 	if (sock->type == SOCK_STREAM)
1471da177e4SLinus Torvalds 		return -EINVAL;
14811aa9c28SEric W. Biederman 	sk = sk_alloc(net, family, GFP_KERNEL, &vcc_proto, kern);
1491da177e4SLinus Torvalds 	if (!sk)
1501da177e4SLinus Torvalds 		return -ENOMEM;
1511da177e4SLinus Torvalds 	sock_init_data(sock, sk);
1521da177e4SLinus Torvalds 	sk->sk_state_change = vcc_def_wakeup;
1531da177e4SLinus Torvalds 	sk->sk_write_space = vcc_write_space;
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	vcc = atm_sk(sk);
1561da177e4SLinus Torvalds 	vcc->dev = NULL;
1571da177e4SLinus Torvalds 	memset(&vcc->local, 0, sizeof(struct sockaddr_atmsvc));
1581da177e4SLinus Torvalds 	memset(&vcc->remote, 0, sizeof(struct sockaddr_atmsvc));
1591da177e4SLinus Torvalds 	vcc->qos.txtp.max_sdu = 1 << 16; /* for meta VCs */
16014afee4bSReshetova, Elena 	refcount_set(&sk->sk_wmem_alloc, 1);
1611da177e4SLinus Torvalds 	atomic_set(&sk->sk_rmem_alloc, 0);
1621da177e4SLinus Torvalds 	vcc->push = NULL;
1631da177e4SLinus Torvalds 	vcc->pop = NULL;
164ec809bd8SKrzysztof Mazur 	vcc->owner = NULL;
1651da177e4SLinus Torvalds 	vcc->push_oam = NULL;
166c971f08cSDavid Woodhouse 	vcc->release_cb = NULL;
1671da177e4SLinus Torvalds 	vcc->vpi = vcc->vci = 0; /* no VCI/VPI yet */
1681da177e4SLinus Torvalds 	vcc->atm_options = vcc->aal_options = 0;
1691da177e4SLinus Torvalds 	sk->sk_destruct = vcc_sock_destruct;
1701da177e4SLinus Torvalds 	return 0;
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds 
vcc_destroy_socket(struct sock * sk)1731da177e4SLinus Torvalds static void vcc_destroy_socket(struct sock *sk)
1741da177e4SLinus Torvalds {
1751da177e4SLinus Torvalds 	struct atm_vcc *vcc = atm_sk(sk);
1761da177e4SLinus Torvalds 	struct sk_buff *skb;
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	set_bit(ATM_VF_CLOSE, &vcc->flags);
1791da177e4SLinus Torvalds 	clear_bit(ATM_VF_READY, &vcc->flags);
18093a2014aSCong Wang 	if (vcc->dev && vcc->dev->ops->close)
1811da177e4SLinus Torvalds 		vcc->dev->ops->close(vcc);
1821da177e4SLinus Torvalds 	if (vcc->push)
1831da177e4SLinus Torvalds 		vcc->push(vcc, NULL); /* atmarpd has no push */
184ec809bd8SKrzysztof Mazur 	module_put(vcc->owner);
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds 	while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
1871da177e4SLinus Torvalds 		atm_return(vcc, skb->truesize);
1881da177e4SLinus Torvalds 		kfree_skb(skb);
1891da177e4SLinus Torvalds 	}
1901da177e4SLinus Torvalds 
19193a2014aSCong Wang 	if (vcc->dev && vcc->dev->ops->owner) {
1921da177e4SLinus Torvalds 		module_put(vcc->dev->ops->owner);
1931da177e4SLinus Torvalds 		atm_dev_put(vcc->dev);
1941da177e4SLinus Torvalds 	}
1959301e320SChas Williams 
1969301e320SChas Williams 	vcc_remove_socket(sk);
1971da177e4SLinus Torvalds }
1981da177e4SLinus Torvalds 
vcc_release(struct socket * sock)1991da177e4SLinus Torvalds int vcc_release(struct socket *sock)
2001da177e4SLinus Torvalds {
2011da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 	if (sk) {
2041da177e4SLinus Torvalds 		lock_sock(sk);
2051da177e4SLinus Torvalds 		vcc_destroy_socket(sock->sk);
2061da177e4SLinus Torvalds 		release_sock(sk);
2071da177e4SLinus Torvalds 		sock_put(sk);
2081da177e4SLinus Torvalds 	}
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	return 0;
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds 
vcc_release_async(struct atm_vcc * vcc,int reply)2131da177e4SLinus Torvalds void vcc_release_async(struct atm_vcc *vcc, int reply)
2141da177e4SLinus Torvalds {
2151da177e4SLinus Torvalds 	struct sock *sk = sk_atm(vcc);
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	set_bit(ATM_VF_CLOSE, &vcc->flags);
2181da177e4SLinus Torvalds 	sk->sk_shutdown |= RCV_SHUTDOWN;
2191da177e4SLinus Torvalds 	sk->sk_err = -reply;
2201da177e4SLinus Torvalds 	clear_bit(ATM_VF_WAITING, &vcc->flags);
2211da177e4SLinus Torvalds 	sk->sk_state_change(sk);
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds EXPORT_SYMBOL(vcc_release_async);
2241da177e4SLinus Torvalds 
vcc_process_recv_queue(struct atm_vcc * vcc)2254e55f578SJorge Boncompte [DTI2] void vcc_process_recv_queue(struct atm_vcc *vcc)
2264e55f578SJorge Boncompte [DTI2] {
2274e55f578SJorge Boncompte [DTI2] 	struct sk_buff_head queue, *rq;
2284e55f578SJorge Boncompte [DTI2] 	struct sk_buff *skb, *tmp;
2294e55f578SJorge Boncompte [DTI2] 	unsigned long flags;
2304e55f578SJorge Boncompte [DTI2] 
2314e55f578SJorge Boncompte [DTI2] 	__skb_queue_head_init(&queue);
2324e55f578SJorge Boncompte [DTI2] 	rq = &sk_atm(vcc)->sk_receive_queue;
2334e55f578SJorge Boncompte [DTI2] 
2344e55f578SJorge Boncompte [DTI2] 	spin_lock_irqsave(&rq->lock, flags);
2354e55f578SJorge Boncompte [DTI2] 	skb_queue_splice_init(rq, &queue);
2364e55f578SJorge Boncompte [DTI2] 	spin_unlock_irqrestore(&rq->lock, flags);
2374e55f578SJorge Boncompte [DTI2] 
2384e55f578SJorge Boncompte [DTI2] 	skb_queue_walk_safe(&queue, skb, tmp) {
2394e55f578SJorge Boncompte [DTI2] 		__skb_unlink(skb, &queue);
2404e55f578SJorge Boncompte [DTI2] 		vcc->push(vcc, skb);
2414e55f578SJorge Boncompte [DTI2] 	}
2424e55f578SJorge Boncompte [DTI2] }
2434e55f578SJorge Boncompte [DTI2] EXPORT_SYMBOL(vcc_process_recv_queue);
2444e55f578SJorge Boncompte [DTI2] 
atm_dev_signal_change(struct atm_dev * dev,char signal)2457313bb8fSKarl Hiramoto void atm_dev_signal_change(struct atm_dev *dev, char signal)
2467313bb8fSKarl Hiramoto {
2477313bb8fSKarl Hiramoto 	pr_debug("%s signal=%d dev=%p number=%d dev->signal=%d\n",
2487313bb8fSKarl Hiramoto 		__func__, signal, dev, dev->number, dev->signal);
2497313bb8fSKarl Hiramoto 
2507313bb8fSKarl Hiramoto 	/* atm driver sending invalid signal */
2517313bb8fSKarl Hiramoto 	WARN_ON(signal < ATM_PHY_SIG_LOST || signal > ATM_PHY_SIG_FOUND);
2527313bb8fSKarl Hiramoto 
2537313bb8fSKarl Hiramoto 	if (dev->signal == signal)
2547313bb8fSKarl Hiramoto 		return; /* no change */
2557313bb8fSKarl Hiramoto 
2567313bb8fSKarl Hiramoto 	dev->signal = signal;
2577313bb8fSKarl Hiramoto 
2587313bb8fSKarl Hiramoto 	atomic_notifier_call_chain(&atm_dev_notify_chain, signal, dev);
2597313bb8fSKarl Hiramoto }
2607313bb8fSKarl Hiramoto EXPORT_SYMBOL(atm_dev_signal_change);
2611da177e4SLinus Torvalds 
atm_dev_release_vccs(struct atm_dev * dev)26264bf69ddSStanislaw Gruszka void atm_dev_release_vccs(struct atm_dev *dev)
26364bf69ddSStanislaw Gruszka {
26464bf69ddSStanislaw Gruszka 	int i;
26564bf69ddSStanislaw Gruszka 
26664bf69ddSStanislaw Gruszka 	write_lock_irq(&vcc_sklist_lock);
26764bf69ddSStanislaw Gruszka 	for (i = 0; i < VCC_HTABLE_SIZE; i++) {
26864bf69ddSStanislaw Gruszka 		struct hlist_head *head = &vcc_hash[i];
269b67bfe0dSSasha Levin 		struct hlist_node *tmp;
27064bf69ddSStanislaw Gruszka 		struct sock *s;
27164bf69ddSStanislaw Gruszka 		struct atm_vcc *vcc;
27264bf69ddSStanislaw Gruszka 
273b67bfe0dSSasha Levin 		sk_for_each_safe(s, tmp, head) {
27464bf69ddSStanislaw Gruszka 			vcc = atm_sk(s);
27564bf69ddSStanislaw Gruszka 			if (vcc->dev == dev) {
27664bf69ddSStanislaw Gruszka 				vcc_release_async(vcc, -EPIPE);
27764bf69ddSStanislaw Gruszka 				sk_del_node_init(s);
27864bf69ddSStanislaw Gruszka 			}
27964bf69ddSStanislaw Gruszka 		}
28064bf69ddSStanislaw Gruszka 	}
28164bf69ddSStanislaw Gruszka 	write_unlock_irq(&vcc_sklist_lock);
28264bf69ddSStanislaw Gruszka }
283c031235bSPhilip A. Prindeville EXPORT_SYMBOL(atm_dev_release_vccs);
28464bf69ddSStanislaw Gruszka 
adjust_tp(struct atm_trafprm * tp,unsigned char aal)2851da177e4SLinus Torvalds static int adjust_tp(struct atm_trafprm *tp, unsigned char aal)
2861da177e4SLinus Torvalds {
2871da177e4SLinus Torvalds 	int max_sdu;
2881da177e4SLinus Torvalds 
289a8147d73SJoe Perches 	if (!tp->traffic_class)
290a8147d73SJoe Perches 		return 0;
2911da177e4SLinus Torvalds 	switch (aal) {
2921da177e4SLinus Torvalds 	case ATM_AAL0:
2931da177e4SLinus Torvalds 		max_sdu = ATM_CELL_SIZE-1;
2941da177e4SLinus Torvalds 		break;
2951da177e4SLinus Torvalds 	case ATM_AAL34:
2961da177e4SLinus Torvalds 		max_sdu = ATM_MAX_AAL34_PDU;
2971da177e4SLinus Torvalds 		break;
2981da177e4SLinus Torvalds 	default:
299ef423a41SJoe Perches 		pr_warn("AAL problems ... (%d)\n", aal);
300df561f66SGustavo A. R. Silva 		fallthrough;
3011da177e4SLinus Torvalds 	case ATM_AAL5:
3021da177e4SLinus Torvalds 		max_sdu = ATM_MAX_AAL5_PDU;
3031da177e4SLinus Torvalds 	}
304a8147d73SJoe Perches 	if (!tp->max_sdu)
305a8147d73SJoe Perches 		tp->max_sdu = max_sdu;
306a8147d73SJoe Perches 	else if (tp->max_sdu > max_sdu)
307a8147d73SJoe Perches 		return -EINVAL;
308a8147d73SJoe Perches 	if (!tp->max_cdv)
309a8147d73SJoe Perches 		tp->max_cdv = ATM_MAX_CDV;
3101da177e4SLinus Torvalds 	return 0;
3111da177e4SLinus Torvalds }
3121da177e4SLinus Torvalds 
check_ci(const struct atm_vcc * vcc,short vpi,int vci)31361c33e01SMitchell Blank Jr static int check_ci(const struct atm_vcc *vcc, short vpi, int vci)
3141da177e4SLinus Torvalds {
315a8147d73SJoe Perches 	struct hlist_head *head = &vcc_hash[vci & (VCC_HTABLE_SIZE - 1)];
3161da177e4SLinus Torvalds 	struct sock *s;
3171da177e4SLinus Torvalds 	struct atm_vcc *walk;
3181da177e4SLinus Torvalds 
319b67bfe0dSSasha Levin 	sk_for_each(s, head) {
3201da177e4SLinus Torvalds 		walk = atm_sk(s);
3211da177e4SLinus Torvalds 		if (walk->dev != vcc->dev)
3221da177e4SLinus Torvalds 			continue;
3231da177e4SLinus Torvalds 		if (test_bit(ATM_VF_ADDR, &walk->flags) && walk->vpi == vpi &&
3241da177e4SLinus Torvalds 		    walk->vci == vci && ((walk->qos.txtp.traffic_class !=
3251da177e4SLinus Torvalds 		    ATM_NONE && vcc->qos.txtp.traffic_class != ATM_NONE) ||
3261da177e4SLinus Torvalds 		    (walk->qos.rxtp.traffic_class != ATM_NONE &&
3271da177e4SLinus Torvalds 		    vcc->qos.rxtp.traffic_class != ATM_NONE)))
3281da177e4SLinus Torvalds 			return -EADDRINUSE;
3291da177e4SLinus Torvalds 	}
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	/* allow VCCs with same VPI/VCI iff they don't collide on
3321da177e4SLinus Torvalds 	   TX/RX (but we may refuse such sharing for other reasons,
3331da177e4SLinus Torvalds 	   e.g. if protocol requires to have both channels) */
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	return 0;
3361da177e4SLinus Torvalds }
3371da177e4SLinus Torvalds 
find_ci(const struct atm_vcc * vcc,short * vpi,int * vci)33861c33e01SMitchell Blank Jr static int find_ci(const struct atm_vcc *vcc, short *vpi, int *vci)
3391da177e4SLinus Torvalds {
3401da177e4SLinus Torvalds 	static short p;        /* poor man's per-device cache */
3411da177e4SLinus Torvalds 	static int c;
3421da177e4SLinus Torvalds 	short old_p;
3431da177e4SLinus Torvalds 	int old_c;
3441da177e4SLinus Torvalds 	int err;
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	if (*vpi != ATM_VPI_ANY && *vci != ATM_VCI_ANY) {
3471da177e4SLinus Torvalds 		err = check_ci(vcc, *vpi, *vci);
3481da177e4SLinus Torvalds 		return err;
3491da177e4SLinus Torvalds 	}
3501da177e4SLinus Torvalds 	/* last scan may have left values out of bounds for current device */
3511da177e4SLinus Torvalds 	if (*vpi != ATM_VPI_ANY)
3521da177e4SLinus Torvalds 		p = *vpi;
3531da177e4SLinus Torvalds 	else if (p >= 1 << vcc->dev->ci_range.vpi_bits)
3541da177e4SLinus Torvalds 		p = 0;
3551da177e4SLinus Torvalds 	if (*vci != ATM_VCI_ANY)
3561da177e4SLinus Torvalds 		c = *vci;
3571da177e4SLinus Torvalds 	else if (c < ATM_NOT_RSV_VCI || c >= 1 << vcc->dev->ci_range.vci_bits)
3581da177e4SLinus Torvalds 			c = ATM_NOT_RSV_VCI;
3591da177e4SLinus Torvalds 	old_p = p;
3601da177e4SLinus Torvalds 	old_c = c;
3611da177e4SLinus Torvalds 	do {
3621da177e4SLinus Torvalds 		if (!check_ci(vcc, p, c)) {
3631da177e4SLinus Torvalds 			*vpi = p;
3641da177e4SLinus Torvalds 			*vci = c;
3651da177e4SLinus Torvalds 			return 0;
3661da177e4SLinus Torvalds 		}
3671da177e4SLinus Torvalds 		if (*vci == ATM_VCI_ANY) {
3681da177e4SLinus Torvalds 			c++;
3691da177e4SLinus Torvalds 			if (c >= 1 << vcc->dev->ci_range.vci_bits)
3701da177e4SLinus Torvalds 				c = ATM_NOT_RSV_VCI;
3711da177e4SLinus Torvalds 		}
3721da177e4SLinus Torvalds 		if ((c == ATM_NOT_RSV_VCI || *vci != ATM_VCI_ANY) &&
3731da177e4SLinus Torvalds 		    *vpi == ATM_VPI_ANY) {
3741da177e4SLinus Torvalds 			p++;
375a8147d73SJoe Perches 			if (p >= 1 << vcc->dev->ci_range.vpi_bits)
376a8147d73SJoe Perches 				p = 0;
3771da177e4SLinus Torvalds 		}
378a8147d73SJoe Perches 	} while (old_p != p || old_c != c);
3791da177e4SLinus Torvalds 	return -EADDRINUSE;
3801da177e4SLinus Torvalds }
3811da177e4SLinus Torvalds 
__vcc_connect(struct atm_vcc * vcc,struct atm_dev * dev,short vpi,int vci)3821da177e4SLinus Torvalds static int __vcc_connect(struct atm_vcc *vcc, struct atm_dev *dev, short vpi,
3831da177e4SLinus Torvalds 			 int vci)
3841da177e4SLinus Torvalds {
3851da177e4SLinus Torvalds 	struct sock *sk = sk_atm(vcc);
3861da177e4SLinus Torvalds 	int error;
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 	if ((vpi != ATM_VPI_UNSPEC && vpi != ATM_VPI_ANY &&
3891da177e4SLinus Torvalds 	    vpi >> dev->ci_range.vpi_bits) || (vci != ATM_VCI_UNSPEC &&
3901da177e4SLinus Torvalds 	    vci != ATM_VCI_ANY && vci >> dev->ci_range.vci_bits))
3911da177e4SLinus Torvalds 		return -EINVAL;
3921da177e4SLinus Torvalds 	if (vci > 0 && vci < ATM_NOT_RSV_VCI && !capable(CAP_NET_BIND_SERVICE))
3931da177e4SLinus Torvalds 		return -EPERM;
39464bf69ddSStanislaw Gruszka 	error = -ENODEV;
3951da177e4SLinus Torvalds 	if (!try_module_get(dev->ops->owner))
39664bf69ddSStanislaw Gruszka 		return error;
3971da177e4SLinus Torvalds 	vcc->dev = dev;
3981da177e4SLinus Torvalds 	write_lock_irq(&vcc_sklist_lock);
39964bf69ddSStanislaw Gruszka 	if (test_bit(ATM_DF_REMOVED, &dev->flags) ||
40064bf69ddSStanislaw Gruszka 	    (error = find_ci(vcc, &vpi, &vci))) {
4011da177e4SLinus Torvalds 		write_unlock_irq(&vcc_sklist_lock);
4021da177e4SLinus Torvalds 		goto fail_module_put;
4031da177e4SLinus Torvalds 	}
4041da177e4SLinus Torvalds 	vcc->vpi = vpi;
4051da177e4SLinus Torvalds 	vcc->vci = vci;
4061da177e4SLinus Torvalds 	__vcc_insert_socket(sk);
4071da177e4SLinus Torvalds 	write_unlock_irq(&vcc_sklist_lock);
4081da177e4SLinus Torvalds 	switch (vcc->qos.aal) {
4091da177e4SLinus Torvalds 	case ATM_AAL0:
4101da177e4SLinus Torvalds 		error = atm_init_aal0(vcc);
4111da177e4SLinus Torvalds 		vcc->stats = &dev->stats.aal0;
4121da177e4SLinus Torvalds 		break;
4131da177e4SLinus Torvalds 	case ATM_AAL34:
4141da177e4SLinus Torvalds 		error = atm_init_aal34(vcc);
4151da177e4SLinus Torvalds 		vcc->stats = &dev->stats.aal34;
4161da177e4SLinus Torvalds 		break;
4171da177e4SLinus Torvalds 	case ATM_NO_AAL:
4181da177e4SLinus Torvalds 		/* ATM_AAL5 is also used in the "0 for default" case */
4191da177e4SLinus Torvalds 		vcc->qos.aal = ATM_AAL5;
420df561f66SGustavo A. R. Silva 		fallthrough;
4211da177e4SLinus Torvalds 	case ATM_AAL5:
4221da177e4SLinus Torvalds 		error = atm_init_aal5(vcc);
4231da177e4SLinus Torvalds 		vcc->stats = &dev->stats.aal5;
4241da177e4SLinus Torvalds 		break;
4251da177e4SLinus Torvalds 	default:
4261da177e4SLinus Torvalds 		error = -EPROTOTYPE;
4271da177e4SLinus Torvalds 	}
428a8147d73SJoe Perches 	if (!error)
429a8147d73SJoe Perches 		error = adjust_tp(&vcc->qos.txtp, vcc->qos.aal);
430a8147d73SJoe Perches 	if (!error)
431a8147d73SJoe Perches 		error = adjust_tp(&vcc->qos.rxtp, vcc->qos.aal);
4321da177e4SLinus Torvalds 	if (error)
4331da177e4SLinus Torvalds 		goto fail;
43452240062SStephen Hemminger 	pr_debug("VCC %d.%d, AAL %d\n", vpi, vci, vcc->qos.aal);
43599824461SJoe Perches 	pr_debug("  TX: %d, PCR %d..%d, SDU %d\n",
43699824461SJoe Perches 		 vcc->qos.txtp.traffic_class,
43799824461SJoe Perches 		 vcc->qos.txtp.min_pcr,
43899824461SJoe Perches 		 vcc->qos.txtp.max_pcr,
43999824461SJoe Perches 		 vcc->qos.txtp.max_sdu);
44099824461SJoe Perches 	pr_debug("  RX: %d, PCR %d..%d, SDU %d\n",
44199824461SJoe Perches 		 vcc->qos.rxtp.traffic_class,
44299824461SJoe Perches 		 vcc->qos.rxtp.min_pcr,
44399824461SJoe Perches 		 vcc->qos.rxtp.max_pcr,
44499824461SJoe Perches 		 vcc->qos.rxtp.max_sdu);
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	if (dev->ops->open) {
447a8147d73SJoe Perches 		error = dev->ops->open(vcc);
448a8147d73SJoe Perches 		if (error)
4491da177e4SLinus Torvalds 			goto fail;
4501da177e4SLinus Torvalds 	}
4511da177e4SLinus Torvalds 	return 0;
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds fail:
4541da177e4SLinus Torvalds 	vcc_remove_socket(sk);
4551da177e4SLinus Torvalds fail_module_put:
4561da177e4SLinus Torvalds 	module_put(dev->ops->owner);
4571da177e4SLinus Torvalds 	/* ensure we get dev module ref count correct */
4581da177e4SLinus Torvalds 	vcc->dev = NULL;
4591da177e4SLinus Torvalds 	return error;
4601da177e4SLinus Torvalds }
4611da177e4SLinus Torvalds 
vcc_connect(struct socket * sock,int itf,short vpi,int vci)4621da177e4SLinus Torvalds int vcc_connect(struct socket *sock, int itf, short vpi, int vci)
4631da177e4SLinus Torvalds {
4641da177e4SLinus Torvalds 	struct atm_dev *dev;
4651da177e4SLinus Torvalds 	struct atm_vcc *vcc = ATM_SD(sock);
4661da177e4SLinus Torvalds 	int error;
4671da177e4SLinus Torvalds 
46899824461SJoe Perches 	pr_debug("(vpi %d, vci %d)\n", vpi, vci);
4691da177e4SLinus Torvalds 	if (sock->state == SS_CONNECTED)
4701da177e4SLinus Torvalds 		return -EISCONN;
4711da177e4SLinus Torvalds 	if (sock->state != SS_UNCONNECTED)
4721da177e4SLinus Torvalds 		return -EINVAL;
4731da177e4SLinus Torvalds 	if (!(vpi || vci))
4741da177e4SLinus Torvalds 		return -EINVAL;
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	if (vpi != ATM_VPI_UNSPEC && vci != ATM_VCI_UNSPEC)
4771da177e4SLinus Torvalds 		clear_bit(ATM_VF_PARTIAL, &vcc->flags);
4781da177e4SLinus Torvalds 	else
4791da177e4SLinus Torvalds 		if (test_bit(ATM_VF_PARTIAL, &vcc->flags))
4801da177e4SLinus Torvalds 			return -EINVAL;
48199824461SJoe Perches 	pr_debug("(TX: cl %d,bw %d-%d,sdu %d; "
4821da177e4SLinus Torvalds 		 "RX: cl %d,bw %d-%d,sdu %d,AAL %s%d)\n",
4831da177e4SLinus Torvalds 		 vcc->qos.txtp.traffic_class, vcc->qos.txtp.min_pcr,
4841da177e4SLinus Torvalds 		 vcc->qos.txtp.max_pcr, vcc->qos.txtp.max_sdu,
4851da177e4SLinus Torvalds 		 vcc->qos.rxtp.traffic_class, vcc->qos.rxtp.min_pcr,
4861da177e4SLinus Torvalds 		 vcc->qos.rxtp.max_pcr, vcc->qos.rxtp.max_sdu,
48799824461SJoe Perches 		 vcc->qos.aal == ATM_AAL5 ? "" :
48899824461SJoe Perches 		 vcc->qos.aal == ATM_AAL0 ? "" : " ??? code ",
48999824461SJoe Perches 		 vcc->qos.aal == ATM_AAL0 ? 0 : vcc->qos.aal);
4901da177e4SLinus Torvalds 	if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
4911da177e4SLinus Torvalds 		return -EBADFD;
4921da177e4SLinus Torvalds 	if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
4931da177e4SLinus Torvalds 	    vcc->qos.rxtp.traffic_class == ATM_ANYCLASS)
4941da177e4SLinus Torvalds 		return -EINVAL;
495c9933d08SMitchell Blank Jr 	if (likely(itf != ATM_ITF_ANY)) {
496a8147d73SJoe Perches 		dev = try_then_request_module(atm_dev_lookup(itf),
497a8147d73SJoe Perches 					      "atm-device-%d", itf);
498c9933d08SMitchell Blank Jr 	} else {
499c9933d08SMitchell Blank Jr 		dev = NULL;
50057b47a53SIngo Molnar 		mutex_lock(&atm_dev_mutex);
501c9933d08SMitchell Blank Jr 		if (!list_empty(&atm_devs)) {
502a8147d73SJoe Perches 			dev = list_entry(atm_devs.next,
503a8147d73SJoe Perches 					 struct atm_dev, dev_list);
504c9933d08SMitchell Blank Jr 			atm_dev_hold(dev);
505c9933d08SMitchell Blank Jr 		}
50657b47a53SIngo Molnar 		mutex_unlock(&atm_dev_mutex);
507c9933d08SMitchell Blank Jr 	}
5081da177e4SLinus Torvalds 	if (!dev)
5091da177e4SLinus Torvalds 		return -ENODEV;
5101da177e4SLinus Torvalds 	error = __vcc_connect(vcc, dev, vpi, vci);
5111da177e4SLinus Torvalds 	if (error) {
5121da177e4SLinus Torvalds 		atm_dev_put(dev);
5131da177e4SLinus Torvalds 		return error;
5141da177e4SLinus Torvalds 	}
5151da177e4SLinus Torvalds 	if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC)
5161da177e4SLinus Torvalds 		set_bit(ATM_VF_PARTIAL, &vcc->flags);
5171da177e4SLinus Torvalds 	if (test_bit(ATM_VF_READY, &ATM_SD(sock)->flags))
5181da177e4SLinus Torvalds 		sock->state = SS_CONNECTED;
5191da177e4SLinus Torvalds 	return 0;
5201da177e4SLinus Torvalds }
5211da177e4SLinus Torvalds 
vcc_recvmsg(struct socket * sock,struct msghdr * msg,size_t size,int flags)5221b784140SYing Xue int vcc_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
5231b784140SYing Xue 		int flags)
5241da177e4SLinus Torvalds {
5251da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
5261da177e4SLinus Torvalds 	struct atm_vcc *vcc;
5271da177e4SLinus Torvalds 	struct sk_buff *skb;
5281da177e4SLinus Torvalds 	int copied, error = -EINVAL;
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds 	if (sock->state != SS_CONNECTED)
5311da177e4SLinus Torvalds 		return -ENOTCONN;
53240ba8499SJorge Boncompte [DTI2] 
53340ba8499SJorge Boncompte [DTI2] 	/* only handle MSG_DONTWAIT and MSG_PEEK */
53440ba8499SJorge Boncompte [DTI2] 	if (flags & ~(MSG_DONTWAIT | MSG_PEEK))
5351da177e4SLinus Torvalds 		return -EOPNOTSUPP;
53640ba8499SJorge Boncompte [DTI2] 
5371da177e4SLinus Torvalds 	vcc = ATM_SD(sock);
5381da177e4SLinus Torvalds 	if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
5391da177e4SLinus Torvalds 	    test_bit(ATM_VF_CLOSE, &vcc->flags) ||
5401da177e4SLinus Torvalds 	    !test_bit(ATM_VF_READY, &vcc->flags))
5411da177e4SLinus Torvalds 		return 0;
5421da177e4SLinus Torvalds 
543f4b41f06SOliver Hartkopp 	skb = skb_recv_datagram(sk, flags, &error);
5441da177e4SLinus Torvalds 	if (!skb)
5451da177e4SLinus Torvalds 		return error;
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	copied = skb->len;
5481da177e4SLinus Torvalds 	if (copied > size) {
5491da177e4SLinus Torvalds 		copied = size;
5501da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
5511da177e4SLinus Torvalds 	}
5521da177e4SLinus Torvalds 
55351f3d02bSDavid S. Miller 	error = skb_copy_datagram_msg(skb, 0, msg, copied);
5541da177e4SLinus Torvalds 	if (error)
5551da177e4SLinus Torvalds 		return error;
556*6fd1d51cSErin MacNeil 	sock_recv_cmsgs(msg, sk, skb);
55740ba8499SJorge Boncompte [DTI2] 
55840ba8499SJorge Boncompte [DTI2] 	if (!(flags & MSG_PEEK)) {
55940ba8499SJorge Boncompte [DTI2] 		pr_debug("%d -= %d\n", atomic_read(&sk->sk_rmem_alloc),
56040ba8499SJorge Boncompte [DTI2] 			 skb->truesize);
5611da177e4SLinus Torvalds 		atm_return(vcc, skb->truesize);
56240ba8499SJorge Boncompte [DTI2] 	}
56340ba8499SJorge Boncompte [DTI2] 
5641da177e4SLinus Torvalds 	skb_free_datagram(sk, skb);
5651da177e4SLinus Torvalds 	return copied;
5661da177e4SLinus Torvalds }
5671da177e4SLinus Torvalds 
vcc_sendmsg(struct socket * sock,struct msghdr * m,size_t size)5681b784140SYing Xue int vcc_sendmsg(struct socket *sock, struct msghdr *m, size_t size)
5691da177e4SLinus Torvalds {
5701da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
5711da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
5721da177e4SLinus Torvalds 	struct atm_vcc *vcc;
5731da177e4SLinus Torvalds 	struct sk_buff *skb;
5741da177e4SLinus Torvalds 	int eff, error;
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 	lock_sock(sk);
5771da177e4SLinus Torvalds 	if (sock->state != SS_CONNECTED) {
5781da177e4SLinus Torvalds 		error = -ENOTCONN;
5791da177e4SLinus Torvalds 		goto out;
5801da177e4SLinus Torvalds 	}
5811da177e4SLinus Torvalds 	if (m->msg_name) {
5821da177e4SLinus Torvalds 		error = -EISCONN;
5831da177e4SLinus Torvalds 		goto out;
5841da177e4SLinus Torvalds 	}
5851da177e4SLinus Torvalds 	vcc = ATM_SD(sock);
5861da177e4SLinus Torvalds 	if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
5871da177e4SLinus Torvalds 	    test_bit(ATM_VF_CLOSE, &vcc->flags) ||
5881da177e4SLinus Torvalds 	    !test_bit(ATM_VF_READY, &vcc->flags)) {
5891da177e4SLinus Torvalds 		error = -EPIPE;
5901da177e4SLinus Torvalds 		send_sig(SIGPIPE, current, 0);
5911da177e4SLinus Torvalds 		goto out;
5921da177e4SLinus Torvalds 	}
5931da177e4SLinus Torvalds 	if (!size) {
5941da177e4SLinus Torvalds 		error = 0;
5951da177e4SLinus Torvalds 		goto out;
5961da177e4SLinus Torvalds 	}
5977424ce65SAl Viro 	if (size > vcc->qos.txtp.max_sdu) {
5981da177e4SLinus Torvalds 		error = -EMSGSIZE;
5991da177e4SLinus Torvalds 		goto out;
6001da177e4SLinus Torvalds 	}
601e49332bdSJesper Juhl 
6021da177e4SLinus Torvalds 	eff = (size+3) & ~3; /* align to word boundary */
603aa395145SEric Dumazet 	prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
6041da177e4SLinus Torvalds 	error = 0;
605c55fa3ccSFrancois Romieu 	while (!vcc_tx_ready(vcc, eff)) {
6061da177e4SLinus Torvalds 		if (m->msg_flags & MSG_DONTWAIT) {
6071da177e4SLinus Torvalds 			error = -EAGAIN;
6081da177e4SLinus Torvalds 			break;
6091da177e4SLinus Torvalds 		}
6101da177e4SLinus Torvalds 		schedule();
6111da177e4SLinus Torvalds 		if (signal_pending(current)) {
6121da177e4SLinus Torvalds 			error = -ERESTARTSYS;
6131da177e4SLinus Torvalds 			break;
6141da177e4SLinus Torvalds 		}
6151da177e4SLinus Torvalds 		if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
6161da177e4SLinus Torvalds 		    test_bit(ATM_VF_CLOSE, &vcc->flags) ||
6171da177e4SLinus Torvalds 		    !test_bit(ATM_VF_READY, &vcc->flags)) {
6181da177e4SLinus Torvalds 			error = -EPIPE;
6191da177e4SLinus Torvalds 			send_sig(SIGPIPE, current, 0);
6201da177e4SLinus Torvalds 			break;
6211da177e4SLinus Torvalds 		}
622aa395145SEric Dumazet 		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
6231da177e4SLinus Torvalds 	}
624aa395145SEric Dumazet 	finish_wait(sk_sleep(sk), &wait);
6251da177e4SLinus Torvalds 	if (error)
6261da177e4SLinus Torvalds 		goto out;
627c55fa3ccSFrancois Romieu 
628c55fa3ccSFrancois Romieu 	skb = alloc_skb(eff, GFP_KERNEL);
629c55fa3ccSFrancois Romieu 	if (!skb) {
630c55fa3ccSFrancois Romieu 		error = -ENOMEM;
631c55fa3ccSFrancois Romieu 		goto out;
632c55fa3ccSFrancois Romieu 	}
633c55fa3ccSFrancois Romieu 	pr_debug("%d += %d\n", sk_wmem_alloc_get(sk), skb->truesize);
6349bbe60a6SDavid Woodhouse 	atm_account_tx(vcc, skb);
635c55fa3ccSFrancois Romieu 
6361da177e4SLinus Torvalds 	skb->dev = NULL; /* for paths shared with net_device interfaces */
637cbbd26b8SAl Viro 	if (!copy_from_iter_full(skb_put(skb, size), size, &m->msg_iter)) {
6381da177e4SLinus Torvalds 		kfree_skb(skb);
6391da177e4SLinus Torvalds 		error = -EFAULT;
6401da177e4SLinus Torvalds 		goto out;
6411da177e4SLinus Torvalds 	}
642a8147d73SJoe Perches 	if (eff != size)
643a8147d73SJoe Perches 		memset(skb->data + size, 0, eff-size);
6441da177e4SLinus Torvalds 	error = vcc->dev->ops->send(vcc, skb);
6451da177e4SLinus Torvalds 	error = error ? error : size;
6461da177e4SLinus Torvalds out:
6471da177e4SLinus Torvalds 	release_sock(sk);
6481da177e4SLinus Torvalds 	return error;
6491da177e4SLinus Torvalds }
6501da177e4SLinus Torvalds 
vcc_poll(struct file * file,struct socket * sock,poll_table * wait)651a11e1d43SLinus Torvalds __poll_t vcc_poll(struct file *file, struct socket *sock, poll_table *wait)
6521da177e4SLinus Torvalds {
6531da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
654a11e1d43SLinus Torvalds 	struct atm_vcc *vcc;
655a11e1d43SLinus Torvalds 	__poll_t mask;
656a11e1d43SLinus Torvalds 
65789ab066dSKarsten Graul 	sock_poll_wait(file, sock, wait);
658a11e1d43SLinus Torvalds 	mask = 0;
659a11e1d43SLinus Torvalds 
660a11e1d43SLinus Torvalds 	vcc = ATM_SD(sock);
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 	/* exceptional events */
6631da177e4SLinus Torvalds 	if (sk->sk_err)
664a9a08845SLinus Torvalds 		mask = EPOLLERR;
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
6671da177e4SLinus Torvalds 	    test_bit(ATM_VF_CLOSE, &vcc->flags))
668a9a08845SLinus Torvalds 		mask |= EPOLLHUP;
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 	/* readable? */
6713ef7cf57SEric Dumazet 	if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
672a9a08845SLinus Torvalds 		mask |= EPOLLIN | EPOLLRDNORM;
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	/* writable? */
6751da177e4SLinus Torvalds 	if (sock->state == SS_CONNECTING &&
6761da177e4SLinus Torvalds 	    test_bit(ATM_VF_WAITING, &vcc->flags))
6771da177e4SLinus Torvalds 		return mask;
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds 	if (vcc->qos.txtp.traffic_class != ATM_NONE &&
6801da177e4SLinus Torvalds 	    vcc_writable(sk))
681a9a08845SLinus Torvalds 		mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 	return mask;
6841da177e4SLinus Torvalds }
6851da177e4SLinus Torvalds 
atm_change_qos(struct atm_vcc * vcc,struct atm_qos * qos)6861da177e4SLinus Torvalds static int atm_change_qos(struct atm_vcc *vcc, struct atm_qos *qos)
6871da177e4SLinus Torvalds {
6881da177e4SLinus Torvalds 	int error;
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 	/*
6911da177e4SLinus Torvalds 	 * Don't let the QoS change the already connected AAL type nor the
6921da177e4SLinus Torvalds 	 * traffic class.
6931da177e4SLinus Torvalds 	 */
6941da177e4SLinus Torvalds 	if (qos->aal != vcc->qos.aal ||
6951da177e4SLinus Torvalds 	    qos->rxtp.traffic_class != vcc->qos.rxtp.traffic_class ||
6961da177e4SLinus Torvalds 	    qos->txtp.traffic_class != vcc->qos.txtp.traffic_class)
6971da177e4SLinus Torvalds 		return -EINVAL;
6981da177e4SLinus Torvalds 	error = adjust_tp(&qos->txtp, qos->aal);
699a8147d73SJoe Perches 	if (!error)
700a8147d73SJoe Perches 		error = adjust_tp(&qos->rxtp, qos->aal);
701a8147d73SJoe Perches 	if (error)
702a8147d73SJoe Perches 		return error;
703a8147d73SJoe Perches 	if (!vcc->dev->ops->change_qos)
704a8147d73SJoe Perches 		return -EOPNOTSUPP;
7051da177e4SLinus Torvalds 	if (sk_atm(vcc)->sk_family == AF_ATMPVC)
7061da177e4SLinus Torvalds 		return vcc->dev->ops->change_qos(vcc, qos, ATM_MF_SET);
7071da177e4SLinus Torvalds 	return svc_change_qos(vcc, qos);
7081da177e4SLinus Torvalds }
7091da177e4SLinus Torvalds 
check_tp(const struct atm_trafprm * tp)71061c33e01SMitchell Blank Jr static int check_tp(const struct atm_trafprm *tp)
7111da177e4SLinus Torvalds {
7121da177e4SLinus Torvalds 	/* @@@ Should be merged with adjust_tp */
713a8147d73SJoe Perches 	if (!tp->traffic_class || tp->traffic_class == ATM_ANYCLASS)
714a8147d73SJoe Perches 		return 0;
7151da177e4SLinus Torvalds 	if (tp->traffic_class != ATM_UBR && !tp->min_pcr && !tp->pcr &&
716a8147d73SJoe Perches 	    !tp->max_pcr)
717a8147d73SJoe Perches 		return -EINVAL;
718a8147d73SJoe Perches 	if (tp->min_pcr == ATM_MAX_PCR)
719a8147d73SJoe Perches 		return -EINVAL;
7201da177e4SLinus Torvalds 	if (tp->min_pcr && tp->max_pcr && tp->max_pcr != ATM_MAX_PCR &&
721a8147d73SJoe Perches 	    tp->min_pcr > tp->max_pcr)
722a8147d73SJoe Perches 		return -EINVAL;
7231da177e4SLinus Torvalds 	/*
7241da177e4SLinus Torvalds 	 * We allow pcr to be outside [min_pcr,max_pcr], because later
7251da177e4SLinus Torvalds 	 * adjustment may still push it in the valid range.
7261da177e4SLinus Torvalds 	 */
7271da177e4SLinus Torvalds 	return 0;
7281da177e4SLinus Torvalds }
7291da177e4SLinus Torvalds 
check_qos(const struct atm_qos * qos)73061c33e01SMitchell Blank Jr static int check_qos(const struct atm_qos *qos)
7311da177e4SLinus Torvalds {
7321da177e4SLinus Torvalds 	int error;
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 	if (!qos->txtp.traffic_class && !qos->rxtp.traffic_class)
7351da177e4SLinus Torvalds 		return -EINVAL;
7361da177e4SLinus Torvalds 	if (qos->txtp.traffic_class != qos->rxtp.traffic_class &&
7371da177e4SLinus Torvalds 	    qos->txtp.traffic_class && qos->rxtp.traffic_class &&
7381da177e4SLinus Torvalds 	    qos->txtp.traffic_class != ATM_ANYCLASS &&
739a8147d73SJoe Perches 	    qos->rxtp.traffic_class != ATM_ANYCLASS)
740a8147d73SJoe Perches 		return -EINVAL;
7411da177e4SLinus Torvalds 	error = check_tp(&qos->txtp);
742a8147d73SJoe Perches 	if (error)
743a8147d73SJoe Perches 		return error;
7441da177e4SLinus Torvalds 	return check_tp(&qos->rxtp);
7451da177e4SLinus Torvalds }
7461da177e4SLinus Torvalds 
vcc_setsockopt(struct socket * sock,int level,int optname,sockptr_t optval,unsigned int optlen)7471da177e4SLinus Torvalds int vcc_setsockopt(struct socket *sock, int level, int optname,
748a7b75c5aSChristoph Hellwig 		   sockptr_t optval, unsigned int optlen)
7491da177e4SLinus Torvalds {
7501da177e4SLinus Torvalds 	struct atm_vcc *vcc;
7511da177e4SLinus Torvalds 	unsigned long value;
7521da177e4SLinus Torvalds 	int error;
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds 	if (__SO_LEVEL_MATCH(optname, level) && optlen != __SO_SIZE(optname))
7551da177e4SLinus Torvalds 		return -EINVAL;
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 	vcc = ATM_SD(sock);
7581da177e4SLinus Torvalds 	switch (optname) {
7591da177e4SLinus Torvalds 	case SO_ATMQOS:
7601da177e4SLinus Torvalds 	{
7611da177e4SLinus Torvalds 		struct atm_qos qos;
7621da177e4SLinus Torvalds 
763a7b75c5aSChristoph Hellwig 		if (copy_from_sockptr(&qos, optval, sizeof(qos)))
7641da177e4SLinus Torvalds 			return -EFAULT;
7651da177e4SLinus Torvalds 		error = check_qos(&qos);
766a8147d73SJoe Perches 		if (error)
767a8147d73SJoe Perches 			return error;
7681da177e4SLinus Torvalds 		if (sock->state == SS_CONNECTED)
7691da177e4SLinus Torvalds 			return atm_change_qos(vcc, &qos);
7701da177e4SLinus Torvalds 		if (sock->state != SS_UNCONNECTED)
7711da177e4SLinus Torvalds 			return -EBADFD;
7721da177e4SLinus Torvalds 		vcc->qos = qos;
7731da177e4SLinus Torvalds 		set_bit(ATM_VF_HASQOS, &vcc->flags);
7741da177e4SLinus Torvalds 		return 0;
7751da177e4SLinus Torvalds 	}
7761da177e4SLinus Torvalds 	case SO_SETCLP:
777a7b75c5aSChristoph Hellwig 		if (copy_from_sockptr(&value, optval, sizeof(value)))
7781da177e4SLinus Torvalds 			return -EFAULT;
779a8147d73SJoe Perches 		if (value)
780a8147d73SJoe Perches 			vcc->atm_options |= ATM_ATMOPT_CLP;
781a8147d73SJoe Perches 		else
782a8147d73SJoe Perches 			vcc->atm_options &= ~ATM_ATMOPT_CLP;
7831da177e4SLinus Torvalds 		return 0;
7841da177e4SLinus Torvalds 	default:
785a8147d73SJoe Perches 		return -EINVAL;
7861da177e4SLinus Torvalds 	}
7871da177e4SLinus Torvalds }
7881da177e4SLinus Torvalds 
vcc_getsockopt(struct socket * sock,int level,int optname,char __user * optval,int __user * optlen)7891da177e4SLinus Torvalds int vcc_getsockopt(struct socket *sock, int level, int optname,
7901da177e4SLinus Torvalds 		   char __user *optval, int __user *optlen)
7911da177e4SLinus Torvalds {
7921da177e4SLinus Torvalds 	struct atm_vcc *vcc;
7931da177e4SLinus Torvalds 	int len;
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 	if (get_user(len, optlen))
7961da177e4SLinus Torvalds 		return -EFAULT;
7971da177e4SLinus Torvalds 	if (__SO_LEVEL_MATCH(optname, level) && len != __SO_SIZE(optname))
7981da177e4SLinus Torvalds 		return -EINVAL;
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds 	vcc = ATM_SD(sock);
8011da177e4SLinus Torvalds 	switch (optname) {
8021da177e4SLinus Torvalds 	case SO_ATMQOS:
8031da177e4SLinus Torvalds 		if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
8041da177e4SLinus Torvalds 			return -EINVAL;
805a8147d73SJoe Perches 		return copy_to_user(optval, &vcc->qos, sizeof(vcc->qos))
806a8147d73SJoe Perches 			? -EFAULT : 0;
8071da177e4SLinus Torvalds 	case SO_SETCLP:
808a8147d73SJoe Perches 		return put_user(vcc->atm_options & ATM_ATMOPT_CLP ? 1 : 0,
809a8147d73SJoe Perches 				(unsigned long __user *)optval) ? -EFAULT : 0;
8101da177e4SLinus Torvalds 	case SO_ATMPVC:
8111da177e4SLinus Torvalds 	{
8121da177e4SLinus Torvalds 		struct sockaddr_atmpvc pvc;
8131da177e4SLinus Torvalds 
814a8147d73SJoe Perches 		if (!vcc->dev || !test_bit(ATM_VF_ADDR, &vcc->flags))
8151da177e4SLinus Torvalds 			return -ENOTCONN;
816e862f1a9SMathias Krause 		memset(&pvc, 0, sizeof(pvc));
8171da177e4SLinus Torvalds 		pvc.sap_family = AF_ATMPVC;
8181da177e4SLinus Torvalds 		pvc.sap_addr.itf = vcc->dev->number;
8191da177e4SLinus Torvalds 		pvc.sap_addr.vpi = vcc->vpi;
8201da177e4SLinus Torvalds 		pvc.sap_addr.vci = vcc->vci;
821a8147d73SJoe Perches 		return copy_to_user(optval, &pvc, sizeof(pvc)) ? -EFAULT : 0;
8221da177e4SLinus Torvalds 	}
8231da177e4SLinus Torvalds 	default:
824a8147d73SJoe Perches 		return -EINVAL;
8251da177e4SLinus Torvalds 	}
8261da177e4SLinus Torvalds }
8271da177e4SLinus Torvalds 
register_atmdevice_notifier(struct notifier_block * nb)8287313bb8fSKarl Hiramoto int register_atmdevice_notifier(struct notifier_block *nb)
8297313bb8fSKarl Hiramoto {
8307313bb8fSKarl Hiramoto 	return atomic_notifier_chain_register(&atm_dev_notify_chain, nb);
8317313bb8fSKarl Hiramoto }
8327313bb8fSKarl Hiramoto EXPORT_SYMBOL_GPL(register_atmdevice_notifier);
8337313bb8fSKarl Hiramoto 
unregister_atmdevice_notifier(struct notifier_block * nb)8347313bb8fSKarl Hiramoto void unregister_atmdevice_notifier(struct notifier_block *nb)
8357313bb8fSKarl Hiramoto {
8367313bb8fSKarl Hiramoto 	atomic_notifier_chain_unregister(&atm_dev_notify_chain, nb);
8377313bb8fSKarl Hiramoto }
8387313bb8fSKarl Hiramoto EXPORT_SYMBOL_GPL(unregister_atmdevice_notifier);
8397313bb8fSKarl Hiramoto 
atm_init(void)8401da177e4SLinus Torvalds static int __init atm_init(void)
8411da177e4SLinus Torvalds {
8421da177e4SLinus Torvalds 	int error;
8431da177e4SLinus Torvalds 
844a8147d73SJoe Perches 	error = proto_register(&vcc_proto, 0);
845a8147d73SJoe Perches 	if (error < 0)
8461da177e4SLinus Torvalds 		goto out;
847a8147d73SJoe Perches 	error = atmpvc_init();
848a8147d73SJoe Perches 	if (error < 0) {
84999824461SJoe Perches 		pr_err("atmpvc_init() failed with %d\n", error);
8501da177e4SLinus Torvalds 		goto out_unregister_vcc_proto;
8511da177e4SLinus Torvalds 	}
852a8147d73SJoe Perches 	error = atmsvc_init();
853a8147d73SJoe Perches 	if (error < 0) {
85499824461SJoe Perches 		pr_err("atmsvc_init() failed with %d\n", error);
8551da177e4SLinus Torvalds 		goto out_atmpvc_exit;
8561da177e4SLinus Torvalds 	}
857a8147d73SJoe Perches 	error = atm_proc_init();
858a8147d73SJoe Perches 	if (error < 0) {
85999824461SJoe Perches 		pr_err("atm_proc_init() failed with %d\n", error);
8601da177e4SLinus Torvalds 		goto out_atmsvc_exit;
8611da177e4SLinus Torvalds 	}
862a8147d73SJoe Perches 	error = atm_sysfs_init();
863a8147d73SJoe Perches 	if (error < 0) {
86499824461SJoe Perches 		pr_err("atm_sysfs_init() failed with %d\n", error);
865656d98b0SRoman Kagan 		goto out_atmproc_exit;
866656d98b0SRoman Kagan 	}
8671da177e4SLinus Torvalds out:
8681da177e4SLinus Torvalds 	return error;
869656d98b0SRoman Kagan out_atmproc_exit:
870656d98b0SRoman Kagan 	atm_proc_exit();
8711da177e4SLinus Torvalds out_atmsvc_exit:
8721da177e4SLinus Torvalds 	atmsvc_exit();
8731da177e4SLinus Torvalds out_atmpvc_exit:
8741da177e4SLinus Torvalds 	atmsvc_exit();
8751da177e4SLinus Torvalds out_unregister_vcc_proto:
8761da177e4SLinus Torvalds 	proto_unregister(&vcc_proto);
8771da177e4SLinus Torvalds 	goto out;
8781da177e4SLinus Torvalds }
8791da177e4SLinus Torvalds 
atm_exit(void)8801da177e4SLinus Torvalds static void __exit atm_exit(void)
8811da177e4SLinus Torvalds {
8821da177e4SLinus Torvalds 	atm_proc_exit();
883656d98b0SRoman Kagan 	atm_sysfs_exit();
8841da177e4SLinus Torvalds 	atmsvc_exit();
8851da177e4SLinus Torvalds 	atmpvc_exit();
8861da177e4SLinus Torvalds 	proto_unregister(&vcc_proto);
8871da177e4SLinus Torvalds }
8881da177e4SLinus Torvalds 
88984ff602eSDaniel Walker subsys_initcall(atm_init);
89084ff602eSDaniel Walker 
8911da177e4SLinus Torvalds module_exit(atm_exit);
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds MODULE_LICENSE("GPL");
8941da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_ATMPVC);
8951da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_ATMSVC);
896