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 #include <linux/errno.h>
24 #include <linux/types.h>
25 #include <linux/socket.h>
27 #include <linux/kernel.h>
28 #include <linux/string.h>
29 #include <linux/net.h>
30 #include <linux/inet.h>
31 #include <linux/netdevice.h>
32 #include <linux/if_arp.h>
33 #include <linux/skbuff.h>
35 #include <asm/system.h>
36 #include <asm/uaccess.h>
38 #include <linux/interrupt.h>
39 #include <linux/notifier.h>
40 #include <linux/stat.h>
41 #include <linux/netfilter.h>
42 #include <linux/module.h>
43 #include <linux/lapb.h>
44 #include <linux/init.h>
46 static char bcast_addr[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
48 /* If this number is made larger, check that the temporary string buffer
49 * in lapbeth_new_device is large enough to store the probe device name.*/
50 #define MAXLAPBDEV 100
53 struct list_head node;
54 struct net_device *ethdev; /* link to ethernet device */
55 struct net_device *axdev; /* lapbeth device (lapb#) */
56 struct net_device_stats stats; /* some statistics */
59 static struct list_head lapbeth_devices = LIST_HEAD_INIT(lapbeth_devices);
61 /* ------------------------------------------------------------------------ */
64 * Get the LAPB device for the ethernet device
66 static struct lapbethdev *lapbeth_get_x25_dev(struct net_device *dev)
68 struct lapbethdev *lapbeth;
70 list_for_each_entry_rcu(lapbeth, &lapbeth_devices, node) {
71 if (lapbeth->ethdev == dev)
77 static __inline__ int dev_is_ethdev(struct net_device *dev)
79 return dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5);
82 /* ------------------------------------------------------------------------ */
85 * Receive a LAPB frame via an ethernet interface.
87 static int lapbeth_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *ptype)
90 struct lapbethdev *lapbeth;
92 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
95 if (!pskb_may_pull(skb, 2))
99 lapbeth = lapbeth_get_x25_dev(dev);
102 if (!netif_running(lapbeth->axdev))
105 lapbeth->stats.rx_packets++;
107 len = skb->data[0] + skb->data[1] * 256;
108 lapbeth->stats.rx_bytes += len;
110 skb_pull(skb, 2); /* Remove the length bytes */
111 skb_trim(skb, len); /* Set the length of the data */
113 if ((err = lapb_data_received(lapbeth, skb)) != LAPB_OK) {
114 printk(KERN_DEBUG "lapbether: lapb_data_received err - %d\n", err);
128 static int lapbeth_data_indication(void *token, struct sk_buff *skb)
130 struct lapbethdev *lapbeth = (struct lapbethdev *)token;
141 skb->dev = lapbeth->axdev;
142 skb->protocol = htons(ETH_P_X25);
143 skb->mac.raw = skb->data;
144 skb->pkt_type = PACKET_HOST;
146 skb->dev->last_rx = jiffies;
147 return netif_rx(skb);
151 * Send a LAPB frame via an ethernet interface
153 static int lapbeth_xmit(struct sk_buff *skb, struct net_device *dev)
155 struct lapbethdev *lapbeth = (struct lapbethdev *)dev->priv;
159 * Just to be *really* sure not to send anything if the interface
160 * is down, the ethernet device may have gone.
162 if (!netif_running(dev)) {
166 switch (skb->data[0]) {
171 if ((err = lapb_connect_request(lapbeth)) != LAPB_OK)
172 printk(KERN_ERR "lapbeth: lapb_connect_request "
176 if ((err = lapb_disconnect_request(lapbeth)) != LAPB_OK)
177 printk(KERN_ERR "lapbeth: lapb_disconnect_request "
186 if ((err = lapb_data_request(lapbeth, skb)) != LAPB_OK) {
187 printk(KERN_ERR "lapbeth: lapb_data_request error - %d\n", err);
201 static void lapbeth_data_transmit(void *token, struct sk_buff *skb)
203 struct lapbethdev *lapbeth = (struct lapbethdev *)token;
205 struct net_device *dev;
208 skb->protocol = htons(ETH_P_X25);
210 ptr = skb_push(skb, 2);
215 lapbeth->stats.tx_packets++;
216 lapbeth->stats.tx_bytes += size;
218 skb->dev = dev = lapbeth->ethdev;
220 dev->hard_header(skb, dev, ETH_P_DEC, bcast_addr, NULL, 0);
225 static void lapbeth_connected(void *token, int reason)
227 struct lapbethdev *lapbeth = (struct lapbethdev *)token;
229 struct sk_buff *skb = dev_alloc_skb(1);
232 printk(KERN_ERR "lapbeth: out of memory\n");
236 ptr = skb_put(skb, 1);
239 skb->dev = lapbeth->axdev;
240 skb->protocol = htons(ETH_P_X25);
241 skb->mac.raw = skb->data;
242 skb->pkt_type = PACKET_HOST;
244 skb->dev->last_rx = jiffies;
248 static void lapbeth_disconnected(void *token, int reason)
250 struct lapbethdev *lapbeth = (struct lapbethdev *)token;
252 struct sk_buff *skb = dev_alloc_skb(1);
255 printk(KERN_ERR "lapbeth: out of memory\n");
259 ptr = skb_put(skb, 1);
262 skb->dev = lapbeth->axdev;
263 skb->protocol = htons(ETH_P_X25);
264 skb->mac.raw = skb->data;
265 skb->pkt_type = PACKET_HOST;
267 skb->dev->last_rx = jiffies;
274 static struct net_device_stats *lapbeth_get_stats(struct net_device *dev)
276 struct lapbethdev *lapbeth = (struct lapbethdev *)dev->priv;
277 return &lapbeth->stats;
283 static int lapbeth_set_mac_address(struct net_device *dev, void *addr)
285 struct sockaddr *sa = (struct sockaddr *)addr;
286 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
291 static struct lapb_register_struct lapbeth_callbacks = {
292 .connect_confirmation = lapbeth_connected,
293 .connect_indication = lapbeth_connected,
294 .disconnect_confirmation = lapbeth_disconnected,
295 .disconnect_indication = lapbeth_disconnected,
296 .data_indication = lapbeth_data_indication,
297 .data_transmit = lapbeth_data_transmit,
302 * open/close a device
304 static int lapbeth_open(struct net_device *dev)
306 struct lapbethdev *lapbeth;
309 lapbeth = (struct lapbethdev *)dev->priv;
310 if ((err = lapb_register(lapbeth, &lapbeth_callbacks)) != LAPB_OK) {
311 printk(KERN_ERR "lapbeth: lapb_register error - %d\n", err);
315 netif_start_queue(dev);
319 static int lapbeth_close(struct net_device *dev)
321 struct lapbethdev *lapbeth = (struct lapbethdev *)dev->priv;
324 netif_stop_queue(dev);
326 if ((err = lapb_unregister(lapbeth)) != LAPB_OK)
327 printk(KERN_ERR "lapbeth: lapb_unregister error - %d\n", err);
332 /* ------------------------------------------------------------------------ */
334 static void lapbeth_setup(struct net_device *dev)
336 dev->hard_start_xmit = lapbeth_xmit;
337 dev->open = lapbeth_open;
338 dev->stop = lapbeth_close;
339 dev->destructor = free_netdev;
340 dev->set_mac_address = lapbeth_set_mac_address;
341 dev->get_stats = lapbeth_get_stats;
342 dev->type = ARPHRD_X25;
343 dev->hard_header_len = 3;
346 SET_MODULE_OWNER(dev);
350 * Setup a new device.
352 static int lapbeth_new_device(struct net_device *dev)
354 struct net_device *ndev;
355 struct lapbethdev *lapbeth;
360 ndev = alloc_netdev(sizeof(*lapbeth), "lapb%d",
365 lapbeth = ndev->priv;
366 lapbeth->axdev = ndev;
369 lapbeth->ethdev = dev;
371 rc = dev_alloc_name(ndev, ndev->name);
376 if (register_netdevice(ndev))
379 list_add_rcu(&lapbeth->node, &lapbeth_devices);
390 * Free a lapb network device.
392 static void lapbeth_free_device(struct lapbethdev *lapbeth)
394 dev_put(lapbeth->ethdev);
395 list_del_rcu(&lapbeth->node);
396 unregister_netdevice(lapbeth->axdev);
400 * Handle device status changes.
402 static int lapbeth_device_event(struct notifier_block *this,
403 unsigned long event, void *ptr)
405 struct lapbethdev *lapbeth;
406 struct net_device *dev = (struct net_device *)ptr;
408 if (!dev_is_ethdev(dev))
414 /* New ethernet device -> new LAPB interface */
415 if (lapbeth_get_x25_dev(dev) == NULL)
416 lapbeth_new_device(dev);
419 /* ethernet device closed -> close LAPB interface */
420 lapbeth = lapbeth_get_x25_dev(dev);
422 dev_close(lapbeth->axdev);
424 case NETDEV_UNREGISTER:
425 /* ethernet device disappears -> remove LAPB interface */
426 lapbeth = lapbeth_get_x25_dev(dev);
428 lapbeth_free_device(lapbeth);
436 /* ------------------------------------------------------------------------ */
438 static struct packet_type lapbeth_packet_type = {
439 .type = __constant_htons(ETH_P_DEC),
443 static struct notifier_block lapbeth_dev_notifier = {
444 .notifier_call = lapbeth_device_event,
447 static char banner[] __initdata = KERN_INFO "LAPB Ethernet driver version 0.02\n";
449 static int __init lapbeth_init_driver(void)
451 dev_add_pack(&lapbeth_packet_type);
453 register_netdevice_notifier(&lapbeth_dev_notifier);
459 module_init(lapbeth_init_driver);
461 static void __exit lapbeth_cleanup_driver(void)
463 struct lapbethdev *lapbeth;
464 struct list_head *entry, *tmp;
466 dev_remove_pack(&lapbeth_packet_type);
467 unregister_netdevice_notifier(&lapbeth_dev_notifier);
470 list_for_each_safe(entry, tmp, &lapbeth_devices) {
471 lapbeth = list_entry(entry, struct lapbethdev, node);
473 unregister_netdevice(lapbeth->axdev);
477 module_exit(lapbeth_cleanup_driver);
479 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
480 MODULE_DESCRIPTION("The unofficial LAPB over Ethernet driver");
481 MODULE_LICENSE("GPL");