root/net/inet/icmp.c

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

DEFINITIONS

This source file includes following definitions.
  1. print_icmp
  2. icmp_send
  3. icmp_unreach
  4. icmp_redirect
  5. icmp_echo
  6. icmp_info
  7. icmp_address
  8. icmp_rcv
  9. icmp_ioctl

   1 /*
   2  * INET         An implementation of the TCP/IP protocol suite for the LINUX
   3  *              operating system.  INET is implemented using the  BSD Socket
   4  *              interface as the means of communication with the user level.
   5  *
   6  *              Internet Control Message Protocol (ICMP)
   7  *
   8  * Version:     @(#)icmp.c      1.0.11  06/02/93
   9  *
  10  * Authors:     Ross Biro, <bir7@leland.Stanford.Edu>
  11  *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12  *              Mark Evans, <evansmp@uhura.aston.ac.uk>
  13  *
  14  * Fixes:       
  15  *              Alan Cox        :       Generic queue usage.
  16  *              Gerhard Koerting:       ICMP addressing corrected
  17  *              Alan Cox        :       Use tos/ttl settings
  18  *
  19  *
  20  *              This program is free software; you can redistribute it and/or
  21  *              modify it under the terms of the GNU General Public License
  22  *              as published by the Free Software Foundation; either version
  23  *              2 of the License, or (at your option) any later version.
  24  */
  25 #include <linux/types.h>
  26 #include <linux/sched.h>
  27 #include <linux/kernel.h>
  28 #include <linux/fcntl.h>
  29 #include <linux/socket.h>
  30 #include <linux/in.h>
  31 #include "inet.h"
  32 #include "dev.h"
  33 #include "ip.h"
  34 #include "route.h"
  35 #include "protocol.h"
  36 #include "icmp.h"
  37 #include "tcp.h"
  38 #include "skbuff.h"
  39 #include "sock.h"
  40 #include <linux/errno.h>
  41 #include <linux/timer.h>
  42 #include <asm/system.h>
  43 #include <asm/segment.h>
  44 
  45 
  46 #define min(a,b)        ((a)<(b)?(a):(b))
  47 
  48 
  49 /* An array of errno for error messages from dest unreach. */
  50 struct icmp_err icmp_err_convert[] = {
  51   { ENETUNREACH,        1 },    /*      ICMP_NET_UNREACH        */
  52   { EHOSTUNREACH,       1 },    /*      ICMP_HOST_UNREACH       */
  53   { ENOPROTOOPT,        1 },    /*      ICMP_PROT_UNREACH       */
  54   { ECONNREFUSED,       1 },    /*      ICMP_PORT_UNREACH       */
  55   { EOPNOTSUPP,         0 },    /*      ICMP_FRAG_NEEDED        */
  56   { EOPNOTSUPP,         0 },    /*      ICMP_SR_FAILED          */
  57   { ENETUNREACH,        1 },    /*      ICMP_NET_UNKNOWN        */
  58   { EHOSTDOWN,          1 },    /*      ICMP_HOST_UNKNOWN       */
  59   { ENONET,             1 },    /*      ICMP_HOST_ISOLATED      */
  60   { ENETUNREACH,        1 },    /*      ICMP_NET_ANO            */
  61   { EHOSTUNREACH,       1 },    /*      ICMP_HOST_ANO           */
  62   { EOPNOTSUPP,         0 },    /*      ICMP_NET_UNR_TOS        */
  63   { EOPNOTSUPP,         0 }     /*      ICMP_HOST_UNR_TOS       */
  64 };
  65 
  66 
  67 /* Display the contents of an ICMP header. */
  68 static void
  69 print_icmp(struct icmphdr *icmph)
     /* [previous][next][first][last][top][bottom][index][help] */
  70 {
  71   if (inet_debug != DBG_ICMP) return;
  72 
  73   printk("ICMP: type = %d, code = %d, checksum = %X\n",
  74                         icmph->type, icmph->code, icmph->checksum);
  75   printk("      gateway = %s\n", in_ntoa(icmph->un.gateway));
  76 }
  77 
  78 
  79 /* Send an ICMP message. */
  80 void
  81 icmp_send(struct sk_buff *skb_in, int type, int code, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
  82 {
  83   struct sk_buff *skb;
  84   struct iphdr *iph;
  85   int offset;
  86   struct icmphdr *icmph;
  87   int len;
  88 
  89   DPRINTF((DBG_ICMP, "icmp_send(skb_in = %X, type = %d, code = %d, dev=%X)\n",
  90                                                 skb_in, type, code, dev));
  91 
  92   /* Get some memory for the reply. */
  93   len = sizeof(struct sk_buff) + dev->hard_header_len +
  94         sizeof(struct iphdr) + sizeof(struct icmphdr) +
  95         sizeof(struct iphdr) + 8;       /* amount of header to return */
  96            
  97   skb = (struct sk_buff *) alloc_skb(len, GFP_ATOMIC);
  98   if (skb == NULL) 
  99         return;
 100 
 101   skb->sk = NULL;
 102   skb->mem_addr = skb;
 103   skb->mem_len = len;
 104   len -= sizeof(struct sk_buff);
 105 
 106   /* Find the IP header. */
 107   iph = (struct iphdr *) (skb_in->data + dev->hard_header_len);
 108 
 109   /* Build Layer 2-3 headers for message back to source. */
 110   offset = ip_build_header(skb, dev->pa_addr, iph->saddr,
 111                            &dev, IPPROTO_ICMP, NULL, len, skb_in->ip_hdr->tos,255);
 112   if (offset < 0) {
 113         skb->sk = NULL;
 114         kfree_skb(skb, FREE_READ);
 115         return;
 116   }
 117 
 118   /* Re-adjust length according to actual IP header size. */
 119   skb->len = offset + sizeof(struct icmphdr) + sizeof(struct iphdr) + 8;
 120   icmph = (struct icmphdr *) (skb->data + offset);
 121   icmph->type = type;
 122   icmph->code = code;
 123   icmph->checksum = 0;
 124   icmph->un.gateway = 0;
 125   memcpy(icmph + 1, iph, sizeof(struct iphdr) + 8);
 126 
 127   icmph->checksum = ip_compute_csum((unsigned char *)icmph,
 128                          sizeof(struct icmphdr) + sizeof(struct iphdr) + 8);
 129 
 130   DPRINTF((DBG_ICMP, ">>\n"));
 131   print_icmp(icmph);
 132 
 133   /* Send it and free it. */
 134   ip_queue_xmit(NULL, dev, skb, 1);
 135 }
 136 
 137 
 138 /* Handle ICMP_UNREACH and ICMP_QUENCH. */
 139 static void
 140 icmp_unreach(struct icmphdr *icmph, struct sk_buff *skb)
     /* [previous][next][first][last][top][bottom][index][help] */
 141 {
 142   struct inet_protocol *ipprot;
 143   struct iphdr *iph;
 144   unsigned char hash;
 145   int err;
 146 
 147   err = (icmph->type << 8) | icmph->code;
 148   iph = (struct iphdr *) (icmph + 1);
 149   switch(icmph->code & 7) {
 150         case ICMP_NET_UNREACH:
 151                 DPRINTF((DBG_ICMP, "ICMP: %s: network unreachable.\n",
 152                                                         in_ntoa(iph->daddr)));
 153                 break;
 154         case ICMP_HOST_UNREACH:
 155                 DPRINTF((DBG_ICMP, "ICMP: %s: host unreachable.\n",
 156                                                 in_ntoa(iph->daddr)));
 157                 break;
 158         case ICMP_PROT_UNREACH:
 159                 printk("ICMP: %s:%d: protocol unreachable.\n",
 160                         in_ntoa(iph->daddr), ntohs(iph->protocol));
 161                 break;
 162         case ICMP_PORT_UNREACH:
 163                 DPRINTF((DBG_ICMP, "ICMP: %s:%d: port unreachable.\n",
 164                         in_ntoa(iph->daddr), -1 /* FIXME: ntohs(iph->port) */));
 165                 break;
 166         case ICMP_FRAG_NEEDED:
 167                 printk("ICMP: %s: fragmentation needed and DF set.\n",
 168                                                         in_ntoa(iph->daddr));
 169                 break;
 170         case ICMP_SR_FAILED:
 171                 printk("ICMP: %s: Source Route Failed.\n", in_ntoa(iph->daddr));
 172                 break;
 173         default:
 174                 DPRINTF((DBG_ICMP, "ICMP: Unreachable: CODE=%d from %s\n",
 175                                 (icmph->code & 7), in_ntoa(iph->daddr)));
 176                 break;
 177   }
 178 
 179   /* Get the protocol(s). */
 180   hash = iph->protocol & (MAX_INET_PROTOS -1);
 181 
 182   /* This can change while we are doing it. */
 183   ipprot = (struct inet_protocol *) inet_protos[hash];
 184   while(ipprot != NULL) {
 185         struct inet_protocol *nextip;
 186 
 187         nextip = (struct inet_protocol *) ipprot->next;
 188 
 189         /* Pass it off to everyone who wants it. */
 190         if (iph->protocol == ipprot->protocol && ipprot->err_handler) {
 191                 ipprot->err_handler(err, (unsigned char *)(icmph + 1),
 192                                     iph->daddr, iph->saddr, ipprot);
 193         }
 194 
 195         ipprot = nextip;
 196   }
 197   skb->sk = NULL;
 198   kfree_skb(skb, FREE_READ);
 199 }
 200 
 201 
 202 /* Handle ICMP_REDIRECT. */
 203 static void
 204 icmp_redirect(struct icmphdr *icmph, struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 205 {
 206   struct iphdr *iph;
 207   unsigned long ip;
 208 
 209   iph = (struct iphdr *) (icmph + 1);
 210   ip = iph->daddr;
 211   switch(icmph->code & 7) {
 212         case ICMP_REDIR_NET:
 213                 rt_add((RTF_DYNAMIC | RTF_MODIFIED | RTF_GATEWAY),
 214                         ip, 0, icmph->un.gateway, dev);
 215                 break;
 216         case ICMP_REDIR_HOST:
 217                 rt_add((RTF_DYNAMIC | RTF_MODIFIED | RTF_HOST | RTF_GATEWAY),
 218                         ip, 0, icmph->un.gateway, dev);
 219                 break;
 220         case ICMP_REDIR_NETTOS:
 221         case ICMP_REDIR_HOSTTOS:
 222                 printk("ICMP: cannot handle TOS redirects yet!\n");
 223                 break;
 224         default:
 225                 DPRINTF((DBG_ICMP, "ICMP: Unreach: CODE=%d\n",
 226                                                 (icmph->code & 7)));
 227                 break;
 228   }
 229   skb->sk = NULL;
 230   kfree_skb(skb, FREE_READ);
 231 }
 232 
 233 
 234 /* Handle ICMP_ECHO ("ping") requests. */
 235 static void
 236 icmp_echo(struct icmphdr *icmph, struct sk_buff *skb, struct device *dev,
     /* [previous][next][first][last][top][bottom][index][help] */
 237           unsigned long saddr, unsigned long daddr, int len,
 238           struct options *opt)
 239 {
 240   struct icmphdr *icmphr;
 241   struct sk_buff *skb2;
 242   int size, offset;
 243 
 244   size = sizeof(struct sk_buff) + dev->hard_header_len + 64 + len;
 245   skb2 = alloc_skb(size, GFP_ATOMIC);
 246   if (skb2 == NULL) {
 247         skb->sk = NULL;
 248         kfree_skb(skb, FREE_READ);
 249         return;
 250   }
 251   skb2->sk = NULL;
 252   skb2->mem_addr = skb2;
 253   skb2->mem_len = size;
 254   skb2->free = 1;
 255 
 256   /* Build Layer 2-3 headers for message back to source */
 257   offset = ip_build_header(skb2, daddr, saddr, &dev,
 258                                 IPPROTO_ICMP, opt, len, skb->ip_hdr->tos,255);
 259   if (offset < 0) {
 260         printk("ICMP: Could not build IP Header for ICMP ECHO Response\n");
 261         kfree_skb(skb2,FREE_WRITE);
 262         skb->sk = NULL;
 263         kfree_skb(skb, FREE_READ);
 264         return;
 265   }
 266 
 267   /* Re-adjust length according to actual IP header size. */
 268   skb2->len = offset + len;
 269 
 270   /* Build ICMP_ECHO Response message. */
 271   icmphr = (struct icmphdr *) (skb2->data + offset);
 272   memcpy((char *) icmphr, (char *) icmph, len);
 273   icmphr->type = ICMP_ECHOREPLY;
 274   icmphr->code = 0;
 275   icmphr->checksum = 0;
 276   icmphr->checksum = ip_compute_csum((unsigned char *)icmphr, len);
 277 
 278   /* Ship it out - free it when done */
 279   ip_queue_xmit((struct sock *)NULL, dev, skb2, 1);
 280 
 281   skb->sk = NULL;
 282   kfree_skb(skb, FREE_READ);
 283 }
 284 
 285 
 286 /* Handle the ICMP INFORMATION REQUEST. */
 287 static void
 288 icmp_info(struct icmphdr *icmph, struct sk_buff *skb, struct device *dev,
     /* [previous][next][first][last][top][bottom][index][help] */
 289           unsigned long saddr, unsigned long daddr, int len,
 290           struct options *opt)
 291 {
 292   /* NOT YET */
 293   skb->sk = NULL;
 294   kfree_skb(skb, FREE_READ);
 295 }
 296 
 297 
 298 /* Handle ICMP_ADRESS_MASK requests. */
 299 static void
 300 icmp_address(struct icmphdr *icmph, struct sk_buff *skb, struct device *dev,
     /* [previous][next][first][last][top][bottom][index][help] */
 301           unsigned long saddr, unsigned long daddr, int len,
 302           struct options *opt)
 303 {
 304   struct icmphdr *icmphr;
 305   struct sk_buff *skb2;
 306   int size, offset;
 307 
 308   size = sizeof(struct sk_buff) + dev->hard_header_len + 64 + len;
 309   skb2 = alloc_skb(size, GFP_ATOMIC);
 310   if (skb2 == NULL) {
 311         skb->sk = NULL;
 312         kfree_skb(skb, FREE_READ);
 313         return;
 314   }
 315   skb2->sk = NULL;
 316   skb2->mem_addr = skb2;
 317   skb2->mem_len = size;
 318   skb2->free = 1;
 319 
 320   /* Build Layer 2-3 headers for message back to source */
 321   offset = ip_build_header(skb2, daddr, saddr, &dev,
 322                                 IPPROTO_ICMP, opt, len, skb->ip_hdr->tos,255);
 323   if (offset < 0) {
 324         printk("ICMP: Could not build IP Header for ICMP ADDRESS Response\n");
 325         kfree_skb(skb2,FREE_WRITE);
 326         skb->sk = NULL;
 327         kfree_skb(skb, FREE_READ);
 328         return;
 329   }
 330 
 331   /* Re-adjust length according to actual IP header size. */
 332   skb2->len = offset + len;
 333 
 334   /* Build ICMP ADDRESS MASK Response message. */
 335   icmphr = (struct icmphdr *) (skb2->data + offset);
 336   icmphr->type = ICMP_ADDRESSREPLY;
 337   icmphr->code = 0;
 338   icmphr->checksum = 0;
 339   icmphr->un.echo.id = icmph->un.echo.id;
 340   icmphr->un.echo.sequence = icmph->un.echo.sequence;
 341   memcpy((char *) (icmphr + 1), (char *) &dev->pa_mask, sizeof(dev->pa_mask));
 342 
 343   icmphr->checksum = ip_compute_csum((unsigned char *)icmphr, len);
 344 
 345   /* Ship it out - free it when done */
 346   ip_queue_xmit((struct sock *)NULL, dev, skb2, 1);
 347 
 348   skb->sk = NULL;
 349   kfree_skb(skb, FREE_READ);
 350 }
 351 
 352 
 353 /* Deal with incoming ICMP packets. */
 354 int
 355 icmp_rcv(struct sk_buff *skb1, struct device *dev, struct options *opt,
     /* [previous][next][first][last][top][bottom][index][help] */
 356          unsigned long daddr, unsigned short len,
 357          unsigned long saddr, int redo, struct inet_protocol *protocol)
 358 {
 359   struct icmphdr *icmph;
 360   unsigned char *buff;
 361 
 362   /* Drop broadcast packets. */
 363   if (chk_addr(daddr) == IS_BROADCAST) {
 364         DPRINTF((DBG_ICMP, "ICMP: Discarded broadcast from %s\n",
 365                                                         in_ntoa(saddr)));
 366         skb1->sk = NULL;
 367         kfree_skb(skb1, FREE_READ);
 368         return(0);
 369   }
 370 
 371   buff = skb1->h.raw;
 372   icmph = (struct icmphdr *) buff;
 373 
 374   /* Validate the packet first */
 375   if (ip_compute_csum((unsigned char *) icmph, len)) {
 376         /* Failed checksum! */
 377         printk("ICMP: failed checksum from %s!\n", in_ntoa(saddr));
 378         skb1->sk = NULL;
 379         kfree_skb(skb1, FREE_READ);
 380         return(0);
 381   }
 382   print_icmp(icmph);
 383 
 384   /* Parse the ICMP message */
 385   switch(icmph->type) {
 386         case ICMP_TIME_EXCEEDED:
 387         case ICMP_DEST_UNREACH:
 388         case ICMP_SOURCE_QUENCH:
 389                 icmp_unreach(icmph, skb1);
 390                 return(0);
 391         case ICMP_REDIRECT:
 392                 icmp_redirect(icmph, skb1, dev);
 393                 return(0);
 394         case ICMP_ECHO: 
 395                 icmp_echo(icmph, skb1, dev, saddr, daddr, len, opt);
 396                 return 0;
 397         case ICMP_ECHOREPLY:
 398                 skb1->sk = NULL;
 399                 kfree_skb(skb1, FREE_READ);
 400                 return(0);
 401         case ICMP_INFO_REQUEST:
 402                 icmp_info(icmph, skb1, dev, saddr, daddr, len, opt);
 403                 return 0;
 404         case ICMP_INFO_REPLY:
 405                 skb1->sk = NULL;
 406                 kfree_skb(skb1, FREE_READ);
 407                 return(0);
 408         case ICMP_ADDRESS:
 409                 icmp_address(icmph, skb1, dev, saddr, daddr, len, opt);
 410                 return 0;
 411         case ICMP_ADDRESSREPLY:
 412                 skb1->sk = NULL;
 413                 kfree_skb(skb1, FREE_READ);
 414                 return(0);
 415         default:
 416                 DPRINTF((DBG_ICMP,
 417                         "ICMP: Unsupported ICMP from %s, type = 0x%X\n",
 418                                                 in_ntoa(saddr), icmph->type));
 419                 skb1->sk = NULL;
 420                 kfree_skb(skb1, FREE_READ);
 421                 return(0);
 422   }
 423   /*NOTREACHED*/
 424   skb1->sk = NULL;
 425   kfree_skb(skb1, FREE_READ);
 426   return(-1);
 427 }
 428 
 429 
 430 /* Perform any ICMP-related I/O control requests. */
 431 int
 432 icmp_ioctl(struct sock *sk, int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 433 {
 434   switch(cmd) {
 435         case DDIOCSDBG:
 436                 return(dbg_ioctl((void *) arg, DBG_ICMP));
 437         default:
 438                 return(-EINVAL);
 439   }
 440   return(0);
 441 }

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