xref: /openbmc/linux/net/atm/common.c (revision c9933d0856d6d0ede6b4b30e5e7330614f5203af)
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 
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #include <linux/config.h>
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>
211da177e4SLinus Torvalds #include <net/sock.h>		/* struct sock */
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #include <asm/uaccess.h>
241da177e4SLinus Torvalds #include <asm/atomic.h>
251da177e4SLinus Torvalds #include <asm/poll.h>
261da177e4SLinus Torvalds 
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 
351da177e4SLinus Torvalds #if 0
361da177e4SLinus Torvalds #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
371da177e4SLinus Torvalds #else
381da177e4SLinus Torvalds #define DPRINTK(format,args...)
391da177e4SLinus Torvalds #endif
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds struct hlist_head vcc_hash[VCC_HTABLE_SIZE];
421da177e4SLinus Torvalds DEFINE_RWLOCK(vcc_sklist_lock);
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds static void __vcc_insert_socket(struct sock *sk)
451da177e4SLinus Torvalds {
461da177e4SLinus Torvalds 	struct atm_vcc *vcc = atm_sk(sk);
471da177e4SLinus Torvalds 	struct hlist_head *head = &vcc_hash[vcc->vci &
481da177e4SLinus Torvalds 					(VCC_HTABLE_SIZE - 1)];
4981c3d547SEric Dumazet 	sk->sk_hash = vcc->vci & (VCC_HTABLE_SIZE - 1);
501da177e4SLinus Torvalds 	sk_add_node(sk, head);
511da177e4SLinus Torvalds }
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds void vcc_insert_socket(struct sock *sk)
541da177e4SLinus Torvalds {
551da177e4SLinus Torvalds 	write_lock_irq(&vcc_sklist_lock);
561da177e4SLinus Torvalds 	__vcc_insert_socket(sk);
571da177e4SLinus Torvalds 	write_unlock_irq(&vcc_sklist_lock);
581da177e4SLinus Torvalds }
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds static void vcc_remove_socket(struct sock *sk)
611da177e4SLinus Torvalds {
621da177e4SLinus Torvalds 	write_lock_irq(&vcc_sklist_lock);
631da177e4SLinus Torvalds 	sk_del_node_init(sk);
641da177e4SLinus Torvalds 	write_unlock_irq(&vcc_sklist_lock);
651da177e4SLinus Torvalds }
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds static struct sk_buff *alloc_tx(struct atm_vcc *vcc,unsigned int size)
691da177e4SLinus Torvalds {
701da177e4SLinus Torvalds 	struct sk_buff *skb;
711da177e4SLinus Torvalds 	struct sock *sk = sk_atm(vcc);
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds 	if (atomic_read(&sk->sk_wmem_alloc) && !atm_may_send(vcc, size)) {
741da177e4SLinus Torvalds 		DPRINTK("Sorry: wmem_alloc = %d, size = %d, sndbuf = %d\n",
751da177e4SLinus Torvalds 			atomic_read(&sk->sk_wmem_alloc), size,
761da177e4SLinus Torvalds 			sk->sk_sndbuf);
771da177e4SLinus Torvalds 		return NULL;
781da177e4SLinus Torvalds 	}
791da177e4SLinus Torvalds 	while (!(skb = alloc_skb(size,GFP_KERNEL))) schedule();
801da177e4SLinus Torvalds 	DPRINTK("AlTx %d += %d\n", atomic_read(&sk->sk_wmem_alloc),
811da177e4SLinus Torvalds 		skb->truesize);
821da177e4SLinus Torvalds 	atomic_add(skb->truesize, &sk->sk_wmem_alloc);
831da177e4SLinus Torvalds 	return skb;
841da177e4SLinus Torvalds }
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds EXPORT_SYMBOL(vcc_hash);
881da177e4SLinus Torvalds EXPORT_SYMBOL(vcc_sklist_lock);
891da177e4SLinus Torvalds EXPORT_SYMBOL(vcc_insert_socket);
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds static void vcc_sock_destruct(struct sock *sk)
921da177e4SLinus Torvalds {
931da177e4SLinus Torvalds 	if (atomic_read(&sk->sk_rmem_alloc))
941da177e4SLinus Torvalds 		printk(KERN_DEBUG "vcc_sock_destruct: rmem leakage (%d bytes) detected.\n", atomic_read(&sk->sk_rmem_alloc));
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	if (atomic_read(&sk->sk_wmem_alloc))
971da177e4SLinus Torvalds 		printk(KERN_DEBUG "vcc_sock_destruct: wmem leakage (%d bytes) detected.\n", atomic_read(&sk->sk_wmem_alloc));
981da177e4SLinus Torvalds }
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds static void vcc_def_wakeup(struct sock *sk)
1011da177e4SLinus Torvalds {
1021da177e4SLinus Torvalds 	read_lock(&sk->sk_callback_lock);
1031da177e4SLinus Torvalds 	if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
1041da177e4SLinus Torvalds 		wake_up(sk->sk_sleep);
1051da177e4SLinus Torvalds 	read_unlock(&sk->sk_callback_lock);
1061da177e4SLinus Torvalds }
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds static inline int vcc_writable(struct sock *sk)
1091da177e4SLinus Torvalds {
1101da177e4SLinus Torvalds 	struct atm_vcc *vcc = atm_sk(sk);
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 	return (vcc->qos.txtp.max_sdu +
1131da177e4SLinus Torvalds 	        atomic_read(&sk->sk_wmem_alloc)) <= sk->sk_sndbuf;
1141da177e4SLinus Torvalds }
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds static void vcc_write_space(struct sock *sk)
1171da177e4SLinus Torvalds {
1181da177e4SLinus Torvalds 	read_lock(&sk->sk_callback_lock);
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	if (vcc_writable(sk)) {
1211da177e4SLinus Torvalds 		if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
1221da177e4SLinus Torvalds 			wake_up_interruptible(sk->sk_sleep);
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 		sk_wake_async(sk, 2, POLL_OUT);
1251da177e4SLinus Torvalds 	}
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	read_unlock(&sk->sk_callback_lock);
1281da177e4SLinus Torvalds }
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds static struct proto vcc_proto = {
1311da177e4SLinus Torvalds 	.name	  = "VCC",
1321da177e4SLinus Torvalds 	.owner	  = THIS_MODULE,
1331da177e4SLinus Torvalds 	.obj_size = sizeof(struct atm_vcc),
1341da177e4SLinus Torvalds };
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds int vcc_create(struct socket *sock, int protocol, int family)
1371da177e4SLinus Torvalds {
1381da177e4SLinus Torvalds 	struct sock *sk;
1391da177e4SLinus Torvalds 	struct atm_vcc *vcc;
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 	sock->sk = NULL;
1421da177e4SLinus Torvalds 	if (sock->type == SOCK_STREAM)
1431da177e4SLinus Torvalds 		return -EINVAL;
1441da177e4SLinus Torvalds 	sk = sk_alloc(family, GFP_KERNEL, &vcc_proto, 1);
1451da177e4SLinus Torvalds 	if (!sk)
1461da177e4SLinus Torvalds 		return -ENOMEM;
1471da177e4SLinus Torvalds 	sock_init_data(sock, sk);
1481da177e4SLinus Torvalds 	sk->sk_state_change = vcc_def_wakeup;
1491da177e4SLinus Torvalds 	sk->sk_write_space = vcc_write_space;
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 	vcc = atm_sk(sk);
1521da177e4SLinus Torvalds 	vcc->dev = NULL;
1531da177e4SLinus Torvalds 	memset(&vcc->local,0,sizeof(struct sockaddr_atmsvc));
1541da177e4SLinus Torvalds 	memset(&vcc->remote,0,sizeof(struct sockaddr_atmsvc));
1551da177e4SLinus Torvalds 	vcc->qos.txtp.max_sdu = 1 << 16; /* for meta VCs */
1561da177e4SLinus Torvalds 	atomic_set(&sk->sk_wmem_alloc, 0);
1571da177e4SLinus Torvalds 	atomic_set(&sk->sk_rmem_alloc, 0);
1581da177e4SLinus Torvalds 	vcc->push = NULL;
1591da177e4SLinus Torvalds 	vcc->pop = NULL;
1601da177e4SLinus Torvalds 	vcc->push_oam = NULL;
1611da177e4SLinus Torvalds 	vcc->vpi = vcc->vci = 0; /* no VCI/VPI yet */
1621da177e4SLinus Torvalds 	vcc->atm_options = vcc->aal_options = 0;
1631da177e4SLinus Torvalds 	sk->sk_destruct = vcc_sock_destruct;
1641da177e4SLinus Torvalds 	return 0;
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds static void vcc_destroy_socket(struct sock *sk)
1691da177e4SLinus Torvalds {
1701da177e4SLinus Torvalds 	struct atm_vcc *vcc = atm_sk(sk);
1711da177e4SLinus Torvalds 	struct sk_buff *skb;
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	set_bit(ATM_VF_CLOSE, &vcc->flags);
1741da177e4SLinus Torvalds 	clear_bit(ATM_VF_READY, &vcc->flags);
1751da177e4SLinus Torvalds 	if (vcc->dev) {
1761da177e4SLinus Torvalds 		if (vcc->dev->ops->close)
1771da177e4SLinus Torvalds 			vcc->dev->ops->close(vcc);
1781da177e4SLinus Torvalds 		if (vcc->push)
1791da177e4SLinus Torvalds 			vcc->push(vcc, NULL); /* atmarpd has no push */
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 		while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
1821da177e4SLinus Torvalds 			atm_return(vcc,skb->truesize);
1831da177e4SLinus Torvalds 			kfree_skb(skb);
1841da177e4SLinus Torvalds 		}
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds 		module_put(vcc->dev->ops->owner);
1871da177e4SLinus Torvalds 		atm_dev_put(vcc->dev);
1881da177e4SLinus Torvalds 	}
1899301e320SChas Williams 
1909301e320SChas Williams 	vcc_remove_socket(sk);
1911da177e4SLinus Torvalds }
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds int vcc_release(struct socket *sock)
1951da177e4SLinus Torvalds {
1961da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	if (sk) {
1991da177e4SLinus Torvalds 		lock_sock(sk);
2001da177e4SLinus Torvalds 		vcc_destroy_socket(sock->sk);
2011da177e4SLinus Torvalds 		release_sock(sk);
2021da177e4SLinus Torvalds 		sock_put(sk);
2031da177e4SLinus Torvalds 	}
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	return 0;
2061da177e4SLinus Torvalds }
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds void vcc_release_async(struct atm_vcc *vcc, int reply)
2101da177e4SLinus Torvalds {
2111da177e4SLinus Torvalds 	struct sock *sk = sk_atm(vcc);
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 	set_bit(ATM_VF_CLOSE, &vcc->flags);
2141da177e4SLinus Torvalds 	sk->sk_shutdown |= RCV_SHUTDOWN;
2151da177e4SLinus Torvalds 	sk->sk_err = -reply;
2161da177e4SLinus Torvalds 	clear_bit(ATM_VF_WAITING, &vcc->flags);
2171da177e4SLinus Torvalds 	sk->sk_state_change(sk);
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds EXPORT_SYMBOL(vcc_release_async);
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds static int adjust_tp(struct atm_trafprm *tp,unsigned char aal)
2251da177e4SLinus Torvalds {
2261da177e4SLinus Torvalds 	int max_sdu;
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	if (!tp->traffic_class) return 0;
2291da177e4SLinus Torvalds 	switch (aal) {
2301da177e4SLinus Torvalds 		case ATM_AAL0:
2311da177e4SLinus Torvalds 			max_sdu = ATM_CELL_SIZE-1;
2321da177e4SLinus Torvalds 			break;
2331da177e4SLinus Torvalds 		case ATM_AAL34:
2341da177e4SLinus Torvalds 			max_sdu = ATM_MAX_AAL34_PDU;
2351da177e4SLinus Torvalds 			break;
2361da177e4SLinus Torvalds 		default:
2371da177e4SLinus Torvalds 			printk(KERN_WARNING "ATM: AAL problems ... "
2381da177e4SLinus Torvalds 			    "(%d)\n",aal);
2391da177e4SLinus Torvalds 			/* fall through */
2401da177e4SLinus Torvalds 		case ATM_AAL5:
2411da177e4SLinus Torvalds 			max_sdu = ATM_MAX_AAL5_PDU;
2421da177e4SLinus Torvalds 	}
2431da177e4SLinus Torvalds 	if (!tp->max_sdu) tp->max_sdu = max_sdu;
2441da177e4SLinus Torvalds 	else if (tp->max_sdu > max_sdu) return -EINVAL;
2451da177e4SLinus Torvalds 	if (!tp->max_cdv) tp->max_cdv = ATM_MAX_CDV;
2461da177e4SLinus Torvalds 	return 0;
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds static int check_ci(struct atm_vcc *vcc, short vpi, int vci)
2511da177e4SLinus Torvalds {
2521da177e4SLinus Torvalds 	struct hlist_head *head = &vcc_hash[vci &
2531da177e4SLinus Torvalds 					(VCC_HTABLE_SIZE - 1)];
2541da177e4SLinus Torvalds 	struct hlist_node *node;
2551da177e4SLinus Torvalds 	struct sock *s;
2561da177e4SLinus Torvalds 	struct atm_vcc *walk;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	sk_for_each(s, node, head) {
2591da177e4SLinus Torvalds 		walk = atm_sk(s);
2601da177e4SLinus Torvalds 		if (walk->dev != vcc->dev)
2611da177e4SLinus Torvalds 			continue;
2621da177e4SLinus Torvalds 		if (test_bit(ATM_VF_ADDR, &walk->flags) && walk->vpi == vpi &&
2631da177e4SLinus Torvalds 		    walk->vci == vci && ((walk->qos.txtp.traffic_class !=
2641da177e4SLinus Torvalds 		    ATM_NONE && vcc->qos.txtp.traffic_class != ATM_NONE) ||
2651da177e4SLinus Torvalds 		    (walk->qos.rxtp.traffic_class != ATM_NONE &&
2661da177e4SLinus Torvalds 		    vcc->qos.rxtp.traffic_class != ATM_NONE)))
2671da177e4SLinus Torvalds 			return -EADDRINUSE;
2681da177e4SLinus Torvalds 	}
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	/* allow VCCs with same VPI/VCI iff they don't collide on
2711da177e4SLinus Torvalds 	   TX/RX (but we may refuse such sharing for other reasons,
2721da177e4SLinus Torvalds 	   e.g. if protocol requires to have both channels) */
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	return 0;
2751da177e4SLinus Torvalds }
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds static int find_ci(struct atm_vcc *vcc, short *vpi, int *vci)
2791da177e4SLinus Torvalds {
2801da177e4SLinus Torvalds 	static short p;        /* poor man's per-device cache */
2811da177e4SLinus Torvalds 	static int c;
2821da177e4SLinus Torvalds 	short old_p;
2831da177e4SLinus Torvalds 	int old_c;
2841da177e4SLinus Torvalds 	int err;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	if (*vpi != ATM_VPI_ANY && *vci != ATM_VCI_ANY) {
2871da177e4SLinus Torvalds 		err = check_ci(vcc, *vpi, *vci);
2881da177e4SLinus Torvalds 		return err;
2891da177e4SLinus Torvalds 	}
2901da177e4SLinus Torvalds 	/* last scan may have left values out of bounds for current device */
2911da177e4SLinus Torvalds 	if (*vpi != ATM_VPI_ANY)
2921da177e4SLinus Torvalds 		p = *vpi;
2931da177e4SLinus Torvalds 	else if (p >= 1 << vcc->dev->ci_range.vpi_bits)
2941da177e4SLinus Torvalds 		p = 0;
2951da177e4SLinus Torvalds 	if (*vci != ATM_VCI_ANY)
2961da177e4SLinus Torvalds 		c = *vci;
2971da177e4SLinus Torvalds 	else if (c < ATM_NOT_RSV_VCI || c >= 1 << vcc->dev->ci_range.vci_bits)
2981da177e4SLinus Torvalds 			c = ATM_NOT_RSV_VCI;
2991da177e4SLinus Torvalds 	old_p = p;
3001da177e4SLinus Torvalds 	old_c = c;
3011da177e4SLinus Torvalds 	do {
3021da177e4SLinus Torvalds 		if (!check_ci(vcc, p, c)) {
3031da177e4SLinus Torvalds 			*vpi = p;
3041da177e4SLinus Torvalds 			*vci = c;
3051da177e4SLinus Torvalds 			return 0;
3061da177e4SLinus Torvalds 		}
3071da177e4SLinus Torvalds 		if (*vci == ATM_VCI_ANY) {
3081da177e4SLinus Torvalds 			c++;
3091da177e4SLinus Torvalds 			if (c >= 1 << vcc->dev->ci_range.vci_bits)
3101da177e4SLinus Torvalds 				c = ATM_NOT_RSV_VCI;
3111da177e4SLinus Torvalds 		}
3121da177e4SLinus Torvalds 		if ((c == ATM_NOT_RSV_VCI || *vci != ATM_VCI_ANY) &&
3131da177e4SLinus Torvalds 		    *vpi == ATM_VPI_ANY) {
3141da177e4SLinus Torvalds 			p++;
3151da177e4SLinus Torvalds 			if (p >= 1 << vcc->dev->ci_range.vpi_bits) p = 0;
3161da177e4SLinus Torvalds 		}
3171da177e4SLinus Torvalds 	}
3181da177e4SLinus Torvalds 	while (old_p != p || old_c != c);
3191da177e4SLinus Torvalds 	return -EADDRINUSE;
3201da177e4SLinus Torvalds }
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds static int __vcc_connect(struct atm_vcc *vcc, struct atm_dev *dev, short vpi,
3241da177e4SLinus Torvalds 			 int vci)
3251da177e4SLinus Torvalds {
3261da177e4SLinus Torvalds 	struct sock *sk = sk_atm(vcc);
3271da177e4SLinus Torvalds 	int error;
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	if ((vpi != ATM_VPI_UNSPEC && vpi != ATM_VPI_ANY &&
3301da177e4SLinus Torvalds 	    vpi >> dev->ci_range.vpi_bits) || (vci != ATM_VCI_UNSPEC &&
3311da177e4SLinus Torvalds 	    vci != ATM_VCI_ANY && vci >> dev->ci_range.vci_bits))
3321da177e4SLinus Torvalds 		return -EINVAL;
3331da177e4SLinus Torvalds 	if (vci > 0 && vci < ATM_NOT_RSV_VCI && !capable(CAP_NET_BIND_SERVICE))
3341da177e4SLinus Torvalds 		return -EPERM;
3351da177e4SLinus Torvalds 	error = 0;
3361da177e4SLinus Torvalds 	if (!try_module_get(dev->ops->owner))
3371da177e4SLinus Torvalds 		return -ENODEV;
3381da177e4SLinus Torvalds 	vcc->dev = dev;
3391da177e4SLinus Torvalds 	write_lock_irq(&vcc_sklist_lock);
3401da177e4SLinus Torvalds 	if ((error = find_ci(vcc, &vpi, &vci))) {
3411da177e4SLinus Torvalds 		write_unlock_irq(&vcc_sklist_lock);
3421da177e4SLinus Torvalds 		goto fail_module_put;
3431da177e4SLinus Torvalds 	}
3441da177e4SLinus Torvalds 	vcc->vpi = vpi;
3451da177e4SLinus Torvalds 	vcc->vci = vci;
3461da177e4SLinus Torvalds 	__vcc_insert_socket(sk);
3471da177e4SLinus Torvalds 	write_unlock_irq(&vcc_sklist_lock);
3481da177e4SLinus Torvalds 	switch (vcc->qos.aal) {
3491da177e4SLinus Torvalds 		case ATM_AAL0:
3501da177e4SLinus Torvalds 			error = atm_init_aal0(vcc);
3511da177e4SLinus Torvalds 			vcc->stats = &dev->stats.aal0;
3521da177e4SLinus Torvalds 			break;
3531da177e4SLinus Torvalds 		case ATM_AAL34:
3541da177e4SLinus Torvalds 			error = atm_init_aal34(vcc);
3551da177e4SLinus Torvalds 			vcc->stats = &dev->stats.aal34;
3561da177e4SLinus Torvalds 			break;
3571da177e4SLinus Torvalds 		case ATM_NO_AAL:
3581da177e4SLinus Torvalds 			/* ATM_AAL5 is also used in the "0 for default" case */
3591da177e4SLinus Torvalds 			vcc->qos.aal = ATM_AAL5;
3601da177e4SLinus Torvalds 			/* fall through */
3611da177e4SLinus Torvalds 		case ATM_AAL5:
3621da177e4SLinus Torvalds 			error = atm_init_aal5(vcc);
3631da177e4SLinus Torvalds 			vcc->stats = &dev->stats.aal5;
3641da177e4SLinus Torvalds 			break;
3651da177e4SLinus Torvalds 		default:
3661da177e4SLinus Torvalds 			error = -EPROTOTYPE;
3671da177e4SLinus Torvalds 	}
3681da177e4SLinus Torvalds 	if (!error) error = adjust_tp(&vcc->qos.txtp,vcc->qos.aal);
3691da177e4SLinus Torvalds 	if (!error) error = adjust_tp(&vcc->qos.rxtp,vcc->qos.aal);
3701da177e4SLinus Torvalds 	if (error)
3711da177e4SLinus Torvalds 		goto fail;
3721da177e4SLinus Torvalds 	DPRINTK("VCC %d.%d, AAL %d\n",vpi,vci,vcc->qos.aal);
3731da177e4SLinus Torvalds 	DPRINTK("  TX: %d, PCR %d..%d, SDU %d\n",vcc->qos.txtp.traffic_class,
3741da177e4SLinus Torvalds 	    vcc->qos.txtp.min_pcr,vcc->qos.txtp.max_pcr,vcc->qos.txtp.max_sdu);
3751da177e4SLinus Torvalds 	DPRINTK("  RX: %d, PCR %d..%d, SDU %d\n",vcc->qos.rxtp.traffic_class,
3761da177e4SLinus Torvalds 	    vcc->qos.rxtp.min_pcr,vcc->qos.rxtp.max_pcr,vcc->qos.rxtp.max_sdu);
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	if (dev->ops->open) {
3791da177e4SLinus Torvalds 		if ((error = dev->ops->open(vcc)))
3801da177e4SLinus Torvalds 			goto fail;
3811da177e4SLinus Torvalds 	}
3821da177e4SLinus Torvalds 	return 0;
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds fail:
3851da177e4SLinus Torvalds 	vcc_remove_socket(sk);
3861da177e4SLinus Torvalds fail_module_put:
3871da177e4SLinus Torvalds 	module_put(dev->ops->owner);
3881da177e4SLinus Torvalds 	/* ensure we get dev module ref count correct */
3891da177e4SLinus Torvalds 	vcc->dev = NULL;
3901da177e4SLinus Torvalds 	return error;
3911da177e4SLinus Torvalds }
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds int vcc_connect(struct socket *sock, int itf, short vpi, int vci)
3951da177e4SLinus Torvalds {
3961da177e4SLinus Torvalds 	struct atm_dev *dev;
3971da177e4SLinus Torvalds 	struct atm_vcc *vcc = ATM_SD(sock);
3981da177e4SLinus Torvalds 	int error;
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds 	DPRINTK("vcc_connect (vpi %d, vci %d)\n",vpi,vci);
4011da177e4SLinus Torvalds 	if (sock->state == SS_CONNECTED)
4021da177e4SLinus Torvalds 		return -EISCONN;
4031da177e4SLinus Torvalds 	if (sock->state != SS_UNCONNECTED)
4041da177e4SLinus Torvalds 		return -EINVAL;
4051da177e4SLinus Torvalds 	if (!(vpi || vci))
4061da177e4SLinus Torvalds 		return -EINVAL;
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	if (vpi != ATM_VPI_UNSPEC && vci != ATM_VCI_UNSPEC)
4091da177e4SLinus Torvalds 		clear_bit(ATM_VF_PARTIAL,&vcc->flags);
4101da177e4SLinus Torvalds 	else
4111da177e4SLinus Torvalds 		if (test_bit(ATM_VF_PARTIAL,&vcc->flags))
4121da177e4SLinus Torvalds 			return -EINVAL;
4131da177e4SLinus Torvalds 	DPRINTK("vcc_connect (TX: cl %d,bw %d-%d,sdu %d; "
4141da177e4SLinus Torvalds 	    "RX: cl %d,bw %d-%d,sdu %d,AAL %s%d)\n",
4151da177e4SLinus Torvalds 	    vcc->qos.txtp.traffic_class,vcc->qos.txtp.min_pcr,
4161da177e4SLinus Torvalds 	    vcc->qos.txtp.max_pcr,vcc->qos.txtp.max_sdu,
4171da177e4SLinus Torvalds 	    vcc->qos.rxtp.traffic_class,vcc->qos.rxtp.min_pcr,
4181da177e4SLinus Torvalds 	    vcc->qos.rxtp.max_pcr,vcc->qos.rxtp.max_sdu,
4191da177e4SLinus Torvalds 	    vcc->qos.aal == ATM_AAL5 ? "" : vcc->qos.aal == ATM_AAL0 ? "" :
4201da177e4SLinus Torvalds 	    " ??? code ",vcc->qos.aal == ATM_AAL0 ? 0 : vcc->qos.aal);
4211da177e4SLinus Torvalds 	if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
4221da177e4SLinus Torvalds 		return -EBADFD;
4231da177e4SLinus Torvalds 	if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
4241da177e4SLinus Torvalds 	    vcc->qos.rxtp.traffic_class == ATM_ANYCLASS)
4251da177e4SLinus Torvalds 		return -EINVAL;
426*c9933d08SMitchell Blank Jr 	if (likely(itf != ATM_ITF_ANY)) {
4271da177e4SLinus Torvalds 		dev = atm_dev_lookup(itf);
428*c9933d08SMitchell Blank Jr 	} else {
429*c9933d08SMitchell Blank Jr 		dev = NULL;
430*c9933d08SMitchell Blank Jr 		spin_lock(&atm_dev_lock);
431*c9933d08SMitchell Blank Jr 		if (!list_empty(&atm_devs)) {
432*c9933d08SMitchell Blank Jr 			dev = list_entry(atm_devs.next, struct atm_dev, dev_list);
433*c9933d08SMitchell Blank Jr 			atm_dev_hold(dev);
434*c9933d08SMitchell Blank Jr 		}
435*c9933d08SMitchell Blank Jr 		spin_unlock(&atm_dev_lock);
436*c9933d08SMitchell Blank Jr 	}
4371da177e4SLinus Torvalds 	if (!dev)
4381da177e4SLinus Torvalds 		return -ENODEV;
4391da177e4SLinus Torvalds 	error = __vcc_connect(vcc, dev, vpi, vci);
4401da177e4SLinus Torvalds 	if (error) {
4411da177e4SLinus Torvalds 		atm_dev_put(dev);
4421da177e4SLinus Torvalds 		return error;
4431da177e4SLinus Torvalds 	}
4441da177e4SLinus Torvalds 	if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC)
4451da177e4SLinus Torvalds 		set_bit(ATM_VF_PARTIAL,&vcc->flags);
4461da177e4SLinus Torvalds 	if (test_bit(ATM_VF_READY,&ATM_SD(sock)->flags))
4471da177e4SLinus Torvalds 		sock->state = SS_CONNECTED;
4481da177e4SLinus Torvalds 	return 0;
4491da177e4SLinus Torvalds }
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds int vcc_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
4531da177e4SLinus Torvalds 		size_t size, int flags)
4541da177e4SLinus Torvalds {
4551da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4561da177e4SLinus Torvalds 	struct atm_vcc *vcc;
4571da177e4SLinus Torvalds 	struct sk_buff *skb;
4581da177e4SLinus Torvalds 	int copied, error = -EINVAL;
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	if (sock->state != SS_CONNECTED)
4611da177e4SLinus Torvalds 		return -ENOTCONN;
4621da177e4SLinus Torvalds 	if (flags & ~MSG_DONTWAIT)		/* only handle MSG_DONTWAIT */
4631da177e4SLinus Torvalds 		return -EOPNOTSUPP;
4641da177e4SLinus Torvalds 	vcc = ATM_SD(sock);
4651da177e4SLinus Torvalds 	if (test_bit(ATM_VF_RELEASED,&vcc->flags) ||
4661da177e4SLinus Torvalds 	    test_bit(ATM_VF_CLOSE,&vcc->flags) ||
4671da177e4SLinus Torvalds 	    !test_bit(ATM_VF_READY, &vcc->flags))
4681da177e4SLinus Torvalds 		return 0;
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &error);
4711da177e4SLinus Torvalds 	if (!skb)
4721da177e4SLinus Torvalds 		return error;
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds 	copied = skb->len;
4751da177e4SLinus Torvalds 	if (copied > size) {
4761da177e4SLinus Torvalds 		copied = size;
4771da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
4781da177e4SLinus Torvalds 	}
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds         error = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
4811da177e4SLinus Torvalds         if (error)
4821da177e4SLinus Torvalds                 return error;
4831da177e4SLinus Torvalds         sock_recv_timestamp(msg, sk, skb);
4841da177e4SLinus Torvalds         DPRINTK("RcvM %d -= %d\n", atomic_read(&sk->rmem_alloc), skb->truesize);
4851da177e4SLinus Torvalds         atm_return(vcc, skb->truesize);
4861da177e4SLinus Torvalds         skb_free_datagram(sk, skb);
4871da177e4SLinus Torvalds         return copied;
4881da177e4SLinus Torvalds }
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds int vcc_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
4921da177e4SLinus Torvalds 		size_t total_len)
4931da177e4SLinus Torvalds {
4941da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4951da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
4961da177e4SLinus Torvalds 	struct atm_vcc *vcc;
4971da177e4SLinus Torvalds 	struct sk_buff *skb;
4981da177e4SLinus Torvalds 	int eff,error;
4991da177e4SLinus Torvalds 	const void __user *buff;
5001da177e4SLinus Torvalds 	int size;
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	lock_sock(sk);
5031da177e4SLinus Torvalds 	if (sock->state != SS_CONNECTED) {
5041da177e4SLinus Torvalds 		error = -ENOTCONN;
5051da177e4SLinus Torvalds 		goto out;
5061da177e4SLinus Torvalds 	}
5071da177e4SLinus Torvalds 	if (m->msg_name) {
5081da177e4SLinus Torvalds 		error = -EISCONN;
5091da177e4SLinus Torvalds 		goto out;
5101da177e4SLinus Torvalds 	}
5111da177e4SLinus Torvalds 	if (m->msg_iovlen != 1) {
5121da177e4SLinus Torvalds 		error = -ENOSYS; /* fix this later @@@ */
5131da177e4SLinus Torvalds 		goto out;
5141da177e4SLinus Torvalds 	}
5151da177e4SLinus Torvalds 	buff = m->msg_iov->iov_base;
5161da177e4SLinus Torvalds 	size = m->msg_iov->iov_len;
5171da177e4SLinus Torvalds 	vcc = ATM_SD(sock);
5181da177e4SLinus Torvalds 	if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
5191da177e4SLinus Torvalds 	    test_bit(ATM_VF_CLOSE, &vcc->flags) ||
5201da177e4SLinus Torvalds 	    !test_bit(ATM_VF_READY, &vcc->flags)) {
5211da177e4SLinus Torvalds 		error = -EPIPE;
5221da177e4SLinus Torvalds 		send_sig(SIGPIPE, current, 0);
5231da177e4SLinus Torvalds 		goto out;
5241da177e4SLinus Torvalds 	}
5251da177e4SLinus Torvalds 	if (!size) {
5261da177e4SLinus Torvalds 		error = 0;
5271da177e4SLinus Torvalds 		goto out;
5281da177e4SLinus Torvalds 	}
5291da177e4SLinus Torvalds 	if (size < 0 || size > vcc->qos.txtp.max_sdu) {
5301da177e4SLinus Torvalds 		error = -EMSGSIZE;
5311da177e4SLinus Torvalds 		goto out;
5321da177e4SLinus Torvalds 	}
533e49332bdSJesper Juhl 
5341da177e4SLinus Torvalds 	eff = (size+3) & ~3; /* align to word boundary */
5351da177e4SLinus Torvalds 	prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5361da177e4SLinus Torvalds 	error = 0;
5371da177e4SLinus Torvalds 	while (!(skb = alloc_tx(vcc,eff))) {
5381da177e4SLinus Torvalds 		if (m->msg_flags & MSG_DONTWAIT) {
5391da177e4SLinus Torvalds 			error = -EAGAIN;
5401da177e4SLinus Torvalds 			break;
5411da177e4SLinus Torvalds 		}
5421da177e4SLinus Torvalds 		schedule();
5431da177e4SLinus Torvalds 		if (signal_pending(current)) {
5441da177e4SLinus Torvalds 			error = -ERESTARTSYS;
5451da177e4SLinus Torvalds 			break;
5461da177e4SLinus Torvalds 		}
5471da177e4SLinus Torvalds 		if (test_bit(ATM_VF_RELEASED,&vcc->flags) ||
5481da177e4SLinus Torvalds 		    test_bit(ATM_VF_CLOSE,&vcc->flags) ||
5491da177e4SLinus Torvalds 		    !test_bit(ATM_VF_READY,&vcc->flags)) {
5501da177e4SLinus Torvalds 			error = -EPIPE;
5511da177e4SLinus Torvalds 			send_sig(SIGPIPE, current, 0);
5521da177e4SLinus Torvalds 			break;
5531da177e4SLinus Torvalds 		}
5541da177e4SLinus Torvalds 		prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5551da177e4SLinus Torvalds 	}
5561da177e4SLinus Torvalds 	finish_wait(sk->sk_sleep, &wait);
5571da177e4SLinus Torvalds 	if (error)
5581da177e4SLinus Torvalds 		goto out;
5591da177e4SLinus Torvalds 	skb->dev = NULL; /* for paths shared with net_device interfaces */
5601da177e4SLinus Torvalds 	ATM_SKB(skb)->atm_options = vcc->atm_options;
5611da177e4SLinus Torvalds 	if (copy_from_user(skb_put(skb,size),buff,size)) {
5621da177e4SLinus Torvalds 		kfree_skb(skb);
5631da177e4SLinus Torvalds 		error = -EFAULT;
5641da177e4SLinus Torvalds 		goto out;
5651da177e4SLinus Torvalds 	}
5661da177e4SLinus Torvalds 	if (eff != size) memset(skb->data+size,0,eff-size);
5671da177e4SLinus Torvalds 	error = vcc->dev->ops->send(vcc,skb);
5681da177e4SLinus Torvalds 	error = error ? error : size;
5691da177e4SLinus Torvalds out:
5701da177e4SLinus Torvalds 	release_sock(sk);
5711da177e4SLinus Torvalds 	return error;
5721da177e4SLinus Torvalds }
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds unsigned int vcc_poll(struct file *file, struct socket *sock, poll_table *wait)
5761da177e4SLinus Torvalds {
5771da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
5781da177e4SLinus Torvalds 	struct atm_vcc *vcc;
5791da177e4SLinus Torvalds 	unsigned int mask;
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	poll_wait(file, sk->sk_sleep, wait);
5821da177e4SLinus Torvalds 	mask = 0;
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	vcc = ATM_SD(sock);
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 	/* exceptional events */
5871da177e4SLinus Torvalds 	if (sk->sk_err)
5881da177e4SLinus Torvalds 		mask = POLLERR;
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
5911da177e4SLinus Torvalds 	    test_bit(ATM_VF_CLOSE, &vcc->flags))
5921da177e4SLinus Torvalds 		mask |= POLLHUP;
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 	/* readable? */
5951da177e4SLinus Torvalds 	if (!skb_queue_empty(&sk->sk_receive_queue))
5961da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	/* writable? */
5991da177e4SLinus Torvalds 	if (sock->state == SS_CONNECTING &&
6001da177e4SLinus Torvalds 	    test_bit(ATM_VF_WAITING, &vcc->flags))
6011da177e4SLinus Torvalds 		return mask;
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	if (vcc->qos.txtp.traffic_class != ATM_NONE &&
6041da177e4SLinus Torvalds 	    vcc_writable(sk))
6051da177e4SLinus Torvalds 		mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	return mask;
6081da177e4SLinus Torvalds }
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds static int atm_change_qos(struct atm_vcc *vcc,struct atm_qos *qos)
6121da177e4SLinus Torvalds {
6131da177e4SLinus Torvalds 	int error;
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds 	/*
6161da177e4SLinus Torvalds 	 * Don't let the QoS change the already connected AAL type nor the
6171da177e4SLinus Torvalds 	 * traffic class.
6181da177e4SLinus Torvalds 	 */
6191da177e4SLinus Torvalds 	if (qos->aal != vcc->qos.aal ||
6201da177e4SLinus Torvalds 	    qos->rxtp.traffic_class != vcc->qos.rxtp.traffic_class ||
6211da177e4SLinus Torvalds 	    qos->txtp.traffic_class != vcc->qos.txtp.traffic_class)
6221da177e4SLinus Torvalds 		return -EINVAL;
6231da177e4SLinus Torvalds 	error = adjust_tp(&qos->txtp,qos->aal);
6241da177e4SLinus Torvalds 	if (!error) error = adjust_tp(&qos->rxtp,qos->aal);
6251da177e4SLinus Torvalds 	if (error) return error;
6261da177e4SLinus Torvalds 	if (!vcc->dev->ops->change_qos) return -EOPNOTSUPP;
6271da177e4SLinus Torvalds 	if (sk_atm(vcc)->sk_family == AF_ATMPVC)
6281da177e4SLinus Torvalds 		return vcc->dev->ops->change_qos(vcc,qos,ATM_MF_SET);
6291da177e4SLinus Torvalds 	return svc_change_qos(vcc,qos);
6301da177e4SLinus Torvalds }
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds static int check_tp(struct atm_trafprm *tp)
6341da177e4SLinus Torvalds {
6351da177e4SLinus Torvalds 	/* @@@ Should be merged with adjust_tp */
6361da177e4SLinus Torvalds 	if (!tp->traffic_class || tp->traffic_class == ATM_ANYCLASS) return 0;
6371da177e4SLinus Torvalds 	if (tp->traffic_class != ATM_UBR && !tp->min_pcr && !tp->pcr &&
6381da177e4SLinus Torvalds 	    !tp->max_pcr) return -EINVAL;
6391da177e4SLinus Torvalds 	if (tp->min_pcr == ATM_MAX_PCR) return -EINVAL;
6401da177e4SLinus Torvalds 	if (tp->min_pcr && tp->max_pcr && tp->max_pcr != ATM_MAX_PCR &&
6411da177e4SLinus Torvalds 	    tp->min_pcr > tp->max_pcr) return -EINVAL;
6421da177e4SLinus Torvalds 	/*
6431da177e4SLinus Torvalds 	 * We allow pcr to be outside [min_pcr,max_pcr], because later
6441da177e4SLinus Torvalds 	 * adjustment may still push it in the valid range.
6451da177e4SLinus Torvalds 	 */
6461da177e4SLinus Torvalds 	return 0;
6471da177e4SLinus Torvalds }
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds static int check_qos(struct atm_qos *qos)
6511da177e4SLinus Torvalds {
6521da177e4SLinus Torvalds 	int error;
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	if (!qos->txtp.traffic_class && !qos->rxtp.traffic_class)
6551da177e4SLinus Torvalds                 return -EINVAL;
6561da177e4SLinus Torvalds 	if (qos->txtp.traffic_class != qos->rxtp.traffic_class &&
6571da177e4SLinus Torvalds 	    qos->txtp.traffic_class && qos->rxtp.traffic_class &&
6581da177e4SLinus Torvalds 	    qos->txtp.traffic_class != ATM_ANYCLASS &&
6591da177e4SLinus Torvalds 	    qos->rxtp.traffic_class != ATM_ANYCLASS) return -EINVAL;
6601da177e4SLinus Torvalds 	error = check_tp(&qos->txtp);
6611da177e4SLinus Torvalds 	if (error) return error;
6621da177e4SLinus Torvalds 	return check_tp(&qos->rxtp);
6631da177e4SLinus Torvalds }
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds int vcc_setsockopt(struct socket *sock, int level, int optname,
6661da177e4SLinus Torvalds 		   char __user *optval, int optlen)
6671da177e4SLinus Torvalds {
6681da177e4SLinus Torvalds 	struct atm_vcc *vcc;
6691da177e4SLinus Torvalds 	unsigned long value;
6701da177e4SLinus Torvalds 	int error;
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	if (__SO_LEVEL_MATCH(optname, level) && optlen != __SO_SIZE(optname))
6731da177e4SLinus Torvalds 		return -EINVAL;
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 	vcc = ATM_SD(sock);
6761da177e4SLinus Torvalds 	switch (optname) {
6771da177e4SLinus Torvalds 		case SO_ATMQOS:
6781da177e4SLinus Torvalds 			{
6791da177e4SLinus Torvalds 				struct atm_qos qos;
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 				if (copy_from_user(&qos,optval,sizeof(qos)))
6821da177e4SLinus Torvalds 					return -EFAULT;
6831da177e4SLinus Torvalds 				error = check_qos(&qos);
6841da177e4SLinus Torvalds 				if (error) return error;
6851da177e4SLinus Torvalds 				if (sock->state == SS_CONNECTED)
6861da177e4SLinus Torvalds 					return atm_change_qos(vcc,&qos);
6871da177e4SLinus Torvalds 				if (sock->state != SS_UNCONNECTED)
6881da177e4SLinus Torvalds 					return -EBADFD;
6891da177e4SLinus Torvalds 				vcc->qos = qos;
6901da177e4SLinus Torvalds 				set_bit(ATM_VF_HASQOS,&vcc->flags);
6911da177e4SLinus Torvalds 				return 0;
6921da177e4SLinus Torvalds 			}
6931da177e4SLinus Torvalds 		case SO_SETCLP:
6941da177e4SLinus Torvalds 			if (get_user(value,(unsigned long __user *)optval))
6951da177e4SLinus Torvalds 				return -EFAULT;
6961da177e4SLinus Torvalds 			if (value) vcc->atm_options |= ATM_ATMOPT_CLP;
6971da177e4SLinus Torvalds 			else vcc->atm_options &= ~ATM_ATMOPT_CLP;
6981da177e4SLinus Torvalds 			return 0;
6991da177e4SLinus Torvalds 		default:
7001da177e4SLinus Torvalds 			if (level == SOL_SOCKET) return -EINVAL;
7011da177e4SLinus Torvalds 			break;
7021da177e4SLinus Torvalds 	}
7031da177e4SLinus Torvalds 	if (!vcc->dev || !vcc->dev->ops->setsockopt) return -EINVAL;
7041da177e4SLinus Torvalds 	return vcc->dev->ops->setsockopt(vcc,level,optname,optval,optlen);
7051da177e4SLinus Torvalds }
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds int vcc_getsockopt(struct socket *sock, int level, int optname,
7091da177e4SLinus Torvalds 		   char __user *optval, int __user *optlen)
7101da177e4SLinus Torvalds {
7111da177e4SLinus Torvalds 	struct atm_vcc *vcc;
7121da177e4SLinus Torvalds 	int len;
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 	if (get_user(len, optlen))
7151da177e4SLinus Torvalds 		return -EFAULT;
7161da177e4SLinus Torvalds 	if (__SO_LEVEL_MATCH(optname, level) && len != __SO_SIZE(optname))
7171da177e4SLinus Torvalds 		return -EINVAL;
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds 	vcc = ATM_SD(sock);
7201da177e4SLinus Torvalds 	switch (optname) {
7211da177e4SLinus Torvalds 		case SO_ATMQOS:
7221da177e4SLinus Torvalds 			if (!test_bit(ATM_VF_HASQOS,&vcc->flags))
7231da177e4SLinus Torvalds 				return -EINVAL;
7241da177e4SLinus Torvalds 			return copy_to_user(optval,&vcc->qos,sizeof(vcc->qos)) ?
7251da177e4SLinus Torvalds 			    -EFAULT : 0;
7261da177e4SLinus Torvalds 		case SO_SETCLP:
7271da177e4SLinus Torvalds 			return put_user(vcc->atm_options & ATM_ATMOPT_CLP ? 1 :
7281da177e4SLinus Torvalds 			  0,(unsigned long __user *)optval) ? -EFAULT : 0;
7291da177e4SLinus Torvalds 		case SO_ATMPVC:
7301da177e4SLinus Torvalds 			{
7311da177e4SLinus Torvalds 				struct sockaddr_atmpvc pvc;
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds 				if (!vcc->dev ||
7341da177e4SLinus Torvalds 				    !test_bit(ATM_VF_ADDR,&vcc->flags))
7351da177e4SLinus Torvalds 					return -ENOTCONN;
7361da177e4SLinus Torvalds 				pvc.sap_family = AF_ATMPVC;
7371da177e4SLinus Torvalds 				pvc.sap_addr.itf = vcc->dev->number;
7381da177e4SLinus Torvalds 				pvc.sap_addr.vpi = vcc->vpi;
7391da177e4SLinus Torvalds 				pvc.sap_addr.vci = vcc->vci;
7401da177e4SLinus Torvalds 				return copy_to_user(optval,&pvc,sizeof(pvc)) ?
7411da177e4SLinus Torvalds 				    -EFAULT : 0;
7421da177e4SLinus Torvalds 			}
7431da177e4SLinus Torvalds 		default:
7441da177e4SLinus Torvalds 			if (level == SOL_SOCKET) return -EINVAL;
7451da177e4SLinus Torvalds 			break;
7461da177e4SLinus Torvalds 	}
7471da177e4SLinus Torvalds 	if (!vcc->dev || !vcc->dev->ops->getsockopt) return -EINVAL;
7481da177e4SLinus Torvalds 	return vcc->dev->ops->getsockopt(vcc, level, optname, optval, len);
7491da177e4SLinus Torvalds }
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds static int __init atm_init(void)
7521da177e4SLinus Torvalds {
7531da177e4SLinus Torvalds 	int error;
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	if ((error = proto_register(&vcc_proto, 0)) < 0)
7561da177e4SLinus Torvalds 		goto out;
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds 	if ((error = atmpvc_init()) < 0) {
7591da177e4SLinus Torvalds 		printk(KERN_ERR "atmpvc_init() failed with %d\n", error);
7601da177e4SLinus Torvalds 		goto out_unregister_vcc_proto;
7611da177e4SLinus Torvalds 	}
7621da177e4SLinus Torvalds 	if ((error = atmsvc_init()) < 0) {
7631da177e4SLinus Torvalds 		printk(KERN_ERR "atmsvc_init() failed with %d\n", error);
7641da177e4SLinus Torvalds 		goto out_atmpvc_exit;
7651da177e4SLinus Torvalds 	}
7661da177e4SLinus Torvalds         if ((error = atm_proc_init()) < 0) {
7671da177e4SLinus Torvalds 		printk(KERN_ERR "atm_proc_init() failed with %d\n",error);
7681da177e4SLinus Torvalds 		goto out_atmsvc_exit;
7691da177e4SLinus Torvalds 	}
7701da177e4SLinus Torvalds out:
7711da177e4SLinus Torvalds 	return error;
7721da177e4SLinus Torvalds out_atmsvc_exit:
7731da177e4SLinus Torvalds 	atmsvc_exit();
7741da177e4SLinus Torvalds out_atmpvc_exit:
7751da177e4SLinus Torvalds 	atmsvc_exit();
7761da177e4SLinus Torvalds out_unregister_vcc_proto:
7771da177e4SLinus Torvalds 	proto_unregister(&vcc_proto);
7781da177e4SLinus Torvalds 	goto out;
7791da177e4SLinus Torvalds }
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds static void __exit atm_exit(void)
7821da177e4SLinus Torvalds {
7831da177e4SLinus Torvalds 	atm_proc_exit();
7841da177e4SLinus Torvalds 	atmsvc_exit();
7851da177e4SLinus Torvalds 	atmpvc_exit();
7861da177e4SLinus Torvalds 	proto_unregister(&vcc_proto);
7871da177e4SLinus Torvalds }
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds module_init(atm_init);
7901da177e4SLinus Torvalds module_exit(atm_exit);
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds MODULE_LICENSE("GPL");
7931da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_ATMPVC);
7941da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_ATMSVC);
795