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