chiark / gitweb /
7fdc317818f2b511c6f3c0afd31882032d1f9968
[unet] / unet.c
1 /* -*-c-*-
2  *
3  * $Id: unet.c,v 1.2 2001/02/03 18:39:59 mdw Exp $
4  *
5  * User-space network device support.
6  *
7  * (c) 1998 Mark Wooding
8  */
9
10 /*----- Licensing notice --------------------------------------------------*
11  *
12  * This file is part of Usernet.
13  *
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.
18  *
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.
23  *
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.
27  */
28
29 /*----- Revision history --------------------------------------------------*
30  *
31  * $Log: unet.c,v $
32  * Revision 1.2  2001/02/03 18:39:59  mdw
33  * Setting the maximum interface count now does the right thing.
34  *
35  * Revision 1.1  2001/01/25 22:03:39  mdw
36  * Initial check-in (somewhat belated).
37  *
38  */
39
40 /*----- Include files -----------------------------------------------------*/
41
42 #include <linux/module.h>
43
44 #include <asm/byteorder.h>
45 #include <asm/segment.h>
46
47 #include <linux/types.h>
48 #include <linux/kernel.h>
49 #include <linux/wait.h>
50 #include <linux/sched.h>
51 #include <linux/fs.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>
59
60 #include "unetconf.h"
61 #include "unet.h"
62
63 MODULE_AUTHOR("Mark Wooding");
64 MODULE_DESCRIPTION("Allows userland handling of a network interface");
65
66 /*----- Debugging macros --------------------------------------------------*/
67
68 #define UNET_DEBUGALWAYS 1
69 #define UNET_DEBUGRUNTIME 0
70 #define UNET_DEBUGNEVER -1
71
72 /* --- If the macro isn't defined then be switchable at runtime --- */
73
74 #ifndef UNET_DEBUG
75 #  define UNET_DEBUG UNET_DEBUGRUNTIME
76 #endif
77
78 /* --- Define the base macro @D@ according to the debug setting --- */
79
80 #if UNET_DEBUG == UNET_DEBUGALWAYS
81 #  define D(x) { x }
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
87 #  define D(x)
88 #  define DIF(u, x)
89 #elif
90 #  error UNET_DEBUG set to invalid value (bug in configure script?)
91 #endif
92
93 /*----- Type definitions --------------------------------------------------*/
94
95 /* --- Unet connection status --- *
96  *
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.
103  */
104
105 struct unet {
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 */
115 };
116
117 /*----- Static variables --------------------------------------------------*/
118
119 #if UNET_DEBUG == UNET_DEBUGRUNTIME
120 static int unet_debug = 0;
121 MODULE_PARM(unet_debug, "i");
122 #endif
123
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;
129
130 MODULE_PARM(unet_npersist, "i");
131 MODULE_PARM(unet_maxif, "i");
132
133 /*----- Debugging code ----------------------------------------------------*/
134
135 #if UNET_DEBUG != UNET_DEBUGNEVER
136
137 /* --- @unet_dumpBlock@ --- *
138  *
139  * Arguments:   @struct unet *u@ = pointer to block to dump
140  *
141  * Returns:     ---
142  *
143  * Use:         Dumps a unet object to syslogd.
144  */
145
146 static void unet_dumpBlock(struct unet *u)
147 {
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);
160 }
161
162 /* --- @unet_dump@ --- *
163  *
164  * Arguments:   ---
165  *
166  * Returns:     ---
167  *
168  * Use:         Dumps the entire unet state to syslogd.
169  */
170
171 static void unet_dump(void)
172 {
173   int i;
174   struct unet *u;
175
176   for (i = 0; i < unet_npersist; i++)
177     unet_dumpBlock(&unet_persistent[i]);
178   for (u = unet_list; u; u = u->next)
179     unet_dumpBlock(u);
180 }
181
182 /* --- @unet_hexdump@ --- *
183  *
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
187  *
188  * Returns:     ---
189  *
190  * Use:         Dumps a hex block to the kernel log.
191  */
192
193 #define UNET_HEXROWSZ 16
194
195 static void unet_hexdump(const char *prefix, const char *b, size_t sz)
196 {
197   const unsigned char *p = b;
198   size_t i;
199   unsigned long o = 0;
200   size_t c;
201
202   char buf[256], *q;
203
204   /* --- Now start work --- */
205
206   while (sz) {
207     q = buf;
208     q += sprintf(q, "%s%08lx : ", prefix, o);
209     for (i = 0; i < UNET_HEXROWSZ; i++) {
210       if (i < sz)
211         q += sprintf(q, "%02x ", p[i]);
212       else
213         q += sprintf(q, "** ");
214     }
215     *q++ = ':'; *q++ = ' ';
216     for (i = 0; i < UNET_HEXROWSZ; i++) {
217       if (i < sz)
218         *q++ = (p[i] >= 32 && p[i] < 127) ? p[i] : '.';
219       else
220         *q++ = '*';
221     }
222     *q++ = '\n';
223     *q++ = 0;
224     printk("%s", buf);
225     c = (sz >= UNET_HEXROWSZ) ? UNET_HEXROWSZ : sz;
226     sz -= c, p += c, o += c;
227   }
228 }
229
230 #endif
231
232 /*----- The unet network interfaces ---------------------------------------*/
233
234 /* --- @unet_ifopen@ --- *
235  *
236  * Arguments:   @struct device *nif@ = pointer to network interface
237  *
238  * Returns:     Zero or error condition.
239  *
240  * Use:         Turns on the network interface ready for action.  Oh, yes.
241  */
242
243 static int unet_ifopen(struct device *nif)
244 {
245   D( struct unet *u = nif->priv;
246      if (u->f & UNIF_DEBUG)
247        printk(KERN_DEBUG "unet: opening interface %s\n", u->name); )
248   MOD_INC_USE_COUNT;
249   return (0);
250 }
251
252 /* --- @unet_ifclose@ --- *
253  *
254  * Arguments:   @struct device *nif@ = pointer to network interface
255  *
256  * Returns:     Zero or error condition.
257  *
258  * Use:         Turns off the network interface.
259  */
260
261 static int unet_ifclose(struct device *nif)
262 {
263   D( struct unet *u = nif->priv;
264      if (u->f & UNIF_DEBUG)
265        printk(KERN_DEBUG "unet: closing interface %d\n", u->seq); )
266   MOD_DEC_USE_COUNT;
267   return (0);
268 }
269
270 /* --- @unet_iftx@ --- *
271  *
272  * Arguments:   @struct sk_buff *skb@ = incoming network packet
273  *              @struct device *nif@ = pointer to network interface
274  *
275  * Returns:     Zero or error condition.
276  *
277  * Use:         Queues a network packet ready for collection by the user
278  *              end of the business.
279  */
280
281 static int unet_iftx(struct sk_buff *skb, struct device *nif)
282 {
283   struct unet *u = nif->priv;
284   int qed;
285
286   DIF(u,
287       printk(KERN_DEBUG "unet: packet received on if %s\n", u->name);
288       unet_hexdump(KERN_DEBUG "      ", skb->data, skb->len); )
289
290   /* --- Discard packets when nobody's listening --- */
291
292   if ((u->f & UNIF_OPEN) == 0) {
293     DIF(u, printk(KERN_DEBUG "unet: dropped packet: nobody's listening\n"); )
294     dev_kfree_skb(skb);
295     u->e.tx_dropped++;
296     return (0);
297   }
298
299   /* --- Discard packets when the queue's too long --- */
300
301   if (u->skbq.qlen >= UNET_QMAXLEN) {
302     DIF(u, printk(KERN_DEBUG "unet: refused packet: queue overflow\n"); )
303     return (-1);
304   }
305
306   /* --- Attach the buffer to the waiting list --- */
307
308   qed = u->skbq.qlen;
309   skb_queue_tail(&u->skbq, skb);
310   u->e.tx_packets++;
311   DIF(u, printk(KERN_DEBUG "unet: queued packet OK\n"); )
312
313   /* --- If there are waiting processes, give 'em a kick --- */
314
315   if (qed == 0) {
316     DIF(u, printk(KERN_DEBUG "unet: waking up sleeping listeners\n"); )
317     wake_up_interruptible(&u->q);
318   }
319   return (0);
320 }
321
322 /* --- @unet_ifgetstats@ --- *
323  *
324  * Arguments:   @struct device *nif@ = pointer to network interface
325  *
326  * Returns:     Pointer to a statistics buffer.
327  *
328  * Use:         Returns a block of interface statistics.
329  */
330
331 static struct enet_statistics *unet_ifgetstats(struct device *nif)
332 {
333   struct unet *u = nif->priv;
334   DIF(u, printk(KERN_DEBUG "unet: stats request for if %s\n", u->name); )
335   return (&u->e);
336 }
337
338 /* --- @unet_ifinit@ --- *
339  *
340  * Arguments:   @struct device *nif@ = pointer to network interface
341  *
342  * Returns:     Zero or error condition.
343  *
344  * Use:         Initialises a unet network interface.
345  */
346
347 static int unet_ifinit(struct device *nif)
348 {
349   struct unet *u = nif->priv;
350
351   DIF(u, printk(KERN_DEBUG "unet: initialise interface %s\n", u->name); )
352
353   /* --- Initialise statistics gathering --- */
354
355   memset(&u->e, 0, sizeof(u->e));
356   u->nif.get_stats = unet_ifgetstats;
357
358   /* --- Opening and closing interfaces --- */
359
360   u->nif.open = unet_ifopen;
361   u->nif.stop = unet_ifclose;
362
363   /* --- Sending packets to the `outside world' --- */
364
365   u->nif.hard_start_xmit = unet_iftx;
366
367   /* --- Some initialisation magic --- */
368
369 #ifdef notdef
370   for (i = 0; i < DEV_NUMBUFFS; i++)
371     skb_queue_head_init(&u->nif.buffs[i]);
372 #endif
373
374   /* --- Configure other grotty bits of the interface --- */
375
376   u->nif.hard_header = 0;
377   u->nif.rebuild_header = 0;
378   u->nif.set_mac_address = 0;
379
380   u->nif.type = ARPHRD_LOOPBACK;        /* Got a better idea? */
381   u->nif.hard_header_len = 0;
382   u->nif.mtu = 1500 - MAX_HEADER;
383   u->nif.addr_len = 0;
384   u->nif.tx_queue_len = 16;             /* I keep my own queue */
385
386   memset(u->nif.broadcast, 0xFFu, MAX_ADDR_LEN);
387   
388   u->nif.flags = IFF_NOARP;
389
390   /* --- Finished! --- */
391
392   return (0);
393 }
394
395 /*----- Attachment management ---------------------------------------------*/
396
397 /* --- @unet_setup@ --- *
398  *
399  * Arguments:   @struct unet *u@ = pointer to a unet block
400  *              @int seq@ = sequence number to allocate
401  *
402  * Returns:     Zero or error condition.
403  *
404  * Use:         Initialises a unet block ready for action.
405  */
406
407 static int unet_setup(struct unet *u, int seq)
408 {
409   static struct device tpl;
410   int e;
411
412   D( printk(KERN_DEBUG "unet: setting up unet block %d\n", seq); )
413
414   /* --- A little bit of initialisation --- */
415
416   u->seq = seq;
417   u->f = 0;
418   u->q = 0;
419
420   /* --- Inherit device debug flag from global flag --- */
421
422 #if UNET_DEBUG == UNET_DEBUGRUNTIME
423   if (unet_debug)
424     u->f |= UNIF_DEBUG;
425 #elif UNET_DEBUG == UNET_DEBUGALWAYS
426   u->f |= UNIF_DEBUG;
427 #endif
428
429   /* --- Set up the network device --- */
430
431   u->nif = tpl;
432   sprintf(u->name, "unet%d", seq);
433   u->nif.name = u->name;
434   u->nif.priv = u;
435   u->nif.init = unet_ifinit;
436   u->protocol = htons(ETH_P_IP);
437
438   if ((e = register_netdev(&u->nif)) != 0) {
439     printk(KERN_ERR "unet: couldn't register net interface\n");
440     return (e);
441   }
442
443   /* --- Empty the skbuff list --- */
444
445   skb_queue_head_init(&u->skbq);
446       
447   /* --- We're only finished, that's all --- */
448
449   return (0);
450 }
451
452 /* --- @unet_flush@ --- *
453  *
454  * Arguments:   @struct unet *u@ = pointer to a unet block
455  *
456  * Returns:     ---
457  *
458  * Use:         Releases all the packets waiting for transmission.
459  */
460
461 static void unet_flush(struct unet *u)
462 {
463   struct sk_buff *skb;
464
465 #if UNET_DEBUG != UNET_DEBUGNEVER
466   int i = 0;
467 #endif
468
469   DIF(u, printk(KERN_DEBUG "unet: flushing packets on %s\n", u->name); )
470
471   while ((skb = skb_dequeue(&u->skbq)) != 0) {
472     dev_kfree_skb(skb);
473     D( i++; )
474   }
475   DIF(u, printk(KERN_DEBUG "unet: released %d waiting packets\n", i); )
476 }
477
478 /* --- @unet_kill@ --- *
479  *
480  * Arguments:   @struct unet *u@ = pointer to a unet block
481  *
482  * Returns:     ---
483  *
484  * Use:         Kills and decommissions a Usernet block.
485  */
486
487 static void unet_kill(struct unet *u)
488 {
489   unet_flush(u);
490   unregister_netdev(&u->nif);
491 }
492
493 /*----- Handling the unet device ------------------------------------------*/
494
495 /* --- @unet_devopen@ --- *
496  *
497  * Arguments:   @struct inode *ino@ = inode block to open
498  *              @struct file *f@ = file block to play with
499  *
500  * Returns:     Zero or error condition.
501  *
502  * Use:         Handles the opening of a unet device.
503  */
504
505 static int unet_devopen(struct inode *ino, struct file *f)
506 {
507   struct unet *u, **up;
508   int seq;
509   int e = 0;
510
511   D( printk(KERN_DEBUG "unet: device open request\n"); )
512
513   /* --- Decide whether this is a persistent unet --- */
514
515   if ((seq = MINOR(ino->i_rdev)) < unet_npersist) {
516     u = &unet_persistent[seq];
517     if (u->f & UNIF_OPEN) {
518       e = -EBUSY;
519       goto tidy_0;
520     }
521     D( printk(KERN_DEBUG "unet: opened persistent %s\n", u->name); )
522   }
523
524   /* --- Otherwise we've got to create a new one --- */
525
526   else if (seq == UNET_TRANSMINOR) {
527
528     /* --- Try to find a spare sequence number --- */
529
530     if (unet_nif >= unet_maxif) {
531       printk(KERN_NOTICE "unet: no interfaces left\n");
532       e = -ENFILE;
533       goto tidy_0;
534     }
535     for (seq = unet_npersist, up = &unet_list; *up != 0;
536          seq++, up = &(*up)->next) {
537       if ((*up)->seq > seq)
538         break;
539     }
540     D( printk(KERN_DEBUG "unet: allocated sequence number %d\n", seq); )
541
542     /* --- Allocate a new block --- */
543
544     if ((u = kmalloc(sizeof(*u), GFP_KERNEL)) == 0) {
545       printk(KERN_ERR "unet: couldn't allocate a unet block\n");
546       e = -ENOMEM;
547       goto tidy_0;
548     }
549
550     /* --- Initialise the block --- */
551
552     if ((e = unet_setup(u, seq)) != 0)
553       goto tidy_1;
554     u->f |= UNIF_TRANS;
555     unet_nif++;
556
557     /* --- Link the block into the list --- */
558
559     u->next = *up;
560     u->prev = up;
561     if (*up)
562       (*up)->prev = &u->next;
563     *up = u;
564
565     D( printk(KERN_DEBUG "unet: opened transient %d\n", seq);
566        unet_dumpBlock(u); )
567   } else
568     return (-ENODEV);
569
570   /* --- Done --- */
571
572   u->f |= UNIF_OPEN;
573   f->private_data = u;
574   MOD_INC_USE_COUNT;
575   return (0);
576
577   /* --- Tidy up after little disasters --- */
578
579 tidy_1:
580   kfree(u);
581 tidy_0:
582   return (e);
583 }
584
585 /* --- @unet_devclose@ --- *
586  *
587  * Arguments:   @struct inode *ino@ = pointer to inode block
588  *              @struct file *f@ = pointer to file block
589  *
590  * Returns:     ---
591  *
592  * Use:         Frees up a unet connection.
593  */
594
595 static int unet_devclose(struct inode *ino, struct file *f)
596 {
597   struct unet *u = f->private_data;
598
599   DIF(u, printk(KERN_DEBUG "unet: closing %s\n", u->name); )
600
601   /* --- A transient unet needs to be destroyed --- */
602
603   if (u->f & UNIF_TRANS) {
604     int seq = u->seq;
605     *u->prev = u->next;
606     if (u->next)
607       u->next->prev = u->prev;
608     unet_kill(u);
609     kfree(u);
610     unet_nif--;
611     D( printk(KERN_DEBUG "unet: released transient %d\n", seq); )
612   }
613
614   /* --- A persistent unet needs to be shutdown --- */
615
616   else {
617     u->f &= ~UNIF_OPEN;
618     unet_flush(u);
619     DIF(u, printk(KERN_DEBUG "unet: unblocked persistent unet\n"); )
620   }
621
622   MOD_DEC_USE_COUNT;
623   return (0);
624 }
625
626 /* --- @unet_devpoll@ --- *
627  *
628  * Arguments:   @struct file *f@ = pointer to my file block
629  *              @struct poll_table_struct *p@ = poll table to wait on
630  *
631  * Returns:     Nonzero if OK to continue, zero if waiting.
632  *
633  * Use:         Plays a unet device's part in a @poll@(2) call.
634  */
635
636 static unsigned unet_devpoll(struct file *f, struct poll_table_struct *p)
637 {
638   struct unet *u = f->private_data;
639   unsigned m = 0;
640
641   DIF(u, printk(KERN_DEBUG "unet: poll request for %s\n", u->name); )
642
643   poll_wait(f, &u->q, p);
644   if (skb_peek(&u->skbq))
645     m |= POLLIN | POLLRDNORM;
646   m |= POLLOUT | POLLWRNORM;
647   return (m);
648 }
649
650 /* --- @unet_devwrite@ --- *
651  *
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)
656  *
657  * Returns:     Number of bytes written, or error condition.
658  *
659  * Use:         Sends a packet of data.  No buffering is done.
660  */
661
662 static ssize_t unet_devwrite(struct file *f, const char *buf, size_t sz,
663                              loff_t *off)
664 {
665   struct unet *u = f->private_data;
666   struct sk_buff *skb;
667
668   DIF(u, printk(KERN_DEBUG "unet: write request for %s\n", u->name); )
669
670   /* --- Dump the packet we're meant to send --- */
671
672 #ifdef UNET_DEBUG
673   if (u->f & UNIF_DEBUG) {
674     void *b = kmalloc(sz, GFP_KERNEL);
675     if (b) {
676       copy_from_user(b, buf, sz);
677       unet_hexdump(KERN_DEBUG "      ", b, sz);
678       kfree(b);
679     } else
680       printk(KERN_NOTICE "unet: not enough memory to dump block");
681   }
682 #endif
683
684   /* --- Allocate an skbuff for the block --- */
685
686   if ((skb = dev_alloc_skb(sz)) == 0) {
687     printk(KERN_ERR "unet: failed to allocate skbuff\n");
688     u->e.rx_dropped++;
689     return (-ENOSR);
690   }
691
692   /* --- Copy my data into the skbuff --- */
693
694   copy_from_user(skb_put(skb, sz), buf, sz);
695   skb->dev = &u->nif;
696   skb->mac.raw = skb->data;
697   skb->protocol = u->protocol;
698   netif_rx(skb);
699   u->e.rx_packets++;
700   return (sz);
701 }
702
703 /* --- @unet_devread@ --- *
704  *
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)
709  *
710  * Returns:     Number of bytes read, or error condition.
711  *
712  * Use:         Reads the next packet waiting for the device.
713  */
714
715 static ssize_t unet_devread(struct file *f, char *buf, size_t sz,
716                             loff_t *off)
717 {
718   struct unet *u = f->private_data;
719   struct sk_buff *skb;
720
721   DIF(u, printk(KERN_DEBUG "unet: read request for %s\n", u->name); )
722
723   /* --- Is the user sane? --- *
724    *
725    * The UDP protocol returns immediately in response to a zero-length read,
726    * and following this behaviour seems to cause `least surprise'.
727    */
728
729   if (!sz)
730     return (0);
731
732   /* --- Make sure there's a packet waiting for me --- */
733
734   if ((skb = skb_dequeue(&u->skbq)) == 0) {
735     struct wait_queue wq = { current, 0 };
736
737     DIF(u, printk(KERN_DEBUG "unet: no packets waiting\n"); )
738
739     /* --- Check for nonblocking I/O --- */
740
741     if (f->f_flags & O_NONBLOCK) {
742       DIF(u, printk(KERN_DEBUG "unet: nonblocking read: fail\n"); )
743       return (-EWOULDBLOCK);
744     }
745
746     /* --- Otherwise block until there's a packet --- */
747
748     current->state = TASK_INTERRUPTIBLE;
749     add_wait_queue(&u->q, &wq);
750
751     do {
752
753       if (signal_pending(current)) {
754
755         DIF(u, printk(KERN_DEBUG "unet: interrupted by signal\n"); )
756
757         remove_wait_queue(&u->q, &wq);
758         current->state = TASK_RUNNING;
759         return (-ERESTARTSYS);
760       }
761
762       DIF(u, printk(KERN_DEBUG "unet: blocking until packet arrives\n"); )
763
764       schedule();
765
766     } while ((skb = skb_dequeue(&u->skbq)) == 0);
767
768     remove_wait_queue(&u->q, &wq);
769     current->state = TASK_RUNNING;
770   }
771
772   DIF(u, printk(KERN_DEBUG "unet: found a packet\n"); )
773
774   /* --- There is now a packet waiting --- */
775
776   if (sz > skb->len)
777     sz = skb->len;
778   copy_to_user(buf, skb->data, sz);
779   dev_kfree_skb(skb);
780
781   DIF(u, printk(KERN_DEBUG "unet: passed packet on to user\n"); )
782
783   return (sz);
784 }
785
786 /* --- @unet_devioctl@ --- *
787  *
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
792  *
793  * Returns:     Positive return value, or negative error condition.
794  *
795  * Use:         Performs miscellaneous operations on a unet device.
796  */
797
798 static int unet_devioctl(struct inode *ino,
799                          struct file *f,
800                          unsigned int c,
801                          unsigned long arg)
802 {
803   struct unet *u = f->private_data;
804   int e = 0;
805
806   DIF(u, printk(KERN_DEBUG "unet: ioctl request for %s\n", u->name); )
807
808   switch (c) {
809
810     /* --- @FIONREAD@ --- *
811      *
812      * Caller wants to know how big the next packet will be.  Don't
813      * disappoint.
814      */
815
816     case FIONREAD: {
817       struct sk_buff *skb = skb_peek(&u->skbq);
818
819       DIF(u, printk(KERN_DEBUG "unet: FIONREAD found %d bytes\n",
820                     skb ? skb->len : 0); )
821
822       if (skb)
823         e = skb->len;
824     } break;
825
826     /* --- @UNIOCGINFO@ --- *
827      *
828      * Caller wants information about me.
829      */
830
831     case UNIOCGINFO: {
832       struct unet_info uni, *unip;
833
834       DIF(u, printk(KERN_DEBUG "unet: UNIOCGINFO called\n"); )
835
836       unip = (struct unet_info *)arg;
837
838       /* --- Special case --- *
839        *
840        * If the pointer is null, this call means `are you there?' and can
841        * be used to check for a Usernet attachment.
842        */
843
844       if (!unip)
845         break;
846
847       /* --- Ensure that the area can be written to --- */
848
849       if ((e = verify_area(VERIFY_WRITE, unip, sizeof(*unip))) != 0)
850         return (e);
851
852       /* --- Build the information block in memory --- */
853
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;
860
861       /* --- Copy it to the user and return --- */
862
863       copy_to_user(unip, &uni, sizeof(uni));
864     } break;
865
866     /* --- @UNIOCSDEBUG@ --- */
867
868 #if UNET_DEBUG != UNET_DEBUGNEVER
869     case UNIOCSDEBUG: {
870       int n = !!arg;
871       int o = !!(u->f & UNIF_DEBUG);
872
873       if (!capable(CAP_SYS_ADMIN))
874         return (-EPERM);
875       if (n || o) {
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");
882       }
883       if (n)
884         u->f |= UNIF_DEBUG;
885       else
886         u->f &= ~UNIF_DEBUG;
887     } break;
888 #endif
889
890     /* --- @UNIOCGPROTO@ and @UNIOCSPROTO@ --- */
891
892     case UNIOCGPROTO:
893       D( printk(KERN_DEBUG "unet: UNIOCGPROTO on %s: read protocol: %d\n",
894                 u->name, ntohs(u->protocol)); )
895       e = ntohs(u->protocol);
896       break;
897     case UNIOCSPROTO:
898       D( printk(KERN_DEBUG "unet: UNIOCSPROTO on %s: set protocol: %d\n",
899                 u->name, (int)arg); )
900       u->protocol = htons(arg);
901       break;
902
903     /* --- @UNIOCGGDEBUG@ and @UNIOCSGDEBUG@ --- */
904
905     case UNIOCGGDEBUG:
906       D( printk(KERN_DEBUG "unet: UNIOCGGDEBUG: get global debug: on\n"); )
907 #if UNET_DEBUG == UNET_DEBUGRUNTIME
908       e = unet_debug;
909 #elif UNET_DEBUG == UNET_DEBUGALWAYS
910       e = 1;
911 #elif UNET_DEBUG == UNET_DEBUGNEVER
912       e = 0;
913 #endif
914       break;
915
916 #if UNET_DEBUG == UNET_DEBUGRUNTIME
917     case UNIOCSGDEBUG:
918       if (!capable(CAP_SYS_ADMIN))
919         return (-EPERM);
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");
927       }
928       unet_debug = !!arg;
929       break;
930 #endif
931
932     /* --- @UNIOCDUMP@ --- */
933
934 #if UNET_DEBUG != UNET_DEBUGNEVER
935     case UNIOCDUMP:
936       D( unet_dumpBlock(u); )
937       break;
938 #endif
939
940     /* --- @UNIOCGMAXIF@ --- */
941
942     case UNIOCGMAXIF:
943       D( printk(KERN_DEBUG "unet: UNIOCGMAXIF: unet_maxif = %d\n",
944                 unet_maxif); )
945       e = unet_maxif;
946       break;
947
948     /* --- @UNIOCSMAXIF@ --- */
949
950     case UNIOCSMAXIF:
951       if (!capable(CAP_SYS_ADMIN))
952         return (-EPERM);
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)
957         return (-EINVAL);
958       if (arg < unet_nif)
959         return (-EBUSY);
960       unet_maxif = arg;
961       e = 0;
962       D( printk(KERN_DEBUG "unet: UNIOCSMAXIF: set unet_maxif = %d\n",
963                 unet_maxif); )
964       break;
965
966     /* --- Everything else --- *
967      *
968      * You lose.
969      */
970
971     default:
972       D( printk(KERN_DEBUG "unet: unknown ioctl %08x\n", c); )
973       e = -EINVAL;
974   }
975
976   return (e);
977 }
978
979 /*----- The unet device ---------------------------------------------------*/
980
981 /* --- Device operations --- */
982
983 struct file_operations unet_fops = {
984   0,                                    /* unet_devlseek */
985   unet_devread,
986   unet_devwrite,
987   0,                                    /* unet_devreaddir */
988   unet_devpoll,
989   unet_devioctl,
990   0,                                    /* unet_devmmap */
991   unet_devopen,
992   0,                                    /* unet_flush */
993   unet_devclose
994 };
995
996 /*----- Initaliseation and shutdown ---------------------------------------*/
997
998 /* --- @unet_init@ --- *
999  *
1000  * Arguments:   ---
1001  *
1002  * Returns:     Zero or error condition.
1003  *
1004  * Use:         Registers the unet device.
1005  */
1006
1007 __initfunc(int unet_init(void))
1008 {
1009   int e = 0;
1010   int i = 0;
1011
1012   /* --- Register my character device --- */
1013
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);
1016     goto tidy_0;
1017   }
1018
1019   /* --- Make the persistent unet devices --- */
1020
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",
1024            unet_npersist);
1025     e = -ENOMEM;
1026     goto tidy_1;
1027   }
1028     
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);
1032       goto tidy_2;
1033     }
1034   }
1035   unet_nif = unet_npersist;
1036
1037   /* --- Done --- */
1038
1039   D( unet_dump(); )
1040   return (0);
1041
1042   /* --- Some bad stuff happened --- */
1043
1044 tidy_2:
1045   while (i > 0) {
1046     i--;
1047     unregister_netdev(&unet_persistent[i].nif);
1048   }
1049   kfree(unet_persistent);
1050
1051 tidy_1:
1052   unregister_chrdev(UNET_MAJOR, "unet");
1053
1054 tidy_0:
1055   return (e);
1056 }
1057
1058 #ifdef MODULE
1059
1060 /* --- @init_module@ --- *
1061  *
1062  * Arguments:   ---
1063  *
1064  * Returns:     Zero or error condition.
1065  *
1066  * Use:         Initialises the unet kernel module.
1067  */
1068
1069 int init_module(void)
1070 {
1071   if (unet_npersist < 0 || unet_maxif < unet_npersist
1072 #if UNET_DEBUG == UNET_DEBUGRUNTIME
1073       || (unet_debug != 0 && unet_debug != 1)
1074 #endif
1075     )
1076     return (-EINVAL);
1077   return (unet_init());
1078 }
1079
1080 /* --- @cleanup_module@ --- *
1081  *
1082  * Arguments:   ---
1083  *
1084  * Returns:     ---
1085  *
1086  * Use:         Tidies up the unet module ready for it to be killed.
1087  */
1088
1089 void cleanup_module(void)
1090 {
1091   int i;
1092
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]);
1096   }
1097   kfree(unet_persistent);
1098
1099   unregister_chrdev(UNET_MAJOR, "unet");
1100 }
1101
1102 #endif
1103
1104 /*----- That's all, folks -------------------------------------------------*/