Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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