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/config.h>
58 #include <linux/errno.h>
59 #include <linux/types.h>
60 #include <linux/socket.h>
61 #include <linux/in.h>
62 #include <linux/kernel.h>
63 #include <linux/string.h>
64 #include <linux/net.h>
65 #include <net/ax25.h>
66 #include <linux/inet.h>
67 #include <linux/netdevice.h>
68 #include <linux/if_ether.h>
69 #include <linux/if_arp.h>
70 #include <linux/skbuff.h>
71 #include <net/sock.h>
72 #include <asm/segment.h>
73 #include <asm/system.h>
74 #include <asm/uaccess.h>
75 #include <linux/mm.h>
76 #include <linux/interrupt.h>
77 #include <linux/notifier.h>
78 #include <linux/proc_fs.h>
79 #include <linux/stat.h>
80 #include <linux/netfilter.h>
81 #include <linux/module.h>
82 #include <linux/init.h>
83 #include <linux/rtnetlink.h>
84
85 #include <net/ip.h>
86 #include <net/arp.h>
87
88 #include <linux/bpqether.h>
89
90 static char banner[] __initdata = KERN_INFO "AX.25: bpqether driver version 004\n";
91
92 static unsigned char ax25_bcast[AX25_ADDR_LEN] =
93 {'Q' << 1, 'S' << 1, 'T' << 1, ' ' << 1, ' ' << 1, ' ' << 1, '0' << 1};
94 static unsigned char ax25_defaddr[AX25_ADDR_LEN] =
95 {'L' << 1, 'I' << 1, 'N' << 1, 'U' << 1, 'X' << 1, ' ' << 1, '1' << 1};
96
97 static char bcast_addr[6]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
98
99 static char bpq_eth_addr[6];
100
101 static int bpq_rcv(struct sk_buff *, struct net_device *, struct packet_type *);
102 static int bpq_device_event(struct notifier_block *, unsigned long, void *);
103 static char *bpq_print_ethaddr(unsigned char *);
104
105 static struct packet_type bpq_packet_type = {
106 type: __constant_htons(ETH_P_BPQ),
107 func: bpq_rcv,
108 };
109
110 static struct notifier_block bpq_dev_notifier = {
111 notifier_call: bpq_device_event,
112 };
113
114
115 #define MAXBPQDEV 100
116
117 static struct bpqdev {
118 struct bpqdev *next;
119 char ethname[14]; /* ether device name */
120 struct net_device *ethdev; /* link to ethernet device */
121 struct net_device axdev; /* bpq device (bpq#) */
122 struct net_device_stats stats; /* some statistics */
123 char dest_addr[6]; /* ether destination address */
124 char acpt_addr[6]; /* accept ether frames from this address only */
125 } *bpq_devices;
126
127
128 /* ------------------------------------------------------------------------ */
129
130
131 /*
132 * Get the ethernet device for a BPQ device
133 */
bpq_get_ether_dev(struct net_device * dev)134 static inline struct net_device *bpq_get_ether_dev(struct net_device *dev)
135 {
136 struct bpqdev *bpq = (struct bpqdev *) dev->priv;
137
138 return bpq ? bpq->ethdev : NULL;
139 }
140
141 /*
142 * Get the BPQ device for the ethernet device
143 */
bpq_get_ax25_dev(struct net_device * dev)144 static inline struct net_device *bpq_get_ax25_dev(struct net_device *dev)
145 {
146 struct bpqdev *bpq;
147
148 for (bpq = bpq_devices; bpq != NULL; bpq = bpq->next)
149 if (bpq->ethdev == dev)
150 return &bpq->axdev;
151
152 return NULL;
153 }
154
dev_is_ethdev(struct net_device * dev)155 static inline int dev_is_ethdev(struct net_device *dev)
156 {
157 return (
158 dev->type == ARPHRD_ETHER
159 && strncmp(dev->name, "dummy", 5)
160 );
161 }
162
163 /*
164 * Sanity check: remove all devices that ceased to exists and
165 * return '1' if the given BPQ device was affected.
166 */
bpq_check_devices(struct net_device * dev)167 static int bpq_check_devices(struct net_device *dev)
168 {
169 struct bpqdev *bpq, *bpq_prev, *bpq_next;
170 int result = 0;
171 unsigned long flags;
172
173 save_flags(flags);
174 cli();
175
176 bpq_prev = NULL;
177
178 for (bpq = bpq_devices; bpq != NULL; bpq = bpq_next) {
179 bpq_next = bpq->next;
180 if (!dev_get(bpq->ethname)) {
181 if (bpq_prev)
182 bpq_prev->next = bpq->next;
183 else
184 bpq_devices = bpq->next;
185
186 if (&bpq->axdev == dev)
187 result = 1;
188
189 /* We should be locked, call
190 * unregister_netdevice directly
191 */
192
193 unregister_netdevice(&bpq->axdev);
194 kfree(bpq);
195 }
196 else
197 bpq_prev = bpq;
198 }
199
200 restore_flags(flags);
201
202 return result;
203 }
204
205
206 /* ------------------------------------------------------------------------ */
207
208
209 /*
210 * Receive an AX.25 frame via an ethernet interface.
211 */
bpq_rcv(struct sk_buff * skb,struct net_device * dev,struct packet_type * ptype)212 static int bpq_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *ptype)
213 {
214 int len;
215 char * ptr;
216 struct ethhdr *eth = (struct ethhdr *)skb->mac.raw;
217 struct bpqdev *bpq;
218
219 skb->sk = NULL; /* Initially we don't know who it's for */
220
221 dev = bpq_get_ax25_dev(dev);
222
223 if (dev == NULL || !netif_running(dev)) {
224 kfree_skb(skb);
225 return 0;
226 }
227
228 /*
229 * if we want to accept frames from just one ethernet device
230 * we check the source address of the sender.
231 */
232
233 bpq = (struct bpqdev *)dev->priv;
234
235 if (!(bpq->acpt_addr[0] & 0x01) && memcmp(eth->h_source, bpq->acpt_addr, ETH_ALEN)) {
236 printk(KERN_DEBUG "bpqether: wrong dest %s\n", bpq_print_ethaddr(eth->h_source));
237 kfree_skb(skb);
238 return 0;
239 }
240
241 len = skb->data[0] + skb->data[1] * 256 - 5;
242
243 skb_pull(skb, 2); /* Remove the length bytes */
244 skb_trim(skb, len); /* Set the length of the data */
245
246 bpq->stats.rx_packets++;
247 bpq->stats.rx_bytes += len;
248
249 ptr = skb_push(skb, 1);
250 *ptr = 0;
251
252 skb->dev = dev;
253 skb->protocol = htons(ETH_P_AX25);
254 skb->mac.raw = skb->data;
255 skb->pkt_type = PACKET_HOST;
256
257 netif_rx(skb);
258
259 return 0;
260 }
261
262 /*
263 * Send an AX.25 frame via an ethernet interface
264 */
bpq_xmit(struct sk_buff * skb,struct net_device * dev)265 static int bpq_xmit(struct sk_buff *skb, struct net_device *dev)
266 {
267 struct sk_buff *newskb;
268 unsigned char *ptr;
269 struct bpqdev *bpq;
270 int size;
271
272 /*
273 * Just to be *really* sure not to send anything if the interface
274 * is down, the ethernet device may have gone.
275 */
276 if (!netif_running(dev)) {
277 bpq_check_devices(dev);
278 kfree_skb(skb);
279 return -ENODEV;
280 }
281
282 skb_pull(skb, 1);
283 size = skb->len;
284
285 /*
286 * The AX.25 code leaves enough room for the ethernet header, but
287 * sendto() does not.
288 */
289 if (skb_headroom(skb) < AX25_BPQ_HEADER_LEN) { /* Ough! */
290 if ((newskb = skb_realloc_headroom(skb, AX25_BPQ_HEADER_LEN)) == NULL) {
291 printk(KERN_WARNING "bpqether: out of memory\n");
292 kfree_skb(skb);
293 return -ENOMEM;
294 }
295
296 if (skb->sk != NULL)
297 skb_set_owner_w(newskb, skb->sk);
298
299 kfree_skb(skb);
300 skb = newskb;
301 }
302
303 skb->protocol = htons(ETH_P_AX25);
304
305 ptr = skb_push(skb, 2);
306
307 *ptr++ = (size + 5) % 256;
308 *ptr++ = (size + 5) / 256;
309
310 bpq = (struct bpqdev *)dev->priv;
311
312 if ((dev = bpq_get_ether_dev(dev)) == NULL) {
313 bpq->stats.tx_dropped++;
314 kfree_skb(skb);
315 return -ENODEV;
316 }
317
318 skb->dev = dev;
319 skb->nh.raw = skb->data;
320 dev->hard_header(skb, dev, ETH_P_BPQ, bpq->dest_addr, NULL, 0);
321 bpq->stats.tx_packets++;
322 bpq->stats.tx_bytes+=skb->len;
323
324 dev_queue_xmit(skb);
325 netif_wake_queue(dev);
326 return 0;
327 }
328
329 /*
330 * Statistics
331 */
bpq_get_stats(struct net_device * dev)332 static struct net_device_stats *bpq_get_stats(struct net_device *dev)
333 {
334 struct bpqdev *bpq = (struct bpqdev *) dev->priv;
335
336 return &bpq->stats;
337 }
338
339 /*
340 * Set AX.25 callsign
341 */
bpq_set_mac_address(struct net_device * dev,void * addr)342 static int bpq_set_mac_address(struct net_device *dev, void *addr)
343 {
344 struct sockaddr *sa = (struct sockaddr *)addr;
345
346 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
347
348 return 0;
349 }
350
351 /* Ioctl commands
352 *
353 * SIOCSBPQETHOPT reserved for enhancements
354 * SIOCSBPQETHADDR set the destination and accepted
355 * source ethernet address (broadcast
356 * or multicast: accept all)
357 */
bpq_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)358 static int bpq_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
359 {
360 struct bpq_ethaddr *ethaddr = (struct bpq_ethaddr *)ifr->ifr_data;
361 struct bpqdev *bpq = dev->priv;
362 struct bpq_req req;
363
364 if (!capable(CAP_NET_ADMIN))
365 return -EPERM;
366
367 if (bpq == NULL) /* woops! */
368 return -ENODEV;
369
370 switch (cmd) {
371 case SIOCSBPQETHOPT:
372 if (copy_from_user(&req, ifr->ifr_data, sizeof(struct bpq_req)))
373 return -EFAULT;
374 switch (req.cmd) {
375 case SIOCGBPQETHPARAM:
376 case SIOCSBPQETHPARAM:
377 default:
378 return -EINVAL;
379 }
380
381 break;
382
383 case SIOCSBPQETHADDR:
384 if (copy_from_user(bpq->dest_addr, ethaddr->destination, ETH_ALEN))
385 return -EFAULT;
386 if (copy_from_user(bpq->acpt_addr, ethaddr->accept, ETH_ALEN))
387 return -EFAULT;
388 break;
389
390 default:
391 return -EINVAL;
392 }
393
394 return 0;
395 }
396
397 /*
398 * open/close a device
399 */
bpq_open(struct net_device * dev)400 static int bpq_open(struct net_device *dev)
401 {
402 if (bpq_check_devices(dev))
403 return -ENODEV; /* oops, it's gone */
404
405 MOD_INC_USE_COUNT;
406
407 netif_start_queue(dev);
408 return 0;
409 }
410
bpq_close(struct net_device * dev)411 static int bpq_close(struct net_device *dev)
412 {
413 netif_stop_queue(dev);
414 MOD_DEC_USE_COUNT;
415 return 0;
416 }
417
418 /*
419 * currently unused
420 */
bpq_dev_init(struct net_device * dev)421 static int bpq_dev_init(struct net_device *dev)
422 {
423 return 0;
424 }
425
426
427 /* ------------------------------------------------------------------------ */
428
429
430 /*
431 * Proc filesystem
432 */
bpq_print_ethaddr(unsigned char * e)433 static char * bpq_print_ethaddr(unsigned char *e)
434 {
435 static char buf[18];
436
437 sprintf(buf, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
438 e[0], e[1], e[2], e[3], e[4], e[5]);
439
440 return buf;
441 }
442
bpq_get_info(char * buffer,char ** start,off_t offset,int length)443 static int bpq_get_info(char *buffer, char **start, off_t offset, int length)
444 {
445 struct bpqdev *bpqdev;
446 int len = 0;
447 off_t pos = 0;
448 off_t begin = 0;
449
450 cli();
451
452 len += sprintf(buffer, "dev ether destination accept from\n");
453
454 for (bpqdev = bpq_devices; bpqdev != NULL; bpqdev = bpqdev->next) {
455 len += sprintf(buffer + len, "%-5s %-10s %s ",
456 bpqdev->axdev.name, bpqdev->ethname,
457 bpq_print_ethaddr(bpqdev->dest_addr));
458
459 len += sprintf(buffer + len, "%s\n",
460 (bpqdev->acpt_addr[0] & 0x01) ? "*" : bpq_print_ethaddr(bpqdev->acpt_addr));
461
462 pos = begin + len;
463
464 if (pos < offset) {
465 len = 0;
466 begin = pos;
467 }
468
469 if (pos > offset + length)
470 break;
471 }
472
473 sti();
474
475 *start = buffer + (offset - begin);
476 len -= (offset - begin);
477
478 if (len > length) len = length;
479
480 return len;
481 }
482
483
484 /* ------------------------------------------------------------------------ */
485
486
487 /*
488 * Setup a new device.
489 */
bpq_new_device(struct net_device * dev)490 static int bpq_new_device(struct net_device *dev)
491 {
492 int k;
493 struct bpqdev *bpq, *bpq2;
494
495 if ((bpq = kmalloc(sizeof(struct bpqdev), GFP_KERNEL)) == NULL)
496 return -ENOMEM;
497
498 memset(bpq, 0, sizeof(struct bpqdev));
499
500 bpq->ethdev = dev;
501
502 bpq->ethname[sizeof(bpq->ethname)-1] = '\0';
503 strncpy(bpq->ethname, dev->name, sizeof(bpq->ethname)-1);
504
505 memcpy(bpq->dest_addr, bcast_addr, sizeof(bpq_eth_addr));
506 memcpy(bpq->acpt_addr, bcast_addr, sizeof(bpq_eth_addr));
507
508 dev = &bpq->axdev;
509
510 for (k = 0; k < MAXBPQDEV; k++) {
511 struct net_device *odev;
512
513 sprintf(dev->name, "bpq%d", k);
514
515 if ((odev = __dev_get_by_name(dev->name)) == NULL || bpq_check_devices(odev))
516 break;
517 }
518
519 if (k == MAXBPQDEV) {
520 kfree(bpq);
521 return -ENODEV;
522 }
523
524 dev->priv = (void *)bpq; /* pointer back */
525 dev->init = bpq_dev_init;
526
527 /* We should be locked, call register_netdevice() directly. */
528
529 if (register_netdevice(dev) != 0) {
530 kfree(bpq);
531 return -EIO;
532 }
533
534 dev->hard_start_xmit = bpq_xmit;
535 dev->open = bpq_open;
536 dev->stop = bpq_close;
537 dev->set_mac_address = bpq_set_mac_address;
538 dev->get_stats = bpq_get_stats;
539 dev->do_ioctl = bpq_ioctl;
540
541 memcpy(dev->broadcast, ax25_bcast, AX25_ADDR_LEN);
542 memcpy(dev->dev_addr, ax25_defaddr, AX25_ADDR_LEN);
543
544 dev->flags = 0;
545
546 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
547 dev->hard_header = ax25_encapsulate;
548 dev->rebuild_header = ax25_rebuild_header;
549 #endif
550
551 dev->type = ARPHRD_AX25;
552 dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN;
553 dev->mtu = AX25_DEF_PACLEN;
554 dev->addr_len = AX25_ADDR_LEN;
555
556 cli();
557
558 if (bpq_devices == NULL) {
559 bpq_devices = bpq;
560 } else {
561 for (bpq2 = bpq_devices; bpq2->next != NULL; bpq2 = bpq2->next);
562 bpq2->next = bpq;
563 }
564
565 sti();
566
567 return 0;
568 }
569
570
571 /*
572 * Handle device status changes.
573 */
bpq_device_event(struct notifier_block * this,unsigned long event,void * ptr)574 static int bpq_device_event(struct notifier_block *this,unsigned long event, void *ptr)
575 {
576 struct net_device *dev = (struct net_device *)ptr;
577
578 if (!dev_is_ethdev(dev))
579 return NOTIFY_DONE;
580
581 bpq_check_devices(NULL);
582
583 switch (event) {
584 case NETDEV_UP: /* new ethernet device -> new BPQ interface */
585 if (bpq_get_ax25_dev(dev) == NULL)
586 bpq_new_device(dev);
587 break;
588
589 case NETDEV_DOWN: /* ethernet device closed -> close BPQ interface */
590 if ((dev = bpq_get_ax25_dev(dev)) != NULL)
591 dev_close(dev);
592 break;
593
594 default:
595 break;
596 }
597
598 return NOTIFY_DONE;
599 }
600
601
602 /* ------------------------------------------------------------------------ */
603
604 /*
605 * Initialize driver. To be called from af_ax25 if not compiled as a
606 * module
607 */
bpq_init_driver(void)608 static int __init bpq_init_driver(void)
609 {
610 struct net_device *dev;
611
612 dev_add_pack(&bpq_packet_type);
613
614 register_netdevice_notifier(&bpq_dev_notifier);
615
616 printk(banner);
617
618 proc_net_create("bpqether", 0, bpq_get_info);
619
620 read_lock_bh(&dev_base_lock);
621 for (dev = dev_base; dev != NULL; dev = dev->next) {
622 if (dev_is_ethdev(dev)) {
623 read_unlock_bh(&dev_base_lock);
624 bpq_new_device(dev);
625 read_lock_bh(&dev_base_lock);
626 }
627 }
628 read_unlock_bh(&dev_base_lock);
629 return 0;
630 }
631
bpq_cleanup_driver(void)632 static void __exit bpq_cleanup_driver(void)
633 {
634 struct bpqdev *bpq;
635
636 dev_remove_pack(&bpq_packet_type);
637
638 unregister_netdevice_notifier(&bpq_dev_notifier);
639
640 proc_net_remove("bpqether");
641
642 for (bpq = bpq_devices; bpq != NULL; bpq = bpq->next)
643 unregister_netdev(&bpq->axdev);
644 }
645
646 MODULE_AUTHOR("Joerg Reuter DL1BKE <jreuter@yaina.de>");
647 MODULE_DESCRIPTION("Transmit and receive AX.25 packets over Ethernet");
648 MODULE_LICENSE("GPL");
649 module_init(bpq_init_driver);
650 module_exit(bpq_cleanup_driver);
651