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

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