HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

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