treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 41
[sfrench/cifs-2.6.git] / drivers / net / hamradio / bpqether.c
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 static struct lock_class_key bpq_netdev_addr_lock_key;
117
118 static void bpq_set_lockdep_class_one(struct net_device *dev,
119                                       struct netdev_queue *txq,
120                                       void *_unused)
121 {
122         lockdep_set_class(&txq->_xmit_lock, &bpq_netdev_xmit_lock_key);
123 }
124
125 static void bpq_set_lockdep_class(struct net_device *dev)
126 {
127         lockdep_set_class(&dev->addr_list_lock, &bpq_netdev_addr_lock_key);
128         netdev_for_each_tx_queue(dev, bpq_set_lockdep_class_one, NULL);
129 }
130
131 /* ------------------------------------------------------------------------ */
132
133
134 /*
135  *      Get the ethernet device for a BPQ device
136  */
137 static inline struct net_device *bpq_get_ether_dev(struct net_device *dev)
138 {
139         struct bpqdev *bpq = netdev_priv(dev);
140
141         return bpq ? bpq->ethdev : NULL;
142 }
143
144 /*
145  *      Get the BPQ device for the ethernet device
146  */
147 static inline struct net_device *bpq_get_ax25_dev(struct net_device *dev)
148 {
149         struct bpqdev *bpq;
150
151         list_for_each_entry_rcu(bpq, &bpq_devices, bpq_list) {
152                 if (bpq->ethdev == dev)
153                         return bpq->axdev;
154         }
155         return NULL;
156 }
157
158 static inline int dev_is_ethdev(struct net_device *dev)
159 {
160         return dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5);
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 (!net_eq(dev_net(dev), &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             !ether_addr_equal(eth->h_source, bpq->acpt_addr))
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         dev->stats.rx_packets++;
213         dev->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 unlock:
221
222         rcu_read_unlock();
223
224         return 0;
225 drop_unlock:
226         kfree_skb(skb);
227         goto unlock;
228
229 drop:
230         kfree_skb(skb);
231         return 0;
232 }
233
234 /*
235  *      Send an AX.25 frame via an ethernet interface
236  */
237 static netdev_tx_t bpq_xmit(struct sk_buff *skb, struct net_device *dev)
238 {
239         unsigned char *ptr;
240         struct bpqdev *bpq;
241         struct net_device *orig_dev;
242         int size;
243
244         if (skb->protocol == htons(ETH_P_IP))
245                 return ax25_ip_xmit(skb);
246
247         /*
248          * Just to be *really* sure not to send anything if the interface
249          * is down, the ethernet device may have gone.
250          */
251         if (!netif_running(dev)) {
252                 kfree_skb(skb);
253                 return NETDEV_TX_OK;
254         }
255
256         skb_pull(skb, 1);                       /* Drop KISS byte */
257         size = skb->len;
258
259         /*
260          * We're about to mess with the skb which may still shared with the
261          * generic networking code so unshare and ensure it's got enough
262          * space for the BPQ headers.
263          */
264         if (skb_cow(skb, AX25_BPQ_HEADER_LEN)) {
265                 if (net_ratelimit())
266                         pr_err("bpqether: out of memory\n");
267                 kfree_skb(skb);
268
269                 return NETDEV_TX_OK;
270         }
271
272         ptr = skb_push(skb, 2);                 /* Make space for length */
273
274         *ptr++ = (size + 5) % 256;
275         *ptr++ = (size + 5) / 256;
276
277         bpq = netdev_priv(dev);
278
279         orig_dev = dev;
280         if ((dev = bpq_get_ether_dev(dev)) == NULL) {
281                 orig_dev->stats.tx_dropped++;
282                 kfree_skb(skb);
283                 return NETDEV_TX_OK;
284         }
285
286         skb->protocol = ax25_type_trans(skb, dev);
287         skb_reset_network_header(skb);
288         dev_hard_header(skb, dev, ETH_P_BPQ, bpq->dest_addr, NULL, 0);
289         dev->stats.tx_packets++;
290         dev->stats.tx_bytes+=skb->len;
291   
292         dev_queue_xmit(skb);
293         netif_wake_queue(dev);
294         return NETDEV_TX_OK;
295 }
296
297 /*
298  *      Set AX.25 callsign
299  */
300 static int bpq_set_mac_address(struct net_device *dev, void *addr)
301 {
302     struct sockaddr *sa = (struct sockaddr *)addr;
303
304     memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
305
306     return 0;
307 }
308
309 /*      Ioctl commands
310  *
311  *              SIOCSBPQETHOPT          reserved for enhancements
312  *              SIOCSBPQETHADDR         set the destination and accepted
313  *                                      source ethernet address (broadcast
314  *                                      or multicast: accept all)
315  */
316 static int bpq_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
317 {
318         struct bpq_ethaddr __user *ethaddr = ifr->ifr_data;
319         struct bpqdev *bpq = netdev_priv(dev);
320         struct bpq_req req;
321
322         if (!capable(CAP_NET_ADMIN))
323                 return -EPERM;
324
325         switch (cmd) {
326                 case SIOCSBPQETHOPT:
327                         if (copy_from_user(&req, ifr->ifr_data, sizeof(struct bpq_req)))
328                                 return -EFAULT;
329                         switch (req.cmd) {
330                                 case SIOCGBPQETHPARAM:
331                                 case SIOCSBPQETHPARAM:
332                                 default:
333                                         return -EINVAL;
334                         }
335
336                         break;
337
338                 case SIOCSBPQETHADDR:
339                         if (copy_from_user(bpq->dest_addr, ethaddr->destination, ETH_ALEN))
340                                 return -EFAULT;
341                         if (copy_from_user(bpq->acpt_addr, ethaddr->accept, ETH_ALEN))
342                                 return -EFAULT;
343                         break;
344
345                 default:
346                         return -EINVAL;
347         }
348
349         return 0;
350 }
351
352 /*
353  * open/close a device
354  */
355 static int bpq_open(struct net_device *dev)
356 {
357         netif_start_queue(dev);
358         return 0;
359 }
360
361 static int bpq_close(struct net_device *dev)
362 {
363         netif_stop_queue(dev);
364         return 0;
365 }
366
367
368 /* ------------------------------------------------------------------------ */
369
370
371 /*
372  *      Proc filesystem
373  */
374 static void *bpq_seq_start(struct seq_file *seq, loff_t *pos)
375         __acquires(RCU)
376 {
377         int i = 1;
378         struct bpqdev *bpqdev;
379
380         rcu_read_lock();
381
382         if (*pos == 0)
383                 return SEQ_START_TOKEN;
384         
385         list_for_each_entry_rcu(bpqdev, &bpq_devices, bpq_list) {
386                 if (i == *pos)
387                         return bpqdev;
388         }
389         return NULL;
390 }
391
392 static void *bpq_seq_next(struct seq_file *seq, void *v, loff_t *pos)
393 {
394         struct list_head *p;
395         struct bpqdev *bpqdev = v;
396
397         ++*pos;
398
399         if (v == SEQ_START_TOKEN)
400                 p = rcu_dereference(list_next_rcu(&bpq_devices));
401         else
402                 p = rcu_dereference(list_next_rcu(&bpqdev->bpq_list));
403
404         return (p == &bpq_devices) ? NULL 
405                 : list_entry(p, struct bpqdev, bpq_list);
406 }
407
408 static void bpq_seq_stop(struct seq_file *seq, void *v)
409         __releases(RCU)
410 {
411         rcu_read_unlock();
412 }
413
414
415 static int bpq_seq_show(struct seq_file *seq, void *v)
416 {
417         if (v == SEQ_START_TOKEN)
418                 seq_puts(seq, 
419                          "dev   ether      destination        accept from\n");
420         else {
421                 const struct bpqdev *bpqdev = v;
422
423                 seq_printf(seq, "%-5s %-10s %pM  ",
424                         bpqdev->axdev->name, bpqdev->ethdev->name,
425                         bpqdev->dest_addr);
426
427                 if (is_multicast_ether_addr(bpqdev->acpt_addr))
428                         seq_printf(seq, "*\n");
429                 else
430                         seq_printf(seq, "%pM\n", bpqdev->acpt_addr);
431
432         }
433         return 0;
434 }
435
436 static const struct seq_operations bpq_seqops = {
437         .start = bpq_seq_start,
438         .next = bpq_seq_next,
439         .stop = bpq_seq_stop,
440         .show = bpq_seq_show,
441 };
442
443 /* ------------------------------------------------------------------------ */
444
445 static const struct net_device_ops bpq_netdev_ops = {
446         .ndo_open            = bpq_open,
447         .ndo_stop            = bpq_close,
448         .ndo_start_xmit      = bpq_xmit,
449         .ndo_set_mac_address = bpq_set_mac_address,
450         .ndo_do_ioctl        = bpq_ioctl,
451 };
452
453 static void bpq_setup(struct net_device *dev)
454 {
455         dev->netdev_ops      = &bpq_netdev_ops;
456         dev->needs_free_netdev = true;
457
458         memcpy(dev->broadcast, &ax25_bcast, AX25_ADDR_LEN);
459         memcpy(dev->dev_addr,  &ax25_defaddr, AX25_ADDR_LEN);
460
461         dev->flags      = 0;
462         dev->features   = NETIF_F_LLTX; /* Allow recursion */
463
464 #if IS_ENABLED(CONFIG_AX25)
465         dev->header_ops      = &ax25_header_ops;
466 #endif
467
468         dev->type            = ARPHRD_AX25;
469         dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN;
470         dev->mtu             = AX25_DEF_PACLEN;
471         dev->addr_len        = AX25_ADDR_LEN;
472
473 }
474
475 /*
476  *      Setup a new device.
477  */
478 static int bpq_new_device(struct net_device *edev)
479 {
480         int err;
481         struct net_device *ndev;
482         struct bpqdev *bpq;
483
484         ndev = alloc_netdev(sizeof(struct bpqdev), "bpq%d", NET_NAME_UNKNOWN,
485                             bpq_setup);
486         if (!ndev)
487                 return -ENOMEM;
488
489                 
490         bpq = netdev_priv(ndev);
491         dev_hold(edev);
492         bpq->ethdev = edev;
493         bpq->axdev = ndev;
494
495         eth_broadcast_addr(bpq->dest_addr);
496         eth_broadcast_addr(bpq->acpt_addr);
497
498         err = register_netdevice(ndev);
499         if (err)
500                 goto error;
501         bpq_set_lockdep_class(ndev);
502
503         /* List protected by RTNL */
504         list_add_rcu(&bpq->bpq_list, &bpq_devices);
505         return 0;
506
507  error:
508         dev_put(edev);
509         free_netdev(ndev);
510         return err;
511         
512 }
513
514 static void bpq_free_device(struct net_device *ndev)
515 {
516         struct bpqdev *bpq = netdev_priv(ndev);
517
518         dev_put(bpq->ethdev);
519         list_del_rcu(&bpq->bpq_list);
520
521         unregister_netdevice(ndev);
522 }
523
524 /*
525  *      Handle device status changes.
526  */
527 static int bpq_device_event(struct notifier_block *this,
528                             unsigned long event, void *ptr)
529 {
530         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
531
532         if (!net_eq(dev_net(dev), &init_net))
533                 return NOTIFY_DONE;
534
535         if (!dev_is_ethdev(dev))
536                 return NOTIFY_DONE;
537
538         switch (event) {
539         case NETDEV_UP:         /* new ethernet device -> new BPQ interface */
540                 if (bpq_get_ax25_dev(dev) == NULL)
541                         bpq_new_device(dev);
542                 break;
543
544         case NETDEV_DOWN:       /* ethernet device closed -> close BPQ interface */
545                 if ((dev = bpq_get_ax25_dev(dev)) != NULL)
546                         dev_close(dev);
547                 break;
548
549         case NETDEV_UNREGISTER: /* ethernet device removed -> free BPQ interface */
550                 if ((dev = bpq_get_ax25_dev(dev)) != NULL)
551                         bpq_free_device(dev);
552                 break;
553         default:
554                 break;
555         }
556
557         return NOTIFY_DONE;
558 }
559
560
561 /* ------------------------------------------------------------------------ */
562
563 /*
564  * Initialize driver. To be called from af_ax25 if not compiled as a
565  * module
566  */
567 static int __init bpq_init_driver(void)
568 {
569 #ifdef CONFIG_PROC_FS
570         if (!proc_create_seq("bpqether", 0444, init_net.proc_net, &bpq_seqops)) {
571                 printk(KERN_ERR
572                         "bpq: cannot create /proc/net/bpqether entry.\n");
573                 return -ENOENT;
574         }
575 #endif  /* CONFIG_PROC_FS */
576
577         dev_add_pack(&bpq_packet_type);
578
579         register_netdevice_notifier(&bpq_dev_notifier);
580
581         printk(banner);
582
583         return 0;
584 }
585
586 static void __exit bpq_cleanup_driver(void)
587 {
588         struct bpqdev *bpq;
589
590         dev_remove_pack(&bpq_packet_type);
591
592         unregister_netdevice_notifier(&bpq_dev_notifier);
593
594         remove_proc_entry("bpqether", init_net.proc_net);
595
596         rtnl_lock();
597         while (!list_empty(&bpq_devices)) {
598                 bpq = list_entry(bpq_devices.next, struct bpqdev, bpq_list);
599                 bpq_free_device(bpq->axdev);
600         }
601         rtnl_unlock();
602 }
603
604 MODULE_AUTHOR("Joerg Reuter DL1BKE <jreuter@yaina.de>");
605 MODULE_DESCRIPTION("Transmit and receive AX.25 packets over Ethernet");
606 MODULE_LICENSE("GPL");
607 module_init(bpq_init_driver);
608 module_exit(bpq_cleanup_driver);