root/net/socket.c

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

DEFINITIONS

This source file includes following definitions.
  1. family_name
  2. get_fd
  3. toss_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_readdir
  13. sock_ioctl
  14. sock_select
  15. sock_close
  16. sock_awaitconn
  17. sock_socket
  18. sock_socketpair
  19. sock_bind
  20. sock_listen
  21. sock_accept
  22. sock_connect
  23. sock_getsockname
  24. sock_getpeername
  25. sys_send
  26. sys_sendto
  27. sys_recv
  28. sys_recvfrom
  29. sys_setsockopt
  30. sys_getsockopt
  31. sys_shutdown
  32. sock_fcntl
  33. sys_socketcall
  34. sock_init

   1 /* modified by Ross Biro to help support inet sockets. */
   2 #include <linux/signal.h>
   3 #include <linux/errno.h>
   4 #include <linux/sched.h>
   5 #include <linux/kernel.h>
   6 #include <linux/stat.h>
   7 #include <linux/socket.h>
   8 #include <linux/fcntl.h>
   9 #include <linux/termios.h>
  10 #include <linux/config.h>
  11 
  12 #include <asm/system.h>
  13 #include <asm/segment.h>
  14 
  15 #include "kern_sock.h"
  16 #include "socketcall.h"
  17 
  18 extern int sys_close(int fd);
  19 
  20 extern struct proto_ops unix_proto_ops;
  21 #ifdef CONFIG_TCPIP
  22 extern struct proto_ops inet_proto_ops;
  23 #endif
  24 
  25 static struct {
  26         short family;
  27         char *name;
  28         struct proto_ops *ops;
  29 } proto_table[] = {
  30         {AF_UNIX,       "AF_UNIX",      &unix_proto_ops},
  31 #ifdef CONFIG_TCPIP
  32         {AF_INET,       "AF_INET",      &inet_proto_ops},
  33 #endif
  34 };
  35 #define NPROTO (sizeof(proto_table) / sizeof(proto_table[0]))
  36 
  37 static char *
  38 family_name(int family)
     /* [previous][next][first][last][top][bottom][index][help] */
  39 {
  40         int i;
  41 
  42         for (i = 0; i < NPROTO; ++i)
  43                 if (proto_table[i].family == family)
  44                         return proto_table[i].name;
  45         return "UNKNOWN";
  46 }
  47 
  48 static int sock_lseek(struct inode *inode, struct file *file, off_t offset,
  49                       int whence);
  50 static int sock_read(struct inode *inode, struct file *file, char *buf,
  51                      int size);
  52 static int sock_write(struct inode *inode, struct file *file, char *buf,
  53                       int size);
  54 static int sock_readdir(struct inode *inode, struct file *file,
  55                         struct dirent *dirent, int count);
  56 static void sock_close(struct inode *inode, struct file *file);
  57 static int sock_select(struct inode *inode, struct file *file, int which, select_table *seltable);
  58 static int sock_ioctl(struct inode *inode, struct file *file,
  59                       unsigned int cmd, unsigned long arg);
  60 
  61 static struct file_operations socket_file_ops = {
  62         sock_lseek,
  63         sock_read,
  64         sock_write,
  65         sock_readdir,
  66         sock_select,
  67         sock_ioctl,
  68         NULL,           /* mmap */
  69         NULL,           /* no special open code... */
  70         sock_close
  71 };
  72 
  73 struct socket sockets[NSOCKETS];
  74 static struct wait_queue *socket_wait_free = NULL;
  75 
  76 /*
  77  * obtains the first available file descriptor and sets it up for use
  78  */
  79 static int
  80 get_fd(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  81 {
  82         int fd;
  83         struct file *file;
  84 
  85         /*
  86          * find a file descriptor suitable for return to the user.
  87          */
  88         file = get_empty_filp();
  89         if (!file)
  90                 return -1;
  91         for (fd = 0; fd < NR_OPEN; ++fd)
  92                 if (!current->filp[fd])
  93                         break;
  94         if (fd == NR_OPEN) {
  95                 file->f_count = 0;
  96                 return -1;
  97         }
  98         FD_CLR(fd, &current->close_on_exec);
  99         current->filp[fd] = file;
 100         file->f_op = &socket_file_ops;
 101         file->f_mode = 3;
 102         file->f_flags = 0;
 103         file->f_count = 1;
 104         file->f_inode = inode;
 105         if (inode)
 106                 inode->i_count++;
 107         file->f_pos = 0;
 108         return fd;
 109 }
 110 
 111 /*
 112  * reverses the action of get_fd() by releasing the file. it closes the
 113  * descriptor, but makes sure it does nothing more. called when an incomplete
 114  * socket must be closed, along with sock_release().
 115  */
 116 static inline void
 117 toss_fd(int fd)
     /* [previous][next][first][last][top][bottom][index][help] */
 118 {
 119   /* the count protects us from iput. */
 120         sys_close(fd);
 121 }
 122 
 123 struct socket *
 124 socki_lookup(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 125 {
 126         struct socket *sock;
 127 
 128         for (sock = sockets; sock <= last_socket; ++sock)
 129                 if (sock->state != SS_FREE && SOCK_INODE(sock) == inode)
 130                         return sock;
 131         return NULL;
 132 }
 133 
 134 static inline struct socket *
 135 sockfd_lookup(int fd, struct file **pfile)
     /* [previous][next][first][last][top][bottom][index][help] */
 136 {
 137         struct file *file;
 138 
 139         if (fd < 0 || fd >= NR_OPEN || !(file = current->filp[fd]))
 140                 return NULL;
 141         if (pfile)
 142                 *pfile = file;
 143         return socki_lookup(file->f_inode);
 144 }
 145 
 146 static struct socket *
 147 sock_alloc(int wait)
     /* [previous][next][first][last][top][bottom][index][help] */
 148 {
 149         struct socket *sock;
 150 
 151         while (1) {
 152                 cli();
 153                 for (sock = sockets; sock <= last_socket; ++sock)
 154                         if (sock->state == SS_FREE) {
 155                                 sock->state = SS_UNCONNECTED;
 156                                 sti();
 157                                 sock->flags = 0;
 158                                 sock->ops = NULL;
 159                                 sock->data = NULL;
 160                                 sock->conn = NULL;
 161                                 sock->iconn = NULL;
 162                                 /*
 163                                  * this really shouldn't be necessary, but
 164                                  * everything else depends on inodes, so we
 165                                  * grab it.
 166                                  * sleeps are also done on the i_wait member
 167                                  * of this inode.
 168                                  * the close system call will iput this inode
 169                                  * for us.
 170                                  */
 171                                 if (!(SOCK_INODE(sock) = get_empty_inode())) {
 172                                         printk("sock_alloc: no more inodes\n");
 173                                         sock->state = SS_FREE;
 174                                         return NULL;
 175                                 }
 176                                 SOCK_INODE(sock)->i_mode = S_IFSOCK;
 177                                 SOCK_INODE(sock)->i_uid = current->euid;
 178                                 SOCK_INODE(sock)->i_gid = current->egid;
 179 
 180                                 sock->wait = &SOCK_INODE(sock)->i_wait;
 181                                 PRINTK(("sock_alloc: socket 0x%x,inode 0x%x\n",
 182                                        sock, SOCK_INODE(sock)));
 183                                 return sock;
 184                         }
 185                 sti();
 186                 if (!wait)
 187                         return NULL;
 188                 PRINTK(("sock_alloc: no free sockets, sleeping...\n"));
 189                 interruptible_sleep_on(&socket_wait_free);
 190                 if (current->signal & ~current->blocked) {
 191                         PRINTK(("sock_alloc: sleep was interrupted\n"));
 192                         return NULL;
 193                 }
 194                 PRINTK(("sock_alloc: wakeup... trying again...\n"));
 195         }
 196 }
 197 
 198 static inline void
 199 sock_release_peer(struct socket *peer)
     /* [previous][next][first][last][top][bottom][index][help] */
 200 {
 201         peer->state = SS_DISCONNECTING;
 202         wake_up(peer->wait);
 203 }
 204 
 205 static void
 206 sock_release(struct socket *sock)
     /* [previous][next][first][last][top][bottom][index][help] */
 207 {
 208         int oldstate;
 209         struct socket *peersock, *nextsock;
 210 
 211         PRINTK(("sock_release: socket 0x%x, inode 0x%x\n", sock,
 212                SOCK_INODE(sock)));
 213         if ((oldstate = sock->state) != SS_UNCONNECTED)
 214                 sock->state = SS_DISCONNECTING;
 215         /*
 216          * wake up anyone waiting for connections
 217          */
 218         for (peersock = sock->iconn; peersock; peersock = nextsock) {
 219                 nextsock = peersock->next;
 220                 sock_release_peer(peersock);
 221         }
 222         /*
 223          * wake up anyone we're connected to. first, we release the
 224          * protocol, to give it a chance to flush data, etc.
 225          */
 226         peersock = (oldstate == SS_CONNECTED) ? sock->conn : NULL;
 227         if (sock->ops)
 228                 sock->ops->release(sock, peersock);
 229         if (peersock)
 230                 sock_release_peer(peersock);
 231         sock->state = SS_FREE;          /* this really releases us */
 232         wake_up(&socket_wait_free);
 233         iput(SOCK_INODE(sock)); /* we need to do this.  If sock alloc was
 234                                    called we already have an inode. */
 235 }
 236 
 237 static int
 238 sock_lseek(struct inode *inode, struct file *file, off_t offset, int whence)
     /* [previous][next][first][last][top][bottom][index][help] */
 239 {
 240         PRINTK(("sock_lseek: huh?\n"));
 241         return -ESPIPE;
 242 }
 243 
 244 static int
 245 sock_read(struct inode *inode, struct file *file, char *ubuf, int size)
     /* [previous][next][first][last][top][bottom][index][help] */
 246 {
 247         struct socket *sock;
 248 
 249         PRINTK(("sock_read: buf=0x%x, size=%d\n", ubuf, size));
 250         if (!(sock = socki_lookup(inode))) {
 251                 printk("sock_read: can't find socket for inode!\n");
 252                 return -EBADF;
 253         }
 254         if (sock->flags & SO_ACCEPTCON)
 255                 return -EINVAL;
 256         return sock->ops->read(sock, ubuf, size, (file->f_flags & O_NONBLOCK));
 257 }
 258 
 259 static int
 260 sock_write(struct inode *inode, struct file *file, char *ubuf, int size)
     /* [previous][next][first][last][top][bottom][index][help] */
 261 {
 262         struct socket *sock;
 263 
 264         PRINTK(("sock_write: buf=0x%x, size=%d\n", ubuf, size));
 265         if (!(sock = socki_lookup(inode))) {
 266                 printk("sock_write: can't find socket for inode!\n");
 267                 return -EBADF;
 268         }
 269         if (sock->flags & SO_ACCEPTCON)
 270                 return -EINVAL;
 271         return sock->ops->write(sock, ubuf, size,(file->f_flags & O_NONBLOCK));
 272 }
 273 
 274 static int
 275 sock_readdir(struct inode *inode, struct file *file, struct dirent *dirent,
     /* [previous][next][first][last][top][bottom][index][help] */
 276              int count)
 277 {
 278         PRINTK(("sock_readdir: huh?\n"));
 279         return -EBADF;
 280 }
 281 
 282 int
 283 sock_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
     /* [previous][next][first][last][top][bottom][index][help] */
 284            unsigned long arg)
 285 {
 286         struct socket *sock;
 287 
 288         PRINTK(("sock_ioctl: inode=0x%x cmd=0x%x arg=%d\n", inode, cmd, arg));
 289         if (!(sock = socki_lookup(inode))) {
 290                 printk("sock_ioctl: can't find socket for inode!\n");
 291                 return -EBADF;
 292         }
 293         return sock->ops->ioctl(sock, cmd, arg);
 294 }
 295 
 296 static int
 297 sock_select(struct inode *inode, struct file *file, int sel_type, select_table * wait)
     /* [previous][next][first][last][top][bottom][index][help] */
 298 {
 299         struct socket *sock;
 300 
 301         PRINTK(("sock_select: inode = 0x%x, kind = %s\n", inode,
 302                (sel_type == SEL_IN) ? "in" :
 303                (sel_type == SEL_OUT) ? "out" : "ex"));
 304         if (!(sock = socki_lookup(inode))) {
 305                 printk("sock_select: can't find socket for inode!\n");
 306                 return 0;
 307         }
 308         /*
 309          * we can't return errors to select, so its either yes or no.
 310          */
 311         if (sock->ops && sock->ops->select)
 312                 return sock->ops->select(sock, sel_type, wait);
 313         return 0;
 314 }
 315 
 316 void
 317 sock_close(struct inode *inode, struct file *file)
     /* [previous][next][first][last][top][bottom][index][help] */
 318 {
 319         struct socket *sock;
 320 
 321         PRINTK(("sock_close: inode=0x%x (cnt=%d)\n", inode, inode->i_count));
 322         /*
 323          * it's possible the inode is NULL if we're closing an unfinished
 324          * socket.
 325          */
 326         if (!inode)
 327                 return;
 328         if (!(sock = socki_lookup(inode))) {
 329                 printk("sock_close: can't find socket for inode!\n");
 330                 return;
 331         }
 332         sock_release(sock);
 333 }
 334 
 335 int
 336 sock_awaitconn(struct socket *mysock, struct socket *servsock)
     /* [previous][next][first][last][top][bottom][index][help] */
 337 {
 338         struct socket **last;
 339 
 340         PRINTK(("sock_awaitconn: trying to connect socket 0x%x to 0x%x\n",
 341                mysock, servsock));
 342         if (!(servsock->flags & SO_ACCEPTCON)) {
 343                 PRINTK(("sock_awaitconn: server not accepting connections\n"));
 344                 return -EINVAL;
 345         }
 346 
 347         /*
 348          * put ourselves on the server's incomplete connection queue.
 349          */
 350         mysock->next = NULL;
 351         cli();
 352         last = &servsock->iconn;
 353         while (*last)
 354                 last = &(*last)->next;
 355         *last = mysock;
 356         mysock->state = SS_CONNECTING;
 357         mysock->conn = servsock;
 358         sti();
 359 
 360         /*
 361          * wake up server, then await connection. server will set state to
 362          * SS_CONNECTED if we're connected.
 363          */
 364         wake_up(servsock->wait);
 365         if (mysock->state != SS_CONNECTED) {
 366                 interruptible_sleep_on(mysock->wait);
 367                 if (mysock->state != SS_CONNECTED &&
 368                     mysock->state != SS_DISCONNECTING) 
 369                   /* SS_DISCONNECTING means the server accepted
 370                    * the connection, maybe wrote some data, and then
 371                    * closed it again, all before we got to run.
 372                    * The only way a socket can get to SS_DISCONNECTING
 373                    * is through sock_release or sock_release_peer.
 374                    * The former is only called in case of various setup
 375                    * failures or sock_close (which obviously isn't the
 376                    * case as we still have it open); the latter is
 377                    * called when the server (to which we obviously
 378                    * must have been connected) closes its end.
 379                    */
 380                     {
 381                         /*
 382                          * if we're not connected we could have been
 383                          * 1) interrupted, so we need to remove ourselves
 384                          *    from the server list
 385                          * 2) rejected (mysock->conn == NULL), and have
 386                          *    already been removed from the list
 387                          */
 388                         if (mysock->conn == servsock) {
 389                                 cli();
 390                                 last = &servsock->iconn;
 391                                 while (*last) {
 392                                         if (*last == mysock) {
 393                                                 *last = mysock->next;
 394                                                 break;
 395                                         }
 396                                         last = &(*last)->next;
 397                                 }
 398                                 sti();
 399                         }
 400                         return mysock->conn ? -EINTR : -EACCES;
 401                 }
 402         }
 403         return 0;
 404 }
 405 
 406 /*
 407  * perform the socket system call. we locate the appropriate family, then
 408  * create a fresh socket.
 409  */
 410 static int
 411 sock_socket(int family, int type, int protocol)
     /* [previous][next][first][last][top][bottom][index][help] */
 412 {
 413         int i, fd;
 414         struct socket *sock;
 415         struct proto_ops *ops;
 416 
 417         PRINTK(("sys_socket: family = %d (%s), type = %d, protocol = %d\n",
 418                family, family_name(family), type, protocol));
 419 
 420         /*
 421          * locate the correct protocol family
 422          */
 423         for (i = 0; i < NPROTO; ++i)
 424                 if (proto_table[i].family == family)
 425                         break;
 426         if (i == NPROTO) {
 427                 PRINTK(("sys_socket: family not found\n"));
 428                 return -EINVAL;
 429         }
 430         ops = proto_table[i].ops;
 431 
 432         /*
 433          * check that this is a type that we know how to manipulate and
 434          * the protocol makes sense here. the family can still reject the
 435          * protocol later.
 436          */
 437         if ((type != SOCK_STREAM &&
 438              type != SOCK_DGRAM &&
 439              type != SOCK_SEQPACKET &&
 440              type != SOCK_RAW) ||
 441             protocol < 0)
 442                 return -EINVAL;
 443 
 444         /*
 445          * allocate the socket and allow the family to set things up. if
 446          * the protocol is 0, the family is instructed to select an appropriate
 447          * default.
 448          */
 449         if (!(sock = sock_alloc(1))) {
 450                 printk("sys_socket: no more sockets\n");
 451                 return -EAGAIN;
 452         }
 453         sock->type = type;
 454         sock->ops = ops;
 455         if ((i = sock->ops->create(sock, protocol)) < 0) {
 456                 sock_release(sock);
 457                 return i;
 458         }
 459 
 460         if ((fd = get_fd(SOCK_INODE(sock))) < 0) {
 461                 sock_release(sock);
 462                 return -EINVAL;
 463         }
 464 
 465         return fd;
 466 }
 467 
 468 static int
 469 sock_socketpair(int family, int type, int protocol, int usockvec[2])
     /* [previous][next][first][last][top][bottom][index][help] */
 470 {
 471         int fd1, fd2, i;
 472         struct socket *sock1, *sock2;
 473 
 474         PRINTK(("sys_socketpair: family = %d, type = %d, protocol = %d\n",
 475                family, type, protocol));
 476 
 477         /*
 478          * obtain the first socket and check if the underlying protocol
 479          * supports the socketpair call
 480          */
 481         if ((fd1 = sock_socket(family, type, protocol)) < 0)
 482                 return fd1;
 483         sock1 = sockfd_lookup(fd1, NULL);
 484         if (!sock1->ops->socketpair) {
 485                 sys_close(fd1);
 486                 return -EINVAL;
 487         }
 488 
 489         /*
 490          * now grab another socket and try to connect the two together
 491          */
 492         if ((fd2 = sock_socket(family, type, protocol)) < 0) {
 493                 sys_close(fd1);
 494                 return -EINVAL;
 495         }
 496         sock2 = sockfd_lookup(fd2, NULL);
 497         if ((i = sock1->ops->socketpair(sock1, sock2)) < 0) {
 498                 sys_close(fd1);
 499                 sys_close(fd2);
 500                 return i;
 501         }
 502         sock1->conn = sock2;
 503         sock2->conn = sock1;
 504         sock1->state = SS_CONNECTED;
 505         sock2->state = SS_CONNECTED;
 506 
 507         verify_area(VERIFY_WRITE,usockvec, 2 * sizeof(int));
 508         put_fs_long(fd1, &usockvec[0]);
 509         put_fs_long(fd2, &usockvec[1]);
 510 
 511         return 0;
 512 }
 513 
 514 /*
 515  * binds a name to a socket. nothing much to do here since its the
 516  * protocol's responsibility to handle the local address
 517  */
 518 static int
 519 sock_bind(int fd, struct sockaddr *umyaddr, int addrlen)
     /* [previous][next][first][last][top][bottom][index][help] */
 520 {
 521         struct socket *sock;
 522         int i;
 523 
 524         PRINTK(("sys_bind: fd = %d\n", fd));
 525         if (!(sock = sockfd_lookup(fd, NULL)))
 526                 return -EBADF;
 527         if ((i = sock->ops->bind(sock, umyaddr, addrlen)) < 0) {
 528                 PRINTK(("sys_bind: bind failed\n"));
 529                 return i;
 530         }
 531         return 0;
 532 }
 533 
 534 /*
 535  * perform a listen. basically, we allow the protocol to do anything
 536  * necessary for a listen, and if that works, we mark the socket as
 537  * ready for listening.
 538  */
 539 static int
 540 sock_listen(int fd, int backlog)
     /* [previous][next][first][last][top][bottom][index][help] */
 541 {
 542         struct socket *sock;
 543 
 544         PRINTK(("sys_listen: fd = %d\n", fd));
 545         if (!(sock = sockfd_lookup(fd, NULL)))
 546                 return -EBADF;
 547         if (sock->state != SS_UNCONNECTED) {
 548                 PRINTK(("sys_listen: socket isn't unconnected\n"));
 549                 return -EINVAL;
 550         }
 551         if (sock->flags & SO_ACCEPTCON) {
 552                 PRINTK(("sys_listen: socket already accepting connections!\n"));
 553                 return -EINVAL;
 554         }
 555         if (sock->ops && sock->ops->listen)
 556           sock->ops->listen (sock, backlog);
 557         sock->flags |= SO_ACCEPTCON;
 558         return 0;
 559 }
 560 
 561 /*
 562  * for accept, we attempt to create a new socket, set up the link with the
 563  * client, wake up the client, then return the new connected fd.
 564  */
 565 static int
 566 sock_accept(int fd, struct sockaddr *upeer_sockaddr, int *upeer_addrlen)
     /* [previous][next][first][last][top][bottom][index][help] */
 567 {
 568         struct file *file;
 569         struct socket *sock, *newsock;
 570         int i;
 571 
 572         PRINTK(("sys_accept: fd = %d\n", fd));
 573         if (!(sock = sockfd_lookup(fd, &file)))
 574                 return -EBADF;
 575         if (sock->state != SS_UNCONNECTED) {
 576                 PRINTK(("sys_accept: socket isn't unconnected\n"));
 577                 return -EINVAL;
 578         }
 579         if (!(sock->flags & SO_ACCEPTCON)) {
 580                 PRINTK(("sys_accept: socket not accepting connections!\n"));
 581                 return -EINVAL;
 582         }
 583 
 584         if (!(newsock = sock_alloc(0))) {
 585                 printk("sys_accept: no more sockets\n");
 586                 return -EAGAIN;
 587         }
 588         newsock->type = sock->type;
 589         newsock->ops = sock->ops;
 590         if ((i = sock->ops->dup(newsock, sock)) < 0) {
 591                 sock_release(newsock);
 592                 return i;
 593         }
 594 
 595         i = newsock->ops->accept(sock, newsock, file->f_flags);
 596 
 597         if ( i < 0)
 598           {
 599             sock_release(newsock);
 600             return (i);
 601           }
 602 
 603         if ((fd = get_fd(SOCK_INODE(newsock))) < 0) {
 604                 sock_release(newsock);
 605                 return -EINVAL;
 606         }
 607 
 608         PRINTK(("sys_accept: connected socket 0x%x via 0x%x\n",
 609                sock, newsock));
 610 
 611         if (upeer_sockaddr)
 612                 newsock->ops->getname(newsock, upeer_sockaddr,
 613                                       upeer_addrlen, 1);
 614 
 615         return fd;
 616 }
 617 
 618 /*
 619  * attempt to connect to a socket with the server address.
 620  */
 621 static int
 622 sock_connect(int fd, struct sockaddr *uservaddr, int addrlen)
     /* [previous][next][first][last][top][bottom][index][help] */
 623 {
 624         struct socket *sock;
 625         struct file *file;
 626         int i;
 627 
 628         PRINTK(("sys_connect: fd = %d\n", fd));
 629         if (!(sock = sockfd_lookup(fd, &file)))
 630                 return -EBADF;
 631         switch (sock->state) {
 632                 case SS_UNCONNECTED:
 633                         /* This is ok... continue with connect */
 634                         break;
 635                 case SS_CONNECTED:
 636                         /* Socket is already connected */
 637                         return -EISCONN;
 638                 case SS_CONNECTING:
 639                         /* Not yet connected... */
 640                         /* we will check this. */
 641                         return (sock->ops->connect(sock, uservaddr, addrlen, file->f_flags));
 642                 default:
 643                         PRINTK(("sys_connect: socket not unconnected\n"));
 644                         return -EINVAL;
 645         }
 646         i = sock->ops->connect(sock, uservaddr, addrlen, file->f_flags);
 647         if (i < 0) {
 648                 PRINTK(("sys_connect: connect failed\n"));
 649                 return i;
 650         }
 651         return 0;
 652 }
 653 
 654 static int
 655 sock_getsockname(int fd, struct sockaddr *usockaddr, int *usockaddr_len)
     /* [previous][next][first][last][top][bottom][index][help] */
 656 {
 657         struct socket *sock;
 658 
 659         PRINTK(("sys_getsockname: fd = %d\n", fd));
 660         if (!(sock = sockfd_lookup(fd, NULL)))
 661                 return -EBADF;
 662         return sock->ops->getname(sock, usockaddr, usockaddr_len, 0);
 663 }
 664 
 665 static int
 666 sock_getpeername(int fd, struct sockaddr *usockaddr, int *usockaddr_len)
     /* [previous][next][first][last][top][bottom][index][help] */
 667 {
 668         struct socket *sock;
 669 
 670         PRINTK(("sys_getpeername: fd = %d\n", fd));
 671         if (!(sock = sockfd_lookup(fd, NULL)))
 672                 return -EBADF;
 673         return sock->ops->getname(sock, usockaddr, usockaddr_len, 1);
 674 }
 675 
 676 
 677 /* send - shutdown added by bir7@leland.stanford.edu */
 678 
 679 static int
 680 sys_send( int fd, void * buff, int len, unsigned flags)
     /* [previous][next][first][last][top][bottom][index][help] */
 681 {
 682         struct socket *sock;
 683         struct file *file;
 684 
 685         PRINTK(("sys_send (fd = %d, buff = %X, len = %d, flags = %X)\n",
 686                fd, buff, len, flags));
 687 
 688         if (fd < 0 || fd >= NR_OPEN ||  ((file = current->filp[fd]) == NULL))
 689           return (-EBADF);
 690 
 691         if (!(sock = sockfd_lookup(fd, NULL)))
 692                 return (-ENOTSOCK);
 693 
 694         return (sock->ops->send (sock, buff, len, (file->f_flags & O_NONBLOCK),
 695                                  flags));
 696 
 697 }
 698 
 699 static int
 700 sys_sendto( int fd, void * buff, int len, unsigned flags,
     /* [previous][next][first][last][top][bottom][index][help] */
 701            struct sockaddr *addr, int addr_len)
 702 {
 703         struct socket *sock;
 704         struct file *file;
 705 
 706         PRINTK(("sys_sendto (fd = %d, buff = %X, len = %d, flags = %X,"
 707                " addr=%X, alen = %d\n", fd, buff, len, flags, addr, addr_len));
 708 
 709         if (fd < 0 || fd >= NR_OPEN ||  ((file = current->filp[fd]) == NULL))
 710           return (-EBADF);
 711 
 712         if (!(sock = sockfd_lookup(fd, NULL)))
 713                 return (-ENOTSOCK);
 714 
 715         return (sock->ops->sendto (sock, buff, len,
 716                                    (file->f_flags & O_NONBLOCK),
 717                                    flags, addr, addr_len));
 718 
 719 }
 720 
 721 
 722 static int
 723 sys_recv( int fd, void * buff, int len, unsigned flags)
     /* [previous][next][first][last][top][bottom][index][help] */
 724 {
 725         struct socket *sock;
 726         struct file *file;
 727 
 728         PRINTK(("sys_recv (fd = %d, buff = %X, len = %d, flags = %X)\n",
 729                fd, buff, len, flags));
 730 
 731         if (fd < 0 || fd >= NR_OPEN ||  ((file = current->filp[fd]) == NULL))
 732           return (-EBADF);
 733 
 734         if (!(sock = sockfd_lookup(fd, NULL)))
 735                 return (-ENOTSOCK);
 736 
 737         return (sock->ops->recv (sock, buff, len,(file->f_flags & O_NONBLOCK),
 738                                  flags));
 739 
 740 }
 741 
 742 static int
 743 sys_recvfrom( int fd, void * buff, int len, unsigned flags,
     /* [previous][next][first][last][top][bottom][index][help] */
 744              struct sockaddr *addr, int *addr_len)
 745 {
 746         struct socket *sock;
 747         struct file *file;
 748 
 749         PRINTK(("sys_recvfrom (fd = %d, buff = %X, len = %d, flags = %X,"
 750                " addr=%X, alen=%X\n", fd, buff, len, flags, addr, addr_len));
 751 
 752         if (fd < 0 || fd >= NR_OPEN ||  ((file = current->filp[fd]) == NULL))
 753           return (-EBADF);
 754 
 755         if (!(sock = sockfd_lookup(fd, NULL)))
 756                 return (-ENOTSOCK);
 757 
 758         return (sock->ops->recvfrom (sock, buff, len,
 759                                      (file->f_flags & O_NONBLOCK),
 760                                      flags, addr, addr_len));
 761 
 762 }
 763 
 764 
 765 static int
 766 sys_setsockopt (int fd, int level, int optname, char *optval, int optlen)
     /* [previous][next][first][last][top][bottom][index][help] */
 767 {
 768         struct socket *sock;
 769         struct file *file;
 770         
 771         PRINTK (("sys_setsockopt(fd=%d, level=%d, optname=%d,\n",fd, level,
 772                 optname));
 773         PRINTK (("               optval = %X, optlen = %d)\n", optval, optlen));
 774 
 775         if (fd < 0 || fd >= NR_OPEN ||  ((file = current->filp[fd]) == NULL))
 776           return (-EBADF);
 777 
 778         if (!(sock = sockfd_lookup(fd, NULL)))
 779                 return (-ENOTSOCK);
 780 
 781         return (sock->ops->setsockopt (sock, level, optname, optval, optlen));
 782 
 783 }
 784 
 785 static int
 786 sys_getsockopt (int fd, int level, int optname, char *optval, int *optlen)
     /* [previous][next][first][last][top][bottom][index][help] */
 787 {
 788         struct socket *sock;
 789         struct file *file;
 790         PRINTK (("sys_getsockopt(fd=%d, level=%d, optname=%d,\n",fd, level,
 791                 optname));
 792         PRINTK (("               optval = %X, optlen = %X)\n", optval, optlen));
 793 
 794         if (fd < 0 || fd >= NR_OPEN ||  ((file = current->filp[fd]) == NULL))
 795           return (-EBADF);
 796 
 797         if (!(sock = sockfd_lookup(fd, NULL)))
 798             return (-ENOTSOCK);
 799             
 800         if (!sock->ops || !sock->ops->getsockopt)
 801                 return 0;
 802         return sock->ops->getsockopt(sock, level, optname, optval, optlen);
 803 
 804 }
 805 
 806 
 807 static int
 808 sys_shutdown( int fd, int how)
     /* [previous][next][first][last][top][bottom][index][help] */
 809 {
 810         struct socket *sock;
 811         struct file *file;
 812 
 813         PRINTK(("sys_shutdown (fd = %d, how = %d)\n",fd, how));
 814 
 815         file = current->filp[fd];
 816         if (fd < 0 || fd >= NR_OPEN || file == NULL)
 817           return (-EBADF);
 818 
 819         if (!(sock = sockfd_lookup(fd, NULL)))
 820                 return (-ENOTSOCK);
 821 
 822         return (sock->ops->shutdown (sock, how));
 823 
 824 }
 825 
 826 int
 827 sock_fcntl(struct file *filp, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 828 {
 829    struct socket *sock;
 830    sock = socki_lookup (filp->f_inode);
 831    
 832    if (sock != NULL && sock->ops != NULL && sock->ops->fcntl != NULL)
 833      return (sock->ops->fcntl (sock, cmd, arg));
 834 
 835    return (-EINVAL);
 836 }
 837 
 838 
 839 /*
 840  * system call vectors. since i want to rewrite sockets as streams, we have
 841  * this level of indirection. not a lot of overhead, since more of the work is
 842  * done via read/write/select directly
 843  */
 844 int
 845 sys_socketcall(int call, unsigned long *args)
     /* [previous][next][first][last][top][bottom][index][help] */
 846 {
 847         switch (call) {
 848         case SYS_SOCKET:
 849                 verify_area(VERIFY_WRITE,args, 3 * sizeof(long));
 850                 return sock_socket(get_fs_long(args+0),
 851                                    get_fs_long(args+1),
 852                                    get_fs_long(args+2));
 853 
 854         case SYS_BIND:
 855                 verify_area(VERIFY_WRITE,args, 3 * sizeof(long));
 856                 return sock_bind(get_fs_long(args+0),
 857                                  (struct sockaddr *)get_fs_long(args+1),
 858                                  get_fs_long(args+2));
 859 
 860         case SYS_CONNECT:
 861                 verify_area(VERIFY_WRITE,args, 3 * sizeof(long));
 862                 return sock_connect(get_fs_long(args+0),
 863                                     (struct sockaddr *)get_fs_long(args+1),
 864                                     get_fs_long(args+2));
 865 
 866         case SYS_LISTEN:
 867                 verify_area(VERIFY_WRITE,args, 2 * sizeof(long));
 868                 return sock_listen(get_fs_long(args+0),
 869                                    get_fs_long(args+1));
 870 
 871         case SYS_ACCEPT:
 872                 verify_area(VERIFY_WRITE,args, 3 * sizeof(long));
 873                 return sock_accept(get_fs_long(args+0),
 874                                    (struct sockaddr *)get_fs_long(args+1),
 875                                    (int *)get_fs_long(args+2));
 876 
 877         case SYS_GETSOCKNAME:
 878                 verify_area(VERIFY_WRITE,args, 3 * sizeof(long));
 879                 return sock_getsockname(get_fs_long(args+0),
 880                                         (struct sockaddr *)get_fs_long(args+1),
 881                                         (int *)get_fs_long(args+2));
 882 
 883         case SYS_GETPEERNAME:
 884                 verify_area(VERIFY_WRITE,args, 3 * sizeof(long));
 885                 return sock_getpeername(get_fs_long(args+0),
 886                                         (struct sockaddr *)get_fs_long(args+1),
 887                                         (int *)get_fs_long(args+2));
 888 
 889         case SYS_SOCKETPAIR:
 890                 verify_area(VERIFY_WRITE,args, 4 * sizeof(long));
 891                 return sock_socketpair(get_fs_long(args+0),
 892                                        get_fs_long(args+1),
 893                                        get_fs_long(args+2),
 894                                        (int *)get_fs_long(args+3));
 895 
 896       case SYS_SEND:
 897           verify_area(VERIFY_WRITE,args, 4 * sizeof (unsigned long));
 898           return ( sys_send (get_fs_long(args+0),
 899                              (void *)get_fs_long(args+1),
 900                              get_fs_long(args+2),
 901                              get_fs_long(args+3)));
 902                              
 903       case SYS_SENDTO:
 904           verify_area(VERIFY_WRITE,args, 6 * sizeof (unsigned long));
 905           return ( sys_sendto (get_fs_long(args+0),
 906                              (void *)get_fs_long(args+1),
 907                              get_fs_long(args+2),
 908                              get_fs_long(args+3),
 909                              (struct sockaddr *)get_fs_long(args+4),
 910                              get_fs_long(args+5)));
 911 
 912     
 913       case SYS_RECV:
 914           verify_area(VERIFY_WRITE,args, 4 * sizeof (unsigned long));
 915           return ( sys_recv (get_fs_long(args+0),
 916                              (void *)get_fs_long(args+1),
 917                              get_fs_long(args+2),
 918                              get_fs_long(args+3)));
 919                              
 920       case SYS_RECVFROM:
 921           verify_area(VERIFY_WRITE,args, 6 * sizeof (unsigned long));
 922           return ( sys_recvfrom (get_fs_long(args+0),
 923                                  (void *)get_fs_long(args+1),
 924                                  get_fs_long(args+2),
 925                                  get_fs_long(args+3),
 926                                  (struct sockaddr *)get_fs_long(args+4),
 927                                  (int *)get_fs_long(args+5)));
 928 
 929       case SYS_SHUTDOWN:
 930           verify_area(VERIFY_WRITE, args, 2* sizeof (unsigned long));
 931           return ( sys_shutdown (get_fs_long (args+0),
 932                                  get_fs_long (args+1)));
 933 
 934       case SYS_SETSOCKOPT:
 935           verify_area(VERIFY_WRITE, args, 5*sizeof (unsigned long));
 936           return (sys_setsockopt (get_fs_long (args+0),
 937                                   get_fs_long (args+1),
 938                                   get_fs_long (args+2),
 939                                   (char *)get_fs_long (args+3),
 940                                   get_fs_long (args+4)));
 941 
 942 
 943       case SYS_GETSOCKOPT:
 944           verify_area(VERIFY_WRITE, args, 5*sizeof (unsigned long));
 945           return (sys_getsockopt (get_fs_long (args+0),
 946                                   get_fs_long (args+1),
 947                                   get_fs_long (args+2),
 948                                   (char *)get_fs_long (args+3),
 949                                   (int *)get_fs_long (args+4)));
 950 
 951         default:
 952                 return -EINVAL;
 953         }
 954 }
 955 
 956 void
 957 sock_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 958 {
 959         struct socket *sock;
 960         int i, ok;
 961 
 962         for (sock = sockets; sock <= last_socket; ++sock)
 963                 sock->state = SS_FREE;
 964         for (i = ok = 0; i < NPROTO; ++i) {
 965                 if ((*proto_table[i].ops->init)() < 0) {
 966                         printk("sock_init: init failed family %d (%s)\n",
 967                                proto_table[i].family,
 968                                proto_table[i].name);
 969                         proto_table[i].family = -1;
 970                 }
 971                 else
 972                         ++ok;
 973         }
 974         if (!ok)
 975                 printk("sock_init: warning: no protocols initialized\n");
 976         return;
 977 }
 978 

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