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