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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/module.h>
21 #include <linux/errno.h>
22 #include <linux/types.h>
23 #include <linux/socket.h>
25 #include <linux/kernel.h>
26 #include <linux/jiffies.h>
27 #include <linux/timer.h>
28 #include <linux/string.h>
29 #include <linux/sockios.h>
30 #include <linux/net.h>
31 #include <linux/inet.h>
32 #include <linux/if_arp.h>
33 #include <linux/skbuff.h>
34 #include <linux/slab.h>
36 #include <linux/uaccess.h>
37 #include <linux/fcntl.h>
39 #include <linux/interrupt.h>
40 #include <linux/stat.h>
41 #include <linux/init.h>
44 static LIST_HEAD(lapb_list
);
45 static DEFINE_RWLOCK(lapb_list_lock
);
48 * Free an allocated lapb control block.
50 static void lapb_free_cb(struct lapb_cb
*lapb
)
55 static __inline__
void lapb_hold(struct lapb_cb
*lapb
)
57 atomic_inc(&lapb
->refcnt
);
60 static __inline__
void lapb_put(struct lapb_cb
*lapb
)
62 if (atomic_dec_and_test(&lapb
->refcnt
))
67 * Socket removal during an interrupt is now safe.
69 static void __lapb_remove_cb(struct lapb_cb
*lapb
)
71 if (lapb
->node
.next
) {
72 list_del(&lapb
->node
);
76 EXPORT_SYMBOL(lapb_register
);
79 * Add a socket to the bound sockets list.
81 static void __lapb_insert_cb(struct lapb_cb
*lapb
)
83 list_add(&lapb
->node
, &lapb_list
);
87 static struct lapb_cb
*__lapb_devtostruct(struct net_device
*dev
)
89 struct list_head
*entry
;
90 struct lapb_cb
*lapb
, *use
= NULL
;
92 list_for_each(entry
, &lapb_list
) {
93 lapb
= list_entry(entry
, struct lapb_cb
, node
);
94 if (lapb
->dev
== dev
) {
106 static struct lapb_cb
*lapb_devtostruct(struct net_device
*dev
)
110 read_lock_bh(&lapb_list_lock
);
111 rc
= __lapb_devtostruct(dev
);
112 read_unlock_bh(&lapb_list_lock
);
117 * Create an empty LAPB control block.
119 static struct lapb_cb
*lapb_create_cb(void)
121 struct lapb_cb
*lapb
= kzalloc(sizeof(*lapb
), GFP_ATOMIC
);
127 skb_queue_head_init(&lapb
->write_queue
);
128 skb_queue_head_init(&lapb
->ack_queue
);
130 init_timer(&lapb
->t1timer
);
131 init_timer(&lapb
->t2timer
);
133 lapb
->t1
= LAPB_DEFAULT_T1
;
134 lapb
->t2
= LAPB_DEFAULT_T2
;
135 lapb
->n2
= LAPB_DEFAULT_N2
;
136 lapb
->mode
= LAPB_DEFAULT_MODE
;
137 lapb
->window
= LAPB_DEFAULT_WINDOW
;
138 lapb
->state
= LAPB_STATE_0
;
139 atomic_set(&lapb
->refcnt
, 1);
144 int lapb_register(struct net_device
*dev
,
145 const struct lapb_register_struct
*callbacks
)
147 struct lapb_cb
*lapb
;
148 int rc
= LAPB_BADTOKEN
;
150 write_lock_bh(&lapb_list_lock
);
152 lapb
= __lapb_devtostruct(dev
);
158 lapb
= lapb_create_cb();
164 lapb
->callbacks
= callbacks
;
166 __lapb_insert_cb(lapb
);
168 lapb_start_t1timer(lapb
);
172 write_unlock_bh(&lapb_list_lock
);
176 int lapb_unregister(struct net_device
*dev
)
178 struct lapb_cb
*lapb
;
179 int rc
= LAPB_BADTOKEN
;
181 write_lock_bh(&lapb_list_lock
);
182 lapb
= __lapb_devtostruct(dev
);
186 lapb_stop_t1timer(lapb
);
187 lapb_stop_t2timer(lapb
);
189 lapb_clear_queues(lapb
);
191 __lapb_remove_cb(lapb
);
196 write_unlock_bh(&lapb_list_lock
);
199 EXPORT_SYMBOL(lapb_unregister
);
201 int lapb_getparms(struct net_device
*dev
, struct lapb_parms_struct
*parms
)
203 int rc
= LAPB_BADTOKEN
;
204 struct lapb_cb
*lapb
= lapb_devtostruct(dev
);
209 parms
->t1
= lapb
->t1
/ HZ
;
210 parms
->t2
= lapb
->t2
/ HZ
;
211 parms
->n2
= lapb
->n2
;
212 parms
->n2count
= lapb
->n2count
;
213 parms
->state
= lapb
->state
;
214 parms
->window
= lapb
->window
;
215 parms
->mode
= lapb
->mode
;
217 if (!timer_pending(&lapb
->t1timer
))
220 parms
->t1timer
= (lapb
->t1timer
.expires
- jiffies
) / HZ
;
222 if (!timer_pending(&lapb
->t2timer
))
225 parms
->t2timer
= (lapb
->t2timer
.expires
- jiffies
) / HZ
;
232 EXPORT_SYMBOL(lapb_getparms
);
234 int lapb_setparms(struct net_device
*dev
, struct lapb_parms_struct
*parms
)
236 int rc
= LAPB_BADTOKEN
;
237 struct lapb_cb
*lapb
= lapb_devtostruct(dev
);
243 if (parms
->t1
< 1 || parms
->t2
< 1 || parms
->n2
< 1)
246 if (lapb
->state
== LAPB_STATE_0
) {
247 if (parms
->mode
& LAPB_EXTENDED
) {
248 if (parms
->window
< 1 || parms
->window
> 127)
251 if (parms
->window
< 1 || parms
->window
> 7)
254 lapb
->mode
= parms
->mode
;
255 lapb
->window
= parms
->window
;
258 lapb
->t1
= parms
->t1
* HZ
;
259 lapb
->t2
= parms
->t2
* HZ
;
260 lapb
->n2
= parms
->n2
;
268 EXPORT_SYMBOL(lapb_setparms
);
270 int lapb_connect_request(struct net_device
*dev
)
272 struct lapb_cb
*lapb
= lapb_devtostruct(dev
);
273 int rc
= LAPB_BADTOKEN
;
279 if (lapb
->state
== LAPB_STATE_1
)
283 if (lapb
->state
== LAPB_STATE_3
|| lapb
->state
== LAPB_STATE_4
)
286 lapb_establish_data_link(lapb
);
288 lapb_dbg(0, "(%p) S0 -> S1\n", lapb
->dev
);
289 lapb
->state
= LAPB_STATE_1
;
297 EXPORT_SYMBOL(lapb_connect_request
);
299 int lapb_disconnect_request(struct net_device
*dev
)
301 struct lapb_cb
*lapb
= lapb_devtostruct(dev
);
302 int rc
= LAPB_BADTOKEN
;
307 switch (lapb
->state
) {
309 rc
= LAPB_NOTCONNECTED
;
313 lapb_dbg(1, "(%p) S1 TX DISC(1)\n", lapb
->dev
);
314 lapb_dbg(0, "(%p) S1 -> S0\n", lapb
->dev
);
315 lapb_send_control(lapb
, LAPB_DISC
, LAPB_POLLON
, LAPB_COMMAND
);
316 lapb
->state
= LAPB_STATE_0
;
317 lapb_start_t1timer(lapb
);
318 rc
= LAPB_NOTCONNECTED
;
326 lapb_clear_queues(lapb
);
328 lapb_send_control(lapb
, LAPB_DISC
, LAPB_POLLON
, LAPB_COMMAND
);
329 lapb_start_t1timer(lapb
);
330 lapb_stop_t2timer(lapb
);
331 lapb
->state
= LAPB_STATE_2
;
333 lapb_dbg(1, "(%p) S3 DISC(1)\n", lapb
->dev
);
334 lapb_dbg(0, "(%p) S3 -> S2\n", lapb
->dev
);
342 EXPORT_SYMBOL(lapb_disconnect_request
);
344 int lapb_data_request(struct net_device
*dev
, struct sk_buff
*skb
)
346 struct lapb_cb
*lapb
= lapb_devtostruct(dev
);
347 int rc
= LAPB_BADTOKEN
;
352 rc
= LAPB_NOTCONNECTED
;
353 if (lapb
->state
!= LAPB_STATE_3
&& lapb
->state
!= LAPB_STATE_4
)
356 skb_queue_tail(&lapb
->write_queue
, skb
);
364 EXPORT_SYMBOL(lapb_data_request
);
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
);
379 EXPORT_SYMBOL(lapb_data_received
);
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 static int __init
lapb_init(void)
431 static void __exit
lapb_exit(void)
433 WARN_ON(!list_empty(&lapb_list
));
436 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
437 MODULE_DESCRIPTION("The X.25 Link Access Procedure B link layer protocol");
438 MODULE_LICENSE("GPL");
440 module_init(lapb_init
);
441 module_exit(lapb_exit
);