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 <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/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
bpq_info_open(struct inode * inode,struct file * file)448 static int bpq_info_open(struct inode *inode, struct file *file)
449 {
450 return seq_open(file, &bpq_seqops);
451 }
452
453 static const struct file_operations bpq_info_fops = {
454 .owner = THIS_MODULE,
455 .open = bpq_info_open,
456 .read = seq_read,
457 .llseek = seq_lseek,
458 .release = seq_release,
459 };
460
461
462 /* ------------------------------------------------------------------------ */
463
464 static const struct net_device_ops bpq_netdev_ops = {
465 .ndo_open = bpq_open,
466 .ndo_stop = bpq_close,
467 .ndo_start_xmit = bpq_xmit,
468 .ndo_set_mac_address = bpq_set_mac_address,
469 .ndo_do_ioctl = bpq_ioctl,
470 };
471
bpq_setup(struct net_device * dev)472 static void bpq_setup(struct net_device *dev)
473 {
474 dev->netdev_ops = &bpq_netdev_ops;
475 dev->destructor = free_netdev;
476
477 memcpy(dev->broadcast, &ax25_bcast, AX25_ADDR_LEN);
478 memcpy(dev->dev_addr, &ax25_defaddr, AX25_ADDR_LEN);
479
480 dev->flags = 0;
481 dev->features = NETIF_F_LLTX; /* Allow recursion */
482
483 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
484 dev->header_ops = &ax25_header_ops;
485 #endif
486
487 dev->type = ARPHRD_AX25;
488 dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN;
489 dev->mtu = AX25_DEF_PACLEN;
490 dev->addr_len = AX25_ADDR_LEN;
491
492 }
493
494 /*
495 * Setup a new device.
496 */
bpq_new_device(struct net_device * edev)497 static int bpq_new_device(struct net_device *edev)
498 {
499 int err;
500 struct net_device *ndev;
501 struct bpqdev *bpq;
502
503 ndev = alloc_netdev(sizeof(struct bpqdev), "bpq%d", NET_NAME_UNKNOWN,
504 bpq_setup);
505 if (!ndev)
506 return -ENOMEM;
507
508
509 bpq = netdev_priv(ndev);
510 dev_hold(edev);
511 bpq->ethdev = edev;
512 bpq->axdev = ndev;
513
514 eth_broadcast_addr(bpq->dest_addr);
515 eth_broadcast_addr(bpq->acpt_addr);
516
517 err = register_netdevice(ndev);
518 if (err)
519 goto error;
520 bpq_set_lockdep_class(ndev);
521
522 /* List protected by RTNL */
523 list_add_rcu(&bpq->bpq_list, &bpq_devices);
524 return 0;
525
526 error:
527 dev_put(edev);
528 free_netdev(ndev);
529 return err;
530
531 }
532
bpq_free_device(struct net_device * ndev)533 static void bpq_free_device(struct net_device *ndev)
534 {
535 struct bpqdev *bpq = netdev_priv(ndev);
536
537 dev_put(bpq->ethdev);
538 list_del_rcu(&bpq->bpq_list);
539
540 unregister_netdevice(ndev);
541 }
542
543 /*
544 * Handle device status changes.
545 */
bpq_device_event(struct notifier_block * this,unsigned long event,void * ptr)546 static int bpq_device_event(struct notifier_block *this,
547 unsigned long event, void *ptr)
548 {
549 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
550
551 if (!net_eq(dev_net(dev), &init_net))
552 return NOTIFY_DONE;
553
554 if (!dev_is_ethdev(dev))
555 return NOTIFY_DONE;
556
557 switch (event) {
558 case NETDEV_UP: /* new ethernet device -> new BPQ interface */
559 if (bpq_get_ax25_dev(dev) == NULL)
560 bpq_new_device(dev);
561 break;
562
563 case NETDEV_DOWN: /* ethernet device closed -> close BPQ interface */
564 if ((dev = bpq_get_ax25_dev(dev)) != NULL)
565 dev_close(dev);
566 break;
567
568 case NETDEV_UNREGISTER: /* ethernet device removed -> free BPQ interface */
569 if ((dev = bpq_get_ax25_dev(dev)) != NULL)
570 bpq_free_device(dev);
571 break;
572 default:
573 break;
574 }
575
576 return NOTIFY_DONE;
577 }
578
579
580 /* ------------------------------------------------------------------------ */
581
582 /*
583 * Initialize driver. To be called from af_ax25 if not compiled as a
584 * module
585 */
bpq_init_driver(void)586 static int __init bpq_init_driver(void)
587 {
588 #ifdef CONFIG_PROC_FS
589 if (!proc_create("bpqether", S_IRUGO, init_net.proc_net,
590 &bpq_info_fops)) {
591 printk(KERN_ERR
592 "bpq: cannot create /proc/net/bpqether entry.\n");
593 return -ENOENT;
594 }
595 #endif /* CONFIG_PROC_FS */
596
597 dev_add_pack(&bpq_packet_type);
598
599 register_netdevice_notifier(&bpq_dev_notifier);
600
601 printk(banner);
602
603 return 0;
604 }
605
bpq_cleanup_driver(void)606 static void __exit bpq_cleanup_driver(void)
607 {
608 struct bpqdev *bpq;
609
610 dev_remove_pack(&bpq_packet_type);
611
612 unregister_netdevice_notifier(&bpq_dev_notifier);
613
614 remove_proc_entry("bpqether", init_net.proc_net);
615
616 rtnl_lock();
617 while (!list_empty(&bpq_devices)) {
618 bpq = list_entry(bpq_devices.next, struct bpqdev, bpq_list);
619 bpq_free_device(bpq->axdev);
620 }
621 rtnl_unlock();
622 }
623
624 MODULE_AUTHOR("Joerg Reuter DL1BKE <jreuter@yaina.de>");
625 MODULE_DESCRIPTION("Transmit and receive AX.25 packets over Ethernet");
626 MODULE_LICENSE("GPL");
627 module_init(bpq_init_driver);
628 module_exit(bpq_cleanup_driver);
629