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

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