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 <asm/uaccess.h>
13 #include <asm/atomic.h>
16 extern int atm_init_aal5(struct atm_vcc
*vcc
); /* "raw" AAL5 transport */
19 #define PRIV(dev) ((struct atmtcp_dev_data *) ((dev)->dev_data))
22 struct atmtcp_dev_data
{
23 struct atm_vcc
*vcc
; /* control VCC; NULL if detached */
24 int persist
; /* non-zero if persistent */
28 #define DEV_LABEL "atmtcp"
30 #define MAX_VPI_BITS 8 /* simplifies life */
31 #define MAX_VCI_BITS 16
35 * Hairy code ahead: the control VCC may be closed while we're still
36 * waiting for an answer, so we need to re-validate out_vcc every once
41 static int atmtcp_send_control(struct atm_vcc
*vcc
,int type
,
42 const struct atmtcp_control
*msg
,int flag
)
44 DECLARE_WAITQUEUE(wait
,current
);
45 struct atm_vcc
*out_vcc
;
47 struct atmtcp_control
*new_msg
;
51 out_vcc
= PRIV(vcc
->dev
) ? PRIV(vcc
->dev
)->vcc
: NULL
;
52 if (!out_vcc
) return -EUNATCH
;
53 skb
= alloc_skb(sizeof(*msg
),GFP_KERNEL
);
54 if (!skb
) return -ENOMEM
;
56 out_vcc
= PRIV(vcc
->dev
) ? PRIV(vcc
->dev
)->vcc
: NULL
;
61 atm_force_charge(out_vcc
,skb
->truesize
);
62 new_msg
= (struct atmtcp_control
*) skb_put(skb
,sizeof(*new_msg
));
64 new_msg
->hdr
.length
= ATMTCP_HDR_MAGIC
;
66 memset(&new_msg
->vcc
,0,sizeof(atm_kptr_t
));
67 *(struct atm_vcc
**) &new_msg
->vcc
= vcc
;
68 old_test
= test_bit(flag
,&vcc
->flags
);
69 out_vcc
->push(out_vcc
,skb
);
70 add_wait_queue(vcc
->sk
->sk_sleep
, &wait
);
71 while (test_bit(flag
,&vcc
->flags
) == old_test
) {
73 out_vcc
= PRIV(vcc
->dev
) ? PRIV(vcc
->dev
)->vcc
: NULL
;
78 set_current_state(TASK_UNINTERRUPTIBLE
);
81 set_current_state(TASK_RUNNING
);
82 remove_wait_queue(vcc
->sk
->sk_sleep
, &wait
);
87 static int atmtcp_recv_control(const struct atmtcp_control
*msg
)
89 struct atm_vcc
*vcc
= *(struct atm_vcc
**) &msg
->vcc
;
91 vcc
->vpi
= msg
->addr
.sap_addr
.vpi
;
92 vcc
->vci
= msg
->addr
.sap_addr
.vci
;
94 vcc
->sk
->sk_err
= -msg
->result
;
96 case ATMTCP_CTRL_OPEN
:
97 change_bit(ATM_VF_READY
,&vcc
->flags
);
99 case ATMTCP_CTRL_CLOSE
:
100 change_bit(ATM_VF_ADDR
,&vcc
->flags
);
103 printk(KERN_ERR
"atmtcp_recv_control: unknown type %d\n",
107 wake_up(vcc
->sk
->sk_sleep
);
112 static void atmtcp_v_dev_close(struct atm_dev
*dev
)
114 /* Nothing.... Isn't this simple :-) -- REW */
118 static int atmtcp_v_open(struct atm_vcc
*vcc
)
120 struct atmtcp_control msg
;
122 short vpi
= vcc
->vpi
;
125 memset(&msg
,0,sizeof(msg
));
126 msg
.addr
.sap_family
= AF_ATMPVC
;
127 msg
.hdr
.vpi
= htons(vpi
);
128 msg
.addr
.sap_addr
.vpi
= vpi
;
129 msg
.hdr
.vci
= htons(vci
);
130 msg
.addr
.sap_addr
.vci
= vci
;
131 if (vpi
== ATM_VPI_UNSPEC
|| vci
== ATM_VCI_UNSPEC
) return 0;
132 msg
.type
= ATMTCP_CTRL_OPEN
;
134 set_bit(ATM_VF_ADDR
,&vcc
->flags
);
135 clear_bit(ATM_VF_READY
,&vcc
->flags
); /* just in case ... */
136 error
= atmtcp_send_control(vcc
,ATMTCP_CTRL_OPEN
,&msg
,ATM_VF_READY
);
137 if (error
) return error
;
138 return -vcc
->sk
->sk_err
;
142 static void atmtcp_v_close(struct atm_vcc
*vcc
)
144 struct atmtcp_control msg
;
146 memset(&msg
,0,sizeof(msg
));
147 msg
.addr
.sap_family
= AF_ATMPVC
;
148 msg
.addr
.sap_addr
.vpi
= vcc
->vpi
;
149 msg
.addr
.sap_addr
.vci
= vcc
->vci
;
150 clear_bit(ATM_VF_READY
,&vcc
->flags
);
151 (void) atmtcp_send_control(vcc
,ATMTCP_CTRL_CLOSE
,&msg
,ATM_VF_ADDR
);
155 static int atmtcp_v_ioctl(struct atm_dev
*dev
,unsigned int cmd
,void __user
*arg
)
157 struct atm_cirange ci
;
159 struct hlist_node
*node
;
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
, node
, 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 memcpy(skb_put(new_skb
,skb
->len
),skb
->data
,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 struct hlist_node
*node
;
251 struct atm_vcc
*walk
;
254 atmtcp_dev
= (struct atm_dev
*) vcc
->dev_data
;
255 dev_data
= PRIV(atmtcp_dev
);
256 dev_data
->vcc
= NULL
;
257 if (dev_data
->persist
) return;
258 atmtcp_dev
->dev_data
= NULL
;
260 shutdown_atm_dev(atmtcp_dev
);
261 vcc
->dev_data
= NULL
;
262 read_lock(&vcc_sklist_lock
);
263 for(i
= 0; i
< VCC_HTABLE_SIZE
; ++i
) {
264 struct hlist_head
*head
= &vcc_hash
[i
];
266 sk_for_each(s
, node
, head
) {
268 if (walk
->dev
!= atmtcp_dev
)
270 wake_up(walk
->sk
->sk_sleep
);
273 read_unlock(&vcc_sklist_lock
);
277 static int atmtcp_c_send(struct atm_vcc
*vcc
,struct sk_buff
*skb
)
280 struct atmtcp_hdr
*hdr
;
282 struct hlist_node
*node
;
283 struct atm_vcc
*out_vcc
= NULL
;
284 struct sk_buff
*new_skb
;
287 if (!skb
->len
) return 0;
289 hdr
= (struct atmtcp_hdr
*) skb
->data
;
290 if (hdr
->length
== ATMTCP_HDR_MAGIC
) {
291 result
= atmtcp_recv_control(
292 (struct atmtcp_control
*) skb
->data
);
295 read_lock(&vcc_sklist_lock
);
296 for(i
= 0; i
< VCC_HTABLE_SIZE
; ++i
) {
297 struct hlist_head
*head
= &vcc_hash
[i
];
299 sk_for_each(s
, node
, head
) {
301 if (out_vcc
->dev
!= dev
)
303 if (out_vcc
->vpi
== ntohs(hdr
->vpi
) &&
304 out_vcc
->vci
== ntohs(hdr
->vci
) &&
305 out_vcc
->qos
.rxtp
.traffic_class
!= ATM_NONE
)
309 read_unlock(&vcc_sklist_lock
);
311 atomic_inc(&vcc
->stats
->tx_err
);
314 skb_pull(skb
,sizeof(struct atmtcp_hdr
));
315 new_skb
= atm_alloc_charge(out_vcc
,skb
->len
,GFP_KERNEL
);
320 do_gettimeofday(&new_skb
->stamp
);
321 memcpy(skb_put(new_skb
,skb
->len
),skb
->data
,skb
->len
);
322 out_vcc
->push(out_vcc
,new_skb
);
323 atomic_inc(&vcc
->stats
->tx
);
324 atomic_inc(&out_vcc
->stats
->rx
);
326 if (vcc
->pop
) vcc
->pop(vcc
,skb
);
327 else dev_kfree_skb(skb
);
333 * Device operations for the virtual ATM devices created by ATMTCP.
337 static struct atmdev_ops atmtcp_v_dev_ops
= {
338 .dev_close
= atmtcp_v_dev_close
,
339 .open
= atmtcp_v_open
,
340 .close
= atmtcp_v_close
,
341 .ioctl
= atmtcp_v_ioctl
,
342 .send
= atmtcp_v_send
,
343 .proc_read
= atmtcp_v_proc
,
349 * Device operations for the ATMTCP control device.
353 static struct atmdev_ops atmtcp_c_dev_ops
= {
354 .close
= atmtcp_c_close
,
355 .send
= atmtcp_c_send
359 static struct atm_dev atmtcp_control_dev
= {
360 .ops
= &atmtcp_c_dev_ops
,
363 .lock
= SPIN_LOCK_UNLOCKED
367 static int atmtcp_create(int itf
,int persist
,struct atm_dev
**result
)
369 struct atmtcp_dev_data
*dev_data
;
372 dev_data
= kmalloc(sizeof(*dev_data
),GFP_KERNEL
);
376 dev
= atm_dev_register(DEV_LABEL
,&atmtcp_v_dev_ops
,itf
,NULL
);
379 return itf
== -1 ? -ENOMEM
: -EBUSY
;
381 dev
->ci_range
.vpi_bits
= MAX_VPI_BITS
;
382 dev
->ci_range
.vci_bits
= MAX_VCI_BITS
;
383 dev
->dev_data
= dev_data
;
384 PRIV(dev
)->vcc
= NULL
;
385 PRIV(dev
)->persist
= persist
;
386 if (result
) *result
= dev
;
391 int atmtcp_attach(struct atm_vcc
*vcc
,int itf
)
396 if (itf
!= -1) dev
= atm_dev_lookup(itf
);
398 if (dev
->ops
!= &atmtcp_v_dev_ops
) {
402 if (PRIV(dev
)->vcc
) return -EBUSY
;
407 error
= atmtcp_create(itf
,0,&dev
);
408 if (error
) return error
;
410 PRIV(dev
)->vcc
= vcc
;
411 vcc
->dev
= &atmtcp_control_dev
;
412 vcc_insert_socket(vcc
->sk
);
413 set_bit(ATM_VF_META
,&vcc
->flags
);
414 set_bit(ATM_VF_READY
,&vcc
->flags
);
416 (void) atm_init_aal5(vcc
); /* @@@ losing AAL in transit ... */
417 vcc
->stats
= &atmtcp_control_dev
.stats
.aal5
;
422 int atmtcp_create_persistent(int itf
)
424 return atmtcp_create(itf
,1,NULL
);
428 int atmtcp_remove_persistent(int itf
)
431 struct atmtcp_dev_data
*dev_data
;
433 dev
= atm_dev_lookup(itf
);
434 if (!dev
) return -ENODEV
;
435 if (dev
->ops
!= &atmtcp_v_dev_ops
) {
439 dev_data
= PRIV(dev
);
440 if (!dev_data
->persist
) return 0;
441 dev_data
->persist
= 0;
442 if (PRIV(dev
)->vcc
) return 0;
445 shutdown_atm_dev(dev
);
449 static int atmtcp_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
452 struct atm_vcc
*vcc
= ATM_SD(sock
);
454 if (cmd
!= SIOCSIFATMTCP
&& cmd
!= ATMTCP_CREATE
&& cmd
!= ATMTCP_REMOVE
)
457 if (!capable(CAP_NET_ADMIN
))
462 err
= atmtcp_attach(vcc
, (int) arg
);
464 sock
->state
= SS_CONNECTED
;
465 __module_get(THIS_MODULE
);
469 err
= atmtcp_create_persistent((int) arg
);
472 err
= atmtcp_remove_persistent((int) arg
);
478 static struct atm_ioctl atmtcp_ioctl_ops
= {
479 .owner
= THIS_MODULE
,
480 .ioctl
= atmtcp_ioctl
,
483 static __init
int atmtcp_init(void)
485 register_atm_ioctl(&atmtcp_ioctl_ops
);
490 static void __exit
atmtcp_exit(void)
492 deregister_atm_ioctl(&atmtcp_ioctl_ops
);
495 MODULE_LICENSE("GPL");
496 module_init(atmtcp_init
);
497 module_exit(atmtcp_exit
);