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