1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * This code REQUIRES 2.1.15 or higher/ NET3.038
8 * LAPB 001 Jonathan Naylor Started Coding
9 * LAPB 002 Jonathan Naylor New timer architecture.
10 * 2000-10-29 Henner Eisen lapb_data_indication() return status.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/errno.h>
17 #include <linux/types.h>
18 #include <linux/socket.h>
20 #include <linux/kernel.h>
21 #include <linux/jiffies.h>
22 #include <linux/timer.h>
23 #include <linux/string.h>
24 #include <linux/sockios.h>
25 #include <linux/net.h>
26 #include <linux/inet.h>
27 #include <linux/if_arp.h>
28 #include <linux/skbuff.h>
29 #include <linux/slab.h>
31 #include <linux/uaccess.h>
32 #include <linux/fcntl.h>
34 #include <linux/interrupt.h>
35 #include <linux/stat.h>
36 #include <linux/init.h>
39 static LIST_HEAD(lapb_list);
40 static DEFINE_RWLOCK(lapb_list_lock);
43 * Free an allocated lapb control block.
45 static void lapb_free_cb(struct lapb_cb *lapb)
50 static __inline__ void lapb_hold(struct lapb_cb *lapb)
52 refcount_inc(&lapb->refcnt);
55 static __inline__ void lapb_put(struct lapb_cb *lapb)
57 if (refcount_dec_and_test(&lapb->refcnt))
62 * Socket removal during an interrupt is now safe.
64 static void __lapb_remove_cb(struct lapb_cb *lapb)
66 if (lapb->node.next) {
67 list_del(&lapb->node);
73 * Add a socket to the bound sockets list.
75 static void __lapb_insert_cb(struct lapb_cb *lapb)
77 list_add(&lapb->node, &lapb_list);
81 static struct lapb_cb *__lapb_devtostruct(struct net_device *dev)
83 struct lapb_cb *lapb, *use = NULL;
85 list_for_each_entry(lapb, &lapb_list, node) {
86 if (lapb->dev == dev) {
98 static struct lapb_cb *lapb_devtostruct(struct net_device *dev)
102 read_lock_bh(&lapb_list_lock);
103 rc = __lapb_devtostruct(dev);
104 read_unlock_bh(&lapb_list_lock);
109 * Create an empty LAPB control block.
111 static struct lapb_cb *lapb_create_cb(void)
113 struct lapb_cb *lapb = kzalloc(sizeof(*lapb), GFP_ATOMIC);
118 skb_queue_head_init(&lapb->write_queue);
119 skb_queue_head_init(&lapb->ack_queue);
121 timer_setup(&lapb->t1timer, NULL, 0);
122 timer_setup(&lapb->t2timer, NULL, 0);
123 lapb->t1timer_running = false;
124 lapb->t2timer_running = false;
126 lapb->t1 = LAPB_DEFAULT_T1;
127 lapb->t2 = LAPB_DEFAULT_T2;
128 lapb->n2 = LAPB_DEFAULT_N2;
129 lapb->mode = LAPB_DEFAULT_MODE;
130 lapb->window = LAPB_DEFAULT_WINDOW;
131 lapb->state = LAPB_STATE_0;
133 spin_lock_init(&lapb->lock);
134 refcount_set(&lapb->refcnt, 1);
139 int lapb_register(struct net_device *dev,
140 const struct lapb_register_struct *callbacks)
142 struct lapb_cb *lapb;
143 int rc = LAPB_BADTOKEN;
145 write_lock_bh(&lapb_list_lock);
147 lapb = __lapb_devtostruct(dev);
153 lapb = lapb_create_cb();
159 lapb->callbacks = callbacks;
161 __lapb_insert_cb(lapb);
163 lapb_start_t1timer(lapb);
167 write_unlock_bh(&lapb_list_lock);
170 EXPORT_SYMBOL(lapb_register);
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);
183 /* Wait for other refs to "lapb" to drop */
184 while (refcount_read(&lapb->refcnt) > 2)
187 spin_lock_bh(&lapb->lock);
189 lapb_stop_t1timer(lapb);
190 lapb_stop_t2timer(lapb);
192 lapb_clear_queues(lapb);
194 spin_unlock_bh(&lapb->lock);
196 /* Wait for running timers to stop */
197 del_timer_sync(&lapb->t1timer);
198 del_timer_sync(&lapb->t2timer);
200 __lapb_remove_cb(lapb);
205 write_unlock_bh(&lapb_list_lock);
208 EXPORT_SYMBOL(lapb_unregister);
210 int lapb_getparms(struct net_device *dev, struct lapb_parms_struct *parms)
212 int rc = LAPB_BADTOKEN;
213 struct lapb_cb *lapb = lapb_devtostruct(dev);
218 spin_lock_bh(&lapb->lock);
220 parms->t1 = lapb->t1 / HZ;
221 parms->t2 = lapb->t2 / HZ;
222 parms->n2 = lapb->n2;
223 parms->n2count = lapb->n2count;
224 parms->state = lapb->state;
225 parms->window = lapb->window;
226 parms->mode = lapb->mode;
228 if (!timer_pending(&lapb->t1timer))
231 parms->t1timer = (lapb->t1timer.expires - jiffies) / HZ;
233 if (!timer_pending(&lapb->t2timer))
236 parms->t2timer = (lapb->t2timer.expires - jiffies) / HZ;
238 spin_unlock_bh(&lapb->lock);
244 EXPORT_SYMBOL(lapb_getparms);
246 int lapb_setparms(struct net_device *dev, struct lapb_parms_struct *parms)
248 int rc = LAPB_BADTOKEN;
249 struct lapb_cb *lapb = lapb_devtostruct(dev);
254 spin_lock_bh(&lapb->lock);
257 if (parms->t1 < 1 || parms->t2 < 1 || parms->n2 < 1)
260 if (lapb->state == LAPB_STATE_0) {
261 if (parms->mode & LAPB_EXTENDED) {
262 if (parms->window < 1 || parms->window > 127)
265 if (parms->window < 1 || parms->window > 7)
268 lapb->mode = parms->mode;
269 lapb->window = parms->window;
272 lapb->t1 = parms->t1 * HZ;
273 lapb->t2 = parms->t2 * HZ;
274 lapb->n2 = parms->n2;
278 spin_unlock_bh(&lapb->lock);
283 EXPORT_SYMBOL(lapb_setparms);
285 int lapb_connect_request(struct net_device *dev)
287 struct lapb_cb *lapb = lapb_devtostruct(dev);
288 int rc = LAPB_BADTOKEN;
293 spin_lock_bh(&lapb->lock);
296 if (lapb->state == LAPB_STATE_1)
300 if (lapb->state == LAPB_STATE_3 || lapb->state == LAPB_STATE_4)
303 lapb_establish_data_link(lapb);
305 lapb_dbg(0, "(%p) S0 -> S1\n", lapb->dev);
306 lapb->state = LAPB_STATE_1;
310 spin_unlock_bh(&lapb->lock);
315 EXPORT_SYMBOL(lapb_connect_request);
317 static int __lapb_disconnect_request(struct lapb_cb *lapb)
319 switch (lapb->state) {
321 return LAPB_NOTCONNECTED;
324 lapb_dbg(1, "(%p) S1 TX DISC(1)\n", lapb->dev);
325 lapb_dbg(0, "(%p) S1 -> S0\n", lapb->dev);
326 lapb_send_control(lapb, LAPB_DISC, LAPB_POLLON, LAPB_COMMAND);
327 lapb->state = LAPB_STATE_0;
328 lapb_start_t1timer(lapb);
329 return LAPB_NOTCONNECTED;
335 lapb_clear_queues(lapb);
337 lapb_send_control(lapb, LAPB_DISC, LAPB_POLLON, LAPB_COMMAND);
338 lapb_start_t1timer(lapb);
339 lapb_stop_t2timer(lapb);
340 lapb->state = LAPB_STATE_2;
342 lapb_dbg(1, "(%p) S3 DISC(1)\n", lapb->dev);
343 lapb_dbg(0, "(%p) S3 -> S2\n", lapb->dev);
348 int lapb_disconnect_request(struct net_device *dev)
350 struct lapb_cb *lapb = lapb_devtostruct(dev);
351 int rc = LAPB_BADTOKEN;
356 spin_lock_bh(&lapb->lock);
358 rc = __lapb_disconnect_request(lapb);
360 spin_unlock_bh(&lapb->lock);
365 EXPORT_SYMBOL(lapb_disconnect_request);
367 int lapb_data_request(struct net_device *dev, struct sk_buff *skb)
369 struct lapb_cb *lapb = lapb_devtostruct(dev);
370 int rc = LAPB_BADTOKEN;
375 spin_lock_bh(&lapb->lock);
377 rc = LAPB_NOTCONNECTED;
378 if (lapb->state != LAPB_STATE_3 && lapb->state != LAPB_STATE_4)
381 skb_queue_tail(&lapb->write_queue, skb);
385 spin_unlock_bh(&lapb->lock);
390 EXPORT_SYMBOL(lapb_data_request);
392 int lapb_data_received(struct net_device *dev, struct sk_buff *skb)
394 struct lapb_cb *lapb = lapb_devtostruct(dev);
395 int rc = LAPB_BADTOKEN;
398 spin_lock_bh(&lapb->lock);
399 lapb_data_input(lapb, skb);
400 spin_unlock_bh(&lapb->lock);
407 EXPORT_SYMBOL(lapb_data_received);
409 void lapb_connect_confirmation(struct lapb_cb *lapb, int reason)
411 if (lapb->callbacks->connect_confirmation)
412 lapb->callbacks->connect_confirmation(lapb->dev, reason);
415 void lapb_connect_indication(struct lapb_cb *lapb, int reason)
417 if (lapb->callbacks->connect_indication)
418 lapb->callbacks->connect_indication(lapb->dev, reason);
421 void lapb_disconnect_confirmation(struct lapb_cb *lapb, int reason)
423 if (lapb->callbacks->disconnect_confirmation)
424 lapb->callbacks->disconnect_confirmation(lapb->dev, reason);
427 void lapb_disconnect_indication(struct lapb_cb *lapb, int reason)
429 if (lapb->callbacks->disconnect_indication)
430 lapb->callbacks->disconnect_indication(lapb->dev, reason);
433 int lapb_data_indication(struct lapb_cb *lapb, struct sk_buff *skb)
435 if (lapb->callbacks->data_indication)
436 return lapb->callbacks->data_indication(lapb->dev, skb);
439 return NET_RX_SUCCESS; /* For now; must be != NET_RX_DROP */
442 int lapb_data_transmit(struct lapb_cb *lapb, struct sk_buff *skb)
446 if (lapb->callbacks->data_transmit) {
447 lapb->callbacks->data_transmit(lapb->dev, skb);
454 /* Handle device status changes. */
455 static int lapb_device_event(struct notifier_block *this, unsigned long event,
458 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
459 struct lapb_cb *lapb;
461 if (!net_eq(dev_net(dev), &init_net))
464 if (dev->type != ARPHRD_X25)
467 lapb = lapb_devtostruct(dev);
471 spin_lock_bh(&lapb->lock);
475 lapb_dbg(0, "(%p) Interface up: %s\n", dev, dev->name);
477 if (netif_carrier_ok(dev)) {
478 lapb_dbg(0, "(%p): Carrier is already up: %s\n", dev,
480 if (lapb->mode & LAPB_DCE) {
481 lapb_start_t1timer(lapb);
483 if (lapb->state == LAPB_STATE_0) {
484 lapb->state = LAPB_STATE_1;
485 lapb_establish_data_link(lapb);
490 case NETDEV_GOING_DOWN:
491 if (netif_carrier_ok(dev))
492 __lapb_disconnect_request(lapb);
495 lapb_dbg(0, "(%p) Interface down: %s\n", dev, dev->name);
496 lapb_dbg(0, "(%p) S%d -> S0\n", dev, lapb->state);
497 lapb_clear_queues(lapb);
498 lapb->state = LAPB_STATE_0;
500 lapb_stop_t1timer(lapb);
501 lapb_stop_t2timer(lapb);
504 if (netif_carrier_ok(dev)) {
505 lapb_dbg(0, "(%p): Carrier detected: %s\n", dev,
507 if (lapb->mode & LAPB_DCE) {
508 lapb_start_t1timer(lapb);
510 if (lapb->state == LAPB_STATE_0) {
511 lapb->state = LAPB_STATE_1;
512 lapb_establish_data_link(lapb);
516 lapb_dbg(0, "(%p) Carrier lost: %s\n", dev, dev->name);
517 lapb_dbg(0, "(%p) S%d -> S0\n", dev, lapb->state);
518 lapb_clear_queues(lapb);
519 lapb->state = LAPB_STATE_0;
521 lapb_stop_t1timer(lapb);
522 lapb_stop_t2timer(lapb);
527 spin_unlock_bh(&lapb->lock);
532 static struct notifier_block lapb_dev_notifier = {
533 .notifier_call = lapb_device_event,
536 static int __init lapb_init(void)
538 return register_netdevice_notifier(&lapb_dev_notifier);
541 static void __exit lapb_exit(void)
543 WARN_ON(!list_empty(&lapb_list));
545 unregister_netdevice_notifier(&lapb_dev_notifier);
548 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
549 MODULE_DESCRIPTION("The X.25 Link Access Procedure B link layer protocol");
550 MODULE_LICENSE("GPL");
552 module_init(lapb_init);
553 module_exit(lapb_exit);