+/* -*-c-*-
+ *
+ * $Id: unet.c,v 1.1 2001/01/25 22:03:39 mdw Exp $
+ *
+ * User-space network device support.
+ *
+ * (c) 1998 Mark Wooding
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of Usernet.
+ *
+ * Usernet 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.
+ *
+ * Usernet 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Usernet; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------*
+ *
+ * $Log: unet.c,v $
+ * Revision 1.1 2001/01/25 22:03:39 mdw
+ * Initial check-in (somewhat belated).
+ *
+ */
+
+/*----- Include files -----------------------------------------------------*/
+
+#include <linux/module.h>
+
+#include <asm/byteorder.h>
+#include <asm/segment.h>
+
+#include <linux/types.h>
+#include <linux/kernel.h>
+#include <linux/wait.h>
+#include <linux/sched.h>
+#include <linux/fs.h>
+#include <linux/poll.h>
+#include <linux/netdevice.h>
+#include <linux/etherdevice.h>
+#include <linux/if_arp.h>
+#include <linux/malloc.h>
+#include <linux/errno.h>
+#include <linux/init.h>
+
+#include "unetconf.h"
+#include "unet.h"
+
+MODULE_AUTHOR("Mark Wooding");
+MODULE_DESCRIPTION("Allows userland handling of a network interface");
+
+/*----- Debugging macros --------------------------------------------------*/
+
+#define UNET_DEBUGALWAYS 1
+#define UNET_DEBUGRUNTIME 0
+#define UNET_DEBUGNEVER -1
+
+/* --- If the macro isn't defined then be switchable at runtime --- */
+
+#ifndef UNET_DEBUG
+# define UNET_DEBUG UNET_DEBUGRUNTIME
+#endif
+
+/* --- Define the base macro @D@ according to the debug setting --- */
+
+#if UNET_DEBUG == UNET_DEBUGALWAYS
+# define D(x) { x }
+# define DIF(u, x) if ((u)->f & UNIF_DEBUG) { x }
+#elif UNET_DEBUG == UNET_DEBUGRUNTIME
+# define D(x) if (unet_debug) { x }
+# define DIF(u, x) if ((u)->f & UNIF_DEBUG) { x }
+#elif UNET_DEBUG == UNET_DEBUGNEVER
+# define D(x)
+# define DIF(u, x)
+#elif
+# error UNET_DEBUG set to invalid value (bug in configure script?)
+#endif
+
+/*----- Type definitions --------------------------------------------------*/
+
+/* --- Unet connection status --- *
+ *
+ * Records are stored in a slightly strange doubly linked list. The list
+ * exists so that I can find the next unused sequence number when creating
+ * new connections. It's odd because of the type of the @prev@ node;
+ * rather than being the address of the previous item, it's the address of
+ * the previous item's pointer to me, which among other good things means
+ * that it works on the list head too.
+ */
+
+struct unet {
+ struct unet *next, **prev; /* List of unet blocks */
+ struct device nif; /* Network interface block */
+ int seq; /* Sequence number for connection */
+ char name[UNET_NAMEMAX]; /* Buffer for my interface name */
+ struct wait_queue *q; /* Wait list for device reads */
+ struct sk_buff_head skbq; /* Queue of packets waiting */
+ struct enet_statistics e; /* Pointer to statistics block */
+ unsigned short protocol; /* Protocol for outgoing packets */
+ unsigned f; /* Userful flags */
+};
+
+/*----- Static variables --------------------------------------------------*/
+
+#if UNET_DEBUG == UNET_DEBUGRUNTIME
+static int unet_debug = 0;
+MODULE_PARM(unet_debug, "i");
+#endif
+
+static int unet_npersist = UNET_NPERSIST;
+static int unet_maxif = UNET_MAXIF;
+static struct unet *unet_persistent;
+static struct unet *unet_list = 0;
+
+MODULE_PARM(unet_npersist, "i");
+MODULE_PARM(unet_maxif, "i");
+
+/*----- Debugging code ----------------------------------------------------*/
+
+#if UNET_DEBUG != UNET_DEBUGNEVER
+
+/* --- @unet_dumpBlock@ --- *
+ *
+ * Arguments: @struct unet *u@ = pointer to block to dump
+ *
+ * Returns: ---
+ *
+ * Use: Dumps a unet object to syslogd.
+ */
+
+static void unet_dumpBlock(struct unet *u)
+{
+ printk(KERN_DEBUG "unet: dumping unet block at %p\n", u);
+ printk(KERN_DEBUG " sequence number = %d\n", u->seq);
+ printk(KERN_DEBUG " interface name = `%s'\n", u->name);
+ printk(KERN_DEBUG " flags =%s%s%s\n",
+ u->f & UNIF_TRANS ? " TRANS" : "",
+ u->f & UNIF_OPEN ? " OPEN" : "",
+ u->f & UNIF_DEBUG ? " DEBUG" : "");
+ printk(KERN_DEBUG " interface type = %d\n", u->nif.type);
+ printk(KERN_DEBUG " header len = %d\n", u->nif.hard_header_len);
+ printk(KERN_DEBUG " mtu = %d\n", u->nif.mtu);
+ printk(KERN_DEBUG " protocol = %d\n", ntohs(u->protocol));
+ printk(KERN_DEBUG " address len = %d\n", u->nif.addr_len);
+}
+
+/* --- @unet_dump@ --- *
+ *
+ * Arguments: ---
+ *
+ * Returns: ---
+ *
+ * Use: Dumps the entire unet state to syslogd.
+ */
+
+static void unet_dump(void)
+{
+ int i;
+ struct unet *u;
+
+ for (i = 0; i < unet_npersist; i++)
+ unet_dumpBlock(&unet_persistent[i]);
+ for (u = unet_list; u; u = u->next)
+ unet_dumpBlock(u);
+}
+
+/* --- @unet_hexdump@ --- *
+ *
+ * Arguments: @const char *prefix@ = prefix to print on output lines
+ * @const void *b@ = pointer to block to dump
+ * @size_t sz@ = size of block to dump
+ *
+ * Returns: ---
+ *
+ * Use: Dumps a hex block to the kernel log.
+ */
+
+#define UNET_HEXROWSZ 16
+
+static void unet_hexdump(const char *prefix, const char *b, size_t sz)
+{
+ const unsigned char *p = b;
+ size_t i;
+ unsigned long o = 0;
+ size_t c;
+
+ char buf[256], *q;
+
+ /* --- Now start work --- */
+
+ while (sz) {
+ q = buf;
+ q += sprintf(q, "%s%08lx : ", prefix, o);
+ for (i = 0; i < UNET_HEXROWSZ; i++) {
+ if (i < sz)
+ q += sprintf(q, "%02x ", p[i]);
+ else
+ q += sprintf(q, "** ");
+ }
+ *q++ = ':'; *q++ = ' ';
+ for (i = 0; i < UNET_HEXROWSZ; i++) {
+ if (i < sz)
+ *q++ = (p[i] >= 32 && p[i] < 127) ? p[i] : '.';
+ else
+ *q++ = '*';
+ }
+ *q++ = '\n';
+ *q++ = 0;
+ printk("%s", buf);
+ c = (sz >= UNET_HEXROWSZ) ? UNET_HEXROWSZ : sz;
+ sz -= c, p += c, o += c;
+ }
+}
+
+#endif
+
+/*----- The unet network interfaces ---------------------------------------*/
+
+/* --- @unet_ifopen@ --- *
+ *
+ * Arguments: @struct device *nif@ = pointer to network interface
+ *
+ * Returns: Zero or error condition.
+ *
+ * Use: Turns on the network interface ready for action. Oh, yes.
+ */
+
+static int unet_ifopen(struct device *nif)
+{
+ D( struct unet *u = nif->priv;
+ if (u->f & UNIF_DEBUG)
+ printk(KERN_DEBUG "unet: opening interface %s\n", u->name); )
+ MOD_INC_USE_COUNT;
+ return (0);
+}
+
+/* --- @unet_ifclose@ --- *
+ *
+ * Arguments: @struct device *nif@ = pointer to network interface
+ *
+ * Returns: Zero or error condition.
+ *
+ * Use: Turns off the network interface.
+ */
+
+static int unet_ifclose(struct device *nif)
+{
+ D( struct unet *u = nif->priv;
+ if (u->f & UNIF_DEBUG)
+ printk(KERN_DEBUG "unet: closing interface %d\n", u->seq); )
+ MOD_DEC_USE_COUNT;
+ return (0);
+}
+
+/* --- @unet_iftx@ --- *
+ *
+ * Arguments: @struct sk_buff *skb@ = incoming network packet
+ * @struct device *nif@ = pointer to network interface
+ *
+ * Returns: Zero or error condition.
+ *
+ * Use: Queues a network packet ready for collection by the user
+ * end of the business.
+ */
+
+static int unet_iftx(struct sk_buff *skb, struct device *nif)
+{
+ struct unet *u = nif->priv;
+ int qed;
+
+ DIF(u,
+ printk(KERN_DEBUG "unet: packet received on if %s\n", u->name);
+ unet_hexdump(KERN_DEBUG " ", skb->data, skb->len); )
+
+ /* --- Discard packets when nobody's listening --- */
+
+ if ((u->f & UNIF_OPEN) == 0) {
+ DIF(u, printk(KERN_DEBUG "unet: dropped packet: nobody's listening\n"); )
+ dev_kfree_skb(skb);
+ u->e.tx_dropped++;
+ return (0);
+ }
+
+ /* --- Discard packets when the queue's too long --- */
+
+ if (u->skbq.qlen >= UNET_QMAXLEN) {
+ DIF(u, printk(KERN_DEBUG "unet: refused packet: queue overflow\n"); )
+ return (-1);
+ }
+
+ /* --- Attach the buffer to the waiting list --- */
+
+ qed = u->skbq.qlen;
+ skb_queue_tail(&u->skbq, skb);
+ u->e.tx_packets++;
+ DIF(u, printk(KERN_DEBUG "unet: queued packet OK\n"); )
+
+ /* --- If there are waiting processes, give 'em a kick --- */
+
+ if (qed == 0) {
+ DIF(u, printk(KERN_DEBUG "unet: waking up sleeping listeners\n"); )
+ wake_up_interruptible(&u->q);
+ }
+ return (0);
+}
+
+/* --- @unet_ifgetstats@ --- *
+ *
+ * Arguments: @struct device *nif@ = pointer to network interface
+ *
+ * Returns: Pointer to a statistics buffer.
+ *
+ * Use: Returns a block of interface statistics.
+ */
+
+static struct enet_statistics *unet_ifgetstats(struct device *nif)
+{
+ struct unet *u = nif->priv;
+ DIF(u, printk(KERN_DEBUG "unet: stats request for if %s\n", u->name); )
+ return (&u->e);
+}
+
+/* --- @unet_ifinit@ --- *
+ *
+ * Arguments: @struct device *nif@ = pointer to network interface
+ *
+ * Returns: Zero or error condition.
+ *
+ * Use: Initialises a unet network interface.
+ */
+
+static int unet_ifinit(struct device *nif)
+{
+ struct unet *u = nif->priv;
+
+ DIF(u, printk(KERN_DEBUG "unet: initialise interface %s\n", u->name); )
+
+ /* --- Initialise statistics gathering --- */
+
+ memset(&u->e, 0, sizeof(u->e));
+ u->nif.get_stats = unet_ifgetstats;
+
+ /* --- Opening and closing interfaces --- */
+
+ u->nif.open = unet_ifopen;
+ u->nif.stop = unet_ifclose;
+
+ /* --- Sending packets to the `outside world' --- */
+
+ u->nif.hard_start_xmit = unet_iftx;
+
+ /* --- Some initialisation magic --- */
+
+#ifdef notdef
+ for (i = 0; i < DEV_NUMBUFFS; i++)
+ skb_queue_head_init(&u->nif.buffs[i]);
+#endif
+
+ /* --- Configure other grotty bits of the interface --- */
+
+ u->nif.hard_header = 0;
+ u->nif.rebuild_header = 0;
+ u->nif.set_mac_address = 0;
+
+ u->nif.type = ARPHRD_LOOPBACK; /* Got a better idea? */
+ u->nif.hard_header_len = 0;
+ u->nif.mtu = 1500 - MAX_HEADER;
+ u->nif.addr_len = 0;
+ u->nif.tx_queue_len = 16; /* I keep my own queue */
+
+ memset(u->nif.broadcast, 0xFFu, MAX_ADDR_LEN);
+
+ u->nif.flags = IFF_NOARP;
+
+ /* --- Finished! --- */
+
+ return (0);
+}
+
+/*----- Attachment management ---------------------------------------------*/
+
+/* --- @unet_setup@ --- *
+ *
+ * Arguments: @struct unet *u@ = pointer to a unet block
+ * @int seq@ = sequence number to allocate
+ *
+ * Returns: Zero or error condition.
+ *
+ * Use: Initialises a unet block ready for action.
+ */
+
+static int unet_setup(struct unet *u, int seq)
+{
+ static struct device tpl;
+ int e;
+
+ D( printk(KERN_DEBUG "unet: setting up unet block %d\n", seq); )
+
+ /* --- A little bit of initialisation --- */
+
+ u->seq = seq;
+ u->f = 0;
+ u->q = 0;
+
+ /* --- Inherit device debug flag from global flag --- */
+
+#if UNET_DEBUG == UNET_DEBUGRUNTIME
+ if (unet_debug)
+ u->f |= UNIF_DEBUG;
+#elif UNET_DEBUG == UNET_DEBUGALWAYS
+ u->f |= UNIF_DEBUG;
+#endif
+
+ /* --- Set up the network device --- */
+
+ u->nif = tpl;
+ sprintf(u->name, "unet%d", seq);
+ u->nif.name = u->name;
+ u->nif.priv = u;
+ u->nif.init = unet_ifinit;
+ u->protocol = htons(ETH_P_IP);
+
+ if ((e = register_netdev(&u->nif)) != 0) {
+ printk(KERN_ERR "unet: couldn't register net interface\n");
+ return (e);
+ }
+
+ /* --- Empty the skbuff list --- */
+
+ skb_queue_head_init(&u->skbq);
+
+ /* --- We're only finished, that's all --- */
+
+ return (0);
+}
+
+/* --- @unet_flush@ --- *
+ *
+ * Arguments: @struct unet *u@ = pointer to a unet block
+ *
+ * Returns: ---
+ *
+ * Use: Releases all the packets waiting for transmission.
+ */
+
+static void unet_flush(struct unet *u)
+{
+ struct sk_buff *skb;
+
+#if UNET_DEBUG != UNET_DEBUGNEVER
+ int i = 0;
+#endif
+
+ DIF(u, printk(KERN_DEBUG "unet: flushing packets on %s\n", u->name); )
+
+ while ((skb = skb_dequeue(&u->skbq)) != 0) {
+ dev_kfree_skb(skb);
+ D( i++; )
+ }
+ DIF(u, printk(KERN_DEBUG "unet: released %d waiting packets\n", i); )
+}
+
+/* --- @unet_kill@ --- *
+ *
+ * Arguments: @struct unet *u@ = pointer to a unet block
+ *
+ * Returns: ---
+ *
+ * Use: Kills and decommissions a Usernet block.
+ */
+
+static void unet_kill(struct unet *u)
+{
+ unet_flush(u);
+ unregister_netdev(&u->nif);
+}
+
+/*----- Handling the unet device ------------------------------------------*/
+
+/* --- @unet_devopen@ --- *
+ *
+ * Arguments: @struct inode *ino@ = inode block to open
+ * @struct file *f@ = file block to play with
+ *
+ * Returns: Zero or error condition.
+ *
+ * Use: Handles the opening of a unet device.
+ */
+
+static int unet_devopen(struct inode *ino, struct file *f)
+{
+ struct unet *u, **up;
+ int seq;
+ int e = 0;
+
+ D( printk(KERN_DEBUG "unet: device open request\n"); )
+
+ /* --- Decide whether this is a persistent unet --- */
+
+ if ((seq = MINOR(ino->i_rdev)) < unet_npersist) {
+ u = &unet_persistent[seq];
+ if (u->f & UNIF_OPEN) {
+ e = -EBUSY;
+ goto tidy_0;
+ }
+ D( printk(KERN_DEBUG "unet: opened persistent %s\n", u->name); )
+ }
+
+ /* --- Otherwise we've got to create a new one --- */
+
+ else {
+
+ /* --- Try to find a spare sequence number --- */
+
+ for (seq = unet_npersist, up = &unet_list; *up != 0;
+ seq++, up = &(*up)->next) {
+ if ((*up)->seq > seq)
+ break;
+ if (seq >= unet_maxif) {
+ printk("unet: all unets are occupied\n");
+ e = -ENFILE;
+ goto tidy_0;
+ }
+ }
+
+ D( printk(KERN_DEBUG "unet: allocated sequence number %d\n", seq); )
+
+ /* --- Allocate a new block --- */
+
+ if ((u = kmalloc(sizeof(*u), GFP_KERNEL)) == 0) {
+ printk(KERN_ERR "unet: couldn't allocate a unet block\n");
+ e = -ENOMEM;
+ goto tidy_0;
+ }
+
+ /* --- Initialise the block --- */
+
+ if ((e = unet_setup(u, seq)) != 0)
+ goto tidy_1;
+ u->f |= UNIF_TRANS;
+
+ /* --- Link the block into the list --- */
+
+ u->next = *up;
+ u->prev = up;
+ if (*up)
+ (*up)->prev = &u->next;
+ *up = u;
+
+ D( printk(KERN_DEBUG "unet: opened transient %d\n", seq);
+ unet_dumpBlock(u); )
+ }
+
+ /* --- Done --- */
+
+ u->f |= UNIF_OPEN;
+ f->private_data = u;
+ MOD_INC_USE_COUNT;
+ return (0);
+
+ /* --- Tidy up after little disasters --- */
+
+tidy_1:
+ kfree(u);
+tidy_0:
+ return (e);
+}
+
+/* --- @unet_devclose@ --- *
+ *
+ * Arguments: @struct inode *ino@ = pointer to inode block
+ * @struct file *f@ = pointer to file block
+ *
+ * Returns: ---
+ *
+ * Use: Frees up a unet connection.
+ */
+
+static int unet_devclose(struct inode *ino, struct file *f)
+{
+ struct unet *u = f->private_data;
+
+ DIF(u, printk(KERN_DEBUG "unet: closing %s\n", u->name); )
+
+ /* --- A transient unet needs to be destroyed --- */
+
+ if (u->f & UNIF_TRANS) {
+ *u->prev = u->next;
+ if (u->next)
+ u->next->prev = u->prev;
+ unet_kill(u);
+ kfree(u);
+ D( printk(KERN_DEBUG "unet: released transient unet\n"); )
+ }
+
+ /* --- A persistent unet needs to be shutdown --- */
+
+ else {
+ u->f &= ~UNIF_OPEN;
+ unet_flush(u);
+ DIF(u, printk(KERN_DEBUG "unet: unblocked persistent unet\n"); )
+ }
+
+ MOD_DEC_USE_COUNT;
+ return (0);
+}
+
+/* --- @unet_devpoll@ --- *
+ *
+ * Arguments: @struct file *f@ = pointer to my file block
+ * @struct poll_table_struct *p@ = poll table to wait on
+ *
+ * Returns: Nonzero if OK to continue, zero if waiting.
+ *
+ * Use: Plays a unet device's part in a @poll@(2) call.
+ */
+
+static unsigned unet_devpoll(struct file *f, struct poll_table_struct *p)
+{
+ struct unet *u = f->private_data;
+ unsigned m = 0;
+
+ DIF(u, printk(KERN_DEBUG "unet: poll request for %s\n", u->name); )
+
+ poll_wait(f, &u->q, p);
+ if (skb_peek(&u->skbq))
+ m |= POLLIN | POLLRDNORM;
+ m |= POLLOUT | POLLWRNORM;
+ return (m);
+}
+
+/* --- @unet_devwrite@ --- *
+ *
+ * Arguments: @struct file *f@ = pointer to the file block
+ * @char *buf@ = pointer to caller's buffer
+ * @size_t sz@ = size of data to send
+ * @loff_t *off@ = offset to set (ignored)
+ *
+ * Returns: Number of bytes written, or error condition.
+ *
+ * Use: Sends a packet of data. No buffering is done.
+ */
+
+static ssize_t unet_devwrite(struct file *f, const char *buf, size_t sz,
+ loff_t *off)
+{
+ struct unet *u = f->private_data;
+ struct sk_buff *skb;
+
+ DIF(u, printk(KERN_DEBUG "unet: write request for %s\n", u->name); )
+
+ /* --- Dump the packet we're meant to send --- */
+
+#ifdef UNET_DEBUG
+ if (u->f & UNIF_DEBUG) {
+ void *b = kmalloc(sz, GFP_KERNEL);
+ if (b) {
+ copy_from_user(b, buf, sz);
+ unet_hexdump(KERN_DEBUG " ", b, sz);
+ kfree(b);
+ } else
+ printk(KERN_NOTICE "unet: not enough memory to dump block");
+ }
+#endif
+
+ /* --- Allocate an skbuff for the block --- */
+
+ if ((skb = dev_alloc_skb(sz)) == 0) {
+ printk(KERN_ERR "unet: failed to allocate skbuff\n");
+ u->e.rx_dropped++;
+ return (-ENOSR);
+ }
+
+ /* --- Copy my data into the skbuff --- */
+
+ copy_from_user(skb_put(skb, sz), buf, sz);
+ skb->dev = &u->nif;
+ skb->mac.raw = skb->data;
+ skb->protocol = u->protocol;
+ netif_rx(skb);
+ u->e.rx_packets++;
+ return (sz);
+}
+
+/* --- @unet_devread@ --- *
+ *
+ * Arguments: @struct file *f@ = pointer to the file block
+ * @char *buf@ = pointer to caller's buffer
+ * @size_t sz@ = size of caller's buffer
+ * @loff_t *off@ = offset to set (ignored)
+ *
+ * Returns: Number of bytes read, or error condition.
+ *
+ * Use: Reads the next packet waiting for the device.
+ */
+
+static ssize_t unet_devread(struct file *f, char *buf, size_t sz,
+ loff_t *off)
+{
+ struct unet *u = f->private_data;
+ struct sk_buff *skb;
+
+ DIF(u, printk(KERN_DEBUG "unet: read request for %s\n", u->name); )
+
+ /* --- Is the user sane? --- *
+ *
+ * The UDP protocol returns immediately in response to a zero-length read,
+ * and following this behaviour seems to cause `least surprise'.
+ */
+
+ if (!sz)
+ return (0);
+
+ /* --- Make sure there's a packet waiting for me --- */
+
+ if ((skb = skb_dequeue(&u->skbq)) == 0) {
+ struct wait_queue wq = { current, 0 };
+
+ DIF(u, printk(KERN_DEBUG "unet: no packets waiting\n"); )
+
+ /* --- Check for nonblocking I/O --- */
+
+ if (f->f_flags & O_NONBLOCK) {
+ DIF(u, printk(KERN_DEBUG "unet: nonblocking read: fail\n"); )
+ return (-EWOULDBLOCK);
+ }
+
+ /* --- Otherwise block until there's a packet --- */
+
+ current->state = TASK_INTERRUPTIBLE;
+ add_wait_queue(&u->q, &wq);
+
+ do {
+
+ if (signal_pending(current)) {
+
+ DIF(u, printk(KERN_DEBUG "unet: interrupted by signal\n"); )
+
+ remove_wait_queue(&u->q, &wq);
+ current->state = TASK_RUNNING;
+ return (-ERESTARTSYS);
+ }
+
+ DIF(u, printk(KERN_DEBUG "unet: blocking until packet arrives\n"); )
+
+ schedule();
+
+ } while ((skb = skb_dequeue(&u->skbq)) == 0);
+
+ remove_wait_queue(&u->q, &wq);
+ current->state = TASK_RUNNING;
+ }
+
+ DIF(u, printk(KERN_DEBUG "unet: found a packet\n"); )
+
+ /* --- There is now a packet waiting --- */
+
+ if (sz > skb->len)
+ sz = skb->len;
+ copy_to_user(buf, skb->data, sz);
+ dev_kfree_skb(skb);
+
+ DIF(u, printk(KERN_DEBUG "unet: passed packet on to user\n"); )
+
+ return (sz);
+}
+
+/* --- @unet_devioctl@ --- *
+ *
+ * Arguments: @struct inode *ino@ = pointer to inode block
+ * @struct file *f@ = pointer to file block
+ * @unsigned int c@ = command code to execute
+ * @unsigned long arg@ = argument passed to me
+ *
+ * Returns: Positive return value, or negative error condition.
+ *
+ * Use: Performs miscellaneous operations on a unet device.
+ */
+
+static int unet_devioctl(struct inode *ino,
+ struct file *f,
+ unsigned int c,
+ unsigned long arg)
+{
+ struct unet *u = f->private_data;
+ int e = 0;
+
+ DIF(u, printk(KERN_DEBUG "unet: ioctl request for %s\n", u->name); )
+
+ switch (c) {
+
+ /* --- @FIONREAD@ --- *
+ *
+ * Caller wants to know how big the next packet will be. Don't
+ * disappoint.
+ */
+
+ case FIONREAD: {
+ struct sk_buff *skb = skb_peek(&u->skbq);
+
+ DIF(u, printk(KERN_DEBUG "unet: FIONREAD found %d bytes\n",
+ skb ? skb->len : 0); )
+
+ if (skb)
+ e = skb->len;
+ } break;
+
+ /* --- @UNIOCGINFO@ --- *
+ *
+ * Caller wants information about me.
+ */
+
+ case UNIOCGINFO: {
+ struct unet_info uni, *unip;
+
+ DIF(u, printk(KERN_DEBUG "unet: UNIOCGINFO called\n"); )
+
+ unip = (struct unet_info *)arg;
+
+ /* --- Special case --- *
+ *
+ * If the pointer is null, this call means `are you there?' and can
+ * be used to check for a Usernet attachment.
+ */
+
+ if (!unip)
+ break;
+
+ /* --- Ensure that the area can be written to --- */
+
+ if ((e = verify_area(VERIFY_WRITE, unip, sizeof(*unip))) != 0)
+ return (e);
+
+ /* --- Build the information block in memory --- */
+
+ memset(&uni, 0, sizeof(uni)); /* Paranoia */
+ strncpy(uni.uni_ifname, u->name, UNET_NAMEMAX);
+ uni.uni_mtu = u->nif.mtu;
+ uni.uni_family = AF_INET;
+ uni.uni_proto = ntohs(u->protocol);
+ uni.uni_flags = u->f;
+
+ /* --- Copy it to the user and return --- */
+
+ copy_to_user(unip, &uni, sizeof(uni));
+ } break;
+
+ /* --- @UNIOCSDEBUG@ --- */
+
+#if UNET_DEBUG != UNET_DEBUGNEVER
+ case UNIOCSDEBUG: {
+ int n = !!arg;
+ int o = !!(u->f & UNIF_DEBUG);
+
+ if (n || o) {
+ printk(KERN_DEBUG "unet: UNIOCSDEBUG on %s: %s\n", u->name,
+ (o && n) ? "debugging still on" :
+ (!o && n) ? "debugging turned on" :
+ (o && !n) ? "debugging turned off" :
+ (!o && !n) ? "you can't see this message" :
+ "Logic failure: universe exploding");
+ }
+ if (n)
+ u->f |= UNIF_DEBUG;
+ else
+ u->f &= ~UNIF_DEBUG;
+ } break;
+#endif
+
+ /* --- @UNIOCGPROTO@ and @UNIOCSPROTO@ --- */
+
+ case UNIOCGPROTO:
+ D( printk(KERN_DEBUG "unet: UNIOCGPROTO on %s: read protocol: %d\n",
+ u->name, ntohs(u->protocol)); )
+ e = ntohs(u->protocol);
+ break;
+ case UNIOCSPROTO:
+ D( printk(KERN_DEBUG "unet: UNIOCSPROTO on %s: set protocol: %d\n",
+ u->name, (int)arg); )
+ u->protocol = htons(arg);
+ break;
+
+ /* --- @UNIOCGGDEBUG@ and @UNIOCSGDEBUG@ --- */
+
+ case UNIOCGGDEBUG:
+ D( printk(KERN_DEBUG "unet: UNIOCGGDEBUG: get global debug: on\n"); )
+#if UNET_DEBUG == UNET_DEBUGRUNTIME
+ e = unet_debug;
+#elif UNET_DEBUG == UNET_DEBUGALWAYS
+ e = 1;
+#elif UNET_DEBUG == UNET_DEBUGNEVER
+ e = 0;
+#endif
+ break;
+
+#if UNET_DEBUG == UNET_DEBUGRUNTIME
+ case UNIOCSGDEBUG:
+ printk(KERN_DEBUG "unet: UNIOCSGDEBUG: set global debug: %s\n",
+ (arg && unet_debug) ? "debugging still on" :
+ (!arg && unet_debug) ? "debugging turned off" :
+ (arg && !unet_debug) ? "debugging turned on" :
+ (!arg && !unet_debug) ? "you can't see this message" :
+ "Logic failure: universe exploding");
+ unet_debug = !!arg;
+ break;
+#endif
+
+ /* --- @UNIOCDUMP@ --- */
+
+#if UNET_DEBUG != UNET_DEBUGNEVER
+ case UNIOCDUMP:
+ D( unet_dumpBlock(u); )
+ break;
+#endif
+
+ /* --- @UNIOCGMAXIF@ --- */
+
+ case UNIOCGMAXIF:
+ D( printk(KERN_DEBUG "unet: UNIOCGMAXIF: unet_maxif = %d\n",
+ unet_maxif); )
+ e = unet_maxif;
+ break;
+
+ /* --- @UNIOCSMAXIF@ --- */
+
+ case UNIOCSMAXIF:
+ e = -EINVAL;
+ if (arg < unet_npersist || arg > INT_MAX)
+ return (-EINVAL);
+ for (u = unet_list; u; u = u->next) {
+ if (u->seq >= unet_npersist)
+ return (-EBUSY);
+ }
+ unet_maxif = arg;
+ D( printk(KERN_DEBUG "unet: UNIOCSMAXIF: unet_maxif = %d\n",
+ unet_maxif); )
+ break;
+
+ /* --- Everything else --- *
+ *
+ * You lose.
+ */
+
+ default:
+ D( printk(KERN_DEBUG "unet: unknown ioctl %08x\n", c); )
+ e = -EINVAL;
+ }
+
+ return (e);
+}
+
+/*----- The unet device ---------------------------------------------------*/
+
+/* --- Device operations --- */
+
+struct file_operations unet_fops = {
+ 0, /* unet_devlseek */
+ unet_devread,
+ unet_devwrite,
+ 0, /* unet_devreaddir */
+ unet_devpoll,
+ unet_devioctl,
+ 0, /* unet_devmmap */
+ unet_devopen,
+ 0, /* unet_flush */
+ unet_devclose
+};
+
+/*----- Initaliseation and shutdown ---------------------------------------*/
+
+/* --- @unet_init@ --- *
+ *
+ * Arguments: ---
+ *
+ * Returns: Zero or error condition.
+ *
+ * Use: Registers the unet device.
+ */
+
+__initfunc(int unet_init(void))
+{
+ int e = 0;
+ int i = 0;
+
+ /* --- Register my character device --- */
+
+ if ((e = register_chrdev(UNET_MAJOR, "unet", &unet_fops)) != 0) {
+ printk(KERN_ERR "unet: can't claim major device %d\n", UNET_MAJOR);
+ goto tidy_0;
+ }
+
+ /* --- Make the persistent unet devices --- */
+
+ if ((unet_persistent = kmalloc(unet_npersist * sizeof(struct unet),
+ GFP_KERNEL)) == 0) {
+ printk(KERN_ERR "unet: can't allocate %i persistent unet blocks",
+ unet_npersist);
+ e = -ENOMEM;
+ goto tidy_1;
+ }
+
+ for (i = 0; i < unet_npersist; i++) {
+ if ((e = unet_setup(&unet_persistent[i], i)) != 0) {
+ printk(KERN_ERR "unet: can't create persistent unet %d\n", i);
+ goto tidy_2;
+ }
+ }
+
+ /* --- Done --- */
+
+ D( unet_dump(); )
+ return (0);
+
+ /* --- Some bad stuff happened --- */
+
+tidy_2:
+ while (i > 0) {
+ i--;
+ unregister_netdev(&unet_persistent[i].nif);
+ }
+ kfree(unet_persistent);
+
+tidy_1:
+ unregister_chrdev(UNET_MAJOR, "unet");
+
+tidy_0:
+ return (e);
+}
+
+#ifdef MODULE
+
+/* --- @init_module@ --- *
+ *
+ * Arguments: ---
+ *
+ * Returns: Zero or error condition.
+ *
+ * Use: Initialises the unet kernel module.
+ */
+
+int init_module(void)
+{
+ if (unet_npersist < 0 || unet_maxif < unet_npersist
+#if UNET_DEBUG == UNET_DEBUGRUNTIME
+ || (unet_debug != 0 && unet_debug != 1)
+#endif
+ )
+ return (-EINVAL);
+ return (unet_init());
+}
+
+/* --- @cleanup_module@ --- *
+ *
+ * Arguments: ---
+ *
+ * Returns: ---
+ *
+ * Use: Tidies up the unet module ready for it to be killed.
+ */
+
+void cleanup_module(void)
+{
+ int i;
+
+ for (i = 0; i < unet_npersist; i++) {
+ D( printk(KERN_DEBUG "unet: releasing persistent unet %d\n", i); )
+ unet_kill(&unet_persistent[i]);
+ }
+ kfree(unet_persistent);
+
+ unregister_chrdev(UNET_MAJOR, "unet");
+}
+
+#endif
+
+/*----- That's all, folks -------------------------------------------------*/