WIP FPC-III support
[linux/fpc-iii.git] / drivers / net / wan / lapbether.c
blob605fe555e157d59e2e76d7b58cf8abd39110b183
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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.
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
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
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
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#) */
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)
72 return lapbeth;
74 return NULL;
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)
89 int len, err;
90 struct lapbethdev *lapbeth;
92 if (dev_net(dev) != &init_net)
93 goto drop;
95 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
96 return NET_RX_DROP;
98 if (!pskb_may_pull(skb, 2))
99 goto drop;
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;
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);
117 goto drop_unlock;
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;
130 static int lapbeth_data_indication(struct net_device *dev, struct sk_buff *skb)
132 unsigned char *ptr;
134 if (skb_cow(skb, 1)) {
135 kfree_skb(skb);
136 return NET_RX_DROP;
139 skb_push(skb, 1);
141 ptr = skb->data;
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)
154 int err;
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))
161 goto drop;
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.
166 if (skb->len < 1)
167 goto drop;
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;
190 skb_pull(skb, 1);
192 if ((err = lapb_data_request(dev, skb)) != LAPB_OK) {
193 pr_err("lapb_data_request error - %d\n", err);
194 goto drop;
196 out:
197 return NETDEV_TX_OK;
198 drop:
199 kfree_skb(skb);
200 goto out;
203 static void lapbeth_data_transmit(struct net_device *ndev, struct sk_buff *skb)
205 struct lapbethdev *lapbeth = netdev_priv(ndev);
206 unsigned char *ptr;
207 struct net_device *dev;
208 int size = skb->len;
210 ptr = skb_push(skb, 2);
212 *ptr++ = size % 256;
213 *ptr++ = size / 256;
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);
226 dev_queue_xmit(skb);
229 static void lapbeth_connected(struct net_device *dev, int reason)
231 unsigned char *ptr;
232 struct sk_buff *skb = dev_alloc_skb(1);
234 if (!skb) {
235 pr_err("out of memory\n");
236 return;
239 ptr = skb_put(skb, 1);
240 *ptr = X25_IFACE_CONNECT;
242 skb->protocol = x25_type_trans(skb, dev);
243 netif_rx(skb);
246 static void lapbeth_disconnected(struct net_device *dev, int reason)
248 unsigned char *ptr;
249 struct sk_buff *skb = dev_alloc_skb(1);
251 if (!skb) {
252 pr_err("out of memory\n");
253 return;
256 ptr = skb_put(skb, 1);
257 *ptr = X25_IFACE_DISCONNECT;
259 skb->protocol = x25_type_trans(skb, dev);
260 netif_rx(skb);
264 * Set AX.25 callsign
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);
270 return 0;
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)
288 int err;
290 if ((err = lapb_register(dev, &lapbeth_callbacks)) != LAPB_OK) {
291 pr_err("lapb_register error: %d\n", err);
292 return -ENODEV;
295 netif_start_queue(dev);
296 return 0;
299 static int lapbeth_close(struct net_device *dev)
301 int err;
303 netif_stop_queue(dev);
305 if ((err = lapb_unregister(dev)) != LAPB_OK)
306 pr_err("lapb_unregister error: %d\n", err);
308 return 0;
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;
326 dev->mtu = 1000;
327 dev->addr_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;
337 int rc = -ENOMEM;
339 ASSERT_RTNL();
341 ndev = alloc_netdev(sizeof(*lapbeth), "lapb%d", NET_NAME_UNKNOWN,
342 lapbeth_setup);
343 if (!ndev)
344 goto out;
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;
359 dev_hold(dev);
360 lapbeth->ethdev = dev;
362 rc = -EIO;
363 if (register_netdevice(ndev))
364 goto fail;
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;
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)
398 return NOTIFY_DONE;
400 if (!dev_is_ethdev(dev))
401 return NOTIFY_DONE;
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;
423 return NOTIFY_DONE;
426 /* ------------------------------------------------------------------------ */
428 static struct packet_type lapbeth_packet_type __read_mostly = {
429 .type = cpu_to_be16(ETH_P_DEC),
430 .func = lapbeth_rcv,
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);
446 printk(banner);
448 return 0;
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);
460 rtnl_lock();
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);
467 rtnl_unlock();
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");