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/sched.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 lapb_cb *volatile lapb_list /* = NULL initially */;
45 * Free an allocated lapb control block. This is done to centralise
48 static void lapb_free_cb(lapb_cb *lapb)
56 * Socket removal during an interrupt is now safe.
58 static void lapb_remove_cb(lapb_cb *lapb)
63 save_flags(flags); cli();
65 if ((s = lapb_list) == lapb) {
71 while (s != NULL && s->next != NULL) {
72 if (s->next == lapb) {
85 * Add a socket to the bound sockets list.
87 static void lapb_insert_cb(lapb_cb *lapb)
91 save_flags(flags); cli();
93 lapb->next = lapb_list;
100 * Convert the integer token used by the device driver into a pointer
101 * to a LAPB control structure.
103 static lapb_cb *lapb_tokentostruct(void *token)
107 for (lapb = lapb_list; lapb != NULL; lapb = lapb->next)
108 if (lapb->token == token)
115 * Create an empty LAPB control block.
117 static lapb_cb *lapb_create_cb(void)
121 if ((lapb = kmalloc(sizeof(*lapb), GFP_ATOMIC)) == NULL)
126 memset(lapb, 0x00, sizeof(*lapb));
128 skb_queue_head_init(&lapb->write_queue);
129 skb_queue_head_init(&lapb->ack_queue);
131 init_timer(&lapb->t1timer);
132 init_timer(&lapb->t2timer);
134 lapb->t1 = LAPB_DEFAULT_T1;
135 lapb->t2 = LAPB_DEFAULT_T2;
136 lapb->n2 = LAPB_DEFAULT_N2;
137 lapb->mode = LAPB_DEFAULT_MODE;
138 lapb->window = LAPB_DEFAULT_WINDOW;
139 lapb->state = LAPB_STATE_0;
144 int lapb_register(void *token, struct lapb_register_struct *callbacks)
148 if (lapb_tokentostruct(token) != NULL)
149 return LAPB_BADTOKEN;
151 if ((lapb = lapb_create_cb()) == NULL)
155 lapb->callbacks = *callbacks;
157 lapb_insert_cb(lapb);
159 lapb_start_t1timer(lapb);
164 int lapb_unregister(void *token)
168 if ((lapb = lapb_tokentostruct(token)) == NULL)
169 return LAPB_BADTOKEN;
171 lapb_stop_t1timer(lapb);
172 lapb_stop_t2timer(lapb);
174 lapb_clear_queues(lapb);
176 lapb_remove_cb(lapb);
183 int lapb_getparms(void *token, struct lapb_parms_struct *parms)
187 if ((lapb = lapb_tokentostruct(token)) == NULL)
188 return LAPB_BADTOKEN;
190 parms->t1 = lapb->t1 / HZ;
191 parms->t2 = lapb->t2 / HZ;
192 parms->n2 = lapb->n2;
193 parms->n2count = lapb->n2count;
194 parms->state = lapb->state;
195 parms->window = lapb->window;
196 parms->mode = lapb->mode;
198 if (!timer_pending(&lapb->t1timer))
201 parms->t1timer = (lapb->t1timer.expires - jiffies) / HZ;
203 if (!timer_pending(&lapb->t2timer))
206 parms->t2timer = (lapb->t2timer.expires - jiffies) / HZ;
211 int lapb_setparms(void *token, struct lapb_parms_struct *parms)
215 if ((lapb = lapb_tokentostruct(token)) == NULL)
216 return LAPB_BADTOKEN;
227 if (lapb->state == LAPB_STATE_0) {
228 if (parms->mode & LAPB_EXTENDED) {
229 if (parms->window < 1 || parms->window > 127)
232 if (parms->window < 1 || parms->window > 7)
236 lapb->mode = parms->mode;
237 lapb->window = parms->window;
240 lapb->t1 = parms->t1 * HZ;
241 lapb->t2 = parms->t2 * HZ;
242 lapb->n2 = parms->n2;
247 int lapb_connect_request(void *token)
251 if ((lapb = lapb_tokentostruct(token)) == NULL)
252 return LAPB_BADTOKEN;
254 switch (lapb->state) {
259 return LAPB_CONNECTED;
262 lapb_establish_data_link(lapb);
265 printk(KERN_DEBUG "lapb: (%p) S0 -> S1\n", lapb->token);
268 lapb->state = LAPB_STATE_1;
273 int lapb_disconnect_request(void *token)
277 if ((lapb = lapb_tokentostruct(token)) == NULL)
278 return LAPB_BADTOKEN;
280 switch (lapb->state) {
282 return LAPB_NOTCONNECTED;
286 printk(KERN_DEBUG "lapb: (%p) S1 TX DISC(1)\n", lapb->token);
289 printk(KERN_DEBUG "lapb: (%p) S1 -> S0\n", lapb->token);
291 lapb_send_control(lapb, LAPB_DISC, LAPB_POLLON, LAPB_COMMAND);
292 lapb->state = LAPB_STATE_0;
293 lapb_start_t1timer(lapb);
294 return LAPB_NOTCONNECTED;
300 lapb_clear_queues(lapb);
302 lapb_send_control(lapb, LAPB_DISC, LAPB_POLLON, LAPB_COMMAND);
303 lapb_start_t1timer(lapb);
304 lapb_stop_t2timer(lapb);
305 lapb->state = LAPB_STATE_2;
308 printk(KERN_DEBUG "lapb: (%p) S3 DISC(1)\n", lapb->token);
311 printk(KERN_DEBUG "lapb: (%p) S3 -> S2\n", lapb->token);
317 int lapb_data_request(void *token, struct sk_buff *skb)
321 if ((lapb = lapb_tokentostruct(token)) == NULL)
322 return LAPB_BADTOKEN;
324 if (lapb->state != LAPB_STATE_3 && lapb->state != LAPB_STATE_4)
325 return LAPB_NOTCONNECTED;
327 skb_queue_tail(&lapb->write_queue, skb);
334 int lapb_data_received(void *token, struct sk_buff *skb)
338 if ((lapb = lapb_tokentostruct(token)) == NULL)
339 return LAPB_BADTOKEN;
341 lapb_data_input(lapb, skb);
346 void lapb_connect_confirmation(lapb_cb *lapb, int reason)
348 if (lapb->callbacks.connect_confirmation != NULL)
349 (lapb->callbacks.connect_confirmation)(lapb->token, reason);
352 void lapb_connect_indication(lapb_cb *lapb, int reason)
354 if (lapb->callbacks.connect_indication != NULL)
355 (lapb->callbacks.connect_indication)(lapb->token, reason);
358 void lapb_disconnect_confirmation(lapb_cb *lapb, int reason)
360 if (lapb->callbacks.disconnect_confirmation != NULL)
361 (lapb->callbacks.disconnect_confirmation)(lapb->token, reason);
364 void lapb_disconnect_indication(lapb_cb *lapb, int reason)
366 if (lapb->callbacks.disconnect_indication != NULL)
367 (lapb->callbacks.disconnect_indication)(lapb->token, reason);
370 int lapb_data_indication(lapb_cb *lapb, struct sk_buff *skb)
372 if (lapb->callbacks.data_indication != NULL) {
373 return (lapb->callbacks.data_indication)(lapb->token, skb);
376 return NET_RX_CN_HIGH; /* For now; must be != NET_RX_DROP */
379 int lapb_data_transmit(lapb_cb *lapb, struct sk_buff *skb)
383 if (lapb->callbacks.data_transmit != NULL) {
384 (lapb->callbacks.data_transmit)(lapb->token, skb);
391 EXPORT_SYMBOL(lapb_register);
392 EXPORT_SYMBOL(lapb_unregister);
393 EXPORT_SYMBOL(lapb_getparms);
394 EXPORT_SYMBOL(lapb_setparms);
395 EXPORT_SYMBOL(lapb_connect_request);
396 EXPORT_SYMBOL(lapb_disconnect_request);
397 EXPORT_SYMBOL(lapb_data_request);
398 EXPORT_SYMBOL(lapb_data_received);
400 static char banner[] __initdata = KERN_INFO "NET4: LAPB for Linux. Version 0.01 for NET4.0\n";
402 static int __init lapb_init(void)
408 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
409 MODULE_DESCRIPTION("The X.25 Link Access Procedure B link layer protocol");
410 MODULE_LICENSE("GPL");
412 module_init(lapb_init);