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