Sam Walsh / F7_Ethernet_F746ZG

Fork of F7_Ethernet by Dieter Graef

Committer:
DieterGraef
Date:
Sun Jun 19 16:23:40 2016 +0000
Revision:
0:d26c1b55cfca
Ethernet Library for Nucleo stm32f746ZG and Disco stm32f746NG  works under arm and gcc environment

Who changed what in which revision?

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