3 * $Id: unet.c,v 1.2 2001/02/03 18:39:59 mdw Exp $
5 * User-space network device support.
7 * (c) 1998 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Usernet.
14 * Usernet is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * Usernet is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Usernet; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.2 2001/02/03 18:39:59 mdw
33 * Setting the maximum interface count now does the right thing.
35 * Revision 1.1 2001/01/25 22:03:39 mdw
36 * Initial check-in (somewhat belated).
40 /*----- Include files -----------------------------------------------------*/
42 #include <linux/module.h>
44 #include <asm/byteorder.h>
45 #include <asm/segment.h>
47 #include <linux/types.h>
48 #include <linux/kernel.h>
49 #include <linux/wait.h>
50 #include <linux/sched.h>
52 #include <linux/poll.h>
53 #include <linux/netdevice.h>
54 #include <linux/etherdevice.h>
55 #include <linux/if_arp.h>
56 #include <linux/malloc.h>
57 #include <linux/errno.h>
58 #include <linux/init.h>
63 MODULE_AUTHOR("Mark Wooding");
64 MODULE_DESCRIPTION("Allows userland handling of a network interface");
66 /*----- Debugging macros --------------------------------------------------*/
68 #define UNET_DEBUGALWAYS 1
69 #define UNET_DEBUGRUNTIME 0
70 #define UNET_DEBUGNEVER -1
72 /* --- If the macro isn't defined then be switchable at runtime --- */
75 # define UNET_DEBUG UNET_DEBUGRUNTIME
78 /* --- Define the base macro @D@ according to the debug setting --- */
80 #if UNET_DEBUG == UNET_DEBUGALWAYS
82 # define DIF(u, x) if ((u)->f & UNIF_DEBUG) { x }
83 #elif UNET_DEBUG == UNET_DEBUGRUNTIME
84 # define D(x) if (unet_debug) { x }
85 # define DIF(u, x) if ((u)->f & UNIF_DEBUG) { x }
86 #elif UNET_DEBUG == UNET_DEBUGNEVER
90 # error UNET_DEBUG set to invalid value (bug in configure script?)
93 /*----- Type definitions --------------------------------------------------*/
95 /* --- Unet connection status --- *
97 * Records are stored in a slightly strange doubly linked list. The list
98 * exists so that I can find the next unused sequence number when creating
99 * new connections. It's odd because of the type of the @prev@ node;
100 * rather than being the address of the previous item, it's the address of
101 * the previous item's pointer to me, which among other good things means
102 * that it works on the list head too.
106 struct unet
*next
, **prev
; /* List of unet blocks */
107 struct device nif
; /* Network interface block */
108 int seq
; /* Sequence number for connection */
109 char name
[UNET_NAMEMAX
]; /* Buffer for my interface name */
110 struct wait_queue
*q
; /* Wait list for device reads */
111 struct sk_buff_head skbq
; /* Queue of packets waiting */
112 struct enet_statistics e
; /* Pointer to statistics block */
113 unsigned short protocol
; /* Protocol for outgoing packets */
114 unsigned f
; /* Userful flags */
117 /*----- Static variables --------------------------------------------------*/
119 #if UNET_DEBUG == UNET_DEBUGRUNTIME
120 static int unet_debug
= 0;
121 MODULE_PARM(unet_debug
, "i");
124 static int unet_npersist
= UNET_NPERSIST
;
125 static int unet_maxif
= UNET_MAXIF
;
126 static struct unet
*unet_persistent
;
127 static struct unet
*unet_list
= 0;
128 static int unet_nif
= 0;
130 MODULE_PARM(unet_npersist
, "i");
131 MODULE_PARM(unet_maxif
, "i");
133 /*----- Debugging code ----------------------------------------------------*/
135 #if UNET_DEBUG != UNET_DEBUGNEVER
137 /* --- @unet_dumpBlock@ --- *
139 * Arguments: @struct unet *u@ = pointer to block to dump
143 * Use: Dumps a unet object to syslogd.
146 static void unet_dumpBlock(struct unet
*u
)
148 printk(KERN_DEBUG
"unet: dumping unet block at %p\n", u
);
149 printk(KERN_DEBUG
" sequence number = %d\n", u
->seq
);
150 printk(KERN_DEBUG
" interface name = `%s'\n", u
->name
);
151 printk(KERN_DEBUG
" flags =%s%s%s\n",
152 u
->f
& UNIF_TRANS ?
" TRANS" : "",
153 u
->f
& UNIF_OPEN ?
" OPEN" : "",
154 u
->f
& UNIF_DEBUG ?
" DEBUG" : "");
155 printk(KERN_DEBUG
" interface type = %d\n", u
->nif
.type
);
156 printk(KERN_DEBUG
" header len = %d\n", u
->nif
.hard_header_len
);
157 printk(KERN_DEBUG
" mtu = %d\n", u
->nif
.mtu
);
158 printk(KERN_DEBUG
" protocol = %d\n", ntohs(u
->protocol
));
159 printk(KERN_DEBUG
" address len = %d\n", u
->nif
.addr_len
);
162 /* --- @unet_dump@ --- *
168 * Use: Dumps the entire unet state to syslogd.
171 static void unet_dump(void)
176 for (i
= 0; i
< unet_npersist
; i
++)
177 unet_dumpBlock(&unet_persistent
[i
]);
178 for (u
= unet_list
; u
; u
= u
->next
)
182 /* --- @unet_hexdump@ --- *
184 * Arguments: @const char *prefix@ = prefix to print on output lines
185 * @const void *b@ = pointer to block to dump
186 * @size_t sz@ = size of block to dump
190 * Use: Dumps a hex block to the kernel log.
193 #define UNET_HEXROWSZ 16
195 static void unet_hexdump(const char *prefix
, const char *b
, size_t sz
)
197 const unsigned char *p
= b
;
204 /* --- Now start work --- */
208 q
+= sprintf(q
, "%s%08lx : ", prefix
, o
);
209 for (i
= 0; i
< UNET_HEXROWSZ
; i
++) {
211 q
+= sprintf(q
, "%02x ", p
[i
]);
213 q
+= sprintf(q
, "** ");
215 *q
++ = ':'; *q
++ = ' ';
216 for (i
= 0; i
< UNET_HEXROWSZ
; i
++) {
218 *q
++ = (p
[i
] >= 32 && p
[i
] < 127) ? p
[i
] : '.';
225 c
= (sz
>= UNET_HEXROWSZ
) ? UNET_HEXROWSZ
: sz
;
226 sz
-= c
, p
+= c
, o
+= c
;
232 /*----- The unet network interfaces ---------------------------------------*/
234 /* --- @unet_ifopen@ --- *
236 * Arguments: @struct device *nif@ = pointer to network interface
238 * Returns: Zero or error condition.
240 * Use: Turns on the network interface ready for action. Oh, yes.
243 static int unet_ifopen(struct device
*nif
)
245 D( struct unet
*u
= nif
->priv
;
246 if (u
->f
& UNIF_DEBUG
)
247 printk(KERN_DEBUG
"unet: opening interface %s\n", u
->name
); )
252 /* --- @unet_ifclose@ --- *
254 * Arguments: @struct device *nif@ = pointer to network interface
256 * Returns: Zero or error condition.
258 * Use: Turns off the network interface.
261 static int unet_ifclose(struct device
*nif
)
263 D( struct unet
*u
= nif
->priv
;
264 if (u
->f
& UNIF_DEBUG
)
265 printk(KERN_DEBUG
"unet: closing interface %d\n", u
->seq
); )
270 /* --- @unet_iftx@ --- *
272 * Arguments: @struct sk_buff *skb@ = incoming network packet
273 * @struct device *nif@ = pointer to network interface
275 * Returns: Zero or error condition.
277 * Use: Queues a network packet ready for collection by the user
278 * end of the business.
281 static int unet_iftx(struct sk_buff
*skb
, struct device
*nif
)
283 struct unet
*u
= nif
->priv
;
287 printk(KERN_DEBUG
"unet: packet received on if %s\n", u
->name
);
288 unet_hexdump(KERN_DEBUG
" ", skb
->data
, skb
->len
); )
290 /* --- Discard packets when nobody's listening --- */
292 if ((u
->f
& UNIF_OPEN
) == 0) {
293 DIF(u
, printk(KERN_DEBUG
"unet: dropped packet: nobody's listening\n"); )
299 /* --- Discard packets when the queue's too long --- */
301 if (u
->skbq
.qlen
>= UNET_QMAXLEN
) {
302 DIF(u
, printk(KERN_DEBUG
"unet: refused packet: queue overflow\n"); )
306 /* --- Attach the buffer to the waiting list --- */
309 skb_queue_tail(&u
->skbq
, skb
);
311 DIF(u
, printk(KERN_DEBUG
"unet: queued packet OK\n"); )
313 /* --- If there are waiting processes, give 'em a kick --- */
316 DIF(u
, printk(KERN_DEBUG
"unet: waking up sleeping listeners\n"); )
317 wake_up_interruptible(&u
->q
);
322 /* --- @unet_ifgetstats@ --- *
324 * Arguments: @struct device *nif@ = pointer to network interface
326 * Returns: Pointer to a statistics buffer.
328 * Use: Returns a block of interface statistics.
331 static struct enet_statistics
*unet_ifgetstats(struct device
*nif
)
333 struct unet
*u
= nif
->priv
;
334 DIF(u
, printk(KERN_DEBUG
"unet: stats request for if %s\n", u
->name
); )
338 /* --- @unet_ifinit@ --- *
340 * Arguments: @struct device *nif@ = pointer to network interface
342 * Returns: Zero or error condition.
344 * Use: Initialises a unet network interface.
347 static int unet_ifinit(struct device
*nif
)
349 struct unet
*u
= nif
->priv
;
351 DIF(u
, printk(KERN_DEBUG
"unet: initialise interface %s\n", u
->name
); )
353 /* --- Initialise statistics gathering --- */
355 memset(&u
->e
, 0, sizeof(u
->e
));
356 u
->nif
.get_stats
= unet_ifgetstats
;
358 /* --- Opening and closing interfaces --- */
360 u
->nif
.open
= unet_ifopen
;
361 u
->nif
.stop
= unet_ifclose
;
363 /* --- Sending packets to the `outside world' --- */
365 u
->nif
.hard_start_xmit
= unet_iftx
;
367 /* --- Some initialisation magic --- */
370 for (i
= 0; i
< DEV_NUMBUFFS
; i
++)
371 skb_queue_head_init(&u
->nif
.buffs
[i
]);
374 /* --- Configure other grotty bits of the interface --- */
376 u
->nif
.hard_header
= 0;
377 u
->nif
.rebuild_header
= 0;
378 u
->nif
.set_mac_address
= 0;
380 u
->nif
.type
= ARPHRD_LOOPBACK
; /* Got a better idea? */
381 u
->nif
.hard_header_len
= 0;
382 u
->nif
.mtu
= 1500 - MAX_HEADER
;
384 u
->nif
.tx_queue_len
= 16; /* I keep my own queue */
386 memset(u
->nif
.broadcast
, 0xFFu
, MAX_ADDR_LEN
);
388 u
->nif
.flags
= IFF_NOARP
;
390 /* --- Finished! --- */
395 /*----- Attachment management ---------------------------------------------*/
397 /* --- @unet_setup@ --- *
399 * Arguments: @struct unet *u@ = pointer to a unet block
400 * @int seq@ = sequence number to allocate
402 * Returns: Zero or error condition.
404 * Use: Initialises a unet block ready for action.
407 static int unet_setup(struct unet
*u
, int seq
)
409 static struct device tpl
;
412 D( printk(KERN_DEBUG
"unet: setting up unet block %d\n", seq
); )
414 /* --- A little bit of initialisation --- */
420 /* --- Inherit device debug flag from global flag --- */
422 #if UNET_DEBUG == UNET_DEBUGRUNTIME
425 #elif UNET_DEBUG == UNET_DEBUGALWAYS
429 /* --- Set up the network device --- */
432 sprintf(u
->name
, "unet%d", seq
);
433 u
->nif
.name
= u
->name
;
435 u
->nif
.init
= unet_ifinit
;
436 u
->protocol
= htons(ETH_P_IP
);
438 if ((e
= register_netdev(&u
->nif
)) != 0) {
439 printk(KERN_ERR
"unet: couldn't register net interface\n");
443 /* --- Empty the skbuff list --- */
445 skb_queue_head_init(&u
->skbq
);
447 /* --- We're only finished, that's all --- */
452 /* --- @unet_flush@ --- *
454 * Arguments: @struct unet *u@ = pointer to a unet block
458 * Use: Releases all the packets waiting for transmission.
461 static void unet_flush(struct unet
*u
)
465 #if UNET_DEBUG != UNET_DEBUGNEVER
469 DIF(u
, printk(KERN_DEBUG
"unet: flushing packets on %s\n", u
->name
); )
471 while ((skb
= skb_dequeue(&u
->skbq
)) != 0) {
475 DIF(u
, printk(KERN_DEBUG
"unet: released %d waiting packets\n", i
); )
478 /* --- @unet_kill@ --- *
480 * Arguments: @struct unet *u@ = pointer to a unet block
484 * Use: Kills and decommissions a Usernet block.
487 static void unet_kill(struct unet
*u
)
490 unregister_netdev(&u
->nif
);
493 /*----- Handling the unet device ------------------------------------------*/
495 /* --- @unet_devopen@ --- *
497 * Arguments: @struct inode *ino@ = inode block to open
498 * @struct file *f@ = file block to play with
500 * Returns: Zero or error condition.
502 * Use: Handles the opening of a unet device.
505 static int unet_devopen(struct inode
*ino
, struct file
*f
)
507 struct unet
*u
, **up
;
511 D( printk(KERN_DEBUG
"unet: device open request\n"); )
513 /* --- Decide whether this is a persistent unet --- */
515 if ((seq
= MINOR(ino
->i_rdev
)) < unet_npersist
) {
516 u
= &unet_persistent
[seq
];
517 if (u
->f
& UNIF_OPEN
) {
521 D( printk(KERN_DEBUG
"unet: opened persistent %s\n", u
->name
); )
524 /* --- Otherwise we've got to create a new one --- */
526 else if (seq
== UNET_TRANSMINOR
) {
528 /* --- Try to find a spare sequence number --- */
530 if (unet_nif
>= unet_maxif
) {
531 printk(KERN_NOTICE
"unet: no interfaces left\n");
535 for (seq
= unet_npersist
, up
= &unet_list
; *up
!= 0;
536 seq
++, up
= &(*up
)->next
) {
537 if ((*up
)->seq
> seq
)
540 D( printk(KERN_DEBUG
"unet: allocated sequence number %d\n", seq
); )
542 /* --- Allocate a new block --- */
544 if ((u
= kmalloc(sizeof(*u
), GFP_KERNEL
)) == 0) {
545 printk(KERN_ERR
"unet: couldn't allocate a unet block\n");
550 /* --- Initialise the block --- */
552 if ((e
= unet_setup(u
, seq
)) != 0)
557 /* --- Link the block into the list --- */
562 (*up
)->prev
= &u
->next
;
565 D( printk(KERN_DEBUG
"unet: opened transient %d\n", seq
);
577 /* --- Tidy up after little disasters --- */
585 /* --- @unet_devclose@ --- *
587 * Arguments: @struct inode *ino@ = pointer to inode block
588 * @struct file *f@ = pointer to file block
592 * Use: Frees up a unet connection.
595 static int unet_devclose(struct inode
*ino
, struct file
*f
)
597 struct unet
*u
= f
->private_data
;
599 DIF(u
, printk(KERN_DEBUG
"unet: closing %s\n", u
->name
); )
601 /* --- A transient unet needs to be destroyed --- */
603 if (u
->f
& UNIF_TRANS
) {
607 u
->next
->prev
= u
->prev
;
611 D( printk(KERN_DEBUG
"unet: released transient %d\n", seq
); )
614 /* --- A persistent unet needs to be shutdown --- */
619 DIF(u
, printk(KERN_DEBUG
"unet: unblocked persistent unet\n"); )
626 /* --- @unet_devpoll@ --- *
628 * Arguments: @struct file *f@ = pointer to my file block
629 * @struct poll_table_struct *p@ = poll table to wait on
631 * Returns: Nonzero if OK to continue, zero if waiting.
633 * Use: Plays a unet device's part in a @poll@(2) call.
636 static unsigned unet_devpoll(struct file
*f
, struct poll_table_struct
*p
)
638 struct unet
*u
= f
->private_data
;
641 DIF(u
, printk(KERN_DEBUG
"unet: poll request for %s\n", u
->name
); )
643 poll_wait(f
, &u
->q
, p
);
644 if (skb_peek(&u
->skbq
))
645 m
|= POLLIN
| POLLRDNORM
;
646 m
|= POLLOUT
| POLLWRNORM
;
650 /* --- @unet_devwrite@ --- *
652 * Arguments: @struct file *f@ = pointer to the file block
653 * @char *buf@ = pointer to caller's buffer
654 * @size_t sz@ = size of data to send
655 * @loff_t *off@ = offset to set (ignored)
657 * Returns: Number of bytes written, or error condition.
659 * Use: Sends a packet of data. No buffering is done.
662 static ssize_t
unet_devwrite(struct file
*f
, const char *buf
, size_t sz
,
665 struct unet
*u
= f
->private_data
;
668 DIF(u
, printk(KERN_DEBUG
"unet: write request for %s\n", u
->name
); )
670 /* --- Dump the packet we're meant to send --- */
673 if (u
->f
& UNIF_DEBUG
) {
674 void *b
= kmalloc(sz
, GFP_KERNEL
);
676 copy_from_user(b
, buf
, sz
);
677 unet_hexdump(KERN_DEBUG
" ", b
, sz
);
680 printk(KERN_NOTICE
"unet: not enough memory to dump block");
684 /* --- Allocate an skbuff for the block --- */
686 if ((skb
= dev_alloc_skb(sz
)) == 0) {
687 printk(KERN_ERR
"unet: failed to allocate skbuff\n");
692 /* --- Copy my data into the skbuff --- */
694 copy_from_user(skb_put(skb
, sz
), buf
, sz
);
696 skb
->mac
.raw
= skb
->data
;
697 skb
->protocol
= u
->protocol
;
703 /* --- @unet_devread@ --- *
705 * Arguments: @struct file *f@ = pointer to the file block
706 * @char *buf@ = pointer to caller's buffer
707 * @size_t sz@ = size of caller's buffer
708 * @loff_t *off@ = offset to set (ignored)
710 * Returns: Number of bytes read, or error condition.
712 * Use: Reads the next packet waiting for the device.
715 static ssize_t
unet_devread(struct file
*f
, char *buf
, size_t sz
,
718 struct unet
*u
= f
->private_data
;
721 DIF(u
, printk(KERN_DEBUG
"unet: read request for %s\n", u
->name
); )
723 /* --- Is the user sane? --- *
725 * The UDP protocol returns immediately in response to a zero-length read,
726 * and following this behaviour seems to cause `least surprise'.
732 /* --- Make sure there's a packet waiting for me --- */
734 if ((skb
= skb_dequeue(&u
->skbq
)) == 0) {
735 struct wait_queue wq
= { current
, 0 };
737 DIF(u
, printk(KERN_DEBUG
"unet: no packets waiting\n"); )
739 /* --- Check for nonblocking I/O --- */
741 if (f
->f_flags
& O_NONBLOCK
) {
742 DIF(u
, printk(KERN_DEBUG
"unet: nonblocking read: fail\n"); )
743 return (-EWOULDBLOCK
);
746 /* --- Otherwise block until there's a packet --- */
748 current
->state
= TASK_INTERRUPTIBLE
;
749 add_wait_queue(&u
->q
, &wq
);
753 if (signal_pending(current
)) {
755 DIF(u
, printk(KERN_DEBUG
"unet: interrupted by signal\n"); )
757 remove_wait_queue(&u
->q
, &wq
);
758 current
->state
= TASK_RUNNING
;
759 return (-ERESTARTSYS
);
762 DIF(u
, printk(KERN_DEBUG
"unet: blocking until packet arrives\n"); )
766 } while ((skb
= skb_dequeue(&u
->skbq
)) == 0);
768 remove_wait_queue(&u
->q
, &wq
);
769 current
->state
= TASK_RUNNING
;
772 DIF(u
, printk(KERN_DEBUG
"unet: found a packet\n"); )
774 /* --- There is now a packet waiting --- */
778 copy_to_user(buf
, skb
->data
, sz
);
781 DIF(u
, printk(KERN_DEBUG
"unet: passed packet on to user\n"); )
786 /* --- @unet_devioctl@ --- *
788 * Arguments: @struct inode *ino@ = pointer to inode block
789 * @struct file *f@ = pointer to file block
790 * @unsigned int c@ = command code to execute
791 * @unsigned long arg@ = argument passed to me
793 * Returns: Positive return value, or negative error condition.
795 * Use: Performs miscellaneous operations on a unet device.
798 static int unet_devioctl(struct inode
*ino
,
803 struct unet
*u
= f
->private_data
;
806 DIF(u
, printk(KERN_DEBUG
"unet: ioctl request for %s\n", u
->name
); )
810 /* --- @FIONREAD@ --- *
812 * Caller wants to know how big the next packet will be. Don't
817 struct sk_buff
*skb
= skb_peek(&u
->skbq
);
819 DIF(u
, printk(KERN_DEBUG
"unet: FIONREAD found %d bytes\n",
820 skb ? skb
->len
: 0); )
826 /* --- @UNIOCGINFO@ --- *
828 * Caller wants information about me.
832 struct unet_info uni
, *unip
;
834 DIF(u
, printk(KERN_DEBUG
"unet: UNIOCGINFO called\n"); )
836 unip
= (struct unet_info
*)arg
;
838 /* --- Special case --- *
840 * If the pointer is null, this call means `are you there?' and can
841 * be used to check for a Usernet attachment.
847 /* --- Ensure that the area can be written to --- */
849 if ((e
= verify_area(VERIFY_WRITE
, unip
, sizeof(*unip
))) != 0)
852 /* --- Build the information block in memory --- */
854 memset(&uni
, 0, sizeof(uni
)); /* Paranoia */
855 strncpy(uni
.uni_ifname
, u
->name
, UNET_NAMEMAX
);
856 uni
.uni_mtu
= u
->nif
.mtu
;
857 uni
.uni_family
= AF_INET
;
858 uni
.uni_proto
= ntohs(u
->protocol
);
859 uni
.uni_flags
= u
->f
;
861 /* --- Copy it to the user and return --- */
863 copy_to_user(unip
, &uni
, sizeof(uni
));
866 /* --- @UNIOCSDEBUG@ --- */
868 #if UNET_DEBUG != UNET_DEBUGNEVER
871 int o
= !!(u
->f
& UNIF_DEBUG
);
873 if (!capable(CAP_SYS_ADMIN
))
876 printk(KERN_DEBUG
"unet: UNIOCSDEBUG on %s: %s\n", u
->name
,
877 (o
&& n
) ?
"debugging still on" :
878 (!o
&& n
) ?
"debugging turned on" :
879 (o
&& !n
) ?
"debugging turned off" :
880 (!o
&& !n
) ?
"you can't see this message" :
881 "Logic failure: universe exploding");
890 /* --- @UNIOCGPROTO@ and @UNIOCSPROTO@ --- */
893 D( printk(KERN_DEBUG
"unet: UNIOCGPROTO on %s: read protocol: %d\n",
894 u
->name
, ntohs(u
->protocol
)); )
895 e
= ntohs(u
->protocol
);
898 D( printk(KERN_DEBUG
"unet: UNIOCSPROTO on %s: set protocol: %d\n",
899 u
->name
, (int)arg
); )
900 u
->protocol
= htons(arg
);
903 /* --- @UNIOCGGDEBUG@ and @UNIOCSGDEBUG@ --- */
906 D( printk(KERN_DEBUG
"unet: UNIOCGGDEBUG: get global debug: on\n"); )
907 #if UNET_DEBUG == UNET_DEBUGRUNTIME
909 #elif UNET_DEBUG == UNET_DEBUGALWAYS
911 #elif UNET_DEBUG == UNET_DEBUGNEVER
916 #if UNET_DEBUG == UNET_DEBUGRUNTIME
918 if (!capable(CAP_SYS_ADMIN
))
920 if (arg
|| unet_debug
) {
921 printk(KERN_DEBUG
"unet: UNIOCSGDEBUG: set global debug: %s\n",
922 (arg
&& unet_debug
) ?
"debugging still on" :
923 (!arg
&& unet_debug
) ?
"debugging turned off" :
924 (arg
&& !unet_debug
) ?
"debugging turned on" :
925 (!arg
&& !unet_debug
) ?
"you can't see this message" :
926 "Logic failure: universe exploding");
932 /* --- @UNIOCDUMP@ --- */
934 #if UNET_DEBUG != UNET_DEBUGNEVER
936 D( unet_dumpBlock(u
); )
940 /* --- @UNIOCGMAXIF@ --- */
943 D( printk(KERN_DEBUG
"unet: UNIOCGMAXIF: unet_maxif = %d\n",
948 /* --- @UNIOCSMAXIF@ --- */
951 if (!capable(CAP_SYS_ADMIN
))
953 D( printk(KERN_DEBUG
"unet: UNIOCSMAXIF: "
954 "arg = %ld; npersist = %d; nif = %d\n",
955 arg
, unet_npersist
, unet_nif
); )
956 if (arg
< unet_npersist
|| arg
> INT_MAX
)
962 D( printk(KERN_DEBUG
"unet: UNIOCSMAXIF: set unet_maxif = %d\n",
966 /* --- Everything else --- *
972 D( printk(KERN_DEBUG
"unet: unknown ioctl %08x\n", c
); )
979 /*----- The unet device ---------------------------------------------------*/
981 /* --- Device operations --- */
983 struct file_operations unet_fops
= {
984 0, /* unet_devlseek */
987 0, /* unet_devreaddir */
990 0, /* unet_devmmap */
996 /*----- Initaliseation and shutdown ---------------------------------------*/
998 /* --- @unet_init@ --- *
1002 * Returns: Zero or error condition.
1004 * Use: Registers the unet device.
1007 __initfunc(int unet_init(void))
1012 /* --- Register my character device --- */
1014 if ((e
= register_chrdev(UNET_MAJOR
, "unet", &unet_fops
)) != 0) {
1015 printk(KERN_ERR
"unet: can't claim major device %d\n", UNET_MAJOR
);
1019 /* --- Make the persistent unet devices --- */
1021 if ((unet_persistent
= kmalloc(unet_npersist
* sizeof(struct unet
),
1022 GFP_KERNEL
)) == 0) {
1023 printk(KERN_ERR
"unet: can't allocate %i persistent unet blocks",
1029 for (i
= 0; i
< unet_npersist
; i
++) {
1030 if ((e
= unet_setup(&unet_persistent
[i
], i
)) != 0) {
1031 printk(KERN_ERR
"unet: can't create persistent unet %d\n", i
);
1035 unet_nif
= unet_npersist
;
1042 /* --- Some bad stuff happened --- */
1047 unregister_netdev(&unet_persistent
[i
].nif
);
1049 kfree(unet_persistent
);
1052 unregister_chrdev(UNET_MAJOR
, "unet");
1060 /* --- @init_module@ --- *
1064 * Returns: Zero or error condition.
1066 * Use: Initialises the unet kernel module.
1069 int init_module(void)
1071 if (unet_npersist
< 0 || unet_maxif
< unet_npersist
1072 #if UNET_DEBUG == UNET_DEBUGRUNTIME
1073 || (unet_debug
!= 0 && unet_debug
!= 1)
1077 return (unet_init());
1080 /* --- @cleanup_module@ --- *
1086 * Use: Tidies up the unet module ready for it to be killed.
1089 void cleanup_module(void)
1093 for (i
= 0; i
< unet_npersist
; i
++) {
1094 D( printk(KERN_DEBUG
"unet: releasing persistent unet %d\n", i
); )
1095 unet_kill(&unet_persistent
[i
]);
1097 kfree(unet_persistent
);
1099 unregister_chrdev(UNET_MAJOR
, "unet");
1104 /*----- That's all, folks -------------------------------------------------*/