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                 }
 291 
 292 
 293                 cli();
 294                 sock = freesockets;
 295                 if (sock != NULL) /* Freelist, we pick
 296                                      the first -- or only */
 297                 {
 298                 /*
 299                  * Move it to the `usedsockets' linked-list
 300                  * at its FRONT (thus  ->prevsock = NULL)
 301                  */
 302                         freesockets = sock->nextsock;
 303                         sock->nextsock = usedsockets;
 304                         sock->prevsock = NULL;
 305                         /* Is there something in there already ? */
 306                         if (usedsockets != NULL)
 307                           /* Yes, attach the `previous' pointer */
 308                           usedsockets->prevsock = sock;
 309                         usedsockets = sock;
 310 
 311                         --sockets_in_free;
 312                         ++sockets_in_use;
 313                         sti();
 314                 /*
 315                  *      Got one..
 316                  */
 317                         sock->state = SS_UNCONNECTED;
 318                         sock->flags = 0;
 319                         sock->ops = NULL;
 320                         sock->data = NULL;
 321                         sock->conn = NULL;
 322                         sock->iconn = NULL;
 323                         sock->fasync_list = NULL;
 324                 /*
 325                  * This really shouldn't be necessary, but everything
 326                  * else depends on inodes, so we grab it.
 327                  * Sleeps are also done on the i_wait member of this
 328                  * inode.  The close system call will iput this inode
 329                  * for us.
 330                  */
 331                         if (!(SOCK_INODE(sock) = get_empty_inode())) 
 332                         {
 333                                 printk("NET: sock_alloc: no more inodes\n");
 334                         /*
 335                          * Roll-back the linkage
 336                          */
 337                                 cli();
 338                                 /* Not the last ? */
 339                                 if (sock->nextsock != NULL)
 340                                   sock->nextsock->prevsock = sock->prevsock;
 341                                 /* Not the first ? */
 342                                 if (sock->prevsock != NULL)
 343                                   sock->prevsock->nextsock = sock->nextsock;
 344                                 else
 345                                   /* It is the first, update the head-handle */
 346                                   usedsockets = sock->nextsock;
 347                                 /* Link it back to the free-list */
 348                                 sock->nextsock = freesockets;
 349                                 sock->prevsock = NULL; /* Not used, but .. */
 350                                 freesockets = sock;
 351                                 ++sockets_in_free;
 352                                 --sockets_in_use;
 353                                 sti();
 354                                 sock->state = SS_FREE;
 355                                 return(NULL);
 356                         }
 357                         SOCK_INODE(sock)->i_mode = S_IFSOCK;
 358                         SOCK_INODE(sock)->i_uid = current->euid;
 359                         SOCK_INODE(sock)->i_gid = current->egid;
 360                         SOCK_INODE(sock)->i_socket = sock;
 361 
 362                         sock->wait = &SOCK_INODE(sock)->i_wait;
 363 
 364                         return(sock);
 365                 }
 366                 sti();
 367 
 368                 /*
 369                  * The rest of these are in fact vestigial from the previous
 370                  * version, which didn't have growing list of sockets.
 371                  * These may become necessary if there are 2000 (or whatever
 372                  * the hard limit is set to) sockets already in system,
 373                  * but then the system itself is quite catatonic.. IMO [mea]
 374                  */
 375 
 376                 /*
 377                  *      If its a 'now or never request' then return.
 378                  */
 379                 if (!wait) 
 380                         return(NULL);
 381                 /*
 382                  *      Sleep on the socket free'ing queue.
 383                  */
 384                 interruptible_sleep_on(&socket_wait_free);
 385                 /*
 386                  *      If we have been interrupted then return.
 387                  */
 388                 if (current->signal & ~current->blocked) 
 389                 {
 390                         return(NULL);
 391                 }
 392         }
 393 }
 394 
 395 /*
 396  *      Release a socket.
 397  */
 398 
 399 static inline void sock_release_peer(struct socket *peer)
     /* [previous][next][first][last][top][bottom][index][help] */
 400 {
 401         peer->state = SS_DISCONNECTING;
 402         wake_up_interruptible(peer->wait);
 403         sock_wake_async(peer, 1);
 404 }
 405 
 406 
 407 static void sock_release(struct socket *sock)
     /* [previous][next][first][last][top][bottom][index][help] */
 408 {
 409         int oldstate;
 410         struct inode *inode;
 411         struct socket *peersock, *nextsock;
 412         unsigned long flags;
 413 
 414         if ((oldstate = sock->state) != SS_UNCONNECTED)
 415                 sock->state = SS_DISCONNECTING;
 416 
 417         /*
 418          *      Wake up anyone waiting for connections. 
 419          */
 420 
 421         for (peersock = sock->iconn; peersock; peersock = nextsock) 
 422         {
 423                 nextsock = peersock->next;
 424                 sock_release_peer(peersock);
 425         }
 426 
 427         /*
 428          * Wake up anyone we're connected to. First, we release the
 429          * protocol, to give it a chance to flush data, etc.
 430          */
 431 
 432         peersock = (oldstate == SS_CONNECTED) ? sock->conn : NULL;
 433         if (sock->ops) 
 434                 sock->ops->release(sock, peersock);
 435         if (peersock)
 436                 sock_release_peer(peersock);
 437         inode = SOCK_INODE(sock);
 438         /*
 439          * Remove this `sock' from the doubly-linked chain.
 440          */
 441         save_flags(flags);
 442         cli();
 443         sock->state = SS_FREE;          /* this really releases us */
 444         /* Not the last ? */
 445         if (sock->nextsock != NULL)
 446           sock->nextsock->prevsock = sock->prevsock;
 447         /* Not the first ? */
 448         if (sock->prevsock != NULL)
 449           sock->prevsock->nextsock = sock->nextsock;
 450         else
 451           /* It is the first, update the head-handle */
 452           usedsockets = sock->nextsock;
 453         /* Link it back to the free-list */
 454         sock->nextsock = freesockets;
 455         sock->prevsock = NULL; /* Not really used, but.. */
 456         freesockets = sock;
 457         --sockets_in_use;       /* Bookkeeping.. */
 458         ++sockets_in_free;
 459         restore_flags(flags);
 460         
 461         /*
 462          *      This will wake anyone waiting for a free socket.
 463          */
 464         wake_up_interruptible(&socket_wait_free);
 465 
 466         /*
 467          *      We need to do this. If sock alloc was called we already have an inode. 
 468          */
 469          
 470         iput(inode);
 471 }
 472 
 473 /*
 474  *      Sockets are not seekable.
 475  */
 476 
 477 static int sock_lseek(struct inode *inode, struct file *file, off_t offset, int whence)
     /* [previous][next][first][last][top][bottom][index][help] */
 478 {
 479         return(-ESPIPE);
 480 }
 481 
 482 /*
 483  *      Read data from a socket. ubuf is a user mode pointer. We make sure the user
 484  *      area ubuf...ubuf+size-1 is writable before asking the protocol.
 485  */
 486 
 487 static int sock_read(struct inode *inode, struct file *file, char *ubuf, int size)
     /* [previous][next][first][last][top][bottom][index][help] */
 488 {
 489         struct socket *sock;
 490         int err;
 491   
 492         if (!(sock = socki_lookup(inode))) 
 493         {
 494                 printk("NET: sock_read: can't find socket for inode!\n");
 495                 return(-EBADF);
 496         }
 497         if (sock->flags & SO_ACCEPTCON) 
 498                 return(-EINVAL);
 499 
 500         if(size<0)
 501                 return -EINVAL;
 502         if(size==0)
 503                 return 0;
 504         if ((err=verify_area(VERIFY_WRITE,ubuf,size))<0)
 505                 return err;
 506         return(sock->ops->read(sock, ubuf, size, (file->f_flags & O_NONBLOCK)));
 507 }
 508 
 509 /*
 510  *      Write data to a socket. We verify that the user area ubuf..ubuf+size-1 is
 511  *      readable by the user process.
 512  */
 513 
 514 static int sock_write(struct inode *inode, struct file *file, char *ubuf, int size)
     /* [previous][next][first][last][top][bottom][index][help] */
 515 {
 516         struct socket *sock;
 517         int err;
 518         
 519         if (!(sock = socki_lookup(inode))) 
 520         {
 521                 printk("NET: sock_write: can't find socket for inode!\n");
 522                 return(-EBADF);
 523         }
 524 
 525         if (sock->flags & SO_ACCEPTCON) 
 526                 return(-EINVAL);
 527         
 528         if(size<0)
 529                 return -EINVAL;
 530         if(size==0)
 531                 return 0;
 532                 
 533         if ((err=verify_area(VERIFY_READ,ubuf,size))<0)
 534                 return err;
 535         return(sock->ops->write(sock, ubuf, size,(file->f_flags & O_NONBLOCK)));
 536 }
 537 
 538 /*
 539  *      You can't read directories from a socket!
 540  */
 541  
 542 static int sock_readdir(struct inode *inode, struct file *file, struct dirent *dirent,
     /* [previous][next][first][last][top][bottom][index][help] */
 543              int count)
 544 {
 545         return(-EBADF);
 546 }
 547 
 548 /*
 549  *      With an ioctl arg may well be a user mode pointer, but we don't know what to do
 550  *      with it - thats up to the protocol still.
 551  */
 552 
 553 int sock_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
     /* [previous][next][first][last][top][bottom][index][help] */
 554            unsigned long arg)
 555 {
 556         struct socket *sock;
 557 
 558         if (!(sock = socki_lookup(inode))) 
 559         {
 560                 printk("NET: sock_ioctl: can't find socket for inode!\n");
 561                 return(-EBADF);
 562         }
 563         return(sock->ops->ioctl(sock, cmd, arg));
 564 }
 565 
 566 
 567 static int sock_select(struct inode *inode, struct file *file, int sel_type, select_table * wait)
     /* [previous][next][first][last][top][bottom][index][help] */
 568 {
 569         struct socket *sock;
 570 
 571         if (!(sock = socki_lookup(inode))) 
 572         {
 573                 printk("NET: sock_select: can't find socket for inode!\n");
 574                 return(0);
 575         }
 576 
 577         /*
 578          *      We can't return errors to select, so its either yes or no. 
 579          */
 580 
 581         if (sock->ops && sock->ops->select)
 582                 return(sock->ops->select(sock, sel_type, wait));
 583         return(0);
 584 }
 585 
 586 
 587 void sock_close(struct inode *inode, struct file *filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 588 {
 589         struct socket *sock;
 590 
 591         /*
 592          *      It's possible the inode is NULL if we're closing an unfinished socket. 
 593          */
 594 
 595         if (!inode) 
 596                 return;
 597 
 598         if (!(sock = socki_lookup(inode))) 
 599         {
 600                 printk("NET: sock_close: can't find socket for inode!\n");
 601                 return;
 602         }
 603         sock_fasync(inode, filp, 0);
 604         sock_release(sock);
 605 }
 606 
 607 /*
 608  *      Update the socket async list
 609  */
 610  
 611 static int sock_fasync(struct inode *inode, struct file *filp, int on)
     /* [previous][next][first][last][top][bottom][index][help] */
 612 {
 613         struct fasync_struct *fa, *fna=NULL, **prev;
 614         struct socket *sock;
 615         unsigned long flags;
 616         
 617         if (on)
 618         {
 619                 fna=(struct fasync_struct *)kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
 620                 if(fna==NULL)
 621                         return -ENOMEM;
 622         }
 623 
 624         sock = socki_lookup(inode);
 625         
 626         prev=&(sock->fasync_list);
 627         
 628         save_flags(flags);
 629         cli();
 630         
 631         for(fa=*prev; fa!=NULL; prev=&fa->fa_next,fa=*prev)
 632                 if(fa->fa_file==filp)
 633                         break;
 634         
 635         if(on)
 636         {
 637                 if(fa!=NULL)
 638                 {
 639                         kfree_s(fna,sizeof(struct fasync_struct));
 640                         restore_flags(flags);
 641                         return 0;
 642                 }
 643                 fna->fa_file=filp;
 644                 fna->magic=FASYNC_MAGIC;
 645                 fna->fa_next=sock->fasync_list;
 646                 sock->fasync_list=fna;
 647         }
 648         else
 649         {
 650                 if(fa!=NULL)
 651                 {
 652                         *prev=fa->fa_next;
 653                         kfree_s(fa,sizeof(struct fasync_struct));
 654                 }
 655         }
 656         restore_flags(flags);
 657         return 0;
 658 }
 659 
 660 int sock_wake_async(struct socket *sock, int how)
     /* [previous][next][first][last][top][bottom][index][help] */
 661 {
 662         if (!sock || !sock->fasync_list)
 663                 return -1;
 664         switch (how)
 665         {
 666                 case 0:
 667                         kill_fasync(sock->fasync_list, SIGIO);
 668                         break;
 669                 case 1:
 670                         if (!(sock->flags & SO_WAITDATA))
 671                                 kill_fasync(sock->fasync_list, SIGIO);
 672                         break;
 673                 case 2:
 674                         if (sock->flags & SO_NOSPACE)
 675                         {
 676                                 kill_fasync(sock->fasync_list, SIGIO);
 677                                 sock->flags &= ~SO_NOSPACE;
 678                         }
 679                         break;
 680         }
 681         return 0;
 682 }
 683 
 684         
 685 /*
 686  *      Wait for a connection.
 687  */
 688 
 689 int sock_awaitconn(struct socket *mysock, struct socket *servsock, int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
 690 {
 691         struct socket *last;
 692 
 693         /*
 694          *      We must be listening
 695          */
 696         if (!(servsock->flags & SO_ACCEPTCON)) 
 697         {
 698                 return(-EINVAL);
 699         }
 700 
 701         /*
 702          *      Put ourselves on the server's incomplete connection queue. 
 703          */
 704          
 705         mysock->next = NULL;
 706         cli();
 707         if (!(last = servsock->iconn)) 
 708                 servsock->iconn = mysock;
 709         else 
 710         {
 711                 while (last->next) 
 712                         last = last->next;
 713                 last->next = mysock;
 714         }
 715         mysock->state = SS_CONNECTING;
 716         mysock->conn = servsock;
 717         sti();
 718 
 719         /*
 720          * Wake up server, then await connection. server will set state to
 721          * SS_CONNECTED if we're connected.
 722          */
 723         wake_up_interruptible(servsock->wait);
 724         sock_wake_async(servsock, 0);
 725 
 726         if (mysock->state != SS_CONNECTED) 
 727         {
 728                 if (flags & O_NONBLOCK)
 729                         return -EINPROGRESS;
 730 
 731                 interruptible_sleep_on(mysock->wait);
 732                 if (mysock->state != SS_CONNECTED &&
 733                     mysock->state != SS_DISCONNECTING) 
 734                 {
 735                 /*
 736                  * if we're not connected we could have been
 737                  * 1) interrupted, so we need to remove ourselves
 738                  *    from the server list
 739                  * 2) rejected (mysock->conn == NULL), and have
 740                  *    already been removed from the list
 741                  */
 742                         if (mysock->conn == servsock) 
 743                         {
 744                                 cli();
 745                                 if ((last = servsock->iconn) == mysock)
 746                                         servsock->iconn = mysock->next;
 747                                 else 
 748                                 {
 749                                         while (last->next != mysock) 
 750                                                 last = last->next;
 751                                         last->next = mysock->next;
 752                                 }
 753                                 sti();
 754                         }
 755                         return(mysock->conn ? -EINTR : -EACCES);
 756                 }
 757         }
 758         return(0);
 759 }
 760 
 761 
 762 /*
 763  *      Perform the socket system call. we locate the appropriate
 764  *      family, then create a fresh socket.
 765  */
 766 
 767 static int sock_socket(int family, int type, int protocol)
     /* [previous][next][first][last][top][bottom][index][help] */
 768 {
 769         int i, fd;
 770         struct socket *sock;
 771         struct proto_ops *ops;
 772 
 773         /* Locate the correct protocol family. */
 774         for (i = 0; i < NPROTO; ++i) 
 775         {
 776                 if (pops[i] == NULL) continue;
 777                 if (pops[i]->family == family) 
 778                         break;
 779         }
 780 
 781         if (i == NPROTO) 
 782         {
 783                 return -EINVAL;
 784         }
 785 
 786         ops = pops[i];
 787 
 788 /*
 789  *      Check that this is a type that we know how to manipulate and
 790  *      the protocol makes sense here. The family can still reject the
 791  *      protocol later.
 792  */
 793   
 794         if ((type != SOCK_STREAM && type != SOCK_DGRAM &&
 795                 type != SOCK_SEQPACKET && type != SOCK_RAW &&
 796                 type != SOCK_PACKET) || protocol < 0)
 797                         return(-EINVAL);
 798 
 799 /*
 800  *      Allocate the socket and allow the family to set things up. if
 801  *      the protocol is 0, the family is instructed to select an appropriate
 802  *      default.
 803  */
 804 
 805         if (!(sock = sock_alloc(1))) 
 806         {
 807                 printk("NET: sock_socket: no more sockets\n");
 808                 return(-ENOSR); /* Was: EAGAIN, but we are out of
 809                                    system resources! */
 810         }
 811 
 812         sock->type = type;
 813         sock->ops = ops;
 814         if ((i = sock->ops->create(sock, protocol)) < 0) 
 815         {
 816                 sock_release(sock);
 817                 return(i);
 818         }
 819 
 820         if ((fd = get_fd(SOCK_INODE(sock))) < 0) 
 821         {
 822                 sock_release(sock);
 823                 return(-EINVAL);
 824         }
 825 
 826         return(fd);
 827 }
 828 
 829 /*
 830  *      Create a pair of connected sockets.
 831  */
 832 
 833 static int sock_socketpair(int family, int type, int protocol, unsigned long usockvec[2])
     /* [previous][next][first][last][top][bottom][index][help] */
 834 {
 835         int fd1, fd2, i;
 836         struct socket *sock1, *sock2;
 837         int er;
 838 
 839         /*
 840          * Obtain the first socket and check if the underlying protocol
 841          * supports the socketpair call.
 842          */
 843 
 844         if ((fd1 = sock_socket(family, type, protocol)) < 0) 
 845                 return(fd1);
 846         sock1 = sockfd_lookup(fd1, NULL);
 847         if (!sock1->ops->socketpair) 
 848         {
 849                 sys_close(fd1);
 850                 return(-EINVAL);
 851         }
 852 
 853         /*
 854          *      Now grab another socket and try to connect the two together. 
 855          */
 856 
 857         if ((fd2 = sock_socket(family, type, protocol)) < 0) 
 858         {
 859                 sys_close(fd1);
 860                 return(-EINVAL);
 861         }
 862 
 863         sock2 = sockfd_lookup(fd2, NULL);
 864         if ((i = sock1->ops->socketpair(sock1, sock2)) < 0) 
 865         {
 866                 sys_close(fd1);
 867                 sys_close(fd2);
 868                 return(i);
 869         }
 870 
 871         sock1->conn = sock2;
 872         sock2->conn = sock1;
 873         sock1->state = SS_CONNECTED;
 874         sock2->state = SS_CONNECTED;
 875 
 876         er=verify_area(VERIFY_WRITE, usockvec, 2 * sizeof(int));
 877         if(er)
 878         {
 879                 sys_close(fd1);
 880                 sys_close(fd2);
 881                 return er;
 882         }
 883         put_fs_long(fd1, &usockvec[0]);
 884         put_fs_long(fd2, &usockvec[1]);
 885 
 886         return(0);
 887 }
 888 
 889 
 890 /*
 891  *      Bind a name to a socket. Nothing much to do here since its
 892  *      the protocol's responsibility to handle the local address.
 893  *
 894  *      We move the socket address to kernel space before we call
 895  *      the protocol layer (having also checked the address is ok).
 896  */
 897  
 898 static int sock_bind(int fd, struct sockaddr *umyaddr, int addrlen)
     /* [previous][next][first][last][top][bottom][index][help] */
 899 {
 900         struct socket *sock;
 901         int i;
 902         char address[MAX_SOCK_ADDR];
 903         int err;
 904 
 905         if (fd < 0 || fd >= NR_OPEN || current->files->fd[fd] == NULL)
 906                 return(-EBADF);
 907         
 908         if (!(sock = sockfd_lookup(fd, NULL))) 
 909                 return(-ENOTSOCK);
 910   
 911         if((err=move_addr_to_kernel(umyaddr,addrlen,address))<0)
 912                 return err;
 913   
 914         if ((i = sock->ops->bind(sock, (struct sockaddr *)address, addrlen)) < 0) 
 915         {
 916                 return(i);
 917         }
 918         return(0);
 919 }
 920 
 921 
 922 /*
 923  *      Perform a listen. Basically, we allow the protocol to do anything
 924  *      necessary for a listen, and if that works, we mark the socket as
 925  *      ready for listening.
 926  */
 927 
 928 static int sock_listen(int fd, int backlog)
     /* [previous][next][first][last][top][bottom][index][help] */
 929 {
 930         struct socket *sock;
 931 
 932         if (fd < 0 || fd >= NR_OPEN || current->files->fd[fd] == NULL)
 933                 return(-EBADF);
 934         if (!(sock = sockfd_lookup(fd, NULL))) 
 935                 return(-ENOTSOCK);
 936 
 937         if (sock->state != SS_UNCONNECTED) 
 938         {
 939                 return(-EINVAL);
 940         }
 941 
 942         if (sock->ops && sock->ops->listen)
 943                 sock->ops->listen(sock, backlog);
 944         sock->flags |= SO_ACCEPTCON;
 945         return(0);
 946 }
 947 
 948 
 949 /*
 950  *      For accept, we attempt to create a new socket, set up the link
 951  *      with the client, wake up the client, then return the new
 952  *      connected fd. We collect the address of the connector in kernel
 953  *      space and move it to user at the very end. This is buggy because
 954  *      we open the socket then return an error.
 955  */
 956 
 957 static int sock_accept(int fd, struct sockaddr *upeer_sockaddr, int *upeer_addrlen)
     /* [previous][next][first][last][top][bottom][index][help] */
 958 {
 959         struct file *file;
 960         struct socket *sock, *newsock;
 961         int i;
 962         char address[MAX_SOCK_ADDR];
 963         int len;
 964 
 965         if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
 966                 return(-EBADF);
 967         if (!(sock = sockfd_lookup(fd, &file))) 
 968                 return(-ENOTSOCK);
 969         if (sock->state != SS_UNCONNECTED) 
 970         {
 971                 return(-EINVAL);
 972         }
 973         if (!(sock->flags & SO_ACCEPTCON)) 
 974         {
 975                 return(-EINVAL);
 976         }
 977 
 978         if (!(newsock = sock_alloc(0))) 
 979         {
 980                 printk("NET: sock_accept: no more sockets\n");
 981                 return(-ENOSR); /* Was: EAGAIN, but we are out of system
 982                                    resources! */
 983         }
 984         newsock->type = sock->type;
 985         newsock->ops = sock->ops;
 986         if ((i = sock->ops->dup(newsock, sock)) < 0) 
 987         {
 988                 sock_release(newsock);
 989                 return(i);
 990         }
 991 
 992         i = newsock->ops->accept(sock, newsock, file->f_flags);
 993         if ( i < 0) 
 994         {
 995                 sock_release(newsock);
 996                 return(i);
 997         }
 998 
 999         if ((fd = get_fd(SOCK_INODE(newsock))) < 0) 
1000         {
1001                 sock_release(newsock);
1002                 return(-EINVAL);
1003         }
1004 
1005         if (upeer_sockaddr)
1006         {
1007                 newsock->ops->getname(newsock, (struct sockaddr *)address, &len, 1);
1008                 move_addr_to_user(address,len, upeer_sockaddr, upeer_addrlen);
1009         }
1010         return(fd);
1011 }
1012 
1013 
1014 /*
1015  *      Attempt to connect to a socket with the server address.  The address
1016  *      is in user space so we verify it is OK and move it to kernel space.
1017  */
1018  
1019 static int sock_connect(int fd, struct sockaddr *uservaddr, int addrlen)
     /* [previous][next][first][last][top][bottom][index][help] */
1020 {
1021         struct socket *sock;
1022         struct file *file;
1023         int i;
1024         char address[MAX_SOCK_ADDR];
1025         int err;
1026 
1027         if (fd < 0 || fd >= NR_OPEN || (file=current->files->fd[fd]) == NULL)
1028                 return(-EBADF);
1029         if (!(sock = sockfd_lookup(fd, &file)))
1030                 return(-ENOTSOCK);
1031 
1032         if((err=move_addr_to_kernel(uservaddr,addrlen,address))<0)
1033                 return err;
1034   
1035         switch(sock->state) 
1036         {
1037                 case SS_UNCONNECTED:
1038                         /* This is ok... continue with connect */
1039                         break;
1040                 case SS_CONNECTED:
1041                         /* Socket is already connected */
1042                         if(sock->type == SOCK_DGRAM) /* Hack for now - move this all into the protocol */
1043                                 break;
1044                         return -EISCONN;
1045                 case SS_CONNECTING:
1046                         /* Not yet connected... we will check this. */
1047                 
1048                         /*
1049                          *      FIXME:  for all protocols what happens if you start
1050                          *      an async connect fork and both children connect. Clean
1051                          *      this up in the protocols!
1052                          */
1053                         break;
1054                 default:
1055                         return(-EINVAL);
1056         }
1057         i = sock->ops->connect(sock, (struct sockaddr *)address, addrlen, file->f_flags);
1058         if (i < 0) 
1059         {
1060                 return(i);
1061         }
1062         return(0);
1063 }
1064 
1065 /*
1066  *      Get the local address ('name') of a socket object. Move the obtained
1067  *      name to user space.
1068  */
1069 
1070 static int sock_getsockname(int fd, struct sockaddr *usockaddr, int *usockaddr_len)
     /* [previous][next][first][last][top][bottom][index][help] */
1071 {
1072         struct socket *sock;
1073         char address[MAX_SOCK_ADDR];
1074         int len;
1075         int err;
1076         
1077         if (fd < 0 || fd >= NR_OPEN || current->files->fd[fd] == NULL)
1078                 return(-EBADF);
1079         if (!(sock = sockfd_lookup(fd, NULL)))
1080                 return(-ENOTSOCK);
1081 
1082         err=sock->ops->getname(sock, (struct sockaddr *)address, &len, 0);
1083         if(err)
1084                 return err;
1085         if((err=move_addr_to_user(address,len, usockaddr, usockaddr_len))<0)
1086                 return err;
1087         return 0;
1088 }
1089 
1090 /*
1091  *      Get the remote address ('name') of a socket object. Move the obtained
1092  *      name to user space.
1093  */
1094  
1095 static int sock_getpeername(int fd, struct sockaddr *usockaddr, int *usockaddr_len)
     /* [previous][next][first][last][top][bottom][index][help] */
1096 {
1097         struct socket *sock;
1098         char address[MAX_SOCK_ADDR];
1099         int len;
1100         int err;
1101 
1102         if (fd < 0 || fd >= NR_OPEN || current->files->fd[fd] == NULL)
1103                 return(-EBADF);
1104         if (!(sock = sockfd_lookup(fd, NULL)))
1105                 return(-ENOTSOCK);
1106 
1107         err=sock->ops->getname(sock, (struct sockaddr *)address, &len, 1);
1108         if(err)
1109                 return err;
1110         if((err=move_addr_to_user(address,len, usockaddr, usockaddr_len))<0)
1111                 return err;
1112         return 0;
1113 }
1114 
1115 /*
1116  *      Send a datagram down a socket. The datagram as with write() is
1117  *      in user space. We check it can be read.
1118  */
1119 
1120 static int sock_send(int fd, void * buff, int len, unsigned flags)
     /* [previous][next][first][last][top][bottom][index][help] */
1121 {
1122         struct socket *sock;
1123         struct file *file;
1124         int err;
1125 
1126         if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1127                 return(-EBADF);
1128         if (!(sock = sockfd_lookup(fd, NULL))) 
1129                 return(-ENOTSOCK);
1130 
1131         if(len<0)
1132                 return -EINVAL;
1133         err=verify_area(VERIFY_READ, buff, len);
1134         if(err)
1135                 return err;
1136         return(sock->ops->send(sock, buff, len, (file->f_flags & O_NONBLOCK), flags));
1137 }
1138 
1139 /*
1140  *      Send a datagram to a given address. We move the address into kernel
1141  *      space and check the user space data area is readable before invoking
1142  *      the protocol.
1143  */
1144 
1145 static int sock_sendto(int fd, void * buff, int len, unsigned flags,
     /* [previous][next][first][last][top][bottom][index][help] */
1146            struct sockaddr *addr, int addr_len)
1147 {
1148         struct socket *sock;
1149         struct file *file;
1150         char address[MAX_SOCK_ADDR];
1151         int err;
1152         
1153         if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1154                 return(-EBADF);
1155         if (!(sock = sockfd_lookup(fd, NULL)))
1156                 return(-ENOTSOCK);
1157 
1158         if(len<0)
1159                 return -EINVAL;
1160         err=verify_area(VERIFY_READ,buff,len);
1161         if(err)
1162                 return err;
1163         
1164         if((err=move_addr_to_kernel(addr,addr_len,address))<0)
1165                 return err;
1166 
1167         return(sock->ops->sendto(sock, buff, len, (file->f_flags & O_NONBLOCK),
1168                 flags, (struct sockaddr *)address, addr_len));
1169 }
1170 
1171 
1172 /*
1173  *      Receive a datagram from a socket. This isn't really right. The BSD manual
1174  *      pages explicitly state that recv is recvfrom with a NULL to argument. The
1175  *      Linux stack gets the right results for the wrong reason and this need to
1176  *      be tidied in the inet layer and removed from here.
1177  *      We check the buffer is writable and valid.
1178  */
1179 
1180 static int sock_recv(int fd, void * buff, int len, unsigned flags)
     /* [previous][next][first][last][top][bottom][index][help] */
1181 {
1182         struct socket *sock;
1183         struct file *file;
1184         int err;
1185 
1186         if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1187                 return(-EBADF);
1188 
1189         if (!(sock = sockfd_lookup(fd, NULL))) 
1190                 return(-ENOTSOCK);
1191                 
1192         if(len<0)
1193                 return -EINVAL;
1194         if(len==0)
1195                 return 0;
1196         err=verify_area(VERIFY_WRITE, buff, len);
1197         if(err)
1198                 return err;
1199 
1200         return(sock->ops->recv(sock, buff, len,(file->f_flags & O_NONBLOCK), flags));
1201 }
1202 
1203 /*
1204  *      Receive a frame from the socket and optionally record the address of the 
1205  *      sender. We verify the buffers are writable and if needed move the
1206  *      sender address from kernel to user space.
1207  */
1208 
1209 static int sock_recvfrom(int fd, void * buff, int len, unsigned flags,
     /* [previous][next][first][last][top][bottom][index][help] */
1210              struct sockaddr *addr, int *addr_len)
1211 {
1212         struct socket *sock;
1213         struct file *file;
1214         char address[MAX_SOCK_ADDR];
1215         int err;
1216         int alen;
1217         if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1218                 return(-EBADF);
1219         if (!(sock = sockfd_lookup(fd, NULL))) 
1220                 return(-ENOTSOCK);
1221         if(len<0)
1222                 return -EINVAL;
1223         if(len==0)
1224                 return 0;
1225 
1226         err=verify_area(VERIFY_WRITE,buff,len);
1227         if(err)
1228                 return err;
1229   
1230         len=sock->ops->recvfrom(sock, buff, len, (file->f_flags & O_NONBLOCK),
1231                      flags, (struct sockaddr *)address, &alen);
1232 
1233         if(len<0)
1234                 return len;
1235         if(addr!=NULL && (err=move_addr_to_user(address,alen, addr, addr_len))<0)
1236                 return err;
1237 
1238         return len;
1239 }
1240 
1241 /*
1242  *      Set a socket option. Because we don't know the option lengths we have
1243  *      to pass the user mode parameter for the protocols to sort out.
1244  */
1245  
1246 static int sock_setsockopt(int fd, int level, int optname, char *optval, int optlen)
     /* [previous][next][first][last][top][bottom][index][help] */
1247 {
1248         struct socket *sock;
1249         struct file *file;
1250         
1251         if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1252                 return(-EBADF);
1253         if (!(sock = sockfd_lookup(fd, NULL))) 
1254                 return(-ENOTSOCK);
1255 
1256         return(sock->ops->setsockopt(sock, level, optname, optval, optlen));
1257 }
1258 
1259 /*
1260  *      Get a socket option. Because we don't know the option lengths we have
1261  *      to pass a user mode parameter for the protocols to sort out.
1262  */
1263 
1264 static int sock_getsockopt(int fd, int level, int optname, char *optval, int *optlen)
     /* [previous][next][first][last][top][bottom][index][help] */
1265 {
1266         struct socket *sock;
1267         struct file *file;
1268 
1269         if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1270                 return(-EBADF);
1271         if (!(sock = sockfd_lookup(fd, NULL)))
1272                 return(-ENOTSOCK);
1273             
1274         if (!sock->ops || !sock->ops->getsockopt) 
1275                 return(0);
1276         return(sock->ops->getsockopt(sock, level, optname, optval, optlen));
1277 }
1278 
1279 
1280 /*
1281  *      Shutdown a socket.
1282  */
1283  
1284 static int sock_shutdown(int fd, int how)
     /* [previous][next][first][last][top][bottom][index][help] */
1285 {
1286         struct socket *sock;
1287         struct file *file;
1288 
1289         if (fd < 0 || fd >= NR_OPEN || ((file = current->files->fd[fd]) == NULL))
1290                 return(-EBADF);
1291         if (!(sock = sockfd_lookup(fd, NULL))) 
1292                 return(-ENOTSOCK);
1293 
1294         return(sock->ops->shutdown(sock, how));
1295 }
1296 
1297 
1298 /*
1299  *      Perform a file control on a socket file descriptor.
1300  */
1301 
1302 int sock_fcntl(struct file *filp, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1303 {
1304         struct socket *sock;
1305 
1306         sock = socki_lookup (filp->f_inode);
1307         if (sock != NULL && sock->ops != NULL && sock->ops->fcntl != NULL)
1308                 return(sock->ops->fcntl(sock, cmd, arg));
1309         return(-EINVAL);
1310 }
1311 
1312 
1313 /*
1314  *      System call vectors. Since I (RIB) want to rewrite sockets as streams,
1315  *      we have this level of indirection. Not a lot of overhead, since more of
1316  *      the work is done via read/write/select directly.
1317  *
1318  *      I'm now expanding this up to a higher level to separate the assorted
1319  *      kernel/user space manipulations and global assumptions from the protocol
1320  *      layers proper - AC.
1321  */
1322 
1323 asmlinkage int sys_socketcall(int call, unsigned long *args)
     /* [previous][next][first][last][top][bottom][index][help] */
1324 {
1325         int er;
1326         switch(call) 
1327         {
1328                 case SYS_SOCKET:
1329                         er=verify_area(VERIFY_READ, args, 3 * sizeof(long));
1330                         if(er)
1331                                 return er;
1332                         return(sock_socket(get_fs_long(args+0),
1333                                 get_fs_long(args+1),
1334                                 get_fs_long(args+2)));
1335                 case SYS_BIND:
1336                         er=verify_area(VERIFY_READ, args, 3 * sizeof(long));
1337                         if(er)
1338                                 return er;
1339                         return(sock_bind(get_fs_long(args+0),
1340                                 (struct sockaddr *)get_fs_long(args+1),
1341                                 get_fs_long(args+2)));
1342                 case SYS_CONNECT:
1343                         er=verify_area(VERIFY_READ, args, 3 * sizeof(long));
1344                         if(er)
1345                                 return er;
1346                         return(sock_connect(get_fs_long(args+0),
1347                                 (struct sockaddr *)get_fs_long(args+1),
1348                                 get_fs_long(args+2)));
1349                 case SYS_LISTEN:
1350                         er=verify_area(VERIFY_READ, args, 2 * sizeof(long));
1351                         if(er)
1352                                 return er;
1353                         return(sock_listen(get_fs_long(args+0),
1354                                 get_fs_long(args+1)));
1355                 case SYS_ACCEPT:
1356                         er=verify_area(VERIFY_READ, args, 3 * sizeof(long));
1357                         if(er)
1358                                 return er;
1359                         return(sock_accept(get_fs_long(args+0),
1360                                 (struct sockaddr *)get_fs_long(args+1),
1361                                 (int *)get_fs_long(args+2)));
1362                 case SYS_GETSOCKNAME:
1363                         er=verify_area(VERIFY_READ, args, 3 * sizeof(long));
1364                         if(er)
1365                                 return er;
1366                         return(sock_getsockname(get_fs_long(args+0),
1367                                 (struct sockaddr *)get_fs_long(args+1),
1368                                 (int *)get_fs_long(args+2)));
1369                 case SYS_GETPEERNAME:
1370                         er=verify_area(VERIFY_READ, args, 3 * sizeof(long));
1371                         if(er)
1372                                 return er;
1373                         return(sock_getpeername(get_fs_long(args+0),
1374                                 (struct sockaddr *)get_fs_long(args+1),
1375                                 (int *)get_fs_long(args+2)));
1376                 case SYS_SOCKETPAIR:
1377                         er=verify_area(VERIFY_READ, args, 4 * sizeof(long));
1378                         if(er)
1379                                 return er;
1380                         return(sock_socketpair(get_fs_long(args+0),
1381                                 get_fs_long(args+1),
1382                                 get_fs_long(args+2),
1383                                 (unsigned long *)get_fs_long(args+3)));
1384                 case SYS_SEND:
1385                         er=verify_area(VERIFY_READ, args, 4 * sizeof(unsigned long));
1386                         if(er)
1387                                 return er;
1388                         return(sock_send(get_fs_long(args+0),
1389                                 (void *)get_fs_long(args+1),
1390                                 get_fs_long(args+2),
1391                                 get_fs_long(args+3)));
1392                 case SYS_SENDTO:
1393                         er=verify_area(VERIFY_READ, args, 6 * sizeof(unsigned long));
1394                         if(er)
1395                                 return er;
1396                         return(sock_sendto(get_fs_long(args+0),
1397                                 (void *)get_fs_long(args+1),
1398                                 get_fs_long(args+2),
1399                                 get_fs_long(args+3),
1400                                 (struct sockaddr *)get_fs_long(args+4),
1401                                 get_fs_long(args+5)));
1402                 case SYS_RECV:
1403                         er=verify_area(VERIFY_READ, args, 4 * sizeof(unsigned long));
1404                         if(er)
1405                                 return er;
1406                         return(sock_recv(get_fs_long(args+0),
1407                                 (void *)get_fs_long(args+1),
1408                                 get_fs_long(args+2),
1409                                 get_fs_long(args+3)));
1410                 case SYS_RECVFROM:
1411                         er=verify_area(VERIFY_READ, args, 6 * sizeof(unsigned long));
1412                         if(er)
1413                                 return er;
1414                         return(sock_recvfrom(get_fs_long(args+0),
1415                                 (void *)get_fs_long(args+1),
1416                                 get_fs_long(args+2),
1417                                 get_fs_long(args+3),
1418                                 (struct sockaddr *)get_fs_long(args+4),
1419                                 (int *)get_fs_long(args+5)));
1420                 case SYS_SHUTDOWN:
1421                         er=verify_area(VERIFY_READ, args, 2* sizeof(unsigned long));
1422                         if(er)
1423                                 return er;
1424                         return(sock_shutdown(get_fs_long(args+0),
1425                                 get_fs_long(args+1)));
1426                 case SYS_SETSOCKOPT:
1427                         er=verify_area(VERIFY_READ, args, 5*sizeof(unsigned long));
1428                         if(er)
1429                                 return er;
1430                         return(sock_setsockopt(get_fs_long(args+0),
1431                                 get_fs_long(args+1),
1432                                 get_fs_long(args+2),
1433                                 (char *)get_fs_long(args+3),
1434                                 get_fs_long(args+4)));
1435                 case SYS_GETSOCKOPT:
1436                         er=verify_area(VERIFY_READ, args, 5*sizeof(unsigned long));
1437                         if(er)
1438                                 return er;
1439                         return(sock_getsockopt(get_fs_long(args+0),
1440                                 get_fs_long(args+1),
1441                                 get_fs_long(args+2),
1442                                 (char *)get_fs_long(args+3),
1443                                 (int *)get_fs_long(args+4)));
1444                 default:
1445                         return(-EINVAL);
1446         }
1447 }
1448 
1449 /*
1450  *      This function is called by a protocol handler that wants to
1451  *      advertise its address family, and have it linked into the
1452  *      SOCKET module.
1453  */
1454  
1455 int sock_register(int family, struct proto_ops *ops)
     /* [previous][next][first][last][top][bottom][index][help] */
1456 {
1457         int i;
1458 
1459         cli();
1460         for(i = 0; i < NPROTO; i++) 
1461         {
1462                 if (pops[i] != NULL) 
1463                         continue;
1464                 pops[i] = ops;
1465                 pops[i]->family = family;
1466                 sti();
1467                 return(i);
1468         }
1469         sti();
1470         return(-ENOMEM);
1471 }
1472 
1473 /*
1474  *      This function is called by a protocol handler that wants to
1475  *      remove its address family, and have it unlinked from the
1476  *      SOCKET module.
1477  */
1478  
1479 int sock_unregister(int family)
     /* [previous][next][first][last][top][bottom][index][help] */
1480 {
1481         int i;
1482 
1483         cli();
1484         for(i = 0; i < NPROTO; i++) 
1485         {
1486                 if (pops[i] == NULL) 
1487                         continue;
1488                 if(pops[i]->family == family)
1489                 {
1490                         pops[i]=NULL;
1491                         sti();
1492                         return(i);
1493                 }
1494         }
1495         sti();
1496         return(-ENOENT);
1497 }
1498 
1499 void proto_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1500 {
1501         extern struct net_proto protocols[];    /* Network protocols */
1502         struct net_proto *pro;
1503 
1504         /* Kick all configured protocols. */
1505         pro = protocols;
1506         while (pro->name != NULL) 
1507         {
1508                 (*pro->init_func)(pro);
1509                 pro++;
1510         }
1511         /* We're all done... */
1512 }
1513 
1514 
1515 void sock_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1516 {
1517         int i;
1518 
1519         printk("Swansea University Computer Society NET3.019\n");
1520 
1521         /*
1522          *      Initialize all address (protocol) families. 
1523          */
1524          
1525         for (i = 0; i < NPROTO; ++i) pops[i] = NULL;
1526 
1527         /*
1528          *      Initialize the protocols module. 
1529          */
1530 
1531         proto_init();
1532 
1533 #ifdef CONFIG_NET
1534         /* 
1535          *      Initialize the DEV module. 
1536          */
1537 
1538         dev_init();
1539   
1540         /*
1541          *      And the bottom half handler 
1542          */
1543 
1544         bh_base[NET_BH].routine= net_bh;
1545 #endif  
1546 }
1547 
1548 int socket_get_info(char *buffer, char **start, off_t offset, int length)
     /* [previous][next][first][last][top][bottom][index][help] */
1549 {
1550         int len = sprintf(buffer,
1551                           "sockets: used %d free %d alloced %d highlimit %d\n",
1552                           sockets_in_use, sockets_in_free,
1553                           nsockets, maxnsockets);
1554         if (offset >= len)
1555         {
1556                 *start = buffer;
1557                 return 0;
1558         }
1559         *start = buffer + offset;
1560         len -= offset;
1561         if (len > length)
1562                 len = length;
1563         return len;
1564 }

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