summaryrefslogtreecommitdiffstats
path: root/drivers/net/hamradio/ax25_userdev.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/net/hamradio/ax25_userdev.c')
-rw-r--r--drivers/net/hamradio/ax25_userdev.c596
1 files changed, 596 insertions, 0 deletions
diff --git a/drivers/net/hamradio/ax25_userdev.c b/drivers/net/hamradio/ax25_userdev.c
new file mode 100644
index 000000000..198df9134
--- /dev/null
+++ b/drivers/net/hamradio/ax25_userdev.c
@@ -0,0 +1,596 @@
+/*
+ * AX.25 user space device driver
+ * Portions Copyright 2001, by Joerg Reuter <jreuter@yaina.de>
+ *
+ * based on TUN - Universal TUN/TAP device driver.
+ * Copyright (C) 1999-2000 Maxim Krasnyansky <max_mk@yahoo.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/module.h>
+
+#include <linux/errno.h>
+#include <linux/kernel.h>
+#include <linux/major.h>
+#include <linux/sched.h>
+#include <linux/malloc.h>
+#include <linux/poll.h>
+#include <linux/fcntl.h>
+#include <linux/init.h>
+#include <linux/random.h>
+
+#include <net/ax25.h>
+#include <net/ax25dev.h>
+
+#include <linux/skbuff.h>
+#include <linux/netdevice.h>
+#include <linux/etherdevice.h>
+#include <linux/miscdevice.h>
+#include <linux/rtnetlink.h>
+#include <linux/if.h>
+#include <linux/if_arp.h>
+#include <linux/if_ether.h>
+#include <linux/ax25_userdev.h>
+
+#include <asm/system.h>
+#include <asm/uaccess.h>
+
+
+/* Network device part of the driver */
+
+static unsigned int ax25_udev_ddi_report_dcd(struct net_device *dev);
+static unsigned int ax25_udev_ddi_report_ptt(struct net_device *dev);
+static unsigned int ax25_udev_ddi_report_cts(struct net_device *dev);
+static void ax25_udev_ddi_set_rts(struct net_device *dev);
+static void ax25_udev_ddi_parameter_change_notify(struct net_device *dev, int item, int old, int new);
+
+/* Net device open. */
+static int ax25_udev_net_open(struct net_device *dev)
+{
+ struct ax25_udev_struct *ax25_udev = (struct ax25_udev_struct *) dev->priv;
+
+#ifdef notdef
+ /* I'm not quite sure if these are the right semantics: I think
+ this will make the MAC code believe that we actually are in
+ full duplex mode...
+ */
+
+ if (ax25_udev->capabilities.duplex)
+ ax25_dev_set_value(dev, AX25_VALUES_MEDIA_DUPLEX, 1);
+ else
+ ax25_dev_set_value(dev, AX25_VALUES_MEDIA_DUPLEX, 0);
+#endif
+
+ AX25_PTR(dev)->hw.dcd = ax25_udev_ddi_report_dcd;
+ AX25_PTR(dev)->hw.ptt = ax25_udev_ddi_report_ptt;
+ AX25_PTR(dev)->hw.fast = 0;
+
+ switch(ax25_udev->capabilities.arbitration)
+ {
+ case AX25_UDEV_CAP_ADVANCED_ARBITRATION:
+ AX25_PTR(dev)->hw.cts = ax25_udev_ddi_report_cts;
+ AX25_PTR(dev)->hw.rts = ax25_udev_ddi_set_rts;
+ break;
+ case AX25_UDEV_CAP_OWN_ARBITRATION:
+ AX25_PTR(dev)->hw.fast = 1;
+ /* fall through */
+ default:
+ AX25_PTR(dev)->hw.cts = NULL;
+ AX25_PTR(dev)->hw.rts = NULL;
+ }
+
+ AX25_PTR(dev)->hw.parameter_change_notify = ax25_udev_ddi_parameter_change_notify;
+
+ netif_start_queue(dev);
+ return 0;
+}
+
+/* Net device close. */
+static int ax25_udev_net_close(struct net_device *dev)
+{
+ netif_stop_queue(dev);
+ return 0;
+}
+
+/* Net device start xmit */
+
+static int ax25_udev_net_do_xmit(struct sk_buff *skb, struct net_device *dev)
+{
+ struct ax25_udev_struct *ax25_udev = (struct ax25_udev_struct *)dev->priv;
+
+ /* Queue frame */
+ skb_queue_tail(&ax25_udev->txq, skb);
+ if (skb_queue_len(&ax25_udev->txq) >= AX25_UDEV_TXQ_SIZE)
+ netif_stop_queue(dev);
+
+ if (ax25_udev->flags & AX25_UDEV_FASYNC)
+ kill_fasync(&ax25_udev->fasync, SIGIO, POLL_IN);
+
+ /* Wake up process */
+ wake_up_interruptible(&ax25_udev->read_wait);
+
+ return 0;
+}
+
+
+static int ax25_udev_net_xmit(struct sk_buff *skb, struct net_device *dev)
+{
+ struct ax25_udev_struct *ax25_udev = (struct ax25_udev_struct *)dev->priv;
+ DBG(KERN_INFO "%s: ax25_udev_net_xmit %d\n", ax25_udev->name, skb->len);
+
+ /* prepend packet type */
+ *((ax25_udev_pt_t *) skb_push(skb, sizeof(ax25_udev_pt_t))) = AX25_UDEV_DATA;
+ return ax25_udev_net_do_xmit(skb, dev);
+}
+
+static void ax25_udev_net_mclist(struct net_device *dev)
+{
+ /* Nothing to do for multicast filters.
+ * We always accept all frames.
+ */
+ return;
+}
+
+static int ax25_udev_set_mac_address(struct net_device *dev, void *addr)
+{
+ struct sockaddr *sa = (struct sockaddr *) addr;
+ memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
+ return 0;
+}
+
+static struct net_device_stats *ax25_udev_net_stats(struct net_device *dev)
+{
+ struct ax25_udev_struct *ax25_udev = (struct ax25_udev_struct *)dev->priv;
+
+ return &ax25_udev->stats;
+}
+
+/* Initialize net device. */
+int ax25_udev_net_init(struct net_device *dev)
+{
+ struct ax25_udev_struct *ax25_udev = (struct ax25_udev_struct *)dev->priv;
+
+ DBG(KERN_INFO "%s: ax25_udev_net_init\n", ax25_udev->name);
+
+ SET_MODULE_OWNER(dev);
+ dev->open = ax25_udev_net_open;
+ dev->stop = ax25_udev_net_close;
+ dev->hard_start_xmit = ax25_udev_net_xmit;
+ dev->get_stats = ax25_udev_net_stats;
+ dev->set_mac_address = ax25_udev_set_mac_address;
+ dev->type = ARPHRD_AX25;
+ dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN;
+ dev->mtu = AX25_DEF_PACLEN;
+ dev->addr_len = AX25_ADDR_LEN;
+
+ AX25_PTR(dev) = &ax25_udev->ax25_dev;
+ memset(AX25_PTR(dev), 0, sizeof(struct ax25_dev));
+
+ ax25_udev->capabilities.duplex = AX25_UDEV_CAP_HALF_DUPLEX;
+ ax25_udev->capabilities.arbitration = AX25_UDEV_CAP_SIMPLE_ARBITRATION;
+
+ ax25_dev_set_value(dev, AX25_VALUES_MEDIA_DUPLEX, 0);
+ ax25_dev_set_value(dev, AX25_VALUES_MEDIA_TXDELAY, 160);
+ ax25_dev_set_value(dev, AX25_VALUES_MEDIA_TXTAIL, 10);
+ ax25_dev_set_value(dev, AX25_VALUES_MEDIA_AUTO_ADJUST, 0);
+
+ return 0;
+}
+
+static unsigned int ax25_udev_ddi_report_dcd(struct net_device *dev)
+{
+ struct ax25_udev_struct *ax25_udev = (struct ax25_udev_struct *) dev->priv;
+ return ax25_udev->status.dcd;
+}
+
+static unsigned int ax25_udev_ddi_report_ptt(struct net_device *dev)
+{
+ struct ax25_udev_struct *ax25_udev = (struct ax25_udev_struct *) dev->priv;
+ return ax25_udev->status.ptt;
+}
+
+static unsigned int ax25_udev_ddi_report_cts(struct net_device *dev)
+{
+ struct ax25_udev_struct *ax25_udev = (struct ax25_udev_struct *) dev->priv;
+ return ax25_udev->status.cts;
+}
+
+static void ax25_udev_ddi_set_rts(struct net_device *dev)
+{
+ struct ax25_udev_struct *ax25_udev = (struct ax25_udev_struct *) dev->priv;
+ struct sk_buff *skb;
+
+ skb = dev_alloc_skb(sizeof(ax25_udev_pt_t));
+ if (skb == NULL) return; /* Ouch! */
+
+ skb->dev = &ax25_udev->dev;
+ skb->protocol = htons(ETH_P_AX25);
+ skb->mac.raw = skb->data;
+
+ *((ax25_udev_pt_t *) skb_put(skb, sizeof(ax25_udev_pt_t))) = AX25_UDEV_REQUEST_RTS;
+
+ /* question: is this safe or must we protect it with a spin lock? */
+ ax25_udev_net_do_xmit(skb, dev);
+}
+
+static void ax25_udev_ddi_parameter_change_notify(struct net_device *dev, int item, int old, int new)
+{
+ struct ax25_udev_struct *ax25_udev = (struct ax25_udev_struct *) dev->priv;
+ struct sk_buff *skb;
+
+ skb = dev_alloc_skb(sizeof(ax25_udev_pt_t)+3*sizeof(ax25_udev_val_t));
+ if (skb == NULL) return; /* Ouch! */
+
+ skb->dev = &ax25_udev->dev;
+ skb->protocol = htons(ETH_P_AX25);
+ skb->mac.raw = skb->data;
+
+ *((ax25_udev_pt_t *) skb_put(skb, sizeof(ax25_udev_pt_t))) = AX25_UDEV_SET_MAC_VALUE;
+ *((ax25_udev_val_t *) skb_put(skb, sizeof(ax25_udev_val_t))) = item;
+ *((ax25_udev_val_t *) skb_put(skb, sizeof(ax25_udev_val_t))) = old;
+ *((ax25_udev_val_t *) skb_put(skb, sizeof(ax25_udev_val_t))) = new;
+
+ /* question: is this safe or must we protect it with a spin lock? */
+ ax25_udev_net_do_xmit(skb, dev);
+}
+
+
+/* Character device part */
+
+/* Poll */
+static unsigned int ax25_udev_chr_poll(struct file *file, poll_table * wait)
+{
+ struct ax25_udev_struct *ax25_udev = (struct ax25_udev_struct *)file->private_data;
+
+ DBG(KERN_INFO "%s: ax25_udev_chr_poll\n", ax25_udev->name);
+
+ poll_wait(file, &ax25_udev->read_wait, wait);
+
+ if (skb_queue_len(&ax25_udev->txq))
+ return POLLIN | POLLRDNORM;
+
+ return POLLOUT | POLLWRNORM;
+}
+
+
+/* Write to netdevice (ie. read from daemon) */
+
+static ssize_t ax25_udev_chr_write(struct file * file, const char * buf,
+ size_t count, loff_t *pos)
+{
+ struct ax25_udev_struct *ax25_udev = (struct ax25_udev_struct *)file->private_data;
+ struct sk_buff *skb;
+ ax25_udev_pt_t packet_type;
+ ax25_udev_val_t * arg_ptr;
+ ax25_udev_val_t item, value;
+
+ DBG(KERN_INFO "%s: ax25_udev_chr_write %d\n", ax25_udev->name, count);
+
+ if (!(ax25_udev->flags & AX25_UDEV_IFF_SET))
+ return -EBUSY;
+
+ if (verify_area(VERIFY_READ, buf, count))
+ return -EFAULT;
+
+ if (count > AX25_UDEV_MAX_FRAME)
+ return -EINVAL;
+
+ skb = dev_alloc_skb(count + MAX_HEADER);
+ if (skb == NULL)
+ return -ENOMEM;
+
+ skb_reserve(skb, MAX_HEADER);
+ skb->dev = &ax25_udev->dev;
+ copy_from_user(skb_put(skb, count), buf, count);
+
+ packet_type = *((ax25_udev_pt_t *) skb->data);
+ arg_ptr = (ax25_udev_val_t *) skb_pull(skb, sizeof(ax25_udev_pt_t));
+
+ switch(packet_type)
+ {
+ case AX25_UDEV_DATA:
+ skb->protocol = htons(ETH_P_AX25);
+ skb->mac.raw = skb->data;
+ netif_rx(skb);
+ ax25_udev->stats.rx_packets++;
+ ax25_udev->stats.rx_bytes += count;
+ return count;
+
+ case AX25_UDEV_CAPABILITIES:
+ while (skb->len >= sizeof(ax25_udev_val_t))
+ {
+ switch (*arg_ptr)
+ {
+ case AX25_UDEV_CAP_HALF_DUPLEX:
+ case AX25_UDEV_CAP_FULL_DUPLEX:
+ ax25_udev->capabilities.duplex = *arg_ptr;
+ break;
+ case AX25_UDEV_CAP_ADVANCED_ARBITRATION:
+ case AX25_UDEV_CAP_OWN_ARBITRATION:
+ case AX25_UDEV_CAP_SIMPLE_ARBITRATION:
+ ax25_udev->capabilities.arbitration = *arg_ptr;
+ break;
+ default:
+ break;
+
+ }
+ arg_ptr = (ax25_udev_val_t *) skb_pull(skb, sizeof(ax25_udev_val_t));
+ }
+ break;
+
+ case AX25_UDEV_DCD_STATUS:
+ ax25_udev->status.dcd = *arg_ptr;
+ break;
+
+ case AX25_UDEV_CTS_STATUS:
+ ax25_udev->status.cts = *arg_ptr;
+ break;
+ case AX25_UDEV_PTT_STATUS:
+ ax25_udev->status.ptt = *arg_ptr;
+ break;
+ case AX25_UDEV_SET_MAC_VALUE:
+ while (skb->len >= sizeof(ax25_udev_val_t) * 2)
+ {
+ item = *arg_ptr;
+ arg_ptr = (ax25_udev_val_t *) skb_pull(skb, sizeof(ax25_udev_val_t));
+ value = *arg_ptr;
+ arg_ptr = (ax25_udev_val_t *) skb_pull(skb, sizeof(ax25_udev_val_t));
+
+ if (item < 0 || item > AX25_MAX_VALUES)
+ {
+ kfree_skb(skb);
+ return -EINVAL;
+ }
+
+ ax25_dev_set_value(skb->dev, item, value);
+ }
+
+ break;
+ default:
+ break;
+ }
+
+ kfree_skb(skb);
+
+ return count;
+}
+
+/* Put packet to user space buffer(already verified) */
+static __inline__ ssize_t ax25_udev_put_user(struct ax25_udev_struct *ax25_udev,
+ struct sk_buff *skb,
+ char *buf, int count)
+{
+ int len = count, total = 0;
+ char *ptr = buf;
+
+ len = (skb->len < len)? skb->len:len;
+ copy_to_user(ptr, skb->data, len);
+ total += len;
+
+ ax25_udev->stats.tx_packets++;
+ ax25_udev->stats.tx_bytes += len;
+
+ return total;
+}
+
+
+/* Read */
+static ssize_t ax25_udev_chr_read(struct file * file, char * buf,
+ size_t count, loff_t *pos)
+{
+ struct ax25_udev_struct *ax25_udev = (struct ax25_udev_struct *)file->private_data;
+ DECLARE_WAITQUEUE(wait, current);
+ struct sk_buff *skb;
+ ssize_t ret = 0;
+
+ DBG(KERN_INFO "%s: ax25_udev_chr_read\n", ax25_udev->name);
+
+ add_wait_queue(&ax25_udev->read_wait, &wait);
+ while (count) {
+ current->state = TASK_INTERRUPTIBLE;
+
+ /* Read frames from device queue */
+ if (!(skb=skb_dequeue(&ax25_udev->txq))) {
+ if (file->f_flags & O_NONBLOCK) {
+ ret = -EAGAIN;
+ break;
+ }
+ if (signal_pending(current)) {
+ ret = -ERESTARTSYS;
+ break;
+ }
+
+ /* Nothing to read, let's sleep */
+ schedule();
+ continue;
+ }
+ netif_start_queue(&ax25_udev->dev);
+
+ if (!verify_area(VERIFY_WRITE, buf, count))
+ ret = ax25_udev_put_user(ax25_udev, skb, buf, count);
+ else
+ ret = -EFAULT;
+
+ kfree_skb(skb);
+ break;
+ }
+
+ current->state = TASK_RUNNING;
+ remove_wait_queue(&ax25_udev->read_wait, &wait);
+
+ return ret;
+}
+
+static loff_t ax25_udev_chr_lseek(struct file * file, loff_t offset, int origin)
+{
+ return -ESPIPE;
+}
+
+static int ax25_udev_set_iff(struct ax25_udev_struct *ax25_udev, unsigned long arg)
+{
+ struct ifreq ifr;
+ char *mask;
+
+ if (copy_from_user(&ifr, (void *)arg, sizeof(ifr)))
+ return -EFAULT;
+ ifr.ifr_name[IFNAMSIZ-1] = '\0';
+
+ if (ax25_udev->flags & AX25_UDEV_IFF_SET)
+ return -EEXIST;
+
+ mask = "ax%d";
+ if (*ifr.ifr_name)
+ strcpy(ax25_udev->dev.name, ifr.ifr_name);
+ else
+ strcpy(ax25_udev->dev.name, mask);
+
+ /* Register net device */
+ if (register_netdev(&ax25_udev->dev))
+ return -EBUSY;
+
+ ax25_udev->flags |= AX25_UDEV_IFF_SET;
+ strcpy(ax25_udev->name, ax25_udev->dev.name);
+
+ /* Return iface info to the user space */
+ strcpy(ifr.ifr_name, ax25_udev->dev.name);
+ copy_to_user((void *)arg, &ifr, sizeof(ifr));
+
+ return 0;
+}
+
+static int ax25_udev_chr_ioctl(struct inode *inode, struct file *file,
+ unsigned int cmd, unsigned long arg)
+{
+ struct ax25_udev_struct *ax25_udev = (struct ax25_udev_struct *)file->private_data;
+
+ DBG(KERN_INFO "%s: ax25_udev_chr_ioctl\n", ax25_udev->name);
+
+ switch (cmd) {
+ case AX25_UDEV_SETIFF:
+ return ax25_udev_set_iff(ax25_udev, arg);
+
+#ifdef AX25_UDEV_DEBUG
+ case AX25_UDEV_SETDEBUG:
+ ax25_udev->debug = arg;
+ break;
+#endif
+
+ default:
+ return -EINVAL;
+ };
+
+ return 0;
+}
+
+static int ax25_udev_chr_fasync(int fd, struct file *file, int on)
+{
+ struct ax25_udev_struct *ax25_udev = (struct ax25_udev_struct *)file->private_data;
+ int ret;
+
+ DBG(KERN_INFO "%s: ax25_udev_chr_fasync %d\n", ax25_udev->name, on);
+
+ if ((ret = fasync_helper(fd, file, on, &ax25_udev->fasync)) < 0)
+ return ret;
+
+ if (on)
+ ax25_udev->flags |= AX25_UDEV_FASYNC;
+ else
+ ax25_udev->flags &= ~AX25_UDEV_FASYNC;
+
+ return 0;
+}
+
+static int ax25_udev_chr_open(struct inode *inode, struct file * file)
+{
+ struct ax25_udev_struct *ax25_udev = NULL;
+
+// DBG1(KERN_INFO "ax25_udevX: ax25_udev_chr_open\n");
+
+ ax25_udev = kmalloc(sizeof(struct ax25_udev_struct), GFP_KERNEL);
+ if (ax25_udev == NULL)
+ return -ENOMEM;
+
+ memset(ax25_udev, 0, sizeof(struct ax25_udev_struct));
+ file->private_data = ax25_udev;
+
+ skb_queue_head_init(&ax25_udev->txq);
+ init_waitqueue_head(&ax25_udev->read_wait);
+
+ sprintf(ax25_udev->name, "ax25");
+
+ ax25_udev->dev.init = ax25_udev_net_init;
+ ax25_udev->dev.priv = ax25_udev;
+
+ return 0;
+}
+
+static int ax25_udev_chr_close(struct inode *inode, struct file *file)
+{
+ struct ax25_udev_struct *ax25_udev = (struct ax25_udev_struct *)file->private_data;
+
+ DBG(KERN_INFO "%s: ax25_udev_chr_close\n", ax25_udev->name);
+
+ if (ax25_udev->flags & AX25_UDEV_IFF_SET) {
+ rtnl_lock();
+ dev_close(&ax25_udev->dev);
+ rtnl_unlock();
+
+ /* Drop TX queue */
+ skb_queue_purge(&ax25_udev->txq);
+
+ unregister_netdev(&ax25_udev->dev);
+ }
+
+ kfree(ax25_udev);
+ file->private_data = NULL;
+
+ return 0;
+}
+
+static struct file_operations ax25_udev_fops = {
+ owner: THIS_MODULE,
+ llseek: ax25_udev_chr_lseek,
+ read: ax25_udev_chr_read,
+ write: ax25_udev_chr_write,
+ poll: ax25_udev_chr_poll,
+ ioctl: ax25_udev_chr_ioctl,
+ open: ax25_udev_chr_open,
+ release:ax25_udev_chr_close,
+ fasync: ax25_udev_chr_fasync
+};
+
+static struct miscdevice ax25_udev_miscdev=
+{
+ AX25_UDEV_MINOR,
+ "net/ax25",
+ &ax25_udev_fops
+};
+
+int __init ax25_udev_init(void)
+{
+ printk(KERN_INFO "AX.25: userspace network device support driver version %s\n", AX25_UDEV_VER);
+
+ if (misc_register(&ax25_udev_miscdev)) {
+ printk(KERN_ERR "ax25_udev: Can't register misc device %d\n", AX25_UDEV_MINOR);
+ return -EIO;
+ }
+
+ return 0;
+}
+
+void ax25_udev_cleanup(void)
+{
+ misc_deregister(&ax25_udev_miscdev);
+}
+
+module_init(ax25_udev_init);
+module_exit(ax25_udev_cleanup);