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 <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>
82
83 #include <net/ip.h>
84 #include <net/arp.h>
85 #include <net/net_namespace.h>
86
87 #include <linux/bpqether.h>
88
89 static const char banner[] __initconst = KERN_INFO \
90 "AX.25: bpqether driver version 004\n";
91
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 *);
94
95 static struct packet_type bpq_packet_type __read_mostly = {
96 .type = cpu_to_be16(ETH_P_BPQ),
97 .func = bpq_rcv,
98 };
99
100 static struct notifier_block bpq_dev_notifier = {
101 .notifier_call = bpq_device_event,
102 };
103
104
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 */
111 };
112
113 static LIST_HEAD(bpq_devices);
114
115 /*
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.
119 */
120 static struct lock_class_key bpq_netdev_xmit_lock_key;
121 static struct lock_class_key bpq_netdev_addr_lock_key;
122
bpq_set_lockdep_class_one(struct net_device * dev,struct netdev_queue * txq,void * _unused)123 static void bpq_set_lockdep_class_one(struct net_device *dev,
124 struct netdev_queue *txq,
125 void *_unused)
126 {
127 lockdep_set_class(&txq->_xmit_lock, &bpq_netdev_xmit_lock_key);
128 }
129
bpq_set_lockdep_class(struct net_device * dev)130 static void bpq_set_lockdep_class(struct net_device *dev)
131 {
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);
134 }
135
136 /* ------------------------------------------------------------------------ */
137
138
139 /*
140 * Get the ethernet device for a BPQ device
141 */
bpq_get_ether_dev(struct net_device * dev)142 static inline struct net_device *bpq_get_ether_dev(struct net_device *dev)
143 {
144 struct bpqdev *bpq = netdev_priv(dev);
145
146 return bpq ? bpq->ethdev : NULL;
147 }
148
149 /*
150 * Get the BPQ device for the ethernet device
151 */
bpq_get_ax25_dev(struct net_device * dev)152 static inline struct net_device *bpq_get_ax25_dev(struct net_device *dev)
153 {
154 struct bpqdev *bpq;
155
156 list_for_each_entry_rcu(bpq, &bpq_devices, bpq_list) {
157 if (bpq->ethdev == dev)
158 return bpq->axdev;
159 }
160 return NULL;
161 }
162
dev_is_ethdev(struct net_device * dev)163 static inline int dev_is_ethdev(struct net_device *dev)
164 {
165 return dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5);
166 }
167
168 /* ------------------------------------------------------------------------ */
169
170
171 /*
172 * Receive an AX.25 frame via an ethernet interface.
173 */
bpq_rcv(struct sk_buff * skb,struct net_device * dev,struct packet_type * ptype,struct net_device * orig_dev)174 static int bpq_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *ptype, struct net_device *orig_dev)
175 {
176 int len;
177 char * ptr;
178 struct ethhdr *eth;
179 struct bpqdev *bpq;
180
181 if (!net_eq(dev_net(dev), &init_net))
182 goto drop;
183
184 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
185 return NET_RX_DROP;
186
187 if (!pskb_may_pull(skb, sizeof(struct ethhdr)))
188 goto drop;
189
190 rcu_read_lock();
191 dev = bpq_get_ax25_dev(dev);
192
193 if (dev == NULL || !netif_running(dev))
194 goto drop_unlock;
195
196 /*
197 * if we want to accept frames from just one ethernet device
198 * we check the source address of the sender.
199 */
200
201 bpq = netdev_priv(dev);
202
203 eth = eth_hdr(skb);
204
205 if (!(bpq->acpt_addr[0] & 0x01) &&
206 !ether_addr_equal(eth->h_source, bpq->acpt_addr))
207 goto drop_unlock;
208
209 if (skb_cow(skb, sizeof(struct ethhdr)))
210 goto drop_unlock;
211
212 len = skb->data[0] + skb->data[1] * 256 - 5;
213
214 skb_pull(skb, 2); /* Remove the length bytes */
215 skb_trim(skb, len); /* Set the length of the data */
216
217 dev->stats.rx_packets++;
218 dev->stats.rx_bytes += len;
219
220 ptr = skb_push(skb, 1);
221 *ptr = 0;
222
223 skb->protocol = ax25_type_trans(skb, dev);
224 netif_rx(skb);
225 unlock:
226
227 rcu_read_unlock();
228
229 return 0;
230 drop_unlock:
231 kfree_skb(skb);
232 goto unlock;
233
234 drop:
235 kfree_skb(skb);
236 return 0;
237 }
238
239 /*
240 * Send an AX.25 frame via an ethernet interface
241 */
bpq_xmit(struct sk_buff * skb,struct net_device * dev)242 static netdev_tx_t bpq_xmit(struct sk_buff *skb, struct net_device *dev)
243 {
244 unsigned char *ptr;
245 struct bpqdev *bpq;
246 struct net_device *orig_dev;
247 int size;
248
249 if (skb->protocol == htons(ETH_P_IP))
250 return ax25_ip_xmit(skb);
251
252 /*
253 * Just to be *really* sure not to send anything if the interface
254 * is down, the ethernet device may have gone.
255 */
256 if (!netif_running(dev)) {
257 kfree_skb(skb);
258 return NETDEV_TX_OK;
259 }
260
261 skb_pull(skb, 1); /* Drop KISS byte */
262 size = skb->len;
263
264 /*
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.
268 */
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);
273
274 return NETDEV_TX_OK;
275 }
276
277 ptr = skb_push(skb, 2); /* Make space for length */
278
279 *ptr++ = (size + 5) % 256;
280 *ptr++ = (size + 5) / 256;
281
282 bpq = netdev_priv(dev);
283
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;
289 }
290
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;
296
297 dev_queue_xmit(skb);
298 netif_wake_queue(dev);
299 return NETDEV_TX_OK;
300 }
301
302 /*
303 * Set AX.25 callsign
304 */
bpq_set_mac_address(struct net_device * dev,void * addr)305 static int bpq_set_mac_address(struct net_device *dev, void *addr)
306 {
307 struct sockaddr *sa = (struct sockaddr *)addr;
308
309 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
310
311 return 0;
312 }
313
314 /* Ioctl commands
315 *
316 * SIOCSBPQETHOPT reserved for enhancements
317 * SIOCSBPQETHADDR set the destination and accepted
318 * source ethernet address (broadcast
319 * or multicast: accept all)
320 */
bpq_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)321 static int bpq_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
322 {
323 struct bpq_ethaddr __user *ethaddr = ifr->ifr_data;
324 struct bpqdev *bpq = netdev_priv(dev);
325 struct bpq_req req;
326
327 if (!capable(CAP_NET_ADMIN))
328 return -EPERM;
329
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;
339 }
340
341 break;
342
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;
349
350 default:
351 return -EINVAL;
352 }
353
354 return 0;
355 }
356
357 /*
358 * open/close a device
359 */
bpq_open(struct net_device * dev)360 static int bpq_open(struct net_device *dev)
361 {
362 netif_start_queue(dev);
363 return 0;
364 }
365
bpq_close(struct net_device * dev)366 static int bpq_close(struct net_device *dev)
367 {
368 netif_stop_queue(dev);
369 return 0;
370 }
371
372
373 /* ------------------------------------------------------------------------ */
374
375
376 /*
377 * Proc filesystem
378 */
bpq_seq_start(struct seq_file * seq,loff_t * pos)379 static void *bpq_seq_start(struct seq_file *seq, loff_t *pos)
380 __acquires(RCU)
381 {
382 int i = 1;
383 struct bpqdev *bpqdev;
384
385 rcu_read_lock();
386
387 if (*pos == 0)
388 return SEQ_START_TOKEN;
389
390 list_for_each_entry_rcu(bpqdev, &bpq_devices, bpq_list) {
391 if (i == *pos)
392 return bpqdev;
393 }
394 return NULL;
395 }
396
bpq_seq_next(struct seq_file * seq,void * v,loff_t * pos)397 static void *bpq_seq_next(struct seq_file *seq, void *v, loff_t *pos)
398 {
399 struct list_head *p;
400 struct bpqdev *bpqdev = v;
401
402 ++*pos;
403
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));
408
409 return (p == &bpq_devices) ? NULL
410 : list_entry(p, struct bpqdev, bpq_list);
411 }
412
bpq_seq_stop(struct seq_file * seq,void * v)413 static void bpq_seq_stop(struct seq_file *seq, void *v)
414 __releases(RCU)
415 {
416 rcu_read_unlock();
417 }
418
419
bpq_seq_show(struct seq_file * seq,void * v)420 static int bpq_seq_show(struct seq_file *seq, void *v)
421 {
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;
427
428 seq_printf(seq, "%-5s %-10s %pM ",
429 bpqdev->axdev->name, bpqdev->ethdev->name,
430 bpqdev->dest_addr);
431
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);
436
437 }
438 return 0;
439 }
440
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,
446 };
447
448 /* ------------------------------------------------------------------------ */
449
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,
456 };
457
bpq_setup(struct net_device * dev)458 static void bpq_setup(struct net_device *dev)
459 {
460 dev->netdev_ops = &bpq_netdev_ops;
461 dev->needs_free_netdev = true;
462
463 memcpy(dev->broadcast, &ax25_bcast, AX25_ADDR_LEN);
464 memcpy(dev->dev_addr, &ax25_defaddr, AX25_ADDR_LEN);
465
466 dev->flags = 0;
467 dev->features = NETIF_F_LLTX; /* Allow recursion */
468
469 #if IS_ENABLED(CONFIG_AX25)
470 dev->header_ops = &ax25_header_ops;
471 #endif
472
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;
477
478 }
479
480 /*
481 * Setup a new device.
482 */
bpq_new_device(struct net_device * edev)483 static int bpq_new_device(struct net_device *edev)
484 {
485 int err;
486 struct net_device *ndev;
487 struct bpqdev *bpq;
488
489 ndev = alloc_netdev(sizeof(struct bpqdev), "bpq%d", NET_NAME_UNKNOWN,
490 bpq_setup);
491 if (!ndev)
492 return -ENOMEM;
493
494
495 bpq = netdev_priv(ndev);
496 dev_hold(edev);
497 bpq->ethdev = edev;
498 bpq->axdev = ndev;
499
500 eth_broadcast_addr(bpq->dest_addr);
501 eth_broadcast_addr(bpq->acpt_addr);
502
503 err = register_netdevice(ndev);
504 if (err)
505 goto error;
506 bpq_set_lockdep_class(ndev);
507
508 /* List protected by RTNL */
509 list_add_rcu(&bpq->bpq_list, &bpq_devices);
510 return 0;
511
512 error:
513 dev_put(edev);
514 free_netdev(ndev);
515 return err;
516
517 }
518
bpq_free_device(struct net_device * ndev)519 static void bpq_free_device(struct net_device *ndev)
520 {
521 struct bpqdev *bpq = netdev_priv(ndev);
522
523 dev_put(bpq->ethdev);
524 list_del_rcu(&bpq->bpq_list);
525
526 unregister_netdevice(ndev);
527 }
528
529 /*
530 * Handle device status changes.
531 */
bpq_device_event(struct notifier_block * this,unsigned long event,void * ptr)532 static int bpq_device_event(struct notifier_block *this,
533 unsigned long event, void *ptr)
534 {
535 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
536
537 if (!net_eq(dev_net(dev), &init_net))
538 return NOTIFY_DONE;
539
540 if (!dev_is_ethdev(dev))
541 return NOTIFY_DONE;
542
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;
548
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;
553
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;
560 }
561
562 return NOTIFY_DONE;
563 }
564
565
566 /* ------------------------------------------------------------------------ */
567
568 /*
569 * Initialize driver. To be called from af_ax25 if not compiled as a
570 * module
571 */
bpq_init_driver(void)572 static int __init bpq_init_driver(void)
573 {
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;
579 }
580 #endif /* CONFIG_PROC_FS */
581
582 dev_add_pack(&bpq_packet_type);
583
584 register_netdevice_notifier(&bpq_dev_notifier);
585
586 printk(banner);
587
588 return 0;
589 }
590
bpq_cleanup_driver(void)591 static void __exit bpq_cleanup_driver(void)
592 {
593 struct bpqdev *bpq;
594
595 dev_remove_pack(&bpq_packet_type);
596
597 unregister_netdevice_notifier(&bpq_dev_notifier);
598
599 remove_proc_entry("bpqether", init_net.proc_net);
600
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);
605 }
606 rtnl_unlock();
607 }
608
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);
614