1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * "LAPB via ethernet" driver release 001
5 * This code REQUIRES 2.1.15 or higher/ NET3.038
7 * This is a "pseudo" network driver to allow LAPB over Ethernet.
9 * This driver can use any ethernet destination address, and can be
10 * limited to accept frames from one dedicated ethernet card only.
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
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/errno.h>
21 #include <linux/types.h>
22 #include <linux/socket.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>
33 #include <linux/uaccess.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>
42 #include <net/x25device.h>
44 static const u8 bcast_addr[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
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
51 struct list_head node;
52 struct net_device *ethdev; /* link to ethernet device */
53 struct net_device *axdev; /* lapbeth device (lapb#) */
55 spinlock_t up_lock; /* Protects "up" */
58 static LIST_HEAD(lapbeth_devices);
60 static void lapbeth_connected(struct net_device *dev, int reason);
61 static void lapbeth_disconnected(struct net_device *dev, int reason);
63 /* ------------------------------------------------------------------------ */
66 * Get the LAPB device for the ethernet device
68 static struct lapbethdev *lapbeth_get_x25_dev(struct net_device *dev)
70 struct lapbethdev *lapbeth;
72 list_for_each_entry_rcu(lapbeth, &lapbeth_devices, node, lockdep_rtnl_is_held()) {
73 if (lapbeth->ethdev == dev)
79 static __inline__ int dev_is_ethdev(struct net_device *dev)
81 return dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5);
84 /* ------------------------------------------------------------------------ */
87 * Receive a LAPB frame via an ethernet interface.
89 static int lapbeth_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *ptype, struct net_device *orig_dev)
92 struct lapbethdev *lapbeth;
94 if (dev_net(dev) != &init_net)
97 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
100 if (!pskb_may_pull(skb, 2))
104 lapbeth = lapbeth_get_x25_dev(dev);
106 goto drop_unlock_rcu;
107 spin_lock_bh(&lapbeth->up_lock);
111 len = skb->data[0] + skb->data[1] * 256;
112 dev->stats.rx_packets++;
113 dev->stats.rx_bytes += len;
115 skb_pull(skb, 2); /* Remove the length bytes */
116 skb_trim(skb, len); /* Set the length of the data */
118 if ((err = lapb_data_received(lapbeth->axdev, skb)) != LAPB_OK) {
119 printk(KERN_DEBUG "lapbether: lapb_data_received err - %d\n", err);
123 spin_unlock_bh(&lapbeth->up_lock);
136 static int lapbeth_data_indication(struct net_device *dev, struct sk_buff *skb)
140 if (skb_cow(skb, 1)) {
148 *ptr = X25_IFACE_DATA;
150 skb->protocol = x25_type_trans(skb, dev);
151 return netif_rx(skb);
155 * Send a LAPB frame via an ethernet interface
157 static netdev_tx_t lapbeth_xmit(struct sk_buff *skb,
158 struct net_device *dev)
160 struct lapbethdev *lapbeth = netdev_priv(dev);
163 spin_lock_bh(&lapbeth->up_lock);
167 /* There should be a pseudo header of 1 byte added by upper layers.
168 * Check to make sure it is there before reading it.
173 switch (skb->data[0]) {
176 case X25_IFACE_CONNECT:
177 err = lapb_connect_request(dev);
178 if (err == LAPB_CONNECTED)
179 lapbeth_connected(dev, LAPB_OK);
180 else if (err != LAPB_OK)
181 pr_err("lapb_connect_request error: %d\n", err);
183 case X25_IFACE_DISCONNECT:
184 err = lapb_disconnect_request(dev);
185 if (err == LAPB_NOTCONNECTED)
186 lapbeth_disconnected(dev, LAPB_OK);
187 else if (err != LAPB_OK)
188 pr_err("lapb_disconnect_request err: %d\n", err);
196 if ((err = lapb_data_request(dev, skb)) != LAPB_OK) {
197 pr_err("lapb_data_request error - %d\n", err);
201 spin_unlock_bh(&lapbeth->up_lock);
208 static void lapbeth_data_transmit(struct net_device *ndev, struct sk_buff *skb)
210 struct lapbethdev *lapbeth = netdev_priv(ndev);
212 struct net_device *dev;
215 ptr = skb_push(skb, 2);
220 ndev->stats.tx_packets++;
221 ndev->stats.tx_bytes += size;
223 skb->dev = dev = lapbeth->ethdev;
225 skb->protocol = htons(ETH_P_DEC);
227 skb_reset_network_header(skb);
229 dev_hard_header(skb, dev, ETH_P_DEC, bcast_addr, NULL, 0);
234 static void lapbeth_connected(struct net_device *dev, int reason)
237 struct sk_buff *skb = dev_alloc_skb(1);
240 pr_err("out of memory\n");
244 ptr = skb_put(skb, 1);
245 *ptr = X25_IFACE_CONNECT;
247 skb->protocol = x25_type_trans(skb, dev);
251 static void lapbeth_disconnected(struct net_device *dev, int reason)
254 struct sk_buff *skb = dev_alloc_skb(1);
257 pr_err("out of memory\n");
261 ptr = skb_put(skb, 1);
262 *ptr = X25_IFACE_DISCONNECT;
264 skb->protocol = x25_type_trans(skb, dev);
271 static int lapbeth_set_mac_address(struct net_device *dev, void *addr)
273 struct sockaddr *sa = addr;
274 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
279 static const struct lapb_register_struct lapbeth_callbacks = {
280 .connect_confirmation = lapbeth_connected,
281 .connect_indication = lapbeth_connected,
282 .disconnect_confirmation = lapbeth_disconnected,
283 .disconnect_indication = lapbeth_disconnected,
284 .data_indication = lapbeth_data_indication,
285 .data_transmit = lapbeth_data_transmit,
289 * open/close a device
291 static int lapbeth_open(struct net_device *dev)
293 struct lapbethdev *lapbeth = netdev_priv(dev);
296 if ((err = lapb_register(dev, &lapbeth_callbacks)) != LAPB_OK) {
297 pr_err("lapb_register error: %d\n", err);
301 spin_lock_bh(&lapbeth->up_lock);
303 spin_unlock_bh(&lapbeth->up_lock);
308 static int lapbeth_close(struct net_device *dev)
310 struct lapbethdev *lapbeth = netdev_priv(dev);
313 spin_lock_bh(&lapbeth->up_lock);
315 spin_unlock_bh(&lapbeth->up_lock);
317 if ((err = lapb_unregister(dev)) != LAPB_OK)
318 pr_err("lapb_unregister error: %d\n", err);
323 /* ------------------------------------------------------------------------ */
325 static const struct net_device_ops lapbeth_netdev_ops = {
326 .ndo_open = lapbeth_open,
327 .ndo_stop = lapbeth_close,
328 .ndo_start_xmit = lapbeth_xmit,
329 .ndo_set_mac_address = lapbeth_set_mac_address,
332 static void lapbeth_setup(struct net_device *dev)
334 dev->netdev_ops = &lapbeth_netdev_ops;
335 dev->needs_free_netdev = true;
336 dev->type = ARPHRD_X25;
337 dev->hard_header_len = 0;
343 * Setup a new device.
345 static int lapbeth_new_device(struct net_device *dev)
347 struct net_device *ndev;
348 struct lapbethdev *lapbeth;
353 ndev = alloc_netdev(sizeof(*lapbeth), "lapb%d", NET_NAME_UNKNOWN,
358 /* When transmitting data:
359 * first this driver removes a pseudo header of 1 byte,
360 * then the lapb module prepends an LAPB header of at most 3 bytes,
361 * then this driver prepends a length field of 2 bytes,
362 * then the underlying Ethernet device prepends its own header.
364 ndev->needed_headroom = -1 + 3 + 2 + dev->hard_header_len
365 + dev->needed_headroom;
366 ndev->needed_tailroom = dev->needed_tailroom;
368 lapbeth = netdev_priv(ndev);
369 lapbeth->axdev = ndev;
372 lapbeth->ethdev = dev;
375 spin_lock_init(&lapbeth->up_lock);
378 if (register_netdevice(ndev))
381 list_add_rcu(&lapbeth->node, &lapbeth_devices);
392 * Free a lapb network device.
394 static void lapbeth_free_device(struct lapbethdev *lapbeth)
396 dev_put(lapbeth->ethdev);
397 list_del_rcu(&lapbeth->node);
398 unregister_netdevice(lapbeth->axdev);
402 * Handle device status changes.
404 * Called from notifier with RTNL held.
406 static int lapbeth_device_event(struct notifier_block *this,
407 unsigned long event, void *ptr)
409 struct lapbethdev *lapbeth;
410 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
412 if (dev_net(dev) != &init_net)
415 if (!dev_is_ethdev(dev))
420 /* New ethernet device -> new LAPB interface */
421 if (lapbeth_get_x25_dev(dev) == NULL)
422 lapbeth_new_device(dev);
424 case NETDEV_GOING_DOWN:
425 /* ethernet device closes -> close LAPB interface */
426 lapbeth = lapbeth_get_x25_dev(dev);
428 dev_close(lapbeth->axdev);
430 case NETDEV_UNREGISTER:
431 /* ethernet device disappears -> remove LAPB interface */
432 lapbeth = lapbeth_get_x25_dev(dev);
434 lapbeth_free_device(lapbeth);
441 /* ------------------------------------------------------------------------ */
443 static struct packet_type lapbeth_packet_type __read_mostly = {
444 .type = cpu_to_be16(ETH_P_DEC),
448 static struct notifier_block lapbeth_dev_notifier = {
449 .notifier_call = lapbeth_device_event,
452 static const char banner[] __initconst =
453 KERN_INFO "LAPB Ethernet driver version 0.02\n";
455 static int __init lapbeth_init_driver(void)
457 dev_add_pack(&lapbeth_packet_type);
459 register_netdevice_notifier(&lapbeth_dev_notifier);
465 module_init(lapbeth_init_driver);
467 static void __exit lapbeth_cleanup_driver(void)
469 struct lapbethdev *lapbeth;
470 struct list_head *entry, *tmp;
472 dev_remove_pack(&lapbeth_packet_type);
473 unregister_netdevice_notifier(&lapbeth_dev_notifier);
476 list_for_each_safe(entry, tmp, &lapbeth_devices) {
477 lapbeth = list_entry(entry, struct lapbethdev, node);
479 dev_put(lapbeth->ethdev);
480 unregister_netdevice(lapbeth->axdev);
484 module_exit(lapbeth_cleanup_driver);
486 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
487 MODULE_DESCRIPTION("The unofficial LAPB over Ethernet driver");
488 MODULE_LICENSE("GPL");