root/net/socket.c

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

DEFINITIONS

This source file includes following definitions.
  1. dprintf
  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. sock_send
  26. sock_sendto
  27. sock_recv
  28. sock_recvfrom
  29. sock_setsockopt
  30. sock_getsockopt
  31. sock_shutdown
  32. sock_fcntl
  33. sys_socketcall
  34. net_ioctl
  35. net_fioctl
  36. sock_register
  37. sock_init

   1 /*
   2  * NET          An implementation of the SOCKET network access protocol.
   3  *
   4  * Version:     @(#)socket.c    1.0.5   05/25/93
   5  *
   6  * Authors:     Orest Zborowski, <obz@Kodak.COM>
   7  *              Ross Biro, <bir7@leland.Stanford.Edu>
   8  *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
   9  *
  10  * Fixes:
  11  *              Anonymous       :       NOTSOCK/BADF cleanup. Error fix in
  12  *                                      shutdown()
  13  *              Alan Cox        :       verify_area() fixes
  14  *
  15  *
  16  *              This program is free software; you can redistribute it and/or
  17  *              modify it under the terms of the GNU General Public License
  18  *              as published by the Free Software Foundation; either version
  19  *              2 of the License, or (at your option) any later version.
  20  *
  21  *
  22  *      This module is effectively the top level interface to the BSD socket
  23  *      paradigm. Because it is very simple it works well for Unix domain sockets,
  24  *      but requires a whole layer of substructure for the other protocols.
  25  *
  26  *      In addition it lacks an effective kernel -> kernel interface to go with
  27  *      the user one.
  28  */
  29 
  30 #include <linux/config.h>
  31 #include <linux/signal.h>
  32 #include <linux/errno.h>
  33 #include <linux/sched.h>
  34 #include <linux/kernel.h>
  35 #include <linux/major.h>
  36 #include <linux/stat.h>
  37 #include <linux/socket.h>
  38 #include <linux/fcntl.h>
  39 #include <linux/net.h>
  40 #include <linux/ddi.h>
  41 
  42 #include <asm/system.h>
  43 #include <asm/segment.h>
  44 
  45 #undef SOCK_DEBUG
  46 
  47 #ifdef SOCK_DEBUG
  48 #include <stdarg.h>
  49 #define DPRINTF(x) dprintf x
  50 #else
  51 #define DPRINTF(x) /**/
  52 #endif
  53 
  54 static int sock_lseek(struct inode *inode, struct file *file, off_t offset,
  55                       int whence);
  56 static int sock_read(struct inode *inode, struct file *file, char *buf,
  57                      int size);
  58 static int sock_write(struct inode *inode, struct file *file, char *buf,
  59                       int size);
  60 static int sock_readdir(struct inode *inode, struct file *file,
  61                         struct dirent *dirent, int count);
  62 static void sock_close(struct inode *inode, struct file *file);
  63 static int sock_select(struct inode *inode, struct file *file, int which, select_table *seltable);
  64 static int sock_ioctl(struct inode *inode, struct file *file,
  65                       unsigned int cmd, unsigned long arg);
  66 
  67 
  68 static struct file_operations socket_file_ops = {
  69   sock_lseek,
  70   sock_read,
  71   sock_write,
  72   sock_readdir,
  73   sock_select,
  74   sock_ioctl,
  75   NULL,                 /* mmap */
  76   NULL,                 /* no special open code... */
  77   sock_close
  78 };
  79 
  80 static struct socket sockets[NSOCKETS];
  81 static struct wait_queue *socket_wait_free = NULL;
  82 static struct proto_ops *pops[NPROTO];
  83 static int net_debug = 0;
  84 
  85 #define last_socket     (sockets + NSOCKETS - 1)
  86 
  87 #ifdef SOCK_DEBUG
  88 /* Module debugging. */
  89 static void
  90 dprintf(int level, char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
  91 {
  92   char buff[1024];
  93   va_list args;
  94   extern int vsprintf(char * buf, const char * fmt, va_list args);
  95 
  96   if (level == 0) return;
  97   va_start(args, fmt);
  98   vsprintf(buff, fmt, args);
  99   va_end(args);
 100   printk(buff);
 101 }
 102 #endif
 103 
 104 /* Obtains the first available file descriptor and sets it up for use. */
 105 static int
 106 get_fd(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 107 {
 108   int fd;
 109   struct file *file;
 110 
 111   /* Find a file descriptor suitable for return to the user. */
 112   file = get_empty_filp();
 113   if (!file) return(-1);
 114   for (fd = 0; fd < NR_OPEN; ++fd)
 115         if (!current->filp[fd]) break;
 116   if (fd == NR_OPEN) {
 117         file->f_count = 0;
 118         return(-1);
 119   }
 120   FD_CLR(fd, &current->close_on_exec);
 121   current->filp[fd] = file;
 122   file->f_op = &socket_file_ops;
 123   file->f_mode = 3;
 124   file->f_flags = 0;
 125   file->f_count = 1;
 126   file->f_inode = inode;
 127   if (inode) inode->i_count++;
 128   file->f_pos = 0;
 129   return(fd);
 130 }
 131 
 132 
 133 /*
 134  * Reverses the action of get_fd() by releasing the file. it closes
 135  * the descriptor, but makes sure it does nothing more. Called when
 136  * an incomplete socket must be closed, along with sock_release().
 137  */
 138 static inline void
 139 toss_fd(int fd)
     /* [previous][next][first][last][top][bottom][index][help] */
 140 {
 141   sys_close(fd);                /* the count protects us from iput */
 142 }
 143 
 144 
 145 struct socket *
 146 socki_lookup(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 147 {
 148   struct socket *sock;
 149 
 150   if ((sock = inode->i_socket) != NULL) {
 151         if (sock->state != SS_FREE && SOCK_INODE(sock) == inode)
 152                 return sock;
 153         printk("socket.c: uhhuh. stale inode->i_socket pointer\n");
 154   }
 155   for (sock = sockets; sock <= last_socket; ++sock)
 156         if (sock->state != SS_FREE && SOCK_INODE(sock) == inode) {
 157                 printk("socket.c: uhhuh. Found socket despite no inode->i_socket pointer\n");
 158                 return(sock);
 159         }
 160   return(NULL);
 161 }
 162 
 163 
 164 static inline struct socket *
 165 sockfd_lookup(int fd, struct file **pfile)
     /* [previous][next][first][last][top][bottom][index][help] */
 166 {
 167   struct file *file;
 168 
 169   if (fd < 0 || fd >= NR_OPEN || !(file = current->filp[fd])) return(NULL);
 170   if (pfile) *pfile = file;
 171   return(socki_lookup(file->f_inode));
 172 }
 173 
 174 
 175 static struct socket *
 176 sock_alloc(int wait)
     /* [previous][next][first][last][top][bottom][index][help] */
 177 {
 178   struct socket *sock;
 179 
 180   while (1) {
 181         cli();
 182         for (sock = sockets; sock <= last_socket; ++sock) {
 183                 if (sock->state == SS_FREE) {
 184                         sock->state = SS_UNCONNECTED;
 185                         sti();
 186                         sock->flags = 0;
 187                         sock->ops = NULL;
 188                         sock->data = NULL;
 189                         sock->conn = NULL;
 190                         sock->iconn = NULL;
 191 
 192                         /*
 193                          * This really shouldn't be necessary, but everything
 194                          * else depends on inodes, so we grab it.
 195                          * Sleeps are also done on the i_wait member of this
 196                          * inode.  The close system call will iput this inode
 197                          * for us.
 198                          */
 199                         if (!(SOCK_INODE(sock) = get_empty_inode())) {
 200                                 printk("NET: sock_alloc: no more inodes\n");
 201                                 sock->state = SS_FREE;
 202                                 return(NULL);
 203                         }
 204                         SOCK_INODE(sock)->i_mode = S_IFSOCK;
 205                         SOCK_INODE(sock)->i_uid = current->euid;
 206                         SOCK_INODE(sock)->i_gid = current->egid;
 207                         SOCK_INODE(sock)->i_socket = sock;
 208 
 209                         sock->wait = &SOCK_INODE(sock)->i_wait;
 210                         DPRINTF((net_debug,
 211                                 "NET: sock_alloc: sk 0x%x, ino 0x%x\n",
 212                                                         sock, SOCK_INODE(sock)));
 213                         return(sock);
 214                 }
 215         }
 216         sti();
 217         if (!wait) return(NULL);
 218         DPRINTF((net_debug, "NET: sock_alloc: no free sockets, sleeping...\n"));
 219         interruptible_sleep_on(&socket_wait_free);
 220         if (current->signal & ~current->blocked) {
 221                 DPRINTF((net_debug, "NET: sock_alloc: sleep was interrupted\n"));
 222                 return(NULL);
 223         }
 224         DPRINTF((net_debug, "NET: sock_alloc: wakeup... trying again...\n"));
 225   }
 226 }
 227 
 228 
 229 static inline void
 230 sock_release_peer(struct socket *peer)
     /* [previous][next][first][last][top][bottom][index][help] */
 231 {
 232   peer->state = SS_DISCONNECTING;
 233   wake_up_interruptible(peer->wait);
 234 }
 235 
 236 
 237 static void
 238 sock_release(struct socket *sock)
     /* [previous][next][first][last][top][bottom][index][help] */
 239 {
 240   int oldstate;
 241   struct inode *inode;
 242   struct socket *peersock, *nextsock;
 243 
 244   DPRINTF((net_debug, "NET: sock_release: socket 0x%x, inode 0x%x\n",
 245                                                 sock, SOCK_INODE(sock)));
 246   if ((oldstate = sock->state) != SS_UNCONNECTED)
 247                         sock->state = SS_DISCONNECTING;
 248 
 249   /* Wake up anyone waiting for connections. */
 250   for (peersock = sock->iconn; peersock; peersock = nextsock) {
 251         nextsock = peersock->next;
 252         sock_release_peer(peersock);
 253   }
 254 
 255   /*
 256    * Wake up anyone we're connected to. First, we release the
 257    * protocol, to give it a chance to flush data, etc.
 258    */
 259   peersock = (oldstate == SS_CONNECTED) ? sock->conn : NULL;
 260   if (sock->ops) sock->ops->release(sock, peersock);
 261   if (peersock) sock_release_peer(peersock);
 262   inode = SOCK_INODE(sock);
 263   sock->state = SS_FREE;                /* this really releases us */
 264   wake_up_interruptible(&socket_wait_free);
 265 
 266   /* We need to do this. If sock alloc was called we already have an inode. */
 267   iput(inode);
 268 }
 269 
 270 
 271 static int
 272 sock_lseek(struct inode *inode, struct file *file, off_t offset, int whence)
     /* [previous][next][first][last][top][bottom][index][help] */
 273 {
 274   DPRINTF((net_debug, "NET: sock_lseek: huh?\n"));
 275   return(-ESPIPE);
 276 }
 277 
 278 
 279 static int
 280 sock_read(struct inode *inode, struct file *file, char *ubuf, int size)
     /* [previous][next][first][last][top][bottom][index][help] */
 281 {
 282   struct socket *sock;
 283 
 284   DPRINTF((net_debug, "NET: sock_read: buf=0x%x, size=%d\n", ubuf, size));
 285   if (!(sock = socki_lookup(inode))) {
 286         printk("NET: sock_read: can't find socket for inode!\n");
 287         return(-EBADF);
 288   }
 289   if (sock->flags & SO_ACCEPTCON) return(-EINVAL);
 290   return(sock->ops->read(sock, ubuf, size, (file->f_flags & O_NONBLOCK)));
 291 }
 292 
 293 
 294 static int
 295 sock_write(struct inode *inode, struct file *file, char *ubuf, int size)
     /* [previous][next][first][last][top][bottom][index][help] */
 296 {
 297   struct socket *sock;
 298 
 299   DPRINTF((net_debug, "NET: sock_write: buf=0x%x, size=%d\n", ubuf, size));
 300   if (!(sock = socki_lookup(inode))) {
 301         printk("NET: sock_write: can't find socket for inode!\n");
 302         return(-EBADF);
 303   }
 304   if (sock->flags & SO_ACCEPTCON) return(-EINVAL);
 305   return(sock->ops->write(sock, ubuf, size,(file->f_flags & O_NONBLOCK)));
 306 }
 307 
 308 
 309 static int
 310 sock_readdir(struct inode *inode, struct file *file, struct dirent *dirent,
     /* [previous][next][first][last][top][bottom][index][help] */
 311              int count)
 312 {
 313   DPRINTF((net_debug, "NET: sock_readdir: huh?\n"));
 314   return(-EBADF);
 315 }
 316 
 317 
 318 int
 319 sock_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
     /* [previous][next][first][last][top][bottom][index][help] */
 320            unsigned long arg)
 321 {
 322   struct socket *sock;
 323 
 324   DPRINTF((net_debug, "NET: sock_ioctl: inode=0x%x cmd=0x%x arg=%d\n",
 325                                                         inode, cmd, arg));
 326   if (!(sock = socki_lookup(inode))) {
 327         printk("NET: sock_ioctl: can't find socket for inode!\n");
 328         return(-EBADF);
 329   }
 330   return(sock->ops->ioctl(sock, cmd, arg));
 331 }
 332 
 333 
 334 static int
 335 sock_select(struct inode *inode, struct file *file, int sel_type, select_table * wait)
     /* [previous][next][first][last][top][bottom][index][help] */
 336 {
 337   struct socket *sock;
 338 
 339   DPRINTF((net_debug, "NET: sock_select: inode = 0x%x, kind = %s\n", inode,
 340        (sel_type == SEL_IN) ? "in" :
 341        (sel_type == SEL_OUT) ? "out" : "ex"));
 342   if (!(sock = socki_lookup(inode))) {
 343         printk("NET: sock_select: can't find socket for inode!\n");
 344         return(0);
 345   }
 346 
 347   /* We can't return errors to select, so its either yes or no. */
 348   if (sock->ops && sock->ops->select)
 349         return(sock->ops->select(sock, sel_type, wait));
 350   return(0);
 351 }
 352 
 353 
 354 void
 355 sock_close(struct inode *inode, struct file *file)
     /* [previous][next][first][last][top][bottom][index][help] */
 356 {
 357   struct socket *sock;
 358 
 359   DPRINTF((net_debug, "NET: sock_close: inode=0x%x (cnt=%d)\n",
 360                                                 inode, inode->i_count));
 361 
 362   /* It's possible the inode is NULL if we're closing an unfinished socket. */
 363   if (!inode) return;
 364   if (!(sock = socki_lookup(inode))) {
 365         printk("NET: sock_close: can't find socket for inode!\n");
 366         return;
 367   }
 368   sock_release(sock);
 369 }
 370 
 371 
 372 int
 373 sock_awaitconn(struct socket *mysock, struct socket *servsock)
     /* [previous][next][first][last][top][bottom][index][help] */
 374 {
 375   struct socket *last;
 376 
 377   DPRINTF((net_debug,
 378         "NET: sock_awaitconn: trying to connect socket 0x%x to 0x%x\n",
 379                                                         mysock, servsock));
 380   if (!(servsock->flags & SO_ACCEPTCON)) {
 381         DPRINTF((net_debug,
 382                 "NET: sock_awaitconn: server not accepting connections\n"));
 383         return(-EINVAL);
 384   }
 385 
 386   /* Put ourselves on the server's incomplete connection queue. */
 387   mysock->next = NULL;
 388   cli();
 389   if (!(last = servsock->iconn)) servsock->iconn = mysock;
 390     else {
 391         while (last->next) last = last->next;
 392         last->next = mysock;
 393   }
 394   mysock->state = SS_CONNECTING;
 395   mysock->conn = servsock;
 396   sti();
 397 
 398   /*
 399    * Wake up server, then await connection. server will set state to
 400    * SS_CONNECTED if we're connected.
 401    */
 402   wake_up_interruptible(servsock->wait);
 403   if (mysock->state != SS_CONNECTED) {
 404         interruptible_sleep_on(mysock->wait);
 405         if (mysock->state != SS_CONNECTED &&
 406             mysock->state != SS_DISCONNECTING) {
 407                 /*
 408                  * if we're not connected we could have been
 409                  * 1) interrupted, so we need to remove ourselves
 410                  *    from the server list
 411                  * 2) rejected (mysock->conn == NULL), and have
 412                  *    already been removed from the list
 413                  */
 414                 if (mysock->conn == servsock) {
 415                         cli();
 416                         if ((last = servsock->iconn) == mysock)
 417                                         servsock->iconn = mysock->next;
 418                         else {
 419                                 while (last->next != mysock) last = last->next;
 420                                 last->next = mysock->next;
 421                         }
 422                         sti();
 423                 }
 424                 return(mysock->conn ? -EINTR : -EACCES);
 425         }
 426   }
 427   return(0);
 428 }
 429 
 430 
 431 /*
 432  * Perform the socket system call. we locate the appropriate
 433  * family, then create a fresh socket.
 434  */
 435 static int
 436 sock_socket(int family, int type, int protocol)
     /* [previous][next][first][last][top][bottom][index][help] */
 437 {
 438   int i, fd;
 439   struct socket *sock;
 440   struct proto_ops *ops;
 441 
 442   DPRINTF((net_debug,
 443         "NET: sock_socket: family = %d, type = %d, protocol = %d\n",
 444                                                 family, type, protocol));
 445 
 446   /* Locate the correct protocol family. */
 447   for (i = 0; i < NPROTO; ++i) {
 448         if (pops[i] == NULL) continue;
 449         if (pops[i]->family == family) break;
 450   }
 451   if (i == NPROTO) {
 452         DPRINTF((net_debug, "NET: sock_socket: family not found\n"));
 453         return(-EINVAL);
 454   }
 455   ops = pops[i];
 456 
 457   /*
 458    * Check that this is a type that we know how to manipulate and
 459    * the protocol makes sense here. The family can still reject the
 460    * protocol later.
 461    */
 462   if ((type != SOCK_STREAM && type != SOCK_DGRAM &&
 463        type != SOCK_SEQPACKET && type != SOCK_RAW &&
 464        type != SOCK_PACKET) || protocol < 0)
 465                                                         return(-EINVAL);
 466 
 467   /*
 468    * allocate the socket and allow the family to set things up. if
 469    * the protocol is 0, the family is instructed to select an appropriate
 470    * default.
 471    */
 472   if (!(sock = sock_alloc(1))) {
 473         printk("sock_socket: no more sockets\n");
 474         return(-EAGAIN);
 475   }
 476   sock->type = type;
 477   sock->ops = ops;
 478   if ((i = sock->ops->create(sock, protocol)) < 0) {
 479         sock_release(sock);
 480         return(i);
 481   }
 482 
 483   if ((fd = get_fd(SOCK_INODE(sock))) < 0) {
 484         sock_release(sock);
 485         return(-EINVAL);
 486   }
 487 
 488   return(fd);
 489 }
 490 
 491 
 492 static int
 493 sock_socketpair(int family, int type, int protocol, unsigned long usockvec[2])
     /* [previous][next][first][last][top][bottom][index][help] */
 494 {
 495   int fd1, fd2, i;
 496   struct socket *sock1, *sock2;
 497   int er;
 498 
 499   DPRINTF((net_debug,
 500         "NET: sock_socketpair: family = %d, type = %d, protocol = %d\n",
 501                                                         family, type, protocol));
 502 
 503   /*
 504    * Obtain the first socket and check if the underlying protocol
 505    * supports the socketpair call.
 506    */
 507   if ((fd1 = sock_socket(family, type, protocol)) < 0) return(fd1);
 508   sock1 = sockfd_lookup(fd1, NULL);
 509   if (!sock1->ops->socketpair) {
 510         sys_close(fd1);
 511         return(-EINVAL);
 512   }
 513 
 514   /* Now grab another socket and try to connect the two together. */
 515   if ((fd2 = sock_socket(family, type, protocol)) < 0) {
 516         sys_close(fd1);
 517         return(-EINVAL);
 518   }
 519   sock2 = sockfd_lookup(fd2, NULL);
 520   if ((i = sock1->ops->socketpair(sock1, sock2)) < 0) {
 521         sys_close(fd1);
 522         sys_close(fd2);
 523         return(i);
 524   }
 525   sock1->conn = sock2;
 526   sock2->conn = sock1;
 527   sock1->state = SS_CONNECTED;
 528   sock2->state = SS_CONNECTED;
 529 
 530   er=verify_area(VERIFY_WRITE, usockvec, 2 * sizeof(int));
 531   if(er)
 532         return er;
 533   put_fs_long(fd1, &usockvec[0]);
 534   put_fs_long(fd2, &usockvec[1]);
 535 
 536   return(0);
 537 }
 538 
 539 
 540 /*
 541  * Bind a name to a socket. Nothing much to do here since its
 542  * the protocol's responsibility to handle the local address.
 543  */
 544 static int
 545 sock_bind(int fd, struct sockaddr *umyaddr, int addrlen)
     /* [previous][next][first][last][top][bottom][index][help] */
 546 {
 547   struct socket *sock;
 548   int i;
 549 
 550   DPRINTF((net_debug, "NET: sock_bind: fd = %d\n", fd));
 551   if (fd < 0 || fd >= NR_OPEN || current->filp[fd] == NULL)
 552                                                                 return(-EBADF);
 553   if (!(sock = sockfd_lookup(fd, NULL))) return(-ENOTSOCK);
 554   if ((i = sock->ops->bind(sock, umyaddr, addrlen)) < 0) {
 555         DPRINTF((net_debug, "NET: sock_bind: bind failed\n"));
 556         return(i);
 557   }
 558   return(0);
 559 }
 560 
 561 
 562 /*
 563  * Perform a listen. Basically, we allow the protocol to do anything
 564  * necessary for a listen, and if that works, we mark the socket as
 565  * ready for listening.
 566  */
 567 static int
 568 sock_listen(int fd, int backlog)
     /* [previous][next][first][last][top][bottom][index][help] */
 569 {
 570   struct socket *sock;
 571 
 572   DPRINTF((net_debug, "NET: sock_listen: fd = %d\n", fd));
 573   if (fd < 0 || fd >= NR_OPEN || current->filp[fd] == NULL)
 574                                                                 return(-EBADF);
 575   if (!(sock = sockfd_lookup(fd, NULL))) return(-ENOTSOCK);
 576   if (sock->state != SS_UNCONNECTED) {
 577         DPRINTF((net_debug, "NET: sock_listen: socket isn't unconnected\n"));
 578         return(-EINVAL);
 579   }
 580   if (sock->ops && sock->ops->listen) sock->ops->listen(sock, backlog);
 581   sock->flags |= SO_ACCEPTCON;
 582   return(0);
 583 }
 584 
 585 
 586 /*
 587  * For accept, we attempt to create a new socket, set up the link
 588  * with the client, wake up the client, then return the new
 589  * connected fd.
 590  */
 591 static int
 592 sock_accept(int fd, struct sockaddr *upeer_sockaddr, int *upeer_addrlen)
     /* [previous][next][first][last][top][bottom][index][help] */
 593 {
 594   struct file *file;
 595   struct socket *sock, *newsock;
 596   int i;
 597 
 598   DPRINTF((net_debug, "NET: sock_accept: fd = %d\n", fd));
 599   if (fd < 0 || fd >= NR_OPEN || ((file = current->filp[fd]) == NULL))
 600                                                                 return(-EBADF);
 601   
 602   if (!(sock = sockfd_lookup(fd, &file))) return(-ENOTSOCK);
 603   if (sock->state != SS_UNCONNECTED) {
 604         DPRINTF((net_debug, "NET: sock_accept: socket isn't unconnected\n"));
 605         return(-EINVAL);
 606   }
 607   if (!(sock->flags & SO_ACCEPTCON)) {
 608         DPRINTF((net_debug,
 609                 "NET: sock_accept: socket not accepting connections!\n"));
 610         return(-EINVAL);
 611   }
 612 
 613   if (!(newsock = sock_alloc(0))) {
 614         printk("NET: sock_accept: no more sockets\n");
 615         return(-EAGAIN);
 616   }
 617   newsock->type = sock->type;
 618   newsock->ops = sock->ops;
 619   if ((i = sock->ops->dup(newsock, sock)) < 0) {
 620         sock_release(newsock);
 621         return(i);
 622   }
 623 
 624   i = newsock->ops->accept(sock, newsock, file->f_flags);
 625   if ( i < 0) {
 626         sock_release(newsock);
 627         return(i);
 628   }
 629 
 630   if ((fd = get_fd(SOCK_INODE(newsock))) < 0) {
 631         sock_release(newsock);
 632         return(-EINVAL);
 633   }
 634 
 635   DPRINTF((net_debug, "NET: sock_accept: connected socket 0x%x via 0x%x\n",
 636                                                         sock, newsock));
 637 
 638   if (upeer_sockaddr)
 639         newsock->ops->getname(newsock, upeer_sockaddr, upeer_addrlen, 1);
 640 
 641   return(fd);
 642 }
 643 
 644 
 645 /* Attempt to connect to a socket with the server address. */
 646 static int
 647 sock_connect(int fd, struct sockaddr *uservaddr, int addrlen)
     /* [previous][next][first][last][top][bottom][index][help] */
 648 {
 649   struct socket *sock;
 650   struct file *file;
 651   int i;
 652 
 653   DPRINTF((net_debug, "NET: sock_connect: fd = %d\n", fd));
 654   if (fd < 0 || fd >= NR_OPEN || (file=current->filp[fd]) == NULL)
 655                                                                 return(-EBADF);
 656   
 657   if (!(sock = sockfd_lookup(fd, &file))) return(-ENOTSOCK);
 658   switch(sock->state) {
 659         case SS_UNCONNECTED:
 660                 /* This is ok... continue with connect */
 661                 break;
 662         case SS_CONNECTED:
 663                 /* Socket is already connected */
 664                 return -EISCONN;
 665         case SS_CONNECTING:
 666                 /* Not yet connected... we will check this. */
 667                 return(sock->ops->connect(sock, uservaddr,
 668                                           addrlen, file->f_flags));
 669         default:
 670                 DPRINTF((net_debug,
 671                         "NET: sock_connect: socket not unconnected\n"));
 672                 return(-EINVAL);
 673   }
 674   i = sock->ops->connect(sock, uservaddr, addrlen, file->f_flags);
 675   if (i < 0) {
 676         DPRINTF((net_debug, "NET: sock_connect: connect failed\n"));
 677         return(i);
 678   }
 679   return(0);
 680 }
 681 
 682 
 683 static int
 684 sock_getsockname(int fd, struct sockaddr *usockaddr, int *usockaddr_len)
     /* [previous][next][first][last][top][bottom][index][help] */
 685 {
 686   struct socket *sock;
 687 
 688   DPRINTF((net_debug, "NET: sock_getsockname: fd = %d\n", fd));
 689   if (fd < 0 || fd >= NR_OPEN || current->filp[fd] == NULL)
 690                                                                 return(-EBADF);
 691   if (!(sock = sockfd_lookup(fd, NULL))) return(-ENOTSOCK);
 692   return(sock->ops->getname(sock, usockaddr, usockaddr_len, 0));
 693 }
 694 
 695 
 696 static int
 697 sock_getpeername(int fd, struct sockaddr *usockaddr, int *usockaddr_len)
     /* [previous][next][first][last][top][bottom][index][help] */
 698 {
 699   struct socket *sock;
 700 
 701   DPRINTF((net_debug, "NET: sock_getpeername: fd = %d\n", fd));
 702   if (fd < 0 || fd >= NR_OPEN || current->filp[fd] == NULL)
 703                         return(-EBADF);
 704   if (!(sock = sockfd_lookup(fd, NULL))) return(-ENOTSOCK);
 705   return(sock->ops->getname(sock, usockaddr, usockaddr_len, 1));
 706 }
 707 
 708 
 709 static int
 710 sock_send(int fd, void * buff, int len, unsigned flags)
     /* [previous][next][first][last][top][bottom][index][help] */
 711 {
 712   struct socket *sock;
 713   struct file *file;
 714 
 715   DPRINTF((net_debug,
 716         "NET: sock_send(fd = %d, buff = %X, len = %d, flags = %X)\n",
 717                                                         fd, buff, len, flags));
 718 
 719   if (fd < 0 || fd >= NR_OPEN || ((file = current->filp[fd]) == NULL))
 720                                                                 return(-EBADF);
 721   if (!(sock = sockfd_lookup(fd, NULL))) return(-ENOTSOCK);
 722 
 723   return(sock->ops->send(sock, buff, len, (file->f_flags & O_NONBLOCK), flags));
 724 }
 725 
 726 
 727 static int
 728 sock_sendto(int fd, void * buff, int len, unsigned flags,
     /* [previous][next][first][last][top][bottom][index][help] */
 729            struct sockaddr *addr, int addr_len)
 730 {
 731   struct socket *sock;
 732   struct file *file;
 733 
 734   DPRINTF((net_debug,
 735         "NET: sock_sendto(fd = %d, buff = %X, len = %d, flags = %X,"
 736          " addr=%X, alen = %d\n", fd, buff, len, flags, addr, addr_len));
 737 
 738   if (fd < 0 || fd >= NR_OPEN || ((file = current->filp[fd]) == NULL))
 739                                                                 return(-EBADF);
 740   if (!(sock = sockfd_lookup(fd, NULL))) return(-ENOTSOCK);
 741 
 742   return(sock->ops->sendto(sock, buff, len, (file->f_flags & O_NONBLOCK),
 743                            flags, addr, addr_len));
 744 }
 745 
 746 
 747 static int
 748 sock_recv(int fd, void * buff, int len, unsigned flags)
     /* [previous][next][first][last][top][bottom][index][help] */
 749 {
 750   struct socket *sock;
 751   struct file *file;
 752 
 753   DPRINTF((net_debug,
 754         "NET: sock_recv(fd = %d, buff = %X, len = %d, flags = %X)\n",
 755                                                         fd, buff, len, flags));
 756 
 757   if (fd < 0 || fd >= NR_OPEN || ((file = current->filp[fd]) == NULL))
 758                                                                 return(-EBADF);
 759   if (!(sock = sockfd_lookup(fd, NULL))) return(-ENOTSOCK);
 760 
 761   return(sock->ops->recv(sock, buff, len,(file->f_flags & O_NONBLOCK), flags));
 762 }
 763 
 764 
 765 static int
 766 sock_recvfrom(int fd, void * buff, int len, unsigned flags,
     /* [previous][next][first][last][top][bottom][index][help] */
 767              struct sockaddr *addr, int *addr_len)
 768 {
 769   struct socket *sock;
 770   struct file *file;
 771 
 772   DPRINTF((net_debug,
 773         "NET: sock_recvfrom(fd = %d, buff = %X, len = %d, flags = %X,"
 774         " addr=%X, alen=%X\n", fd, buff, len, flags, addr, addr_len));
 775 
 776   if (fd < 0 || fd >= NR_OPEN || ((file = current->filp[fd]) == NULL))
 777                                                                 return(-EBADF);
 778   if (!(sock = sockfd_lookup(fd, NULL))) return(-ENOTSOCK);
 779 
 780   return(sock->ops->recvfrom(sock, buff, len, (file->f_flags & O_NONBLOCK),
 781                              flags, addr, addr_len));
 782 }
 783 
 784 
 785 static int
 786 sock_setsockopt(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         
 791   DPRINTF((net_debug, "NET: sock_setsockopt(fd=%d, level=%d, optname=%d,\n",
 792                                                         fd, level, optname));
 793   DPRINTF((net_debug, "                     optval = %X, optlen = %d)\n",
 794                                                         optval, optlen));
 795 
 796   if (fd < 0 || fd >= NR_OPEN || ((file = current->filp[fd]) == NULL))
 797                                                                 return(-EBADF);
 798   if (!(sock = sockfd_lookup(fd, NULL))) return(-ENOTSOCK);
 799 
 800   return(sock->ops->setsockopt(sock, level, optname, optval, optlen));
 801 }
 802 
 803 
 804 static int
 805 sock_getsockopt(int fd, int level, int optname, char *optval, int *optlen)
     /* [previous][next][first][last][top][bottom][index][help] */
 806 {
 807   struct socket *sock;
 808   struct file *file;
 809 
 810   DPRINTF((net_debug, "NET: sock_getsockopt(fd=%d, level=%d, optname=%d,\n",
 811                                                 fd, level, optname));
 812   DPRINTF((net_debug, "                     optval = %X, optlen = %X)\n",
 813                                                 optval, optlen));
 814 
 815   if (fd < 0 || fd >= NR_OPEN || ((file = current->filp[fd]) == NULL))
 816                                                                 return(-EBADF);
 817   if (!(sock = sockfd_lookup(fd, NULL))) return(-ENOTSOCK);
 818             
 819   if (!sock->ops || !sock->ops->getsockopt) return(0);
 820   return(sock->ops->getsockopt(sock, level, optname, optval, optlen));
 821 }
 822 
 823 
 824 static int
 825 sock_shutdown(int fd, int how)
     /* [previous][next][first][last][top][bottom][index][help] */
 826 {
 827   struct socket *sock;
 828   struct file *file;
 829 
 830   DPRINTF((net_debug, "NET: sock_shutdown(fd = %d, how = %d)\n", fd, how));
 831 
 832   if (fd < 0 || fd >= NR_OPEN || ((file = current->filp[fd]) == NULL))
 833                                                                 return(-EBADF);
 834 
 835   if (!(sock = sockfd_lookup(fd, NULL))) return(-ENOTSOCK);
 836 
 837   return(sock->ops->shutdown(sock, how));
 838 }
 839 
 840 
 841 int
 842 sock_fcntl(struct file *filp, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 843 {
 844   struct socket *sock;
 845 
 846   sock = socki_lookup (filp->f_inode);
 847   if (sock != NULL && sock->ops != NULL && sock->ops->fcntl != NULL)
 848                                 return(sock->ops->fcntl(sock, cmd, arg));
 849   return(-EINVAL);
 850 }
 851 
 852 
 853 /*
 854  * System call vectors. Since I (RIB) want to rewrite sockets as streams,
 855  * we have this level of indirection. Not a lot of overhead, since more of
 856  * the work is done via read/write/select directly.
 857  */
 858 asmlinkage int
 859 sys_socketcall(int call, unsigned long *args)
     /* [previous][next][first][last][top][bottom][index][help] */
 860 {
 861   int er;
 862   switch(call) {
 863         case SYS_SOCKET:
 864                 er=verify_area(VERIFY_READ, args, 3 * sizeof(long));
 865                 if(er)
 866                         return er;
 867                 return(sock_socket(get_fs_long(args+0),
 868                                    get_fs_long(args+1),
 869                                    get_fs_long(args+2)));
 870         case SYS_BIND:
 871                 er=verify_area(VERIFY_READ, args, 3 * sizeof(long));
 872                 if(er)
 873                         return er;
 874                 return(sock_bind(get_fs_long(args+0),
 875                                  (struct sockaddr *)get_fs_long(args+1),
 876                                  get_fs_long(args+2)));
 877         case SYS_CONNECT:
 878                 er=verify_area(VERIFY_READ, args, 3 * sizeof(long));
 879                 if(er)
 880                         return er;
 881                 return(sock_connect(get_fs_long(args+0),
 882                                     (struct sockaddr *)get_fs_long(args+1),
 883                                     get_fs_long(args+2)));
 884         case SYS_LISTEN:
 885                 er=verify_area(VERIFY_READ, args, 2 * sizeof(long));
 886                 if(er)
 887                         return er;
 888                 return(sock_listen(get_fs_long(args+0),
 889                                    get_fs_long(args+1)));
 890         case SYS_ACCEPT:
 891                 er=verify_area(VERIFY_READ, args, 3 * sizeof(long));
 892                 if(er)
 893                         return er;
 894                 return(sock_accept(get_fs_long(args+0),
 895                                    (struct sockaddr *)get_fs_long(args+1),
 896                                    (int *)get_fs_long(args+2)));
 897         case SYS_GETSOCKNAME:
 898                 er=verify_area(VERIFY_READ, args, 3 * sizeof(long));
 899                 if(er)
 900                         return er;
 901                 return(sock_getsockname(get_fs_long(args+0),
 902                                         (struct sockaddr *)get_fs_long(args+1),
 903                                         (int *)get_fs_long(args+2)));
 904         case SYS_GETPEERNAME:
 905                 er=verify_area(VERIFY_READ, args, 3 * sizeof(long));
 906                 if(er)
 907                         return er;
 908                 return(sock_getpeername(get_fs_long(args+0),
 909                                         (struct sockaddr *)get_fs_long(args+1),
 910                                         (int *)get_fs_long(args+2)));
 911         case SYS_SOCKETPAIR:
 912                 er=verify_area(VERIFY_READ, args, 4 * sizeof(long));
 913                 if(er)
 914                         return er;
 915                 return(sock_socketpair(get_fs_long(args+0),
 916                                        get_fs_long(args+1),
 917                                        get_fs_long(args+2),
 918                                        (unsigned long *)get_fs_long(args+3)));
 919         case SYS_SEND:
 920                 er=verify_area(VERIFY_READ, args, 4 * sizeof(unsigned long));
 921                 if(er)
 922                         return er;
 923                 return(sock_send(get_fs_long(args+0),
 924                                  (void *)get_fs_long(args+1),
 925                                  get_fs_long(args+2),
 926                                  get_fs_long(args+3)));
 927         case SYS_SENDTO:
 928                 er=verify_area(VERIFY_READ, args, 6 * sizeof(unsigned long));
 929                 if(er)
 930                         return er;
 931                 return(sock_sendto(get_fs_long(args+0),
 932                                    (void *)get_fs_long(args+1),
 933                                    get_fs_long(args+2),
 934                                    get_fs_long(args+3),
 935                                    (struct sockaddr *)get_fs_long(args+4),
 936                                    get_fs_long(args+5)));
 937         case SYS_RECV:
 938                 er=verify_area(VERIFY_READ, args, 4 * sizeof(unsigned long));
 939                 if(er)
 940                         return er;
 941                 return(sock_recv(get_fs_long(args+0),
 942                                  (void *)get_fs_long(args+1),
 943                                  get_fs_long(args+2),
 944                                  get_fs_long(args+3)));
 945         case SYS_RECVFROM:
 946                 er=verify_area(VERIFY_READ, args, 6 * sizeof(unsigned long));
 947                 if(er)
 948                         return er;
 949                 return(sock_recvfrom(get_fs_long(args+0),
 950                                      (void *)get_fs_long(args+1),
 951                                      get_fs_long(args+2),
 952                                      get_fs_long(args+3),
 953                                      (struct sockaddr *)get_fs_long(args+4),
 954                                      (int *)get_fs_long(args+5)));
 955         case SYS_SHUTDOWN:
 956                 er=verify_area(VERIFY_READ, args, 2* sizeof(unsigned long));
 957                 if(er)
 958                         return er;
 959                 return(sock_shutdown(get_fs_long(args+0),
 960                                      get_fs_long(args+1)));
 961         case SYS_SETSOCKOPT:
 962                 er=verify_area(VERIFY_READ, args, 5*sizeof(unsigned long));
 963                 if(er)
 964                         return er;
 965                 return(sock_setsockopt(get_fs_long(args+0),
 966                                        get_fs_long(args+1),
 967                                        get_fs_long(args+2),
 968                                        (char *)get_fs_long(args+3),
 969                                        get_fs_long(args+4)));
 970         case SYS_GETSOCKOPT:
 971                 er=verify_area(VERIFY_READ, args, 5*sizeof(unsigned long));
 972                 if(er)
 973                         return er;
 974                 return(sock_getsockopt(get_fs_long(args+0),
 975                                        get_fs_long(args+1),
 976                                        get_fs_long(args+2),
 977                                        (char *)get_fs_long(args+3),
 978                                        (int *)get_fs_long(args+4)));
 979         default:
 980                 return(-EINVAL);
 981   }
 982 }
 983 
 984 
 985 static int
 986 net_ioctl(unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 987 {
 988   int er;
 989   switch(cmd) {
 990         case DDIOCSDBG:
 991                 er=verify_area(VERIFY_READ, (void *)arg, sizeof(long));
 992                 if(er)
 993                         return er;
 994                 net_debug = get_fs_long((long *)arg);
 995                 if (net_debug != 0 && net_debug != 1) {
 996                         net_debug = 0;
 997                         return(-EINVAL);
 998                 }
 999                 return(0);
1000         default:
1001                 return(-EINVAL);
1002   }
1003   /*NOTREACHED*/
1004   return(0);
1005 }
1006 
1007 
1008 /*
1009  * Handle the IOCTL system call for the NET devices.  This basically
1010  * means I/O control for the SOCKET layer (future expansions could be
1011  * a variable number of socket table entries, et al), and for the more
1012  * general protocols like ARP.  The latter currently lives in the INET
1013  * module, so we have to get ugly a tiny little bit.  Later... -FvK
1014  */
1015 static int
1016 net_fioctl(struct inode *inode, struct file *file,
     /* [previous][next][first][last][top][bottom][index][help] */
1017            unsigned int cmd, unsigned long arg)
1018 {
1019   extern int arp_ioctl(unsigned int, void *);
1020 
1021   /* Dispatch on the minor device. */
1022   switch(MINOR(inode->i_rdev)) {
1023         case 0:         /* NET (SOCKET) */
1024                 DPRINTF((net_debug, "NET: SOCKET level I/O control request.\n"));
1025                 return(net_ioctl(cmd, arg));
1026 #ifdef CONFIG_INET
1027         case 1:         /* ARP */
1028                 DPRINTF((net_debug, "NET: ARP level I/O control request.\n"));
1029                 return(arp_ioctl(cmd, (void *) arg));
1030 #endif
1031         default:
1032                 return(-ENODEV);
1033   }
1034   /*NOTREACHED*/
1035   return(-EINVAL);
1036 }
1037 
1038 
1039 static struct file_operations net_fops = {
1040   NULL,         /* LSEEK        */
1041   NULL,         /* READ         */
1042   NULL,         /* WRITE        */
1043   NULL,         /* READDIR      */
1044   NULL,         /* SELECT       */
1045   net_fioctl,   /* IOCTL        */
1046   NULL,         /* MMAP         */
1047   NULL,         /* OPEN         */
1048   NULL          /* CLOSE        */
1049 };
1050 
1051 
1052 /*
1053  * This function is called by a protocol handler that wants to
1054  * advertise its address family, and have it linked into the
1055  * SOCKET module.
1056  */
1057 int
1058 sock_register(int family, struct proto_ops *ops)
     /* [previous][next][first][last][top][bottom][index][help] */
1059 {
1060   int i;
1061 
1062   cli();
1063   for(i = 0; i < NPROTO; i++) {
1064         if (pops[i] != NULL) continue;
1065         pops[i] = ops;
1066         pops[i]->family = family;
1067         sti();
1068         DPRINTF((net_debug, "NET: Installed protocol %d in slot %d (0x%X)\n",
1069                                                 family, i, (long)ops));
1070         return(i);
1071   }
1072   sti();
1073   return(-ENOMEM);
1074 }
1075 
1076 
1077 void
1078 sock_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1079 {
1080   struct socket *sock;
1081   int i;
1082 
1083   /* Set up our SOCKET VFS major device. */
1084   if (register_chrdev(SOCKET_MAJOR, "socket", &net_fops) < 0) {
1085         printk("NET: cannot register major device %d!\n", SOCKET_MAJOR);
1086         return;
1087   }
1088 
1089   /* Release all sockets. */
1090   for (sock = sockets; sock <= last_socket; ++sock) sock->state = SS_FREE;
1091 
1092   /* Initialize all address (protocol) families. */
1093   for (i = 0; i < NPROTO; ++i) pops[i] = NULL;
1094 
1095   /* Initialize the DDI module. */
1096   ddi_init();
1097 
1098   /* Initialize the ARP module. */
1099 #if 0
1100   arp_init();
1101 #endif
1102 }

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