Import 2.1.15
[davej-history.git] / drivers / net / bpqether.c
blob167d3bd185cc64c08f47cf5af9c1922834e3c214
1 /*
2 * G8BPQ compatible "AX.25 via ethernet" driver release 003
4 * This is ALPHA test software. This code may break your machine, randomly
5 * fail to work with new releases, misbehave and/or generally screw up.
6 * It might even work.
8 * This code REQUIRES 2.0.0 or higher/ NET3.029
10 * This module:
11 * This module is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
16 * This is a "pseudo" network driver to allow AX.25 over Ethernet
17 * using G8BPQ encapsulation. It has been extracted from the protocol
18 * implementation because
20 * - things got unreadable within the protocol stack
21 * - to cure the protocol stack from "feature-ism"
22 * - a protocol implementation shouldn't need to know on
23 * which hardware it is running
24 * - user-level programs like the AX.25 utilities shouldn't
25 * need to know about the hardware.
26 * - IP over ethernet encapsulated AX.25 was impossible
27 * - rxecho.c did not work
28 * - to have room for extensions
29 * - it just deserves to "live" as an own driver
31 * This driver can use any ethernet destination address, and can be
32 * limited to accept frames from one dedicated ethernet card only.
34 * Note that the driver sets up the BPQ devices automagically on
35 * startup or (if started before the "insmod" of an ethernet device)
36 * on "ifconfig up". It hopefully will remove the BPQ on "rmmod"ing
37 * the ethernet device (in fact: as soon as another ethernet or bpq
38 * device gets "ifconfig"ured).
40 * I have heard that several people are thinking of experiments
41 * with highspeed packet radio using existing ethernet cards.
42 * Well, this driver is prepared for this purpose, just add
43 * your tx key control and a txdelay / tailtime algorithm,
44 * probably some buffering, and /voila/...
46 * History
47 * BPQ 001 Joerg(DL1BKE) Extracted BPQ code from AX.25
48 * protocol stack and added my own
49 * yet existing patches
50 * BPQ 002 Joerg(DL1BKE) Scan network device list on
51 * startup.
52 * BPQ 003 Joerg(DL1BKE) Ethernet destination address
53 * and accepted source address
54 * can be configured by an ioctl()
55 * call.
58 #include <linux/config.h>
59 #include <linux/errno.h>
60 #include <linux/types.h>
61 #include <linux/socket.h>
62 #include <linux/in.h>
63 #include <linux/kernel.h>
64 #include <linux/string.h>
65 #include <linux/net.h>
66 #include <net/ax25.h>
67 #include <linux/inet.h>
68 #include <linux/netdevice.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/firewall.h>
81 #include <linux/module.h>
82 #include <linux/net_alias.h>
84 #include <net/ip.h>
85 #include <net/arp.h>
87 #include <linux/bpqether.h>
89 static unsigned char ax25_bcast[AX25_ADDR_LEN] =
90 {'Q'<<1,'S'<<1,'T'<<1,' '<<1,' '<<1,' '<<1,'0'<<1};
91 static unsigned char ax25_defaddr[AX25_ADDR_LEN] =
92 {'L'<<1,'I'<<1,'N'<<1,'U'<<1,'X'<<1,' '<<1,'1'<<1};
94 static char bcast_addr[6]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
96 static char bpq_eth_addr[6];
98 static intbpq_rcv(struct sk_buff *,struct device *,struct packet_type *);
99 static intbpq_device_event(struct notifier_block *,unsigned long,void*);
100 static char*bpq_print_ethaddr(unsigned char*);
102 static struct packet_type bpq_packet_type = {
103 0,/* ntohs(ETH_P_BPQ),*/
104 0,/* copy */
105 bpq_rcv,
106 NULL,
107 NULL,
110 static struct notifier_block bpq_dev_notifier = {
111 bpq_device_event,
116 #define MAXBPQDEV 100
118 static struct bpqdev {
119 struct bpqdev *next;
120 char ethname[14];/* ether device name */
121 struct device *ethdev;/* link to ethernet device */
122 struct device axdev;/* bpq device (bpq#) */
123 struct enet_statistics stats;/* some statistics */
124 char dest_addr[6];/* ether destination address */
125 char acpt_addr[6];/* accept ether frames from this address only */
126 } *bpq_devices = NULL;
129 /* ------------------------------------------------------------------------ */
133 * Get the ethernet device for a BPQ device
135 static __inline__ struct device *bpq_get_ether_dev(struct device *dev)
137 struct bpqdev *bpq;
139 bpq = (struct bpqdev *)dev->priv;
141 return(bpq != NULL) ? bpq->ethdev : NULL;
145 * Get the BPQ device for the ethernet device
147 static __inline__ struct device *bpq_get_ax25_dev(struct device *dev)
149 struct bpqdev *bpq;
151 for(bpq = bpq_devices; bpq != NULL; bpq = bpq->next)
152 if(bpq->ethdev == dev)
153 return&bpq->axdev;
155 return NULL;
158 static __inline__ intdev_is_ethdev(struct device *dev)
160 return(
161 dev->type == ARPHRD_ETHER
162 &&strncmp(dev->name,"dummy",5)
163 #ifdef CONFIG_NET_ALIAS
164 && !net_alias_is(dev)
165 #endif
170 * Sanity check: remove all devices that ceased to exists and
171 * return '1' if the given BPQ device was affected.
173 static intbpq_check_devices(struct device *dev)
175 struct bpqdev *bpq, *bpq_prev;
176 int result =0;
177 unsigned long flags;
179 save_flags(flags);
180 cli();
182 bpq_prev = NULL;
184 for(bpq = bpq_devices; bpq != NULL; bpq = bpq->next) {
185 if(!dev_get(bpq->ethname)) {
186 if(bpq_prev)
187 bpq_prev->next = bpq->next;
188 else
189 bpq_devices = bpq->next;
191 if(&bpq->axdev == dev)
192 result =1;
194 unregister_netdev(&bpq->axdev);
195 kfree(bpq);
198 bpq_prev = bpq;
201 restore_flags(flags);
203 return result;
207 /* ------------------------------------------------------------------------ */
211 * Receive an AX.25 frame via an ethernet interface.
213 static intbpq_rcv(struct sk_buff *skb,struct device *dev,struct packet_type *ptype)
215 int len;
216 char* ptr;
217 struct ethhdr *eth = (struct ethhdr *)skb->mac.raw;
218 struct bpqdev *bpq;
220 skb->sk = NULL;/* Initially we don't know who it's for */
222 dev =bpq_get_ax25_dev(dev);
224 if(dev == NULL || dev->start ==0) {
225 kfree_skb(skb, FREE_READ);
226 return0;
230 * if we want to accept frames from just one ethernet device
231 * we check the source address of the sender.
234 bpq = (struct bpqdev *)dev->priv;
236 if(!(bpq->acpt_addr[0] &0x01) &&memcmp(eth->h_source, bpq->acpt_addr, ETH_ALEN)) {
237 printk(KERN_DEBUG "bpqether: wrong dest %s\n",bpq_print_ethaddr(eth->h_source));
238 kfree_skb(skb, FREE_READ);
239 return0;
242 ((struct bpqdev *)dev->priv)->stats.rx_packets++;
244 len = skb->data[0] + skb->data[1] *256-5;
246 skb_pull(skb,2);/* Remove the length bytes */
247 skb_trim(skb, len);/* Set the length of the data */
249 ptr =skb_push(skb,1);
250 *ptr =0;
252 skb->dev = dev;
253 skb->protocol =htons(ETH_P_AX25);
254 skb->mac.raw = skb->data;
255 skb->pkt_type = PACKET_HOST;
257 netif_rx(skb);
259 return0;
263 * Send an AX.25 frame via an ethernet interface
265 static intbpq_xmit(struct sk_buff *skb,struct device *dev)
267 unsigned char*ptr;
268 struct bpqdev *bpq;
269 int size;
272 * Just to be *really* sure not to send anything if the interface
273 * is down, the ethernet device may have gone.
275 if(!dev->start) {
276 bpq_check_devices(dev);
277 dev_kfree_skb(skb, FREE_WRITE);
278 return-ENODEV;
281 skb_pull(skb,1);
282 size = skb->len;
285 * The AX.25 code leaves enough room for the ethernet header, but
286 * sendto() does not.
288 if(skb_headroom(skb) < AX25_BPQ_HEADER_LEN) {/* Ough! */
289 struct sk_buff *newskb =alloc_skb(skb->len + AX25_BPQ_HEADER_LEN, GFP_ATOMIC);
291 if(newskb == NULL) {/* Argh! */
292 printk(KERN_WARNING "bpq_xmit: not enough space to add BPQ Ether header\n");
293 dev_kfree_skb(skb, FREE_WRITE);
294 return-ENOMEM;
297 newskb->arp =1;
298 if(skb->sk)
299 skb_set_owner_w(newskb, skb->sk);
301 skb_reserve(newskb, AX25_BPQ_HEADER_LEN);
302 memcpy(skb_put(newskb, size), skb->data, size);
303 dev_kfree_skb(skb, FREE_WRITE);
304 skb = newskb;
307 skb->protocol =htons(ETH_P_AX25);
309 ptr =skb_push(skb,2);
311 *ptr++ = (size +5) %256;
312 *ptr++ = (size +5) /256;
314 bpq = (struct bpqdev *)dev->priv;
315 bpq->stats.tx_packets++;
317 if((dev =bpq_get_ether_dev(dev)) == NULL) {
318 bpq->stats.tx_dropped++;
319 dev_kfree_skb(skb, FREE_WRITE);
320 return-ENODEV;
323 skb->dev = dev;
324 dev->hard_header(skb, dev, ETH_P_BPQ, bpq->dest_addr, NULL,0);
326 return dev->hard_start_xmit(skb, dev);
330 * Statistics
332 static struct enet_statistics *bpq_get_stats(struct device *dev)
334 struct bpqdev *bpq;
336 bpq = (struct bpqdev *)dev->priv;
338 return&bpq->stats;
343 * Rebuild header...
345 static intbpq_rebuild_header(void*buff,struct device *dev,unsigned long raddr,struct sk_buff *skb)
347 returnax25_rebuild_header((unsigned char*)buff, dev, raddr, skb);
351 * Set AX.25 callsign
353 static intbpq_set_mac_address(struct device *dev,void*addr)
355 struct sockaddr *sa = (struct sockaddr *)addr;
357 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
359 return0;
362 /* Ioctl commands
364 * SIOCSBPQETHOPT reserved for enhancements
365 * SIOCSBPQETHADDR set the destination and accepted
366 * source ethernet address (broadcast
367 * or multicast: accept all)
369 static intbpq_ioctl(struct device *dev,struct ifreq *ifr,int cmd)
371 int err;
372 struct bpq_ethaddr *ethaddr = (struct bpq_ethaddr *)ifr->ifr_data;
373 struct bpqdev *bpq = dev->priv;
374 struct bpq_req req;
376 if(!suser())
377 return-EPERM;
379 if(bpq == NULL)/* woops! */
380 return-ENODEV;
382 switch(cmd) {
383 case SIOCSBPQETHOPT:
384 if((err =verify_area(VERIFY_WRITE, ifr->ifr_data,sizeof(struct bpq_req))) !=0)
385 return err;
386 copy_from_user(&req, ifr->ifr_data,sizeof(struct bpq_req));
387 switch(req.cmd) {
388 case SIOCGBPQETHPARAM:
389 case SIOCSBPQETHPARAM:
390 default:
391 return-EINVAL;
394 break;
396 case SIOCSBPQETHADDR:
397 if((err =verify_area(VERIFY_READ, ethaddr,sizeof(struct bpq_ethaddr))) !=0)
398 return err;
399 copy_from_user(bpq->dest_addr, ethaddr->destination, ETH_ALEN);
400 copy_from_user(bpq->acpt_addr, ethaddr->accept, ETH_ALEN);
401 break;
403 default:
404 return-EINVAL;
407 return0;
411 * open/close a device
413 static intbpq_open(struct device *dev)
415 if(bpq_check_devices(dev))
416 return-ENODEV;/* oops, it's gone */
418 dev->tbusy =0;
419 dev->start =1;
421 MOD_INC_USE_COUNT;
423 return0;
426 static intbpq_close(struct device *dev)
428 dev->tbusy =1;
429 dev->start =0;
431 MOD_DEC_USE_COUNT;
433 return0;
437 * currently unused
439 static intbpq_dev_init(struct device *dev)
441 return0;
445 /* ------------------------------------------------------------------------ */
449 * Proc filesystem
451 static char*bpq_print_ethaddr(unsigned char*e)
453 static char buf[18];
455 sprintf(buf,"%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
456 e[0], e[1], e[2], e[3], e[4], e[5]);
458 return buf;
461 intbpq_get_info(char*buffer,char**start, off_t offset,int length,int dummy)
463 struct bpqdev *bpqdev;
464 int len =0;
465 off_t pos =0;
466 off_t begin =0;
468 cli();
470 len +=sprintf(buffer,"dev ether destination accept from\n");
472 for(bpqdev = bpq_devices; bpqdev != NULL; bpqdev = bpqdev->next) {
473 len +=sprintf(buffer + len,"%-5s %-10s %s ",
474 bpqdev->axdev.name, bpqdev->ethname,
475 bpq_print_ethaddr(bpqdev->dest_addr));
477 len +=sprintf(buffer + len,"%s\n",
478 (bpqdev->acpt_addr[0] &0x01) ?"*":bpq_print_ethaddr(bpqdev->acpt_addr));
480 pos = begin + len;
482 if(pos < offset) {
483 len =0;
484 begin = pos;
487 if(pos > offset + length)
488 break;
491 sti();
493 *start = buffer + (offset - begin);
494 len -= (offset - begin);
496 if(len > length) len = length;
498 return len;
502 /* ------------------------------------------------------------------------ */
506 * Setup a new device.
508 static intbpq_new_device(struct device *dev)
510 int k;
511 unsigned char*buf;
512 struct bpqdev *bpq, *bpq2;
514 if((bpq = (struct bpqdev *)kmalloc(sizeof(struct bpqdev), GFP_KERNEL)) == NULL)
515 return-ENOMEM;
517 memset(bpq,0,sizeof(struct bpqdev));
519 bpq->ethdev = dev;
521 bpq->ethname[sizeof(bpq->ethname)-1] ='\0';
522 strncpy(bpq->ethname, dev->name,sizeof(bpq->ethname)-1);
524 memcpy(bpq->dest_addr, bcast_addr,sizeof(bpq_eth_addr));
525 memcpy(bpq->acpt_addr, bcast_addr,sizeof(bpq_eth_addr));
527 dev = &bpq->axdev;
528 buf = (unsigned char*)kmalloc(14, GFP_KERNEL);
530 for(k =0; k < MAXBPQDEV; k++) {
531 struct device *odev;
533 sprintf(buf,"bpq%d", k);
535 if((odev =dev_get(buf)) == NULL ||bpq_check_devices(odev))
536 break;
539 if(k == MAXBPQDEV) {
540 kfree(bpq);
541 return-ENODEV;
544 dev->priv = (void*)bpq;/* pointer back */
545 dev->name = buf;
546 dev->init = bpq_dev_init;
548 if(register_netdev(dev) !=0) {
549 kfree(bpq);
550 return-EIO;
553 for(k=0; k < DEV_NUMBUFFS; k++)
554 skb_queue_head_init(&dev->buffs[k]);
556 dev->hard_start_xmit = bpq_xmit;
557 dev->hard_header = ax25_encapsulate;
558 dev->rebuild_header = bpq_rebuild_header;
559 dev->open = bpq_open;
560 dev->stop = bpq_close;
561 dev->set_mac_address = bpq_set_mac_address;
562 dev->get_stats = bpq_get_stats;
563 dev->do_ioctl = bpq_ioctl;
565 memcpy(dev->broadcast, ax25_bcast, AX25_ADDR_LEN);
566 memcpy(dev->dev_addr, ax25_defaddr, AX25_ADDR_LEN);
568 /* preset with reasonable values */
570 #if CONFIG_INET
571 dev->flags =0;
572 dev->family = AF_INET;
573 dev->pa_addr =0;
574 dev->pa_brdaddr =0;
575 dev->pa_mask =0;
576 dev->pa_alen =4;
577 #endif
579 dev->type = ARPHRD_AX25;
580 dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN;
581 dev->mtu = AX25_DEF_PACLEN;
582 dev->addr_len = AX25_ADDR_LEN;
584 cli();
586 if(bpq_devices == NULL) {
587 bpq_devices = bpq;
588 }else{
589 for(bpq2 = bpq_devices; bpq2->next != NULL; bpq2 = bpq2->next);
590 bpq2->next = bpq;
593 sti();
595 return0;
600 * Handle device status changes.
602 static intbpq_device_event(struct notifier_block *this,unsigned long event,void*ptr)
604 struct device *dev = (struct device *)ptr;
606 if(!dev_is_ethdev(dev))
607 return NOTIFY_DONE;
609 bpq_check_devices(NULL);
611 switch(event) {
612 case NETDEV_UP:/* new ethernet device -> new BPQ interface */
613 if(bpq_get_ax25_dev(dev) == NULL)
614 bpq_new_device(dev);
615 break;
617 case NETDEV_DOWN:/* ethernet device closed -> close BPQ interface */
618 if((dev =bpq_get_ax25_dev(dev)) != NULL)
619 dev_close(dev);
620 break;
622 default:
623 break;
626 return NOTIFY_DONE;
630 /* ------------------------------------------------------------------------ */
633 * Initialize driver. To be called from af_ax25 if not compiled as a
634 * module
636 intbpq_init(void)
638 struct device *dev;
640 bpq_packet_type.type =htons(ETH_P_BPQ);
641 dev_add_pack(&bpq_packet_type);
643 register_netdevice_notifier(&bpq_dev_notifier);
645 printk(KERN_INFO "AX.25 ethernet driver version 0.01\n");
647 #ifdef CONFIG_PROC_FS
648 proc_net_register(&(struct proc_dir_entry) {
649 PROC_NET_AX25_BPQETHER,8,"bpqether",
650 S_IFREG | S_IRUGO,1,0,0,
651 0, &proc_net_inode_operations,
652 bpq_get_info
654 #endif
656 for(dev = dev_base; dev != NULL; dev = dev->next) {
657 if(dev_is_ethdev(dev))
658 bpq_new_device(dev);
661 return0;
664 #ifdef MODULE
665 intinit_module(void)
667 register_symtab(NULL);
669 returnbpq_init();
672 voidcleanup_module(void)
674 struct bpqdev *bpq;
676 dev_remove_pack(&bpq_packet_type);
678 unregister_netdevice_notifier(&bpq_dev_notifier);
680 #ifdef CONFIG_PROC_FS
681 proc_net_unregister(PROC_NET_AX25_BPQETHER);
682 #endif
684 for(bpq = bpq_devices; bpq != NULL; bpq = bpq->next)
685 unregister_netdev(&bpq->axdev);
687 #endif
close