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 
 131 #include <net/snmp.h>
 132 #include <net/ip.h>
 133 #include <net/protocol.h>
 134 #include <net/route.h>
 135 #include <net/tcp.h>
 136 #include <net/udp.h>
 137 #include <linux/skbuff.h>
 138 #include <net/sock.h>
 139 #include <net/arp.h>
 140 #include <net/icmp.h>
 141 #include <net/raw.h>
 142 #include <net/checksum.h>
 143 #include <linux/igmp.h>
 144 #include <linux/ip_fw.h>
 145 
 146 #define CONFIG_IP_DEFRAG
 147 
 148 extern int last_retran;
 149 extern void sort_send(struct sock *sk);
 150 
 151 #define min(a,b)        ((a)<(b)?(a):(b))
 152 
 153 /*
 154  *      SNMP management statistics
 155  */
 156 
 157 #ifdef CONFIG_IP_FORWARD
 158 struct ip_mib ip_statistics={1,64,};    /* Forwarding=Yes, Default TTL=64 */
 159 #else
 160 struct ip_mib ip_statistics={2,64,};    /* Forwarding=No, Default TTL=64 */
 161 #endif
 162 
 163 /*
 164  *      Handle the issuing of an ioctl() request
 165  *      for the ip device. This is scheduled to
 166  *      disappear
 167  */
 168 
 169 int ip_ioctl(struct sock *sk, int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 170 {
 171         switch(cmd)
 172         {
 173                 default:
 174                         return(-EINVAL);
 175         }
 176 }
 177 
 178 
 179 /*
 180  *      Take an skb, and fill in the MAC header.
 181  */
 182 
 183 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] */
 184 {
 185         int mac = 0;
 186 
 187         skb->dev = dev;
 188         skb->arp = 1;
 189         if (dev->hard_header)
 190         {
 191                 /*
 192                  *      Build a hardware header. Source address is our mac, destination unknown
 193                  *      (rebuild header will sort this out)
 194                  */
 195                 skb_reserve(skb,(dev->hard_header_len+15)&~15); /* 16 byte aligned IP headers are good */
 196                 mac = dev->hard_header(skb, dev, ETH_P_IP, NULL, NULL, len);
 197                 if (mac < 0)
 198                 {
 199                         mac = -mac;
 200                         skb->arp = 0;
 201                         skb->raddr = daddr;     /* next routing address */
 202                 }
 203         }
 204         return mac;
 205 }
 206 
 207 int ip_id_count = 0;
 208 
 209 /*
 210  * This routine builds the appropriate hardware/IP headers for
 211  * the routine.  It assumes that if *dev != NULL then the
 212  * protocol knows what it's doing, otherwise it uses the
 213  * routing/ARP tables to select a device struct.
 214  */
 215 int ip_build_header(struct sk_buff *skb, unsigned long saddr, unsigned long daddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 216                 struct device **dev, int type, struct options *opt, int len, int tos, int ttl)
 217 {
 218         struct rtable *rt;
 219         unsigned long raddr;
 220         int tmp;
 221         unsigned long src;
 222         struct iphdr *iph;
 223 
 224         /*
 225          *      See if we need to look up the device.
 226          */
 227 
 228 #ifdef CONFIG_IP_MULTICAST      
 229         if(MULTICAST(daddr) && *dev==NULL && skb->sk && *skb->sk->ip_mc_name)
 230                 *dev=dev_get(skb->sk->ip_mc_name);
 231 #endif
 232         if (*dev == NULL)
 233         {
 234                 if(skb->localroute)
 235                         rt = ip_rt_local(daddr, NULL, &src);
 236                 else
 237                         rt = ip_rt_route(daddr, NULL, &src);
 238                 if (rt == NULL)
 239                 {
 240                         ip_statistics.IpOutNoRoutes++;
 241                         return(-ENETUNREACH);
 242                 }
 243 
 244                 *dev = rt->rt_dev;
 245                 /*
 246                  *      If the frame is from us and going off machine it MUST MUST MUST
 247                  *      have the output device ip address and never the loopback
 248                  */
 249                 if (LOOPBACK(saddr) && !LOOPBACK(daddr))
 250                         saddr = src;/*rt->rt_dev->pa_addr;*/
 251                 raddr = rt->rt_gateway;
 252 
 253         }
 254         else
 255         {
 256                 /*
 257                  *      We still need the address of the first hop.
 258                  */
 259                 if(skb->localroute)
 260                         rt = ip_rt_local(daddr, NULL, &src);
 261                 else
 262                         rt = ip_rt_route(daddr, NULL, &src);
 263                 /*
 264                  *      If the frame is from us and going off machine it MUST MUST MUST
 265                  *      have the output device ip address and never the loopback
 266                  */
 267                 if (LOOPBACK(saddr) && !LOOPBACK(daddr))
 268                         saddr = src;/*rt->rt_dev->pa_addr;*/
 269 
 270                 raddr = (rt == NULL) ? 0 : rt->rt_gateway;
 271         }
 272 
 273         /*
 274          *      No source addr so make it our addr
 275          */
 276         if (saddr == 0)
 277                 saddr = src;
 278 
 279         /*
 280          *      No gateway so aim at the real destination
 281          */
 282         if (raddr == 0)
 283                 raddr = daddr;
 284 
 285         /*
 286          *      Now build the MAC header.
 287          */
 288 
 289         tmp = ip_send(skb, raddr, len, *dev, saddr);
 290 
 291         /*
 292          *      Book keeping
 293          */
 294 
 295         skb->dev = *dev;
 296         skb->saddr = saddr;
 297         if (skb->sk)
 298                 skb->sk->saddr = saddr;
 299 
 300         /*
 301          *      Now build the IP header.
 302          */
 303 
 304         /*
 305          *      If we are using IPPROTO_RAW, then we don't need an IP header, since
 306          *      one is being supplied to us by the user
 307          */
 308 
 309         if(type == IPPROTO_RAW)
 310                 return (tmp);
 311 
 312         /*
 313          *      Build the IP addresses
 314          */
 315          
 316         iph=(struct iphdr *)skb_put(skb,sizeof(struct iphdr));
 317 
 318         iph->version  = 4;
 319         iph->ihl      = 5;
 320         iph->tos      = tos;
 321         iph->frag_off = 0;
 322         iph->ttl      = ttl;
 323         iph->daddr    = daddr;
 324         iph->saddr    = saddr;
 325         iph->protocol = type;
 326         skb->ip_hdr   = iph;
 327 
 328         return(20 + tmp);       /* IP header plus MAC header size */
 329 }
 330 
 331 
 332 /*
 333  *      Generate a checksum for an outgoing IP datagram.
 334  */
 335 
 336 void ip_send_check(struct iphdr *iph)
     /* [previous][next][first][last][top][bottom][index][help] */
 337 {
 338         iph->check = 0;
 339         iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
 340 }
 341 
 342 /************************ Fragment Handlers From NET2E **********************************/
 343 
 344 
 345 /*
 346  *      This fragment handler is a bit of a heap. On the other hand it works quite
 347  *      happily and handles things quite well.
 348  */
 349 
 350 static struct ipq *ipqueue = NULL;              /* IP fragment queue    */
 351 
 352 /*
 353  *      Create a new fragment entry.
 354  */
 355 
 356 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] */
 357 {
 358         struct ipfrag *fp;
 359 
 360         fp = (struct ipfrag *) kmalloc(sizeof(struct ipfrag), GFP_ATOMIC);
 361         if (fp == NULL)
 362         {
 363                 NETDEBUG(printk("IP: frag_create: no memory left !\n"));
 364                 return(NULL);
 365         }
 366         memset(fp, 0, sizeof(struct ipfrag));
 367 
 368         /* Fill in the structure. */
 369         fp->offset = offset;
 370         fp->end = end;
 371         fp->len = end - offset;
 372         fp->skb = skb;
 373         fp->ptr = ptr;
 374 
 375         return(fp);
 376 }
 377 
 378 
 379 /*
 380  *      Find the correct entry in the "incomplete datagrams" queue for
 381  *      this IP datagram, and return the queue entry address if found.
 382  */
 383 
 384 static struct ipq *ip_find(struct iphdr *iph)
     /* [previous][next][first][last][top][bottom][index][help] */
 385 {
 386         struct ipq *qp;
 387         struct ipq *qplast;
 388 
 389         cli();
 390         qplast = NULL;
 391         for(qp = ipqueue; qp != NULL; qplast = qp, qp = qp->next)
 392         {
 393                 if (iph->id== qp->iph->id && iph->saddr == qp->iph->saddr &&
 394                         iph->daddr == qp->iph->daddr && iph->protocol == qp->iph->protocol)
 395                 {
 396                         del_timer(&qp->timer);  /* So it doesn't vanish on us. The timer will be reset anyway */
 397                         sti();
 398                         return(qp);
 399                 }
 400         }
 401         sti();
 402         return(NULL);
 403 }
 404 
 405 
 406 /*
 407  *      Remove an entry from the "incomplete datagrams" queue, either
 408  *      because we completed, reassembled and processed it, or because
 409  *      it timed out.
 410  */
 411 
 412 static void ip_free(struct ipq *qp)
     /* [previous][next][first][last][top][bottom][index][help] */
 413 {
 414         struct ipfrag *fp;
 415         struct ipfrag *xp;
 416 
 417         /*
 418          * Stop the timer for this entry.
 419          */
 420 
 421         del_timer(&qp->timer);
 422 
 423         /* Remove this entry from the "incomplete datagrams" queue. */
 424         cli();
 425         if (qp->prev == NULL)
 426         {
 427                 ipqueue = qp->next;
 428                 if (ipqueue != NULL)
 429                         ipqueue->prev = NULL;
 430         }
 431         else
 432         {
 433                 qp->prev->next = qp->next;
 434                 if (qp->next != NULL)
 435                         qp->next->prev = qp->prev;
 436         }
 437 
 438         /* Release all fragment data. */
 439 
 440         fp = qp->fragments;
 441         while (fp != NULL)
 442         {
 443                 xp = fp->next;
 444                 IS_SKB(fp->skb);
 445                 kfree_skb(fp->skb,FREE_READ);
 446                 kfree_s(fp, sizeof(struct ipfrag));
 447                 fp = xp;
 448         }
 449 
 450         /* Release the IP header. */
 451         kfree_s(qp->iph, 64 + 8);
 452 
 453         /* Finally, release the queue descriptor itself. */
 454         kfree_s(qp, sizeof(struct ipq));
 455         sti();
 456 }
 457 
 458 
 459 /*
 460  *      Oops- a fragment queue timed out.  Kill it and send an ICMP reply.
 461  */
 462 
 463 static void ip_expire(unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 464 {
 465         struct ipq *qp;
 466 
 467         qp = (struct ipq *)arg;
 468 
 469         /*
 470          *      Send an ICMP "Fragment Reassembly Timeout" message.
 471          */
 472 
 473         ip_statistics.IpReasmTimeout++;
 474         ip_statistics.IpReasmFails++;   
 475         /* This if is always true... shrug */
 476         if(qp->fragments!=NULL)
 477                 icmp_send(qp->fragments->skb,ICMP_TIME_EXCEEDED,
 478                                 ICMP_EXC_FRAGTIME, 0, qp->dev);
 479 
 480         /*
 481          *      Nuke the fragment queue.
 482          */
 483         ip_free(qp);
 484 }
 485 
 486 
 487 /*
 488  *      Add an entry to the 'ipq' queue for a newly received IP datagram.
 489  *      We will (hopefully :-) receive all other fragments of this datagram
 490  *      in time, so we just create a queue for this datagram, in which we
 491  *      will insert the received fragments at their respective positions.
 492  */
 493 
 494 static struct ipq *ip_create(struct sk_buff *skb, struct iphdr *iph, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 495 {
 496         struct ipq *qp;
 497         int ihlen;
 498 
 499         qp = (struct ipq *) kmalloc(sizeof(struct ipq), GFP_ATOMIC);
 500         if (qp == NULL)
 501         {
 502                 NETDEBUG(printk("IP: create: no memory left !\n"));
 503                 return(NULL);
 504                 skb->dev = qp->dev;
 505         }
 506         memset(qp, 0, sizeof(struct ipq));
 507 
 508         /*
 509          *      Allocate memory for the IP header (plus 8 octets for ICMP).
 510          */
 511 
 512         ihlen = iph->ihl * 4;
 513         qp->iph = (struct iphdr *) kmalloc(64 + 8, GFP_ATOMIC);
 514         if (qp->iph == NULL)
 515         {
 516                 NETDEBUG(printk("IP: create: no memory left !\n"));
 517                 kfree_s(qp, sizeof(struct ipq));
 518                 return(NULL);
 519         }
 520 
 521         memcpy(qp->iph, iph, ihlen + 8);
 522         qp->len = 0;
 523         qp->ihlen = ihlen;
 524         qp->fragments = NULL;
 525         qp->dev = dev;
 526 
 527         /* Start a timer for this entry. */
 528         qp->timer.expires = jiffies + IP_FRAG_TIME;     /* about 30 seconds     */
 529         qp->timer.data = (unsigned long) qp;            /* pointer to queue     */
 530         qp->timer.function = ip_expire;                 /* expire function      */
 531         add_timer(&qp->timer);
 532 
 533         /* Add this entry to the queue. */
 534         qp->prev = NULL;
 535         cli();
 536         qp->next = ipqueue;
 537         if (qp->next != NULL)
 538                 qp->next->prev = qp;
 539         ipqueue = qp;
 540         sti();
 541         return(qp);
 542 }
 543 
 544 
 545 /*
 546  *      See if a fragment queue is complete.
 547  */
 548 
 549 static int ip_done(struct ipq *qp)
     /* [previous][next][first][last][top][bottom][index][help] */
 550 {
 551         struct ipfrag *fp;
 552         int offset;
 553 
 554         /* Only possible if we received the final fragment. */
 555         if (qp->len == 0)
 556                 return(0);
 557 
 558         /* Check all fragment offsets to see if they connect. */
 559         fp = qp->fragments;
 560         offset = 0;
 561         while (fp != NULL)
 562         {
 563                 if (fp->offset > offset)
 564                         return(0);      /* fragment(s) missing */
 565                 offset = fp->end;
 566                 fp = fp->next;
 567         }
 568 
 569         /* All fragments are present. */
 570         return(1);
 571 }
 572 
 573 
 574 /*
 575  *      Build a new IP datagram from all its fragments.
 576  *
 577  *      FIXME: We copy here because we lack an effective way of handling lists
 578  *      of bits on input. Until the new skb data handling is in I'm not going
 579  *      to touch this with a bargepole. 
 580  */
 581 
 582 static struct sk_buff *ip_glue(struct ipq *qp)
     /* [previous][next][first][last][top][bottom][index][help] */
 583 {
 584         struct sk_buff *skb;
 585         struct iphdr *iph;
 586         struct ipfrag *fp;
 587         unsigned char *ptr;
 588         int count, len;
 589 
 590         /*
 591          *      Allocate a new buffer for the datagram.
 592          */
 593         len = qp->ihlen + qp->len;
 594 
 595         if ((skb = dev_alloc_skb(len)) == NULL)
 596         {
 597                 ip_statistics.IpReasmFails++;
 598                 NETDEBUG(printk("IP: queue_glue: no memory for gluing queue %p\n", qp));
 599                 ip_free(qp);
 600                 return(NULL);
 601         }
 602 
 603         /* Fill in the basic details. */
 604         skb_put(skb,len);
 605         skb->h.raw = skb->data;
 606         skb->free = 1;
 607 
 608         /* Copy the original IP headers into the new buffer. */
 609         ptr = (unsigned char *) skb->h.raw;
 610         memcpy(ptr, ((unsigned char *) qp->iph), qp->ihlen);
 611         ptr += qp->ihlen;
 612 
 613         count = 0;
 614 
 615         /* Copy the data portions of all fragments into the new buffer. */
 616         fp = qp->fragments;
 617         while(fp != NULL)
 618         {
 619                 if(count+fp->len > skb->len)
 620                 {
 621                         NETDEBUG(printk("Invalid fragment list: Fragment over size.\n"));
 622                         ip_free(qp);
 623                         kfree_skb(skb,FREE_WRITE);
 624                         ip_statistics.IpReasmFails++;
 625                         return NULL;
 626                 }
 627                 memcpy((ptr + fp->offset), fp->ptr, fp->len);
 628                 count += fp->len;
 629                 fp = fp->next;
 630         }
 631 
 632         /* We glued together all fragments, so remove the queue entry. */
 633         ip_free(qp);
 634 
 635         /* Done with all fragments. Fixup the new IP header. */
 636         iph = skb->h.iph;
 637         iph->frag_off = 0;
 638         iph->tot_len = htons((iph->ihl * 4) + count);
 639         skb->ip_hdr = iph;
 640 
 641         ip_statistics.IpReasmOKs++;
 642         return(skb);
 643 }
 644 
 645 
 646 /*
 647  *      Process an incoming IP datagram fragment.
 648  */
 649 
 650 static struct sk_buff *ip_defrag(struct iphdr *iph, struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 651 {
 652         struct ipfrag *prev, *next, *tmp;
 653         struct ipfrag *tfp;
 654         struct ipq *qp;
 655         struct sk_buff *skb2;
 656         unsigned char *ptr;
 657         int flags, offset;
 658         int i, ihl, end;
 659 
 660         ip_statistics.IpReasmReqds++;
 661 
 662         /* Find the entry of this IP datagram in the "incomplete datagrams" queue. */
 663         qp = ip_find(iph);
 664 
 665         /* Is this a non-fragmented datagram? */
 666         offset = ntohs(iph->frag_off);
 667         flags = offset & ~IP_OFFSET;
 668         offset &= IP_OFFSET;
 669         if (((flags & IP_MF) == 0) && (offset == 0))
 670         {
 671                 if (qp != NULL)
 672                         ip_free(qp);    /* Huh? How could this exist?? */
 673                 return(skb);
 674         }
 675 
 676         offset <<= 3;           /* offset is in 8-byte chunks */
 677 
 678         /*
 679          * If the queue already existed, keep restarting its timer as long
 680          * as we still are receiving fragments.  Otherwise, create a fresh
 681          * queue entry.
 682          */
 683 
 684         if (qp != NULL)
 685         {
 686                 del_timer(&qp->timer);
 687                 qp->timer.expires = jiffies + IP_FRAG_TIME;     /* about 30 seconds */
 688                 qp->timer.data = (unsigned long) qp;    /* pointer to queue */
 689                 qp->timer.function = ip_expire;         /* expire function */
 690                 add_timer(&qp->timer);
 691         }
 692         else
 693         {
 694                 /*
 695                  *      If we failed to create it, then discard the frame
 696                  */
 697                 if ((qp = ip_create(skb, iph, dev)) == NULL)
 698                 {
 699                         skb->sk = NULL;
 700                         kfree_skb(skb, FREE_READ);
 701                         ip_statistics.IpReasmFails++;
 702                         return NULL;
 703                 }
 704         }
 705 
 706         /*
 707          *      Determine the position of this fragment.
 708          */
 709 
 710         ihl = iph->ihl * 4;
 711         end = offset + ntohs(iph->tot_len) - ihl;
 712 
 713         /*
 714          *      Point into the IP datagram 'data' part.
 715          */
 716 
 717         ptr = skb->data + ihl;
 718 
 719         /*
 720          *      Is this the final fragment?
 721          */
 722 
 723         if ((flags & IP_MF) == 0)
 724                 qp->len = end;
 725 
 726         /*
 727          *      Find out which fragments are in front and at the back of us
 728          *      in the chain of fragments so far.  We must know where to put
 729          *      this fragment, right?
 730          */
 731 
 732         prev = NULL;
 733         for(next = qp->fragments; next != NULL; next = next->next)
 734         {
 735                 if (next->offset > offset)
 736                         break;  /* bingo! */
 737                 prev = next;
 738         }
 739 
 740         /*
 741          *      We found where to put this one.
 742          *      Check for overlap with preceding fragment, and, if needed,
 743          *      align things so that any overlaps are eliminated.
 744          */
 745         if (prev != NULL && offset < prev->end)
 746         {
 747                 i = prev->end - offset;
 748                 offset += i;    /* ptr into datagram */
 749                 ptr += i;       /* ptr into fragment data */
 750         }
 751 
 752         /*
 753          * Look for overlap with succeeding segments.
 754          * If we can merge fragments, do it.
 755          */
 756 
 757         for(tmp=next; tmp != NULL; tmp = tfp)
 758         {
 759                 tfp = tmp->next;
 760                 if (tmp->offset >= end)
 761                         break;          /* no overlaps at all */
 762 
 763                 i = end - next->offset;                 /* overlap is 'i' bytes */
 764                 tmp->len -= i;                          /* so reduce size of    */
 765                 tmp->offset += i;                       /* next fragment        */
 766                 tmp->ptr += i;
 767                 /*
 768                  *      If we get a frag size of <= 0, remove it and the packet
 769                  *      that it goes with.
 770                  */
 771                 if (tmp->len <= 0)
 772                 {
 773                         if (tmp->prev != NULL)
 774                                 tmp->prev->next = tmp->next;
 775                         else
 776                                 qp->fragments = tmp->next;
 777 
 778                         if (tfp->next != NULL)
 779                                 tmp->next->prev = tmp->prev;
 780                         
 781                         next=tfp;       /* We have killed the original next frame */
 782 
 783                         kfree_skb(tmp->skb,FREE_READ);
 784                         kfree_s(tmp, sizeof(struct ipfrag));
 785                 }
 786         }
 787 
 788         /*
 789          *      Insert this fragment in the chain of fragments.
 790          */
 791 
 792         tfp = NULL;
 793         tfp = ip_frag_create(offset, end, skb, ptr);
 794 
 795         /*
 796          *      No memory to save the fragment - so throw the lot
 797          */
 798 
 799         if (!tfp)
 800         {
 801                 skb->sk = NULL;
 802                 kfree_skb(skb, FREE_READ);
 803                 return NULL;
 804         }
 805         tfp->prev = prev;
 806         tfp->next = next;
 807         if (prev != NULL)
 808                 prev->next = tfp;
 809         else
 810                 qp->fragments = tfp;
 811 
 812         if (next != NULL)
 813                 next->prev = tfp;
 814 
 815         /*
 816          *      OK, so we inserted this new fragment into the chain.
 817          *      Check if we now have a full IP datagram which we can
 818          *      bump up to the IP layer...
 819          */
 820 
 821         if (ip_done(qp))
 822         {
 823                 skb2 = ip_glue(qp);             /* glue together the fragments */
 824                 return(skb2);
 825         }
 826         return(NULL);
 827 }
 828 
 829 
 830 /*
 831  *      This IP datagram is too large to be sent in one piece.  Break it up into
 832  *      smaller pieces (each of size equal to the MAC header plus IP header plus
 833  *      a block of the data of the original IP data part) that will yet fit in a
 834  *      single device frame, and queue such a frame for sending by calling the
 835  *      ip_queue_xmit().  Note that this is recursion, and bad things will happen
 836  *      if this function causes a loop...
 837  *
 838  *      Yes this is inefficient, feel free to submit a quicker one.
 839  *
 840  *      **Protocol Violation**
 841  *      We copy all the options to each fragment. !FIXME!
 842  */
 843  
 844 void ip_fragment(struct sock *sk, struct sk_buff *skb, struct device *dev, int is_frag)
     /* [previous][next][first][last][top][bottom][index][help] */
 845 {
 846         struct iphdr *iph;
 847         unsigned char *raw;
 848         unsigned char *ptr;
 849         struct sk_buff *skb2;
 850         int left, mtu, hlen, len;
 851         int offset;
 852         unsigned long flags;
 853 
 854         /*
 855          *      Point into the IP datagram header.
 856          */
 857 
 858         raw = skb->data;
 859 #if 0
 860         iph = (struct iphdr *) (raw + dev->hard_header_len);    
 861         skb->ip_hdr = iph;
 862 #else
 863         iph = skb->ip_hdr;
 864 #endif
 865 
 866         /*
 867          *      Setup starting values.
 868          */
 869 
 870         hlen = iph->ihl * 4;
 871         left = ntohs(iph->tot_len) - hlen;      /* Space per frame */
 872         hlen += dev->hard_header_len;           /* Total header size */
 873         mtu = (dev->mtu - hlen);                /* Size of data space */
 874         ptr = (raw + hlen);                     /* Where to start from */
 875 
 876         /*
 877          *      Check for any "DF" flag. [DF means do not fragment]
 878          */
 879 
 880         if (ntohs(iph->frag_off) & IP_DF)
 881         {
 882                 /*
 883                  *      Reply giving the MTU of the failed hop.
 884                  */
 885                 ip_statistics.IpFragFails++;
 886                 icmp_send(skb,ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, dev->mtu, dev);
 887                 return;
 888         }
 889 
 890         /*
 891          *      The protocol doesn't seem to say what to do in the case that the
 892          *      frame + options doesn't fit the mtu. As it used to fall down dead
 893          *      in this case we were fortunate it didn't happen
 894          */
 895 
 896         if(mtu<8)
 897         {
 898                 /* It's wrong but it's better than nothing */
 899                 icmp_send(skb,ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED,dev->mtu, dev);
 900                 ip_statistics.IpFragFails++;
 901                 return;
 902         }
 903 
 904         /*
 905          *      Fragment the datagram.
 906          */
 907 
 908         /*
 909          *      The initial offset is 0 for a complete frame. When
 910          *      fragmenting fragments it's wherever this one starts.
 911          */
 912 
 913         if (is_frag & 2)
 914                 offset = (ntohs(iph->frag_off) & IP_OFFSET) << 3;
 915         else
 916                 offset = 0;
 917 
 918 
 919         /*
 920          *      Keep copying data until we run out.
 921          */
 922 
 923         while(left > 0)
 924         {
 925                 len = left;
 926                 /* IF: it doesn't fit, use 'mtu' - the data space left */
 927                 if (len > mtu)
 928                         len = mtu;
 929                 /* IF: we are not sending upto and including the packet end
 930                    then align the next start on an eight byte boundary */
 931                 if (len < left)
 932                 {
 933                         len/=8;
 934                         len*=8;
 935                 }
 936                 /*
 937                  *      Allocate buffer.
 938                  */
 939 
 940                 if ((skb2 = alloc_skb(len + hlen+15,GFP_ATOMIC)) == NULL)
 941                 {
 942                         NETDEBUG(printk("IP: frag: no memory for new fragment!\n"));
 943                         ip_statistics.IpFragFails++;
 944                         return;
 945                 }
 946 
 947                 /*
 948                  *      Set up data on packet
 949                  */
 950 
 951                 skb2->arp = skb->arp;
 952                 if(skb->free==0)
 953                         printk("IP fragmenter: BUG free!=1 in fragmenter\n");
 954                 skb2->free = 1;
 955                 skb_put(skb2,len + hlen);
 956                 skb2->h.raw=(char *) skb2->data;
 957                 /*
 958                  *      Charge the memory for the fragment to any owner
 959                  *      it might possess
 960                  */
 961 
 962                 save_flags(flags);
 963                 if (sk)
 964                 {
 965                         cli();
 966                         sk->wmem_alloc += skb2->truesize;
 967                         skb2->sk=sk;
 968                 }
 969                 restore_flags(flags);
 970                 skb2->raddr = skb->raddr;       /* For rebuild_header - must be here */
 971 
 972                 /*
 973                  *      Copy the packet header into the new buffer.
 974                  */
 975 
 976                 memcpy(skb2->h.raw, raw, hlen);
 977 
 978                 /*
 979                  *      Copy a block of the IP datagram.
 980                  */
 981                 memcpy(skb2->h.raw + hlen, ptr, len);
 982                 left -= len;
 983 
 984                 skb2->h.raw+=dev->hard_header_len;
 985 
 986                 /*
 987                  *      Fill in the new header fields.
 988                  */
 989                 iph = (struct iphdr *)(skb2->h.raw/*+dev->hard_header_len*/);
 990                 iph->frag_off = htons((offset >> 3));
 991                 skb2->ip_hdr = iph;
 992                 /*
 993                  *      Added AC : If we are fragmenting a fragment thats not the
 994                  *                 last fragment then keep MF on each bit
 995                  */
 996                 if (left > 0 || (is_frag & 1))
 997                         iph->frag_off |= htons(IP_MF);
 998                 ptr += len;
 999                 offset += len;
1000 
1001                 /*
1002                  *      Put this fragment into the sending queue.
1003                  */
1004 
1005                 ip_statistics.IpFragCreates++;
1006 
1007                 ip_queue_xmit(sk, dev, skb2, 2);
1008         }
1009         ip_statistics.IpFragOKs++;
1010 }
1011 
1012 
1013 
1014 #ifdef CONFIG_IP_FORWARD
1015 
1016 /*
1017  *      Forward an IP datagram to its next destination.
1018  */
1019 
1020 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] */
1021 {
1022         struct device *dev2;    /* Output device */
1023         struct iphdr *iph;      /* Our header */
1024         struct sk_buff *skb2;   /* Output packet */
1025         struct rtable *rt;      /* Route we use */
1026         unsigned char *ptr;     /* Data pointer */
1027         unsigned long raddr;    /* Router IP address */
1028 #ifdef CONFIG_IP_FIREWALL
1029         int fw_res = 0;         /* Forwarding result */ 
1030         
1031         /* 
1032          *      See if we are allowed to forward this.
1033          *      Note: demasqueraded fragments are always 'back'warded.
1034          */
1035 
1036         
1037         if(!(is_frag&4))
1038         {
1039                 fw_res=ip_fw_chk(skb->h.iph, dev, ip_fw_fwd_chain, ip_fw_fwd_policy, 0);
1040                 switch (fw_res) {
1041                 case 1:
1042 #ifdef CONFIG_IP_MASQUERADE
1043                 case 2:
1044 #endif
1045                         break;
1046                 case -1:
1047                         icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0, dev);
1048                         /* fall thru */
1049                 default:
1050                         return -1;
1051                 }
1052         }
1053 #endif
1054         /*
1055          *      According to the RFC, we must first decrease the TTL field. If
1056          *      that reaches zero, we must reply an ICMP control message telling
1057          *      that the packet's lifetime expired.
1058          *
1059          *      Exception:
1060          *      We may not generate an ICMP for an ICMP. icmp_send does the
1061          *      enforcement of this so we can forget it here. It is however
1062          *      sometimes VERY important.
1063          */
1064 
1065         iph = skb->h.iph;
1066         iph->ttl--;
1067 
1068         /*
1069          *      Re-compute the IP header checksum.
1070          *      This is inefficient. We know what has happened to the header
1071          *      and could thus adjust the checksum as Phil Karn does in KA9Q
1072          */
1073 
1074         iph->check = ntohs(iph->check) + 0x0100;
1075         if ((iph->check & 0xFF00) == 0)
1076                 iph->check++;           /* carry overflow */
1077         iph->check = htons(iph->check);
1078 
1079         if (iph->ttl <= 0)
1080         {
1081                 /* Tell the sender its packet died... */
1082                 icmp_send(skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, 0, dev);
1083                 return -1;
1084         }
1085 
1086         /*
1087          * OK, the packet is still valid.  Fetch its destination address,
1088          * and give it to the IP sender for further processing.
1089          */
1090 
1091         rt = ip_rt_route(target_addr, NULL, NULL);
1092         if (rt == NULL)
1093         {
1094                 /*
1095                  *      Tell the sender its packet cannot be delivered. Again
1096                  *      ICMP is screened later.
1097                  */
1098                 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_UNREACH, 0, dev);
1099                 return -1;
1100         }
1101 
1102 
1103         /*
1104          * Gosh.  Not only is the packet valid; we even know how to
1105          * forward it onto its final destination.  Can we say this
1106          * is being plain lucky?
1107          * If the router told us that there is no GW, use the dest.
1108          * IP address itself- we seem to be connected directly...
1109          */
1110 
1111         raddr = rt->rt_gateway;
1112 
1113         if (raddr != 0)
1114         {
1115                 /*
1116                  *      Strict routing permits no gatewaying
1117                  */
1118                 
1119                 if(target_strict)
1120                 {
1121                         icmp_send(skb, ICMP_DEST_UNREACH, ICMP_SR_FAILED, 0, dev);
1122                         return -1;
1123                 }
1124         
1125                 /*
1126                  *      There is a gateway so find the correct route for it.
1127                  *      Gateways cannot in turn be gatewayed.
1128                  */
1129 
1130                 rt = ip_rt_route(raddr, NULL, NULL);
1131                 if (rt == NULL)
1132                 {
1133                         /*
1134                          *      Tell the sender its packet cannot be delivered...
1135                          */
1136                         icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0, dev);
1137                         return -1;
1138                 }
1139                 if (rt->rt_gateway != 0)
1140                         raddr = rt->rt_gateway;
1141         }
1142         else
1143                 raddr = target_addr;
1144 
1145         /*
1146          *      Having picked a route we can now send the frame out.
1147          */
1148 
1149         dev2 = rt->rt_dev;
1150         
1151         /*
1152          *      In IP you never have to forward a frame on the interface that it 
1153          *      arrived upon. We now generate an ICMP HOST REDIRECT giving the route
1154          *      we calculated.
1155          */
1156 #ifndef CONFIG_IP_NO_ICMP_REDIRECT
1157         if (dev == dev2 && !((iph->saddr^iph->daddr)&dev->pa_mask) && (rt->rt_flags&RTF_MODIFIED))
1158                 icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, raddr, dev);
1159 #endif          
1160 
1161         /*
1162          * We now may allocate a new buffer, and copy the datagram into it.
1163          * If the indicated interface is up and running, kick it.
1164          */
1165 
1166         if (dev2->flags & IFF_UP)
1167         {
1168 #ifdef CONFIG_IP_MASQUERADE
1169                 /*
1170                  * If this fragment needs masquerading, make it so...
1171                  * (Dont masquerade de-masqueraded fragments)
1172                  */
1173                 if (!(is_frag&4) && fw_res==2)
1174                         ip_fw_masquerade(&skb, dev2);
1175 #endif
1176                 IS_SKB(skb);
1177 
1178                 if(skb_headroom(skb)<dev2->hard_header_len)
1179                 {
1180                         skb2 = alloc_skb(dev2->hard_header_len + skb->len + 15, GFP_ATOMIC);
1181                         IS_SKB(skb2);
1182                 
1183                         /*
1184                          *      This is rare and since IP is tolerant of network failures
1185                          *      quite harmless.
1186                          */
1187                 
1188                         if (skb2 == NULL)
1189                         {
1190                                 NETDEBUG(printk("\nIP: No memory available for IP forward\n"));
1191                                 return -1;
1192                         }
1193                 
1194                         /*
1195                          *      Add the physical headers.
1196                          */
1197 
1198                         ip_send(skb2,raddr,skb->len,dev2,dev2->pa_addr);
1199 
1200                         /*
1201                          *      We have to copy the bytes over as the new header wouldn't fit
1202                          *      the old buffer. This should be very rare.
1203                          */              
1204                         
1205                         ptr = skb_put(skb2,skb->len);
1206                         skb2->free = 1;
1207                         skb2->h.raw = ptr;
1208 
1209                         /*
1210                          *      Copy the packet data into the new buffer.
1211                          */
1212                         memcpy(ptr, skb->h.raw, skb->len);
1213                 }
1214                 else
1215                 {
1216                         /* 
1217                          *      Build a new MAC header. 
1218                          */
1219 
1220                         skb2 = skb;             
1221                         skb2->dev=dev2;
1222                         skb->arp=1;
1223                         skb->raddr=raddr;
1224                         if(dev2->hard_header)
1225                         {
1226                                 if(dev2->hard_header(skb, dev2, ETH_P_IP, NULL, NULL, skb->len)<0)
1227                                         skb->arp=0;
1228                         }
1229                         ip_statistics.IpForwDatagrams++;
1230                 }
1231                 /*
1232                  *      See if it needs fragmenting. Note in ip_rcv we tagged
1233                  *      the fragment type. This must be right so that
1234                  *      the fragmenter does the right thing.
1235                  */
1236 
1237                 if(skb2->len > dev2->mtu + dev2->hard_header_len)
1238                 {
1239                         ip_fragment(NULL,skb2,dev2, is_frag);
1240                         kfree_skb(skb2,FREE_WRITE);
1241                 }
1242                 else
1243                 {
1244 #ifdef CONFIG_IP_ACCT           
1245                         /*
1246                          *      Count mapping we shortcut
1247                          */
1248                          
1249                         ip_fw_chk(iph,dev,ip_acct_chain,IP_FW_F_ACCEPT,1);
1250 #endif                  
1251                         
1252                         /*
1253                          *      Map service types to priority. We lie about
1254                          *      throughput being low priority, but it's a good
1255                          *      choice to help improve general usage.
1256                          */
1257                         if(iph->tos & IPTOS_LOWDELAY)
1258                                 dev_queue_xmit(skb2, dev2, SOPRI_INTERACTIVE);
1259                         else if(iph->tos & IPTOS_THROUGHPUT)
1260                                 dev_queue_xmit(skb2, dev2, SOPRI_BACKGROUND);
1261                         else
1262                                 dev_queue_xmit(skb2, dev2, SOPRI_NORMAL);
1263                 }
1264         }
1265         else
1266                 return -1;
1267         
1268         /*
1269          *      Tell the caller if their buffer is free.
1270          */
1271          
1272         if(skb==skb2)
1273                 return 0;
1274         return 1;
1275 }
1276 
1277 
1278 #endif
1279 
1280 /*
1281  *      This function receives all incoming IP datagrams.
1282  *
1283  *      On entry skb->data points to the start of the IP header and
1284  *      the MAC header has been removed.
1285  */
1286 
1287 int ip_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
     /* [previous][next][first][last][top][bottom][index][help] */
1288 {
1289         struct iphdr *iph = skb->h.iph;
1290         struct sock *raw_sk=NULL;
1291         unsigned char hash;
1292         unsigned char flag = 0;
1293         struct inet_protocol *ipprot;
1294         int brd=IS_MYADDR;
1295         unsigned long target_addr;
1296         int target_strict=0;
1297         int is_frag=0;
1298 #ifdef CONFIG_IP_FIREWALL
1299         int err;
1300 #endif  
1301 
1302         ip_statistics.IpInReceives++;
1303 
1304         /*
1305          *      Tag the ip header of this packet so we can find it
1306          */
1307 
1308         skb->ip_hdr = iph;
1309 
1310         /*
1311          *      RFC1122: 3.1.2.2 MUST silently discard any IP frame that fails the checksum.
1312          *      RFC1122: 3.1.2.3 MUST discard a frame with invalid source address [NEEDS FIXING].
1313          *
1314          *      Is the datagram acceptable?
1315          *
1316          *      1.      Length at least the size of an ip header
1317          *      2.      Version of 4
1318          *      3.      Checksums correctly. [Speed optimisation for later, skip loopback checksums]
1319          *      4.      Doesn't have a bogus length
1320          *      (5.     We ought to check for IP multicast addresses and undefined types.. does this matter ?)
1321          */
1322 
1323         if (skb->len<sizeof(struct iphdr) || iph->ihl<5 || iph->version != 4 || ip_fast_csum((unsigned char *)iph, iph->ihl) !=0
1324                 || skb->len < ntohs(iph->tot_len))
1325         {
1326                 ip_statistics.IpInHdrErrors++;
1327                 kfree_skb(skb, FREE_WRITE);
1328                 return(0);
1329         }
1330 
1331         /*
1332          *      Our transport medium may have padded the buffer out. Now we know it
1333          *      is IP we can trim to the true length of the frame.
1334          *      Note this now means skb->len holds ntohs(iph->tot_len).
1335          */
1336 
1337         skb_trim(skb,ntohs(iph->tot_len));
1338         
1339         /*
1340          *      See if the firewall wants to dispose of the packet. 
1341          */
1342 
1343 #ifdef  CONFIG_IP_FIREWALL
1344         
1345         if ((err=ip_fw_chk(iph,dev,ip_fw_blk_chain,ip_fw_blk_policy, 0))<1)
1346         {
1347                 if(err==-1)
1348                         icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0, dev);
1349                 kfree_skb(skb, FREE_WRITE);
1350                 return 0;       
1351         }
1352 
1353 #endif
1354         
1355 
1356         /*
1357          *      Next analyse the packet for options. Studies show under one packet in
1358          *      a thousand have options....
1359          */
1360          
1361         target_addr = iph->daddr;
1362 
1363         if (iph->ihl != 5)
1364         { 
1365                 /* Humph.. options. Lots of annoying fiddly bits */
1366                 
1367                 /*
1368                  *      This is straight from the RFC. It might even be right ;)
1369                  *
1370                  *      RFC 1122: 3.2.1.8 STREAMID option is obsolete and MUST be ignored.
1371                  *      RFC 1122: 3.2.1.8 MUST NOT crash on a zero length option.
1372                  *      RFC 1122: 3.2.1.8 MUST support acting as final destination of a source route.
1373                  */
1374                  
1375                 int opt_space=4*(iph->ihl-5);
1376                 int opt_size;
1377                 unsigned char *opt_ptr=skb->h.raw+sizeof(struct iphdr);
1378         
1379                 skb->ip_summed=0;               /* Our free checksum is bogus for this case */
1380                         
1381                 while(opt_space>0)
1382                 {
1383                         if(*opt_ptr==IPOPT_NOOP)
1384                         {
1385                                 opt_ptr++;
1386                                 opt_space--;
1387                                 continue;
1388                         }
1389                         if(*opt_ptr==IPOPT_END)
1390                                 break;  /* Done */
1391                         if(opt_space<2 || (opt_size=opt_ptr[1])<2 || opt_ptr[1]>opt_space)
1392                         {
1393                                 /*
1394                                  *      RFC 1122: 3.2.2.5  SHOULD send parameter problem reports.
1395                                  */
1396                                 icmp_send(skb, ICMP_PARAMETERPROB, 0, 0, skb->dev);
1397                                 kfree_skb(skb, FREE_READ);
1398                                 return -EINVAL;
1399                         }
1400                         switch(opt_ptr[0])
1401                         {
1402                                 case IPOPT_SEC:
1403                                         /* Should we drop this ?? */
1404                                         break;
1405                                 case IPOPT_SSRR:        /* These work almost the same way */
1406                                         target_strict=1;
1407                                         /* Fall through */
1408                                 case IPOPT_LSRR:
1409 #ifdef CONFIG_IP_NOSR
1410                                         kfree_skb(skb, FREE_READ);
1411                                         return -EINVAL;
1412 #endif                                  
1413                                 case IPOPT_RR:
1414                                 /*
1415                                  *      RFC 1122: 3.2.1.8 Support for RR is OPTIONAL.
1416                                  */
1417                                         if (iph->daddr!=skb->dev->pa_addr && (brd = ip_chk_addr(iph->daddr)) == 0) 
1418                                                 break;
1419                                         if((opt_size<3) || ( opt_ptr[0]==IPOPT_RR && opt_ptr[2] > opt_size-4 ))
1420                                         {
1421                                                 if(ip_chk_addr(iph->daddr))
1422                                                         icmp_send(skb, ICMP_PARAMETERPROB, 0, 0, skb->dev);
1423                                                 kfree_skb(skb, FREE_READ);
1424                                                 return -EINVAL;
1425                                         }
1426                                         if(opt_ptr[2] > opt_size-4 )
1427                                                 break;
1428                                         /* Bytes are [IPOPT_xxRR][Length][EntryPointer][Entry0][Entry1].... */
1429                                         /* This isn't going to be too portable - FIXME */
1430                                         if(opt_ptr[0]!=IPOPT_RR)
1431                                         {
1432                                                 int t;
1433                                                 target_addr=*(u32 *)(&opt_ptr[opt_ptr[2]]);     /* Get hop */
1434                                                 t=ip_chk_addr(target_addr);
1435                                                 if(t==IS_MULTICAST||t==IS_BROADCAST)
1436                                                 {
1437                                                         if(ip_chk_addr(iph->daddr))
1438                                                                 icmp_send(skb, ICMP_PARAMETERPROB, 0, 0, skb->dev);
1439                                                         kfree_skb(skb,FREE_READ);
1440                                                         return -EINVAL;                                         
1441                                                 }
1442                                         }
1443                                         *(u32 *)(&opt_ptr[opt_ptr[2]])=skb->dev->pa_addr;       /* Record hop */
1444                                         break;
1445                                 case IPOPT_TIMESTAMP:
1446                                 /*
1447                                  *      RFC 1122: 3.2.1.8 The timestamp option is OPTIONAL but if implemented
1448                                  *      MUST meet various rules (read the spec).
1449                                  */
1450                                         NETDEBUG(printk("ICMP: Someone finish the timestamp routine ;)\n"));
1451                                         break;
1452                                 default:
1453                                         break;
1454                         }
1455                         opt_ptr+=opt_size;
1456                         opt_space-=opt_size;
1457                 }
1458                                         
1459         }
1460 
1461 
1462         /*
1463          *      Remember if the frame is fragmented.
1464          */
1465          
1466         if(iph->frag_off)
1467         {
1468                 if (iph->frag_off & htons(IP_MF))
1469                         is_frag|=1;
1470                 /*
1471                  *      Last fragment ?
1472                  */
1473         
1474                 if (iph->frag_off & htons(IP_OFFSET))
1475                         is_frag|=2;
1476         }
1477         
1478         /*
1479          *      Do any IP forwarding required.  chk_addr() is expensive -- avoid it someday.
1480          *
1481          *      This is inefficient. While finding out if it is for us we could also compute
1482          *      the routing table entry. This is where the great unified cache theory comes
1483          *      in as and when someone implements it
1484          *
1485          *      For most hosts over 99% of packets match the first conditional
1486          *      and don't go via ip_chk_addr. Note: brd is set to IS_MYADDR at
1487          *      function entry.
1488          */
1489 
1490         if ( iph->daddr == skb->dev->pa_addr || (brd = ip_chk_addr(iph->daddr)) != 0)
1491         {
1492 #ifdef CONFIG_IP_MULTICAST      
1493 
1494                 if(brd==IS_MULTICAST && iph->daddr!=IGMP_ALL_HOSTS && !(dev->flags&IFF_LOOPBACK))
1495                 {
1496                         /*
1497                          *      Check it is for one of our groups
1498                          */
1499                         struct ip_mc_list *ip_mc=dev->ip_mc_list;
1500                         do
1501                         {
1502                                 if(ip_mc==NULL)
1503                                 {       
1504                                         kfree_skb(skb, FREE_WRITE);
1505                                         return 0;
1506                                 }
1507                                 if(ip_mc->multiaddr==iph->daddr)
1508                                         break;
1509                                 ip_mc=ip_mc->next;
1510                         }
1511                         while(1);
1512                 }
1513 #endif
1514 
1515 #ifdef CONFIG_IP_MASQUERADE
1516                 /*
1517                  * Do we need to de-masquerade this fragment?
1518                  */
1519                 if (ip_fw_demasquerade(skb)) 
1520                 {
1521                         struct iphdr *iph=skb->h.iph;
1522                         if(ip_forward(skb, dev, is_frag|4, iph->daddr, 0))
1523                                 kfree_skb(skb, FREE_WRITE);
1524                         return(0);
1525                 }
1526 #endif
1527 
1528                 /*
1529                  *      Account for the packet
1530                  */
1531  
1532 #ifdef CONFIG_IP_ACCT
1533                 ip_fw_chk(iph,dev,ip_acct_chain,IP_FW_F_ACCEPT,1);
1534 #endif  
1535 
1536                 /*
1537                  *      Reassemble IP fragments.
1538                  */
1539 
1540                 if(is_frag)
1541                 {
1542                         /* Defragment. Obtain the complete packet if there is one */
1543                         skb=ip_defrag(iph,skb,dev);
1544                         if(skb==NULL)
1545                                 return 0;
1546                         skb->dev = dev;
1547                         iph=skb->h.iph;
1548                 }
1549 
1550                 /*
1551                  *      Point into the IP datagram, just past the header.
1552                  */
1553 
1554                 skb->ip_hdr = iph;
1555                 skb->h.raw += iph->ihl*4;
1556 
1557                 /*
1558                  *      Deliver to raw sockets. This is fun as to avoid copies we want to make no surplus copies.
1559                  *
1560                  *      RFC 1122: SHOULD pass TOS value up to the transport layer.
1561                  */
1562  
1563                 hash = iph->protocol & (SOCK_ARRAY_SIZE-1);
1564 
1565                 /* 
1566                  *      If there maybe a raw socket we must check - if not we don't care less 
1567                  */
1568                  
1569                 if((raw_sk=raw_prot.sock_array[hash])!=NULL)
1570                 {
1571                         struct sock *sknext=NULL;
1572                         struct sk_buff *skb1;
1573                         raw_sk=get_sock_raw(raw_sk, hash,  iph->saddr, iph->daddr);
1574                         if(raw_sk)      /* Any raw sockets */
1575                         {
1576                                 do
1577                                 {
1578                                         /* Find the next */
1579                                         sknext=get_sock_raw(raw_sk->next, hash, iph->saddr, iph->daddr);
1580                                         if(sknext)
1581                                                 skb1=skb_clone(skb, GFP_ATOMIC);
1582                                         else
1583                                                 break;  /* One pending raw socket left */
1584                                         if(skb1)
1585                                                 raw_rcv(raw_sk, skb1, dev, iph->saddr,iph->daddr);
1586                                         raw_sk=sknext;
1587                                 }
1588                                 while(raw_sk!=NULL);
1589                                 
1590                                 /*
1591                                  *      Here either raw_sk is the last raw socket, or NULL if none 
1592                                  */
1593                                  
1594                                 /*
1595                                  *      We deliver to the last raw socket AFTER the protocol checks as it avoids a surplus copy 
1596                                  */
1597                         }
1598                 }
1599         
1600                 /*
1601                  *      skb->h.raw now points at the protocol beyond the IP header.
1602                  */
1603         
1604                 hash = iph->protocol & (MAX_INET_PROTOS -1);
1605                 for (ipprot = (struct inet_protocol *)inet_protos[hash];ipprot != NULL;ipprot=(struct inet_protocol *)ipprot->next)
1606                 {
1607                         struct sk_buff *skb2;
1608         
1609                         if (ipprot->protocol != iph->protocol)
1610                                 continue;
1611                        /*
1612                         *       See if we need to make a copy of it.  This will
1613                         *       only be set if more than one protocol wants it.
1614                         *       and then not for the last one. If there is a pending
1615                         *       raw delivery wait for that
1616                         */
1617         
1618                         if (ipprot->copy || raw_sk)
1619                         {
1620                                 skb2 = skb_clone(skb, GFP_ATOMIC);
1621                                 if(skb2==NULL)
1622                                         continue;
1623                         }
1624                         else
1625                         {
1626                                 skb2 = skb;
1627                         }
1628                         flag = 1;
1629 
1630                        /*
1631                         *       Pass on the datagram to each protocol that wants it,
1632                         *       based on the datagram protocol.  We should really
1633                         *       check the protocol handler's return values here...
1634                         */
1635 
1636                         ipprot->handler(skb2, dev, NULL, iph->daddr,
1637                                 (ntohs(iph->tot_len) - (iph->ihl * 4)),
1638                                 iph->saddr, 0, ipprot);
1639 
1640                 }
1641 
1642                 /*
1643                  *      All protocols checked.
1644                  *      If this packet was a broadcast, we may *not* reply to it, since that
1645                  *      causes (proven, grin) ARP storms and a leakage of memory (i.e. all
1646                  *      ICMP reply messages get queued up for transmission...)
1647                  */
1648 
1649                 if(raw_sk!=NULL)        /* Shift to last raw user */
1650                         raw_rcv(raw_sk, skb, dev, iph->saddr, iph->daddr);
1651                 else if (!flag)         /* Free and report errors */
1652                 {
1653                         if (brd != IS_BROADCAST && brd!=IS_MULTICAST)
1654                                 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, 0, dev);   
1655                         kfree_skb(skb, FREE_WRITE);
1656                 }
1657 
1658                 return(0);
1659         }
1660 
1661         /*
1662          *      Do any IP forwarding required.
1663          */
1664         
1665         /*
1666          *      Don't forward multicast or broadcast frames.
1667          */
1668 
1669         if(skb->pkt_type!=PACKET_HOST || brd==IS_BROADCAST)
1670         {
1671                 kfree_skb(skb,FREE_WRITE);
1672                 return 0;
1673         }
1674 
1675         /*
1676          *      The packet is for another target. Forward the frame
1677          */
1678 
1679 #ifdef CONFIG_IP_FORWARD
1680         if(ip_forward(skb, dev, is_frag, target_addr, target_strict))
1681                 kfree_skb(skb, FREE_WRITE);
1682 #else
1683 /*      printk("Machine %lx tried to use us as a forwarder to %lx but we have forwarding disabled!\n",
1684                         iph->saddr,iph->daddr);*/
1685         ip_statistics.IpInAddrErrors++;
1686         kfree_skb(skb, FREE_WRITE);
1687 #endif
1688         return(0);
1689 }
1690         
1691 
1692 /*
1693  *      Loop a packet back to the sender.
1694  */
1695  
1696 static void ip_loopback(struct device *old_dev, struct sk_buff *skb)
     /* [previous][next][first][last][top][bottom][index][help] */
1697 {
1698         extern struct device loopback_dev;
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] */