Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  G8BPQ compatible "AX.25 via ethernet" driver release 004
0004  *
0005  *  This code REQUIRES 2.0.0 or higher/ NET3.029
0006  *
0007  *  This is a "pseudo" network driver to allow AX.25 over Ethernet
0008  *  using G8BPQ encapsulation. It has been extracted from the protocol
0009  *  implementation because
0010  *
0011  *      - things got unreadable within the protocol stack
0012  *      - to cure the protocol stack from "feature-ism"
0013  *      - a protocol implementation shouldn't need to know on
0014  *        which hardware it is running
0015  *      - user-level programs like the AX.25 utilities shouldn't
0016  *        need to know about the hardware.
0017  *      - IP over ethernet encapsulated AX.25 was impossible
0018  *      - rxecho.c did not work
0019  *      - to have room for extensions
0020  *      - it just deserves to "live" as an own driver
0021  *
0022  *  This driver can use any ethernet destination address, and can be
0023  *  limited to accept frames from one dedicated ethernet card only.
0024  *
0025  *  Note that the driver sets up the BPQ devices automagically on
0026  *  startup or (if started before the "insmod" of an ethernet device)
0027  *  on "ifconfig up". It hopefully will remove the BPQ on "rmmod"ing
0028  *  the ethernet device (in fact: as soon as another ethernet or bpq
0029  *  device gets "ifconfig"ured).
0030  *
0031  *  I have heard that several people are thinking of experiments
0032  *  with highspeed packet radio using existing ethernet cards.
0033  *  Well, this driver is prepared for this purpose, just add
0034  *  your tx key control and a txdelay / tailtime algorithm,
0035  *  probably some buffering, and /voila/...
0036  *
0037  *  History
0038  *  BPQ   001   Joerg(DL1BKE)       Extracted BPQ code from AX.25
0039  *                      protocol stack and added my own
0040  *                      yet existing patches
0041  *  BPQ   002   Joerg(DL1BKE)       Scan network device list on
0042  *                      startup.
0043  *  BPQ   003   Joerg(DL1BKE)       Ethernet destination address
0044  *                      and accepted source address
0045  *                      can be configured by an ioctl()
0046  *                      call.
0047  *                      Fixed to match Linux networking
0048  *                      changes - 2.1.15.
0049  *  BPQ   004   Joerg(DL1BKE)       Fixed to not lock up on ifconfig.
0050  */
0051 
0052 #include <linux/errno.h>
0053 #include <linux/types.h>
0054 #include <linux/socket.h>
0055 #include <linux/in.h>
0056 #include <linux/kernel.h>
0057 #include <linux/string.h>
0058 #include <linux/net.h>
0059 #include <linux/slab.h>
0060 #include <net/ax25.h>
0061 #include <linux/inet.h>
0062 #include <linux/netdevice.h>
0063 #include <linux/etherdevice.h>
0064 #include <linux/if_arp.h>
0065 #include <linux/skbuff.h>
0066 #include <net/sock.h>
0067 #include <linux/uaccess.h>
0068 #include <linux/mm.h>
0069 #include <linux/interrupt.h>
0070 #include <linux/notifier.h>
0071 #include <linux/proc_fs.h>
0072 #include <linux/seq_file.h>
0073 #include <linux/stat.h>
0074 #include <linux/module.h>
0075 #include <linux/init.h>
0076 #include <linux/rtnetlink.h>
0077 
0078 #include <net/ip.h>
0079 #include <net/arp.h>
0080 #include <net/net_namespace.h>
0081 
0082 #include <linux/bpqether.h>
0083 
0084 static const char banner[] __initconst = KERN_INFO \
0085     "AX.25: bpqether driver version 004\n";
0086 
0087 static int bpq_rcv(struct sk_buff *, struct net_device *, struct packet_type *, struct net_device *);
0088 static int bpq_device_event(struct notifier_block *, unsigned long, void *);
0089 
0090 static struct packet_type bpq_packet_type __read_mostly = {
0091     .type   = cpu_to_be16(ETH_P_BPQ),
0092     .func   = bpq_rcv,
0093 };
0094 
0095 static struct notifier_block bpq_dev_notifier = {
0096     .notifier_call = bpq_device_event,
0097 };
0098 
0099 
0100 struct bpqdev {
0101     struct list_head bpq_list;  /* list of bpq devices chain */
0102     struct net_device *ethdev;  /* link to ethernet device */
0103     struct net_device *axdev;   /* bpq device (bpq#) */
0104     char   dest_addr[6];        /* ether destination address */
0105     char   acpt_addr[6];        /* accept ether frames from this address only */
0106 };
0107 
0108 static LIST_HEAD(bpq_devices);
0109 
0110 /*
0111  * bpqether network devices are paired with ethernet devices below them, so
0112  * form a special "super class" of normal ethernet devices; split their locks
0113  * off into a separate class since they always nest.
0114  */
0115 static struct lock_class_key bpq_netdev_xmit_lock_key;
0116 static struct lock_class_key bpq_netdev_addr_lock_key;
0117 
0118 static void bpq_set_lockdep_class_one(struct net_device *dev,
0119                       struct netdev_queue *txq,
0120                       void *_unused)
0121 {
0122     lockdep_set_class(&txq->_xmit_lock, &bpq_netdev_xmit_lock_key);
0123 }
0124 
0125 static void bpq_set_lockdep_class(struct net_device *dev)
0126 {
0127     lockdep_set_class(&dev->addr_list_lock, &bpq_netdev_addr_lock_key);
0128     netdev_for_each_tx_queue(dev, bpq_set_lockdep_class_one, NULL);
0129 }
0130 
0131 /* ------------------------------------------------------------------------ */
0132 
0133 
0134 /*
0135  *  Get the ethernet device for a BPQ device
0136  */
0137 static inline struct net_device *bpq_get_ether_dev(struct net_device *dev)
0138 {
0139     struct bpqdev *bpq = netdev_priv(dev);
0140 
0141     return bpq ? bpq->ethdev : NULL;
0142 }
0143 
0144 /*
0145  *  Get the BPQ device for the ethernet device
0146  */
0147 static inline struct net_device *bpq_get_ax25_dev(struct net_device *dev)
0148 {
0149     struct bpqdev *bpq;
0150 
0151     list_for_each_entry_rcu(bpq, &bpq_devices, bpq_list,
0152                 lockdep_rtnl_is_held()) {
0153         if (bpq->ethdev == dev)
0154             return bpq->axdev;
0155     }
0156     return NULL;
0157 }
0158 
0159 static inline int dev_is_ethdev(struct net_device *dev)
0160 {
0161     return dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5);
0162 }
0163 
0164 /* ------------------------------------------------------------------------ */
0165 
0166 
0167 /*
0168  *  Receive an AX.25 frame via an ethernet interface.
0169  */
0170 static int bpq_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *ptype, struct net_device *orig_dev)
0171 {
0172     int len;
0173     char * ptr;
0174     struct ethhdr *eth;
0175     struct bpqdev *bpq;
0176 
0177     if (!net_eq(dev_net(dev), &init_net))
0178         goto drop;
0179 
0180     if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
0181         return NET_RX_DROP;
0182 
0183     if (!pskb_may_pull(skb, sizeof(struct ethhdr)))
0184         goto drop;
0185 
0186     rcu_read_lock();
0187     dev = bpq_get_ax25_dev(dev);
0188 
0189     if (dev == NULL || !netif_running(dev)) 
0190         goto drop_unlock;
0191 
0192     /*
0193      * if we want to accept frames from just one ethernet device
0194      * we check the source address of the sender.
0195      */
0196 
0197     bpq = netdev_priv(dev);
0198 
0199     eth = eth_hdr(skb);
0200 
0201     if (!(bpq->acpt_addr[0] & 0x01) &&
0202         !ether_addr_equal(eth->h_source, bpq->acpt_addr))
0203         goto drop_unlock;
0204 
0205     if (skb_cow(skb, sizeof(struct ethhdr)))
0206         goto drop_unlock;
0207 
0208     len = skb->data[0] + skb->data[1] * 256 - 5;
0209 
0210     skb_pull(skb, 2);   /* Remove the length bytes */
0211     skb_trim(skb, len); /* Set the length of the data */
0212 
0213     dev->stats.rx_packets++;
0214     dev->stats.rx_bytes += len;
0215 
0216     ptr = skb_push(skb, 1);
0217     *ptr = 0;
0218 
0219     skb->protocol = ax25_type_trans(skb, dev);
0220     netif_rx(skb);
0221 unlock:
0222 
0223     rcu_read_unlock();
0224 
0225     return 0;
0226 drop_unlock:
0227     kfree_skb(skb);
0228     goto unlock;
0229 
0230 drop:
0231     kfree_skb(skb);
0232     return 0;
0233 }
0234 
0235 /*
0236  *  Send an AX.25 frame via an ethernet interface
0237  */
0238 static netdev_tx_t bpq_xmit(struct sk_buff *skb, struct net_device *dev)
0239 {
0240     unsigned char *ptr;
0241     struct bpqdev *bpq;
0242     struct net_device *orig_dev;
0243     int size;
0244 
0245     if (skb->protocol == htons(ETH_P_IP))
0246         return ax25_ip_xmit(skb);
0247 
0248     /*
0249      * Just to be *really* sure not to send anything if the interface
0250      * is down, the ethernet device may have gone.
0251      */
0252     if (!netif_running(dev)) {
0253         kfree_skb(skb);
0254         return NETDEV_TX_OK;
0255     }
0256 
0257     skb_pull(skb, 1);           /* Drop KISS byte */
0258     size = skb->len;
0259 
0260     /*
0261      * We're about to mess with the skb which may still shared with the
0262      * generic networking code so unshare and ensure it's got enough
0263      * space for the BPQ headers.
0264      */
0265     if (skb_cow(skb, AX25_BPQ_HEADER_LEN)) {
0266         if (net_ratelimit())
0267             pr_err("bpqether: out of memory\n");
0268         kfree_skb(skb);
0269 
0270         return NETDEV_TX_OK;
0271     }
0272 
0273     ptr = skb_push(skb, 2);         /* Make space for length */
0274 
0275     *ptr++ = (size + 5) % 256;
0276     *ptr++ = (size + 5) / 256;
0277 
0278     bpq = netdev_priv(dev);
0279 
0280     orig_dev = dev;
0281     if ((dev = bpq_get_ether_dev(dev)) == NULL) {
0282         orig_dev->stats.tx_dropped++;
0283         kfree_skb(skb);
0284         return NETDEV_TX_OK;
0285     }
0286 
0287     skb->protocol = ax25_type_trans(skb, dev);
0288     skb_reset_network_header(skb);
0289     dev_hard_header(skb, dev, ETH_P_BPQ, bpq->dest_addr, NULL, 0);
0290     dev->stats.tx_packets++;
0291     dev->stats.tx_bytes+=skb->len;
0292   
0293     dev_queue_xmit(skb);
0294     netif_wake_queue(dev);
0295     return NETDEV_TX_OK;
0296 }
0297 
0298 /*
0299  *  Set AX.25 callsign
0300  */
0301 static int bpq_set_mac_address(struct net_device *dev, void *addr)
0302 {
0303     struct sockaddr *sa = (struct sockaddr *)addr;
0304 
0305     dev_addr_set(dev, sa->sa_data);
0306 
0307     return 0;
0308 }
0309 
0310 /*  Ioctl commands
0311  *
0312  *      SIOCSBPQETHOPT      reserved for enhancements
0313  *      SIOCSBPQETHADDR     set the destination and accepted
0314  *                  source ethernet address (broadcast
0315  *                  or multicast: accept all)
0316  */
0317 static int bpq_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
0318                   void __user *data, int cmd)
0319 {
0320     struct bpq_ethaddr __user *ethaddr = data;
0321     struct bpqdev *bpq = netdev_priv(dev);
0322     struct bpq_req req;
0323 
0324     if (!capable(CAP_NET_ADMIN))
0325         return -EPERM;
0326 
0327     switch (cmd) {
0328         case SIOCSBPQETHOPT:
0329             if (copy_from_user(&req, data, sizeof(struct bpq_req)))
0330                 return -EFAULT;
0331             switch (req.cmd) {
0332                 case SIOCGBPQETHPARAM:
0333                 case SIOCSBPQETHPARAM:
0334                 default:
0335                     return -EINVAL;
0336             }
0337 
0338             break;
0339 
0340         case SIOCSBPQETHADDR:
0341             if (copy_from_user(bpq->dest_addr, ethaddr->destination, ETH_ALEN))
0342                 return -EFAULT;
0343             if (copy_from_user(bpq->acpt_addr, ethaddr->accept, ETH_ALEN))
0344                 return -EFAULT;
0345             break;
0346 
0347         default:
0348             return -EINVAL;
0349     }
0350 
0351     return 0;
0352 }
0353 
0354 /*
0355  * open/close a device
0356  */
0357 static int bpq_open(struct net_device *dev)
0358 {
0359     netif_start_queue(dev);
0360     return 0;
0361 }
0362 
0363 static int bpq_close(struct net_device *dev)
0364 {
0365     netif_stop_queue(dev);
0366     return 0;
0367 }
0368 
0369 
0370 /* ------------------------------------------------------------------------ */
0371 
0372 #ifdef CONFIG_PROC_FS
0373 /*
0374  *  Proc filesystem
0375  */
0376 static void *bpq_seq_start(struct seq_file *seq, loff_t *pos)
0377     __acquires(RCU)
0378 {
0379     int i = 1;
0380     struct bpqdev *bpqdev;
0381 
0382     rcu_read_lock();
0383 
0384     if (*pos == 0)
0385         return SEQ_START_TOKEN;
0386     
0387     list_for_each_entry_rcu(bpqdev, &bpq_devices, bpq_list) {
0388         if (i == *pos)
0389             return bpqdev;
0390     }
0391     return NULL;
0392 }
0393 
0394 static void *bpq_seq_next(struct seq_file *seq, void *v, loff_t *pos)
0395 {
0396     struct list_head *p;
0397     struct bpqdev *bpqdev = v;
0398 
0399     ++*pos;
0400 
0401     if (v == SEQ_START_TOKEN)
0402         p = rcu_dereference(list_next_rcu(&bpq_devices));
0403     else
0404         p = rcu_dereference(list_next_rcu(&bpqdev->bpq_list));
0405 
0406     return (p == &bpq_devices) ? NULL 
0407         : list_entry(p, struct bpqdev, bpq_list);
0408 }
0409 
0410 static void bpq_seq_stop(struct seq_file *seq, void *v)
0411     __releases(RCU)
0412 {
0413     rcu_read_unlock();
0414 }
0415 
0416 
0417 static int bpq_seq_show(struct seq_file *seq, void *v)
0418 {
0419     if (v == SEQ_START_TOKEN)
0420         seq_puts(seq, 
0421              "dev   ether      destination        accept from\n");
0422     else {
0423         const struct bpqdev *bpqdev = v;
0424 
0425         seq_printf(seq, "%-5s %-10s %pM  ",
0426             bpqdev->axdev->name, bpqdev->ethdev->name,
0427             bpqdev->dest_addr);
0428 
0429         if (is_multicast_ether_addr(bpqdev->acpt_addr))
0430             seq_printf(seq, "*\n");
0431         else
0432             seq_printf(seq, "%pM\n", bpqdev->acpt_addr);
0433 
0434     }
0435     return 0;
0436 }
0437 
0438 static const struct seq_operations bpq_seqops = {
0439     .start = bpq_seq_start,
0440     .next = bpq_seq_next,
0441     .stop = bpq_seq_stop,
0442     .show = bpq_seq_show,
0443 };
0444 #endif
0445 /* ------------------------------------------------------------------------ */
0446 
0447 static const struct net_device_ops bpq_netdev_ops = {
0448     .ndo_open        = bpq_open,
0449     .ndo_stop        = bpq_close,
0450     .ndo_start_xmit      = bpq_xmit,
0451     .ndo_set_mac_address = bpq_set_mac_address,
0452     .ndo_siocdevprivate  = bpq_siocdevprivate,
0453 };
0454 
0455 static void bpq_setup(struct net_device *dev)
0456 {
0457     dev->netdev_ops      = &bpq_netdev_ops;
0458     dev->needs_free_netdev = true;
0459 
0460     dev->flags      = 0;
0461     dev->features   = NETIF_F_LLTX; /* Allow recursion */
0462 
0463 #if IS_ENABLED(CONFIG_AX25)
0464     dev->header_ops      = &ax25_header_ops;
0465 #endif
0466 
0467     dev->type            = ARPHRD_AX25;
0468     dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN;
0469     dev->mtu             = AX25_DEF_PACLEN;
0470     dev->addr_len        = AX25_ADDR_LEN;
0471 
0472     memcpy(dev->broadcast, &ax25_bcast, AX25_ADDR_LEN);
0473     dev_addr_set(dev, (u8 *)&ax25_defaddr);
0474 }
0475 
0476 /*
0477  *  Setup a new device.
0478  */
0479 static int bpq_new_device(struct net_device *edev)
0480 {
0481     int err;
0482     struct net_device *ndev;
0483     struct bpqdev *bpq;
0484 
0485     ndev = alloc_netdev(sizeof(struct bpqdev), "bpq%d", NET_NAME_UNKNOWN,
0486                 bpq_setup);
0487     if (!ndev)
0488         return -ENOMEM;
0489 
0490         
0491     bpq = netdev_priv(ndev);
0492     dev_hold(edev);
0493     bpq->ethdev = edev;
0494     bpq->axdev = ndev;
0495 
0496     eth_broadcast_addr(bpq->dest_addr);
0497     eth_broadcast_addr(bpq->acpt_addr);
0498 
0499     err = register_netdevice(ndev);
0500     if (err)
0501         goto error;
0502     bpq_set_lockdep_class(ndev);
0503 
0504     /* List protected by RTNL */
0505     list_add_rcu(&bpq->bpq_list, &bpq_devices);
0506     return 0;
0507 
0508  error:
0509     dev_put(edev);
0510     free_netdev(ndev);
0511     return err;
0512     
0513 }
0514 
0515 static void bpq_free_device(struct net_device *ndev)
0516 {
0517     struct bpqdev *bpq = netdev_priv(ndev);
0518 
0519     dev_put(bpq->ethdev);
0520     list_del_rcu(&bpq->bpq_list);
0521 
0522     unregister_netdevice(ndev);
0523 }
0524 
0525 /*
0526  *  Handle device status changes.
0527  */
0528 static int bpq_device_event(struct notifier_block *this,
0529                 unsigned long event, void *ptr)
0530 {
0531     struct net_device *dev = netdev_notifier_info_to_dev(ptr);
0532 
0533     if (!net_eq(dev_net(dev), &init_net))
0534         return NOTIFY_DONE;
0535 
0536     if (!dev_is_ethdev(dev))
0537         return NOTIFY_DONE;
0538 
0539     switch (event) {
0540     case NETDEV_UP:     /* new ethernet device -> new BPQ interface */
0541         if (bpq_get_ax25_dev(dev) == NULL)
0542             bpq_new_device(dev);
0543         break;
0544 
0545     case NETDEV_DOWN:   /* ethernet device closed -> close BPQ interface */
0546         if ((dev = bpq_get_ax25_dev(dev)) != NULL)
0547             dev_close(dev);
0548         break;
0549 
0550     case NETDEV_UNREGISTER: /* ethernet device removed -> free BPQ interface */
0551         if ((dev = bpq_get_ax25_dev(dev)) != NULL)
0552             bpq_free_device(dev);
0553         break;
0554     default:
0555         break;
0556     }
0557 
0558     return NOTIFY_DONE;
0559 }
0560 
0561 
0562 /* ------------------------------------------------------------------------ */
0563 
0564 /*
0565  * Initialize driver. To be called from af_ax25 if not compiled as a
0566  * module
0567  */
0568 static int __init bpq_init_driver(void)
0569 {
0570 #ifdef CONFIG_PROC_FS
0571     if (!proc_create_seq("bpqether", 0444, init_net.proc_net, &bpq_seqops)) {
0572         printk(KERN_ERR
0573             "bpq: cannot create /proc/net/bpqether entry.\n");
0574         return -ENOENT;
0575     }
0576 #endif  /* CONFIG_PROC_FS */
0577 
0578     dev_add_pack(&bpq_packet_type);
0579 
0580     register_netdevice_notifier(&bpq_dev_notifier);
0581 
0582     printk(banner);
0583 
0584     return 0;
0585 }
0586 
0587 static void __exit bpq_cleanup_driver(void)
0588 {
0589     struct bpqdev *bpq;
0590 
0591     dev_remove_pack(&bpq_packet_type);
0592 
0593     unregister_netdevice_notifier(&bpq_dev_notifier);
0594 
0595     remove_proc_entry("bpqether", init_net.proc_net);
0596 
0597     rtnl_lock();
0598     while (!list_empty(&bpq_devices)) {
0599         bpq = list_entry(bpq_devices.next, struct bpqdev, bpq_list);
0600         bpq_free_device(bpq->axdev);
0601     }
0602     rtnl_unlock();
0603 }
0604 
0605 MODULE_AUTHOR("Joerg Reuter DL1BKE <jreuter@yaina.de>");
0606 MODULE_DESCRIPTION("Transmit and receive AX.25 packets over Ethernet");
0607 MODULE_LICENSE("GPL");
0608 module_init(bpq_init_driver);
0609 module_exit(bpq_cleanup_driver);