xref: /openbmc/linux/drivers/net/wan/lapbether.c (revision 27ab1c1c)
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 	netif_start_queue(dev);
296 	return 0;
297 }
298 
299 static int lapbeth_close(struct net_device *dev)
300 {
301 	int err;
302 
303 	netif_stop_queue(dev);
304 
305 	if ((err = lapb_unregister(dev)) != LAPB_OK)
306 		pr_err("lapb_unregister error: %d\n", err);
307 
308 	return 0;
309 }
310 
311 /* ------------------------------------------------------------------------ */
312 
313 static const struct net_device_ops lapbeth_netdev_ops = {
314 	.ndo_open	     = lapbeth_open,
315 	.ndo_stop	     = lapbeth_close,
316 	.ndo_start_xmit	     = lapbeth_xmit,
317 	.ndo_set_mac_address = lapbeth_set_mac_address,
318 };
319 
320 static void lapbeth_setup(struct net_device *dev)
321 {
322 	dev->netdev_ops	     = &lapbeth_netdev_ops;
323 	dev->needs_free_netdev = true;
324 	dev->type            = ARPHRD_X25;
325 	dev->hard_header_len = 0;
326 	dev->mtu             = 1000;
327 	dev->addr_len        = 0;
328 }
329 
330 /*
331  *	Setup a new device.
332  */
333 static int lapbeth_new_device(struct net_device *dev)
334 {
335 	struct net_device *ndev;
336 	struct lapbethdev *lapbeth;
337 	int rc = -ENOMEM;
338 
339 	ASSERT_RTNL();
340 
341 	ndev = alloc_netdev(sizeof(*lapbeth), "lapb%d", NET_NAME_UNKNOWN,
342 			    lapbeth_setup);
343 	if (!ndev)
344 		goto out;
345 
346 	/* When transmitting data:
347 	 * first this driver removes a pseudo header of 1 byte,
348 	 * then the lapb module prepends an LAPB header of at most 3 bytes,
349 	 * then this driver prepends a length field of 2 bytes,
350 	 * then the underlying Ethernet device prepends its own header.
351 	 */
352 	ndev->needed_headroom = -1 + 3 + 2 + dev->hard_header_len
353 					   + dev->needed_headroom;
354 	ndev->needed_tailroom = dev->needed_tailroom;
355 
356 	lapbeth = netdev_priv(ndev);
357 	lapbeth->axdev = ndev;
358 
359 	dev_hold(dev);
360 	lapbeth->ethdev = dev;
361 
362 	rc = -EIO;
363 	if (register_netdevice(ndev))
364 		goto fail;
365 
366 	list_add_rcu(&lapbeth->node, &lapbeth_devices);
367 	rc = 0;
368 out:
369 	return rc;
370 fail:
371 	dev_put(dev);
372 	free_netdev(ndev);
373 	goto out;
374 }
375 
376 /*
377  *	Free a lapb network device.
378  */
379 static void lapbeth_free_device(struct lapbethdev *lapbeth)
380 {
381 	dev_put(lapbeth->ethdev);
382 	list_del_rcu(&lapbeth->node);
383 	unregister_netdevice(lapbeth->axdev);
384 }
385 
386 /*
387  *	Handle device status changes.
388  *
389  * Called from notifier with RTNL held.
390  */
391 static int lapbeth_device_event(struct notifier_block *this,
392 				unsigned long event, void *ptr)
393 {
394 	struct lapbethdev *lapbeth;
395 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
396 
397 	if (dev_net(dev) != &init_net)
398 		return NOTIFY_DONE;
399 
400 	if (!dev_is_ethdev(dev))
401 		return NOTIFY_DONE;
402 
403 	switch (event) {
404 	case NETDEV_UP:
405 		/* New ethernet device -> new LAPB interface	 */
406 		if (lapbeth_get_x25_dev(dev) == NULL)
407 			lapbeth_new_device(dev);
408 		break;
409 	case NETDEV_DOWN:
410 		/* ethernet device closed -> close LAPB interface */
411 		lapbeth = lapbeth_get_x25_dev(dev);
412 		if (lapbeth)
413 			dev_close(lapbeth->axdev);
414 		break;
415 	case NETDEV_UNREGISTER:
416 		/* ethernet device disappears -> remove LAPB interface */
417 		lapbeth = lapbeth_get_x25_dev(dev);
418 		if (lapbeth)
419 			lapbeth_free_device(lapbeth);
420 		break;
421 	}
422 
423 	return NOTIFY_DONE;
424 }
425 
426 /* ------------------------------------------------------------------------ */
427 
428 static struct packet_type lapbeth_packet_type __read_mostly = {
429 	.type = cpu_to_be16(ETH_P_DEC),
430 	.func = lapbeth_rcv,
431 };
432 
433 static struct notifier_block lapbeth_dev_notifier = {
434 	.notifier_call = lapbeth_device_event,
435 };
436 
437 static const char banner[] __initconst =
438 	KERN_INFO "LAPB Ethernet driver version 0.02\n";
439 
440 static int __init lapbeth_init_driver(void)
441 {
442 	dev_add_pack(&lapbeth_packet_type);
443 
444 	register_netdevice_notifier(&lapbeth_dev_notifier);
445 
446 	printk(banner);
447 
448 	return 0;
449 }
450 module_init(lapbeth_init_driver);
451 
452 static void __exit lapbeth_cleanup_driver(void)
453 {
454 	struct lapbethdev *lapbeth;
455 	struct list_head *entry, *tmp;
456 
457 	dev_remove_pack(&lapbeth_packet_type);
458 	unregister_netdevice_notifier(&lapbeth_dev_notifier);
459 
460 	rtnl_lock();
461 	list_for_each_safe(entry, tmp, &lapbeth_devices) {
462 		lapbeth = list_entry(entry, struct lapbethdev, node);
463 
464 		dev_put(lapbeth->ethdev);
465 		unregister_netdevice(lapbeth->axdev);
466 	}
467 	rtnl_unlock();
468 }
469 module_exit(lapbeth_cleanup_driver);
470 
471 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
472 MODULE_DESCRIPTION("The unofficial LAPB over Ethernet driver");
473 MODULE_LICENSE("GPL");
474