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

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