Back to home page

Quest Cross Reference

 
 

    


Warning, cross-references for /kernel/lwip/core/ipv4/icmp.c need to be fixed.

0001 /**
0002  * @file
0003  * ICMP - Internet Control Message Protocol
0004  *
0005  */
0006 
0007 /*
0008  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
0009  * All rights reserved.
0010  *
0011  * Redistribution and use in source and binary forms, with or without modification,
0012  * are permitted provided that the following conditions are met:
0013  *
0014  * 1. Redistributions of source code must retain the above copyright notice,
0015  *    this list of conditions and the following disclaimer.
0016  * 2. Redistributions in binary form must reproduce the above copyright notice,
0017  *    this list of conditions and the following disclaimer in the documentation
0018  *    and/or other materials provided with the distribution.
0019  * 3. The name of the author may not be used to endorse or promote products
0020  *    derived from this software without specific prior written permission.
0021  *
0022  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
0023  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
0024  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
0025  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0026  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
0027  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0028  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0029  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
0030  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
0031  * OF SUCH DAMAGE.
0032  *
0033  * This file is part of the lwIP TCP/IP stack.
0034  *
0035  * Author: Adam Dunkels <adam@sics.se>
0036  *
0037  */
0038 
0039 /* Some ICMP messages should be passed to the transport protocols. This
0040    is not implemented. */
0041 
0042 #include "lwip/opt.h"
0043 
0044 #if LWIP_ICMP /* don't build if not configured for use in lwipopts.h */
0045 
0046 #include "lwip/icmp.h"
0047 #include "lwip/inet.h"
0048 #include "lwip/inet_chksum.h"
0049 #include "lwip/ip.h"
0050 #include "lwip/def.h"
0051 #include "lwip/stats.h"
0052 #include "lwip/snmp.h"
0053 
0054 #include <string.h>
0055 
0056 /** Small optimization: set to 0 if incoming PBUF_POOL pbuf always can be
0057  * used to modify and send a response packet (and to 1 if this is not the case,
0058  * e.g. when link header is stripped of when receiving) */
0059 #ifndef LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
0060 #define LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN 1
0061 #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
0062 
0063 /* The amount of data from the original packet to return in a dest-unreachable */
0064 #define ICMP_DEST_UNREACH_DATASIZE 8
0065 
0066 static void icmp_send_response(struct pbuf *p, u8_t type, u8_t code);
0067 
0068 /**
0069  * Processes ICMP input packets, called from ip_input().
0070  *
0071  * Currently only processes icmp echo requests and sends
0072  * out the echo response.
0073  *
0074  * @param p the icmp echo request packet, p->payload pointing to the ip header
0075  * @param inp the netif on which this packet was received
0076  */
0077 void
0078 icmp_input(struct pbuf *p, struct netif *inp)
0079 {
0080   u8_t type;
0081 #ifdef LWIP_DEBUG
0082   u8_t code;
0083 #endif /* LWIP_DEBUG */
0084   struct icmp_echo_hdr *iecho;
0085   struct ip_hdr *iphdr;
0086   struct ip_addr tmpaddr;
0087   s16_t hlen;
0088 
0089   ICMP_STATS_INC(icmp.recv);
0090   snmp_inc_icmpinmsgs();
0091 
0092 
0093   iphdr = p->payload;
0094   hlen = IPH_HL(iphdr) * 4;
0095   if (pbuf_header(p, -hlen) || (p->tot_len < sizeof(u16_t)*2)) {
0096     LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short ICMP (%"U16_F" bytes) received\n", p->tot_len));
0097     goto lenerr;
0098   }
0099 
0100   type = *((u8_t *)p->payload);
0101 #ifdef LWIP_DEBUG
0102   code = *(((u8_t *)p->payload)+1);
0103 #endif /* LWIP_DEBUG */
0104   switch (type) {
0105   case ICMP_ECHO:
0106 #if !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING
0107     {
0108       int accepted = 1;
0109 #if !LWIP_MULTICAST_PING
0110       /* multicast destination address? */
0111       if (ip_addr_ismulticast(&iphdr->dest)) {
0112         accepted = 0;
0113       }
0114 #endif /* LWIP_MULTICAST_PING */
0115 #if !LWIP_BROADCAST_PING
0116       /* broadcast destination address? */
0117       if (ip_addr_isbroadcast(&iphdr->dest, inp)) {
0118         accepted = 0;
0119       }
0120 #endif /* LWIP_BROADCAST_PING */
0121       /* broadcast or multicast destination address not acceptd? */
0122       if (!accepted) {
0123         LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to multicast or broadcast pings\n"));
0124         ICMP_STATS_INC(icmp.err);
0125         pbuf_free(p);
0126         return;
0127       }
0128     }
0129 #endif /* !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING */
0130     LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n"));
0131     if (p->tot_len < sizeof(struct icmp_echo_hdr)) {
0132       LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n"));
0133       goto lenerr;
0134     }
0135     if (inet_chksum_pbuf(p) != 0) {
0136       LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo\n"));
0137       pbuf_free(p);
0138       ICMP_STATS_INC(icmp.chkerr);
0139       snmp_inc_icmpinerrors();
0140       return;
0141     }
0142 #if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
0143     if (pbuf_header(p, (PBUF_IP_HLEN + PBUF_LINK_HLEN))) {
0144       /* p is not big enough to contain link headers
0145        * allocate a new one and copy p into it
0146        */
0147       struct pbuf *r;
0148       /* switch p->payload to ip header */
0149       if (pbuf_header(p, hlen)) {
0150         LWIP_ASSERT("icmp_input: moving p->payload to ip header failed\n", 0);
0151         goto memerr;
0152       }
0153       /* allocate new packet buffer with space for link headers */
0154       r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
0155       if (r == NULL) {
0156         LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: allocating new pbuf failed\n"));
0157         goto memerr;
0158       }
0159       LWIP_ASSERT("check that first pbuf can hold struct the ICMP header",
0160                   (r->len >= hlen + sizeof(struct icmp_echo_hdr)));
0161       /* copy the whole packet including ip header */
0162       if (pbuf_copy(r, p) != ERR_OK) {
0163         LWIP_ASSERT("icmp_input: copying to new pbuf failed\n", 0);
0164         goto memerr;
0165       }
0166       iphdr = r->payload;
0167       /* switch r->payload back to icmp header */
0168       if (pbuf_header(r, -hlen)) {
0169         LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0);
0170         goto memerr;
0171       }
0172       /* free the original p */
0173       pbuf_free(p);
0174       /* we now have an identical copy of p that has room for link headers */
0175       p = r;
0176     } else {
0177       /* restore p->payload to point to icmp header */
0178       if (pbuf_header(p, -(s16_t)(PBUF_IP_HLEN + PBUF_LINK_HLEN))) {
0179         LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0);
0180         goto memerr;
0181       }
0182     }
0183 #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
0184     /* At this point, all checks are OK. */
0185     /* We generate an answer by switching the dest and src ip addresses,
0186      * setting the icmp type to ECHO_RESPONSE and updating the checksum. */
0187     iecho = p->payload;
0188     tmpaddr.addr = iphdr->src.addr;
0189     iphdr->src.addr = iphdr->dest.addr;
0190     iphdr->dest.addr = tmpaddr.addr;
0191     ICMPH_TYPE_SET(iecho, ICMP_ER);
0192     /* adjust the checksum */
0193     if (iecho->chksum >= htons(0xffff - (ICMP_ECHO << 8))) {
0194       iecho->chksum += htons(ICMP_ECHO << 8) + 1;
0195     } else {
0196       iecho->chksum += htons(ICMP_ECHO << 8);
0197     }
0198 
0199     /* Set the correct TTL and recalculate the header checksum. */
0200     IPH_TTL_SET(iphdr, ICMP_TTL);
0201     IPH_CHKSUM_SET(iphdr, 0);
0202 #if CHECKSUM_GEN_IP
0203     IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
0204 #endif /* CHECKSUM_GEN_IP */
0205 
0206     ICMP_STATS_INC(icmp.xmit);
0207     /* increase number of messages attempted to send */
0208     snmp_inc_icmpoutmsgs();
0209     /* increase number of echo replies attempted to send */
0210     snmp_inc_icmpoutechoreps();
0211 
0212     if(pbuf_header(p, hlen)) {
0213       LWIP_ASSERT("Can't move over header in packet", 0);
0214     } else {
0215       err_t ret;
0216       ret = ip_output_if(p, &(iphdr->src), IP_HDRINCL,
0217                    ICMP_TTL, 0, IP_PROTO_ICMP, inp);
0218       if (ret != ERR_OK) {
0219         LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ip_output_if returned an error: %c.\n", ret));
0220       }
0221     }
0222     break;
0223   default:
0224     LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" code %"S16_F" not supported.\n", 
0225                 (s16_t)type, (s16_t)code));
0226     ICMP_STATS_INC(icmp.proterr);
0227     ICMP_STATS_INC(icmp.drop);
0228   }
0229   pbuf_free(p);
0230   return;
0231 lenerr:
0232   pbuf_free(p);
0233   ICMP_STATS_INC(icmp.lenerr);
0234   snmp_inc_icmpinerrors();
0235   return;
0236 #if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
0237 memerr:
0238   pbuf_free(p);
0239   ICMP_STATS_INC(icmp.err);
0240   snmp_inc_icmpinerrors();
0241   return;
0242 #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
0243 }
0244 
0245 /**
0246  * Send an icmp 'destination unreachable' packet, called from ip_input() if
0247  * the transport layer protocol is unknown and from udp_input() if the local
0248  * port is not bound.
0249  *
0250  * @param p the input packet for which the 'unreachable' should be sent,
0251  *          p->payload pointing to the IP header
0252  * @param t type of the 'unreachable' packet
0253  */
0254 void
0255 icmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t)
0256 {
0257   icmp_send_response(p, ICMP_DUR, t);
0258 }
0259 
0260 #if IP_FORWARD || IP_REASSEMBLY
0261 /**
0262  * Send a 'time exceeded' packet, called from ip_forward() if TTL is 0.
0263  *
0264  * @param p the input packet for which the 'time exceeded' should be sent,
0265  *          p->payload pointing to the IP header
0266  * @param t type of the 'time exceeded' packet
0267  */
0268 void
0269 icmp_time_exceeded(struct pbuf *p, enum icmp_te_type t)
0270 {
0271   icmp_send_response(p, ICMP_TE, t);
0272 }
0273 
0274 #endif /* IP_FORWARD || IP_REASSEMBLY */
0275 
0276 /**
0277  * Send an icmp packet in response to an incoming packet.
0278  *
0279  * @param p the input packet for which the 'unreachable' should be sent,
0280  *          p->payload pointing to the IP header
0281  * @param type Type of the ICMP header
0282  * @param code Code of the ICMP header
0283  */
0284 static void
0285 icmp_send_response(struct pbuf *p, u8_t type, u8_t code)
0286 {
0287   struct pbuf *q;
0288   struct ip_hdr *iphdr;
0289   /* we can use the echo header here */
0290   struct icmp_echo_hdr *icmphdr;
0291 
0292   /* ICMP header + IP header + 8 bytes of data */
0293   q = pbuf_alloc(PBUF_IP, sizeof(struct icmp_echo_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE,
0294                  PBUF_RAM);
0295   if (q == NULL) {
0296     LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded: failed to allocate pbuf for ICMP packet.\n"));
0297     return;
0298   }
0299   LWIP_ASSERT("check that first pbuf can hold icmp message",
0300              (q->len >= (sizeof(struct icmp_echo_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE)));
0301 
0302   iphdr = p->payload;
0303   LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded from "));
0304   ip_addr_debug_print(ICMP_DEBUG, &(iphdr->src));
0305   LWIP_DEBUGF(ICMP_DEBUG, (" to "));
0306   ip_addr_debug_print(ICMP_DEBUG, &(iphdr->dest));
0307   LWIP_DEBUGF(ICMP_DEBUG, ("\n"));
0308 
0309   icmphdr = q->payload;
0310   icmphdr->type = type;
0311   icmphdr->code = code;
0312   icmphdr->id = 0;
0313   icmphdr->seqno = 0;
0314 
0315   /* copy fields from original packet */
0316   SMEMCPY((u8_t *)q->payload + sizeof(struct icmp_echo_hdr), (u8_t *)p->payload,
0317           IP_HLEN + ICMP_DEST_UNREACH_DATASIZE);
0318 
0319   /* calculate checksum */
0320   icmphdr->chksum = 0;
0321   icmphdr->chksum = inet_chksum(icmphdr, q->len);
0322   ICMP_STATS_INC(icmp.xmit);
0323   /* increase number of messages attempted to send */
0324   snmp_inc_icmpoutmsgs();
0325   /* increase number of destination unreachable messages attempted to send */
0326   snmp_inc_icmpouttimeexcds();
0327   ip_output(q, NULL, &(iphdr->src), ICMP_TTL, 0, IP_PROTO_ICMP);
0328   pbuf_free(q);
0329 }
0330 
0331 #endif /* LWIP_ICMP */