Port of LwIP performed by Ralf in 2010. Not recommended for use with recent mbed libraries, but good demos of raw LwIP possible

Dependents:   LwIP_raw_API_serverExample tiny-dtls

Committer:
RodColeman
Date:
Tue Sep 18 14:41:24 2012 +0000
Revision:
0:0791c1fece8e
[mbed] converted /Eth_TCP_Wei_Server/lwip

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RodColeman 0:0791c1fece8e 1 /**
RodColeman 0:0791c1fece8e 2 * @file
RodColeman 0:0791c1fece8e 3 * ICMP - Internet Control Message Protocol
RodColeman 0:0791c1fece8e 4 *
RodColeman 0:0791c1fece8e 5 */
RodColeman 0:0791c1fece8e 6
RodColeman 0:0791c1fece8e 7 /*
RodColeman 0:0791c1fece8e 8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
RodColeman 0:0791c1fece8e 9 * All rights reserved.
RodColeman 0:0791c1fece8e 10 *
RodColeman 0:0791c1fece8e 11 * Redistribution and use in source and binary forms, with or without modification,
RodColeman 0:0791c1fece8e 12 * are permitted provided that the following conditions are met:
RodColeman 0:0791c1fece8e 13 *
RodColeman 0:0791c1fece8e 14 * 1. Redistributions of source code must retain the above copyright notice,
RodColeman 0:0791c1fece8e 15 * this list of conditions and the following disclaimer.
RodColeman 0:0791c1fece8e 16 * 2. Redistributions in binary form must reproduce the above copyright notice,
RodColeman 0:0791c1fece8e 17 * this list of conditions and the following disclaimer in the documentation
RodColeman 0:0791c1fece8e 18 * and/or other materials provided with the distribution.
RodColeman 0:0791c1fece8e 19 * 3. The name of the author may not be used to endorse or promote products
RodColeman 0:0791c1fece8e 20 * derived from this software without specific prior written permission.
RodColeman 0:0791c1fece8e 21 *
RodColeman 0:0791c1fece8e 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
RodColeman 0:0791c1fece8e 23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
RodColeman 0:0791c1fece8e 24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
RodColeman 0:0791c1fece8e 25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
RodColeman 0:0791c1fece8e 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
RodColeman 0:0791c1fece8e 27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
RodColeman 0:0791c1fece8e 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
RodColeman 0:0791c1fece8e 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
RodColeman 0:0791c1fece8e 30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
RodColeman 0:0791c1fece8e 31 * OF SUCH DAMAGE.
RodColeman 0:0791c1fece8e 32 *
RodColeman 0:0791c1fece8e 33 * This file is part of the lwIP TCP/IP stack.
RodColeman 0:0791c1fece8e 34 *
RodColeman 0:0791c1fece8e 35 * Author: Adam Dunkels <adam@sics.se>
RodColeman 0:0791c1fece8e 36 *
RodColeman 0:0791c1fece8e 37 */
RodColeman 0:0791c1fece8e 38
RodColeman 0:0791c1fece8e 39 /* Some ICMP messages should be passed to the transport protocols. This
RodColeman 0:0791c1fece8e 40 is not implemented. */
RodColeman 0:0791c1fece8e 41
RodColeman 0:0791c1fece8e 42 #include "lwip/opt.h"
RodColeman 0:0791c1fece8e 43
RodColeman 0:0791c1fece8e 44 #if LWIP_ICMP /* don't build if not configured for use in lwipopts.h */
RodColeman 0:0791c1fece8e 45
RodColeman 0:0791c1fece8e 46 #include "lwip/icmp.h"
RodColeman 0:0791c1fece8e 47 #include "lwip/inet.h"
RodColeman 0:0791c1fece8e 48 #include "lwip/inet_chksum.h"
RodColeman 0:0791c1fece8e 49 #include "lwip/ip.h"
RodColeman 0:0791c1fece8e 50 #include "lwip/def.h"
RodColeman 0:0791c1fece8e 51 #include "lwip/stats.h"
RodColeman 0:0791c1fece8e 52 #include "lwip/snmp.h"
RodColeman 0:0791c1fece8e 53
RodColeman 0:0791c1fece8e 54 #include <string.h>
RodColeman 0:0791c1fece8e 55
RodColeman 0:0791c1fece8e 56 /** Small optimization: set to 0 if incoming PBUF_POOL pbuf always can be
RodColeman 0:0791c1fece8e 57 * used to modify and send a response packet (and to 1 if this is not the case,
RodColeman 0:0791c1fece8e 58 * e.g. when link header is stripped of when receiving) */
RodColeman 0:0791c1fece8e 59 #ifndef LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
RodColeman 0:0791c1fece8e 60 #define LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN 1
RodColeman 0:0791c1fece8e 61 #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
RodColeman 0:0791c1fece8e 62
RodColeman 0:0791c1fece8e 63 /* The amount of data from the original packet to return in a dest-unreachable */
RodColeman 0:0791c1fece8e 64 #define ICMP_DEST_UNREACH_DATASIZE 8
RodColeman 0:0791c1fece8e 65
RodColeman 0:0791c1fece8e 66 static void icmp_send_response(struct pbuf *p, u8_t type, u8_t code);
RodColeman 0:0791c1fece8e 67
RodColeman 0:0791c1fece8e 68 /**
RodColeman 0:0791c1fece8e 69 * Processes ICMP input packets, called from ip_input().
RodColeman 0:0791c1fece8e 70 *
RodColeman 0:0791c1fece8e 71 * Currently only processes icmp echo requests and sends
RodColeman 0:0791c1fece8e 72 * out the echo response.
RodColeman 0:0791c1fece8e 73 *
RodColeman 0:0791c1fece8e 74 * @param p the icmp echo request packet, p->payload pointing to the ip header
RodColeman 0:0791c1fece8e 75 * @param inp the netif on which this packet was received
RodColeman 0:0791c1fece8e 76 */
RodColeman 0:0791c1fece8e 77 void
RodColeman 0:0791c1fece8e 78 icmp_input(struct pbuf *p, struct netif *inp)
RodColeman 0:0791c1fece8e 79 {
RodColeman 0:0791c1fece8e 80 u8_t type;
RodColeman 0:0791c1fece8e 81 #ifdef LWIP_DEBUG
RodColeman 0:0791c1fece8e 82 u8_t code;
RodColeman 0:0791c1fece8e 83 #endif /* LWIP_DEBUG */
RodColeman 0:0791c1fece8e 84 struct icmp_echo_hdr *iecho;
RodColeman 0:0791c1fece8e 85 struct ip_hdr *iphdr;
RodColeman 0:0791c1fece8e 86 struct ip_addr tmpaddr;
RodColeman 0:0791c1fece8e 87 s16_t hlen;
RodColeman 0:0791c1fece8e 88
RodColeman 0:0791c1fece8e 89 ICMP_STATS_INC(icmp.recv);
RodColeman 0:0791c1fece8e 90 snmp_inc_icmpinmsgs();
RodColeman 0:0791c1fece8e 91
RodColeman 0:0791c1fece8e 92
RodColeman 0:0791c1fece8e 93 iphdr = (struct ip_hdr *)p->payload;
RodColeman 0:0791c1fece8e 94 hlen = IPH_HL(iphdr) * 4;
RodColeman 0:0791c1fece8e 95 if (pbuf_header(p, -hlen) || (p->tot_len < sizeof(u16_t)*2)) {
RodColeman 0:0791c1fece8e 96 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short ICMP (%"U16_F" bytes) received\n", p->tot_len));
RodColeman 0:0791c1fece8e 97 goto lenerr;
RodColeman 0:0791c1fece8e 98 }
RodColeman 0:0791c1fece8e 99
RodColeman 0:0791c1fece8e 100 type = *((u8_t *)p->payload);
RodColeman 0:0791c1fece8e 101 #ifdef LWIP_DEBUG
RodColeman 0:0791c1fece8e 102 code = *(((u8_t *)p->payload)+1);
RodColeman 0:0791c1fece8e 103 #endif /* LWIP_DEBUG */
RodColeman 0:0791c1fece8e 104 switch (type) {
RodColeman 0:0791c1fece8e 105 case ICMP_ECHO:
RodColeman 0:0791c1fece8e 106 #if !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING
RodColeman 0:0791c1fece8e 107 {
RodColeman 0:0791c1fece8e 108 int accepted = 1;
RodColeman 0:0791c1fece8e 109 #if !LWIP_MULTICAST_PING
RodColeman 0:0791c1fece8e 110 /* multicast destination address? */
RodColeman 0:0791c1fece8e 111 if (ip_addr_ismulticast(&iphdr->dest)) {
RodColeman 0:0791c1fece8e 112 accepted = 0;
RodColeman 0:0791c1fece8e 113 }
RodColeman 0:0791c1fece8e 114 #endif /* LWIP_MULTICAST_PING */
RodColeman 0:0791c1fece8e 115 #if !LWIP_BROADCAST_PING
RodColeman 0:0791c1fece8e 116 /* broadcast destination address? */
RodColeman 0:0791c1fece8e 117 if (ip_addr_isbroadcast(&iphdr->dest, inp)) {
RodColeman 0:0791c1fece8e 118 accepted = 0;
RodColeman 0:0791c1fece8e 119 }
RodColeman 0:0791c1fece8e 120 #endif /* LWIP_BROADCAST_PING */
RodColeman 0:0791c1fece8e 121 /* broadcast or multicast destination address not acceptd? */
RodColeman 0:0791c1fece8e 122 if (!accepted) {
RodColeman 0:0791c1fece8e 123 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to multicast or broadcast pings\n"));
RodColeman 0:0791c1fece8e 124 ICMP_STATS_INC(icmp.err);
RodColeman 0:0791c1fece8e 125 pbuf_free(p);
RodColeman 0:0791c1fece8e 126 return;
RodColeman 0:0791c1fece8e 127 }
RodColeman 0:0791c1fece8e 128 }
RodColeman 0:0791c1fece8e 129 #endif /* !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING */
RodColeman 0:0791c1fece8e 130 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n"));
RodColeman 0:0791c1fece8e 131 if (p->tot_len < sizeof(struct icmp_echo_hdr)) {
RodColeman 0:0791c1fece8e 132 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n"));
RodColeman 0:0791c1fece8e 133 goto lenerr;
RodColeman 0:0791c1fece8e 134 }
RodColeman 0:0791c1fece8e 135 if (inet_chksum_pbuf(p) != 0) {
RodColeman 0:0791c1fece8e 136 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo\n"));
RodColeman 0:0791c1fece8e 137 pbuf_free(p);
RodColeman 0:0791c1fece8e 138 ICMP_STATS_INC(icmp.chkerr);
RodColeman 0:0791c1fece8e 139 snmp_inc_icmpinerrors();
RodColeman 0:0791c1fece8e 140 return;
RodColeman 0:0791c1fece8e 141 }
RodColeman 0:0791c1fece8e 142 #if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
RodColeman 0:0791c1fece8e 143 if (pbuf_header(p, (PBUF_IP_HLEN + PBUF_LINK_HLEN))) {
RodColeman 0:0791c1fece8e 144 /* p is not big enough to contain link headers
RodColeman 0:0791c1fece8e 145 * allocate a new one and copy p into it
RodColeman 0:0791c1fece8e 146 */
RodColeman 0:0791c1fece8e 147 struct pbuf *r;
RodColeman 0:0791c1fece8e 148 /* switch p->payload to ip header */
RodColeman 0:0791c1fece8e 149 if (pbuf_header(p, hlen)) {
RodColeman 0:0791c1fece8e 150 LWIP_ASSERT("icmp_input: moving p->payload to ip header failed\n", 0);
RodColeman 0:0791c1fece8e 151 goto memerr;
RodColeman 0:0791c1fece8e 152 }
RodColeman 0:0791c1fece8e 153 /* allocate new packet buffer with space for link headers */
RodColeman 0:0791c1fece8e 154 r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
RodColeman 0:0791c1fece8e 155 if (r == NULL) {
RodColeman 0:0791c1fece8e 156 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: allocating new pbuf failed\n"));
RodColeman 0:0791c1fece8e 157 goto memerr;
RodColeman 0:0791c1fece8e 158 }
RodColeman 0:0791c1fece8e 159 LWIP_ASSERT("check that first pbuf can hold struct the ICMP header",
RodColeman 0:0791c1fece8e 160 (r->len >= hlen + sizeof(struct icmp_echo_hdr)));
RodColeman 0:0791c1fece8e 161 /* copy the whole packet including ip header */
RodColeman 0:0791c1fece8e 162 if (pbuf_copy(r, p) != ERR_OK) {
RodColeman 0:0791c1fece8e 163 LWIP_ASSERT("icmp_input: copying to new pbuf failed\n", 0);
RodColeman 0:0791c1fece8e 164 goto memerr;
RodColeman 0:0791c1fece8e 165 }
RodColeman 0:0791c1fece8e 166 iphdr = (struct ip_hdr *)r->payload;
RodColeman 0:0791c1fece8e 167 /* switch r->payload back to icmp header */
RodColeman 0:0791c1fece8e 168 if (pbuf_header(r, -hlen)) {
RodColeman 0:0791c1fece8e 169 LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0);
RodColeman 0:0791c1fece8e 170 goto memerr;
RodColeman 0:0791c1fece8e 171 }
RodColeman 0:0791c1fece8e 172 /* free the original p */
RodColeman 0:0791c1fece8e 173 pbuf_free(p);
RodColeman 0:0791c1fece8e 174 /* we now have an identical copy of p that has room for link headers */
RodColeman 0:0791c1fece8e 175 p = r;
RodColeman 0:0791c1fece8e 176 } else {
RodColeman 0:0791c1fece8e 177 /* restore p->payload to point to icmp header */
RodColeman 0:0791c1fece8e 178 if (pbuf_header(p, -(s16_t)(PBUF_IP_HLEN + PBUF_LINK_HLEN))) {
RodColeman 0:0791c1fece8e 179 LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0);
RodColeman 0:0791c1fece8e 180 goto memerr;
RodColeman 0:0791c1fece8e 181 }
RodColeman 0:0791c1fece8e 182 }
RodColeman 0:0791c1fece8e 183 #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
RodColeman 0:0791c1fece8e 184 /* At this point, all checks are OK. */
RodColeman 0:0791c1fece8e 185 /* We generate an answer by switching the dest and src ip addresses,
RodColeman 0:0791c1fece8e 186 * setting the icmp type to ECHO_RESPONSE and updating the checksum. */
RodColeman 0:0791c1fece8e 187 iecho = (struct icmp_echo_hdr *)p->payload;
RodColeman 0:0791c1fece8e 188 tmpaddr.addr = iphdr->src.addr;
RodColeman 0:0791c1fece8e 189 iphdr->src.addr = iphdr->dest.addr;
RodColeman 0:0791c1fece8e 190 iphdr->dest.addr = tmpaddr.addr;
RodColeman 0:0791c1fece8e 191 ICMPH_TYPE_SET(iecho, ICMP_ER);
RodColeman 0:0791c1fece8e 192 /* adjust the checksum */
RodColeman 0:0791c1fece8e 193 if (iecho->chksum >= htons(0xffff - (ICMP_ECHO << 8))) {
RodColeman 0:0791c1fece8e 194 iecho->chksum += htons(ICMP_ECHO << 8) + 1;
RodColeman 0:0791c1fece8e 195 } else {
RodColeman 0:0791c1fece8e 196 iecho->chksum += htons(ICMP_ECHO << 8);
RodColeman 0:0791c1fece8e 197 }
RodColeman 0:0791c1fece8e 198
RodColeman 0:0791c1fece8e 199 /* Set the correct TTL and recalculate the header checksum. */
RodColeman 0:0791c1fece8e 200 IPH_TTL_SET(iphdr, ICMP_TTL);
RodColeman 0:0791c1fece8e 201 IPH_CHKSUM_SET(iphdr, 0);
RodColeman 0:0791c1fece8e 202 #if CHECKSUM_GEN_IP
RodColeman 0:0791c1fece8e 203 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
RodColeman 0:0791c1fece8e 204 #endif /* CHECKSUM_GEN_IP */
RodColeman 0:0791c1fece8e 205
RodColeman 0:0791c1fece8e 206 ICMP_STATS_INC(icmp.xmit);
RodColeman 0:0791c1fece8e 207 /* increase number of messages attempted to send */
RodColeman 0:0791c1fece8e 208 snmp_inc_icmpoutmsgs();
RodColeman 0:0791c1fece8e 209 /* increase number of echo replies attempted to send */
RodColeman 0:0791c1fece8e 210 snmp_inc_icmpoutechoreps();
RodColeman 0:0791c1fece8e 211
RodColeman 0:0791c1fece8e 212 if(pbuf_header(p, hlen)) {
RodColeman 0:0791c1fece8e 213 LWIP_ASSERT("Can't move over header in packet", 0);
RodColeman 0:0791c1fece8e 214 } else {
RodColeman 0:0791c1fece8e 215 err_t ret;
RodColeman 0:0791c1fece8e 216 ret = ip_output_if(p, &(iphdr->src), IP_HDRINCL,
RodColeman 0:0791c1fece8e 217 ICMP_TTL, 0, IP_PROTO_ICMP, inp);
RodColeman 0:0791c1fece8e 218 if (ret != ERR_OK) {
RodColeman 0:0791c1fece8e 219 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ip_output_if returned an error: %c.\n", ret));
RodColeman 0:0791c1fece8e 220 }
RodColeman 0:0791c1fece8e 221 }
RodColeman 0:0791c1fece8e 222 break;
RodColeman 0:0791c1fece8e 223 default:
RodColeman 0:0791c1fece8e 224 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" code %"S16_F" not supported.\n",
RodColeman 0:0791c1fece8e 225 (s16_t)type, (s16_t)code));
RodColeman 0:0791c1fece8e 226 ICMP_STATS_INC(icmp.proterr);
RodColeman 0:0791c1fece8e 227 ICMP_STATS_INC(icmp.drop);
RodColeman 0:0791c1fece8e 228 }
RodColeman 0:0791c1fece8e 229 pbuf_free(p);
RodColeman 0:0791c1fece8e 230 return;
RodColeman 0:0791c1fece8e 231 lenerr:
RodColeman 0:0791c1fece8e 232 pbuf_free(p);
RodColeman 0:0791c1fece8e 233 ICMP_STATS_INC(icmp.lenerr);
RodColeman 0:0791c1fece8e 234 snmp_inc_icmpinerrors();
RodColeman 0:0791c1fece8e 235 return;
RodColeman 0:0791c1fece8e 236 #if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
RodColeman 0:0791c1fece8e 237 memerr:
RodColeman 0:0791c1fece8e 238 pbuf_free(p);
RodColeman 0:0791c1fece8e 239 ICMP_STATS_INC(icmp.err);
RodColeman 0:0791c1fece8e 240 snmp_inc_icmpinerrors();
RodColeman 0:0791c1fece8e 241 return;
RodColeman 0:0791c1fece8e 242 #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
RodColeman 0:0791c1fece8e 243 }
RodColeman 0:0791c1fece8e 244
RodColeman 0:0791c1fece8e 245 /**
RodColeman 0:0791c1fece8e 246 * Send an icmp 'destination unreachable' packet, called from ip_input() if
RodColeman 0:0791c1fece8e 247 * the transport layer protocol is unknown and from udp_input() if the local
RodColeman 0:0791c1fece8e 248 * port is not bound.
RodColeman 0:0791c1fece8e 249 *
RodColeman 0:0791c1fece8e 250 * @param p the input packet for which the 'unreachable' should be sent,
RodColeman 0:0791c1fece8e 251 * p->payload pointing to the IP header
RodColeman 0:0791c1fece8e 252 * @param t type of the 'unreachable' packet
RodColeman 0:0791c1fece8e 253 */
RodColeman 0:0791c1fece8e 254 void
RodColeman 0:0791c1fece8e 255 icmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t)
RodColeman 0:0791c1fece8e 256 {
RodColeman 0:0791c1fece8e 257 icmp_send_response(p, ICMP_DUR, t);
RodColeman 0:0791c1fece8e 258 }
RodColeman 0:0791c1fece8e 259
RodColeman 0:0791c1fece8e 260 #if IP_FORWARD || IP_REASSEMBLY
RodColeman 0:0791c1fece8e 261 /**
RodColeman 0:0791c1fece8e 262 * Send a 'time exceeded' packet, called from ip_forward() if TTL is 0.
RodColeman 0:0791c1fece8e 263 *
RodColeman 0:0791c1fece8e 264 * @param p the input packet for which the 'time exceeded' should be sent,
RodColeman 0:0791c1fece8e 265 * p->payload pointing to the IP header
RodColeman 0:0791c1fece8e 266 * @param t type of the 'time exceeded' packet
RodColeman 0:0791c1fece8e 267 */
RodColeman 0:0791c1fece8e 268 void
RodColeman 0:0791c1fece8e 269 icmp_time_exceeded(struct pbuf *p, enum icmp_te_type t)
RodColeman 0:0791c1fece8e 270 {
RodColeman 0:0791c1fece8e 271 icmp_send_response(p, ICMP_TE, t);
RodColeman 0:0791c1fece8e 272 }
RodColeman 0:0791c1fece8e 273
RodColeman 0:0791c1fece8e 274 #endif /* IP_FORWARD || IP_REASSEMBLY */
RodColeman 0:0791c1fece8e 275
RodColeman 0:0791c1fece8e 276 /**
RodColeman 0:0791c1fece8e 277 * Send an icmp packet in response to an incoming packet.
RodColeman 0:0791c1fece8e 278 *
RodColeman 0:0791c1fece8e 279 * @param p the input packet for which the 'unreachable' should be sent,
RodColeman 0:0791c1fece8e 280 * p->payload pointing to the IP header
RodColeman 0:0791c1fece8e 281 * @param type Type of the ICMP header
RodColeman 0:0791c1fece8e 282 * @param code Code of the ICMP header
RodColeman 0:0791c1fece8e 283 */
RodColeman 0:0791c1fece8e 284 static void
RodColeman 0:0791c1fece8e 285 icmp_send_response(struct pbuf *p, u8_t type, u8_t code)
RodColeman 0:0791c1fece8e 286 {
RodColeman 0:0791c1fece8e 287 struct pbuf *q;
RodColeman 0:0791c1fece8e 288 struct ip_hdr *iphdr;
RodColeman 0:0791c1fece8e 289 /* we can use the echo header here */
RodColeman 0:0791c1fece8e 290 struct icmp_echo_hdr *icmphdr;
RodColeman 0:0791c1fece8e 291
RodColeman 0:0791c1fece8e 292 /* ICMP header + IP header + 8 bytes of data */
RodColeman 0:0791c1fece8e 293 q = pbuf_alloc(PBUF_IP, sizeof(struct icmp_echo_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE,
RodColeman 0:0791c1fece8e 294 PBUF_RAM);
RodColeman 0:0791c1fece8e 295 if (q == NULL) {
RodColeman 0:0791c1fece8e 296 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded: failed to allocate pbuf for ICMP packet.\n"));
RodColeman 0:0791c1fece8e 297 return;
RodColeman 0:0791c1fece8e 298 }
RodColeman 0:0791c1fece8e 299 LWIP_ASSERT("check that first pbuf can hold icmp message",
RodColeman 0:0791c1fece8e 300 (q->len >= (sizeof(struct icmp_echo_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE)));
RodColeman 0:0791c1fece8e 301
RodColeman 0:0791c1fece8e 302 iphdr = (struct ip_hdr *)p->payload;
RodColeman 0:0791c1fece8e 303 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded from "));
RodColeman 0:0791c1fece8e 304 ip_addr_debug_print(ICMP_DEBUG, &(iphdr->src));
RodColeman 0:0791c1fece8e 305 LWIP_DEBUGF(ICMP_DEBUG, (" to "));
RodColeman 0:0791c1fece8e 306 ip_addr_debug_print(ICMP_DEBUG, &(iphdr->dest));
RodColeman 0:0791c1fece8e 307 LWIP_DEBUGF(ICMP_DEBUG, ("\n"));
RodColeman 0:0791c1fece8e 308
RodColeman 0:0791c1fece8e 309 icmphdr = (struct icmp_echo_hdr *)q->payload;
RodColeman 0:0791c1fece8e 310 icmphdr->type = type;
RodColeman 0:0791c1fece8e 311 icmphdr->code = code;
RodColeman 0:0791c1fece8e 312 icmphdr->id = 0;
RodColeman 0:0791c1fece8e 313 icmphdr->seqno = 0;
RodColeman 0:0791c1fece8e 314
RodColeman 0:0791c1fece8e 315 /* copy fields from original packet */
RodColeman 0:0791c1fece8e 316 SMEMCPY((u8_t *)q->payload + sizeof(struct icmp_echo_hdr), (u8_t *)p->payload,
RodColeman 0:0791c1fece8e 317 IP_HLEN + ICMP_DEST_UNREACH_DATASIZE);
RodColeman 0:0791c1fece8e 318
RodColeman 0:0791c1fece8e 319 /* calculate checksum */
RodColeman 0:0791c1fece8e 320 icmphdr->chksum = 0;
RodColeman 0:0791c1fece8e 321 icmphdr->chksum = inet_chksum(icmphdr, q->len);
RodColeman 0:0791c1fece8e 322 ICMP_STATS_INC(icmp.xmit);
RodColeman 0:0791c1fece8e 323 /* increase number of messages attempted to send */
RodColeman 0:0791c1fece8e 324 snmp_inc_icmpoutmsgs();
RodColeman 0:0791c1fece8e 325 /* increase number of destination unreachable messages attempted to send */
RodColeman 0:0791c1fece8e 326 snmp_inc_icmpouttimeexcds();
RodColeman 0:0791c1fece8e 327 ip_output(q, NULL, &(iphdr->src), ICMP_TTL, 0, IP_PROTO_ICMP);
RodColeman 0:0791c1fece8e 328 pbuf_free(q);
RodColeman 0:0791c1fece8e 329 }
RodColeman 0:0791c1fece8e 330
RodColeman 0:0791c1fece8e 331 #endif /* LWIP_ICMP */