2 * "LAPB via ethernet" driver release 001
4 * This code REQUIRES 2.1.15 or higher/ NET3.038
7 * This module is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * This is a "pseudo" network driver to allow LAPB over Ethernet.
14 * This driver can use any ethernet destination address, and can be
15 * limited to accept frames from one dedicated ethernet card only.
18 * LAPBETH 001 Jonathan Naylor Cloned from bpqether.c
19 * 2000-10-29 Henner Eisen lapb_data_indication() return status.
20 * 2000-11-14 Henner Eisen dev_hold/put, NETDEV_GOING_DOWN support
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/errno.h>
26 #include <linux/types.h>
27 #include <linux/socket.h>
29 #include <linux/slab.h>
30 #include <linux/kernel.h>
31 #include <linux/string.h>
32 #include <linux/net.h>
33 #include <linux/inet.h>
34 #include <linux/netdevice.h>
35 #include <linux/if_arp.h>
36 #include <linux/skbuff.h>
38 #include <asm/uaccess.h>
40 #include <linux/interrupt.h>
41 #include <linux/notifier.h>
42 #include <linux/stat.h>
43 #include <linux/module.h>
44 #include <linux/lapb.h>
45 #include <linux/init.h>
47 #include <net/x25device.h>
49 static const u8 bcast_addr[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
51 /* If this number is made larger, check that the temporary string buffer
52 * in lapbeth_new_device is large enough to store the probe device name.*/
53 #define MAXLAPBDEV 100
56 struct list_head node;
57 struct net_device *ethdev; /* link to ethernet device */
58 struct net_device *axdev; /* lapbeth device (lapb#) */
60 spinlock_t up_lock; /* Protects "up" */
63 static LIST_HEAD(lapbeth_devices);
65 /* ------------------------------------------------------------------------ */
68 * Get the LAPB device for the ethernet device
70 static struct lapbethdev *lapbeth_get_x25_dev(struct net_device *dev)
72 struct lapbethdev *lapbeth;
74 list_for_each_entry_rcu(lapbeth, &lapbeth_devices, node) {
75 if (lapbeth->ethdev == dev)
81 static __inline__ int dev_is_ethdev(struct net_device *dev)
83 return dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5);
86 /* ------------------------------------------------------------------------ */
89 * Receive a LAPB frame via an ethernet interface.
91 static int lapbeth_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *ptype, struct net_device *orig_dev)
94 struct lapbethdev *lapbeth;
96 if (dev_net(dev) != &init_net)
99 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
102 if (!pskb_may_pull(skb, 2))
106 lapbeth = lapbeth_get_x25_dev(dev);
108 goto drop_unlock_rcu;
109 spin_lock_bh(&lapbeth->up_lock);
113 len = skb->data[0] + skb->data[1] * 256;
114 dev->stats.rx_packets++;
115 dev->stats.rx_bytes += len;
117 skb_pull(skb, 2); /* Remove the length bytes */
118 skb_trim(skb, len); /* Set the length of the data */
120 if ((err = lapb_data_received(lapbeth->axdev, skb)) != LAPB_OK) {
121 printk(KERN_DEBUG "lapbether: lapb_data_received err - %d\n", err);
125 spin_unlock_bh(&lapbeth->up_lock);
138 static int lapbeth_data_indication(struct net_device *dev, struct sk_buff *skb)
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 if ((err = lapb_connect_request(dev)) != LAPB_OK)
178 pr_err("lapb_connect_request error: %d\n", err);
180 case X25_IFACE_DISCONNECT:
181 if ((err = lapb_disconnect_request(dev)) != LAPB_OK)
182 pr_err("lapb_disconnect_request err: %d\n", err);
190 if ((err = lapb_data_request(dev, skb)) != LAPB_OK) {
191 pr_err("lapb_data_request error - %d\n", err);
195 spin_unlock_bh(&lapbeth->up_lock);
202 static void lapbeth_data_transmit(struct net_device *ndev, struct sk_buff *skb)
204 struct lapbethdev *lapbeth = netdev_priv(ndev);
206 struct net_device *dev;
209 ptr = skb_push(skb, 2);
214 ndev->stats.tx_packets++;
215 ndev->stats.tx_bytes += size;
217 skb->dev = dev = lapbeth->ethdev;
219 skb->protocol = htons(ETH_P_DEC);
221 skb_reset_network_header(skb);
223 dev_hard_header(skb, dev, ETH_P_DEC, bcast_addr, NULL, 0);
228 static void lapbeth_connected(struct net_device *dev, int reason)
231 struct sk_buff *skb = dev_alloc_skb(1);
234 pr_err("out of memory\n");
238 ptr = skb_put(skb, 1);
239 *ptr = X25_IFACE_CONNECT;
241 skb->protocol = x25_type_trans(skb, dev);
245 static void lapbeth_disconnected(struct net_device *dev, int reason)
248 struct sk_buff *skb = dev_alloc_skb(1);
251 pr_err("out of memory\n");
255 ptr = skb_put(skb, 1);
256 *ptr = X25_IFACE_DISCONNECT;
258 skb->protocol = x25_type_trans(skb, dev);
265 static int lapbeth_set_mac_address(struct net_device *dev, void *addr)
267 struct sockaddr *sa = addr;
268 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
273 static const struct lapb_register_struct lapbeth_callbacks = {
274 .connect_confirmation = lapbeth_connected,
275 .connect_indication = lapbeth_connected,
276 .disconnect_confirmation = lapbeth_disconnected,
277 .disconnect_indication = lapbeth_disconnected,
278 .data_indication = lapbeth_data_indication,
279 .data_transmit = lapbeth_data_transmit,
283 * open/close a device
285 static int lapbeth_open(struct net_device *dev)
287 struct lapbethdev *lapbeth = netdev_priv(dev);
290 if ((err = lapb_register(dev, &lapbeth_callbacks)) != LAPB_OK) {
291 pr_err("lapb_register error: %d\n", err);
295 spin_lock_bh(&lapbeth->up_lock);
297 spin_unlock_bh(&lapbeth->up_lock);
302 static int lapbeth_close(struct net_device *dev)
304 struct lapbethdev *lapbeth = netdev_priv(dev);
307 spin_lock_bh(&lapbeth->up_lock);
309 spin_unlock_bh(&lapbeth->up_lock);
311 if ((err = lapb_unregister(dev)) != LAPB_OK)
312 pr_err("lapb_unregister error: %d\n", err);
317 /* ------------------------------------------------------------------------ */
319 static const struct net_device_ops lapbeth_netdev_ops = {
320 .ndo_open = lapbeth_open,
321 .ndo_stop = lapbeth_close,
322 .ndo_start_xmit = lapbeth_xmit,
323 .ndo_set_mac_address = lapbeth_set_mac_address,
326 static void lapbeth_setup(struct net_device *dev)
328 dev->netdev_ops = &lapbeth_netdev_ops;
329 dev->destructor = free_netdev;
330 dev->type = ARPHRD_X25;
331 dev->hard_header_len = 0;
337 * Setup a new device.
339 static int lapbeth_new_device(struct net_device *dev)
341 struct net_device *ndev;
342 struct lapbethdev *lapbeth;
347 ndev = alloc_netdev(sizeof(*lapbeth), "lapb%d", NET_NAME_UNKNOWN,
352 /* When transmitting data:
353 * first this driver removes a pseudo header of 1 byte,
354 * then the lapb module prepends an LAPB header of at most 3 bytes,
355 * then this driver prepends a length field of 2 bytes,
356 * then the underlying Ethernet device prepends its own header.
358 ndev->needed_headroom = -1 + 3 + 2 + dev->hard_header_len
359 + dev->needed_headroom;
360 ndev->needed_tailroom = dev->needed_tailroom;
362 lapbeth = netdev_priv(ndev);
363 lapbeth->axdev = ndev;
366 lapbeth->ethdev = dev;
369 spin_lock_init(&lapbeth->up_lock);
372 if (register_netdevice(ndev))
375 list_add_rcu(&lapbeth->node, &lapbeth_devices);
387 * Free a lapb network device.
389 static void lapbeth_free_device(struct lapbethdev *lapbeth)
391 dev_put(lapbeth->ethdev);
392 list_del_rcu(&lapbeth->node);
393 unregister_netdevice(lapbeth->axdev);
397 * Handle device status changes.
399 * Called from notifier with RTNL held.
401 static int lapbeth_device_event(struct notifier_block *this,
402 unsigned long event, void *ptr)
404 struct lapbethdev *lapbeth;
405 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
407 if (dev_net(dev) != &init_net)
410 if (!dev_is_ethdev(dev))
415 /* New ethernet device -> new LAPB interface */
416 if (lapbeth_get_x25_dev(dev) == NULL)
417 lapbeth_new_device(dev);
420 /* ethernet device closed -> close LAPB interface */
421 lapbeth = lapbeth_get_x25_dev(dev);
423 dev_close(lapbeth->axdev);
425 case NETDEV_UNREGISTER:
426 /* ethernet device disappears -> remove LAPB interface */
427 lapbeth = lapbeth_get_x25_dev(dev);
429 lapbeth_free_device(lapbeth);
436 /* ------------------------------------------------------------------------ */
438 static struct packet_type lapbeth_packet_type __read_mostly = {
439 .type = cpu_to_be16(ETH_P_DEC),
443 static struct notifier_block lapbeth_dev_notifier = {
444 .notifier_call = lapbeth_device_event,
447 static const char banner[] __initconst =
448 KERN_INFO "LAPB Ethernet driver version 0.02\n";
450 static int __init lapbeth_init_driver(void)
452 dev_add_pack(&lapbeth_packet_type);
454 register_netdevice_notifier(&lapbeth_dev_notifier);
460 module_init(lapbeth_init_driver);
462 static void __exit lapbeth_cleanup_driver(void)
464 struct lapbethdev *lapbeth;
465 struct list_head *entry, *tmp;
467 dev_remove_pack(&lapbeth_packet_type);
468 unregister_netdevice_notifier(&lapbeth_dev_notifier);
471 list_for_each_safe(entry, tmp, &lapbeth_devices) {
472 lapbeth = list_entry(entry, struct lapbethdev, node);
474 dev_put(lapbeth->ethdev);
475 unregister_netdevice(lapbeth->axdev);
479 module_exit(lapbeth_cleanup_driver);
481 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
482 MODULE_DESCRIPTION("The unofficial LAPB over Ethernet driver");
483 MODULE_LICENSE("GPL");