root/net/socket.c

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

DEFINITIONS

This source file includes following definitions.
  1. move_addr_to_kernel
  2. move_addr_to_user
  3. get_fd
  4. toss_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

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

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