xref: /openbmc/linux/drivers/net/wan/lapbether.c (revision 8ffdff6a)
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/x25device.h>
43 
44 static const u8 bcast_addr[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
45 
46 /* If this number is made larger, check that the temporary string buffer
47  * in lapbeth_new_device is large enough to store the probe device name.*/
48 #define MAXLAPBDEV 100
49 
50 struct lapbethdev {
51 	struct list_head	node;
52 	struct net_device	*ethdev;	/* link to ethernet device */
53 	struct net_device	*axdev;		/* lapbeth device (lapb#) */
54 };
55 
56 static LIST_HEAD(lapbeth_devices);
57 
58 static void lapbeth_connected(struct net_device *dev, int reason);
59 static void lapbeth_disconnected(struct net_device *dev, int reason);
60 
61 /* ------------------------------------------------------------------------ */
62 
63 /*
64  *	Get the LAPB device for the ethernet device
65  */
66 static struct lapbethdev *lapbeth_get_x25_dev(struct net_device *dev)
67 {
68 	struct lapbethdev *lapbeth;
69 
70 	list_for_each_entry_rcu(lapbeth, &lapbeth_devices, node, lockdep_rtnl_is_held()) {
71 		if (lapbeth->ethdev == dev)
72 			return lapbeth;
73 	}
74 	return NULL;
75 }
76 
77 static __inline__ int dev_is_ethdev(struct net_device *dev)
78 {
79 	return dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5);
80 }
81 
82 /* ------------------------------------------------------------------------ */
83 
84 /*
85  *	Receive a LAPB frame via an ethernet interface.
86  */
87 static int lapbeth_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *ptype, struct net_device *orig_dev)
88 {
89 	int len, err;
90 	struct lapbethdev *lapbeth;
91 
92 	if (dev_net(dev) != &init_net)
93 		goto drop;
94 
95 	if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
96 		return NET_RX_DROP;
97 
98 	if (!pskb_may_pull(skb, 2))
99 		goto drop;
100 
101 	rcu_read_lock();
102 	lapbeth = lapbeth_get_x25_dev(dev);
103 	if (!lapbeth)
104 		goto drop_unlock;
105 	if (!netif_running(lapbeth->axdev))
106 		goto drop_unlock;
107 
108 	len = skb->data[0] + skb->data[1] * 256;
109 	dev->stats.rx_packets++;
110 	dev->stats.rx_bytes += len;
111 
112 	skb_pull(skb, 2);	/* Remove the length bytes */
113 	skb_trim(skb, len);	/* Set the length of the data */
114 
115 	if ((err = lapb_data_received(lapbeth->axdev, skb)) != LAPB_OK) {
116 		printk(KERN_DEBUG "lapbether: lapb_data_received err - %d\n", err);
117 		goto drop_unlock;
118 	}
119 out:
120 	rcu_read_unlock();
121 	return 0;
122 drop_unlock:
123 	kfree_skb(skb);
124 	goto out;
125 drop:
126 	kfree_skb(skb);
127 	return 0;
128 }
129 
130 static int lapbeth_data_indication(struct net_device *dev, struct sk_buff *skb)
131 {
132 	unsigned char *ptr;
133 
134 	if (skb_cow(skb, 1)) {
135 		kfree_skb(skb);
136 		return NET_RX_DROP;
137 	}
138 
139 	skb_push(skb, 1);
140 
141 	ptr  = skb->data;
142 	*ptr = X25_IFACE_DATA;
143 
144 	skb->protocol = x25_type_trans(skb, dev);
145 	return netif_rx(skb);
146 }
147 
148 /*
149  *	Send a LAPB frame via an ethernet interface
150  */
151 static netdev_tx_t lapbeth_xmit(struct sk_buff *skb,
152 				      struct net_device *dev)
153 {
154 	int err;
155 
156 	/*
157 	 * Just to be *really* sure not to send anything if the interface
158 	 * is down, the ethernet device may have gone.
159 	 */
160 	if (!netif_running(dev))
161 		goto drop;
162 
163 	/* There should be a pseudo header of 1 byte added by upper layers.
164 	 * Check to make sure it is there before reading it.
165 	 */
166 	if (skb->len < 1)
167 		goto drop;
168 
169 	switch (skb->data[0]) {
170 	case X25_IFACE_DATA:
171 		break;
172 	case X25_IFACE_CONNECT:
173 		err = lapb_connect_request(dev);
174 		if (err == LAPB_CONNECTED)
175 			lapbeth_connected(dev, LAPB_OK);
176 		else if (err != LAPB_OK)
177 			pr_err("lapb_connect_request error: %d\n", err);
178 		goto drop;
179 	case X25_IFACE_DISCONNECT:
180 		err = lapb_disconnect_request(dev);
181 		if (err == LAPB_NOTCONNECTED)
182 			lapbeth_disconnected(dev, LAPB_OK);
183 		else if (err != LAPB_OK)
184 			pr_err("lapb_disconnect_request err: %d\n", err);
185 		fallthrough;
186 	default:
187 		goto drop;
188 	}
189 
190 	skb_pull(skb, 1);
191 
192 	if ((err = lapb_data_request(dev, skb)) != LAPB_OK) {
193 		pr_err("lapb_data_request error - %d\n", err);
194 		goto drop;
195 	}
196 out:
197 	return NETDEV_TX_OK;
198 drop:
199 	kfree_skb(skb);
200 	goto out;
201 }
202 
203 static void lapbeth_data_transmit(struct net_device *ndev, struct sk_buff *skb)
204 {
205 	struct lapbethdev *lapbeth = netdev_priv(ndev);
206 	unsigned char *ptr;
207 	struct net_device *dev;
208 	int size = skb->len;
209 
210 	ptr = skb_push(skb, 2);
211 
212 	*ptr++ = size % 256;
213 	*ptr++ = size / 256;
214 
215 	ndev->stats.tx_packets++;
216 	ndev->stats.tx_bytes += size;
217 
218 	skb->dev = dev = lapbeth->ethdev;
219 
220 	skb->protocol = htons(ETH_P_DEC);
221 
222 	skb_reset_network_header(skb);
223 
224 	dev_hard_header(skb, dev, ETH_P_DEC, bcast_addr, NULL, 0);
225 
226 	dev_queue_xmit(skb);
227 }
228 
229 static void lapbeth_connected(struct net_device *dev, int reason)
230 {
231 	unsigned char *ptr;
232 	struct sk_buff *skb = dev_alloc_skb(1);
233 
234 	if (!skb) {
235 		pr_err("out of memory\n");
236 		return;
237 	}
238 
239 	ptr  = skb_put(skb, 1);
240 	*ptr = X25_IFACE_CONNECT;
241 
242 	skb->protocol = x25_type_trans(skb, dev);
243 	netif_rx(skb);
244 }
245 
246 static void lapbeth_disconnected(struct net_device *dev, int reason)
247 {
248 	unsigned char *ptr;
249 	struct sk_buff *skb = dev_alloc_skb(1);
250 
251 	if (!skb) {
252 		pr_err("out of memory\n");
253 		return;
254 	}
255 
256 	ptr  = skb_put(skb, 1);
257 	*ptr = X25_IFACE_DISCONNECT;
258 
259 	skb->protocol = x25_type_trans(skb, dev);
260 	netif_rx(skb);
261 }
262 
263 /*
264  *	Set AX.25 callsign
265  */
266 static int lapbeth_set_mac_address(struct net_device *dev, void *addr)
267 {
268 	struct sockaddr *sa = addr;
269 	memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
270 	return 0;
271 }
272 
273 
274 static const struct lapb_register_struct lapbeth_callbacks = {
275 	.connect_confirmation    = lapbeth_connected,
276 	.connect_indication      = lapbeth_connected,
277 	.disconnect_confirmation = lapbeth_disconnected,
278 	.disconnect_indication   = lapbeth_disconnected,
279 	.data_indication         = lapbeth_data_indication,
280 	.data_transmit           = lapbeth_data_transmit,
281 };
282 
283 /*
284  * open/close a device
285  */
286 static int lapbeth_open(struct net_device *dev)
287 {
288 	int err;
289 
290 	if ((err = lapb_register(dev, &lapbeth_callbacks)) != LAPB_OK) {
291 		pr_err("lapb_register error: %d\n", err);
292 		return -ENODEV;
293 	}
294 
295 	return 0;
296 }
297 
298 static int lapbeth_close(struct net_device *dev)
299 {
300 	int err;
301 
302 	if ((err = lapb_unregister(dev)) != LAPB_OK)
303 		pr_err("lapb_unregister error: %d\n", err);
304 
305 	return 0;
306 }
307 
308 /* ------------------------------------------------------------------------ */
309 
310 static const struct net_device_ops lapbeth_netdev_ops = {
311 	.ndo_open	     = lapbeth_open,
312 	.ndo_stop	     = lapbeth_close,
313 	.ndo_start_xmit	     = lapbeth_xmit,
314 	.ndo_set_mac_address = lapbeth_set_mac_address,
315 };
316 
317 static void lapbeth_setup(struct net_device *dev)
318 {
319 	dev->netdev_ops	     = &lapbeth_netdev_ops;
320 	dev->needs_free_netdev = true;
321 	dev->type            = ARPHRD_X25;
322 	dev->hard_header_len = 0;
323 	dev->mtu             = 1000;
324 	dev->addr_len        = 0;
325 }
326 
327 /*
328  *	Setup a new device.
329  */
330 static int lapbeth_new_device(struct net_device *dev)
331 {
332 	struct net_device *ndev;
333 	struct lapbethdev *lapbeth;
334 	int rc = -ENOMEM;
335 
336 	ASSERT_RTNL();
337 
338 	ndev = alloc_netdev(sizeof(*lapbeth), "lapb%d", NET_NAME_UNKNOWN,
339 			    lapbeth_setup);
340 	if (!ndev)
341 		goto out;
342 
343 	/* When transmitting data:
344 	 * first this driver removes a pseudo header of 1 byte,
345 	 * then the lapb module prepends an LAPB header of at most 3 bytes,
346 	 * then this driver prepends a length field of 2 bytes,
347 	 * then the underlying Ethernet device prepends its own header.
348 	 */
349 	ndev->needed_headroom = -1 + 3 + 2 + dev->hard_header_len
350 					   + dev->needed_headroom;
351 	ndev->needed_tailroom = dev->needed_tailroom;
352 
353 	lapbeth = netdev_priv(ndev);
354 	lapbeth->axdev = ndev;
355 
356 	dev_hold(dev);
357 	lapbeth->ethdev = dev;
358 
359 	rc = -EIO;
360 	if (register_netdevice(ndev))
361 		goto fail;
362 
363 	list_add_rcu(&lapbeth->node, &lapbeth_devices);
364 	rc = 0;
365 out:
366 	return rc;
367 fail:
368 	dev_put(dev);
369 	free_netdev(ndev);
370 	goto out;
371 }
372 
373 /*
374  *	Free a lapb network device.
375  */
376 static void lapbeth_free_device(struct lapbethdev *lapbeth)
377 {
378 	dev_put(lapbeth->ethdev);
379 	list_del_rcu(&lapbeth->node);
380 	unregister_netdevice(lapbeth->axdev);
381 }
382 
383 /*
384  *	Handle device status changes.
385  *
386  * Called from notifier with RTNL held.
387  */
388 static int lapbeth_device_event(struct notifier_block *this,
389 				unsigned long event, void *ptr)
390 {
391 	struct lapbethdev *lapbeth;
392 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
393 
394 	if (dev_net(dev) != &init_net)
395 		return NOTIFY_DONE;
396 
397 	if (!dev_is_ethdev(dev))
398 		return NOTIFY_DONE;
399 
400 	switch (event) {
401 	case NETDEV_UP:
402 		/* New ethernet device -> new LAPB interface	 */
403 		if (lapbeth_get_x25_dev(dev) == NULL)
404 			lapbeth_new_device(dev);
405 		break;
406 	case NETDEV_DOWN:
407 		/* ethernet device closed -> close LAPB interface */
408 		lapbeth = lapbeth_get_x25_dev(dev);
409 		if (lapbeth)
410 			dev_close(lapbeth->axdev);
411 		break;
412 	case NETDEV_UNREGISTER:
413 		/* ethernet device disappears -> remove LAPB interface */
414 		lapbeth = lapbeth_get_x25_dev(dev);
415 		if (lapbeth)
416 			lapbeth_free_device(lapbeth);
417 		break;
418 	}
419 
420 	return NOTIFY_DONE;
421 }
422 
423 /* ------------------------------------------------------------------------ */
424 
425 static struct packet_type lapbeth_packet_type __read_mostly = {
426 	.type = cpu_to_be16(ETH_P_DEC),
427 	.func = lapbeth_rcv,
428 };
429 
430 static struct notifier_block lapbeth_dev_notifier = {
431 	.notifier_call = lapbeth_device_event,
432 };
433 
434 static const char banner[] __initconst =
435 	KERN_INFO "LAPB Ethernet driver version 0.02\n";
436 
437 static int __init lapbeth_init_driver(void)
438 {
439 	dev_add_pack(&lapbeth_packet_type);
440 
441 	register_netdevice_notifier(&lapbeth_dev_notifier);
442 
443 	printk(banner);
444 
445 	return 0;
446 }
447 module_init(lapbeth_init_driver);
448 
449 static void __exit lapbeth_cleanup_driver(void)
450 {
451 	struct lapbethdev *lapbeth;
452 	struct list_head *entry, *tmp;
453 
454 	dev_remove_pack(&lapbeth_packet_type);
455 	unregister_netdevice_notifier(&lapbeth_dev_notifier);
456 
457 	rtnl_lock();
458 	list_for_each_safe(entry, tmp, &lapbeth_devices) {
459 		lapbeth = list_entry(entry, struct lapbethdev, node);
460 
461 		dev_put(lapbeth->ethdev);
462 		unregister_netdevice(lapbeth->axdev);
463 	}
464 	rtnl_unlock();
465 }
466 module_exit(lapbeth_cleanup_driver);
467 
468 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
469 MODULE_DESCRIPTION("The unofficial LAPB over Ethernet driver");
470 MODULE_LICENSE("GPL");
471