xref: /openbmc/linux/net/atm/common.c (revision 11aa9c28b4209242a9de0a661a7b3405adb568a0)
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>
161da177e4SLinus Torvalds #include <linux/sched.h>
171da177e4SLinus Torvalds #include <linux/time.h>		/* struct timeval */
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 
651da177e4SLinus Torvalds static struct sk_buff *alloc_tx(struct atm_vcc *vcc, unsigned int size)
661da177e4SLinus Torvalds {
671da177e4SLinus Torvalds 	struct sk_buff *skb;
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);
731da177e4SLinus Torvalds 		return NULL;
741da177e4SLinus Torvalds 	}
7581e2a3d5SEric Dumazet 	while (!(skb = alloc_skb(size, GFP_KERNEL)))
7681e2a3d5SEric Dumazet 		schedule();
77a8147d73SJoe Perches 	pr_debug("%d += %d\n", sk_wmem_alloc_get(sk), skb->truesize);
781da177e4SLinus Torvalds 	atomic_add(skb->truesize, &sk->sk_wmem_alloc);
791da177e4SLinus Torvalds 	return skb;
801da177e4SLinus Torvalds }
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds static void vcc_sock_destruct(struct sock *sk)
831da177e4SLinus Torvalds {
841da177e4SLinus Torvalds 	if (atomic_read(&sk->sk_rmem_alloc))
85a8147d73SJoe Perches 		printk(KERN_DEBUG "%s: rmem leakage (%d bytes) detected.\n",
86a8147d73SJoe Perches 		       __func__, atomic_read(&sk->sk_rmem_alloc));
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 	if (atomic_read(&sk->sk_wmem_alloc))
89a8147d73SJoe Perches 		printk(KERN_DEBUG "%s: wmem leakage (%d bytes) detected.\n",
90a8147d73SJoe Perches 		       __func__, atomic_read(&sk->sk_wmem_alloc));
911da177e4SLinus Torvalds }
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds static void vcc_def_wakeup(struct sock *sk)
941da177e4SLinus Torvalds {
9543815482SEric Dumazet 	struct socket_wq *wq;
9643815482SEric Dumazet 
9743815482SEric Dumazet 	rcu_read_lock();
9843815482SEric Dumazet 	wq = rcu_dereference(sk->sk_wq);
9943815482SEric Dumazet 	if (wq_has_sleeper(wq))
10043815482SEric Dumazet 		wake_up(&wq->wait);
10143815482SEric Dumazet 	rcu_read_unlock();
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds static inline int vcc_writable(struct sock *sk)
1051da177e4SLinus Torvalds {
1061da177e4SLinus Torvalds 	struct atm_vcc *vcc = atm_sk(sk);
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 	return (vcc->qos.txtp.max_sdu +
1091da177e4SLinus Torvalds 		atomic_read(&sk->sk_wmem_alloc)) <= sk->sk_sndbuf;
1101da177e4SLinus Torvalds }
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds static void vcc_write_space(struct sock *sk)
1131da177e4SLinus Torvalds {
11443815482SEric Dumazet 	struct socket_wq *wq;
11543815482SEric Dumazet 
11643815482SEric Dumazet 	rcu_read_lock();
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	if (vcc_writable(sk)) {
11943815482SEric Dumazet 		wq = rcu_dereference(sk->sk_wq);
12043815482SEric Dumazet 		if (wq_has_sleeper(wq))
12143815482SEric Dumazet 			wake_up_interruptible(&wq->wait);
1221da177e4SLinus Torvalds 
1238d8ad9d7SPavel Emelyanov 		sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
1241da177e4SLinus Torvalds 	}
1251da177e4SLinus Torvalds 
12643815482SEric Dumazet 	rcu_read_unlock();
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds 
129c971f08cSDavid Woodhouse static void vcc_release_cb(struct sock *sk)
130c971f08cSDavid Woodhouse {
131c971f08cSDavid Woodhouse 	struct atm_vcc *vcc = atm_sk(sk);
132c971f08cSDavid Woodhouse 
133c971f08cSDavid Woodhouse 	if (vcc->release_cb)
134c971f08cSDavid Woodhouse 		vcc->release_cb(vcc);
135c971f08cSDavid Woodhouse }
136c971f08cSDavid Woodhouse 
1371da177e4SLinus Torvalds static struct proto vcc_proto = {
1381da177e4SLinus Torvalds 	.name	  = "VCC",
1391da177e4SLinus Torvalds 	.owner	  = THIS_MODULE,
1401da177e4SLinus Torvalds 	.obj_size = sizeof(struct atm_vcc),
141c971f08cSDavid Woodhouse 	.release_cb = vcc_release_cb,
1421da177e4SLinus Torvalds };
1431da177e4SLinus Torvalds 
144*11aa9c28SEric W. Biederman int vcc_create(struct net *net, struct socket *sock, int protocol, int family, int kern)
1451da177e4SLinus Torvalds {
1461da177e4SLinus Torvalds 	struct sock *sk;
1471da177e4SLinus Torvalds 	struct atm_vcc *vcc;
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	sock->sk = NULL;
1501da177e4SLinus Torvalds 	if (sock->type == SOCK_STREAM)
1511da177e4SLinus Torvalds 		return -EINVAL;
152*11aa9c28SEric W. Biederman 	sk = sk_alloc(net, family, GFP_KERNEL, &vcc_proto, kern);
1531da177e4SLinus Torvalds 	if (!sk)
1541da177e4SLinus Torvalds 		return -ENOMEM;
1551da177e4SLinus Torvalds 	sock_init_data(sock, sk);
1561da177e4SLinus Torvalds 	sk->sk_state_change = vcc_def_wakeup;
1571da177e4SLinus Torvalds 	sk->sk_write_space = vcc_write_space;
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	vcc = atm_sk(sk);
1601da177e4SLinus Torvalds 	vcc->dev = NULL;
1611da177e4SLinus Torvalds 	memset(&vcc->local, 0, sizeof(struct sockaddr_atmsvc));
1621da177e4SLinus Torvalds 	memset(&vcc->remote, 0, sizeof(struct sockaddr_atmsvc));
1631da177e4SLinus Torvalds 	vcc->qos.txtp.max_sdu = 1 << 16; /* for meta VCs */
16481e2a3d5SEric Dumazet 	atomic_set(&sk->sk_wmem_alloc, 1);
1651da177e4SLinus Torvalds 	atomic_set(&sk->sk_rmem_alloc, 0);
1661da177e4SLinus Torvalds 	vcc->push = NULL;
1671da177e4SLinus Torvalds 	vcc->pop = NULL;
168ec809bd8SKrzysztof Mazur 	vcc->owner = NULL;
1691da177e4SLinus Torvalds 	vcc->push_oam = NULL;
170c971f08cSDavid Woodhouse 	vcc->release_cb = NULL;
1711da177e4SLinus Torvalds 	vcc->vpi = vcc->vci = 0; /* no VCI/VPI yet */
1721da177e4SLinus Torvalds 	vcc->atm_options = vcc->aal_options = 0;
1731da177e4SLinus Torvalds 	sk->sk_destruct = vcc_sock_destruct;
1741da177e4SLinus Torvalds 	return 0;
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds static void vcc_destroy_socket(struct sock *sk)
1781da177e4SLinus Torvalds {
1791da177e4SLinus Torvalds 	struct atm_vcc *vcc = atm_sk(sk);
1801da177e4SLinus Torvalds 	struct sk_buff *skb;
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	set_bit(ATM_VF_CLOSE, &vcc->flags);
1831da177e4SLinus Torvalds 	clear_bit(ATM_VF_READY, &vcc->flags);
1841da177e4SLinus Torvalds 	if (vcc->dev) {
1851da177e4SLinus Torvalds 		if (vcc->dev->ops->close)
1861da177e4SLinus Torvalds 			vcc->dev->ops->close(vcc);
1871da177e4SLinus Torvalds 		if (vcc->push)
1881da177e4SLinus Torvalds 			vcc->push(vcc, NULL); /* atmarpd has no push */
189ec809bd8SKrzysztof Mazur 		module_put(vcc->owner);
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 		while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
1921da177e4SLinus Torvalds 			atm_return(vcc, skb->truesize);
1931da177e4SLinus Torvalds 			kfree_skb(skb);
1941da177e4SLinus Torvalds 		}
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 		module_put(vcc->dev->ops->owner);
1971da177e4SLinus Torvalds 		atm_dev_put(vcc->dev);
1981da177e4SLinus Torvalds 	}
1999301e320SChas Williams 
2009301e320SChas Williams 	vcc_remove_socket(sk);
2011da177e4SLinus Torvalds }
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds int vcc_release(struct socket *sock)
2041da177e4SLinus Torvalds {
2051da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	if (sk) {
2081da177e4SLinus Torvalds 		lock_sock(sk);
2091da177e4SLinus Torvalds 		vcc_destroy_socket(sock->sk);
2101da177e4SLinus Torvalds 		release_sock(sk);
2111da177e4SLinus Torvalds 		sock_put(sk);
2121da177e4SLinus Torvalds 	}
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	return 0;
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds void vcc_release_async(struct atm_vcc *vcc, int reply)
2181da177e4SLinus Torvalds {
2191da177e4SLinus Torvalds 	struct sock *sk = sk_atm(vcc);
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	set_bit(ATM_VF_CLOSE, &vcc->flags);
2221da177e4SLinus Torvalds 	sk->sk_shutdown |= RCV_SHUTDOWN;
2231da177e4SLinus Torvalds 	sk->sk_err = -reply;
2241da177e4SLinus Torvalds 	clear_bit(ATM_VF_WAITING, &vcc->flags);
2251da177e4SLinus Torvalds 	sk->sk_state_change(sk);
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds EXPORT_SYMBOL(vcc_release_async);
2281da177e4SLinus Torvalds 
2294e55f578SJorge Boncompte [DTI2] void vcc_process_recv_queue(struct atm_vcc *vcc)
2304e55f578SJorge Boncompte [DTI2] {
2314e55f578SJorge Boncompte [DTI2] 	struct sk_buff_head queue, *rq;
2324e55f578SJorge Boncompte [DTI2] 	struct sk_buff *skb, *tmp;
2334e55f578SJorge Boncompte [DTI2] 	unsigned long flags;
2344e55f578SJorge Boncompte [DTI2] 
2354e55f578SJorge Boncompte [DTI2] 	__skb_queue_head_init(&queue);
2364e55f578SJorge Boncompte [DTI2] 	rq = &sk_atm(vcc)->sk_receive_queue;
2374e55f578SJorge Boncompte [DTI2] 
2384e55f578SJorge Boncompte [DTI2] 	spin_lock_irqsave(&rq->lock, flags);
2394e55f578SJorge Boncompte [DTI2] 	skb_queue_splice_init(rq, &queue);
2404e55f578SJorge Boncompte [DTI2] 	spin_unlock_irqrestore(&rq->lock, flags);
2414e55f578SJorge Boncompte [DTI2] 
2424e55f578SJorge Boncompte [DTI2] 	skb_queue_walk_safe(&queue, skb, tmp) {
2434e55f578SJorge Boncompte [DTI2] 		__skb_unlink(skb, &queue);
2444e55f578SJorge Boncompte [DTI2] 		vcc->push(vcc, skb);
2454e55f578SJorge Boncompte [DTI2] 	}
2464e55f578SJorge Boncompte [DTI2] }
2474e55f578SJorge Boncompte [DTI2] EXPORT_SYMBOL(vcc_process_recv_queue);
2484e55f578SJorge Boncompte [DTI2] 
2497313bb8fSKarl Hiramoto void atm_dev_signal_change(struct atm_dev *dev, char signal)
2507313bb8fSKarl Hiramoto {
2517313bb8fSKarl Hiramoto 	pr_debug("%s signal=%d dev=%p number=%d dev->signal=%d\n",
2527313bb8fSKarl Hiramoto 		__func__, signal, dev, dev->number, dev->signal);
2537313bb8fSKarl Hiramoto 
2547313bb8fSKarl Hiramoto 	/* atm driver sending invalid signal */
2557313bb8fSKarl Hiramoto 	WARN_ON(signal < ATM_PHY_SIG_LOST || signal > ATM_PHY_SIG_FOUND);
2567313bb8fSKarl Hiramoto 
2577313bb8fSKarl Hiramoto 	if (dev->signal == signal)
2587313bb8fSKarl Hiramoto 		return; /* no change */
2597313bb8fSKarl Hiramoto 
2607313bb8fSKarl Hiramoto 	dev->signal = signal;
2617313bb8fSKarl Hiramoto 
2627313bb8fSKarl Hiramoto 	atomic_notifier_call_chain(&atm_dev_notify_chain, signal, dev);
2637313bb8fSKarl Hiramoto }
2647313bb8fSKarl Hiramoto EXPORT_SYMBOL(atm_dev_signal_change);
2651da177e4SLinus Torvalds 
26664bf69ddSStanislaw Gruszka void atm_dev_release_vccs(struct atm_dev *dev)
26764bf69ddSStanislaw Gruszka {
26864bf69ddSStanislaw Gruszka 	int i;
26964bf69ddSStanislaw Gruszka 
27064bf69ddSStanislaw Gruszka 	write_lock_irq(&vcc_sklist_lock);
27164bf69ddSStanislaw Gruszka 	for (i = 0; i < VCC_HTABLE_SIZE; i++) {
27264bf69ddSStanislaw Gruszka 		struct hlist_head *head = &vcc_hash[i];
273b67bfe0dSSasha Levin 		struct hlist_node *tmp;
27464bf69ddSStanislaw Gruszka 		struct sock *s;
27564bf69ddSStanislaw Gruszka 		struct atm_vcc *vcc;
27664bf69ddSStanislaw Gruszka 
277b67bfe0dSSasha Levin 		sk_for_each_safe(s, tmp, head) {
27864bf69ddSStanislaw Gruszka 			vcc = atm_sk(s);
27964bf69ddSStanislaw Gruszka 			if (vcc->dev == dev) {
28064bf69ddSStanislaw Gruszka 				vcc_release_async(vcc, -EPIPE);
28164bf69ddSStanislaw Gruszka 				sk_del_node_init(s);
28264bf69ddSStanislaw Gruszka 			}
28364bf69ddSStanislaw Gruszka 		}
28464bf69ddSStanislaw Gruszka 	}
28564bf69ddSStanislaw Gruszka 	write_unlock_irq(&vcc_sklist_lock);
28664bf69ddSStanislaw Gruszka }
287c031235bSPhilip A. Prindeville EXPORT_SYMBOL(atm_dev_release_vccs);
28864bf69ddSStanislaw Gruszka 
2891da177e4SLinus Torvalds static int adjust_tp(struct atm_trafprm *tp, unsigned char aal)
2901da177e4SLinus Torvalds {
2911da177e4SLinus Torvalds 	int max_sdu;
2921da177e4SLinus Torvalds 
293a8147d73SJoe Perches 	if (!tp->traffic_class)
294a8147d73SJoe Perches 		return 0;
2951da177e4SLinus Torvalds 	switch (aal) {
2961da177e4SLinus Torvalds 	case ATM_AAL0:
2971da177e4SLinus Torvalds 		max_sdu = ATM_CELL_SIZE-1;
2981da177e4SLinus Torvalds 		break;
2991da177e4SLinus Torvalds 	case ATM_AAL34:
3001da177e4SLinus Torvalds 		max_sdu = ATM_MAX_AAL34_PDU;
3011da177e4SLinus Torvalds 		break;
3021da177e4SLinus Torvalds 	default:
303ef423a41SJoe Perches 		pr_warn("AAL problems ... (%d)\n", aal);
3041da177e4SLinus Torvalds 		/* fall through */
3051da177e4SLinus Torvalds 	case ATM_AAL5:
3061da177e4SLinus Torvalds 		max_sdu = ATM_MAX_AAL5_PDU;
3071da177e4SLinus Torvalds 	}
308a8147d73SJoe Perches 	if (!tp->max_sdu)
309a8147d73SJoe Perches 		tp->max_sdu = max_sdu;
310a8147d73SJoe Perches 	else if (tp->max_sdu > max_sdu)
311a8147d73SJoe Perches 		return -EINVAL;
312a8147d73SJoe Perches 	if (!tp->max_cdv)
313a8147d73SJoe Perches 		tp->max_cdv = ATM_MAX_CDV;
3141da177e4SLinus Torvalds 	return 0;
3151da177e4SLinus Torvalds }
3161da177e4SLinus Torvalds 
31761c33e01SMitchell Blank Jr static int check_ci(const struct atm_vcc *vcc, short vpi, int vci)
3181da177e4SLinus Torvalds {
319a8147d73SJoe Perches 	struct hlist_head *head = &vcc_hash[vci & (VCC_HTABLE_SIZE - 1)];
3201da177e4SLinus Torvalds 	struct sock *s;
3211da177e4SLinus Torvalds 	struct atm_vcc *walk;
3221da177e4SLinus Torvalds 
323b67bfe0dSSasha Levin 	sk_for_each(s, head) {
3241da177e4SLinus Torvalds 		walk = atm_sk(s);
3251da177e4SLinus Torvalds 		if (walk->dev != vcc->dev)
3261da177e4SLinus Torvalds 			continue;
3271da177e4SLinus Torvalds 		if (test_bit(ATM_VF_ADDR, &walk->flags) && walk->vpi == vpi &&
3281da177e4SLinus Torvalds 		    walk->vci == vci && ((walk->qos.txtp.traffic_class !=
3291da177e4SLinus Torvalds 		    ATM_NONE && vcc->qos.txtp.traffic_class != ATM_NONE) ||
3301da177e4SLinus Torvalds 		    (walk->qos.rxtp.traffic_class != ATM_NONE &&
3311da177e4SLinus Torvalds 		    vcc->qos.rxtp.traffic_class != ATM_NONE)))
3321da177e4SLinus Torvalds 			return -EADDRINUSE;
3331da177e4SLinus Torvalds 	}
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	/* allow VCCs with same VPI/VCI iff they don't collide on
3361da177e4SLinus Torvalds 	   TX/RX (but we may refuse such sharing for other reasons,
3371da177e4SLinus Torvalds 	   e.g. if protocol requires to have both channels) */
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	return 0;
3401da177e4SLinus Torvalds }
3411da177e4SLinus Torvalds 
34261c33e01SMitchell Blank Jr static int find_ci(const struct atm_vcc *vcc, short *vpi, int *vci)
3431da177e4SLinus Torvalds {
3441da177e4SLinus Torvalds 	static short p;        /* poor man's per-device cache */
3451da177e4SLinus Torvalds 	static int c;
3461da177e4SLinus Torvalds 	short old_p;
3471da177e4SLinus Torvalds 	int old_c;
3481da177e4SLinus Torvalds 	int err;
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	if (*vpi != ATM_VPI_ANY && *vci != ATM_VCI_ANY) {
3511da177e4SLinus Torvalds 		err = check_ci(vcc, *vpi, *vci);
3521da177e4SLinus Torvalds 		return err;
3531da177e4SLinus Torvalds 	}
3541da177e4SLinus Torvalds 	/* last scan may have left values out of bounds for current device */
3551da177e4SLinus Torvalds 	if (*vpi != ATM_VPI_ANY)
3561da177e4SLinus Torvalds 		p = *vpi;
3571da177e4SLinus Torvalds 	else if (p >= 1 << vcc->dev->ci_range.vpi_bits)
3581da177e4SLinus Torvalds 		p = 0;
3591da177e4SLinus Torvalds 	if (*vci != ATM_VCI_ANY)
3601da177e4SLinus Torvalds 		c = *vci;
3611da177e4SLinus Torvalds 	else if (c < ATM_NOT_RSV_VCI || c >= 1 << vcc->dev->ci_range.vci_bits)
3621da177e4SLinus Torvalds 			c = ATM_NOT_RSV_VCI;
3631da177e4SLinus Torvalds 	old_p = p;
3641da177e4SLinus Torvalds 	old_c = c;
3651da177e4SLinus Torvalds 	do {
3661da177e4SLinus Torvalds 		if (!check_ci(vcc, p, c)) {
3671da177e4SLinus Torvalds 			*vpi = p;
3681da177e4SLinus Torvalds 			*vci = c;
3691da177e4SLinus Torvalds 			return 0;
3701da177e4SLinus Torvalds 		}
3711da177e4SLinus Torvalds 		if (*vci == ATM_VCI_ANY) {
3721da177e4SLinus Torvalds 			c++;
3731da177e4SLinus Torvalds 			if (c >= 1 << vcc->dev->ci_range.vci_bits)
3741da177e4SLinus Torvalds 				c = ATM_NOT_RSV_VCI;
3751da177e4SLinus Torvalds 		}
3761da177e4SLinus Torvalds 		if ((c == ATM_NOT_RSV_VCI || *vci != ATM_VCI_ANY) &&
3771da177e4SLinus Torvalds 		    *vpi == ATM_VPI_ANY) {
3781da177e4SLinus Torvalds 			p++;
379a8147d73SJoe Perches 			if (p >= 1 << vcc->dev->ci_range.vpi_bits)
380a8147d73SJoe Perches 				p = 0;
3811da177e4SLinus Torvalds 		}
382a8147d73SJoe Perches 	} while (old_p != p || old_c != c);
3831da177e4SLinus Torvalds 	return -EADDRINUSE;
3841da177e4SLinus Torvalds }
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds static int __vcc_connect(struct atm_vcc *vcc, struct atm_dev *dev, short vpi,
3871da177e4SLinus Torvalds 			 int vci)
3881da177e4SLinus Torvalds {
3891da177e4SLinus Torvalds 	struct sock *sk = sk_atm(vcc);
3901da177e4SLinus Torvalds 	int error;
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	if ((vpi != ATM_VPI_UNSPEC && vpi != ATM_VPI_ANY &&
3931da177e4SLinus Torvalds 	    vpi >> dev->ci_range.vpi_bits) || (vci != ATM_VCI_UNSPEC &&
3941da177e4SLinus Torvalds 	    vci != ATM_VCI_ANY && vci >> dev->ci_range.vci_bits))
3951da177e4SLinus Torvalds 		return -EINVAL;
3961da177e4SLinus Torvalds 	if (vci > 0 && vci < ATM_NOT_RSV_VCI && !capable(CAP_NET_BIND_SERVICE))
3971da177e4SLinus Torvalds 		return -EPERM;
39864bf69ddSStanislaw Gruszka 	error = -ENODEV;
3991da177e4SLinus Torvalds 	if (!try_module_get(dev->ops->owner))
40064bf69ddSStanislaw Gruszka 		return error;
4011da177e4SLinus Torvalds 	vcc->dev = dev;
4021da177e4SLinus Torvalds 	write_lock_irq(&vcc_sklist_lock);
40364bf69ddSStanislaw Gruszka 	if (test_bit(ATM_DF_REMOVED, &dev->flags) ||
40464bf69ddSStanislaw Gruszka 	    (error = find_ci(vcc, &vpi, &vci))) {
4051da177e4SLinus Torvalds 		write_unlock_irq(&vcc_sklist_lock);
4061da177e4SLinus Torvalds 		goto fail_module_put;
4071da177e4SLinus Torvalds 	}
4081da177e4SLinus Torvalds 	vcc->vpi = vpi;
4091da177e4SLinus Torvalds 	vcc->vci = vci;
4101da177e4SLinus Torvalds 	__vcc_insert_socket(sk);
4111da177e4SLinus Torvalds 	write_unlock_irq(&vcc_sklist_lock);
4121da177e4SLinus Torvalds 	switch (vcc->qos.aal) {
4131da177e4SLinus Torvalds 	case ATM_AAL0:
4141da177e4SLinus Torvalds 		error = atm_init_aal0(vcc);
4151da177e4SLinus Torvalds 		vcc->stats = &dev->stats.aal0;
4161da177e4SLinus Torvalds 		break;
4171da177e4SLinus Torvalds 	case ATM_AAL34:
4181da177e4SLinus Torvalds 		error = atm_init_aal34(vcc);
4191da177e4SLinus Torvalds 		vcc->stats = &dev->stats.aal34;
4201da177e4SLinus Torvalds 		break;
4211da177e4SLinus Torvalds 	case ATM_NO_AAL:
4221da177e4SLinus Torvalds 		/* ATM_AAL5 is also used in the "0 for default" case */
4231da177e4SLinus Torvalds 		vcc->qos.aal = ATM_AAL5;
4241da177e4SLinus Torvalds 		/* fall through */
4251da177e4SLinus Torvalds 	case ATM_AAL5:
4261da177e4SLinus Torvalds 		error = atm_init_aal5(vcc);
4271da177e4SLinus Torvalds 		vcc->stats = &dev->stats.aal5;
4281da177e4SLinus Torvalds 		break;
4291da177e4SLinus Torvalds 	default:
4301da177e4SLinus Torvalds 		error = -EPROTOTYPE;
4311da177e4SLinus Torvalds 	}
432a8147d73SJoe Perches 	if (!error)
433a8147d73SJoe Perches 		error = adjust_tp(&vcc->qos.txtp, vcc->qos.aal);
434a8147d73SJoe Perches 	if (!error)
435a8147d73SJoe Perches 		error = adjust_tp(&vcc->qos.rxtp, vcc->qos.aal);
4361da177e4SLinus Torvalds 	if (error)
4371da177e4SLinus Torvalds 		goto fail;
43852240062SStephen Hemminger 	pr_debug("VCC %d.%d, AAL %d\n", vpi, vci, vcc->qos.aal);
43999824461SJoe Perches 	pr_debug("  TX: %d, PCR %d..%d, SDU %d\n",
44099824461SJoe Perches 		 vcc->qos.txtp.traffic_class,
44199824461SJoe Perches 		 vcc->qos.txtp.min_pcr,
44299824461SJoe Perches 		 vcc->qos.txtp.max_pcr,
44399824461SJoe Perches 		 vcc->qos.txtp.max_sdu);
44499824461SJoe Perches 	pr_debug("  RX: %d, PCR %d..%d, SDU %d\n",
44599824461SJoe Perches 		 vcc->qos.rxtp.traffic_class,
44699824461SJoe Perches 		 vcc->qos.rxtp.min_pcr,
44799824461SJoe Perches 		 vcc->qos.rxtp.max_pcr,
44899824461SJoe Perches 		 vcc->qos.rxtp.max_sdu);
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	if (dev->ops->open) {
451a8147d73SJoe Perches 		error = dev->ops->open(vcc);
452a8147d73SJoe Perches 		if (error)
4531da177e4SLinus Torvalds 			goto fail;
4541da177e4SLinus Torvalds 	}
4551da177e4SLinus Torvalds 	return 0;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds fail:
4581da177e4SLinus Torvalds 	vcc_remove_socket(sk);
4591da177e4SLinus Torvalds fail_module_put:
4601da177e4SLinus Torvalds 	module_put(dev->ops->owner);
4611da177e4SLinus Torvalds 	/* ensure we get dev module ref count correct */
4621da177e4SLinus Torvalds 	vcc->dev = NULL;
4631da177e4SLinus Torvalds 	return error;
4641da177e4SLinus Torvalds }
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds int vcc_connect(struct socket *sock, int itf, short vpi, int vci)
4671da177e4SLinus Torvalds {
4681da177e4SLinus Torvalds 	struct atm_dev *dev;
4691da177e4SLinus Torvalds 	struct atm_vcc *vcc = ATM_SD(sock);
4701da177e4SLinus Torvalds 	int error;
4711da177e4SLinus Torvalds 
47299824461SJoe Perches 	pr_debug("(vpi %d, vci %d)\n", vpi, vci);
4731da177e4SLinus Torvalds 	if (sock->state == SS_CONNECTED)
4741da177e4SLinus Torvalds 		return -EISCONN;
4751da177e4SLinus Torvalds 	if (sock->state != SS_UNCONNECTED)
4761da177e4SLinus Torvalds 		return -EINVAL;
4771da177e4SLinus Torvalds 	if (!(vpi || vci))
4781da177e4SLinus Torvalds 		return -EINVAL;
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	if (vpi != ATM_VPI_UNSPEC && vci != ATM_VCI_UNSPEC)
4811da177e4SLinus Torvalds 		clear_bit(ATM_VF_PARTIAL, &vcc->flags);
4821da177e4SLinus Torvalds 	else
4831da177e4SLinus Torvalds 		if (test_bit(ATM_VF_PARTIAL, &vcc->flags))
4841da177e4SLinus Torvalds 			return -EINVAL;
48599824461SJoe Perches 	pr_debug("(TX: cl %d,bw %d-%d,sdu %d; "
4861da177e4SLinus Torvalds 		 "RX: cl %d,bw %d-%d,sdu %d,AAL %s%d)\n",
4871da177e4SLinus Torvalds 		 vcc->qos.txtp.traffic_class, vcc->qos.txtp.min_pcr,
4881da177e4SLinus Torvalds 		 vcc->qos.txtp.max_pcr, vcc->qos.txtp.max_sdu,
4891da177e4SLinus Torvalds 		 vcc->qos.rxtp.traffic_class, vcc->qos.rxtp.min_pcr,
4901da177e4SLinus Torvalds 		 vcc->qos.rxtp.max_pcr, vcc->qos.rxtp.max_sdu,
49199824461SJoe Perches 		 vcc->qos.aal == ATM_AAL5 ? "" :
49299824461SJoe Perches 		 vcc->qos.aal == ATM_AAL0 ? "" : " ??? code ",
49399824461SJoe Perches 		 vcc->qos.aal == ATM_AAL0 ? 0 : vcc->qos.aal);
4941da177e4SLinus Torvalds 	if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
4951da177e4SLinus Torvalds 		return -EBADFD;
4961da177e4SLinus Torvalds 	if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
4971da177e4SLinus Torvalds 	    vcc->qos.rxtp.traffic_class == ATM_ANYCLASS)
4981da177e4SLinus Torvalds 		return -EINVAL;
499c9933d08SMitchell Blank Jr 	if (likely(itf != ATM_ITF_ANY)) {
500a8147d73SJoe Perches 		dev = try_then_request_module(atm_dev_lookup(itf),
501a8147d73SJoe Perches 					      "atm-device-%d", itf);
502c9933d08SMitchell Blank Jr 	} else {
503c9933d08SMitchell Blank Jr 		dev = NULL;
50457b47a53SIngo Molnar 		mutex_lock(&atm_dev_mutex);
505c9933d08SMitchell Blank Jr 		if (!list_empty(&atm_devs)) {
506a8147d73SJoe Perches 			dev = list_entry(atm_devs.next,
507a8147d73SJoe Perches 					 struct atm_dev, dev_list);
508c9933d08SMitchell Blank Jr 			atm_dev_hold(dev);
509c9933d08SMitchell Blank Jr 		}
51057b47a53SIngo Molnar 		mutex_unlock(&atm_dev_mutex);
511c9933d08SMitchell Blank Jr 	}
5121da177e4SLinus Torvalds 	if (!dev)
5131da177e4SLinus Torvalds 		return -ENODEV;
5141da177e4SLinus Torvalds 	error = __vcc_connect(vcc, dev, vpi, vci);
5151da177e4SLinus Torvalds 	if (error) {
5161da177e4SLinus Torvalds 		atm_dev_put(dev);
5171da177e4SLinus Torvalds 		return error;
5181da177e4SLinus Torvalds 	}
5191da177e4SLinus Torvalds 	if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC)
5201da177e4SLinus Torvalds 		set_bit(ATM_VF_PARTIAL, &vcc->flags);
5211da177e4SLinus Torvalds 	if (test_bit(ATM_VF_READY, &ATM_SD(sock)->flags))
5221da177e4SLinus Torvalds 		sock->state = SS_CONNECTED;
5231da177e4SLinus Torvalds 	return 0;
5241da177e4SLinus Torvalds }
5251da177e4SLinus Torvalds 
5261b784140SYing Xue int vcc_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
5271b784140SYing Xue 		int flags)
5281da177e4SLinus Torvalds {
5291da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
5301da177e4SLinus Torvalds 	struct atm_vcc *vcc;
5311da177e4SLinus Torvalds 	struct sk_buff *skb;
5321da177e4SLinus Torvalds 	int copied, error = -EINVAL;
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	if (sock->state != SS_CONNECTED)
5351da177e4SLinus Torvalds 		return -ENOTCONN;
53640ba8499SJorge Boncompte [DTI2] 
53740ba8499SJorge Boncompte [DTI2] 	/* only handle MSG_DONTWAIT and MSG_PEEK */
53840ba8499SJorge Boncompte [DTI2] 	if (flags & ~(MSG_DONTWAIT | MSG_PEEK))
5391da177e4SLinus Torvalds 		return -EOPNOTSUPP;
54040ba8499SJorge Boncompte [DTI2] 
5411da177e4SLinus Torvalds 	vcc = ATM_SD(sock);
5421da177e4SLinus Torvalds 	if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
5431da177e4SLinus Torvalds 	    test_bit(ATM_VF_CLOSE, &vcc->flags) ||
5441da177e4SLinus Torvalds 	    !test_bit(ATM_VF_READY, &vcc->flags))
5451da177e4SLinus Torvalds 		return 0;
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &error);
5481da177e4SLinus Torvalds 	if (!skb)
5491da177e4SLinus Torvalds 		return error;
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	copied = skb->len;
5521da177e4SLinus Torvalds 	if (copied > size) {
5531da177e4SLinus Torvalds 		copied = size;
5541da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
5551da177e4SLinus Torvalds 	}
5561da177e4SLinus Torvalds 
55751f3d02bSDavid S. Miller 	error = skb_copy_datagram_msg(skb, 0, msg, copied);
5581da177e4SLinus Torvalds 	if (error)
5591da177e4SLinus Torvalds 		return error;
5603b885787SNeil Horman 	sock_recv_ts_and_drops(msg, sk, skb);
56140ba8499SJorge Boncompte [DTI2] 
56240ba8499SJorge Boncompte [DTI2] 	if (!(flags & MSG_PEEK)) {
56340ba8499SJorge Boncompte [DTI2] 		pr_debug("%d -= %d\n", atomic_read(&sk->sk_rmem_alloc),
56440ba8499SJorge Boncompte [DTI2] 			 skb->truesize);
5651da177e4SLinus Torvalds 		atm_return(vcc, skb->truesize);
56640ba8499SJorge Boncompte [DTI2] 	}
56740ba8499SJorge Boncompte [DTI2] 
5681da177e4SLinus Torvalds 	skb_free_datagram(sk, skb);
5691da177e4SLinus Torvalds 	return copied;
5701da177e4SLinus Torvalds }
5711da177e4SLinus Torvalds 
5721b784140SYing Xue int vcc_sendmsg(struct socket *sock, struct msghdr *m, size_t size)
5731da177e4SLinus Torvalds {
5741da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
5751da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
5761da177e4SLinus Torvalds 	struct atm_vcc *vcc;
5771da177e4SLinus Torvalds 	struct sk_buff *skb;
5781da177e4SLinus Torvalds 	int eff, error;
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 	lock_sock(sk);
5811da177e4SLinus Torvalds 	if (sock->state != SS_CONNECTED) {
5821da177e4SLinus Torvalds 		error = -ENOTCONN;
5831da177e4SLinus Torvalds 		goto out;
5841da177e4SLinus Torvalds 	}
5851da177e4SLinus Torvalds 	if (m->msg_name) {
5861da177e4SLinus Torvalds 		error = -EISCONN;
5871da177e4SLinus Torvalds 		goto out;
5881da177e4SLinus Torvalds 	}
5891da177e4SLinus Torvalds 	vcc = ATM_SD(sock);
5901da177e4SLinus Torvalds 	if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
5911da177e4SLinus Torvalds 	    test_bit(ATM_VF_CLOSE, &vcc->flags) ||
5921da177e4SLinus Torvalds 	    !test_bit(ATM_VF_READY, &vcc->flags)) {
5931da177e4SLinus Torvalds 		error = -EPIPE;
5941da177e4SLinus Torvalds 		send_sig(SIGPIPE, current, 0);
5951da177e4SLinus Torvalds 		goto out;
5961da177e4SLinus Torvalds 	}
5971da177e4SLinus Torvalds 	if (!size) {
5981da177e4SLinus Torvalds 		error = 0;
5991da177e4SLinus Torvalds 		goto out;
6001da177e4SLinus Torvalds 	}
6017424ce65SAl Viro 	if (size > vcc->qos.txtp.max_sdu) {
6021da177e4SLinus Torvalds 		error = -EMSGSIZE;
6031da177e4SLinus Torvalds 		goto out;
6041da177e4SLinus Torvalds 	}
605e49332bdSJesper Juhl 
6061da177e4SLinus Torvalds 	eff = (size+3) & ~3; /* align to word boundary */
607aa395145SEric Dumazet 	prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
6081da177e4SLinus Torvalds 	error = 0;
6091da177e4SLinus Torvalds 	while (!(skb = alloc_tx(vcc, eff))) {
6101da177e4SLinus Torvalds 		if (m->msg_flags & MSG_DONTWAIT) {
6111da177e4SLinus Torvalds 			error = -EAGAIN;
6121da177e4SLinus Torvalds 			break;
6131da177e4SLinus Torvalds 		}
6141da177e4SLinus Torvalds 		schedule();
6151da177e4SLinus Torvalds 		if (signal_pending(current)) {
6161da177e4SLinus Torvalds 			error = -ERESTARTSYS;
6171da177e4SLinus Torvalds 			break;
6181da177e4SLinus Torvalds 		}
6191da177e4SLinus Torvalds 		if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
6201da177e4SLinus Torvalds 		    test_bit(ATM_VF_CLOSE, &vcc->flags) ||
6211da177e4SLinus Torvalds 		    !test_bit(ATM_VF_READY, &vcc->flags)) {
6221da177e4SLinus Torvalds 			error = -EPIPE;
6231da177e4SLinus Torvalds 			send_sig(SIGPIPE, current, 0);
6241da177e4SLinus Torvalds 			break;
6251da177e4SLinus Torvalds 		}
626aa395145SEric Dumazet 		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
6271da177e4SLinus Torvalds 	}
628aa395145SEric Dumazet 	finish_wait(sk_sleep(sk), &wait);
6291da177e4SLinus Torvalds 	if (error)
6301da177e4SLinus Torvalds 		goto out;
6311da177e4SLinus Torvalds 	skb->dev = NULL; /* for paths shared with net_device interfaces */
6321da177e4SLinus Torvalds 	ATM_SKB(skb)->atm_options = vcc->atm_options;
633c0371da6SAl Viro 	if (copy_from_iter(skb_put(skb, size), size, &m->msg_iter) != size) {
6341da177e4SLinus Torvalds 		kfree_skb(skb);
6351da177e4SLinus Torvalds 		error = -EFAULT;
6361da177e4SLinus Torvalds 		goto out;
6371da177e4SLinus Torvalds 	}
638a8147d73SJoe Perches 	if (eff != size)
639a8147d73SJoe Perches 		memset(skb->data + size, 0, eff-size);
6401da177e4SLinus Torvalds 	error = vcc->dev->ops->send(vcc, skb);
6411da177e4SLinus Torvalds 	error = error ? error : size;
6421da177e4SLinus Torvalds out:
6431da177e4SLinus Torvalds 	release_sock(sk);
6441da177e4SLinus Torvalds 	return error;
6451da177e4SLinus Torvalds }
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds unsigned int vcc_poll(struct file *file, struct socket *sock, poll_table *wait)
6481da177e4SLinus Torvalds {
6491da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
6501da177e4SLinus Torvalds 	struct atm_vcc *vcc;
6511da177e4SLinus Torvalds 	unsigned int mask;
6521da177e4SLinus Torvalds 
653aa395145SEric Dumazet 	sock_poll_wait(file, sk_sleep(sk), wait);
6541da177e4SLinus Torvalds 	mask = 0;
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	vcc = ATM_SD(sock);
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 	/* exceptional events */
6591da177e4SLinus Torvalds 	if (sk->sk_err)
6601da177e4SLinus Torvalds 		mask = POLLERR;
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 	if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
6631da177e4SLinus Torvalds 	    test_bit(ATM_VF_CLOSE, &vcc->flags))
6641da177e4SLinus Torvalds 		mask |= POLLHUP;
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	/* readable? */
6671da177e4SLinus Torvalds 	if (!skb_queue_empty(&sk->sk_receive_queue))
6681da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 	/* writable? */
6711da177e4SLinus Torvalds 	if (sock->state == SS_CONNECTING &&
6721da177e4SLinus Torvalds 	    test_bit(ATM_VF_WAITING, &vcc->flags))
6731da177e4SLinus Torvalds 		return mask;
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 	if (vcc->qos.txtp.traffic_class != ATM_NONE &&
6761da177e4SLinus Torvalds 	    vcc_writable(sk))
6771da177e4SLinus Torvalds 		mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds 	return mask;
6801da177e4SLinus Torvalds }
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds static int atm_change_qos(struct atm_vcc *vcc, struct atm_qos *qos)
6831da177e4SLinus Torvalds {
6841da177e4SLinus Torvalds 	int error;
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 	/*
6871da177e4SLinus Torvalds 	 * Don't let the QoS change the already connected AAL type nor the
6881da177e4SLinus Torvalds 	 * traffic class.
6891da177e4SLinus Torvalds 	 */
6901da177e4SLinus Torvalds 	if (qos->aal != vcc->qos.aal ||
6911da177e4SLinus Torvalds 	    qos->rxtp.traffic_class != vcc->qos.rxtp.traffic_class ||
6921da177e4SLinus Torvalds 	    qos->txtp.traffic_class != vcc->qos.txtp.traffic_class)
6931da177e4SLinus Torvalds 		return -EINVAL;
6941da177e4SLinus Torvalds 	error = adjust_tp(&qos->txtp, qos->aal);
695a8147d73SJoe Perches 	if (!error)
696a8147d73SJoe Perches 		error = adjust_tp(&qos->rxtp, qos->aal);
697a8147d73SJoe Perches 	if (error)
698a8147d73SJoe Perches 		return error;
699a8147d73SJoe Perches 	if (!vcc->dev->ops->change_qos)
700a8147d73SJoe Perches 		return -EOPNOTSUPP;
7011da177e4SLinus Torvalds 	if (sk_atm(vcc)->sk_family == AF_ATMPVC)
7021da177e4SLinus Torvalds 		return vcc->dev->ops->change_qos(vcc, qos, ATM_MF_SET);
7031da177e4SLinus Torvalds 	return svc_change_qos(vcc, qos);
7041da177e4SLinus Torvalds }
7051da177e4SLinus Torvalds 
70661c33e01SMitchell Blank Jr static int check_tp(const struct atm_trafprm *tp)
7071da177e4SLinus Torvalds {
7081da177e4SLinus Torvalds 	/* @@@ Should be merged with adjust_tp */
709a8147d73SJoe Perches 	if (!tp->traffic_class || tp->traffic_class == ATM_ANYCLASS)
710a8147d73SJoe Perches 		return 0;
7111da177e4SLinus Torvalds 	if (tp->traffic_class != ATM_UBR && !tp->min_pcr && !tp->pcr &&
712a8147d73SJoe Perches 	    !tp->max_pcr)
713a8147d73SJoe Perches 		return -EINVAL;
714a8147d73SJoe Perches 	if (tp->min_pcr == ATM_MAX_PCR)
715a8147d73SJoe Perches 		return -EINVAL;
7161da177e4SLinus Torvalds 	if (tp->min_pcr && tp->max_pcr && tp->max_pcr != ATM_MAX_PCR &&
717a8147d73SJoe Perches 	    tp->min_pcr > tp->max_pcr)
718a8147d73SJoe Perches 		return -EINVAL;
7191da177e4SLinus Torvalds 	/*
7201da177e4SLinus Torvalds 	 * We allow pcr to be outside [min_pcr,max_pcr], because later
7211da177e4SLinus Torvalds 	 * adjustment may still push it in the valid range.
7221da177e4SLinus Torvalds 	 */
7231da177e4SLinus Torvalds 	return 0;
7241da177e4SLinus Torvalds }
7251da177e4SLinus Torvalds 
72661c33e01SMitchell Blank Jr static int check_qos(const struct atm_qos *qos)
7271da177e4SLinus Torvalds {
7281da177e4SLinus Torvalds 	int error;
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 	if (!qos->txtp.traffic_class && !qos->rxtp.traffic_class)
7311da177e4SLinus Torvalds 		return -EINVAL;
7321da177e4SLinus Torvalds 	if (qos->txtp.traffic_class != qos->rxtp.traffic_class &&
7331da177e4SLinus Torvalds 	    qos->txtp.traffic_class && qos->rxtp.traffic_class &&
7341da177e4SLinus Torvalds 	    qos->txtp.traffic_class != ATM_ANYCLASS &&
735a8147d73SJoe Perches 	    qos->rxtp.traffic_class != ATM_ANYCLASS)
736a8147d73SJoe Perches 		return -EINVAL;
7371da177e4SLinus Torvalds 	error = check_tp(&qos->txtp);
738a8147d73SJoe Perches 	if (error)
739a8147d73SJoe Perches 		return error;
7401da177e4SLinus Torvalds 	return check_tp(&qos->rxtp);
7411da177e4SLinus Torvalds }
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds int vcc_setsockopt(struct socket *sock, int level, int optname,
744b7058842SDavid S. Miller 		   char __user *optval, unsigned int optlen)
7451da177e4SLinus Torvalds {
7461da177e4SLinus Torvalds 	struct atm_vcc *vcc;
7471da177e4SLinus Torvalds 	unsigned long value;
7481da177e4SLinus Torvalds 	int error;
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 	if (__SO_LEVEL_MATCH(optname, level) && optlen != __SO_SIZE(optname))
7511da177e4SLinus Torvalds 		return -EINVAL;
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 	vcc = ATM_SD(sock);
7541da177e4SLinus Torvalds 	switch (optname) {
7551da177e4SLinus Torvalds 	case SO_ATMQOS:
7561da177e4SLinus Torvalds 	{
7571da177e4SLinus Torvalds 		struct atm_qos qos;
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 		if (copy_from_user(&qos, optval, sizeof(qos)))
7601da177e4SLinus Torvalds 			return -EFAULT;
7611da177e4SLinus Torvalds 		error = check_qos(&qos);
762a8147d73SJoe Perches 		if (error)
763a8147d73SJoe Perches 			return error;
7641da177e4SLinus Torvalds 		if (sock->state == SS_CONNECTED)
7651da177e4SLinus Torvalds 			return atm_change_qos(vcc, &qos);
7661da177e4SLinus Torvalds 		if (sock->state != SS_UNCONNECTED)
7671da177e4SLinus Torvalds 			return -EBADFD;
7681da177e4SLinus Torvalds 		vcc->qos = qos;
7691da177e4SLinus Torvalds 		set_bit(ATM_VF_HASQOS, &vcc->flags);
7701da177e4SLinus Torvalds 		return 0;
7711da177e4SLinus Torvalds 	}
7721da177e4SLinus Torvalds 	case SO_SETCLP:
7731da177e4SLinus Torvalds 		if (get_user(value, (unsigned long __user *)optval))
7741da177e4SLinus Torvalds 			return -EFAULT;
775a8147d73SJoe Perches 		if (value)
776a8147d73SJoe Perches 			vcc->atm_options |= ATM_ATMOPT_CLP;
777a8147d73SJoe Perches 		else
778a8147d73SJoe Perches 			vcc->atm_options &= ~ATM_ATMOPT_CLP;
7791da177e4SLinus Torvalds 		return 0;
7801da177e4SLinus Torvalds 	default:
781a8147d73SJoe Perches 		if (level == SOL_SOCKET)
782a8147d73SJoe Perches 			return -EINVAL;
7831da177e4SLinus Torvalds 		break;
7841da177e4SLinus Torvalds 	}
785a8147d73SJoe Perches 	if (!vcc->dev || !vcc->dev->ops->setsockopt)
786a8147d73SJoe Perches 		return -EINVAL;
7871da177e4SLinus Torvalds 	return vcc->dev->ops->setsockopt(vcc, level, optname, optval, optlen);
7881da177e4SLinus Torvalds }
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds int vcc_getsockopt(struct socket *sock, int level, int optname,
7911da177e4SLinus Torvalds 		   char __user *optval, int __user *optlen)
7921da177e4SLinus Torvalds {
7931da177e4SLinus Torvalds 	struct atm_vcc *vcc;
7941da177e4SLinus Torvalds 	int len;
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	if (get_user(len, optlen))
7971da177e4SLinus Torvalds 		return -EFAULT;
7981da177e4SLinus Torvalds 	if (__SO_LEVEL_MATCH(optname, level) && len != __SO_SIZE(optname))
7991da177e4SLinus Torvalds 		return -EINVAL;
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 	vcc = ATM_SD(sock);
8021da177e4SLinus Torvalds 	switch (optname) {
8031da177e4SLinus Torvalds 	case SO_ATMQOS:
8041da177e4SLinus Torvalds 		if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
8051da177e4SLinus Torvalds 			return -EINVAL;
806a8147d73SJoe Perches 		return copy_to_user(optval, &vcc->qos, sizeof(vcc->qos))
807a8147d73SJoe Perches 			? -EFAULT : 0;
8081da177e4SLinus Torvalds 	case SO_SETCLP:
809a8147d73SJoe Perches 		return put_user(vcc->atm_options & ATM_ATMOPT_CLP ? 1 : 0,
810a8147d73SJoe Perches 				(unsigned long __user *)optval) ? -EFAULT : 0;
8111da177e4SLinus Torvalds 	case SO_ATMPVC:
8121da177e4SLinus Torvalds 	{
8131da177e4SLinus Torvalds 		struct sockaddr_atmpvc pvc;
8141da177e4SLinus Torvalds 
815a8147d73SJoe Perches 		if (!vcc->dev || !test_bit(ATM_VF_ADDR, &vcc->flags))
8161da177e4SLinus Torvalds 			return -ENOTCONN;
817e862f1a9SMathias Krause 		memset(&pvc, 0, sizeof(pvc));
8181da177e4SLinus Torvalds 		pvc.sap_family = AF_ATMPVC;
8191da177e4SLinus Torvalds 		pvc.sap_addr.itf = vcc->dev->number;
8201da177e4SLinus Torvalds 		pvc.sap_addr.vpi = vcc->vpi;
8211da177e4SLinus Torvalds 		pvc.sap_addr.vci = vcc->vci;
822a8147d73SJoe Perches 		return copy_to_user(optval, &pvc, sizeof(pvc)) ? -EFAULT : 0;
8231da177e4SLinus Torvalds 	}
8241da177e4SLinus Torvalds 	default:
825a8147d73SJoe Perches 		if (level == SOL_SOCKET)
826a8147d73SJoe Perches 			return -EINVAL;
8271da177e4SLinus Torvalds 		break;
8281da177e4SLinus Torvalds 	}
829a8147d73SJoe Perches 	if (!vcc->dev || !vcc->dev->ops->getsockopt)
830a8147d73SJoe Perches 		return -EINVAL;
8311da177e4SLinus Torvalds 	return vcc->dev->ops->getsockopt(vcc, level, optname, optval, len);
8321da177e4SLinus Torvalds }
8331da177e4SLinus Torvalds 
8347313bb8fSKarl Hiramoto int register_atmdevice_notifier(struct notifier_block *nb)
8357313bb8fSKarl Hiramoto {
8367313bb8fSKarl Hiramoto 	return atomic_notifier_chain_register(&atm_dev_notify_chain, nb);
8377313bb8fSKarl Hiramoto }
8387313bb8fSKarl Hiramoto EXPORT_SYMBOL_GPL(register_atmdevice_notifier);
8397313bb8fSKarl Hiramoto 
8407313bb8fSKarl Hiramoto void unregister_atmdevice_notifier(struct notifier_block *nb)
8417313bb8fSKarl Hiramoto {
8427313bb8fSKarl Hiramoto 	atomic_notifier_chain_unregister(&atm_dev_notify_chain, nb);
8437313bb8fSKarl Hiramoto }
8447313bb8fSKarl Hiramoto EXPORT_SYMBOL_GPL(unregister_atmdevice_notifier);
8457313bb8fSKarl Hiramoto 
8461da177e4SLinus Torvalds static int __init atm_init(void)
8471da177e4SLinus Torvalds {
8481da177e4SLinus Torvalds 	int error;
8491da177e4SLinus Torvalds 
850a8147d73SJoe Perches 	error = proto_register(&vcc_proto, 0);
851a8147d73SJoe Perches 	if (error < 0)
8521da177e4SLinus Torvalds 		goto out;
853a8147d73SJoe Perches 	error = atmpvc_init();
854a8147d73SJoe Perches 	if (error < 0) {
85599824461SJoe Perches 		pr_err("atmpvc_init() failed with %d\n", error);
8561da177e4SLinus Torvalds 		goto out_unregister_vcc_proto;
8571da177e4SLinus Torvalds 	}
858a8147d73SJoe Perches 	error = atmsvc_init();
859a8147d73SJoe Perches 	if (error < 0) {
86099824461SJoe Perches 		pr_err("atmsvc_init() failed with %d\n", error);
8611da177e4SLinus Torvalds 		goto out_atmpvc_exit;
8621da177e4SLinus Torvalds 	}
863a8147d73SJoe Perches 	error = atm_proc_init();
864a8147d73SJoe Perches 	if (error < 0) {
86599824461SJoe Perches 		pr_err("atm_proc_init() failed with %d\n", error);
8661da177e4SLinus Torvalds 		goto out_atmsvc_exit;
8671da177e4SLinus Torvalds 	}
868a8147d73SJoe Perches 	error = atm_sysfs_init();
869a8147d73SJoe Perches 	if (error < 0) {
87099824461SJoe Perches 		pr_err("atm_sysfs_init() failed with %d\n", error);
871656d98b0SRoman Kagan 		goto out_atmproc_exit;
872656d98b0SRoman Kagan 	}
8731da177e4SLinus Torvalds out:
8741da177e4SLinus Torvalds 	return error;
875656d98b0SRoman Kagan out_atmproc_exit:
876656d98b0SRoman Kagan 	atm_proc_exit();
8771da177e4SLinus Torvalds out_atmsvc_exit:
8781da177e4SLinus Torvalds 	atmsvc_exit();
8791da177e4SLinus Torvalds out_atmpvc_exit:
8801da177e4SLinus Torvalds 	atmsvc_exit();
8811da177e4SLinus Torvalds out_unregister_vcc_proto:
8821da177e4SLinus Torvalds 	proto_unregister(&vcc_proto);
8831da177e4SLinus Torvalds 	goto out;
8841da177e4SLinus Torvalds }
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds static void __exit atm_exit(void)
8871da177e4SLinus Torvalds {
8881da177e4SLinus Torvalds 	atm_proc_exit();
889656d98b0SRoman Kagan 	atm_sysfs_exit();
8901da177e4SLinus Torvalds 	atmsvc_exit();
8911da177e4SLinus Torvalds 	atmpvc_exit();
8921da177e4SLinus Torvalds 	proto_unregister(&vcc_proto);
8931da177e4SLinus Torvalds }
8941da177e4SLinus Torvalds 
89584ff602eSDaniel Walker subsys_initcall(atm_init);
89684ff602eSDaniel Walker 
8971da177e4SLinus Torvalds module_exit(atm_exit);
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds MODULE_LICENSE("GPL");
9001da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_ATMPVC);
9011da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_ATMSVC);
902