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

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