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     __asm__("\t clc\n"
 429             "1:\n"
 430             "\t lodsl\n"
 431             "\t adcl %%eax, %%ebx\n"
 432             "\t loop 1b\n"
 433             "\t adcl $0, %%ebx\n"
 434             "\t movl %%ebx, %%eax\n"
 435             "\t shrl $16, %%eax\n"
 436             "\t addw %%ax, %%bx\n"
 437             "\t adcw $0, %%bx\n"
 438             : "=b" (sum) , "=S" (buff)
 439             : "0" (sum), "c" (wlen) ,"1" (buff)
 440             : "ax", "cx", "si", "bx" );
 441     return (~sum) & 0xffff;
 442 }
 443 
 444 /*
 445  * This routine does all the checksum computations that don't
 446  * require anything special (like copying or special headers).
 447  */
 448 unsigned short
 449 ip_compute_csum(unsigned char * buff, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 450 {
 451   unsigned long sum = 0;
 452 
 453   /* Do the first multiple of 4 bytes and convert to 16 bits. */
 454   if (len > 3) {
 455         __asm__("\t clc\n"
 456                 "1:\n"
 457                 "\t lodsl\n"
 458                 "\t adcl %%eax, %%ebx\n"
 459                 "\t loop 1b\n"
 460                 "\t adcl $0, %%ebx\n"
 461                 "\t movl %%ebx, %%eax\n"
 462                 "\t shrl $16, %%eax\n"
 463                 "\t addw %%ax, %%bx\n"
 464                 "\t adcw $0, %%bx\n"
 465                 : "=b" (sum) , "=S" (buff)
 466                 : "0" (sum), "c" (len >> 2) ,"1" (buff)
 467                 : "ax", "cx", "si", "bx" );
 468   }
 469   if (len & 2) {
 470         __asm__("\t lodsw\n"
 471                 "\t addw %%ax, %%bx\n"
 472                 "\t adcw $0, %%bx\n"
 473                 : "=b" (sum), "=S" (buff)
 474                 : "0" (sum), "1" (buff)
 475                 : "bx", "ax", "si");
 476   }
 477   if (len & 1) {
 478         __asm__("\t lodsb\n"
 479                 "\t movb $0, %%ah\n"
 480                 "\t addw %%ax, %%bx\n"
 481                 "\t adcw $0, %%bx\n"
 482                 : "=b" (sum), "=S" (buff)
 483                 : "0" (sum), "1" (buff)
 484                 : "bx", "ax", "si");
 485   }
 486   sum =~sum;
 487   return(sum & 0xffff);
 488 }
 489 
 490 /* Check the header of an incoming IP datagram.  This version is still used in slhc.c. */
 491 int
 492 ip_csum(struct iphdr *iph)
     /* [previous][next][first][last][top][bottom][index][help] */
 493 {
 494   return ip_fast_csum((unsigned char *)iph, iph->ihl);
 495 }
 496 
 497 /* Generate a checksym for an outgoing IP datagram. */
 498 static void
 499 ip_send_check(struct iphdr *iph)
     /* [previous][next][first][last][top][bottom][index][help] */
 500 {
 501    iph->check = 0;
 502    iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
 503 }
 504 
 505 /************************ Fragment Handlers From NET2E not yet with tweaks to beat 4K **********************************/
 506 
 507 static struct ipq *ipqueue = NULL;              /* IP fragment queue    */
 508  /* Create a new fragment entry. */
 509 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] */
 510 {
 511         struct ipfrag *fp;
 512  
 513         fp = (struct ipfrag *) kmalloc(sizeof(struct ipfrag), GFP_ATOMIC);
 514         if (fp == NULL) 
 515         {
 516                 printk("IP: frag_create: no memory left !\n");
 517                 return(NULL);
 518         }
 519         memset(fp, 0, sizeof(struct ipfrag));
 520 
 521         /* Fill in the structure. */
 522         fp->offset = offset;
 523         fp->end = end;
 524         fp->len = end - offset;
 525         fp->skb = skb;
 526         fp->ptr = ptr;
 527  
 528         return(fp);
 529 }
 530  
 531  
 532 /*
 533  * Find the correct entry in the "incomplete datagrams" queue for
 534  * this IP datagram, and return the queue entry address if found.
 535  */
 536 static struct ipq *ip_find(struct iphdr *iph)
     /* [previous][next][first][last][top][bottom][index][help] */
 537 {
 538         struct ipq *qp;
 539         struct ipq *qplast;
 540  
 541         cli();
 542         qplast = NULL;
 543         for(qp = ipqueue; qp != NULL; qplast = qp, qp = qp->next) 
 544         {
 545                 if (iph->id== qp->iph->id && iph->saddr == qp->iph->saddr &&
 546                         iph->daddr == qp->iph->daddr && iph->protocol == qp->iph->protocol) 
 547                 {
 548                         del_timer(&qp->timer);  /* So it doesnt vanish on us. The timer will be reset anyway */
 549                         sti();
 550                         return(qp);
 551                 }
 552         }
 553         sti();
 554         return(NULL);
 555 }
 556  
 557  
 558 /*
 559  * Remove an entry from the "incomplete datagrams" queue, either
 560  * because we completed, reassembled and processed it, or because
 561  * it timed out.
 562  */
 563 
 564 static void ip_free(struct ipq *qp)
     /* [previous][next][first][last][top][bottom][index][help] */
 565 {
 566         struct ipfrag *fp;
 567         struct ipfrag *xp;
 568 
 569         /* Stop the timer for this entry. */
 570 /*      printk("ip_free\n");*/
 571         del_timer(&qp->timer);
 572 
 573         /* Remove this entry from the "incomplete datagrams" queue. */
 574         cli();
 575         if (qp->prev == NULL) 
 576         {
 577                 ipqueue = qp->next;
 578                 if (ipqueue != NULL) 
 579                         ipqueue->prev = NULL;
 580         } 
 581         else 
 582         {
 583                 qp->prev->next = qp->next;
 584                 if (qp->next != NULL) 
 585                         qp->next->prev = qp->prev;
 586         }
 587  
 588         /* Release all fragment data. */
 589 /*      printk("ip_free: kill frag data\n");*/
 590         fp = qp->fragments;
 591         while (fp != NULL) 
 592         {
 593                 xp = fp->next;
 594                 IS_SKB(fp->skb);
 595                 kfree_skb(fp->skb,FREE_READ);
 596                 kfree_s(fp, sizeof(struct ipfrag));
 597                 fp = xp;
 598         }
 599         
 600 /*      printk("ip_free: cleanup\n");*/
 601  
 602         /* Release the MAC header. */
 603         kfree_s(qp->mac, qp->maclen);
 604  
 605         /* Release the IP header. */
 606         kfree_s(qp->iph, qp->ihlen + 8);
 607  
 608         /* Finally, release the queue descriptor itself. */
 609         kfree_s(qp, sizeof(struct ipq));
 610 /*      printk("ip_free:done\n");*/
 611         sti();
 612  }
 613  
 614  
 615  /* Oops- a fragment queue timed out.  Kill it and send an ICMP reply. */
 616  
 617 static void ip_expire(unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 618 {
 619         struct ipq *qp;
 620  
 621         qp = (struct ipq *)arg;
 622         DPRINTF((DBG_IP, "IP: queue_expire: fragment queue 0x%X timed out!\n", qp));
 623  
 624         /* Send an ICMP "Fragment Reassembly Timeout" message. */
 625 #if 0           
 626         icmp_send(qp->iph->ip_src.s_addr, ICMP_TIME_EXCEEDED,
 627                     ICMP_EXC_FRAGTIME, qp->iph);
 628 #endif           
 629         if(qp->fragments!=NULL)
 630                 icmp_send(qp->fragments->skb,ICMP_TIME_EXCEEDED,
 631                                 ICMP_EXC_FRAGTIME, qp->dev);
 632  
 633         /* Nuke the fragment queue. */
 634         ip_free(qp);
 635 }
 636  
 637  
 638 /*
 639  * Add an entry to the 'ipq' queue for a newly received IP datagram.
 640  * We will (hopefully :-) receive all other fragments of this datagram
 641  * in time, so we just create a queue for this datagram, in which we
 642  * will insert the received fragments at their respective positions.
 643  */
 644 
 645 static struct ipq *ip_create(struct sk_buff *skb, struct iphdr *iph, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 646 {
 647         struct ipq *qp;
 648         int maclen;
 649         int ihlen;
 650 
 651         qp = (struct ipq *) kmalloc(sizeof(struct ipq), GFP_ATOMIC);
 652         if (qp == NULL) 
 653         {
 654                 printk("IP: create: no memory left !\n");
 655                 return(NULL);
 656         }
 657         memset(qp, 0, sizeof(struct ipq));
 658 
 659         /* Allocate memory for the MAC header. */
 660         maclen = ((unsigned long) iph) - ((unsigned long) (skb + 1));
 661         qp->mac = (unsigned char *) kmalloc(maclen, GFP_ATOMIC);
 662         if (qp->mac == NULL) 
 663         {
 664                 printk("IP: create: no memory left !\n");
 665                 kfree_s(qp, sizeof(struct ipq));
 666                 return(NULL);
 667         }
 668 
 669         /* Allocate memory for the IP header (plus 8 octects for ICMP). */
 670         ihlen = (iph->ihl * sizeof(unsigned long));
 671         qp->iph = (struct iphdr *) kmalloc(ihlen + 8, GFP_ATOMIC);
 672         if (qp->iph == NULL) 
 673         {
 674                 printk("IP: create: no memory left !\n");
 675                 kfree_s(qp->mac, maclen);
 676                 kfree_s(qp, sizeof(struct ipq));
 677                 return(NULL);
 678         }
 679 
 680         /* Fill in the structure. */
 681         memcpy(qp->mac, (skb + 1), maclen);
 682         memcpy(qp->iph, iph, ihlen + 8);
 683         qp->len = 0;
 684         qp->ihlen = ihlen;
 685         qp->maclen = maclen;
 686         qp->fragments = NULL;
 687         qp->dev = dev;
 688 /*      printk("Protocol = %d\n",qp->iph->protocol);*/
 689         
 690         /* Start a timer for this entry. */
 691         qp->timer.expires = IP_FRAG_TIME;               /* about 30 seconds     */
 692         qp->timer.data = (unsigned long) qp;            /* pointer to queue     */
 693         qp->timer.function = ip_expire;                 /* expire function      */
 694         add_timer(&qp->timer);
 695 
 696         /* Add this entry to the queue. */
 697         qp->prev = NULL;
 698         cli();
 699         qp->next = ipqueue;
 700         if (qp->next != NULL) 
 701                 qp->next->prev = qp;
 702         ipqueue = qp;
 703         sti();
 704         return(qp);
 705 }
 706  
 707  
 708  /* See if a fragment queue is complete. */
 709 static int ip_done(struct ipq *qp)
     /* [previous][next][first][last][top][bottom][index][help] */
 710 {
 711         struct ipfrag *fp;
 712         int offset;
 713  
 714         /* Only possible if we received the final fragment. */
 715         if (qp->len == 0) 
 716                 return(0);
 717  
 718         /* Check all fragment offsets to see if they connect. */
 719         fp = qp->fragments;
 720         offset = 0;
 721         while (fp != NULL) 
 722         {
 723                 if (fp->offset > offset) 
 724                         return(0);      /* fragment(s) missing */
 725                 offset = fp->end;
 726                 fp = fp->next;
 727         }
 728  
 729         /* All fragments are present. */
 730         return(1);
 731  }
 732  
 733  
 734 /* Build a new IP datagram from all its fragments. */
 735 static struct sk_buff *ip_glue(struct ipq *qp)
     /* [previous][next][first][last][top][bottom][index][help] */
 736 {
 737         struct sk_buff *skb;
 738         struct iphdr *iph;
 739         struct ipfrag *fp;
 740         unsigned char *ptr;
 741         int count, len;
 742  
 743         /* Allocate a new buffer for the datagram. */
 744         len = sizeof(struct sk_buff)+qp->maclen + qp->ihlen + qp->len;
 745         if ((skb = alloc_skb(len,GFP_ATOMIC)) == NULL) 
 746         {
 747                 printk("IP: queue_glue: no memory for glueing queue 0x%X\n", (int) qp);
 748                 ip_free(qp);
 749                 return(NULL);
 750         }
 751  
 752         /* Fill in the basic details. */
 753         skb->len = (len - qp->maclen);
 754         skb->h.raw = (unsigned char *) (skb + 1);
 755         skb->free = 1;
 756         skb->lock = 1;
 757  
 758         /* Copy the original MAC and IP headers into the new buffer. */
 759         ptr = (unsigned char *) skb->h.raw;
 760         memcpy(ptr, ((unsigned char *) qp->mac), qp->maclen);
 761 /*      printk("Copied %d bytes of mac header.\n",qp->maclen);*/
 762         ptr += qp->maclen;
 763         memcpy(ptr, ((unsigned char *) qp->iph), qp->ihlen);
 764 /*      printk("Copied %d byte of ip header.\n",qp->ihlen);*/
 765         ptr += qp->ihlen;
 766         skb->h.raw += qp->maclen;
 767         
 768 /*      printk("Protocol = %d\n",skb->h.iph->protocol);*/
 769         count = 0;
 770  
 771         /* Copy the data portions of all fragments into the new buffer. */
 772         fp = qp->fragments;
 773         while(fp != NULL) 
 774         {
 775                 if(count+fp->len>skb->len)
 776                 {
 777                         printk("Invalid fragment list: Fragment over size.\n");
 778                         kfree_skb(skb,FREE_WRITE);
 779                         return NULL;
 780                 }
 781 /*              printk("Fragment %d size %d\n",fp->offset,fp->len);*/
 782                 memcpy((ptr + fp->offset), fp->ptr, fp->len);
 783                 count += fp->len;
 784                 fp = fp->next;
 785         }
 786  
 787         /* We glued together all fragments, so remove the queue entry. */
 788         ip_free(qp);
 789  
 790         /* Done with all fragments. Fixup the new IP header. */
 791         iph = skb->h.iph;
 792         iph->frag_off = 0;
 793         iph->tot_len = htons((iph->ihl * sizeof(unsigned long)) + count);
 794         return(skb);
 795 }
 796  
 797 
 798 /* Process an incoming IP datagram fragment. */
 799 static struct sk_buff *ip_defrag(struct iphdr *iph, struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 800 {
 801         struct ipfrag *prev, *next;
 802         struct ipfrag *tfp;
 803         struct ipq *qp;
 804         struct sk_buff *skb2;
 805         unsigned char *ptr;
 806         int flags, offset;
 807         int i, ihl, end;
 808 
 809         /* Find the entry of this IP datagram in the "incomplete datagrams" queue. */
 810         qp = ip_find(iph);
 811  
 812         /* Is this a non-fragmented datagram? */
 813         offset = ntohs(iph->frag_off);
 814         flags = offset & ~IP_OFFSET;
 815         offset &= IP_OFFSET;
 816         if (((flags & IP_MF) == 0) && (offset == 0)) 
 817         {
 818                 if (qp != NULL) 
 819                         ip_free(qp);    /* Huh? How could this exist?? */
 820                 return(skb);
 821         }
 822         offset <<= 3;           /* offset is in 8-byte chunks */
 823  
 824         /*
 825          * If the queue already existed, keep restarting its timer as long
 826          * as we still are receiving fragments.  Otherwise, create a fresh
 827          * queue entry.
 828          */
 829         if (qp != NULL) 
 830         {
 831                 del_timer(&qp->timer);
 832                 qp->timer.expires = IP_FRAG_TIME;       /* about 30 seconds     */
 833                 qp->timer.data = (unsigned long) qp;    /* pointer to queue     */
 834                 qp->timer.function = ip_expire;         /* expire function      */
 835                 add_timer(&qp->timer);
 836         } 
 837         else 
 838         {
 839                 if ((qp = ip_create(skb, iph, dev)) == NULL) 
 840                         return(NULL);
 841         }
 842  
 843         /* Determine the position of this fragment. */
 844         ihl = (iph->ihl * sizeof(unsigned long));
 845         end = offset + ntohs(iph->tot_len) - ihl;
 846  
 847         /* Point into the IP datagram 'data' part. */
 848         ptr = ((unsigned char *) (skb + 1)) + dev->hard_header_len + ihl;
 849  
 850         /* Is this the final fragment? */
 851         if ((flags & IP_MF) == 0) 
 852                 qp->len = end;
 853  
 854         /*
 855          * Find out which fragments are in front and at the back of us
 856          * in the chain of fragments so far.  We must know where to put
 857          * this fragment, right?
 858          */
 859         prev = NULL;
 860         for(next = qp->fragments; next != NULL; next = next->next) 
 861         {
 862                 if (next->offset > offset) 
 863                         break;  /* bingo! */
 864                 prev = next;
 865         }       
 866  
 867         /*
 868          * We found where to put this one.
 869          * Check for overlap with preceeding fragment, and, if needed,
 870          * align things so that any overlaps are eliminated.
 871          */
 872         if (prev != NULL && offset < prev->end) 
 873         {
 874                 i = prev->end - offset;
 875                 offset += i;    /* ptr into datagram */
 876                 ptr += i;       /* ptr into fragment data */
 877                 DPRINTF((DBG_IP, "IP: defrag: fixed low overlap %d bytes\n", i));
 878         }       
 879  
 880         /*
 881          * Look for overlap with succeeding segments.
 882          * If we can merge fragments, do it.
 883          */
 884    
 885         for(; next != NULL; next = tfp) 
 886         {
 887                 tfp = next->next;
 888                 if (next->offset >= end) 
 889                         break;          /* no overlaps at all */
 890  
 891                 i = end - next->offset;                 /* overlap is 'i' bytes */
 892                 next->len -= i;                         /* so reduce size of    */
 893                 next->offset += i;                      /* next fragment        */
 894                 next->ptr += i;
 895                 
 896                 /* If we get a frag size of <= 0, remove it. */
 897                 if (next->len <= 0) 
 898                 {
 899                         DPRINTF((DBG_IP, "IP: defrag: removing frag 0x%X (len %d)\n",
 900                                                         next, next->len));
 901                         if (next->prev != NULL) 
 902                                 next->prev->next = next->next;
 903                         else 
 904                                 qp->fragments = next->next;
 905                 
 906                         if (tfp->next != NULL) 
 907                                 next->next->prev = next->prev;
 908                         
 909                         kfree_s(next, sizeof(struct ipfrag));
 910                 }
 911                 DPRINTF((DBG_IP, "IP: defrag: fixed high overlap %d bytes\n", i));
 912         }
 913  
 914         /* Insert this fragment in the chain of fragments. */
 915         tfp = NULL;
 916         tfp = ip_frag_create(offset, end, skb, ptr);
 917         tfp->prev = prev;
 918         tfp->next = next;
 919         if (prev != NULL) 
 920                 prev->next = tfp;
 921         else 
 922                 qp->fragments = tfp;
 923    
 924         if (next != NULL) 
 925                 next->prev = tfp;
 926  
 927         /*
 928          * OK, so we inserted this new fragment into the chain.
 929          * Check if we now have a full IP datagram which we can
 930          * bump up to the IP layer...
 931          */
 932    
 933         if (ip_done(qp)) 
 934         {
 935                 skb2 = ip_glue(qp);             /* glue together the fragments */
 936                 return(skb2);
 937         }
 938         return(NULL);
 939  }
 940  
 941  
 942  /*
 943   * This IP datagram is too large to be sent in one piece.  Break it up into
 944   * smaller pieces (each of size equal to the MAC header plus IP header plus
 945   * a block of the data of the original IP data part) that will yet fit in a
 946   * single device frame, and queue such a frame for sending by calling the
 947   * ip_queue_xmit().  Note that this is recursion, and bad things will happen
 948   * if this function causes a loop...
 949   */
 950  void ip_fragment(struct sock *sk, struct sk_buff *skb, struct device *dev, int is_frag)
     /* [previous][next][first][last][top][bottom][index][help] */
 951  {
 952         struct iphdr *iph;
 953         unsigned char *raw;
 954         unsigned char *ptr;
 955         struct sk_buff *skb2;
 956         int left, mtu, hlen, len;
 957         int offset;
 958  
 959         /* Point into the IP datagram header. */
 960         raw = (unsigned char *) (skb + 1);
 961         iph = (struct iphdr *) (raw + dev->hard_header_len);
 962         
 963         /* Setup starting values. */
 964         hlen = (iph->ihl * sizeof(unsigned long));
 965         left = ntohs(iph->tot_len) - hlen;
 966         hlen += dev->hard_header_len;
 967         mtu = (dev->mtu - hlen);
 968         ptr = (raw + hlen);
 969         
 970         DPRINTF((DBG_IP, "IP: Fragmentation Desired\n"));
 971         DPRINTF((DBG_IP, "    DEV=%s, MTU=%d, LEN=%d SRC=%s",
 972                 dev->name, dev->mtu, left, in_ntoa(iph->saddr)));
 973         DPRINTF((DBG_IP, " DST=%s\n", in_ntoa(iph->daddr)));
 974  
 975         /* Check for any "DF" flag. */
 976         if (ntohs(iph->frag_off) & IP_DF) 
 977         {
 978                 DPRINTF((DBG_IP, "IP: Fragmentation Desired, but DF set !\n"));
 979                 DPRINTF((DBG_IP, "    DEV=%s, MTU=%d, LEN=%d SRC=%s",
 980                         dev->name, dev->mtu, left, in_ntoa(iph->saddr)));
 981                 DPRINTF((DBG_IP, " DST=%s\n", in_ntoa(iph->daddr)));
 982  
 983                 /*
 984                  * FIXME:
 985                  * We should send an ICMP warning message here!
 986                  */
 987                  
 988                 icmp_send(skb,ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, dev); 
 989                 return;
 990         }
 991  
 992         /* Fragment the datagram. */
 993         if (is_frag & 2)
 994           offset = (ntohs(iph->frag_off) & 0x1fff) << 3;
 995         else
 996           offset = 0;
 997         while(left > 0) 
 998         {
 999                 len = left;
1000                 if (len+8 > mtu) 
1001                         len = (dev->mtu - hlen - 8);
1002                 if ((left - len) >= 8) 
1003                 {
1004                         len /= 8;
1005                         len *= 8;
1006                 }
1007                 DPRINTF((DBG_IP,"IP: frag: creating fragment of %d bytes (%d total)\n",
1008                                                         len, len + hlen));
1009  
1010                 /* Allocate buffer. */
1011                 if ((skb2 = alloc_skb(sizeof(struct sk_buff) + len + hlen,GFP_KERNEL)) == NULL) 
1012                 {
1013                         printk("IP: frag: no memory for new fragment!\n");
1014                         return;
1015                 }
1016                 skb2->arp = skb->arp;
1017                 skb2->free = skb->free;
1018                 skb2->len = len + hlen;
1019                 skb2->h.raw=(char *)(skb2+1);
1020  
1021                 if (sk) 
1022                         sk->wmem_alloc += skb2->mem_len;
1023  
1024                 /* Copy the packet header into the new buffer. */
1025                 memcpy(skb2->h.raw, raw, hlen);
1026  
1027                 /* Copy a block of the IP datagram. */
1028                 memcpy(skb2->h.raw + hlen, ptr, len);
1029                 left -= len;
1030 
1031                 skb2->h.raw+=dev->hard_header_len; 
1032                 /* Fill in the new header fields. */
1033                 iph = (struct iphdr *)(skb2->h.raw/*+dev->hard_header_len*/);
1034                 iph->frag_off = htons((offset >> 3));
1035                 /* Added AC : If we are fragmenting a fragment thats not the
1036                    last fragment then keep MF on each bit */
1037                 if (left > 0 || (is_frag & 1)) 
1038                         iph->frag_off |= htons(IP_MF);
1039                 ptr += len;
1040                 offset += len;
1041 /*              printk("Queue frag\n");*/
1042  
1043                 /* Put this fragment into the sending queue. */
1044                 ip_queue_xmit(sk, dev, skb2, 1);
1045 /*              printk("Queued\n");*/
1046         }
1047  }
1048  
1049 
1050 
1051 #ifdef CONFIG_IP_FORWARD
1052 
1053 /* Forward an IP datagram to its next destination. */
1054 static void
1055 ip_forward(struct sk_buff *skb, struct device *dev, int is_frag)
     /* [previous][next][first][last][top][bottom][index][help] */
1056 {
1057   struct device *dev2;
1058   struct iphdr *iph;
1059   struct sk_buff *skb2;
1060   struct rtable *rt;
1061   unsigned char *ptr;
1062   unsigned long raddr;
1063 
1064   /*
1065    * Only forward packets that were fired at us when we are in promiscuous
1066    * mode. In standard mode we rely on the driver to filter for us.
1067    */
1068    
1069   if(dev->flags&IFF_PROMISC)
1070   {
1071         if(memcmp((char *)&skb[1],dev->dev_addr,dev->addr_len))
1072                 return;
1073   }
1074   
1075   /*
1076    * According to the RFC, we must first decrease the TTL field. If
1077    * that reaches zero, we must reply an ICMP control message telling
1078    * that the packet's lifetime expired.
1079    */
1080   iph = skb->h.iph;
1081   iph->ttl--;
1082   if (iph->ttl <= 0) {
1083         DPRINTF((DBG_IP, "\nIP: *** datagram expired: TTL=0 (ignored) ***\n"));
1084         DPRINTF((DBG_IP, "    SRC = %s   ", in_ntoa(iph->saddr)));
1085         DPRINTF((DBG_IP, "    DST = %s (ignored)\n", in_ntoa(iph->daddr)));
1086 
1087         /* Tell the sender its packet died... */
1088         icmp_send(skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, dev);
1089         return;
1090   }
1091 
1092   /* Re-compute the IP header checksum. */
1093   ip_send_check(iph);
1094 
1095   /*
1096    * OK, the packet is still valid.  Fetch its destination address,
1097    * and give it to the IP sender for further processing.
1098    */
1099   rt = rt_route(iph->daddr, NULL);
1100   if (rt == NULL) {
1101         DPRINTF((DBG_IP, "\nIP: *** routing (phase I) failed ***\n"));
1102 
1103         /* Tell the sender its packet cannot be delivered... */
1104         icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_UNREACH, dev);
1105         return;
1106   }
1107 
1108   /*
1109    * Gosh.  Not only is the packet valid; we even know how to
1110    * forward it onto its final destination.  Can we say this
1111    * is being plain lucky?
1112    * If the router told us that there is no GW, use the dest.
1113    * IP address itself- we seem to be connected directly...
1114    */
1115   raddr = rt->rt_gateway;
1116   if (raddr != 0) {
1117         rt = rt_route(raddr, NULL);
1118         if (rt == NULL) {
1119                 DPRINTF((DBG_IP, "\nIP: *** routing (phase II) failed ***\n"));
1120 
1121                 /* Tell the sender its packet cannot be delivered... */
1122                 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, dev);
1123                 return;
1124         }
1125         if (rt->rt_gateway != 0) raddr = rt->rt_gateway;
1126   } else raddr = iph->daddr;
1127   dev2 = rt->rt_dev;
1128 
1129   if (dev == dev2)
1130         return;
1131   /*
1132    * We now allocate a new buffer, and copy the datagram into it.
1133    * If the indicated interface is up and running, kick it.
1134    */
1135   DPRINTF((DBG_IP, "\nIP: *** fwd %s -> ", in_ntoa(iph->saddr)));
1136   DPRINTF((DBG_IP, "%s (via %s), LEN=%d\n",
1137                         in_ntoa(raddr), dev2->name, skb->len));
1138 
1139   if (dev2->flags & IFF_UP) {
1140         skb2 = (struct sk_buff *) alloc_skb(sizeof(struct sk_buff) +
1141                        dev2->hard_header_len + skb->len, GFP_ATOMIC);
1142         if (skb2 == NULL) {
1143                 printk("\nIP: No memory available for IP forward\n");
1144                 return;
1145         }
1146         ptr = (unsigned char *)(skb2 + 1);
1147         skb2->sk = NULL;
1148         skb2->free = 1;
1149         skb2->len = skb->len + dev2->hard_header_len;
1150         skb2->mem_addr = skb2;
1151         skb2->mem_len = sizeof(struct sk_buff) + skb2->len;
1152         skb2->next = NULL;
1153         skb2->h.raw = ptr;
1154 
1155         /* Copy the packet data into the new buffer. */
1156         memcpy(ptr + dev2->hard_header_len, skb->h.raw, skb->len);
1157                 
1158         /* Now build the MAC header. */
1159         (void) ip_send(skb2, raddr, skb->len, dev2, dev2->pa_addr);
1160 
1161         if(skb2->len > dev2->mtu)
1162         {
1163                 ip_fragment(NULL,skb2,dev2, is_frag);
1164                 kfree_skb(skb2,FREE_WRITE);
1165         }
1166         else
1167                 dev2->queue_xmit(skb2, dev2, SOPRI_NORMAL);
1168   }
1169 }
1170 
1171 
1172 #endif
1173 
1174 /* This function receives all incoming IP datagrams. */
1175 int
1176 ip_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
     /* [previous][next][first][last][top][bottom][index][help] */
1177 {
1178   struct iphdr *iph = skb->h.iph;
1179   unsigned char hash;
1180   unsigned char flag = 0;
1181   unsigned char opts_p = 0;     /* Set iff the packet has options. */
1182   struct inet_protocol *ipprot;
1183   static struct options opt; /* since we don't use these yet, and they
1184                                 take up stack space. */
1185   int brd;
1186   int is_frag=0;
1187 
1188   DPRINTF((DBG_IP, "<<\n"));
1189 
1190   /* Is the datagram acceptable? */
1191   if (iph->version != 4 || ip_fast_csum((unsigned char *)iph, iph->ihl) !=0) {
1192         DPRINTF((DBG_IP, "\nIP: *** datagram error ***\n"));
1193         DPRINTF((DBG_IP, "    SRC = %s   ", in_ntoa(iph->saddr)));
1194         DPRINTF((DBG_IP, "    DST = %s (ignored)\n", in_ntoa(iph->daddr)));
1195         skb->sk = NULL;
1196         kfree_skb(skb, FREE_WRITE);
1197         return(0);
1198   }
1199   
1200   if (iph->ihl != 5) {          /* Fast path for the typical optionless IP packet. */
1201       ip_print(iph);            /* Bogus, only for debugging. */
1202       memset((char *) &opt, 0, sizeof(opt));
1203       if (do_options(iph, &opt) != 0)
1204           return 0;
1205       opts_p = 1;
1206   }
1207 
1208   if (iph->frag_off & 0x0020)
1209         is_frag|=1;
1210   if (ntohs(iph->frag_off) & 0x1fff)
1211         is_frag|=2;
1212         
1213   /* Do any IP forwarding required.  chk_addr() is expensive -- avoid it someday. */
1214   if ((brd = chk_addr(iph->daddr)) == 0) {
1215 #ifdef CONFIG_IP_FORWARD
1216         ip_forward(skb, dev, is_frag);
1217 #else
1218         printk("Machine %x tried to use us as a forwarder to %x but we have forwarding disabled!\n",
1219                         iph->saddr,iph->daddr);
1220 #endif                  
1221         skb->sk = NULL;
1222         kfree_skb(skb, FREE_WRITE);
1223         return(0);
1224   }
1225 
1226   /*
1227    * Reassemble IP fragments. 
1228    */
1229 
1230   if(is_frag)
1231   {
1232 #ifdef CONFIG_IP_DEFRAG
1233         skb=ip_defrag(iph,skb,dev);
1234         if(skb==NULL)
1235         {
1236                 return 0;
1237         }
1238         iph=skb->h.iph;
1239 #else
1240         printk("\nIP: *** datagram fragmentation not yet implemented ***\n");
1241         printk("    SRC = %s   ", in_ntoa(iph->saddr));
1242         printk("    DST = %s (ignored)\n", in_ntoa(iph->daddr));
1243         icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, dev);
1244         skb->sk = NULL;
1245         kfree_skb(skb, FREE_WRITE);
1246         return(0);
1247 #endif
1248   }
1249 
1250 
1251 
1252   if(brd==IS_INVBCAST)
1253   {
1254 /*      printk("Invalid broadcast address from %x [target %x] (Probably they have a wrong netmask)\n",
1255                 iph->saddr,iph->daddr);*/
1256         skb->sk=NULL;
1257         kfree_skb(skb,FREE_WRITE);
1258         return(0);
1259   }
1260   
1261   /* Point into the IP datagram, just past the header. */
1262   skb->h.raw += iph->ihl*4;
1263   hash = iph->protocol & (MAX_INET_PROTOS -1);
1264   for (ipprot = (struct inet_protocol *)inet_protos[hash];
1265        ipprot != NULL;
1266        ipprot=(struct inet_protocol *)ipprot->next)
1267     {
1268        struct sk_buff *skb2;
1269 
1270        if (ipprot->protocol != iph->protocol) continue;
1271        DPRINTF((DBG_IP, "Using protocol = %X:\n", ipprot));
1272        print_ipprot(ipprot);
1273 
1274        /*
1275         * See if we need to make a copy of it.  This will
1276         * only be set if more than one protocol wants it. 
1277         * and then not for the last one.
1278         */
1279        if (ipprot->copy) {
1280                 skb2 = alloc_skb(skb->mem_len, GFP_ATOMIC);
1281                 if (skb2 == NULL) 
1282                         continue;
1283                 memcpy(skb2, skb, skb->mem_len);
1284                 skb2->mem_addr = skb2;
1285                 skb2->h.raw = (unsigned char *)(
1286                                 (unsigned long)skb2 +
1287                                 (unsigned long) skb->h.raw -
1288                                 (unsigned long)skb);
1289                 skb2->free=1;
1290         } else {
1291                 skb2 = skb;
1292         }
1293         flag = 1;
1294 
1295        /*
1296         * Pass on the datagram to each protocol that wants it,
1297         * based on the datagram protocol.  We should really
1298         * check the protocol handler's return values here...
1299         */
1300         ipprot->handler(skb2, dev, opts_p ? &opt : 0, iph->daddr,
1301                         (ntohs(iph->tot_len) - (iph->ihl * 4)),
1302                         iph->saddr, 0, ipprot);
1303 
1304   }
1305 
1306   /*
1307    * All protocols checked.
1308    * If this packet was a broadcast, we may *not* reply to it, since that
1309    * causes (proven, grin) ARP storms and a leakage of memory (i.e. all
1310    * ICMP reply messages get queued up for transmission...)
1311    */
1312   if (!flag) {
1313         if (brd != IS_BROADCAST)
1314                 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, dev);
1315         skb->sk = NULL;
1316         kfree_skb(skb, FREE_WRITE);
1317   }
1318 
1319   return(0);
1320 }
1321 
1322 
1323 /*
1324  * Queues a packet to be sent, and starts the transmitter
1325  * if necessary.  if free = 1 then we free the block after
1326  * transmit, otherwise we don't.
1327  * This routine also needs to put in the total length, and
1328  * compute the checksum.
1329  */
1330 void
1331 ip_queue_xmit(struct sock *sk, struct device *dev, 
     /* [previous][next][first][last][top][bottom][index][help] */
1332               struct sk_buff *skb, int free)
1333 {
1334   struct iphdr *iph;
1335   unsigned char *ptr;
1336 
1337   if (sk == NULL) free = 1;
1338   if (dev == NULL) {
1339         printk("IP: ip_queue_xmit dev = NULL\n");
1340         return;
1341   }
1342   IS_SKB(skb);
1343   skb->free = free;
1344   skb->dev = dev;
1345   skb->when = jiffies;
1346   
1347   DPRINTF((DBG_IP, ">>\n"));
1348   ptr = (unsigned char *)(skb + 1);
1349   ptr += dev->hard_header_len;
1350   iph = (struct iphdr *)ptr;
1351   iph->tot_len = ntohs(skb->len-dev->hard_header_len);
1352 
1353   if(skb->len > dev->mtu)
1354   {
1355 /*      printk("Fragment!\n");*/
1356         ip_fragment(sk,skb,dev,0);
1357         IS_SKB(skb);
1358         kfree_skb(skb,FREE_WRITE);
1359         return;
1360   }
1361   
1362   ip_send_check(iph);
1363   ip_print(iph);
1364   skb->next = NULL;
1365 
1366   /* See if this is the one trashing our queue. Ross? */
1367   skb->magic = 1;
1368   if (!free) {
1369         skb->link3 = NULL;
1370         sk->packets_out++;
1371         cli();
1372         if (sk->send_head == NULL) {
1373                 sk->send_tail = skb;
1374                 sk->send_head = skb;
1375         } else {
1376                 /* See if we've got a problem. */
1377                 if (sk->send_tail == NULL) {
1378                         printk("IP: ***bug sk->send_tail == NULL != sk->send_head\n");
1379                         sort_send(sk);
1380                 } else {
1381                         sk->send_tail->link3 = skb;
1382                         sk->send_tail = skb;
1383                 }
1384         }
1385         sti();
1386         reset_timer(sk, TIME_WRITE,
1387                 backoff(sk->backoff) * (2 * sk->mdev + sk->rtt));
1388   } else {
1389         skb->sk = sk;
1390   }
1391 
1392   /* If the indicated interface is up and running, kick it. */
1393   if (dev->flags & IFF_UP) {
1394         if (sk != NULL) {
1395                 dev->queue_xmit(skb, dev, sk->priority);
1396         } 
1397         else {
1398                 dev->queue_xmit(skb, dev, SOPRI_NORMAL);
1399         }
1400   } else {
1401         if (free) kfree_skb(skb, FREE_WRITE);
1402   }
1403 }
1404 
1405 
1406 void
1407 ip_retransmit(struct sock *sk, int all)
     /* [previous][next][first][last][top][bottom][index][help] */
1408 {
1409   struct sk_buff * skb;
1410   struct proto *prot;
1411   struct device *dev;
1412 
1413   prot = sk->prot;
1414   skb = sk->send_head;
1415   while (skb != NULL) {
1416         dev = skb->dev;
1417         /* I know this can't happen but as it does.. */
1418         if(dev==NULL)
1419         {
1420                 printk("ip_retransmit: NULL device bug!\n");
1421                 goto oops;
1422         }
1423 
1424         IS_SKB(skb);
1425         
1426         /*
1427          * The rebuild_header function sees if the ARP is done.
1428          * If not it sends a new ARP request, and if so it builds
1429          * the header.
1430          */
1431         cli();  /* We might get interrupted by an arp reply here and fill
1432                    the frame in twice. Because of the technique used this
1433                    would be a little sad */
1434         if (!skb->arp) {
1435                 if (dev->rebuild_header(skb+1, dev)) {
1436                         sti();  /* Failed to rebuild - next */
1437                         if (!all) break;
1438                         skb = (struct sk_buff *)skb->link3;
1439                         continue;
1440                 }
1441         }
1442         skb->arp = 1;
1443         sti();
1444         skb->when = jiffies;
1445 
1446         /* If the interface is (still) up and running, kick it. */
1447         if (dev->flags & IFF_UP) {
1448                 if (sk) dev->queue_xmit(skb, dev, sk->priority);
1449         /*        else dev->queue_xmit(skb, dev, SOPRI_NORMAL ); CANNOT HAVE SK=NULL HERE */
1450         }
1451 
1452 oops:   sk->retransmits++;
1453         sk->prot->retransmits ++;
1454         if (!all) break;
1455 
1456         /* This should cut it off before we send too many packets. */
1457         if (sk->retransmits > sk->cong_window) break;
1458         skb = (struct sk_buff *)skb->link3;
1459   }
1460 
1461   /*
1462    * Increase the RTT time every time we retransmit. 
1463    * This will cause exponential back off on how hard we try to
1464    * get through again.  Once we get through, the rtt will settle
1465    * back down reasonably quickly.
1466    */
1467   sk->backoff++;
1468   reset_timer(sk, TIME_WRITE, backoff(sk->backoff) * (2 * sk->mdev + sk->rtt));
1469 }
1470 
1471 /* Backoff function - the subject of much research */
1472 int backoff(int n)
     /* [previous][next][first][last][top][bottom][index][help] */
1473 {
1474         /* Use binary exponential up to retry #4, and quadratic after that
1475          * This yields the sequence
1476          * 1, 2, 4, 8, 16, 25, 36, 49, 64, 81, 100 ...
1477          */
1478 
1479         if(n<0)
1480         {
1481                 printk("Backoff < 0!\n");
1482                 return 16;      /* Make up a value */
1483         }
1484         
1485         if(n <= 4)
1486                 return 1 << n;  /* Binary exponential back off */
1487         else
1488         {
1489                 if(n<255)
1490                         return n * n;   /* Quadratic back off */
1491                 else
1492                 {
1493                         printk("Overloaded backoff!\n");
1494                         return 255*255;
1495                 }
1496         }
1497 }
1498 

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