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

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