root/net/inet/ip.c

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

DEFINITIONS

This source file includes following definitions.
  1. ip_ioctl
  2. strict_route
  3. loose_route
  4. ip_route_check
  5. build_options
  6. ip_send
  7. ip_build_header
  8. do_options
  9. ip_fast_csum
  10. ip_compute_csum
  11. ip_csum
  12. ip_send_check
  13. ip_frag_create
  14. ip_find
  15. ip_free
  16. ip_expire
  17. ip_create
  18. ip_done
  19. ip_glue
  20. ip_defrag
  21. ip_fragment
  22. ip_forward
  23. ip_rcv
  24. ip_loopback
  25. ip_queue_xmit
  26. ip_mc_procinfo
  27. ip_setsockopt
  28. ip_getsockopt
  29. ip_rt_event
  30. ip_init

   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  *              Alan Cox, <gw4pts@gw4pts.ampr.org>
  14  *              Richard Underwood
  15  *
  16  * Fixes:
  17  *              Alan Cox        :       Commented a couple of minor bits of surplus code
  18  *              Alan Cox        :       Undefining IP_FORWARD doesn't include the code
  19  *                                      (just stops a compiler warning).
  20  *              Alan Cox        :       Frames with >=MAX_ROUTE record routes, strict routes or loose routes
  21  *                                      are junked rather than corrupting things.
  22  *              Alan Cox        :       Frames to bad broadcast subnets are dumped
  23  *                                      We used to process them non broadcast and
  24  *                                      boy could that cause havoc.
  25  *              Alan Cox        :       ip_forward sets the free flag on the
  26  *                                      new frame it queues. Still crap because
  27  *                                      it copies the frame but at least it
  28  *                                      doesn't eat memory too.
  29  *              Alan Cox        :       Generic queue code and memory fixes.
  30  *              Fred Van Kempen :       IP fragment support (borrowed from NET2E)
  31  *              Gerhard Koerting:       Forward fragmented frames correctly.
  32  *              Gerhard Koerting:       Fixes to my fix of the above 8-).
  33  *              Gerhard Koerting:       IP interface addressing fix.
  34  *              Linus Torvalds  :       More robustness checks
  35  *              Alan Cox        :       Even more checks: Still not as robust as it ought to be
  36  *              Alan Cox        :       Save IP header pointer for later
  37  *              Alan Cox        :       ip option setting
  38  *              Alan Cox        :       Use ip_tos/ip_ttl settings
  39  *              Alan Cox        :       Fragmentation bogosity removed
  40  *                                      (Thanks to Mark.Bush@prg.ox.ac.uk)
  41  *              Dmitry Gorodchanin :    Send of a raw packet crash fix.
  42  *              Alan Cox        :       Silly ip bug when an overlength
  43  *                                      fragment turns up. Now frees the
  44  *                                      queue.
  45  *              Linus Torvalds/ :       Memory leakage on fragmentation
  46  *              Alan Cox        :       handling.
  47  *              Gerhard Koerting:       Forwarding uses IP priority hints
  48  *              Teemu Rantanen  :       Fragment problems.
  49  *              Alan Cox        :       General cleanup, comments and reformat
  50  *              Alan Cox        :       SNMP statistics
  51  *              Alan Cox        :       BSD address rule semantics. Also see
  52  *                                      UDP as there is a nasty checksum issue
  53  *                                      if you do things the wrong way.
  54  *              Alan Cox        :       Always defrag, moved IP_FORWARD to the config.in file
  55  *              Alan Cox        :       IP options adjust sk->priority.
  56  *              Pedro Roque     :       Fix mtu/length error in ip_forward.
  57  *              Alan Cox        :       Avoid ip_chk_addr when possible.
  58  *      Richard Underwood       :       IP multicasting.
  59  *              Alan Cox        :       Cleaned up multicast handlers.
  60  *              Alan Cox        :       RAW sockets demultiplex in the BSD style.
  61  *              Gunther Mayer   :       Fix the SNMP reporting typo
  62  *              Alan Cox        :       Always in group 224.0.0.1
  63  *              Alan Cox        :       Multicast loopback error for 224.0.0.1
  64  *              Alan Cox        :       IP_MULTICAST_LOOP option.
  65  *              Alan Cox        :       Use notifiers.
  66  *
  67  * To Fix:
  68  *              IP option processing is mostly not needed. ip_forward needs to know about routing rules
  69  *              and time stamp but that's about all. Use the route mtu field here too
  70  *              IP fragmentation wants rewriting cleanly. The RFC815 algorithm is much more efficient
  71  *              and could be made very efficient with the addition of some virtual memory hacks to permit
  72  *              the allocation of a buffer that can then be 'grown' by twiddling page tables.
  73  *              Output fragmentation wants updating along with the buffer management to use a single 
  74  *              interleaved copy algorithm so that fragmenting has a one copy overhead. Actual packet
  75  *              output should probably do its own fragmentation at the UDP/RAW layer. TCP shouldn't cause
  76  *              fragmentation anyway.
  77  *
  78  *              This program is free software; you can redistribute it and/or
  79  *              modify it under the terms of the GNU General Public License
  80  *              as published by the Free Software Foundation; either version
  81  *              2 of the License, or (at your option) any later version.
  82  */
  83 
  84 #include <asm/segment.h>
  85 #include <asm/system.h>
  86 #include <linux/types.h>
  87 #include <linux/kernel.h>
  88 #include <linux/sched.h>
  89 #include <linux/string.h>
  90 #include <linux/errno.h>
  91 #include <linux/config.h>
  92 
  93 #include <linux/socket.h>
  94 #include <linux/sockios.h>
  95 #include <linux/in.h>
  96 #include <linux/inet.h>
  97 #include <linux/netdevice.h>
  98 #include <linux/etherdevice.h>
  99 
 100 #include "snmp.h"
 101 #include "ip.h"
 102 #include "protocol.h"
 103 #include "route.h"
 104 #include "tcp.h"
 105 #include <linux/skbuff.h>
 106 #include "sock.h"
 107 #include "arp.h"
 108 #include "icmp.h"
 109 #include "raw.h"
 110 #include "igmp.h"
 111 #include <linux/ip_fw.h>
 112 
 113 #define CONFIG_IP_DEFRAG
 114 
 115 extern int last_retran;
 116 extern void sort_send(struct sock *sk);
 117 
 118 #define min(a,b)        ((a)<(b)?(a):(b))
 119 #define LOOPBACK(x)     (((x) & htonl(0xff000000)) == htonl(0x7f000000))
 120 
 121 /*
 122  *      SNMP management statistics
 123  */
 124 
 125 #ifdef CONFIG_IP_FORWARD
 126 struct ip_mib ip_statistics={1,64,};    /* Forwarding=Yes, Default TTL=64 */
 127 #else
 128 struct ip_mib ip_statistics={0,64,};    /* Forwarding=No, Default TTL=64 */
 129 #endif
 130 
 131 /*
 132  *      Handle the issuing of an ioctl() request
 133  *      for the ip device. This is scheduled to
 134  *      disappear
 135  */
 136 
 137 int ip_ioctl(struct sock *sk, int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 138 {
 139         switch(cmd)
 140         {
 141                 default:
 142                         return(-EINVAL);
 143         }
 144 }
 145 
 146 
 147 /* these two routines will do routing. */
 148 
 149 static void
 150 strict_route(struct iphdr *iph, struct options *opt)
     /* [previous][next][first][last][top][bottom][index][help] */
 151 {
 152 }
 153 
 154 
 155 static void
 156 loose_route(struct iphdr *iph, struct options *opt)
     /* [previous][next][first][last][top][bottom][index][help] */
 157 {
 158 }
 159 
 160 
 161 
 162 
 163 /* This routine will check to see if we have lost a gateway. */
 164 void
 165 ip_route_check(unsigned long daddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 166 {
 167 }
 168 
 169 
 170 #if 0
 171 /* this routine puts the options at the end of an ip header. */
 172 static int
 173 build_options(struct iphdr *iph, struct options *opt)
     /* [previous][next][first][last][top][bottom][index][help] */
 174 {
 175   unsigned char *ptr;
 176   /* currently we don't support any options. */
 177   ptr = (unsigned char *)(iph+1);
 178   *ptr = 0;
 179   return (4);
 180 }
 181 #endif
 182 
 183 
 184 /*
 185  *      Take an skb, and fill in the MAC header.
 186  */
 187 
 188 static int ip_send(struct sk_buff *skb, unsigned long daddr, int len, struct device *dev, unsigned long saddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 189 {
 190         int mac = 0;
 191 
 192         skb->dev = dev;
 193         skb->arp = 1;
 194         if (dev->hard_header)
 195         {
 196                 /*
 197                  *      Build a hardware header. Source address is our mac, destination unknown
 198                  *      (rebuild header will sort this out)
 199                  */
 200                 mac = dev->hard_header(skb->data, dev, ETH_P_IP, NULL, NULL, len, skb);
 201                 if (mac < 0)
 202                 {
 203                         mac = -mac;
 204                         skb->arp = 0;
 205                         skb->raddr = daddr;     /* next routing address */
 206                 }
 207         }
 208         return mac;
 209 }
 210 
 211 int ip_id_count = 0;
 212 
 213 /*
 214  * This routine builds the appropriate hardware/IP headers for
 215  * the routine.  It assumes that if *dev != NULL then the
 216  * protocol knows what it's doing, otherwise it uses the
 217  * routing/ARP tables to select a device struct.
 218  */
 219 int ip_build_header(struct sk_buff *skb, unsigned long saddr, unsigned long daddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 220                 struct device **dev, int type, struct options *opt, int len, int tos, int ttl)
 221 {
 222         static struct options optmem;
 223         struct iphdr *iph;
 224         struct rtable *rt;
 225         unsigned char *buff;
 226         unsigned long raddr;
 227         int tmp;
 228         unsigned long src;
 229 
 230         buff = skb->data;
 231 
 232         /*
 233          *      See if we need to look up the device.
 234          */
 235 
 236 #ifdef CONFIG_INET_MULTICAST    
 237         if(MULTICAST(daddr) && *dev==NULL && skb->sk && *skb->sk->ip_mc_name)
 238                 *dev=dev_get(skb->sk->ip_mc_name);
 239 #endif
 240         if (*dev == NULL)
 241         {
 242                 if(skb->localroute)
 243                         rt = ip_rt_local(daddr, &optmem, &src);
 244                 else
 245                         rt = ip_rt_route(daddr, &optmem, &src);
 246                 if (rt == NULL)
 247                 {
 248                         ip_statistics.IpOutNoRoutes++;
 249                         return(-ENETUNREACH);
 250                 }
 251 
 252                 *dev = rt->rt_dev;
 253                 /*
 254                  *      If the frame is from us and going off machine it MUST MUST MUST
 255                  *      have the output device ip address and never the loopback
 256                  */
 257                 if (LOOPBACK(saddr) && !LOOPBACK(daddr))
 258                         saddr = src;/*rt->rt_dev->pa_addr;*/
 259                 raddr = rt->rt_gateway;
 260 
 261                 opt = &optmem;
 262         }
 263         else
 264         {
 265                 /*
 266                  *      We still need the address of the first hop.
 267                  */
 268                 if(skb->localroute)
 269                         rt = ip_rt_local(daddr, &optmem, &src);
 270                 else
 271                         rt = ip_rt_route(daddr, &optmem, &src);
 272                 /*
 273                  *      If the frame is from us and going off machine it MUST MUST MUST
 274                  *      have the output device ip address and never the loopback
 275                  */
 276                 if (LOOPBACK(saddr) && !LOOPBACK(daddr))
 277                         saddr = src;/*rt->rt_dev->pa_addr;*/
 278 
 279                 raddr = (rt == NULL) ? 0 : rt->rt_gateway;
 280         }
 281 
 282         /*
 283          *      No source addr so make it our addr
 284          */
 285         if (saddr == 0)
 286                 saddr = src;
 287 
 288         /*
 289          *      No gateway so aim at the real destination
 290          */
 291         if (raddr == 0)
 292                 raddr = daddr;
 293 
 294         /*
 295          *      Now build the MAC header.
 296          */
 297 
 298         tmp = ip_send(skb, raddr, len, *dev, saddr);
 299         buff += tmp;
 300         len -= tmp;
 301 
 302         /*
 303          *      Book keeping
 304          */
 305 
 306         skb->dev = *dev;
 307         skb->saddr = saddr;
 308         if (skb->sk)
 309                 skb->sk->saddr = saddr;
 310 
 311         /*
 312          *      Now build the IP header.
 313          */
 314 
 315         /*
 316          *      If we are using IPPROTO_RAW, then we don't need an IP header, since
 317          *      one is being supplied to us by the user
 318          */
 319 
 320         if(type == IPPROTO_RAW)
 321                 return (tmp);
 322 
 323         iph = (struct iphdr *)buff;
 324         iph->version  = 4;
 325         iph->tos      = tos;
 326         iph->frag_off = 0;
 327         iph->ttl      = ttl;
 328         iph->daddr    = daddr;
 329         iph->saddr    = saddr;
 330         iph->protocol = type;
 331         iph->ihl      = 5;
 332         skb->ip_hdr   = iph;
 333 
 334         /* Setup the IP options. */
 335 #ifdef Not_Yet_Avail
 336         build_options(iph, opt);
 337 #endif
 338 
 339         return(20 + tmp);       /* IP header plus MAC header size */
 340 }
 341 
 342 
 343 static int
 344 do_options(struct iphdr *iph, struct options *opt)
     /* [previous][next][first][last][top][bottom][index][help] */
 345 {
 346   unsigned char *buff;
 347   int done = 0;
 348   int i, len = sizeof(struct iphdr);
 349 
 350   /* Zero out the options. */
 351   opt->record_route.route_size = 0;
 352   opt->loose_route.route_size  = 0;
 353   opt->strict_route.route_size = 0;
 354   opt->tstamp.ptr              = 0;
 355   opt->security                = 0;
 356   opt->compartment             = 0;
 357   opt->handling                = 0;
 358   opt->stream                  = 0;
 359   opt->tcc                     = 0;
 360   return(0);
 361 
 362   /* Advance the pointer to start at the options. */
 363   buff = (unsigned char *)(iph + 1);
 364 
 365   /* Now start the processing. */
 366   while (!done && len < iph->ihl*4) switch(*buff) {
 367         case IPOPT_END:
 368                 done = 1;
 369                 break;
 370         case IPOPT_NOOP:
 371                 buff++;
 372                 len++;
 373                 break;
 374         case IPOPT_SEC:
 375                 buff++;
 376                 if (*buff != 11) return(1);
 377                 buff++;
 378                 opt->security = ntohs(*(unsigned short *)buff);
 379                 buff += 2;
 380                 opt->compartment = ntohs(*(unsigned short *)buff);
 381                 buff += 2;
 382                 opt->handling = ntohs(*(unsigned short *)buff);
 383                 buff += 2;
 384                 opt->tcc = ((*buff) << 16) + ntohs(*(unsigned short *)(buff+1));
 385                 buff += 3;
 386                 len += 11;
 387                 break;
 388         case IPOPT_LSRR:
 389                 buff++;
 390                 if ((*buff - 3)% 4 != 0) return(1);
 391                 len += *buff;
 392                 opt->loose_route.route_size = (*buff -3)/4;
 393                 buff++;
 394                 if (*buff % 4 != 0) return(1);
 395                 opt->loose_route.pointer = *buff/4 - 1;
 396                 buff++;
 397                 buff++;
 398                 for (i = 0; i < opt->loose_route.route_size; i++) {
 399                         if(i>=MAX_ROUTE)
 400                                 return(1);
 401                         opt->loose_route.route[i] = *(unsigned long *)buff;
 402                         buff += 4;
 403                 }
 404                 break;
 405         case IPOPT_SSRR:
 406                 buff++;
 407                 if ((*buff - 3)% 4 != 0) return(1);
 408                 len += *buff;
 409                 opt->strict_route.route_size = (*buff -3)/4;
 410                 buff++;
 411                 if (*buff % 4 != 0) return(1);
 412                 opt->strict_route.pointer = *buff/4 - 1;
 413                 buff++;
 414                 buff++;
 415                 for (i = 0; i < opt->strict_route.route_size; i++) {
 416                         if(i>=MAX_ROUTE)
 417                                 return(1);
 418                         opt->strict_route.route[i] = *(unsigned long *)buff;
 419                         buff += 4;
 420                 }
 421                 break;
 422         case IPOPT_RR:
 423                 buff++;
 424                 if ((*buff - 3)% 4 != 0) return(1);
 425                 len += *buff;
 426                 opt->record_route.route_size = (*buff -3)/4;
 427                 buff++;
 428                 if (*buff % 4 != 0) return(1);
 429                 opt->record_route.pointer = *buff/4 - 1;
 430                 buff++;
 431                 buff++;
 432                 for (i = 0; i < opt->record_route.route_size; i++) {
 433                         if(i>=MAX_ROUTE)
 434                                 return 1;
 435                         opt->record_route.route[i] = *(unsigned long *)buff;
 436                         buff += 4;
 437                 }
 438                 break;
 439         case IPOPT_SID:
 440                 len += 4;
 441                 buff +=2;
 442                 opt->stream = *(unsigned short *)buff;
 443                 buff += 2;
 444                 break;
 445         case IPOPT_TIMESTAMP:
 446                 buff++;
 447                 len += *buff;
 448                 if (*buff % 4 != 0) return(1);
 449                 opt->tstamp.len = *buff / 4 - 1;
 450                 buff++;
 451                 if ((*buff - 1) % 4 != 0) return(1);
 452                 opt->tstamp.ptr = (*buff-1)/4;
 453                 buff++;
 454                 opt->tstamp.x.full_char = *buff;
 455                 buff++;
 456                 for (i = 0; i < opt->tstamp.len; i++) {
 457                         opt->tstamp.data[i] = *(unsigned long *)buff;
 458                         buff += 4;
 459                 }
 460                 break;
 461         default:
 462                 return(1);
 463   }
 464 
 465   if (opt->record_route.route_size == 0) {
 466         if (opt->strict_route.route_size != 0) {
 467                 memcpy(&(opt->record_route), &(opt->strict_route),
 468                                              sizeof(opt->record_route));
 469         } else if (opt->loose_route.route_size != 0) {
 470                 memcpy(&(opt->record_route), &(opt->loose_route),
 471                                              sizeof(opt->record_route));
 472         }
 473   }
 474 
 475   if (opt->strict_route.route_size != 0 &&
 476       opt->strict_route.route_size != opt->strict_route.pointer) {
 477         strict_route(iph, opt);
 478         return(0);
 479   }
 480 
 481   if (opt->loose_route.route_size != 0 &&
 482       opt->loose_route.route_size != opt->loose_route.pointer) {
 483         loose_route(iph, opt);
 484         return(0);
 485   }
 486 
 487   return(0);
 488 }
 489 
 490 /*
 491  *      This is a version of ip_compute_csum() optimized for IP headers, which
 492  *      always checksum on 4 octet boundaries.
 493  */
 494 
 495 static inline unsigned short ip_fast_csum(unsigned char * buff, int wlen)
     /* [previous][next][first][last][top][bottom][index][help] */
 496 {
 497         unsigned long sum = 0;
 498 
 499         if (wlen)
 500         {
 501         unsigned long bogus;
 502          __asm__("clc\n"
 503                 "1:\t"
 504                 "lodsl\n\t"
 505                 "adcl %3, %0\n\t"
 506                 "decl %2\n\t"
 507                 "jne 1b\n\t"
 508                 "adcl $0, %0\n\t"
 509                 "movl %0, %3\n\t"
 510                 "shrl $16, %3\n\t"
 511                 "addw %w3, %w0\n\t"
 512                 "adcw $0, %w0"
 513             : "=r" (sum), "=S" (buff), "=r" (wlen), "=a" (bogus)
 514             : "0"  (sum),  "1" (buff),  "2" (wlen));
 515         }
 516         return (~sum) & 0xffff;
 517 }
 518 
 519 /*
 520  * This routine does all the checksum computations that don't
 521  * require anything special (like copying or special headers).
 522  */
 523 
 524 unsigned short ip_compute_csum(unsigned char * buff, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 525 {
 526         unsigned long sum = 0;
 527 
 528         /* Do the first multiple of 4 bytes and convert to 16 bits. */
 529         if (len > 3)
 530         {
 531                 __asm__("clc\n"
 532                 "1:\t"
 533                 "lodsl\n\t"
 534                 "adcl %%eax, %%ebx\n\t"
 535                 "loop 1b\n\t"
 536                 "adcl $0, %%ebx\n\t"
 537                 "movl %%ebx, %%eax\n\t"
 538                 "shrl $16, %%eax\n\t"
 539                 "addw %%ax, %%bx\n\t"
 540                 "adcw $0, %%bx"
 541                 : "=b" (sum) , "=S" (buff)
 542                 : "0" (sum), "c" (len >> 2) ,"1" (buff)
 543                 : "ax", "cx", "si", "bx" );
 544         }
 545         if (len & 2)
 546         {
 547                 __asm__("lodsw\n\t"
 548                 "addw %%ax, %%bx\n\t"
 549                 "adcw $0, %%bx"
 550                 : "=b" (sum), "=S" (buff)
 551                 : "0" (sum), "1" (buff)
 552                 : "bx", "ax", "si");
 553         }
 554         if (len & 1)
 555         {
 556                 __asm__("lodsb\n\t"
 557                 "movb $0, %%ah\n\t"
 558                 "addw %%ax, %%bx\n\t"
 559                 "adcw $0, %%bx"
 560                 : "=b" (sum), "=S" (buff)
 561                 : "0" (sum), "1" (buff)
 562                 : "bx", "ax", "si");
 563         }
 564         sum =~sum;
 565         return(sum & 0xffff);
 566 }
 567 
 568 /*
 569  *      Check the header of an incoming IP datagram.  This version is still used in slhc.c.
 570  */
 571 
 572 int ip_csum(struct iphdr *iph)
     /* [previous][next][first][last][top][bottom][index][help] */
 573 {
 574         return ip_fast_csum((unsigned char *)iph, iph->ihl);
 575 }
 576 
 577 /*
 578  *      Generate a checksum for an outgoing IP datagram.
 579  */
 580 
 581 void ip_send_check(struct iphdr *iph)
     /* [previous][next][first][last][top][bottom][index][help] */
 582 {
 583         iph->check = 0;
 584         iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
 585 }
 586 
 587 /************************ Fragment Handlers From NET2E **********************************/
 588 
 589 
 590 /*
 591  *      This fragment handler is a bit of a heap. On the other hand it works quite
 592  *      happily and handles things quite well.
 593  */
 594 
 595 static struct ipq *ipqueue = NULL;              /* IP fragment queue    */
 596 
 597 /*
 598  *      Create a new fragment entry.
 599  */
 600 
 601 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] */
 602 {
 603         struct ipfrag *fp;
 604 
 605         fp = (struct ipfrag *) kmalloc(sizeof(struct ipfrag), GFP_ATOMIC);
 606         if (fp == NULL)
 607         {
 608                 printk("IP: frag_create: no memory left !\n");
 609                 return(NULL);
 610         }
 611         memset(fp, 0, sizeof(struct ipfrag));
 612 
 613         /* Fill in the structure. */
 614         fp->offset = offset;
 615         fp->end = end;
 616         fp->len = end - offset;
 617         fp->skb = skb;
 618         fp->ptr = ptr;
 619 
 620         return(fp);
 621 }
 622 
 623 
 624 /*
 625  *      Find the correct entry in the "incomplete datagrams" queue for
 626  *      this IP datagram, and return the queue entry address if found.
 627  */
 628 
 629 static struct ipq *ip_find(struct iphdr *iph)
     /* [previous][next][first][last][top][bottom][index][help] */
 630 {
 631         struct ipq *qp;
 632         struct ipq *qplast;
 633 
 634         cli();
 635         qplast = NULL;
 636         for(qp = ipqueue; qp != NULL; qplast = qp, qp = qp->next)
 637         {
 638                 if (iph->id== qp->iph->id && iph->saddr == qp->iph->saddr &&
 639                         iph->daddr == qp->iph->daddr && iph->protocol == qp->iph->protocol)
 640                 {
 641                         del_timer(&qp->timer);  /* So it doesn't vanish on us. The timer will be reset anyway */
 642                         sti();
 643                         return(qp);
 644                 }
 645         }
 646         sti();
 647         return(NULL);
 648 }
 649 
 650 
 651 /*
 652  *      Remove an entry from the "incomplete datagrams" queue, either
 653  *      because we completed, reassembled and processed it, or because
 654  *      it timed out.
 655  */
 656 
 657 static void ip_free(struct ipq *qp)
     /* [previous][next][first][last][top][bottom][index][help] */
 658 {
 659         struct ipfrag *fp;
 660         struct ipfrag *xp;
 661 
 662         /*
 663          * Stop the timer for this entry.
 664          */
 665 
 666         del_timer(&qp->timer);
 667 
 668         /* Remove this entry from the "incomplete datagrams" queue. */
 669         cli();
 670         if (qp->prev == NULL)
 671         {
 672                 ipqueue = qp->next;
 673                 if (ipqueue != NULL)
 674                         ipqueue->prev = NULL;
 675         }
 676         else
 677         {
 678                 qp->prev->next = qp->next;
 679                 if (qp->next != NULL)
 680                         qp->next->prev = qp->prev;
 681         }
 682 
 683         /* Release all fragment data. */
 684 
 685         fp = qp->fragments;
 686         while (fp != NULL)
 687         {
 688                 xp = fp->next;
 689                 IS_SKB(fp->skb);
 690                 kfree_skb(fp->skb,FREE_READ);
 691                 kfree_s(fp, sizeof(struct ipfrag));
 692                 fp = xp;
 693         }
 694 
 695         /* Release the MAC header. */
 696         kfree_s(qp->mac, qp->maclen);
 697 
 698         /* Release the IP header. */
 699         kfree_s(qp->iph, qp->ihlen + 8);
 700 
 701         /* Finally, release the queue descriptor itself. */
 702         kfree_s(qp, sizeof(struct ipq));
 703         sti();
 704 }
 705 
 706 
 707 /*
 708  *      Oops- a fragment queue timed out.  Kill it and send an ICMP reply.
 709  */
 710 
 711 static void ip_expire(unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 712 {
 713         struct ipq *qp;
 714 
 715         qp = (struct ipq *)arg;
 716 
 717         /*
 718          *      Send an ICMP "Fragment Reassembly Timeout" message.
 719          */
 720 
 721         ip_statistics.IpReasmTimeout++;
 722         ip_statistics.IpReasmFails++;   
 723         /* This if is always true... shrug */
 724         if(qp->fragments!=NULL)
 725                 icmp_send(qp->fragments->skb,ICMP_TIME_EXCEEDED,
 726                                 ICMP_EXC_FRAGTIME, qp->dev);
 727 
 728         /*
 729          *      Nuke the fragment queue.
 730          */
 731         ip_free(qp);
 732 }
 733 
 734 
 735 /*
 736  *      Add an entry to the 'ipq' queue for a newly received IP datagram.
 737  *      We will (hopefully :-) receive all other fragments of this datagram
 738  *      in time, so we just create a queue for this datagram, in which we
 739  *      will insert the received fragments at their respective positions.
 740  */
 741 
 742 static struct ipq *ip_create(struct sk_buff *skb, struct iphdr *iph, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 743 {
 744         struct ipq *qp;
 745         int maclen;
 746         int ihlen;
 747 
 748         qp = (struct ipq *) kmalloc(sizeof(struct ipq), GFP_ATOMIC);
 749         if (qp == NULL)
 750         {
 751                 printk("IP: create: no memory left !\n");
 752                 return(NULL);
 753                 skb->dev = qp->dev;
 754         }
 755         memset(qp, 0, sizeof(struct ipq));
 756 
 757         /*
 758          *      Allocate memory for the MAC header.
 759          *
 760          *      FIXME: We have a maximum MAC address size limit and define
 761          *      elsewhere. We should use it here and avoid the 3 kmalloc() calls
 762          */
 763 
 764         maclen = ((unsigned long) iph) - ((unsigned long) skb->data);
 765         qp->mac = (unsigned char *) kmalloc(maclen, GFP_ATOMIC);
 766         if (qp->mac == NULL)
 767         {
 768                 printk("IP: create: no memory left !\n");
 769                 kfree_s(qp, sizeof(struct ipq));
 770                 return(NULL);
 771         }
 772 
 773         /*
 774          *      Allocate memory for the IP header (plus 8 octets for ICMP).
 775          */
 776 
 777         ihlen = (iph->ihl * sizeof(unsigned long));
 778         qp->iph = (struct iphdr *) kmalloc(ihlen + 8, GFP_ATOMIC);
 779         if (qp->iph == NULL)
 780         {
 781                 printk("IP: create: no memory left !\n");
 782                 kfree_s(qp->mac, maclen);
 783                 kfree_s(qp, sizeof(struct ipq));
 784                 return(NULL);
 785         }
 786 
 787         /* Fill in the structure. */
 788         memcpy(qp->mac, skb->data, maclen);
 789         memcpy(qp->iph, iph, ihlen + 8);
 790         qp->len = 0;
 791         qp->ihlen = ihlen;
 792         qp->maclen = maclen;
 793         qp->fragments = NULL;
 794         qp->dev = dev;
 795 
 796         /* Start a timer for this entry. */
 797         qp->timer.expires = IP_FRAG_TIME;               /* about 30 seconds     */
 798         qp->timer.data = (unsigned long) qp;            /* pointer to queue     */
 799         qp->timer.function = ip_expire;                 /* expire function      */
 800         add_timer(&qp->timer);
 801 
 802         /* Add this entry to the queue. */
 803         qp->prev = NULL;
 804         cli();
 805         qp->next = ipqueue;
 806         if (qp->next != NULL)
 807                 qp->next->prev = qp;
 808         ipqueue = qp;
 809         sti();
 810         return(qp);
 811 }
 812 
 813 
 814 /*
 815  *      See if a fragment queue is complete.
 816  */
 817 
 818 static int ip_done(struct ipq *qp)
     /* [previous][next][first][last][top][bottom][index][help] */
 819 {
 820         struct ipfrag *fp;
 821         int offset;
 822 
 823         /* Only possible if we received the final fragment. */
 824         if (qp->len == 0)
 825                 return(0);
 826 
 827         /* Check all fragment offsets to see if they connect. */
 828         fp = qp->fragments;
 829         offset = 0;
 830         while (fp != NULL)
 831         {
 832                 if (fp->offset > offset)
 833                         return(0);      /* fragment(s) missing */
 834                 offset = fp->end;
 835                 fp = fp->next;
 836         }
 837 
 838         /* All fragments are present. */
 839         return(1);
 840 }
 841 
 842 
 843 /*
 844  *      Build a new IP datagram from all its fragments.
 845  *
 846  *      FIXME: We copy here because we lack an effective way of handling lists
 847  *      of bits on input. Until the new skb data handling is in I'm not going
 848  *      to touch this with a bargepole. This also causes a 4Kish limit on
 849  *      packet sizes.
 850  */
 851 
 852 static struct sk_buff *ip_glue(struct ipq *qp)
     /* [previous][next][first][last][top][bottom][index][help] */
 853 {
 854         struct sk_buff *skb;
 855         struct iphdr *iph;
 856         struct ipfrag *fp;
 857         unsigned char *ptr;
 858         int count, len;
 859 
 860         /*
 861          *      Allocate a new buffer for the datagram.
 862          */
 863 
 864         len = qp->maclen + qp->ihlen + qp->len;
 865 
 866         if ((skb = alloc_skb(len,GFP_ATOMIC)) == NULL)
 867         {
 868                 ip_statistics.IpReasmFails++;
 869                 printk("IP: queue_glue: no memory for gluing queue 0x%X\n", (int) qp);
 870                 ip_free(qp);
 871                 return(NULL);
 872         }
 873 
 874         /* Fill in the basic details. */
 875         skb->len = (len - qp->maclen);
 876         skb->h.raw = skb->data;
 877         skb->free = 1;
 878 
 879         /* Copy the original MAC and IP headers into the new buffer. */
 880         ptr = (unsigned char *) skb->h.raw;
 881         memcpy(ptr, ((unsigned char *) qp->mac), qp->maclen);
 882         ptr += qp->maclen;
 883         memcpy(ptr, ((unsigned char *) qp->iph), qp->ihlen);
 884         ptr += qp->ihlen;
 885         skb->h.raw += qp->maclen;
 886 
 887         count = 0;
 888 
 889         /* Copy the data portions of all fragments into the new buffer. */
 890         fp = qp->fragments;
 891         while(fp != NULL)
 892         {
 893                 if(count+fp->len > skb->len)
 894                 {
 895                         printk("Invalid fragment list: Fragment over size.\n");
 896                         ip_free(qp);
 897                         kfree_skb(skb,FREE_WRITE);
 898                         ip_statistics.IpReasmFails++;
 899                         return NULL;
 900                 }
 901                 memcpy((ptr + fp->offset), fp->ptr, fp->len);
 902                 count += fp->len;
 903                 fp = fp->next;
 904         }
 905 
 906         /* We glued together all fragments, so remove the queue entry. */
 907         ip_free(qp);
 908 
 909         /* Done with all fragments. Fixup the new IP header. */
 910         iph = skb->h.iph;
 911         iph->frag_off = 0;
 912         iph->tot_len = htons((iph->ihl * sizeof(unsigned long)) + count);
 913         skb->ip_hdr = iph;
 914 
 915         ip_statistics.IpReasmOKs++;
 916         return(skb);
 917 }
 918 
 919 
 920 /*
 921  *      Process an incoming IP datagram fragment.
 922  */
 923 
 924 static struct sk_buff *ip_defrag(struct iphdr *iph, struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 925 {
 926         struct ipfrag *prev, *next;
 927         struct ipfrag *tfp;
 928         struct ipq *qp;
 929         struct sk_buff *skb2;
 930         unsigned char *ptr;
 931         int flags, offset;
 932         int i, ihl, end;
 933 
 934         ip_statistics.IpReasmReqds++;
 935 
 936         /* Find the entry of this IP datagram in the "incomplete datagrams" queue. */
 937         qp = ip_find(iph);
 938 
 939         /* Is this a non-fragmented datagram? */
 940         offset = ntohs(iph->frag_off);
 941         flags = offset & ~IP_OFFSET;
 942         offset &= IP_OFFSET;
 943         if (((flags & IP_MF) == 0) && (offset == 0))
 944         {
 945                 if (qp != NULL)
 946                         ip_free(qp);    /* Huh? How could this exist?? */
 947                 return(skb);
 948         }
 949 
 950         offset <<= 3;           /* offset is in 8-byte chunks */
 951 
 952         /*
 953          * If the queue already existed, keep restarting its timer as long
 954          * as we still are receiving fragments.  Otherwise, create a fresh
 955          * queue entry.
 956          */
 957 
 958         if (qp != NULL)
 959         {
 960                 del_timer(&qp->timer);
 961                 qp->timer.expires = IP_FRAG_TIME;       /* about 30 seconds */
 962                 qp->timer.data = (unsigned long) qp;    /* pointer to queue */
 963                 qp->timer.function = ip_expire;         /* expire function */
 964                 add_timer(&qp->timer);
 965         }
 966         else
 967         {
 968                 /*
 969                  *      If we failed to create it, then discard the frame
 970                  */
 971                 if ((qp = ip_create(skb, iph, dev)) == NULL)
 972                 {
 973                         skb->sk = NULL;
 974                         kfree_skb(skb, FREE_READ);
 975                         ip_statistics.IpReasmFails++;
 976                         return NULL;
 977                 }
 978         }
 979 
 980         /*
 981          *      Determine the position of this fragment.
 982          */
 983 
 984         ihl = (iph->ihl * sizeof(unsigned long));
 985         end = offset + ntohs(iph->tot_len) - ihl;
 986 
 987         /*
 988          *      Point into the IP datagram 'data' part.
 989          */
 990 
 991         ptr = skb->data + dev->hard_header_len + ihl;
 992 
 993         /*
 994          *      Is this the final fragment?
 995          */
 996 
 997         if ((flags & IP_MF) == 0)
 998                 qp->len = end;
 999 
1000         /*
1001          *      Find out which fragments are in front and at the back of us
1002          *      in the chain of fragments so far.  We must know where to put
1003          *      this fragment, right?
1004          */
1005 
1006         prev = NULL;
1007         for(next = qp->fragments; next != NULL; next = next->next)
1008         {
1009                 if (next->offset > offset)
1010                         break;  /* bingo! */
1011                 prev = next;
1012         }
1013 
1014         /*
1015          *      We found where to put this one.
1016          *      Check for overlap with preceding fragment, and, if needed,
1017          *      align things so that any overlaps are eliminated.
1018          */
1019         if (prev != NULL && offset < prev->end)
1020         {
1021                 i = prev->end - offset;
1022                 offset += i;    /* ptr into datagram */
1023                 ptr += i;       /* ptr into fragment data */
1024         }
1025 
1026         /*
1027          * Look for overlap with succeeding segments.
1028          * If we can merge fragments, do it.
1029          */
1030 
1031         for(; next != NULL; next = tfp)
1032         {
1033                 tfp = next->next;
1034                 if (next->offset >= end)
1035                         break;          /* no overlaps at all */
1036 
1037                 i = end - next->offset;                 /* overlap is 'i' bytes */
1038                 next->len -= i;                         /* so reduce size of    */
1039                 next->offset += i;                      /* next fragment        */
1040                 next->ptr += i;
1041 
1042                 /*
1043                  *      If we get a frag size of <= 0, remove it and the packet
1044                  *      that it goes with.
1045                  */
1046                 if (next->len <= 0)
1047                 {
1048                         if (next->prev != NULL)
1049                                 next->prev->next = next->next;
1050                         else
1051                                 qp->fragments = next->next;
1052 
1053                         if (tfp->next != NULL)
1054                                 next->next->prev = next->prev;
1055 
1056                         kfree_skb(next->skb,FREE_READ);
1057                         kfree_s(next, sizeof(struct ipfrag));
1058                 }
1059         }
1060 
1061         /*
1062          *      Insert this fragment in the chain of fragments.
1063          */
1064 
1065         tfp = NULL;
1066         tfp = ip_frag_create(offset, end, skb, ptr);
1067 
1068         /*
1069          *      No memory to save the fragment - so throw the lot
1070          */
1071 
1072         if (!tfp)
1073         {
1074                 skb->sk = NULL;
1075                 kfree_skb(skb, FREE_READ);
1076                 return NULL;
1077         }
1078         tfp->prev = prev;
1079         tfp->next = next;
1080         if (prev != NULL)
1081                 prev->next = tfp;
1082         else
1083                 qp->fragments = tfp;
1084 
1085         if (next != NULL)
1086                 next->prev = tfp;
1087 
1088         /*
1089          *      OK, so we inserted this new fragment into the chain.
1090          *      Check if we now have a full IP datagram which we can
1091          *      bump up to the IP layer...
1092          */
1093 
1094         if (ip_done(qp))
1095         {
1096                 skb2 = ip_glue(qp);             /* glue together the fragments */
1097                 return(skb2);
1098         }
1099         return(NULL);
1100 }
1101 
1102 
1103 /*
1104  *      This IP datagram is too large to be sent in one piece.  Break it up into
1105  *      smaller pieces (each of size equal to the MAC header plus IP header plus
1106  *      a block of the data of the original IP data part) that will yet fit in a
1107  *      single device frame, and queue such a frame for sending by calling the
1108  *      ip_queue_xmit().  Note that this is recursion, and bad things will happen
1109  *      if this function causes a loop...
1110  *
1111  *      Yes this is inefficient, feel free to submit a quicker one.
1112  *
1113  *      **Protocol Violation**
1114  *      We copy all the options to each fragment. !FIXME!
1115  */
1116 void ip_fragment(struct sock *sk, struct sk_buff *skb, struct device *dev, int is_frag)
     /* [previous][next][first][last][top][bottom][index][help] */
1117 {
1118         struct iphdr *iph;
1119         unsigned char *raw;
1120         unsigned char *ptr;
1121         struct sk_buff *skb2;
1122         int left, mtu, hlen, len;
1123         int offset;
1124         unsigned long flags;
1125 
1126         /*
1127          *      Point into the IP datagram header.
1128          */
1129 
1130         raw = skb->data;
1131         iph = (struct iphdr *) (raw + dev->hard_header_len);
1132 
1133         skb->ip_hdr = iph;
1134 
1135         /*
1136          *      Setup starting values.
1137          */
1138 
1139         hlen = (iph->ihl * sizeof(unsigned long));
1140         left = ntohs(iph->tot_len) - hlen;      /* Space per frame */
1141         hlen += dev->hard_header_len;           /* Total header size */
1142         mtu = (dev->mtu - hlen);                /* Size of data space */
1143         ptr = (raw + hlen);                     /* Where to start from */
1144 
1145         /*
1146          *      Check for any "DF" flag. [DF means do not fragment]
1147          */
1148 
1149         if (ntohs(iph->frag_off) & IP_DF)
1150         {
1151                 ip_statistics.IpFragFails++;
1152                 icmp_send(skb,ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, dev);
1153                 return;
1154         }
1155 
1156         /*
1157          *      The protocol doesn't seem to say what to do in the case that the
1158          *      frame + options doesn't fit the mtu. As it used to fall down dead
1159          *      in this case we were fortunate it didn't happen
1160          */
1161 
1162         if(mtu<8)
1163         {
1164                 /* It's wrong but its better than nothing */
1165                 icmp_send(skb,ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED,dev);
1166                 ip_statistics.IpFragFails++;
1167                 return;
1168         }
1169 
1170         /*
1171          *      Fragment the datagram.
1172          */
1173 
1174         /*
1175          *      The initial offset is 0 for a complete frame. When
1176          *      fragmenting fragments its wherever this one starts.
1177          */
1178 
1179         if (is_frag & 2)
1180                 offset = (ntohs(iph->frag_off) & 0x1fff) << 3;
1181         else
1182                 offset = 0;
1183 
1184 
1185         /*
1186          *      Keep copying data until we run out.
1187          */
1188 
1189         while(left > 0)
1190         {
1191                 len = left;
1192                 /* IF: it doesn't fit, use 'mtu' - the data space left */
1193                 if (len > mtu)
1194                         len = mtu;
1195                 /* IF: we are not sending upto and including the packet end
1196                    then align the next start on an eight byte boundary */
1197                 if (len < left)
1198                 {
1199                         len/=8;
1200                         len*=8;
1201                 }
1202                 /*
1203                  *      Allocate buffer.
1204                  */
1205 
1206                 if ((skb2 = alloc_skb(len + hlen,GFP_ATOMIC)) == NULL)
1207                 {
1208                         printk("IP: frag: no memory for new fragment!\n");
1209                         ip_statistics.IpFragFails++;
1210                         return;
1211                 }
1212 
1213                 /*
1214                  *      Set up data on packet
1215                  */
1216 
1217                 skb2->arp = skb->arp;
1218                 if(skb->free==0)
1219                         printk("IP fragmenter: BUG free!=1 in fragmenter\n");
1220                 skb2->free = 1;
1221                 skb2->len = len + hlen;
1222                 skb2->h.raw=(char *) skb2->data;
1223                 /*
1224                  *      Charge the memory for the fragment to any owner
1225                  *      it might possess
1226                  */
1227 
1228                 save_flags(flags);
1229                 if (sk)
1230                 {
1231                         cli();
1232                         sk->wmem_alloc += skb2->mem_len;
1233                         skb2->sk=sk;
1234                 }
1235                 restore_flags(flags);
1236                 skb2->raddr = skb->raddr;       /* For rebuild_header - must be here */
1237 
1238                 /*
1239                  *      Copy the packet header into the new buffer.
1240                  */
1241 
1242                 memcpy(skb2->h.raw, raw, hlen);
1243 
1244                 /*
1245                  *      Copy a block of the IP datagram.
1246                  */
1247                 memcpy(skb2->h.raw + hlen, ptr, len);
1248                 left -= len;
1249 
1250                 skb2->h.raw+=dev->hard_header_len;
1251 
1252                 /*
1253                  *      Fill in the new header fields.
1254                  */
1255                 iph = (struct iphdr *)(skb2->h.raw/*+dev->hard_header_len*/);
1256                 iph->frag_off = htons((offset >> 3));
1257                 /*
1258                  *      Added AC : If we are fragmenting a fragment thats not the
1259                  *                 last fragment then keep MF on each bit
1260                  */
1261                 if (left > 0 || (is_frag & 1))
1262                         iph->frag_off |= htons(IP_MF);
1263                 ptr += len;
1264                 offset += len;
1265 
1266                 /*
1267                  *      Put this fragment into the sending queue.
1268                  */
1269 
1270                 ip_statistics.IpFragCreates++;
1271 
1272                 ip_queue_xmit(sk, dev, skb2, 2);
1273         }
1274         ip_statistics.IpFragOKs++;
1275 }
1276 
1277 
1278 
1279 #ifdef CONFIG_IP_FORWARD
1280 
1281 /*
1282  *      Forward an IP datagram to its next destination.
1283  */
1284 
1285 static void ip_forward(struct sk_buff *skb, struct device *dev, int is_frag)
     /* [previous][next][first][last][top][bottom][index][help] */
1286 {
1287         struct device *dev2;    /* Output device */
1288         struct iphdr *iph;      /* Our header */
1289         struct sk_buff *skb2;   /* Output packet */
1290         struct rtable *rt;      /* Route we use */
1291         unsigned char *ptr;     /* Data pointer */
1292         unsigned long raddr;    /* Router IP address */
1293 
1294         /* 
1295          *      See if we are allowed to forward this.
1296          */
1297 
1298 #ifdef CONFIG_IP_FIREWALL
1299         if(!ip_fw_chk(skb->h.iph, ip_fw_fwd_chain))
1300         {
1301                 return;
1302         }
1303 #endif
1304         /*
1305          *      According to the RFC, we must first decrease the TTL field. If
1306          *      that reaches zero, we must reply an ICMP control message telling
1307          *      that the packet's lifetime expired.
1308          *
1309          *      Exception:
1310          *      We may not generate an ICMP for an ICMP. icmp_send does the
1311          *      enforcement of this so we can forget it here. It is however
1312          *      sometimes VERY important.
1313          */
1314 
1315         iph = skb->h.iph;
1316         iph->ttl--;
1317         if (iph->ttl <= 0)
1318         {
1319                 /* Tell the sender its packet died... */
1320                 icmp_send(skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, dev);
1321                 return;
1322         }
1323 
1324         /*
1325          *      Re-compute the IP header checksum.
1326          *      This is inefficient. We know what has happened to the header
1327          *      and could thus adjust the checksum as Phil Karn does in KA9Q
1328          */
1329 
1330         ip_send_check(iph);
1331 
1332         /*
1333          * OK, the packet is still valid.  Fetch its destination address,
1334          * and give it to the IP sender for further processing.
1335          */
1336 
1337         rt = ip_rt_route(iph->daddr, NULL, NULL);
1338         if (rt == NULL)
1339         {
1340                 /*
1341                  *      Tell the sender its packet cannot be delivered. Again
1342                  *      ICMP is screened later.
1343                  */
1344                 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_UNREACH, dev);
1345                 return;
1346         }
1347 
1348 
1349         /*
1350          * Gosh.  Not only is the packet valid; we even know how to
1351          * forward it onto its final destination.  Can we say this
1352          * is being plain lucky?
1353          * If the router told us that there is no GW, use the dest.
1354          * IP address itself- we seem to be connected directly...
1355          */
1356 
1357         raddr = rt->rt_gateway;
1358 
1359         if (raddr != 0)
1360         {
1361                 /*
1362                  *      There is a gateway so find the correct route for it.
1363                  *      Gateways cannot in turn be gatewayed.
1364                  */
1365                 rt = ip_rt_route(raddr, NULL, NULL);
1366                 if (rt == NULL)
1367                 {
1368                         /*
1369                          *      Tell the sender its packet cannot be delivered...
1370                          */
1371                         icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, dev);
1372                         return;
1373                 }
1374                 if (rt->rt_gateway != 0)
1375                         raddr = rt->rt_gateway;
1376         }
1377         else
1378                 raddr = iph->daddr;
1379 
1380         /*
1381          *      Having picked a route we can now send the frame out.
1382          */
1383 
1384         dev2 = rt->rt_dev;
1385 
1386         /*
1387          *      In IP you never forward a frame on the interface that it arrived
1388          *      upon. We should generate an ICMP HOST REDIRECT giving the route
1389          *      we calculated.
1390          *      For now just dropping the packet is an acceptable compromise.
1391          */
1392 
1393         if (dev == dev2)
1394                 return;
1395 
1396         /*
1397          * We now allocate a new buffer, and copy the datagram into it.
1398          * If the indicated interface is up and running, kick it.
1399          */
1400 
1401         if (dev2->flags & IFF_UP)
1402         {
1403 
1404                 /*
1405                  *      Current design decrees we copy the packet. For identical header
1406                  *      lengths we could avoid it. The new skb code will let us push
1407                  *      data so the problem goes away then.
1408                  */
1409 
1410                 skb2 = alloc_skb(dev2->hard_header_len + skb->len, GFP_ATOMIC);
1411                 /*
1412                  *      This is rare and since IP is tolerant of network failures
1413                  *      quite harmless.
1414                  */
1415                 if (skb2 == NULL)
1416                 {
1417                         printk("\nIP: No memory available for IP forward\n");
1418                         return;
1419                 }
1420                 ptr = skb2->data;
1421                 skb2->free = 1;
1422                 skb2->len = skb->len + dev2->hard_header_len;
1423                 skb2->h.raw = ptr;
1424 
1425                 /*
1426                  *      Copy the packet data into the new buffer.
1427                  */
1428                 memcpy(ptr + dev2->hard_header_len, skb->h.raw, skb->len);
1429 
1430                 /* Now build the MAC header. */
1431                 (void) ip_send(skb2, raddr, skb->len, dev2, dev2->pa_addr);
1432 
1433                 ip_statistics.IpForwDatagrams++;
1434 
1435                 /*
1436                  *      See if it needs fragmenting. Note in ip_rcv we tagged
1437                  *      the fragment type. This must be right so that
1438                  *      the fragmenter does the right thing.
1439                  */
1440 
1441                 if(skb2->len > dev2->mtu + dev2->hard_header_len)
1442                 {
1443                         ip_fragment(NULL,skb2,dev2, is_frag);
1444                         kfree_skb(skb2,FREE_WRITE);
1445                 }
1446                 else
1447                 {
1448 #ifdef CONFIG_IP_ACCT           
1449                         /*
1450                          *      Count mapping we shortcut
1451                          */
1452                          
1453                         ip_acct_cnt(iph,ip_acct_chain,1);
1454 #endif                  
1455                         
1456                         /*
1457                          *      Map service types to priority. We lie about
1458                          *      throughput being low priority, but its a good
1459                          *      choice to help improve general usage.
1460                          */
1461                         if(iph->tos & IPTOS_LOWDELAY)
1462                                 dev_queue_xmit(skb2, dev2, SOPRI_INTERACTIVE);
1463                         else if(iph->tos & IPTOS_THROUGHPUT)
1464                                 dev_queue_xmit(skb2, dev2, SOPRI_BACKGROUND);
1465                         else
1466                                 dev_queue_xmit(skb2, dev2, SOPRI_NORMAL);
1467                 }
1468         }
1469 }
1470 
1471 
1472 #endif
1473 
1474 /*
1475  *      This function receives all incoming IP datagrams.
1476  */
1477 
1478 int ip_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
     /* [previous][next][first][last][top][bottom][index][help] */
1479 {
1480         struct iphdr *iph = skb->h.iph;
1481         struct sock *raw_sk=NULL;
1482         unsigned char hash;
1483         unsigned char flag = 0;
1484         unsigned char opts_p = 0;       /* Set iff the packet has options. */
1485         struct inet_protocol *ipprot;
1486         static struct options opt; /* since we don't use these yet, and they
1487                                 take up stack space. */
1488         int brd=IS_MYADDR;
1489         int is_frag=0;
1490 
1491         ip_statistics.IpInReceives++;
1492 
1493         /*
1494          *      Tag the ip header of this packet so we can find it
1495          */
1496 
1497         skb->ip_hdr = iph;
1498 
1499         /*
1500          *      Is the datagram acceptable?
1501          *
1502          *      1.      Length at least the size of an ip header
1503          *      2.      Version of 4
1504          *      3.      Checksums correctly. [Speed optimisation for later, skip loopback checksums]
1505          *      (4.     We ought to check for IP multicast addresses and undefined types.. does this matter ?)
1506          */
1507 
1508         if (skb->len<sizeof(struct iphdr) || iph->ihl<5 || iph->version != 4 || ip_fast_csum((unsigned char *)iph, iph->ihl) !=0)
1509         {
1510                 ip_statistics.IpInHdrErrors++;
1511                 kfree_skb(skb, FREE_WRITE);
1512                 return(0);
1513         }
1514         
1515         /*
1516          *      See if the firewall wants to dispose of the packet. 
1517          */
1518 
1519 #ifdef  CONFIG_IP_FIREWALL
1520         
1521         if(!LOOPBACK(iph->daddr) && !ip_fw_chk(iph,ip_fw_blk_chain))
1522         {
1523                 kfree_skb(skb, FREE_WRITE);
1524                 return 0;       
1525         }
1526 
1527 #endif
1528         
1529         /*
1530          *      Our transport medium may have padded the buffer out. Now we know it
1531          *      is IP we can trim to the true length of the frame.
1532          */
1533 
1534         skb->len=ntohs(iph->tot_len);
1535 
1536         /*
1537          *      Next analyse the packet for options. Studies show under one packet in
1538          *      a thousand have options....
1539          */
1540 
1541         if (iph->ihl != 5)
1542         {       /* Fast path for the typical optionless IP packet. */
1543                 memset((char *) &opt, 0, sizeof(opt));
1544                 if (do_options(iph, &opt) != 0)
1545                         return 0;
1546                 opts_p = 1;
1547         }
1548 
1549         /*
1550          *      Remember if the frame is fragmented.
1551          */
1552          
1553         if(iph->frag_off)
1554         {
1555                 if (iph->frag_off & 0x0020)
1556                         is_frag|=1;
1557                 /*
1558                  *      Last fragment ?
1559                  */
1560         
1561                 if (ntohs(iph->frag_off) & 0x1fff)
1562                         is_frag|=2;
1563         }
1564         
1565         /*
1566          *      Do any IP forwarding required.  chk_addr() is expensive -- avoid it someday.
1567          *
1568          *      This is inefficient. While finding out if it is for us we could also compute
1569          *      the routing table entry. This is where the great unified cache theory comes
1570          *      in as and when someone implements it
1571          *
1572          *      For most hosts over 99% of packets match the first conditional
1573          *      and don't go via ip_chk_addr. Note: brd is set to IS_MYADDR at
1574          *      function entry.
1575          */
1576 
1577         if ( iph->daddr != skb->dev->pa_addr && (brd = ip_chk_addr(iph->daddr)) == 0)
1578         {
1579                 /*
1580                  *      Don't forward multicast or broadcast frames.
1581                  */
1582 
1583                 if(skb->pkt_type!=PACKET_HOST || brd==IS_BROADCAST)
1584                 {
1585                         kfree_skb(skb,FREE_WRITE);
1586                         return 0;
1587                 }
1588 
1589                 /*
1590                  *      The packet is for another target. Forward the frame
1591                  */
1592 
1593 #ifdef CONFIG_IP_FORWARD
1594                 ip_forward(skb, dev, is_frag);
1595 #else
1596 /*              printk("Machine %lx tried to use us as a forwarder to %lx but we have forwarding disabled!\n",
1597                         iph->saddr,iph->daddr);*/
1598                 ip_statistics.IpInAddrErrors++;
1599 #endif
1600                 /*
1601                  *      The forwarder is inefficient and copies the packet. We
1602                  *      free the original now.
1603                  */
1604 
1605                 kfree_skb(skb, FREE_WRITE);
1606                 return(0);
1607         }
1608         
1609 #ifdef CONFIG_IP_MULTICAST      
1610 
1611         if(brd==IS_MULTICAST && iph->daddr!=IGMP_ALL_HOSTS && !(dev->flags&IFF_LOOPBACK))
1612         {
1613                 /*
1614                  *      Check it is for one of our groups
1615                  */
1616                 struct ip_mc_list *ip_mc=dev->ip_mc_list;
1617                 do
1618                 {
1619                         if(ip_mc==NULL)
1620                         {       
1621                                 kfree_skb(skb, FREE_WRITE);
1622                                 return 0;
1623                         }
1624                         if(ip_mc->multiaddr==iph->daddr)
1625                                 break;
1626                         ip_mc=ip_mc->next;
1627                 }
1628                 while(1);
1629         }
1630 #endif
1631         /*
1632          *      Account for the packet
1633          */
1634          
1635 #ifdef CONFIG_IP_ACCT
1636         ip_acct_cnt(iph,ip_acct_chain,1);
1637 #endif  
1638 
1639         /*
1640          * Reassemble IP fragments.
1641          */
1642 
1643         if(is_frag)
1644         {
1645                 /* Defragment. Obtain the complete packet if there is one */
1646                 skb=ip_defrag(iph,skb,dev);
1647                 if(skb==NULL)
1648                         return 0;
1649                 skb->dev = dev;
1650                 iph=skb->h.iph;
1651         }
1652         
1653                  
1654 
1655         /*
1656          *      Point into the IP datagram, just past the header.
1657          */
1658 
1659         skb->ip_hdr = iph;
1660         skb->h.raw += iph->ihl*4;
1661         
1662         /*
1663          *      Deliver to raw sockets. This is fun as to avoid copies we want to make no surplus copies.
1664          */
1665          
1666         hash = iph->protocol & (SOCK_ARRAY_SIZE-1);
1667         
1668         /* If there maybe a raw socket we must check - if not we don't care less */
1669         if((raw_sk=raw_prot.sock_array[hash])!=NULL)
1670         {
1671                 struct sock *sknext=NULL;
1672                 struct sk_buff *skb1;
1673                 raw_sk=get_sock_raw(raw_sk, hash,  iph->saddr, iph->daddr);
1674                 if(raw_sk)      /* Any raw sockets */
1675                 {
1676                         do
1677                         {
1678                                 /* Find the next */
1679                                 sknext=get_sock_raw(raw_sk->next, hash, iph->saddr, iph->daddr);
1680                                 if(sknext)
1681                                         skb1=skb_clone(skb, GFP_ATOMIC);
1682                                 else
1683                                         break;  /* One pending raw socket left */
1684                                 if(skb1)
1685                                         raw_rcv(raw_sk, skb1, dev, iph->saddr,iph->daddr);
1686                                 raw_sk=sknext;
1687                         }
1688                         while(raw_sk!=NULL);
1689                         /* Here either raw_sk is the last raw socket, or NULL if none */
1690                         /* We deliver to the last raw socket AFTER the protocol checks as it avoids a surplus copy */
1691                 }
1692         }
1693         
1694         /*
1695          *      skb->h.raw now points at the protocol beyond the IP header.
1696          */
1697 
1698         hash = iph->protocol & (MAX_INET_PROTOS -1);
1699         for (ipprot = (struct inet_protocol *)inet_protos[hash];ipprot != NULL;ipprot=(struct inet_protocol *)ipprot->next)
1700         {
1701                 struct sk_buff *skb2;
1702 
1703                 if (ipprot->protocol != iph->protocol)
1704                         continue;
1705        /*
1706         *       See if we need to make a copy of it.  This will
1707         *       only be set if more than one protocol wants it.
1708         *       and then not for the last one. If there is a pending
1709         *       raw delivery wait for that
1710         */
1711                 if (ipprot->copy || raw_sk)
1712                 {
1713                         skb2 = skb_clone(skb, GFP_ATOMIC);
1714                         if(skb2==NULL)
1715                                 continue;
1716                 }
1717                 else
1718                 {
1719                         skb2 = skb;
1720                 }
1721                 flag = 1;
1722 
1723                /*
1724                 * Pass on the datagram to each protocol that wants it,
1725                 * based on the datagram protocol.  We should really
1726                 * check the protocol handler's return values here...
1727                 */
1728                 ipprot->handler(skb2, dev, opts_p ? &opt : 0, iph->daddr,
1729                                 (ntohs(iph->tot_len) - (iph->ihl * 4)),
1730                                 iph->saddr, 0, ipprot);
1731 
1732         }
1733 
1734         /*
1735          * All protocols checked.
1736          * If this packet was a broadcast, we may *not* reply to it, since that
1737          * causes (proven, grin) ARP storms and a leakage of memory (i.e. all
1738          * ICMP reply messages get queued up for transmission...)
1739          */
1740 
1741         if(raw_sk!=NULL)        /* Shift to last raw user */
1742                 raw_rcv(raw_sk, skb, dev, iph->saddr, iph->daddr);
1743         else if (!flag)         /* Free and report errors */
1744         {
1745                 if (brd != IS_BROADCAST && brd!=IS_MULTICAST)
1746                         icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, dev);
1747                 kfree_skb(skb, FREE_WRITE);
1748         }
1749 
1750         return(0);
1751 }
1752 
1753 /*
1754  *      Loop a packet back to the sender.
1755  */
1756  
1757 static void ip_loopback(struct device *old_dev, struct sk_buff *skb)
     /* [previous][next][first][last][top][bottom][index][help] */
1758 {
1759         extern struct device loopback_dev;
1760         struct device *dev=&loopback_dev;
1761         int len=skb->len-old_dev->hard_header_len;
1762         struct sk_buff *newskb=alloc_skb(len+dev->hard_header_len, GFP_ATOMIC);
1763         
1764         if(newskb==NULL)
1765                 return;
1766                 
1767         newskb->link3=NULL;
1768         newskb->sk=NULL;
1769         newskb->dev=dev;
1770         newskb->saddr=skb->saddr;
1771         newskb->daddr=skb->daddr;
1772         newskb->raddr=skb->raddr;
1773         newskb->free=1;
1774         newskb->lock=0;
1775         newskb->users=0;
1776         newskb->pkt_type=skb->pkt_type;
1777         newskb->len=len+dev->hard_header_len;
1778         
1779         
1780         newskb->ip_hdr=(struct iphdr *)(newskb->data+ip_send(newskb, skb->ip_hdr->daddr, len, dev, skb->ip_hdr->saddr));
1781         memcpy(newskb->ip_hdr,skb->ip_hdr,len);
1782 
1783         /* Recurse. The device check against IFF_LOOPBACK will stop infinite recursion */
1784                 
1785         /*printk("Loopback output queued [%lX to %lX].\n", newskb->ip_hdr->saddr,newskb->ip_hdr->daddr);*/
1786         ip_queue_xmit(NULL, dev, newskb, 1);
1787 }
1788 
1789 
1790 /*
1791  * Queues a packet to be sent, and starts the transmitter
1792  * if necessary.  if free = 1 then we free the block after
1793  * transmit, otherwise we don't. If free==2 we not only
1794  * free the block but also don't assign a new ip seq number.
1795  * This routine also needs to put in the total length,
1796  * and compute the checksum
1797  */
1798 
1799 void ip_queue_xmit(struct sock *sk, struct device *dev,
     /* [previous][next][first][last][top][bottom][index][help] */
1800               struct sk_buff *skb, int free)
1801 {
1802         struct iphdr *iph;
1803         unsigned char *ptr;
1804 
1805         /* Sanity check */
1806         if (dev == NULL)
1807         {
1808                 printk("IP: ip_queue_xmit dev = NULL\n");
1809                 return;
1810         }
1811 
1812         IS_SKB(skb);
1813 
1814         /*
1815          *      Do some book-keeping in the packet for later
1816          */
1817 
1818 
1819         skb->dev = dev;
1820         skb->when = jiffies;
1821 
1822         /*
1823          *      Find the IP header and set the length. This is bad
1824          *      but once we get the skb data handling code in the
1825          *      hardware will push its header sensibly and we will
1826          *      set skb->ip_hdr to avoid this mess and the fixed
1827          *      header length problem
1828          */
1829 
1830         ptr = skb->data;
1831         ptr += dev->hard_header_len;
1832         iph = (struct iphdr *)ptr;
1833         skb->ip_hdr = iph;
1834         iph->tot_len = ntohs(skb->len-dev->hard_header_len);
1835 
1836         /*
1837          *      No reassigning numbers to fragments...
1838          */
1839 
1840         if(free!=2)
1841                 iph->id      = htons(ip_id_count++);
1842         else
1843                 free=1;
1844 
1845         /* All buffers without an owner socket get freed */
1846         if (sk == NULL)
1847                 free = 1;
1848 
1849         skb->free = free;
1850 
1851         /*
1852          *      Do we need to fragment. Again this is inefficient.
1853          *      We need to somehow lock the original buffer and use
1854          *      bits of it.
1855          */
1856 
1857         if(skb->len > dev->mtu + dev->hard_header_len)
1858         {
1859                 ip_fragment(sk,skb,dev,0);
1860                 IS_SKB(skb);
1861                 kfree_skb(skb,FREE_WRITE);
1862                 return;
1863         }
1864 
1865         /*
1866          *      Add an IP checksum
1867          */
1868 
1869         ip_send_check(iph);
1870 
1871         /*
1872          *      Print the frame when debugging
1873          */
1874 
1875         /*
1876          *      More debugging. You cannot queue a packet already on a list
1877          *      Spot this and moan loudly.
1878          */
1879         if (skb->next != NULL)
1880         {
1881                 printk("ip_queue_xmit: next != NULL\n");
1882                 skb_unlink(skb);
1883         }
1884 
1885         /*
1886          *      If a sender wishes the packet to remain unfreed
1887          *      we add it to his send queue. This arguably belongs
1888          *      in the TCP level since nobody else uses it. BUT
1889          *      remember IPng might change all the rules.
1890          */
1891 
1892         if (!free)
1893         {
1894                 unsigned long flags;
1895                 /* The socket now has more outstanding blocks */
1896 
1897                 sk->packets_out++;
1898 
1899                 /* Protect the list for a moment */
1900                 save_flags(flags);
1901                 cli();
1902 
1903                 if (skb->link3 != NULL)
1904                 {
1905                         printk("ip.c: link3 != NULL\n");
1906                         skb->link3 = NULL;
1907                 }
1908                 if (sk->send_head == NULL)
1909                 {
1910                         sk->send_tail = skb;
1911                         sk->send_head = skb;
1912                 }
1913                 else
1914                 {
1915                         sk->send_tail->link3 = skb;
1916                         sk->send_tail = skb;
1917                 }
1918                 /* skb->link3 is NULL */
1919 
1920                 /* Interrupt restore */
1921                 restore_flags(flags);
1922         }
1923         else
1924                 /* Remember who owns the buffer */
1925                 skb->sk = sk;
1926 
1927         /*
1928          *      If the indicated interface is up and running, send the packet.
1929          */
1930          
1931         ip_statistics.IpOutRequests++;
1932 #ifdef CONFIG_IP_ACCT
1933         ip_acct_cnt(iph,ip_acct_chain,1);
1934 #endif  
1935         
1936 #ifdef CONFIG_IP_MULTICAST      
1937 
1938         /*
1939          *      Multicasts are looped back for other local users
1940          */
1941          
1942         if (MULTICAST(iph->daddr) && !(dev->flags&IFF_LOOPBACK))
1943         {
1944                 if(sk==NULL || sk->ip_mc_loop)
1945                 {
1946                         if(iph->daddr==IGMP_ALL_HOSTS)
1947                                 ip_loopback(dev,skb);
1948                         else
1949                         {
1950                                 struct ip_mc_list *imc=dev->ip_mc_list;
1951                                 while(imc!=NULL)
1952                                 {
1953                                         if(imc->multiaddr==iph->daddr)
1954                                         {
1955                                                 ip_loopback(dev,skb);
1956                                                 break;
1957                                         }
1958                                         imc=imc->next;
1959                                 }
1960                         }
1961                 }
1962                 /* Multicasts with ttl 0 must not go beyond the host */
1963                 
1964                 if(skb->ip_hdr->ttl==0)
1965                 {
1966                         kfree_skb(skb, FREE_READ);
1967                         return;
1968                 }
1969         }
1970 #endif
1971         if((dev->flags&IFF_BROADCAST) && iph->daddr==dev->pa_brdaddr && !(dev->flags&IFF_LOOPBACK))
1972                 ip_loopback(dev,skb);
1973                 
1974         if (dev->flags & IFF_UP)
1975         {
1976                 /*
1977                  *      If we have an owner use its priority setting,
1978                  *      otherwise use NORMAL
1979                  */
1980 
1981                 if (sk != NULL)
1982                 {
1983                         dev_queue_xmit(skb, dev, sk->priority);
1984                 }
1985                 else
1986                 {
1987                         dev_queue_xmit(skb, dev, SOPRI_NORMAL);
1988                 }
1989         }
1990         else
1991         {
1992                 ip_statistics.IpOutDiscards++;
1993                 if (free)
1994                         kfree_skb(skb, FREE_WRITE);
1995         }
1996 }
1997 
1998 
1999 
2000 #ifdef CONFIG_IP_MULTICAST
2001 
2002 /*
2003  *      Write an multicast group list table for the IGMP daemon to
2004  *      read.
2005  */
2006  
2007 int ip_mc_procinfo(char *buffer, char **start, off_t offset, int length)
     /* [previous][next][first][last][top][bottom][index][help] */
2008 {
2009         off_t pos=0, begin=0;
2010         struct ip_mc_list *im;
2011         unsigned long flags;
2012         int len=0;
2013         struct device *dev;
2014         
2015         len=sprintf(buffer,"Device    : Count\tGroup    Users Timer\n");  
2016         save_flags(flags);
2017         cli();
2018         
2019         for(dev = dev_base; dev; dev = dev->next)
2020         {
2021                 if((dev->flags&IFF_UP)&&(dev->flags&IFF_MULTICAST))
2022                 {
2023                         len+=sprintf(buffer+len,"%-10s: %5d\n",
2024                                         dev->name, dev->mc_count);
2025                         for(im = dev->ip_mc_list; im; im = im->next)
2026                         {
2027                                 len+=sprintf(buffer+len,
2028                                         "\t\t\t%08lX %5d %d:%08lX\n",
2029                                         im->multiaddr, im->users,
2030                                         im->tm_running, im->timer.expires);
2031                                 pos=begin+len;
2032                                 if(pos<offset)
2033                                 {
2034                                         len=0;
2035                                         begin=pos;
2036                                 }
2037                                 if(pos>offset+length)
2038                                         break;
2039                         }
2040                 }
2041         }
2042         restore_flags(flags);
2043         *start=buffer+(offset-begin);
2044         len-=(offset-begin);
2045         if(len>length)
2046                 len=length;     
2047         return len;
2048 }
2049 
2050 
2051 #endif  
2052 /*
2053  *      Socket option code for IP. This is the end of the line after any TCP,UDP etc options on
2054  *      an IP socket.
2055  *
2056  *      We implement IP_TOS (type of service), IP_TTL (time to live).
2057  *
2058  *      Next release we will sort out IP_OPTIONS since for some people are kind of important.
2059  */
2060 
2061 int ip_setsockopt(struct sock *sk, int level, int optname, char *optval, int optlen)
     /* [previous][next][first][last][top][bottom][index][help] */
2062 {
2063         int val,err;
2064 #if defined(CONFIG_IP_FIREWALL) || defined(CONFIG_IP_ACCT)
2065         struct ip_fw tmp_fw;
2066 #endif  
2067         if (optval == NULL)
2068                 return(-EINVAL);
2069 
2070         err=verify_area(VERIFY_READ, optval, sizeof(int));
2071         if(err)
2072                 return err;
2073 
2074         val = get_fs_long((unsigned long *)optval);
2075 
2076         if(level!=SOL_IP)
2077                 return -EOPNOTSUPP;
2078 
2079 #ifdef CONFIG_IP_MULTICAST
2080         if(optname==IP_MULTICAST_TTL)
2081         {
2082                 unsigned char ucval;
2083                 ucval=get_fs_byte((unsigned char *)optval);
2084                 printk("MC TTL %d\n", ucval);
2085                 if(ucval<1||ucval>255)
2086                         return -EINVAL;
2087                 sk->ip_mc_ttl=(int)ucval;
2088                 return 0;
2089         }
2090 #endif
2091 
2092         switch(optname)
2093         {
2094                 case IP_TOS:
2095                         if(val<0||val>255)
2096                                 return -EINVAL;
2097                         sk->ip_tos=val;
2098                         if(val==IPTOS_LOWDELAY)
2099                                 sk->priority=SOPRI_INTERACTIVE;
2100                         if(val==IPTOS_THROUGHPUT)
2101                                 sk->priority=SOPRI_BACKGROUND;
2102                         return 0;
2103                 case IP_TTL:
2104                         if(val<1||val>255)
2105                                 return -EINVAL;
2106                         sk->ip_ttl=val;
2107                         return 0;
2108 #ifdef CONFIG_IP_MULTICAST
2109 #ifdef GCC_WORKS
2110                 case IP_MULTICAST_TTL: 
2111                 {
2112                         unsigned char ucval;
2113 
2114                         ucval=get_fs_byte((unsigned char *)optval);
2115                         printk("MC TTL %d\n", ucval);
2116                         if(ucval<1||ucval>255)
2117                                 return -EINVAL;
2118                         sk->ip_mc_ttl=(int)ucval;
2119                         return 0;
2120                 }
2121 #endif
2122                 case IP_MULTICAST_LOOP: 
2123                 {
2124                         unsigned char ucval;
2125 
2126                         ucval=get_fs_byte((unsigned char *)optval);
2127                         if(ucval!=0 && ucval!=1)
2128                                  return -EINVAL;
2129                         sk->ip_mc_loop=(int)ucval;
2130                         return 0;
2131                 }
2132                 case IP_MULTICAST_IF: 
2133                 {
2134                         /* Not fully tested */
2135                         struct in_addr addr;
2136                         struct device *dev=NULL;
2137                         
2138                         /*
2139                          *      Check the arguments are allowable
2140                          */
2141 
2142                         err=verify_area(VERIFY_READ, optval, sizeof(addr));
2143                         if(err)
2144                                 return err;
2145                                 
2146                         memcpy_fromfs(&addr,optval,sizeof(addr));
2147                         
2148                         printk("MC bind %s\n", in_ntoa(addr.s_addr));
2149                         
2150                         /*
2151                          *      What address has been requested
2152                          */
2153                         
2154                         if(addr.s_addr==INADDR_ANY)     /* Default */
2155                         {
2156                                 sk->ip_mc_name[0]=0;
2157                                 return 0;
2158                         }
2159                         
2160                         /*
2161                          *      Find the device
2162                          */
2163                          
2164                         for(dev = dev_base; dev; dev = dev->next)
2165                         {
2166                                 if((dev->flags&IFF_UP)&&(dev->flags&IFF_MULTICAST)&&
2167                                         (dev->pa_addr==addr.s_addr))
2168                                         break;
2169                         }
2170                         
2171                         /*
2172                          *      Did we find one
2173                          */
2174                          
2175                         if(dev) 
2176                         {
2177                                 strcpy(sk->ip_mc_name,dev->name);
2178                                 return 0;
2179                         }
2180                         return -EADDRNOTAVAIL;
2181                 }
2182                 
2183                 case IP_ADD_MEMBERSHIP: 
2184                 {
2185                 
2186 /*
2187  *      FIXME: Add/Del membership should have a semaphore protecting them from re-entry
2188  */
2189                         struct ip_mreq mreq;
2190                         static struct options optmem;
2191                         unsigned long route_src;
2192                         struct rtable *rt;
2193                         struct device *dev=NULL;
2194                         
2195                         /*
2196                          *      Check the arguments.
2197                          */
2198 
2199                         err=verify_area(VERIFY_READ, optval, sizeof(mreq));
2200                         if(err)
2201                                 return err;
2202 
2203                         memcpy_fromfs(&mreq,optval,sizeof(mreq));
2204 
2205                         /* 
2206                          *      Get device for use later
2207                          */
2208 
2209                         if(mreq.imr_interface.s_addr==INADDR_ANY) 
2210                         {
2211                                 /*
2212                                  *      Not set so scan.
2213                                  */
2214                                 if((rt=ip_rt_route(mreq.imr_multiaddr.s_addr,&optmem, &route_src))!=NULL)
2215                                 {
2216                                         dev=rt->rt_dev;
2217                                         rt->rt_use--;
2218                                 }
2219                         }
2220                         else
2221                         {
2222                                 /*
2223                                  *      Find a suitable device.
2224                                  */
2225                                 for(dev = dev_base; dev; dev = dev->next)
2226                                 {
2227                                         if((dev->flags&IFF_UP)&&(dev->flags&IFF_MULTICAST)&&
2228                                                 (dev->pa_addr==mreq.imr_interface.s_addr))
2229                                                 break;
2230                                 }
2231                         }
2232                         
2233                         /*
2234                          *      No device, no cookies.
2235                          */
2236                          
2237                         if(!dev)
2238                                 return -ENODEV;
2239                                 
2240                         /*
2241                          *      Join group.
2242                          */
2243                          
2244                         return ip_mc_join_group(sk,dev,mreq.imr_multiaddr.s_addr);
2245                 }
2246                 
2247                 case IP_DROP_MEMBERSHIP: 
2248                 {
2249                         struct ip_mreq mreq;
2250                         struct rtable *rt;
2251                         static struct options optmem;
2252                         unsigned long route_src;
2253                         struct device *dev=NULL;
2254 
2255                         /*
2256                          *      Check the arguments
2257                          */
2258                          
2259                         err=verify_area(VERIFY_READ, optval, sizeof(mreq));
2260                         if(err)
2261                                 return err;
2262 
2263                         memcpy_fromfs(&mreq,optval,sizeof(mreq));
2264 
2265                         /*
2266                          *      Get device for use later 
2267                          */
2268  
2269                         if(mreq.imr_interface.s_addr==INADDR_ANY) 
2270                         {
2271                                 if((rt=ip_rt_route(mreq.imr_multiaddr.s_addr,&optmem, &route_src))!=NULL)
2272                                 {
2273                                         dev=rt->rt_dev;
2274                                         rt->rt_use--;
2275                                 }
2276                         }
2277                         else 
2278                         {
2279                                 for(dev = dev_base; dev; dev = dev->next)
2280                                 {
2281                                         if((dev->flags&IFF_UP)&& (dev->flags&IFF_MULTICAST)&&
2282                                                         (dev->pa_addr==mreq.imr_interface.s_addr))
2283                                                 break;
2284                                 }
2285                         }
2286                         
2287                         /*
2288                          *      Did we find a suitable device.
2289                          */
2290                          
2291                         if(!dev)
2292                                 return -ENODEV;
2293                                 
2294                         /*
2295                          *      Leave group
2296                          */
2297                          
2298                         return ip_mc_leave_group(sk,dev,mreq.imr_multiaddr.s_addr);
2299                 }
2300 #endif                  
2301 #ifdef CONFIG_IP_FIREWALL
2302                 case IP_FW_ADD_BLK:
2303                 case IP_FW_DEL_BLK:
2304                 case IP_FW_ADD_FWD:
2305                 case IP_FW_DEL_FWD:
2306                 case IP_FW_CHK_BLK:
2307                 case IP_FW_CHK_FWD:
2308                 case IP_FW_FLUSH:
2309                 case IP_FW_POLICY:
2310                         if(!suser())
2311                                 return -EPERM;
2312                         if(optlen>sizeof(tmp_fw) || optlen<1)
2313                                 return -EINVAL;
2314                         err=verify_area(VERIFY_READ,optval,optlen);
2315                         if(err)
2316                                 return err;
2317                         memcpy_fromfs(&tmp_fw,optval,optlen);
2318                         err=ip_fw_ctl(optname, &tmp_fw,optlen);
2319                         return -err;    /* -0 is 0 after all */
2320                         
2321 #endif
2322 #ifdef CONFIG_IP_ACCT
2323                 case IP_ACCT_DEL:
2324                 case IP_ACCT_ADD:
2325                 case IP_ACCT_FLUSH:
2326                 case IP_ACCT_ZERO:
2327                         if(!suser())
2328                                 return -EPERM;
2329                         if(optlen>sizeof(tmp_fw) || optlen<1)
2330                                 return -EINVAL;
2331                         err=verify_area(VERIFY_READ,optval,optlen);
2332                         if(err)
2333                                 return err;
2334                         memcpy_fromfs(&tmp_fw, optval,optlen);
2335                         err=ip_acct_ctl(optname, &tmp_fw,optlen);
2336                         return -err;    /* -0 is 0 after all */
2337 #endif
2338                 /* IP_OPTIONS and friends go here eventually */
2339                 default:
2340                         return(-ENOPROTOOPT);
2341         }
2342 }
2343 
2344 /*
2345  *      Get the options. Note for future reference. The GET of IP options gets the
2346  *      _received_ ones. The set sets the _sent_ ones.
2347  */
2348 
2349 int ip_getsockopt(struct sock *sk, int level, int optname, char *optval, int *optlen)
     /* [previous][next][first][last][top][bottom][index][help] */
2350 {
2351         int val,err;
2352 #ifdef CONFIG_IP_MULTICAST
2353         int len;
2354 #endif
2355         
2356         if(level!=SOL_IP)
2357                 return -EOPNOTSUPP;
2358 
2359         switch(optname)
2360         {
2361                 case IP_TOS:
2362                         val=sk->ip_tos;
2363                         break;
2364                 case IP_TTL:
2365                         val=sk->ip_ttl;
2366                         break;
2367 #ifdef CONFIG_IP_MULTICAST                      
2368                 case IP_MULTICAST_TTL:
2369                         val=sk->ip_mc_ttl;
2370                         break;
2371                 case IP_MULTICAST_LOOP:
2372                         val=sk->ip_mc_loop;
2373                         break;
2374                 case IP_MULTICAST_IF:
2375                         err=verify_area(VERIFY_WRITE, optlen, sizeof(int));
2376                         if(err)
2377                                 return err;
2378                         len=strlen(sk->ip_mc_name);
2379                         err=verify_area(VERIFY_WRITE, optval, len);
2380                         if(err)
2381                                 return err;
2382                         put_fs_long(len,(unsigned long *) optlen);
2383                         memcpy_tofs((void *)optval,sk->ip_mc_name, len);
2384                         return 0;
2385 #endif
2386                 default:
2387                         return(-ENOPROTOOPT);
2388         }
2389         err=verify_area(VERIFY_WRITE, optlen, sizeof(int));
2390         if(err)
2391                 return err;
2392         put_fs_long(sizeof(int),(unsigned long *) optlen);
2393 
2394         err=verify_area(VERIFY_WRITE, optval, sizeof(int));
2395         if(err)
2396                 return err;
2397         put_fs_long(val,(unsigned long *)optval);
2398 
2399         return(0);
2400 }
2401 
2402 /*
2403  *      IP protocol layer initialiser
2404  */
2405 
2406 static struct packet_type ip_packet_type =
2407 {
2408         0,      /* MUTTER ntohs(ETH_P_IP),*/
2409         NULL,   /* All devices */
2410         ip_rcv,
2411         NULL,
2412         NULL,
2413 };
2414 
2415 /*
2416  *      Device notifier
2417  */
2418  
2419 static int ip_rt_event(unsigned long event, void *ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
2420 {
2421         if(event==NETDEV_DOWN)
2422                 ip_rt_flush(ptr);
2423         return NOTIFY_DONE;
2424 }
2425 
2426 struct notifier_block ip_rt_notifier={
2427         ip_rt_event,
2428         NULL,
2429         0
2430 };
2431 
2432 /*
2433  *      IP registers the packet type and then calls the subprotocol initialisers
2434  */
2435 
2436 void ip_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2437 {
2438         ip_packet_type.type=htons(ETH_P_IP);
2439         dev_add_pack(&ip_packet_type);
2440 
2441         /* So we flush routes when a device is downed */        
2442         register_netdevice_notifier(&ip_rt_notifier);
2443 /*      ip_raw_init();
2444         ip_packet_init();
2445         ip_tcp_init();
2446         ip_udp_init();*/
2447 }

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