1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * "LAPB via ethernet" driver release 001 4 * 5 * This code REQUIRES 2.1.15 or higher/ NET3.038 6 * 7 * This is a "pseudo" network driver to allow LAPB over Ethernet. 8 * 9 * This driver can use any ethernet destination address, and can be 10 * limited to accept frames from one dedicated ethernet card only. 11 * 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 16 */ 17 18#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 19 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> 41 42#include <net/x25device.h> 43 44static const u8 bcast_addr[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; 45 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 49 50struct lapbethdev { 51 struct list_head node; 52 struct net_device *ethdev; /* link to ethernet device */ 53 struct net_device *axdev; /* lapbeth device (lapb#) */ 54 bool up; 55 spinlock_t up_lock; /* Protects "up" */ 56}; 57 58static LIST_HEAD(lapbeth_devices); 59 60/* ------------------------------------------------------------------------ */ 61 62/* 63 * Get the LAPB device for the ethernet device 64 */ 65static struct lapbethdev *lapbeth_get_x25_dev(struct net_device *dev) 66{ 67 struct lapbethdev *lapbeth; 68 69 list_for_each_entry_rcu(lapbeth, &lapbeth_devices, node, lockdep_rtnl_is_held()) { 70 if (lapbeth->ethdev == dev) 71 return lapbeth; 72 } 73 return NULL; 74} 75 76static __inline__ int dev_is_ethdev(struct net_device *dev) 77{ 78 return dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5); 79} 80 81/* ------------------------------------------------------------------------ */ 82 83/* 84 * Receive a LAPB frame via an ethernet interface. 85 */ 86static int lapbeth_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *ptype, struct net_device *orig_dev) 87{ 88 int len, err; 89 struct lapbethdev *lapbeth; 90 91 if (dev_net(dev) != &init_net) 92 goto drop; 93 94 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) 95 return NET_RX_DROP; 96 97 if (!pskb_may_pull(skb, 2)) 98 goto drop; 99 100 rcu_read_lock(); 101 lapbeth = lapbeth_get_x25_dev(dev); 102 if (!lapbeth) 103 goto drop_unlock_rcu; 104 spin_lock_bh(&lapbeth->up_lock); 105 if (!lapbeth->up) 106 goto drop_unlock; 107 108 len = skb->data[0] + skb->data[1] * 256; 109 dev->stats.rx_packets++; 110 dev->stats.rx_bytes += len; 111 112 skb_pull(skb, 2); /* Remove the length bytes */ 113 skb_trim(skb, len); /* Set the length of the data */ 114 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; 118 } 119out: 120 spin_unlock_bh(&lapbeth->up_lock); 121 rcu_read_unlock(); 122 return 0; 123drop_unlock: 124 kfree_skb(skb); 125 goto out; 126drop_unlock_rcu: 127 rcu_read_unlock(); 128drop: 129 kfree_skb(skb); 130 return 0; 131} 132 133static int lapbeth_data_indication(struct net_device *dev, struct sk_buff *skb) 134{ 135 unsigned char *ptr; 136 137 if (skb_cow(skb, 1)) { 138 kfree_skb(skb); 139 return NET_RX_DROP; 140 } 141 142 skb_push(skb, 1); 143 144 ptr = skb->data; 145 *ptr = X25_IFACE_DATA; 146 147 skb->protocol = x25_type_trans(skb, dev); 148 return netif_rx(skb); 149} 150 151/* 152 * Send a LAPB frame via an ethernet interface 153 */ 154static netdev_tx_t lapbeth_xmit(struct sk_buff *skb, 155 struct net_device *dev) 156{ 157 struct lapbethdev *lapbeth = netdev_priv(dev); 158 int err; 159 160 spin_lock_bh(&lapbeth->up_lock); 161 if (!lapbeth->up) 162 goto drop; 163 164 /* There should be a pseudo header of 1 byte added by upper layers. 165 * Check to make sure it is there before reading it. 166 */ 167 if (skb->len < 1) 168 goto drop; 169 170 switch (skb->data[0]) { 171 case X25_IFACE_DATA: 172 break; 173 case X25_IFACE_CONNECT: 174 if ((err = lapb_connect_request(dev)) != LAPB_OK) 175 pr_err("lapb_connect_request error: %d\n", err); 176 goto drop; 177 case X25_IFACE_DISCONNECT: 178 if ((err = lapb_disconnect_request(dev)) != LAPB_OK) 179 pr_err("lapb_disconnect_request err: %d\n", err); 180 fallthrough; 181 default: 182 goto drop; 183 } 184 185 skb_pull(skb, 1); 186 187 if ((err = lapb_data_request(dev, skb)) != LAPB_OK) { 188 pr_err("lapb_data_request error - %d\n", err); 189 goto drop; 190 } 191out: 192 spin_unlock_bh(&lapbeth->up_lock); 193 return NETDEV_TX_OK; 194drop: 195 kfree_skb(skb); 196 goto out; 197} 198 199static void lapbeth_data_transmit(struct net_device *ndev, struct sk_buff *skb) 200{ 201 struct lapbethdev *lapbeth = netdev_priv(ndev); 202 unsigned char *ptr; 203 struct net_device *dev; 204 int size = skb->len; 205 206 ptr = skb_push(skb, 2); 207 208 *ptr++ = size % 256; 209 *ptr++ = size / 256; 210 211 ndev->stats.tx_packets++; 212 ndev->stats.tx_bytes += size; 213 214 skb->dev = dev = lapbeth->ethdev; 215 216 skb->protocol = htons(ETH_P_DEC); 217 218 skb_reset_network_header(skb); 219 220 dev_hard_header(skb, dev, ETH_P_DEC, bcast_addr, NULL, 0); 221 222 dev_queue_xmit(skb); 223} 224 225static void lapbeth_connected(struct net_device *dev, int reason) 226{ 227 unsigned char *ptr; 228 struct sk_buff *skb = dev_alloc_skb(1); 229 230 if (!skb) { 231 pr_err("out of memory\n"); 232 return; 233 } 234 235 ptr = skb_put(skb, 1); 236 *ptr = X25_IFACE_CONNECT; 237 238 skb->protocol = x25_type_trans(skb, dev); 239 netif_rx(skb); 240} 241 242static void lapbeth_disconnected(struct net_device *dev, int reason) 243{ 244 unsigned char *ptr; 245 struct sk_buff *skb = dev_alloc_skb(1); 246 247 if (!skb) { 248 pr_err("out of memory\n"); 249 return; 250 } 251 252 ptr = skb_put(skb, 1); 253 *ptr = X25_IFACE_DISCONNECT; 254 255 skb->protocol = x25_type_trans(skb, dev); 256 netif_rx(skb); 257} 258 259/* 260 * Set AX.25 callsign 261 */ 262static int lapbeth_set_mac_address(struct net_device *dev, void *addr) 263{ 264 struct sockaddr *sa = addr; 265 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len); 266 return 0; 267} 268 269 270static const struct lapb_register_struct lapbeth_callbacks = { 271 .connect_confirmation = lapbeth_connected, 272 .connect_indication = lapbeth_connected, 273 .disconnect_confirmation = lapbeth_disconnected, 274 .disconnect_indication = lapbeth_disconnected, 275 .data_indication = lapbeth_data_indication, 276 .data_transmit = lapbeth_data_transmit, 277}; 278 279/* 280 * open/close a device 281 */ 282static int lapbeth_open(struct net_device *dev) 283{ 284 struct lapbethdev *lapbeth = netdev_priv(dev); 285 int err; 286 287 if ((err = lapb_register(dev, &lapbeth_callbacks)) != LAPB_OK) { 288 pr_err("lapb_register error: %d\n", err); 289 return -ENODEV; 290 } 291 292 spin_lock_bh(&lapbeth->up_lock); 293 lapbeth->up = true; 294 spin_unlock_bh(&lapbeth->up_lock); 295 296 return 0; 297} 298 299static int lapbeth_close(struct net_device *dev) 300{ 301 struct lapbethdev *lapbeth = netdev_priv(dev); 302 int err; 303 304 spin_lock_bh(&lapbeth->up_lock); 305 lapbeth->up = false; 306 spin_unlock_bh(&lapbeth->up_lock); 307 308 if ((err = lapb_unregister(dev)) != LAPB_OK) 309 pr_err("lapb_unregister error: %d\n", err); 310 311 return 0; 312} 313 314/* ------------------------------------------------------------------------ */ 315 316static const struct net_device_ops lapbeth_netdev_ops = { 317 .ndo_open = lapbeth_open, 318 .ndo_stop = lapbeth_close, 319 .ndo_start_xmit = lapbeth_xmit, 320 .ndo_set_mac_address = lapbeth_set_mac_address, 321}; 322 323static void lapbeth_setup(struct net_device *dev) 324{ 325 dev->netdev_ops = &lapbeth_netdev_ops; 326 dev->needs_free_netdev = true; 327 dev->type = ARPHRD_X25; 328 dev->hard_header_len = 0; 329 dev->mtu = 1000; 330 dev->addr_len = 0; 331} 332 333/* 334 * Setup a new device. 335 */ 336static int lapbeth_new_device(struct net_device *dev) 337{ 338 struct net_device *ndev; 339 struct lapbethdev *lapbeth; 340 int rc = -ENOMEM; 341 342 ASSERT_RTNL(); 343 344 if (dev->type != ARPHRD_ETHER) 345 return -EINVAL; 346 347 ndev = alloc_netdev(sizeof(*lapbeth), "lapb%d", NET_NAME_UNKNOWN, 348 lapbeth_setup); 349 if (!ndev) 350 goto out; 351 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. 357 */ 358 ndev->needed_headroom = -1 + 3 + 2 + dev->hard_header_len 359 + dev->needed_headroom; 360 ndev->needed_tailroom = dev->needed_tailroom; 361 362 lapbeth = netdev_priv(ndev); 363 lapbeth->axdev = ndev; 364 365 dev_hold(dev); 366 lapbeth->ethdev = dev; 367 368 lapbeth->up = false; 369 spin_lock_init(&lapbeth->up_lock); 370 371 rc = -EIO; 372 if (register_netdevice(ndev)) 373 goto fail; 374 375 list_add_rcu(&lapbeth->node, &lapbeth_devices); 376 rc = 0; 377out: 378 return rc; 379fail: 380 dev_put(dev); 381 free_netdev(ndev); 382 goto out; 383} 384 385/* 386 * Free a lapb network device. 387 */ 388static void lapbeth_free_device(struct lapbethdev *lapbeth) 389{ 390 dev_put(lapbeth->ethdev); 391 list_del_rcu(&lapbeth->node); 392 unregister_netdevice(lapbeth->axdev); 393} 394 395/* 396 * Handle device status changes. 397 * 398 * Called from notifier with RTNL held. 399 */ 400static int lapbeth_device_event(struct notifier_block *this, 401 unsigned long event, void *ptr) 402{ 403 struct lapbethdev *lapbeth; 404 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 405 406 if (dev_net(dev) != &init_net) 407 return NOTIFY_DONE; 408 409 if (!dev_is_ethdev(dev) && !lapbeth_get_x25_dev(dev)) 410 return NOTIFY_DONE; 411 412 switch (event) { 413 case NETDEV_UP: 414 /* New ethernet device -> new LAPB interface */ 415 if (lapbeth_get_x25_dev(dev) == NULL) 416 lapbeth_new_device(dev); 417 break; 418 case NETDEV_DOWN: 419 /* ethernet device closed -> close LAPB interface */ 420 lapbeth = lapbeth_get_x25_dev(dev); 421 if (lapbeth) 422 dev_close(lapbeth->axdev); 423 break; 424 case NETDEV_UNREGISTER: 425 /* ethernet device disappears -> remove LAPB interface */ 426 lapbeth = lapbeth_get_x25_dev(dev); 427 if (lapbeth) 428 lapbeth_free_device(lapbeth); 429 break; 430 } 431 432 return NOTIFY_DONE; 433} 434 435/* ------------------------------------------------------------------------ */ 436 437static struct packet_type lapbeth_packet_type __read_mostly = { 438 .type = cpu_to_be16(ETH_P_DEC), 439 .func = lapbeth_rcv, 440}; 441 442static struct notifier_block lapbeth_dev_notifier = { 443 .notifier_call = lapbeth_device_event, 444}; 445 446static const char banner[] __initconst = 447 KERN_INFO "LAPB Ethernet driver version 0.02\n"; 448 449static int __init lapbeth_init_driver(void) 450{ 451 dev_add_pack(&lapbeth_packet_type); 452 453 register_netdevice_notifier(&lapbeth_dev_notifier); 454 455 printk(banner); 456 457 return 0; 458} 459module_init(lapbeth_init_driver); 460 461static void __exit lapbeth_cleanup_driver(void) 462{ 463 struct lapbethdev *lapbeth; 464 struct list_head *entry, *tmp; 465 466 dev_remove_pack(&lapbeth_packet_type); 467 unregister_netdevice_notifier(&lapbeth_dev_notifier); 468 469 rtnl_lock(); 470 list_for_each_safe(entry, tmp, &lapbeth_devices) { 471 lapbeth = list_entry(entry, struct lapbethdev, node); 472 473 dev_put(lapbeth->ethdev); 474 unregister_netdevice(lapbeth->axdev); 475 } 476 rtnl_unlock(); 477} 478module_exit(lapbeth_cleanup_driver); 479 480MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>"); 481MODULE_DESCRIPTION("The unofficial LAPB over Ethernet driver"); 482MODULE_LICENSE("GPL"); 483