Linux 4.19.133
[linux/fpc-iii.git] / drivers / net / hamradio / bpqether.c
blob777fa59f5e0cd5abdfb8390ac358d09cf77636a1
1 /*
2 * G8BPQ compatible "AX.25 via ethernet" driver release 004
4 * This code REQUIRES 2.0.0 or higher/ NET3.029
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.
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
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
27 * This driver can use any ethernet destination address, and can be
28 * limited to accept frames from one dedicated ethernet card only.
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).
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/...
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.
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 <linux/slab.h>
65 #include <net/ax25.h>
66 #include <linux/inet.h>
67 #include <linux/netdevice.h>
68 #include <linux/etherdevice.h>
69 #include <linux/if_arp.h>
70 #include <linux/skbuff.h>
71 #include <net/sock.h>
72 #include <linux/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/module.h>
80 #include <linux/init.h>
81 #include <linux/rtnetlink.h>
83 #include <net/ip.h>
84 #include <net/arp.h>
85 #include <net/net_namespace.h>
87 #include <linux/bpqether.h>
89 static const char banner[] __initconst = KERN_INFO \
90 "AX.25: bpqether driver version 004\n";
92 static int bpq_rcv(struct sk_buff *, struct net_device *, struct packet_type *, struct net_device *);
93 static int bpq_device_event(struct notifier_block *, unsigned long, void *);
95 static struct packet_type bpq_packet_type __read_mostly = {
96 .type = cpu_to_be16(ETH_P_BPQ),
97 .func = bpq_rcv,
100 static struct notifier_block bpq_dev_notifier = {
101 .notifier_call = bpq_device_event,
105 struct bpqdev {
106 struct list_head bpq_list; /* list of bpq devices chain */
107 struct net_device *ethdev; /* link to ethernet device */
108 struct net_device *axdev; /* bpq device (bpq#) */
109 char dest_addr[6]; /* ether destination address */
110 char acpt_addr[6]; /* accept ether frames from this address only */
113 static LIST_HEAD(bpq_devices);
116 * bpqether network devices are paired with ethernet devices below them, so
117 * form a special "super class" of normal ethernet devices; split their locks
118 * off into a separate class since they always nest.
120 static struct lock_class_key bpq_netdev_xmit_lock_key;
121 static struct lock_class_key bpq_netdev_addr_lock_key;
123 static void bpq_set_lockdep_class_one(struct net_device *dev,
124 struct netdev_queue *txq,
125 void *_unused)
127 lockdep_set_class(&txq->_xmit_lock, &bpq_netdev_xmit_lock_key);
130 static void bpq_set_lockdep_class(struct net_device *dev)
132 lockdep_set_class(&dev->addr_list_lock, &bpq_netdev_addr_lock_key);
133 netdev_for_each_tx_queue(dev, bpq_set_lockdep_class_one, NULL);
136 /* ------------------------------------------------------------------------ */
140 * Get the ethernet device for a BPQ device
142 static inline struct net_device *bpq_get_ether_dev(struct net_device *dev)
144 struct bpqdev *bpq = netdev_priv(dev);
146 return bpq ? bpq->ethdev : NULL;
150 * Get the BPQ device for the ethernet device
152 static inline struct net_device *bpq_get_ax25_dev(struct net_device *dev)
154 struct bpqdev *bpq;
156 list_for_each_entry_rcu(bpq, &bpq_devices, bpq_list) {
157 if (bpq->ethdev == dev)
158 return bpq->axdev;
160 return NULL;
163 static inline int dev_is_ethdev(struct net_device *dev)
165 return dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5);
168 /* ------------------------------------------------------------------------ */
172 * Receive an AX.25 frame via an ethernet interface.
174 static int bpq_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *ptype, struct net_device *orig_dev)
176 int len;
177 char * ptr;
178 struct ethhdr *eth;
179 struct bpqdev *bpq;
181 if (!net_eq(dev_net(dev), &init_net))
182 goto drop;
184 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
185 return NET_RX_DROP;
187 if (!pskb_may_pull(skb, sizeof(struct ethhdr)))
188 goto drop;
190 rcu_read_lock();
191 dev = bpq_get_ax25_dev(dev);
193 if (dev == NULL || !netif_running(dev))
194 goto drop_unlock;
197 * if we want to accept frames from just one ethernet device
198 * we check the source address of the sender.
201 bpq = netdev_priv(dev);
203 eth = eth_hdr(skb);
205 if (!(bpq->acpt_addr[0] & 0x01) &&
206 !ether_addr_equal(eth->h_source, bpq->acpt_addr))
207 goto drop_unlock;
209 if (skb_cow(skb, sizeof(struct ethhdr)))
210 goto drop_unlock;
212 len = skb->data[0] + skb->data[1] * 256 - 5;
214 skb_pull(skb, 2); /* Remove the length bytes */
215 skb_trim(skb, len); /* Set the length of the data */
217 dev->stats.rx_packets++;
218 dev->stats.rx_bytes += len;
220 ptr = skb_push(skb, 1);
221 *ptr = 0;
223 skb->protocol = ax25_type_trans(skb, dev);
224 netif_rx(skb);
225 unlock:
227 rcu_read_unlock();
229 return 0;
230 drop_unlock:
231 kfree_skb(skb);
232 goto unlock;
234 drop:
235 kfree_skb(skb);
236 return 0;
240 * Send an AX.25 frame via an ethernet interface
242 static netdev_tx_t bpq_xmit(struct sk_buff *skb, struct net_device *dev)
244 unsigned char *ptr;
245 struct bpqdev *bpq;
246 struct net_device *orig_dev;
247 int size;
249 if (skb->protocol == htons(ETH_P_IP))
250 return ax25_ip_xmit(skb);
253 * Just to be *really* sure not to send anything if the interface
254 * is down, the ethernet device may have gone.
256 if (!netif_running(dev)) {
257 kfree_skb(skb);
258 return NETDEV_TX_OK;
261 skb_pull(skb, 1); /* Drop KISS byte */
262 size = skb->len;
265 * We're about to mess with the skb which may still shared with the
266 * generic networking code so unshare and ensure it's got enough
267 * space for the BPQ headers.
269 if (skb_cow(skb, AX25_BPQ_HEADER_LEN)) {
270 if (net_ratelimit())
271 pr_err("bpqether: out of memory\n");
272 kfree_skb(skb);
274 return NETDEV_TX_OK;
277 ptr = skb_push(skb, 2); /* Make space for length */
279 *ptr++ = (size + 5) % 256;
280 *ptr++ = (size + 5) / 256;
282 bpq = netdev_priv(dev);
284 orig_dev = dev;
285 if ((dev = bpq_get_ether_dev(dev)) == NULL) {
286 orig_dev->stats.tx_dropped++;
287 kfree_skb(skb);
288 return NETDEV_TX_OK;
291 skb->protocol = ax25_type_trans(skb, dev);
292 skb_reset_network_header(skb);
293 dev_hard_header(skb, dev, ETH_P_BPQ, bpq->dest_addr, NULL, 0);
294 dev->stats.tx_packets++;
295 dev->stats.tx_bytes+=skb->len;
297 dev_queue_xmit(skb);
298 netif_wake_queue(dev);
299 return NETDEV_TX_OK;
303 * Set AX.25 callsign
305 static int bpq_set_mac_address(struct net_device *dev, void *addr)
307 struct sockaddr *sa = (struct sockaddr *)addr;
309 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
311 return 0;
314 /* Ioctl commands
316 * SIOCSBPQETHOPT reserved for enhancements
317 * SIOCSBPQETHADDR set the destination and accepted
318 * source ethernet address (broadcast
319 * or multicast: accept all)
321 static int bpq_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
323 struct bpq_ethaddr __user *ethaddr = ifr->ifr_data;
324 struct bpqdev *bpq = netdev_priv(dev);
325 struct bpq_req req;
327 if (!capable(CAP_NET_ADMIN))
328 return -EPERM;
330 switch (cmd) {
331 case SIOCSBPQETHOPT:
332 if (copy_from_user(&req, ifr->ifr_data, sizeof(struct bpq_req)))
333 return -EFAULT;
334 switch (req.cmd) {
335 case SIOCGBPQETHPARAM:
336 case SIOCSBPQETHPARAM:
337 default:
338 return -EINVAL;
341 break;
343 case SIOCSBPQETHADDR:
344 if (copy_from_user(bpq->dest_addr, ethaddr->destination, ETH_ALEN))
345 return -EFAULT;
346 if (copy_from_user(bpq->acpt_addr, ethaddr->accept, ETH_ALEN))
347 return -EFAULT;
348 break;
350 default:
351 return -EINVAL;
354 return 0;
358 * open/close a device
360 static int bpq_open(struct net_device *dev)
362 netif_start_queue(dev);
363 return 0;
366 static int bpq_close(struct net_device *dev)
368 netif_stop_queue(dev);
369 return 0;
373 /* ------------------------------------------------------------------------ */
377 * Proc filesystem
379 static void *bpq_seq_start(struct seq_file *seq, loff_t *pos)
380 __acquires(RCU)
382 int i = 1;
383 struct bpqdev *bpqdev;
385 rcu_read_lock();
387 if (*pos == 0)
388 return SEQ_START_TOKEN;
390 list_for_each_entry_rcu(bpqdev, &bpq_devices, bpq_list) {
391 if (i == *pos)
392 return bpqdev;
394 return NULL;
397 static void *bpq_seq_next(struct seq_file *seq, void *v, loff_t *pos)
399 struct list_head *p;
400 struct bpqdev *bpqdev = v;
402 ++*pos;
404 if (v == SEQ_START_TOKEN)
405 p = rcu_dereference(list_next_rcu(&bpq_devices));
406 else
407 p = rcu_dereference(list_next_rcu(&bpqdev->bpq_list));
409 return (p == &bpq_devices) ? NULL
410 : list_entry(p, struct bpqdev, bpq_list);
413 static void bpq_seq_stop(struct seq_file *seq, void *v)
414 __releases(RCU)
416 rcu_read_unlock();
420 static int bpq_seq_show(struct seq_file *seq, void *v)
422 if (v == SEQ_START_TOKEN)
423 seq_puts(seq,
424 "dev ether destination accept from\n");
425 else {
426 const struct bpqdev *bpqdev = v;
428 seq_printf(seq, "%-5s %-10s %pM ",
429 bpqdev->axdev->name, bpqdev->ethdev->name,
430 bpqdev->dest_addr);
432 if (is_multicast_ether_addr(bpqdev->acpt_addr))
433 seq_printf(seq, "*\n");
434 else
435 seq_printf(seq, "%pM\n", bpqdev->acpt_addr);
438 return 0;
441 static const struct seq_operations bpq_seqops = {
442 .start = bpq_seq_start,
443 .next = bpq_seq_next,
444 .stop = bpq_seq_stop,
445 .show = bpq_seq_show,
448 /* ------------------------------------------------------------------------ */
450 static const struct net_device_ops bpq_netdev_ops = {
451 .ndo_open = bpq_open,
452 .ndo_stop = bpq_close,
453 .ndo_start_xmit = bpq_xmit,
454 .ndo_set_mac_address = bpq_set_mac_address,
455 .ndo_do_ioctl = bpq_ioctl,
458 static void bpq_setup(struct net_device *dev)
460 dev->netdev_ops = &bpq_netdev_ops;
461 dev->needs_free_netdev = true;
463 memcpy(dev->broadcast, &ax25_bcast, AX25_ADDR_LEN);
464 memcpy(dev->dev_addr, &ax25_defaddr, AX25_ADDR_LEN);
466 dev->flags = 0;
467 dev->features = NETIF_F_LLTX; /* Allow recursion */
469 #if IS_ENABLED(CONFIG_AX25)
470 dev->header_ops = &ax25_header_ops;
471 #endif
473 dev->type = ARPHRD_AX25;
474 dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN;
475 dev->mtu = AX25_DEF_PACLEN;
476 dev->addr_len = AX25_ADDR_LEN;
481 * Setup a new device.
483 static int bpq_new_device(struct net_device *edev)
485 int err;
486 struct net_device *ndev;
487 struct bpqdev *bpq;
489 ndev = alloc_netdev(sizeof(struct bpqdev), "bpq%d", NET_NAME_UNKNOWN,
490 bpq_setup);
491 if (!ndev)
492 return -ENOMEM;
495 bpq = netdev_priv(ndev);
496 dev_hold(edev);
497 bpq->ethdev = edev;
498 bpq->axdev = ndev;
500 eth_broadcast_addr(bpq->dest_addr);
501 eth_broadcast_addr(bpq->acpt_addr);
503 err = register_netdevice(ndev);
504 if (err)
505 goto error;
506 bpq_set_lockdep_class(ndev);
508 /* List protected by RTNL */
509 list_add_rcu(&bpq->bpq_list, &bpq_devices);
510 return 0;
512 error:
513 dev_put(edev);
514 free_netdev(ndev);
515 return err;
519 static void bpq_free_device(struct net_device *ndev)
521 struct bpqdev *bpq = netdev_priv(ndev);
523 dev_put(bpq->ethdev);
524 list_del_rcu(&bpq->bpq_list);
526 unregister_netdevice(ndev);
530 * Handle device status changes.
532 static int bpq_device_event(struct notifier_block *this,
533 unsigned long event, void *ptr)
535 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
537 if (!net_eq(dev_net(dev), &init_net))
538 return NOTIFY_DONE;
540 if (!dev_is_ethdev(dev))
541 return NOTIFY_DONE;
543 switch (event) {
544 case NETDEV_UP: /* new ethernet device -> new BPQ interface */
545 if (bpq_get_ax25_dev(dev) == NULL)
546 bpq_new_device(dev);
547 break;
549 case NETDEV_DOWN: /* ethernet device closed -> close BPQ interface */
550 if ((dev = bpq_get_ax25_dev(dev)) != NULL)
551 dev_close(dev);
552 break;
554 case NETDEV_UNREGISTER: /* ethernet device removed -> free BPQ interface */
555 if ((dev = bpq_get_ax25_dev(dev)) != NULL)
556 bpq_free_device(dev);
557 break;
558 default:
559 break;
562 return NOTIFY_DONE;
566 /* ------------------------------------------------------------------------ */
569 * Initialize driver. To be called from af_ax25 if not compiled as a
570 * module
572 static int __init bpq_init_driver(void)
574 #ifdef CONFIG_PROC_FS
575 if (!proc_create_seq("bpqether", 0444, init_net.proc_net, &bpq_seqops)) {
576 printk(KERN_ERR
577 "bpq: cannot create /proc/net/bpqether entry.\n");
578 return -ENOENT;
580 #endif /* CONFIG_PROC_FS */
582 dev_add_pack(&bpq_packet_type);
584 register_netdevice_notifier(&bpq_dev_notifier);
586 printk(banner);
588 return 0;
591 static void __exit bpq_cleanup_driver(void)
593 struct bpqdev *bpq;
595 dev_remove_pack(&bpq_packet_type);
597 unregister_netdevice_notifier(&bpq_dev_notifier);
599 remove_proc_entry("bpqether", init_net.proc_net);
601 rtnl_lock();
602 while (!list_empty(&bpq_devices)) {
603 bpq = list_entry(bpq_devices.next, struct bpqdev, bpq_list);
604 bpq_free_device(bpq->axdev);
606 rtnl_unlock();
609 MODULE_AUTHOR("Joerg Reuter DL1BKE <jreuter@yaina.de>");
610 MODULE_DESCRIPTION("Transmit and receive AX.25 packets over Ethernet");
611 MODULE_LICENSE("GPL");
612 module_init(bpq_init_driver);
613 module_exit(bpq_cleanup_driver);