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

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