root/net/ipv4/icmp.c

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

DEFINITIONS

This source file includes following definitions.
  1. icmp_out_count
  2. icmp_glue_bits
  3. icmp_build_xmit
  4. icmp_send
  5. icmp_unreach
  6. icmp_redirect
  7. icmp_echo
  8. icmp_timestamp
  9. icmp_address
  10. icmp_discard
  11. icmp_rcv
  12. icmp_init

   1 /*
   2  *      NET3:   Implementation of the ICMP protocol layer. 
   3  *      
   4  *              Alan Cox, <alan@cymru.net>
   5  *
   6  *      This program is free software; you can redistribute it and/or
   7  *      modify it under the terms of the GNU General Public License
   8  *      as published by the Free Software Foundation; either version
   9  *      2 of the License, or (at your option) any later version.
  10  *
  11  *      Some of the function names and the icmp unreach table for this
  12  *      module were derived from [icmp.c 1.0.11 06/02/93] by
  13  *      Ross Biro, Fred N. van Kempen, Mark Evans, Alan Cox, Gerhard Koerting.
  14  *      Other than that this module is a complete rewrite.
  15  *
  16  *      Fixes:
  17  *              Mike Shaver     :       RFC1122 checks.
  18  *              Alan Cox        :       Multicast ping reply as self.
  19  *              Alan Cox        :       Fix atomicity lockup in ip_build_xmit call
  20  *
  21  *
  22  *
  23  * RFC1122 Status: (boy, are there a lot of rules for ICMP)
  24  *  3.2.2 (Generic ICMP stuff)
  25  *   MUST discard messages of unknown type. (OK)
  26  *   MUST copy at least the first 8 bytes from the offending packet
  27  *     when sending ICMP errors. (OK)
  28  *   MUST pass received ICMP errors up to protocol level. (OK)
  29  *   SHOULD send ICMP errors with TOS == 0. (OK)
  30  *   MUST NOT send ICMP errors in reply to:
  31  *     ICMP errors (OK)
  32  *     Broadcast/multicast datagrams (OK)
  33  *     MAC broadcasts (OK)
  34  *     Non-initial fragments (OK)
  35  *     Datagram with a source address that isn't a single host. (OK)
  36  *  3.2.2.1 (Destination Unreachable)
  37  *   All the rules govern the IP layer, and are dealt with in ip.c, not here.
  38  *  3.2.2.2 (Redirect)
  39  *   Host SHOULD NOT send ICMP_REDIRECTs.  (OK)
  40  *   MUST update routing table in response to host or network redirects. 
  41  *     (host OK, network NOT YET) [Intentionally -- AC]
  42  *   SHOULD drop redirects if they're not from directly connected gateway
  43  *     (OK -- we drop it if it's not from our old gateway, which is close
  44  *      enough)
  45  * 3.2.2.3 (Source Quench)
  46  *   MUST pass incoming SOURCE_QUENCHs to transport layer (OK)
  47  *   Other requirements are dealt with at the transport layer.
  48  * 3.2.2.4 (Time Exceeded)
  49  *   MUST pass TIME_EXCEEDED to transport layer (OK)
  50  *   Other requirements dealt with at IP (generating TIME_EXCEEDED).
  51  * 3.2.2.5 (Parameter Problem)
  52  *   SHOULD generate these, but it doesn't say for what.  So we're OK. =)
  53  *   MUST pass received PARAMPROBLEM to transport layer (NOT YET)
  54  *      [Solaris 2.X seems to assert EPROTO when this occurs] -- AC
  55  * 3.2.2.6 (Echo Request/Reply)
  56  *   MUST reply to ECHO_REQUEST, and give app to do ECHO stuff (OK, OK)
  57  *   MAY discard broadcast ECHO_REQUESTs. (We don't, but that's OK.)
  58  *   MUST reply using same source address as the request was sent to.
  59  *     We're OK for unicast ECHOs, and it doesn't say anything about
  60  *     how to handle broadcast ones, since it's optional.
  61  *   MUST copy data from REQUEST to REPLY (OK)
  62  *     unless it would require illegal fragmentation (N/A)
  63  *   MUST pass REPLYs to transport/user layer (OK)
  64  *   MUST use any provided source route (reversed) for REPLY. (NOT YET)
  65  * 3.2.2.7 (Information Request/Reply)
  66  *   MUST NOT implement this. (I guess that means silently discard...?) (OK)
  67  * 3.2.2.8 (Timestamp Request/Reply)
  68  *   MAY implement (OK)
  69  *   SHOULD be in-kernel for "minimum variability" (OK)
  70  *   MAY discard broadcast REQUESTs.  (OK, but see source for inconsistency)
  71  *   MUST reply using same source address as the request was sent to. (OK)
  72  *   MUST reverse source route, as per ECHO (NOT YET)
  73  *   MUST pass REPLYs to transport/user layer (requires RAW, just like ECHO) (OK)
  74  *   MUST update clock for timestamp at least 15 times/sec (OK)
  75  *   MUST be "correct within a few minutes" (OK)
  76  * 3.2.2.9 (Address Mask Request/Reply)
  77  *   MAY implement (OK)
  78  *   MUST send a broadcast REQUEST if using this system to set netmask
  79  *     (OK... we don't use it)
  80  *   MUST discard received REPLYs if not using this system (OK)
  81  *   MUST NOT send replies unless specifically made agent for this sort
  82  *     of thing. (OK)
  83  */
  84 
  85 #include <linux/config.h>
  86 #include <linux/types.h>
  87 #include <linux/sched.h>
  88 #include <linux/kernel.h>
  89 #include <linux/fcntl.h>
  90 #include <linux/socket.h>
  91 #include <linux/in.h>
  92 #include <linux/inet.h>
  93 #include <linux/netdevice.h>
  94 #include <linux/string.h>
  95 #include <net/snmp.h>
  96 #include <net/ip.h>
  97 #include <net/route.h>
  98 #include <net/protocol.h>
  99 #include <net/icmp.h>
 100 #include <net/tcp.h>
 101 #include <net/snmp.h>
 102 #include <linux/skbuff.h>
 103 #include <net/sock.h>
 104 #include <linux/errno.h>
 105 #include <linux/timer.h>
 106 #include <asm/system.h>
 107 #include <asm/segment.h>
 108 #include <net/checksum.h>
 109 
 110 #define min(a,b)        ((a)<(b)?(a):(b))
 111 
 112 /*
 113  *      Statistics
 114  */
 115  
 116 struct icmp_mib icmp_statistics;
 117 
 118 /* An array of errno for error messages from dest unreach. */
 119 /* RFC 1122: 3.2.2.1 States that NET_UNREACH, HOS_UNREACH and SR_FAIELD MUST be considered 'transient errrs'. */
 120 
 121 struct icmp_err icmp_err_convert[] = {
 122   { ENETUNREACH,        0 },    /*      ICMP_NET_UNREACH        */
 123   { EHOSTUNREACH,       0 },    /*      ICMP_HOST_UNREACH       */
 124   { ENOPROTOOPT,        1 },    /*      ICMP_PROT_UNREACH       */
 125   { ECONNREFUSED,       1 },    /*      ICMP_PORT_UNREACH       */
 126   { EOPNOTSUPP,         0 },    /*      ICMP_FRAG_NEEDED        */
 127   { EOPNOTSUPP,         0 },    /*      ICMP_SR_FAILED          */
 128   { ENETUNREACH,        1 },    /*      ICMP_NET_UNKNOWN        */
 129   { EHOSTDOWN,          1 },    /*      ICMP_HOST_UNKNOWN       */
 130   { ENONET,             1 },    /*      ICMP_HOST_ISOLATED      */
 131   { ENETUNREACH,        1 },    /*      ICMP_NET_ANO            */
 132   { EHOSTUNREACH,       1 },    /*      ICMP_HOST_ANO           */
 133   { EOPNOTSUPP,         0 },    /*      ICMP_NET_UNR_TOS        */
 134   { EOPNOTSUPP,         0 }     /*      ICMP_HOST_UNR_TOS       */
 135 };
 136 
 137 /*
 138  *      A spare long used to speed up statistics udpating
 139  */
 140  
 141 unsigned long dummy;
 142 
 143 /*
 144  *      ICMP control array. This specifies what to do with each ICMP.
 145  */
 146  
 147 struct icmp_control
 148 {
 149         unsigned long *output;          /* Address to increment on output */
 150         unsigned long *input;           /* Address to increment on input */
 151         void (*handler)(struct icmphdr *icmph, struct sk_buff *skb, struct device *dev, __u32 saddr, __u32 daddr, int len);
 152         unsigned long error;            /* This ICMP is classed as an error message */  
 153 };
 154 
 155 static struct icmp_control icmp_pointers[19];
 156 
 157 /*
 158  *      Build xmit assembly blocks
 159  */
 160 
 161 struct icmp_bxm
 162 {
 163         void *data_ptr;
 164         int data_len;
 165         struct icmphdr icmph;
 166         unsigned long csum;
 167         struct options replyopts;
 168         unsigned char  optbuf[40];
 169 };
 170 
 171 /*
 172  *      The ICMP socket. This is the most convenient way to flow control
 173  *      our ICMP output as well as maintain a clean interface throughout
 174  *      all layers. All Socketless IP sends will soon be gone.
 175  */
 176         
 177 struct socket icmp_socket;
 178 
 179 /*
 180  *      Send an ICMP frame.
 181  */
 182  
 183 
 184 /*
 185  *      Maintain the counters used in the SNMP statistics for outgoing ICMP
 186  */
 187  
 188 static void icmp_out_count(int type)
     /* [previous][next][first][last][top][bottom][index][help] */
 189 {
 190         if(type>18)
 191                 return;
 192         (*icmp_pointers[type].output)++;
 193         icmp_statistics.IcmpOutMsgs++;
 194 }
 195  
 196 /*
 197  *      Checksum each fragment, and on the first include the headers and final checksum.
 198  */
 199  
 200 static void icmp_glue_bits(const void *p, __u32 saddr, char *to, unsigned int offset, unsigned int fraglen)
     /* [previous][next][first][last][top][bottom][index][help] */
 201 {
 202         struct icmp_bxm *icmp_param = (struct icmp_bxm *)p;
 203         struct icmphdr *icmph;
 204         unsigned long csum;
 205 
 206         if (offset) {
 207                 icmp_param->csum=csum_partial_copy(icmp_param->data_ptr+offset-sizeof(struct icmphdr), 
 208                                 to, fraglen,icmp_param->csum);
 209                 return;
 210         }
 211 
 212         /*
 213          *      First fragment includes header. Note that we've done
 214          *      the other fragments first, so that we get the checksum
 215          *      for the whole packet here.
 216          */
 217         csum = csum_partial_copy((void *)&icmp_param->icmph,
 218                 to, sizeof(struct icmphdr), 
 219                 icmp_param->csum);
 220         csum = csum_partial_copy(icmp_param->data_ptr,
 221                 to+sizeof(struct icmphdr),
 222                 fraglen-sizeof(struct icmphdr), csum);
 223         icmph=(struct icmphdr *)to;
 224         icmph->checksum = csum_fold(csum);
 225 }
 226  
 227 /*
 228  *      Driving logic for building and sending ICMP messages.
 229  */
 230 
 231 static void icmp_build_xmit(struct icmp_bxm *icmp_param, __u32 saddr, __u32 daddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 232 {
 233         struct sock *sk=icmp_socket.data;
 234         icmp_param->icmph.checksum=0;
 235         icmp_param->csum=0;
 236         icmp_out_count(icmp_param->icmph.type);
 237         ip_build_xmit(sk, icmp_glue_bits, icmp_param, 
 238                 icmp_param->data_len+sizeof(struct icmphdr),
 239                 daddr, saddr, &icmp_param->replyopts, 0, IPPROTO_ICMP, 1);
 240 }
 241 
 242 
 243 /*
 244  *      Send an ICMP message in response to a situation
 245  *
 246  *      RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header. MAY send more (we don't).
 247  *                      MUST NOT change this header information.
 248  *                      MUST NOT reply to a multicast/broadcast IP address.
 249  *                      MUST NOT reply to a multicast/broadcast MAC address.
 250  *                      MUST reply to only the first fragment.
 251  */
 252 
 253 void icmp_send(struct sk_buff *skb_in, int type, int code, unsigned long info, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 254 {
 255         struct iphdr *iph;
 256         struct icmphdr *icmph;
 257         int atype;
 258         struct icmp_bxm icmp_param;
 259         __u32 saddr;
 260         
 261         /*
 262          *      Find the original header
 263          */
 264          
 265         iph = skb_in->ip_hdr;
 266         
 267         /*
 268          *      No replies to physical multicast/broadcast
 269          */
 270          
 271         if(skb_in->pkt_type!=PACKET_HOST)
 272                 return;
 273                 
 274         /*
 275          *      Now check at the protocol level
 276          */
 277          
 278         atype=ip_chk_addr(iph->daddr);
 279         if(atype==IS_BROADCAST||atype==IS_MULTICAST)
 280                 return;
 281                 
 282         /*
 283          *      Only reply to fragment 0. We byte re-order the constant
 284          *      mask for efficiency.
 285          */
 286          
 287         if(iph->frag_off&htons(IP_OFFSET))
 288                 return;
 289                 
 290         /* 
 291          *      If we send an ICMP error to an ICMP error a mess would result..
 292          */
 293          
 294         if(icmp_pointers[type].error)
 295         {
 296                 /*
 297                  *      We are an error, check if we are replying to an ICMP error
 298                  */
 299                  
 300                 if(iph->protocol==IPPROTO_ICMP)
 301                 {
 302                         icmph = (struct icmphdr *)((char *)iph + (iph->ihl<<2));
 303                         /*
 304                          *      Assume any unknown ICMP type is an error. This isn't
 305                          *      specified by the RFC, but think about it..
 306                          */
 307                         if(icmph->type>18 || icmp_pointers[icmph->type].error)
 308                                 return;
 309                 }
 310         }
 311         
 312         /*
 313          *      Tell our driver what to send
 314          */
 315          
 316         saddr=iph->daddr;
 317         if(saddr!=dev->pa_addr && ip_chk_addr(saddr)!=IS_MYADDR)
 318                 saddr=dev->pa_addr;
 319         
 320         icmp_param.icmph.type=type;
 321         icmp_param.icmph.code=code;
 322         icmp_param.icmph.un.gateway = info;
 323         icmp_param.data_ptr=iph;
 324         icmp_param.data_len=(iph->ihl<<2)+8;    /* RFC says return header + 8 bytes */
 325         
 326         /*
 327          *      Set it to build.
 328          */
 329 
 330         if (ip_options_echo(&icmp_param.replyopts, NULL, saddr, iph->saddr, skb_in) == 0)
 331           icmp_build_xmit(&icmp_param, saddr, iph->saddr);
 332 }
 333 
 334 
 335 /* 
 336  *      Handle ICMP_DEST_UNREACH, ICMP_TIME_EXCEED, and ICMP_QUENCH. 
 337  */
 338  
 339 static void icmp_unreach(struct icmphdr *icmph, struct sk_buff *skb, struct device *dev, __u32 saddr, __u32 daddr, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 340 {
 341         struct iphdr *iph;
 342         int hash;
 343         struct inet_protocol *ipprot;
 344         unsigned char *dp;      
 345         
 346         iph = (struct iphdr *) (icmph + 1);
 347         
 348         dp= ((unsigned char *)iph)+(iph->ihl<<2);
 349         
 350         if(icmph->type==ICMP_DEST_UNREACH)
 351         {
 352                 switch(icmph->code & 15)
 353                 {
 354                         case ICMP_NET_UNREACH:
 355                                 break;
 356                         case ICMP_HOST_UNREACH:
 357                                 break;
 358                         case ICMP_PROT_UNREACH:
 359                                 printk("ICMP: %s:%d: protocol unreachable.\n",
 360                                         in_ntoa(iph->daddr), ntohs(iph->protocol));
 361                                 break;
 362                         case ICMP_PORT_UNREACH:
 363                                 break;
 364                         case ICMP_FRAG_NEEDED:
 365                                 printk("ICMP: %s: fragmentation needed and DF set.\n",
 366                                                                 in_ntoa(iph->daddr));
 367                                 break;
 368                         case ICMP_SR_FAILED:
 369                                 printk("ICMP: %s: Source Route Failed.\n", in_ntoa(iph->daddr));
 370                                 break;
 371                         default:
 372                                 break;
 373                 }
 374                 if(icmph->code>12)      /* Invalid type */
 375                         return;
 376         }
 377         
 378         /*
 379          *      Throw it at our lower layers
 380          *
 381          *      RFC 1122: 3.2.2 MUST extract the protocol ID from the passed header.
 382          *      RFC 1122: 3.2.2.1 MUST pass ICMP unreach messages to the transport layer.
 383          *      RFC 1122: 3.2.2.2 MUST pass ICMP time expired messages to transport layer.
 384          */
 385 
 386         /*
 387          *      Get the protocol(s). 
 388          */
 389          
 390         hash = iph->protocol & (MAX_INET_PROTOS -1);
 391 
 392         /*
 393          *      This can't change while we are doing it. 
 394          *
 395          *      FIXME: Deliver to appropriate raw sockets too.
 396          */
 397          
 398         ipprot = (struct inet_protocol *) inet_protos[hash];
 399         while(ipprot != NULL) 
 400         {
 401                 struct inet_protocol *nextip;
 402 
 403                 nextip = (struct inet_protocol *) ipprot->next;
 404         
 405                 /* 
 406                  *      Pass it off to everyone who wants it. 
 407                  */
 408 
 409                 /* RFC1122: OK. Passes appropriate ICMP errors to the */
 410                 /* appropriate protocol layer (MUST), as per 3.2.2. */
 411 
 412                 if (iph->protocol == ipprot->protocol && ipprot->err_handler) 
 413                 {
 414                         ipprot->err_handler(icmph->type, icmph->code, dp,
 415                                             iph->daddr, iph->saddr, ipprot);
 416                 }
 417 
 418                 ipprot = nextip;
 419         }
 420         kfree_skb(skb, FREE_READ);
 421 }
 422 
 423 
 424 /*
 425  *      Handle ICMP_REDIRECT. 
 426  */
 427 
 428 static void icmp_redirect(struct icmphdr *icmph, struct sk_buff *skb, struct device *dev, __u32 source, __u32 daddr, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 429 {
 430 #ifndef CONFIG_IP_FORWARD
 431         struct rtable *rt;
 432 #endif
 433         struct iphdr *iph;
 434         unsigned long ip;
 435 
 436         /*
 437          *      Get the copied header of the packet that caused the redirect
 438          */
 439          
 440         iph = (struct iphdr *) (icmph + 1);
 441         ip = iph->daddr;
 442 
 443 #ifdef CONFIG_IP_FORWARD
 444         /*
 445          *      We are a router. Routers should not respond to ICMP_REDIRECT messages.
 446          */
 447         printk("icmp: ICMP redirect from %s on %s ignored.\n", in_ntoa(source), dev->name);
 448 #else   
 449         switch(icmph->code & 7) 
 450         {
 451                 case ICMP_REDIR_NET:
 452                         /*
 453                          *      This causes a problem with subnetted networks. What we should do
 454                          *      is use ICMP_ADDRESS to get the subnet mask of the problem route
 455                          *      and set both. But we don't..
 456                          */
 457 #ifdef not_a_good_idea
 458                         ip_rt_add((RTF_DYNAMIC | RTF_MODIFIED | RTF_GATEWAY),
 459                                 ip, 0, icmph->un.gateway, dev,0, 0, 0);
 460 #endif
 461                         /*
 462                          *      As per RFC recommendations now handle it as
 463                          *      a host redirect.
 464                          */
 465                          
 466                 case ICMP_REDIR_HOST:
 467                         /*
 468                          *      Add better route to host.
 469                          *      But first check that the redirect
 470                          *      comes from the old gateway..
 471                          *      And make sure it's an ok host address
 472                          *      (not some confused thing sending our
 473                          *      address)
 474                          */
 475                         rt = ip_rt_route(ip, NULL, NULL);
 476                         if (!rt)
 477                                 break;
 478                         if (rt->rt_gateway != source || 
 479                                 ((icmph->un.gateway^dev->pa_addr)&dev->pa_mask) ||
 480                                 ip_chk_addr(icmph->un.gateway))
 481                                 break;
 482                         printk("ICMP redirect from %s\n", in_ntoa(source));
 483                         ip_rt_add((RTF_DYNAMIC | RTF_MODIFIED | RTF_HOST | RTF_GATEWAY),
 484                                 ip, 0, icmph->un.gateway, dev,0, 0, 0, 0);
 485                         break;
 486                 case ICMP_REDIR_NETTOS:
 487                 case ICMP_REDIR_HOSTTOS:
 488                         printk("ICMP: cannot handle TOS redirects yet!\n");
 489                         break;
 490                 default:
 491                         break;
 492         }
 493 #endif          
 494         /*
 495          *      Discard the original packet
 496          */
 497          
 498         kfree_skb(skb, FREE_READ);
 499 }
 500 
 501 /*
 502  *      Handle ICMP_ECHO ("ping") requests. 
 503  *
 504  *      RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo requests.
 505  *      RFC 1122: 3.2.2.6 Data received in the ICMP_ECHO request MUST be included in the reply.
 506  *      See also WRT handling of options once they are done and working.
 507  */
 508  
 509 static void icmp_echo(struct icmphdr *icmph, struct sk_buff *skb, struct device *dev, __u32 saddr, __u32 daddr, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 510 {
 511         struct icmp_bxm icmp_param;
 512         icmp_param.icmph=*icmph;
 513         icmp_param.icmph.type=ICMP_ECHOREPLY;
 514         icmp_param.data_ptr=(icmph+1);
 515         icmp_param.data_len=len;
 516         if (ip_options_echo(&icmp_param.replyopts, NULL, daddr, saddr, skb)==0)
 517                 icmp_build_xmit(&icmp_param, daddr, saddr);
 518         kfree_skb(skb, FREE_READ);
 519 }
 520 
 521 /*
 522  *      Handle ICMP Timestamp requests. 
 523  *      RFC 1122: 3.2.2.8 MAY implement ICMP timestamp requests.
 524  *                SHOULD be in the kernel for minimum random latency.
 525  *                MUST be accurate to a few minutes.
 526  *                MUST be updated at least at 15Hz.
 527  */
 528  
 529 static void icmp_timestamp(struct icmphdr *icmph, struct sk_buff *skb, struct device *dev, __u32 saddr, __u32 daddr, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 530 {
 531         __u32 times[3];         /* So the new timestamp works on ALPHA's.. */
 532         struct icmp_bxm icmp_param;
 533         
 534         /*
 535          *      Too short.
 536          */
 537          
 538         if(len<12)
 539         {
 540                 icmp_statistics.IcmpInErrors++;
 541                 kfree_skb(skb, FREE_READ);
 542                 return;
 543         }
 544         
 545         /*
 546          *      Fill in the current time as ms since midnight UT: 
 547          */
 548          
 549         {
 550                 struct timeval tv;
 551                 do_gettimeofday(&tv);
 552                 times[1] = htonl((tv.tv_sec % 86400) * 1000 + tv.tv_usec / 1000);
 553         }
 554         times[2] = times[1];
 555         memcpy((void *)&times[0], icmph+1, 4);          /* Incoming stamp */
 556         icmp_param.icmph=*icmph;
 557         icmp_param.icmph.type=ICMP_TIMESTAMPREPLY;
 558         icmp_param.icmph.code=0;
 559         icmp_param.data_ptr=&times;
 560         icmp_param.data_len=12;
 561         if (ip_options_echo(&icmp_param.replyopts, NULL, daddr, saddr, skb)==0)
 562                 icmp_build_xmit(&icmp_param, daddr, saddr);
 563         kfree_skb(skb,FREE_READ);
 564 }
 565 
 566 
 567 /* 
 568  *      Handle ICMP_ADDRESS_MASK requests.  (RFC950)
 569  *
 570  * RFC1122 (3.2.2.9).  A host MUST only send replies to 
 571  * ADDRESS_MASK requests if it's been configured as an address mask 
 572  * agent.  Receiving a request doesn't constitute implicit permission to 
 573  * act as one. Of course, implementing this correctly requires (SHOULD) 
 574  * a way to turn the functionality on and off.  Another one for sysctl(), 
 575  * I guess. -- MS 
 576  * Botched with a CONFIG option for now - Linus add scts sysctl please.. 
 577  */
 578  
 579 static void icmp_address(struct icmphdr *icmph, struct sk_buff *skb, struct device *dev, __u32 saddr, __u32 daddr, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 580 {
 581 #ifdef CONFIG_IP_ADDR_AGENT
 582         __u32 answer;
 583         struct icmp_bxm icmp_param;
 584         icmp_param.icmph.type=ICMP_ADDRESSREPLY;
 585         icmp_param.icmph.code=0;
 586         icmp_param.icmph.un.echo.id = icmph->un.echo.id;
 587         icmp_param.icmph.un.echo.sequence = icmph->un.echo.sequence;
 588         icmp_param.data_ptr=&dev->pa_mask;
 589         icmp_param.data_len=4;
 590         if (ip_options_echo(&icmp_param.replyopts, NULL, daddr, saddr, skb)==0)
 591                 icmp_build_xmit(&icmp_param, daddr, saddr);
 592 #endif  
 593         kfree_skb(skb, FREE_READ);      
 594 }
 595 
 596 static void icmp_discard(struct icmphdr *icmph, struct sk_buff *skb, struct device *dev, __u32 saddr, __u32 daddr, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 597 {
 598         kfree_skb(skb, FREE_READ);
 599 }
 600 
 601 /* 
 602  *      Deal with incoming ICMP packets. 
 603  */
 604  
 605 int icmp_rcv(struct sk_buff *skb, struct device *dev, struct options *opt,
     /* [previous][next][first][last][top][bottom][index][help] */
 606          __u32 daddr, unsigned short len,
 607          __u32 saddr, int redo, struct inet_protocol *protocol)
 608 {
 609         struct icmphdr *icmph=(void *)skb->h.raw;
 610         icmp_statistics.IcmpInMsgs++;
 611         
 612         /*
 613          *      Validate the packet
 614          */
 615         
 616         if (ip_compute_csum((unsigned char *) icmph, len)) 
 617         {
 618                 /* Failed checksum! */
 619                 icmp_statistics.IcmpInErrors++;
 620                 printk("ICMP: failed checksum from %s!\n", in_ntoa(saddr));
 621                 kfree_skb(skb, FREE_READ);
 622                 return(0);
 623         }
 624         
 625         /*
 626          *      18 is the highest 'known' icmp type. Anything else is a mystery
 627          *
 628          *      RFC 1122: 3.2.2  Unknown ICMP messages types MUST be silently discarded.
 629          */
 630          
 631         if(icmph->type > 18)
 632         {
 633                 icmp_statistics.IcmpInErrors++;         /* Is this right - or do we ignore ? */
 634                 kfree_skb(skb,FREE_READ);
 635                 return(0);
 636         }
 637         
 638         /*
 639          *      Parse the ICMP message 
 640          */
 641 
 642         if (daddr!=dev->pa_addr && ip_chk_addr(daddr) != IS_MYADDR)
 643         {
 644                 /*
 645                  *      RFC 1122: 3.2.2.6 An ICMP_ECHO to broadcast MAY be silently ignored (we don't as it is used
 646                  *      by some network mapping tools).
 647                  *      RFC 1122: 3.2.2.8 An ICMP_TIMESTAMP MAY be silently discarded if to broadcast/multicast.
 648                  */
 649                 if (icmph->type != ICMP_ECHO) 
 650                 {
 651                         icmp_statistics.IcmpInErrors++;
 652                         kfree_skb(skb, FREE_READ);
 653                         return(0);
 654                 }
 655                 /*
 656                  *      Reply the multicast/broadcast using a legal
 657                  *      interface - in this case the device we got
 658                  *      it from.
 659                  */
 660                 daddr=dev->pa_addr;
 661         }
 662         
 663         len-=sizeof(struct icmphdr);
 664         (*icmp_pointers[icmph->type].input)++;
 665         (icmp_pointers[icmph->type].handler)(icmph,skb,skb->dev,saddr,daddr,len);
 666         return 0;
 667 }
 668 
 669 /*
 670  *      This table is the definition of how we handle ICMP.
 671  */
 672  
 673 static struct icmp_control icmp_pointers[19] = {
 674 /* ECHO REPLY (0) */
 675  { &icmp_statistics.IcmpOutEchoReps, &icmp_statistics.IcmpInEchoReps, icmp_discard, 0 },
 676  { &dummy, &icmp_statistics.IcmpInErrors, icmp_discard, 1 },
 677  { &dummy, &icmp_statistics.IcmpInErrors, icmp_discard, 1 },
 678 /* DEST UNREACH (3) */
 679  { &icmp_statistics.IcmpOutDestUnreachs, &icmp_statistics.IcmpInDestUnreachs, icmp_unreach, 1 },
 680 /* SOURCE QUENCH (4) */
 681  { &icmp_statistics.IcmpOutSrcQuenchs, &icmp_statistics.IcmpInSrcQuenchs, icmp_unreach, 1 },
 682 /* REDIRECT (5) */
 683  { &icmp_statistics.IcmpOutRedirects, &icmp_statistics.IcmpInRedirects, icmp_redirect, 1 },
 684  { &dummy, &icmp_statistics.IcmpInErrors, icmp_discard, 1 },
 685  { &dummy, &icmp_statistics.IcmpInErrors, icmp_discard, 1 },
 686 /* ECHO (8) */
 687  { &icmp_statistics.IcmpOutEchos, &icmp_statistics.IcmpInEchos, icmp_echo, 0 },
 688  { &dummy, &icmp_statistics.IcmpInErrors, icmp_discard, 1 },
 689  { &dummy, &icmp_statistics.IcmpInErrors, icmp_discard, 1 },
 690 /* TIME EXCEEDED (11) */
 691  { &icmp_statistics.IcmpOutTimeExcds, &icmp_statistics.IcmpInTimeExcds, icmp_unreach, 1 },
 692 /* PARAMETER PROBLEM (12) */
 693 /* FIXME: RFC1122 3.2.2.5 - MUST pass PARAM_PROB messages to transport layer */
 694  { &icmp_statistics.IcmpOutParmProbs, &icmp_statistics.IcmpInParmProbs, icmp_discard, 1 },
 695 /* TIMESTAMP (13) */
 696  { &icmp_statistics.IcmpOutTimestamps, &icmp_statistics.IcmpInTimestamps, icmp_timestamp, 0 },
 697 /* TIMESTAMP REPLY (14) */
 698  { &icmp_statistics.IcmpOutTimestampReps, &icmp_statistics.IcmpInTimestampReps, icmp_discard, 0 },
 699 /* INFO (15) */
 700  { &dummy, &dummy, icmp_discard, 0 },
 701 /* INFO REPLY (16) */
 702  { &dummy, &dummy, icmp_discard, 0 },
 703 /* ADDR MASK (17) */
 704  { &icmp_statistics.IcmpOutAddrMasks, &icmp_statistics.IcmpInAddrMasks, icmp_address, 0 },
 705 /* ADDR MASK REPLY (18) */
 706  { &icmp_statistics.IcmpOutAddrMaskReps, &icmp_statistics.IcmpInAddrMaskReps, icmp_discard, 0 }
 707 };
 708 
 709 void icmp_init(struct proto_ops *ops)
     /* [previous][next][first][last][top][bottom][index][help] */
 710 {
 711         struct sock *sk;
 712         int err;
 713         icmp_socket.type=SOCK_RAW;
 714         icmp_socket.ops=ops;
 715         if((err=ops->create(&icmp_socket, IPPROTO_ICMP))<0)
 716                 panic("Failed to create the ICMP control socket.\n");
 717         sk=icmp_socket.data;
 718         sk->allocation=GFP_ATOMIC;
 719         sk->num = 256;                  /* Don't receive any data */
 720 }
 721 

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