xref: /linux/net/lapb/lapb_iface.c (revision 4201c9260a8d3c4ef238e51692a7e9b4e1e29efe)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	LAPB release 002
4  *
5  *	This code REQUIRES 2.1.15 or higher/ NET3.038
6  *
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.
11  */
12 
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 
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>
38 
39 static LIST_HEAD(lapb_list);
40 static DEFINE_RWLOCK(lapb_list_lock);
41 
42 /*
43  *	Free an allocated lapb control block.
44  */
45 static void lapb_free_cb(struct lapb_cb *lapb)
46 {
47 	kfree(lapb);
48 }
49 
50 static __inline__ void lapb_hold(struct lapb_cb *lapb)
51 {
52 	refcount_inc(&lapb->refcnt);
53 }
54 
55 static __inline__ void lapb_put(struct lapb_cb *lapb)
56 {
57 	if (refcount_dec_and_test(&lapb->refcnt))
58 		lapb_free_cb(lapb);
59 }
60 
61 /*
62  *	Socket removal during an interrupt is now safe.
63  */
64 static 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 }
71 
72 /*
73  *	Add a socket to the bound sockets list.
74  */
75 static void __lapb_insert_cb(struct lapb_cb *lapb)
76 {
77 	list_add(&lapb->node, &lapb_list);
78 	lapb_hold(lapb);
79 }
80 
81 static struct lapb_cb *__lapb_devtostruct(struct net_device *dev)
82 {
83 	struct list_head *entry;
84 	struct lapb_cb *lapb, *use = NULL;
85 
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;
91 		}
92 	}
93 
94 	if (use)
95 		lapb_hold(use);
96 
97 	return use;
98 }
99 
100 static struct lapb_cb *lapb_devtostruct(struct net_device *dev)
101 {
102 	struct lapb_cb *rc;
103 
104 	read_lock_bh(&lapb_list_lock);
105 	rc = __lapb_devtostruct(dev);
106 	read_unlock_bh(&lapb_list_lock);
107 
108 	return rc;
109 }
110 /*
111  *	Create an empty LAPB control block.
112  */
113 static struct lapb_cb *lapb_create_cb(void)
114 {
115 	struct lapb_cb *lapb = kzalloc(sizeof(*lapb), GFP_ATOMIC);
116 
117 	if (!lapb)
118 		goto out;
119 
120 	skb_queue_head_init(&lapb->write_queue);
121 	skb_queue_head_init(&lapb->ack_queue);
122 
123 	timer_setup(&lapb->t1timer, NULL, 0);
124 	timer_setup(&lapb->t2timer, NULL, 0);
125 
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;
135 }
136 
137 int lapb_register(struct net_device *dev,
138 		  const struct lapb_register_struct *callbacks)
139 {
140 	struct lapb_cb *lapb;
141 	int rc = LAPB_BADTOKEN;
142 
143 	write_lock_bh(&lapb_list_lock);
144 
145 	lapb = __lapb_devtostruct(dev);
146 	if (lapb) {
147 		lapb_put(lapb);
148 		goto out;
149 	}
150 
151 	lapb = lapb_create_cb();
152 	rc = LAPB_NOMEM;
153 	if (!lapb)
154 		goto out;
155 
156 	lapb->dev       = dev;
157 	lapb->callbacks = callbacks;
158 
159 	__lapb_insert_cb(lapb);
160 
161 	lapb_start_t1timer(lapb);
162 
163 	rc = LAPB_OK;
164 out:
165 	write_unlock_bh(&lapb_list_lock);
166 	return rc;
167 }
168 EXPORT_SYMBOL(lapb_register);
169 
170 int lapb_unregister(struct net_device *dev)
171 {
172 	struct lapb_cb *lapb;
173 	int rc = LAPB_BADTOKEN;
174 
175 	write_lock_bh(&lapb_list_lock);
176 	lapb = __lapb_devtostruct(dev);
177 	if (!lapb)
178 		goto out;
179 
180 	lapb_stop_t1timer(lapb);
181 	lapb_stop_t2timer(lapb);
182 
183 	lapb_clear_queues(lapb);
184 
185 	__lapb_remove_cb(lapb);
186 
187 	lapb_put(lapb);
188 	rc = LAPB_OK;
189 out:
190 	write_unlock_bh(&lapb_list_lock);
191 	return rc;
192 }
193 EXPORT_SYMBOL(lapb_unregister);
194 
195 int lapb_getparms(struct net_device *dev, struct lapb_parms_struct *parms)
196 {
197 	int rc = LAPB_BADTOKEN;
198 	struct lapb_cb *lapb = lapb_devtostruct(dev);
199 
200 	if (!lapb)
201 		goto out;
202 
203 	parms->t1      = lapb->t1 / HZ;
204 	parms->t2      = lapb->t2 / HZ;
205 	parms->n2      = lapb->n2;
206 	parms->n2count = lapb->n2count;
207 	parms->state   = lapb->state;
208 	parms->window  = lapb->window;
209 	parms->mode    = lapb->mode;
210 
211 	if (!timer_pending(&lapb->t1timer))
212 		parms->t1timer = 0;
213 	else
214 		parms->t1timer = (lapb->t1timer.expires - jiffies) / HZ;
215 
216 	if (!timer_pending(&lapb->t2timer))
217 		parms->t2timer = 0;
218 	else
219 		parms->t2timer = (lapb->t2timer.expires - jiffies) / HZ;
220 
221 	lapb_put(lapb);
222 	rc = LAPB_OK;
223 out:
224 	return rc;
225 }
226 EXPORT_SYMBOL(lapb_getparms);
227 
228 int lapb_setparms(struct net_device *dev, struct lapb_parms_struct *parms)
229 {
230 	int rc = LAPB_BADTOKEN;
231 	struct lapb_cb *lapb = lapb_devtostruct(dev);
232 
233 	if (!lapb)
234 		goto out;
235 
236 	rc = LAPB_INVALUE;
237 	if (parms->t1 < 1 || parms->t2 < 1 || parms->n2 < 1)
238 		goto out_put;
239 
240 	if (lapb->state == LAPB_STATE_0) {
241 		if (parms->mode & LAPB_EXTENDED) {
242 			if (parms->window < 1 || parms->window > 127)
243 				goto out_put;
244 		} else {
245 			if (parms->window < 1 || parms->window > 7)
246 				goto out_put;
247 		}
248 		lapb->mode    = parms->mode;
249 		lapb->window  = parms->window;
250 	}
251 
252 	lapb->t1    = parms->t1 * HZ;
253 	lapb->t2    = parms->t2 * HZ;
254 	lapb->n2    = parms->n2;
255 
256 	rc = LAPB_OK;
257 out_put:
258 	lapb_put(lapb);
259 out:
260 	return rc;
261 }
262 EXPORT_SYMBOL(lapb_setparms);
263 
264 int lapb_connect_request(struct net_device *dev)
265 {
266 	struct lapb_cb *lapb = lapb_devtostruct(dev);
267 	int rc = LAPB_BADTOKEN;
268 
269 	if (!lapb)
270 		goto out;
271 
272 	rc = LAPB_OK;
273 	if (lapb->state == LAPB_STATE_1)
274 		goto out_put;
275 
276 	rc = LAPB_CONNECTED;
277 	if (lapb->state == LAPB_STATE_3 || lapb->state == LAPB_STATE_4)
278 		goto out_put;
279 
280 	lapb_establish_data_link(lapb);
281 
282 	lapb_dbg(0, "(%p) S0 -> S1\n", lapb->dev);
283 	lapb->state = LAPB_STATE_1;
284 
285 	rc = LAPB_OK;
286 out_put:
287 	lapb_put(lapb);
288 out:
289 	return rc;
290 }
291 EXPORT_SYMBOL(lapb_connect_request);
292 
293 int lapb_disconnect_request(struct net_device *dev)
294 {
295 	struct lapb_cb *lapb = lapb_devtostruct(dev);
296 	int rc = LAPB_BADTOKEN;
297 
298 	if (!lapb)
299 		goto out;
300 
301 	switch (lapb->state) {
302 	case LAPB_STATE_0:
303 		rc = LAPB_NOTCONNECTED;
304 		goto out_put;
305 
306 	case LAPB_STATE_1:
307 		lapb_dbg(1, "(%p) S1 TX DISC(1)\n", lapb->dev);
308 		lapb_dbg(0, "(%p) S1 -> S0\n", lapb->dev);
309 		lapb_send_control(lapb, LAPB_DISC, LAPB_POLLON, LAPB_COMMAND);
310 		lapb->state = LAPB_STATE_0;
311 		lapb_start_t1timer(lapb);
312 		rc = LAPB_NOTCONNECTED;
313 		goto out_put;
314 
315 	case LAPB_STATE_2:
316 		rc = LAPB_OK;
317 		goto out_put;
318 	}
319 
320 	lapb_clear_queues(lapb);
321 	lapb->n2count = 0;
322 	lapb_send_control(lapb, LAPB_DISC, LAPB_POLLON, LAPB_COMMAND);
323 	lapb_start_t1timer(lapb);
324 	lapb_stop_t2timer(lapb);
325 	lapb->state = LAPB_STATE_2;
326 
327 	lapb_dbg(1, "(%p) S3 DISC(1)\n", lapb->dev);
328 	lapb_dbg(0, "(%p) S3 -> S2\n", lapb->dev);
329 
330 	rc = LAPB_OK;
331 out_put:
332 	lapb_put(lapb);
333 out:
334 	return rc;
335 }
336 EXPORT_SYMBOL(lapb_disconnect_request);
337 
338 int lapb_data_request(struct net_device *dev, struct sk_buff *skb)
339 {
340 	struct lapb_cb *lapb = lapb_devtostruct(dev);
341 	int rc = LAPB_BADTOKEN;
342 
343 	if (!lapb)
344 		goto out;
345 
346 	rc = LAPB_NOTCONNECTED;
347 	if (lapb->state != LAPB_STATE_3 && lapb->state != LAPB_STATE_4)
348 		goto out_put;
349 
350 	skb_queue_tail(&lapb->write_queue, skb);
351 	lapb_kick(lapb);
352 	rc = LAPB_OK;
353 out_put:
354 	lapb_put(lapb);
355 out:
356 	return rc;
357 }
358 EXPORT_SYMBOL(lapb_data_request);
359 
360 int lapb_data_received(struct net_device *dev, struct sk_buff *skb)
361 {
362 	struct lapb_cb *lapb = lapb_devtostruct(dev);
363 	int rc = LAPB_BADTOKEN;
364 
365 	if (lapb) {
366 		lapb_data_input(lapb, skb);
367 		lapb_put(lapb);
368 		rc = LAPB_OK;
369 	}
370 
371 	return rc;
372 }
373 EXPORT_SYMBOL(lapb_data_received);
374 
375 void lapb_connect_confirmation(struct lapb_cb *lapb, int reason)
376 {
377 	if (lapb->callbacks->connect_confirmation)
378 		lapb->callbacks->connect_confirmation(lapb->dev, reason);
379 }
380 
381 void lapb_connect_indication(struct lapb_cb *lapb, int reason)
382 {
383 	if (lapb->callbacks->connect_indication)
384 		lapb->callbacks->connect_indication(lapb->dev, reason);
385 }
386 
387 void lapb_disconnect_confirmation(struct lapb_cb *lapb, int reason)
388 {
389 	if (lapb->callbacks->disconnect_confirmation)
390 		lapb->callbacks->disconnect_confirmation(lapb->dev, reason);
391 }
392 
393 void lapb_disconnect_indication(struct lapb_cb *lapb, int reason)
394 {
395 	if (lapb->callbacks->disconnect_indication)
396 		lapb->callbacks->disconnect_indication(lapb->dev, reason);
397 }
398 
399 int lapb_data_indication(struct lapb_cb *lapb, struct sk_buff *skb)
400 {
401 	if (lapb->callbacks->data_indication)
402 		return lapb->callbacks->data_indication(lapb->dev, skb);
403 
404 	kfree_skb(skb);
405 	return NET_RX_SUCCESS; /* For now; must be != NET_RX_DROP */
406 }
407 
408 int lapb_data_transmit(struct lapb_cb *lapb, struct sk_buff *skb)
409 {
410 	int used = 0;
411 
412 	if (lapb->callbacks->data_transmit) {
413 		lapb->callbacks->data_transmit(lapb->dev, skb);
414 		used = 1;
415 	}
416 
417 	return used;
418 }
419 
420 static int __init lapb_init(void)
421 {
422 	return 0;
423 }
424 
425 static void __exit lapb_exit(void)
426 {
427 	WARN_ON(!list_empty(&lapb_list));
428 }
429 
430 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
431 MODULE_DESCRIPTION("The X.25 Link Access Procedure B link layer protocol");
432 MODULE_LICENSE("GPL");
433 
434 module_init(lapb_init);
435 module_exit(lapb_exit);
436