root/net/inet/ip.c

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

DEFINITIONS

This source file includes following definitions.
  1. ip_print
  2. ip_ioctl
  3. strict_route
  4. loose_route
  5. print_ipprot
  6. ip_route_check
  7. build_options
  8. ip_send
  9. ip_build_header
  10. do_options
  11. ip_compute_csum
  12. ip_csum
  13. ip_send_check
  14. ip_forward
  15. ip_rcv
  16. ip_queue_xmit
  17. ip_retransmit
  18. backoff

   1 /*
   2  * INET         An implementation of the TCP/IP protocol suite for the LINUX
   3  *              operating system.  INET is implemented using the  BSD Socket
   4  *              interface as the means of communication with the user level.
   5  *
   6  *              The Internet Protocol (IP) module.
   7  *
   8  * Version:     @(#)ip.c        1.0.16  06/02/93
   9  *
  10  * Authors:     Ross Biro, <bir7@leland.Stanford.Edu>
  11  *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12  *
  13  *              This program is free software; you can redistribute it and/or
  14  *              modify it under the terms of the GNU General Public License
  15  *              as published by the Free Software Foundation; either version
  16  *              2 of the License, or (at your option) any later version.
  17  */
  18 #include <asm/segment.h>
  19 #include <asm/system.h>
  20 #include <linux/types.h>
  21 #include <linux/kernel.h>
  22 #include <linux/sched.h>
  23 #include <linux/string.h>
  24 #include <linux/errno.h>
  25 #include <linux/socket.h>
  26 #include <linux/sockios.h>
  27 #include <linux/in.h>
  28 #include "inet.h"
  29 #include "timer.h"
  30 #include "dev.h"
  31 #include "eth.h"
  32 #include "ip.h"
  33 #include "protocol.h"
  34 #include "route.h"
  35 #include "tcp.h"
  36 #include "skbuff.h"
  37 #include "sock.h"
  38 #include "arp.h"
  39 #include "icmp.h"
  40 
  41 extern int last_retran;
  42 extern void sort_send(struct sock *sk);
  43 
  44 void
  45 ip_print(struct iphdr *ip)
     /* [previous][next][first][last][top][bottom][index][help] */
  46 {
  47   unsigned char buff[32];
  48   unsigned char *ptr;
  49   int addr, len, i;
  50 
  51   if (inet_debug != DBG_IP) return;
  52 
  53   /* Dump the IP header. */
  54   printk("IP: ihl=%d, version=%d, tos=%d, tot_len=%d\n",
  55            ip->ihl, ip->version, ip->tos, ntohs(ip->tot_len));
  56   printk("    id=%X, ttl=%d, prot=%d, check=%X\n",
  57            ip->id, ip->ttl, ip->protocol, ip->check);
  58   printk("    frag_off=%d\n", ip->frag_off);
  59   printk("    soucre=%s ", in_ntoa(ip->saddr));
  60   printk("dest=%s\n", in_ntoa(ip->daddr));
  61   printk("    ----\n");
  62 
  63   /* Dump the data. */
  64   ptr = (unsigned char *)(ip + 1);
  65   addr = 0;
  66   len = ntohs(ip->tot_len) - (4 * ip->ihl);
  67   while (len > 0) {
  68         printk("    %04X: ", addr);
  69         for(i = 0; i < 16; i++) {
  70                 if (len > 0) {
  71                         printk("%02X ", (*ptr & 0xFF));
  72                         buff[i] = *ptr++;
  73                         if (buff[i] < 32 || buff[i] > 126) buff[i] = '.';
  74                 } else {
  75                         printk("   ");
  76                         buff[i] = ' ';
  77                 }
  78                 addr++;
  79                 len--;
  80         };
  81         buff[i] = '\0';
  82         printk("  \"%s\"\n", buff);
  83   }
  84   printk("    ----\n\n");
  85 }
  86 
  87 
  88 int
  89 ip_ioctl(struct sock *sk, int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
  90 {
  91   switch(cmd) {
  92         case DDIOCSDBG:
  93                 return(dbg_ioctl((void *) arg, DBG_IP));
  94         default:
  95                 return(-EINVAL);
  96   }
  97 }
  98 
  99 
 100 /* these two routines will do routining. */
 101 static void
 102 strict_route(struct iphdr *iph, struct options *opt)
     /* [previous][next][first][last][top][bottom][index][help] */
 103 {
 104 }
 105 
 106 
 107 static void
 108 loose_route(struct iphdr *iph, struct options *opt)
     /* [previous][next][first][last][top][bottom][index][help] */
 109 {
 110 }
 111 
 112 
 113 static void
 114 print_ipprot(struct inet_protocol *ipprot)
     /* [previous][next][first][last][top][bottom][index][help] */
 115 {
 116   DPRINTF((DBG_IP, "handler = %X, protocol = %d, copy=%d \n",
 117            ipprot->handler, ipprot->protocol, ipprot->copy));
 118 }
 119 
 120 
 121 /* This routine will check to see if we have lost a gateway. */
 122 void
 123 ip_route_check(unsigned long daddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 124 {
 125 }
 126 
 127 
 128 #if 0
 129 /* this routine puts the options at the end of an ip header. */
 130 static int
 131 build_options(struct iphdr *iph, struct options *opt)
     /* [previous][next][first][last][top][bottom][index][help] */
 132 {
 133   unsigned char *ptr;
 134   /* currently we don't support any options. */
 135   ptr = (unsigned char *)(iph+1);
 136   *ptr = 0;
 137   return (4);
 138 }
 139 #endif
 140 
 141 
 142 /* Take an skb, and fill in the MAC header. */
 143 static int
 144 ip_send(struct sk_buff *skb, unsigned long daddr, int len, struct device *dev,
     /* [previous][next][first][last][top][bottom][index][help] */
 145         unsigned long saddr)
 146 {
 147   unsigned char *ptr;
 148   int mac;
 149 
 150   ptr = (unsigned char *)(skb + 1);
 151   mac = 0;
 152   skb->arp = 1;
 153   if (dev->hard_header) {
 154         mac = dev->hard_header(ptr, dev, ETH_P_IP, daddr, saddr, len);
 155   }
 156   if (mac < 0) {
 157         mac = -mac;
 158         skb->arp = 0;
 159   }
 160   skb->dev = dev;
 161   return(mac);
 162 }
 163 
 164 
 165 /*
 166  * This routine builds the appropriate hardware/IP headers for
 167  * the routine.  It assumes that if *dev != NULL then the
 168  * protocol knows what it's doing, otherwise it uses the
 169  * routing/ARP tables to select a device struct.
 170  */
 171 int
 172 ip_build_header(struct sk_buff *skb, unsigned long saddr, unsigned long daddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 173                 struct device **dev, int type, struct options *opt, int len)
 174 {
 175   static struct options optmem;
 176   struct iphdr *iph;
 177   struct rtable *rt;
 178   unsigned char *buff;
 179   unsigned long raddr;
 180   static int count = 0;
 181   int tmp;
 182 
 183   if (saddr == 0) saddr = my_addr();
 184   DPRINTF((DBG_IP, "ip_build_header (skb=%X, saddr=%X, daddr=%X, *dev=%X,\n"
 185            "                 type=%d, opt=%X, len = %d)\n",
 186            skb, saddr, daddr, *dev, type, opt, len));
 187   buff = (unsigned char *)(skb + 1);
 188 
 189   /* See if we need to look up the device. */
 190   if (*dev == NULL) {
 191         rt = rt_route(daddr, &optmem);
 192         if (rt == NULL) return(-ENETUNREACH);
 193 
 194         *dev = rt->rt_dev;
 195         if (daddr != 0x0100007F) saddr = rt->rt_dev->pa_addr;
 196         raddr = rt->rt_gateway;
 197 
 198         DPRINTF((DBG_IP, "ip_build_header: saddr set to %s\n", in_ntoa(saddr)));
 199         opt = &optmem;
 200   } else {
 201         /* We still need the address of the first hop. */
 202         rt = rt_route(daddr, &optmem);
 203         raddr = (rt == NULL) ? 0 : rt->rt_gateway;
 204   }
 205   if (raddr == 0) raddr = daddr;
 206 
 207   /* Now build the MAC header. */
 208   tmp = ip_send(skb, raddr, len, *dev, saddr);
 209   buff += tmp;
 210   len -= tmp;
 211 
 212   skb->dev = *dev;
 213   skb->saddr = saddr;
 214   if (skb->sk) skb->sk->saddr = saddr;
 215 
 216   /* Now build the IP header. */
 217 
 218   /* If we are using IPPROTO_RAW, then we don't need an IP header, since
 219      one is being supplied to us by the user */
 220 
 221   if(type == IPPROTO_RAW) return (tmp);
 222 
 223   iph = (struct iphdr *)buff;
 224   iph->version  = 4;
 225   iph->tos      = 0;
 226   iph->frag_off = 0;
 227   iph->ttl      = 32;
 228   iph->daddr    = daddr;
 229   iph->saddr    = saddr;
 230   iph->protocol = type;
 231   iph->ihl      = 5;
 232   iph->id       = htons(count++);
 233 
 234   /* Setup the IP options. */
 235 #ifdef Not_Yet_Avail
 236   build_options(iph, opt);
 237 #endif
 238 
 239   return(20 + tmp);     /* IP header plus MAC header size */
 240 }
 241 
 242 
 243 static int
 244 do_options(struct iphdr *iph, struct options *opt)
     /* [previous][next][first][last][top][bottom][index][help] */
 245 {
 246   unsigned char *buff;
 247   int done = 0;
 248   int i, len = sizeof(struct iphdr);
 249 
 250   /* Zero out the options. */
 251   opt->record_route.route_size = 0;
 252   opt->loose_route.route_size  = 0;
 253   opt->strict_route.route_size = 0;
 254   opt->tstamp.ptr              = 0;
 255   opt->security                = 0;
 256   opt->compartment             = 0;
 257   opt->handling                = 0;
 258   opt->stream                  = 0;
 259   opt->tcc                     = 0;
 260   return(0);
 261 
 262   /* Advance the pointer to start at the options. */
 263   buff = (unsigned char *)(iph + 1);
 264 
 265   /* Now start the processing. */
 266   while (!done && len < iph->ihl*4) switch(*buff) {
 267         case IPOPT_END:
 268                 done = 1;
 269                 break;
 270         case IPOPT_NOOP:
 271                 buff++;
 272                 len++;
 273                 break;
 274         case IPOPT_SEC:
 275                 buff++;
 276                 if (*buff != 11) return(1);
 277                 buff++;
 278                 opt->security = ntohs(*(unsigned short *)buff);
 279                 buff += 2;
 280                 opt->compartment = ntohs(*(unsigned short *)buff);
 281                 buff += 2;
 282                 opt->handling = ntohs(*(unsigned short *)buff);
 283                 buff += 2;
 284                 opt->tcc = ((*buff) << 16) + ntohs(*(unsigned short *)(buff+1));
 285                 buff += 3;
 286                 len += 11;
 287                 break;
 288         case IPOPT_LSRR:
 289                 buff++;
 290                 if ((*buff - 3)% 4 != 0) return(1);
 291                 len += *buff;
 292                 opt->loose_route.route_size = (*buff -3)/4;
 293                 buff++;
 294                 if (*buff % 4 != 0) return(1);
 295                 opt->loose_route.pointer = *buff/4 - 1;
 296                 buff++;
 297                 buff++;
 298                 for (i = 0; i < opt->loose_route.route_size; i++) {
 299                         opt->loose_route.route[i] = *(unsigned long *)buff;
 300                         buff += 4;
 301                 }
 302                 break;
 303         case IPOPT_SSRR:
 304                 buff++;
 305                 if ((*buff - 3)% 4 != 0) return(1);
 306                 len += *buff;
 307                 opt->strict_route.route_size = (*buff -3)/4;
 308                 buff++;
 309                 if (*buff % 4 != 0) return(1);
 310                 opt->strict_route.pointer = *buff/4 - 1;
 311                 buff++;
 312                 buff++;
 313                 for (i = 0; i < opt->strict_route.route_size; i++) {
 314                         opt->strict_route.route[i] = *(unsigned long *)buff;
 315                         buff += 4;
 316                 }
 317                 break;
 318         case IPOPT_RR:
 319                 buff++;
 320                 if ((*buff - 3)% 4 != 0) return(1);
 321                 len += *buff;
 322                 opt->record_route.route_size = (*buff -3)/4;
 323                 buff++;
 324                 if (*buff % 4 != 0) return(1);
 325                 opt->record_route.pointer = *buff/4 - 1;
 326                 buff++;
 327                 buff++;
 328                 for (i = 0; i < opt->record_route.route_size; i++) {
 329                         opt->record_route.route[i] = *(unsigned long *)buff;
 330                         buff += 4;
 331                 }
 332                 break;
 333         case IPOPT_SID:
 334                 len += 4;
 335                 buff +=2;
 336                 opt->stream = *(unsigned short *)buff;
 337                 buff += 2;
 338                 break;
 339         case IPOPT_TIMESTAMP:
 340                 buff++;
 341                 len += *buff;
 342                 if (*buff % 4 != 0) return(1);
 343                 opt->tstamp.len = *buff / 4 - 1;
 344                 buff++;
 345                 if ((*buff - 1) % 4 != 0) return(1);
 346                 opt->tstamp.ptr = (*buff-1)/4;
 347                 buff++;
 348                 opt->tstamp.x.full_char = *buff;
 349                 buff++;
 350                 for (i = 0; i < opt->tstamp.len; i++) {
 351                         opt->tstamp.data[i] = *(unsigned long *)buff;
 352                         buff += 4;
 353                 }
 354                 break;
 355         default:
 356                 return(1);
 357   }
 358 
 359   if (opt->record_route.route_size == 0) {
 360         if (opt->strict_route.route_size != 0) {
 361                 memcpy(&(opt->record_route), &(opt->strict_route),
 362                                              sizeof(opt->record_route));
 363         } else if (opt->loose_route.route_size != 0) {
 364                 memcpy(&(opt->record_route), &(opt->loose_route),
 365                                              sizeof(opt->record_route));
 366         }
 367   }
 368 
 369   if (opt->strict_route.route_size != 0 &&
 370       opt->strict_route.route_size != opt->strict_route.pointer) {
 371         strict_route(iph, opt);
 372         return(0);
 373   }
 374 
 375   if (opt->loose_route.route_size != 0 &&
 376       opt->loose_route.route_size != opt->loose_route.pointer) {
 377         loose_route(iph, opt);
 378         return(0);
 379   }
 380 
 381   return(0);
 382 }
 383 
 384 
 385 /*
 386  * This routine does all the checksum computations that don't
 387  * require anything special (like copying or special headers).
 388  */
 389 unsigned short
 390 ip_compute_csum(unsigned char * buff, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 391 {
 392   unsigned long sum = 0;
 393 
 394   /* Do the first multiple of 4 bytes and convert to 16 bits. */
 395   if (len > 3) {
 396         __asm__("\t clc\n"
 397                 "1:\n"
 398                 "\t lodsl\n"
 399                 "\t adcl %%eax, %%ebx\n"
 400                 "\t loop 1b\n"
 401                 "\t adcl $0, %%ebx\n"
 402                 "\t movl %%ebx, %%eax\n"
 403                 "\t shrl $16, %%eax\n"
 404                 "\t addw %%ax, %%bx\n"
 405                 "\t adcw $0, %%bx\n"
 406                 : "=b" (sum) , "=S" (buff)
 407                 : "0" (sum), "c" (len >> 2) ,"1" (buff)
 408                 : "ax", "cx", "si", "bx" );
 409   }
 410   if (len & 2) {
 411         __asm__("\t lodsw\n"
 412                 "\t addw %%ax, %%bx\n"
 413                 "\t adcw $0, %%bx\n"
 414                 : "=b" (sum), "=S" (buff)
 415                 : "0" (sum), "1" (buff)
 416                 : "bx", "ax", "si");
 417   }
 418   if (len & 1) {
 419         __asm__("\t lodsb\n"
 420                 "\t movb $0, %%ah\n"
 421                 "\t addw %%ax, %%bx\n"
 422                 "\t adcw $0, %%bx\n"
 423                 : "=b" (sum), "=S" (buff)
 424                 : "0" (sum), "1" (buff)
 425                 : "bx", "ax", "si");
 426   }
 427   sum =~sum;
 428   return(sum & 0xffff);
 429 }
 430 
 431 
 432 /* Check the header of an incoming IP datagram. */
 433 int
 434 ip_csum(struct iphdr *iph)
     /* [previous][next][first][last][top][bottom][index][help] */
 435 {
 436   if (iph->check == 0) return(0);
 437   if (ip_compute_csum((unsigned char *)iph, iph->ihl*4) == 0) return(0);
 438   return(1);
 439 }
 440 
 441 
 442 /* Generate a checksym for an outgoing IP datagram. */
 443 static void
 444 ip_send_check(struct iphdr *iph)
     /* [previous][next][first][last][top][bottom][index][help] */
 445 {
 446    iph->check = 0;
 447    iph->check = ip_compute_csum((unsigned char *)iph, iph->ihl*4);
 448 }
 449 
 450 
 451 /* Forward an IP datagram to its next destination. */
 452 static void
 453 ip_forward(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 454 {
 455   struct device *dev2;
 456   struct iphdr *iph;
 457   struct sk_buff *skb2;
 458   struct rtable *rt;
 459   unsigned char *ptr;
 460   unsigned long raddr;
 461 
 462   /*
 463    * According to the RFC, we must first decrease the TTL field. If
 464    * that reaches zero, we must reply an ICMP control message telling
 465    * that the packet's lifetime expired.
 466    */
 467   iph = skb->h.iph;
 468   iph->ttl--;
 469   if (iph->ttl <= 0) {
 470         DPRINTF((DBG_IP, "\nIP: *** datagram expired: TTL=0 (ignored) ***\n"));
 471         DPRINTF((DBG_IP, "    SRC = %s   ", in_ntoa(iph->saddr)));
 472         DPRINTF((DBG_IP, "    DST = %s (ignored)\n", in_ntoa(iph->daddr)));
 473 
 474         /* Tell the sender its packet died... */
 475         icmp_send(skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, dev);
 476         return;
 477   }
 478 
 479   /* Re-compute the IP header checksum. */
 480   ip_send_check(iph);
 481 
 482   /*
 483    * OK, the packet is still valid.  Fetch its destination address,
 484    * and give it to the IP sender for further processing.
 485    */
 486   rt = rt_route(iph->daddr, NULL);
 487   if (rt == NULL) {
 488         DPRINTF((DBG_IP, "\nIP: *** routing (phase I) failed ***\n"));
 489 
 490         /* Tell the sender its packet cannot be delivered... */
 491         icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_UNREACH, dev);
 492         return;
 493   }
 494 
 495   /*
 496    * Gosh.  Not only is the packet valid; we even know how to
 497    * forward it onto its final destination.  Can we say this
 498    * is being plain lucky?
 499    * If the router told us that there is no GW, use the dest.
 500    * IP address itself- we seem to be connected directly...
 501    */
 502   raddr = rt->rt_gateway;
 503   if (raddr != 0) {
 504         rt = rt_route(raddr, NULL);
 505         if (rt == NULL) {
 506                 DPRINTF((DBG_IP, "\nIP: *** routing (phase II) failed ***\n"));
 507 
 508                 /* Tell the sender its packet cannot be delivered... */
 509                 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, dev);
 510                 return;
 511         }
 512         if (rt->rt_gateway != 0) raddr = rt->rt_gateway;
 513   } else raddr = iph->daddr;
 514   dev2 = rt->rt_dev;
 515 
 516   /*
 517    * We now allocate a new buffer, and copy the datagram into it.
 518    * If the indicated interface is up and running, kick it.
 519    */
 520   DPRINTF((DBG_IP, "\nIP: *** fwd %s -> ", in_ntoa(iph->saddr)));
 521   DPRINTF((DBG_IP, "%s (via %s), LEN=%d\n",
 522                         in_ntoa(raddr), dev2->name, skb->len));
 523 
 524   if (dev2->flags & IFF_UP) {
 525         skb2 = (struct sk_buff *) kmalloc(sizeof(struct sk_buff) +
 526                        dev2->hard_header_len + skb->len, GFP_ATOMIC);
 527         if (skb2 == NULL) {
 528                 printk("\nIP: No memory available for IP forward\n");
 529                 return;
 530         }
 531         ptr = (unsigned char *)(skb2 + 1);
 532         skb2->lock = 0;
 533         skb2->sk = NULL;
 534         skb2->len = skb->len + dev2->hard_header_len;
 535         skb2->mem_addr = skb2;
 536         skb2->mem_len = sizeof(struct sk_buff) + skb2->len;
 537         skb2->next = NULL;
 538         skb2->h.raw = ptr;
 539 
 540         /* Copy the packet data into the new buffer. */
 541         skb2->h.raw = ptr;
 542         memcpy(ptr + dev2->hard_header_len, skb->h.raw, skb->len);
 543                 
 544         /* Now build the MAC header. */
 545         (void) ip_send(skb2, raddr, skb->len, dev2, dev2->pa_addr);
 546 
 547         dev2->queue_xmit(skb2, dev2, SOPRI_NORMAL);
 548   }
 549 }
 550 
 551 
 552 /* This function receives all incoming IP datagrams. */
 553 int
 554 ip_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
     /* [previous][next][first][last][top][bottom][index][help] */
 555 {
 556   struct iphdr *iph;
 557   unsigned char hash;
 558   unsigned char flag = 0;
 559   struct inet_protocol *ipprot;
 560   static struct options opt; /* since we don't use these yet, and they
 561                                 take up stack space. */
 562   int brd;
 563 
 564   iph = skb->h.iph;
 565   memset((char *) &opt, 0, sizeof(opt));
 566   DPRINTF((DBG_IP, "<<\n"));
 567   ip_print(iph);
 568 
 569   /* Is the datagram acceptable? */
 570   if (ip_csum(iph) || do_options(iph, &opt) || iph->version != 4) {
 571         DPRINTF((DBG_IP, "\nIP: *** datagram error ***\n"));
 572         DPRINTF((DBG_IP, "    SRC = %s   ", in_ntoa(iph->saddr)));
 573         DPRINTF((DBG_IP, "    DST = %s (ignored)\n", in_ntoa(iph->daddr)));
 574         skb->sk = NULL;
 575         kfree_skb(skb, FREE_WRITE);
 576         return(0);
 577   }
 578 
 579   /* Do any IP forwarding required. */
 580   if ((brd = chk_addr(iph->daddr)) == 0) {
 581         ip_forward(skb, dev);
 582         skb->sk = NULL;
 583         kfree_skb(skb, FREE_WRITE);
 584         return(0);
 585   }
 586 
 587   /*
 588    * Deal with fragments: not really...
 589    * Fragmentation is definitely a required part of IP (yeah, guys,
 590    * I read Linux-Activists.NET too :-), but the current "sk_buff"
 591    * allocation stuff doesn't make things simpler.  When we're all
 592    * done cleaning up the mess, we'll add Ross Biro's "mbuf" stuff
 593    * to the code, which will replace the sk_buff stuff completely.
 594    * That will (a) make the code even cleaner, (b) allow me to do
 595    * the DDI (Device Driver Interface) the way I want to, and (c),
 596    * it will allow for easy addition of fragging.  Any takers? -FvK
 597    */
 598   if ((iph->frag_off & 32) || (ntohs(iph->frag_off) & 0x1fff)) {
 599         printk("\nIP: *** datagram fragmentation not yet implemented ***\n");
 600         printk("    SRC = %s   ", in_ntoa(iph->saddr));
 601         printk("    DST = %s (ignored)\n", in_ntoa(iph->daddr));
 602         icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, dev);
 603         skb->sk = NULL;
 604         kfree_skb(skb, FREE_WRITE);
 605         return(0);
 606   }
 607 
 608   /* Point into the IP datagram, just past the header. */
 609   skb->h.raw += iph->ihl*4;
 610   hash = iph->protocol & (MAX_INET_PROTOS -1);
 611   for (ipprot = (struct inet_protocol *)inet_protos[hash];
 612        ipprot != NULL;
 613        ipprot=(struct inet_protocol *)ipprot->next)
 614     {
 615        struct sk_buff *skb2;
 616 
 617        if (ipprot->protocol != iph->protocol) continue;
 618        DPRINTF((DBG_IP, "Using protocol = %X:\n", ipprot));
 619        print_ipprot(ipprot);
 620 
 621        /*
 622         * See if we need to make a copy of it.  This will
 623         * only be set if more than one protpocol wants it. 
 624         * and then not for the last one.
 625         */
 626        if (ipprot->copy) {
 627                 skb2 = (struct sk_buff *) kmalloc (skb->mem_len, GFP_ATOMIC);
 628                 if (skb2 == NULL) continue;
 629                 memcpy(skb2, skb, skb->mem_len);
 630                 skb2->mem_addr = skb2;
 631                 skb2->lock = 0;
 632                 skb2->h.raw = (unsigned char *)(
 633                                 (unsigned long)skb2 +
 634                                 (unsigned long) skb->h.raw -
 635                                 (unsigned long)skb);
 636         } else {
 637                 skb2 = skb;
 638         }
 639         flag = 1;
 640 
 641        /*
 642         * Pass on the datagram to each protocol that wants it,
 643         * based on the datagram protocol.  We should really
 644         * check the protocol handler's return values here...
 645         */
 646         ipprot->handler(skb2, dev, &opt, iph->daddr,
 647                         (ntohs(iph->tot_len) - (iph->ihl * 4)),
 648                         iph->saddr, 0, ipprot);
 649 
 650   }
 651 
 652   /*
 653    * All protocols checked.
 654    * If this packet was a broadcast, we may *not* reply to it, since that
 655    * causes (proven, grin) ARP storms and a leakage of memory (i.e. all
 656    * ICMP reply messages get queued up for transmission...)
 657    */
 658   if (!flag) {
 659         if (brd != IS_BROADCAST)
 660                 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, dev);
 661         skb->sk = NULL;
 662         kfree_skb(skb, FREE_WRITE);
 663   }
 664 
 665   return(0);
 666 }
 667 
 668 
 669 /*
 670  * Queues a packet to be sent, and starts the transmitter
 671  * if necessary.  if free = 1 then we free the block after
 672  * transmit, otherwise we don't.
 673  * This routine also needs to put in the total length, and
 674  * compute the checksum.
 675  */
 676 void
 677 ip_queue_xmit(struct sock *sk, struct device *dev, 
     /* [previous][next][first][last][top][bottom][index][help] */
 678               struct sk_buff *skb, int free)
 679 {
 680   struct iphdr *iph;
 681   unsigned char *ptr;
 682 
 683   if (sk == NULL) free = 1;
 684   if (dev == NULL) {
 685         printk("IP: ip_queue_xmit dev = NULL\n");
 686         return;
 687   }
 688   skb->free = free;
 689   skb->dev = dev;
 690   skb->when = jiffies;
 691 
 692   DPRINTF((DBG_IP, ">>\n"));
 693   ptr = (unsigned char *)(skb + 1);
 694   ptr += dev->hard_header_len;
 695   iph = (struct iphdr *)ptr;
 696   iph->tot_len = ntohs(skb->len - dev->hard_header_len);
 697   ip_send_check(iph);
 698   ip_print(iph);
 699   skb->next = NULL;
 700 
 701   /* See if this is the one trashing our queue. Ross? */
 702   skb->magic = 1;
 703   if (!free) {
 704         skb->link3 = NULL;
 705         sk->packets_out++;
 706         cli();
 707         if (sk->send_head == NULL) {
 708                 sk->send_tail = skb;
 709                 sk->send_head = skb;
 710         } else {
 711                 /* See if we've got a problem. */
 712                 if (sk->send_tail == NULL) {
 713                         printk("IP: ***bug sk->send_tail == NULL != sk->send_head\n");
 714                         sort_send(sk);
 715                 } else {
 716                         sk->send_tail->link3 = skb;
 717                         sk->send_tail = skb;
 718                 }
 719         }
 720         sti();
 721         sk->time_wait.len = backoff(sk->backoff) * (2 * sk->mdev + sk->rtt);
 722         sk->timeout = TIME_WRITE;
 723         reset_timer ((struct timer *)&sk->time_wait);
 724   } else {
 725         skb->sk = sk;
 726   }
 727 
 728   /* If the indicated interface is up and running, kick it. */
 729   if (dev->flags & IFF_UP) {
 730         if (sk != NULL) {
 731                 dev->queue_xmit(skb, dev, sk->priority);
 732         } else {
 733                 dev->queue_xmit(skb, dev, SOPRI_NORMAL);
 734         }
 735   } else {
 736         if (free) kfree_skb(skb, FREE_WRITE);
 737   }
 738 }
 739 
 740 
 741 void
 742 ip_retransmit(struct sock *sk, int all)
     /* [previous][next][first][last][top][bottom][index][help] */
 743 {
 744   struct sk_buff * skb;
 745   struct proto *prot;
 746   struct device *dev;
 747 
 748   prot = sk->prot;
 749   skb = sk->send_head;
 750   while (skb != NULL) {
 751         dev = skb->dev;
 752 
 753         /*
 754          * The rebuild_header function sees if the ARP is done.
 755          * If not it sends a new ARP request, and if so it builds
 756          * the header.
 757          */
 758         if (!skb->arp) {
 759                 if (dev->rebuild_header((struct enet_header *)(skb+1),dev)) {
 760                         if (!all) break;
 761                         skb = (struct sk_buff *)skb->link3;
 762                         continue;
 763                 }
 764         }
 765         skb->arp = 1;
 766         skb->when = jiffies;
 767 
 768         /* If the interface is (still) up and running, kick it. */
 769         if (dev->flags & IFF_UP) {
 770                 if (sk) dev->queue_xmit(skb, dev, sk->priority);
 771                   else dev->queue_xmit(skb, dev, SOPRI_NORMAL );
 772         }
 773 
 774         sk->retransmits++;
 775         sk->prot->retransmits ++;
 776         if (!all) break;
 777 
 778         /* This should cut it off before we send too many packets. */
 779         if (sk->retransmits > sk->cong_window) break;
 780         skb = (struct sk_buff *)skb->link3;
 781   }
 782 
 783   /*
 784    * Double the RTT time every time we retransmit. 
 785    * This will cause exponential back off on how hard we try to
 786    * get through again.  Once we get through, the rtt will settle
 787    * back down reasonably quickly.
 788    */
 789   sk->backoff++;
 790   sk->time_wait.len = backoff(sk->backoff) * (2 * sk->mdev + sk->rtt);
 791   sk->timeout = TIME_WRITE;
 792   reset_timer((struct timer *)&sk->time_wait);
 793 
 794 }
 795 
 796 /* Backoff function - the subject of much research */
 797 int backoff(int n)
     /* [previous][next][first][last][top][bottom][index][help] */
 798 {
 799         /* Use binary exponential up to retry #4, and quadratic after that
 800          * This yields the sequence
 801          * 1, 2, 4, 8, 16, 25, 36, 49, 64, 81, 100 ...
 802          */
 803 
 804         if(n <= 4)
 805                 return 1 << n;  /* Binary exponential back off */
 806         else
 807                 return n * n;   /* Quadratic back off */
 808 }

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