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