root/net/socket.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sock_setup
  2. move_addr_to_kernel
  3. move_addr_to_user
  4. get_fd
  5. socki_lookup
  6. sockfd_lookup
  7. sock_alloc
  8. sock_release_peer
  9. sock_release
  10. sock_lseek
  11. sock_read
  12. sock_write
  13. sock_readdir
  14. sock_ioctl
  15. sock_select
  16. sock_close
  17. sock_fasync
  18. sock_wake_async
  19. sock_awaitconn
  20. sock_socket
  21. sock_socketpair
  22. sock_bind
  23. sock_listen
  24. sock_accept
  25. sock_connect
  26. sock_getsockname
  27. sock_getpeername
  28. sock_send
  29. sock_sendto
  30. sock_recv
  31. sock_recvfrom
  32. sock_setsockopt
  33. sock_getsockopt
  34. sock_shutdown
  35. sock_fcntl
  36. sys_socketcall
  37. sock_register
  38. sock_unregister
  39. proto_init
  40. sock_init
  41. socket_get_info

   1 /*
   2  * NET          An implementation of the SOCKET network access protocol.
   3  *
   4  * Version:     @(#)socket.c    1.0.5   05/25/93
   5  *
   6  * Authors:     Orest Zborowski, <obz@Kodak.COM>
   7  *              Ross Biro, <bir7@leland.Stanford.Edu>
   8  *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
   9  *
  10  * Fixes:
  11  *              Anonymous       :       NOTSOCK/BADF cleanup. Error fix in
  12  *                                      shutdown()
  13  *              Alan Cox        :       verify_area() fixes
  14  *              Alan Cox        :       Removed DDI
  15  *              Jonathan Kamens :       SOCK_DGRAM reconnect bug
  16  *              Alan Cox        :       Moved a load of checks to the very
  17  *                                      top level.
  18  *              Alan Cox        :       Move address structures to/from user
  19  *                                      mode above the protocol layers.
  20  *              Rob Janssen     :       Allow 0 length sends.
  21  *              Alan Cox        :       Asynchronous I/O support (cribbed from the
  22  *                                      tty drivers).
  23  *              Niibe Yutaka    :       Asynchronous I/O for writes (4.4BSD style)
  24  *              Jeff Uphoff     :       Made max number of sockets command-line
  25  *                                      configurable.
  26  *              Matti Aarnio    :       Made the number of sockets dynamic,
  27  *                                      to be allocated when needed, and mr.
  28  *                                      Uphoff's max is used as max to be
  29  *                                      allowed to allocate.
  30  *
  31  *
  32  *              This program is free software; you can redistribute it and/or
  33  *              modify it under the terms of the GNU General Public License
  34  *              as published by the Free Software Foundation; either version
  35  *              2 of the License, or (at your option) any later version.
  36  *
  37  *
  38  *      This module is effectively the top level interface to the BSD socket
  39  *      paradigm. Because it is very simple it works well for Unix domain sockets,
  40  *      but requires a whole layer of substructure for the other protocols.
  41  *
  42  *      In addition it lacks an effective kernel -> kernel interface to go with
  43  *      the user one.
  44  */
  45 
  46 #include <linux/config.h>
  47 #include <linux/signal.h>
  48 #include <linux/errno.h>
  49 #include <linux/sched.h>
  50 #include <linux/kernel.h>
  51 #include <linux/major.h>
  52 #include <linux/stat.h>
  53 #include <linux/socket.h>
  54 #include <linux/fcntl.h>
  55 #include <linux/net.h>
  56 #include <linux/interrupt.h>
  57 #include <linux/netdevice.h>
  58 
  59 #include <asm/system.h>
  60 #include <asm/segment.h>
  61 
  62 static int sock_lseek(struct inode *inode, struct file *file, off_t offset,
  63                       int whence);
  64 static int sock_read(struct inode *inode, struct file *file, char *buf,
  65                      int size);
  66 static int sock_write(struct inode *inode, struct file *file, char *buf,
  67                       int size);
  68 static int sock_readdir(struct inode *inode, struct file *file,
  69                         struct dirent *dirent, int count);
  70 static void sock_close(struct inode *inode, struct file *file);
  71 static int sock_select(struct inode *inode, struct file *file, int which, select_table *seltable);
  72 static int sock_ioctl(struct inode *inode, struct file *file,
  73                       unsigned int cmd, unsigned long arg);
  74 static int sock_fasync(struct inode *inode, struct file *filp, int on);
  75                    
  76 
  77 
  78 /*
  79  *      Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
  80  *      in the operation structures but are done directly via the socketcall() multiplexor.
  81  */
  82 
  83 static struct file_operations socket_file_ops = {
  84         sock_lseek,
  85         sock_read,
  86         sock_write,
  87         sock_readdir,
  88         sock_select,
  89         sock_ioctl,
  90         NULL,                   /* mmap */
  91         NULL,                   /* no special open code... */
  92         sock_close,
  93         NULL,                   /* no fsync */
  94         sock_fasync
  95 };
  96 
  97 /*
  98  *      The lists of sockets
  99  */
 100 
 101 static struct socket *freesockets = NULL;       /* List of free sockets,
 102                                                    pick the first */
 103 static struct socket *usedsockets = NULL;       /* Doubly-linked list of the
 104                                                    active sockets */
 105 
 106 /*
 107  *      Used to wait for a socket.
 108  */
 109 static struct wait_queue *socket_wait_free = NULL;
 110 /*
 111  *      The protocol list. Each protocol is registered in here.
 112  */
 113 static struct proto_ops *pops[NPROTO];
 114 /*      
 115  *      Maximum number of sockets -- override-able on command-line.
 116  */
 117 static int maxnsockets = NSOCKETS;
 118 /*
 119  *      Number of sockets allocated
 120  */
 121 static int nsockets = 0;
 122 /*
 123  *      Statistics counters of the free/used lists
 124  */
 125 static int sockets_in_use  = 0;
 126 static int sockets_in_free = 0;
 127 
 128 /*      
 129  *      Overrides default max number of sockets if supplied on command-line.
 130  */
 131 void sock_setup(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
 132 {
 133         maxnsockets = ints[0] ? ints[1] : NSOCKETS;
 134 }
 135 
 136 
 137 /*
 138  *      Support routines. Move socket addresses back and forth across the kernel/user
 139  *      divide and look after the messy bits.
 140  */
 141 
 142 #define MAX_SOCK_ADDR   128             /* 108 for Unix domain - 16 for IP, 16 for IPX, about 80 for AX.25 */
 143  
 144 static int move_addr_to_kernel(void *uaddr, int ulen, void *kaddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 145 {
 146         int err;
 147         if(ulen<0||ulen>MAX_SOCK_ADDR)
 148                 return -EINVAL;
 149         if(ulen==0)
 150                 return 0;
 151         if((err=verify_area(VERIFY_READ,uaddr,ulen))<0)
 152                 return err;
 153         memcpy_fromfs(kaddr,uaddr,ulen);
 154         return 0;
 155 }
 156 
 157 static int move_addr_to_user(void *kaddr, int klen, void *uaddr, int *ulen)
     /* [previous][next][first][last][top][bottom][index][help] */
 158 {
 159         int err;
 160         int len;
 161 
 162                 
 163         if((err=verify_area(VERIFY_WRITE,ulen,sizeof(*ulen)))<0)
 164                 return err;
 165         len=get_fs_long(ulen);
 166         if(len>klen)
 167                 len=klen;
 168         if(len<0 || len> MAX_SOCK_ADDR)
 169                 return -EINVAL;
 170         if(len)
 171         {
 172                 if((err=verify_area(VERIFY_WRITE,uaddr,len))<0)
 173                         return err;
 174                 memcpy_tofs(uaddr,kaddr,len);
 175         }
 176         put_fs_long(len,ulen);
 177         return 0;
 178 }
 179 
 180 /*
 181  *      Obtains the first available file descriptor and sets it up for use. 
 182  */
 183 
 184 static int get_fd(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 185 {
 186         int fd;
 187         struct file *file;
 188 
 189         /*
 190          *      Find a file descriptor suitable for return to the user. 
 191          */
 192 
 193         file = get_empty_filp();
 194         if (!file) 
 195                 return(-1);
 196 
 197         for (fd = 0; fd < NR_OPEN; ++fd)
 198                 if (!current->files->fd[fd]) 
 199                         break;
 200         if (fd == NR_OPEN) 
 201         {
 202                 file->f_count = 0;
 203                 return(-1);
 204         }
 205 
 206         FD_CLR(fd, &current->files->close_on_exec);
 207                 current->files->fd[fd] = file;
 208         file->f_op = &socket_file_ops;
 209         file->f_mode = 3;
 210         file->f_flags = O_RDWR;
 211         file->f_count = 1;
 212         file->f_inode = inode;
 213         if (inode) 
 214                 inode->i_count++;
 215         file->f_pos = 0;
 216         return(fd);
 217 }
 218 
 219 
 220 /*
 221  *      Go from an inode to its socket slot.
 222  */
 223 
 224 struct socket *socki_lookup(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 225 {
 226         struct socket *sock;
 227 
 228         if ((sock = inode->i_socket) != NULL) 
 229         {
 230                 if (sock->state != SS_FREE && SOCK_INODE(sock) == inode)
 231                         return sock;
 232                 printk("socket.c: uhhuh. stale inode->i_socket pointer\n");
 233         }
 234         for (sock = usedsockets; sock != NULL; sock = sock->nextsock)
 235                 if (sock->state != SS_FREE && SOCK_INODE(sock) == inode) 
 236                 {
 237                         printk("socket.c: uhhuh. Found socket despite no inode->i_socket pointer\n");
 238                         return(sock);
 239                 }
 240                 return(NULL);
 241 }
 242 
 243 /*
 244  *      Go from a file number to its socket slot.
 245  */
 246 
 247 static inline struct socket *sockfd_lookup(int fd, struct file **pfile)
     /* [previous][next][first][last][top][bottom][index][help] */
 248 {
 249         struct file *file;
 250 
 251         if (fd < 0 || fd >= NR_OPEN || !(file = current->files->fd[fd])) 
 252                 return(NULL);
 253 
 254         if (pfile) 
 255                 *pfile = file;
 256 
 257         return(socki_lookup(file->f_inode));
 258 }
 259 
 260 /*
 261  *      Allocate a socket. Wait if we are out of sockets.
 262  */
 263 
 264 static struct socket *sock_alloc(int wait)
     /* [previous][next][first][last][top][bottom][index][help] */
 265 {
 266         struct socket *sock;
 267         int i;
 268 
 269         while (1) 
 270         {
 271                 if (freesockets == NULL)
 272                 {
 273                         /* Lets see if we can allocate some more */
 274                         cli();
 275                         /* Alloc them from same memory page, if possible.
 276                            Nothing SHOULD prevent us from allocing one at
 277                            the time.. */
 278                         for (i = 0; i < 16 && nsockets < maxnsockets; ++i)
 279                         {
 280                                 sock = (struct socket*)kmalloc(sizeof(struct socket),GFP_KERNEL);
 281                                 if (sock == NULL) break; /* Ah well.. */
 282                                 sock->state = SS_FREE;
 283                                 sock->nextsock = freesockets;
 284                                 sock->prevsock = NULL;
 285                                 freesockets = sock;
 286                                 ++sockets_in_free;
 287                                 ++nsockets;
 288                         }
 289                         sti();
 290 printk("sock_alloc: Alloced some more, now %d sockets\n", nsockets);
 291                 }
 292 
 293 
 294                 cli();
 295                 sock = freesockets;
 296                 if (sock != NULL) /* Freelist, we pick
 297                                      the first -- or only */
 298                 {
 299                 /*
 300                  * Move it to the `usedsockets' linked-list
 301                  * at its FRONT (thus  ->prevsock = NULL)
 302                  */
 303                         freesockets = sock->nextsock;
 304                         sock->nextsock = usedsockets;
 305                         sock->prevsock = NULL;
 306                         /* Is there something in there already ? */
 307                         if (usedsockets != NULL)
 308                           /* Yes, attach the `previous' pointer */
 309                           usedsockets->prevsock = sock;
 310                         usedsockets = sock;
 311 
 312                         --sockets_in_free;
 313                         ++sockets_in_use;
 314                         sti();
 315                 /*
 316                  *      Got one..
 317                  */
 318                         sock->state = SS_UNCONNECTED;
 319                         sock->flags = 0;
 320                         sock->ops = NULL;
 321                         sock->data = NULL;
 322                         sock->conn = NULL;
 323                         sock->iconn = NULL;
 324                         sock->fasync_list = NULL;
 325                 /*
 326                  * This really shouldn't be necessary, but everything
 327                  * else depends on inodes, so we grab it.
 328                  * Sleeps are also done on the i_wait member of this
 329                  * inode.  The close system call will iput this inode
 330                  * for us.
 331                  */
 332                         if (!(SOCK_INODE(sock) = get_empty_inode())) 
 333                         {
 334                                 printk("NET: sock_alloc: no more inodes\n");
 335                         /*
 336                          * Roll-back the linkage
 337                          */
 338                                 cli();
 339                                 /* Not the last ? */
 340                                 if (sock->nextsock != NULL)
 341                                   sock->nextsock->prevsock = sock->prevsock;
 342                                 /* Not the first ? */
 343                                 if (sock->prevsock != NULL)
 344                                   sock->prevsock->nextsock = sock->nextsock;
 345                                 else
 346                                   /* It is the first, update the head-handle */
 347                                   usedsockets = sock->nextsock;
 348                                 /* Link it back to the free-list */
 349                                 sock->nextsock = freesockets;
 350                                 sock->prevsock = NULL; /* Not used, but .. */
 351                                 freesockets = sock;
 352                                 ++sockets_in_free;
 353                                 --sockets_in_use;
 354                                 sti();
 355                                 sock->state = SS_FREE;
 356                                 return(NULL);
 357                         }
 358                         SOCK_INODE(sock)->i_mode = S_IFSOCK;
 359                         SOCK_INODE(sock)->i_uid = current->euid;
 360                         SOCK_INODE(sock)->i_gid = current->egid;
 361                         SOCK_INODE(sock)->i_socket = sock;
 362 
 363                         sock->wait = &SOCK_INODE(sock)->i_wait;
 364 
 365                         return(sock);
 366                 }
 367                 sti();
 368 
 369                 /*
 370                  * The rest of these are in fact vestigal from the previous
 371                  * version, which didn't have growing list of sockets.
 372                  * These may become necessary if there are 2000 (or whatever
 373                  * the hard limit is set to) sockets already in system,
 374                  * but then the system itself is quite catatonic.. IMO [mea]
 375                  */
 376 
 377                 /*
 378                  *      If its a 'now or never request' then return.
 379                  */
 380                 if (!wait) 
 381                         return(NULL);
 382                 /*
 383                  *      Sleep on the socket free'ing queue.
 384                  */
 385                 interruptible_sleep_on(&socket_wait_free);
 386                 /*
 387                  *      If we have been interrupted then return.
 388                  */
 389                 if (current->signal & ~current->blocked) 
 390                 {
 391                         return(NULL);
 392                 }
 393         }
 394 }
 395 
 396 /*
 397  *      Release a socket.
 398  */
 399 
 400 static inline void sock_release_peer(struct socket *peer)
     /* [previous][next][first][last][top][bottom][index][help] */
 401 {
 402         peer->state = SS_DISCONNECTING;
 403         wake_up_interruptible(peer->wait);
 404         sock_wake_async(peer, 1);
 405 }
 406 
 407 
 408 static void sock_release(struct socket *sock)
     /* [previous][next][first][last][top][bottom][index][help] */
 409 {
 410         int oldstate;
 411         struct inode *inode;
 412         struct socket *peersock, *nextsock;
 413         unsigned long flags;
 414 
 415         if ((oldstate = sock->state) != SS_UNCONNECTED)
 416                 sock->state = SS_DISCONNECTING;
 417 
 418         /*
 419          *      Wake up anyone waiting for connections. 
 420          */
 421 
 422         for (peersock = sock->iconn; peersock; peersock = nextsock) 
 423         {
 424                 nextsock = peersock->next;
 425                 sock_release_peer(peersock);
 426         }
 427 
 428         /*
 429          * Wake up anyone we're connected to. First, we release the
 430          * protocol, to give it a chance to flush data, etc.
 431          */
 432 
 433         peersock = (oldstate == SS_CONNECTED) ? sock->conn : NULL;
 434         if (sock->ops) 
 435                 sock->ops->release(sock, peersock);
 436         if (peersock)
 437                 sock_release_peer(peersock);
 438         inode = SOCK_INODE(sock);
 439         /*
 440          * Remove this `sock' from the doubly-linked chain.
 441          */
 442         save_flags(flags);
 443         cli();
 444         sock->state = SS_FREE;          /* this really releases us */
 445         /* Not the last ? */
 446         if (sock->nextsock != NULL)
 447           sock->nextsock->prevsock = sock->prevsock;
 448         /* Not the first ? */
 449         if (sock->prevsock != NULL)
 450           sock->prevsock->nextsock = sock->nextsock;
 451         else
 452           /* It is the first, update the head-handle */
 453           usedsockets = sock->nextsock;
 454         /* Link it back to the free-list */
 455         sock->nextsock = freesockets;
 456         sock->prevsock = NULL; /* Not really used, but.. */
 457         freesockets = sock;
 458         --sockets_in_use;       /* Bookkeeping.. */
 459         ++sockets_in_free;
 460         restore_flags(flags);
 461         
 462         /*
 463          *      This will wake anyone waiting for a free socket.
 464          */
 465         wake_up_interruptible(&socket_wait_free);
 466 
 467         /*
 468          *      We need to do this. If sock alloc was called we already have an inode. 
 469          */
 470          
 471         iput(inode);
 472 }
 473 
 474 /*
 475  *      Sockets are not seekable.
 476  */
 477 
 478 static int sock_lseek(struct inode *inode, struct file *file, off_t offset, int whence)
     /* [previous][next][first][last][top][bottom][index][help] */
 479 {
 480         return(-ESPIPE);
 481 }
 482 
 483 /*
 484  *      Read data from a socket. ubuf is a user mode pointer. We make sure the user
 485  *      area ubuf...ubuf+size-1 is writable before asking the protocol.
 486  */
 487 
 488 static int sock_read(struct inode *inode, struct file *file, char *ubuf, int size)
     /* [previous][next][first][last][top][bottom][index][help] */
 489 {
 490         struct socket *sock;
 491         int err;
 492   
 493         if (!(sock = socki_lookup(inode))) 
 494         {
 495                 printk("NET: sock_read: can't find socket for inode!\n");
 496                 return(-EBADF);
 497         }
 498         if (sock->flags & SO_ACCEPTCON) 
 499                 return(-EINVAL);
 500 
 501         if(size<0)
 502                 return -EINVAL;
 503         if(size==0)
 504                 return 0;
 505         if ((err=verify_area(VERIFY_WRITE,ubuf,size))<0)
 506                 return err;
 507         return(sock->ops->read(sock, ubuf, size, (file->f_flags & O_NONBLOCK)));
 508 }
 509 
 510 /*
 511  *      Write data to a socket. We verify that the user area ubuf..ubuf+size-1 is
 512  *      readable by the user process.
 513  */
 514 
 515 static int sock_write(struct inode *inode, struct file *file, char *ubuf, int size)
     /* [previous][next][first][last][top][bottom][index][help] */
 516 {
 517         struct socket *sock;
 518         int err;
 519         
 520         if (!(sock = socki_lookup(inode))) 
 521         {
 522                 printk("NET: sock_write: can't find socket for inode!\n");
 523                 return(-EBADF);
 524         }
 525 
 526         if (sock->flags & SO_ACCEPTCON) 
 527                 return(-EINVAL);
 528         
 529         if(size<0)
 530                 return -EINVAL;
 531         if(size==0)
 532                 return 0;
 533                 
 534         if ((err=verify_area(VERIFY_READ,ubuf,size))<0)
 535                 return err;
 536         return(sock->ops->write(sock, ubuf, size,(file->f_flags & O_NONBLOCK)));
 537 }
 538 
 539 /*
 540  *      You can't read directories from a socket!
 541  */
 542  
 543 static int sock_readdir(struct inode *inode, struct file *file, struct dirent *dirent,
     /* [previous][next][first][last][top][bottom][index][help] */
 544              int count)
 545 {
 546         return(-EBADF);
 547 }
 548 
 549 /*
 550  *      With an ioctl arg may well be a user mode pointer, but we don't know what to do
 551  *      with it - thats up to the protocol still.
 552  */
 553 
 554 int sock_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
     /* [previous][next][first][last][top][bottom][index][help] */
 555            unsigned long arg)
 556 {
 557         struct socket *sock;
 558 
 559         if (!(sock = socki_lookup(inode))) 
 560         {
 561                 printk("NET: sock_ioctl: can't find socket for inode!\n");
 562                 return(-EBADF);
 563         }
 564         return(sock->ops->ioctl(sock, cmd, arg));
 565 }
 566 
 567 
 568 static int sock_select(struct inode *inode, struct file *file, int sel_type, select_table * wait)
     /* [previous][next][first][last][top][bottom][index][help] */
 569 {
 570         struct socket *sock;
 571 
 572         if (!(sock = socki_lookup(inode))) 
 573         {
 574                 printk("NET: sock_select: can't find socket for inode!\n");
 575                 return(0);
 576         }
 577 
 578         /*
 579          *      We can't return errors to select, so its either yes or no. 
 580          */
 581 
 582         if (sock->ops && sock->ops->select)
 583                 return(sock->ops->select(sock, sel_type, wait));
 584         return(0);
 585 }
 586 
 587 
 588 void sock_close(struct inode *inode, struct file *filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 589 {
 590         struct socket *sock;
 591 
 592         /*
 593          *      It's possible the inode is NULL if we're closing an unfinished socket. 
 594          */
 595 
 596         if (!inode) 
 597                 return;
 598 
 599         if (!(sock = socki_lookup(inode))) 
 600         {
 601                 printk("NET: sock_close: can't find socket for inode!\n");
 602                 return;
 603         }
 604         sock_fasync(inode, filp, 0);
 605         sock_release(sock);
 606 }
 607 
 608 /*
 609  *      Update the socket async list
 610  */
 611  
 612 static int sock_fasync(struct inode *inode, struct file *filp, int on)
     /* [previous][next][first][last][top][bottom][index][help] */
 613 {
 614         struct fasync_struct *fa, *fna=NULL, **prev;
 615         struct socket *sock;
 616         unsigned long flags;
 617         
 618         if (on)
 619         {
 620                 fna=(struct fasync_struct *)kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
 621                 if(fna==NULL)
 622                         return -ENOMEM;
 623         }
 624 
 625         sock = socki_lookup(inode);
 626         
 627         prev=&(sock->fasync_list);
 628         
 629         save_flags(flags);
 630         cli();
 631         
 632         for(fa=*prev; fa!=NULL; prev=&fa->fa_next,fa=*prev)
 633                 if(fa->fa_file==filp)
 634                         break;
 635         
 636         if(on)
 637         {
 638                 if(fa!=NULL)
 639                 {
 640                         kfree_s(fna,sizeof(struct fasync_struct));
 641                         restore_flags(flags);
 642                         return 0;
 643                 }
 644                 fna->fa_file=filp;
 645                 fna->magic=FASYNC_MAGIC;
 646                 fna->fa_next=sock->fasync_list;
 647                 sock->fasync_list=fna;
 648         }
 649         else
 650         {
 651                 if(fa!=NULL)
 652                 {
 653                         *prev=fa->fa_next;
 654                         kfree_s(fa,sizeof(struct fasync_struct));
 655                 }
 656         }
 657         restore_flags(flags);
 658         return 0;
 659 }
 660 
 661 int sock_wake_async(struct socket *sock, int how)
     /* [previous][next][first][last][top][bottom][index][help] */
 662 {
 663         if (!sock || !sock->fasync_list)
 664                 return -1;
 665         switch (how)
 666         {
 667                 case 0:
 668                         kill_fasync(sock->fasync_list, SIGIO);
 669                         break;
 670                 case 1:
 671                         if (!(sock->flags & SO_WAITDATA))
 672                                 kill_fasync(sock->fasync_list, SIGIO);
 673                         break;
 674                 case 2:
 675                         if (sock->flags & SO_NOSPACE)
 676                         {
 677                                 kill_fasync(sock->fasync_list, SIGIO);
 678                                 sock->flags &= ~SO_NOSPACE;
 679                         }
 680                         break;
 681         }
 682         return 0;
 683 }
 684 
 685         
 686 /*
 687  *      Wait for a connection.
 688  */
 689 
 690 int sock_awaitconn(struct socket *mysock, struct socket *servsock, int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
 691 {
 692         struct socket *last;
 693 
 694         /*
 695          *      We must be listening
 696          */
 697         if (!(servsock->flags & SO_ACCEPTCON)) 
 698         {
 699                 return(-EINVAL);
 700         }
 701 
 702         /*
 703          *      Put ourselves on the server's incomplete connection queue. 
 704          */
 705          
 706         mysock->next = NULL;
 707         cli();
 708         if (!(last = servsock->iconn)) 
 709                 servsock->iconn = mysock;
 710         else 
 711         {
 712                 while (last->next) 
 713                         last = last->next;
 714                 last->next = mysock;
 715         }
 716         mysock->state = SS_CONNECTING;
 717         mysock->conn = servsock;
 718         sti();
 719 
 720         /*
 721          * Wake up server, then await connection. server will set state to
 722          * SS_CONNECTED if we're connected.
 723          */
 724         wake_up_interruptible(servsock->wait);
 725         sock_wake_async(servsock, 0);
 726 
 727         if (mysock->state != SS_CONNECTED) 
 728         {
 729                 if (flags & O_NONBLOCK)
 730                         return -EINPROGRESS;
 731 
 732                 interruptible_sleep_on(mysock->wait);
 733                 if (mysock->state != SS_CONNECTED &&
 734                     mysock->state != SS_DISCONNECTING) 
 735                 {
 736                 /*
 737                  * if we're not connected we could have been
 738                  * 1) interrupted, so we need to remove ourselves
 739                  *    from the server list
 740                  * 2) rejected (mysock->conn == NULL), and have
 741                  *    already been removed from the list
 742                  */
 743                         if (mysock->conn == servsock) 
 744                         {
 745                                 cli();
 746                                 if ((last = servsock->iconn) == mysock)
 747                                         servsock->iconn = mysock->next;
 748                                 else 
 749                                 {
 750                                         while (last->next != mysock) 
 751                                                 last = last->next;
 752                                         last->next = mysock->next;
 753                                 }
 754                                 sti();
 755                         }
 756                         return(mysock->conn ? -EINTR : -EACCES);
 757                 }
 758         }
 759         return(0);
 760 }
 761 
 762 
 763 /*
 764  *      Perform the socket system call. we locate the appropriate
 765  *      family, then create a fresh socket.
 766  */
 767 
 768 static int sock_socket(int family, int type, int protocol)
     /* [previous][next][first][last][top][bottom][index][help] */
 769 {
 770         int i, fd;
 771         struct socket *sock;
 772         struct proto_ops *ops;
 773 
 774         /* Locate the correct protocol family. */
 775         for (i = 0; i < NPROTO; ++i) 
 776         {
 777                 if (pops[i] == NULL) continue;
 778                 if (pops[i]->family == family) 
 779                         break;
 780         }
 781 
 782         if (i == NPROTO) 
 783         {
 784                 return -EINVAL;
 785         }
 786 
 787         ops = pops[i];
 788 
 789 /*
 790  *      Check that this is a type that we know how to manipulate and
 791  *      the protocol makes sense here. The family can still reject the
 792  *      protocol later.
 793  */
 794   
 795         if ((type != SOCK_STREAM && type != SOCK_DGRAM &&
 796                 type != SOCK_SEQPACKET && type != SOCK_RAW &&
 797                 type != SOCK_PACKET) || protocol < 0)
 798                         return(-EINVAL);
 799 
 800 /*
 801  *      Allocate the socket and allow the family to set things up. if
 802  *      the protocol is 0, the family is instructed to select an appropriate
 803  *      default.
 804  */
 805 
 806         if (!(sock = sock_alloc(1))) 
 807         {
 808                 printk("NET: sock_socket: no more sockets\n");
 809                 return(-ENOSR); /* Was: EAGAIN, but we are out of
 810                                    system resources! */
 811         }
 812 
 813         sock->type = type;
 814         sock->ops = ops;
 815         if ((i = sock->ops->create(sock, protocol)) < 0) 
 816         {
 817                 sock_release(sock);
 818                 return(i);
 819         }
 820 
 821         if ((fd = get_fd(SOCK_INODE(sock))) < 0) 
 822         {
 823                 sock_release(sock);
 824                 return(-EINVAL);
 825         }
 826 
 827         return(fd);
 828 }
 829 
 830 /*
 831  *      Create a pair of connected sockets.
 832  */
 833 
 834 static int sock_socketpair(int family, int type, int protocol, unsigned long usockvec[2])
     /* [previous][next][first][last][top][bottom][index][help] */
 835 {
 836         int fd1, fd2, i;
 837         struct socket *sock1, *sock2;
 838         int er;
 839 
 840         /*
 841          * Obtain the first socket and check if the underlying protocol
 842          * supports the socketpair call.
 843          */
 844 
 845         if ((fd1 = sock_socket(family, type, protocol)) < 0) 
 846                 return(fd1);
 847         sock1 = sockfd_lookup(fd1, NULL);
 848         if (!sock1->ops->socketpair) 
 849         {
 850                 sys_close(fd1);
 851                 return(-EINVAL);
 852         }
 853 
 854         /*
 855          *      Now grab another socket and try to connect the two together. 
 856          */
 857 
 858         if ((fd2 = sock_socket(family, type, protocol)) < 0) 
 859         {
 860                 sys_close(fd1);
 861                 return(-EINVAL);
 862         }
 863 
 864         sock2 = sockfd_lookup(fd2, NULL);
 865         if ((i = sock1->ops->socketpair(sock1, sock2)) < 0) 
 866         {
 867                 sys_close(fd1);
 868                 sys_close(fd2);
 869                 return(i);
 870         }
 871 
 872         sock1->conn = sock2;
 873         sock2->conn = sock1;
 874         sock1->state = SS_CONNECTED;
 875         sock2->state = SS_CONNECTED;
 876 
 877         er=verify_area(VERIFY_WRITE, usockvec, 2 * sizeof(int));
 878         if(er)
 879         {
 880                 sys_close(fd1);
 881                 sys_close(fd2);
 882                 return er;
 883         }
 884         put_fs_long(fd1, &usockvec[0]);
 885         put_fs_long(fd2, &usockvec[1]);
 886 
 887         return(0);
 888 }
 889 
 890 
 891 /*
 892  *      Bind a name to a socket. Nothing much to do here since its
 893  *      the protocol's responsibility to handle the local address.
 894  *
 895  *      We move the socket address to kernel space before we call
 896  *      the protocol layer (having also checked the address is ok).
 897  */
 898  
 899 static int sock_bind(int fd, struct sockaddr *umyaddr, int addrlen)
     /* [previous][next][first][last][top][bottom][index][help] */
 900 {
 901         struct socket *sock;
 902         int i;
 903         char address[MAX_SOCK_ADDR];
 904         int err;
 905 
 906         if (fd < 0 || fd >= NR_OPEN || current->files->fd[fd] == NULL)
 907                 return(-EBADF);
 908         
 909         if (!(sock = sockfd_lookup(fd, NULL))) 
 910                 return(-ENOTSOCK);
 911   
 912         if((err=move_addr_to_kernel(umyaddr,addrlen,address))<0)
 913                 return err;
 914   
 915         if ((i = sock->ops->bind(sock, (struct sockaddr *)address, addrlen)) < 0) 
 916         {
 917                 return(i);
 918         }
 919         return(0);
 920 }
 921 
 922 
 923 /*
 924  *      Perform a listen. Basically, we allow the protocol to do anything
 925  *      necessary for a listen, and if that works, we mark the socket as
 926  *      ready for listening.
 927  */
 928 
 929 static int sock_listen(int fd, int backlog)
     /* [previous][next][first][last][top][bottom][index][help] */
 930 {
 931         struct socket *sock;
 932 
 933         if (fd < 0 || fd >= NR_OPEN || current->files->fd[fd] == NULL)
 934                 return(-EBADF);
 935         if (!(sock = sockfd_lookup(fd, NULL))) 
 936                 return(-ENOTSOCK);
 937 
 938         if (sock->state != SS_UNCONNECTED) 
 939         {
 940                 return(-EINVAL);
 941         }
 942 
 943         if (sock->ops && sock->ops->listen)
 944                 sock->ops->listen(sock, backlog);
 945         sock->flags |= SO_ACCEPTCON;
 946         return(0);
 947 }
 948 
 949 
 950 /*
 951  *      For accept, we attempt to create a new socket, set up the link
 952  *      with the client, wake up the client, then return the new
 953  *      connected fd. We collect the address of the connector in kernel
 954  *      space and move it to user at the very end. This is buggy because
 955  *      we open the socket then return an error.
 956  */
 957 
 958 static int sock_accept(int fd, struct sockaddr *upeer_sockaddr, int *upeer_addrlen)
     /* [previous][next][first][last][top][bottom][index][help] */
 959 {
 960         struct file *file;
 961         struct socket *sock, *newsock;
 962         int i;
 963         char address[MAX_SOCK_ADDR];
 964         int len;
 965 
 966         if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
 967                 return(-EBADF);
 968         if (!(sock = sockfd_lookup(fd, &file))) 
 969                 return(-ENOTSOCK);
 970         if (sock->state != SS_UNCONNECTED) 
 971         {
 972                 return(-EINVAL);
 973         }
 974         if (!(sock->flags & SO_ACCEPTCON)) 
 975         {
 976                 return(-EINVAL);
 977         }
 978 
 979         if (!(newsock = sock_alloc(0))) 
 980         {
 981                 printk("NET: sock_accept: no more sockets\n");
 982                 return(-ENOSR); /* Was: EGAIN, but we are out of system
 983                                    resources! */
 984         }
 985         newsock->type = sock->type;
 986         newsock->ops = sock->ops;
 987         if ((i = sock->ops->dup(newsock, sock)) < 0) 
 988         {
 989                 sock_release(newsock);
 990                 return(i);
 991         }
 992 
 993         i = newsock->ops->accept(sock, newsock, file->f_flags);
 994         if ( i < 0) 
 995         {
 996                 sock_release(newsock);
 997                 return(i);
 998         }
 999 
1000         if ((fd = get_fd(SOCK_INODE(newsock))) < 0) 
1001         {
1002                 sock_release(newsock);
1003                 return(-EINVAL);
1004         }
1005 
1006         if (upeer_sockaddr)
1007         {
1008                 newsock->ops->getname(newsock, (struct sockaddr *)address, &len, 1);
1009                 move_addr_to_user(address,len, upeer_sockaddr, upeer_addrlen);
1010         }
1011         return(fd);
1012 }
1013 
1014 
1015 /*
1016  *      Attempt to connect to a socket with the server address.  The address
1017  *      is in user space so we verify it is OK and move it to kernel space.
1018  */
1019  
1020 static int sock_connect(int fd, struct sockaddr *uservaddr, int addrlen)
     /* [previous][next][first][last][top][bottom][index][help] */
1021 {
1022         struct socket *sock;
1023         struct file *file;
1024         int i;
1025         char address[MAX_SOCK_ADDR];
1026         int err;
1027 
1028         if (fd < 0 || fd >= NR_OPEN || (file=current->files->fd[fd]) == NULL)
1029                 return(-EBADF);
1030         if (!(sock = sockfd_lookup(fd, &file)))
1031                 return(-ENOTSOCK);
1032 
1033         if((err=move_addr_to_kernel(uservaddr,addrlen,address))<0)
1034                 return err;
1035   
1036         switch(sock->state) 
1037         {
1038                 case SS_UNCONNECTED:
1039                         /* This is ok... continue with connect */
1040                         break;
1041                 case SS_CONNECTED:
1042                         /* Socket is already connected */
1043                         if(sock->type == SOCK_DGRAM) /* Hack for now - move this all into the protocol */
1044                                 break;
1045                         return -EISCONN;
1046                 case SS_CONNECTING:
1047                         /* Not yet connected... we will check this. */
1048                 
1049                         /*
1050                          *      FIXME:  for all protocols what happens if you start
1051                          *      an async connect fork and both children connect. Clean
1052                          *      this up in the protocols!
1053                          */
1054                         break;
1055                 default:
1056                         return(-EINVAL);
1057         }
1058         i = sock->ops->connect(sock, (struct sockaddr *)address, addrlen, file->f_flags);
1059         if (i < 0) 
1060         {
1061                 return(i);
1062         }
1063         return(0);
1064 }
1065 
1066 /*
1067  *      Get the local address ('name') of a socket object. Move the obtained
1068  *      name to user space.
1069  */
1070 
1071 static int sock_getsockname(int fd, struct sockaddr *usockaddr, int *usockaddr_len)
     /* [previous][next][first][last][top][bottom][index][help] */
1072 {
1073         struct socket *sock;
1074         char address[MAX_SOCK_ADDR];
1075         int len;
1076         int err;
1077         
1078         if (fd < 0 || fd >= NR_OPEN || current->files->fd[fd] == NULL)
1079                 return(-EBADF);
1080         if (!(sock = sockfd_lookup(fd, NULL)))
1081                 return(-ENOTSOCK);
1082 
1083         err=sock->ops->getname(sock, (struct sockaddr *)address, &len, 0);
1084         if(err)
1085                 return err;
1086         if((err=move_addr_to_user(address,len, usockaddr, usockaddr_len))<0)
1087                 return err;
1088         return 0;
1089 }
1090 
1091 /*
1092  *      Get the remote address ('name') of a socket object. Move the obtained
1093  *      name to user space.
1094  */
1095  
1096 static int sock_getpeername(int fd, struct sockaddr *usockaddr, int *usockaddr_len)
     /* [previous][next][first][last][top][bottom][index][help] */
1097 {
1098         struct socket *sock;
1099         char address[MAX_SOCK_ADDR];
1100         int len;
1101         int err;
1102 
1103         if (fd < 0 || fd >= NR_OPEN || current->files->fd[fd] == NULL)
1104                 return(-EBADF);
1105         if (!(sock = sockfd_lookup(fd, NULL)))
1106                 return(-ENOTSOCK);
1107 
1108         err=sock->ops->getname(sock, (struct sockaddr *)address, &len, 1);
1109         if(err)
1110                 return err;
1111         if((err=move_addr_to_user(address,len, usockaddr, usockaddr_len))<0)
1112                 return err;
1113         return 0;
1114 }
1115 
1116 /*
1117  *      Send a datagram down a socket. The datagram as with write() is
1118  *      in user space. We check it can be read.
1119  */
1120 
1121 static int sock_send(int fd, void * buff, int len, unsigned flags)
     /* [previous][next][first][last][top][bottom][index][help] */
1122 {
1123         struct socket *sock;
1124         struct file *file;
1125         int err;
1126 
1127         if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1128                 return(-EBADF);
1129         if (!(sock = sockfd_lookup(fd, NULL))) 
1130                 return(-ENOTSOCK);
1131 
1132         if(len<0)
1133                 return -EINVAL;
1134         err=verify_area(VERIFY_READ, buff, len);
1135         if(err)
1136                 return err;
1137         return(sock->ops->send(sock, buff, len, (file->f_flags & O_NONBLOCK), flags));
1138 }
1139 
1140 /*
1141  *      Send a datagram to a given address. We move the address into kernel
1142  *      space and check the user space data area is readable before invoking
1143  *      the protocol.
1144  */
1145 
1146 static int sock_sendto(int fd, void * buff, int len, unsigned flags,
     /* [previous][next][first][last][top][bottom][index][help] */
1147            struct sockaddr *addr, int addr_len)
1148 {
1149         struct socket *sock;
1150         struct file *file;
1151         char address[MAX_SOCK_ADDR];
1152         int err;
1153         
1154         if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1155                 return(-EBADF);
1156         if (!(sock = sockfd_lookup(fd, NULL)))
1157                 return(-ENOTSOCK);
1158 
1159         if(len<0)
1160                 return -EINVAL;
1161         err=verify_area(VERIFY_READ,buff,len);
1162         if(err)
1163                 return err;
1164         
1165         if((err=move_addr_to_kernel(addr,addr_len,address))<0)
1166                 return err;
1167 
1168         return(sock->ops->sendto(sock, buff, len, (file->f_flags & O_NONBLOCK),
1169                 flags, (struct sockaddr *)address, addr_len));
1170 }
1171 
1172 
1173 /*
1174  *      Receive a datagram from a socket. This isn't really right. The BSD manual
1175  *      pages explicitly state that recv is recvfrom with a NULL to argument. The
1176  *      Linux stack gets the right results for the wrong reason and this need to
1177  *      be tidied in the inet layer and removed from here.
1178  *      We check the buffer is writable and valid.
1179  */
1180 
1181 static int sock_recv(int fd, void * buff, int len, unsigned flags)
     /* [previous][next][first][last][top][bottom][index][help] */
1182 {
1183         struct socket *sock;
1184         struct file *file;
1185         int err;
1186 
1187         if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1188                 return(-EBADF);
1189 
1190         if (!(sock = sockfd_lookup(fd, NULL))) 
1191                 return(-ENOTSOCK);
1192                 
1193         if(len<0)
1194                 return -EINVAL;
1195         if(len==0)
1196                 return 0;
1197         err=verify_area(VERIFY_WRITE, buff, len);
1198         if(err)
1199                 return err;
1200 
1201         return(sock->ops->recv(sock, buff, len,(file->f_flags & O_NONBLOCK), flags));
1202 }
1203 
1204 /*
1205  *      Receive a frame from the socket and optionally record the address of the 
1206  *      sender. We verify the buffers are writable and if needed move the
1207  *      sender address from kernel to user space.
1208  */
1209 
1210 static int sock_recvfrom(int fd, void * buff, int len, unsigned flags,
     /* [previous][next][first][last][top][bottom][index][help] */
1211              struct sockaddr *addr, int *addr_len)
1212 {
1213         struct socket *sock;
1214         struct file *file;
1215         char address[MAX_SOCK_ADDR];
1216         int err;
1217         int alen;
1218         if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1219                 return(-EBADF);
1220         if (!(sock = sockfd_lookup(fd, NULL))) 
1221                 return(-ENOTSOCK);
1222         if(len<0)
1223                 return -EINVAL;
1224         if(len==0)
1225                 return 0;
1226 
1227         err=verify_area(VERIFY_WRITE,buff,len);
1228         if(err)
1229                 return err;
1230   
1231         len=sock->ops->recvfrom(sock, buff, len, (file->f_flags & O_NONBLOCK),
1232                      flags, (struct sockaddr *)address, &alen);
1233 
1234         if(len<0)
1235                 return len;
1236         if(addr!=NULL && (err=move_addr_to_user(address,alen, addr, addr_len))<0)
1237                 return err;
1238 
1239         return len;
1240 }
1241 
1242 /*
1243  *      Set a socket option. Because we don't know the option lengths we have
1244  *      to pass the user mode parameter for the protocols to sort out.
1245  */
1246  
1247 static int sock_setsockopt(int fd, int level, int optname, char *optval, int optlen)
     /* [previous][next][first][last][top][bottom][index][help] */
1248 {
1249         struct socket *sock;
1250         struct file *file;
1251         
1252         if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1253                 return(-EBADF);
1254         if (!(sock = sockfd_lookup(fd, NULL))) 
1255                 return(-ENOTSOCK);
1256 
1257         return(sock->ops->setsockopt(sock, level, optname, optval, optlen));
1258 }
1259 
1260 /*
1261  *      Get a socket option. Because we don't know the option lengths we have
1262  *      to pass a user mode parameter for the protocols to sort out.
1263  */
1264 
1265 static int sock_getsockopt(int fd, int level, int optname, char *optval, int *optlen)
     /* [previous][next][first][last][top][bottom][index][help] */
1266 {
1267         struct socket *sock;
1268         struct file *file;
1269 
1270         if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1271                 return(-EBADF);
1272         if (!(sock = sockfd_lookup(fd, NULL)))
1273                 return(-ENOTSOCK);
1274             
1275         if (!sock->ops || !sock->ops->getsockopt) 
1276                 return(0);
1277         return(sock->ops->getsockopt(sock, level, optname, optval, optlen));
1278 }
1279 
1280 
1281 /*
1282  *      Shutdown a socket.
1283  */
1284  
1285 static int sock_shutdown(int fd, int how)
     /* [previous][next][first][last][top][bottom][index][help] */
1286 {
1287         struct socket *sock;
1288         struct file *file;
1289 
1290         if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1291                 return(-EBADF);
1292         if (!(sock = sockfd_lookup(fd, NULL))) 
1293                 return(-ENOTSOCK);
1294 
1295         return(sock->ops->shutdown(sock, how));
1296 }
1297 
1298 
1299 /*
1300  *      Perform a file control on a socket file descriptor.
1301  */
1302 
1303 int sock_fcntl(struct file *filp, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1304 {
1305         struct socket *sock;
1306 
1307         sock = socki_lookup (filp->f_inode);
1308         if (sock != NULL && sock->ops != NULL && sock->ops->fcntl != NULL)
1309                 return(sock->ops->fcntl(sock, cmd, arg));
1310         return(-EINVAL);
1311 }
1312 
1313 
1314 /*
1315  *      System call vectors. Since I (RIB) want to rewrite sockets as streams,
1316  *      we have this level of indirection. Not a lot of overhead, since more of
1317  *      the work is done via read/write/select directly.
1318  *
1319  *      I'm now expanding this up to a higher level to separate the assorted
1320  *      kernel/user space manipulations and global assumptions from the protocol
1321  *      layers proper - AC.
1322  */
1323 
1324 asmlinkage int sys_socketcall(int call, unsigned long *args)
     /* [previous][next][first][last][top][bottom][index][help] */
1325 {
1326         int er;
1327         switch(call) 
1328         {
1329                 case SYS_SOCKET:
1330                         er=verify_area(VERIFY_READ, args, 3 * sizeof(long));
1331                         if(er)
1332                                 return er;
1333                         return(sock_socket(get_fs_long(args+0),
1334                                 get_fs_long(args+1),
1335                                 get_fs_long(args+2)));
1336                 case SYS_BIND:
1337                         er=verify_area(VERIFY_READ, args, 3 * sizeof(long));
1338                         if(er)
1339                                 return er;
1340                         return(sock_bind(get_fs_long(args+0),
1341                                 (struct sockaddr *)get_fs_long(args+1),
1342                                 get_fs_long(args+2)));
1343                 case SYS_CONNECT:
1344                         er=verify_area(VERIFY_READ, args, 3 * sizeof(long));
1345                         if(er)
1346                                 return er;
1347                         return(sock_connect(get_fs_long(args+0),
1348                                 (struct sockaddr *)get_fs_long(args+1),
1349                                 get_fs_long(args+2)));
1350                 case SYS_LISTEN:
1351                         er=verify_area(VERIFY_READ, args, 2 * sizeof(long));
1352                         if(er)
1353                                 return er;
1354                         return(sock_listen(get_fs_long(args+0),
1355                                 get_fs_long(args+1)));
1356                 case SYS_ACCEPT:
1357                         er=verify_area(VERIFY_READ, args, 3 * sizeof(long));
1358                         if(er)
1359                                 return er;
1360                         return(sock_accept(get_fs_long(args+0),
1361                                 (struct sockaddr *)get_fs_long(args+1),
1362                                 (int *)get_fs_long(args+2)));
1363                 case SYS_GETSOCKNAME:
1364                         er=verify_area(VERIFY_READ, args, 3 * sizeof(long));
1365                         if(er)
1366                                 return er;
1367                         return(sock_getsockname(get_fs_long(args+0),
1368                                 (struct sockaddr *)get_fs_long(args+1),
1369                                 (int *)get_fs_long(args+2)));
1370                 case SYS_GETPEERNAME:
1371                         er=verify_area(VERIFY_READ, args, 3 * sizeof(long));
1372                         if(er)
1373                                 return er;
1374                         return(sock_getpeername(get_fs_long(args+0),
1375                                 (struct sockaddr *)get_fs_long(args+1),
1376                                 (int *)get_fs_long(args+2)));
1377                 case SYS_SOCKETPAIR:
1378                         er=verify_area(VERIFY_READ, args, 4 * sizeof(long));
1379                         if(er)
1380                                 return er;
1381                         return(sock_socketpair(get_fs_long(args+0),
1382                                 get_fs_long(args+1),
1383                                 get_fs_long(args+2),
1384                                 (unsigned long *)get_fs_long(args+3)));
1385                 case SYS_SEND:
1386                         er=verify_area(VERIFY_READ, args, 4 * sizeof(unsigned long));
1387                         if(er)
1388                                 return er;
1389                         return(sock_send(get_fs_long(args+0),
1390                                 (void *)get_fs_long(args+1),
1391                                 get_fs_long(args+2),
1392                                 get_fs_long(args+3)));
1393                 case SYS_SENDTO:
1394                         er=verify_area(VERIFY_READ, args, 6 * sizeof(unsigned long));
1395                         if(er)
1396                                 return er;
1397                         return(sock_sendto(get_fs_long(args+0),
1398                                 (void *)get_fs_long(args+1),
1399                                 get_fs_long(args+2),
1400                                 get_fs_long(args+3),
1401                                 (struct sockaddr *)get_fs_long(args+4),
1402                                 get_fs_long(args+5)));
1403                 case SYS_RECV:
1404                         er=verify_area(VERIFY_READ, args, 4 * sizeof(unsigned long));
1405                         if(er)
1406                                 return er;
1407                         return(sock_recv(get_fs_long(args+0),
1408                                 (void *)get_fs_long(args+1),
1409                                 get_fs_long(args+2),
1410                                 get_fs_long(args+3)));
1411                 case SYS_RECVFROM:
1412                         er=verify_area(VERIFY_READ, args, 6 * sizeof(unsigned long));
1413                         if(er)
1414                                 return er;
1415                         return(sock_recvfrom(get_fs_long(args+0),
1416                                 (void *)get_fs_long(args+1),
1417                                 get_fs_long(args+2),
1418                                 get_fs_long(args+3),
1419                                 (struct sockaddr *)get_fs_long(args+4),
1420                                 (int *)get_fs_long(args+5)));
1421                 case SYS_SHUTDOWN:
1422                         er=verify_area(VERIFY_READ, args, 2* sizeof(unsigned long));
1423                         if(er)
1424                                 return er;
1425                         return(sock_shutdown(get_fs_long(args+0),
1426                                 get_fs_long(args+1)));
1427                 case SYS_SETSOCKOPT:
1428                         er=verify_area(VERIFY_READ, args, 5*sizeof(unsigned long));
1429                         if(er)
1430                                 return er;
1431                         return(sock_setsockopt(get_fs_long(args+0),
1432                                 get_fs_long(args+1),
1433                                 get_fs_long(args+2),
1434                                 (char *)get_fs_long(args+3),
1435                                 get_fs_long(args+4)));
1436                 case SYS_GETSOCKOPT:
1437                         er=verify_area(VERIFY_READ, args, 5*sizeof(unsigned long));
1438                         if(er)
1439                                 return er;
1440                         return(sock_getsockopt(get_fs_long(args+0),
1441                                 get_fs_long(args+1),
1442                                 get_fs_long(args+2),
1443                                 (char *)get_fs_long(args+3),
1444                                 (int *)get_fs_long(args+4)));
1445                 default:
1446                         return(-EINVAL);
1447         }
1448 }
1449 
1450 /*
1451  *      This function is called by a protocol handler that wants to
1452  *      advertise its address family, and have it linked into the
1453  *      SOCKET module.
1454  */
1455  
1456 int sock_register(int family, struct proto_ops *ops)
     /* [previous][next][first][last][top][bottom][index][help] */
1457 {
1458         int i;
1459 
1460         cli();
1461         for(i = 0; i < NPROTO; i++) 
1462         {
1463                 if (pops[i] != NULL) 
1464                         continue;
1465                 pops[i] = ops;
1466                 pops[i]->family = family;
1467                 sti();
1468                 return(i);
1469         }
1470         sti();
1471         return(-ENOMEM);
1472 }
1473 
1474 /*
1475  *      This function is called by a protocol handler that wants to
1476  *      remove its address family, and have it unlinked from the
1477  *      SOCKET module.
1478  */
1479  
1480 int sock_unregister(int family)
     /* [previous][next][first][last][top][bottom][index][help] */
1481 {
1482         int i;
1483 
1484         cli();
1485         for(i = 0; i < NPROTO; i++) 
1486         {
1487                 if (pops[i] == NULL) 
1488                         continue;
1489                 if(pops[i]->family == family)
1490                 {
1491                         pops[i]=NULL;
1492                         sti();
1493                         return(i);
1494                 }
1495         }
1496         sti();
1497         return(-ENOENT);
1498 }
1499 
1500 void proto_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1501 {
1502         extern struct net_proto protocols[];    /* Network protocols */
1503         struct net_proto *pro;
1504 
1505         /* Kick all configured protocols. */
1506         pro = protocols;
1507         while (pro->name != NULL) 
1508         {
1509                 (*pro->init_func)(pro);
1510                 pro++;
1511         }
1512         /* We're all done... */
1513 }
1514 
1515 
1516 void sock_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1517 {
1518         int i;
1519 
1520         printk("Swansea University Computer Society NET3.019\n");
1521 
1522         /*
1523          *      Initialize all address (protocol) families. 
1524          */
1525          
1526         for (i = 0; i < NPROTO; ++i) pops[i] = NULL;
1527 
1528         /*
1529          *      Initialize the protocols module. 
1530          */
1531 
1532         proto_init();
1533 
1534 #ifdef CONFIG_NET
1535         /* 
1536          *      Initialize the DEV module. 
1537          */
1538 
1539         dev_init();
1540   
1541         /*
1542          *      And the bottom half handler 
1543          */
1544 
1545         bh_base[NET_BH].routine= net_bh;
1546 #endif  
1547 }
1548 
1549 int socket_get_info(char *buffer, char **start, off_t offset, int length)
     /* [previous][next][first][last][top][bottom][index][help] */
1550 {
1551         int len = sprintf(buffer,
1552                           "sockets: used %d free %d alloced %d highlimit %d\n",
1553                           sockets_in_use, sockets_in_free,
1554                           nsockets, maxnsockets);
1555         if (offset >= len)
1556         {
1557                 *start = buffer;
1558                 return 0;
1559         }
1560         *start = buffer + offset;
1561         len -= offset;
1562         if (len > length)
1563                 len = length;
1564         return len;
1565 }

/* [previous][next][first][last][top][bottom][index][help] */