patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / net / hamradio / bpqether.c
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/system.h>
73 #include <asm/uaccess.h>
74 #include <linux/mm.h>
75 #include <linux/interrupt.h>
76 #include <linux/notifier.h>
77 #include <linux/proc_fs.h>
78 #include <linux/seq_file.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 const char *bpq_print_ethaddr(const 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 struct bpqdev {
118         struct list_head bpq_list;      /* list of bpq devices chain */
119         struct net_device *ethdev;      /* link to ethernet device */
120         struct net_device *axdev;       /* bpq device (bpq#) */
121         struct net_device_stats stats;  /* some statistics */
122         char   dest_addr[6];            /* ether destination address */
123         char   acpt_addr[6];            /* accept ether frames from this address only */
124 };
125
126 static LIST_HEAD(bpq_devices);
127
128
129 /* ------------------------------------------------------------------------ */
130
131
132 /*
133  *      Get the ethernet device for a BPQ device
134  */
135 static inline struct net_device *bpq_get_ether_dev(struct net_device *dev)
136 {
137         struct bpqdev *bpq = (struct bpqdev *) dev->priv;
138
139         return bpq ? bpq->ethdev : NULL;
140 }
141
142 /*
143  *      Get the BPQ device for the ethernet device
144  */
145 static inline struct net_device *bpq_get_ax25_dev(struct net_device *dev)
146 {
147         struct bpqdev *bpq;
148
149         list_for_each_entry(bpq, &bpq_devices, bpq_list) {
150                 if (bpq->ethdev == dev)
151                         return bpq->axdev;
152         }
153         return NULL;
154 }
155
156 static inline int dev_is_ethdev(struct net_device *dev)
157 {
158         return (
159                         dev->type == ARPHRD_ETHER
160                         && strncmp(dev->name, "dummy", 5)
161         );
162 }
163
164 /* ------------------------------------------------------------------------ */
165
166
167 /*
168  *      Receive an AX.25 frame via an ethernet interface.
169  */
170 static int bpq_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *ptype)
171 {
172         int len;
173         char * ptr;
174         struct ethhdr *eth;
175         struct bpqdev *bpq;
176
177         if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
178                 return NET_RX_DROP;
179
180         if (!pskb_may_pull(skb, sizeof(struct ethhdr)))
181                 goto drop;
182
183         rcu_read_lock();
184         dev = bpq_get_ax25_dev(dev);
185
186         if (dev == NULL || !netif_running(dev)) 
187                 goto drop_unlock;
188
189         /*
190          * if we want to accept frames from just one ethernet device
191          * we check the source address of the sender.
192          */
193
194         bpq = (struct bpqdev *)dev->priv;
195
196         eth = (struct ethhdr *)skb->mac.raw;
197
198         if (!(bpq->acpt_addr[0] & 0x01) &&
199             memcmp(eth->h_source, bpq->acpt_addr, ETH_ALEN))
200                 goto drop_unlock;
201
202         if (skb_cow(skb, sizeof(struct ethhdr)))
203                 goto drop_unlock;
204
205         len = skb->data[0] + skb->data[1] * 256 - 5;
206
207         skb_pull(skb, 2);       /* Remove the length bytes */
208         skb_trim(skb, len);     /* Set the length of the data */
209
210         bpq->stats.rx_packets++;
211         bpq->stats.rx_bytes += len;
212
213         ptr = skb_push(skb, 1);
214         *ptr = 0;
215
216         skb->dev = dev;
217         skb->protocol = htons(ETH_P_AX25);
218         skb->mac.raw = skb->data;
219         skb->pkt_type = PACKET_HOST;
220
221         netif_rx(skb);
222         dev->last_rx = jiffies;
223 unlock:
224
225         rcu_read_unlock();
226
227         return 0;
228 drop_unlock:
229         kfree_skb(skb);
230         goto unlock;
231
232 drop:
233         kfree_skb(skb);
234         return 0;
235 }
236
237 /*
238  *      Send an AX.25 frame via an ethernet interface
239  */
240 static int bpq_xmit(struct sk_buff *skb, struct net_device *dev)
241 {
242         struct sk_buff *newskb;
243         unsigned char *ptr;
244         struct bpqdev *bpq;
245         int size;
246
247         /*
248          * Just to be *really* sure not to send anything if the interface
249          * is down, the ethernet device may have gone.
250          */
251         if (!netif_running(dev)) {
252                 kfree_skb(skb);
253                 return -ENODEV;
254         }
255
256         skb_pull(skb, 1);
257         size = skb->len;
258
259         /*
260          * The AX.25 code leaves enough room for the ethernet header, but
261          * sendto() does not.
262          */
263         if (skb_headroom(skb) < AX25_BPQ_HEADER_LEN) {  /* Ough! */
264                 if ((newskb = skb_realloc_headroom(skb, AX25_BPQ_HEADER_LEN)) == NULL) {
265                         printk(KERN_WARNING "bpqether: out of memory\n");
266                         kfree_skb(skb);
267                         return -ENOMEM;
268                 }
269
270                 if (skb->sk != NULL)
271                         skb_set_owner_w(newskb, skb->sk);
272
273                 kfree_skb(skb);
274                 skb = newskb;
275         }
276
277         skb->protocol = htons(ETH_P_AX25);
278
279         ptr = skb_push(skb, 2);
280
281         *ptr++ = (size + 5) % 256;
282         *ptr++ = (size + 5) / 256;
283
284         bpq = (struct bpqdev *)dev->priv;
285
286         if ((dev = bpq_get_ether_dev(dev)) == NULL) {
287                 bpq->stats.tx_dropped++;
288                 kfree_skb(skb);
289                 return -ENODEV;
290         }
291
292         skb->dev = dev;
293         skb->nh.raw = skb->data;
294         dev->hard_header(skb, dev, ETH_P_BPQ, bpq->dest_addr, NULL, 0);
295         bpq->stats.tx_packets++;
296         bpq->stats.tx_bytes+=skb->len;
297   
298         dev_queue_xmit(skb);
299         netif_wake_queue(dev);
300         return 0;
301 }
302
303 /*
304  *      Statistics
305  */
306 static struct net_device_stats *bpq_get_stats(struct net_device *dev)
307 {
308         struct bpqdev *bpq = (struct bpqdev *) dev->priv;
309
310         return &bpq->stats;
311 }
312
313 /*
314  *      Set AX.25 callsign
315  */
316 static int bpq_set_mac_address(struct net_device *dev, void *addr)
317 {
318     struct sockaddr *sa = (struct sockaddr *)addr;
319
320     memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
321
322     return 0;
323 }
324
325 /*      Ioctl commands
326  *
327  *              SIOCSBPQETHOPT          reserved for enhancements
328  *              SIOCSBPQETHADDR         set the destination and accepted
329  *                                      source ethernet address (broadcast
330  *                                      or multicast: accept all)
331  */
332 static int bpq_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
333 {
334         struct bpq_ethaddr __user *ethaddr = ifr->ifr_data;
335         struct bpqdev *bpq = dev->priv;
336         struct bpq_req req;
337
338         if (!capable(CAP_NET_ADMIN))
339                 return -EPERM;
340
341         if (bpq == NULL)                /* woops! */
342                 return -ENODEV;
343
344         switch (cmd) {
345                 case SIOCSBPQETHOPT:
346                         if (copy_from_user(&req, ifr->ifr_data, sizeof(struct bpq_req)))
347                                 return -EFAULT;
348                         switch (req.cmd) {
349                                 case SIOCGBPQETHPARAM:
350                                 case SIOCSBPQETHPARAM:
351                                 default:
352                                         return -EINVAL;
353                         }
354
355                         break;
356
357                 case SIOCSBPQETHADDR:
358                         if (copy_from_user(bpq->dest_addr, ethaddr->destination, ETH_ALEN))
359                                 return -EFAULT;
360                         if (copy_from_user(bpq->acpt_addr, ethaddr->accept, ETH_ALEN))
361                                 return -EFAULT;
362                         break;
363
364                 default:
365                         return -EINVAL;
366         }
367
368         return 0;
369 }
370
371 /*
372  * open/close a device
373  */
374 static int bpq_open(struct net_device *dev)
375 {
376         netif_start_queue(dev);
377         return 0;
378 }
379
380 static int bpq_close(struct net_device *dev)
381 {
382         netif_stop_queue(dev);
383         return 0;
384 }
385
386
387 /* ------------------------------------------------------------------------ */
388
389
390 /*
391  *      Proc filesystem
392  */
393 static const char * bpq_print_ethaddr(const unsigned char *e)
394 {
395         static char buf[18];
396
397         sprintf(buf, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
398                 e[0], e[1], e[2], e[3], e[4], e[5]);
399
400         return buf;
401 }
402
403 static void *bpq_seq_start(struct seq_file *seq, loff_t *pos)
404 {
405         int i = 1;
406         struct bpqdev *bpqdev;
407
408         rcu_read_lock();
409
410         if (*pos == 0)
411                 return SEQ_START_TOKEN;
412         
413         list_for_each_entry(bpqdev, &bpq_devices, bpq_list) {
414                 if (i == *pos)
415                         return bpqdev;
416         }
417         return NULL;
418 }
419
420 static void *bpq_seq_next(struct seq_file *seq, void *v, loff_t *pos)
421 {
422         struct list_head *p;
423
424         ++*pos;
425
426         if (v == SEQ_START_TOKEN)
427                 p = bpq_devices.next;
428         else
429                 p = ((struct bpqdev *)v)->bpq_list.next;
430
431         return (p == &bpq_devices) ? NULL 
432                 : list_entry(p, struct bpqdev, bpq_list);
433 }
434
435 static void bpq_seq_stop(struct seq_file *seq, void *v)
436 {
437         rcu_read_unlock();
438 }
439
440
441 static int bpq_seq_show(struct seq_file *seq, void *v)
442 {
443         if (v == SEQ_START_TOKEN)
444                 seq_puts(seq, 
445                          "dev   ether      destination        accept from\n");
446         else {
447                 const struct bpqdev *bpqdev = v;
448
449                 seq_printf(seq, "%-5s %-10s %s  ",
450                         bpqdev->axdev->name, bpqdev->ethdev->name,
451                         bpq_print_ethaddr(bpqdev->dest_addr));
452
453                 seq_printf(seq, "%s\n",
454                         (bpqdev->acpt_addr[0] & 0x01) ? "*" 
455                            : bpq_print_ethaddr(bpqdev->acpt_addr));
456
457         }
458         return 0;
459 }
460
461 static struct seq_operations bpq_seqops = {
462         .start = bpq_seq_start,
463         .next = bpq_seq_next,
464         .stop = bpq_seq_stop,
465         .show = bpq_seq_show,
466 };
467
468 static int bpq_info_open(struct inode *inode, struct file *file)
469 {
470         return seq_open(file, &bpq_seqops);
471 }
472
473 static struct file_operations bpq_info_fops = {
474         .owner = THIS_MODULE,
475         .open = bpq_info_open,
476         .read = seq_read,
477         .llseek = seq_lseek,
478         .release = seq_release,
479 };
480
481
482 /* ------------------------------------------------------------------------ */
483
484
485 static void bpq_setup(struct net_device *dev)
486 {
487
488         dev->hard_start_xmit = bpq_xmit;
489         dev->open            = bpq_open;
490         dev->stop            = bpq_close;
491         dev->set_mac_address = bpq_set_mac_address;
492         dev->get_stats       = bpq_get_stats;
493         dev->do_ioctl        = bpq_ioctl;
494         dev->destructor      = free_netdev;
495
496         memcpy(dev->broadcast, ax25_bcast, AX25_ADDR_LEN);
497         memcpy(dev->dev_addr,  ax25_defaddr, AX25_ADDR_LEN);
498
499         dev->flags      = 0;
500
501 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
502         dev->hard_header     = ax25_encapsulate;
503         dev->rebuild_header  = ax25_rebuild_header;
504 #endif
505
506         dev->type            = ARPHRD_AX25;
507         dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN;
508         dev->mtu             = AX25_DEF_PACLEN;
509         dev->addr_len        = AX25_ADDR_LEN;
510
511 }
512
513 /*
514  *      Setup a new device.
515  */
516 static int bpq_new_device(struct net_device *edev)
517 {
518         int err;
519         struct net_device *ndev;
520         struct bpqdev *bpq;
521
522         ndev = alloc_netdev(sizeof(struct bpqdev), "bpq%d",
523                            bpq_setup);
524         if (!ndev)
525                 return -ENOMEM;
526
527                 
528         bpq = ndev->priv;
529         dev_hold(edev);
530         bpq->ethdev = edev;
531         bpq->axdev = ndev;
532
533         memcpy(bpq->dest_addr, bcast_addr, sizeof(bpq_eth_addr));
534         memcpy(bpq->acpt_addr, bcast_addr, sizeof(bpq_eth_addr));
535
536         err = dev_alloc_name(ndev, ndev->name);
537         if (err < 0) 
538                 goto error;
539
540         err = register_netdevice(ndev);
541         if (err)
542                 goto error;
543
544         /* List protected by RTNL */
545         list_add_rcu(&bpq->bpq_list, &bpq_devices);
546         return 0;
547
548  error:
549         dev_put(edev);
550         free_netdev(ndev);
551         return err;
552         
553 }
554
555 static void bpq_free_device(struct net_device *ndev)
556 {
557         struct bpqdev *bpq = ndev->priv;
558
559         dev_put(bpq->ethdev);
560         list_del_rcu(&bpq->bpq_list);
561
562         unregister_netdevice(ndev);
563 }
564
565 /*
566  *      Handle device status changes.
567  */
568 static int bpq_device_event(struct notifier_block *this,unsigned long event, void *ptr)
569 {
570         struct net_device *dev = (struct net_device *)ptr;
571
572         if (!dev_is_ethdev(dev))
573                 return NOTIFY_DONE;
574
575         rcu_read_lock();
576
577         switch (event) {
578         case NETDEV_UP:         /* new ethernet device -> new BPQ interface */
579                 if (bpq_get_ax25_dev(dev) == NULL)
580                         bpq_new_device(dev);
581                 break;
582
583         case NETDEV_DOWN:       /* ethernet device closed -> close BPQ interface */
584                 if ((dev = bpq_get_ax25_dev(dev)) != NULL)
585                         dev_close(dev);
586                 break;
587
588         case NETDEV_UNREGISTER: /* ethernet device removed -> free BPQ interface */
589                 if ((dev = bpq_get_ax25_dev(dev)) != NULL)
590                         bpq_free_device(dev);
591                 break;
592         default:
593                 break;
594         }
595         rcu_read_unlock();
596
597         return NOTIFY_DONE;
598 }
599
600
601 /* ------------------------------------------------------------------------ */
602
603 /*
604  * Initialize driver. To be called from af_ax25 if not compiled as a
605  * module
606  */
607 static int __init bpq_init_driver(void)
608 {
609 #ifdef CONFIG_PROC_FS
610         if (!proc_net_fops_create("bpqether", S_IRUGO, &bpq_info_fops)) {
611                 printk(KERN_ERR
612                         "bpq: cannot create /proc/net/bpqether entry.\n");
613                 return -ENOENT;
614         }
615 #endif  /* CONFIG_PROC_FS */
616
617         dev_add_pack(&bpq_packet_type);
618
619         register_netdevice_notifier(&bpq_dev_notifier);
620
621         printk(banner);
622
623         return 0;
624 }
625
626 static void __exit bpq_cleanup_driver(void)
627 {
628         struct bpqdev *bpq;
629
630         dev_remove_pack(&bpq_packet_type);
631
632         unregister_netdevice_notifier(&bpq_dev_notifier);
633
634         proc_net_remove("bpqether");
635
636         rtnl_lock();
637         while (!list_empty(&bpq_devices)) {
638                 bpq = list_entry(bpq_devices.next, struct bpqdev, bpq_list);
639                 bpq_free_device(bpq->axdev);
640         }
641         rtnl_unlock();
642 }
643
644 MODULE_AUTHOR("Joerg Reuter DL1BKE <jreuter@yaina.de>");
645 MODULE_DESCRIPTION("Transmit and receive AX.25 packets over Ethernet");
646 MODULE_LICENSE("GPL");
647 module_init(bpq_init_driver);
648 module_exit(bpq_cleanup_driver);