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
  29. ip_setsockopt
  30. ip_getsockopt

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

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