SNMP agent attached to SPI slave

Dependencies:   mbed

Committer:
lorcansmith
Date:
Mon Aug 13 15:07:40 2012 +0000
Revision:
0:2a53a4c3238c
v1.1 release includes ioAlarm traps

Who changed what in which revision?

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