root/net/tcp/tcp.c

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

DEFINITIONS

This source file includes following definitions.
  1. min
  2. print_th
  3. get_firstr
  4. diff
  5. tcp_time_wait
  6. tcp_retransmit
  7. tcp_err
  8. tcp_readable
  9. tcp_select
  10. tcp_ioctl
  11. tcp_check
  12. tcp_send_check
  13. tcp_send_partial
  14. tcp_send_ack
  15. tcp_build_header
  16. tcp_write
  17. tcp_sendto
  18. tcp_read_wakeup
  19. cleanup_rbuf
  20. tcp_read_urg
  21. tcp_read
  22. tcp_shutdown
  23. tcp_recvfrom
  24. tcp_reset
  25. tcp_conn_request
  26. tcp_close
  27. tcp_write_xmit
  28. tcp_ack
  29. tcp_data
  30. tcp_urg
  31. tcp_fin
  32. tcp_accept
  33. tcp_connect
  34. tcp_sequence
  35. tcp_options
  36. tcp_rcv
  37. tcp_write_wakeup

   1  /* tcp.c */
   2  /*
   3      Copyright (C) 1992  Ross Biro
   4 
   5      This program is free software; you can redistribute it and/or modify
   6      it under the terms of the GNU General Public License as published by
   7      the Free Software Foundation; either version 2, or (at your option)
   8      any later version.
   9 
  10      This program is distributed in the hope that it will be useful,
  11      but WITHOUT ANY WARRANTY; without even the implied warranty of
  12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13      GNU General Public License for more details.
  14 
  15      You should have received a copy of the GNU General Public License
  16      along with this program; if not, write to the Free Software
  17      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
  18 
  19      The Author may be reached as bir7@leland.stanford.edu or
  20      C/O Department of Mathematics; Stanford University; Stanford, CA 94305
  21 
  22       Modifications for half-duplex connections base on those by, 
  23       Tim MacKenzie (tym@dibbler.cs.moansh.edu.au)
  24 
  25  */
  26 /* $Id: tcp.c,v 0.8.4.12 1992/12/12 19:25:04 bir7 Exp $ */
  27 /* $Log: tcp.c,v $
  28  * Revision 0.8.4.12  1992/12/12  19:25:04  bir7
  29  * Fixed anti-memory Leak in shutdown.
  30  *
  31  * Revision 0.8.4.11  1992/12/12  01:50:49  bir7
  32  * Fixed several bugs including half-duplex connections.
  33  *
  34  * Revision 0.8.4.10  1992/12/08  20:49:15  bir7
  35  * Fixed minor bugs and checked out MSS.
  36  *
  37  * Revision 0.8.4.9  1992/12/06  23:29:59  bir7
  38  * Added support for mss and half completed packets.  Also added
  39  * support for shrinking windows.
  40  *
  41  * Revision 0.8.4.8  1992/12/05  21:35:53  bir7
  42  *
  43  * Revision 0.8.4.7  1992/12/03  19:52:20  bir7
  44  * fixed = <-> == bug.
  45  *
  46  * Revision 0.8.4.6  1992/11/18  15:38:03  bir7
  47  * fixed minor problem in waiting for memory.
  48  *
  49  * Revision 0.8.4.4  1992/11/16  16:13:40  bir7
  50  * Fixed some error returns and undid one of the accept changes.
  51  *
  52  * Revision 0.8.4.3  1992/11/15  14:55:30  bir7
  53  * Fixed problem with accept.  It was charging the memory for the initial
  54  * sock buff to one socket, but freeing it from another.
  55  *
  56  * Revision 0.8.4.2  1992/11/10  10:38:48  bir7
  57  * Change free_s to kfree_s and accidently changed free_skb to kfree_skb.
  58  *
  59  * Revision 0.8.4.1  1992/11/10  00:17:18  bir7
  60  * version change only.
  61  *
  62  * Revision 0.8.3.3  1992/11/10  00:14:47  bir7
  63  * Changed malloc to kmalloc and added Id and Log
  64  *
  65  */
  66 
  67 #include <linux/types.h>
  68 #include <linux/sched.h>
  69 #include <linux/mm.h>
  70 #include <linux/string.h>
  71 #include <linux/socket.h>
  72 #include <netinet/in.h>
  73 #include <linux/fcntl.h>
  74 #include "timer.h"
  75 #include "ip.h"
  76 #include "icmp.h"
  77 #include "tcp.h"
  78 #include "sock.h"
  79 #include "arp.h"
  80 #include <linux/errno.h>
  81 #include <linux/timer.h>
  82 #include <asm/system.h>
  83 #include <asm/segment.h>
  84 #include <linux/mm.h>
  85 /* #include <signal.h>*/
  86 #include <linux/termios.h> /* for ioctl's */
  87 
  88 #ifdef PRINTK
  89 #undef PRINTK
  90 #endif
  91 
  92 #undef TCP_DEBUG
  93 
  94 #ifdef TCP_DEBUG
  95 #define PRINTK printk
  96 #else
  97 #define PRINTK dummy_routine
  98 #endif
  99 
 100 #define tmax(a,b) (before ((a),(b)) ? (b) : (a))
 101 #define swap(a,b) {unsigned long c; c=a; a=b; b=c;}
 102 
 103 extern struct proto tcp_prot;
 104 
 105 static  int 
 106 min (unsigned int a, unsigned int b)
     /* [previous][next][first][last][top][bottom][index][help] */
 107 {
 108    if (a < b) return (a);
 109    return (b);
 110 }
 111 
 112 void
 113 print_th (struct tcp_header *th)
     /* [previous][next][first][last][top][bottom][index][help] */
 114 {
 115    unsigned char *ptr;
 116    ptr = (unsigned char *)(th + 1);
 117    PRINTK ("tcp header:\n");
 118    PRINTK ("  source=%d, dest=%d, seq =%d, ack_seq = %d\n",
 119            net16(th->source), net16(th->dest), net32(th->seq),
 120            net32(th->ack_seq));
 121    PRINTK ("  fin=%d, syn=%d, rst=%d, psh=%d, ack=%d, urg=%d res1=%d res2=%d\n"
 122            ,th->fin, th->syn, th->rst, th->psh, th->ack, th->urg,
 123            th->res1, th->res2);
 124    PRINTK ("  window = %d, check = %d urg_ptr = %d\n",
 125            net16(th->window), net16(th->check), net16(th->urg_ptr));
 126    PRINTK ("  doff = %d\n",th->doff);
 127    PRINTK ("options = %d %d %d %d\n", ptr[0], ptr[1], ptr[2], ptr[3]);
 128  }
 129 
 130  /* This routine grabs the first thing off of a rcv queue. */
 131 static  struct sk_buff *
 132 get_firstr(volatile struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 133 {
 134    struct sk_buff *skb;
 135    skb = sk->rqueue;
 136    if (skb == NULL) return (NULL);
 137    sk->rqueue = skb->next;
 138    if (sk->rqueue == skb)
 139      {
 140        sk->rqueue = NULL;
 141      }
 142    else
 143      {
 144        sk->rqueue->prev=skb->prev;
 145        sk->rqueue->prev->next = sk->rqueue;
 146      }
 147    return (skb);
 148 }
 149 
 150 static  long
 151 diff (unsigned long seq1, unsigned long seq2)
     /* [previous][next][first][last][top][bottom][index][help] */
 152 {
 153    long d;
 154    d=seq1-seq2;
 155    if (d > 0) return (d);
 156    /* I hope this returns what I want. */
 157    return (~d+1);
 158 }
 159 
 160  /* enter the time wait state. */
 161 static  void
 162 tcp_time_wait (volatile struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 163 {
 164    sk->state = TCP_TIME_WAIT;
 165    sk->shutdown = SHUTDOWN_MASK;
 166    if (!sk->dead) wake_up (sk->sleep);
 167    sk->time_wait.len = TCP_TIMEWAIT_LEN;
 168    sk->timeout = TIME_CLOSE;
 169    reset_timer ((struct timer *)&sk->time_wait);
 170 }
 171 
 172 static void
 173 tcp_retransmit (volatile struct sock *sk, int all)
     /* [previous][next][first][last][top][bottom][index][help] */
 174 {
 175    if (all) 
 176      {
 177         ip_retransmit (sk, all);
 178         return;
 179      }
 180    sk->rtt *= 2; /* exponential back off. */
 181    if (sk->cong_window > 1)
 182      sk->cong_window = sk->cong_window / 2;
 183    sk->exp_growth = 0;
 184 
 185    /* do the actuall retransmit. */
 186    ip_retransmit (sk, all);
 187    
 188 }
 189 
 190 /* this routine is called by the icmp module when it gets some
 191    sort of error condition.  If err < 0 then the socket should
 192    be closed and the error returned to the user.  If err > 0
 193    it's just the icmp type << 8 | icmp code.  
 194    header points to the first 8 bytes of the tcp header.  We need
 195    to find the appropriate port. */
 196 
 197 void
 198 tcp_err (int err, unsigned char *header, unsigned long daddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 199          unsigned long saddr, struct ip_protocol *protocol)
 200 {
 201    struct tcp_header *th;
 202    volatile struct sock *sk;
 203    
 204    PRINTK ("tcp_err(err=%d, header=%X, daddr=%X saddr=%X, protocol=%X)\n",
 205            err, header, daddr, saddr, protocol);
 206 
 207    th = (struct tcp_header *)header;
 208    sk = get_sock (&tcp_prot, net16(th->dest), saddr, th->source, daddr);
 209    print_th (th);
 210 
 211    if (sk == NULL) return;
 212 
 213    if (err & 0xff00 == (ICMP_SOURCE_QUENCH << 8))
 214      {
 215         /* for now we will just trigger a linear backoff. The slow start
 216            code should cause a real backoff here. */
 217 
 218         if (sk->cong_window > 1)
 219           sk->cong_window --;
 220 
 221         return;
 222      }
 223 
 224    printk ("tcp.c: icmp_err got error\n");
 225    sk->err = icmp_err_convert[err & 0xff].errno;
 226    if (icmp_err_convert[err & 0xff].fatal)
 227      {
 228         if (sk->state != TCP_ESTABLISHED)
 229           {
 230             sk->state = TCP_CLOSE;
 231           }
 232         sk->prot->close(sk, 0);
 233      }
 234 
 235    return;
 236    
 237 }
 238 
 239 static int
 240 tcp_readable (volatile struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 241 {
 242   unsigned long counted;
 243   unsigned long amount;
 244   struct sk_buff *skb;
 245   int count=0;
 246   int sum;
 247 
 248   if (sk == NULL || sk->rqueue == NULL) return (0);
 249 
 250   counted = sk->copied_seq;
 251   amount = 0;
 252   skb = sk->rqueue->next;
 253 
 254   /* go until a push or until we are out of data.  */
 255   do {
 256     count ++;
 257     if (count > 20)
 258       {
 259         printk ("tcp_readable, more than 20 packets without a psh\n");
 260         printk ("possible read_queue corruption.\n");
 261         return (amount);
 262       }
 263     if (before (counted+1, skb->h.th->seq)) break;
 264     sum = skb->len - ( counted - skb->h.th->seq);
 265     if (skb->h.th->syn) sum ++;
 266     if (sum >= 0)
 267       {
 268         amount += sum;
 269         if (skb->h.th->syn) amount --;
 270         counted += sum;
 271         if (skb->h.th->psh) break;
 272       }
 273     skb = skb->next;
 274   } while (skb != sk->rqueue->next);
 275   return (amount);
 276 }
 277 
 278 
 279 static int
 280 tcp_select (volatile struct sock *sk, int sel_type, select_table *wait)
     /* [previous][next][first][last][top][bottom][index][help] */
 281 {
 282   sk->inuse = 1;
 283   switch (sel_type)
 284     {
 285     case SEL_IN:
 286        select_wait (sk->sleep, wait);
 287        if (sk->rqueue != NULL)
 288          {
 289            if (sk->state == TCP_LISTEN || tcp_readable(sk))
 290              {
 291                release_sock (sk);
 292                return (1);
 293              }
 294          }
 295 
 296        if (sk->shutdown & RCV_SHUTDOWN)
 297          {
 298            release_sock (sk);
 299            return (1);
 300          }
 301        else
 302          {
 303            release_sock (sk);
 304            return (0);
 305         }
 306 
 307     case SEL_OUT:
 308        select_wait (sk->sleep, wait);
 309 
 310        if (sk->shutdown & SEND_SHUTDOWN)
 311          {
 312            release_sock (sk);
 313            return (0);
 314          }
 315        /* hack so it will probably be able to write something
 316           if it says it's ok to write. */
 317        if (sk->prot->wspace(sk) >= MIN_WRITE_SPACE)
 318          {
 319            release_sock (sk);
 320            return (1);
 321          }
 322        release_sock (sk);
 323        return (0);
 324 
 325 
 326      case SEL_EX:
 327        select_wait(sk->sleep,wait);
 328        if (sk->err)
 329          {
 330            release_sock (sk);
 331            return (1);
 332          }
 333        release_sock (sk);
 334        return (0);
 335      }
 336 
 337   release_sock (sk);
 338   return (0);
 339 }
 340 
 341 static int
 342 tcp_ioctl (volatile struct sock *sk, int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 343 {
 344   PRINTK ("tcp_ioctl (sk=%X, cmd = %d, arg=%X)\n", sk, cmd, arg);
 345    switch (cmd)
 346      {
 347        default:
 348         return (-EINVAL);
 349 
 350       case TIOCINQ:
 351 /*      case FIONREAD:*/
 352         {
 353           unsigned long amount;
 354 
 355           if (sk->state == TCP_LISTEN)
 356             return (-EINVAL);
 357 
 358           amount = 0;
 359           sk->inuse = 1;
 360           if (sk->rqueue != NULL)
 361             {
 362               amount = tcp_readable(sk);
 363             }
 364           release_sock (sk);
 365           PRINTK ("returning %d\n", amount);
 366           verify_area ((void *)arg, sizeof (unsigned long));
 367           put_fs_long (amount, (unsigned long *)arg);
 368           return (0);
 369         }
 370 
 371        case SIOCATMARK:
 372         {
 373           struct sk_buff *skb;
 374           int answ=0;
 375           /* try to figure out if we need to read some urgent data. */
 376           sk->inuse = 1;
 377           if (sk->rqueue != NULL)
 378             {
 379               skb = sk->rqueue->next;
 380               if (sk->copied_seq+1 == skb->h.th->seq && skb->h.th->urg)
 381                 answ = 1;
 382             }
 383           release_sock (sk);
 384           verify_area ((void *) arg, sizeof (unsigned long));
 385           put_fs_long (answ, (void *) arg);
 386           return (0);
 387         }
 388        
 389       case TIOCOUTQ:
 390         {
 391           unsigned long amount;
 392           if (sk->state == TCP_LISTEN)
 393             return (-EINVAL);
 394           amount = sk->prot->wspace(sk)/2;
 395           verify_area ((void *)arg, sizeof (unsigned long));
 396           put_fs_long (amount, (unsigned long *)arg);
 397           return (0);
 398         }
 399 
 400      }
 401 }
 402 
 403 
 404 /* this routine computes a tcp checksum */
 405 static  unsigned short
 406 tcp_check (struct tcp_header *th, int len, unsigned long saddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 407            unsigned long daddr)
 408 {     
 409    unsigned long sum;
 410    
 411    if (saddr == 0) saddr = MY_IP_ADDR;
 412    print_th (th);
 413    __asm__("\t addl %%ecx,%%ebx\n"
 414            "\t adcl %%edx,%%ebx\n"
 415            "\t adcl $0, %%ebx\n"
 416            : "=b" (sum)
 417            : "0" (daddr), "c" (saddr), "d" ((net16(len) << 16) + IPPROTO_TCP*256)
 418            : "cx","bx","dx" );
 419    
 420    if (len > 3)
 421      {
 422         __asm__(
 423                 "\tclc\n"
 424                 "1:\n"
 425                 "\t lodsl\n"
 426                 "\t adcl %%eax, %%ebx\n"
 427                 "\t loop 1b\n"
 428                 "\t adcl $0, %%ebx\n"
 429                 : "=b" (sum) , "=S" (th)
 430                 : "0" (sum), "c" (len/4) ,"1" (th)
 431                 : "ax", "cx", "bx", "si" );
 432      }
 433    
 434    /* convert from 32 bits to 16 bits. */
 435    __asm__(
 436            "\t movl %%ebx, %%ecx\n"
 437            "\t shrl $16,%%ecx\n"
 438            "\t addw %%cx, %%bx\n"
 439            "\t adcw $0, %%bx\n"
 440            : "=b" (sum)
 441            : "0" (sum)
 442            : "bx", "cx");
 443    
 444    /* check for an extra word. */
 445    if ((len & 2) != 0)
 446      {
 447         __asm__("\t lodsw\n"
 448                 "\t addw %%ax,%%bx\n"
 449                 "\t adcw $0, %%bx\n"
 450                 : "=b" (sum), "=S" (th)
 451                 : "0" (sum) ,"1" (th)
 452                 : "si", "ax", "bx");
 453      }
 454    
 455    /* now check for the extra byte. */
 456    if ((len & 1) != 0)
 457      {
 458         __asm__("\t lodsb\n"
 459                 "\t movb $0,%%ah\n"
 460                 "\t addw %%ax,%%bx\n"
 461                 "\t adcw $0, %%bx\n"
 462                 : "=b" (sum)
 463                 : "0" (sum) ,"S" (th)
 464                 : "si", "ax", "bx");
 465      }
 466    
 467    /* we only want the bottom 16 bits, but we never cleared
 468       the top 16. */
 469    return ((~sum) & 0xffff);
 470 }
 471 
 472 
 473 static  void
 474 tcp_send_check (struct tcp_header *th, unsigned long saddr, 
     /* [previous][next][first][last][top][bottom][index][help] */
 475                  unsigned long daddr, int len, volatile struct sock *sk)
 476 {
 477 
 478    th->check = 0;
 479    if (sk && sk->no_check) return;
 480    th->check = tcp_check (th, len, saddr, daddr);
 481    return;
 482 }
 483 
 484 static void
 485 tcp_send_partial(volatile struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 486 {
 487   struct sk_buff *skb;
 488   
 489   if (sk == NULL || sk->send_tmp == NULL) return;
 490   
 491   skb = sk->send_tmp;
 492   /* we need to complete and send the packet. */
 493   tcp_send_check (skb->h.th, sk->saddr, sk->daddr,
 494                   skb->len-(unsigned long)skb->h.th +
 495                   (unsigned long)(skb+1), sk);
 496   
 497   skb->h.seq = sk->send_seq;
 498   if (after (sk->send_seq , sk->window_seq) ||
 499       sk->packets_out >= sk->cong_window)
 500     {
 501       PRINTK ("sk->cong_window = %d, sk->packets_out = %d\n",
 502               sk->cong_window, sk->packets_out);
 503       PRINTK ("sk->send_seq = %d, sk->window_seq = %d\n",
 504               sk->send_seq, sk->window_seq);
 505       skb->next = NULL;
 506       skb->magic = TCP_WRITE_QUEUE_MAGIC;
 507       if (sk->wback == NULL)
 508         {
 509           sk->wfront=skb;
 510         }
 511       else
 512         {
 513           sk->wback->next = skb;
 514         }
 515       sk->wback = skb;
 516     }
 517   else
 518     {
 519       sk->prot->queue_xmit (sk, skb->dev, skb,0);
 520     }
 521   sk->send_tmp = NULL;
 522 }
 523 
 524 
 525 
 526 /* This routine sends an ack and also updates the window. */
 527 static  void
 528 tcp_send_ack (unsigned long sequence, unsigned long ack,
     /* [previous][next][first][last][top][bottom][index][help] */
 529                volatile struct sock *sk,
 530                struct tcp_header *th, unsigned long daddr)
 531 {
 532    struct sk_buff *buff;
 533    struct tcp_header *t1;
 534    struct device *dev=NULL;
 535    int tmp;
 536 
 537    /* we need to grab some memory, and put together an ack, and then
 538       put it into the queue to be sent. */
 539 
 540    buff=sk->prot->wmalloc(sk,MAX_ACK_SIZE,1, GFP_ATOMIC);
 541    if (buff == NULL) 
 542      {
 543         /* force it to send an ack. */
 544         sk->ack_backlog++;
 545         if (sk->timeout != TIME_WRITE && tcp_connected (sk->state))
 546           {
 547              sk->timeout = TIME_WRITE;
 548              sk->time_wait.len = 10; /* got to do it quickly. */
 549              reset_timer ((struct timer *)&sk->time_wait);
 550           }
 551         return;
 552      }
 553 
 554    buff->mem_addr = buff;
 555    buff->mem_len = MAX_ACK_SIZE;
 556    buff->lock = 0;
 557    buff->len=sizeof (struct tcp_header);
 558    buff->sk = sk;
 559    t1 = (struct tcp_header *)(buff + 1);
 560    /* put in the ip_header and routing stuff. */
 561    tmp = sk->prot->build_header (buff, sk->saddr, daddr, &dev,
 562                                  IPPROTO_TCP, sk->opt, MAX_ACK_SIZE);
 563    if (tmp < 0)
 564      {
 565        sk->prot->wfree(sk, buff->mem_addr, buff->mem_len);
 566        return;
 567      }
 568    buff->len += tmp;
 569    t1 = (struct tcp_header *)((char *)t1 +tmp);
 570 
 571    memcpy (t1, th, sizeof (*t1)); /* this should probably be removed. */
 572 
 573    /* swap the send and the receive. */
 574    t1->dest = th->source;
 575    t1->source = th->dest;
 576    t1->seq = net32(sequence);
 577    t1->ack = 1;
 578    sk->window = sk->prot->rspace(sk);
 579    t1->window = net16(sk->window);
 580    t1->res1=0;
 581    t1->res2=0;
 582    t1->rst = 0;
 583    t1->urg = 0;
 584    t1->syn = 0;
 585    t1->psh = 0;
 586    t1->fin = 0;
 587    if (ack == sk->acked_seq)
 588      {
 589         sk->ack_backlog = 0;
 590         sk->bytes_rcv = 0;
 591         sk->ack_timed = 0;
 592         if (sk->send_head == NULL &&
 593             sk->wfront == NULL)
 594           {
 595              delete_timer((struct timer *)&sk->time_wait);
 596              sk->timeout = 0;
 597           }
 598         
 599      }
 600    t1->ack_seq = net32(ack);
 601    t1->doff = sizeof (*t1)/4;
 602    tcp_send_check (t1, sk->saddr, daddr, sizeof (*t1), sk);
 603    sk->prot->queue_xmit(sk, dev, buff, 1);
 604 }
 605 
 606 /* this routine builds a generic tcp header. */
 607 static  int
 608 tcp_build_header(struct tcp_header *th, volatile struct sock *sk, int push)
     /* [previous][next][first][last][top][bottom][index][help] */
 609 {
 610 
 611  /* want to get rid of this. */
 612   memcpy (th,(void *) &(sk->dummy_th), sizeof (*th));
 613   th->seq = net32(sk->send_seq);
 614   th->psh = (push == 0) ? 1 : 0;
 615   th->doff = sizeof (*th)/4;
 616   th->ack = 1;
 617   th->fin = 0;
 618   sk->ack_backlog = 0;
 619   sk->bytes_rcv = 0;
 620   sk->ack_timed = 0;
 621   th->ack_seq = net32(sk->acked_seq);
 622   sk->window = sk->prot->rspace(sk);
 623   th->window = net16(sk->window);
 624 
 625   return (sizeof (*th));
 626 }
 627 
 628 /* This routine copies from a user buffer into a socket, and starts
 629    the transmit system. */
 630 
 631 static int
 632 tcp_write (volatile struct sock *sk, unsigned char *from,
     /* [previous][next][first][last][top][bottom][index][help] */
 633            int len, int nonblock, unsigned flags)
 634 {
 635   int copied=0;
 636   int copy;
 637   int tmp;
 638   struct sk_buff *skb;
 639   unsigned char *buff;
 640   struct proto *prot;
 641   struct device *dev=NULL;
 642 
 643   PRINTK ("tcp_write (sk=%X, from=%X, len=%d, nonblock=%d, flags=%X)\n",
 644           sk, from, len, nonblock, flags);
 645 
 646   print_sk (sk);
 647 
 648   prot = sk->prot;
 649   while (len > 0)
 650     {
 651        /* first thing we do is make sure that we are established. */     
 652 
 653       sk->inuse = 1; /* no one else will use this socket. */
 654       while (sk->state != TCP_ESTABLISHED && sk->state != TCP_CLOSE_WAIT)
 655         {
 656           if (sk->state != TCP_SYN_SENT &&
 657               sk->state != TCP_SYN_RECV)
 658            {
 659               release_sock (sk);
 660               PRINTK ("tcp_write: return 1\n");
 661               if (copied) return (copied);
 662 
 663               if (sk->err)
 664                 {
 665                   tmp = -sk->err;
 666                   sk->err = 0;
 667                   return (tmp);
 668                 }
 669 
 670               if (sk->keepopen)
 671                 {
 672                     send_sig (SIGPIPE, current, 0);
 673                 }
 674               return (-EPIPE);
 675             }
 676 
 677           if (nonblock)
 678             {
 679               release_sock (sk);
 680               PRINTK ("tcp_write: return 2\n");
 681               if (copied) return (copied);
 682               return (-EAGAIN);
 683             }
 684 
 685           /* now here is a race condition.
 686              release_sock could cause the connection to
 687              enter the established mode, if that is the
 688              case, then we will block here for ever, because
 689              we will have gotten our wakeup call before we
 690              go to sleep. */
 691           release_sock (sk);
 692           cli();
 693           if (sk->state != TCP_ESTABLISHED && sk->state != TCP_CLOSE_WAIT &&
 694               sk->err == 0)
 695             {
 696               interruptible_sleep_on (sk->sleep);
 697               if (current->signal & ~current->blocked)
 698                 {
 699                    sti();
 700                    PRINTK ("tcp_write: return 3\n");
 701                    if (copied) return (copied);
 702                    return (-ERESTARTSYS);
 703                 }
 704             }
 705           sti();
 706           sk->inuse = 1;
 707         }
 708 
 709       /* Now we need to check if we have a half built packet. */
 710       if (sk->send_tmp != NULL)
 711         {
 712           /* if sk->mss has been changed this could cause problems. */
 713           /* add more stuff to the end of skb->len*/
 714           skb = sk->send_tmp;
 715           if (!(flags & MSG_OOB))
 716             {
 717               copy = min (sk->mss - skb->len + 128 + prot->max_header, len);
 718               
 719               /* this is really a bug. */
 720               if (copy <= 0)
 721                 copy = 0;
 722           
 723               memcpy_fromfs ((unsigned char *)(skb+1) + skb->len, from, copy);
 724               skb->len += copy;
 725               from += copy;
 726               copied += copy;
 727               len -= copy;
 728               sk->send_seq += copy;
 729             }
 730 
 731           if (skb->len - (unsigned long)skb->h.th +
 732               (unsigned long)(skb+1) >= sk->mss
 733               || (flags & MSG_OOB))
 734             {
 735               tcp_send_partial (sk);
 736             }
 737           continue;
 738           
 739         }
 740 
 741       /* we also need to worry about the window.  The smallest we
 742          will send is about 200 bytes. */
 743 
 744       copy = min (sk->mtu, diff(sk->window_seq, sk->send_seq));
 745 
 746       /* redundent check here. */
 747       if (copy < 200 || copy > sk->mtu) copy = sk->mtu;
 748       copy = min (copy, len);
 749 
 750       /* we should really check the window here also. */
 751       if (sk->packets_out && copy < sk->mss && !(flags & MSG_OOB)) 
 752         {
 753           /* we will release the socket incase we sleep here. */
 754           release_sock (sk);
 755           skb=prot->wmalloc (sk,
 756                              sk->mss + 128 + prot->max_header + sizeof (*skb),
 757                              0, GFP_KERNEL);
 758           sk->inuse = 1;
 759           sk->send_tmp = skb;
 760           if (skb != NULL)
 761             skb->mem_len = sk->mss + 128 + prot->max_header+sizeof (*skb);
 762         }
 763       else
 764         {
 765           /* we will release the socket incase we sleep here. */
 766           release_sock (sk);
 767           skb=prot->wmalloc (sk, copy + prot->max_header+sizeof (*skb),0,
 768                              GFP_KERNEL);
 769           sk->inuse = 1;
 770           if (skb != NULL)
 771             skb->mem_len = copy+prot->max_header+sizeof (*skb);
 772         }
 773 
 774       /* if we didn't get any memory, we need to sleep. */
 775       if (skb == NULL)
 776         {
 777           if (nonblock)
 778             {
 779               release_sock (sk);
 780               PRINTK ("tcp_write: return 4\n");
 781               if (copied) return (copied);
 782               return (-EAGAIN);
 783             }
 784 
 785           /* here is another race condition. */
 786           tmp = sk->wmem_alloc;
 787           release_sock (sk);
 788 
 789           /* again we will try to avoid it. */
 790           cli ();
 791           if (tmp <= sk->wmem_alloc 
 792               && (sk->state == TCP_ESTABLISHED || sk->state == TCP_CLOSE_WAIT )
 793               && sk->err == 0)
 794             {
 795               interruptible_sleep_on (sk->sleep);
 796               if (current->signal & ~current->blocked)
 797                 {
 798                    sti();
 799                    PRINTK ("tcp_write: return 5\n");
 800                    if (copied) return (copied);
 801                    return (-ERESTARTSYS);
 802                 }
 803             }
 804           sk->inuse = 1;
 805           sti();
 806           continue;
 807         }
 808 
 809       skb->mem_addr = skb;
 810       skb->len = 0;
 811       skb->sk = sk;
 812       skb->lock = 0;
 813       skb->free = 0;
 814 
 815       buff =(unsigned char *)( skb+1);
 816        /* we need to optimize this.  Perhaps some hints here
 817           would be good. */
 818 
 819       tmp = prot->build_header (skb, sk->saddr, sk->daddr, &dev,
 820                                 IPPROTO_TCP, sk->opt, skb->mem_len);
 821       if (tmp < 0 )
 822         {
 823           prot->wfree (sk, skb->mem_addr, skb->mem_len);
 824           release_sock (sk);
 825           PRINTK ("tcp_write: return 6\n");
 826           if (copied) return (copied);
 827           return (tmp);
 828         }
 829       skb->len += tmp;
 830       skb->dev = dev;
 831       buff+=tmp;
 832       skb->h.th =(struct tcp_header *) buff;
 833       tmp = tcp_build_header((struct tcp_header *)buff, sk, len-copy);
 834 
 835       if (tmp < 0)
 836         {
 837           prot->wfree (sk, skb->mem_addr, skb->mem_len);
 838           release_sock (sk);
 839           PRINTK ("tcp_write: return 7\n");
 840           if (copied) return (copied);
 841           return (tmp);
 842         }
 843 
 844       if (flags & MSG_OOB)
 845         {
 846                 ((struct tcp_header *)buff)->urg = 1;
 847                 ((struct tcp_header *)buff)->urg_ptr = copy;
 848         }
 849       skb->len += tmp;
 850       memcpy_fromfs (buff+tmp, from, copy);
 851 
 852       from += copy;
 853       copied += copy;
 854       len -= copy;
 855       skb->len += copy;
 856       skb->free = 0;
 857       sk->send_seq += copy;
 858 
 859       if (sk->send_tmp != NULL)
 860         {
 861           continue;
 862         }
 863 
 864       tcp_send_check ((struct tcp_header *)buff, sk->saddr, sk->daddr,
 865                        copy +sizeof (struct tcp_header), sk);
 866 
 867 
 868       skb->h.seq = sk->send_seq;
 869       if (after (sk->send_seq , sk->window_seq) ||
 870           sk->packets_out >= sk->cong_window)
 871         {
 872           PRINTK ("sk->cong_window = %d, sk->packets_out = %d\n",
 873                   sk->cong_window, sk->packets_out);
 874           PRINTK ("sk->send_seq = %d, sk->window_seq = %d\n",
 875                   sk->send_seq, sk->window_seq);
 876           skb->next = NULL;
 877           skb->magic = TCP_WRITE_QUEUE_MAGIC;
 878           if (sk->wback == NULL)
 879             {
 880               sk->wfront=skb;
 881             }
 882           else
 883             {
 884               sk->wback->next = skb;
 885             }
 886           sk->wback = skb;
 887         }
 888       else
 889         {
 890           prot->queue_xmit (sk, dev, skb,0);
 891         }
 892     }
 893   sk->err = 0;
 894   release_sock (sk);
 895   PRINTK ("tcp_write: return 8\n");
 896   return (copied);
 897 }
 898 
 899 static int
 900 tcp_sendto (volatile struct sock *sk, unsigned char *from,
     /* [previous][next][first][last][top][bottom][index][help] */
 901             int len, int nonblock, unsigned flags,
 902             struct sockaddr_in *addr, int addr_len)
 903 {
 904   struct sockaddr_in sin;
 905   if (addr_len < sizeof (sin))
 906     return (-EINVAL);
 907   memcpy_fromfs (&sin, addr, sizeof (sin));
 908   if (sin.sin_family && sin.sin_family != AF_INET)
 909     return (-EINVAL);
 910   if (sin.sin_port != sk->dummy_th.dest)
 911     return (-EINVAL);
 912   if (sin.sin_addr.s_addr != sk->daddr)
 913     return (-EINVAL);
 914   return (tcp_write (sk, from, len, nonblock, flags));
 915 }
 916 
 917 static  void
 918 tcp_read_wakeup(volatile struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 919 {
 920   int tmp;
 921   struct device *dev = NULL;
 922   struct tcp_header *t1;
 923   struct sk_buff *buff;
 924 
 925   if (!sk->ack_backlog ) return;
 926   PRINTK ("in tcp read wakeup\n");
 927   /* we need to put code here to prevent this routine from being called. */
 928   /* being called once in a while is ok, so only check if this is the
 929      second time in a row. */
 930 
 931   /* we need to grab some memory, and put together an ack, and then
 932      put it into the queue to be sent. */
 933 
 934   buff=sk->prot->wmalloc(sk,MAX_ACK_SIZE,1, GFP_ATOMIC);
 935   if (buff == NULL) 
 936     {
 937        /* try again real soon. */
 938        sk->timeout = TIME_WRITE;
 939        sk->time_wait.len = 10;
 940        reset_timer((struct timer *) &sk->time_wait);
 941        return;
 942     }
 943 
 944   buff->mem_addr = buff;
 945   buff->mem_len = MAX_ACK_SIZE;
 946   buff->lock = 0;
 947   buff->len=sizeof (struct tcp_header);
 948   buff->sk = sk;
 949 
 950   /* put in the ip_header and routing stuff. */
 951   tmp = sk->prot->build_header (buff, sk->saddr, sk->daddr, &dev,
 952                                 IPPROTO_TCP, sk->opt, MAX_ACK_SIZE);
 953   if (tmp < 0)
 954     {
 955       sk->prot->wfree(sk, buff->mem_addr, buff->mem_len);
 956       return;
 957     }
 958 
 959   buff->len += tmp;
 960   t1 = (struct tcp_header *)((char *)(buff+1) +tmp);
 961 
 962   memcpy (t1,(void *) &sk->dummy_th, sizeof (*t1));
 963   t1->seq = net32(sk->send_seq);
 964   t1->ack = 1;
 965   t1->res1=0;
 966   t1->res2=0;
 967   t1->rst = 0;
 968   t1->urg = 0;
 969   t1->syn = 0;
 970   t1->psh = 0;
 971   sk->ack_backlog = 0;
 972   sk->bytes_rcv = 0;
 973   sk->window = sk->prot->rspace(sk);
 974   t1->window = net16(sk->window);
 975   t1->ack_seq = net32(sk->acked_seq);
 976   t1->doff = sizeof (*t1)/4;
 977   tcp_send_check (t1, sk->saddr, sk->daddr, sizeof (*t1), sk);
 978   sk->prot->queue_xmit(sk, dev, buff, 1);
 979 }
 980 
 981 
 982 /* This routine frees used buffers. */
 983 /* It should consider sending an ack to let the
 984    other end know we now have a bigger window. */
 985 
 986 static  void
 987 cleanup_rbuf (volatile struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 988 {
 989 /*   PRINTK ("cleaning rbuf for sk=%X\n",sk);*/
 990   /* we have to loop through all the buffer headers, and 
 991      try to free up all the space we can. */
 992   while (sk->rqueue != NULL )
 993     {
 994       struct sk_buff *skb;
 995       skb=sk->rqueue->next;
 996       if (!skb->used) break;
 997       if (sk->rqueue == skb)
 998         {
 999           sk->rqueue = NULL;
1000         }
1001       else
1002         {
1003           skb->next->prev = skb->prev;
1004           skb->prev->next = skb->next;
1005         }
1006       skb->sk = sk;
1007       kfree_skb (skb, FREE_READ);
1008     }
1009    /* at this point we should send an ack if the difference in
1010       the window, and the amount of space is bigger than
1011       TCP_WINDOW_DIFF */
1012    PRINTK ("sk->window left = %d, sk->prot->rspace(sk)=%d\n",
1013            sk->window - sk->bytes_rcv, sk->prot->rspace(sk));
1014 
1015    if ((sk->prot->rspace(sk) >
1016         (sk->window - sk->bytes_rcv + TCP_WINDOW_DIFF)))
1017      {
1018        sk->ack_backlog++;
1019        if (sk->ack_backlog > sk->max_ack_backlog)
1020          {
1021            tcp_read_wakeup (sk);
1022          }
1023        else
1024          {
1025            /* force it to send an ack soon. */
1026            if ( before (jiffies + TCP_ACK_TIME, sk->time_wait.when))
1027              {
1028                sk->time_wait.len = TCP_ACK_TIME;
1029                sk->timeout = TIME_WRITE;
1030                reset_timer ((struct timer *)&sk->time_wait);
1031              }
1032          }
1033      }
1034 
1035 }
1036 
1037 /* handle reading urgent data. */
1038 static  int
1039 tcp_read_urg(volatile struct sock * sk, int nonblock,
     /* [previous][next][first][last][top][bottom][index][help] */
1040              unsigned char *to, int len, unsigned flags)
1041 {
1042     int copied = 0;
1043     struct sk_buff *skb;
1044     PRINTK ("tcp_read_urg(sk=%X, to=%X, len=%d, flags=%X)\n",
1045             sk, to, len, flags);
1046     print_sk(sk);
1047     while (len > 0)
1048       {
1049          sk->inuse = 1;
1050           while (sk->urg==0 || sk->rqueue == NULL)
1051             {
1052               if (sk->err)
1053                 {
1054                   int tmp;
1055                   release_sock (sk);
1056                   if (copied) return (copied);
1057                   tmp = -sk->err;
1058                   sk->err = 0;
1059                   return (tmp);
1060                 }
1061 
1062                if (sk->state == TCP_CLOSE)
1063                  {
1064                    release_sock (sk);
1065                    if (copied) return (copied);
1066                    if (!sk->done)
1067                      {
1068                        sk->done = 1;
1069                        return (0);
1070                      }
1071                    return (-ENOTCONN);
1072                  }
1073                  
1074                 if (sk->shutdown & RCV_SHUTDOWN)
1075                   {
1076                     release_sock(sk);
1077                     if (copied == 0)
1078                       sk->done = 1;
1079                     return (copied);
1080                   }
1081 
1082               if (nonblock)
1083                 {
1084                   release_sock (sk);
1085                   if (copied) return (copied);
1086                   return (-EAGAIN);
1087                 }
1088 
1089                /* now at this point, we may have gotten some data. */
1090                release_sock (sk);
1091                cli();
1092                if ((sk->urg == 0 || sk->rqueue == NULL) && sk->err == 0 
1093                    && !(sk->shutdown & RCV_SHUTDOWN) )
1094                  {
1095                     interruptible_sleep_on (sk->sleep);
1096                     if (current->signal & ~current->blocked)
1097                       {
1098                          sti();
1099                          if (copied) return (copied);
1100                          return (-ERESTARTSYS);
1101                       }
1102                  }
1103                sti();
1104                sk->inuse = 1;
1105             }
1106           /* now we have some urgent data, we must find it.*/
1107           for (skb = sk->rqueue->next; skb->next != sk->rqueue;
1108                skb = skb->next)
1109             {
1110                 int offset;
1111                 int amt;
1112                 if (!skb->h.th->urg) continue;
1113                 offset = 0;
1114                 amt = min(skb->h.th->urg_ptr,len);
1115                 verify_area (to, amt);
1116                 memcpy_tofs (to, (unsigned char *)(skb->h.th) +
1117                              skb->h.th->doff*4
1118                              + offset, amt);
1119 
1120                 if (!(flags & MSG_PEEK))
1121                   {
1122                      skb->urg_used = 1;
1123                      sk->urg --;
1124                   }
1125                 release_sock (sk);
1126                 copied += amt;
1127                 return (copied);
1128             }
1129       }
1130     return (0);
1131 }
1132 
1133 /* This routine copies from a sock struct into the user buffer. */
1134 static  int
1135 tcp_read (volatile struct sock *sk, unsigned char *to,
     /* [previous][next][first][last][top][bottom][index][help] */
1136           int len, int nonblock, unsigned flags)
1137 {
1138     int copied=0; /* will be used to say how much has been copied. */
1139     struct sk_buff *skb;
1140     unsigned long offset;
1141     unsigned long used;
1142 
1143     if (len == 0) return (0);
1144     if (len < 0) 
1145       {
1146          return (-EINVAL);
1147       }
1148     
1149     /* this error should be checked. */
1150     if (sk->state == TCP_LISTEN) return (-ENOTCONN);
1151 
1152     /* urgent data needs to be handled specially. */
1153     if ((flags & MSG_OOB))
1154       return (tcp_read_urg (sk, nonblock, to, len, flags));
1155 
1156     /* so no-one else will use this socket. */
1157     sk->inuse = 1;
1158     if (sk->rqueue != NULL)
1159       skb=sk->rqueue->next;
1160     else
1161       skb = NULL;
1162 
1163     PRINTK("tcp_read (sk=%X, to=%X, len=%d, nonblock=%d, flags=%X)\n",
1164            sk, to, len, nonblock, flags);
1165 
1166     while ( len > 0)
1167       {
1168           while ( skb == NULL || before (sk->copied_seq+1, skb->h.th->seq) ||
1169                  skb->used) /* skb->used just checks to see if we've
1170                                gone all the way around. */
1171             {
1172 
1173                PRINTK("skb = %X:\n",skb);
1174                print_skb(skb);
1175                print_sk (sk);
1176 
1177                cleanup_rbuf(sk);
1178 
1179                if (sk->err)
1180                  {
1181                    int tmp;
1182                    release_sock (sk);
1183                    if (copied) return (copied);
1184                    tmp = -sk->err;
1185                    sk->err = 0;
1186                    return (tmp);
1187                  }
1188 
1189                if (sk->state == TCP_CLOSE)
1190                  {
1191                    release_sock (sk);
1192                    if (copied) return (copied);
1193                    if (!sk->done)
1194                      {
1195                        sk->done = 1;
1196                        return (0);
1197                      }
1198                    return (-ENOTCONN);
1199                  }
1200 
1201                if (sk->shutdown & RCV_SHUTDOWN)
1202                    {
1203                      release_sock (sk);
1204                      if (copied == 0) sk->done = 1;
1205                      return (copied);
1206                   }
1207                         
1208                
1209                 if (nonblock)
1210                   {
1211                     release_sock (sk);
1212                     if (copied) return (copied);
1213                     return (-EAGAIN);
1214                   }
1215 
1216                if ((flags & MSG_PEEK) && copied != 0)
1217                  {
1218                    release_sock (sk);
1219                    return (copied);
1220                  }
1221                  
1222                PRINTK ("tcp_read about to sleep. state = %d\n",sk->state);
1223 
1224                release_sock (sk); /* now we may have some data waiting. */
1225                /* or we could have changed state. */
1226                cli();
1227                if ( sk->shutdown & RCV_SHUTDOWN || sk->err != 0)
1228                  {
1229                    sk->inuse = 1;
1230                    sti();
1231                    continue;
1232                  }
1233 
1234                 if ( sk->rqueue == NULL ||
1235                     before (sk->copied_seq+1, sk->rqueue->next->h.th->seq) )
1236                   {
1237                      interruptible_sleep_on (sk->sleep);
1238                      if (current->signal & ~current->blocked)
1239                        {
1240                           sti ();
1241                           if (copied) return (copied);
1242                           return (-ERESTARTSYS);
1243                        }
1244                   }
1245                 sti();
1246                 PRINTK ("tcp_read woke up. \n");
1247 
1248                 sk->inuse = 1;
1249 
1250                 if (sk->rqueue != NULL)
1251                   skb=sk->rqueue->next;
1252                 else
1253                   skb = NULL;
1254 
1255             }
1256 
1257           /* Copy anything from the current block that needs to go
1258              into the user buffer. */
1259 
1260          offset = sk->copied_seq+1 - skb->h.th->seq;
1261 
1262          if (skb->h.th->syn) offset --;
1263          if (offset < skb->len )
1264            {
1265               /* if there is urgent data we must either return or skip
1266                  over it. */
1267               if (skb->h.th->urg)
1268                 {
1269                    if (skb->urg_used)
1270                      {
1271                         if (flags & MSG_PEEK) break;
1272                         sk->copied_seq += skb->h.th->urg_ptr;
1273                         offset += skb->h.th->urg_ptr;
1274                         if (offset > skb->len)
1275                           {
1276                              skb->used = 1;
1277                              skb=skb->next;
1278                              continue;
1279                           }
1280                      }
1281                    else
1282                      {
1283                         break;
1284                      }
1285                 }
1286               used = min(skb->len - offset, len);
1287 
1288               verify_area (to, used);
1289               memcpy_tofs(to, ((unsigned char *)skb->h.th) +
1290                           skb->h.th->doff*4 +
1291                           offset,
1292                           used);
1293               copied += used;
1294               len -= used;
1295               to += used;
1296               if (!(flags & MSG_PEEK))
1297                 sk->copied_seq += used;
1298               
1299               /* mark this data used if we are really reading it, and if
1300                  it doesn't contain any urgent data. And we have used all
1301                  the data. */
1302               if (!(flags & MSG_PEEK) &&
1303                   (!skb->h.th->urg || skb->urg_used) &&
1304                   (used + offset >= skb->len) )
1305                 skb->used = 1;
1306               
1307               /* see if this is the end of a message or if the remaining data
1308                  is urgent. */
1309               if ( skb->h.th->psh || skb->h.th->urg)
1310                 {
1311                    break;
1312                 }
1313            }
1314          else /* already used this data, must be a retransmit. */
1315            {
1316               skb->used = 1;
1317            }
1318          skb=skb->next;
1319       }
1320     cleanup_rbuf (sk);
1321     release_sock (sk);
1322     if (copied == 0 && nonblock) return (-EAGAIN);
1323     PRINTK ("tcp_read returning %d\n", copied);
1324     return (copied);
1325 }
1326 
1327   
1328 /* sends a fin without closing the connection.  Not called
1329    at interrupt time. */
1330 
1331 void
1332 tcp_shutdown (volatile struct sock *sk, int how)
     /* [previous][next][first][last][top][bottom][index][help] */
1333 {
1334   /* we need to grab some memory, and put together a fin, and then
1335      put it into the queue to be sent. */
1336   struct sk_buff *buff;
1337   struct tcp_header *t1,*th;
1338   struct proto *prot;
1339   int tmp;
1340   struct device *dev=NULL;
1341 
1342 /* Written; Tim MacKenzie (tym@dibbler.cs.monash.edu.au) 4 Dec '92.
1343  * Most of this is guesswork, so maybe it will work...
1344  */
1345 
1346   if (!(how & SEND_SHUTDOWN)) return;
1347   sk->inuse = 1;
1348 
1349   /* clear out any half completed packets. */
1350   if (sk->send_tmp)
1351     tcp_send_partial(sk);
1352 
1353   prot = (struct proto *)sk->prot;
1354   th=(struct tcp_header *)&sk->dummy_th;
1355   release_sock (sk); /* incase the malloc sleeps. */
1356   buff=prot->wmalloc(sk, MAX_RESET_SIZE,1 , GFP_KERNEL);
1357   if (buff == NULL) 
1358     {
1359       return;
1360     }
1361   sk->inuse = 1;
1362 
1363 
1364   PRINTK("tcp_shutdown_send buff = %X\n", buff);
1365   buff->mem_addr = buff;
1366   buff->mem_len = MAX_RESET_SIZE;
1367   buff->lock = 0;
1368   buff->sk = sk;
1369   buff->len = sizeof (*t1);
1370 
1371   t1=(struct tcp_header *)(buff + 1);
1372   /* put in the ip_header and routing stuff. */
1373   tmp = prot->build_header (buff,sk->saddr, sk->daddr, &dev,
1374                             IPPROTO_TCP, sk->opt,
1375                             sizeof(struct tcp_header));
1376   if (tmp < 0)
1377     {
1378       prot->wfree (sk,buff->mem_addr, buff->mem_len);
1379       release_sock(sk);
1380       PRINTK ("Unable to build header for fin.\n");
1381       return;
1382     }
1383 
1384   t1 = (struct tcp_header *)((char *)t1 +tmp);
1385   buff ->len += tmp;
1386   buff->dev = dev;
1387 
1388   memcpy (t1, th, sizeof (*t1));
1389 
1390   t1->seq = net32(sk->send_seq);
1391 
1392   sk->send_seq++;
1393   buff->h.seq = sk->send_seq;
1394   t1->ack = 1;
1395 
1396   t1->ack_seq = net32(sk->acked_seq);
1397   t1->window = net16(sk->prot->rspace(sk));
1398   t1->fin = 1;
1399   t1->rst = 0;
1400 
1401   t1->doff = sizeof (*t1)/4;
1402   tcp_send_check (t1, sk->saddr, sk->daddr, sizeof (*t1), sk);
1403 
1404   /* can't just queue this up.  It should go at the end of
1405      the write queue. */
1406   if (sk->wback != NULL)
1407     {
1408       buff->next = NULL;
1409       sk->wback->next = buff;
1410       sk->wback = buff;
1411       buff->magic = TCP_WRITE_QUEUE_MAGIC;
1412     }
1413   else
1414     {
1415       sk->prot->queue_xmit (sk, dev, buff,0);
1416     }
1417 
1418   if (sk->state == TCP_ESTABLISHED)
1419     {
1420       sk->state = TCP_FIN_WAIT1;
1421     }
1422   else
1423     {
1424       sk->state = TCP_FIN_WAIT2;
1425     }
1426   release_sock(sk);
1427 }
1428 
1429 
1430 static int
1431 tcp_recvfrom (volatile struct sock *sk, unsigned char *to,
     /* [previous][next][first][last][top][bottom][index][help] */
1432               int to_len, int nonblock, unsigned flags,
1433               struct sockaddr_in *addr, int *addr_len)
1434 {
1435   int result = tcp_read(sk, to, to_len, nonblock, flags);
1436   struct sockaddr_in sin;
1437   int len;
1438   if (result < 0)
1439     return (result);
1440   len = get_fs_long(addr_len);
1441   if (len > sizeof (sin))
1442     len = sizeof (sin);
1443   sin.sin_family = AF_INET;
1444   sin.sin_port = sk->dummy_th.dest;
1445   sin.sin_addr.s_addr = sk->daddr;
1446   verify_area (addr, len);
1447   memcpy_tofs (addr, &sin, len);
1448   verify_area (addr_len, sizeof (len));
1449   put_fs_long (len, addr_len);
1450   return (result);
1451 }
1452 
1453 /* this routine will send a reset to the other tcp. */
1454 static  void
1455 tcp_reset(unsigned long saddr, unsigned long daddr, struct tcp_header *th,
     /* [previous][next][first][last][top][bottom][index][help] */
1456            struct proto *prot, struct options *opt, struct device *dev)
1457 {
1458   /* we need to grab some memory, and put together a reset, and then
1459      put it into the queue to be sent. */
1460   struct sk_buff *buff;
1461   struct tcp_header *t1;
1462   int tmp;
1463   buff=prot->wmalloc(NULL, MAX_RESET_SIZE,1, GFP_ATOMIC);
1464   if (buff == NULL) return;
1465 
1466   PRINTK("tcp_reset buff = %X\n", buff);
1467   buff->mem_addr = buff;
1468   buff->mem_len = MAX_RESET_SIZE;
1469   buff->lock = 0;
1470   buff->len = sizeof (*t1);
1471   buff->sk = NULL;
1472   buff->dev = dev;
1473 
1474   t1=(struct tcp_header *)(buff + 1);
1475   /* put in the ip_header and routing stuff. */
1476   tmp = prot->build_header (buff, saddr, daddr, &dev, IPPROTO_TCP, opt,
1477                             sizeof(struct tcp_header));
1478   if (tmp < 0)
1479     {
1480       prot->wfree (NULL,buff->mem_addr, buff->mem_len);
1481       return;
1482     }
1483   t1 = (struct tcp_header *)((char *)t1 +tmp);
1484   buff->len += tmp;
1485   memcpy (t1, th, sizeof (*t1));
1486   /* swap the send and the receive. */
1487   t1->dest = th->source;
1488   t1->source = th->dest;
1489   t1->seq = th->ack_seq; /* add one so it will be in
1490                             the right range.*/
1491   t1->rst = 1;
1492   t1->ack = 0;
1493   t1->syn = 0;
1494   t1->urg = 0;
1495   t1->fin = 0;
1496   t1->psh = 0;
1497   t1->doff = sizeof (*t1)/4;
1498   tcp_send_check (t1, saddr, daddr, sizeof (*t1), NULL);
1499   prot->queue_xmit(NULL, dev, buff, 1);
1500   
1501 }
1502 
1503 
1504 /* This routine handles a connection request.  This should make sure
1505    we haven't already responded. */
1506 /* Because of the way BSD works, we have to send a syn/ack now. This also
1507  means it will be harder to close a socket which is listening. */
1508 
1509 static  void
1510 tcp_conn_request(volatile struct sock *sk, struct sk_buff *skb,
     /* [previous][next][first][last][top][bottom][index][help] */
1511                  unsigned long daddr,
1512                  unsigned long saddr, struct options *opt, struct device *dev)
1513 {
1514   struct sk_buff *buff;
1515   struct tcp_header *t1;
1516   unsigned char *ptr;
1517   volatile struct sock *newsk;
1518   struct tcp_header *th;
1519   int tmp;
1520   th = skb->h.th;
1521 
1522   PRINTK ("tcp_conn_request (sk = %X, skb = %X, daddr = %X, sadd4= %X, \n"
1523           "                  opt = %X, dev = %X)\n",
1524           sk, skb, daddr, saddr, opt, dev);
1525   
1526   /* if the socket is dead, don't accept the connection. */
1527   if (!sk->dead)
1528     {
1529        wake_up(sk->sleep);
1530     }
1531   else
1532     {
1533        PRINTK ("tcp_conn_request on dead socket\n");
1534        tcp_reset (daddr, saddr, th, sk->prot, opt, dev);
1535        kfree_skb (skb, FREE_READ);
1536        return;
1537     }
1538 
1539   /* make sure we can accept more.  This will prevent a flurry of
1540      syns from eating up all our memory. */
1541   if (sk->ack_backlog >= sk->max_ack_backlog)
1542     {
1543        kfree_skb (skb, FREE_READ);
1544        return;
1545     }
1546 
1547   /* we need to build a new sock struct. */
1548   /* It is sort of bad to have a socket without an inode attached to
1549      it, but the wake_up's will just wake up the listening socket,
1550      and if the listening socket is destroyed before this is taken
1551      off of the queue, this will take care of it. */
1552 
1553   newsk = kmalloc(sizeof (struct sock), GFP_ATOMIC);
1554   if (newsk == NULL) 
1555     {
1556        /* just ignore the syn.  It will get retransmitted. */
1557        kfree_skb (skb, FREE_READ);
1558        return;
1559     }
1560 
1561 
1562   PRINTK ("newsk = %X\n", newsk);
1563   memcpy ((void *)newsk, (void *)sk, sizeof (*newsk));
1564   newsk->wback = NULL;
1565   newsk->wfront = NULL;
1566   newsk->rqueue = NULL;
1567   newsk->send_head = NULL;
1568   newsk->send_tail = NULL;
1569   newsk->back_log = NULL;
1570   newsk->blog = 0;
1571   newsk->intr = 0;
1572   newsk->proc = 0;
1573   newsk->done = 0;
1574   newsk->send_tmp = NULL;
1575   newsk->pair = NULL;
1576   newsk->wmem_alloc = 0;
1577   newsk->rmem_alloc = 0;
1578 
1579   newsk->max_unacked = MAX_WINDOW - TCP_WINDOW_DIFF;
1580 
1581   newsk->err = 0;
1582   newsk->shutdown = 0;
1583   newsk->ack_backlog = 0;
1584   newsk->acked_seq = skb->h.th->seq+1;
1585   newsk->fin_seq = skb->h.th->seq;
1586   newsk->copied_seq = skb->h.th->seq;
1587   newsk->state = TCP_SYN_RECV;
1588   newsk->timeout = 0;
1589   newsk->send_seq = timer_seq*SEQ_TICK-seq_offset;
1590   newsk->rcv_ack_seq = newsk->send_seq;
1591   newsk->urg =0;
1592   newsk->retransmits = 0;
1593   newsk->destroy = 0;
1594   newsk->time_wait.sk = newsk;
1595   newsk->time_wait.next = NULL;
1596   newsk->dummy_th.source = skb->h.th->dest;
1597   newsk->dummy_th.dest = skb->h.th->source;
1598   /* swap these two, they are from our point of view. */
1599   newsk->daddr=saddr;
1600   newsk->saddr=daddr;
1601 
1602   put_sock (newsk->num,newsk);
1603   newsk->dummy_th.res1=0;
1604   newsk->dummy_th.doff=6;
1605   newsk->dummy_th.fin=0;
1606   newsk->dummy_th.syn=0;
1607   newsk->dummy_th.rst=0;
1608   newsk->dummy_th.psh=0;
1609   newsk->dummy_th.ack=0;
1610   newsk->dummy_th.urg=0;
1611   newsk->dummy_th.res2=0;
1612   newsk->acked_seq = skb->h.th->seq+1;
1613   newsk->copied_seq = skb->h.th->seq;
1614 
1615   if (skb->h.th->doff == 5)
1616     {
1617       newsk->mtu=576-HEADER_SIZE;
1618     }
1619   else
1620     {
1621       ptr = (unsigned char *)(skb->h.th + 1);
1622       if (ptr[0] != 2 || ptr[1] != 4)
1623         {
1624            newsk->mtu=576-HEADER_SIZE;
1625         }
1626       else
1627         {
1628           newsk->mtu = min (ptr[2]*256+ptr[3]-HEADER_SIZE,
1629                             dev->mtu-HEADER_SIZE);
1630         }
1631     }
1632 
1633   print_sk (newsk);
1634   buff=newsk->prot->wmalloc(newsk,MAX_SYN_SIZE,1, GFP_ATOMIC);
1635   if (buff == NULL)
1636     {
1637        sk->err = -ENOMEM;
1638        newsk->dead = 1;
1639        release_sock (newsk);
1640        kfree_skb (skb, FREE_READ);
1641        return;
1642     }
1643   
1644   buff->lock = 0;
1645   buff->mem_addr = buff;
1646   buff->mem_len = MAX_SYN_SIZE;
1647   buff->len=sizeof (struct tcp_header)+4;
1648   buff->sk = newsk;
1649   
1650   t1=(struct tcp_header *)(buff + 1);
1651   /* put in the ip_header and routing stuff. */
1652 
1653   tmp = sk->prot->build_header (buff, newsk->saddr, newsk->daddr, &dev,
1654                                 IPPROTO_TCP, NULL, MAX_SYN_SIZE);
1655 
1656   /* something went wrong. */
1657   if (tmp < 0)
1658     {
1659        sk->err = tmp;
1660        sk->prot->wfree(newsk, buff->mem_addr, buff->mem_len);
1661        newsk->dead = 1;
1662        release_sock (newsk);
1663        skb->sk = sk;
1664        kfree_skb (skb, FREE_READ);
1665        return;
1666     }
1667 
1668   buff->len += tmp;
1669   t1 = (struct tcp_header *)((char *)t1 +tmp);
1670   
1671   memcpy (t1, skb->h.th, sizeof (*t1));
1672   buff->h.seq = newsk->send_seq;
1673   /* swap the send and the receive. */
1674   t1->dest = skb->h.th->source;
1675   t1->source = newsk->dummy_th.source;
1676   t1->seq = net32(newsk->send_seq++);
1677   t1->ack = 1;
1678   newsk->window = newsk->prot->rspace(newsk);
1679   t1->window = net16(newsk->window);
1680   t1->res1=0;
1681   t1->res2=0;
1682   t1->rst = 0;
1683   t1->urg = 0;
1684   t1->psh = 0;
1685   t1->syn = 1;
1686   t1->ack_seq = net32(skb->h.th->seq+1);
1687   t1->doff = sizeof (*t1)/4+1;
1688 
1689   ptr = (unsigned char *)(t1+1);
1690   ptr[0]=2;
1691   ptr[1]=4;
1692   ptr[2]=((dev->mtu - HEADER_SIZE) >> 8) & 0xff;
1693   ptr[3]=(dev->mtu - HEADER_SIZE) & 0xff;
1694 
1695   tcp_send_check (t1, daddr, saddr, sizeof (*t1)+4, newsk);
1696   newsk->prot->queue_xmit(newsk, dev, buff, 0);
1697 
1698   newsk->time_wait.len = TCP_CONNECT_TIME;
1699   PRINTK ("newsk->time_wait.sk = %X\n", newsk->time_wait.sk);
1700   reset_timer ((struct timer *)&newsk->time_wait);
1701   skb->sk = newsk;
1702   /* charge the sock_buff to newsk. */
1703   sk->rmem_alloc -= skb->mem_len;
1704   newsk->rmem_alloc += skb->mem_len;
1705 
1706   if (sk->rqueue == NULL)
1707     {
1708       skb->next = skb;
1709       skb->prev = skb;
1710       sk->rqueue = skb;
1711     }
1712   else
1713     {
1714       skb->next = sk->rqueue;
1715       skb->prev = sk->rqueue->prev;
1716       sk->rqueue->prev = skb;
1717       skb->prev->next = skb;
1718     }
1719   sk->ack_backlog++;
1720   release_sock (newsk);
1721 }
1722 
1723 static  void
1724 tcp_close (volatile struct sock *sk, int timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
1725 {
1726   /* we need to grab some memory, and put together a fin, and then
1727      put it into the queue to be sent. */
1728   struct sk_buff *buff;
1729   int need_reset = 0;
1730   struct tcp_header *t1,*th;
1731   struct proto *prot;
1732   struct device *dev=NULL;
1733   int tmp;
1734   PRINTK ("tcp_close ((struct sock *)%X, %d)\n",sk, timeout);
1735   print_sk (sk);
1736   sk->inuse = 1;
1737   sk->keepopen = 1;
1738   sk->shutdown = SHUTDOWN_MASK;
1739 
1740   if (!sk->dead)
1741     wake_up (sk->sleep);
1742 
1743 
1744   /* we need to flush the recv. buffs. */
1745   
1746   if (sk->rqueue != NULL)
1747     {
1748        struct sk_buff *skb;
1749        struct sk_buff *skb2;
1750        skb = sk->rqueue;
1751        do {
1752           skb2=skb->next;
1753           /* if there is some real unread data, send a reset. */
1754           if (skb->len > 0 &&
1755               after (skb->h.th->seq + skb->len + 1, sk->copied_seq))
1756             need_reset = 1;
1757           kfree_skb (skb, FREE_READ);
1758           skb=skb2;
1759        } while (skb != sk->rqueue);
1760     }
1761   sk->rqueue = NULL;
1762 
1763   /* get rid on any half completed packets. */
1764   if (sk->send_tmp)
1765     {
1766       tcp_send_partial (sk);
1767     }
1768 
1769   switch (sk->state)
1770     {
1771 
1772     case TCP_FIN_WAIT1:
1773     case TCP_FIN_WAIT2:
1774     case TCP_LAST_ACK:
1775       /* start a timer. */
1776       sk->time_wait.len = 4*sk->rtt;;
1777       sk->timeout = TIME_CLOSE;
1778       reset_timer ((struct timer *)&sk->time_wait);
1779       if (timeout)
1780         tcp_time_wait(sk);
1781       release_sock (sk);
1782       break;
1783       
1784     case TCP_TIME_WAIT:
1785       if (timeout)
1786         sk->state = TCP_CLOSE;
1787       release_sock (sk);
1788       return;
1789       
1790     case TCP_LISTEN:
1791       sk->state = TCP_CLOSE;
1792       release_sock(sk);
1793       return;
1794 
1795     case TCP_CLOSE:
1796 
1797       release_sock(sk);
1798       return;
1799        
1800 
1801     case TCP_CLOSE_WAIT:
1802     case TCP_ESTABLISHED:
1803     case TCP_SYN_SENT:
1804     case TCP_SYN_RECV:
1805 
1806       prot = (struct proto *)sk->prot;
1807       th=(struct tcp_header *)&sk->dummy_th;
1808 
1809       buff=prot->wmalloc(sk, MAX_FIN_SIZE,1, GFP_ATOMIC);
1810       if (buff == NULL)
1811         {
1812           /* this will force it to try again later. */
1813           if (sk->state != TCP_CLOSE_WAIT)
1814             sk->state = TCP_ESTABLISHED;
1815           sk->timeout = TIME_CLOSE;
1816           sk->time_wait.len = 100; /* wait a second. */
1817           reset_timer ((struct timer *)&sk->time_wait);
1818           return;
1819         }
1820 
1821       buff->lock = 0;
1822       buff->mem_addr = buff;
1823       buff->mem_len = MAX_FIN_SIZE;
1824       buff->sk = sk;
1825       buff->len = sizeof (*t1);
1826       t1=(struct tcp_header *)(buff + 1);
1827       /* put in the ip_header and routing stuff. */
1828       tmp = prot->build_header (buff,sk->saddr, sk->daddr, &dev,
1829                                 IPPROTO_TCP, sk->opt,
1830                                 sizeof(struct tcp_header));
1831       if (tmp < 0)
1832         {
1833           prot->wfree (sk,buff->mem_addr, buff->mem_len);
1834           PRINTK ("Unable to build header for fin.\n");
1835           release_sock(sk);
1836           return;
1837         }
1838 
1839       t1 = (struct tcp_header *)((char *)t1 +tmp);
1840       buff ->len += tmp;
1841       buff->dev = dev;
1842       memcpy (t1, th, sizeof (*t1));
1843       t1->seq = net32(sk->send_seq);
1844       sk->send_seq++;
1845       buff->h.seq = sk->send_seq;
1846       t1->ack = 1;
1847 
1848        /* ack everything immediately from now on. */
1849       sk->delay_acks = 0;
1850       t1->ack_seq = net32(sk->acked_seq);
1851       t1->window = net16(sk->prot->rspace(sk));
1852       t1->fin = 1;
1853       t1->rst = need_reset;
1854       t1->doff = sizeof (*t1)/4;
1855       tcp_send_check (t1, sk->saddr, sk->daddr, sizeof (*t1), sk);
1856 
1857       if (sk->wfront == NULL)
1858         {
1859           prot->queue_xmit(sk, dev, buff, 0);
1860         }
1861       else
1862         {
1863            sk->time_wait.len = sk->rtt;
1864            sk->timeout = TIME_WRITE;
1865            reset_timer ((struct timer *)&sk->time_wait);
1866            buff->next = NULL;
1867            if (sk->wback == NULL)
1868              {
1869                 sk->wfront=buff;
1870              }
1871            else
1872              {
1873                 sk->wback->next = buff;
1874              }
1875            sk->wback = buff;
1876            buff->magic = TCP_WRITE_QUEUE_MAGIC;
1877            
1878         }
1879 
1880        if (sk->state == TCP_CLOSE_WAIT)
1881         {
1882           sk->state = TCP_FIN_WAIT2;
1883         }
1884        else
1885          {
1886            sk->state = TCP_FIN_WAIT1;
1887          }
1888     }
1889   release_sock (sk);
1890 }
1891 
1892 
1893 /* This routine takes stuff off of the write queue, and puts it in the
1894    xmit queue. */
1895 static  void
1896 tcp_write_xmit (volatile struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
1897 {
1898   struct sk_buff *skb;
1899   PRINTK ("tcp_write_xmit (sk=%X)\n",sk);
1900   while (sk->wfront != NULL && before (sk->wfront->h.seq, sk->window_seq) &&
1901          sk->packets_out < sk->cong_window)
1902     {
1903       skb = sk->wfront;
1904       sk->wfront = skb->next;
1905       if (sk->wfront == NULL)
1906         sk->wback = NULL;
1907       skb->next = NULL;
1908       if (skb->magic != TCP_WRITE_QUEUE_MAGIC)
1909         {
1910           PRINTK ("tcp.c skb with bad magic (%X) on write queue. Squashing "
1911                   "queue\n", skb->magic);
1912           sk->wfront = NULL;
1913           sk->wback = NULL;
1914           return;
1915         }
1916       skb->magic = 0;
1917       PRINTK("Sending a packet.\n");
1918       sk->prot->queue_xmit (sk, skb->dev, skb, skb->free);
1919     }
1920 }
1921 
1922 
1923 
1924 /* This routine deals with incoming acks, but not outgoing ones. */
1925 
1926 static  int
1927 tcp_ack (volatile struct sock *sk, struct tcp_header *th, unsigned long saddr)
     /* [previous][next][first][last][top][bottom][index][help] */
1928 {
1929   unsigned long ack;
1930   ack = net32(th->ack_seq);
1931 
1932   PRINTK ("tcp_ack ack=%d, window=%d, "
1933           "sk->rcv_ack_seq=%d, sk->window_seq = %d\n",
1934           ack, net16(th->window), sk->rcv_ack_seq, sk->window_seq);
1935   if (after (ack, sk->send_seq+1) || before (ack, sk->rcv_ack_seq-1))
1936     {
1937       if (after (ack, sk->send_seq) || (sk->state != TCP_ESTABLISHED &&
1938                                         sk->state != TCP_CLOSE_WAIT)) 
1939         {
1940           return (0);
1941         }
1942       if (sk->keepopen)
1943         reset_timer ((struct timer *)&sk->time_wait);
1944       sk->retransmits = 0;
1945       return (1);
1946     }
1947 
1948   /* see if our window has been shrunk. */
1949   if (after (sk->window_seq, ack+net16(th->window)))
1950     {
1951       /* we may need to move packets from the send queue to the
1952          write queue. if the window has been shrunk on us. */
1953       /* the rfc says you are not allowed to shrink your window like
1954          this, but if the other end does, you must be able to deal
1955          with it. */
1956 
1957       struct sk_buff *skb;
1958       struct sk_buff *skb2=NULL;
1959       struct sk_buff *wskb=NULL;
1960 
1961       sk->window_seq = ack + net16(th->window);
1962       cli();
1963       for (skb = sk->send_head; skb != NULL; skb=skb->link3)
1964         {
1965           if (after( skb->h.seq, sk->window_seq))
1966             {
1967 
1968               /* remove it from the send queue. */
1969               if (skb2 == NULL)
1970                 {
1971                   sk->send_head = skb->link3;
1972                 }
1973               else
1974                 {
1975                   skb2->link3 = skb->link3;
1976                 }
1977               if (sk->send_tail == skb)
1978                 sk->send_tail = skb2;
1979 
1980               /* we may need to remove this from the dev send list. */
1981               if (skb->next != NULL)
1982                 {
1983                   int i;
1984                   if (skb->next != skb)
1985                     {
1986                       skb->next->prev = skb->prev;
1987                       skb->prev->next = skb->next;
1988                     }
1989                   for (i = 0; i < DEV_NUMBUFFS; i++)
1990                     {
1991                       if (skb->dev->buffs[i] == skb)
1992                         {
1993                           if (skb->next == skb)
1994                             skb->dev->buffs[i] = NULL;
1995                           else
1996                             skb->dev->buffs[i] = skb->next;
1997                           break;
1998                         }
1999                     }
2000                   if (arp_q == skb)
2001                     {
2002                       if (skb->next == skb)
2003                         arp_q = NULL;
2004                       else
2005                         arp_q = skb->next;
2006                     }
2007                 }
2008 
2009               /* now add it to the write_queue. */
2010               skb->magic = TCP_WRITE_QUEUE_MAGIC;
2011               if (wskb == NULL)
2012                 {
2013                   skb->next = sk->wfront;
2014                   sk->wfront = skb;
2015                 }
2016               else
2017                 {
2018                   skb->next = wskb->next;
2019                   wskb->next = skb;
2020                 }
2021               wskb = skb;
2022             }
2023           else
2024             {
2025               skb2 = skb;
2026             }
2027         }
2028       sti();
2029     }
2030 
2031   sk->window_seq = ack + net16(th->window);
2032 
2033   /* we don't want too many packets out there. */
2034   if (sk->cong_window < 2048 && ack != sk->rcv_ack_seq)
2035     {
2036        if (sk->exp_growth)
2037          sk->cong_window *= 2;
2038        else
2039          sk->cong_window++;
2040     }
2041 
2042   PRINTK ("tcp_ack: Updating rcv ack sequence. \n");
2043   sk->rcv_ack_seq = ack;
2044 
2045   /* see if we can take anything off of the retransmit queue. */
2046   while (sk->send_head != NULL)
2047     {
2048       if (before (sk->send_head->h.seq, ack+1))
2049         {
2050           struct sk_buff *oskb;
2051           /* we have one less packet out there. */
2052           sk->packets_out --;
2053           PRINTK ("skb=%X acked\n", sk->send_head);
2054           /* wake up the process, it can probably
2055              write more. */
2056           if (!sk->dead)
2057             wake_up (sk->sleep);
2058 
2059           cli();
2060 
2061           oskb = sk->send_head;
2062           /* estimate the rtt. */
2063           sk->rtt += ((jiffies - oskb->when) - sk->rtt)/2;
2064           if (sk->rtt < 30) sk->rtt = 30;
2065           sk->send_head = oskb->link3;
2066           if (sk->send_head == NULL) 
2067             {
2068               sk->send_tail = NULL;
2069             }
2070           /* we may need to remove this from the dev send list. */
2071           if (oskb->next != NULL)
2072             {
2073                int i;
2074                if (oskb->next != oskb)
2075                  {
2076                     oskb->next->prev = oskb->prev;
2077                     oskb->prev->next = oskb->next;
2078                  }
2079                for (i = 0; i < DEV_NUMBUFFS; i++)
2080                  {
2081                    if (oskb->dev->buffs[i] == oskb)
2082                      {
2083                        if (oskb== oskb->next)
2084                          oskb->dev->buffs[i]= NULL;
2085                        else
2086                          oskb->dev->buffs[i] = oskb->next;
2087                        break;
2088                      }
2089                  }
2090                if (arp_q == oskb)
2091                  {
2092                    if (oskb == oskb->next)
2093                      arp_q = NULL;
2094                    else
2095                      arp_q = oskb->next;
2096                  }
2097             }
2098           oskb->magic = 0;
2099           kfree_skb  (oskb, FREE_WRITE); /* write. */
2100           sti();
2101           if (!sk->dead)
2102             wake_up(sk->sleep);
2103         }
2104       else
2105         {
2106           break;
2107         }
2108 
2109     }
2110 
2111 
2112   /* at this point we need to check to see if we have anything
2113      which needs to be retransmiteed.  If we have failed to get
2114      some acks i.e. had to retransmit something, and we succeded, we
2115      should then attempt to retransmit everything right now. */
2116 
2117   if (sk->retransmits && sk->send_head != NULL)
2118     {
2119       PRINTK ("retransmitting\n");
2120       sk->prot->retransmit (sk,1);
2121     }
2122   sk->retransmits = 0;
2123 
2124   /* maybe we can take some stuff off of the write queue, and put it onto
2125      the xmit queue. */
2126   if (sk->wfront != NULL && sk->packets_out < sk->cong_window)
2127     {
2128       if (after (sk->window_seq, sk->wfront->h.seq))
2129         {
2130           tcp_write_xmit (sk);
2131         }
2132     }
2133   else
2134     {
2135        if (sk->send_head == NULL && sk->ack_backlog == 0 &&
2136            sk->state != TCP_TIME_WAIT && !sk->keepopen)
2137          {
2138            PRINTK ("Nothing to do, going to sleep.\n"); 
2139             if (!sk->dead)
2140               wake_up (sk->sleep);
2141 
2142             /* Lets send a probe once in a while. */
2143             sk->time_wait.len = TCP_PROBEWAIT_LEN;
2144             sk->timeout = TIME_KEEPOPEN;
2145             reset_timer((struct timer *)&sk->time_wait);
2146             sk->timeout = 0;
2147          }
2148        else
2149          {
2150             if (sk->state == TCP_TIME_WAIT)
2151               {
2152                  sk->time_wait.len = TCP_TIMEWAIT_LEN;
2153                  sk->timeout = TIME_CLOSE;
2154               }
2155             sk->timeout = TIME_WRITE;
2156             sk->time_wait.len = sk->rtt*2;
2157             reset_timer ((struct timer *)&sk->time_wait);
2158          }
2159     }
2160 
2161 
2162   if (sk->packets_out == 0 && sk->send_tmp != NULL &&
2163       sk->wfront == NULL && sk->send_head == NULL)
2164     {
2165       tcp_send_partial (sk);
2166     }
2167 
2168   /* see if we are done. */
2169   if ( sk->state == TCP_TIME_WAIT)
2170     {
2171        if (!sk->dead) wake_up (sk->sleep);
2172        if (sk->rcv_ack_seq == sk->send_seq &&    
2173            sk->acked_seq == sk->fin_seq)
2174          {
2175            sk->state = TCP_CLOSE;
2176            sk->shutdown = SHUTDOWN_MASK;
2177          }
2178     }
2179 
2180   if (sk->state == TCP_LAST_ACK || sk->state == TCP_FIN_WAIT2)
2181     {
2182       if (!sk->dead) wake_up (sk->sleep);
2183       if (sk->rcv_ack_seq == sk->send_seq)
2184         {
2185            if (sk->acked_seq != sk->fin_seq)
2186              {
2187                 tcp_time_wait(sk);
2188              }
2189            else
2190              {
2191                PRINTK ("tcp_ack closing socket - %X\n", sk);
2192                print_sk (sk);
2193                tcp_send_ack (sk->send_seq, sk->acked_seq, sk, th, sk->daddr);
2194                sk->shutdown = SHUTDOWN_MASK;
2195                sk->state = TCP_CLOSE;
2196              }
2197         }
2198     }
2199 
2200   PRINTK ("leaving tcp_ack\n");
2201 
2202   return (1);
2203 }
2204 
2205 /* This routine handles the data.  If there is room in the buffer, it
2206    will be have already been moved into it.  If there is no room,
2207    then we will just have to discard the packet. */
2208 
2209 static  int
2210 tcp_data (struct sk_buff *skb, volatile struct sock *sk, 
     /* [previous][next][first][last][top][bottom][index][help] */
2211           unsigned long saddr, unsigned short len)
2212 {
2213   struct sk_buff *skb1, *skb2;
2214   struct tcp_header *th;
2215 
2216   th = skb->h.th;
2217   print_th (th);
2218   skb->len = len - (th->doff*4);
2219 
2220   PRINTK("tcp_data len = %d sk = %X:\n",skb->len, sk);
2221   print_sk(sk);
2222 
2223   sk->bytes_rcv += skb->len;
2224 
2225   if (skb->len == 0 && !th->fin && !th->urg && !th->psh)
2226     {
2227       /* don't want to keep passing ack's back and fourth. */
2228       if (!th->ack)
2229         tcp_send_ack (sk->send_seq, sk->acked_seq,sk, th, saddr);
2230       kfree_skb(skb, FREE_READ);
2231       return (0);
2232     }
2233 
2234   if (sk->shutdown & RCV_SHUTDOWN)
2235     {
2236        sk->acked_seq = th->seq + skb->len + th->syn + th->fin;
2237        tcp_reset (sk->saddr, sk->daddr, skb->h.th,
2238                   sk->prot, NULL, skb->dev);
2239        sk->state = TCP_CLOSE;
2240        sk->err = EPIPE;
2241        sk->shutdown = SHUTDOWN_MASK;
2242        PRINTK ("tcp_data: closing socket - %X\n", sk);
2243        print_sk (sk);
2244        kfree_skb (skb, FREE_READ);
2245        if (!sk->dead) wake_up (sk->sleep);
2246        return (0);
2247     }
2248 
2249   /* now we have to walk the chain, and figure out where this one
2250      goes into it.  This is set up so that the last packet we received
2251      will be the first one we look at, that way if everything comes
2252      in order, there will be no performance loss, and if they come
2253      out of order we will be able to fit things in nicely. */
2254   
2255   if (sk->rqueue == NULL)
2256     {
2257        PRINTK ("tcp_data: skb = %X:\n",skb);
2258        print_skb (skb);
2259 
2260        sk->rqueue = skb;
2261        skb->next = skb;
2262        skb->prev = skb;
2263        skb1= NULL;
2264     }
2265   else
2266     {
2267       PRINTK ("tcp_data adding to chain sk = %X:\n",sk);
2268       print_sk (sk);
2269 
2270       for (skb1=sk->rqueue; ; skb1=skb1->prev)
2271         {
2272           PRINTK ("skb1=%X\n",skb1);
2273           print_skb(skb1);
2274           PRINTK ("skb1->h.th->seq = %d\n", skb1->h.th->seq);
2275           if (after ( th->seq+1, skb1->h.th->seq))
2276             {
2277               skb->prev = skb1;
2278               skb->next = skb1->next;
2279               skb->next->prev = skb;
2280               skb1->next = skb;
2281               if (skb1 == sk->rqueue)
2282                 sk->rqueue = skb;
2283               break;
2284             }
2285           if  ( skb1->prev == sk->rqueue)
2286             {
2287                skb->next= skb1;
2288                skb->prev = skb1->prev;
2289                skb->prev->next = skb;
2290                skb1->prev = skb;
2291                skb1 = NULL; /* so we know we might be able to ack stuff. */
2292                break;
2293             }
2294         }
2295 
2296       PRINTK ("skb = %X:\n",skb);
2297       print_skb (skb);
2298       PRINTK ("sk now equals:\n");
2299       print_sk (sk);
2300 
2301     }
2302 
2303   th->ack_seq = th->seq + skb->len;
2304   if (th->syn) th->ack_seq ++;
2305   if (th->fin) th->ack_seq ++;
2306 
2307   if (before (sk->acked_seq, sk->copied_seq))
2308     {
2309        printk ("*** tcp.c:tcp_data bug acked < copied\n");
2310        sk->acked_seq = sk->copied_seq;
2311     }
2312 
2313   /* now figure out if we can ack anything. */
2314   if (skb1 == NULL || skb1->acked || before (th->seq, sk->acked_seq+1))
2315     {
2316       if (before (th->seq, sk->acked_seq+1))
2317         {
2318           if (after (th->ack_seq, sk->acked_seq))
2319               sk->acked_seq = th->ack_seq;
2320           skb->acked = 1;
2321 
2322           /* when we ack the fin, we turn on the RCV_SHUTDOWN flag. */
2323           if (skb->h.th->fin)  
2324             {
2325               if (!sk->dead) wake_up (sk->sleep);
2326               sk->shutdown |= RCV_SHUTDOWN;
2327             }
2328           
2329           for (skb2=skb->next; skb2 != sk->rqueue->next; skb2=skb2->next)
2330             {
2331                if (before(skb2->h.th->seq, sk->acked_seq+1))
2332                  {
2333                    if (after (skb2->h.th->ack_seq, sk->acked_seq))
2334                      sk->acked_seq = skb2->h.th->ack_seq;
2335                     skb2->acked = 1;
2336 
2337                    /* when we ack the fin, we turn on the RCV_SHUTDOWN flag. */
2338                    if (skb2->h.th->fin)  
2339                      {
2340                        sk->shutdown |= RCV_SHUTDOWN;
2341                        if (!sk->dead) wake_up (sk->sleep);
2342                      }
2343           
2344                     /* force an immediate ack. */
2345                     sk->ack_backlog = sk->max_ack_backlog;
2346                  }
2347                else
2348                  {
2349                    break;
2350                  }
2351             }
2352 
2353           /* this also takes care of updating the window. */
2354           /* this if statement needs to be simplified. */
2355 
2356           if (!sk->delay_acks || 
2357               sk->ack_backlog >= sk->max_ack_backlog || 
2358               sk->bytes_rcv > sk->max_unacked || 
2359               th->fin)
2360             {
2361                 tcp_send_ack (sk->send_seq, sk->acked_seq,sk,th, saddr);
2362             }
2363           else
2364             {
2365                sk->ack_backlog++;
2366                sk->time_wait.len = TCP_ACK_TIME;
2367                sk->timeout = TIME_WRITE;
2368                reset_timer ((struct timer *)&sk->time_wait);
2369                sk->retransmits = 0;
2370             }
2371        }
2372    }
2373   else
2374     {
2375        /* we missed a packet.  Send an ack to try to resync things. */
2376        tcp_send_ack (sk->send_seq, sk->acked_seq, sk, th, saddr);
2377     }
2378 
2379   /* now tell the user we may have some data. */
2380   if (!sk->dead)
2381     {
2382        wake_up (sk->sleep);
2383     }
2384   else
2385     {
2386        PRINTK ("data received on dead socket. \n");
2387     }
2388 
2389   if (sk->state == TCP_FIN_WAIT2 && sk->acked_seq == sk->fin_seq 
2390       && sk->rcv_ack_seq == sk->send_seq)
2391     {
2392       PRINTK ("tcp_data: entering last_ack state sk = %X\n", sk);
2393       print_sk (sk);
2394       tcp_send_ack (sk->send_seq, sk->acked_seq, sk, th, saddr);
2395       sk->shutdown = SHUTDOWN_MASK;
2396       sk->state = TCP_LAST_ACK;
2397       if (!sk->dead) wake_up (sk->sleep);
2398     }
2399 
2400   return (0);
2401 }
2402 
2403 static  int
2404 tcp_urg (volatile struct sock *sk, struct tcp_header *th, unsigned long saddr)
     /* [previous][next][first][last][top][bottom][index][help] */
2405 {
2406     extern int kill_pg (int pg, int sig, int priv);
2407     extern int kill_proc (int pid, int sig, int priv);
2408     
2409     if (!sk->dead)
2410       wake_up(sk->sleep);
2411     
2412     if (sk->urginline)
2413       {
2414           th->urg = 0;
2415           th->psh = 1;
2416           return (0);
2417       }
2418 
2419     sk->urg++;
2420 
2421     if (!sk->urg)
2422       {
2423           /* so if we get more urgent data, we don't 
2424              signal the user again. */
2425           if (sk->proc == 0) return (0);
2426           if (sk->proc > 0)
2427             {
2428                 kill_proc (sk->proc, SIGURG, 1);
2429             }
2430           else 
2431             {
2432                 kill_pg (-sk->proc, SIGURG, 1);
2433             }
2434       }
2435     return (0);
2436 }
2437 
2438 /* this deals with incoming fins. */
2439 static  int
2440 tcp_fin (volatile struct sock *sk, struct tcp_header *th, 
     /* [previous][next][first][last][top][bottom][index][help] */
2441          unsigned long saddr, struct device *dev)
2442 {
2443   PRINTK ("tcp_fin (sk=%X, th=%X, saddr=%X, dev=%X)\n",
2444           sk, th, saddr, dev);
2445   
2446   if (!sk->dead)
2447     {
2448       wake_up (sk->sleep);
2449     }
2450 
2451   switch (sk->state)
2452     {
2453     case TCP_SYN_RECV:
2454     case TCP_SYN_SENT:
2455     case TCP_ESTABLISHED:
2456       sk->fin_seq = th->seq+1; /* Contains the one that needs to be acked */
2457       sk->state = TCP_CLOSE_WAIT;
2458       break;
2459 
2460     case TCP_CLOSE_WAIT:
2461     case TCP_FIN_WAIT2:
2462       break; /* we got a retransmit of the fin. */
2463 
2464     case TCP_FIN_WAIT1:
2465       sk->fin_seq = th->seq+1; /* Contains the one that needs to be acked */
2466       sk->state = TCP_FIN_WAIT2;
2467       break;
2468 
2469     default:
2470     case TCP_TIME_WAIT:
2471       sk->state = TCP_LAST_ACK;
2472       /* start the timers. */
2473       sk->time_wait.len = TCP_TIMEWAIT_LEN;
2474       sk->timeout = TIME_CLOSE;
2475       reset_timer ((struct timer *)&sk->time_wait);
2476       return (0);
2477 
2478     }
2479   /* there is no longer any reason to do this.  Just let tcp_data
2480      deal with it. */
2481   sk->ack_backlog ++;
2482 
2483 #if 0
2484   /* send an ack */
2485   buff=sk->prot->wmalloc(sk,MAX_ACK_SIZE,1, GFP_ATOMIC);
2486   if (buff == NULL)
2487     {
2488        /* we will ignore the fin.  That way it will be sent again. */
2489        return (1);
2490     }
2491 
2492   buff->mem_addr = buff;
2493   buff->mem_len = MAX_ACK_SIZE;
2494   buff->len=sizeof (struct tcp_header);
2495   buff->sk = sk;
2496 
2497   t1 = (struct tcp_header *)(buff + 1);
2498   /* put in the ip_header and routing stuff. */
2499   tmp = sk->prot->build_header (buff, sk->saddr, sk->daddr, &dev,
2500                                 IPPROTO_TCP,  sk->opt, MAX_ACK_SIZE);
2501   if (tmp < 0)
2502     {
2503       sk->prot->wfree(sk, buff->mem_addr, buff->mem_len);
2504       return (0);
2505     }
2506 
2507   buff->len += tmp;
2508   t1 = (struct tcp_header *)((char *)t1 +tmp);
2509   
2510   memcpy (t1, th, sizeof (*t1));
2511 
2512   /* swap the send and the receive. */
2513   t1->dest = th->source;
2514   t1->source = th->dest;
2515 
2516 
2517   t1->seq = net32(sk->send_seq);
2518 
2519   /* contains the one that needs to be acked. */
2520   /* sk->fin_seq = th->seq+1;*/
2521 
2522   buff->h.seq = sk->send_seq;
2523   t1->window = net16(sk->prot->rspace(sk));
2524 
2525   t1->res1=0;
2526   t1->res2=0;
2527   t1->rst = 0;
2528   t1->urg = 0;
2529   t1->syn = 0;
2530   t1->psh = 0;
2531   t1->ack = 1; 
2532   t1->fin = 0;
2533   t1->ack_seq = net32(sk->acked_seq);
2534 
2535   t1->doff = sizeof (*t1)/4;
2536   tcp_send_check (t1, sk->saddr, sk->daddr, sizeof (*t1), sk);
2537 
2538   /* can't just queue this up.  It should go at the end of
2539      the write queue. */
2540   if (sk->wback != NULL)
2541     {
2542       buff->next = NULL;
2543       sk->wback->next = buff;
2544       sk->wback = buff;
2545       buff->magic = TCP_WRITE_QUEUE_MAGIC;
2546     }
2547   else
2548     {
2549       sk->prot->queue_xmit (sk, dev, buff,0);
2550     }
2551 #endif
2552   return (0);
2553 }
2554 
2555 
2556 /* this will accept the next outstanding connection. */
2557 
2558 static volatile struct sock *
2559 tcp_accept (volatile struct sock *sk, int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
2560 {
2561   volatile struct sock *newsk;
2562   struct sk_buff *skb;
2563   
2564   PRINTK ("tcp_accept(sk=%X, flags=%X)\n", sk, flags);
2565   print_sk(sk);
2566   /* we need to make sure that this socket is listening, and that
2567      it has something pending. */
2568   
2569   if (sk->state != TCP_LISTEN)
2570     {
2571       sk->err = EINVAL;
2572       return (NULL); 
2573     }
2574   /* avoid the race. */
2575 
2576   sk->inuse = 1;
2577   cli();
2578   while ( (skb = get_firstr(sk)) == NULL )
2579     {
2580       if (flags & O_NONBLOCK)
2581         {
2582           sti();
2583           release_sock (sk);
2584           sk->err = EAGAIN;
2585           return (NULL);
2586         }
2587 
2588       release_sock (sk);
2589       interruptible_sleep_on (sk->sleep);
2590       if (current->signal & ~current->blocked)
2591         {
2592            sti();
2593            sk->err = ERESTARTSYS;
2594            return (NULL);
2595         }
2596 
2597       sk->inuse = 1;
2598     }
2599   sti();
2600 
2601   /* now all we need to do is return skb->sk. */
2602   newsk = skb->sk;
2603 
2604   kfree_skb (skb, FREE_READ);
2605   sk->ack_backlog--;
2606   release_sock (sk);
2607   return (newsk);
2608 }
2609 
2610 
2611 
2612 /* this will initiate an outgoing connection. */
2613 static int
2614 tcp_connect (volatile struct sock *sk, struct sockaddr_in *usin, int addr_len)
     /* [previous][next][first][last][top][bottom][index][help] */
2615 {
2616   struct sk_buff *buff;
2617   struct sockaddr_in sin;
2618   struct device *dev=NULL;
2619   unsigned char *ptr;
2620   int tmp;
2621   struct tcp_header *t1;
2622   if (sk->state != TCP_CLOSE) return (-EISCONN);
2623   if (addr_len < 8) return (-EINVAL);
2624 
2625 /*  verify_area (usin, addr_len);*/
2626   memcpy_fromfs (&sin,usin, min(sizeof (sin), addr_len));
2627 
2628   if (sin.sin_family && sin.sin_family != AF_INET) return (-EAFNOSUPPORT);
2629   sk->inuse = 1;
2630   sk->daddr = sin.sin_addr.s_addr;
2631   sk->send_seq = timer_seq*SEQ_TICK-seq_offset;
2632   sk->rcv_ack_seq = sk->send_seq -1;
2633   sk->err = 0;
2634   sk->dummy_th.dest = sin.sin_port;
2635   release_sock (sk);
2636 
2637   buff=sk->prot->wmalloc(sk,MAX_SYN_SIZE,0, GFP_KERNEL);
2638   if (buff == NULL) 
2639     {
2640       return (-ENOMEM);
2641     }
2642   sk->inuse = 1;
2643   buff->lock = 0;
2644   buff->mem_addr = buff;
2645   buff->mem_len = MAX_SYN_SIZE;
2646   buff->len=24;
2647   buff->sk = sk;
2648   t1=(struct tcp_header *)(buff + 1);
2649   /* put in the ip_header and routing stuff. */
2650   /* We need to build the routing stuff fromt the things saved
2651      in skb. */
2652   tmp = sk->prot->build_header (buff, sk->saddr, sk->daddr, &dev,
2653                                 IPPROTO_TCP, NULL, MAX_SYN_SIZE);
2654   if (tmp < 0)
2655     {
2656       sk->prot->wfree(sk, buff->mem_addr, buff->mem_len);
2657       release_sock (sk);
2658       return (-ENETUNREACH);
2659     }
2660   buff->len += tmp;
2661   t1 = (struct tcp_header *)((char *)t1 +tmp);
2662 
2663   memcpy (t1, (void *)&(sk->dummy_th), sizeof (*t1));
2664   t1->seq = net32(sk->send_seq++);
2665   buff->h.seq = sk->send_seq;
2666   t1->ack = 0;
2667   t1->window = 2;
2668   t1->res1=0;
2669   t1->res2=0;
2670   t1->rst = 0;
2671   t1->urg = 0;
2672   t1->psh = 0;
2673   t1->syn = 1;
2674   t1->urg_ptr = 0;
2675   t1->doff =6;
2676   /* put in the tcp options to say mtu. */
2677   ptr=(unsigned char *)(t1+1);
2678   ptr[0]=2;
2679   ptr[1]=4;
2680   ptr[2]=(dev->mtu- HEADER_SIZE) >> 8;
2681   ptr[3]=(dev->mtu- HEADER_SIZE) & 0xff;
2682   sk->mtu = dev->mtu - HEADER_SIZE;
2683   tcp_send_check (t1, sk->saddr, sk->daddr,
2684                   sizeof (struct tcp_header) + 4, sk);
2685   /* this must go first otherwise a really quick response will
2686      get reset. */
2687   sk->state = TCP_SYN_SENT;
2688 
2689   sk->prot->queue_xmit(sk, dev, buff, 0);
2690   
2691   sk->time_wait.len = TCP_CONNECT_TIME;
2692   reset_timer ((struct timer *)&sk->time_wait);
2693   sk->retransmits = TCP_RETR2 - TCP_SYN_RETRIES;
2694   release_sock (sk);
2695   return (0);
2696 }
2697 
2698 
2699 /* this functions checks to see if the tcp header is actually
2700    acceptible. */
2701 
2702 static  int
2703 tcp_sequence (volatile struct sock *sk, struct tcp_header *th, short len,
     /* [previous][next][first][last][top][bottom][index][help] */
2704               struct options *opt, unsigned long saddr)
2705 {
2706    /* this isn't quite right.  sk->acked_seq could be more recent
2707       than sk->window.  This is however close enough.  We will accept
2708       slightly more packets than we should, but it should not cause
2709       problems unless someone is trying to forge packets. */
2710 
2711   PRINTK ("tcp_sequence (sk=%X, th=%X, len = %d, opt=%d, saddr=%X)\n",
2712           sk, th, len, opt, saddr);
2713 
2714   if (between(th->seq, sk->acked_seq, sk->acked_seq + sk->window)||
2715       between(th->seq + len-sizeof (*th), sk->acked_seq, 
2716               sk->acked_seq + sk->window) ||
2717       (before (th->seq, sk->acked_seq) &&
2718        after (th->seq + len - sizeof (*th), sk->acked_seq + sk->window)))
2719     {
2720        return (1);
2721     }
2722 
2723   PRINTK ("tcp_sequence: rejecting packet. \n");
2724 
2725   /* if it's too far ahead, send an ack to let the other end
2726      know what we expect. */
2727   if (after (th->seq, sk->acked_seq + sk->window))
2728     {
2729        tcp_send_ack (sk->send_seq, sk->acked_seq, sk, th, saddr);
2730        return (0);
2731     }
2732 
2733   /* in case it's just a late ack, let it through */
2734   if (th->ack && len == th->doff*4 && after (th->seq, sk->acked_seq - 32767) &&
2735       !th->fin && !th->syn) return (1);
2736 
2737   if (!th->rst)
2738     {
2739        /* try to resync things. */
2740        tcp_send_ack (net32(th->ack_seq), sk->acked_seq, sk, th, saddr);
2741     }
2742 
2743 
2744   return (0);
2745 }
2746 
2747 /* This deals with the tcp option.  It isn't very general yet. */
2748 static void
2749 tcp_options (volatile struct sock *sk, struct tcp_header *th)
     /* [previous][next][first][last][top][bottom][index][help] */
2750 {
2751   unsigned char *ptr;
2752   ptr = (unsigned char *)(th + 1);
2753   if (ptr[0] != 2 || ptr[1] != 4)
2754     {
2755        sk->mtu = min (sk->mtu, 576-HEADER_SIZE);
2756        return;
2757     }
2758   sk->mtu = min (sk->mtu, ptr[2]*256 + ptr[3] - HEADER_SIZE);
2759 }
2760 
2761 int
2762 tcp_rcv(struct sk_buff *skb, struct device *dev, struct options *opt,
     /* [previous][next][first][last][top][bottom][index][help] */
2763         unsigned long daddr, unsigned short len,
2764         unsigned long saddr, int redo, struct ip_protocol * protocol)
2765 {
2766   struct tcp_header *th;
2767   volatile struct sock *sk;
2768 
2769   if (!skb)
2770     {
2771       PRINTK ("tcp.c: tcp_rcv skb = NULL\n");
2772       return (0);
2773     }
2774 #if 0 /* it's ok for protocol to be NULL */
2775   if (!protocol)
2776     {
2777       PRINTK ("tcp.c: tcp_rcv protocol = NULL\n");
2778       return (0);
2779     }
2780 
2781   if (!opt) /* it's ok for opt to be NULL */
2782     {
2783       PRINTK ("tcp.c: tcp_rcv opt = NULL\n");
2784     }
2785 #endif
2786   if (!dev)
2787     {
2788       PRINTK ("tcp.c: tcp_rcv dev = NULL\n");
2789       return (0);
2790     }
2791 
2792   th = skb->h.th;
2793 
2794   /* find the socket. */
2795   sk=get_sock(&tcp_prot, net16(th->dest), saddr, th->source, daddr);
2796   PRINTK("<<\n");
2797   PRINTK("len = %d, redo = %d, skb=%X\n", len, redo, skb);
2798 
2799   if (sk)
2800     {
2801       PRINTK ("sk = %X:\n",sk);
2802       print_sk (sk);
2803     }
2804 
2805   if (!redo)
2806     {
2807        if (th->check && tcp_check (th, len, saddr, daddr ))
2808          {
2809             skb->sk = NULL;
2810             PRINTK ("packet dropped with bad checksum.\n");
2811             kfree_skb (skb, 0);
2812             /* we don't release the socket because it was never
2813                marked in use. */
2814             return (0);
2815          }
2816 
2817        /*See if we know about the socket. */
2818        if (sk == NULL)
2819         {
2820           if (!th->rst)
2821             tcp_reset (daddr, saddr, th, &tcp_prot, opt,dev);
2822           skb->sk = NULL;
2823           kfree_skb (skb, 0);
2824           return (0);
2825         }
2826 
2827        skb->len = len;
2828        skb->sk = sk;
2829        skb->acked = 0;
2830        skb->used = 0;
2831        skb->free = 0;
2832        skb->urg_used = 0;
2833        skb->saddr = daddr;
2834        skb->daddr = saddr;
2835 
2836        th->seq = net32(th->seq);
2837 
2838        cli();
2839 
2840        /* we may need to add it to the backlog here. */
2841        if (sk->inuse)
2842          {
2843             if (sk->back_log == NULL)
2844               {
2845                  sk->back_log = skb;
2846                  skb->next = skb;
2847                  skb->prev = skb;
2848               }
2849             else
2850               {
2851                  skb->next = sk->back_log;
2852                  skb->prev = sk->back_log->prev;
2853                  skb->prev->next = skb;
2854                  skb->next->prev = skb;
2855               }
2856             sti();
2857             return (0);
2858          }
2859        sk->inuse = 1;
2860        sti();
2861      }
2862   else
2863     {
2864       if (!sk)
2865         {
2866           PRINTK ("tcp.c: tcp_rcv bug sk=NULL redo = 1\n");
2867           return (0);
2868         }
2869     }
2870 
2871   if (!sk->prot)
2872     {
2873       PRINTK ("tcp.c: tcp_rcv sk->prot = NULL \n");
2874       return (0);
2875     }
2876 
2877   /* charge the memory to the socket. */
2878   if (sk->rmem_alloc + skb->mem_len >= SK_RMEM_MAX)
2879     {
2880        skb->sk = NULL;
2881        PRINTK ("dropping packet due to lack of buffer space.\n");
2882        kfree_skb (skb, 0);
2883        release_sock (sk);
2884        return (0);
2885     }
2886        
2887   sk->rmem_alloc += skb->mem_len;
2888 
2889   PRINTK ("About to do switch. \n");
2890 
2891   /* now deal with it. */
2892 
2893   switch (sk->state)
2894     {
2895        /* this should close the system down if it's waiting for an
2896           ack that is never going to be sent. */
2897     case TCP_LAST_ACK:
2898       if (th->rst)
2899         {
2900           sk->err = ECONNRESET;
2901           sk->state = TCP_CLOSE;
2902           sk->shutdown = SHUTDOWN_MASK;
2903           if (!sk->dead)
2904             {
2905               wake_up (sk->sleep);
2906             }
2907           kfree_skb (skb, FREE_READ);
2908           release_sock(sk);
2909           return (0);
2910         }
2911 
2912     case TCP_ESTABLISHED:
2913     case TCP_CLOSE_WAIT:
2914     case TCP_FIN_WAIT1:
2915     case TCP_FIN_WAIT2:
2916     case TCP_TIME_WAIT:
2917    
2918       if (!tcp_sequence (sk, th, len, opt, saddr))
2919         {
2920            kfree_skb (skb, FREE_READ);
2921            release_sock(sk);
2922            return (0);
2923         }
2924 
2925       if (th->rst)
2926         {
2927           /* this means the thing should really be closed. */
2928           sk->err = ECONNRESET;
2929 
2930           if (sk->state == TCP_CLOSE_WAIT)
2931             {
2932               sk->err = EPIPE;
2933             }
2934 
2935           /* a reset with a fin just means that the
2936              data was not all read. */
2937           if (!th->fin)
2938             {
2939               sk->state = TCP_CLOSE;
2940               sk->shutdown = SHUTDOWN_MASK;
2941               if (!sk->dead)
2942                 {
2943                   wake_up (sk->sleep);
2944                 }
2945               kfree_skb (skb, FREE_READ);
2946               release_sock(sk);
2947               return (0);
2948             }
2949         }
2950       if (opt && (opt->security != 0 || opt->compartment != 0 || th->syn))
2951         {
2952            sk->err = ECONNRESET;
2953            sk->state = TCP_CLOSE;
2954            sk->state = SHUTDOWN_MASK;
2955            tcp_reset (daddr, saddr,  th, sk->prot, opt,dev);
2956            if (!sk->dead)
2957              {
2958                 wake_up (sk->sleep);
2959              }
2960            kfree_skb (skb, FREE_READ);
2961            release_sock(sk);
2962            return (0);
2963         }
2964 
2965       if (th->ack)
2966         {
2967            if(!tcp_ack (sk, th, saddr))
2968             {
2969                kfree_skb (skb, FREE_READ);
2970                release_sock(sk);
2971                return (0);
2972            }
2973         }
2974       if (th->urg)
2975         {
2976           if (tcp_urg (sk, th, saddr))
2977             {
2978                kfree_skb (skb, FREE_READ);
2979                release_sock(sk);
2980                return (0);
2981             }
2982         }
2983 
2984       if (th->fin && tcp_fin (sk, th, saddr, dev))
2985         {
2986           kfree_skb (skb, FREE_READ);
2987           release_sock(sk);
2988           return (0);
2989         }
2990 
2991       if ( tcp_data (skb, sk, saddr, len))
2992         {
2993            kfree_skb (skb, FREE_READ);
2994            release_sock(sk);
2995            return (0);
2996         }
2997 
2998       release_sock(sk);
2999       return (0);
3000 
3001     case TCP_CLOSE:
3002 
3003       if (sk->dead || sk->daddr)
3004         {
3005            PRINTK ("packet received for closed,dead socket\n");
3006            kfree_skb (skb, FREE_READ);
3007            release_sock (sk);
3008            return (0);
3009         }
3010 
3011       if (!th->rst)
3012         {
3013           if (!th->ack)
3014             th->ack_seq=0;
3015           tcp_reset (daddr, saddr, th, sk->prot, opt,dev);
3016         }
3017       kfree_skb (skb, FREE_READ);
3018       release_sock(sk);
3019       return (0);
3020 
3021     case TCP_LISTEN:
3022       if (th->rst)
3023         {
3024            kfree_skb (skb, FREE_READ);
3025            release_sock(sk);
3026            return (0);
3027         }
3028       if (th->ack)
3029         {
3030           tcp_reset (daddr, saddr, th, sk->prot, opt,dev );
3031           kfree_skb (skb, FREE_READ);
3032           release_sock(sk);
3033           return (0);
3034         }
3035 
3036       if (th->syn)
3037         {
3038 /*        if (opt->security != 0 || opt->compartment != 0)
3039             {
3040               tcp_reset (daddr, saddr, th, prot, opt,dev);
3041               release_sock(sk);
3042               return (0);
3043             } */
3044 
3045           /* now we just put the whole thing including the header
3046              and saddr, and protocol pointer into the buffer.
3047              We can't respond until the user tells us to accept
3048              the connection. */
3049 
3050           tcp_conn_request (sk, skb, daddr, saddr, opt, dev);
3051 
3052           release_sock(sk);
3053           return (0);
3054         }
3055 
3056       kfree_skb (skb, FREE_READ);
3057       release_sock(sk);
3058       return (0);
3059 
3060     default:
3061       if (!tcp_sequence (sk, th, len, opt, saddr)) 
3062         {
3063            kfree_skb (skb, FREE_READ);
3064            release_sock(sk);
3065            return (0);
3066         }
3067 
3068     case TCP_SYN_SENT:
3069       if (th->rst)
3070         {
3071           sk->err = ECONNREFUSED ;
3072           sk->state = TCP_CLOSE;
3073           sk->state = SHUTDOWN_MASK;
3074           if (!sk->dead)
3075             {
3076               wake_up (sk->sleep);
3077             }
3078           kfree_skb (skb, FREE_READ);
3079           release_sock(sk);
3080           return (0);
3081         }
3082 /*      if (opt->security != 0 || opt->compartment != 0 )
3083         {
3084           sk->err = ECONNRESET;
3085           sk->state = TCP_CLOSE;
3086           tcp_reset (daddr, saddr,  th, sk->prot, opt, dev);
3087           if (!sk->dead)
3088           {
3089           wake_up (sk->sleep);
3090           }
3091           kfree_skb (skb, FREE_READ);
3092           release_sock(sk);
3093           return (0);
3094         } */
3095 
3096       if (!th->ack) 
3097         {
3098           if (th->syn)
3099             {
3100               sk->state = TCP_SYN_RECV;
3101             }
3102 
3103           kfree_skb (skb, FREE_READ);
3104           release_sock(sk);
3105           return (0);
3106         }
3107 
3108       switch (sk->state)
3109         {
3110         case TCP_SYN_SENT:
3111           if (!tcp_ack(sk, th, saddr))
3112             {
3113               tcp_reset(daddr, saddr, th, sk->prot, opt,dev);
3114               kfree_skb (skb, FREE_READ);
3115               release_sock(sk);
3116               return (0);
3117             }
3118 
3119           /* if the syn bit is also set, switch to tcp_syn_recv,
3120              and then to established. */
3121               
3122           if (!th->syn) 
3123             {
3124               kfree_skb (skb, FREE_READ);
3125               release_sock (sk);
3126               return (0);
3127             }
3128 
3129           /* ack the syn and fall through. */
3130           sk->acked_seq = th->seq+1;
3131           sk->fin_seq = th->seq;
3132           tcp_send_ack (sk->send_seq, th->seq+1, sk, 
3133                              th, sk->daddr);
3134         
3135         case TCP_SYN_RECV:
3136           if (!tcp_ack(sk, th, saddr))
3137             {
3138               tcp_reset(daddr, saddr, th, sk->prot, opt, dev);
3139               kfree_skb (skb, FREE_READ);
3140               release_sock(sk);
3141               return (0);
3142             }
3143 
3144           sk->state = TCP_ESTABLISHED;
3145           /* now we need to finish filling out some of the tcp
3146              header. */
3147 
3148           /* we need to check for mtu info. */
3149           tcp_options(sk, th);
3150           sk->dummy_th.dest = th->source;
3151           sk->copied_seq = sk->acked_seq-1;
3152           if (!sk->dead)
3153             {
3154               wake_up (sk->sleep);
3155             }
3156 
3157           /* now process the rest like we were already in the established
3158              state. */
3159           if (th->urg)
3160             if (tcp_urg (sk, th, saddr))
3161               {
3162                  kfree_skb (skb, FREE_READ);
3163                  release_sock(sk);
3164                  return (0);
3165               }
3166           if (tcp_data (skb, sk, saddr, len))
3167             kfree_skb (skb, FREE_READ);
3168           
3169           if (th->fin)
3170             tcp_fin(sk, th, saddr, dev);
3171 
3172           release_sock(sk);
3173           return (0);
3174         }
3175 
3176       if (th->urg)
3177         {
3178           if (tcp_urg (sk, th, saddr))
3179             {
3180                kfree_skb (skb, FREE_READ);
3181                release_sock (sk);
3182                return (0);
3183             }
3184         }
3185 
3186       if (tcp_data (skb, sk, saddr, len))
3187         {
3188            kfree_skb (skb, FREE_READ);
3189            release_sock (sk);
3190            return (0);
3191         }
3192 
3193       if (!th->fin)
3194         {
3195           release_sock(sk);
3196           return (0);
3197         }
3198       tcp_fin (sk, th, saddr, dev);
3199       release_sock(sk);
3200       return (0);
3201     }
3202 }
3203 
3204 
3205 /* this routine sends a packet with an out of date sequence number. It
3206    assumes the other end will try to ack it. */
3207 
3208 static  void
3209 tcp_write_wakeup(volatile struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
3210 {
3211   struct sk_buff *buff;
3212   struct tcp_header *t1;
3213   struct device *dev=NULL;
3214   int tmp;
3215   if (sk -> state != TCP_ESTABLISHED && sk->state != TCP_CLOSE_WAIT) return;
3216 
3217   buff=sk->prot->wmalloc(sk,MAX_ACK_SIZE,1, GFP_ATOMIC);
3218   /* no big loss. */
3219   if (buff == NULL) return;
3220 
3221   buff->lock = 0;
3222   buff->mem_addr = buff;
3223   buff->mem_len = MAX_ACK_SIZE;
3224   buff->len=sizeof (struct tcp_header);
3225   buff->free = 1;
3226   buff->sk = sk;
3227   PRINTK ("in tcp_write_wakeup\n");
3228   t1=(struct tcp_header *)(buff + 1);
3229 
3230   /* put in the ip_header and routing stuff. */
3231   tmp = sk->prot->build_header (buff, sk->saddr, sk->daddr, &dev,
3232                                 IPPROTO_TCP, sk->opt, MAX_ACK_SIZE);
3233   if (tmp < 0)
3234     {
3235       sk->prot->wfree(sk, buff->mem_addr, buff->mem_len);
3236       return;
3237     }
3238 
3239   buff->len += tmp;
3240   t1 = (struct tcp_header *)((char *)t1 +tmp);
3241 
3242   memcpy (t1,(void *) &sk->dummy_th, sizeof (*t1));
3243 
3244   /* use a previous sequence.  This should cause the other end
3245      to send an ack. */
3246   t1->seq = net32(sk->send_seq-1);
3247   t1->ack = 1; 
3248   t1->res1= 0;
3249   t1->res2= 0;
3250   t1->rst = 0;
3251   t1->urg = 0;
3252   t1->psh = 0;
3253   t1->fin = 0;
3254   t1->syn = 0;
3255   t1->ack_seq = net32(sk->acked_seq);
3256   t1->window = net16(sk->prot->rspace(sk));
3257   t1->doff = sizeof (*t1)/4;
3258   tcp_send_check (t1, sk->saddr, sk->daddr, sizeof (*t1), sk);
3259   /* send it and free it.  This will prevent the timer from 
3260      automatically being restarted. */
3261   sk->prot->queue_xmit(sk, dev, buff, 1);
3262 
3263 }
3264 
3265 struct proto tcp_prot =
3266 {
3267   sock_wmalloc,
3268   sock_rmalloc,
3269   sock_wfree,
3270   sock_rfree,
3271   sock_rspace,
3272   sock_wspace,
3273   tcp_close,
3274   tcp_read,
3275   tcp_write,
3276   tcp_sendto,
3277   tcp_recvfrom,
3278   ip_build_header,
3279   tcp_connect,
3280   tcp_accept,
3281   ip_queue_xmit,
3282   tcp_retransmit,
3283   tcp_write_wakeup,
3284   tcp_read_wakeup,
3285   tcp_rcv,
3286   tcp_select,
3287   tcp_ioctl,
3288   NULL,
3289   tcp_shutdown,
3290   128,
3291   0,
3292   {NULL,}
3293 };
3294 
3295 
3296 
3297 

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