4 * This code REQUIRES 2.1.15 or higher/ NET3.038
7 * This module is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 * LAPB 001 Jonathan Naylor Started Coding
14 * LAPB 002 Jonathan Naylor New timer architecture.
15 * 2000-10-29 Henner Eisen lapb_data_indication() return status.
18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/types.h>
21 #include <linux/socket.h>
23 #include <linux/kernel.h>
24 #include <linux/jiffies.h>
25 #include <linux/timer.h>
26 #include <linux/string.h>
27 #include <linux/sockios.h>
28 #include <linux/net.h>
29 #include <linux/inet.h>
30 #include <linux/if_arp.h>
31 #include <linux/skbuff.h>
32 #include <linux/slab.h>
34 #include <asm/uaccess.h>
35 #include <asm/system.h>
36 #include <linux/fcntl.h>
38 #include <linux/interrupt.h>
39 #include <linux/stat.h>
40 #include <linux/init.h>
43 static LIST_HEAD(lapb_list
);
44 static DEFINE_RWLOCK(lapb_list_lock
);
47 * Free an allocated lapb control block.
49 static void lapb_free_cb(struct lapb_cb
*lapb
)
54 static __inline__
void lapb_hold(struct lapb_cb
*lapb
)
56 atomic_inc(&lapb
->refcnt
);
59 static __inline__
void lapb_put(struct lapb_cb
*lapb
)
61 if (atomic_dec_and_test(&lapb
->refcnt
))
66 * Socket removal during an interrupt is now safe.
68 static void __lapb_remove_cb(struct lapb_cb
*lapb
)
70 if (lapb
->node
.next
) {
71 list_del(&lapb
->node
);
77 * Add a socket to the bound sockets list.
79 static void __lapb_insert_cb(struct lapb_cb
*lapb
)
81 list_add(&lapb
->node
, &lapb_list
);
85 static struct lapb_cb
*__lapb_devtostruct(struct net_device
*dev
)
87 struct list_head
*entry
;
88 struct lapb_cb
*lapb
, *use
= NULL
;
90 list_for_each(entry
, &lapb_list
) {
91 lapb
= list_entry(entry
, struct lapb_cb
, node
);
92 if (lapb
->dev
== dev
) {
104 static struct lapb_cb
*lapb_devtostruct(struct net_device
*dev
)
108 read_lock_bh(&lapb_list_lock
);
109 rc
= __lapb_devtostruct(dev
);
110 read_unlock_bh(&lapb_list_lock
);
115 * Create an empty LAPB control block.
117 static struct lapb_cb
*lapb_create_cb(void)
119 struct lapb_cb
*lapb
= kzalloc(sizeof(*lapb
), GFP_ATOMIC
);
125 skb_queue_head_init(&lapb
->write_queue
);
126 skb_queue_head_init(&lapb
->ack_queue
);
128 init_timer(&lapb
->t1timer
);
129 init_timer(&lapb
->t2timer
);
131 lapb
->t1
= LAPB_DEFAULT_T1
;
132 lapb
->t2
= LAPB_DEFAULT_T2
;
133 lapb
->n2
= LAPB_DEFAULT_N2
;
134 lapb
->mode
= LAPB_DEFAULT_MODE
;
135 lapb
->window
= LAPB_DEFAULT_WINDOW
;
136 lapb
->state
= LAPB_STATE_0
;
137 atomic_set(&lapb
->refcnt
, 1);
142 int lapb_register(struct net_device
*dev
, struct lapb_register_struct
*callbacks
)
144 struct lapb_cb
*lapb
;
145 int rc
= LAPB_BADTOKEN
;
147 write_lock_bh(&lapb_list_lock
);
149 lapb
= __lapb_devtostruct(dev
);
155 lapb
= lapb_create_cb();
161 lapb
->callbacks
= *callbacks
;
163 __lapb_insert_cb(lapb
);
165 lapb_start_t1timer(lapb
);
169 write_unlock_bh(&lapb_list_lock
);
173 int lapb_unregister(struct net_device
*dev
)
175 struct lapb_cb
*lapb
;
176 int rc
= LAPB_BADTOKEN
;
178 write_lock_bh(&lapb_list_lock
);
179 lapb
= __lapb_devtostruct(dev
);
183 lapb_stop_t1timer(lapb
);
184 lapb_stop_t2timer(lapb
);
186 lapb_clear_queues(lapb
);
188 __lapb_remove_cb(lapb
);
193 write_unlock_bh(&lapb_list_lock
);
197 int lapb_getparms(struct net_device
*dev
, struct lapb_parms_struct
*parms
)
199 int rc
= LAPB_BADTOKEN
;
200 struct lapb_cb
*lapb
= lapb_devtostruct(dev
);
205 parms
->t1
= lapb
->t1
/ HZ
;
206 parms
->t2
= lapb
->t2
/ HZ
;
207 parms
->n2
= lapb
->n2
;
208 parms
->n2count
= lapb
->n2count
;
209 parms
->state
= lapb
->state
;
210 parms
->window
= lapb
->window
;
211 parms
->mode
= lapb
->mode
;
213 if (!timer_pending(&lapb
->t1timer
))
216 parms
->t1timer
= (lapb
->t1timer
.expires
- jiffies
) / HZ
;
218 if (!timer_pending(&lapb
->t2timer
))
221 parms
->t2timer
= (lapb
->t2timer
.expires
- jiffies
) / HZ
;
229 int lapb_setparms(struct net_device
*dev
, struct lapb_parms_struct
*parms
)
231 int rc
= LAPB_BADTOKEN
;
232 struct lapb_cb
*lapb
= lapb_devtostruct(dev
);
238 if (parms
->t1
< 1 || parms
->t2
< 1 || parms
->n2
< 1)
241 if (lapb
->state
== LAPB_STATE_0
) {
242 if (parms
->mode
& LAPB_EXTENDED
) {
243 if (parms
->window
< 1 || parms
->window
> 127)
246 if (parms
->window
< 1 || parms
->window
> 7)
249 lapb
->mode
= parms
->mode
;
250 lapb
->window
= parms
->window
;
253 lapb
->t1
= parms
->t1
* HZ
;
254 lapb
->t2
= parms
->t2
* HZ
;
255 lapb
->n2
= parms
->n2
;
264 int lapb_connect_request(struct net_device
*dev
)
266 struct lapb_cb
*lapb
= lapb_devtostruct(dev
);
267 int rc
= LAPB_BADTOKEN
;
273 if (lapb
->state
== LAPB_STATE_1
)
277 if (lapb
->state
== LAPB_STATE_3
|| lapb
->state
== LAPB_STATE_4
)
280 lapb_establish_data_link(lapb
);
283 printk(KERN_DEBUG
"lapb: (%p) S0 -> S1\n", lapb
->dev
);
285 lapb
->state
= LAPB_STATE_1
;
294 int lapb_disconnect_request(struct net_device
*dev
)
296 struct lapb_cb
*lapb
= lapb_devtostruct(dev
);
297 int rc
= LAPB_BADTOKEN
;
302 switch (lapb
->state
) {
304 rc
= LAPB_NOTCONNECTED
;
309 printk(KERN_DEBUG
"lapb: (%p) S1 TX DISC(1)\n", lapb
->dev
);
312 printk(KERN_DEBUG
"lapb: (%p) S1 -> S0\n", lapb
->dev
);
314 lapb_send_control(lapb
, LAPB_DISC
, LAPB_POLLON
, LAPB_COMMAND
);
315 lapb
->state
= LAPB_STATE_0
;
316 lapb_start_t1timer(lapb
);
317 rc
= LAPB_NOTCONNECTED
;
325 lapb_clear_queues(lapb
);
327 lapb_send_control(lapb
, LAPB_DISC
, LAPB_POLLON
, LAPB_COMMAND
);
328 lapb_start_t1timer(lapb
);
329 lapb_stop_t2timer(lapb
);
330 lapb
->state
= LAPB_STATE_2
;
333 printk(KERN_DEBUG
"lapb: (%p) S3 DISC(1)\n", lapb
->dev
);
336 printk(KERN_DEBUG
"lapb: (%p) S3 -> S2\n", lapb
->dev
);
346 int lapb_data_request(struct net_device
*dev
, struct sk_buff
*skb
)
348 struct lapb_cb
*lapb
= lapb_devtostruct(dev
);
349 int rc
= LAPB_BADTOKEN
;
354 rc
= LAPB_NOTCONNECTED
;
355 if (lapb
->state
!= LAPB_STATE_3
&& lapb
->state
!= LAPB_STATE_4
)
358 skb_queue_tail(&lapb
->write_queue
, skb
);
367 int lapb_data_received(struct net_device
*dev
, struct sk_buff
*skb
)
369 struct lapb_cb
*lapb
= lapb_devtostruct(dev
);
370 int rc
= LAPB_BADTOKEN
;
373 lapb_data_input(lapb
, skb
);
381 void lapb_connect_confirmation(struct lapb_cb
*lapb
, int reason
)
383 if (lapb
->callbacks
.connect_confirmation
)
384 lapb
->callbacks
.connect_confirmation(lapb
->dev
, reason
);
387 void lapb_connect_indication(struct lapb_cb
*lapb
, int reason
)
389 if (lapb
->callbacks
.connect_indication
)
390 lapb
->callbacks
.connect_indication(lapb
->dev
, reason
);
393 void lapb_disconnect_confirmation(struct lapb_cb
*lapb
, int reason
)
395 if (lapb
->callbacks
.disconnect_confirmation
)
396 lapb
->callbacks
.disconnect_confirmation(lapb
->dev
, reason
);
399 void lapb_disconnect_indication(struct lapb_cb
*lapb
, int reason
)
401 if (lapb
->callbacks
.disconnect_indication
)
402 lapb
->callbacks
.disconnect_indication(lapb
->dev
, reason
);
405 int lapb_data_indication(struct lapb_cb
*lapb
, struct sk_buff
*skb
)
407 if (lapb
->callbacks
.data_indication
)
408 return lapb
->callbacks
.data_indication(lapb
->dev
, skb
);
411 return NET_RX_SUCCESS
; /* For now; must be != NET_RX_DROP */
414 int lapb_data_transmit(struct lapb_cb
*lapb
, struct sk_buff
*skb
)
418 if (lapb
->callbacks
.data_transmit
) {
419 lapb
->callbacks
.data_transmit(lapb
->dev
, skb
);
426 EXPORT_SYMBOL(lapb_register
);
427 EXPORT_SYMBOL(lapb_unregister
);
428 EXPORT_SYMBOL(lapb_getparms
);
429 EXPORT_SYMBOL(lapb_setparms
);
430 EXPORT_SYMBOL(lapb_connect_request
);
431 EXPORT_SYMBOL(lapb_disconnect_request
);
432 EXPORT_SYMBOL(lapb_data_request
);
433 EXPORT_SYMBOL(lapb_data_received
);
435 static int __init
lapb_init(void)
440 static void __exit
lapb_exit(void)
442 WARN_ON(!list_empty(&lapb_list
));
445 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
446 MODULE_DESCRIPTION("The X.25 Link Access Procedure B link layer protocol");
447 MODULE_LICENSE("GPL");
449 module_init(lapb_init
);
450 module_exit(lapb_exit
);