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

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