Merge tag 'iommu-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux
[linux/fpc-iii.git] / net / lapb / lapb_iface.c
blob40961889e9c01add01792690e61c16a04647cc4c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * LAPB release 002
5 * This code REQUIRES 2.1.15 or higher/ NET3.038
7 * History
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>
19 #include <linux/in.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>
30 #include <net/sock.h>
31 #include <linux/uaccess.h>
32 #include <linux/fcntl.h>
33 #include <linux/mm.h>
34 #include <linux/interrupt.h>
35 #include <linux/stat.h>
36 #include <linux/init.h>
37 #include <net/lapb.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)
47 kfree(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))
58 lapb_free_cb(lapb);
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);
68 lapb_put(lapb);
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);
78 lapb_hold(lapb);
81 static struct lapb_cb *__lapb_devtostruct(struct net_device *dev)
83 struct list_head *entry;
84 struct lapb_cb *lapb, *use = NULL;
86 list_for_each(entry, &lapb_list) {
87 lapb = list_entry(entry, struct lapb_cb, node);
88 if (lapb->dev == dev) {
89 use = lapb;
90 break;
94 if (use)
95 lapb_hold(use);
97 return use;
100 static struct lapb_cb *lapb_devtostruct(struct net_device *dev)
102 struct lapb_cb *rc;
104 read_lock_bh(&lapb_list_lock);
105 rc = __lapb_devtostruct(dev);
106 read_unlock_bh(&lapb_list_lock);
108 return rc;
111 * Create an empty LAPB control block.
113 static struct lapb_cb *lapb_create_cb(void)
115 struct lapb_cb *lapb = kzalloc(sizeof(*lapb), GFP_ATOMIC);
117 if (!lapb)
118 goto out;
120 skb_queue_head_init(&lapb->write_queue);
121 skb_queue_head_init(&lapb->ack_queue);
123 timer_setup(&lapb->t1timer, NULL, 0);
124 timer_setup(&lapb->t2timer, NULL, 0);
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;
132 refcount_set(&lapb->refcnt, 1);
133 out:
134 return lapb;
137 int lapb_register(struct net_device *dev,
138 const struct lapb_register_struct *callbacks)
140 struct lapb_cb *lapb;
141 int rc = LAPB_BADTOKEN;
143 write_lock_bh(&lapb_list_lock);
145 lapb = __lapb_devtostruct(dev);
146 if (lapb) {
147 lapb_put(lapb);
148 goto out;
151 lapb = lapb_create_cb();
152 rc = LAPB_NOMEM;
153 if (!lapb)
154 goto out;
156 lapb->dev = dev;
157 lapb->callbacks = callbacks;
159 __lapb_insert_cb(lapb);
161 lapb_start_t1timer(lapb);
163 rc = LAPB_OK;
164 out:
165 write_unlock_bh(&lapb_list_lock);
166 return rc;
168 EXPORT_SYMBOL(lapb_register);
170 int lapb_unregister(struct net_device *dev)
172 struct lapb_cb *lapb;
173 int rc = LAPB_BADTOKEN;
175 write_lock_bh(&lapb_list_lock);
176 lapb = __lapb_devtostruct(dev);
177 if (!lapb)
178 goto out;
179 lapb_put(lapb);
181 lapb_stop_t1timer(lapb);
182 lapb_stop_t2timer(lapb);
184 lapb_clear_queues(lapb);
186 __lapb_remove_cb(lapb);
188 lapb_put(lapb);
189 rc = LAPB_OK;
190 out:
191 write_unlock_bh(&lapb_list_lock);
192 return rc;
194 EXPORT_SYMBOL(lapb_unregister);
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);
201 if (!lapb)
202 goto out;
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))
213 parms->t1timer = 0;
214 else
215 parms->t1timer = (lapb->t1timer.expires - jiffies) / HZ;
217 if (!timer_pending(&lapb->t2timer))
218 parms->t2timer = 0;
219 else
220 parms->t2timer = (lapb->t2timer.expires - jiffies) / HZ;
222 lapb_put(lapb);
223 rc = LAPB_OK;
224 out:
225 return rc;
227 EXPORT_SYMBOL(lapb_getparms);
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);
234 if (!lapb)
235 goto out;
237 rc = LAPB_INVALUE;
238 if (parms->t1 < 1 || parms->t2 < 1 || parms->n2 < 1)
239 goto out_put;
241 if (lapb->state == LAPB_STATE_0) {
242 if (parms->mode & LAPB_EXTENDED) {
243 if (parms->window < 1 || parms->window > 127)
244 goto out_put;
245 } else {
246 if (parms->window < 1 || parms->window > 7)
247 goto out_put;
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;
257 rc = LAPB_OK;
258 out_put:
259 lapb_put(lapb);
260 out:
261 return rc;
263 EXPORT_SYMBOL(lapb_setparms);
265 int lapb_connect_request(struct net_device *dev)
267 struct lapb_cb *lapb = lapb_devtostruct(dev);
268 int rc = LAPB_BADTOKEN;
270 if (!lapb)
271 goto out;
273 rc = LAPB_OK;
274 if (lapb->state == LAPB_STATE_1)
275 goto out_put;
277 rc = LAPB_CONNECTED;
278 if (lapb->state == LAPB_STATE_3 || lapb->state == LAPB_STATE_4)
279 goto out_put;
281 lapb_establish_data_link(lapb);
283 lapb_dbg(0, "(%p) S0 -> S1\n", lapb->dev);
284 lapb->state = LAPB_STATE_1;
286 rc = LAPB_OK;
287 out_put:
288 lapb_put(lapb);
289 out:
290 return rc;
292 EXPORT_SYMBOL(lapb_connect_request);
294 int lapb_disconnect_request(struct net_device *dev)
296 struct lapb_cb *lapb = lapb_devtostruct(dev);
297 int rc = LAPB_BADTOKEN;
299 if (!lapb)
300 goto out;
302 switch (lapb->state) {
303 case LAPB_STATE_0:
304 rc = LAPB_NOTCONNECTED;
305 goto out_put;
307 case LAPB_STATE_1:
308 lapb_dbg(1, "(%p) S1 TX DISC(1)\n", lapb->dev);
309 lapb_dbg(0, "(%p) S1 -> S0\n", lapb->dev);
310 lapb_send_control(lapb, LAPB_DISC, LAPB_POLLON, LAPB_COMMAND);
311 lapb->state = LAPB_STATE_0;
312 lapb_start_t1timer(lapb);
313 rc = LAPB_NOTCONNECTED;
314 goto out_put;
316 case LAPB_STATE_2:
317 rc = LAPB_OK;
318 goto out_put;
321 lapb_clear_queues(lapb);
322 lapb->n2count = 0;
323 lapb_send_control(lapb, LAPB_DISC, LAPB_POLLON, LAPB_COMMAND);
324 lapb_start_t1timer(lapb);
325 lapb_stop_t2timer(lapb);
326 lapb->state = LAPB_STATE_2;
328 lapb_dbg(1, "(%p) S3 DISC(1)\n", lapb->dev);
329 lapb_dbg(0, "(%p) S3 -> S2\n", lapb->dev);
331 rc = LAPB_OK;
332 out_put:
333 lapb_put(lapb);
334 out:
335 return rc;
337 EXPORT_SYMBOL(lapb_disconnect_request);
339 int lapb_data_request(struct net_device *dev, struct sk_buff *skb)
341 struct lapb_cb *lapb = lapb_devtostruct(dev);
342 int rc = LAPB_BADTOKEN;
344 if (!lapb)
345 goto out;
347 rc = LAPB_NOTCONNECTED;
348 if (lapb->state != LAPB_STATE_3 && lapb->state != LAPB_STATE_4)
349 goto out_put;
351 skb_queue_tail(&lapb->write_queue, skb);
352 lapb_kick(lapb);
353 rc = LAPB_OK;
354 out_put:
355 lapb_put(lapb);
356 out:
357 return rc;
359 EXPORT_SYMBOL(lapb_data_request);
361 int lapb_data_received(struct net_device *dev, struct sk_buff *skb)
363 struct lapb_cb *lapb = lapb_devtostruct(dev);
364 int rc = LAPB_BADTOKEN;
366 if (lapb) {
367 lapb_data_input(lapb, skb);
368 lapb_put(lapb);
369 rc = LAPB_OK;
372 return rc;
374 EXPORT_SYMBOL(lapb_data_received);
376 void lapb_connect_confirmation(struct lapb_cb *lapb, int reason)
378 if (lapb->callbacks->connect_confirmation)
379 lapb->callbacks->connect_confirmation(lapb->dev, reason);
382 void lapb_connect_indication(struct lapb_cb *lapb, int reason)
384 if (lapb->callbacks->connect_indication)
385 lapb->callbacks->connect_indication(lapb->dev, reason);
388 void lapb_disconnect_confirmation(struct lapb_cb *lapb, int reason)
390 if (lapb->callbacks->disconnect_confirmation)
391 lapb->callbacks->disconnect_confirmation(lapb->dev, reason);
394 void lapb_disconnect_indication(struct lapb_cb *lapb, int reason)
396 if (lapb->callbacks->disconnect_indication)
397 lapb->callbacks->disconnect_indication(lapb->dev, reason);
400 int lapb_data_indication(struct lapb_cb *lapb, struct sk_buff *skb)
402 if (lapb->callbacks->data_indication)
403 return lapb->callbacks->data_indication(lapb->dev, skb);
405 kfree_skb(skb);
406 return NET_RX_SUCCESS; /* For now; must be != NET_RX_DROP */
409 int lapb_data_transmit(struct lapb_cb *lapb, struct sk_buff *skb)
411 int used = 0;
413 if (lapb->callbacks->data_transmit) {
414 lapb->callbacks->data_transmit(lapb->dev, skb);
415 used = 1;
418 return used;
421 /* Handle device status changes. */
422 static int lapb_device_event(struct notifier_block *this, unsigned long event,
423 void *ptr)
425 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
426 struct lapb_cb *lapb;
428 if (!net_eq(dev_net(dev), &init_net))
429 return NOTIFY_DONE;
431 if (dev->type != ARPHRD_X25)
432 return NOTIFY_DONE;
434 lapb = lapb_devtostruct(dev);
435 if (!lapb)
436 return NOTIFY_DONE;
438 switch (event) {
439 case NETDEV_UP:
440 lapb_dbg(0, "(%p) Interface up: %s\n", dev, dev->name);
442 if (netif_carrier_ok(dev)) {
443 lapb_dbg(0, "(%p): Carrier is already up: %s\n", dev,
444 dev->name);
445 if (lapb->mode & LAPB_DCE) {
446 lapb_start_t1timer(lapb);
447 } else {
448 if (lapb->state == LAPB_STATE_0) {
449 lapb->state = LAPB_STATE_1;
450 lapb_establish_data_link(lapb);
454 break;
455 case NETDEV_GOING_DOWN:
456 if (netif_carrier_ok(dev))
457 lapb_disconnect_request(dev);
458 break;
459 case NETDEV_DOWN:
460 lapb_dbg(0, "(%p) Interface down: %s\n", dev, dev->name);
461 lapb_dbg(0, "(%p) S%d -> S0\n", dev, lapb->state);
462 lapb_clear_queues(lapb);
463 lapb->state = LAPB_STATE_0;
464 lapb->n2count = 0;
465 lapb_stop_t1timer(lapb);
466 lapb_stop_t2timer(lapb);
467 break;
468 case NETDEV_CHANGE:
469 if (netif_carrier_ok(dev)) {
470 lapb_dbg(0, "(%p): Carrier detected: %s\n", dev,
471 dev->name);
472 if (lapb->mode & LAPB_DCE) {
473 lapb_start_t1timer(lapb);
474 } else {
475 if (lapb->state == LAPB_STATE_0) {
476 lapb->state = LAPB_STATE_1;
477 lapb_establish_data_link(lapb);
480 } else {
481 lapb_dbg(0, "(%p) Carrier lost: %s\n", dev, dev->name);
482 lapb_dbg(0, "(%p) S%d -> S0\n", dev, lapb->state);
483 lapb_clear_queues(lapb);
484 lapb->state = LAPB_STATE_0;
485 lapb->n2count = 0;
486 lapb_stop_t1timer(lapb);
487 lapb_stop_t2timer(lapb);
489 break;
492 lapb_put(lapb);
493 return NOTIFY_DONE;
496 static struct notifier_block lapb_dev_notifier = {
497 .notifier_call = lapb_device_event,
500 static int __init lapb_init(void)
502 return register_netdevice_notifier(&lapb_dev_notifier);
505 static void __exit lapb_exit(void)
507 WARN_ON(!list_empty(&lapb_list));
509 unregister_netdevice_notifier(&lapb_dev_notifier);
512 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
513 MODULE_DESCRIPTION("The X.25 Link Access Procedure B link layer protocol");
514 MODULE_LICENSE("GPL");
516 module_init(lapb_init);
517 module_exit(lapb_exit);