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. socki_lookup
  5. sockfd_lookup
  6. sock_alloc
  7. sock_release_peer
  8. sock_release
  9. sock_lseek
  10. sock_read
  11. sock_write
  12. sock_ioctl
  13. sock_select
  14. sock_close
  15. sock_fasync
  16. sock_wake_async
  17. sys_socket
  18. sys_socketpair
  19. sys_bind
  20. sys_listen
  21. sys_accept
  22. sys_connect
  23. sys_getsockname
  24. sys_getpeername
  25. sys_send
  26. sys_sendto
  27. sys_recv
  28. sys_recvfrom
  29. sys_setsockopt
  30. sys_getsockopt
  31. sys_shutdown
  32. sys_sendmsg
  33. sys_recvmsg
  34. sock_fcntl
  35. sys_socketcall
  36. sock_register
  37. sock_unregister
  38. proto_init
  39. sock_init
  40. socket_get_info

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

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