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

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