root/net/ipv4/ip.c

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

DEFINITIONS

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

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