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

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

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