xref: /openbmc/linux/net/atm/common.c (revision d750dbdc071bf863112ea83c64cd7c44d4bad261)
11da177e4SLinus Torvalds /* net/atm/common.c - ATM sockets (common part for PVC and SVC) */
21da177e4SLinus Torvalds 
31da177e4SLinus Torvalds /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
41da177e4SLinus Torvalds 
599824461SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds #include <linux/module.h>
81da177e4SLinus Torvalds #include <linux/kmod.h>
91da177e4SLinus Torvalds #include <linux/net.h>		/* struct socket, struct proto_ops */
101da177e4SLinus Torvalds #include <linux/atm.h>		/* ATM stuff */
111da177e4SLinus Torvalds #include <linux/atmdev.h>
121da177e4SLinus Torvalds #include <linux/socket.h>	/* SOL_SOCKET */
131da177e4SLinus Torvalds #include <linux/errno.h>	/* error codes */
141da177e4SLinus Torvalds #include <linux/capability.h>
15e49332bdSJesper Juhl #include <linux/mm.h>
163f07c014SIngo Molnar #include <linux/sched/signal.h>
17*d750dbdcSTina Ruchandani #include <linux/time64.h>	/* 64-bit time for seconds */
181da177e4SLinus Torvalds #include <linux/skbuff.h>
191da177e4SLinus Torvalds #include <linux/bitops.h>
201da177e4SLinus Torvalds #include <linux/init.h>
215a0e3ad6STejun Heo #include <linux/slab.h>
221da177e4SLinus Torvalds #include <net/sock.h>		/* struct sock */
23a8147d73SJoe Perches #include <linux/uaccess.h>
24a8147d73SJoe Perches #include <linux/poll.h>
251da177e4SLinus Torvalds 
2660063497SArun Sharma #include <linux/atomic.h>
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds #include "resources.h"		/* atm_find_dev */
291da177e4SLinus Torvalds #include "common.h"		/* prototypes */
301da177e4SLinus Torvalds #include "protocols.h"		/* atm_init_<transport> */
311da177e4SLinus Torvalds #include "addr.h"		/* address registry */
321da177e4SLinus Torvalds #include "signaling.h"		/* for WAITING and sigd_attach */
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds struct hlist_head vcc_hash[VCC_HTABLE_SIZE];
35a8147d73SJoe Perches EXPORT_SYMBOL(vcc_hash);
36a8147d73SJoe Perches 
371da177e4SLinus Torvalds DEFINE_RWLOCK(vcc_sklist_lock);
38a8147d73SJoe Perches EXPORT_SYMBOL(vcc_sklist_lock);
391da177e4SLinus Torvalds 
407313bb8fSKarl Hiramoto static ATOMIC_NOTIFIER_HEAD(atm_dev_notify_chain);
417313bb8fSKarl Hiramoto 
421da177e4SLinus Torvalds static void __vcc_insert_socket(struct sock *sk)
431da177e4SLinus Torvalds {
441da177e4SLinus Torvalds 	struct atm_vcc *vcc = atm_sk(sk);
45a8147d73SJoe Perches 	struct hlist_head *head = &vcc_hash[vcc->vci & (VCC_HTABLE_SIZE - 1)];
4681c3d547SEric Dumazet 	sk->sk_hash = vcc->vci & (VCC_HTABLE_SIZE - 1);
471da177e4SLinus Torvalds 	sk_add_node(sk, head);
481da177e4SLinus Torvalds }
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds void vcc_insert_socket(struct sock *sk)
511da177e4SLinus Torvalds {
521da177e4SLinus Torvalds 	write_lock_irq(&vcc_sklist_lock);
531da177e4SLinus Torvalds 	__vcc_insert_socket(sk);
541da177e4SLinus Torvalds 	write_unlock_irq(&vcc_sklist_lock);
551da177e4SLinus Torvalds }
56a8147d73SJoe Perches EXPORT_SYMBOL(vcc_insert_socket);
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds static void vcc_remove_socket(struct sock *sk)
591da177e4SLinus Torvalds {
601da177e4SLinus Torvalds 	write_lock_irq(&vcc_sklist_lock);
611da177e4SLinus Torvalds 	sk_del_node_init(sk);
621da177e4SLinus Torvalds 	write_unlock_irq(&vcc_sklist_lock);
631da177e4SLinus Torvalds }
641da177e4SLinus Torvalds 
65c55fa3ccSFrancois Romieu static bool vcc_tx_ready(struct atm_vcc *vcc, unsigned int size)
661da177e4SLinus Torvalds {
671da177e4SLinus Torvalds 	struct sock *sk = sk_atm(vcc);
681da177e4SLinus Torvalds 
6981e2a3d5SEric Dumazet 	if (sk_wmem_alloc_get(sk) && !atm_may_send(vcc, size)) {
7052240062SStephen Hemminger 		pr_debug("Sorry: wmem_alloc = %d, size = %d, sndbuf = %d\n",
7199824461SJoe Perches 			 sk_wmem_alloc_get(sk), size, sk->sk_sndbuf);
72c55fa3ccSFrancois Romieu 		return false;
731da177e4SLinus Torvalds 	}
74c55fa3ccSFrancois Romieu 	return true;
751da177e4SLinus Torvalds }
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds static void vcc_sock_destruct(struct sock *sk)
781da177e4SLinus Torvalds {
791da177e4SLinus Torvalds 	if (atomic_read(&sk->sk_rmem_alloc))
80a8147d73SJoe Perches 		printk(KERN_DEBUG "%s: rmem leakage (%d bytes) detected.\n",
81a8147d73SJoe Perches 		       __func__, atomic_read(&sk->sk_rmem_alloc));
821da177e4SLinus Torvalds 
8314afee4bSReshetova, Elena 	if (refcount_read(&sk->sk_wmem_alloc))
84a8147d73SJoe Perches 		printk(KERN_DEBUG "%s: wmem leakage (%d bytes) detected.\n",
8514afee4bSReshetova, Elena 		       __func__, refcount_read(&sk->sk_wmem_alloc));
861da177e4SLinus Torvalds }
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds static void vcc_def_wakeup(struct sock *sk)
891da177e4SLinus Torvalds {
9043815482SEric Dumazet 	struct socket_wq *wq;
9143815482SEric Dumazet 
9243815482SEric Dumazet 	rcu_read_lock();
9343815482SEric Dumazet 	wq = rcu_dereference(sk->sk_wq);
941ce0bf50SHerbert Xu 	if (skwq_has_sleeper(wq))
9543815482SEric Dumazet 		wake_up(&wq->wait);
9643815482SEric Dumazet 	rcu_read_unlock();
971da177e4SLinus Torvalds }
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds static inline int vcc_writable(struct sock *sk)
1001da177e4SLinus Torvalds {
1011da177e4SLinus Torvalds 	struct atm_vcc *vcc = atm_sk(sk);
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 	return (vcc->qos.txtp.max_sdu +
10414afee4bSReshetova, Elena 		refcount_read(&sk->sk_wmem_alloc)) <= sk->sk_sndbuf;
1051da177e4SLinus Torvalds }
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds static void vcc_write_space(struct sock *sk)
1081da177e4SLinus Torvalds {
10943815482SEric Dumazet 	struct socket_wq *wq;
11043815482SEric Dumazet 
11143815482SEric Dumazet 	rcu_read_lock();
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 	if (vcc_writable(sk)) {
11443815482SEric Dumazet 		wq = rcu_dereference(sk->sk_wq);
1151ce0bf50SHerbert Xu 		if (skwq_has_sleeper(wq))
11643815482SEric Dumazet 			wake_up_interruptible(&wq->wait);
1171da177e4SLinus Torvalds 
1188d8ad9d7SPavel Emelyanov 		sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
1191da177e4SLinus Torvalds 	}
1201da177e4SLinus Torvalds 
12143815482SEric Dumazet 	rcu_read_unlock();
1221da177e4SLinus Torvalds }
1231da177e4SLinus Torvalds 
124c971f08cSDavid Woodhouse static void vcc_release_cb(struct sock *sk)
125c971f08cSDavid Woodhouse {
126c971f08cSDavid Woodhouse 	struct atm_vcc *vcc = atm_sk(sk);
127c971f08cSDavid Woodhouse 
128c971f08cSDavid Woodhouse 	if (vcc->release_cb)
129c971f08cSDavid Woodhouse 		vcc->release_cb(vcc);
130c971f08cSDavid Woodhouse }
131c971f08cSDavid Woodhouse 
1321da177e4SLinus Torvalds static struct proto vcc_proto = {
1331da177e4SLinus Torvalds 	.name	  = "VCC",
1341da177e4SLinus Torvalds 	.owner	  = THIS_MODULE,
1351da177e4SLinus Torvalds 	.obj_size = sizeof(struct atm_vcc),
136c971f08cSDavid Woodhouse 	.release_cb = vcc_release_cb,
1371da177e4SLinus Torvalds };
1381da177e4SLinus Torvalds 
13911aa9c28SEric W. Biederman int vcc_create(struct net *net, struct socket *sock, int protocol, int family, int kern)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	struct sock *sk;
1421da177e4SLinus Torvalds 	struct atm_vcc *vcc;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	sock->sk = NULL;
1451da177e4SLinus Torvalds 	if (sock->type == SOCK_STREAM)
1461da177e4SLinus Torvalds 		return -EINVAL;
14711aa9c28SEric W. Biederman 	sk = sk_alloc(net, family, GFP_KERNEL, &vcc_proto, kern);
1481da177e4SLinus Torvalds 	if (!sk)
1491da177e4SLinus Torvalds 		return -ENOMEM;
1501da177e4SLinus Torvalds 	sock_init_data(sock, sk);
1511da177e4SLinus Torvalds 	sk->sk_state_change = vcc_def_wakeup;
1521da177e4SLinus Torvalds 	sk->sk_write_space = vcc_write_space;
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	vcc = atm_sk(sk);
1551da177e4SLinus Torvalds 	vcc->dev = NULL;
1561da177e4SLinus Torvalds 	memset(&vcc->local, 0, sizeof(struct sockaddr_atmsvc));
1571da177e4SLinus Torvalds 	memset(&vcc->remote, 0, sizeof(struct sockaddr_atmsvc));
1581da177e4SLinus Torvalds 	vcc->qos.txtp.max_sdu = 1 << 16; /* for meta VCs */
15914afee4bSReshetova, Elena 	refcount_set(&sk->sk_wmem_alloc, 1);
1601da177e4SLinus Torvalds 	atomic_set(&sk->sk_rmem_alloc, 0);
1611da177e4SLinus Torvalds 	vcc->push = NULL;
1621da177e4SLinus Torvalds 	vcc->pop = NULL;
163ec809bd8SKrzysztof Mazur 	vcc->owner = NULL;
1641da177e4SLinus Torvalds 	vcc->push_oam = NULL;
165c971f08cSDavid Woodhouse 	vcc->release_cb = NULL;
1661da177e4SLinus Torvalds 	vcc->vpi = vcc->vci = 0; /* no VCI/VPI yet */
1671da177e4SLinus Torvalds 	vcc->atm_options = vcc->aal_options = 0;
1681da177e4SLinus Torvalds 	sk->sk_destruct = vcc_sock_destruct;
1691da177e4SLinus Torvalds 	return 0;
1701da177e4SLinus Torvalds }
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds static void vcc_destroy_socket(struct sock *sk)
1731da177e4SLinus Torvalds {
1741da177e4SLinus Torvalds 	struct atm_vcc *vcc = atm_sk(sk);
1751da177e4SLinus Torvalds 	struct sk_buff *skb;
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	set_bit(ATM_VF_CLOSE, &vcc->flags);
1781da177e4SLinus Torvalds 	clear_bit(ATM_VF_READY, &vcc->flags);
1791da177e4SLinus Torvalds 	if (vcc->dev) {
1801da177e4SLinus Torvalds 		if (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 
1911da177e4SLinus Torvalds 		module_put(vcc->dev->ops->owner);
1921da177e4SLinus Torvalds 		atm_dev_put(vcc->dev);
1931da177e4SLinus Torvalds 	}
1949301e320SChas Williams 
1959301e320SChas Williams 	vcc_remove_socket(sk);
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds int vcc_release(struct socket *sock)
1991da177e4SLinus Torvalds {
2001da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	if (sk) {
2031da177e4SLinus Torvalds 		lock_sock(sk);
2041da177e4SLinus Torvalds 		vcc_destroy_socket(sock->sk);
2051da177e4SLinus Torvalds 		release_sock(sk);
2061da177e4SLinus Torvalds 		sock_put(sk);
2071da177e4SLinus Torvalds 	}
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	return 0;
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds void vcc_release_async(struct atm_vcc *vcc, int reply)
2131da177e4SLinus Torvalds {
2141da177e4SLinus Torvalds 	struct sock *sk = sk_atm(vcc);
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	set_bit(ATM_VF_CLOSE, &vcc->flags);
2171da177e4SLinus Torvalds 	sk->sk_shutdown |= RCV_SHUTDOWN;
2181da177e4SLinus Torvalds 	sk->sk_err = -reply;
2191da177e4SLinus Torvalds 	clear_bit(ATM_VF_WAITING, &vcc->flags);
2201da177e4SLinus Torvalds 	sk->sk_state_change(sk);
2211da177e4SLinus Torvalds }
2221da177e4SLinus Torvalds EXPORT_SYMBOL(vcc_release_async);
2231da177e4SLinus Torvalds 
2244e55f578SJorge Boncompte [DTI2] void vcc_process_recv_queue(struct atm_vcc *vcc)
2254e55f578SJorge Boncompte [DTI2] {
2264e55f578SJorge Boncompte [DTI2] 	struct sk_buff_head queue, *rq;
2274e55f578SJorge Boncompte [DTI2] 	struct sk_buff *skb, *tmp;
2284e55f578SJorge Boncompte [DTI2] 	unsigned long flags;
2294e55f578SJorge Boncompte [DTI2] 
2304e55f578SJorge Boncompte [DTI2] 	__skb_queue_head_init(&queue);
2314e55f578SJorge Boncompte [DTI2] 	rq = &sk_atm(vcc)->sk_receive_queue;
2324e55f578SJorge Boncompte [DTI2] 
2334e55f578SJorge Boncompte [DTI2] 	spin_lock_irqsave(&rq->lock, flags);
2344e55f578SJorge Boncompte [DTI2] 	skb_queue_splice_init(rq, &queue);
2354e55f578SJorge Boncompte [DTI2] 	spin_unlock_irqrestore(&rq->lock, flags);
2364e55f578SJorge Boncompte [DTI2] 
2374e55f578SJorge Boncompte [DTI2] 	skb_queue_walk_safe(&queue, skb, tmp) {
2384e55f578SJorge Boncompte [DTI2] 		__skb_unlink(skb, &queue);
2394e55f578SJorge Boncompte [DTI2] 		vcc->push(vcc, skb);
2404e55f578SJorge Boncompte [DTI2] 	}
2414e55f578SJorge Boncompte [DTI2] }
2424e55f578SJorge Boncompte [DTI2] EXPORT_SYMBOL(vcc_process_recv_queue);
2434e55f578SJorge Boncompte [DTI2] 
2447313bb8fSKarl Hiramoto void atm_dev_signal_change(struct atm_dev *dev, char signal)
2457313bb8fSKarl Hiramoto {
2467313bb8fSKarl Hiramoto 	pr_debug("%s signal=%d dev=%p number=%d dev->signal=%d\n",
2477313bb8fSKarl Hiramoto 		__func__, signal, dev, dev->number, dev->signal);
2487313bb8fSKarl Hiramoto 
2497313bb8fSKarl Hiramoto 	/* atm driver sending invalid signal */
2507313bb8fSKarl Hiramoto 	WARN_ON(signal < ATM_PHY_SIG_LOST || signal > ATM_PHY_SIG_FOUND);
2517313bb8fSKarl Hiramoto 
2527313bb8fSKarl Hiramoto 	if (dev->signal == signal)
2537313bb8fSKarl Hiramoto 		return; /* no change */
2547313bb8fSKarl Hiramoto 
2557313bb8fSKarl Hiramoto 	dev->signal = signal;
2567313bb8fSKarl Hiramoto 
2577313bb8fSKarl Hiramoto 	atomic_notifier_call_chain(&atm_dev_notify_chain, signal, dev);
2587313bb8fSKarl Hiramoto }
2597313bb8fSKarl Hiramoto EXPORT_SYMBOL(atm_dev_signal_change);
2601da177e4SLinus Torvalds 
26164bf69ddSStanislaw Gruszka void atm_dev_release_vccs(struct atm_dev *dev)
26264bf69ddSStanislaw Gruszka {
26364bf69ddSStanislaw Gruszka 	int i;
26464bf69ddSStanislaw Gruszka 
26564bf69ddSStanislaw Gruszka 	write_lock_irq(&vcc_sklist_lock);
26664bf69ddSStanislaw Gruszka 	for (i = 0; i < VCC_HTABLE_SIZE; i++) {
26764bf69ddSStanislaw Gruszka 		struct hlist_head *head = &vcc_hash[i];
268b67bfe0dSSasha Levin 		struct hlist_node *tmp;
26964bf69ddSStanislaw Gruszka 		struct sock *s;
27064bf69ddSStanislaw Gruszka 		struct atm_vcc *vcc;
27164bf69ddSStanislaw Gruszka 
272b67bfe0dSSasha Levin 		sk_for_each_safe(s, tmp, head) {
27364bf69ddSStanislaw Gruszka 			vcc = atm_sk(s);
27464bf69ddSStanislaw Gruszka 			if (vcc->dev == dev) {
27564bf69ddSStanislaw Gruszka 				vcc_release_async(vcc, -EPIPE);
27664bf69ddSStanislaw Gruszka 				sk_del_node_init(s);
27764bf69ddSStanislaw Gruszka 			}
27864bf69ddSStanislaw Gruszka 		}
27964bf69ddSStanislaw Gruszka 	}
28064bf69ddSStanislaw Gruszka 	write_unlock_irq(&vcc_sklist_lock);
28164bf69ddSStanislaw Gruszka }
282c031235bSPhilip A. Prindeville EXPORT_SYMBOL(atm_dev_release_vccs);
28364bf69ddSStanislaw Gruszka 
2841da177e4SLinus Torvalds static int adjust_tp(struct atm_trafprm *tp, unsigned char aal)
2851da177e4SLinus Torvalds {
2861da177e4SLinus Torvalds 	int max_sdu;
2871da177e4SLinus Torvalds 
288a8147d73SJoe Perches 	if (!tp->traffic_class)
289a8147d73SJoe Perches 		return 0;
2901da177e4SLinus Torvalds 	switch (aal) {
2911da177e4SLinus Torvalds 	case ATM_AAL0:
2921da177e4SLinus Torvalds 		max_sdu = ATM_CELL_SIZE-1;
2931da177e4SLinus Torvalds 		break;
2941da177e4SLinus Torvalds 	case ATM_AAL34:
2951da177e4SLinus Torvalds 		max_sdu = ATM_MAX_AAL34_PDU;
2961da177e4SLinus Torvalds 		break;
2971da177e4SLinus Torvalds 	default:
298ef423a41SJoe Perches 		pr_warn("AAL problems ... (%d)\n", aal);
2991da177e4SLinus Torvalds 		/* fall through */
3001da177e4SLinus Torvalds 	case ATM_AAL5:
3011da177e4SLinus Torvalds 		max_sdu = ATM_MAX_AAL5_PDU;
3021da177e4SLinus Torvalds 	}
303a8147d73SJoe Perches 	if (!tp->max_sdu)
304a8147d73SJoe Perches 		tp->max_sdu = max_sdu;
305a8147d73SJoe Perches 	else if (tp->max_sdu > max_sdu)
306a8147d73SJoe Perches 		return -EINVAL;
307a8147d73SJoe Perches 	if (!tp->max_cdv)
308a8147d73SJoe Perches 		tp->max_cdv = ATM_MAX_CDV;
3091da177e4SLinus Torvalds 	return 0;
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
31261c33e01SMitchell Blank Jr static int check_ci(const struct atm_vcc *vcc, short vpi, int vci)
3131da177e4SLinus Torvalds {
314a8147d73SJoe Perches 	struct hlist_head *head = &vcc_hash[vci & (VCC_HTABLE_SIZE - 1)];
3151da177e4SLinus Torvalds 	struct sock *s;
3161da177e4SLinus Torvalds 	struct atm_vcc *walk;
3171da177e4SLinus Torvalds 
318b67bfe0dSSasha Levin 	sk_for_each(s, head) {
3191da177e4SLinus Torvalds 		walk = atm_sk(s);
3201da177e4SLinus Torvalds 		if (walk->dev != vcc->dev)
3211da177e4SLinus Torvalds 			continue;
3221da177e4SLinus Torvalds 		if (test_bit(ATM_VF_ADDR, &walk->flags) && walk->vpi == vpi &&
3231da177e4SLinus Torvalds 		    walk->vci == vci && ((walk->qos.txtp.traffic_class !=
3241da177e4SLinus Torvalds 		    ATM_NONE && vcc->qos.txtp.traffic_class != ATM_NONE) ||
3251da177e4SLinus Torvalds 		    (walk->qos.rxtp.traffic_class != ATM_NONE &&
3261da177e4SLinus Torvalds 		    vcc->qos.rxtp.traffic_class != ATM_NONE)))
3271da177e4SLinus Torvalds 			return -EADDRINUSE;
3281da177e4SLinus Torvalds 	}
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	/* allow VCCs with same VPI/VCI iff they don't collide on
3311da177e4SLinus Torvalds 	   TX/RX (but we may refuse such sharing for other reasons,
3321da177e4SLinus Torvalds 	   e.g. if protocol requires to have both channels) */
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	return 0;
3351da177e4SLinus Torvalds }
3361da177e4SLinus Torvalds 
33761c33e01SMitchell Blank Jr static int find_ci(const struct atm_vcc *vcc, short *vpi, int *vci)
3381da177e4SLinus Torvalds {
3391da177e4SLinus Torvalds 	static short p;        /* poor man's per-device cache */
3401da177e4SLinus Torvalds 	static int c;
3411da177e4SLinus Torvalds 	short old_p;
3421da177e4SLinus Torvalds 	int old_c;
3431da177e4SLinus Torvalds 	int err;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	if (*vpi != ATM_VPI_ANY && *vci != ATM_VCI_ANY) {
3461da177e4SLinus Torvalds 		err = check_ci(vcc, *vpi, *vci);
3471da177e4SLinus Torvalds 		return err;
3481da177e4SLinus Torvalds 	}
3491da177e4SLinus Torvalds 	/* last scan may have left values out of bounds for current device */
3501da177e4SLinus Torvalds 	if (*vpi != ATM_VPI_ANY)
3511da177e4SLinus Torvalds 		p = *vpi;
3521da177e4SLinus Torvalds 	else if (p >= 1 << vcc->dev->ci_range.vpi_bits)
3531da177e4SLinus Torvalds 		p = 0;
3541da177e4SLinus Torvalds 	if (*vci != ATM_VCI_ANY)
3551da177e4SLinus Torvalds 		c = *vci;
3561da177e4SLinus Torvalds 	else if (c < ATM_NOT_RSV_VCI || c >= 1 << vcc->dev->ci_range.vci_bits)
3571da177e4SLinus Torvalds 			c = ATM_NOT_RSV_VCI;
3581da177e4SLinus Torvalds 	old_p = p;
3591da177e4SLinus Torvalds 	old_c = c;
3601da177e4SLinus Torvalds 	do {
3611da177e4SLinus Torvalds 		if (!check_ci(vcc, p, c)) {
3621da177e4SLinus Torvalds 			*vpi = p;
3631da177e4SLinus Torvalds 			*vci = c;
3641da177e4SLinus Torvalds 			return 0;
3651da177e4SLinus Torvalds 		}
3661da177e4SLinus Torvalds 		if (*vci == ATM_VCI_ANY) {
3671da177e4SLinus Torvalds 			c++;
3681da177e4SLinus Torvalds 			if (c >= 1 << vcc->dev->ci_range.vci_bits)
3691da177e4SLinus Torvalds 				c = ATM_NOT_RSV_VCI;
3701da177e4SLinus Torvalds 		}
3711da177e4SLinus Torvalds 		if ((c == ATM_NOT_RSV_VCI || *vci != ATM_VCI_ANY) &&
3721da177e4SLinus Torvalds 		    *vpi == ATM_VPI_ANY) {
3731da177e4SLinus Torvalds 			p++;
374a8147d73SJoe Perches 			if (p >= 1 << vcc->dev->ci_range.vpi_bits)
375a8147d73SJoe Perches 				p = 0;
3761da177e4SLinus Torvalds 		}
377a8147d73SJoe Perches 	} while (old_p != p || old_c != c);
3781da177e4SLinus Torvalds 	return -EADDRINUSE;
3791da177e4SLinus Torvalds }
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds static int __vcc_connect(struct atm_vcc *vcc, struct atm_dev *dev, short vpi,
3821da177e4SLinus Torvalds 			 int vci)
3831da177e4SLinus Torvalds {
3841da177e4SLinus Torvalds 	struct sock *sk = sk_atm(vcc);
3851da177e4SLinus Torvalds 	int error;
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	if ((vpi != ATM_VPI_UNSPEC && vpi != ATM_VPI_ANY &&
3881da177e4SLinus Torvalds 	    vpi >> dev->ci_range.vpi_bits) || (vci != ATM_VCI_UNSPEC &&
3891da177e4SLinus Torvalds 	    vci != ATM_VCI_ANY && vci >> dev->ci_range.vci_bits))
3901da177e4SLinus Torvalds 		return -EINVAL;
3911da177e4SLinus Torvalds 	if (vci > 0 && vci < ATM_NOT_RSV_VCI && !capable(CAP_NET_BIND_SERVICE))
3921da177e4SLinus Torvalds 		return -EPERM;
39364bf69ddSStanislaw Gruszka 	error = -ENODEV;
3941da177e4SLinus Torvalds 	if (!try_module_get(dev->ops->owner))
39564bf69ddSStanislaw Gruszka 		return error;
3961da177e4SLinus Torvalds 	vcc->dev = dev;
3971da177e4SLinus Torvalds 	write_lock_irq(&vcc_sklist_lock);
39864bf69ddSStanislaw Gruszka 	if (test_bit(ATM_DF_REMOVED, &dev->flags) ||
39964bf69ddSStanislaw Gruszka 	    (error = find_ci(vcc, &vpi, &vci))) {
4001da177e4SLinus Torvalds 		write_unlock_irq(&vcc_sklist_lock);
4011da177e4SLinus Torvalds 		goto fail_module_put;
4021da177e4SLinus Torvalds 	}
4031da177e4SLinus Torvalds 	vcc->vpi = vpi;
4041da177e4SLinus Torvalds 	vcc->vci = vci;
4051da177e4SLinus Torvalds 	__vcc_insert_socket(sk);
4061da177e4SLinus Torvalds 	write_unlock_irq(&vcc_sklist_lock);
4071da177e4SLinus Torvalds 	switch (vcc->qos.aal) {
4081da177e4SLinus Torvalds 	case ATM_AAL0:
4091da177e4SLinus Torvalds 		error = atm_init_aal0(vcc);
4101da177e4SLinus Torvalds 		vcc->stats = &dev->stats.aal0;
4111da177e4SLinus Torvalds 		break;
4121da177e4SLinus Torvalds 	case ATM_AAL34:
4131da177e4SLinus Torvalds 		error = atm_init_aal34(vcc);
4141da177e4SLinus Torvalds 		vcc->stats = &dev->stats.aal34;
4151da177e4SLinus Torvalds 		break;
4161da177e4SLinus Torvalds 	case ATM_NO_AAL:
4171da177e4SLinus Torvalds 		/* ATM_AAL5 is also used in the "0 for default" case */
4181da177e4SLinus Torvalds 		vcc->qos.aal = ATM_AAL5;
4191da177e4SLinus Torvalds 		/* fall through */
4201da177e4SLinus Torvalds 	case ATM_AAL5:
4211da177e4SLinus Torvalds 		error = atm_init_aal5(vcc);
4221da177e4SLinus Torvalds 		vcc->stats = &dev->stats.aal5;
4231da177e4SLinus Torvalds 		break;
4241da177e4SLinus Torvalds 	default:
4251da177e4SLinus Torvalds 		error = -EPROTOTYPE;
4261da177e4SLinus Torvalds 	}
427a8147d73SJoe Perches 	if (!error)
428a8147d73SJoe Perches 		error = adjust_tp(&vcc->qos.txtp, vcc->qos.aal);
429a8147d73SJoe Perches 	if (!error)
430a8147d73SJoe Perches 		error = adjust_tp(&vcc->qos.rxtp, vcc->qos.aal);
4311da177e4SLinus Torvalds 	if (error)
4321da177e4SLinus Torvalds 		goto fail;
43352240062SStephen Hemminger 	pr_debug("VCC %d.%d, AAL %d\n", vpi, vci, vcc->qos.aal);
43499824461SJoe Perches 	pr_debug("  TX: %d, PCR %d..%d, SDU %d\n",
43599824461SJoe Perches 		 vcc->qos.txtp.traffic_class,
43699824461SJoe Perches 		 vcc->qos.txtp.min_pcr,
43799824461SJoe Perches 		 vcc->qos.txtp.max_pcr,
43899824461SJoe Perches 		 vcc->qos.txtp.max_sdu);
43999824461SJoe Perches 	pr_debug("  RX: %d, PCR %d..%d, SDU %d\n",
44099824461SJoe Perches 		 vcc->qos.rxtp.traffic_class,
44199824461SJoe Perches 		 vcc->qos.rxtp.min_pcr,
44299824461SJoe Perches 		 vcc->qos.rxtp.max_pcr,
44399824461SJoe Perches 		 vcc->qos.rxtp.max_sdu);
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	if (dev->ops->open) {
446a8147d73SJoe Perches 		error = dev->ops->open(vcc);
447a8147d73SJoe Perches 		if (error)
4481da177e4SLinus Torvalds 			goto fail;
4491da177e4SLinus Torvalds 	}
4501da177e4SLinus Torvalds 	return 0;
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds fail:
4531da177e4SLinus Torvalds 	vcc_remove_socket(sk);
4541da177e4SLinus Torvalds fail_module_put:
4551da177e4SLinus Torvalds 	module_put(dev->ops->owner);
4561da177e4SLinus Torvalds 	/* ensure we get dev module ref count correct */
4571da177e4SLinus Torvalds 	vcc->dev = NULL;
4581da177e4SLinus Torvalds 	return error;
4591da177e4SLinus Torvalds }
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds int vcc_connect(struct socket *sock, int itf, short vpi, int vci)
4621da177e4SLinus Torvalds {
4631da177e4SLinus Torvalds 	struct atm_dev *dev;
4641da177e4SLinus Torvalds 	struct atm_vcc *vcc = ATM_SD(sock);
4651da177e4SLinus Torvalds 	int error;
4661da177e4SLinus Torvalds 
46799824461SJoe Perches 	pr_debug("(vpi %d, vci %d)\n", vpi, vci);
4681da177e4SLinus Torvalds 	if (sock->state == SS_CONNECTED)
4691da177e4SLinus Torvalds 		return -EISCONN;
4701da177e4SLinus Torvalds 	if (sock->state != SS_UNCONNECTED)
4711da177e4SLinus Torvalds 		return -EINVAL;
4721da177e4SLinus Torvalds 	if (!(vpi || vci))
4731da177e4SLinus Torvalds 		return -EINVAL;
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	if (vpi != ATM_VPI_UNSPEC && vci != ATM_VCI_UNSPEC)
4761da177e4SLinus Torvalds 		clear_bit(ATM_VF_PARTIAL, &vcc->flags);
4771da177e4SLinus Torvalds 	else
4781da177e4SLinus Torvalds 		if (test_bit(ATM_VF_PARTIAL, &vcc->flags))
4791da177e4SLinus Torvalds 			return -EINVAL;
48099824461SJoe Perches 	pr_debug("(TX: cl %d,bw %d-%d,sdu %d; "
4811da177e4SLinus Torvalds 		 "RX: cl %d,bw %d-%d,sdu %d,AAL %s%d)\n",
4821da177e4SLinus Torvalds 		 vcc->qos.txtp.traffic_class, vcc->qos.txtp.min_pcr,
4831da177e4SLinus Torvalds 		 vcc->qos.txtp.max_pcr, vcc->qos.txtp.max_sdu,
4841da177e4SLinus Torvalds 		 vcc->qos.rxtp.traffic_class, vcc->qos.rxtp.min_pcr,
4851da177e4SLinus Torvalds 		 vcc->qos.rxtp.max_pcr, vcc->qos.rxtp.max_sdu,
48699824461SJoe Perches 		 vcc->qos.aal == ATM_AAL5 ? "" :
48799824461SJoe Perches 		 vcc->qos.aal == ATM_AAL0 ? "" : " ??? code ",
48899824461SJoe Perches 		 vcc->qos.aal == ATM_AAL0 ? 0 : vcc->qos.aal);
4891da177e4SLinus Torvalds 	if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
4901da177e4SLinus Torvalds 		return -EBADFD;
4911da177e4SLinus Torvalds 	if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
4921da177e4SLinus Torvalds 	    vcc->qos.rxtp.traffic_class == ATM_ANYCLASS)
4931da177e4SLinus Torvalds 		return -EINVAL;
494c9933d08SMitchell Blank Jr 	if (likely(itf != ATM_ITF_ANY)) {
495a8147d73SJoe Perches 		dev = try_then_request_module(atm_dev_lookup(itf),
496a8147d73SJoe Perches 					      "atm-device-%d", itf);
497c9933d08SMitchell Blank Jr 	} else {
498c9933d08SMitchell Blank Jr 		dev = NULL;
49957b47a53SIngo Molnar 		mutex_lock(&atm_dev_mutex);
500c9933d08SMitchell Blank Jr 		if (!list_empty(&atm_devs)) {
501a8147d73SJoe Perches 			dev = list_entry(atm_devs.next,
502a8147d73SJoe Perches 					 struct atm_dev, dev_list);
503c9933d08SMitchell Blank Jr 			atm_dev_hold(dev);
504c9933d08SMitchell Blank Jr 		}
50557b47a53SIngo Molnar 		mutex_unlock(&atm_dev_mutex);
506c9933d08SMitchell Blank Jr 	}
5071da177e4SLinus Torvalds 	if (!dev)
5081da177e4SLinus Torvalds 		return -ENODEV;
5091da177e4SLinus Torvalds 	error = __vcc_connect(vcc, dev, vpi, vci);
5101da177e4SLinus Torvalds 	if (error) {
5111da177e4SLinus Torvalds 		atm_dev_put(dev);
5121da177e4SLinus Torvalds 		return error;
5131da177e4SLinus Torvalds 	}
5141da177e4SLinus Torvalds 	if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC)
5151da177e4SLinus Torvalds 		set_bit(ATM_VF_PARTIAL, &vcc->flags);
5161da177e4SLinus Torvalds 	if (test_bit(ATM_VF_READY, &ATM_SD(sock)->flags))
5171da177e4SLinus Torvalds 		sock->state = SS_CONNECTED;
5181da177e4SLinus Torvalds 	return 0;
5191da177e4SLinus Torvalds }
5201da177e4SLinus Torvalds 
5211b784140SYing Xue int vcc_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
5221b784140SYing Xue 		int flags)
5231da177e4SLinus Torvalds {
5241da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
5251da177e4SLinus Torvalds 	struct atm_vcc *vcc;
5261da177e4SLinus Torvalds 	struct sk_buff *skb;
5271da177e4SLinus Torvalds 	int copied, error = -EINVAL;
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	if (sock->state != SS_CONNECTED)
5301da177e4SLinus Torvalds 		return -ENOTCONN;
53140ba8499SJorge Boncompte [DTI2] 
53240ba8499SJorge Boncompte [DTI2] 	/* only handle MSG_DONTWAIT and MSG_PEEK */
53340ba8499SJorge Boncompte [DTI2] 	if (flags & ~(MSG_DONTWAIT | MSG_PEEK))
5341da177e4SLinus Torvalds 		return -EOPNOTSUPP;
53540ba8499SJorge Boncompte [DTI2] 
5361da177e4SLinus Torvalds 	vcc = ATM_SD(sock);
5371da177e4SLinus Torvalds 	if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
5381da177e4SLinus Torvalds 	    test_bit(ATM_VF_CLOSE, &vcc->flags) ||
5391da177e4SLinus Torvalds 	    !test_bit(ATM_VF_READY, &vcc->flags))
5401da177e4SLinus Torvalds 		return 0;
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &error);
5431da177e4SLinus Torvalds 	if (!skb)
5441da177e4SLinus Torvalds 		return error;
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds 	copied = skb->len;
5471da177e4SLinus Torvalds 	if (copied > size) {
5481da177e4SLinus Torvalds 		copied = size;
5491da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
5501da177e4SLinus Torvalds 	}
5511da177e4SLinus Torvalds 
55251f3d02bSDavid S. Miller 	error = skb_copy_datagram_msg(skb, 0, msg, copied);
5531da177e4SLinus Torvalds 	if (error)
5541da177e4SLinus Torvalds 		return error;
5553b885787SNeil Horman 	sock_recv_ts_and_drops(msg, sk, skb);
55640ba8499SJorge Boncompte [DTI2] 
55740ba8499SJorge Boncompte [DTI2] 	if (!(flags & MSG_PEEK)) {
55840ba8499SJorge Boncompte [DTI2] 		pr_debug("%d -= %d\n", atomic_read(&sk->sk_rmem_alloc),
55940ba8499SJorge Boncompte [DTI2] 			 skb->truesize);
5601da177e4SLinus Torvalds 		atm_return(vcc, skb->truesize);
56140ba8499SJorge Boncompte [DTI2] 	}
56240ba8499SJorge Boncompte [DTI2] 
5631da177e4SLinus Torvalds 	skb_free_datagram(sk, skb);
5641da177e4SLinus Torvalds 	return copied;
5651da177e4SLinus Torvalds }
5661da177e4SLinus Torvalds 
5671b784140SYing Xue int vcc_sendmsg(struct socket *sock, struct msghdr *m, size_t size)
5681da177e4SLinus Torvalds {
5691da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
5701da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
5711da177e4SLinus Torvalds 	struct atm_vcc *vcc;
5721da177e4SLinus Torvalds 	struct sk_buff *skb;
5731da177e4SLinus Torvalds 	int eff, error;
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	lock_sock(sk);
5761da177e4SLinus Torvalds 	if (sock->state != SS_CONNECTED) {
5771da177e4SLinus Torvalds 		error = -ENOTCONN;
5781da177e4SLinus Torvalds 		goto out;
5791da177e4SLinus Torvalds 	}
5801da177e4SLinus Torvalds 	if (m->msg_name) {
5811da177e4SLinus Torvalds 		error = -EISCONN;
5821da177e4SLinus Torvalds 		goto out;
5831da177e4SLinus Torvalds 	}
5841da177e4SLinus Torvalds 	vcc = ATM_SD(sock);
5851da177e4SLinus Torvalds 	if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
5861da177e4SLinus Torvalds 	    test_bit(ATM_VF_CLOSE, &vcc->flags) ||
5871da177e4SLinus Torvalds 	    !test_bit(ATM_VF_READY, &vcc->flags)) {
5881da177e4SLinus Torvalds 		error = -EPIPE;
5891da177e4SLinus Torvalds 		send_sig(SIGPIPE, current, 0);
5901da177e4SLinus Torvalds 		goto out;
5911da177e4SLinus Torvalds 	}
5921da177e4SLinus Torvalds 	if (!size) {
5931da177e4SLinus Torvalds 		error = 0;
5941da177e4SLinus Torvalds 		goto out;
5951da177e4SLinus Torvalds 	}
5967424ce65SAl Viro 	if (size > vcc->qos.txtp.max_sdu) {
5971da177e4SLinus Torvalds 		error = -EMSGSIZE;
5981da177e4SLinus Torvalds 		goto out;
5991da177e4SLinus Torvalds 	}
600e49332bdSJesper Juhl 
6011da177e4SLinus Torvalds 	eff = (size+3) & ~3; /* align to word boundary */
602aa395145SEric Dumazet 	prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
6031da177e4SLinus Torvalds 	error = 0;
604c55fa3ccSFrancois Romieu 	while (!vcc_tx_ready(vcc, eff)) {
6051da177e4SLinus Torvalds 		if (m->msg_flags & MSG_DONTWAIT) {
6061da177e4SLinus Torvalds 			error = -EAGAIN;
6071da177e4SLinus Torvalds 			break;
6081da177e4SLinus Torvalds 		}
6091da177e4SLinus Torvalds 		schedule();
6101da177e4SLinus Torvalds 		if (signal_pending(current)) {
6111da177e4SLinus Torvalds 			error = -ERESTARTSYS;
6121da177e4SLinus Torvalds 			break;
6131da177e4SLinus Torvalds 		}
6141da177e4SLinus Torvalds 		if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
6151da177e4SLinus Torvalds 		    test_bit(ATM_VF_CLOSE, &vcc->flags) ||
6161da177e4SLinus Torvalds 		    !test_bit(ATM_VF_READY, &vcc->flags)) {
6171da177e4SLinus Torvalds 			error = -EPIPE;
6181da177e4SLinus Torvalds 			send_sig(SIGPIPE, current, 0);
6191da177e4SLinus Torvalds 			break;
6201da177e4SLinus Torvalds 		}
621aa395145SEric Dumazet 		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
6221da177e4SLinus Torvalds 	}
623aa395145SEric Dumazet 	finish_wait(sk_sleep(sk), &wait);
6241da177e4SLinus Torvalds 	if (error)
6251da177e4SLinus Torvalds 		goto out;
626c55fa3ccSFrancois Romieu 
627c55fa3ccSFrancois Romieu 	skb = alloc_skb(eff, GFP_KERNEL);
628c55fa3ccSFrancois Romieu 	if (!skb) {
629c55fa3ccSFrancois Romieu 		error = -ENOMEM;
630c55fa3ccSFrancois Romieu 		goto out;
631c55fa3ccSFrancois Romieu 	}
632c55fa3ccSFrancois Romieu 	pr_debug("%d += %d\n", sk_wmem_alloc_get(sk), skb->truesize);
63314afee4bSReshetova, Elena 	refcount_add(skb->truesize, &sk->sk_wmem_alloc);
634c55fa3ccSFrancois Romieu 
6351da177e4SLinus Torvalds 	skb->dev = NULL; /* for paths shared with net_device interfaces */
6361da177e4SLinus Torvalds 	ATM_SKB(skb)->atm_options = vcc->atm_options;
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 
6511da177e4SLinus Torvalds unsigned int vcc_poll(struct file *file, struct socket *sock, poll_table *wait)
6521da177e4SLinus Torvalds {
6531da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
6541da177e4SLinus Torvalds 	struct atm_vcc *vcc;
6551da177e4SLinus Torvalds 	unsigned int mask;
6561da177e4SLinus Torvalds 
657aa395145SEric Dumazet 	sock_poll_wait(file, sk_sleep(sk), wait);
6581da177e4SLinus Torvalds 	mask = 0;
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 	vcc = ATM_SD(sock);
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 	/* exceptional events */
6631da177e4SLinus Torvalds 	if (sk->sk_err)
6641da177e4SLinus Torvalds 		mask = POLLERR;
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
6671da177e4SLinus Torvalds 	    test_bit(ATM_VF_CLOSE, &vcc->flags))
6681da177e4SLinus Torvalds 		mask |= POLLHUP;
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 	/* readable? */
6711da177e4SLinus Torvalds 	if (!skb_queue_empty(&sk->sk_receive_queue))
6721da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
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))
6811da177e4SLinus Torvalds 		mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 	return mask;
6841da177e4SLinus Torvalds }
6851da177e4SLinus Torvalds 
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 
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 
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 
7471da177e4SLinus Torvalds int vcc_setsockopt(struct socket *sock, int level, int optname,
748b7058842SDavid S. Miller 		   char __user *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 
7631da177e4SLinus Torvalds 		if (copy_from_user(&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:
7771da177e4SLinus Torvalds 		if (get_user(value, (unsigned long __user *)optval))
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 		if (level == SOL_SOCKET)
786a8147d73SJoe Perches 			return -EINVAL;
7871da177e4SLinus Torvalds 		break;
7881da177e4SLinus Torvalds 	}
789a8147d73SJoe Perches 	if (!vcc->dev || !vcc->dev->ops->setsockopt)
790a8147d73SJoe Perches 		return -EINVAL;
7911da177e4SLinus Torvalds 	return vcc->dev->ops->setsockopt(vcc, level, optname, optval, optlen);
7921da177e4SLinus Torvalds }
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds int vcc_getsockopt(struct socket *sock, int level, int optname,
7951da177e4SLinus Torvalds 		   char __user *optval, int __user *optlen)
7961da177e4SLinus Torvalds {
7971da177e4SLinus Torvalds 	struct atm_vcc *vcc;
7981da177e4SLinus Torvalds 	int len;
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds 	if (get_user(len, optlen))
8011da177e4SLinus Torvalds 		return -EFAULT;
8021da177e4SLinus Torvalds 	if (__SO_LEVEL_MATCH(optname, level) && len != __SO_SIZE(optname))
8031da177e4SLinus Torvalds 		return -EINVAL;
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 	vcc = ATM_SD(sock);
8061da177e4SLinus Torvalds 	switch (optname) {
8071da177e4SLinus Torvalds 	case SO_ATMQOS:
8081da177e4SLinus Torvalds 		if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
8091da177e4SLinus Torvalds 			return -EINVAL;
810a8147d73SJoe Perches 		return copy_to_user(optval, &vcc->qos, sizeof(vcc->qos))
811a8147d73SJoe Perches 			? -EFAULT : 0;
8121da177e4SLinus Torvalds 	case SO_SETCLP:
813a8147d73SJoe Perches 		return put_user(vcc->atm_options & ATM_ATMOPT_CLP ? 1 : 0,
814a8147d73SJoe Perches 				(unsigned long __user *)optval) ? -EFAULT : 0;
8151da177e4SLinus Torvalds 	case SO_ATMPVC:
8161da177e4SLinus Torvalds 	{
8171da177e4SLinus Torvalds 		struct sockaddr_atmpvc pvc;
8181da177e4SLinus Torvalds 
819a8147d73SJoe Perches 		if (!vcc->dev || !test_bit(ATM_VF_ADDR, &vcc->flags))
8201da177e4SLinus Torvalds 			return -ENOTCONN;
821e862f1a9SMathias Krause 		memset(&pvc, 0, sizeof(pvc));
8221da177e4SLinus Torvalds 		pvc.sap_family = AF_ATMPVC;
8231da177e4SLinus Torvalds 		pvc.sap_addr.itf = vcc->dev->number;
8241da177e4SLinus Torvalds 		pvc.sap_addr.vpi = vcc->vpi;
8251da177e4SLinus Torvalds 		pvc.sap_addr.vci = vcc->vci;
826a8147d73SJoe Perches 		return copy_to_user(optval, &pvc, sizeof(pvc)) ? -EFAULT : 0;
8271da177e4SLinus Torvalds 	}
8281da177e4SLinus Torvalds 	default:
829a8147d73SJoe Perches 		if (level == SOL_SOCKET)
830a8147d73SJoe Perches 			return -EINVAL;
8311da177e4SLinus Torvalds 		break;
8321da177e4SLinus Torvalds 	}
833a8147d73SJoe Perches 	if (!vcc->dev || !vcc->dev->ops->getsockopt)
834a8147d73SJoe Perches 		return -EINVAL;
8351da177e4SLinus Torvalds 	return vcc->dev->ops->getsockopt(vcc, level, optname, optval, len);
8361da177e4SLinus Torvalds }
8371da177e4SLinus Torvalds 
8387313bb8fSKarl Hiramoto int register_atmdevice_notifier(struct notifier_block *nb)
8397313bb8fSKarl Hiramoto {
8407313bb8fSKarl Hiramoto 	return atomic_notifier_chain_register(&atm_dev_notify_chain, nb);
8417313bb8fSKarl Hiramoto }
8427313bb8fSKarl Hiramoto EXPORT_SYMBOL_GPL(register_atmdevice_notifier);
8437313bb8fSKarl Hiramoto 
8447313bb8fSKarl Hiramoto void unregister_atmdevice_notifier(struct notifier_block *nb)
8457313bb8fSKarl Hiramoto {
8467313bb8fSKarl Hiramoto 	atomic_notifier_chain_unregister(&atm_dev_notify_chain, nb);
8477313bb8fSKarl Hiramoto }
8487313bb8fSKarl Hiramoto EXPORT_SYMBOL_GPL(unregister_atmdevice_notifier);
8497313bb8fSKarl Hiramoto 
8501da177e4SLinus Torvalds static int __init atm_init(void)
8511da177e4SLinus Torvalds {
8521da177e4SLinus Torvalds 	int error;
8531da177e4SLinus Torvalds 
854a8147d73SJoe Perches 	error = proto_register(&vcc_proto, 0);
855a8147d73SJoe Perches 	if (error < 0)
8561da177e4SLinus Torvalds 		goto out;
857a8147d73SJoe Perches 	error = atmpvc_init();
858a8147d73SJoe Perches 	if (error < 0) {
85999824461SJoe Perches 		pr_err("atmpvc_init() failed with %d\n", error);
8601da177e4SLinus Torvalds 		goto out_unregister_vcc_proto;
8611da177e4SLinus Torvalds 	}
862a8147d73SJoe Perches 	error = atmsvc_init();
863a8147d73SJoe Perches 	if (error < 0) {
86499824461SJoe Perches 		pr_err("atmsvc_init() failed with %d\n", error);
8651da177e4SLinus Torvalds 		goto out_atmpvc_exit;
8661da177e4SLinus Torvalds 	}
867a8147d73SJoe Perches 	error = atm_proc_init();
868a8147d73SJoe Perches 	if (error < 0) {
86999824461SJoe Perches 		pr_err("atm_proc_init() failed with %d\n", error);
8701da177e4SLinus Torvalds 		goto out_atmsvc_exit;
8711da177e4SLinus Torvalds 	}
872a8147d73SJoe Perches 	error = atm_sysfs_init();
873a8147d73SJoe Perches 	if (error < 0) {
87499824461SJoe Perches 		pr_err("atm_sysfs_init() failed with %d\n", error);
875656d98b0SRoman Kagan 		goto out_atmproc_exit;
876656d98b0SRoman Kagan 	}
8771da177e4SLinus Torvalds out:
8781da177e4SLinus Torvalds 	return error;
879656d98b0SRoman Kagan out_atmproc_exit:
880656d98b0SRoman Kagan 	atm_proc_exit();
8811da177e4SLinus Torvalds out_atmsvc_exit:
8821da177e4SLinus Torvalds 	atmsvc_exit();
8831da177e4SLinus Torvalds out_atmpvc_exit:
8841da177e4SLinus Torvalds 	atmsvc_exit();
8851da177e4SLinus Torvalds out_unregister_vcc_proto:
8861da177e4SLinus Torvalds 	proto_unregister(&vcc_proto);
8871da177e4SLinus Torvalds 	goto out;
8881da177e4SLinus Torvalds }
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds static void __exit atm_exit(void)
8911da177e4SLinus Torvalds {
8921da177e4SLinus Torvalds 	atm_proc_exit();
893656d98b0SRoman Kagan 	atm_sysfs_exit();
8941da177e4SLinus Torvalds 	atmsvc_exit();
8951da177e4SLinus Torvalds 	atmpvc_exit();
8961da177e4SLinus Torvalds 	proto_unregister(&vcc_proto);
8971da177e4SLinus Torvalds }
8981da177e4SLinus Torvalds 
89984ff602eSDaniel Walker subsys_initcall(atm_init);
90084ff602eSDaniel Walker 
9011da177e4SLinus Torvalds module_exit(atm_exit);
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds MODULE_LICENSE("GPL");
9041da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_ATMPVC);
9051da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_ATMSVC);
906