1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	"LAPB via ethernet" driver release 001
4  *
5  *	This code REQUIRES 2.1.15 or higher/ NET3.038
6  *
7  *	This is a "pseudo" network driver to allow LAPB over Ethernet.
8  *
9  *	This driver can use any ethernet destination address, and can be
10  *	limited to accept frames from one dedicated ethernet card only.
11  *
12  *	History
13  *	LAPBETH 001	Jonathan Naylor		Cloned from bpqether.c
14  *	2000-10-29	Henner Eisen	lapb_data_indication() return status.
15  *	2000-11-14	Henner Eisen	dev_hold/put, NETDEV_GOING_DOWN support
16  */
17 
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 
20 #include <linux/errno.h>
21 #include <linux/types.h>
22 #include <linux/socket.h>
23 #include <linux/in.h>
24 #include <linux/slab.h>
25 #include <linux/kernel.h>
26 #include <linux/string.h>
27 #include <linux/net.h>
28 #include <linux/inet.h>
29 #include <linux/netdevice.h>
30 #include <linux/if_arp.h>
31 #include <linux/skbuff.h>
32 #include <net/sock.h>
33 #include <linux/uaccess.h>
34 #include <linux/mm.h>
35 #include <linux/interrupt.h>
36 #include <linux/notifier.h>
37 #include <linux/stat.h>
38 #include <linux/module.h>
39 #include <linux/lapb.h>
40 #include <linux/init.h>
41 
42 #include <net/netdev_lock.h>
43 #include <net/x25device.h>
44 
45 static const u8 bcast_addr[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
46 
47 /* If this number is made larger, check that the temporary string buffer
48  * in lapbeth_new_device is large enough to store the probe device name.
49  */
50 #define MAXLAPBDEV 100
51 
52 struct lapbethdev {
53 	struct list_head	node;
54 	struct net_device	*ethdev;	/* link to ethernet device */
55 	struct net_device	*axdev;		/* lapbeth device (lapb#) */
56 	bool			up;
57 	spinlock_t		up_lock;	/* Protects "up" */
58 	struct sk_buff_head	rx_queue;
59 	struct napi_struct	napi;
60 };
61 
62 static LIST_HEAD(lapbeth_devices);
63 
64 static void lapbeth_connected(struct net_device *dev, int reason);
65 static void lapbeth_disconnected(struct net_device *dev, int reason);
66 
67 /* ------------------------------------------------------------------------ */
68 
69 /*	Get the LAPB device for the ethernet device
70  */
lapbeth_get_x25_dev(struct net_device * dev)71 static struct lapbethdev *lapbeth_get_x25_dev(struct net_device *dev)
72 {
73 	struct lapbethdev *lapbeth;
74 
75 	list_for_each_entry_rcu(lapbeth, &lapbeth_devices, node, lockdep_rtnl_is_held()) {
76 		if (lapbeth->ethdev == dev)
77 			return lapbeth;
78 	}
79 	return NULL;
80 }
81 
dev_is_ethdev(struct net_device * dev)82 static __inline__ int dev_is_ethdev(struct net_device *dev)
83 {
84 	return dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5);
85 }
86 
87 /* ------------------------------------------------------------------------ */
88 
lapbeth_napi_poll(struct napi_struct * napi,int budget)89 static int lapbeth_napi_poll(struct napi_struct *napi, int budget)
90 {
91 	struct lapbethdev *lapbeth = container_of(napi, struct lapbethdev,
92 						  napi);
93 	struct sk_buff *skb;
94 	int processed = 0;
95 
96 	for (; processed < budget; ++processed) {
97 		skb = skb_dequeue(&lapbeth->rx_queue);
98 		if (!skb)
99 			break;
100 		netif_receive_skb_core(skb);
101 	}
102 
103 	if (processed < budget)
104 		napi_complete(napi);
105 
106 	return processed;
107 }
108 
109 /*	Receive a LAPB frame via an ethernet interface.
110  */
lapbeth_rcv(struct sk_buff * skb,struct net_device * dev,struct packet_type * ptype,struct net_device * orig_dev)111 static int lapbeth_rcv(struct sk_buff *skb, struct net_device *dev,
112 		       struct packet_type *ptype, struct net_device *orig_dev)
113 {
114 	int len, err;
115 	struct lapbethdev *lapbeth;
116 
117 	if (dev_net(dev) != &init_net)
118 		goto drop;
119 
120 	skb = skb_share_check(skb, GFP_ATOMIC);
121 	if (!skb)
122 		return NET_RX_DROP;
123 
124 	if (!pskb_may_pull(skb, 2))
125 		goto drop;
126 
127 	rcu_read_lock();
128 	lapbeth = lapbeth_get_x25_dev(dev);
129 	if (!lapbeth)
130 		goto drop_unlock_rcu;
131 	spin_lock_bh(&lapbeth->up_lock);
132 	if (!lapbeth->up)
133 		goto drop_unlock;
134 
135 	len = skb->data[0] + skb->data[1] * 256;
136 	dev->stats.rx_packets++;
137 	dev->stats.rx_bytes += len;
138 
139 	skb_pull(skb, 2);	/* Remove the length bytes */
140 	skb_trim(skb, len);	/* Set the length of the data */
141 
142 	err = lapb_data_received(lapbeth->axdev, skb);
143 	if (err != LAPB_OK) {
144 		printk(KERN_DEBUG "lapbether: lapb_data_received err - %d\n", err);
145 		goto drop_unlock;
146 	}
147 out:
148 	spin_unlock_bh(&lapbeth->up_lock);
149 	rcu_read_unlock();
150 	return 0;
151 drop_unlock:
152 	kfree_skb(skb);
153 	goto out;
154 drop_unlock_rcu:
155 	rcu_read_unlock();
156 drop:
157 	kfree_skb(skb);
158 	return 0;
159 }
160 
lapbeth_data_indication(struct net_device * dev,struct sk_buff * skb)161 static int lapbeth_data_indication(struct net_device *dev, struct sk_buff *skb)
162 {
163 	struct lapbethdev *lapbeth = netdev_priv(dev);
164 	unsigned char *ptr;
165 
166 	if (skb_cow(skb, 1)) {
167 		kfree_skb(skb);
168 		return NET_RX_DROP;
169 	}
170 
171 	skb_push(skb, 1);
172 
173 	ptr  = skb->data;
174 	*ptr = X25_IFACE_DATA;
175 
176 	skb->protocol = x25_type_trans(skb, dev);
177 
178 	skb_queue_tail(&lapbeth->rx_queue, skb);
179 	napi_schedule(&lapbeth->napi);
180 	return NET_RX_SUCCESS;
181 }
182 
183 /*	Send a LAPB frame via an ethernet interface
184  */
lapbeth_xmit(struct sk_buff * skb,struct net_device * dev)185 static netdev_tx_t lapbeth_xmit(struct sk_buff *skb,
186 				struct net_device *dev)
187 {
188 	struct lapbethdev *lapbeth = netdev_priv(dev);
189 	int err;
190 
191 	spin_lock_bh(&lapbeth->up_lock);
192 	if (!lapbeth->up)
193 		goto drop;
194 
195 	/* There should be a pseudo header of 1 byte added by upper layers.
196 	 * Check to make sure it is there before reading it.
197 	 */
198 	if (skb->len < 1)
199 		goto drop;
200 
201 	switch (skb->data[0]) {
202 	case X25_IFACE_DATA:
203 		break;
204 	case X25_IFACE_CONNECT:
205 		err = lapb_connect_request(dev);
206 		if (err == LAPB_CONNECTED)
207 			lapbeth_connected(dev, LAPB_OK);
208 		else if (err != LAPB_OK)
209 			pr_err("lapb_connect_request error: %d\n", err);
210 		goto drop;
211 	case X25_IFACE_DISCONNECT:
212 		err = lapb_disconnect_request(dev);
213 		if (err == LAPB_NOTCONNECTED)
214 			lapbeth_disconnected(dev, LAPB_OK);
215 		else if (err != LAPB_OK)
216 			pr_err("lapb_disconnect_request err: %d\n", err);
217 		fallthrough;
218 	default:
219 		goto drop;
220 	}
221 
222 	skb_pull(skb, 1);
223 
224 	err = lapb_data_request(dev, skb);
225 	if (err != LAPB_OK) {
226 		pr_err("lapb_data_request error - %d\n", err);
227 		goto drop;
228 	}
229 out:
230 	spin_unlock_bh(&lapbeth->up_lock);
231 	return NETDEV_TX_OK;
232 drop:
233 	kfree_skb(skb);
234 	goto out;
235 }
236 
lapbeth_data_transmit(struct net_device * ndev,struct sk_buff * skb)237 static void lapbeth_data_transmit(struct net_device *ndev, struct sk_buff *skb)
238 {
239 	struct lapbethdev *lapbeth = netdev_priv(ndev);
240 	unsigned char *ptr;
241 	struct net_device *dev;
242 	int size = skb->len;
243 
244 	ptr = skb_push(skb, 2);
245 
246 	*ptr++ = size % 256;
247 	*ptr++ = size / 256;
248 
249 	ndev->stats.tx_packets++;
250 	ndev->stats.tx_bytes += size;
251 
252 	skb->dev = dev = lapbeth->ethdev;
253 
254 	skb->protocol = htons(ETH_P_DEC);
255 
256 	skb_reset_network_header(skb);
257 
258 	dev_hard_header(skb, dev, ETH_P_DEC, bcast_addr, NULL, 0);
259 
260 	dev_queue_xmit(skb);
261 }
262 
lapbeth_connected(struct net_device * dev,int reason)263 static void lapbeth_connected(struct net_device *dev, int reason)
264 {
265 	struct lapbethdev *lapbeth = netdev_priv(dev);
266 	unsigned char *ptr;
267 	struct sk_buff *skb = __dev_alloc_skb(1, GFP_ATOMIC | __GFP_NOMEMALLOC);
268 
269 	if (!skb)
270 		return;
271 
272 	ptr  = skb_put(skb, 1);
273 	*ptr = X25_IFACE_CONNECT;
274 
275 	skb->protocol = x25_type_trans(skb, dev);
276 
277 	skb_queue_tail(&lapbeth->rx_queue, skb);
278 	napi_schedule(&lapbeth->napi);
279 }
280 
lapbeth_disconnected(struct net_device * dev,int reason)281 static void lapbeth_disconnected(struct net_device *dev, int reason)
282 {
283 	struct lapbethdev *lapbeth = netdev_priv(dev);
284 	unsigned char *ptr;
285 	struct sk_buff *skb = __dev_alloc_skb(1, GFP_ATOMIC | __GFP_NOMEMALLOC);
286 
287 	if (!skb)
288 		return;
289 
290 	ptr  = skb_put(skb, 1);
291 	*ptr = X25_IFACE_DISCONNECT;
292 
293 	skb->protocol = x25_type_trans(skb, dev);
294 
295 	skb_queue_tail(&lapbeth->rx_queue, skb);
296 	napi_schedule(&lapbeth->napi);
297 }
298 
299 /*	Set AX.25 callsign
300  */
lapbeth_set_mac_address(struct net_device * dev,void * addr)301 static int lapbeth_set_mac_address(struct net_device *dev, void *addr)
302 {
303 	struct sockaddr *sa = addr;
304 
305 	dev_addr_set(dev, sa->sa_data);
306 	return 0;
307 }
308 
309 static const struct lapb_register_struct lapbeth_callbacks = {
310 	.connect_confirmation    = lapbeth_connected,
311 	.connect_indication      = lapbeth_connected,
312 	.disconnect_confirmation = lapbeth_disconnected,
313 	.disconnect_indication   = lapbeth_disconnected,
314 	.data_indication         = lapbeth_data_indication,
315 	.data_transmit           = lapbeth_data_transmit,
316 };
317 
318 /* open/close a device
319  */
lapbeth_open(struct net_device * dev)320 static int lapbeth_open(struct net_device *dev)
321 {
322 	struct lapbethdev *lapbeth = netdev_priv(dev);
323 	int err;
324 
325 	napi_enable(&lapbeth->napi);
326 
327 	err = lapb_register(dev, &lapbeth_callbacks);
328 	if (err != LAPB_OK) {
329 		napi_disable(&lapbeth->napi);
330 		pr_err("lapb_register error: %d\n", err);
331 		return -ENODEV;
332 	}
333 
334 	spin_lock_bh(&lapbeth->up_lock);
335 	lapbeth->up = true;
336 	spin_unlock_bh(&lapbeth->up_lock);
337 
338 	return 0;
339 }
340 
lapbeth_close(struct net_device * dev)341 static int lapbeth_close(struct net_device *dev)
342 {
343 	struct lapbethdev *lapbeth = netdev_priv(dev);
344 	int err;
345 
346 	spin_lock_bh(&lapbeth->up_lock);
347 	lapbeth->up = false;
348 	spin_unlock_bh(&lapbeth->up_lock);
349 
350 	err = lapb_unregister(dev);
351 	if (err != LAPB_OK)
352 		pr_err("lapb_unregister error: %d\n", err);
353 
354 	napi_disable(&lapbeth->napi);
355 
356 	return 0;
357 }
358 
359 /* ------------------------------------------------------------------------ */
360 
361 static const struct net_device_ops lapbeth_netdev_ops = {
362 	.ndo_open	     = lapbeth_open,
363 	.ndo_stop	     = lapbeth_close,
364 	.ndo_start_xmit	     = lapbeth_xmit,
365 	.ndo_set_mac_address = lapbeth_set_mac_address,
366 };
367 
lapbeth_setup(struct net_device * dev)368 static void lapbeth_setup(struct net_device *dev)
369 {
370 	netdev_lockdep_set_classes(dev);
371 	dev->netdev_ops	     = &lapbeth_netdev_ops;
372 	dev->needs_free_netdev = true;
373 	dev->type            = ARPHRD_X25;
374 	dev->hard_header_len = 0;
375 	dev->mtu             = 1000;
376 	dev->addr_len        = 0;
377 }
378 
379 /*	Setup a new device.
380  */
lapbeth_new_device(struct net_device * dev)381 static int lapbeth_new_device(struct net_device *dev)
382 {
383 	struct net_device *ndev;
384 	struct lapbethdev *lapbeth;
385 	int rc = -ENOMEM;
386 
387 	ASSERT_RTNL();
388 
389 	if (dev->type != ARPHRD_ETHER)
390 		return -EINVAL;
391 
392 	ndev = alloc_netdev(sizeof(*lapbeth), "lapb%d", NET_NAME_UNKNOWN,
393 			    lapbeth_setup);
394 	if (!ndev)
395 		goto out;
396 
397 	/* When transmitting data:
398 	 * first this driver removes a pseudo header of 1 byte,
399 	 * then the lapb module prepends an LAPB header of at most 3 bytes,
400 	 * then this driver prepends a length field of 2 bytes,
401 	 * then the underlying Ethernet device prepends its own header.
402 	 */
403 	ndev->needed_headroom = -1 + 3 + 2 + dev->hard_header_len
404 					   + dev->needed_headroom;
405 	ndev->needed_tailroom = dev->needed_tailroom;
406 
407 	lapbeth = netdev_priv(ndev);
408 	lapbeth->axdev = ndev;
409 
410 	dev_hold(dev);
411 	lapbeth->ethdev = dev;
412 
413 	lapbeth->up = false;
414 	spin_lock_init(&lapbeth->up_lock);
415 
416 	skb_queue_head_init(&lapbeth->rx_queue);
417 	netif_napi_add_weight(ndev, &lapbeth->napi, lapbeth_napi_poll, 16);
418 
419 	rc = -EIO;
420 	if (register_netdevice(ndev))
421 		goto fail;
422 
423 	list_add_rcu(&lapbeth->node, &lapbeth_devices);
424 	rc = 0;
425 out:
426 	return rc;
427 fail:
428 	dev_put(dev);
429 	free_netdev(ndev);
430 	goto out;
431 }
432 
433 /*	Free a lapb network device.
434  */
lapbeth_free_device(struct lapbethdev * lapbeth)435 static void lapbeth_free_device(struct lapbethdev *lapbeth)
436 {
437 	dev_put(lapbeth->ethdev);
438 	list_del_rcu(&lapbeth->node);
439 	unregister_netdevice(lapbeth->axdev);
440 }
441 
442 /*	Handle device status changes.
443  *
444  * Called from notifier with RTNL held.
445  */
lapbeth_device_event(struct notifier_block * this,unsigned long event,void * ptr)446 static int lapbeth_device_event(struct notifier_block *this,
447 				unsigned long event, void *ptr)
448 {
449 	struct lapbethdev *lapbeth;
450 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
451 
452 	if (dev_net(dev) != &init_net)
453 		return NOTIFY_DONE;
454 
455 	if (!dev_is_ethdev(dev) && !lapbeth_get_x25_dev(dev))
456 		return NOTIFY_DONE;
457 
458 	switch (event) {
459 	case NETDEV_UP:
460 		/* New ethernet device -> new LAPB interface	 */
461 		if (!lapbeth_get_x25_dev(dev))
462 			lapbeth_new_device(dev);
463 		break;
464 	case NETDEV_GOING_DOWN:
465 		/* ethernet device closes -> close LAPB interface */
466 		lapbeth = lapbeth_get_x25_dev(dev);
467 		if (lapbeth)
468 			dev_close(lapbeth->axdev);
469 		break;
470 	case NETDEV_UNREGISTER:
471 		/* ethernet device disappears -> remove LAPB interface */
472 		lapbeth = lapbeth_get_x25_dev(dev);
473 		if (lapbeth)
474 			lapbeth_free_device(lapbeth);
475 		break;
476 	}
477 
478 	return NOTIFY_DONE;
479 }
480 
481 /* ------------------------------------------------------------------------ */
482 
483 static struct packet_type lapbeth_packet_type __read_mostly = {
484 	.type = cpu_to_be16(ETH_P_DEC),
485 	.func = lapbeth_rcv,
486 };
487 
488 static struct notifier_block lapbeth_dev_notifier = {
489 	.notifier_call = lapbeth_device_event,
490 };
491 
492 static const char banner[] __initconst =
493 	KERN_INFO "LAPB Ethernet driver version 0.02\n";
494 
lapbeth_init_driver(void)495 static int __init lapbeth_init_driver(void)
496 {
497 	dev_add_pack(&lapbeth_packet_type);
498 
499 	register_netdevice_notifier(&lapbeth_dev_notifier);
500 
501 	printk(banner);
502 
503 	return 0;
504 }
505 module_init(lapbeth_init_driver);
506 
lapbeth_cleanup_driver(void)507 static void __exit lapbeth_cleanup_driver(void)
508 {
509 	struct lapbethdev *lapbeth;
510 	struct list_head *entry, *tmp;
511 
512 	dev_remove_pack(&lapbeth_packet_type);
513 	unregister_netdevice_notifier(&lapbeth_dev_notifier);
514 
515 	rtnl_lock();
516 	list_for_each_safe(entry, tmp, &lapbeth_devices) {
517 		lapbeth = list_entry(entry, struct lapbethdev, node);
518 
519 		dev_put(lapbeth->ethdev);
520 		unregister_netdevice(lapbeth->axdev);
521 	}
522 	rtnl_unlock();
523 }
524 module_exit(lapbeth_cleanup_driver);
525 
526 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
527 MODULE_DESCRIPTION("The unofficial LAPB over Ethernet driver");
528 MODULE_LICENSE("GPL");
529