1 /* drivers/atm/atmtcp.c - ATM over TCP "device" driver */
3 /* Written 1997-2000 by Werner Almesberger, EPFL LRC/ICA */
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>
12 #include <linux/slab.h>
13 #include <asm/uaccess.h>
14 #include <linux/atomic.h>
17 extern int atm_init_aal5(struct atm_vcc *vcc); /* "raw" AAL5 transport */
20 #define PRIV(dev) ((struct atmtcp_dev_data *) ((dev)->dev_data))
23 struct atmtcp_dev_data {
24 struct atm_vcc *vcc; /* control VCC; NULL if detached */
25 int persist; /* non-zero if persistent */
29 #define DEV_LABEL "atmtcp"
31 #define MAX_VPI_BITS 8 /* simplifies life */
32 #define MAX_VCI_BITS 16
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
42 static int atmtcp_send_control(struct atm_vcc *vcc,int type,
43 const struct atmtcp_control *msg,int flag)
45 DECLARE_WAITQUEUE(wait,current);
46 struct atm_vcc *out_vcc;
48 struct atmtcp_control *new_msg;
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;
57 out_vcc = PRIV(vcc->dev) ? PRIV(vcc->dev)->vcc : NULL;
62 atm_force_charge(out_vcc,skb->truesize);
63 new_msg = (struct atmtcp_control *) skb_put(skb,sizeof(*new_msg));
65 new_msg->hdr.length = ATMTCP_HDR_MAGIC;
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);
71 add_wait_queue(sk_sleep(sk_atm(vcc)), &wait);
72 while (test_bit(flag,&vcc->flags) == old_test) {
74 out_vcc = PRIV(vcc->dev) ? PRIV(vcc->dev)->vcc : NULL;
79 set_current_state(TASK_UNINTERRUPTIBLE);
82 set_current_state(TASK_RUNNING);
83 remove_wait_queue(sk_sleep(sk_atm(vcc)), &wait);
88 static int atmtcp_recv_control(const struct atmtcp_control *msg)
90 struct atm_vcc *vcc = *(struct atm_vcc **) &msg->vcc;
92 vcc->vpi = msg->addr.sap_addr.vpi;
93 vcc->vci = msg->addr.sap_addr.vci;
95 sk_atm(vcc)->sk_err = -msg->result;
97 case ATMTCP_CTRL_OPEN:
98 change_bit(ATM_VF_READY,&vcc->flags);
100 case ATMTCP_CTRL_CLOSE:
101 change_bit(ATM_VF_ADDR,&vcc->flags);
104 printk(KERN_ERR "atmtcp_recv_control: unknown type %d\n",
108 wake_up(sk_sleep(sk_atm(vcc)));
113 static void atmtcp_v_dev_close(struct atm_dev *dev)
115 /* Nothing.... Isn't this simple :-) -- REW */
119 static int atmtcp_v_open(struct atm_vcc *vcc)
121 struct atmtcp_control msg;
123 short vpi = vcc->vpi;
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;
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;
143 static void atmtcp_v_close(struct atm_vcc *vcc)
145 struct atmtcp_control msg;
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);
156 static int atmtcp_v_ioctl(struct atm_dev *dev,unsigned int cmd,void __user *arg)
158 struct atm_cirange ci;
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];
173 sk_for_each(s, head) {
177 if ((vcc->vpi >> ci.vpi_bits) ||
178 (vcc->vci >> ci.vci_bits)) {
179 read_unlock(&vcc_sklist_lock);
184 read_unlock(&vcc_sklist_lock);
190 static int atmtcp_v_send(struct atm_vcc *vcc,struct sk_buff *skb)
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;
198 if (vcc->qos.txtp.traffic_class == ATM_NONE) {
199 if (vcc->pop) vcc->pop(vcc,skb);
200 else dev_kfree_skb(skb);
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);
212 size = skb->len+sizeof(struct atmtcp_hdr);
213 new_skb = atm_alloc_charge(out_vcc,size,GFP_ATOMIC);
215 if (vcc->pop) vcc->pop(vcc,skb);
216 else dev_kfree_skb(skb);
217 atomic_inc(&vcc->stats->tx_err);
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);
224 skb_copy_from_linear_data(skb, skb_put(new_skb, skb->len), skb->len);
225 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);
234 static int atmtcp_v_proc(struct atm_dev *dev,loff_t *pos,char *page)
236 struct atmtcp_dev_data *dev_data = PRIV(dev);
239 if (!dev_data->persist) return sprintf(page,"ephemeral\n");
240 return sprintf(page,"persistent, %sconnected\n",
241 dev_data->vcc ? "" : "dis");
245 static void atmtcp_c_close(struct atm_vcc *vcc)
247 struct atm_dev *atmtcp_dev;
248 struct atmtcp_dev_data *dev_data;
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;
256 atm_dev_deregister(atmtcp_dev);
257 vcc->dev_data = NULL;
258 module_put(THIS_MODULE);
262 static struct atm_vcc *find_vcc(struct atm_dev *dev, short vpi, int vci)
264 struct hlist_head *head;
268 head = &vcc_hash[vci & (VCC_HTABLE_SIZE -1)];
270 sk_for_each(s, head) {
272 if (vcc->dev == dev &&
273 vcc->vci == vci && vcc->vpi == vpi &&
274 vcc->qos.rxtp.traffic_class != ATM_NONE) {
282 static int atmtcp_c_send(struct atm_vcc *vcc,struct sk_buff *skb)
285 struct atmtcp_hdr *hdr;
286 struct atm_vcc *out_vcc;
287 struct sk_buff *new_skb;
290 if (!skb->len) return 0;
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);
298 read_lock(&vcc_sklist_lock);
299 out_vcc = find_vcc(dev, ntohs(hdr->vpi), ntohs(hdr->vci));
300 read_unlock(&vcc_sklist_lock);
303 atomic_inc(&vcc->stats->tx_err);
306 skb_pull(skb,sizeof(struct atmtcp_hdr));
307 new_skb = atm_alloc_charge(out_vcc,skb->len,GFP_KERNEL);
312 __net_timestamp(new_skb);
313 skb_copy_from_linear_data(skb, skb_put(new_skb, skb->len), skb->len);
314 out_vcc->push(out_vcc,new_skb);
315 atomic_inc(&vcc->stats->tx);
316 atomic_inc(&out_vcc->stats->rx);
318 if (vcc->pop) vcc->pop(vcc,skb);
319 else dev_kfree_skb(skb);
325 * Device operations for the virtual ATM devices created by ATMTCP.
329 static struct atmdev_ops atmtcp_v_dev_ops = {
330 .dev_close = atmtcp_v_dev_close,
331 .open = atmtcp_v_open,
332 .close = atmtcp_v_close,
333 .ioctl = atmtcp_v_ioctl,
334 .send = atmtcp_v_send,
335 .proc_read = atmtcp_v_proc,
341 * Device operations for the ATMTCP control device.
345 static struct atmdev_ops atmtcp_c_dev_ops = {
346 .close = atmtcp_c_close,
347 .send = atmtcp_c_send
351 static struct atm_dev atmtcp_control_dev = {
352 .ops = &atmtcp_c_dev_ops,
355 .lock = __SPIN_LOCK_UNLOCKED(atmtcp_control_dev.lock)
359 static int atmtcp_create(int itf,int persist,struct atm_dev **result)
361 struct atmtcp_dev_data *dev_data;
364 dev_data = kmalloc(sizeof(*dev_data),GFP_KERNEL);
368 dev = atm_dev_register(DEV_LABEL,NULL,&atmtcp_v_dev_ops,itf,NULL);
371 return itf == -1 ? -ENOMEM : -EBUSY;
373 dev->ci_range.vpi_bits = MAX_VPI_BITS;
374 dev->ci_range.vci_bits = MAX_VCI_BITS;
375 dev->dev_data = dev_data;
376 PRIV(dev)->vcc = NULL;
377 PRIV(dev)->persist = persist;
378 if (result) *result = dev;
383 static int atmtcp_attach(struct atm_vcc *vcc,int itf)
388 if (itf != -1) dev = atm_dev_lookup(itf);
390 if (dev->ops != &atmtcp_v_dev_ops) {
394 if (PRIV(dev)->vcc) {
402 error = atmtcp_create(itf,0,&dev);
403 if (error) return error;
405 PRIV(dev)->vcc = vcc;
406 vcc->dev = &atmtcp_control_dev;
407 vcc_insert_socket(sk_atm(vcc));
408 set_bit(ATM_VF_META,&vcc->flags);
409 set_bit(ATM_VF_READY,&vcc->flags);
411 (void) atm_init_aal5(vcc); /* @@@ losing AAL in transit ... */
412 vcc->stats = &atmtcp_control_dev.stats.aal5;
417 static int atmtcp_create_persistent(int itf)
419 return atmtcp_create(itf,1,NULL);
423 static int atmtcp_remove_persistent(int itf)
426 struct atmtcp_dev_data *dev_data;
428 dev = atm_dev_lookup(itf);
429 if (!dev) return -ENODEV;
430 if (dev->ops != &atmtcp_v_dev_ops) {
434 dev_data = PRIV(dev);
435 if (!dev_data->persist) return 0;
436 dev_data->persist = 0;
437 if (PRIV(dev)->vcc) return 0;
440 atm_dev_deregister(dev);
444 static int atmtcp_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
447 struct atm_vcc *vcc = ATM_SD(sock);
449 if (cmd != SIOCSIFATMTCP && cmd != ATMTCP_CREATE && cmd != ATMTCP_REMOVE)
452 if (!capable(CAP_NET_ADMIN))
457 err = atmtcp_attach(vcc, (int) arg);
459 sock->state = SS_CONNECTED;
460 __module_get(THIS_MODULE);
464 err = atmtcp_create_persistent((int) arg);
467 err = atmtcp_remove_persistent((int) arg);
473 static struct atm_ioctl atmtcp_ioctl_ops = {
474 .owner = THIS_MODULE,
475 .ioctl = atmtcp_ioctl,
478 static __init int atmtcp_init(void)
480 register_atm_ioctl(&atmtcp_ioctl_ops);
485 static void __exit atmtcp_exit(void)
487 deregister_atm_ioctl(&atmtcp_ioctl_ops);
490 MODULE_LICENSE("GPL");
491 module_init(atmtcp_init);
492 module_exit(atmtcp_exit);