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>
33 #include <asm/uaccess.h>
34 #include <asm/system.h>
35 #include <linux/fcntl.h>
37 #include <linux/interrupt.h>
38 #include <linux/stat.h>
39 #include <linux/init.h>
42 static struct list_head lapb_list = LIST_HEAD_INIT(lapb_list);
43 static DEFINE_RWLOCK(lapb_list_lock);
46 * Free an allocated lapb control block.
48 static void lapb_free_cb(struct lapb_cb *lapb)
53 static __inline__ void lapb_hold(struct lapb_cb *lapb)
55 atomic_inc(&lapb->refcnt);
58 static __inline__ void lapb_put(struct lapb_cb *lapb)
60 if (atomic_dec_and_test(&lapb->refcnt))
65 * Socket removal during an interrupt is now safe.
67 static void __lapb_remove_cb(struct lapb_cb *lapb)
69 if (lapb->node.next) {
70 list_del(&lapb->node);
76 * Add a socket to the bound sockets list.
78 static void __lapb_insert_cb(struct lapb_cb *lapb)
80 list_add(&lapb->node, &lapb_list);
84 static struct lapb_cb *__lapb_devtostruct(struct net_device *dev)
86 struct list_head *entry;
87 struct lapb_cb *lapb, *use = NULL;
89 list_for_each(entry, &lapb_list) {
90 lapb = list_entry(entry, struct lapb_cb, node);
91 if (lapb->dev == dev) {
103 static struct lapb_cb *lapb_devtostruct(struct net_device *dev)
107 read_lock_bh(&lapb_list_lock);
108 rc = __lapb_devtostruct(dev);
109 read_unlock_bh(&lapb_list_lock);
114 * Create an empty LAPB control block.
116 static struct lapb_cb *lapb_create_cb(void)
118 struct lapb_cb *lapb = kzalloc(sizeof(*lapb), GFP_ATOMIC);
124 skb_queue_head_init(&lapb->write_queue);
125 skb_queue_head_init(&lapb->ack_queue);
127 init_timer(&lapb->t1timer);
128 init_timer(&lapb->t2timer);
130 lapb->t1 = LAPB_DEFAULT_T1;
131 lapb->t2 = LAPB_DEFAULT_T2;
132 lapb->n2 = LAPB_DEFAULT_N2;
133 lapb->mode = LAPB_DEFAULT_MODE;
134 lapb->window = LAPB_DEFAULT_WINDOW;
135 lapb->state = LAPB_STATE_0;
136 atomic_set(&lapb->refcnt, 1);
141 int lapb_register(struct net_device *dev, struct lapb_register_struct *callbacks)
143 struct lapb_cb *lapb;
144 int rc = LAPB_BADTOKEN;
146 write_lock_bh(&lapb_list_lock);
148 lapb = __lapb_devtostruct(dev);
154 lapb = lapb_create_cb();
160 lapb->callbacks = *callbacks;
162 __lapb_insert_cb(lapb);
164 lapb_start_t1timer(lapb);
168 write_unlock_bh(&lapb_list_lock);
172 int lapb_unregister(struct net_device *dev)
174 struct lapb_cb *lapb;
175 int rc = LAPB_BADTOKEN;
177 write_lock_bh(&lapb_list_lock);
178 lapb = __lapb_devtostruct(dev);
182 lapb_stop_t1timer(lapb);
183 lapb_stop_t2timer(lapb);
185 lapb_clear_queues(lapb);
187 __lapb_remove_cb(lapb);
192 write_unlock_bh(&lapb_list_lock);
196 int lapb_getparms(struct net_device *dev, struct lapb_parms_struct *parms)
198 int rc = LAPB_BADTOKEN;
199 struct lapb_cb *lapb = lapb_devtostruct(dev);
204 parms->t1 = lapb->t1 / HZ;
205 parms->t2 = lapb->t2 / HZ;
206 parms->n2 = lapb->n2;
207 parms->n2count = lapb->n2count;
208 parms->state = lapb->state;
209 parms->window = lapb->window;
210 parms->mode = lapb->mode;
212 if (!timer_pending(&lapb->t1timer))
215 parms->t1timer = (lapb->t1timer.expires - jiffies) / HZ;
217 if (!timer_pending(&lapb->t2timer))
220 parms->t2timer = (lapb->t2timer.expires - jiffies) / HZ;
228 int lapb_setparms(struct net_device *dev, struct lapb_parms_struct *parms)
230 int rc = LAPB_BADTOKEN;
231 struct lapb_cb *lapb = lapb_devtostruct(dev);
237 if (parms->t1 < 1 || parms->t2 < 1 || parms->n2 < 1)
240 if (lapb->state == LAPB_STATE_0) {
241 if (parms->mode & LAPB_EXTENDED) {
242 if (parms->window < 1 || parms->window > 127)
245 if (parms->window < 1 || parms->window > 7)
248 lapb->mode = parms->mode;
249 lapb->window = parms->window;
252 lapb->t1 = parms->t1 * HZ;
253 lapb->t2 = parms->t2 * HZ;
254 lapb->n2 = parms->n2;
263 int lapb_connect_request(struct net_device *dev)
265 struct lapb_cb *lapb = lapb_devtostruct(dev);
266 int rc = LAPB_BADTOKEN;
272 if (lapb->state == LAPB_STATE_1)
276 if (lapb->state == LAPB_STATE_3 || lapb->state == LAPB_STATE_4)
279 lapb_establish_data_link(lapb);
282 printk(KERN_DEBUG "lapb: (%p) S0 -> S1\n", lapb->dev);
284 lapb->state = LAPB_STATE_1;
293 int lapb_disconnect_request(struct net_device *dev)
295 struct lapb_cb *lapb = lapb_devtostruct(dev);
296 int rc = LAPB_BADTOKEN;
301 switch (lapb->state) {
303 rc = LAPB_NOTCONNECTED;
308 printk(KERN_DEBUG "lapb: (%p) S1 TX DISC(1)\n", lapb->dev);
311 printk(KERN_DEBUG "lapb: (%p) S1 -> S0\n", lapb->dev);
313 lapb_send_control(lapb, LAPB_DISC, LAPB_POLLON, LAPB_COMMAND);
314 lapb->state = LAPB_STATE_0;
315 lapb_start_t1timer(lapb);
316 rc = LAPB_NOTCONNECTED;
324 lapb_clear_queues(lapb);
326 lapb_send_control(lapb, LAPB_DISC, LAPB_POLLON, LAPB_COMMAND);
327 lapb_start_t1timer(lapb);
328 lapb_stop_t2timer(lapb);
329 lapb->state = LAPB_STATE_2;
332 printk(KERN_DEBUG "lapb: (%p) S3 DISC(1)\n", lapb->dev);
335 printk(KERN_DEBUG "lapb: (%p) S3 -> S2\n", lapb->dev);
345 int lapb_data_request(struct net_device *dev, struct sk_buff *skb)
347 struct lapb_cb *lapb = lapb_devtostruct(dev);
348 int rc = LAPB_BADTOKEN;
353 rc = LAPB_NOTCONNECTED;
354 if (lapb->state != LAPB_STATE_3 && lapb->state != LAPB_STATE_4)
357 skb_queue_tail(&lapb->write_queue, skb);
366 int lapb_data_received(struct net_device *dev, struct sk_buff *skb)
368 struct lapb_cb *lapb = lapb_devtostruct(dev);
369 int rc = LAPB_BADTOKEN;
372 lapb_data_input(lapb, skb);
380 void lapb_connect_confirmation(struct lapb_cb *lapb, int reason)
382 if (lapb->callbacks.connect_confirmation)
383 lapb->callbacks.connect_confirmation(lapb->dev, reason);
386 void lapb_connect_indication(struct lapb_cb *lapb, int reason)
388 if (lapb->callbacks.connect_indication)
389 lapb->callbacks.connect_indication(lapb->dev, reason);
392 void lapb_disconnect_confirmation(struct lapb_cb *lapb, int reason)
394 if (lapb->callbacks.disconnect_confirmation)
395 lapb->callbacks.disconnect_confirmation(lapb->dev, reason);
398 void lapb_disconnect_indication(struct lapb_cb *lapb, int reason)
400 if (lapb->callbacks.disconnect_indication)
401 lapb->callbacks.disconnect_indication(lapb->dev, reason);
404 int lapb_data_indication(struct lapb_cb *lapb, struct sk_buff *skb)
406 if (lapb->callbacks.data_indication)
407 return lapb->callbacks.data_indication(lapb->dev, skb);
410 return NET_RX_CN_HIGH; /* For now; must be != NET_RX_DROP */
413 int lapb_data_transmit(struct lapb_cb *lapb, struct sk_buff *skb)
417 if (lapb->callbacks.data_transmit) {
418 lapb->callbacks.data_transmit(lapb->dev, skb);
425 EXPORT_SYMBOL(lapb_register);
426 EXPORT_SYMBOL(lapb_unregister);
427 EXPORT_SYMBOL(lapb_getparms);
428 EXPORT_SYMBOL(lapb_setparms);
429 EXPORT_SYMBOL(lapb_connect_request);
430 EXPORT_SYMBOL(lapb_disconnect_request);
431 EXPORT_SYMBOL(lapb_data_request);
432 EXPORT_SYMBOL(lapb_data_received);
434 static int __init lapb_init(void)
439 static void __exit lapb_exit(void)
441 WARN_ON(!list_empty(&lapb_list));
444 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
445 MODULE_DESCRIPTION("The X.25 Link Access Procedure B link layer protocol");
446 MODULE_LICENSE("GPL");
448 module_init(lapb_init);
449 module_exit(lapb_exit);