blob: cdb913e7627e078f80d70835bccad23c3aa4bdff [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * LAPB release 002
3 *
4 * This code REQUIRES 2.1.15 or higher/ NET3.038
5 *
6 * This module:
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.
11 *
12 * History
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.
16 */
YOSHIFUJI Hideaki56d6c3d2007-02-09 23:24:59 +090017
Joe Perchesa508da62012-05-17 10:25:49 +000018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/module.h>
21#include <linux/errno.h>
22#include <linux/types.h>
23#include <linux/socket.h>
24#include <linux/in.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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <net/sock.h>
36#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/fcntl.h>
38#include <linux/mm.h>
39#include <linux/interrupt.h>
40#include <linux/stat.h>
41#include <linux/init.h>
42#include <net/lapb.h>
43
Denis Cheng14d0e7b2007-12-07 00:50:15 -080044static LIST_HEAD(lapb_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static DEFINE_RWLOCK(lapb_list_lock);
46
47/*
YOSHIFUJI Hideaki56d6c3d2007-02-09 23:24:59 +090048 * Free an allocated lapb control block.
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 */
50static void lapb_free_cb(struct lapb_cb *lapb)
51{
52 kfree(lapb);
53}
54
55static __inline__ void lapb_hold(struct lapb_cb *lapb)
56{
57 atomic_inc(&lapb->refcnt);
58}
59
60static __inline__ void lapb_put(struct lapb_cb *lapb)
61{
62 if (atomic_dec_and_test(&lapb->refcnt))
63 lapb_free_cb(lapb);
64}
65
66/*
67 * Socket removal during an interrupt is now safe.
68 */
69static void __lapb_remove_cb(struct lapb_cb *lapb)
70{
71 if (lapb->node.next) {
72 list_del(&lapb->node);
73 lapb_put(lapb);
74 }
75}
Fabian Frederick75da1462014-10-22 21:01:41 +020076EXPORT_SYMBOL(lapb_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/*
79 * Add a socket to the bound sockets list.
80 */
81static void __lapb_insert_cb(struct lapb_cb *lapb)
82{
83 list_add(&lapb->node, &lapb_list);
84 lapb_hold(lapb);
85}
86
87static struct lapb_cb *__lapb_devtostruct(struct net_device *dev)
88{
89 struct list_head *entry;
90 struct lapb_cb *lapb, *use = NULL;
91
92 list_for_each(entry, &lapb_list) {
93 lapb = list_entry(entry, struct lapb_cb, node);
94 if (lapb->dev == dev) {
95 use = lapb;
96 break;
97 }
98 }
99
100 if (use)
101 lapb_hold(use);
102
103 return use;
104}
105
106static struct lapb_cb *lapb_devtostruct(struct net_device *dev)
107{
108 struct lapb_cb *rc;
109
110 read_lock_bh(&lapb_list_lock);
111 rc = __lapb_devtostruct(dev);
112 read_unlock_bh(&lapb_list_lock);
113
114 return rc;
115}
116/*
117 * Create an empty LAPB control block.
118 */
119static struct lapb_cb *lapb_create_cb(void)
120{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700121 struct lapb_cb *lapb = kzalloc(sizeof(*lapb), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123
124 if (!lapb)
125 goto out;
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 skb_queue_head_init(&lapb->write_queue);
128 skb_queue_head_init(&lapb->ack_queue);
129
130 init_timer(&lapb->t1timer);
131 init_timer(&lapb->t2timer);
132
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);
140out:
141 return lapb;
142}
143
stephen hemmingerd97a0772011-09-16 11:04:29 +0000144int lapb_register(struct net_device *dev,
145 const struct lapb_register_struct *callbacks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 struct lapb_cb *lapb;
148 int rc = LAPB_BADTOKEN;
149
150 write_lock_bh(&lapb_list_lock);
151
152 lapb = __lapb_devtostruct(dev);
153 if (lapb) {
154 lapb_put(lapb);
155 goto out;
156 }
157
158 lapb = lapb_create_cb();
159 rc = LAPB_NOMEM;
160 if (!lapb)
161 goto out;
162
163 lapb->dev = dev;
stephen hemmingerd97a0772011-09-16 11:04:29 +0000164 lapb->callbacks = callbacks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 __lapb_insert_cb(lapb);
167
168 lapb_start_t1timer(lapb);
169
170 rc = LAPB_OK;
171out:
172 write_unlock_bh(&lapb_list_lock);
173 return rc;
174}
175
176int lapb_unregister(struct net_device *dev)
177{
178 struct lapb_cb *lapb;
179 int rc = LAPB_BADTOKEN;
180
181 write_lock_bh(&lapb_list_lock);
182 lapb = __lapb_devtostruct(dev);
183 if (!lapb)
184 goto out;
Jeremy Sowden3c77a1c2019-06-16 16:54:37 +0100185 lapb_put(lapb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 lapb_stop_t1timer(lapb);
188 lapb_stop_t2timer(lapb);
189
190 lapb_clear_queues(lapb);
191
192 __lapb_remove_cb(lapb);
193
194 lapb_put(lapb);
195 rc = LAPB_OK;
196out:
197 write_unlock_bh(&lapb_list_lock);
198 return rc;
199}
Fabian Frederick75da1462014-10-22 21:01:41 +0200200EXPORT_SYMBOL(lapb_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202int lapb_getparms(struct net_device *dev, struct lapb_parms_struct *parms)
203{
204 int rc = LAPB_BADTOKEN;
205 struct lapb_cb *lapb = lapb_devtostruct(dev);
206
207 if (!lapb)
208 goto out;
209
210 parms->t1 = lapb->t1 / HZ;
211 parms->t2 = lapb->t2 / HZ;
212 parms->n2 = lapb->n2;
213 parms->n2count = lapb->n2count;
214 parms->state = lapb->state;
215 parms->window = lapb->window;
216 parms->mode = lapb->mode;
217
218 if (!timer_pending(&lapb->t1timer))
219 parms->t1timer = 0;
220 else
221 parms->t1timer = (lapb->t1timer.expires - jiffies) / HZ;
222
223 if (!timer_pending(&lapb->t2timer))
224 parms->t2timer = 0;
225 else
226 parms->t2timer = (lapb->t2timer.expires - jiffies) / HZ;
227
228 lapb_put(lapb);
229 rc = LAPB_OK;
230out:
231 return rc;
232}
Fabian Frederick75da1462014-10-22 21:01:41 +0200233EXPORT_SYMBOL(lapb_getparms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235int lapb_setparms(struct net_device *dev, struct lapb_parms_struct *parms)
236{
237 int rc = LAPB_BADTOKEN;
238 struct lapb_cb *lapb = lapb_devtostruct(dev);
239
240 if (!lapb)
241 goto out;
242
243 rc = LAPB_INVALUE;
244 if (parms->t1 < 1 || parms->t2 < 1 || parms->n2 < 1)
245 goto out_put;
246
247 if (lapb->state == LAPB_STATE_0) {
Diego Calleja558e10a2006-08-05 21:15:58 -0700248 if (parms->mode & LAPB_EXTENDED) {
249 if (parms->window < 1 || parms->window > 127)
250 goto out_put;
251 } else {
252 if (parms->window < 1 || parms->window > 7)
253 goto out_put;
254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 lapb->mode = parms->mode;
256 lapb->window = parms->window;
257 }
258
259 lapb->t1 = parms->t1 * HZ;
260 lapb->t2 = parms->t2 * HZ;
261 lapb->n2 = parms->n2;
262
263 rc = LAPB_OK;
264out_put:
265 lapb_put(lapb);
266out:
267 return rc;
268}
Fabian Frederick75da1462014-10-22 21:01:41 +0200269EXPORT_SYMBOL(lapb_setparms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271int lapb_connect_request(struct net_device *dev)
272{
273 struct lapb_cb *lapb = lapb_devtostruct(dev);
274 int rc = LAPB_BADTOKEN;
275
276 if (!lapb)
277 goto out;
278
279 rc = LAPB_OK;
280 if (lapb->state == LAPB_STATE_1)
281 goto out_put;
282
283 rc = LAPB_CONNECTED;
284 if (lapb->state == LAPB_STATE_3 || lapb->state == LAPB_STATE_4)
285 goto out_put;
286
287 lapb_establish_data_link(lapb);
288
Joe Perchesa508da62012-05-17 10:25:49 +0000289 lapb_dbg(0, "(%p) S0 -> S1\n", lapb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 lapb->state = LAPB_STATE_1;
291
292 rc = LAPB_OK;
293out_put:
294 lapb_put(lapb);
295out:
296 return rc;
297}
Fabian Frederick75da1462014-10-22 21:01:41 +0200298EXPORT_SYMBOL(lapb_connect_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300int lapb_disconnect_request(struct net_device *dev)
301{
302 struct lapb_cb *lapb = lapb_devtostruct(dev);
303 int rc = LAPB_BADTOKEN;
304
305 if (!lapb)
306 goto out;
307
308 switch (lapb->state) {
Joe Perchesfcb261f2011-07-01 09:43:09 +0000309 case LAPB_STATE_0:
310 rc = LAPB_NOTCONNECTED;
311 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Joe Perchesfcb261f2011-07-01 09:43:09 +0000313 case LAPB_STATE_1:
Joe Perchesa508da62012-05-17 10:25:49 +0000314 lapb_dbg(1, "(%p) S1 TX DISC(1)\n", lapb->dev);
315 lapb_dbg(0, "(%p) S1 -> S0\n", lapb->dev);
Joe Perchesfcb261f2011-07-01 09:43:09 +0000316 lapb_send_control(lapb, LAPB_DISC, LAPB_POLLON, LAPB_COMMAND);
317 lapb->state = LAPB_STATE_0;
318 lapb_start_t1timer(lapb);
319 rc = LAPB_NOTCONNECTED;
320 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Joe Perchesfcb261f2011-07-01 09:43:09 +0000322 case LAPB_STATE_2:
323 rc = LAPB_OK;
324 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326
327 lapb_clear_queues(lapb);
328 lapb->n2count = 0;
329 lapb_send_control(lapb, LAPB_DISC, LAPB_POLLON, LAPB_COMMAND);
330 lapb_start_t1timer(lapb);
331 lapb_stop_t2timer(lapb);
332 lapb->state = LAPB_STATE_2;
333
Joe Perchesa508da62012-05-17 10:25:49 +0000334 lapb_dbg(1, "(%p) S3 DISC(1)\n", lapb->dev);
335 lapb_dbg(0, "(%p) S3 -> S2\n", lapb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 rc = LAPB_OK;
338out_put:
339 lapb_put(lapb);
340out:
341 return rc;
342}
Fabian Frederick75da1462014-10-22 21:01:41 +0200343EXPORT_SYMBOL(lapb_disconnect_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345int lapb_data_request(struct net_device *dev, struct sk_buff *skb)
346{
347 struct lapb_cb *lapb = lapb_devtostruct(dev);
348 int rc = LAPB_BADTOKEN;
349
350 if (!lapb)
351 goto out;
352
353 rc = LAPB_NOTCONNECTED;
354 if (lapb->state != LAPB_STATE_3 && lapb->state != LAPB_STATE_4)
355 goto out_put;
356
357 skb_queue_tail(&lapb->write_queue, skb);
358 lapb_kick(lapb);
359 rc = LAPB_OK;
360out_put:
361 lapb_put(lapb);
362out:
363 return rc;
364}
Fabian Frederick75da1462014-10-22 21:01:41 +0200365EXPORT_SYMBOL(lapb_data_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367int lapb_data_received(struct net_device *dev, struct sk_buff *skb)
368{
369 struct lapb_cb *lapb = lapb_devtostruct(dev);
370 int rc = LAPB_BADTOKEN;
371
372 if (lapb) {
373 lapb_data_input(lapb, skb);
374 lapb_put(lapb);
375 rc = LAPB_OK;
376 }
377
378 return rc;
379}
Fabian Frederick75da1462014-10-22 21:01:41 +0200380EXPORT_SYMBOL(lapb_data_received);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382void lapb_connect_confirmation(struct lapb_cb *lapb, int reason)
383{
stephen hemmingerd97a0772011-09-16 11:04:29 +0000384 if (lapb->callbacks->connect_confirmation)
385 lapb->callbacks->connect_confirmation(lapb->dev, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
388void lapb_connect_indication(struct lapb_cb *lapb, int reason)
389{
stephen hemmingerd97a0772011-09-16 11:04:29 +0000390 if (lapb->callbacks->connect_indication)
391 lapb->callbacks->connect_indication(lapb->dev, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394void lapb_disconnect_confirmation(struct lapb_cb *lapb, int reason)
395{
stephen hemmingerd97a0772011-09-16 11:04:29 +0000396 if (lapb->callbacks->disconnect_confirmation)
397 lapb->callbacks->disconnect_confirmation(lapb->dev, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400void lapb_disconnect_indication(struct lapb_cb *lapb, int reason)
401{
stephen hemmingerd97a0772011-09-16 11:04:29 +0000402 if (lapb->callbacks->disconnect_indication)
403 lapb->callbacks->disconnect_indication(lapb->dev, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406int lapb_data_indication(struct lapb_cb *lapb, struct sk_buff *skb)
407{
stephen hemmingerd97a0772011-09-16 11:04:29 +0000408 if (lapb->callbacks->data_indication)
409 return lapb->callbacks->data_indication(lapb->dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 kfree_skb(skb);
Florian Westphal0e8635a2009-06-20 00:53:25 +0000412 return NET_RX_SUCCESS; /* For now; must be != NET_RX_DROP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
415int lapb_data_transmit(struct lapb_cb *lapb, struct sk_buff *skb)
416{
417 int used = 0;
418
stephen hemmingerd97a0772011-09-16 11:04:29 +0000419 if (lapb->callbacks->data_transmit) {
420 lapb->callbacks->data_transmit(lapb->dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 used = 1;
422 }
423
424 return used;
425}
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427static int __init lapb_init(void)
428{
429 return 0;
430}
431
432static void __exit lapb_exit(void)
433{
434 WARN_ON(!list_empty(&lapb_list));
435}
436
437MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
438MODULE_DESCRIPTION("The X.25 Link Access Procedure B link layer protocol");
439MODULE_LICENSE("GPL");
440
441module_init(lapb_init);
442module_exit(lapb_exit);