root/net/inet/sockinet.c

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

DEFINITIONS

This source file includes following definitions.
  1. sk_inuse
  2. get_new_socknum
  3. put_sock
  4. remove_sock
  5. destroy_sock
  6. inet_fcntl
  7. inet_setsockopt
  8. inet_getsockopt
  9. inet_listen
  10. def_callback1
  11. def_callback2
  12. inet_create
  13. inet_dup
  14. inet_release
  15. inet_bind
  16. inet_connect
  17. inet_socketpair
  18. inet_accept
  19. inet_getname
  20. inet_read
  21. inet_recv
  22. inet_write
  23. inet_send
  24. inet_sendto
  25. inet_recvfrom
  26. inet_shutdown
  27. inet_select
  28. inet_ioctl
  29. get_sock
  30. release_sock
  31. inet_fioctl
  32. inet_proto_init

   1 /*
   2  * INET         An implementation of the TCP/IP protocol suite for the LINUX
   3  *              operating system.  INET is implemented using the  BSD Socket
   4  *              interface as the means of communication with the user level.
   5  *
   6  *              SOCK - AF_INET protocol family socket handler.
   7  *
   8  * Version:     @(#)sock.c      1.28    24/12/93
   9  *
  10  * Authors:     Ross Biro, <bir7@leland.Stanford.Edu>
  11  *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12  *              Florian La Roche, <flla@stud.uni-sb.de>
  13  *
  14  * Fixes:
  15  *              Alan Cox        :       Numerous verify_area() problems
  16  *              Alan Cox        :       Connecting on a connecting socket
  17  *                                      now returns an error for tcp.
  18  *              Alan Cox        :       sock->protocol is set correctly.
  19  *                                      and is not sometimes left as 0.
  20  *              Alan Cox        :       connect handles icmp errors on a
  21  *                                      connect properly. Unfortunately there
  22  *                                      is a restart syscall nasty there. I
  23  *                                      can't match BSD without hacking the C
  24  *                                      library. Ideas urgently sought!
  25  *              Alan Cox        :       Disallow bind() to addresses that are
  26  *                                      not ours - especially broadcast ones!!
  27  *              Alan Cox        :       Socket 1024 _IS_ ok for users. (fencepost)
  28  *              Alan Cox        :       sock_wfree/sock_rfree don't destroy sockets,
  29  *                                      instead they leave that for the DESTROY timer.
  30  *              Alan Cox        :       Clean up error flag in accept
  31  *              Alan Cox        :       TCP ack handling is buggy, the DESTROY timer
  32  *                                      was buggy. Put a remove_sock() in the handler
  33  *                                      for memory when we hit 0. Also altered the timer
  34  *                                      code. The ACK stuff can wait and needs major 
  35  *                                      TCP layer surgery.
  36  *              Alan Cox        :       Fixed TCP ack bug, removed remove sock
  37  *                                      and fixed timer/inet_bh race.
  38  *              Alan Cox        :       Added zapped flag for TCP
  39  *              Alan Cox        :       Move kfree_skb into skbuff.c and tidied up surplus code
  40  *              Alan Cox        :       for new sk_buff allocations wmalloc/rmalloc now call alloc_skb
  41  *              Alan Cox        :       kfree_s calls now are kfree_skbmem so we can track skb resources
  42  *              Alan Cox        :       Supports socket option broadcast now as does udp. Packet and raw need fixing.
  43  *              Alan Cox        :       Added RCVBUF,SNDBUF size setting. It suddenely occured to me how easy it was so...
  44  *              Rick Sladkey    :       Relaxed UDP rules for matching packets.
  45  *              C.E.Hawkins     :       IFF_PROMISC/SIOCGHWADDR support
  46  *      Pauline Middelink       :       Pidentd support
  47  *              Alan Cox        :       Fixed connect() taking signals I think.
  48  *              Alan Cox        :       SO_LINGER supported
  49  *              Alan Cox        :       Error reporting fixes
  50  *              Anonymous       :       inet_create tidied up (sk->reuse setting)
  51  *              Alan Cox        :       Tidy up for release.
  52  *              Alan Cox        :       Moved this to sockinet.c and removed generic code.
  53  *              Alan Cox        :       inet sockets don't set sk->type!
  54  *
  55  *
  56  * To Fix:
  57  *
  58  *
  59  *              This program is free software; you can redistribute it and/or
  60  *              modify it under the terms of the GNU General Public License
  61  *              as published by the Free Software Foundation; either version
  62  *              2 of the License, or (at your option) any later version.
  63  */
  64 
  65 #include <linux/config.h>
  66 #include <linux/errno.h>
  67 #include <linux/types.h>
  68 #include <linux/socket.h>
  69 #include <linux/in.h>
  70 #include <linux/kernel.h>
  71 #include <linux/major.h>
  72 #include <linux/sched.h>
  73 #include <linux/timer.h>
  74 #include <linux/string.h>
  75 #include <linux/sockios.h>
  76 #include <linux/net.h>
  77 #include <linux/fcntl.h>
  78 #include <linux/mm.h>
  79 #include <linux/interrupt.h>
  80 
  81 #include <asm/segment.h>
  82 #include <asm/system.h>
  83 
  84 #include "inet.h"
  85 #include "devinet.h"
  86 #include "ip.h"
  87 #include "protocol.h"
  88 #include "arp.h"
  89 #include "route.h"
  90 #include "tcp.h"
  91 #include "udp.h"
  92 #include "skbuff.h"
  93 #include "sockinet.h"
  94 #include "raw.h"
  95 #include "icmp.h"
  96 
  97 
  98 int inet_debug = DBG_OFF;               /* INET module debug flag       */
  99 
 100 
 101 #define min(a,b)        ((a)<(b)?(a):(b))
 102 
 103 extern struct proto packet_prot;
 104 
 105 
 106 
 107 static int sk_inuse(struct proto *prot, int num)
     /* [previous][next][first][last][top][bottom][index][help] */
 108 {
 109         struct sock *sk;
 110 
 111         for(sk = prot->sock_array[num & (SOCK_ARRAY_SIZE -1 )];sk != NULL;sk=sk->next) 
 112         {
 113                 if (sk->num == num) return(1);
 114         }
 115         return(0);
 116 }
 117 
 118 
 119 unsigned short get_new_socknum(struct proto *prot, unsigned short base)
     /* [previous][next][first][last][top][bottom][index][help] */
 120 {
 121         static int start=0;
 122 
 123         /*
 124          * Used to cycle through the port numbers so the
 125          * chances of a confused connection drop.
 126          */
 127         int i, j;
 128         int best = 0;
 129         int size = 32767; /* a big num. */
 130         struct sock *sk;
 131 
 132         if (base == 0) 
 133                 base = PROT_SOCK+1+(start % 1024);
 134         if (base <= PROT_SOCK) 
 135         {
 136                 base += PROT_SOCK+(start % 1024);
 137         }
 138 
 139         /* Now look through the entire array and try to find an empty ptr. */
 140         for(i=0; i < SOCK_ARRAY_SIZE; i++) 
 141         {
 142                 j = 0;
 143                 sk = prot->sock_array[(i+base+1) &(SOCK_ARRAY_SIZE -1)];
 144                 while(sk != NULL) 
 145                 {
 146                         sk = sk->next;
 147                         j++;
 148                 }
 149                 if (j == 0) 
 150                 {
 151                         start =(i+1+start )%1024;
 152                         DPRINTF((DBG_INET, "get_new_socknum returning %d, start = %d\n",
 153                                                         i + base + 1, start));
 154                         return(i+base+1);
 155                 }
 156                 if (j < size) 
 157                 {
 158                         best = i;
 159                         size = j;
 160                 }
 161         }
 162 
 163         /* Now make sure the one we want is not in use. */
 164         while(sk_inuse(prot, base +best+1)) 
 165         {
 166                 best += SOCK_ARRAY_SIZE;
 167         }
 168         DPRINTF((DBG_INET, "get_new_socknum returning %d, start = %d\n",
 169                                                 best + base + 1, start));
 170         return(best+base+1);
 171 }
 172 
 173 
 174 void put_sock(unsigned short num, struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 175 {
 176         struct sock *sk1;
 177         struct sock *sk2;
 178         int mask;
 179 
 180         DPRINTF((DBG_INET, "put_sock(num = %d, sk = %X\n", num, sk));
 181         sk->num = num;
 182         sk->next = NULL;
 183         num = num &(SOCK_ARRAY_SIZE -1);
 184 
 185         /* We can't have an interupt re-enter here. */
 186         cli();
 187         if (sk->prot->sock_array[num] == NULL) 
 188         {
 189                 sk->prot->sock_array[num] = sk;
 190                 sti();
 191                 return;
 192         }
 193         sti();
 194         for(mask = 0xff000000; mask != 0xffffffff; mask = (mask >> 8) | mask) 
 195         {
 196                 if ((mask & sk->saddr) &&
 197                     (mask & sk->saddr) != (mask & 0xffffffff)) 
 198                 {
 199                         mask = mask << 8;
 200                         break;
 201                 }
 202         }
 203         DPRINTF((DBG_INET, "mask = %X\n", mask));
 204 
 205         cli();
 206         sk1 = sk->prot->sock_array[num];
 207         for(sk2 = sk1; sk2 != NULL; sk2=sk2->next) 
 208         {
 209                 if (!(sk2->saddr & mask)) 
 210                 {
 211                         if (sk2 == sk1) 
 212                         {
 213                                 sk->next = sk->prot->sock_array[num];
 214                                 sk->prot->sock_array[num] = sk;
 215                                 sti();
 216                                 return;
 217                         }
 218                         sk->next = sk2;
 219                         sk1->next= sk;
 220                         sti();
 221                         return;
 222                 }
 223                 sk1 = sk2;
 224         }
 225 
 226         /* Goes at the end. */
 227         sk->next = NULL;
 228         sk1->next = sk;
 229         sti();
 230 }
 231 
 232 
 233 static void remove_sock(struct sock *sk1)
     /* [previous][next][first][last][top][bottom][index][help] */
 234 {
 235         struct sock *sk2;
 236 
 237         DPRINTF((DBG_INET, "remove_sock(sk1=%X)\n", sk1));
 238         
 239         if (!sk1) 
 240         {
 241                 printk("sock.c: remove_sock: sk1 == NULL\n");
 242                 return;
 243         }       
 244 
 245         if (!sk1->prot) 
 246         {
 247                 printk("sock.c: remove_sock: sk1->prot == NULL\n");
 248                 return;
 249         }
 250 
 251         /* We can't have this changing out from under us. */
 252         cli();
 253         sk2 = sk1->prot->sock_array[sk1->num &(SOCK_ARRAY_SIZE -1)];
 254         if (sk2 == sk1) 
 255         {
 256                 sk1->prot->sock_array[sk1->num &(SOCK_ARRAY_SIZE -1)] = sk1->next;
 257                 sti();
 258                 return;
 259         }
 260 
 261         while(sk2 && sk2->next != sk1) 
 262         {
 263                 sk2 = sk2->next;
 264         }
 265 
 266         if (sk2) 
 267         {
 268                 sk2->next = sk1->next;
 269                 sti();
 270                 return;
 271         }
 272   
 273         sti();
 274 
 275         if (sk1->num != 0) 
 276                 DPRINTF((DBG_INET, "remove_sock: sock not found.\n"));
 277 }
 278 
 279 
 280 void destroy_sock(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 281 {
 282         struct sk_buff *skb;
 283 
 284         DPRINTF((DBG_INET, "destroying socket %X\n", sk));
 285         sk->inuse = 1;                  /* just to be safe. */
 286 
 287         /* Incase it's sleeping somewhere. */
 288         if (!sk->dead) 
 289                 wake_up(sk->sleep);
 290 
 291         remove_sock(sk);
 292   
 293         /* Now we can no longer get new packets. */
 294         delete_timer(sk);
 295 
 296 
 297         if (sk->send_tmp != NULL) 
 298         {
 299                 IS_SKB(sk->send_tmp);
 300                 kfree_skb(sk->send_tmp, FREE_WRITE);
 301         }
 302 
 303         /* Cleanup up the write buffer. */
 304         for(skb = sk->wfront; skb != NULL; ) 
 305         {
 306                 struct sk_buff *skb2;
 307 
 308                 skb2=(struct sk_buff *)skb->next;
 309                 if (skb->magic != TCP_WRITE_QUEUE_MAGIC) 
 310                 {
 311                         printk("sock.c:destroy_sock write queue with bad magic(%X)\n",
 312                                                                 skb->magic);
 313                         break;
 314                 }
 315                 IS_SKB(skb);
 316                 kfree_skb(skb, FREE_WRITE);
 317                 skb = skb2;
 318         }
 319 
 320         sk->wfront = NULL;
 321         sk->wback = NULL;
 322 
 323         if (sk->rqueue != NULL) 
 324         {
 325                 while((skb=skb_dequeue(&sk->rqueue))!=NULL)
 326                 {
 327                 /*
 328                  * This will take care of closing sockets that were
 329                  * listening and didn't accept everything.
 330                  */
 331                         if (skb->sk != NULL && skb->sk != sk) 
 332                         {
 333                                 IS_SKB(skb);
 334                                 skb->sk->dead = 1;
 335                                 skb->sk->prot->close(skb->sk, 0);
 336                         }
 337                         IS_SKB(skb);
 338                         kfree_skb(skb, FREE_READ);
 339                 }
 340         }
 341         sk->rqueue = NULL;
 342 
 343   /* Now we need to clean up the send head. */
 344         for(skb = sk->send_head; skb != NULL; ) 
 345         {
 346                 struct sk_buff *skb2;
 347 
 348                 /*
 349                  * We need to remove skb from the transmit queue,
 350                  * or maybe the arp queue.
 351                  */
 352                 cli();
 353 
 354                 if (skb->next != NULL) 
 355                 {
 356                         IS_SKB(skb);
 357                         skb_unlink(skb);
 358                 }
 359 
 360                 skb->dev = NULL;
 361                 sti();
 362                 skb2 = (struct sk_buff *)skb->link3;
 363                 kfree_skb(skb, FREE_WRITE);
 364                 skb = skb2;
 365         }       
 366         sk->send_head = NULL;
 367 
 368         /* And now the backlog. */
 369         if (sk->back_log != NULL) 
 370         {
 371                 /* this should never happen. */
 372                 printk("Socket Error: Socket destroyed with data backlog.\n");
 373                 cli();
 374                 skb = (struct sk_buff *)sk->back_log;
 375                 do 
 376                 {
 377                         struct sk_buff *skb2;
 378         
 379                         skb2 = (struct sk_buff *)skb->next;
 380                         kfree_skb(skb, FREE_READ);
 381                         skb = skb2;
 382                 }
 383                 while(skb != sk->back_log);
 384                 sti();
 385         }
 386         sk->back_log = NULL;
 387 
 388         /* Now if it has a half accepted/ closed socket. */
 389         if (sk->pair) 
 390         {
 391                 sk->pair->dead = 1;
 392                 sk->pair->prot->close(sk->pair, 0);
 393                 sk->pair = NULL;
 394         }
 395 
 396   /*
 397    * Now if everything is gone we can free the socket
 398    * structure, otherwise we need to keep it around until
 399    * everything is gone.
 400    */
 401           if (sk->rmem_alloc == 0 && sk->wmem_alloc == 0) 
 402           {
 403                 kfree_s((void *)sk,sizeof(*sk));
 404           } 
 405           else 
 406           {
 407                 /* this should never happen. */
 408                 /* actually it can if an ack has just been sent. */
 409                 DPRINTF((DBG_INET, "possible memory leak in socket = %X\n", sk));
 410                 sk->destroy = 1;
 411                 sk->ack_backlog = 0;
 412                 sk->inuse = 0;
 413                 reset_timer(sk, TIME_DESTROY, SOCK_DESTROY_TIME);
 414         }
 415         DPRINTF((DBG_INET, "leaving destroy_sock\n"));
 416 }
 417 
 418 
 419 static int inet_fcntl(struct socket *sock, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 420 {
 421         struct sock *sk;
 422 
 423         sk = (struct sock *) sock->data;
 424         if (sk == NULL) 
 425         {
 426                 printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
 427                 return(0);
 428         }
 429 
 430         switch(cmd) 
 431         {
 432                 case F_SETOWN:
 433                 /*
 434                  * This is a little restrictive, but it's the only
 435                  * way to make sure that you can't send a sigurg to
 436                  * another process.
 437                  */
 438                         if (!suser() && current->pgrp != -arg && current->pid != arg) 
 439                                 return(-EPERM);
 440                         sk->proc = arg;
 441                         return(0);
 442                         case F_GETOWN:
 443                         return(sk->proc);
 444                 default:
 445                         return(-EINVAL);
 446         }
 447 }
 448 
 449 
 450 /*
 451  *      Set socket options on an inet socket.
 452  */
 453  
 454 static int inet_setsockopt(struct socket *sock, int level, int optname,
     /* [previous][next][first][last][top][bottom][index][help] */
 455                     char *optval, int optlen)
 456 {
 457         struct sock *sk = (struct sock *) sock->data;  
 458         if (level == SOL_SOCKET)
 459                 return sock_setsockopt(sk,level,optname,optval,optlen);
 460         if (sk->prot->setsockopt==NULL)
 461                 return(-EOPNOTSUPP);
 462         else
 463                 return sk->prot->setsockopt(sk,level,optname,optval,optlen);
 464 }
 465 
 466 
 467 
 468 
 469 static int inet_getsockopt(struct socket *sock, int level, int optname,
     /* [previous][next][first][last][top][bottom][index][help] */
 470                     char *optval, int *optlen)
 471 {
 472         struct sock *sk = sock->data;   
 473         if (level == SOL_SOCKET) 
 474                 return sock_getsockopt(sk,level,optname,optval,optlen);
 475         if(sk->prot->getsockopt==NULL)          
 476                 return(-EOPNOTSUPP);
 477         else
 478                 return sk->prot->getsockopt(sk,level,optname,optval,optlen);
 479 }
 480 
 481 
 482 
 483 static int inet_listen(struct socket *sock, int backlog)
     /* [previous][next][first][last][top][bottom][index][help] */
 484 {
 485         struct sock *sk;
 486 
 487         sk = (struct sock *) sock->data;
 488         if (sk == NULL) 
 489         {
 490                 printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
 491                 return(0);
 492         }
 493         
 494         /* No listen() on a busy socket. */
 495         
 496         if(sk->state != TCP_CLOSE)
 497                 return -EINVAL;
 498 
 499         /* We may need to bind the socket. */
 500         if (sk->num == 0) 
 501         {
 502                 sk->num = get_new_socknum(sk->prot, 0);
 503                 if (sk->num == 0)       
 504                         return(-EAGAIN);
 505                 put_sock(sk->num, sk);
 506                 sk->dummy_th.source = ntohs(sk->num);
 507         }
 508  
 509         /* Some sanity checks here are a good idea */
 510         if(backlog<0)
 511                 return -EINVAL;
 512         /* Pick a number, any number 8-) */
 513         if(backlog>5)
 514                 backlog=5;
 515 
 516         /* We might as well re use these. */ 
 517         sk->max_ack_backlog = backlog;
 518         if (sk->state != TCP_LISTEN) 
 519         {
 520                 sk->ack_backlog = 0;
 521                 sk->state = TCP_LISTEN;
 522         }
 523         return(0);
 524 }
 525 
 526 /*
 527  *      Default callbacks for user INET sockets. These just wake up
 528  *      the user owning the socket.
 529  */
 530 
 531 static void def_callback1(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 532 {
 533         if(!sk->dead)
 534                 wake_up(sk->sleep);
 535 }
 536 
 537 static void def_callback2(struct sock *sk,int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 538 {
 539         if(!sk->dead)
 540                 wake_up(sk->sleep);
 541 }
 542 
 543 
 544 /*
 545  *      Create an inet socket. Note that sock=NULL is now legal, and means a kernel
 546  *      created socket.
 547  */
 548  
 549 static int inet_create(struct socket *sock, int protocol)
     /* [previous][next][first][last][top][bottom][index][help] */
 550 {
 551         struct sock *sk;
 552         struct proto *prot;
 553         int err;
 554 
 555         sk = (struct sock *) kmalloc(sizeof(*sk), GFP_KERNEL);
 556         
 557         if (sk == NULL) 
 558                 return(-ENOMEM);
 559         sk->num = 0;
 560         sk->reuse = 0;
 561         switch(sock->type) 
 562         {
 563                 case SOCK_STREAM:
 564                 case SOCK_SEQPACKET:
 565                         if (protocol && protocol != IPPROTO_TCP) 
 566                         {
 567                                 kfree_s((void *)sk, sizeof(*sk));
 568                                 return(-EPROTONOSUPPORT);
 569                         }
 570                         protocol = IPPROTO_TCP;
 571                         sk->no_check = TCP_NO_CHECK;
 572                         prot = &tcp_prot;
 573                         break;
 574 
 575                 case SOCK_DGRAM:
 576                         if (protocol && protocol != IPPROTO_UDP) 
 577                         {
 578                                 kfree_s((void *)sk, sizeof(*sk));
 579                                 return(-EPROTONOSUPPORT);
 580                         }
 581                         protocol = IPPROTO_UDP;
 582                         sk->no_check = UDP_NO_CHECK;
 583                         prot=&udp_prot;
 584                         break;
 585       
 586                 case SOCK_RAW:
 587                         if (!suser()) 
 588                         {
 589                                 kfree_s((void *)sk, sizeof(*sk));
 590                                 return(-EPERM);
 591                         }
 592                         if (!protocol) 
 593                         {
 594                                 kfree_s((void *)sk, sizeof(*sk));
 595                                 return(-EPROTONOSUPPORT);
 596                         }
 597                         prot = &raw_prot;
 598                         sk->reuse = 1;
 599                         sk->no_check = 0;       /*
 600                                                  * Doesn't matter no checksum is
 601                                                  * preformed anyway.
 602                                                  */
 603                         sk->num = protocol;
 604                         break;
 605 
 606                 case SOCK_PACKET:
 607                         if (!suser()) 
 608                         {
 609                                 kfree_s((void *)sk, sizeof(*sk));
 610                                 return(-EPERM);
 611                         }
 612                         if (!protocol) 
 613                         {
 614                                 kfree_s((void *)sk, sizeof(*sk));
 615                                 return(-EPROTONOSUPPORT);
 616                         }
 617                         prot = &packet_prot;
 618                         sk->reuse = 1;
 619                         sk->no_check = 0;       /* Doesn't matter no checksum is
 620                                                  * preformed anyway.
 621                                                  */
 622                         sk->num = protocol;
 623                         break;
 624 
 625                 default:
 626                         kfree_s((void *)sk, sizeof(*sk));
 627                         return(-ESOCKTNOSUPPORT);
 628         }
 629         sk->socket = sock;
 630         sk->type = sock->type;
 631         sk->protocol = protocol;
 632         sk->wmem_alloc = 0;
 633         sk->rmem_alloc = 0;
 634         sk->sndbuf = SK_WMEM_MAX;
 635         sk->rcvbuf = SK_RMEM_MAX;
 636         sk->pair = NULL;
 637         sk->opt = NULL;
 638         sk->send_seq = 0;
 639         sk->acked_seq = 0;
 640         sk->copied_seq = 0;
 641         sk->fin_seq = 0;
 642         sk->proc = 0;
 643         sk->rtt = TCP_WRITE_TIME;
 644         sk->mdev = 0;
 645         sk->backoff = 0;
 646         sk->packets_out = 0;
 647         sk->cong_window = 1; /* start with only sending one packet at a time. */
 648         sk->exp_growth = 1;  /* if set cong_window grow exponentially every time
 649                                   we get an ack. */
 650         sk->urginline = 0;
 651         sk->intr = 0;
 652         sk->linger = 0;
 653         sk->destroy = 0;
 654 
 655         sk->priority = 1;
 656         sk->shutdown = 0;
 657         sk->urg = 0;
 658         sk->keepopen = 0;
 659         sk->zapped = 0;
 660         sk->done = 0;
 661         sk->ack_backlog = 0;
 662         sk->window = 0;
 663         sk->bytes_rcv = 0;
 664         sk->state = TCP_CLOSE;
 665         sk->dead = 0;
 666         sk->ack_timed = 0;
 667         sk->send_tmp = NULL;
 668         sk->mss = 0; /* we will try not to send any packets smaller than this. */
 669         sk->debug = 0;
 670         
 671         /* this is how many unacked bytes we will accept for this socket.  */
 672         sk->max_unacked = 2048;         /* needs to be at most 2 full packets. */
 673 
 674         /* how many packets we should send before forcing an ack. 
 675          * if this is set to zero it is the same as sk->delay_acks = 0 
 676          */
 677         sk->max_ack_backlog = 0;
 678         sk->inuse = 0;
 679         sk->delay_acks = 0;
 680         sk->wback = NULL;
 681         sk->wfront = NULL;
 682         sk->rqueue = NULL;
 683         sk->mtu = 576;  /* This is a reasonable typical choice. RFC791 guarantees this is acceptable */
 684         sk->prot = prot;
 685         sk->sleep = sock->wait;
 686         sk->daddr = 0;
 687         sk->saddr = my_addr();
 688         sk->err = 0;
 689         sk->next = NULL;
 690         sk->pair = NULL;
 691         sk->send_tail = NULL;
 692         sk->send_head = NULL;
 693         sk->timeout = 0;
 694         sk->broadcast = 0;
 695         sk->timer.data = (unsigned long)sk;
 696         sk->timer.function = &net_timer;
 697         sk->back_log = NULL;
 698         sk->blog = 0;
 699         sock->data =(void *) sk;
 700         sk->dummy_th.doff = sizeof(sk->dummy_th)/4;
 701         sk->dummy_th.res1=0;
 702         sk->dummy_th.res2=0;
 703         sk->dummy_th.urg_ptr = 0;
 704         sk->dummy_th.fin = 0;
 705         sk->dummy_th.syn = 0;
 706         sk->dummy_th.rst = 0;
 707         sk->dummy_th.psh = 0;
 708         sk->dummy_th.ack = 0;
 709         sk->dummy_th.urg = 0;
 710         sk->dummy_th.dest = 0;
 711         
 712         sk->ip_tos=0;
 713         sk->ip_ttl=64;
 714         
 715         sk->state_change = def_callback1;
 716         sk->data_ready = def_callback2;
 717         sk->write_space = def_callback1;
 718         sk->error_report = def_callback1;
 719 
 720         if (sk->num) 
 721         {
 722                 /*
 723                  * It assumes that any protocol which allows
 724                  * the user to assign a number at socket
 725                  * creation time automatically
 726                  * shares.
 727                  */
 728                 put_sock(sk->num, sk);
 729                 sk->dummy_th.source = ntohs(sk->num);
 730         }
 731 
 732         if (sk->prot->init) 
 733         {
 734                 err = sk->prot->init(sk);
 735                 if (err != 0) 
 736                 {
 737                         destroy_sock(sk);
 738                         return(err);
 739                 }
 740         }
 741         return(0);
 742 }
 743 
 744 
 745 static int inet_dup(struct socket *newsock, struct socket *oldsock)
     /* [previous][next][first][last][top][bottom][index][help] */
 746 {
 747         return(inet_create(newsock,((struct sock *)(oldsock->data))->protocol));
 748 }
 749 
 750 
 751 /* The peer socket should always be NULL. */
 752 static int inet_release(struct socket *sock, struct socket *peer)
     /* [previous][next][first][last][top][bottom][index][help] */
 753 {
 754         struct sock *sk;
 755 
 756         sk = (struct sock *) sock->data;
 757         if (sk == NULL) 
 758                 return(0);
 759 
 760         DPRINTF((DBG_INET, "inet_release(sock = %X, peer = %X)\n", sock, peer));
 761         wake_up(sk->sleep);
 762 
 763         /* Start closing the connection.  This may take a while. */
 764         /*
 765          * If linger is set, we don't return until the close
 766          * is complete.  Other wise we return immediately. The
 767          * actually closing is done the same either way.
 768          */
 769         if (sk->linger == 0) 
 770         {
 771                 sk->prot->close(sk,0);
 772                 sk->dead = 1;
 773         } 
 774         else 
 775         {
 776                 DPRINTF((DBG_INET, "sk->linger set.\n"));
 777                 sk->prot->close(sk, 0);
 778                 cli();
 779                 if (sk->lingertime)
 780                         current->timeout = jiffies + HZ*sk->lingertime;
 781                 while(sk->state != TCP_CLOSE && sk->state != TCP_FIN_WAIT2 && sk->state !=TCP_TIME_WAIT && current->timeout>0) 
 782                 {
 783                         interruptible_sleep_on(sk->sleep);
 784                         if (current->signal & ~current->blocked) 
 785                         {
 786                                 sti();
 787                                 current->timeout=0;
 788                                 return(-ERESTARTSYS);
 789                         }
 790                 }
 791                 current->timeout=0;
 792                 sti();
 793                 sk->dead = 1;
 794         }
 795         sk->inuse = 1;
 796 
 797   /* This will destroy it. */
 798         release_sock(sk);
 799         sock->data = NULL;
 800         DPRINTF((DBG_INET, "inet_release returning\n"));
 801         return(0);
 802 }
 803 
 804 
 805 /* this needs to be changed to dissallow
 806    the rebinding of sockets.   What error
 807    should it return? */
 808 
 809 static int inet_bind(struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 810                int addr_len)
 811 {
 812         struct sockaddr_in addr;
 813         struct sock *sk, *sk2;
 814         unsigned short snum;
 815         int err;
 816 
 817         sk = (struct sock *) sock->data;
 818         if (sk == NULL) 
 819         {
 820                 printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
 821                 return(0);
 822         }
 823 
 824         /* check this error. */
 825         if (sk->state != TCP_CLOSE) 
 826                 return(-EIO);
 827         if (sk->num != 0) 
 828                 return(-EINVAL);
 829 
 830         err=verify_area(VERIFY_READ, uaddr, addr_len);
 831         if(err)
 832                 return err;
 833         memcpy_fromfs(&addr, uaddr, min(sizeof(addr), addr_len));
 834 
 835         snum = ntohs(addr.sin_port);
 836         DPRINTF((DBG_INET, "bind sk =%X to port = %d\n", sk, snum));
 837         sk = (struct sock *) sock->data;
 838         if (snum == 0) 
 839         {
 840                 snum = get_new_socknum(sk->prot, 0);
 841         }
 842         if (snum < PROT_SOCK && !suser()) 
 843                 return(-EACCES);
 844 
 845         if (addr.sin_addr.s_addr!=0 && chk_addr(addr.sin_addr.s_addr)!=IS_MYADDR)
 846                 return(-EADDRNOTAVAIL); /* Source address MUST be ours! */
 847         
 848         if (chk_addr(addr.sin_addr.s_addr) || addr.sin_addr.s_addr == 0)
 849                 sk->saddr = addr.sin_addr.s_addr;
 850 
 851         DPRINTF((DBG_INET, "sock_array[%d] = %X:\n", snum &(SOCK_ARRAY_SIZE -1),
 852                 sk->prot->sock_array[snum &(SOCK_ARRAY_SIZE -1)]));
 853 
 854         /* Make sure we are allowed to bind here. */
 855         cli();
 856 outside_loop:
 857         for(sk2 = sk->prot->sock_array[snum & (SOCK_ARRAY_SIZE -1)];sk2 != NULL; sk2 = sk2->next) 
 858         {
 859                 if (sk2->num != snum) 
 860                         continue;
 861                 if (sk2->saddr != sk->saddr) 
 862                         continue;
 863                 if (sk2->dead && !sk2->inuse)   /* Added in use check AC 24/12/93 */
 864                 {
 865                         destroy_sock(sk2);
 866                         goto outside_loop;
 867                 }
 868                 if (!sk->reuse) 
 869                 {
 870                         sti();
 871                         return(-EADDRINUSE);
 872                 }
 873                 if (sk2->num != snum) 
 874                         continue;               /* more than one */
 875                 if (sk2->saddr != sk->saddr) 
 876                         continue;       /* socket per slot ! -FB */
 877                 if (!sk2->reuse) 
 878                 {
 879                         sti();
 880                         return(-EADDRINUSE);
 881                 }
 882         }
 883         sti();
 884 
 885         remove_sock(sk);
 886         put_sock(snum, sk);
 887         sk->dummy_th.source = ntohs(sk->num);
 888         sk->daddr = 0;
 889         sk->dummy_th.dest = 0;
 890         return(0);
 891 }
 892 
 893 
 894 static int inet_connect(struct socket *sock, struct sockaddr * uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 895                   int addr_len, int flags)
 896 {
 897         struct sock *sk;
 898         int err;
 899 
 900         sock->conn = NULL;
 901         sk = (struct sock *) sock->data;
 902         if (sk == NULL) 
 903         {
 904                 printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
 905                 return(0);
 906         }
 907 
 908         if (sock->state == SS_CONNECTING && sk->state == TCP_ESTABLISHED)
 909         {
 910                 sock->state = SS_CONNECTED;
 911           /* Connection completing after a connect/EINPROGRESS/select/connect */
 912                 return 0;       /* Rock and roll */
 913         }
 914 
 915         if (sock->state == SS_CONNECTING && sk->protocol == IPPROTO_TCP && (flags & O_NONBLOCK))
 916                 return -EALREADY;       /* Connecting is currently in progress */
 917         
 918         if (sock->state != SS_CONNECTING) 
 919         {
 920                 /* We may need to bind the socket. */
 921                 if (sk->num == 0) 
 922                 {
 923                         sk->num = get_new_socknum(sk->prot, 0);
 924                         if (sk->num == 0) 
 925                                 return(-EAGAIN);
 926                         put_sock(sk->num, sk);
 927                         sk->dummy_th.source = htons(sk->num);
 928                 }
 929 
 930                 if (sk->prot->connect == NULL) 
 931                         return(-EOPNOTSUPP);
 932   
 933                 err = sk->prot->connect(sk, (struct sockaddr_in *)uaddr, addr_len);
 934                 if (err < 0)
 935                         return(err);
 936   
 937                 sock->state = SS_CONNECTING;
 938         }
 939 
 940         if (sk->state != TCP_ESTABLISHED &&(flags & O_NONBLOCK)) 
 941                 return(-EINPROGRESS);
 942 
 943         cli(); /* avoid the race condition */
 944         while(sk->state == TCP_SYN_SENT || sk->state == TCP_SYN_RECV) 
 945         {
 946                 interruptible_sleep_on(sk->sleep);
 947                 if (current->signal & ~current->blocked) 
 948                 {
 949                         sti();
 950                         return(-ERESTARTSYS);
 951                 }
 952                 /* This fixes a nasty in the tcp/ip code. There is a hideous hassle with
 953                    icmp error packets wanting to close a tcp or udp socket. */
 954                 if(sk->err && sk->protocol == IPPROTO_TCP)
 955                 {
 956                         sti();
 957                         sock->state = SS_UNCONNECTED;
 958                         err = -sk->err;
 959                         sk->err=0;
 960                         return err; /* set by tcp_err() */
 961                 }
 962         }
 963         sti();
 964         sock->state = SS_CONNECTED;
 965 
 966         if (sk->state != TCP_ESTABLISHED && sk->err) 
 967         {
 968                 sock->state = SS_UNCONNECTED;
 969                 err=sk->err;
 970                 sk->err=0;
 971                 return(err);
 972         }
 973         return(0);
 974 }
 975 
 976 
 977 static int inet_socketpair(struct socket *sock1, struct socket *sock2)
     /* [previous][next][first][last][top][bottom][index][help] */
 978 {
 979         return(-EOPNOTSUPP);
 980 }
 981 
 982 
 983 static int inet_accept(struct socket *sock, struct socket *newsock, int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
 984 {
 985         struct sock *sk1, *sk2;
 986         int err;
 987 
 988         sk1 = (struct sock *) sock->data;
 989         if (sk1 == NULL) 
 990         {
 991                 printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
 992                 return(0);
 993         }
 994 
 995         /*
 996          * We've been passed an extra socket.
 997          * We need to free it up because the tcp module creates
 998          * it's own when it accepts one.
 999          */
1000         
1001         if (newsock->data) 
1002                 kfree_s(newsock->data, sizeof(struct sock));
1003         newsock->data = NULL;
1004 
1005         if (sk1->prot->accept == NULL) 
1006                 return(-EOPNOTSUPP);
1007 
1008         /* Restore the state if we have been interrupted, and then returned. */
1009         if (sk1->pair != NULL ) 
1010         {
1011                 sk2 = sk1->pair;
1012                 sk1->pair = NULL;
1013         } 
1014         else 
1015         {
1016                 sk2 = sk1->prot->accept(sk1,flags);
1017                 if (sk2 == NULL) 
1018                 {
1019                         if (sk1->err <= 0)
1020                                 printk("Warning sock.c:sk1->err <= 0.  Returning non-error.\n");
1021                         err=sk1->err;
1022                         sk1->err=0;
1023                         return(-err);
1024                 }
1025         }
1026         newsock->data = (void *)sk2;
1027         sk2->sleep = newsock->wait;
1028         newsock->conn = NULL;
1029         if (flags & O_NONBLOCK) 
1030                 return(0);
1031 
1032         cli(); /* avoid the race. */
1033 
1034         while(sk2->state == TCP_SYN_RECV) 
1035         {
1036                 interruptible_sleep_on(sk2->sleep);
1037                 if (current->signal & ~current->blocked) 
1038                 {
1039                         sti();
1040                         sk1->pair = sk2;
1041                         sk2->sleep = NULL;
1042                         newsock->data = NULL;
1043                         return(-ERESTARTSYS);
1044                 }
1045         }
1046         sti();
1047 
1048         if (sk2->state != TCP_ESTABLISHED && sk2->err > 0) 
1049         {
1050 
1051                 err = -sk2->err;
1052                 sk2->err=0;
1053                 destroy_sock(sk2);
1054                 newsock->data = NULL;
1055                 return(err);
1056         }
1057         newsock->state = SS_CONNECTED;
1058         return(0);
1059 }
1060 
1061 
1062 static int inet_getname(struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
1063                  int *uaddr_len, int peer)
1064 {
1065         struct sockaddr_in sin;
1066         struct sock *sk;
1067         int len;
1068         int err;
1069   
1070   
1071         err = verify_area(VERIFY_WRITE,uaddr_len,sizeof(long));
1072         if(err)
1073                 return err;
1074         
1075         len=get_fs_long(uaddr_len);
1076         
1077         err = verify_area(VERIFY_WRITE, uaddr, len);
1078         if(err)
1079                 return err;
1080         
1081         /* Check this error. */
1082         if (len < sizeof(sin))  
1083                 return(-EINVAL);
1084 
1085         sin.sin_family = AF_INET;
1086         sk = (struct sock *) sock->data;
1087         if (sk == NULL) 
1088         {
1089                 printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1090                 return(0);
1091         }       
1092         if (peer) 
1093         {
1094                 if (!tcp_connected(sk->state)) 
1095                         return(-ENOTCONN);
1096                 sin.sin_port = sk->dummy_th.dest;
1097                 sin.sin_addr.s_addr = sk->daddr;
1098         } 
1099         else 
1100         {
1101                 sin.sin_port = sk->dummy_th.source;
1102                 if (sk->saddr == 0) 
1103                         sin.sin_addr.s_addr = my_addr();
1104                 else 
1105                         sin.sin_addr.s_addr = sk->saddr;
1106         }
1107         len = sizeof(sin);
1108         memcpy_tofs(uaddr, &sin, sizeof(sin));
1109         put_fs_long(len, uaddr_len);
1110         return(0);
1111 }
1112 
1113 
1114 static int inet_read(struct socket *sock, char *ubuf, int size, int noblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1115 {
1116         struct sock *sk;
1117 
1118         sk = (struct sock *) sock->data;
1119         if (sk == NULL) 
1120         {
1121                 printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1122                 return(0);
1123         }
1124 
1125         /* We may need to bind the socket. */
1126         if (sk->num == 0) 
1127         {
1128                 sk->num = get_new_socknum(sk->prot, 0);
1129                 if (sk->num == 0) 
1130                         return(-EAGAIN);
1131                 put_sock(sk->num, sk);
1132                 sk->dummy_th.source = ntohs(sk->num);
1133         }
1134         return(sk->prot->read(sk, (unsigned char *) ubuf, size, noblock,0));
1135 }
1136 
1137 
1138 static int inet_recv(struct socket *sock, void *ubuf, int size, int noblock, unsigned flags)
     /* [previous][next][first][last][top][bottom][index][help] */
1139 {
1140         struct sock *sk;
1141 
1142         sk = (struct sock *) sock->data;
1143         if (sk == NULL) 
1144         {
1145                 printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1146                 return(0);
1147         }
1148 
1149         /* We may need to bind the socket. */
1150         if (sk->num == 0) 
1151         {
1152                 sk->num = get_new_socknum(sk->prot, 0);
1153                 if (sk->num == 0) 
1154                         return(-EAGAIN);
1155                 put_sock(sk->num, sk);
1156                 sk->dummy_th.source = ntohs(sk->num);
1157         }
1158         return(sk->prot->read(sk, (unsigned char *) ubuf, size, noblock, flags));
1159 }
1160 
1161 
1162 static int inet_write(struct socket *sock, char *ubuf, int size, int noblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1163 {
1164         struct sock *sk;
1165 
1166         sk = (struct sock *) sock->data;
1167         if (sk == NULL) 
1168         {
1169                 printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1170                 return(0);
1171         }
1172         if (sk->shutdown & SEND_SHUTDOWN) 
1173         {
1174                 send_sig(SIGPIPE, current, 1);
1175                 return(-EPIPE);
1176         }
1177 
1178         /* We may need to bind the socket. */
1179         if (sk->num == 0) 
1180         {
1181                 sk->num = get_new_socknum(sk->prot, 0);
1182                 if (sk->num == 0) 
1183                         return(-EAGAIN);
1184                 put_sock(sk->num, sk);
1185                 sk->dummy_th.source = ntohs(sk->num);
1186         }
1187 
1188         return(sk->prot->write(sk, (unsigned char *) ubuf, size, noblock, 0));
1189 }
1190 
1191 
1192 static int inet_send(struct socket *sock, void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1193                unsigned flags)
1194 {
1195         struct sock *sk;
1196 
1197         sk = (struct sock *) sock->data;
1198         if (sk == NULL) 
1199         {
1200                 printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1201                 return(0);
1202         }
1203         if (sk->shutdown & SEND_SHUTDOWN) 
1204         {
1205                 send_sig(SIGPIPE, current, 1);
1206                 return(-EPIPE);
1207         }
1208 
1209         /* We may need to bind the socket. */
1210         if (sk->num == 0) 
1211         {
1212                 sk->num = get_new_socknum(sk->prot, 0);
1213                 if (sk->num == 0) 
1214                         return(-EAGAIN);
1215                 put_sock(sk->num, sk);
1216                 sk->dummy_th.source = ntohs(sk->num);
1217         }
1218 
1219         return(sk->prot->write(sk, (unsigned char *) ubuf, size, noblock, flags));
1220 }
1221 
1222 
1223 static int inet_sendto(struct socket *sock, void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1224             unsigned flags, struct sockaddr *sin, int addr_len)
1225 {
1226         struct sock *sk;
1227 
1228         sk = (struct sock *) sock->data;
1229         if (sk == NULL) 
1230         {
1231                 printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1232                 return(0);
1233         }
1234         if (sk->shutdown & SEND_SHUTDOWN) 
1235         {
1236                 send_sig(SIGPIPE, current, 1);
1237                 return(-EPIPE);
1238         }
1239 
1240         if (sk->prot->sendto == NULL) 
1241                 return(-EOPNOTSUPP);
1242 
1243         /* We may need to bind the socket. */
1244         if (sk->num == 0) 
1245         {
1246                 sk->num = get_new_socknum(sk->prot, 0);
1247                 if (sk->num == 0) 
1248                         return(-EAGAIN);
1249                 put_sock(sk->num, sk);
1250                 sk->dummy_th.source = ntohs(sk->num);
1251         }
1252 
1253         return(sk->prot->sendto(sk, (unsigned char *) ubuf, size, noblock, flags, 
1254                            (struct sockaddr_in *)sin, addr_len));
1255 }
1256 
1257 
1258 static int inet_recvfrom(struct socket *sock, void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1259                    unsigned flags, struct sockaddr *sin, int *addr_len )
1260 {
1261         struct sock *sk;
1262 
1263         sk = (struct sock *) sock->data;
1264         if (sk == NULL) 
1265         {
1266                 printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1267                 return(0);
1268         }
1269 
1270         if (sk->prot->recvfrom == NULL) 
1271                 return(-EOPNOTSUPP);
1272 
1273         /* We may need to bind the socket. */
1274         if (sk->num == 0) 
1275         {
1276                 sk->num = get_new_socknum(sk->prot, 0);
1277                 if (sk->num == 0) 
1278                         return(-EAGAIN);
1279                 put_sock(sk->num, sk);
1280                 sk->dummy_th.source = ntohs(sk->num);
1281         }
1282 
1283         return(sk->prot->recvfrom(sk, (unsigned char *) ubuf, size, noblock, flags,
1284                              (struct sockaddr_in*)sin, addr_len));
1285 }
1286 
1287 
1288 static int inet_shutdown(struct socket *sock, int how)
     /* [previous][next][first][last][top][bottom][index][help] */
1289 {
1290         struct sock *sk;
1291 
1292         /*
1293          * This should really check to make sure
1294          * the socket is a TCP socket.
1295          */
1296         how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
1297                        1->2 bit 2 snds.
1298                        2->3 */
1299         if (how & ~SHUTDOWN_MASK) 
1300                 return(-EINVAL);
1301         sk = (struct sock *) sock->data;
1302         if (sk == NULL) 
1303         {
1304                 printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1305                 return(0);
1306         }
1307         if (sock->state == SS_CONNECTING && sk->state == TCP_ESTABLISHED)
1308                 sock->state = SS_CONNECTED;
1309 
1310         if (!tcp_connected(sk->state)) 
1311                 return(-ENOTCONN);
1312         sk->shutdown |= how;
1313         if (sk->prot->shutdown) 
1314                 sk->prot->shutdown(sk, how);
1315         return(0);
1316 }
1317 
1318 
1319 static int inet_select(struct socket *sock, int sel_type, select_table *wait )
     /* [previous][next][first][last][top][bottom][index][help] */
1320 {
1321         struct sock *sk;
1322 
1323         sk = (struct sock *) sock->data;
1324         if (sk == NULL) 
1325         {
1326                 printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1327                 return(0);
1328         }
1329 
1330         if (sk->prot->select == NULL) 
1331         {
1332                 DPRINTF((DBG_INET, "select on non-selectable socket.\n"));
1333                 return(0);
1334         }
1335         return(sk->prot->select(sk, sel_type, wait));
1336 }
1337 
1338 
1339 static int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1340 {
1341         struct sock *sk;
1342         int err;
1343 
1344         DPRINTF((DBG_INET, "INET: in inet_ioctl\n"));
1345         sk = NULL;
1346         if (sock && (sk = (struct sock *) sock->data) == NULL) 
1347         {
1348                 printk("AF_INET: Warning: sock->data = NULL: %d\n" , __LINE__);
1349                 return(0);
1350         }
1351 
1352         switch(cmd) 
1353         {
1354                 case FIOSETOWN:
1355                 case SIOCSPGRP:
1356                         err=verify_area(VERIFY_READ,(int *)arg,sizeof(long));
1357                         if(err)
1358                                 return err;
1359                         if (sk)
1360                                 sk->proc = get_fs_long((int *) arg);
1361                         return(0);
1362                 case FIOGETOWN:
1363                 case SIOCGPGRP:
1364                         if (sk) 
1365                         {
1366                                 err=verify_area(VERIFY_WRITE,(void *) arg, sizeof(long));
1367                                 if(err)
1368                                         return err;
1369                                 put_fs_long(sk->proc,(int *)arg);
1370                         }
1371                         return(0);
1372                 case DDIOCSDBG:
1373                         return(dbg_ioctl((void *) arg, DBG_INET));
1374         
1375                 case SIOCADDRT:
1376                 case SIOCDELRT:
1377                         return(rt_ioctl(cmd,(void *) arg));
1378 
1379                 case SIOCDARP:
1380                 case SIOCGARP:
1381                 case SIOCSARP:
1382                         return(arp_ioctl(cmd,(void *) arg));
1383 
1384                 case IP_SET_DEV:
1385                 case SIOCGIFCONF:
1386                 case SIOCGIFFLAGS:
1387                 case SIOCSIFFLAGS:
1388                 case SIOCGIFADDR:
1389                 case SIOCSIFADDR:
1390                 case SIOCGIFDSTADDR:
1391                 case SIOCSIFDSTADDR:
1392                 case SIOCGIFBRDADDR:
1393                 case SIOCSIFBRDADDR:
1394                 case SIOCGIFNETMASK:
1395                 case SIOCSIFNETMASK:
1396                 case SIOCGIFMETRIC:
1397                 case SIOCSIFMETRIC:
1398                 case SIOCGIFMEM:
1399                 case SIOCSIFMEM:
1400                 case SIOCGIFMTU:
1401                 case SIOCSIFMTU:
1402                 case SIOCSIFLINK:
1403                 case SIOCGIFHWADDR:
1404                         return(dev_ioctl(cmd,(void *) arg));
1405         
1406                 default:
1407                         if (!sk || !sk->prot->ioctl) 
1408                                 return(-EINVAL);
1409                         return(sk->prot->ioctl(sk, cmd, arg));
1410         }
1411         /*NOTREACHED*/
1412         return(0);
1413 }
1414 
1415 
1416 
1417 /*
1418  * This routine must find a socket given a TCP or UDP header.
1419  * Everyhting is assumed to be in net order.
1420  */
1421 struct sock *get_sock(struct proto *prot, unsigned short num,
     /* [previous][next][first][last][top][bottom][index][help] */
1422                                 unsigned long raddr,
1423                                 unsigned short rnum, unsigned long laddr)
1424 {
1425         struct sock *s;
1426         unsigned short hnum;
1427 
1428         hnum = ntohs(num);
1429         DPRINTF((DBG_INET, "get_sock(prot=%X, num=%d, raddr=%X, rnum=%d, laddr=%X)\n",
1430                   prot, num, raddr, rnum, laddr));
1431 
1432   /*
1433    * SOCK_ARRAY_SIZE must be a power of two.  This will work better
1434    * than a prime unless 3 or more sockets end up using the same
1435    * array entry.  This should not be a problem because most
1436    * well known sockets don't overlap that much, and for
1437    * the other ones, we can just be careful about picking our
1438    * socket number when we choose an arbitrary one.
1439    */
1440         for(s = prot->sock_array[hnum & (SOCK_ARRAY_SIZE - 1)];s != NULL; s = s->next) 
1441         {
1442                 if (s->num != hnum) 
1443                         continue;
1444                 if(s->dead && (s->state == TCP_CLOSE))
1445                         continue;
1446                 if(prot == &udp_prot)
1447                         return s;
1448                 if(ip_addr_match(s->daddr,raddr)==0)
1449                         continue;
1450                 if (s->dummy_th.dest != rnum && s->dummy_th.dest != 0) 
1451                         continue;
1452                 if(ip_addr_match(s->saddr,laddr) == 0)
1453                         continue;
1454                 return(s);
1455         }
1456         return(NULL);
1457 }
1458 
1459 
1460 void release_sock(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
1461 {
1462         if (!sk) 
1463         {
1464                 printk("sock.c: release_sock sk == NULL\n");
1465                 return;
1466         }
1467         /* This one _IS_ legal */
1468         if (!sk->prot) 
1469         {
1470                 return;
1471         }
1472 
1473         if (sk->blog) 
1474                 return;
1475 
1476         /* See if we have any packets built up. */
1477         cli();
1478         sk->inuse = 1;
1479         while(sk->back_log != NULL) 
1480         {
1481                 struct sk_buff *skb;
1482 
1483                 sk->blog = 1;
1484                 skb =(struct sk_buff *)sk->back_log;
1485                 DPRINTF((DBG_INET, "release_sock: skb = %X:\n", skb));
1486                 if (skb->next != skb) 
1487                 {
1488                         sk->back_log = skb->next;
1489                         skb->prev->next = skb->next;
1490                         skb->next->prev = skb->prev;
1491                 } 
1492                 else 
1493                 {
1494                         sk->back_log = NULL;
1495                 }
1496                 sti();
1497                 DPRINTF((DBG_INET, "sk->back_log = %X\n", sk->back_log));
1498                 if (sk->prot->rcv) 
1499                         sk->prot->rcv(skb, skb->dev, sk->opt,
1500                                          skb->saddr, skb->len, skb->daddr, 1,
1501                                         /* Only used for/by raw sockets. */
1502                                         (struct inet_protocol *)sk->pair); 
1503                 cli();
1504         }
1505         sk->blog = 0;
1506         sk->inuse = 0;
1507         sti();
1508         if (sk->dead && sk->state == TCP_CLOSE) 
1509         {
1510                 /* Should be about 2 rtt's */
1511                 reset_timer(sk, TIME_DONE, min(sk->rtt * 2, TCP_DONE_TIME));
1512         }
1513 }
1514 
1515 
1516 static int inet_fioctl(struct inode *inode, struct file *file,
     /* [previous][next][first][last][top][bottom][index][help] */
1517          unsigned int cmd, unsigned long arg)
1518 {
1519         int minor, ret;
1520 
1521         /* Extract the minor number on which we work. */
1522         minor = MINOR(inode->i_rdev);
1523         if (minor != 0) 
1524                 return(-ENODEV);
1525 
1526         /* Now dispatch on the minor device. */
1527         switch(minor) 
1528         {
1529                 case 0:         /* INET */
1530                         ret = inet_ioctl(NULL, cmd, arg);
1531                         break;
1532                 case 1:         /* IP */
1533                         ret = ip_ioctl(NULL, cmd, arg);
1534                         break;
1535                 case 2:         /* ICMP */
1536                         ret = icmp_ioctl(NULL, cmd, arg);
1537                         break;
1538                 case 3:         /* TCP */
1539                         ret = tcp_ioctl(NULL, cmd, arg);
1540                         break;
1541                 case 4:         /* UDP */
1542                         ret = udp_ioctl(NULL, cmd, arg);
1543                         break;
1544                 default:
1545                         ret = -ENODEV;
1546         }
1547 
1548         return(ret);
1549 }
1550 
1551 
1552 static struct file_operations inet_fops = 
1553 {
1554         NULL,           /* LSEEK        */
1555         NULL,           /* READ         */
1556         NULL,           /* WRITE        */
1557         NULL,           /* READDIR      */
1558         NULL,           /* SELECT       */
1559         inet_fioctl,    /* IOCTL        */
1560         NULL,           /* MMAP         */
1561         NULL,           /* OPEN         */
1562         NULL            /* CLOSE        */
1563 };      
1564 
1565 
1566 static struct proto_ops inet_proto_ops = 
1567 {
1568         AF_INET,
1569 
1570         inet_create,
1571         inet_dup,
1572         inet_release,
1573         inet_bind,
1574         inet_connect,
1575         inet_socketpair,
1576         inet_accept,
1577         inet_getname, 
1578         inet_read,
1579         inet_write,
1580         inet_select,
1581         inet_ioctl,
1582         inet_listen,
1583         inet_send,
1584         inet_recv,
1585         inet_sendto,
1586         inet_recvfrom,
1587         inet_shutdown,
1588         inet_setsockopt,
1589         inet_getsockopt,
1590         inet_fcntl,
1591 };
1592 
1593 extern unsigned long seq_offset;
1594 
1595 /* 
1596  *      Called by ddi.c on kernel startup.  
1597  */
1598  
1599 void inet_proto_init(struct ddi_proto *pro)
     /* [previous][next][first][last][top][bottom][index][help] */
1600 {
1601         struct inet_protocol *p;
1602         int i;
1603 
1604         printk("Swansea University Computer Society Net2Debugged [1.28]\n");
1605         /* Set up our UNIX VFS major device. */
1606         if (register_chrdev(AF_INET_MAJOR, "af_inet", &inet_fops) < 0) 
1607         {
1608                 printk("%s: cannot register major device %d!\n",
1609                                         pro->name, AF_INET_MAJOR);
1610                 return;
1611         }
1612 
1613         /* Tell SOCKET that we are alive... */
1614         (void) sock_register(inet_proto_ops.family, &inet_proto_ops);
1615 
1616         seq_offset = CURRENT_TIME*250;
1617 
1618         /* Add all the protocols. */
1619         for(i = 0; i < SOCK_ARRAY_SIZE; i++) 
1620         {
1621                 tcp_prot.sock_array[i] = NULL;
1622                 udp_prot.sock_array[i] = NULL;
1623                 raw_prot.sock_array[i] = NULL;
1624         }
1625         printk("IP Protocols: ");
1626         for(p = inet_protocol_base; p != NULL;) 
1627         {
1628                 struct inet_protocol *tmp;
1629         
1630                 tmp = (struct inet_protocol *) p->next;
1631                 inet_add_protocol(p);
1632                 printk("%s%s",p->name,tmp?", ":"\n");
1633                 p = tmp;
1634         }
1635 
1636 }

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