root/net/tcp/ip.c

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

DEFINITIONS

This source file includes following definitions.
  1. get_protocol
  2. add_ip_protocol
  3. delete_ip_protocol
  4. ip_addr_match
  5. my_ip_addr
  6. strict_route
  7. loose_route
  8. print_rt
  9. print_ipprot
  10. ip_route
  11. add_route
  12. ip_set_dev
  13. ip_route_check
  14. build_options
  15. ip_build_header
  16. do_options
  17. ip_compute_csum
  18. ip_csum
  19. ip_send_check
  20. ip_rcv
  21. ip_queue_xmit
  22. ip_retransmit
  23. print_iph
  24. ip_handoff

   1 /* ip.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 /* $Id: ip.c,v 0.8.4.8 1992/12/12 19:25:04 bir7 Exp $ */
  23 /* $Log: ip.c,v $
  24  * Revision 0.8.4.8  1992/12/12  19:25:04  bir7
  25  * Cleaned up Log messages.
  26  *
  27  * Revision 0.8.4.7  1992/12/06  23:29:59  bir7
  28  * Changed retransmit to double rtt.
  29  *
  30  * Revision 0.8.4.6  1992/12/05  21:35:53  bir7
  31  * fixed checking of wrong fragmentation bit.
  32  *
  33  * Revision 0.8.4.5  1992/12/03  19:52:20  bir7
  34  * added paranoid queue checking
  35  *
  36  * Revision 0.8.4.4  1992/11/18  15:38:03  bir7
  37  * Fixed bug in copying packet and checking packet type.
  38  *
  39  * Revision 0.8.4.3  1992/11/17  14:19:47  bir7
  40  *
  41  * Revision 0.8.4.2  1992/11/10  10:38:48  bir7
  42  * Change free_s to kfree_s and accidently changed free_skb to kfree_skb.
  43  *
  44  * Revision 0.8.4.1  1992/11/10  00:17:18  bir7
  45  * version change only.
  46  *
  47  * Revision 0.8.3.3  1992/11/10  00:14:47  bir7
  48  * Changed malloc to kmalloc and added Id and Log
  49  *
  50  */
  51 
  52 #include <asm/segment.h>
  53 #include <asm/system.h>
  54 #include <linux/types.h>
  55 #include <linux/kernel.h>
  56 #include <linux/sched.h>
  57 #include <linux/string.h>
  58 #include <linux/socket.h>
  59 #include <netinet/in.h>
  60 #include "timer.h"
  61 #include "ip.h"
  62 #include "tcp.h"
  63 #include "sock.h"
  64 #include <linux/errno.h>
  65 #include "arp.h"
  66 #include "icmp.h"
  67 
  68 unsigned long ip_addr[MAX_IP_ADDRES]={0,0,0};
  69 
  70 #undef IP_DEBUG
  71 
  72 #ifdef IP_DEBUG
  73 #define PRINTK(X) printk X
  74 #else
  75 #define PRINTK(X) /**/
  76 #endif
  77 
  78 static struct rtable *rt_base=NULL; /* used to base all the routing data. */
  79 
  80 struct ip_protocol *ip_protos[MAX_IP_PROTOS] = { NULL, };
  81 
  82 #if 0
  83 static  struct ip_protocol *
  84 get_protocol(unsigned char prot)
     /* [previous][next][first][last][top][bottom][index][help] */
  85 {
  86    unsigned char hash;
  87    struct ip_protocol *p;
  88    PRINTK (("get_protocol (%d)\n ", prot));
  89    hash = prot & (MAX_IP_PROTOS -1);
  90    for (p = ip_protos[hash] ; p != NULL; p=p->next)
  91      {
  92         PRINTK (("trying protocol %d\n", p->protocol));
  93         if (p->protocol == prot)
  94              return (p);
  95      }
  96    return (NULL);
  97     
  98 }
  99 #endif
 100 
 101 void
 102 add_ip_protocol (struct ip_protocol *prot)
     /* [previous][next][first][last][top][bottom][index][help] */
 103 {
 104    unsigned char hash;
 105    struct ip_protocol *p2;
 106    hash = prot->protocol & (MAX_IP_PROTOS-1);
 107    prot ->next = ip_protos[hash];
 108    ip_protos[hash] = prot;
 109    prot->copy = 0;
 110    /* set the copy bit if we need to. */
 111    for (p2 = prot->next; p2 != NULL; p2=p2->next)
 112      {
 113         if (p2->protocol == prot->protocol)
 114           {
 115              prot->copy = 1;
 116              break;
 117           }
 118      }
 119 
 120 }
 121 
 122 int
 123 delete_ip_protocol (struct ip_protocol *prot)
     /* [previous][next][first][last][top][bottom][index][help] */
 124 {
 125    struct ip_protocol *p;
 126    struct ip_protocol *lp=NULL;
 127    unsigned char hash;
 128 
 129 
 130    hash = prot->protocol & (MAX_IP_PROTOS -1);
 131    if (prot == ip_protos[hash])
 132      {
 133         ip_protos[hash]=ip_protos[hash]->next;
 134         return (0);
 135      }
 136 
 137    for (p = ip_protos[hash]; p != NULL; p = p->next)
 138      {
 139         /* we have to worry if the protocol being deleted is the
 140            last one on the list, then we may need to reset someones
 141            copied bit. */
 142         if (p->next != NULL && p->next == prot)
 143           {
 144              /* if we are the last one with this protocol and
 145                 there is a previous one, reset its copy bit. */
 146 
 147              if (p->copy == 0 && lp != NULL)
 148                lp->copy = 0;
 149              p->next = prot->next;
 150              return (0);
 151           }
 152 
 153         if (p->next != NULL && p->next->protocol == prot->protocol)
 154           {
 155              lp = p;
 156           }
 157      }
 158    return (-1);
 159 }
 160 
 161 /* addr1 is the address which may or may not be broadcast etc.
 162    addr2 is the "real addr." */
 163 
 164 int
 165 ip_addr_match (unsigned long addr1, unsigned long addr2)
     /* [previous][next][first][last][top][bottom][index][help] */
 166 {
 167   int i;
 168   if (addr1 == addr2) return (1);
 169   for (i = 0; i < 4; i++, addr1 >>= 8, addr2 >>= 8)
 170     {
 171       if ((addr1 & 0xff) != (addr2 & 0xff))
 172         {
 173           /* the only way this could be a match is for the rest of
 174              addr1 to be 0. */
 175           if (addr1 != 0) 
 176             {
 177               return (0);
 178             }
 179           return (1);
 180         }
 181     }
 182   return (1);
 183 }
 184 
 185 int
 186 my_ip_addr(unsigned long addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 187 {
 188   int i;
 189   for (i = 0; i < MAX_IP_ADDRES; i++)
 190     {
 191       if (ip_addr[i] == 0) return (0);
 192       if (ip_addr_match (addr, ip_addr[i])) return (1);
 193     }
 194   return (0);
 195 }
 196 
 197 /* these two routines will do routining. */
 198 static  void
 199 strict_route(struct ip_header *iph, struct options *opt)
     /* [previous][next][first][last][top][bottom][index][help] */
 200 {
 201 }
 202 
 203 static  void
 204 loose_route(struct ip_header *iph, struct options *opt)
     /* [previous][next][first][last][top][bottom][index][help] */
 205 {
 206 }
 207 
 208 void
 209 print_rt(struct rtable *rt)
     /* [previous][next][first][last][top][bottom][index][help] */
 210 {
 211   PRINTK (("net = %08X router = %08X\n",rt->net, rt->router));
 212   PRINTK (("dev = %X, next = %X\n",rt->dev, rt->next));
 213 }
 214 
 215 void
 216 print_ipprot (struct ip_protocol *ipprot)
     /* [previous][next][first][last][top][bottom][index][help] */
 217 {
 218    PRINTK (("handler = %X, protocol = %d, copy=%d \n",
 219             ipprot->handler, ipprot->protocol, ipprot->copy));
 220 }
 221 
 222 /* This assumes that address are all in net order. */
 223 static  struct device *
 224 ip_route(struct options *opt, unsigned long daddr, unsigned long *raddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 225 {
 226   struct rtable *rt;
 227   /* look through the routing table for some
 228      kind of match. */
 229   for (rt=rt_base; rt != NULL; rt=rt->next)
 230     {
 231       /* see if we found one. */
 232       if (ip_addr_match (rt->net, daddr))
 233         {
 234           PRINTK (("IP: %X via %s (%X)\n", daddr, rt->dev->name, rt->router));
 235           *raddr = rt->router;
 236           return (rt->dev);
 237         }
 238     }
 239   return (NULL);
 240 };
 241 
 242 void
 243 add_route (struct rtable *rt)
     /* [previous][next][first][last][top][bottom][index][help] */
 244 {
 245   int mask;
 246   struct rtable *r;
 247   struct rtable *r1;
 248   PRINTK (("add_route (rt=%X):\n",rt));
 249   print_rt(rt);
 250 
 251   if (rt_base == NULL)
 252     {
 253       rt->next = NULL;
 254       rt_base = rt;
 255       return;
 256     }
 257 
 258   /* what we have to do is loop though this until we have found the
 259      first address which has the same generality as the one in rt.  Then
 260      we can put rt in after it. */
 261   for (mask = 0xff000000; mask != 0xffffffff; mask = (mask >> 8) | mask)
 262     {
 263       if (mask & rt->net)
 264         {
 265           mask = mask << 8;
 266           break;
 267         }
 268     }
 269   PRINTK (("mask = %X\n",mask));
 270   r1=rt_base;
 271   for (r=rt_base; r != NULL; r=r->next)
 272     {
 273        /* see if we are getting a duplicate. */
 274        if (r->net == rt->net)
 275          {
 276             if (r == rt_base)
 277               {
 278                  rt->next = r->next;
 279                  rt_base = rt;
 280               }
 281             else
 282               {
 283                  rt->next = r->next;
 284                  r1->next = rt;
 285               }
 286             kfree_s (r, sizeof (*r));
 287             return;
 288          }
 289 
 290       if (!(r->net & mask))
 291         {
 292            PRINTK (("adding before r=%X\n",r));
 293            print_rt(r);
 294            if (r == rt_base)
 295              {
 296                 rt->next = rt_base;
 297                 rt_base = rt;
 298                 return;
 299              }
 300            rt->next = r;
 301            r1->next = rt;
 302            return;
 303         }
 304       r1 = r;
 305     }
 306   PRINTK (("adding after r1=%X\n",r1));
 307   print_rt(r1);
 308   /* goes at the end. */
 309   rt->next = NULL;
 310   r1->next = rt;
 311 }
 312 
 313 int
 314 ip_set_dev (struct ip_config *u_ipc)
     /* [previous][next][first][last][top][bottom][index][help] */
 315 {
 316   struct rtable *rt;
 317   struct device *dev;
 318   struct ip_config ipc;
 319   static int ip_ads = 0;
 320 
 321   if (ip_ads >= MAX_IP_ADDRES) return (-EINVAL);
 322 
 323 /*  verify_area (u_ipc, sizeof (ipc));*/
 324   memcpy_fromfs(&ipc, u_ipc, sizeof (ipc));
 325   ipc.name[MAX_IP_NAME-1] = 0;
 326   dev = get_dev (ipc.name);
 327 
 328   if (dev == NULL) return (-EINVAL);
 329 
 330   /* see if we need to add a broadcast address. */
 331   if (ipc.net != -1)
 332     {
 333        PRINTK (("new broadcast for %s: %08X\n", dev->name, ipc.net));
 334        arp_add_broad (ipc.net, dev);
 335        rt = kmalloc (sizeof (*rt), GFP_KERNEL);
 336        if (rt == NULL) return (-ENOMEM);
 337        rt->net = ipc.net;
 338        rt->dev = dev;
 339        rt->router = 0;
 340        add_route (rt);
 341 /*     dev->net = ipc.net;*/
 342     }
 343 
 344   if (ipc.router != -1)
 345     {
 346        PRINTK (("new router for %s: %08X\n", dev->name, ipc.router));
 347        rt = kmalloc (sizeof (*rt),GFP_KERNEL);
 348        if (rt == NULL) return (-ENOMEM);
 349        rt->net = 0;
 350        rt->dev = dev;
 351        rt->router = ipc.router;
 352        add_route (rt);
 353     }
 354 
 355   if (dev->loopback)
 356     {
 357        PRINTK (("new loopback addr: %08X\n", ipc.paddr));
 358        rt = kmalloc (sizeof (*rt), GFP_KERNEL);
 359        if (rt == NULL) return (-ENOMEM);
 360        rt->net = ipc.paddr;
 361        rt->dev = dev;
 362        rt->router = 0;
 363        add_route (rt);
 364     }
 365 
 366 
 367   if (!my_ip_addr (ipc.paddr))
 368     {
 369        PRINTK (("new identity: %08X\n", ipc.paddr));
 370        ip_addr[ip_ads++] = ipc.paddr;
 371     }
 372 
 373   dev->up = ipc.up;
 374   if (dev->up)
 375     {
 376        if (dev->open)
 377          dev->open(dev);
 378     }
 379   else
 380     {
 381        if (dev->stop)
 382          dev->stop(dev);
 383     }
 384 
 385   return (0);
 386 }
 387 
 388 /* this routine will check to see if we have lost a gateway. */
 389 void
 390 ip_route_check (unsigned long daddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 391 {
 392 }
 393 
 394 #if 0
 395 /* this routine puts the options at the end of an ip header. */
 396 static  int
 397 build_options (struct ip_header *iph, struct options *opt)
     /* [previous][next][first][last][top][bottom][index][help] */
 398 {
 399   unsigned char *ptr;
 400   /* currently we don't support any options. */
 401   ptr = (unsigned char *)(iph+1);
 402   *ptr = 0;
 403   return (4);
 404 }
 405 #endif
 406 
 407 /* This routine builds the appropriate hardware/ip headers for
 408    the routine.  It assumes that if *prot != NULL then the
 409    protocol knows what it's doing, otherwise it uses the
 410    routing/arp tables to select a protocol struct. */
 411 
 412 int
 413 ip_build_header (struct sk_buff *skb, unsigned long saddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 414                  unsigned long daddr, struct device **dev, int type,
 415                  struct options *opt, int len)
 416 {
 417   static struct options optmem;
 418   struct ip_header *iph;
 419   unsigned char *buff;
 420   static int count = 0;
 421   unsigned long raddr; /* for the router. */
 422   int tmp;
 423   if (saddr == 0) saddr = MY_IP_ADDR;
 424   PRINTK (("ip_build_header (skb=%X, saddr=%X, daddr=%X, *dev=%X,\n"
 425            "                 type=%d, opt=%X, len = %d)\n",
 426            skb, saddr, daddr, *dev, type, opt, len));
 427   buff = (unsigned char *)(skb + 1);
 428   /* see if we need to look up the device. */
 429   if (*dev == NULL)
 430     {
 431       *dev = ip_route(&optmem,daddr, &raddr);
 432       if (*dev == NULL)
 433         {
 434           return (-ENETUNREACH);
 435         }
 436       opt = &optmem;
 437     }
 438   else
 439     {
 440       /* we still need the address of the first hop. */
 441       ip_route (&optmem, daddr, &raddr);
 442     }
 443   if (raddr == 0) raddr = daddr;
 444   /* now build the header. */
 445   /* we need to worry about routing in here.  daddr should
 446      really be the address of the next hop. */
 447   /* but raddr is . */
 448   if ((*dev)->hard_header)
 449     {
 450        tmp = (*dev)->hard_header(buff, *dev, ETHERTYPE_IP, raddr, saddr, len);
 451     }
 452   else
 453     {
 454        tmp = 0;
 455     }
 456   if (tmp < 0)
 457     {
 458        tmp = -tmp;
 459        skb->arp = 0;
 460     }
 461   else
 462     {
 463        skb->arp = 1;
 464     }
 465   buff += tmp;
 466   len -= tmp;
 467   skb->dev = *dev;
 468   /* now build the ip header. */
 469   iph = (struct ip_header *)buff;
 470   iph->version = 4;
 471   iph->tos = 0;
 472   iph->frag_off = 0;
 473   iph->ttl = 32;
 474   iph->daddr = daddr;
 475   iph->saddr = saddr;
 476   iph->protocol=type;
 477   iph->ihl = 5;
 478   iph->id = net16(count++);
 479   /* build_options (iph, opt);*/
 480   return (20+tmp);
 481 }
 482 
 483 static  int
 484 do_options(struct ip_header *iph, struct options *opt)
     /* [previous][next][first][last][top][bottom][index][help] */
 485 {
 486   unsigned char *buff;
 487   int done = 0;
 488   int len=sizeof (*iph);
 489   int i;
 490   /* zero  out the options. */
 491   opt->record_route.route_size = 0;
 492   opt->loose_route.route_size = 0;
 493   opt->strict_route.route_size = 0;
 494   opt->tstamp.ptr = 0;
 495   opt->security = 0;
 496   opt->compartment = 0;
 497   opt->handling = 0;
 498   opt->stream = 0;
 499   opt->tcc = 0;
 500   return (0);
 501   /* advance the pointer to start at the options. */
 502   buff = (unsigned char *)(iph + 1);
 503 
 504   /*now start the processing. */
 505   while (!done && len < iph->ihl*4)
 506     {
 507       switch (*buff)
 508         {
 509         case IPOPT_END:
 510           done=1;
 511           break;
 512 
 513         case IPOPT_NOOP:
 514           buff++;
 515           len ++;
 516           break;
 517 
 518         case IPOPT_SEC:
 519           buff++;
 520           if (*buff != 11)
 521             return (1);
 522           buff++;
 523           opt->security = net16(*(unsigned short *)buff);
 524           buff += 2;
 525           opt->compartment = net16(*(unsigned short *)buff);
 526           buff += 2;
 527           opt-> handling = net16(*(unsigned short *)buff);
 528           buff += 2;
 529           opt->tcc = ((*buff) << 16) + net16(*(unsigned short *)(buff+1));
 530           buff += 3;
 531           len += 11;
 532           break;
 533 
 534         case IPOPT_LSRR:
 535           buff ++;
 536           if ((*buff - 3)% 4 != 0) return (1);
 537           len += *buff;
 538           opt->loose_route.route_size = (*buff -3)/4;
 539           buff ++;
 540           if (*buff % 4 != 0) return (1);
 541           opt->loose_route.pointer = *buff/4 - 1;
 542           buff ++;
 543           buff ++;
 544           for (i = 0; i < opt->loose_route.route_size; i++)
 545             {
 546               opt->loose_route.route[i]=*(unsigned long *)buff;
 547               buff += 4;
 548             }
 549           break;
 550 
 551 
 552         case IPOPT_SSRR:
 553           buff ++;
 554           if ((*buff - 3)% 4 != 0) return (1);
 555           len += *buff;
 556           opt->strict_route.route_size = (*buff -3)/4;
 557           buff ++;
 558           if (*buff % 4 != 0) return (1);
 559           opt->strict_route.pointer = *buff/4 - 1;
 560           buff ++;
 561           buff ++;
 562           for (i = 0; i < opt->strict_route.route_size; i++)
 563             {
 564               opt->strict_route.route[i]=*(unsigned long *)buff;
 565               buff += 4;
 566             }
 567           break;
 568 
 569         case IPOPT_RR:
 570           buff ++;
 571           if ((*buff - 3)% 4 != 0) return (1);
 572           len += *buff;
 573           opt->record_route.route_size = (*buff -3)/4;
 574           buff ++;
 575           if (*buff % 4 != 0) return (1);
 576           opt->record_route.pointer = *buff/4 - 1;
 577           buff ++;
 578           buff ++;
 579           for (i = 0; i < opt->record_route.route_size; i++)
 580             {
 581               opt->record_route.route[i]=*(unsigned long *)buff;
 582               buff += 4;
 583             }
 584           break;
 585 
 586         case IPOPT_SID:
 587           len += 4;
 588           buff +=2;
 589           opt->stream = *(unsigned short *)buff;
 590           buff += 2;
 591           break;
 592 
 593         case IPOPT_TIMESTAMP:
 594           buff ++;
 595           len += *buff;
 596           if (*buff % 4 != 0) return (1);
 597           opt->tstamp.len = *buff / 4 - 1;
 598           buff ++;
 599           if ((*buff - 1) % 4 != 0) return (1);
 600           opt->tstamp.ptr = (*buff-1)/4;
 601           buff ++;
 602           opt->tstamp.x.full_char = *buff;
 603           buff ++;
 604           for (i = 0; i < opt->tstamp.len; i++)
 605             {
 606               opt->tstamp.data[i] = *(unsigned long *)buff;
 607               buff += 4;
 608             }
 609           break;
 610 
 611         default:
 612           return (1);
 613         }
 614     }
 615   if (opt->record_route.route_size == 0)
 616     {
 617       if (opt->strict_route.route_size != 0)
 618         {
 619           memcpy (&(opt->record_route), &(opt->strict_route),
 620                   sizeof (opt->record_route));
 621         }
 622       else if (opt->loose_route.route_size != 0)
 623         {
 624           memcpy (&(opt->record_route), &(opt->loose_route),
 625                   sizeof (opt->record_route));
 626         }
 627     }
 628 
 629   if (opt->strict_route.route_size != 0 &&
 630       opt->strict_route.route_size != opt->strict_route.pointer)
 631     {
 632       strict_route (iph, opt);
 633       return (0);
 634     }
 635 
 636   if (opt->loose_route.route_size != 0 &&
 637       opt->loose_route.route_size != opt->loose_route.pointer)
 638     {
 639       loose_route (iph, opt);
 640       return (0);
 641     }
 642 
 643   return (0);
 644 }
 645 
 646 
 647 /* This routine does all the checksum computations that don't require
 648    anything special (like copying or special headers.) */
 649 
 650 unsigned short
 651 ip_compute_csum(unsigned char * buff, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 652 {
 653   unsigned long sum = 0;
 654   if (len > 3)
 655     {
 656        /* do the first multiple of 4 bytes and convert to 16 bits. */
 657        __asm__("\t clc\n"
 658                "1:\n"
 659                "\t lodsl\n"
 660                "\t adcl %%eax, %%ebx\n"
 661                "\t loop 1b\n"
 662                "\t adcl $0, %%ebx\n"
 663                "\t movl %%ebx, %%eax\n"
 664                "\t shrl $16, %%eax\n"
 665                "\t addw %%ax, %%bx\n"
 666                "\t adcw $0, %%bx\n"
 667                : "=b" (sum) , "=S" (buff)
 668                : "0" (sum), "c" (len >> 2) ,"1" (buff)
 669                : "ax", "cx", "si", "bx" );
 670     }
 671   if (len & 2)
 672     {
 673        __asm__("\t lodsw\n"
 674                "\t addw %%ax, %%bx\n"
 675                "\t adcw $0, %%bx\n"
 676                : "=b" (sum), "=S" (buff)
 677                : "0" (sum), "1" (buff)
 678                : "bx", "ax", "si");
 679     }
 680   if (len & 1)
 681     {
 682        __asm__("\t lodsb\n"
 683                "\t movb $0, %%ah\n"
 684                "\t addw %%ax, %%bx\n"
 685                "\t adcw $0, %%bx\n"
 686                : "=b" (sum), "=S" (buff)
 687                : "0" (sum), "1" (buff)
 688                : "bx", "ax", "si");
 689     }
 690   sum =~sum;
 691   return (sum&0xffff);
 692 }
 693 
 694 static  int
 695 ip_csum(struct ip_header *iph)
     /* [previous][next][first][last][top][bottom][index][help] */
 696 {
 697   if (iph->check == 0) return (0);
 698   if (ip_compute_csum((unsigned char *)iph, iph->ihl*4) == 0)  return (0);
 699   return (1);
 700 }
 701 
 702 static  void
 703 ip_send_check(struct ip_header *iph)
     /* [previous][next][first][last][top][bottom][index][help] */
 704 {
 705    iph->check = 0;
 706    iph->check = ip_compute_csum((unsigned char *)iph, iph->ihl*4);
 707 }
 708 
 709 int
 710 ip_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
     /* [previous][next][first][last][top][bottom][index][help] */
 711 {
 712   struct ip_header *iph;
 713   unsigned char hash;
 714   unsigned char flag=0;
 715   static struct options opt; /* since we don't use these yet, and they
 716                                 take up stack space. */
 717   struct ip_protocol *ipprot;
 718 
 719   iph=skb->h.iph;
 720 
 721   PRINTK (("<<\n"));
 722   print_iph(iph);
 723 
 724   if (ip_csum (iph) || do_options (iph,&opt) || iph->version != 4)
 725     {
 726        PRINTK (("ip packet thrown out. \n"));
 727        skb->sk = NULL;
 728        kfree_skb(skb, 0);
 729        return (0);
 730     }
 731 
 732   /* for now we will only deal with packets meant for us. */
 733   if (!my_ip_addr(iph->daddr))
 734     {
 735        PRINTK (("packet meant for someone else.\n"));
 736        skb->sk = NULL;
 737        kfree_skb(skb, 0);
 738        return (0);
 739     }
 740 
 741   /* deal with fragments.  or don't for now.*/
 742   if ((iph->frag_off & 32) || (net16(iph->frag_off)&0x1fff))
 743     {
 744        printk ("packet fragmented. \n");
 745        skb->sk = NULL;
 746        kfree_skb(skb, 0);
 747        return(0);
 748     }
 749 
 750   skb->h.raw += iph->ihl*4;
 751 
 752   hash = iph->protocol & (MAX_IP_PROTOS -1);
 753   for (ipprot = ip_protos[hash]; ipprot != NULL; ipprot=ipprot->next)
 754     {
 755        struct sk_buff *skb2;
 756        if (ipprot->protocol != iph->protocol) continue;
 757        PRINTK (("Using protocol = %X:\n", ipprot));
 758        print_ipprot (ipprot);
 759        /* pass it off to everyone who wants it. */
 760        /* we should check the return values here. */
 761        /* see if we need to make a copy of it.  This will
 762           only be set if more than one protpocol wants it. 
 763           and then not for the last one. */
 764 
 765        if (ipprot->copy)
 766          {
 767             skb2 = kmalloc (skb->mem_len, GFP_ATOMIC);
 768             if (skb2 == NULL) continue;
 769             memcpy (skb2, skb, skb->mem_len);
 770             skb2->mem_addr = skb2;
 771             skb2->lock = 0;
 772             skb2->h.raw = (void *)((unsigned long)skb2
 773                                    + (unsigned long)skb->h.raw
 774                                    - (unsigned long)skb);
 775          }
 776        else
 777          {
 778             skb2 = skb;
 779          }
 780        flag = 1;
 781        ipprot->handler (skb2, dev, &opt, iph->daddr,
 782                         net16(iph->tot_len) - iph->ihl*4,
 783                         iph->saddr, 0, ipprot);
 784 
 785     }
 786   if (!flag)
 787     {
 788        icmp_reply (skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, dev);
 789        skb->sk = NULL;
 790        kfree_skb (skb, 0);
 791     }
 792 
 793 
 794   return (0);
 795 }
 796 
 797 
 798 /* queues a packet to be sent, and starts the transmitter if
 799    necessary.  if free = 1 then we free the block after transmit,
 800    otherwise we don't. */
 801 /* This routine also needs to put in the total length, and compute
 802    the checksum. */
 803 void
 804 ip_queue_xmit (volatile struct sock *sk, struct device *dev, 
     /* [previous][next][first][last][top][bottom][index][help] */
 805                struct sk_buff *skb, int free)
 806 {
 807   struct ip_header *iph;
 808   unsigned char *ptr;
 809   if (sk == NULL) free = 1;
 810 
 811   if (dev == NULL)
 812     {
 813       printk ("ip.c: ip_queue_xmit dev = NULL\n");
 814       return;
 815     }
 816 
 817   skb->free = free;
 818   skb->dev = dev;
 819   skb->when = jiffies;
 820   PRINTK ((">>\n"));
 821   ptr = (unsigned char *)(skb + 1);
 822   ptr += dev->hard_header_len;
 823   iph = (struct ip_header *)ptr;
 824   iph->tot_len = net16(skb->len-dev->hard_header_len);
 825   ip_send_check (iph);
 826   print_iph(iph);
 827   skb->next = NULL;
 828 
 829   /* see if this is the one
 830      trashing our queue. */
 831   skb->magic = 1;
 832 
 833   if (!free)
 834     {
 835       skb->link3 = NULL;
 836       sk->packets_out++;
 837       cli();
 838       if (sk->send_tail == NULL)
 839         {
 840           sk->send_tail = skb;
 841           sk->send_head = skb;
 842         }
 843       else
 844         {
 845           sk->send_tail->link3 = skb;
 846           sk->send_tail = skb;
 847         }
 848       sti();
 849       sk->time_wait.len = sk->rtt*2;
 850       sk->timeout=TIME_WRITE;
 851       reset_timer ((struct timer *)&sk->time_wait);
 852    }
 853   else
 854     {
 855        skb->sk = sk;
 856     }
 857   if (dev->up)
 858     {
 859        if (sk != NULL)
 860          {
 861            dev->queue_xmit(skb, dev, sk->priority);
 862          }
 863        else
 864          {
 865            dev->queue_xmit (skb, dev, SOPRI_NORMAL);
 866          }
 867     }
 868   else
 869     {
 870        if (free) 
 871          kfree_skb (skb, FREE_WRITE);
 872     }
 873 }
 874 
 875 void
 876 ip_retransmit (volatile struct sock *sk, int all)
     /* [previous][next][first][last][top][bottom][index][help] */
 877 {
 878   struct sk_buff * skb;
 879   struct proto *prot;
 880   struct device *dev;
 881 
 882   prot = sk->prot;
 883   skb = sk->send_head;
 884   while (skb != NULL)
 885     {
 886       dev = skb->dev;
 887       /* rebuild_header sees if the arp is done.  If not it sends a new
 888          arp, and if so it builds the header. */
 889       if (!skb->arp)
 890         {
 891           if (dev->rebuild_header ((struct enet_header *)(skb+1),dev))
 892             {
 893                if (!all) break;
 894                skb=skb->link3;
 895                continue;
 896             }
 897        }
 898       skb->arp = 1;
 899       skb->when = jiffies;
 900 
 901       if (dev->up)
 902         if (sk)
 903           dev->queue_xmit(skb, dev, sk->priority);
 904         else
 905           dev->queue_xmit(skb, dev, SOPRI_NORMAL );
 906 
 907       sk->retransmits++;
 908       sk->prot->retransmits ++;
 909       if (!all) break;
 910 
 911       /* this should cut it off before we send too
 912          many packets. */
 913       if (sk->retransmits > sk->cong_window) break;
 914       skb=skb->link3;
 915     }
 916   /* double the rtt time every time we retransmit. 
 917      This will cause exponential back off on how
 918      hard we try to get through again.  Once we
 919      get through, the rtt will settle back down
 920      reasonably quickly. */
 921 
 922   sk->rtt *= 2;
 923   sk->time_wait.len = sk->rtt;
 924   sk->timeout = TIME_WRITE;
 925   reset_timer ((struct timer *)&sk->time_wait);
 926 }
 927 
 928 void
 929 print_iph (struct ip_header *ip)
     /* [previous][next][first][last][top][bottom][index][help] */
 930 {
 931   PRINTK (("ip header:\n"));
 932   PRINTK (("  ihl = %d, version = %d, tos = %d, tot_len = %d\n",
 933            ip->ihl, ip->version, ip->tos, net16(ip->tot_len)));
 934   PRINTK (("  id = %x, ttl = %d, prot = %d, check=%x\n",
 935            ip->id, ip->ttl, ip->protocol, ip->check));
 936   PRINTK ((" frag_off=%d\n", ip->frag_off));
 937   PRINTK (("  saddr = %X, daddr = %X\n",ip->saddr, ip->daddr));
 938 }
 939 
 940 #if 0
 941 int
 942 ip_handoff (volatile struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 943 {
 944    struct ip_protocol *p;
 945    struct sk_buff *skb;
 946    p = get_protocol (sk->protocol);
 947 
 948    if (p == NULL)
 949      {
 950         /* this can never happen. */
 951         printk ("sock_ioctl: protocol not found. \n");
 952         /* what else can I do, I suppose I could send a sigkill. */
 953         return (-EIO);
 954      }
 955 
 956    while (p->handler != sk->prot->rcv)
 957      {
 958         p=p->next;
 959         if (p == NULL)
 960           {
 961              /* this can never happen. */
 962              printk ("sock_ioctl: protocol not found. \n");
 963              /* what else can I do, I suppose I could send a sigkill. */
 964              return (-EIO);
 965           }
 966      }
 967    p = p-> next;
 968    sk->inuse = 1;
 969    
 970    /* now we have to remove the top sock buff.  If there are none, then
 971       we return. */
 972    if (sk->rqueue == NULL) return (0);
 973    skb = sk->rqueue;
 974    if (skb->next == skb)
 975      {
 976         sk->rqueue = NULL;
 977      }
 978    else
 979      {
 980         sk->rqueue = skb->next;
 981         skb->next->prev = skb->prev;
 982         skb->prev->next = skb->next;
 983      }
 984    if (p != NULL)
 985      {
 986         p->handler ((unsigned char *)(skb+1), skb->dev, NULL, skb->saddr,
 987                     skb->len, skb->daddr, p->protocol, 0);
 988      }
 989    kfree_skb (skb, FREE_READ);
 990    release_sock (sk);
 991    return (0);
 992 }
 993 
 994 #endif

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