1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * G8BPQ compatible "AX.25 via ethernet" driver release 004 4 * 5 * This code REQUIRES 2.0.0 or higher/ NET3.029 6 * 7 * This is a "pseudo" network driver to allow AX.25 over Ethernet 8 * using G8BPQ encapsulation. It has been extracted from the protocol 9 * implementation because 10 * 11 * - things got unreadable within the protocol stack 12 * - to cure the protocol stack from "feature-ism" 13 * - a protocol implementation shouldn't need to know on 14 * which hardware it is running 15 * - user-level programs like the AX.25 utilities shouldn't 16 * need to know about the hardware. 17 * - IP over ethernet encapsulated AX.25 was impossible 18 * - rxecho.c did not work 19 * - to have room for extensions 20 * - it just deserves to "live" as an own driver 21 * 22 * This driver can use any ethernet destination address, and can be 23 * limited to accept frames from one dedicated ethernet card only. 24 * 25 * Note that the driver sets up the BPQ devices automagically on 26 * startup or (if started before the "insmod" of an ethernet device) 27 * on "ifconfig up". It hopefully will remove the BPQ on "rmmod"ing 28 * the ethernet device (in fact: as soon as another ethernet or bpq 29 * device gets "ifconfig"ured). 30 * 31 * I have heard that several people are thinking of experiments 32 * with highspeed packet radio using existing ethernet cards. 33 * Well, this driver is prepared for this purpose, just add 34 * your tx key control and a txdelay / tailtime algorithm, 35 * probably some buffering, and /voila/... 36 * 37 * History 38 * BPQ 001 Joerg(DL1BKE) Extracted BPQ code from AX.25 39 * protocol stack and added my own 40 * yet existing patches 41 * BPQ 002 Joerg(DL1BKE) Scan network device list on 42 * startup. 43 * BPQ 003 Joerg(DL1BKE) Ethernet destination address 44 * and accepted source address 45 * can be configured by an ioctl() 46 * call. 47 * Fixed to match Linux networking 48 * changes - 2.1.15. 49 * BPQ 004 Joerg(DL1BKE) Fixed to not lock up on ifconfig. 50 */ 51 52 #include <linux/errno.h> 53 #include <linux/types.h> 54 #include <linux/socket.h> 55 #include <linux/in.h> 56 #include <linux/kernel.h> 57 #include <linux/string.h> 58 #include <linux/net.h> 59 #include <linux/slab.h> 60 #include <net/ax25.h> 61 #include <linux/inet.h> 62 #include <linux/netdevice.h> 63 #include <linux/etherdevice.h> 64 #include <linux/if_arp.h> 65 #include <linux/skbuff.h> 66 #include <net/sock.h> 67 #include <linux/uaccess.h> 68 #include <linux/mm.h> 69 #include <linux/interrupt.h> 70 #include <linux/notifier.h> 71 #include <linux/proc_fs.h> 72 #include <linux/seq_file.h> 73 #include <linux/stat.h> 74 #include <linux/module.h> 75 #include <linux/init.h> 76 #include <linux/rtnetlink.h> 77 78 #include <net/ip.h> 79 #include <net/arp.h> 80 #include <net/net_namespace.h> 81 82 #include <linux/bpqether.h> 83 84 static const char banner[] __initconst = KERN_INFO \ 85 "AX.25: bpqether driver version 004\n"; 86 87 static int bpq_rcv(struct sk_buff *, struct net_device *, struct packet_type *, struct net_device *); 88 static int bpq_device_event(struct notifier_block *, unsigned long, void *); 89 90 static struct packet_type bpq_packet_type __read_mostly = { 91 .type = cpu_to_be16(ETH_P_BPQ), 92 .func = bpq_rcv, 93 }; 94 95 static struct notifier_block bpq_dev_notifier = { 96 .notifier_call = bpq_device_event, 97 }; 98 99 100 struct bpqdev { 101 struct list_head bpq_list; /* list of bpq devices chain */ 102 struct net_device *ethdev; /* link to ethernet device */ 103 struct net_device *axdev; /* bpq device (bpq#) */ 104 char dest_addr[6]; /* ether destination address */ 105 char acpt_addr[6]; /* accept ether frames from this address only */ 106 }; 107 108 static LIST_HEAD(bpq_devices); 109 110 /* 111 * bpqether network devices are paired with ethernet devices below them, so 112 * form a special "super class" of normal ethernet devices; split their locks 113 * off into a separate class since they always nest. 114 */ 115 static struct lock_class_key bpq_netdev_xmit_lock_key; 116 117 static void bpq_set_lockdep_class_one(struct net_device *dev, 118 struct netdev_queue *txq, 119 void *_unused) 120 { 121 lockdep_set_class(&txq->_xmit_lock, &bpq_netdev_xmit_lock_key); 122 } 123 124 static void bpq_set_lockdep_class(struct net_device *dev) 125 { 126 netdev_for_each_tx_queue(dev, bpq_set_lockdep_class_one, NULL); 127 } 128 129 /* ------------------------------------------------------------------------ */ 130 131 132 /* 133 * Get the ethernet device for a BPQ device 134 */ 135 static inline struct net_device *bpq_get_ether_dev(struct net_device *dev) 136 { 137 struct bpqdev *bpq = netdev_priv(dev); 138 139 return bpq ? bpq->ethdev : NULL; 140 } 141 142 /* 143 * Get the BPQ device for the ethernet device 144 */ 145 static inline struct net_device *bpq_get_ax25_dev(struct net_device *dev) 146 { 147 struct bpqdev *bpq; 148 149 list_for_each_entry_rcu(bpq, &bpq_devices, bpq_list) { 150 if (bpq->ethdev == dev) 151 return bpq->axdev; 152 } 153 return NULL; 154 } 155 156 static inline int dev_is_ethdev(struct net_device *dev) 157 { 158 return dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5); 159 } 160 161 /* ------------------------------------------------------------------------ */ 162 163 164 /* 165 * Receive an AX.25 frame via an ethernet interface. 166 */ 167 static int bpq_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *ptype, struct net_device *orig_dev) 168 { 169 int len; 170 char * ptr; 171 struct ethhdr *eth; 172 struct bpqdev *bpq; 173 174 if (!net_eq(dev_net(dev), &init_net)) 175 goto drop; 176 177 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) 178 return NET_RX_DROP; 179 180 if (!pskb_may_pull(skb, sizeof(struct ethhdr))) 181 goto drop; 182 183 rcu_read_lock(); 184 dev = bpq_get_ax25_dev(dev); 185 186 if (dev == NULL || !netif_running(dev)) 187 goto drop_unlock; 188 189 /* 190 * if we want to accept frames from just one ethernet device 191 * we check the source address of the sender. 192 */ 193 194 bpq = netdev_priv(dev); 195 196 eth = eth_hdr(skb); 197 198 if (!(bpq->acpt_addr[0] & 0x01) && 199 !ether_addr_equal(eth->h_source, bpq->acpt_addr)) 200 goto drop_unlock; 201 202 if (skb_cow(skb, sizeof(struct ethhdr))) 203 goto drop_unlock; 204 205 len = skb->data[0] + skb->data[1] * 256 - 5; 206 207 skb_pull(skb, 2); /* Remove the length bytes */ 208 skb_trim(skb, len); /* Set the length of the data */ 209 210 dev->stats.rx_packets++; 211 dev->stats.rx_bytes += len; 212 213 ptr = skb_push(skb, 1); 214 *ptr = 0; 215 216 skb->protocol = ax25_type_trans(skb, dev); 217 netif_rx(skb); 218 unlock: 219 220 rcu_read_unlock(); 221 222 return 0; 223 drop_unlock: 224 kfree_skb(skb); 225 goto unlock; 226 227 drop: 228 kfree_skb(skb); 229 return 0; 230 } 231 232 /* 233 * Send an AX.25 frame via an ethernet interface 234 */ 235 static netdev_tx_t bpq_xmit(struct sk_buff *skb, struct net_device *dev) 236 { 237 unsigned char *ptr; 238 struct bpqdev *bpq; 239 struct net_device *orig_dev; 240 int size; 241 242 if (skb->protocol == htons(ETH_P_IP)) 243 return ax25_ip_xmit(skb); 244 245 /* 246 * Just to be *really* sure not to send anything if the interface 247 * is down, the ethernet device may have gone. 248 */ 249 if (!netif_running(dev)) { 250 kfree_skb(skb); 251 return NETDEV_TX_OK; 252 } 253 254 skb_pull(skb, 1); /* Drop KISS byte */ 255 size = skb->len; 256 257 /* 258 * We're about to mess with the skb which may still shared with the 259 * generic networking code so unshare and ensure it's got enough 260 * space for the BPQ headers. 261 */ 262 if (skb_cow(skb, AX25_BPQ_HEADER_LEN)) { 263 if (net_ratelimit()) 264 pr_err("bpqether: out of memory\n"); 265 kfree_skb(skb); 266 267 return NETDEV_TX_OK; 268 } 269 270 ptr = skb_push(skb, 2); /* Make space for length */ 271 272 *ptr++ = (size + 5) % 256; 273 *ptr++ = (size + 5) / 256; 274 275 bpq = netdev_priv(dev); 276 277 orig_dev = dev; 278 if ((dev = bpq_get_ether_dev(dev)) == NULL) { 279 orig_dev->stats.tx_dropped++; 280 kfree_skb(skb); 281 return NETDEV_TX_OK; 282 } 283 284 skb->protocol = ax25_type_trans(skb, dev); 285 skb_reset_network_header(skb); 286 dev_hard_header(skb, dev, ETH_P_BPQ, bpq->dest_addr, NULL, 0); 287 dev->stats.tx_packets++; 288 dev->stats.tx_bytes+=skb->len; 289 290 dev_queue_xmit(skb); 291 netif_wake_queue(dev); 292 return NETDEV_TX_OK; 293 } 294 295 /* 296 * Set AX.25 callsign 297 */ 298 static int bpq_set_mac_address(struct net_device *dev, void *addr) 299 { 300 struct sockaddr *sa = (struct sockaddr *)addr; 301 302 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len); 303 304 return 0; 305 } 306 307 /* Ioctl commands 308 * 309 * SIOCSBPQETHOPT reserved for enhancements 310 * SIOCSBPQETHADDR set the destination and accepted 311 * source ethernet address (broadcast 312 * or multicast: accept all) 313 */ 314 static int bpq_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 315 { 316 struct bpq_ethaddr __user *ethaddr = ifr->ifr_data; 317 struct bpqdev *bpq = netdev_priv(dev); 318 struct bpq_req req; 319 320 if (!capable(CAP_NET_ADMIN)) 321 return -EPERM; 322 323 switch (cmd) { 324 case SIOCSBPQETHOPT: 325 if (copy_from_user(&req, ifr->ifr_data, sizeof(struct bpq_req))) 326 return -EFAULT; 327 switch (req.cmd) { 328 case SIOCGBPQETHPARAM: 329 case SIOCSBPQETHPARAM: 330 default: 331 return -EINVAL; 332 } 333 334 break; 335 336 case SIOCSBPQETHADDR: 337 if (copy_from_user(bpq->dest_addr, ethaddr->destination, ETH_ALEN)) 338 return -EFAULT; 339 if (copy_from_user(bpq->acpt_addr, ethaddr->accept, ETH_ALEN)) 340 return -EFAULT; 341 break; 342 343 default: 344 return -EINVAL; 345 } 346 347 return 0; 348 } 349 350 /* 351 * open/close a device 352 */ 353 static int bpq_open(struct net_device *dev) 354 { 355 netif_start_queue(dev); 356 return 0; 357 } 358 359 static int bpq_close(struct net_device *dev) 360 { 361 netif_stop_queue(dev); 362 return 0; 363 } 364 365 366 /* ------------------------------------------------------------------------ */ 367 368 369 /* 370 * Proc filesystem 371 */ 372 static void *bpq_seq_start(struct seq_file *seq, loff_t *pos) 373 __acquires(RCU) 374 { 375 int i = 1; 376 struct bpqdev *bpqdev; 377 378 rcu_read_lock(); 379 380 if (*pos == 0) 381 return SEQ_START_TOKEN; 382 383 list_for_each_entry_rcu(bpqdev, &bpq_devices, bpq_list) { 384 if (i == *pos) 385 return bpqdev; 386 } 387 return NULL; 388 } 389 390 static void *bpq_seq_next(struct seq_file *seq, void *v, loff_t *pos) 391 { 392 struct list_head *p; 393 struct bpqdev *bpqdev = v; 394 395 ++*pos; 396 397 if (v == SEQ_START_TOKEN) 398 p = rcu_dereference(list_next_rcu(&bpq_devices)); 399 else 400 p = rcu_dereference(list_next_rcu(&bpqdev->bpq_list)); 401 402 return (p == &bpq_devices) ? NULL 403 : list_entry(p, struct bpqdev, bpq_list); 404 } 405 406 static void bpq_seq_stop(struct seq_file *seq, void *v) 407 __releases(RCU) 408 { 409 rcu_read_unlock(); 410 } 411 412 413 static int bpq_seq_show(struct seq_file *seq, void *v) 414 { 415 if (v == SEQ_START_TOKEN) 416 seq_puts(seq, 417 "dev ether destination accept from\n"); 418 else { 419 const struct bpqdev *bpqdev = v; 420 421 seq_printf(seq, "%-5s %-10s %pM ", 422 bpqdev->axdev->name, bpqdev->ethdev->name, 423 bpqdev->dest_addr); 424 425 if (is_multicast_ether_addr(bpqdev->acpt_addr)) 426 seq_printf(seq, "*\n"); 427 else 428 seq_printf(seq, "%pM\n", bpqdev->acpt_addr); 429 430 } 431 return 0; 432 } 433 434 static const struct seq_operations bpq_seqops = { 435 .start = bpq_seq_start, 436 .next = bpq_seq_next, 437 .stop = bpq_seq_stop, 438 .show = bpq_seq_show, 439 }; 440 441 /* ------------------------------------------------------------------------ */ 442 443 static const struct net_device_ops bpq_netdev_ops = { 444 .ndo_open = bpq_open, 445 .ndo_stop = bpq_close, 446 .ndo_start_xmit = bpq_xmit, 447 .ndo_set_mac_address = bpq_set_mac_address, 448 .ndo_do_ioctl = bpq_ioctl, 449 }; 450 451 static void bpq_setup(struct net_device *dev) 452 { 453 dev->netdev_ops = &bpq_netdev_ops; 454 dev->needs_free_netdev = true; 455 456 memcpy(dev->broadcast, &ax25_bcast, AX25_ADDR_LEN); 457 memcpy(dev->dev_addr, &ax25_defaddr, AX25_ADDR_LEN); 458 459 dev->flags = 0; 460 dev->features = NETIF_F_LLTX; /* Allow recursion */ 461 462 #if IS_ENABLED(CONFIG_AX25) 463 dev->header_ops = &ax25_header_ops; 464 #endif 465 466 dev->type = ARPHRD_AX25; 467 dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN; 468 dev->mtu = AX25_DEF_PACLEN; 469 dev->addr_len = AX25_ADDR_LEN; 470 471 } 472 473 /* 474 * Setup a new device. 475 */ 476 static int bpq_new_device(struct net_device *edev) 477 { 478 int err; 479 struct net_device *ndev; 480 struct bpqdev *bpq; 481 482 ndev = alloc_netdev(sizeof(struct bpqdev), "bpq%d", NET_NAME_UNKNOWN, 483 bpq_setup); 484 if (!ndev) 485 return -ENOMEM; 486 487 488 bpq = netdev_priv(ndev); 489 dev_hold(edev); 490 bpq->ethdev = edev; 491 bpq->axdev = ndev; 492 493 eth_broadcast_addr(bpq->dest_addr); 494 eth_broadcast_addr(bpq->acpt_addr); 495 496 err = register_netdevice(ndev); 497 if (err) 498 goto error; 499 bpq_set_lockdep_class(ndev); 500 501 /* List protected by RTNL */ 502 list_add_rcu(&bpq->bpq_list, &bpq_devices); 503 return 0; 504 505 error: 506 dev_put(edev); 507 free_netdev(ndev); 508 return err; 509 510 } 511 512 static void bpq_free_device(struct net_device *ndev) 513 { 514 struct bpqdev *bpq = netdev_priv(ndev); 515 516 dev_put(bpq->ethdev); 517 list_del_rcu(&bpq->bpq_list); 518 519 unregister_netdevice(ndev); 520 } 521 522 /* 523 * Handle device status changes. 524 */ 525 static int bpq_device_event(struct notifier_block *this, 526 unsigned long event, void *ptr) 527 { 528 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 529 530 if (!net_eq(dev_net(dev), &init_net)) 531 return NOTIFY_DONE; 532 533 if (!dev_is_ethdev(dev)) 534 return NOTIFY_DONE; 535 536 switch (event) { 537 case NETDEV_UP: /* new ethernet device -> new BPQ interface */ 538 if (bpq_get_ax25_dev(dev) == NULL) 539 bpq_new_device(dev); 540 break; 541 542 case NETDEV_DOWN: /* ethernet device closed -> close BPQ interface */ 543 if ((dev = bpq_get_ax25_dev(dev)) != NULL) 544 dev_close(dev); 545 break; 546 547 case NETDEV_UNREGISTER: /* ethernet device removed -> free BPQ interface */ 548 if ((dev = bpq_get_ax25_dev(dev)) != NULL) 549 bpq_free_device(dev); 550 break; 551 default: 552 break; 553 } 554 555 return NOTIFY_DONE; 556 } 557 558 559 /* ------------------------------------------------------------------------ */ 560 561 /* 562 * Initialize driver. To be called from af_ax25 if not compiled as a 563 * module 564 */ 565 static int __init bpq_init_driver(void) 566 { 567 #ifdef CONFIG_PROC_FS 568 if (!proc_create_seq("bpqether", 0444, init_net.proc_net, &bpq_seqops)) { 569 printk(KERN_ERR 570 "bpq: cannot create /proc/net/bpqether entry.\n"); 571 return -ENOENT; 572 } 573 #endif /* CONFIG_PROC_FS */ 574 575 dev_add_pack(&bpq_packet_type); 576 577 register_netdevice_notifier(&bpq_dev_notifier); 578 579 printk(banner); 580 581 return 0; 582 } 583 584 static void __exit bpq_cleanup_driver(void) 585 { 586 struct bpqdev *bpq; 587 588 dev_remove_pack(&bpq_packet_type); 589 590 unregister_netdevice_notifier(&bpq_dev_notifier); 591 592 remove_proc_entry("bpqether", init_net.proc_net); 593 594 rtnl_lock(); 595 while (!list_empty(&bpq_devices)) { 596 bpq = list_entry(bpq_devices.next, struct bpqdev, bpq_list); 597 bpq_free_device(bpq->axdev); 598 } 599 rtnl_unlock(); 600 } 601 602 MODULE_AUTHOR("Joerg Reuter DL1BKE <jreuter@yaina.de>"); 603 MODULE_DESCRIPTION("Transmit and receive AX.25 packets over Ethernet"); 604 MODULE_LICENSE("GPL"); 605 module_init(bpq_init_driver); 606 module_exit(bpq_cleanup_driver); 607