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#) */
56 static LIST_HEAD(lapbeth_devices
);
58 static void lapbeth_connected(struct net_device
*dev
, int reason
);
59 static void lapbeth_disconnected(struct net_device
*dev
, int reason
);
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
, lockdep_rtnl_is_held()) {
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
, struct net_device
*orig_dev
)
90 struct lapbethdev
*lapbeth
;
92 if (dev_net(dev
) != &init_net
)
95 if ((skb
= skb_share_check(skb
, GFP_ATOMIC
)) == NULL
)
98 if (!pskb_may_pull(skb
, 2))
102 lapbeth
= lapbeth_get_x25_dev(dev
);
105 if (!netif_running(lapbeth
->axdev
))
108 len
= skb
->data
[0] + skb
->data
[1] * 256;
109 dev
->stats
.rx_packets
++;
110 dev
->stats
.rx_bytes
+= len
;
112 skb_pull(skb
, 2); /* Remove the length bytes */
113 skb_trim(skb
, len
); /* Set the length of the data */
115 if ((err
= lapb_data_received(lapbeth
->axdev
, skb
)) != LAPB_OK
) {
116 printk(KERN_DEBUG
"lapbether: lapb_data_received err - %d\n", err
);
130 static int lapbeth_data_indication(struct net_device
*dev
, struct sk_buff
*skb
)
134 if (skb_cow(skb
, 1)) {
142 *ptr
= X25_IFACE_DATA
;
144 skb
->protocol
= x25_type_trans(skb
, dev
);
145 return netif_rx(skb
);
149 * Send a LAPB frame via an ethernet interface
151 static netdev_tx_t
lapbeth_xmit(struct sk_buff
*skb
,
152 struct net_device
*dev
)
157 * Just to be *really* sure not to send anything if the interface
158 * is down, the ethernet device may have gone.
160 if (!netif_running(dev
))
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.
169 switch (skb
->data
[0]) {
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
);
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
);
192 if ((err
= lapb_data_request(dev
, skb
)) != LAPB_OK
) {
193 pr_err("lapb_data_request error - %d\n", err
);
203 static void lapbeth_data_transmit(struct net_device
*ndev
, struct sk_buff
*skb
)
205 struct lapbethdev
*lapbeth
= netdev_priv(ndev
);
207 struct net_device
*dev
;
210 ptr
= skb_push(skb
, 2);
215 ndev
->stats
.tx_packets
++;
216 ndev
->stats
.tx_bytes
+= size
;
218 skb
->dev
= dev
= lapbeth
->ethdev
;
220 skb
->protocol
= htons(ETH_P_DEC
);
222 skb_reset_network_header(skb
);
224 dev_hard_header(skb
, dev
, ETH_P_DEC
, bcast_addr
, NULL
, 0);
229 static void lapbeth_connected(struct net_device
*dev
, int reason
)
232 struct sk_buff
*skb
= dev_alloc_skb(1);
235 pr_err("out of memory\n");
239 ptr
= skb_put(skb
, 1);
240 *ptr
= X25_IFACE_CONNECT
;
242 skb
->protocol
= x25_type_trans(skb
, dev
);
246 static void lapbeth_disconnected(struct net_device
*dev
, int reason
)
249 struct sk_buff
*skb
= dev_alloc_skb(1);
252 pr_err("out of memory\n");
256 ptr
= skb_put(skb
, 1);
257 *ptr
= X25_IFACE_DISCONNECT
;
259 skb
->protocol
= x25_type_trans(skb
, dev
);
266 static int lapbeth_set_mac_address(struct net_device
*dev
, void *addr
)
268 struct sockaddr
*sa
= addr
;
269 memcpy(dev
->dev_addr
, sa
->sa_data
, dev
->addr_len
);
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
,
284 * open/close a device
286 static int lapbeth_open(struct net_device
*dev
)
290 if ((err
= lapb_register(dev
, &lapbeth_callbacks
)) != LAPB_OK
) {
291 pr_err("lapb_register error: %d\n", err
);
295 netif_start_queue(dev
);
299 static int lapbeth_close(struct net_device
*dev
)
303 netif_stop_queue(dev
);
305 if ((err
= lapb_unregister(dev
)) != LAPB_OK
)
306 pr_err("lapb_unregister error: %d\n", err
);
311 /* ------------------------------------------------------------------------ */
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
,
320 static void lapbeth_setup(struct net_device
*dev
)
322 dev
->netdev_ops
= &lapbeth_netdev_ops
;
323 dev
->needs_free_netdev
= true;
324 dev
->type
= ARPHRD_X25
;
325 dev
->hard_header_len
= 0;
331 * Setup a new device.
333 static int lapbeth_new_device(struct net_device
*dev
)
335 struct net_device
*ndev
;
336 struct lapbethdev
*lapbeth
;
341 ndev
= alloc_netdev(sizeof(*lapbeth
), "lapb%d", NET_NAME_UNKNOWN
,
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.
352 ndev
->needed_headroom
= -1 + 3 + 2 + dev
->hard_header_len
353 + dev
->needed_headroom
;
354 ndev
->needed_tailroom
= dev
->needed_tailroom
;
356 lapbeth
= netdev_priv(ndev
);
357 lapbeth
->axdev
= ndev
;
360 lapbeth
->ethdev
= dev
;
363 if (register_netdevice(ndev
))
366 list_add_rcu(&lapbeth
->node
, &lapbeth_devices
);
377 * Free a lapb network device.
379 static void lapbeth_free_device(struct lapbethdev
*lapbeth
)
381 dev_put(lapbeth
->ethdev
);
382 list_del_rcu(&lapbeth
->node
);
383 unregister_netdevice(lapbeth
->axdev
);
387 * Handle device status changes.
389 * Called from notifier with RTNL held.
391 static int lapbeth_device_event(struct notifier_block
*this,
392 unsigned long event
, void *ptr
)
394 struct lapbethdev
*lapbeth
;
395 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
397 if (dev_net(dev
) != &init_net
)
400 if (!dev_is_ethdev(dev
))
405 /* New ethernet device -> new LAPB interface */
406 if (lapbeth_get_x25_dev(dev
) == NULL
)
407 lapbeth_new_device(dev
);
410 /* ethernet device closed -> close LAPB interface */
411 lapbeth
= lapbeth_get_x25_dev(dev
);
413 dev_close(lapbeth
->axdev
);
415 case NETDEV_UNREGISTER
:
416 /* ethernet device disappears -> remove LAPB interface */
417 lapbeth
= lapbeth_get_x25_dev(dev
);
419 lapbeth_free_device(lapbeth
);
426 /* ------------------------------------------------------------------------ */
428 static struct packet_type lapbeth_packet_type __read_mostly
= {
429 .type
= cpu_to_be16(ETH_P_DEC
),
433 static struct notifier_block lapbeth_dev_notifier
= {
434 .notifier_call
= lapbeth_device_event
,
437 static const char banner
[] __initconst
=
438 KERN_INFO
"LAPB Ethernet driver version 0.02\n";
440 static int __init
lapbeth_init_driver(void)
442 dev_add_pack(&lapbeth_packet_type
);
444 register_netdevice_notifier(&lapbeth_dev_notifier
);
450 module_init(lapbeth_init_driver
);
452 static void __exit
lapbeth_cleanup_driver(void)
454 struct lapbethdev
*lapbeth
;
455 struct list_head
*entry
, *tmp
;
457 dev_remove_pack(&lapbeth_packet_type
);
458 unregister_netdevice_notifier(&lapbeth_dev_notifier
);
461 list_for_each_safe(entry
, tmp
, &lapbeth_devices
) {
462 lapbeth
= list_entry(entry
, struct lapbethdev
, node
);
464 dev_put(lapbeth
->ethdev
);
465 unregister_netdevice(lapbeth
->axdev
);
469 module_exit(lapbeth_cleanup_driver
);
471 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
472 MODULE_DESCRIPTION("The unofficial LAPB over Ethernet driver");
473 MODULE_LICENSE("GPL");