Deprecated fork of old network stack source from github. Please use official library instead: https://mbed.org/users/mbed_official/code/EthernetInterface/

Committer:
AdamGreen
Date:
Sat Oct 26 08:51:36 2013 +0000
Revision:
1:eadc868c2acf
Parent:
0:3b00827bb0b7
Fix TCP checksum bug and stranded large TCP segments.

Who changed what in which revision?

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