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_frag_create
  16. ip_find
  17. ip_free
  18. ip_expire
  19. ip_create
  20. ip_done
  21. ip_glue
  22. ip_defrag
  23. ip_fragment
  24. ip_forward
  25. ip_rcv
  26. ip_queue_xmit
  27. ip_retransmit
  28. 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  * Fixes:
  15  *              Alan Cox        :       Commented a couple of minor bits of surplus code
  16  *              Alan Cox        :       Undefining IP_FORWARD doesn't include the code
  17  *                                      (just stops a compiler warning).
  18  *              Alan Cox        :       Frames with >=MAX_ROUTE record routes, strict routes or loose routes
  19  *                                      are junked rather than corrupting things.
  20  *              Alan Cox        :       Frames to bad broadcast subnets are dumped
  21  *                                      We used to process them non broadcast and
  22  *                                      boy could that cause havoc.
  23  *              Alan Cox        :       ip_forward sets the free flag on the 
  24  *                                      new frame it queues. Still crap because
  25  *                                      it copies the frame but at least it 
  26  *                                      doesn't eat memory too.
  27  *              Alan Cox        :       Generic queue code and memory fixes.
  28  *              Fred Van Kempen :       IP fragment support (borrowed from NET2E)
  29  *              Gerhard Koerting:       Forward fragmented frames correctly.
  30  *              Gerhard Koerting:       Fixes to my fix of the above 8-).
  31  *              Gerhard Koerting:       IP interface addressing fix.
  32  *
  33  * To Fix:
  34  *              IP option processing is mostly not needed. ip_forward needs to know about routing rules
  35  *              and time stamp but that's about all.
  36  *
  37  *              This program is free software; you can redistribute it and/or
  38  *              modify it under the terms of the GNU General Public License
  39  *              as published by the Free Software Foundation; either version
  40  *              2 of the License, or (at your option) any later version.
  41  */
  42 #include <asm/segment.h>
  43 #include <asm/system.h>
  44 #include <linux/types.h>
  45 #include <linux/kernel.h>
  46 #include <linux/sched.h>
  47 #include <linux/string.h>
  48 #include <linux/errno.h>
  49 #include <linux/socket.h>
  50 #include <linux/sockios.h>
  51 #include <linux/in.h>
  52 #include "inet.h"
  53 #include "dev.h"
  54 #include "eth.h"
  55 #include "ip.h"
  56 #include "protocol.h"
  57 #include "route.h"
  58 #include "tcp.h"
  59 #include "skbuff.h"
  60 #include "sock.h"
  61 #include "arp.h"
  62 #include "icmp.h"
  63 
  64 #define CONFIG_IP_FORWARD
  65 #define CONFIG_IP_DEFRAG
  66 
  67 extern int last_retran;
  68 extern void sort_send(struct sock *sk);
  69 
  70 void
  71 ip_print(struct iphdr *ip)
     /* [previous][next][first][last][top][bottom][index][help] */
  72 {
  73   unsigned char buff[32];
  74   unsigned char *ptr;
  75   int addr, len, i;
  76 
  77   if (inet_debug != DBG_IP) return;
  78 
  79   /* Dump the IP header. */
  80   printk("IP: ihl=%d, version=%d, tos=%d, tot_len=%d\n",
  81            ip->ihl, ip->version, ip->tos, ntohs(ip->tot_len));
  82   printk("    id=%X, ttl=%d, prot=%d, check=%X\n",
  83            ip->id, ip->ttl, ip->protocol, ip->check);
  84   printk("    frag_off=%d\n", ip->frag_off);
  85   printk("    soucre=%s ", in_ntoa(ip->saddr));
  86   printk("dest=%s\n", in_ntoa(ip->daddr));
  87   printk("    ----\n");
  88 
  89   /* Dump the data. */
  90   ptr = (unsigned char *)(ip + 1);
  91   addr = 0;
  92   len = ntohs(ip->tot_len) - (4 * ip->ihl);
  93   while (len > 0) {
  94         printk("    %04X: ", addr);
  95         for(i = 0; i < 16; i++) {
  96                 if (len > 0) {
  97                         printk("%02X ", (*ptr & 0xFF));
  98                         buff[i] = *ptr++;
  99                         if (buff[i] < 32 || buff[i] > 126) buff[i] = '.';
 100                 } else {
 101                         printk("   ");
 102                         buff[i] = ' ';
 103                 }
 104                 addr++;
 105                 len--;
 106         };
 107         buff[i] = '\0';
 108         printk("  \"%s\"\n", buff);
 109   }
 110   printk("    ----\n\n");
 111 }
 112 
 113 
 114 int
 115 ip_ioctl(struct sock *sk, int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 116 {
 117   switch(cmd) {
 118         case DDIOCSDBG:
 119                 return(dbg_ioctl((void *) arg, DBG_IP));
 120         default:
 121                 return(-EINVAL);
 122   }
 123 }
 124 
 125 
 126 /* these two routines will do routining. */
 127 static void
 128 strict_route(struct iphdr *iph, struct options *opt)
     /* [previous][next][first][last][top][bottom][index][help] */
 129 {
 130 }
 131 
 132 
 133 static void
 134 loose_route(struct iphdr *iph, struct options *opt)
     /* [previous][next][first][last][top][bottom][index][help] */
 135 {
 136 }
 137 
 138 
 139 static void
 140 print_ipprot(struct inet_protocol *ipprot)
     /* [previous][next][first][last][top][bottom][index][help] */
 141 {
 142   DPRINTF((DBG_IP, "handler = %X, protocol = %d, copy=%d \n",
 143            ipprot->handler, ipprot->protocol, ipprot->copy));
 144 }
 145 
 146 
 147 /* This routine will check to see if we have lost a gateway. */
 148 void
 149 ip_route_check(unsigned long daddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 150 {
 151 }
 152 
 153 
 154 #if 0
 155 /* this routine puts the options at the end of an ip header. */
 156 static int
 157 build_options(struct iphdr *iph, struct options *opt)
     /* [previous][next][first][last][top][bottom][index][help] */
 158 {
 159   unsigned char *ptr;
 160   /* currently we don't support any options. */
 161   ptr = (unsigned char *)(iph+1);
 162   *ptr = 0;
 163   return (4);
 164 }
 165 #endif
 166 
 167 
 168 /* Take an skb, and fill in the MAC header. */
 169 static int
 170 ip_send(struct sk_buff *skb, unsigned long daddr, int len, struct device *dev,
     /* [previous][next][first][last][top][bottom][index][help] */
 171         unsigned long saddr)
 172 {
 173   unsigned char *ptr;
 174   int mac;
 175 
 176   ptr = (unsigned char *)(skb + 1);
 177   mac = 0;
 178   skb->arp = 1;
 179   if (dev->hard_header) {
 180         mac = dev->hard_header(ptr, dev, ETH_P_IP, daddr, saddr, len);
 181   }
 182   if (mac < 0) {
 183         mac = -mac;
 184         skb->arp = 0;
 185   }
 186   skb->dev = dev;
 187   return(mac);
 188 }
 189 
 190 
 191 /*
 192  * This routine builds the appropriate hardware/IP headers for
 193  * the routine.  It assumes that if *dev != NULL then the
 194  * protocol knows what it's doing, otherwise it uses the
 195  * routing/ARP tables to select a device struct.
 196  */
 197 int
 198 ip_build_header(struct sk_buff *skb, unsigned long saddr, unsigned long daddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 199                 struct device **dev, int type, struct options *opt, int len)
 200 {
 201   static struct options optmem;
 202   struct iphdr *iph;
 203   struct rtable *rt;
 204   unsigned char *buff;
 205   unsigned long raddr;
 206   static int count = 0;
 207   int tmp;
 208 
 209   if (saddr == 0) 
 210         saddr = my_addr();
 211         
 212   DPRINTF((DBG_IP, "ip_build_header (skb=%X, saddr=%X, daddr=%X, *dev=%X,\n"
 213            "                 type=%d, opt=%X, len = %d)\n",
 214            skb, saddr, daddr, *dev, type, opt, len));
 215            
 216   buff = (unsigned char *)(skb + 1);
 217 
 218   /* See if we need to look up the device. */
 219   if (*dev == NULL) {
 220         rt = rt_route(daddr, &optmem);
 221         if (rt == NULL) 
 222                 return(-ENETUNREACH);
 223 
 224         *dev = rt->rt_dev;
 225         if (saddr == 0x0100007FL && daddr != 0x0100007FL) 
 226                 saddr = rt->rt_dev->pa_addr;
 227         raddr = rt->rt_gateway;
 228 
 229         DPRINTF((DBG_IP, "ip_build_header: saddr set to %s\n", in_ntoa(saddr)));
 230         opt = &optmem;
 231   } else {
 232         /* We still need the address of the first hop. */
 233         rt = rt_route(daddr, &optmem);
 234         raddr = (rt == NULL) ? 0 : rt->rt_gateway;
 235   }
 236   if (raddr == 0)
 237         raddr = daddr;
 238 
 239   /* Now build the MAC header. */
 240   tmp = ip_send(skb, raddr, len, *dev, saddr);
 241   buff += tmp;
 242   len -= tmp;
 243 
 244   skb->dev = *dev;
 245   skb->saddr = saddr;
 246   if (skb->sk) skb->sk->saddr = saddr;
 247 
 248   /* Now build the IP header. */
 249 
 250   /* If we are using IPPROTO_RAW, then we don't need an IP header, since
 251      one is being supplied to us by the user */
 252 
 253   if(type == IPPROTO_RAW) return (tmp);
 254 
 255   iph = (struct iphdr *)buff;
 256   iph->version  = 4;
 257   iph->tos      = 0;
 258   iph->frag_off = 0;
 259   iph->ttl      = 32;
 260   iph->daddr    = daddr;
 261   iph->saddr    = saddr;
 262   iph->protocol = type;
 263   iph->ihl      = 5;
 264   iph->id       = htons(count++);
 265 
 266   /* Setup the IP options. */
 267 #ifdef Not_Yet_Avail
 268   build_options(iph, opt);
 269 #endif
 270 
 271   return(20 + tmp);     /* IP header plus MAC header size */
 272 }
 273 
 274 
 275 static int
 276 do_options(struct iphdr *iph, struct options *opt)
     /* [previous][next][first][last][top][bottom][index][help] */
 277 {
 278   unsigned char *buff;
 279   int done = 0;
 280   int i, len = sizeof(struct iphdr);
 281 
 282   /* Zero out the options. */
 283   opt->record_route.route_size = 0;
 284   opt->loose_route.route_size  = 0;
 285   opt->strict_route.route_size = 0;
 286   opt->tstamp.ptr              = 0;
 287   opt->security                = 0;
 288   opt->compartment             = 0;
 289   opt->handling                = 0;
 290   opt->stream                  = 0;
 291   opt->tcc                     = 0;
 292   return(0);
 293 
 294   /* Advance the pointer to start at the options. */
 295   buff = (unsigned char *)(iph + 1);
 296 
 297   /* Now start the processing. */
 298   while (!done && len < iph->ihl*4) switch(*buff) {
 299         case IPOPT_END:
 300                 done = 1;
 301                 break;
 302         case IPOPT_NOOP:
 303                 buff++;
 304                 len++;
 305                 break;
 306         case IPOPT_SEC:
 307                 buff++;
 308                 if (*buff != 11) return(1);
 309                 buff++;
 310                 opt->security = ntohs(*(unsigned short *)buff);
 311                 buff += 2;
 312                 opt->compartment = ntohs(*(unsigned short *)buff);
 313                 buff += 2;
 314                 opt->handling = ntohs(*(unsigned short *)buff);
 315                 buff += 2;
 316                 opt->tcc = ((*buff) << 16) + ntohs(*(unsigned short *)(buff+1));
 317                 buff += 3;
 318                 len += 11;
 319                 break;
 320         case IPOPT_LSRR:
 321                 buff++;
 322                 if ((*buff - 3)% 4 != 0) return(1);
 323                 len += *buff;
 324                 opt->loose_route.route_size = (*buff -3)/4;
 325                 buff++;
 326                 if (*buff % 4 != 0) return(1);
 327                 opt->loose_route.pointer = *buff/4 - 1;
 328                 buff++;
 329                 buff++;
 330                 for (i = 0; i < opt->loose_route.route_size; i++) {
 331                         if(i>=MAX_ROUTE)
 332                                 return(1);
 333                         opt->loose_route.route[i] = *(unsigned long *)buff;
 334                         buff += 4;
 335                 }
 336                 break;
 337         case IPOPT_SSRR:
 338                 buff++;
 339                 if ((*buff - 3)% 4 != 0) return(1);
 340                 len += *buff;
 341                 opt->strict_route.route_size = (*buff -3)/4;
 342                 buff++;
 343                 if (*buff % 4 != 0) return(1);
 344                 opt->strict_route.pointer = *buff/4 - 1;
 345                 buff++;
 346                 buff++;
 347                 for (i = 0; i < opt->strict_route.route_size; i++) {
 348                         if(i>=MAX_ROUTE)
 349                                 return(1);
 350                         opt->strict_route.route[i] = *(unsigned long *)buff;
 351                         buff += 4;
 352                 }
 353                 break;
 354         case IPOPT_RR:
 355                 buff++;
 356                 if ((*buff - 3)% 4 != 0) return(1);
 357                 len += *buff;
 358                 opt->record_route.route_size = (*buff -3)/4;
 359                 buff++;
 360                 if (*buff % 4 != 0) return(1);
 361                 opt->record_route.pointer = *buff/4 - 1;
 362                 buff++;
 363                 buff++;
 364                 for (i = 0; i < opt->record_route.route_size; i++) {
 365                         if(i>=MAX_ROUTE)
 366                                 return 1;
 367                         opt->record_route.route[i] = *(unsigned long *)buff;
 368                         buff += 4;
 369                 }
 370                 break;
 371         case IPOPT_SID:
 372                 len += 4;
 373                 buff +=2;
 374                 opt->stream = *(unsigned short *)buff;
 375                 buff += 2;
 376                 break;
 377         case IPOPT_TIMESTAMP:
 378                 buff++;
 379                 len += *buff;
 380                 if (*buff % 4 != 0) return(1);
 381                 opt->tstamp.len = *buff / 4 - 1;
 382                 buff++;
 383                 if ((*buff - 1) % 4 != 0) return(1);
 384                 opt->tstamp.ptr = (*buff-1)/4;
 385                 buff++;
 386                 opt->tstamp.x.full_char = *buff;
 387                 buff++;
 388                 for (i = 0; i < opt->tstamp.len; i++) {
 389                         opt->tstamp.data[i] = *(unsigned long *)buff;
 390                         buff += 4;
 391                 }
 392                 break;
 393         default:
 394                 return(1);
 395   }
 396 
 397   if (opt->record_route.route_size == 0) {
 398         if (opt->strict_route.route_size != 0) {
 399                 memcpy(&(opt->record_route), &(opt->strict_route),
 400                                              sizeof(opt->record_route));
 401         } else if (opt->loose_route.route_size != 0) {
 402                 memcpy(&(opt->record_route), &(opt->loose_route),
 403                                              sizeof(opt->record_route));
 404         }
 405   }
 406 
 407   if (opt->strict_route.route_size != 0 &&
 408       opt->strict_route.route_size != opt->strict_route.pointer) {
 409         strict_route(iph, opt);
 410         return(0);
 411   }
 412 
 413   if (opt->loose_route.route_size != 0 &&
 414       opt->loose_route.route_size != opt->loose_route.pointer) {
 415         loose_route(iph, opt);
 416         return(0);
 417   }
 418 
 419   return(0);
 420 }
 421 
 422 /* This is a version of ip_compute_csum() optimized for IP headers, which
 423    always checksum on 4 octet boundaries. */
 424 static inline unsigned short
 425 ip_fast_csum(unsigned char * buff, int wlen)
     /* [previous][next][first][last][top][bottom][index][help] */
 426 {
 427     unsigned long sum = 0;
 428 
 429     if (wlen)
 430          __asm__("clc\n"
 431                 "1:\t"
 432                 "lodsl\n\t"
 433                 "adcl %%eax, %0\n\t"
 434                 "decl %2\n\t"
 435                 "jne 1b\n\t"
 436                 "adcl $0, %0\n\t"
 437                 "movl %0, %%eax\n\t"
 438                 "shrl $16, %%eax\n\t"
 439                 "addw %%ax, %w0\n\t"
 440                 "adcw $0, %w0"
 441             : "=r" (sum), "=S" (buff), "=r" (wlen)
 442             : "0"  (sum),  "1" (buff),  "2" (wlen)
 443             : "ax" );
 444     return (~sum) & 0xffff;
 445 }
 446 
 447 /*
 448  * This routine does all the checksum computations that don't
 449  * require anything special (like copying or special headers).
 450  */
 451 unsigned short
 452 ip_compute_csum(unsigned char * buff, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 453 {
 454   unsigned long sum = 0;
 455 
 456   /* Do the first multiple of 4 bytes and convert to 16 bits. */
 457   if (len > 3) {
 458         __asm__("clc\n"
 459                 "1:\t"
 460                 "lodsl\n\t"
 461                 "adcl %%eax, %%ebx\n\t"
 462                 "loop 1b\n\t"
 463                 "adcl $0, %%ebx\n\t"
 464                 "movl %%ebx, %%eax\n\t"
 465                 "shrl $16, %%eax\n\t"
 466                 "addw %%ax, %%bx\n\t"
 467                 "adcw $0, %%bx"
 468                 : "=b" (sum) , "=S" (buff)
 469                 : "0" (sum), "c" (len >> 2) ,"1" (buff)
 470                 : "ax", "cx", "si", "bx" );
 471   }
 472   if (len & 2) {
 473         __asm__("lodsw\n\t"
 474                 "addw %%ax, %%bx\n\t"
 475                 "adcw $0, %%bx"
 476                 : "=b" (sum), "=S" (buff)
 477                 : "0" (sum), "1" (buff)
 478                 : "bx", "ax", "si");
 479   }
 480   if (len & 1) {
 481         __asm__("lodsb\n\t"
 482                 "movb $0, %%ah\n\t"
 483                 "addw %%ax, %%bx\n\t"
 484                 "adcw $0, %%bx"
 485                 : "=b" (sum), "=S" (buff)
 486                 : "0" (sum), "1" (buff)
 487                 : "bx", "ax", "si");
 488   }
 489   sum =~sum;
 490   return(sum & 0xffff);
 491 }
 492 
 493 /* Check the header of an incoming IP datagram.  This version is still used in slhc.c. */
 494 int
 495 ip_csum(struct iphdr *iph)
     /* [previous][next][first][last][top][bottom][index][help] */
 496 {
 497   return ip_fast_csum((unsigned char *)iph, iph->ihl);
 498 }
 499 
 500 /* Generate a checksym for an outgoing IP datagram. */
 501 static void
 502 ip_send_check(struct iphdr *iph)
     /* [previous][next][first][last][top][bottom][index][help] */
 503 {
 504    iph->check = 0;
 505    iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
 506 }
 507 
 508 /************************ Fragment Handlers From NET2E not yet with tweaks to beat 4K **********************************/
 509 
 510 static struct ipq *ipqueue = NULL;              /* IP fragment queue    */
 511  /* Create a new fragment entry. */
 512 static struct ipfrag *ip_frag_create(int offset, int end, struct sk_buff *skb, unsigned char *ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 513 {
 514         struct ipfrag *fp;
 515  
 516         fp = (struct ipfrag *) kmalloc(sizeof(struct ipfrag), GFP_ATOMIC);
 517         if (fp == NULL) 
 518         {
 519                 printk("IP: frag_create: no memory left !\n");
 520                 return(NULL);
 521         }
 522         memset(fp, 0, sizeof(struct ipfrag));
 523 
 524         /* Fill in the structure. */
 525         fp->offset = offset;
 526         fp->end = end;
 527         fp->len = end - offset;
 528         fp->skb = skb;
 529         fp->ptr = ptr;
 530  
 531         return(fp);
 532 }
 533  
 534  
 535 /*
 536  * Find the correct entry in the "incomplete datagrams" queue for
 537  * this IP datagram, and return the queue entry address if found.
 538  */
 539 static struct ipq *ip_find(struct iphdr *iph)
     /* [previous][next][first][last][top][bottom][index][help] */
 540 {
 541         struct ipq *qp;
 542         struct ipq *qplast;
 543  
 544         cli();
 545         qplast = NULL;
 546         for(qp = ipqueue; qp != NULL; qplast = qp, qp = qp->next) 
 547         {
 548                 if (iph->id== qp->iph->id && iph->saddr == qp->iph->saddr &&
 549                         iph->daddr == qp->iph->daddr && iph->protocol == qp->iph->protocol) 
 550                 {
 551                         del_timer(&qp->timer);  /* So it doesnt vanish on us. The timer will be reset anyway */
 552                         sti();
 553                         return(qp);
 554                 }
 555         }
 556         sti();
 557         return(NULL);
 558 }
 559  
 560  
 561 /*
 562  * Remove an entry from the "incomplete datagrams" queue, either
 563  * because we completed, reassembled and processed it, or because
 564  * it timed out.
 565  */
 566 
 567 static void ip_free(struct ipq *qp)
     /* [previous][next][first][last][top][bottom][index][help] */
 568 {
 569         struct ipfrag *fp;
 570         struct ipfrag *xp;
 571 
 572         /* Stop the timer for this entry. */
 573 /*      printk("ip_free\n");*/
 574         del_timer(&qp->timer);
 575 
 576         /* Remove this entry from the "incomplete datagrams" queue. */
 577         cli();
 578         if (qp->prev == NULL) 
 579         {
 580                 ipqueue = qp->next;
 581                 if (ipqueue != NULL) 
 582                         ipqueue->prev = NULL;
 583         } 
 584         else 
 585         {
 586                 qp->prev->next = qp->next;
 587                 if (qp->next != NULL) 
 588                         qp->next->prev = qp->prev;
 589         }
 590  
 591         /* Release all fragment data. */
 592 /*      printk("ip_free: kill frag data\n");*/
 593         fp = qp->fragments;
 594         while (fp != NULL) 
 595         {
 596                 xp = fp->next;
 597                 IS_SKB(fp->skb);
 598                 kfree_skb(fp->skb,FREE_READ);
 599                 kfree_s(fp, sizeof(struct ipfrag));
 600                 fp = xp;
 601         }
 602         
 603 /*      printk("ip_free: cleanup\n");*/
 604  
 605         /* Release the MAC header. */
 606         kfree_s(qp->mac, qp->maclen);
 607  
 608         /* Release the IP header. */
 609         kfree_s(qp->iph, qp->ihlen + 8);
 610  
 611         /* Finally, release the queue descriptor itself. */
 612         kfree_s(qp, sizeof(struct ipq));
 613 /*      printk("ip_free:done\n");*/
 614         sti();
 615  }
 616  
 617  
 618  /* Oops- a fragment queue timed out.  Kill it and send an ICMP reply. */
 619  
 620 static void ip_expire(unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 621 {
 622         struct ipq *qp;
 623  
 624         qp = (struct ipq *)arg;
 625         DPRINTF((DBG_IP, "IP: queue_expire: fragment queue 0x%X timed out!\n", qp));
 626  
 627         /* Send an ICMP "Fragment Reassembly Timeout" message. */
 628 #if 0           
 629         icmp_send(qp->iph->ip_src.s_addr, ICMP_TIME_EXCEEDED,
 630                     ICMP_EXC_FRAGTIME, qp->iph);
 631 #endif           
 632         if(qp->fragments!=NULL)
 633                 icmp_send(qp->fragments->skb,ICMP_TIME_EXCEEDED,
 634                                 ICMP_EXC_FRAGTIME, qp->dev);
 635  
 636         /* Nuke the fragment queue. */
 637         ip_free(qp);
 638 }
 639  
 640  
 641 /*
 642  * Add an entry to the 'ipq' queue for a newly received IP datagram.
 643  * We will (hopefully :-) receive all other fragments of this datagram
 644  * in time, so we just create a queue for this datagram, in which we
 645  * will insert the received fragments at their respective positions.
 646  */
 647 
 648 static struct ipq *ip_create(struct sk_buff *skb, struct iphdr *iph, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 649 {
 650         struct ipq *qp;
 651         int maclen;
 652         int ihlen;
 653 
 654         qp = (struct ipq *) kmalloc(sizeof(struct ipq), GFP_ATOMIC);
 655         if (qp == NULL) 
 656         {
 657                 printk("IP: create: no memory left !\n");
 658                 return(NULL);
 659         }
 660         memset(qp, 0, sizeof(struct ipq));
 661 
 662         /* Allocate memory for the MAC header. */
 663         maclen = ((unsigned long) iph) - ((unsigned long) (skb + 1));
 664         qp->mac = (unsigned char *) kmalloc(maclen, GFP_ATOMIC);
 665         if (qp->mac == NULL) 
 666         {
 667                 printk("IP: create: no memory left !\n");
 668                 kfree_s(qp, sizeof(struct ipq));
 669                 return(NULL);
 670         }
 671 
 672         /* Allocate memory for the IP header (plus 8 octects for ICMP). */
 673         ihlen = (iph->ihl * sizeof(unsigned long));
 674         qp->iph = (struct iphdr *) kmalloc(ihlen + 8, GFP_ATOMIC);
 675         if (qp->iph == NULL) 
 676         {
 677                 printk("IP: create: no memory left !\n");
 678                 kfree_s(qp->mac, maclen);
 679                 kfree_s(qp, sizeof(struct ipq));
 680                 return(NULL);
 681         }
 682 
 683         /* Fill in the structure. */
 684         memcpy(qp->mac, (skb + 1), maclen);
 685         memcpy(qp->iph, iph, ihlen + 8);
 686         qp->len = 0;
 687         qp->ihlen = ihlen;
 688         qp->maclen = maclen;
 689         qp->fragments = NULL;
 690         qp->dev = dev;
 691 /*      printk("Protocol = %d\n",qp->iph->protocol);*/
 692         
 693         /* Start a timer for this entry. */
 694         qp->timer.expires = IP_FRAG_TIME;               /* about 30 seconds     */
 695         qp->timer.data = (unsigned long) qp;            /* pointer to queue     */
 696         qp->timer.function = ip_expire;                 /* expire function      */
 697         add_timer(&qp->timer);
 698 
 699         /* Add this entry to the queue. */
 700         qp->prev = NULL;
 701         cli();
 702         qp->next = ipqueue;
 703         if (qp->next != NULL) 
 704                 qp->next->prev = qp;
 705         ipqueue = qp;
 706         sti();
 707         return(qp);
 708 }
 709  
 710  
 711  /* See if a fragment queue is complete. */
 712 static int ip_done(struct ipq *qp)
     /* [previous][next][first][last][top][bottom][index][help] */
 713 {
 714         struct ipfrag *fp;
 715         int offset;
 716  
 717         /* Only possible if we received the final fragment. */
 718         if (qp->len == 0) 
 719                 return(0);
 720  
 721         /* Check all fragment offsets to see if they connect. */
 722         fp = qp->fragments;
 723         offset = 0;
 724         while (fp != NULL) 
 725         {
 726                 if (fp->offset > offset) 
 727                         return(0);      /* fragment(s) missing */
 728                 offset = fp->end;
 729                 fp = fp->next;
 730         }
 731  
 732         /* All fragments are present. */
 733         return(1);
 734  }
 735  
 736  
 737 /* Build a new IP datagram from all its fragments. */
 738 static struct sk_buff *ip_glue(struct ipq *qp)
     /* [previous][next][first][last][top][bottom][index][help] */
 739 {
 740         struct sk_buff *skb;
 741         struct iphdr *iph;
 742         struct ipfrag *fp;
 743         unsigned char *ptr;
 744         int count, len;
 745  
 746         /* Allocate a new buffer for the datagram. */
 747         len = sizeof(struct sk_buff)+qp->maclen + qp->ihlen + qp->len;
 748         if ((skb = alloc_skb(len,GFP_ATOMIC)) == NULL) 
 749         {
 750                 printk("IP: queue_glue: no memory for glueing queue 0x%X\n", (int) qp);
 751                 ip_free(qp);
 752                 return(NULL);
 753         }
 754  
 755         /* Fill in the basic details. */
 756         skb->len = (len - qp->maclen);
 757         skb->h.raw = (unsigned char *) (skb + 1);
 758         skb->free = 1;
 759         skb->lock = 1;
 760  
 761         /* Copy the original MAC and IP headers into the new buffer. */
 762         ptr = (unsigned char *) skb->h.raw;
 763         memcpy(ptr, ((unsigned char *) qp->mac), qp->maclen);
 764 /*      printk("Copied %d bytes of mac header.\n",qp->maclen);*/
 765         ptr += qp->maclen;
 766         memcpy(ptr, ((unsigned char *) qp->iph), qp->ihlen);
 767 /*      printk("Copied %d byte of ip header.\n",qp->ihlen);*/
 768         ptr += qp->ihlen;
 769         skb->h.raw += qp->maclen;
 770         
 771 /*      printk("Protocol = %d\n",skb->h.iph->protocol);*/
 772         count = 0;
 773  
 774         /* Copy the data portions of all fragments into the new buffer. */
 775         fp = qp->fragments;
 776         while(fp != NULL) 
 777         {
 778                 if(count+fp->len>skb->len)
 779                 {
 780                         printk("Invalid fragment list: Fragment over size.\n");
 781                         kfree_skb(skb,FREE_WRITE);
 782                         return NULL;
 783                 }
 784 /*              printk("Fragment %d size %d\n",fp->offset,fp->len);*/
 785                 memcpy((ptr + fp->offset), fp->ptr, fp->len);
 786                 count += fp->len;
 787                 fp = fp->next;
 788         }
 789  
 790         /* We glued together all fragments, so remove the queue entry. */
 791         ip_free(qp);
 792  
 793         /* Done with all fragments. Fixup the new IP header. */
 794         iph = skb->h.iph;
 795         iph->frag_off = 0;
 796         iph->tot_len = htons((iph->ihl * sizeof(unsigned long)) + count);
 797         return(skb);
 798 }
 799  
 800 
 801 /* Process an incoming IP datagram fragment. */
 802 static struct sk_buff *ip_defrag(struct iphdr *iph, struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 803 {
 804         struct ipfrag *prev, *next;
 805         struct ipfrag *tfp;
 806         struct ipq *qp;
 807         struct sk_buff *skb2;
 808         unsigned char *ptr;
 809         int flags, offset;
 810         int i, ihl, end;
 811 
 812         /* Find the entry of this IP datagram in the "incomplete datagrams" queue. */
 813         qp = ip_find(iph);
 814  
 815         /* Is this a non-fragmented datagram? */
 816         offset = ntohs(iph->frag_off);
 817         flags = offset & ~IP_OFFSET;
 818         offset &= IP_OFFSET;
 819         if (((flags & IP_MF) == 0) && (offset == 0)) 
 820         {
 821                 if (qp != NULL) 
 822                         ip_free(qp);    /* Huh? How could this exist?? */
 823                 return(skb);
 824         }
 825         offset <<= 3;           /* offset is in 8-byte chunks */
 826  
 827         /*
 828          * If the queue already existed, keep restarting its timer as long
 829          * as we still are receiving fragments.  Otherwise, create a fresh
 830          * queue entry.
 831          */
 832         if (qp != NULL) 
 833         {
 834                 del_timer(&qp->timer);
 835                 qp->timer.expires = IP_FRAG_TIME;       /* about 30 seconds     */
 836                 qp->timer.data = (unsigned long) qp;    /* pointer to queue     */
 837                 qp->timer.function = ip_expire;         /* expire function      */
 838                 add_timer(&qp->timer);
 839         } 
 840         else 
 841         {
 842                 if ((qp = ip_create(skb, iph, dev)) == NULL) 
 843                         return(NULL);
 844         }
 845  
 846         /* Determine the position of this fragment. */
 847         ihl = (iph->ihl * sizeof(unsigned long));
 848         end = offset + ntohs(iph->tot_len) - ihl;
 849  
 850         /* Point into the IP datagram 'data' part. */
 851         ptr = ((unsigned char *) (skb + 1)) + dev->hard_header_len + ihl;
 852  
 853         /* Is this the final fragment? */
 854         if ((flags & IP_MF) == 0) 
 855                 qp->len = end;
 856  
 857         /*
 858          * Find out which fragments are in front and at the back of us
 859          * in the chain of fragments so far.  We must know where to put
 860          * this fragment, right?
 861          */
 862         prev = NULL;
 863         for(next = qp->fragments; next != NULL; next = next->next) 
 864         {
 865                 if (next->offset > offset) 
 866                         break;  /* bingo! */
 867                 prev = next;
 868         }       
 869  
 870         /*
 871          * We found where to put this one.
 872          * Check for overlap with preceeding fragment, and, if needed,
 873          * align things so that any overlaps are eliminated.
 874          */
 875         if (prev != NULL && offset < prev->end) 
 876         {
 877                 i = prev->end - offset;
 878                 offset += i;    /* ptr into datagram */
 879                 ptr += i;       /* ptr into fragment data */
 880                 DPRINTF((DBG_IP, "IP: defrag: fixed low overlap %d bytes\n", i));
 881         }       
 882  
 883         /*
 884          * Look for overlap with succeeding segments.
 885          * If we can merge fragments, do it.
 886          */
 887    
 888         for(; next != NULL; next = tfp) 
 889         {
 890                 tfp = next->next;
 891                 if (next->offset >= end) 
 892                         break;          /* no overlaps at all */
 893  
 894                 i = end - next->offset;                 /* overlap is 'i' bytes */
 895                 next->len -= i;                         /* so reduce size of    */
 896                 next->offset += i;                      /* next fragment        */
 897                 next->ptr += i;
 898                 
 899                 /* If we get a frag size of <= 0, remove it. */
 900                 if (next->len <= 0) 
 901                 {
 902                         DPRINTF((DBG_IP, "IP: defrag: removing frag 0x%X (len %d)\n",
 903                                                         next, next->len));
 904                         if (next->prev != NULL) 
 905                                 next->prev->next = next->next;
 906                         else 
 907                                 qp->fragments = next->next;
 908                 
 909                         if (tfp->next != NULL) 
 910                                 next->next->prev = next->prev;
 911                         
 912                         kfree_s(next, sizeof(struct ipfrag));
 913                 }
 914                 DPRINTF((DBG_IP, "IP: defrag: fixed high overlap %d bytes\n", i));
 915         }
 916  
 917         /* Insert this fragment in the chain of fragments. */
 918         tfp = NULL;
 919         tfp = ip_frag_create(offset, end, skb, ptr);
 920         tfp->prev = prev;
 921         tfp->next = next;
 922         if (prev != NULL) 
 923                 prev->next = tfp;
 924         else 
 925                 qp->fragments = tfp;
 926    
 927         if (next != NULL) 
 928                 next->prev = tfp;
 929  
 930         /*
 931          * OK, so we inserted this new fragment into the chain.
 932          * Check if we now have a full IP datagram which we can
 933          * bump up to the IP layer...
 934          */
 935    
 936         if (ip_done(qp)) 
 937         {
 938                 skb2 = ip_glue(qp);             /* glue together the fragments */
 939                 return(skb2);
 940         }
 941         return(NULL);
 942  }
 943  
 944  
 945  /*
 946   * This IP datagram is too large to be sent in one piece.  Break it up into
 947   * smaller pieces (each of size equal to the MAC header plus IP header plus
 948   * a block of the data of the original IP data part) that will yet fit in a
 949   * single device frame, and queue such a frame for sending by calling the
 950   * ip_queue_xmit().  Note that this is recursion, and bad things will happen
 951   * if this function causes a loop...
 952   */
 953  void ip_fragment(struct sock *sk, struct sk_buff *skb, struct device *dev, int is_frag)
     /* [previous][next][first][last][top][bottom][index][help] */
 954  {
 955         struct iphdr *iph;
 956         unsigned char *raw;
 957         unsigned char *ptr;
 958         struct sk_buff *skb2;
 959         int left, mtu, hlen, len;
 960         int offset;
 961  
 962         /* Point into the IP datagram header. */
 963         raw = (unsigned char *) (skb + 1);
 964         iph = (struct iphdr *) (raw + dev->hard_header_len);
 965         
 966         /* Setup starting values. */
 967         hlen = (iph->ihl * sizeof(unsigned long));
 968         left = ntohs(iph->tot_len) - hlen;
 969         hlen += dev->hard_header_len;
 970         mtu = (dev->mtu - hlen);
 971         ptr = (raw + hlen);
 972         
 973         DPRINTF((DBG_IP, "IP: Fragmentation Desired\n"));
 974         DPRINTF((DBG_IP, "    DEV=%s, MTU=%d, LEN=%d SRC=%s",
 975                 dev->name, dev->mtu, left, in_ntoa(iph->saddr)));
 976         DPRINTF((DBG_IP, " DST=%s\n", in_ntoa(iph->daddr)));
 977  
 978         /* Check for any "DF" flag. */
 979         if (ntohs(iph->frag_off) & IP_DF) 
 980         {
 981                 DPRINTF((DBG_IP, "IP: Fragmentation Desired, but DF set !\n"));
 982                 DPRINTF((DBG_IP, "    DEV=%s, MTU=%d, LEN=%d SRC=%s",
 983                         dev->name, dev->mtu, left, in_ntoa(iph->saddr)));
 984                 DPRINTF((DBG_IP, " DST=%s\n", in_ntoa(iph->daddr)));
 985  
 986                 /*
 987                  * FIXME:
 988                  * We should send an ICMP warning message here!
 989                  */
 990                  
 991                 icmp_send(skb,ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, dev); 
 992                 return;
 993         }
 994  
 995         /* Fragment the datagram. */
 996         if (is_frag & 2)
 997           offset = (ntohs(iph->frag_off) & 0x1fff) << 3;
 998         else
 999           offset = 0;
1000         while(left > 0) 
1001         {
1002                 len = left;
1003                 if (len+8 > mtu) 
1004                         len = (dev->mtu - hlen - 8);
1005                 if ((left - len) >= 8) 
1006                 {
1007                         len /= 8;
1008                         len *= 8;
1009                 }
1010                 DPRINTF((DBG_IP,"IP: frag: creating fragment of %d bytes (%d total)\n",
1011                                                         len, len + hlen));
1012  
1013                 /* Allocate buffer. */
1014                 if ((skb2 = alloc_skb(sizeof(struct sk_buff) + len + hlen,GFP_KERNEL)) == NULL) 
1015                 {
1016                         printk("IP: frag: no memory for new fragment!\n");
1017                         return;
1018                 }
1019                 skb2->arp = skb->arp;
1020                 skb2->free = skb->free;
1021                 skb2->len = len + hlen;
1022                 skb2->h.raw=(char *)(skb2+1);
1023  
1024                 if (sk) 
1025                         sk->wmem_alloc += skb2->mem_len;
1026  
1027                 /* Copy the packet header into the new buffer. */
1028                 memcpy(skb2->h.raw, raw, hlen);
1029  
1030                 /* Copy a block of the IP datagram. */
1031                 memcpy(skb2->h.raw + hlen, ptr, len);
1032                 left -= len;
1033 
1034                 skb2->h.raw+=dev->hard_header_len; 
1035                 /* Fill in the new header fields. */
1036                 iph = (struct iphdr *)(skb2->h.raw/*+dev->hard_header_len*/);
1037                 iph->frag_off = htons((offset >> 3));
1038                 /* Added AC : If we are fragmenting a fragment thats not the
1039                    last fragment then keep MF on each bit */
1040                 if (left > 0 || (is_frag & 1)) 
1041                         iph->frag_off |= htons(IP_MF);
1042                 ptr += len;
1043                 offset += len;
1044 /*              printk("Queue frag\n");*/
1045  
1046                 /* Put this fragment into the sending queue. */
1047                 ip_queue_xmit(sk, dev, skb2, 1);
1048 /*              printk("Queued\n");*/
1049         }
1050  }
1051  
1052 
1053 
1054 #ifdef CONFIG_IP_FORWARD
1055 
1056 /* Forward an IP datagram to its next destination. */
1057 static void
1058 ip_forward(struct sk_buff *skb, struct device *dev, int is_frag)
     /* [previous][next][first][last][top][bottom][index][help] */
1059 {
1060   struct device *dev2;
1061   struct iphdr *iph;
1062   struct sk_buff *skb2;
1063   struct rtable *rt;
1064   unsigned char *ptr;
1065   unsigned long raddr;
1066 
1067   /*
1068    * Only forward packets that were fired at us when we are in promiscuous
1069    * mode. In standard mode we rely on the driver to filter for us.
1070    */
1071    
1072   if(dev->flags&IFF_PROMISC)
1073   {
1074         if(memcmp((char *)&skb[1],dev->dev_addr,dev->addr_len))
1075                 return;
1076   }
1077   
1078   /*
1079    * According to the RFC, we must first decrease the TTL field. If
1080    * that reaches zero, we must reply an ICMP control message telling
1081    * that the packet's lifetime expired.
1082    */
1083   iph = skb->h.iph;
1084   iph->ttl--;
1085   if (iph->ttl <= 0) {
1086         DPRINTF((DBG_IP, "\nIP: *** datagram expired: TTL=0 (ignored) ***\n"));
1087         DPRINTF((DBG_IP, "    SRC = %s   ", in_ntoa(iph->saddr)));
1088         DPRINTF((DBG_IP, "    DST = %s (ignored)\n", in_ntoa(iph->daddr)));
1089 
1090         /* Tell the sender its packet died... */
1091         icmp_send(skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, dev);
1092         return;
1093   }
1094 
1095   /* Re-compute the IP header checksum. */
1096   ip_send_check(iph);
1097 
1098   /*
1099    * OK, the packet is still valid.  Fetch its destination address,
1100    * and give it to the IP sender for further processing.
1101    */
1102   rt = rt_route(iph->daddr, NULL);
1103   if (rt == NULL) {
1104         DPRINTF((DBG_IP, "\nIP: *** routing (phase I) failed ***\n"));
1105 
1106         /* Tell the sender its packet cannot be delivered... */
1107         icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_UNREACH, dev);
1108         return;
1109   }
1110 
1111   /*
1112    * Gosh.  Not only is the packet valid; we even know how to
1113    * forward it onto its final destination.  Can we say this
1114    * is being plain lucky?
1115    * If the router told us that there is no GW, use the dest.
1116    * IP address itself- we seem to be connected directly...
1117    */
1118   raddr = rt->rt_gateway;
1119   if (raddr != 0) {
1120         rt = rt_route(raddr, NULL);
1121         if (rt == NULL) {
1122                 DPRINTF((DBG_IP, "\nIP: *** routing (phase II) failed ***\n"));
1123 
1124                 /* Tell the sender its packet cannot be delivered... */
1125                 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, dev);
1126                 return;
1127         }
1128         if (rt->rt_gateway != 0) raddr = rt->rt_gateway;
1129   } else raddr = iph->daddr;
1130   dev2 = rt->rt_dev;
1131 
1132   if (dev == dev2)
1133         return;
1134   /*
1135    * We now allocate a new buffer, and copy the datagram into it.
1136    * If the indicated interface is up and running, kick it.
1137    */
1138   DPRINTF((DBG_IP, "\nIP: *** fwd %s -> ", in_ntoa(iph->saddr)));
1139   DPRINTF((DBG_IP, "%s (via %s), LEN=%d\n",
1140                         in_ntoa(raddr), dev2->name, skb->len));
1141 
1142   if (dev2->flags & IFF_UP) {
1143         skb2 = (struct sk_buff *) alloc_skb(sizeof(struct sk_buff) +
1144                        dev2->hard_header_len + skb->len, GFP_ATOMIC);
1145         if (skb2 == NULL) {
1146                 printk("\nIP: No memory available for IP forward\n");
1147                 return;
1148         }
1149         ptr = (unsigned char *)(skb2 + 1);
1150         skb2->sk = NULL;
1151         skb2->free = 1;
1152         skb2->len = skb->len + dev2->hard_header_len;
1153         skb2->mem_addr = skb2;
1154         skb2->mem_len = sizeof(struct sk_buff) + skb2->len;
1155         skb2->next = NULL;
1156         skb2->h.raw = ptr;
1157 
1158         /* Copy the packet data into the new buffer. */
1159         memcpy(ptr + dev2->hard_header_len, skb->h.raw, skb->len);
1160                 
1161         /* Now build the MAC header. */
1162         (void) ip_send(skb2, raddr, skb->len, dev2, dev2->pa_addr);
1163 
1164         if(skb2->len > dev2->mtu)
1165         {
1166                 ip_fragment(NULL,skb2,dev2, is_frag);
1167                 kfree_skb(skb2,FREE_WRITE);
1168         }
1169         else
1170                 dev2->queue_xmit(skb2, dev2, SOPRI_NORMAL);
1171   }
1172 }
1173 
1174 
1175 #endif
1176 
1177 /* This function receives all incoming IP datagrams. */
1178 int
1179 ip_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
     /* [previous][next][first][last][top][bottom][index][help] */
1180 {
1181   struct iphdr *iph = skb->h.iph;
1182   unsigned char hash;
1183   unsigned char flag = 0;
1184   unsigned char opts_p = 0;     /* Set iff the packet has options. */
1185   struct inet_protocol *ipprot;
1186   static struct options opt; /* since we don't use these yet, and they
1187                                 take up stack space. */
1188   int brd;
1189   int is_frag=0;
1190 
1191   DPRINTF((DBG_IP, "<<\n"));
1192 
1193   /* Is the datagram acceptable? */
1194   if (iph->version != 4 || ip_fast_csum((unsigned char *)iph, iph->ihl) !=0) {
1195         DPRINTF((DBG_IP, "\nIP: *** datagram error ***\n"));
1196         DPRINTF((DBG_IP, "    SRC = %s   ", in_ntoa(iph->saddr)));
1197         DPRINTF((DBG_IP, "    DST = %s (ignored)\n", in_ntoa(iph->daddr)));
1198         skb->sk = NULL;
1199         kfree_skb(skb, FREE_WRITE);
1200         return(0);
1201   }
1202   
1203   if (iph->ihl != 5) {          /* Fast path for the typical optionless IP packet. */
1204       ip_print(iph);            /* Bogus, only for debugging. */
1205       memset((char *) &opt, 0, sizeof(opt));
1206       if (do_options(iph, &opt) != 0)
1207           return 0;
1208       opts_p = 1;
1209   }
1210 
1211   if (iph->frag_off & 0x0020)
1212         is_frag|=1;
1213   if (ntohs(iph->frag_off) & 0x1fff)
1214         is_frag|=2;
1215         
1216   /* Do any IP forwarding required.  chk_addr() is expensive -- avoid it someday. */
1217   if ((brd = chk_addr(iph->daddr)) == 0) {
1218 #ifdef CONFIG_IP_FORWARD
1219         ip_forward(skb, dev, is_frag);
1220 #else
1221         printk("Machine %x tried to use us as a forwarder to %x but we have forwarding disabled!\n",
1222                         iph->saddr,iph->daddr);
1223 #endif                  
1224         skb->sk = NULL;
1225         kfree_skb(skb, FREE_WRITE);
1226         return(0);
1227   }
1228 
1229   /*
1230    * Reassemble IP fragments. 
1231    */
1232 
1233   if(is_frag)
1234   {
1235 #ifdef CONFIG_IP_DEFRAG
1236         skb=ip_defrag(iph,skb,dev);
1237         if(skb==NULL)
1238         {
1239                 return 0;
1240         }
1241         iph=skb->h.iph;
1242 #else
1243         printk("\nIP: *** datagram fragmentation not yet implemented ***\n");
1244         printk("    SRC = %s   ", in_ntoa(iph->saddr));
1245         printk("    DST = %s (ignored)\n", in_ntoa(iph->daddr));
1246         icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, dev);
1247         skb->sk = NULL;
1248         kfree_skb(skb, FREE_WRITE);
1249         return(0);
1250 #endif
1251   }
1252 
1253 
1254 
1255   if(brd==IS_INVBCAST)
1256   {
1257 /*      printk("Invalid broadcast address from %x [target %x] (Probably they have a wrong netmask)\n",
1258                 iph->saddr,iph->daddr);*/
1259         skb->sk=NULL;
1260         kfree_skb(skb,FREE_WRITE);
1261         return(0);
1262   }
1263   
1264   /* Point into the IP datagram, just past the header. */
1265   skb->h.raw += iph->ihl*4;
1266   hash = iph->protocol & (MAX_INET_PROTOS -1);
1267   for (ipprot = (struct inet_protocol *)inet_protos[hash];
1268        ipprot != NULL;
1269        ipprot=(struct inet_protocol *)ipprot->next)
1270     {
1271        struct sk_buff *skb2;
1272 
1273        if (ipprot->protocol != iph->protocol) continue;
1274        DPRINTF((DBG_IP, "Using protocol = %X:\n", ipprot));
1275        print_ipprot(ipprot);
1276 
1277        /*
1278         * See if we need to make a copy of it.  This will
1279         * only be set if more than one protocol wants it. 
1280         * and then not for the last one.
1281         */
1282        if (ipprot->copy) {
1283                 skb2 = alloc_skb(skb->mem_len, GFP_ATOMIC);
1284                 if (skb2 == NULL) 
1285                         continue;
1286                 memcpy(skb2, skb, skb->mem_len);
1287                 skb2->mem_addr = skb2;
1288                 skb2->h.raw = (unsigned char *)(
1289                                 (unsigned long)skb2 +
1290                                 (unsigned long) skb->h.raw -
1291                                 (unsigned long)skb);
1292                 skb2->free=1;
1293         } else {
1294                 skb2 = skb;
1295         }
1296         flag = 1;
1297 
1298        /*
1299         * Pass on the datagram to each protocol that wants it,
1300         * based on the datagram protocol.  We should really
1301         * check the protocol handler's return values here...
1302         */
1303         ipprot->handler(skb2, dev, opts_p ? &opt : 0, iph->daddr,
1304                         (ntohs(iph->tot_len) - (iph->ihl * 4)),
1305                         iph->saddr, 0, ipprot);
1306 
1307   }
1308 
1309   /*
1310    * All protocols checked.
1311    * If this packet was a broadcast, we may *not* reply to it, since that
1312    * causes (proven, grin) ARP storms and a leakage of memory (i.e. all
1313    * ICMP reply messages get queued up for transmission...)
1314    */
1315   if (!flag) {
1316         if (brd != IS_BROADCAST)
1317                 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, dev);
1318         skb->sk = NULL;
1319         kfree_skb(skb, FREE_WRITE);
1320   }
1321 
1322   return(0);
1323 }
1324 
1325 
1326 /*
1327  * Queues a packet to be sent, and starts the transmitter
1328  * if necessary.  if free = 1 then we free the block after
1329  * transmit, otherwise we don't.
1330  * This routine also needs to put in the total length, and
1331  * compute the checksum.
1332  */
1333 void
1334 ip_queue_xmit(struct sock *sk, struct device *dev, 
     /* [previous][next][first][last][top][bottom][index][help] */
1335               struct sk_buff *skb, int free)
1336 {
1337   struct iphdr *iph;
1338   unsigned char *ptr;
1339 
1340   if (sk == NULL) free = 1;
1341   if (dev == NULL) {
1342         printk("IP: ip_queue_xmit dev = NULL\n");
1343         return;
1344   }
1345   IS_SKB(skb);
1346   skb->free = free;
1347   skb->dev = dev;
1348   skb->when = jiffies;
1349   
1350   DPRINTF((DBG_IP, ">>\n"));
1351   ptr = (unsigned char *)(skb + 1);
1352   ptr += dev->hard_header_len;
1353   iph = (struct iphdr *)ptr;
1354   iph->tot_len = ntohs(skb->len-dev->hard_header_len);
1355 
1356   if(skb->len > dev->mtu)
1357   {
1358 /*      printk("Fragment!\n");*/
1359         ip_fragment(sk,skb,dev,0);
1360         IS_SKB(skb);
1361         kfree_skb(skb,FREE_WRITE);
1362         return;
1363   }
1364   
1365   ip_send_check(iph);
1366   ip_print(iph);
1367   skb->next = NULL;
1368 
1369   /* See if this is the one trashing our queue. Ross? */
1370   skb->magic = 1;
1371   if (!free) {
1372         skb->link3 = NULL;
1373         sk->packets_out++;
1374         cli();
1375         if (sk->send_head == NULL) {
1376                 sk->send_tail = skb;
1377                 sk->send_head = skb;
1378         } else {
1379                 /* See if we've got a problem. */
1380                 if (sk->send_tail == NULL) {
1381                         printk("IP: ***bug sk->send_tail == NULL != sk->send_head\n");
1382                         sort_send(sk);
1383                 } else {
1384                         sk->send_tail->link3 = skb;
1385                         sk->send_tail = skb;
1386                 }
1387         }
1388         sti();
1389         reset_timer(sk, TIME_WRITE,
1390                 backoff(sk->backoff) * (2 * sk->mdev + sk->rtt));
1391   } else {
1392         skb->sk = sk;
1393   }
1394 
1395   /* If the indicated interface is up and running, kick it. */
1396   if (dev->flags & IFF_UP) {
1397         if (sk != NULL) {
1398                 dev->queue_xmit(skb, dev, sk->priority);
1399         } 
1400         else {
1401                 dev->queue_xmit(skb, dev, SOPRI_NORMAL);
1402         }
1403   } else {
1404         if (free) kfree_skb(skb, FREE_WRITE);
1405   }
1406 }
1407 
1408 
1409 void
1410 ip_retransmit(struct sock *sk, int all)
     /* [previous][next][first][last][top][bottom][index][help] */
1411 {
1412   struct sk_buff * skb;
1413   struct proto *prot;
1414   struct device *dev;
1415 
1416   prot = sk->prot;
1417   skb = sk->send_head;
1418   while (skb != NULL) {
1419         dev = skb->dev;
1420         /* I know this can't happen but as it does.. */
1421         if(dev==NULL)
1422         {
1423                 printk("ip_retransmit: NULL device bug!\n");
1424                 goto oops;
1425         }
1426 
1427         IS_SKB(skb);
1428         
1429         /*
1430          * The rebuild_header function sees if the ARP is done.
1431          * If not it sends a new ARP request, and if so it builds
1432          * the header.
1433          */
1434         cli();  /* We might get interrupted by an arp reply here and fill
1435                    the frame in twice. Because of the technique used this
1436                    would be a little sad */
1437         if (!skb->arp) {
1438                 if (dev->rebuild_header(skb+1, dev)) {
1439                         sti();  /* Failed to rebuild - next */
1440                         if (!all) break;
1441                         skb = (struct sk_buff *)skb->link3;
1442                         continue;
1443                 }
1444         }
1445         skb->arp = 1;
1446         sti();
1447         skb->when = jiffies;
1448 
1449         /* If the interface is (still) up and running, kick it. */
1450         if (dev->flags & IFF_UP) {
1451                 if (sk) dev->queue_xmit(skb, dev, sk->priority);
1452         /*        else dev->queue_xmit(skb, dev, SOPRI_NORMAL ); CANNOT HAVE SK=NULL HERE */
1453         }
1454 
1455 oops:   sk->retransmits++;
1456         sk->prot->retransmits ++;
1457         if (!all) break;
1458 
1459         /* This should cut it off before we send too many packets. */
1460         if (sk->retransmits > sk->cong_window) break;
1461         skb = (struct sk_buff *)skb->link3;
1462   }
1463 
1464   /*
1465    * Increase the RTT time every time we retransmit. 
1466    * This will cause exponential back off on how hard we try to
1467    * get through again.  Once we get through, the rtt will settle
1468    * back down reasonably quickly.
1469    */
1470   sk->backoff++;
1471   reset_timer(sk, TIME_WRITE, backoff(sk->backoff) * (2 * sk->mdev + sk->rtt));
1472 }
1473 
1474 /* Backoff function - the subject of much research */
1475 int backoff(int n)
     /* [previous][next][first][last][top][bottom][index][help] */
1476 {
1477         /* Use binary exponential up to retry #4, and quadratic after that
1478          * This yields the sequence
1479          * 1, 2, 4, 8, 16, 25, 36, 49, 64, 81, 100 ...
1480          */
1481 
1482         if(n<0)
1483         {
1484                 printk("Backoff < 0!\n");
1485                 return 16;      /* Make up a value */
1486         }
1487         
1488         if(n <= 4)
1489                 return 1 << n;  /* Binary exponential back off */
1490         else
1491         {
1492                 if(n<255)
1493                         return n * n;   /* Quadratic back off */
1494                 else
1495                 {
1496                         printk("Overloaded backoff!\n");
1497                         return 255*255;
1498                 }
1499         }
1500 }
1501 

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