blob: 0e3f8f9dcd294f5a79db29b6752808959f3e6719 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* drivers/atm/atmtcp.c - ATM over TCP "device" driver */
2
3/* Written 1997-2000 by Werner Almesberger, EPFL LRC/ICA */
4
5
6#include <linux/module.h>
7#include <linux/wait.h>
8#include <linux/atmdev.h>
9#include <linux/atm_tcp.h>
10#include <linux/bitops.h>
11#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/uaccess.h>
Arun Sharma600634972011-07-26 16:09:06 -070014#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16
17extern int atm_init_aal5(struct atm_vcc *vcc); /* "raw" AAL5 transport */
18
19
20#define PRIV(dev) ((struct atmtcp_dev_data *) ((dev)->dev_data))
21
22
23struct atmtcp_dev_data {
24 struct atm_vcc *vcc; /* control VCC; NULL if detached */
25 int persist; /* non-zero if persistent */
26};
27
28
29#define DEV_LABEL "atmtcp"
30
31#define MAX_VPI_BITS 8 /* simplifies life */
32#define MAX_VCI_BITS 16
33
34
35/*
36 * Hairy code ahead: the control VCC may be closed while we're still
37 * waiting for an answer, so we need to re-validate out_vcc every once
38 * in a while.
39 */
40
41
42static int atmtcp_send_control(struct atm_vcc *vcc,int type,
43 const struct atmtcp_control *msg,int flag)
44{
45 DECLARE_WAITQUEUE(wait,current);
46 struct atm_vcc *out_vcc;
47 struct sk_buff *skb;
48 struct atmtcp_control *new_msg;
49 int old_test;
50 int error = 0;
51
52 out_vcc = PRIV(vcc->dev) ? PRIV(vcc->dev)->vcc : NULL;
53 if (!out_vcc) return -EUNATCH;
54 skb = alloc_skb(sizeof(*msg),GFP_KERNEL);
55 if (!skb) return -ENOMEM;
56 mb();
57 out_vcc = PRIV(vcc->dev) ? PRIV(vcc->dev)->vcc : NULL;
58 if (!out_vcc) {
59 dev_kfree_skb(skb);
60 return -EUNATCH;
61 }
62 atm_force_charge(out_vcc,skb->truesize);
63 new_msg = (struct atmtcp_control *) skb_put(skb,sizeof(*new_msg));
64 *new_msg = *msg;
65 new_msg->hdr.length = ATMTCP_HDR_MAGIC;
66 new_msg->type = type;
67 memset(&new_msg->vcc,0,sizeof(atm_kptr_t));
68 *(struct atm_vcc **) &new_msg->vcc = vcc;
69 old_test = test_bit(flag,&vcc->flags);
70 out_vcc->push(out_vcc,skb);
Eric Dumazetaa395142010-04-20 13:03:51 +000071 add_wait_queue(sk_sleep(sk_atm(vcc)), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 while (test_bit(flag,&vcc->flags) == old_test) {
73 mb();
74 out_vcc = PRIV(vcc->dev) ? PRIV(vcc->dev)->vcc : NULL;
75 if (!out_vcc) {
76 error = -EUNATCH;
77 break;
78 }
79 set_current_state(TASK_UNINTERRUPTIBLE);
80 schedule();
81 }
82 set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +000083 remove_wait_queue(sk_sleep(sk_atm(vcc)), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return error;
85}
86
87
88static int atmtcp_recv_control(const struct atmtcp_control *msg)
89{
90 struct atm_vcc *vcc = *(struct atm_vcc **) &msg->vcc;
91
92 vcc->vpi = msg->addr.sap_addr.vpi;
93 vcc->vci = msg->addr.sap_addr.vci;
94 vcc->qos = msg->qos;
95 sk_atm(vcc)->sk_err = -msg->result;
96 switch (msg->type) {
97 case ATMTCP_CTRL_OPEN:
98 change_bit(ATM_VF_READY,&vcc->flags);
99 break;
100 case ATMTCP_CTRL_CLOSE:
101 change_bit(ATM_VF_ADDR,&vcc->flags);
102 break;
103 default:
104 printk(KERN_ERR "atmtcp_recv_control: unknown type %d\n",
105 msg->type);
106 return -EINVAL;
107 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000108 wake_up(sk_sleep(sk_atm(vcc)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return 0;
110}
111
112
113static void atmtcp_v_dev_close(struct atm_dev *dev)
114{
115 /* Nothing.... Isn't this simple :-) -- REW */
116}
117
118
119static int atmtcp_v_open(struct atm_vcc *vcc)
120{
121 struct atmtcp_control msg;
122 int error;
123 short vpi = vcc->vpi;
124 int vci = vcc->vci;
125
126 memset(&msg,0,sizeof(msg));
127 msg.addr.sap_family = AF_ATMPVC;
128 msg.hdr.vpi = htons(vpi);
129 msg.addr.sap_addr.vpi = vpi;
130 msg.hdr.vci = htons(vci);
131 msg.addr.sap_addr.vci = vci;
132 if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC) return 0;
133 msg.type = ATMTCP_CTRL_OPEN;
134 msg.qos = vcc->qos;
135 set_bit(ATM_VF_ADDR,&vcc->flags);
136 clear_bit(ATM_VF_READY,&vcc->flags); /* just in case ... */
137 error = atmtcp_send_control(vcc,ATMTCP_CTRL_OPEN,&msg,ATM_VF_READY);
138 if (error) return error;
139 return -sk_atm(vcc)->sk_err;
140}
141
142
143static void atmtcp_v_close(struct atm_vcc *vcc)
144{
145 struct atmtcp_control msg;
146
147 memset(&msg,0,sizeof(msg));
148 msg.addr.sap_family = AF_ATMPVC;
149 msg.addr.sap_addr.vpi = vcc->vpi;
150 msg.addr.sap_addr.vci = vcc->vci;
151 clear_bit(ATM_VF_READY,&vcc->flags);
152 (void) atmtcp_send_control(vcc,ATMTCP_CTRL_CLOSE,&msg,ATM_VF_ADDR);
153}
154
155
156static int atmtcp_v_ioctl(struct atm_dev *dev,unsigned int cmd,void __user *arg)
157{
158 struct atm_cirange ci;
159 struct atm_vcc *vcc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 struct sock *s;
161 int i;
162
163 if (cmd != ATM_SETCIRANGE) return -ENOIOCTLCMD;
164 if (copy_from_user(&ci, arg,sizeof(ci))) return -EFAULT;
165 if (ci.vpi_bits == ATM_CI_MAX) ci.vpi_bits = MAX_VPI_BITS;
166 if (ci.vci_bits == ATM_CI_MAX) ci.vci_bits = MAX_VCI_BITS;
167 if (ci.vpi_bits > MAX_VPI_BITS || ci.vpi_bits < 0 ||
168 ci.vci_bits > MAX_VCI_BITS || ci.vci_bits < 0) return -EINVAL;
169 read_lock(&vcc_sklist_lock);
170 for(i = 0; i < VCC_HTABLE_SIZE; ++i) {
171 struct hlist_head *head = &vcc_hash[i];
172
Sasha Levinb67bfe02013-02-27 17:06:00 -0800173 sk_for_each(s, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 vcc = atm_sk(s);
175 if (vcc->dev != dev)
176 continue;
177 if ((vcc->vpi >> ci.vpi_bits) ||
178 (vcc->vci >> ci.vci_bits)) {
179 read_unlock(&vcc_sklist_lock);
180 return -EBUSY;
181 }
182 }
183 }
184 read_unlock(&vcc_sklist_lock);
185 dev->ci_range = ci;
186 return 0;
187}
188
189
190static int atmtcp_v_send(struct atm_vcc *vcc,struct sk_buff *skb)
191{
192 struct atmtcp_dev_data *dev_data;
193 struct atm_vcc *out_vcc=NULL; /* Initializer quietens GCC warning */
194 struct sk_buff *new_skb;
195 struct atmtcp_hdr *hdr;
196 int size;
197
198 if (vcc->qos.txtp.traffic_class == ATM_NONE) {
199 if (vcc->pop) vcc->pop(vcc,skb);
200 else dev_kfree_skb(skb);
201 return -EINVAL;
202 }
203 dev_data = PRIV(vcc->dev);
204 if (dev_data) out_vcc = dev_data->vcc;
205 if (!dev_data || !out_vcc) {
206 if (vcc->pop) vcc->pop(vcc,skb);
207 else dev_kfree_skb(skb);
208 if (dev_data) return 0;
209 atomic_inc(&vcc->stats->tx_err);
210 return -ENOLINK;
211 }
212 size = skb->len+sizeof(struct atmtcp_hdr);
213 new_skb = atm_alloc_charge(out_vcc,size,GFP_ATOMIC);
214 if (!new_skb) {
215 if (vcc->pop) vcc->pop(vcc,skb);
216 else dev_kfree_skb(skb);
217 atomic_inc(&vcc->stats->tx_err);
218 return -ENOBUFS;
219 }
220 hdr = (void *) skb_put(new_skb,sizeof(struct atmtcp_hdr));
221 hdr->vpi = htons(vcc->vpi);
222 hdr->vci = htons(vcc->vci);
223 hdr->length = htonl(skb->len);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300224 skb_copy_from_linear_data(skb, skb_put(new_skb, skb->len), skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (vcc->pop) vcc->pop(vcc,skb);
226 else dev_kfree_skb(skb);
227 out_vcc->push(out_vcc,new_skb);
228 atomic_inc(&vcc->stats->tx);
229 atomic_inc(&out_vcc->stats->rx);
230 return 0;
231}
232
233
234static int atmtcp_v_proc(struct atm_dev *dev,loff_t *pos,char *page)
235{
236 struct atmtcp_dev_data *dev_data = PRIV(dev);
237
238 if (*pos) return 0;
239 if (!dev_data->persist) return sprintf(page,"ephemeral\n");
240 return sprintf(page,"persistent, %sconnected\n",
241 dev_data->vcc ? "" : "dis");
242}
243
244
245static void atmtcp_c_close(struct atm_vcc *vcc)
246{
247 struct atm_dev *atmtcp_dev;
248 struct atmtcp_dev_data *dev_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 atmtcp_dev = (struct atm_dev *) vcc->dev_data;
251 dev_data = PRIV(atmtcp_dev);
252 dev_data->vcc = NULL;
253 if (dev_data->persist) return;
254 atmtcp_dev->dev_data = NULL;
255 kfree(dev_data);
Stanislaw Gruszka64bf69d2005-11-29 16:16:41 -0800256 atm_dev_deregister(atmtcp_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 vcc->dev_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 module_put(THIS_MODULE);
259}
260
261
262static struct atm_vcc *find_vcc(struct atm_dev *dev, short vpi, int vci)
263{
264 struct hlist_head *head;
265 struct atm_vcc *vcc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 struct sock *s;
267
268 head = &vcc_hash[vci & (VCC_HTABLE_SIZE -1)];
269
Sasha Levinb67bfe02013-02-27 17:06:00 -0800270 sk_for_each(s, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 vcc = atm_sk(s);
272 if (vcc->dev == dev &&
273 vcc->vci == vci && vcc->vpi == vpi &&
274 vcc->qos.rxtp.traffic_class != ATM_NONE) {
275 return vcc;
276 }
277 }
278 return NULL;
279}
280
281
282static int atmtcp_c_send(struct atm_vcc *vcc,struct sk_buff *skb)
283{
284 struct atm_dev *dev;
285 struct atmtcp_hdr *hdr;
286 struct atm_vcc *out_vcc;
287 struct sk_buff *new_skb;
288 int result = 0;
289
290 if (!skb->len) return 0;
291 dev = vcc->dev_data;
292 hdr = (struct atmtcp_hdr *) skb->data;
293 if (hdr->length == ATMTCP_HDR_MAGIC) {
294 result = atmtcp_recv_control(
295 (struct atmtcp_control *) skb->data);
296 goto done;
297 }
298 read_lock(&vcc_sklist_lock);
299 out_vcc = find_vcc(dev, ntohs(hdr->vpi), ntohs(hdr->vci));
300 read_unlock(&vcc_sklist_lock);
301 if (!out_vcc) {
302 atomic_inc(&vcc->stats->tx_err);
303 goto done;
304 }
305 skb_pull(skb,sizeof(struct atmtcp_hdr));
306 new_skb = atm_alloc_charge(out_vcc,skb->len,GFP_KERNEL);
307 if (!new_skb) {
308 result = -ENOBUFS;
309 goto done;
310 }
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700311 __net_timestamp(new_skb);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300312 skb_copy_from_linear_data(skb, skb_put(new_skb, skb->len), skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 out_vcc->push(out_vcc,new_skb);
314 atomic_inc(&vcc->stats->tx);
315 atomic_inc(&out_vcc->stats->rx);
316done:
317 if (vcc->pop) vcc->pop(vcc,skb);
318 else dev_kfree_skb(skb);
319 return result;
320}
321
322
323/*
324 * Device operations for the virtual ATM devices created by ATMTCP.
325 */
326
327
328static struct atmdev_ops atmtcp_v_dev_ops = {
329 .dev_close = atmtcp_v_dev_close,
330 .open = atmtcp_v_open,
331 .close = atmtcp_v_close,
332 .ioctl = atmtcp_v_ioctl,
333 .send = atmtcp_v_send,
334 .proc_read = atmtcp_v_proc,
335 .owner = THIS_MODULE
336};
337
338
339/*
340 * Device operations for the ATMTCP control device.
341 */
342
343
344static struct atmdev_ops atmtcp_c_dev_ops = {
345 .close = atmtcp_c_close,
346 .send = atmtcp_c_send
347};
348
349
350static struct atm_dev atmtcp_control_dev = {
351 .ops = &atmtcp_c_dev_ops,
352 .type = "atmtcp",
353 .number = 999,
Milind Arun Choudhary4ef8d0a2007-04-26 01:37:44 -0700354 .lock = __SPIN_LOCK_UNLOCKED(atmtcp_control_dev.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355};
356
357
358static int atmtcp_create(int itf,int persist,struct atm_dev **result)
359{
360 struct atmtcp_dev_data *dev_data;
361 struct atm_dev *dev;
362
363 dev_data = kmalloc(sizeof(*dev_data),GFP_KERNEL);
364 if (!dev_data)
365 return -ENOMEM;
366
Dan Williamsd9ca6762010-12-08 19:40:47 +0000367 dev = atm_dev_register(DEV_LABEL,NULL,&atmtcp_v_dev_ops,itf,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (!dev) {
369 kfree(dev_data);
370 return itf == -1 ? -ENOMEM : -EBUSY;
371 }
372 dev->ci_range.vpi_bits = MAX_VPI_BITS;
373 dev->ci_range.vci_bits = MAX_VCI_BITS;
374 dev->dev_data = dev_data;
375 PRIV(dev)->vcc = NULL;
376 PRIV(dev)->persist = persist;
377 if (result) *result = dev;
378 return 0;
379}
380
381
382static int atmtcp_attach(struct atm_vcc *vcc,int itf)
383{
384 struct atm_dev *dev;
385
386 dev = NULL;
387 if (itf != -1) dev = atm_dev_lookup(itf);
388 if (dev) {
389 if (dev->ops != &atmtcp_v_dev_ops) {
390 atm_dev_put(dev);
391 return -EMEDIUMTYPE;
392 }
Julia Lawallb9556f92010-12-29 04:01:03 +0000393 if (PRIV(dev)->vcc) {
394 atm_dev_put(dev);
395 return -EBUSY;
396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398 else {
399 int error;
400
401 error = atmtcp_create(itf,0,&dev);
402 if (error) return error;
403 }
404 PRIV(dev)->vcc = vcc;
405 vcc->dev = &atmtcp_control_dev;
406 vcc_insert_socket(sk_atm(vcc));
407 set_bit(ATM_VF_META,&vcc->flags);
408 set_bit(ATM_VF_READY,&vcc->flags);
409 vcc->dev_data = dev;
410 (void) atm_init_aal5(vcc); /* @@@ losing AAL in transit ... */
411 vcc->stats = &atmtcp_control_dev.stats.aal5;
412 return dev->number;
413}
414
415
416static int atmtcp_create_persistent(int itf)
417{
418 return atmtcp_create(itf,1,NULL);
419}
420
421
422static int atmtcp_remove_persistent(int itf)
423{
424 struct atm_dev *dev;
425 struct atmtcp_dev_data *dev_data;
426
427 dev = atm_dev_lookup(itf);
428 if (!dev) return -ENODEV;
429 if (dev->ops != &atmtcp_v_dev_ops) {
430 atm_dev_put(dev);
431 return -EMEDIUMTYPE;
432 }
433 dev_data = PRIV(dev);
434 if (!dev_data->persist) return 0;
435 dev_data->persist = 0;
436 if (PRIV(dev)->vcc) return 0;
437 kfree(dev_data);
438 atm_dev_put(dev);
Stanislaw Gruszka64bf69d2005-11-29 16:16:41 -0800439 atm_dev_deregister(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return 0;
441}
442
443static int atmtcp_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
444{
445 int err = 0;
446 struct atm_vcc *vcc = ATM_SD(sock);
447
448 if (cmd != SIOCSIFATMTCP && cmd != ATMTCP_CREATE && cmd != ATMTCP_REMOVE)
449 return -ENOIOCTLCMD;
450
451 if (!capable(CAP_NET_ADMIN))
452 return -EPERM;
453
454 switch (cmd) {
455 case SIOCSIFATMTCP:
456 err = atmtcp_attach(vcc, (int) arg);
457 if (err >= 0) {
458 sock->state = SS_CONNECTED;
459 __module_get(THIS_MODULE);
460 }
461 break;
462 case ATMTCP_CREATE:
463 err = atmtcp_create_persistent((int) arg);
464 break;
465 case ATMTCP_REMOVE:
466 err = atmtcp_remove_persistent((int) arg);
467 break;
468 }
469 return err;
470}
471
472static struct atm_ioctl atmtcp_ioctl_ops = {
473 .owner = THIS_MODULE,
474 .ioctl = atmtcp_ioctl,
475};
476
477static __init int atmtcp_init(void)
478{
479 register_atm_ioctl(&atmtcp_ioctl_ops);
480 return 0;
481}
482
483
484static void __exit atmtcp_exit(void)
485{
486 deregister_atm_ioctl(&atmtcp_ioctl_ops);
487}
488
489MODULE_LICENSE("GPL");
490module_init(atmtcp_init);
491module_exit(atmtcp_exit);