blob: 74d095a081e3ed0a667ab6805b2c361d51cf81d3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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 Perches99824462010-01-26 11:40:00 +00005#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#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 Juhle49332b2005-05-01 08:59:08 -070015#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#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>
21#include <net/sock.h> /* struct sock */
Joe Perchesa8147d72010-01-26 11:40:06 +000022#include <linux/uaccess.h>
23#include <linux/poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include "resources.h" /* atm_find_dev */
28#include "common.h" /* prototypes */
29#include "protocols.h" /* atm_init_<transport> */
30#include "addr.h" /* address registry */
31#include "signaling.h" /* for WAITING and sigd_attach */
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033struct hlist_head vcc_hash[VCC_HTABLE_SIZE];
Joe Perchesa8147d72010-01-26 11:40:06 +000034EXPORT_SYMBOL(vcc_hash);
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036DEFINE_RWLOCK(vcc_sklist_lock);
Joe Perchesa8147d72010-01-26 11:40:06 +000037EXPORT_SYMBOL(vcc_sklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39static void __vcc_insert_socket(struct sock *sk)
40{
41 struct atm_vcc *vcc = atm_sk(sk);
Joe Perchesa8147d72010-01-26 11:40:06 +000042 struct hlist_head *head = &vcc_hash[vcc->vci & (VCC_HTABLE_SIZE - 1)];
Eric Dumazet81c3d542005-10-03 14:13:38 -070043 sk->sk_hash = vcc->vci & (VCC_HTABLE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 sk_add_node(sk, head);
45}
46
47void vcc_insert_socket(struct sock *sk)
48{
49 write_lock_irq(&vcc_sklist_lock);
50 __vcc_insert_socket(sk);
51 write_unlock_irq(&vcc_sklist_lock);
52}
Joe Perchesa8147d72010-01-26 11:40:06 +000053EXPORT_SYMBOL(vcc_insert_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55static void vcc_remove_socket(struct sock *sk)
56{
57 write_lock_irq(&vcc_sklist_lock);
58 sk_del_node_init(sk);
59 write_unlock_irq(&vcc_sklist_lock);
60}
61
Joe Perchesa8147d72010-01-26 11:40:06 +000062static struct sk_buff *alloc_tx(struct atm_vcc *vcc, unsigned int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
64 struct sk_buff *skb;
65 struct sock *sk = sk_atm(vcc);
66
Eric Dumazet81e2a3d2009-06-17 19:06:12 -070067 if (sk_wmem_alloc_get(sk) && !atm_may_send(vcc, size)) {
Stephen Hemminger52240062007-08-28 15:22:09 -070068 pr_debug("Sorry: wmem_alloc = %d, size = %d, sndbuf = %d\n",
Joe Perches99824462010-01-26 11:40:00 +000069 sk_wmem_alloc_get(sk), size, sk->sk_sndbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 return NULL;
71 }
Eric Dumazet81e2a3d2009-06-17 19:06:12 -070072 while (!(skb = alloc_skb(size, GFP_KERNEL)))
73 schedule();
Joe Perchesa8147d72010-01-26 11:40:06 +000074 pr_debug("%d += %d\n", sk_wmem_alloc_get(sk), skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 atomic_add(skb->truesize, &sk->sk_wmem_alloc);
76 return skb;
77}
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static void vcc_sock_destruct(struct sock *sk)
80{
81 if (atomic_read(&sk->sk_rmem_alloc))
Joe Perchesa8147d72010-01-26 11:40:06 +000082 printk(KERN_DEBUG "%s: rmem leakage (%d bytes) detected.\n",
83 __func__, atomic_read(&sk->sk_rmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 if (atomic_read(&sk->sk_wmem_alloc))
Joe Perchesa8147d72010-01-26 11:40:06 +000086 printk(KERN_DEBUG "%s: wmem leakage (%d bytes) detected.\n",
87 __func__, atomic_read(&sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90static void vcc_def_wakeup(struct sock *sk)
91{
92 read_lock(&sk->sk_callback_lock);
Jiri Olsaa57de0b2009-07-08 12:09:13 +000093 if (sk_has_sleeper(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 wake_up(sk->sk_sleep);
95 read_unlock(&sk->sk_callback_lock);
96}
97
98static inline int vcc_writable(struct sock *sk)
99{
100 struct atm_vcc *vcc = atm_sk(sk);
101
102 return (vcc->qos.txtp.max_sdu +
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900103 atomic_read(&sk->sk_wmem_alloc)) <= sk->sk_sndbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106static void vcc_write_space(struct sock *sk)
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900107{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 read_lock(&sk->sk_callback_lock);
109
110 if (vcc_writable(sk)) {
Jiri Olsaa57de0b2009-07-08 12:09:13 +0000111 if (sk_has_sleeper(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 wake_up_interruptible(sk->sk_sleep);
113
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +0800114 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
116
117 read_unlock(&sk->sk_callback_lock);
118}
119
120static struct proto vcc_proto = {
121 .name = "VCC",
122 .owner = THIS_MODULE,
123 .obj_size = sizeof(struct atm_vcc),
124};
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900125
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700126int vcc_create(struct net *net, struct socket *sock, int protocol, int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 struct sock *sk;
129 struct atm_vcc *vcc;
130
131 sock->sk = NULL;
132 if (sock->type == SOCK_STREAM)
133 return -EINVAL;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700134 sk = sk_alloc(net, family, GFP_KERNEL, &vcc_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (!sk)
136 return -ENOMEM;
137 sock_init_data(sock, sk);
138 sk->sk_state_change = vcc_def_wakeup;
139 sk->sk_write_space = vcc_write_space;
140
141 vcc = atm_sk(sk);
142 vcc->dev = NULL;
Joe Perchesa8147d72010-01-26 11:40:06 +0000143 memset(&vcc->local, 0, sizeof(struct sockaddr_atmsvc));
144 memset(&vcc->remote, 0, sizeof(struct sockaddr_atmsvc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 vcc->qos.txtp.max_sdu = 1 << 16; /* for meta VCs */
Eric Dumazet81e2a3d2009-06-17 19:06:12 -0700146 atomic_set(&sk->sk_wmem_alloc, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 atomic_set(&sk->sk_rmem_alloc, 0);
148 vcc->push = NULL;
149 vcc->pop = NULL;
150 vcc->push_oam = NULL;
151 vcc->vpi = vcc->vci = 0; /* no VCI/VPI yet */
152 vcc->atm_options = vcc->aal_options = 0;
153 sk->sk_destruct = vcc_sock_destruct;
154 return 0;
155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static void vcc_destroy_socket(struct sock *sk)
158{
159 struct atm_vcc *vcc = atm_sk(sk);
160 struct sk_buff *skb;
161
162 set_bit(ATM_VF_CLOSE, &vcc->flags);
163 clear_bit(ATM_VF_READY, &vcc->flags);
164 if (vcc->dev) {
165 if (vcc->dev->ops->close)
166 vcc->dev->ops->close(vcc);
167 if (vcc->push)
168 vcc->push(vcc, NULL); /* atmarpd has no push */
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
Joe Perchesa8147d72010-01-26 11:40:06 +0000171 atm_return(vcc, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 kfree_skb(skb);
173 }
174
175 module_put(vcc->dev->ops->owner);
176 atm_dev_put(vcc->dev);
177 }
Chas Williams9301e322005-09-28 16:35:01 -0700178
179 vcc_remove_socket(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182int vcc_release(struct socket *sock)
183{
184 struct sock *sk = sock->sk;
185
186 if (sk) {
187 lock_sock(sk);
188 vcc_destroy_socket(sock->sk);
189 release_sock(sk);
190 sock_put(sk);
191 }
192
193 return 0;
194}
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196void vcc_release_async(struct atm_vcc *vcc, int reply)
197{
198 struct sock *sk = sk_atm(vcc);
199
200 set_bit(ATM_VF_CLOSE, &vcc->flags);
201 sk->sk_shutdown |= RCV_SHUTDOWN;
202 sk->sk_err = -reply;
203 clear_bit(ATM_VF_WAITING, &vcc->flags);
204 sk->sk_state_change(sk);
205}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206EXPORT_SYMBOL(vcc_release_async);
207
208
Stanislaw Gruszka64bf69d2005-11-29 16:16:41 -0800209void atm_dev_release_vccs(struct atm_dev *dev)
210{
211 int i;
212
213 write_lock_irq(&vcc_sklist_lock);
214 for (i = 0; i < VCC_HTABLE_SIZE; i++) {
215 struct hlist_head *head = &vcc_hash[i];
216 struct hlist_node *node, *tmp;
217 struct sock *s;
218 struct atm_vcc *vcc;
219
220 sk_for_each_safe(s, node, tmp, head) {
221 vcc = atm_sk(s);
222 if (vcc->dev == dev) {
223 vcc_release_async(vcc, -EPIPE);
224 sk_del_node_init(s);
225 }
226 }
227 }
228 write_unlock_irq(&vcc_sklist_lock);
229}
230
Joe Perchesa8147d72010-01-26 11:40:06 +0000231static int adjust_tp(struct atm_trafprm *tp, unsigned char aal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 int max_sdu;
234
Joe Perchesa8147d72010-01-26 11:40:06 +0000235 if (!tp->traffic_class)
236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 switch (aal) {
Joe Perchesa8147d72010-01-26 11:40:06 +0000238 case ATM_AAL0:
239 max_sdu = ATM_CELL_SIZE-1;
240 break;
241 case ATM_AAL34:
242 max_sdu = ATM_MAX_AAL34_PDU;
243 break;
244 default:
245 pr_warning("AAL problems ... (%d)\n", aal);
246 /* fall through */
247 case ATM_AAL5:
248 max_sdu = ATM_MAX_AAL5_PDU;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000250 if (!tp->max_sdu)
251 tp->max_sdu = max_sdu;
252 else if (tp->max_sdu > max_sdu)
253 return -EINVAL;
254 if (!tp->max_cdv)
255 tp->max_cdv = ATM_MAX_CDV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return 0;
257}
258
Mitchell Blank Jr61c33e02008-06-17 16:20:06 -0700259static int check_ci(const struct atm_vcc *vcc, short vpi, int vci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Joe Perchesa8147d72010-01-26 11:40:06 +0000261 struct hlist_head *head = &vcc_hash[vci & (VCC_HTABLE_SIZE - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 struct hlist_node *node;
263 struct sock *s;
264 struct atm_vcc *walk;
265
266 sk_for_each(s, node, head) {
267 walk = atm_sk(s);
268 if (walk->dev != vcc->dev)
269 continue;
270 if (test_bit(ATM_VF_ADDR, &walk->flags) && walk->vpi == vpi &&
271 walk->vci == vci && ((walk->qos.txtp.traffic_class !=
272 ATM_NONE && vcc->qos.txtp.traffic_class != ATM_NONE) ||
273 (walk->qos.rxtp.traffic_class != ATM_NONE &&
274 vcc->qos.rxtp.traffic_class != ATM_NONE)))
275 return -EADDRINUSE;
276 }
277
278 /* allow VCCs with same VPI/VCI iff they don't collide on
279 TX/RX (but we may refuse such sharing for other reasons,
280 e.g. if protocol requires to have both channels) */
281
282 return 0;
283}
284
Mitchell Blank Jr61c33e02008-06-17 16:20:06 -0700285static int find_ci(const struct atm_vcc *vcc, short *vpi, int *vci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 static short p; /* poor man's per-device cache */
288 static int c;
289 short old_p;
290 int old_c;
291 int err;
292
293 if (*vpi != ATM_VPI_ANY && *vci != ATM_VCI_ANY) {
294 err = check_ci(vcc, *vpi, *vci);
295 return err;
296 }
297 /* last scan may have left values out of bounds for current device */
298 if (*vpi != ATM_VPI_ANY)
299 p = *vpi;
300 else if (p >= 1 << vcc->dev->ci_range.vpi_bits)
301 p = 0;
302 if (*vci != ATM_VCI_ANY)
303 c = *vci;
304 else if (c < ATM_NOT_RSV_VCI || c >= 1 << vcc->dev->ci_range.vci_bits)
305 c = ATM_NOT_RSV_VCI;
306 old_p = p;
307 old_c = c;
308 do {
309 if (!check_ci(vcc, p, c)) {
310 *vpi = p;
311 *vci = c;
312 return 0;
313 }
314 if (*vci == ATM_VCI_ANY) {
315 c++;
316 if (c >= 1 << vcc->dev->ci_range.vci_bits)
317 c = ATM_NOT_RSV_VCI;
318 }
319 if ((c == ATM_NOT_RSV_VCI || *vci != ATM_VCI_ANY) &&
320 *vpi == ATM_VPI_ANY) {
321 p++;
Joe Perchesa8147d72010-01-26 11:40:06 +0000322 if (p >= 1 << vcc->dev->ci_range.vpi_bits)
323 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000325 } while (old_p != p || old_c != c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return -EADDRINUSE;
327}
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329static int __vcc_connect(struct atm_vcc *vcc, struct atm_dev *dev, short vpi,
330 int vci)
331{
332 struct sock *sk = sk_atm(vcc);
333 int error;
334
335 if ((vpi != ATM_VPI_UNSPEC && vpi != ATM_VPI_ANY &&
336 vpi >> dev->ci_range.vpi_bits) || (vci != ATM_VCI_UNSPEC &&
337 vci != ATM_VCI_ANY && vci >> dev->ci_range.vci_bits))
338 return -EINVAL;
339 if (vci > 0 && vci < ATM_NOT_RSV_VCI && !capable(CAP_NET_BIND_SERVICE))
340 return -EPERM;
Stanislaw Gruszka64bf69d2005-11-29 16:16:41 -0800341 error = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (!try_module_get(dev->ops->owner))
Stanislaw Gruszka64bf69d2005-11-29 16:16:41 -0800343 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 vcc->dev = dev;
345 write_lock_irq(&vcc_sklist_lock);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900346 if (test_bit(ATM_DF_REMOVED, &dev->flags) ||
Stanislaw Gruszka64bf69d2005-11-29 16:16:41 -0800347 (error = find_ci(vcc, &vpi, &vci))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 write_unlock_irq(&vcc_sklist_lock);
349 goto fail_module_put;
350 }
351 vcc->vpi = vpi;
352 vcc->vci = vci;
353 __vcc_insert_socket(sk);
354 write_unlock_irq(&vcc_sklist_lock);
355 switch (vcc->qos.aal) {
Joe Perchesa8147d72010-01-26 11:40:06 +0000356 case ATM_AAL0:
357 error = atm_init_aal0(vcc);
358 vcc->stats = &dev->stats.aal0;
359 break;
360 case ATM_AAL34:
361 error = atm_init_aal34(vcc);
362 vcc->stats = &dev->stats.aal34;
363 break;
364 case ATM_NO_AAL:
365 /* ATM_AAL5 is also used in the "0 for default" case */
366 vcc->qos.aal = ATM_AAL5;
367 /* fall through */
368 case ATM_AAL5:
369 error = atm_init_aal5(vcc);
370 vcc->stats = &dev->stats.aal5;
371 break;
372 default:
373 error = -EPROTOTYPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000375 if (!error)
376 error = adjust_tp(&vcc->qos.txtp, vcc->qos.aal);
377 if (!error)
378 error = adjust_tp(&vcc->qos.rxtp, vcc->qos.aal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (error)
380 goto fail;
Joe Perches99824462010-01-26 11:40:00 +0000381 pr_debug("VCC %d.%d, AAL %d\n", vpi, vci, vcc->qos.aal);
382 pr_debug(" TX: %d, PCR %d..%d, SDU %d\n",
383 vcc->qos.txtp.traffic_class,
384 vcc->qos.txtp.min_pcr,
385 vcc->qos.txtp.max_pcr,
386 vcc->qos.txtp.max_sdu);
387 pr_debug(" RX: %d, PCR %d..%d, SDU %d\n",
388 vcc->qos.rxtp.traffic_class,
389 vcc->qos.rxtp.min_pcr,
390 vcc->qos.rxtp.max_pcr,
391 vcc->qos.rxtp.max_sdu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 if (dev->ops->open) {
Joe Perchesa8147d72010-01-26 11:40:06 +0000394 error = dev->ops->open(vcc);
395 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 goto fail;
397 }
398 return 0;
399
400fail:
401 vcc_remove_socket(sk);
402fail_module_put:
403 module_put(dev->ops->owner);
404 /* ensure we get dev module ref count correct */
405 vcc->dev = NULL;
406 return error;
407}
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409int vcc_connect(struct socket *sock, int itf, short vpi, int vci)
410{
411 struct atm_dev *dev;
412 struct atm_vcc *vcc = ATM_SD(sock);
413 int error;
414
Joe Perches99824462010-01-26 11:40:00 +0000415 pr_debug("(vpi %d, vci %d)\n", vpi, vci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (sock->state == SS_CONNECTED)
417 return -EISCONN;
418 if (sock->state != SS_UNCONNECTED)
419 return -EINVAL;
420 if (!(vpi || vci))
421 return -EINVAL;
422
423 if (vpi != ATM_VPI_UNSPEC && vci != ATM_VCI_UNSPEC)
Joe Perchesa8147d72010-01-26 11:40:06 +0000424 clear_bit(ATM_VF_PARTIAL, &vcc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 else
Joe Perchesa8147d72010-01-26 11:40:06 +0000426 if (test_bit(ATM_VF_PARTIAL, &vcc->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return -EINVAL;
Joe Perches99824462010-01-26 11:40:00 +0000428 pr_debug("(TX: cl %d,bw %d-%d,sdu %d; "
429 "RX: cl %d,bw %d-%d,sdu %d,AAL %s%d)\n",
430 vcc->qos.txtp.traffic_class, vcc->qos.txtp.min_pcr,
431 vcc->qos.txtp.max_pcr, vcc->qos.txtp.max_sdu,
432 vcc->qos.rxtp.traffic_class, vcc->qos.rxtp.min_pcr,
Joe Perchesa8147d72010-01-26 11:40:06 +0000433 vcc->qos.rxtp.max_pcr, vcc->qos.rxtp.max_sdu,
Joe Perches99824462010-01-26 11:40:00 +0000434 vcc->qos.aal == ATM_AAL5 ? "" :
435 vcc->qos.aal == ATM_AAL0 ? "" : " ??? code ",
436 vcc->qos.aal == ATM_AAL0 ? 0 : vcc->qos.aal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
438 return -EBADFD;
439 if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
440 vcc->qos.rxtp.traffic_class == ATM_ANYCLASS)
441 return -EINVAL;
Mitchell Blank Jrc9933d02005-11-29 16:13:32 -0800442 if (likely(itf != ATM_ITF_ANY)) {
Joe Perchesa8147d72010-01-26 11:40:06 +0000443 dev = try_then_request_module(atm_dev_lookup(itf),
444 "atm-device-%d", itf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 dev = NULL;
Ingo Molnar57b47a52006-03-20 22:35:41 -0800447 mutex_lock(&atm_dev_mutex);
Mitchell Blank Jrc9933d02005-11-29 16:13:32 -0800448 if (!list_empty(&atm_devs)) {
Joe Perchesa8147d72010-01-26 11:40:06 +0000449 dev = list_entry(atm_devs.next,
450 struct atm_dev, dev_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 atm_dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
Ingo Molnar57b47a52006-03-20 22:35:41 -0800453 mutex_unlock(&atm_dev_mutex);
Mitchell Blank Jrc9933d02005-11-29 16:13:32 -0800454 }
455 if (!dev)
456 return -ENODEV;
457 error = __vcc_connect(vcc, dev, vpi, vci);
458 if (error) {
459 atm_dev_put(dev);
460 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
462 if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC)
Joe Perchesa8147d72010-01-26 11:40:06 +0000463 set_bit(ATM_VF_PARTIAL, &vcc->flags);
464 if (test_bit(ATM_VF_READY, &ATM_SD(sock)->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 sock->state = SS_CONNECTED;
466 return 0;
467}
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469int vcc_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
470 size_t size, int flags)
471{
472 struct sock *sk = sock->sk;
473 struct atm_vcc *vcc;
474 struct sk_buff *skb;
475 int copied, error = -EINVAL;
476
477 if (sock->state != SS_CONNECTED)
478 return -ENOTCONN;
479 if (flags & ~MSG_DONTWAIT) /* only handle MSG_DONTWAIT */
480 return -EOPNOTSUPP;
481 vcc = ATM_SD(sock);
Joe Perchesa8147d72010-01-26 11:40:06 +0000482 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
483 test_bit(ATM_VF_CLOSE, &vcc->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 !test_bit(ATM_VF_READY, &vcc->flags))
485 return 0;
486
487 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &error);
488 if (!skb)
489 return error;
490
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900491 copied = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (copied > size) {
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900493 copied = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 msg->msg_flags |= MSG_TRUNC;
495 }
496
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900497 error = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
498 if (error)
499 return error;
Neil Horman3b885782009-10-12 13:26:31 -0700500 sock_recv_ts_and_drops(msg, sk, skb);
Joe Perches99824462010-01-26 11:40:00 +0000501 pr_debug("%d -= %d\n", atomic_read(&sk->sk_rmem_alloc), skb->truesize);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900502 atm_return(vcc, skb->truesize);
503 skb_free_datagram(sk, skb);
504 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507int vcc_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
508 size_t total_len)
509{
510 struct sock *sk = sock->sk;
511 DEFINE_WAIT(wait);
512 struct atm_vcc *vcc;
513 struct sk_buff *skb;
Joe Perchesa8147d72010-01-26 11:40:06 +0000514 int eff, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 const void __user *buff;
516 int size;
517
518 lock_sock(sk);
519 if (sock->state != SS_CONNECTED) {
520 error = -ENOTCONN;
521 goto out;
522 }
523 if (m->msg_name) {
524 error = -EISCONN;
525 goto out;
526 }
527 if (m->msg_iovlen != 1) {
528 error = -ENOSYS; /* fix this later @@@ */
529 goto out;
530 }
531 buff = m->msg_iov->iov_base;
532 size = m->msg_iov->iov_len;
533 vcc = ATM_SD(sock);
534 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
535 test_bit(ATM_VF_CLOSE, &vcc->flags) ||
536 !test_bit(ATM_VF_READY, &vcc->flags)) {
537 error = -EPIPE;
538 send_sig(SIGPIPE, current, 0);
539 goto out;
540 }
541 if (!size) {
542 error = 0;
543 goto out;
544 }
545 if (size < 0 || size > vcc->qos.txtp.max_sdu) {
546 error = -EMSGSIZE;
547 goto out;
548 }
Jesper Juhle49332b2005-05-01 08:59:08 -0700549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 eff = (size+3) & ~3; /* align to word boundary */
551 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
552 error = 0;
Joe Perchesa8147d72010-01-26 11:40:06 +0000553 while (!(skb = alloc_tx(vcc, eff))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (m->msg_flags & MSG_DONTWAIT) {
555 error = -EAGAIN;
556 break;
557 }
558 schedule();
559 if (signal_pending(current)) {
560 error = -ERESTARTSYS;
561 break;
562 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000563 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
564 test_bit(ATM_VF_CLOSE, &vcc->flags) ||
565 !test_bit(ATM_VF_READY, &vcc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 error = -EPIPE;
567 send_sig(SIGPIPE, current, 0);
568 break;
569 }
570 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
571 }
572 finish_wait(sk->sk_sleep, &wait);
573 if (error)
574 goto out;
575 skb->dev = NULL; /* for paths shared with net_device interfaces */
576 ATM_SKB(skb)->atm_options = vcc->atm_options;
Joe Perchesa8147d72010-01-26 11:40:06 +0000577 if (copy_from_user(skb_put(skb, size), buff, size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 kfree_skb(skb);
579 error = -EFAULT;
580 goto out;
581 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000582 if (eff != size)
583 memset(skb->data + size, 0, eff-size);
584 error = vcc->dev->ops->send(vcc, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 error = error ? error : size;
586out:
587 release_sock(sk);
588 return error;
589}
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591unsigned int vcc_poll(struct file *file, struct socket *sock, poll_table *wait)
592{
593 struct sock *sk = sock->sk;
594 struct atm_vcc *vcc;
595 unsigned int mask;
596
Jiri Olsaa57de0b2009-07-08 12:09:13 +0000597 sock_poll_wait(file, sk->sk_sleep, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 mask = 0;
599
600 vcc = ATM_SD(sock);
601
602 /* exceptional events */
603 if (sk->sk_err)
604 mask = POLLERR;
605
606 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
607 test_bit(ATM_VF_CLOSE, &vcc->flags))
608 mask |= POLLHUP;
609
610 /* readable? */
611 if (!skb_queue_empty(&sk->sk_receive_queue))
612 mask |= POLLIN | POLLRDNORM;
613
614 /* writable? */
615 if (sock->state == SS_CONNECTING &&
616 test_bit(ATM_VF_WAITING, &vcc->flags))
617 return mask;
618
619 if (vcc->qos.txtp.traffic_class != ATM_NONE &&
620 vcc_writable(sk))
621 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
622
623 return mask;
624}
625
Joe Perchesa8147d72010-01-26 11:40:06 +0000626static int atm_change_qos(struct atm_vcc *vcc, struct atm_qos *qos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
628 int error;
629
630 /*
631 * Don't let the QoS change the already connected AAL type nor the
632 * traffic class.
633 */
634 if (qos->aal != vcc->qos.aal ||
635 qos->rxtp.traffic_class != vcc->qos.rxtp.traffic_class ||
636 qos->txtp.traffic_class != vcc->qos.txtp.traffic_class)
637 return -EINVAL;
Joe Perchesa8147d72010-01-26 11:40:06 +0000638 error = adjust_tp(&qos->txtp, qos->aal);
639 if (!error)
640 error = adjust_tp(&qos->rxtp, qos->aal);
641 if (error)
642 return error;
643 if (!vcc->dev->ops->change_qos)
644 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (sk_atm(vcc)->sk_family == AF_ATMPVC)
Joe Perchesa8147d72010-01-26 11:40:06 +0000646 return vcc->dev->ops->change_qos(vcc, qos, ATM_MF_SET);
647 return svc_change_qos(vcc, qos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
Mitchell Blank Jr61c33e02008-06-17 16:20:06 -0700650static int check_tp(const struct atm_trafprm *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 /* @@@ Should be merged with adjust_tp */
Joe Perchesa8147d72010-01-26 11:40:06 +0000653 if (!tp->traffic_class || tp->traffic_class == ATM_ANYCLASS)
654 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (tp->traffic_class != ATM_UBR && !tp->min_pcr && !tp->pcr &&
Joe Perchesa8147d72010-01-26 11:40:06 +0000656 !tp->max_pcr)
657 return -EINVAL;
658 if (tp->min_pcr == ATM_MAX_PCR)
659 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (tp->min_pcr && tp->max_pcr && tp->max_pcr != ATM_MAX_PCR &&
Joe Perchesa8147d72010-01-26 11:40:06 +0000661 tp->min_pcr > tp->max_pcr)
662 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /*
664 * We allow pcr to be outside [min_pcr,max_pcr], because later
665 * adjustment may still push it in the valid range.
666 */
667 return 0;
668}
669
Mitchell Blank Jr61c33e02008-06-17 16:20:06 -0700670static int check_qos(const struct atm_qos *qos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 int error;
673
674 if (!qos->txtp.traffic_class && !qos->rxtp.traffic_class)
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900675 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (qos->txtp.traffic_class != qos->rxtp.traffic_class &&
677 qos->txtp.traffic_class && qos->rxtp.traffic_class &&
678 qos->txtp.traffic_class != ATM_ANYCLASS &&
Joe Perchesa8147d72010-01-26 11:40:06 +0000679 qos->rxtp.traffic_class != ATM_ANYCLASS)
680 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 error = check_tp(&qos->txtp);
Joe Perchesa8147d72010-01-26 11:40:06 +0000682 if (error)
683 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return check_tp(&qos->rxtp);
685}
686
687int vcc_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700688 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 struct atm_vcc *vcc;
691 unsigned long value;
692 int error;
693
694 if (__SO_LEVEL_MATCH(optname, level) && optlen != __SO_SIZE(optname))
695 return -EINVAL;
696
697 vcc = ATM_SD(sock);
698 switch (optname) {
Joe Perchesa8147d72010-01-26 11:40:06 +0000699 case SO_ATMQOS:
700 {
701 struct atm_qos qos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Joe Perchesa8147d72010-01-26 11:40:06 +0000703 if (copy_from_user(&qos, optval, sizeof(qos)))
704 return -EFAULT;
705 error = check_qos(&qos);
706 if (error)
707 return error;
708 if (sock->state == SS_CONNECTED)
709 return atm_change_qos(vcc, &qos);
710 if (sock->state != SS_UNCONNECTED)
711 return -EBADFD;
712 vcc->qos = qos;
713 set_bit(ATM_VF_HASQOS, &vcc->flags);
714 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000716 case SO_SETCLP:
717 if (get_user(value, (unsigned long __user *)optval))
718 return -EFAULT;
719 if (value)
720 vcc->atm_options |= ATM_ATMOPT_CLP;
721 else
722 vcc->atm_options &= ~ATM_ATMOPT_CLP;
723 return 0;
724 default:
725 if (level == SOL_SOCKET)
726 return -EINVAL;
727 break;
728 }
729 if (!vcc->dev || !vcc->dev->ops->setsockopt)
730 return -EINVAL;
731 return vcc->dev->ops->setsockopt(vcc, level, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734int vcc_getsockopt(struct socket *sock, int level, int optname,
735 char __user *optval, int __user *optlen)
736{
737 struct atm_vcc *vcc;
738 int len;
739
740 if (get_user(len, optlen))
741 return -EFAULT;
742 if (__SO_LEVEL_MATCH(optname, level) && len != __SO_SIZE(optname))
743 return -EINVAL;
744
745 vcc = ATM_SD(sock);
746 switch (optname) {
Joe Perchesa8147d72010-01-26 11:40:06 +0000747 case SO_ATMQOS:
748 if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
749 return -EINVAL;
750 return copy_to_user(optval, &vcc->qos, sizeof(vcc->qos))
751 ? -EFAULT : 0;
752 case SO_SETCLP:
753 return put_user(vcc->atm_options & ATM_ATMOPT_CLP ? 1 : 0,
754 (unsigned long __user *)optval) ? -EFAULT : 0;
755 case SO_ATMPVC:
756 {
757 struct sockaddr_atmpvc pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Joe Perchesa8147d72010-01-26 11:40:06 +0000759 if (!vcc->dev || !test_bit(ATM_VF_ADDR, &vcc->flags))
760 return -ENOTCONN;
761 pvc.sap_family = AF_ATMPVC;
762 pvc.sap_addr.itf = vcc->dev->number;
763 pvc.sap_addr.vpi = vcc->vpi;
764 pvc.sap_addr.vci = vcc->vci;
765 return copy_to_user(optval, &pvc, sizeof(pvc)) ? -EFAULT : 0;
766 }
767 default:
768 if (level == SOL_SOCKET)
769 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 break;
771 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000772 if (!vcc->dev || !vcc->dev->ops->getsockopt)
773 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return vcc->dev->ops->getsockopt(vcc, level, optname, optval, len);
775}
776
777static int __init atm_init(void)
778{
779 int error;
780
Joe Perchesa8147d72010-01-26 11:40:06 +0000781 error = proto_register(&vcc_proto, 0);
782 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 goto out;
Joe Perchesa8147d72010-01-26 11:40:06 +0000784 error = atmpvc_init();
785 if (error < 0) {
Joe Perches99824462010-01-26 11:40:00 +0000786 pr_err("atmpvc_init() failed with %d\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 goto out_unregister_vcc_proto;
788 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000789 error = atmsvc_init();
790 if (error < 0) {
Joe Perches99824462010-01-26 11:40:00 +0000791 pr_err("atmsvc_init() failed with %d\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 goto out_atmpvc_exit;
793 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000794 error = atm_proc_init();
795 if (error < 0) {
Joe Perches99824462010-01-26 11:40:00 +0000796 pr_err("atm_proc_init() failed with %d\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 goto out_atmsvc_exit;
798 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000799 error = atm_sysfs_init();
800 if (error < 0) {
Joe Perches99824462010-01-26 11:40:00 +0000801 pr_err("atm_sysfs_init() failed with %d\n", error);
Roman Kagan656d98b2006-06-29 12:36:34 -0700802 goto out_atmproc_exit;
803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804out:
805 return error;
Roman Kagan656d98b2006-06-29 12:36:34 -0700806out_atmproc_exit:
807 atm_proc_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808out_atmsvc_exit:
809 atmsvc_exit();
810out_atmpvc_exit:
811 atmsvc_exit();
812out_unregister_vcc_proto:
813 proto_unregister(&vcc_proto);
814 goto out;
815}
816
817static void __exit atm_exit(void)
818{
819 atm_proc_exit();
Roman Kagan656d98b2006-06-29 12:36:34 -0700820 atm_sysfs_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 atmsvc_exit();
822 atmpvc_exit();
823 proto_unregister(&vcc_proto);
824}
825
Daniel Walker84ff6022007-02-05 18:04:06 -0800826subsys_initcall(atm_init);
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828module_exit(atm_exit);
829
830MODULE_LICENSE("GPL");
831MODULE_ALIAS_NETPROTO(PF_ATMPVC);
832MODULE_ALIAS_NETPROTO(PF_ATMSVC);