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_ack
  13. tcp_build_header
  14. tcp_write
  15. tcp_read_wakeup
  16. cleanup_rbuf
  17. tcp_read_urg
  18. tcp_read
  19. tcp_reset
  20. tcp_conn_request
  21. tcp_close
  22. tcp_write_xmit
  23. tcp_ack
  24. tcp_data
  25. tcp_urg
  26. tcp_fin
  27. tcp_accept
  28. tcp_connect
  29. tcp_sequence
  30. tcp_options
  31. tcp_rcv
  32. 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 
  23 #include <linux/types.h>
  24 #include <linux/sched.h>
  25 #include <linux/mm.h>
  26 #include <linux/string.h>
  27 #include <linux/socket.h>
  28 #include <netinet/in.h>
  29 #include <linux/fcntl.h>
  30 #include "timer.h"
  31 #include "ip.h"
  32 #include "icmp.h"
  33 #include "tcp.h"
  34 #include "sock.h"
  35 #include <linux/errno.h>
  36 #include <linux/timer.h>
  37 #include <asm/system.h>
  38 #include <asm/segment.h>
  39 /* #include <signal.h>*/
  40 #include <linux/termios.h> /* for ioctl's */
  41 #include "../kern_sock.h" /* for PRINTK */
  42 
  43 #define tmax(a,b) (before ((a),(b)) ? (b) : (a))
  44 #define swap(a,b) {unsigned long c; c=a; a=b; b=c;}
  45 
  46 extern struct proto tcp_prot;
  47 
  48 static  int 
  49 min (unsigned int a, unsigned int b)
     /* [previous][next][first][last][top][bottom][index][help] */
  50 {
  51    if (a < b) return (a);
  52    return (b);
  53 }
  54 
  55 void
  56 print_th (struct tcp_header *th)
     /* [previous][next][first][last][top][bottom][index][help] */
  57 {
  58    unsigned char *ptr;
  59    ptr = (unsigned char *)(th + 1);
  60    PRINTK ("tcp header:\n");
  61    PRINTK ("  source=%d, dest=%d, seq =%d, ack_seq = %d\n",
  62            net16(th->source), net16(th->dest), net32(th->seq),
  63            net32(th->ack_seq));
  64    PRINTK ("  fin=%d, syn=%d, rst=%d, psh=%d, ack=%d, urg=%d res1=%d res2=%d\n"
  65            ,th->fin, th->syn, th->rst, th->psh, th->ack, th->urg,
  66            th->res1, th->res2);
  67    PRINTK ("  window = %d, check = %d urg_ptr = %d\n",
  68            net16(th->window), net16(th->check), net16(th->urg_ptr));
  69    PRINTK ("  doff = %d\n",th->doff);
  70    PRINTK ("options = %d %d %d %d\n", ptr[0], ptr[1], ptr[2], ptr[3]);
  71  }
  72 
  73  /* This routine grabs the first thing off of a rcv queue. */
  74 static  struct sk_buff *
  75 get_firstr(volatile struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
  76 {
  77    struct sk_buff *skb;
  78    skb = sk->rqueue;
  79    if (skb == NULL) return (NULL);
  80    sk->rqueue = skb->next;
  81    if (sk->rqueue == skb)
  82      {
  83        sk->rqueue = NULL;
  84      }
  85    else
  86      {
  87        sk->rqueue->prev=skb->prev;
  88        sk->rqueue->prev->next = sk->rqueue;
  89      }
  90    return (skb);
  91 }
  92 
  93 static  long
  94 diff (unsigned long seq1, unsigned long seq2)
     /* [previous][next][first][last][top][bottom][index][help] */
  95 {
  96    long d;
  97    d=seq1-seq2;
  98    if (d > 0) return (d);
  99    /* I hope this returns what I want. */
 100    return (~d+1);
 101 }
 102 
 103  /* enter the time wait state. */
 104 static  void
 105 tcp_time_wait (volatile struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 106 {
 107    sk->state = TCP_TIME_WAIT;
 108    sk->time_wait.len = TCP_TIMEWAIT_LEN;
 109    sk->timeout = TIME_CLOSE;
 110    reset_timer ((struct timer *)&sk->time_wait);
 111 }
 112 
 113 static void
 114 tcp_retransmit (volatile struct sock *sk, int all)
     /* [previous][next][first][last][top][bottom][index][help] */
 115 {
 116    if (all) 
 117      {
 118         ip_retransmit (sk, all);
 119         return;
 120      }
 121    sk->rtt *= 2; /* exponential back off. */
 122    if (sk->cong_window > 1)
 123      sk->cong_window = sk->cong_window / 2;
 124    sk->exp_growth = 0;
 125 
 126    /* do the actuall retransmit. */
 127    ip_retransmit (sk, all);
 128    
 129 }
 130 
 131 /* this routine is called by the icmp module when it gets some
 132    sort of error condition.  If err < 0 then the socket should
 133    be closed and the error returned to the user.  If err > 0
 134    it's just the icmp type << 8 | icmp code.  
 135    header points to the first 8 bytes of the tcp header.  We need
 136    to find the appropriate port. */
 137 
 138 void
 139 tcp_err (int err, unsigned char *header, unsigned long daddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 140          unsigned long saddr, struct ip_protocol *protocol)
 141 {
 142    struct tcp_header *th;
 143    volatile struct sock *sk;
 144    
 145    th = (struct tcp_header *)header;
 146    sk = get_sock (&tcp_prot, net16(th->dest), saddr, th->source, daddr);
 147 
 148    if (sk == NULL) return;
 149 
 150    if (err & 0xff00 == (ICMP_SOURCE_QUENCH << 8))
 151      {
 152         /* for now we will just trigger a linear backoff. The slow start
 153            code should cause a real backoff here. */
 154 
 155         if (sk->cong_window > 1)
 156           sk->cong_window --;
 157 
 158         return;
 159      }
 160 
 161    sk->err = icmp_err_convert[err & 0xff].errno;
 162    if (icmp_err_convert[err & 0xff].fatal)
 163      {
 164         if (sk->state != TCP_ESTABLISHED)
 165           sk->state = TCP_CLOSE;
 166         sk->prot->close(sk, 0);
 167      }
 168 
 169    return;
 170    
 171 }
 172 
 173 static int
 174 tcp_select (volatile struct sock *sk, int sel_type, select_table *wait)
     /* [previous][next][first][last][top][bottom][index][help] */
 175 {
 176   switch (sel_type)
 177     {
 178     case SEL_IN:
 179        select_wait (sk->sleep, wait);
 180        if (sk->rqueue != NULL &&
 181            (between (sk->copied_seq, sk->rqueue->next->h.th->seq - 1,
 182                      sk->rqueue->next->h.th->seq + sk->rqueue->next->len) ||
 183             sk->state == TCP_LISTEN))
 184          {
 185             return (1);
 186          }
 187 
 188       switch (sk->state)
 189         {
 190         case TCP_LISTEN:
 191         case TCP_ESTABLISHED:
 192         case TCP_SYN_SENT:
 193         case TCP_SYN_RECV:
 194            return (0);
 195         default:
 196            return (1);
 197         }
 198 
 199     case SEL_OUT:
 200        select_wait (sk->sleep, wait);
 201        if (sk->state != TCP_ESTABLISHED) return (1);
 202       /* hack so it will probably be able to write something
 203          if it says it's ok to write. */
 204       if (sk->prot->wspace(sk) >= MIN_WRITE_SPACE) return (1);
 205       return (0);
 206 
 207     case SEL_EX:
 208       select_wait(sk->sleep,wait);
 209       if (sk->err) return (1);
 210       if (sk->state == TCP_FIN_WAIT1 ||
 211           sk->state == TCP_FIN_WAIT2 ||
 212           sk->state == TCP_TIME_WAIT ||
 213           sk->state == TCP_LAST_ACK)
 214         return (1);
 215       return (0);
 216     }
 217   return (0);
 218 }
 219 
 220 static int
 221 tcp_ioctl (volatile struct sock *sk, int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 222 {
 223    switch (cmd)
 224      {
 225        default:
 226         return (-EINVAL);
 227 
 228       case TIOCINQ:
 229 /*      case FIONREAD:*/
 230         {
 231           unsigned long amount;
 232           struct sk_buff *skb;
 233 
 234           if (sk->state == TCP_LISTEN)
 235             return (-EINVAL);
 236 
 237           amount = 0;
 238           if (sk->rqueue != NULL)
 239             {
 240               skb = sk->rqueue->next;
 241               /* go until a push or until we are out of data. */
 242               do {
 243                 amount += skb -> len;
 244                 if (skb->h.th->psh) break;
 245                 skb = skb->next;
 246               } while (skb != sk->rqueue->next);
 247             }
 248 
 249           verify_area ((void *)arg, sizeof (unsigned long));
 250           put_fs_long (amount, (unsigned long *)arg);
 251           return (0);
 252         }
 253 
 254        case SIOCATMARK:
 255         {
 256           struct sk_buff *skb;
 257           int answ=0;
 258           /* try to figure out if we need to read some urgent data. */
 259           if (sk->rqueue != NULL)
 260             {
 261               skb = sk->rqueue->next;
 262               if (sk->copied_seq+1 == skb->h.th->seq && skb->h.th->urg)
 263                 answ = 1;
 264             }
 265           verify_area ((void *) arg, sizeof (unsigned long));
 266           put_fs_long (answ, (void *) arg);
 267           return (0);
 268         }
 269        
 270       case TIOCOUTQ:
 271         {
 272           unsigned long amount;
 273           if (sk->state == TCP_LISTEN)
 274             return (-EINVAL);
 275           amount = sk->prot->wspace(sk)/2;
 276           verify_area ((void *)arg, sizeof (unsigned long));
 277           put_fs_long (amount, (unsigned long *)arg);
 278           return (0);
 279         }
 280 
 281      }
 282 }
 283 
 284 
 285 /* this routine computes a tcp checksum */
 286 static  unsigned short
 287 tcp_check (struct tcp_header *th, int len, unsigned long saddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 288            unsigned long daddr)
 289 {     
 290    unsigned long sum;
 291    
 292    if (saddr == 0) saddr = MY_IP_ADDR;
 293    print_th (th);
 294    __asm__("\t addl %%ecx,%%ebx\n"
 295            "\t adcl %%edx,%%ebx\n"
 296            "\t adcl $0, %%ebx\n"
 297            : "=b" (sum)
 298            : "0" (daddr), "c" (saddr), "d" ((net16(len) << 16) + IPPROTO_TCP*256)
 299            : "cx","bx","dx" );
 300    
 301    if (len > 3)
 302      {
 303         __asm__(
 304                 "\tclc\n"
 305                 "1:\n"
 306                 "\t lodsl\n"
 307                 "\t adcl %%eax, %%ebx\n"
 308                 "\t loop 1b\n"
 309                 "\t adcl $0, %%ebx\n"
 310                 : "=b" (sum) , "=S" (th)
 311                 : "0" (sum), "c" (len/4) ,"1" (th)
 312                 : "ax", "cx", "bx", "si" );
 313      }
 314    
 315    /* convert from 32 bits to 16 bits. */
 316    __asm__(
 317            "\t movl %%ebx, %%ecx\n"
 318            "\t shrl $16,%%ecx\n"
 319            "\t addw %%cx, %%bx\n"
 320            "\t adcw $0, %%bx\n"
 321            : "=b" (sum)
 322            : "0" (sum)
 323            : "bx", "cx");
 324    
 325    /* check for an extra word. */
 326    if ((len & 2) != 0)
 327      {
 328         __asm__("\t lodsw\n"
 329                 "\t addw %%ax,%%bx\n"
 330                 "\t adcw $0, %%bx\n"
 331                 : "=b" (sum), "=S" (th)
 332                 : "0" (sum) ,"1" (th)
 333                 : "si", "ax", "bx");
 334      }
 335    
 336    /* now check for the extra byte. */
 337    if ((len & 1) != 0)
 338      {
 339         __asm__("\t lodsb\n"
 340                 "\t movb $0,%%ah\n"
 341                 "\t addw %%ax,%%bx\n"
 342                 "\t adcw $0, %%bx\n"
 343                 : "=b" (sum)
 344                 : "0" (sum) ,"S" (th)
 345                 : "si", "ax", "bx");
 346      }
 347    
 348    /* we only want the bottom 16 bits, but we never cleared
 349       the top 16. */
 350    return ((~sum) & 0xffff);
 351 }
 352 
 353 
 354 static  void
 355 tcp_send_check (struct tcp_header *th, unsigned long saddr, 
     /* [previous][next][first][last][top][bottom][index][help] */
 356                  unsigned long daddr, int len, volatile struct sock *sk)
 357  {
 358 
 359    th->check = 0;
 360    if (sk && sk->no_check) return;
 361    th->check = tcp_check (th, len, saddr, daddr);
 362    return;
 363 }
 364 
 365  /* This routine sends an ack and also updates the window. */
 366  static  void
 367  tcp_send_ack (unsigned long sequence, unsigned long ack,
     /* [previous][next][first][last][top][bottom][index][help] */
 368                volatile struct sock *sk,
 369                struct tcp_header *th, unsigned long daddr)
 370  {
 371    struct sk_buff *buff;
 372    struct tcp_header *t1;
 373    struct device *dev=NULL;
 374    int tmp;
 375 
 376    /* we need to grab some memory, and put together an ack, and then
 377       put it into the queue to be sent. */
 378 
 379    buff=sk->prot->wmalloc(sk,MAX_ACK_SIZE,1);
 380    if (buff == NULL) 
 381      {
 382         /* force it to send an ack. */
 383         sk->ack_backlog++;
 384         if (sk->timeout != TIME_WRITE && sk->state < TCP_CLOSING)
 385           {
 386              sk->timeout = TIME_WRITE;
 387              sk->time_wait.len = 10; /* got to do it quickly. */
 388              reset_timer ((struct timer *)&sk->time_wait);
 389           }
 390         return;
 391      }
 392 
 393    buff->mem_addr = buff;
 394    buff->mem_len = MAX_ACK_SIZE;
 395    buff->len=sizeof (struct tcp_header);
 396    buff->sk = sk;
 397    t1 = (struct tcp_header *)(buff + 1);
 398    /* put in the ip_header and routing stuff. */
 399    tmp = sk->prot->build_header (buff, sk->saddr, daddr, &dev,
 400                                  IPPROTO_TCP, sk->opt, MAX_ACK_SIZE);
 401    if (tmp < 0)
 402      {
 403        sk->prot->wfree(sk, buff->mem_addr, buff->mem_len);
 404        return;
 405      }
 406    buff->len += tmp;
 407    t1 = (struct tcp_header *)((char *)t1 +tmp);
 408 
 409    memcpy (t1, th, sizeof (*t1)); /* this should probably be removed. */
 410 
 411    /* swap the send and the receive. */
 412    t1->dest = th->source;
 413    t1->source = th->dest;
 414    t1->seq = net32(sequence);
 415    t1->ack = 1;
 416    sk->window = sk->prot->rspace(sk);
 417    t1->window = net16(sk->window);
 418    t1->res1=0;
 419    t1->res2=0;
 420    t1->rst = 0;
 421    t1->urg = 0;
 422    t1->syn = 0;
 423    t1->psh = 0;
 424    t1->fin = 0;
 425    if (ack == sk->acked_seq)
 426      {
 427         sk->ack_backlog = 0;
 428         sk->bytes_rcv = 0;
 429         sk->ack_timed = 0;
 430         if (sk->send_head == NULL &&
 431             sk->wfront == NULL)
 432           {
 433              delete_timer((struct timer *)&sk->time_wait);
 434              sk->timeout = 0;
 435           }
 436         
 437      }
 438    t1->ack_seq = net32(ack);
 439    t1->doff = sizeof (*t1)/4;
 440    tcp_send_check (t1, sk->saddr, daddr, sizeof (*t1), sk);
 441    sk->prot->queue_xmit(sk, dev, buff, 1);
 442 }
 443 
 444 /* this routine builds a generic tcp header. */
 445 static  int
 446 tcp_build_header(struct tcp_header *th, volatile struct sock *sk, int push)
     /* [previous][next][first][last][top][bottom][index][help] */
 447 {
 448 
 449  /* want to get rid of this. */
 450   memcpy (th,(void *) &(sk->dummy_th), sizeof (*th));
 451   th->seq = net32(sk->send_seq);
 452   th->psh = (push == 0) ? 1 : 0;
 453   th->doff = sizeof (*th)/4;
 454   th->ack = 1;
 455   th->fin = 0;
 456   sk->ack_backlog = 0;
 457   sk->bytes_rcv = 0;
 458   sk->ack_timed = 0;
 459   th->ack_seq = net32(sk->acked_seq);
 460   sk->window = sk->prot->rspace(sk);
 461   th->window = net16(sk->window);
 462 
 463   return (sizeof (*th));
 464 }
 465 
 466 /* This routine copies from a user buffer into a socket, and starts
 467    the transmit system. */
 468 
 469 static int
 470 tcp_write(volatile struct sock *sk, unsigned char *from,
     /* [previous][next][first][last][top][bottom][index][help] */
 471           int len, int nonblock, unsigned flags)
 472 {
 473   int copied=0;
 474   int copy;
 475   int tmp;
 476   struct sk_buff *skb;
 477   unsigned char *buff;
 478   struct proto *prot;
 479   struct device *dev=NULL;
 480 
 481   PRINTK ("in TCP_WRITE sk = %X:\n",sk);
 482   print_sk (sk);
 483 
 484   sk->inuse = 1; /* no one else will use this socket. */
 485   prot = sk->prot;
 486   while (len > 0)
 487     {
 488        /* first thing we do is make sure that we are established. */     
 489 
 490       while (sk->state != TCP_ESTABLISHED)
 491         {
 492           if (sk->state != TCP_SYN_SENT &&
 493               sk->state != TCP_SYN_RECV)
 494            {
 495               release_sock (sk);
 496               if (sk->keepopen)
 497                 {
 498                     send_sig (SIGPIPE, current, 0);
 499                     return (-EINTR);
 500                 }
 501               if (copied) return (copied);
 502               if (sk->err) return (-sk->err);
 503               return (-ENOTCONN);
 504             }
 505 
 506           if (nonblock)
 507             {
 508               release_sock (sk);
 509               return (-EAGAIN);
 510             }
 511 
 512           /* now here is a race condition.
 513              release_sock could cause the connection to
 514              enter the established mode, if that is the
 515              case, then we will block here for ever, because
 516              we will have gotten our wakeup call before we
 517              go to sleep. */
 518           release_sock (sk);
 519           cli();
 520           if (sk->state != TCP_ESTABLISHED)
 521             {
 522               interruptible_sleep_on (sk->sleep);
 523               if (current->signal & ~current->blocked)
 524                 {
 525                    sti();
 526                    if (copied) return (copied);
 527                    return (-ERESTARTSYS);
 528                 }
 529             }
 530           sti();
 531           sk->inuse = 1;
 532         }
 533       /* we also need to worry about the window.  The smallest we
 534          will send is about 200 bytes. */
 535 
 536 
 537       copy = min (sk->mtu, diff(sk->window_seq, sk->send_seq));
 538 
 539       /* redundent check here. */
 540       if (copy < 200 || copy > sk->mtu) copy = sk->mtu;
 541       copy = min (copy, len);
 542 
 543       skb=prot->wmalloc (sk, copy + prot->max_header+sizeof (*skb),0);
 544 
 545       /* if we didn't get any memory, we need to sleep. */
 546       if (skb == NULL)
 547         {
 548           if (nonblock ||  copied)
 549             {
 550               break;
 551             }
 552           /* here is another race condition. */
 553           tmp = sk->wmem_alloc;
 554           release_sock (sk);
 555           /* again we will try to avoid it. */
 556           cli ();
 557           if (tmp <= sk->wmem_alloc)
 558             {
 559               interruptible_sleep_on (sk->sleep);
 560               if (current->signal & ~current->blocked)
 561                 {
 562                    sti();
 563                    if (copied) return (copied);
 564                    return (-ERESTARTSYS);
 565                 }
 566             }
 567           sti();
 568           sk->inuse = 1;
 569           continue;
 570         }
 571       skb->mem_addr = skb;
 572       skb->mem_len = copy+prot->max_header+sizeof (*skb);
 573       skb->len = 0;
 574       skb->sk = sk;
 575       buff =(unsigned char *)( skb+1);
 576        /* we need to optimize this.  Perhaps some hints here
 577           would be good. */
 578 
 579       tmp = prot->build_header (skb, sk->saddr, sk->daddr, &dev,
 580                                 IPPROTO_TCP, sk->opt, skb->mem_len);
 581       if (tmp < 0 )
 582         {
 583           prot->wfree (sk, skb->mem_addr, skb->mem_len);
 584           release_sock (sk);
 585           return (tmp);
 586         }
 587       skb->len += tmp;
 588       skb->dev = dev;
 589       buff+=tmp;
 590       tmp = tcp_build_header((struct tcp_header *)buff, sk, len-copy);
 591       if (tmp < 0)
 592         {
 593           prot->wfree (sk, skb->mem_addr, skb->mem_len);
 594           release_sock (sk);
 595           return (tmp);
 596         }
 597 
 598       if (flags & MSG_OOB)
 599         {
 600                 ((struct tcp_header *)buff)->urg = 1;
 601                 ((struct tcp_header *)buff)->urg_ptr = copy;
 602         }
 603       skb->len += tmp;
 604       memcpy_fromfs (buff+tmp, from, copy);
 605 
 606       tcp_send_check ((struct tcp_header *)buff, sk->saddr, sk->daddr,
 607                        copy +sizeof (struct tcp_header), sk);
 608 
 609       from += copy;
 610       copied += copy;
 611       len -= copy;
 612       skb->len += copy;
 613       skb->free = 0;
 614       sk->send_seq += copy;
 615       skb->h.seq = sk->send_seq;
 616       if (after (sk->send_seq , sk->window_seq) ||
 617           sk->packets_out >= sk->cong_window)
 618         {
 619           PRINTK ("sk->cong_window = %d, sk->packets_out = %d\n",
 620                   sk->cong_window, sk->packets_out);
 621           PRINTK ("sk->send_seq = %d, sk->window_seq = %d\n",
 622                   sk->send_seq, sk->window_seq);
 623           skb->next = NULL;
 624           if (sk->wback == NULL)
 625             {
 626               sk->wfront=skb;
 627             }
 628           else
 629             {
 630               sk->wback->next = skb;
 631             }
 632           sk->wback = skb;
 633         }
 634       else
 635         {
 636           prot->queue_xmit (sk, dev, skb,0);
 637         }
 638     }
 639   sk->err = 0;
 640   release_sock (sk);
 641   return (copied);
 642 }
 643 
 644 
 645 static  void
 646 tcp_read_wakeup(volatile struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 647 {
 648   int tmp;
 649   struct device *dev = NULL;
 650   struct tcp_header *t1;
 651   struct sk_buff *buff;
 652 
 653   if (!sk->ack_backlog ) return;
 654   PRINTK ("in tcp read wakeup\n");
 655   /* we need to put code here to prevent this routine from being called. */
 656   /* being called once in a while is ok, so only check if this is the
 657      second time in a row. */
 658 
 659   /* we need to grab some memory, and put together an ack, and then
 660      put it into the queue to be sent. */
 661 
 662   buff=sk->prot->wmalloc(sk,MAX_ACK_SIZE,1);
 663   if (buff == NULL) 
 664     {
 665        /* try again real soon. */
 666        sk->timeout = TIME_WRITE;
 667        sk->time_wait.len = 10;
 668        reset_timer((struct timer *) &sk->time_wait);
 669        return;
 670     }
 671 
 672   buff->mem_addr = buff;
 673   buff->mem_len = MAX_ACK_SIZE;
 674   buff->len=sizeof (struct tcp_header);
 675   buff->sk = sk;
 676 
 677   /* put in the ip_header and routing stuff. */
 678   tmp = sk->prot->build_header (buff, sk->saddr, sk->daddr, &dev,
 679                                 IPPROTO_TCP, sk->opt, MAX_ACK_SIZE);
 680   if (tmp < 0)
 681     {
 682       sk->prot->wfree(sk, buff->mem_addr, buff->mem_len);
 683       return;
 684     }
 685 
 686   buff->len += tmp;
 687   t1 = (struct tcp_header *)((char *)(buff+1) +tmp);
 688 
 689   memcpy (t1,(void *) &sk->dummy_th, sizeof (*t1));
 690   t1->seq = net32(sk->send_seq);
 691   t1->ack = 1;
 692   t1->res1=0;
 693   t1->res2=0;
 694   t1->rst = 0;
 695   t1->urg = 0;
 696   t1->syn = 0;
 697   t1->psh = 0;
 698   sk->ack_backlog = 0;
 699   sk->bytes_rcv = 0;
 700   sk->window = sk->prot->rspace(sk);
 701   t1->window = net16(sk->window);
 702   t1->ack_seq = net32(sk->acked_seq);
 703   t1->doff = sizeof (*t1)/4;
 704   tcp_send_check (t1, sk->saddr, sk->daddr, sizeof (*t1), sk);
 705   sk->prot->queue_xmit(sk, dev, buff, 1);
 706 }
 707 
 708 
 709 /* This routine frees used buffers. */
 710 /* It should consider sending an ack to let the
 711    other end know we now have a bigger window. */
 712 
 713 static  void
 714 cleanup_rbuf (volatile struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 715 {
 716    PRINTK ("cleaning rbuf for sk=%X\n",sk);
 717   /* we have to loop through all the buffer headers, and 
 718      try to free up all the space we can. */
 719   while (sk->rqueue != NULL )
 720     {
 721       struct sk_buff *skb;
 722       skb=sk->rqueue->next;
 723       if (!skb->used) break;
 724       if (sk->rqueue == skb)
 725         {
 726           sk->rqueue = NULL;
 727         }
 728       else
 729         {
 730           skb->next->prev = skb->prev;
 731           skb->prev->next = skb->next;
 732         }
 733       skb->sk = sk;
 734       free_skb (skb, FREE_READ);
 735     }
 736    /* at this point we should send an ack if the difference in
 737       the window, and the amount of space is bigger than
 738       TCP_WINDOW_DIFF */
 739    PRINTK ("sk->window left = %d, sk->prot->rspace(sk)=%d\n",
 740            sk->window - sk->bytes_rcv, sk->prot->rspace(sk));
 741 
 742    if ((sk->prot->rspace(sk) >
 743         (sk->window - sk->bytes_rcv + TCP_WINDOW_DIFF)) ||
 744        (sk->window - sk->bytes_rcv < 2*sk->mtu)) 
 745      {
 746         /* force it to send an ack. */
 747         sk->ack_backlog++;
 748         if (sk->timeout != TIME_WRITE && sk->state == TCP_ESTABLISHED)
 749           {
 750              sk->time_wait.len = TCP_ACK_TIME;
 751              sk->timeout=TIME_WRITE;
 752              reset_timer ((struct timer *)&sk->time_wait);
 753           }
 754      }
 755 
 756 }
 757 
 758 /* handle reading urgent data. */
 759 static  int
 760 tcp_read_urg(volatile struct sock * sk,
     /* [previous][next][first][last][top][bottom][index][help] */
 761              unsigned char *to, int len, unsigned flags)
 762 {
 763     int copied = 0;
 764     struct sk_buff *skb;
 765     PRINTK ("tcp_read_urg(sk=%X, to=%X, len=%d, flags=%X)\n",
 766             sk, to, len, flags);
 767     print_sk(sk);
 768     while (len > 0)
 769       {
 770          sk->inuse = 1;
 771           while (sk->urg==0 || sk->rqueue == NULL)
 772             {
 773                /* now at this point, we may have gotten some data. */
 774                release_sock (sk);
 775                 if (sk->state > TCP_CLOSING)
 776                   {
 777                       if (copied) return (copied);
 778                       return (-ENOTCONN);
 779                   }
 780                cli();
 781                if (sk->urg == 0 || sk->rqueue == NULL)
 782                  {
 783                     interruptible_sleep_on (sk->sleep);
 784                     if (current->signal & ~current->blocked)
 785                       {
 786                          sti();
 787                          if (copied) return (copied);
 788                          return (-ERESTARTSYS);
 789                       }
 790                  }
 791                sti();
 792                sk->inuse = 1;
 793             }
 794           /* now we have some urgent data, we must find it.*/
 795           for (skb = sk->rqueue->next; skb->next != sk->rqueue;
 796                skb = skb->next)
 797             {
 798                 int offset;
 799                 int amt;
 800                 if (!skb->h.th->urg) continue;
 801                 offset = 0;
 802                 amt = min(skb->h.th->urg_ptr,len);
 803                 verify_area (to, amt);
 804                 memcpy_tofs (to, (unsigned char *)(skb->h.th) +
 805                              skb->h.th->doff*4
 806                              + offset, amt);
 807 
 808                 if (!(flags & MSG_PEEK))
 809                   {
 810                      skb->urg_used = 1;
 811                      sk->urg --;
 812                   }
 813                 release_sock (sk);
 814                 copied += amt;
 815                 return (copied);
 816             }
 817       }
 818     return (0);
 819 }
 820 
 821 /* This routine copies from a sock struct into the user buffer. */
 822 static  int
 823 tcp_read(volatile struct sock *sk, unsigned char *to,
     /* [previous][next][first][last][top][bottom][index][help] */
 824          int len, int nonblock, unsigned flags)
 825 {
 826     int copied=0; /* will be used to say how much has been copied. */
 827     struct sk_buff *skb;
 828     unsigned long offset;
 829     unsigned long used;
 830 
 831     if (len == 0) return (0);
 832     if (len < 0) 
 833       {
 834          return (-EINVAL);
 835       }
 836     
 837     /* this error should be checked. */
 838     if (sk->state == TCP_LISTEN) return (-ENOTCONN);
 839 
 840     /* will catch some errors. */
 841     if (sk->err)
 842       {
 843         int err;
 844         err = -sk->err;
 845         sk->err = 0;
 846         return (err);
 847       }
 848 
 849     /* urgent data needs to be handled specially. */
 850     if ((flags & MSG_OOB))
 851       return (tcp_read_urg (sk, to, len, flags));
 852 
 853     /* so no-one else will use this socket. */
 854     sk->inuse = 1;
 855     if (sk->rqueue != NULL)
 856       skb=sk->rqueue->next;
 857     else
 858       skb = NULL;
 859 
 860     while ( len > 0)
 861       {
 862          PRINTK("tcp_read (sk=%X, to=%X, len=%d, nonblock=%d, flags=%X)\n",
 863                 sk, to, len, nonblock, flags);
 864           while ( skb == NULL || before (sk->copied_seq+1, skb->h.th->seq) ||
 865                  skb->used) /* skb->used just checks to see if we've
 866                                gone all the way around. */
 867             {
 868 
 869                PRINTK("skb = %X:\n",skb);
 870                print_skb(skb);
 871                print_sk (sk);
 872 
 873                cleanup_rbuf(sk);
 874 
 875                 release_sock (sk); /* now we may have some data waiting. */
 876 
 877 
 878                PRINTK ("tcp_read about to sleep. state = %d\n",sk->state);
 879                cli();
 880 
 881                if (sk->state == TCP_CLOSE || sk->state == TCP_TIME_WAIT)
 882                    {
 883                      sti();
 884                      if (copied) return (copied);
 885                      if (sk->err) return (-sk->err);
 886                      if (!sk->done)
 887                        {
 888                           sk->done = 1;
 889                           return (0);
 890                        }
 891                      return (-ENOTCONN);
 892                   }
 893                         
 894 
 895                 if (nonblock || ((flags & MSG_PEEK) && copied))
 896                   {
 897                     sti();
 898                     release_sock (sk);
 899                     if (copied) return (copied);
 900                     return (-EAGAIN);
 901                   }
 902 
 903                 if ( sk->rqueue == NULL ||
 904                     before (sk->copied_seq+1, sk->rqueue->next->h.th->seq))
 905                   {
 906                      interruptible_sleep_on (sk->sleep);
 907                      if (current->signal & ~current->blocked)
 908                        {
 909                           sti ();
 910                           if (copied) return (copied);
 911                           return (-ERESTARTSYS);
 912                        }
 913                   }
 914                 sti();
 915                 PRINTK ("tcp_read woke up. \n");
 916 
 917                 sk->inuse = 1;
 918 
 919                 if (sk->rqueue != NULL)
 920                   skb=sk->rqueue->next;
 921                 else
 922                   skb = NULL;
 923 
 924             }
 925 
 926           /* Copy anything from the current block that needs to go
 927              into the user buffer. */
 928 
 929          offset = sk->copied_seq+1 - skb->h.th->seq;
 930 
 931          if (skb->h.th->syn) offset --;
 932          if (offset < skb->len )
 933            {
 934               /* if there is urgent data we must either return or skip
 935                  over it. */
 936               if (skb->h.th->urg)
 937                 {
 938                    if (skb->urg_used)
 939                      {
 940                         if (flags & MSG_PEEK) break;
 941                         sk->copied_seq += skb->h.th->urg_ptr;
 942                         offset += skb->h.th->urg_ptr;
 943                         if (offset > skb->len)
 944                           {
 945                              skb->used = 1;
 946                              skb=skb->next;
 947                              continue;
 948                           }
 949                      }
 950                    else
 951                      {
 952                         break;
 953                      }
 954                 }
 955               used = min(skb->len - offset, len);
 956 
 957               verify_area (to, used);
 958               memcpy_tofs(to, ((unsigned char *)skb->h.th) +
 959                           skb->h.th->doff*4 +
 960                           offset,
 961                           used);
 962               copied += used;
 963               len -= used;
 964               to += used;
 965               if (!(flags & MSG_PEEK))
 966                 sk->copied_seq += used;
 967               
 968               /* mark this data used if we are really reading it, and if
 969                  it doesn't contain any urgent data. And we have used all
 970                  the data. */
 971               if (!(flags & MSG_PEEK) &&
 972                   (!skb->h.th->urg || skb->urg_used) &&
 973                   (used + offset >= skb->len) )
 974                 skb->used = 1;
 975               
 976               /* see if this is the end of a message or if the remaining data
 977                  is urgent. */
 978               if ( skb->h.th->psh || skb->h.th->urg)
 979                 {
 980                    break;
 981                 }
 982            }
 983          else /* already used this data, must be a retransmit. */
 984            {
 985               skb->used = 1;
 986            }
 987          skb=skb->next;
 988       }
 989     cleanup_rbuf (sk);
 990     release_sock (sk);
 991     if (copied == 0 && nonblock) return (-EAGAIN);
 992     return (copied);
 993 }
 994 
 995 /* this routine will send a reset to the other tcp. */
 996 static  void
 997 tcp_reset(unsigned long saddr, unsigned long daddr, struct tcp_header *th,
     /* [previous][next][first][last][top][bottom][index][help] */
 998            struct proto *prot, struct options *opt, struct device *dev)
 999 {
1000   /* we need to grab some memory, and put together a reset, and then
1001      put it into the queue to be sent. */
1002   struct sk_buff *buff;
1003   struct tcp_header *t1;
1004   int tmp;
1005   buff=prot->wmalloc(NULL, MAX_RESET_SIZE,1);
1006   if (buff == NULL) return;
1007 
1008   PRINTK("tcp_reset buff = %X\n", buff);
1009   buff->mem_addr = buff;
1010   buff->mem_len = MAX_RESET_SIZE;
1011   buff->len = sizeof (*t1);
1012   buff->sk = NULL;
1013   buff->dev = dev;
1014 
1015   t1=(struct tcp_header *)(buff + 1);
1016   /* put in the ip_header and routing stuff. */
1017   tmp = prot->build_header (buff, saddr, daddr, &dev, IPPROTO_TCP, opt,
1018                             sizeof(struct tcp_header));
1019   if (tmp < 0)
1020     {
1021       prot->wfree (NULL,buff->mem_addr, buff->mem_len);
1022       return;
1023     }
1024   t1 = (struct tcp_header *)((char *)t1 +tmp);
1025   buff->len += tmp;
1026   memcpy (t1, th, sizeof (*t1));
1027   /* swap the send and the receive. */
1028   t1->dest = th->source;
1029   t1->source = th->dest;
1030   t1->seq = th->ack_seq; /* add one so it will be in
1031                             the right range.*/
1032   t1->rst = 1;
1033   t1->ack = 0;
1034   t1->syn = 0;
1035   t1->urg = 0;
1036   t1->fin = 0;
1037   t1->psh = 0;
1038   t1->doff = sizeof (*t1)/4;
1039   tcp_send_check (t1, saddr, daddr, sizeof (*t1), NULL);
1040   prot->queue_xmit(NULL, dev, buff, 1);
1041   
1042 }
1043 
1044 
1045 /* This routine handles a connection request.  This should make sure
1046    we haven't already responded. */
1047 /* Because of the way BSD works, we have to send a syn/ack now. This also
1048  means it will be harder to close a socket which is listening. */
1049 
1050 static  void
1051 tcp_conn_request(volatile struct sock *sk, struct sk_buff *skb,
     /* [previous][next][first][last][top][bottom][index][help] */
1052                  unsigned long daddr,
1053                  unsigned long saddr, struct options *opt, struct device *dev)
1054 {
1055   struct sk_buff *buff;
1056   struct tcp_header *t1;
1057   unsigned char *ptr;
1058   volatile struct sock *newsk;
1059   struct tcp_header *th;
1060   int tmp;
1061   th = skb->h.th;
1062 
1063   PRINTK ("tcp_conn_request (sk = %X, skb = %X, daddr = %X, sadd4= %X, \n"
1064           "                  opt = %X, dev = %X)\n",
1065           sk, skb, daddr, saddr, opt, dev);
1066   
1067   /* if the socket is dead, don't accept the connection. */
1068   if (!sk->dead)
1069     {
1070        wake_up(sk->sleep);
1071     }
1072   else
1073     {
1074        PRINTK ("tcp_conn_request on dead socket\n");
1075        tcp_reset (daddr, saddr, th, sk->prot, opt, dev);
1076        free_skb (skb, FREE_READ);
1077        return;
1078     }
1079 
1080 
1081   /* we need to build a new sock struct. */
1082   /* It is sort of bad to have a socket without an inode attached to
1083      it, but the wake_up's will just wake up the listening socket,
1084      and if the listening socket is destroyed before this is taken
1085      off of the queue, this will take care of it. */
1086 
1087   newsk = malloc(sizeof (struct sock));
1088   if (newsk == NULL) 
1089     {
1090        /* just ignore the syn.  It will get retransmitted. */
1091        free_skb (skb, FREE_READ);
1092        return;
1093     }
1094 
1095 
1096   PRINTK ("newsk = %X\n", newsk);
1097   memcpy ((void *)newsk, (void *)sk, sizeof (*newsk));
1098   newsk->wback = NULL;
1099   newsk->wfront = NULL;
1100   newsk->rqueue = NULL;
1101   newsk->send_head = NULL;
1102   newsk->send_tail = NULL;
1103   newsk->back_log = NULL;
1104   newsk->blog = 0;
1105   newsk->intr = 0;
1106   newsk->proc = 0;
1107   newsk->done = 0;
1108 
1109   newsk->pair = NULL;
1110   newsk->wmem_alloc = 0;
1111   newsk->rmem_alloc = 0;
1112 
1113   newsk->max_unacked = MAX_WINDOW - TCP_WINDOW_DIFF;
1114 
1115   newsk->err = 0;
1116   newsk->shutdown = 0;
1117   newsk->ack_backlog = 0;
1118   newsk->acked_seq = skb->h.th->seq+1;
1119   newsk->fin_seq = skb->h.th->seq;
1120   newsk->copied_seq = skb->h.th->seq;
1121   newsk->state = TCP_SYN_RECV;
1122   newsk->timeout = 0;
1123   newsk->send_seq = timer_seq*SEQ_TICK-seq_offset;
1124   newsk->rcv_ack_seq = newsk->send_seq;
1125   newsk->urg =0;
1126   newsk->retransmits = 0;
1127   newsk->destroy = 0;
1128   newsk->time_wait.sk = newsk;
1129   newsk->time_wait.next = NULL;
1130   newsk->dummy_th.source = skb->h.th->dest;
1131   newsk->dummy_th.dest = skb->h.th->source;
1132   /* swap these two, they are from our point of view. */
1133   newsk->daddr=saddr;
1134   newsk->saddr=daddr;
1135 
1136   put_sock (newsk->num,newsk);
1137   newsk->dummy_th.res1=0;
1138   newsk->dummy_th.doff=6;
1139   newsk->dummy_th.fin=0;
1140   newsk->dummy_th.syn=0;
1141   newsk->dummy_th.rst=0;
1142   newsk->dummy_th.psh=0;
1143   newsk->dummy_th.ack=0;
1144   newsk->dummy_th.urg=0;
1145   newsk->dummy_th.res2=0;
1146   newsk->acked_seq = skb->h.th->seq+1;
1147   newsk->copied_seq = skb->h.th->seq;
1148 
1149   if (skb->h.th->doff == 5)
1150     {
1151       newsk->mtu=576-HEADER_SIZE;
1152     }
1153   else
1154     {
1155       ptr = (unsigned char *)(skb+1);
1156       if (ptr[0] != 2 || ptr[1] != 4)
1157         {
1158            newsk->mtu=576-HEADER_SIZE;
1159         }
1160       else
1161         {
1162           newsk->mtu = min (ptr[2]*256+ptr[3]-HEADER_SIZE,
1163                             dev->mtu-HEADER_SIZE);
1164         }
1165     }
1166 
1167   print_sk (newsk);
1168   buff=newsk->prot->wmalloc(newsk,MAX_SYN_SIZE,1);
1169   if (buff == NULL)
1170     {
1171        sk->err = -ENOMEM;
1172        newsk->dead = 1;
1173        release_sock (newsk);
1174        free_skb (skb, FREE_READ);
1175        return;
1176     }
1177   
1178   buff->mem_addr = buff;
1179   buff->mem_len = MAX_SYN_SIZE;
1180   buff->len=sizeof (struct tcp_header)+4;
1181   buff->sk = newsk;
1182   
1183   t1=(struct tcp_header *)(buff + 1);
1184   /* put in the ip_header and routing stuff. */
1185 
1186   tmp = sk->prot->build_header (buff, newsk->saddr, newsk->daddr, &dev,
1187                                 IPPROTO_TCP, NULL, MAX_SYN_SIZE);
1188 
1189   /* something went wrong. */
1190   if (tmp < 0)
1191     {
1192        sk->err = tmp;
1193        sk->prot->wfree(newsk, buff->mem_addr, buff->mem_len);
1194        newsk->dead = 1;
1195        release_sock (newsk);
1196        skb->sk = sk;
1197        free_skb (skb, FREE_READ);
1198        return;
1199     }
1200 
1201   buff->len += tmp;
1202   t1 = (struct tcp_header *)((char *)t1 +tmp);
1203   
1204   memcpy (t1, skb->h.th, sizeof (*t1));
1205   buff->h.seq = newsk->send_seq;
1206   /* swap the send and the receive. */
1207   t1->dest = skb->h.th->source;
1208   t1->source = newsk->dummy_th.source;
1209   t1->seq = net32(newsk->send_seq++);
1210   t1->ack = 1;
1211   newsk->window = sk->prot->rspace(newsk);
1212   t1->window = net16(newsk->window);
1213   t1->res1=0;
1214   t1->res2=0;
1215   t1->rst = 0;
1216   t1->urg = 0;
1217   t1->psh = 0;
1218   t1->syn = 1;
1219   t1->ack_seq = net32(skb->h.th->seq+1);
1220   t1->doff = sizeof (*t1)/4+1;
1221 
1222   ptr = (unsigned char *)(t1+1);
1223   ptr[0]=2;
1224   ptr[1]=4;
1225   ptr[2]=((dev->mtu - HEADER_SIZE) >> 8) & 0xff;
1226   ptr[3]=(dev->mtu - HEADER_SIZE) & 0xff;
1227 
1228   tcp_send_check (t1, daddr, saddr, sizeof (*t1)+4, newsk);
1229   newsk->prot->queue_xmit(newsk, dev, buff, 0);
1230 
1231   newsk->time_wait.len = TCP_CONNECT_TIME;
1232   PRINTK ("newsk->time_wait.sk = %X\n", newsk->time_wait.sk);
1233   reset_timer ((struct timer *)&newsk->time_wait);
1234   skb->sk = newsk;
1235   /* charge the sock_buff to newsk. */
1236   sk->rmem_alloc -= skb->mem_len;
1237   newsk->rmem_alloc += skb->mem_len;
1238 
1239   if (sk->rqueue == NULL)
1240     {
1241       skb->next = skb;
1242       skb->prev = skb;
1243       sk->rqueue = skb;
1244     }
1245   else
1246     {
1247       skb->next = sk->rqueue;
1248       skb->prev = sk->rqueue->prev;
1249       sk->rqueue->prev = skb;
1250       skb->prev->next = skb;
1251     }
1252   release_sock (newsk);
1253 }
1254 
1255 static  void
1256 tcp_close (volatile struct sock *sk, int timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
1257 {
1258   /* we need to grab some memory, and put together a fin, and then
1259      put it into the queue to be sent. */
1260   struct sk_buff *buff;
1261   int need_reset = 0;
1262   struct tcp_header *t1,*th;
1263   struct proto *prot;
1264   struct device *dev=NULL;
1265   int tmp;
1266   PRINTK ("tcp_close ((struct sock *)%X, %d)\n",sk, timeout);
1267   sk->inuse = 1;
1268   sk->keepopen = 0;
1269   sk->shutdown = SHUTDOWN_MASK;
1270 
1271   if (!sk->dead)
1272     wake_up (sk->sleep);
1273 
1274 
1275   /* we need to flush the recv. buffs. */
1276   
1277   if (sk->rqueue != NULL)
1278     {
1279        struct sk_buff *skb;
1280        struct sk_buff *skb2;
1281        skb = sk->rqueue;
1282        do {
1283           skb2=skb->next;
1284           free_skb (skb, FREE_READ);
1285           skb=skb2;
1286        } while (skb != sk->rqueue);
1287        need_reset = 1;
1288     }
1289   sk->rqueue = NULL;
1290 
1291 
1292   switch (sk->state)
1293     {
1294       case TCP_FIN_WAIT1:
1295       case TCP_FIN_WAIT2:
1296       case TCP_LAST_ACK:
1297        if (timeout)
1298          tcp_time_wait(sk);
1299        release_sock (sk);
1300        if (!need_reset)
1301          return;
1302        break;
1303 
1304       case TCP_TIME_WAIT:
1305        if (timeout)
1306          sk->state = TCP_CLOSE;
1307        release_sock (sk);
1308        return;
1309 
1310       case TCP_LISTEN:
1311        sk->state = TCP_CLOSE;
1312        release_sock(sk);
1313        return;
1314 
1315       case TCP_CLOSE:
1316 
1317        release_sock(sk);
1318        return;
1319        
1320 
1321     case TCP_ESTABLISHED:
1322     case TCP_SYN_SENT:
1323     case TCP_SYN_RECV:
1324 
1325       prot = (struct proto *)sk->prot;
1326       th=(struct tcp_header *)&sk->dummy_th;
1327 
1328        buff=prot->wmalloc(sk, MAX_FIN_SIZE,1);
1329        if (buff == NULL)
1330          {
1331             /* this will force it to try again later. */
1332             sk->state = TCP_ESTABLISHED;
1333             sk->timeout = TIME_CLOSE;
1334             sk->time_wait.len = 100; /* wait a second. */
1335             reset_timer ((struct timer *)&sk->time_wait);
1336             return;
1337          }
1338 
1339       buff->mem_addr = buff;
1340       buff->mem_len = MAX_FIN_SIZE;
1341       buff->sk = sk;
1342       buff->len = sizeof (*t1);
1343       t1=(struct tcp_header *)(buff + 1);
1344       /* put in the ip_header and routing stuff. */
1345       tmp = prot->build_header (buff,sk->saddr, sk->daddr, &dev,
1346                                 IPPROTO_TCP, sk->opt,
1347                                 sizeof(struct tcp_header));
1348       if (tmp < 0)
1349         {
1350           prot->wfree (sk,buff->mem_addr, buff->mem_len);
1351           PRINTK ("Unable to build header for fin.\n");
1352           release_sock(sk);
1353           return;
1354         }
1355       t1 = (struct tcp_header *)((char *)t1 +tmp);
1356       buff ->len += tmp;
1357       buff->dev = dev;
1358       memcpy (t1, th, sizeof (*t1));
1359       t1->seq = net32(sk->send_seq);
1360       sk->send_seq++;
1361       buff->h.seq = sk->send_seq;
1362       t1->ack = 1;
1363        /* ack everything immediately from now on. */
1364       sk->delay_acks = 0;
1365       t1->ack_seq = net32(sk->acked_seq);
1366       t1->window = net16(sk->prot->rspace(sk));
1367       t1->fin = 1;
1368       t1->rst = need_reset;
1369       t1->doff = sizeof (*t1)/4;
1370       tcp_send_check (t1, sk->saddr, sk->daddr, sizeof (*t1), sk);
1371 
1372       if (sk->wfront == NULL)
1373         {
1374           prot->queue_xmit(sk, dev, buff, 0);
1375         }
1376       else
1377         {
1378            sk->time_wait.len = sk->rtt;
1379            sk->timeout = TIME_WRITE;
1380            reset_timer ((struct timer *)&sk->time_wait);
1381            buff->next = NULL;
1382            if (sk->wback == NULL)
1383              {
1384                 sk->wfront=buff;
1385              }
1386            else
1387              {
1388                 sk->wback->next = buff;
1389              }
1390            sk->wback = buff;
1391            
1392         }
1393       sk->state = TCP_FIN_WAIT1;
1394     }
1395   release_sock (sk);
1396 }
1397 
1398 
1399 /* This routine takes stuff off of the write queue, and puts it in the
1400    xmit queue. */
1401 static  void
1402 tcp_write_xmit (volatile struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
1403 {
1404   struct sk_buff *skb;
1405   while (sk->wfront != NULL && before (sk->wfront->h.seq, sk->window_seq) &&
1406          sk->packets_out < sk->cong_window)
1407     {
1408       skb = sk->wfront;
1409       sk->wfront = skb->next;
1410       if (sk->wfront == NULL)
1411         sk->wback = NULL;
1412       sk->prot->queue_xmit (sk, skb->dev, skb, skb->free);
1413     }
1414 }
1415 
1416 
1417 
1418 /* This routine deals with incoming acks, but not outgoing ones. */
1419 
1420 static  int
1421 tcp_ack (volatile struct sock *sk, struct tcp_header *th, unsigned long saddr)
     /* [previous][next][first][last][top][bottom][index][help] */
1422 {
1423   unsigned long ack;
1424   ack = net32(th->ack_seq);
1425 
1426   if (!between (ack , sk->rcv_ack_seq, sk->send_seq)) 
1427     {
1428       if (after (ack, sk->send_seq) || sk->state != TCP_ESTABLISHED) 
1429         {
1430           return (0);
1431         }
1432       if (sk->keepopen)
1433         reset_timer ((struct timer *)&sk->time_wait);
1434       sk->retransmits = 0;
1435       return (1);
1436     }
1437 
1438   sk->window_seq = ack + net16(th->window);
1439 
1440 
1441   /* we don't want too many packets out there. */
1442   if (sk->cong_window < 2048 && ack != sk->rcv_ack_seq)
1443     {
1444        if (sk->exp_growth)
1445          sk->cong_window *= 2;
1446        else
1447          sk->cong_window++;
1448     }
1449 
1450   sk->rcv_ack_seq = ack;
1451 
1452   /* see if we can take anything off of the retransmit queue. */
1453   while (sk->send_head != NULL)
1454     {
1455       if (before (sk->send_head->h.seq, ack+1))
1456         {
1457           struct sk_buff *oskb;
1458           /* we have one less packet out there. */
1459           sk->packets_out --;
1460           cli();
1461           oskb = sk->send_head;
1462           /* estimate the rtt. */
1463           sk->rtt += ((jiffies - oskb->when) - sk->rtt)/2;
1464           if (sk->rtt < 30) sk->rtt = 30;
1465           sk->send_head = oskb->link3;
1466           if (sk->send_head == NULL) 
1467             {
1468               sk->send_tail = NULL;
1469             }
1470           /* we may need to remove this from the dev send list. */
1471           if (oskb->next != NULL)
1472             {
1473                if (oskb->next != oskb)
1474                  {
1475                     oskb->next->prev = oskb->prev;
1476                     oskb->prev->next = oskb->next;
1477                  }
1478                else
1479                  {
1480                     int i;
1481                     for (i = 0; i < DEV_NUMBUFFS; i++)
1482                       {
1483                          if (oskb->dev->buffs[i] = oskb)
1484                            {
1485                               oskb->dev->buffs[i]= NULL;
1486                               break;
1487                            }
1488                       }
1489                  }
1490             }
1491           free_skb  (oskb, FREE_WRITE); /* write. */
1492           sti();
1493           if (!sk->dead)
1494             wake_up(sk->sleep);
1495         }
1496       else
1497         {
1498           break;
1499         }
1500 
1501     }
1502 
1503 
1504   /* at this point we need to check to see if we have anything
1505      which needs to be retransmiteed.  If we have failed to get
1506      some acks i.e. had to retransmit something, and we succeded, we
1507      should then attempt to retransmit everything right now. */
1508 
1509   if (sk->retransmits && sk->send_head != NULL)
1510     {
1511       sk->prot->retransmit (sk,1);
1512     }
1513   sk->retransmits = 0;
1514 
1515   /* maybe we can take some stuff off of the write queue, and put it onto
1516      the xmit queue. */
1517   if (sk->wfront != NULL && sk->packets_out < sk->cong_window)
1518     {
1519       if (after (sk->window_seq, sk->wfront->h.seq))
1520         {
1521           tcp_write_xmit (sk);
1522         }
1523     }
1524   else
1525     {
1526        if (sk->send_head == NULL && sk->ack_backlog == 0 &&
1527            sk->state != TCP_TIME_WAIT)
1528          {
1529             delete_timer((struct timer *)&sk->time_wait);
1530             sk->timeout = 0;
1531          }
1532        else
1533          {
1534             if (sk->state == TCP_TIME_WAIT)
1535               {
1536                  sk->time_wait.len = TCP_TIMEWAIT_LEN;
1537                  sk->timeout = TIME_CLOSE;
1538               }
1539             reset_timer ((struct timer *)&sk->time_wait);
1540          }
1541     }
1542 
1543   /* see if we are done. */
1544   if ( sk->state == TCP_TIME_WAIT)
1545     {
1546        if (sk->rcv_ack_seq == sk->send_seq &&    
1547            sk->acked_seq == sk->fin_seq);
1548        if (!sk->dead) wake_up (sk->sleep);
1549        sk->state = TCP_CLOSE;
1550     }
1551 
1552   if (sk->state == TCP_FIN_WAIT1)
1553     {
1554       if (sk->rcv_ack_seq == sk->send_seq)
1555         sk->state = TCP_FIN_WAIT2;
1556     }
1557   
1558   if (sk->state == TCP_LAST_ACK)
1559     {
1560       if (sk->rcv_ack_seq == sk->send_seq)
1561         {
1562            if (sk->acked_seq != sk->fin_seq)
1563              {
1564                 tcp_time_wait(sk);
1565              }
1566            else
1567              {
1568                 sk->state = TCP_CLOSE;
1569              }
1570         }
1571       if (!sk->dead) wake_up (sk->sleep);
1572     }
1573 
1574   return (1);
1575 }
1576 
1577 /* This routine handles the data.  If there is room in the buffer, it
1578    will be have already been moved into it.  If there is no room,
1579    then we will just have to discard the packet. */
1580 
1581 static  int
1582 tcp_data (struct sk_buff *skb, volatile struct sock *sk, 
     /* [previous][next][first][last][top][bottom][index][help] */
1583           unsigned long saddr, unsigned short len)
1584 {
1585   struct sk_buff *skb1, *skb2;
1586   struct tcp_header *th;
1587 
1588   th = skb->h.th;
1589   print_th (th);
1590   skb->len = len - (th->doff*4);
1591 
1592   PRINTK("tcp_data len = %d sk = %X:\n",skb->len, sk);
1593   print_sk(sk);
1594 
1595   sk->bytes_rcv += skb->len;
1596 
1597   if (skb->len == 0 && !th->fin && !th->urg && !th->psh)
1598     {
1599       /* don't want to keep passing ack's back and fourth. */
1600       if (!th->ack)
1601         tcp_send_ack (sk->send_seq, sk->acked_seq,sk, th, saddr);
1602       free_skb(skb, FREE_READ);
1603       return (0);
1604     }
1605 
1606   if (sk->shutdown & RCV_SHUTDOWN)
1607     {
1608        /* just ack everything. */
1609        sk->acked_seq = th->seq + skb->len + th->syn + th->fin;
1610        tcp_send_ack (sk->send_seq, sk->acked_seq, sk, skb->h.th, saddr);
1611        free_skb (skb, FREE_READ);
1612        if (sk->state == TCP_TIME_WAIT && sk->acked_seq == sk->fin_seq)
1613          {
1614             if (!sk->dead) wake_up (sk->sleep);
1615             sk->state = TCP_CLOSE;
1616          }
1617        return (0);
1618     }
1619 
1620   /* now we have to walk the chain, and figure out where this one
1621      goes into it.  This is set up so that the last packet we received
1622      will be the first one we look at, that way if everything comes
1623      in order, there will be no performance loss, and if they come
1624      out of order we will be able to fit things in nicely. */
1625   
1626   if (sk->rqueue == NULL)
1627     {
1628        PRINTK ("tcp_data: skb = %X:\n",skb);
1629        print_skb (skb);
1630 
1631        sk->rqueue = skb;
1632        skb->next = skb;
1633        skb->prev = skb;
1634        skb1= NULL;
1635     }
1636   else
1637     {
1638       PRINTK ("tcp_data adding to chain sk = %X:\n",sk);
1639       print_sk (sk);
1640 
1641       for (skb1=sk->rqueue; ; skb1=skb1->prev)
1642         {
1643           PRINTK ("skb1=%X\n",skb1);
1644           print_skb(skb1);
1645           PRINTK ("skb1->h.th->seq = %d\n", skb1->h.th->seq);
1646           if (after ( th->seq+1, skb1->h.th->seq))
1647             {
1648               skb->prev = skb1;
1649               skb->next = skb1->next;
1650               skb->next->prev = skb;
1651               skb1->next = skb;
1652               if (skb1 == sk->rqueue)
1653                 sk->rqueue = skb;
1654               break;
1655             }
1656           if  ( skb1->prev == sk->rqueue)
1657             {
1658                skb->next= skb1;
1659                skb->prev = skb1->prev;
1660                skb->prev->next = skb;
1661                skb1->prev = skb;
1662                skb1 = NULL; /* so we know we might be able to ack stuff. */
1663                break;
1664             }
1665         }
1666 
1667       PRINTK ("skb = %X:\n",skb);
1668       print_skb (skb);
1669       PRINTK ("sk now equals:\n");
1670       print_sk (sk);
1671 
1672     }
1673 
1674   th->ack_seq = th->seq + skb->len;
1675   if (th->syn) th->ack_seq ++;
1676   if (th->fin) th->ack_seq ++;
1677 
1678   if (before (sk->acked_seq, sk->copied_seq))
1679     {
1680        printk ("*** tcp.c:tcp_data bug acked < copied\n");
1681        sk->acked_seq = sk->copied_seq;
1682     }
1683 
1684   /* now figure out if we can ack anything. */
1685   if (skb1 == NULL || skb1->acked || before (th->seq, sk->acked_seq+1))
1686     {
1687       if (before (th->seq, sk->acked_seq+1))
1688         {
1689           sk->acked_seq = th->ack_seq;
1690           skb->acked = 1;
1691           
1692           for (skb2=skb->next; skb2 != sk->rqueue->next; skb2=skb2->next)
1693             {
1694                if (before(skb2->h.th->seq, sk->acked_seq+1))
1695                  {
1696                     sk->acked_seq = skb2->h.th->ack_seq;
1697                     skb2->acked = 1;
1698                     /* force an immediate ack. */
1699                     sk->ack_backlog = sk->max_ack_backlog;
1700                  }
1701                else
1702                  break;
1703             }
1704 
1705           /* this also takes care of updating the window. */
1706           /* this if statement needs to be simplified. */
1707 
1708           if (!sk->delay_acks || 
1709               sk->ack_backlog >= sk->max_ack_backlog || 
1710               sk->window < 2*sk->mtu + sk->bytes_rcv ||
1711               sk->bytes_rcv > sk->max_unacked || 
1712               th->fin)
1713             {
1714                 tcp_send_ack (sk->send_seq, sk->acked_seq,sk,th, saddr);
1715             }
1716           else
1717             {
1718                sk->ack_backlog++;
1719                sk->time_wait.len = TCP_ACK_TIME;
1720                sk->timeout = TIME_WRITE;
1721                reset_timer ((struct timer *)&sk->time_wait);
1722             }
1723        }
1724    }
1725   else
1726     {
1727        /* we missed a packet.  Send an ack to try to resync things. */
1728        tcp_send_ack (sk->send_seq, sk->acked_seq, sk, th, saddr);
1729     }
1730 
1731   /* now tell the user we may have some data. */
1732   if (!sk->dead)
1733     {
1734        wake_up (sk->sleep);
1735     }
1736   else
1737     {
1738        PRINTK ("data received on dead socket. \n");
1739     }
1740 
1741   if (sk->state > TCP_CLOSING && sk->acked_seq == sk->fin_seq)
1742     {
1743        sk->state = TCP_CLOSE;
1744     }
1745 
1746   return (0);
1747 }
1748 
1749 static  int
1750 tcp_urg (volatile struct sock *sk, struct tcp_header *th, unsigned long saddr)
     /* [previous][next][first][last][top][bottom][index][help] */
1751 {
1752     extern int kill_pg (int pg, int sig, int priv);
1753     extern int kill_proc (int pid, int sig, int priv);
1754     
1755     if (!sk->dead)
1756       wake_up(sk->sleep);
1757     
1758     if (sk->urginline)
1759       {
1760           th->urg = 0;
1761           th->psh = 1;
1762           return (0);
1763       }
1764 
1765     sk->urg++;
1766 
1767     if (!sk->urg)
1768       {
1769           /* so if we get more urgent data, we don't 
1770              signal the user again. */
1771           if (sk->proc == 0) return (0);
1772           if (sk->proc > 0)
1773             {
1774                 kill_proc (sk->proc, SIGURG, 1);
1775             }
1776           else 
1777             {
1778                 kill_pg (-sk->proc, SIGURG, 1);
1779             }
1780       }
1781     return (0);
1782 }
1783 
1784 /* this deals with incoming fins. */
1785 static  int
1786 tcp_fin (volatile struct sock *sk, struct tcp_header *th, 
     /* [previous][next][first][last][top][bottom][index][help] */
1787          unsigned long saddr, struct device *dev)
1788 {
1789   struct sk_buff *buff;
1790   struct tcp_header *t1;
1791   int tmp;
1792   PRINTK ("tcp_fin (sk=%X, th=%X, saddr=%X, dev=%X)\n",
1793           sk, th, saddr, dev);
1794   
1795   if (!sk->dead)
1796     {
1797       wake_up (sk->sleep);
1798     }
1799 
1800   /* after sending the fin, we aren't allowed to write anymore. */
1801   sk->shutdown |= SEND_SHUTDOWN;
1802 
1803   sk->err = 0;
1804   switch (sk->state)
1805     {
1806     case TCP_SYN_RECV:
1807     case TCP_SYN_SENT:
1808     case TCP_ESTABLISHED:
1809       sk->state = TCP_LAST_ACK;
1810       break;
1811 
1812      default:
1813     case TCP_FIN_WAIT1:
1814     case TCP_TIME_WAIT:
1815       sk->state = TCP_LAST_ACK;
1816       /* start the timers. */
1817       sk->time_wait.len = TCP_TIMEWAIT_LEN;
1818       sk->timeout = TIME_CLOSE;
1819       reset_timer ((struct timer *)&sk->time_wait);
1820       return (0);
1821 
1822     case TCP_FIN_WAIT2:
1823       sk->state = TCP_CLOSE;
1824       return (0);
1825     }
1826 
1827   /* send an ack and our own fin. */
1828   buff=sk->prot->wmalloc(sk,MAX_ACK_SIZE,1);
1829   if (buff == NULL)
1830     {
1831        /* we will ignore the fin.  That way it will be sent again. */
1832        return (1);
1833     }
1834 
1835   buff->mem_addr = buff;
1836   buff->mem_len = MAX_ACK_SIZE;
1837   buff->len=sizeof (struct tcp_header);
1838   buff->sk = sk;
1839 
1840   t1 = (struct tcp_header *)(buff + 1);
1841   /* put in the ip_header and routing stuff. */
1842   tmp = sk->prot->build_header (buff, sk->saddr, sk->daddr, &dev,
1843                                 IPPROTO_TCP,  sk->opt, MAX_ACK_SIZE);
1844   if (tmp < 0)
1845     {
1846       sk->prot->wfree(sk, buff->mem_addr, buff->mem_len);
1847       return (0);
1848     }
1849 
1850   buff->len += tmp;
1851   t1 = (struct tcp_header *)((char *)t1 +tmp);
1852   
1853   memcpy (t1, th, sizeof (*t1));
1854 
1855   /* swap the send and the receive. */
1856   t1->dest = th->source;
1857   t1->source = th->dest;
1858 
1859 
1860   t1->seq = net32(sk->send_seq++);
1861 
1862   /* contains the one that needs to be acked. */
1863   sk->fin_seq = th->seq+1;
1864 
1865   buff->h.seq = sk->send_seq;
1866   t1->window = net16(sk->prot->rspace(sk));
1867 
1868   t1->res1=0;
1869   t1->res2=0;
1870   t1->rst = 0;
1871   t1->urg = 0;
1872   t1->syn = 0;
1873   t1->psh = 0;
1874   t1->ack = 1; 
1875   t1->fin = 1;
1876   t1->ack_seq = net32(sk->acked_seq);
1877 
1878   t1->doff = sizeof (*t1)/4;
1879   tcp_send_check (t1, sk->saddr, sk->daddr, sizeof (*t1), sk);
1880 
1881   /* can't just queue this up.  It should go at the end of
1882      the write queue. */
1883   if (sk->wback != NULL)
1884     {
1885       buff->next = NULL;
1886       sk->wback->next = buff;
1887       sk->wback = buff;
1888     }
1889   else
1890     {
1891       sk->prot->queue_xmit (sk, dev, buff,0);
1892     }
1893 
1894   return (0);
1895 }
1896 
1897 
1898 /* this will accept the next outstanding connection. */
1899 
1900 static volatile struct sock *
1901 tcp_accept (volatile struct sock *sk, int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
1902 {
1903   volatile struct sock *newsk;
1904   struct sk_buff *skb;
1905   
1906   PRINTK ("tcp_accept(sk=%X, flags=%X)\n", sk, flags);
1907   print_sk(sk);
1908   /* we need to make sure that this socket is listening, and that
1909      it has something pending. */
1910   
1911   if (sk->state != TCP_LISTEN)
1912     {
1913       sk->err = EINVAL;
1914       return (NULL); 
1915     }
1916   /* avoid the race. */
1917 
1918   sk->inuse = 1;
1919   cli();
1920   while ( (skb = get_firstr(sk)) == NULL )
1921     {
1922       if (flags & O_NONBLOCK)
1923         {
1924           sti();
1925           release_sock (sk);
1926           sk->err = EAGAIN;
1927           return (NULL);
1928         }
1929 
1930       release_sock (sk);
1931       interruptible_sleep_on (sk->sleep);
1932       if (current->signal & ~current->blocked)
1933         {
1934            sti();
1935            sk->err = ERESTARTSYS;
1936            return (NULL);
1937         }
1938 
1939       sk->inuse = 1;
1940     }
1941   sti();
1942 
1943   /* now all we need to do is return skb->sk. */
1944   newsk = skb->sk;
1945   free_skb (skb, FREE_READ);
1946   release_sock (sk);
1947   return (newsk);
1948 }
1949 
1950 
1951 
1952 /* this will initiate an outgoing connection. */
1953 static int
1954 tcp_connect (volatile struct sock *sk, struct sockaddr_in *usin, int addr_len)
     /* [previous][next][first][last][top][bottom][index][help] */
1955 {
1956   struct sk_buff *buff;
1957   struct sockaddr_in sin;
1958   struct device *dev=NULL;
1959   unsigned char *ptr;
1960   int tmp;
1961   struct tcp_header *t1;
1962   if (sk->state != TCP_CLOSE) return (-EISCONN);
1963   if (addr_len < 8) return (-EINVAL);
1964 
1965   verify_area (usin, addr_len);
1966   memcpy_fromfs (&sin,usin, min(sizeof (sin), addr_len));
1967 
1968   if (sin.sin_family && sin.sin_family != AF_INET) return (-EAFNOSUPPORT);
1969 
1970   sk->daddr = sin.sin_addr.s_addr;
1971   sk->send_seq = timer_seq*SEQ_TICK-seq_offset;
1972   sk->rcv_ack_seq = sk->send_seq -1;
1973   sk->err = 0;
1974   sk->dummy_th.dest = sin.sin_port;
1975 
1976   buff=sk->prot->wmalloc(sk,MAX_SYN_SIZE,0);
1977   if (buff == NULL) 
1978     {
1979       return (-ENOMEM);
1980     }
1981   sk->inuse = 1;
1982   buff->mem_addr = buff;
1983   buff->mem_len = MAX_SYN_SIZE;
1984   buff->len=24;
1985   buff->sk = sk;
1986   t1=(struct tcp_header *)(buff + 1);
1987   /* put in the ip_header and routing stuff. */
1988   /* We need to build the routing stuff fromt the things saved
1989      in skb. */
1990   tmp = sk->prot->build_header (buff, sk->saddr, sk->daddr, &dev,
1991                                 IPPROTO_TCP, NULL, MAX_SYN_SIZE);
1992   if (tmp < 0)
1993     {
1994       sk->prot->wfree(sk, buff->mem_addr, buff->mem_len);
1995       release_sock (sk);
1996       return (-ENETUNREACH);
1997     }
1998   buff->len += tmp;
1999   t1 = (struct tcp_header *)((char *)t1 +tmp);
2000 
2001   memcpy (t1, (void *)&(sk->dummy_th), sizeof (*t1));
2002   t1->seq = net32(sk->send_seq++);
2003   buff->h.seq = sk->send_seq;
2004   t1->ack = 0;
2005   t1->window = 2;
2006   t1->res1=0;
2007   t1->res2=0;
2008   t1->rst = 0;
2009   t1->urg = 0;
2010   t1->psh = 0;
2011   t1->syn = 1;
2012   t1->urg_ptr = 0;
2013   t1->doff =6;
2014   /* put in the tcp options to say mtu. */
2015   ptr=(unsigned char *)(t1+1);
2016   ptr[0]=2;
2017   ptr[1]=4;
2018   ptr[2]=(dev->mtu- HEADER_SIZE) >> 8;
2019   ptr[3]=(dev->mtu- HEADER_SIZE) & 0xff;
2020   sk->mtu = dev->mtu - HEADER_SIZE;
2021   tcp_send_check (t1, sk->saddr, sk->daddr,
2022                   sizeof (struct tcp_header) + 4, sk);
2023   /* this must go first otherwise a really quick response will
2024      get reset. */
2025   sk->state = TCP_SYN_SENT;
2026 
2027   sk->prot->queue_xmit(sk, dev, buff, 0);
2028   
2029   sk->time_wait.len = TCP_CONNECT_TIME;
2030   reset_timer ((struct timer *)&sk->time_wait);
2031   sk->retransmits = TCP_RETR1 - TCP_SYN_RETRIES;
2032   release_sock (sk);
2033   return (0);
2034 }
2035 
2036 
2037 /* this functions checks to see if the tcp header is actually
2038    acceptible. */
2039 
2040 static  int
2041 tcp_sequence (volatile struct sock *sk, struct tcp_header *th, short len,
     /* [previous][next][first][last][top][bottom][index][help] */
2042               struct options *opt, unsigned long saddr)
2043 {
2044    /* this isn't quite right.  sk->acked_seq could be more recent
2045       than sk->window.  This is however close enough.  We will accept
2046       slightly more packets than we should, but it should not cause
2047       problems unless someone is trying to forge packets. */
2048 
2049   if (between(th->seq, sk->acked_seq, sk->acked_seq + sk->window)||
2050       between(th->seq + len-sizeof (*th), sk->acked_seq+1, 
2051               sk->acked_seq + sk->window))
2052     {
2053        return (1);
2054     }
2055 
2056   /* if it's too far ahead, send an ack to let the other end
2057      know what we expect. */
2058   if (after (th->seq, sk->acked_seq + sk->window))
2059     {
2060        tcp_send_ack (sk->send_seq, sk->acked_seq, sk, th, saddr);
2061        return (0);
2062     }
2063 
2064   if (!th->rst)
2065     {
2066        if (len != th->doff*4 || th->fin || th->syn)
2067          {
2068             sk->delay_acks = 0;
2069          }
2070 
2071        /* try to resync things. */
2072        tcp_send_ack (net32(th->ack_seq), sk->acked_seq, sk, th, saddr);
2073     }
2074 
2075   /* in case it's just a late ack, let it through */
2076   if (th->ack && len == th->doff*4 && after (th->seq, sk->acked_seq - 4096) &&
2077       !th->fin && !th->syn) return (1);
2078 
2079   return (0);
2080 }
2081 
2082 /* This deals with the tcp option.  It isn't very general yet. */
2083 static void
2084 tcp_options (volatile struct sock *sk, struct tcp_header *th)
     /* [previous][next][first][last][top][bottom][index][help] */
2085 {
2086   unsigned char *ptr;
2087   ptr = (unsigned char *)(th + 1);
2088   if (ptr[0] != 2 || ptr[1] != 4)
2089     {
2090        sk->mtu = min (sk->mtu, 576-HEADER_SIZE);
2091        return;
2092     }
2093   sk->mtu = min (sk->mtu, ptr[2]*256 + ptr[3] - HEADER_SIZE);
2094 }
2095 
2096 int
2097 tcp_rcv(struct sk_buff *skb, struct device *dev, struct options *opt,
     /* [previous][next][first][last][top][bottom][index][help] */
2098         unsigned long daddr, unsigned short len,
2099         unsigned long saddr, int redo, struct ip_protocol * protocol)
2100 {
2101   struct tcp_header *th;
2102   volatile struct sock *sk;
2103 
2104   if (!skb)
2105     {
2106       printk ("tcp.c: tcp_rcv skb = NULL\n");
2107       return (0);
2108     }
2109 #if 0 /* it's ok for protocol to be NULL */
2110   if (!protocol)
2111     {
2112       printk ("tcp.c: tcp_rcv protocol = NULL\n");
2113       return (0);
2114     }
2115 
2116   if (!opt) /* it's ok for opt to be NULL */
2117     {
2118       printk ("tcp.c: tcp_rcv opt = NULL\n");
2119     }
2120 #endif
2121   if (!dev)
2122     {
2123       printk ("tcp.c: tcp_rcv dev = NULL\n");
2124       return (0);
2125     }
2126 
2127   th = skb->h.th;
2128 
2129   /* find the socket. */
2130   sk=get_sock(&tcp_prot, net16(th->dest), saddr, th->source, daddr);
2131   PRINTK("<<\n");
2132   PRINTK("len = %d, redo = %d, skb=%X\n", len, redo, skb);
2133 
2134   if (sk)
2135     {
2136       PRINTK ("sk = %X:\n",sk);
2137       print_sk (sk);
2138     }
2139 
2140   if (!redo)
2141     {
2142        if (th->check && tcp_check (th, len, saddr, daddr ))
2143          {
2144             skb->sk = NULL;
2145             free_skb (skb, 0);
2146             /* we don't release the socket because it was never
2147                marked in use. */
2148             return (0);
2149          }
2150 
2151        /*See if we know about the socket. */
2152        if (sk == NULL)
2153         {
2154           if (!th->rst)
2155             tcp_reset (daddr, saddr, th, &tcp_prot, opt,dev);
2156           skb->sk = NULL;
2157           free_skb (skb, 0);
2158           return (0);
2159         }
2160 
2161        skb->len = len;
2162        skb->sk = sk;
2163        skb->acked = 0;
2164        skb->used = 0;
2165        skb->free = 0;
2166        skb->urg_used = 0;
2167        skb->saddr = daddr;
2168        skb->daddr = saddr;
2169 
2170        th->seq = net32(th->seq);
2171 
2172        cli();
2173 
2174        /* we may need to add it to the backlog here. */
2175        if (sk->inuse)
2176          {
2177             if (sk->back_log == NULL)
2178               {
2179                  sk->back_log = skb;
2180                  skb->next = skb;
2181                  skb->prev = skb;
2182               }
2183             else
2184               {
2185                  skb->next = sk->back_log;
2186                  skb->prev = sk->back_log->prev;
2187                  skb->prev->next = skb;
2188                  skb->next->prev = skb;
2189               }
2190             sti();
2191             return (0);
2192          }
2193        sk->inuse = 1;
2194        sti();
2195      }
2196   else
2197     {
2198       if (!sk)
2199         {
2200           printk ("tcp.c: tcp_rcv bug sk=NULL redo = 1\n");
2201           return (0);
2202         }
2203     }
2204 
2205   if (!sk->prot)
2206     {
2207       printk ("tcp.c: tcp_rcv sk->prot = NULL \n");
2208       return (0);
2209     }
2210 
2211   /* charge the memory to the socket. */
2212   if (sk->rmem_alloc + skb->mem_len >= SK_RMEM_MAX)
2213     {
2214        skb->sk = NULL;
2215        free_skb (skb, 0);
2216        release_sock (sk);
2217        return (0);
2218     }
2219        
2220   sk->rmem_alloc += skb->mem_len;
2221 
2222   PRINTK ("About to do switch. \n");
2223 
2224   /* now deal with it. */
2225 
2226   switch (sk->state)
2227     {
2228        /* this should close the system down if it's waiting for an
2229           ack that is never going to be sent. */
2230     case TCP_LAST_ACK:
2231       if (th->rst)
2232         {
2233           sk->err = ECONNRESET;
2234           sk->state = TCP_CLOSE;
2235           if (!sk->dead)
2236             {
2237               wake_up (sk->sleep);
2238             }
2239           free_skb (skb, FREE_READ);
2240           release_sock(sk);
2241           return (0);
2242         }
2243 
2244     case TCP_ESTABLISHED:
2245     case TCP_FIN_WAIT1:
2246     case TCP_FIN_WAIT2:
2247     case TCP_TIME_WAIT:
2248    
2249       if (!tcp_sequence (sk, th, len, opt, saddr))
2250         {
2251            free_skb (skb, FREE_READ);
2252            release_sock(sk);
2253            return (0);
2254         }
2255 
2256       if (th->rst)
2257         {
2258           sk->err = ECONNRESET;
2259           sk->state = TCP_CLOSE;
2260           if (!sk->dead)
2261             {
2262               wake_up (sk->sleep);
2263             }
2264           free_skb (skb, FREE_READ);
2265           release_sock(sk);
2266           return (0);
2267         }
2268       if (opt && (opt->security != 0 || opt->compartment != 0 || th->syn))
2269         {
2270            sk->err = ECONNRESET;
2271            sk->state = TCP_CLOSE;
2272            tcp_reset (daddr, saddr,  th, sk->prot, opt,dev);
2273            if (!sk->dead)
2274              {
2275                 wake_up (sk->sleep);
2276              }
2277            free_skb (skb, FREE_READ);
2278            release_sock(sk);
2279            return (0);
2280         }
2281 
2282       if (th->ack)
2283         {
2284            if(!tcp_ack (sk, th, saddr))
2285             {
2286                free_skb (skb, FREE_READ);
2287                release_sock(sk);
2288                return (0);
2289            }
2290         }
2291       if (th->urg)
2292         {
2293           if (tcp_urg (sk, th, saddr))
2294             {
2295                free_skb (skb, FREE_READ);
2296                release_sock(sk);
2297                return (0);
2298             }
2299         }
2300 
2301       if ( tcp_data (skb, sk, saddr, len))
2302         {
2303            free_skb (skb, FREE_READ);
2304            release_sock(sk);
2305            return (0);
2306         }
2307 
2308       if (!th->fin)
2309         {
2310           release_sock(sk);
2311           return (0);
2312         }
2313 
2314       tcp_fin (sk, th, saddr, dev);
2315       release_sock(sk);
2316       return (0);
2317 
2318     case TCP_CLOSE:
2319 
2320       if (sk->dead || sk->daddr)
2321         {
2322            PRINTK ("packet received for closed,dead socket\n");
2323            free_skb (skb, FREE_READ);
2324            release_sock (sk);
2325            return (0);
2326         }
2327 
2328       if (!th->rst)
2329         {
2330           if (!th->ack)
2331             th->ack_seq=0;
2332           tcp_reset (daddr, saddr, th, sk->prot, opt,dev);
2333         }
2334       free_skb (skb, FREE_READ);
2335       release_sock(sk);
2336       return (0);
2337 
2338     case TCP_LISTEN:
2339       if (th->rst)
2340         {
2341            free_skb (skb, FREE_READ);
2342            release_sock(sk);
2343            return (0);
2344         }
2345       if (th->ack)
2346         {
2347           tcp_reset (daddr, saddr, th, sk->prot, opt,dev );
2348           free_skb (skb, FREE_READ);
2349           release_sock(sk);
2350           return (0);
2351         }
2352 
2353       if (th->syn)
2354         {
2355 /*        if (opt->security != 0 || opt->compartment != 0)
2356             {
2357               tcp_reset (daddr, saddr, th, prot, opt,dev);
2358               release_sock(sk);
2359               return (0);
2360             } */
2361 
2362           /* now we just put the whole thing including the header
2363              and saddr, and protocol pointer into the buffer.
2364              We can't respond until the user tells us to accept
2365              the connection. */
2366 
2367           tcp_conn_request (sk, skb, daddr, saddr, opt, dev);
2368 
2369           release_sock(sk);
2370           return (0);
2371         }
2372 
2373       free_skb (skb, FREE_READ);
2374       release_sock(sk);
2375       return (0);
2376 
2377     default:
2378       if (!tcp_sequence (sk, th, len, opt, saddr)) 
2379         {
2380            free_skb (skb, FREE_READ);
2381            release_sock(sk);
2382            return (0);
2383         }
2384 
2385     case TCP_SYN_SENT:
2386       if (th->rst)
2387         {
2388           sk->err = ECONNREFUSED;
2389           sk->state = TCP_CLOSE;
2390           if (!sk->dead)
2391             {
2392               wake_up (sk->sleep);
2393             }
2394           free_skb (skb, FREE_READ);
2395           release_sock(sk);
2396           return (0);
2397         }
2398 /*      if (opt->security != 0 || opt->compartment != 0 )
2399         {
2400           sk->err = ECONNRESET;
2401           sk->state = TCP_CLOSE;
2402           tcp_reset (daddr, saddr,  th, sk->prot, opt, dev);
2403           if (!sk->dead)
2404           {
2405           wake_up (sk->sleep);
2406           }
2407           free_skb (skb, FREE_READ);
2408           release_sock(sk);
2409           return (0);
2410         } */
2411 
2412       if (!th->ack) 
2413         {
2414           if (th->syn)
2415             {
2416               sk->state = TCP_SYN_RECV;
2417             }
2418 
2419           free_skb (skb, FREE_READ);
2420           release_sock(sk);
2421           return (0);
2422         }
2423 
2424       switch (sk->state)
2425         {
2426         case TCP_SYN_SENT:
2427           if (!tcp_ack(sk, th, saddr))
2428             {
2429               tcp_reset(daddr, saddr, th, sk->prot, opt,dev);
2430               free_skb (skb, FREE_READ);
2431               release_sock(sk);
2432               return (0);
2433             }
2434 
2435           /* if the syn bit is also set, switch to tcp_syn_recv,
2436              and then to established. */
2437               
2438           if (!th->syn) 
2439             {
2440               free_skb (skb, FREE_READ);
2441               release_sock (sk);
2442               return (0);
2443             }
2444 
2445           /* ack the syn and fall through. */
2446           sk->acked_seq = th->seq+1;
2447           sk->fin_seq = th->seq;
2448           tcp_send_ack (sk->send_seq, th->seq+1, sk, 
2449                              th, sk->daddr);
2450         
2451         case TCP_SYN_RECV:
2452           if (!tcp_ack(sk, th, saddr))
2453             {
2454               tcp_reset(daddr, saddr, th, sk->prot, opt, dev);
2455               free_skb (skb, FREE_READ);
2456               release_sock(sk);
2457               return (0);
2458             }
2459 
2460           sk->state = TCP_ESTABLISHED;
2461           /* now we need to finish filling out some of the tcp
2462              header. */
2463 
2464           /* we need to check for mtu info. */
2465           tcp_options(sk, th);
2466           sk->dummy_th.dest = th->source;
2467           sk->copied_seq = sk->acked_seq-1;
2468           if (!sk->dead)
2469             {
2470               wake_up (sk->sleep);
2471             }
2472 
2473           /* now process the rest like we were already in the established
2474              state. */
2475           if (th->urg)
2476             if (tcp_urg (sk, th, saddr))
2477               {
2478                  free_skb (skb, FREE_READ);
2479                  release_sock(sk);
2480                  return (0);
2481               }
2482           if (tcp_data (skb, sk, saddr, len))
2483             free_skb (skb, FREE_READ);
2484           
2485           if (th->fin)
2486             tcp_fin(sk, th, saddr, dev);
2487 
2488           release_sock(sk);
2489           return (0);
2490         }
2491 
2492       if (th->urg)
2493         {
2494           if (tcp_urg (sk, th, saddr))
2495             {
2496                free_skb (skb, FREE_READ);
2497                release_sock (sk);
2498                return (0);
2499             }
2500         }
2501 
2502       if (tcp_data (skb, sk, saddr, len))
2503         {
2504            free_skb (skb, FREE_READ);
2505            release_sock (sk);
2506            return (0);
2507         }
2508 
2509       if (!th->fin)
2510         {
2511           release_sock(sk);
2512           return (0);
2513         }
2514       tcp_fin (sk, th, saddr, dev);
2515       release_sock(sk);
2516       return (0);
2517     }
2518 }
2519 
2520 
2521 /* this routine sends a packet with an out of date sequence number. It
2522    assumes the other end will try to ack it. */
2523 
2524 static  void
2525 tcp_write_wakeup(volatile struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
2526 {
2527   struct sk_buff *buff;
2528   struct tcp_header *t1;
2529   struct device *dev=NULL;
2530   int tmp;
2531   if (sk -> state != TCP_ESTABLISHED) return;
2532 
2533   buff=sk->prot->wmalloc(sk,MAX_ACK_SIZE,1);
2534   /* no big loss. */
2535   if (buff == NULL) return;
2536 
2537   buff->mem_addr = buff;
2538   buff->mem_len = MAX_ACK_SIZE;
2539   buff->len=sizeof (struct tcp_header);
2540   buff->free = 1;
2541   buff->sk = sk;
2542   PRINTK ("in tcp_write_wakeup\n");
2543   t1=(struct tcp_header *)(buff + 1);
2544 
2545   /* put in the ip_header and routing stuff. */
2546   tmp = sk->prot->build_header (buff, sk->saddr, sk->daddr, &dev,
2547                                 IPPROTO_TCP, sk->opt, MAX_ACK_SIZE);
2548   if (tmp < 0)
2549     {
2550       sk->prot->wfree(sk, buff->mem_addr, buff->mem_len);
2551       return;
2552     }
2553 
2554   buff->len += tmp;
2555   t1 = (struct tcp_header *)((char *)t1 +tmp);
2556 
2557   memcpy (t1,(void *) &sk->dummy_th, sizeof (*t1));
2558 
2559   /* use a previous sequence.  This should cause the other end
2560      to send an ack. */
2561   t1->seq = net32(sk->send_seq-1);
2562   t1->ack = 1; 
2563   t1->res1= 0;
2564   t1->res2= 0;
2565   t1->rst = 0;
2566   t1->urg = 0;
2567   t1->psh = 0;
2568   t1->fin = 0;
2569   t1->syn = 0;
2570   t1->ack_seq = net32(sk->acked_seq);
2571   t1->window = net16(sk->prot->rspace(sk));
2572   t1->doff = sizeof (*t1)/4;
2573   tcp_send_check (t1, sk->saddr, sk->daddr, sizeof (*t1), sk);
2574   /* send it and free it.  This will prevent the timer from 
2575      automatically being restarted. */
2576   sk->prot->queue_xmit(sk, dev, buff, 1);
2577 
2578 }
2579 
2580 struct proto tcp_prot =
2581 {
2582   sock_wmalloc,
2583   sock_rmalloc,
2584   sock_wfree,
2585   sock_rfree,
2586   sock_rspace,
2587   sock_wspace,
2588   tcp_close,
2589   tcp_read,
2590   tcp_write,
2591   NULL,
2592   NULL,
2593   ip_build_header,
2594   tcp_connect,
2595   tcp_accept,
2596   ip_queue_xmit,
2597   tcp_retransmit,
2598   tcp_write_wakeup,
2599   tcp_read_wakeup,
2600   tcp_rcv,
2601   tcp_select,
2602   tcp_ioctl,
2603   NULL,
2604   128,
2605   0,
2606   {NULL,}
2607 };
2608 
2609 
2610 
2611 

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