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