blob: 03f0cd872dcec1596b413e41df89f3ee256bf153 [file] [log] [blame]
Thomas Gleixneree5d8f42019-05-20 19:08:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * LAPB release 002
4 *
5 * This code REQUIRES 2.1.15 or higher/ NET3.038
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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.
11 */
YOSHIFUJI Hideaki56d6c3d2007-02-09 23:24:59 +090012
Joe Perchesa508da62012-05-17 10:25:49 +000013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <net/sock.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080031#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#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>
38
Denis Cheng14d0e7b2007-12-07 00:50:15 -080039static LIST_HEAD(lapb_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static DEFINE_RWLOCK(lapb_list_lock);
41
42/*
YOSHIFUJI Hideaki56d6c3d2007-02-09 23:24:59 +090043 * Free an allocated lapb control block.
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 */
45static void lapb_free_cb(struct lapb_cb *lapb)
46{
47 kfree(lapb);
48}
49
50static __inline__ void lapb_hold(struct lapb_cb *lapb)
51{
Reshetova, Elena0408c582017-07-04 15:53:08 +030052 refcount_inc(&lapb->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
55static __inline__ void lapb_put(struct lapb_cb *lapb)
56{
Reshetova, Elena0408c582017-07-04 15:53:08 +030057 if (refcount_dec_and_test(&lapb->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 lapb_free_cb(lapb);
59}
60
61/*
62 * Socket removal during an interrupt is now safe.
63 */
64static void __lapb_remove_cb(struct lapb_cb *lapb)
65{
66 if (lapb->node.next) {
67 list_del(&lapb->node);
68 lapb_put(lapb);
69 }
70}
Fabian Frederick75da1462014-10-22 21:01:41 +020071EXPORT_SYMBOL(lapb_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/*
74 * Add a socket to the bound sockets list.
75 */
76static void __lapb_insert_cb(struct lapb_cb *lapb)
77{
78 list_add(&lapb->node, &lapb_list);
79 lapb_hold(lapb);
80}
81
82static struct lapb_cb *__lapb_devtostruct(struct net_device *dev)
83{
84 struct list_head *entry;
85 struct lapb_cb *lapb, *use = NULL;
86
87 list_for_each(entry, &lapb_list) {
88 lapb = list_entry(entry, struct lapb_cb, node);
89 if (lapb->dev == dev) {
90 use = lapb;
91 break;
92 }
93 }
94
95 if (use)
96 lapb_hold(use);
97
98 return use;
99}
100
101static struct lapb_cb *lapb_devtostruct(struct net_device *dev)
102{
103 struct lapb_cb *rc;
104
105 read_lock_bh(&lapb_list_lock);
106 rc = __lapb_devtostruct(dev);
107 read_unlock_bh(&lapb_list_lock);
108
109 return rc;
110}
111/*
112 * Create an empty LAPB control block.
113 */
114static struct lapb_cb *lapb_create_cb(void)
115{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700116 struct lapb_cb *lapb = kzalloc(sizeof(*lapb), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118
119 if (!lapb)
120 goto out;
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 skb_queue_head_init(&lapb->write_queue);
123 skb_queue_head_init(&lapb->ack_queue);
124
Kees Cook83a37b32017-10-16 17:28:46 -0700125 timer_setup(&lapb->t1timer, NULL, 0);
126 timer_setup(&lapb->t2timer, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 lapb->t1 = LAPB_DEFAULT_T1;
129 lapb->t2 = LAPB_DEFAULT_T2;
130 lapb->n2 = LAPB_DEFAULT_N2;
131 lapb->mode = LAPB_DEFAULT_MODE;
132 lapb->window = LAPB_DEFAULT_WINDOW;
133 lapb->state = LAPB_STATE_0;
Reshetova, Elena0408c582017-07-04 15:53:08 +0300134 refcount_set(&lapb->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135out:
136 return lapb;
137}
138
stephen hemmingerd97a0772011-09-16 11:04:29 +0000139int lapb_register(struct net_device *dev,
140 const struct lapb_register_struct *callbacks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 struct lapb_cb *lapb;
143 int rc = LAPB_BADTOKEN;
144
145 write_lock_bh(&lapb_list_lock);
146
147 lapb = __lapb_devtostruct(dev);
148 if (lapb) {
149 lapb_put(lapb);
150 goto out;
151 }
152
153 lapb = lapb_create_cb();
154 rc = LAPB_NOMEM;
155 if (!lapb)
156 goto out;
157
158 lapb->dev = dev;
stephen hemmingerd97a0772011-09-16 11:04:29 +0000159 lapb->callbacks = callbacks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 __lapb_insert_cb(lapb);
162
163 lapb_start_t1timer(lapb);
164
165 rc = LAPB_OK;
166out:
167 write_unlock_bh(&lapb_list_lock);
168 return rc;
169}
170
171int lapb_unregister(struct net_device *dev)
172{
173 struct lapb_cb *lapb;
174 int rc = LAPB_BADTOKEN;
175
176 write_lock_bh(&lapb_list_lock);
177 lapb = __lapb_devtostruct(dev);
178 if (!lapb)
179 goto out;
180
181 lapb_stop_t1timer(lapb);
182 lapb_stop_t2timer(lapb);
183
184 lapb_clear_queues(lapb);
185
186 __lapb_remove_cb(lapb);
187
188 lapb_put(lapb);
189 rc = LAPB_OK;
190out:
191 write_unlock_bh(&lapb_list_lock);
192 return rc;
193}
Fabian Frederick75da1462014-10-22 21:01:41 +0200194EXPORT_SYMBOL(lapb_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196int lapb_getparms(struct net_device *dev, struct lapb_parms_struct *parms)
197{
198 int rc = LAPB_BADTOKEN;
199 struct lapb_cb *lapb = lapb_devtostruct(dev);
200
201 if (!lapb)
202 goto out;
203
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;
211
212 if (!timer_pending(&lapb->t1timer))
213 parms->t1timer = 0;
214 else
215 parms->t1timer = (lapb->t1timer.expires - jiffies) / HZ;
216
217 if (!timer_pending(&lapb->t2timer))
218 parms->t2timer = 0;
219 else
220 parms->t2timer = (lapb->t2timer.expires - jiffies) / HZ;
221
222 lapb_put(lapb);
223 rc = LAPB_OK;
224out:
225 return rc;
226}
Fabian Frederick75da1462014-10-22 21:01:41 +0200227EXPORT_SYMBOL(lapb_getparms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229int lapb_setparms(struct net_device *dev, struct lapb_parms_struct *parms)
230{
231 int rc = LAPB_BADTOKEN;
232 struct lapb_cb *lapb = lapb_devtostruct(dev);
233
234 if (!lapb)
235 goto out;
236
237 rc = LAPB_INVALUE;
238 if (parms->t1 < 1 || parms->t2 < 1 || parms->n2 < 1)
239 goto out_put;
240
241 if (lapb->state == LAPB_STATE_0) {
Diego Calleja558e10a2006-08-05 21:15:58 -0700242 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;
248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 lapb->mode = parms->mode;
250 lapb->window = parms->window;
251 }
252
253 lapb->t1 = parms->t1 * HZ;
254 lapb->t2 = parms->t2 * HZ;
255 lapb->n2 = parms->n2;
256
257 rc = LAPB_OK;
258out_put:
259 lapb_put(lapb);
260out:
261 return rc;
262}
Fabian Frederick75da1462014-10-22 21:01:41 +0200263EXPORT_SYMBOL(lapb_setparms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265int lapb_connect_request(struct net_device *dev)
266{
267 struct lapb_cb *lapb = lapb_devtostruct(dev);
268 int rc = LAPB_BADTOKEN;
269
270 if (!lapb)
271 goto out;
272
273 rc = LAPB_OK;
274 if (lapb->state == LAPB_STATE_1)
275 goto out_put;
276
277 rc = LAPB_CONNECTED;
278 if (lapb->state == LAPB_STATE_3 || lapb->state == LAPB_STATE_4)
279 goto out_put;
280
281 lapb_establish_data_link(lapb);
282
Joe Perchesa508da62012-05-17 10:25:49 +0000283 lapb_dbg(0, "(%p) S0 -> S1\n", lapb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 lapb->state = LAPB_STATE_1;
285
286 rc = LAPB_OK;
287out_put:
288 lapb_put(lapb);
289out:
290 return rc;
291}
Fabian Frederick75da1462014-10-22 21:01:41 +0200292EXPORT_SYMBOL(lapb_connect_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294int lapb_disconnect_request(struct net_device *dev)
295{
296 struct lapb_cb *lapb = lapb_devtostruct(dev);
297 int rc = LAPB_BADTOKEN;
298
299 if (!lapb)
300 goto out;
301
302 switch (lapb->state) {
Joe Perchesfcb261f2011-07-01 09:43:09 +0000303 case LAPB_STATE_0:
304 rc = LAPB_NOTCONNECTED;
305 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Joe Perchesfcb261f2011-07-01 09:43:09 +0000307 case LAPB_STATE_1:
Joe Perchesa508da62012-05-17 10:25:49 +0000308 lapb_dbg(1, "(%p) S1 TX DISC(1)\n", lapb->dev);
309 lapb_dbg(0, "(%p) S1 -> S0\n", lapb->dev);
Joe Perchesfcb261f2011-07-01 09:43:09 +0000310 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Joe Perchesfcb261f2011-07-01 09:43:09 +0000316 case LAPB_STATE_2:
317 rc = LAPB_OK;
318 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320
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;
327
Joe Perchesa508da62012-05-17 10:25:49 +0000328 lapb_dbg(1, "(%p) S3 DISC(1)\n", lapb->dev);
329 lapb_dbg(0, "(%p) S3 -> S2\n", lapb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 rc = LAPB_OK;
332out_put:
333 lapb_put(lapb);
334out:
335 return rc;
336}
Fabian Frederick75da1462014-10-22 21:01:41 +0200337EXPORT_SYMBOL(lapb_disconnect_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339int lapb_data_request(struct net_device *dev, struct sk_buff *skb)
340{
341 struct lapb_cb *lapb = lapb_devtostruct(dev);
342 int rc = LAPB_BADTOKEN;
343
344 if (!lapb)
345 goto out;
346
347 rc = LAPB_NOTCONNECTED;
348 if (lapb->state != LAPB_STATE_3 && lapb->state != LAPB_STATE_4)
349 goto out_put;
350
351 skb_queue_tail(&lapb->write_queue, skb);
352 lapb_kick(lapb);
353 rc = LAPB_OK;
354out_put:
355 lapb_put(lapb);
356out:
357 return rc;
358}
Fabian Frederick75da1462014-10-22 21:01:41 +0200359EXPORT_SYMBOL(lapb_data_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361int lapb_data_received(struct net_device *dev, struct sk_buff *skb)
362{
363 struct lapb_cb *lapb = lapb_devtostruct(dev);
364 int rc = LAPB_BADTOKEN;
365
366 if (lapb) {
367 lapb_data_input(lapb, skb);
368 lapb_put(lapb);
369 rc = LAPB_OK;
370 }
371
372 return rc;
373}
Fabian Frederick75da1462014-10-22 21:01:41 +0200374EXPORT_SYMBOL(lapb_data_received);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376void lapb_connect_confirmation(struct lapb_cb *lapb, int reason)
377{
stephen hemmingerd97a0772011-09-16 11:04:29 +0000378 if (lapb->callbacks->connect_confirmation)
379 lapb->callbacks->connect_confirmation(lapb->dev, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
382void lapb_connect_indication(struct lapb_cb *lapb, int reason)
383{
stephen hemmingerd97a0772011-09-16 11:04:29 +0000384 if (lapb->callbacks->connect_indication)
385 lapb->callbacks->connect_indication(lapb->dev, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
388void lapb_disconnect_confirmation(struct lapb_cb *lapb, int reason)
389{
stephen hemmingerd97a0772011-09-16 11:04:29 +0000390 if (lapb->callbacks->disconnect_confirmation)
391 lapb->callbacks->disconnect_confirmation(lapb->dev, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394void lapb_disconnect_indication(struct lapb_cb *lapb, int reason)
395{
stephen hemmingerd97a0772011-09-16 11:04:29 +0000396 if (lapb->callbacks->disconnect_indication)
397 lapb->callbacks->disconnect_indication(lapb->dev, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400int lapb_data_indication(struct lapb_cb *lapb, struct sk_buff *skb)
401{
stephen hemmingerd97a0772011-09-16 11:04:29 +0000402 if (lapb->callbacks->data_indication)
403 return lapb->callbacks->data_indication(lapb->dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 kfree_skb(skb);
Florian Westphal0e8635a2009-06-20 00:53:25 +0000406 return NET_RX_SUCCESS; /* For now; must be != NET_RX_DROP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409int lapb_data_transmit(struct lapb_cb *lapb, struct sk_buff *skb)
410{
411 int used = 0;
412
stephen hemmingerd97a0772011-09-16 11:04:29 +0000413 if (lapb->callbacks->data_transmit) {
414 lapb->callbacks->data_transmit(lapb->dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 used = 1;
416 }
417
418 return used;
419}
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421static int __init lapb_init(void)
422{
423 return 0;
424}
425
426static void __exit lapb_exit(void)
427{
428 WARN_ON(!list_empty(&lapb_list));
429}
430
431MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
432MODULE_DESCRIPTION("The X.25 Link Access Procedure B link layer protocol");
433MODULE_LICENSE("GPL");
434
435module_init(lapb_init);
436module_exit(lapb_exit);