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