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. 8 * This code REQUIRES 2.0.0 or higher/ NET3.029 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/... 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 52 * BPQ 003 Joerg(DL1BKE) Ethernet destination address 53 * and accepted source address 54 * can be configured by an ioctl() 58 #include <linux/config.h> 59 #include <linux/errno.h> 60 #include <linux/types.h> 61 #include <linux/socket.h> 63 #include <linux/kernel.h> 64 #include <linux/string.h> 65 #include <linux/net.h> 67 #include <linux/inet.h> 68 #include <linux/netdevice.h> 69 #include <linux/if_arp.h> 70 #include <linux/skbuff.h> 72 #include <asm/segment.h> 73 #include <asm/system.h> 74 #include <asm/uaccess.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> 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),*/ 110 static struct notifier_block bpq_dev_notifier
= { 116 #define MAXBPQDEV 100 118 static struct bpqdev
{ 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
) 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
) 151 for(bpq
= bpq_devices
; bpq
!= NULL
; bpq
= bpq
->next
) 152 if(bpq
->ethdev
== dev
) 158 static __inline__
intdev_is_ethdev(struct device
*dev
) 161 dev
->type
== ARPHRD_ETHER
162 &&strncmp(dev
->name
,"dummy",5) 163 #ifdef CONFIG_NET_ALIAS 164 && !net_alias_is(dev
) 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
; 184 for(bpq
= bpq_devices
; bpq
!= NULL
; bpq
= bpq
->next
) { 185 if(!dev_get(bpq
->ethname
)) { 187 bpq_prev
->next
= bpq
->next
; 189 bpq_devices
= bpq
->next
; 191 if(&bpq
->axdev
== dev
) 194 unregister_netdev(&bpq
->axdev
); 201 restore_flags(flags
); 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
) 217 struct ethhdr
*eth
= (struct ethhdr
*)skb
->mac
.raw
; 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
); 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
); 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); 253 skb
->protocol
=htons(ETH_P_AX25
); 254 skb
->mac
.raw
= skb
->data
; 255 skb
->pkt_type
= PACKET_HOST
; 263 * Send an AX.25 frame via an ethernet interface 265 static intbpq_xmit(struct sk_buff
*skb
,struct device
*dev
) 272 * Just to be *really* sure not to send anything if the interface 273 * is down, the ethernet device may have gone. 276 bpq_check_devices(dev
); 277 dev_kfree_skb(skb
, FREE_WRITE
); 285 * The AX.25 code leaves enough room for the ethernet header, but 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
); 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
); 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
); 324 dev
->hard_header(skb
, dev
, ETH_P_BPQ
, bpq
->dest_addr
, NULL
,0); 326 return dev
->hard_start_xmit(skb
, dev
); 332 static struct enet_statistics
*bpq_get_stats(struct device
*dev
) 336 bpq
= (struct bpqdev
*)dev
->priv
; 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
); 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
); 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
) 372 struct bpq_ethaddr
*ethaddr
= (struct bpq_ethaddr
*)ifr
->ifr_data
; 373 struct bpqdev
*bpq
= dev
->priv
; 379 if(bpq
== NULL
)/* woops! */ 384 if((err
=verify_area(VERIFY_WRITE
, ifr
->ifr_data
,sizeof(struct bpq_req
))) !=0) 386 copy_from_user(&req
, ifr
->ifr_data
,sizeof(struct bpq_req
)); 388 case SIOCGBPQETHPARAM
: 389 case SIOCSBPQETHPARAM
: 396 case SIOCSBPQETHADDR
: 397 if((err
=verify_area(VERIFY_READ
, ethaddr
,sizeof(struct bpq_ethaddr
))) !=0) 399 copy_from_user(bpq
->dest_addr
, ethaddr
->destination
, ETH_ALEN
); 400 copy_from_user(bpq
->acpt_addr
, ethaddr
->accept
, ETH_ALEN
); 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 */ 426 static intbpq_close(struct device
*dev
) 439 static intbpq_dev_init(struct device
*dev
) 445 /* ------------------------------------------------------------------------ */ 451 static char*bpq_print_ethaddr(unsigned char*e
) 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]); 461 intbpq_get_info(char*buffer
,char**start
, off_t offset
,int length
,int dummy
) 463 struct bpqdev
*bpqdev
; 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
)); 487 if(pos
> offset
+ length
) 493 *start
= buffer
+ (offset
- begin
); 494 len
-= (offset
- begin
); 496 if(len
> length
) len
= length
; 502 /* ------------------------------------------------------------------------ */ 506 * Setup a new device. 508 static intbpq_new_device(struct device
*dev
) 512 struct bpqdev
*bpq
, *bpq2
; 514 if((bpq
= (struct bpqdev
*)kmalloc(sizeof(struct bpqdev
), GFP_KERNEL
)) == NULL
) 517 memset(bpq
,0,sizeof(struct bpqdev
)); 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
)); 528 buf
= (unsigned char*)kmalloc(14, GFP_KERNEL
); 530 for(k
=0; k
< MAXBPQDEV
; k
++) { 533 sprintf(buf
,"bpq%d", k
); 535 if((odev
=dev_get(buf
)) == NULL
||bpq_check_devices(odev
)) 544 dev
->priv
= (void*)bpq
;/* pointer back */ 546 dev
->init
= bpq_dev_init
; 548 if(register_netdev(dev
) !=0) { 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 */ 572 dev
->family
= AF_INET
; 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
; 586 if(bpq_devices
== NULL
) { 589 for(bpq2
= bpq_devices
; bpq2
->next
!= NULL
; bpq2
= bpq2
->next
); 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
)) 609 bpq_check_devices(NULL
); 612 case NETDEV_UP
:/* new ethernet device -> new BPQ interface */ 613 if(bpq_get_ax25_dev(dev
) == NULL
) 617 case NETDEV_DOWN
:/* ethernet device closed -> close BPQ interface */ 618 if((dev
=bpq_get_ax25_dev(dev
)) != NULL
) 630 /* ------------------------------------------------------------------------ */ 633 * Initialize driver. To be called from af_ax25 if not compiled as a 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
, 656 for(dev
= dev_base
; dev
!= NULL
; dev
= dev
->next
) { 657 if(dev_is_ethdev(dev
)) 667 register_symtab(NULL
); 672 voidcleanup_module(void) 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
); 684 for(bpq
= bpq_devices
; bpq
!= NULL
; bpq
= bpq
->next
) 685 unregister_netdev(&bpq
->axdev
);