1195972f6Sopenharmony_ci/**
2195972f6Sopenharmony_ci * @file
3195972f6Sopenharmony_ci * ICMP - Internet Control Message Protocol
4195972f6Sopenharmony_ci *
5195972f6Sopenharmony_ci */
6195972f6Sopenharmony_ci
7195972f6Sopenharmony_ci/*
8195972f6Sopenharmony_ci * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
9195972f6Sopenharmony_ci * All rights reserved.
10195972f6Sopenharmony_ci *
11195972f6Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
12195972f6Sopenharmony_ci * are permitted provided that the following conditions are met:
13195972f6Sopenharmony_ci *
14195972f6Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice,
15195972f6Sopenharmony_ci *    this list of conditions and the following disclaimer.
16195972f6Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice,
17195972f6Sopenharmony_ci *    this list of conditions and the following disclaimer in the documentation
18195972f6Sopenharmony_ci *    and/or other materials provided with the distribution.
19195972f6Sopenharmony_ci * 3. The name of the author may not be used to endorse or promote products
20195972f6Sopenharmony_ci *    derived from this software without specific prior written permission.
21195972f6Sopenharmony_ci *
22195972f6Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23195972f6Sopenharmony_ci * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24195972f6Sopenharmony_ci * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25195972f6Sopenharmony_ci * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26195972f6Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27195972f6Sopenharmony_ci * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28195972f6Sopenharmony_ci * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29195972f6Sopenharmony_ci * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30195972f6Sopenharmony_ci * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31195972f6Sopenharmony_ci * OF SUCH DAMAGE.
32195972f6Sopenharmony_ci *
33195972f6Sopenharmony_ci * This file is part of the lwIP TCP/IP stack.
34195972f6Sopenharmony_ci *
35195972f6Sopenharmony_ci * Author: Adam Dunkels <adam@sics.se>
36195972f6Sopenharmony_ci *
37195972f6Sopenharmony_ci */
38195972f6Sopenharmony_ci
39195972f6Sopenharmony_ci/* Some ICMP messages should be passed to the transport protocols. This
40195972f6Sopenharmony_ci   is not implemented. */
41195972f6Sopenharmony_ci
42195972f6Sopenharmony_ci#include "lwip/opt.h"
43195972f6Sopenharmony_ci
44195972f6Sopenharmony_ci#if LWIP_IPV4 && LWIP_ICMP /* don't build if not configured for use in lwipopts.h */
45195972f6Sopenharmony_ci
46195972f6Sopenharmony_ci#include "lwip/icmp.h"
47195972f6Sopenharmony_ci#include "lwip/inet_chksum.h"
48195972f6Sopenharmony_ci#include "lwip/ip.h"
49195972f6Sopenharmony_ci#include "lwip/def.h"
50195972f6Sopenharmony_ci#include "lwip/stats.h"
51195972f6Sopenharmony_ci
52195972f6Sopenharmony_ci#include <string.h>
53195972f6Sopenharmony_ci
54195972f6Sopenharmony_ci#ifdef LWIP_HOOK_FILENAME
55195972f6Sopenharmony_ci#include LWIP_HOOK_FILENAME
56195972f6Sopenharmony_ci#endif
57195972f6Sopenharmony_ci
58195972f6Sopenharmony_ci/** Small optimization: set to 0 if incoming PBUF_POOL pbuf always can be
59195972f6Sopenharmony_ci * used to modify and send a response packet (and to 1 if this is not the case,
60195972f6Sopenharmony_ci * e.g. when link header is stripped off when receiving) */
61195972f6Sopenharmony_ci#ifndef LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
62195972f6Sopenharmony_ci#define LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN 1
63195972f6Sopenharmony_ci#endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
64195972f6Sopenharmony_ci
65195972f6Sopenharmony_ci/* The maximum amount of data from the original packet to return in a dest-unreachable */
66195972f6Sopenharmony_ci#define ICMP_DEST_UNREACH_DATASIZE 8
67195972f6Sopenharmony_ci
68195972f6Sopenharmony_cistatic void icmp_send_response(struct pbuf *p, u8_t type, u8_t code);
69195972f6Sopenharmony_ci
70195972f6Sopenharmony_ci/**
71195972f6Sopenharmony_ci * Processes ICMP input packets, called from ip_input().
72195972f6Sopenharmony_ci *
73195972f6Sopenharmony_ci * Currently only processes icmp echo requests and sends
74195972f6Sopenharmony_ci * out the echo response.
75195972f6Sopenharmony_ci *
76195972f6Sopenharmony_ci * @param p the icmp echo request packet, p->payload pointing to the icmp header
77195972f6Sopenharmony_ci * @param inp the netif on which this packet was received
78195972f6Sopenharmony_ci */
79195972f6Sopenharmony_civoid
80195972f6Sopenharmony_ciicmp_input(struct pbuf *p, struct netif *inp)
81195972f6Sopenharmony_ci{
82195972f6Sopenharmony_ci  u8_t type;
83195972f6Sopenharmony_ci#ifdef LWIP_DEBUG
84195972f6Sopenharmony_ci  u8_t code;
85195972f6Sopenharmony_ci#endif /* LWIP_DEBUG */
86195972f6Sopenharmony_ci  struct icmp_echo_hdr *iecho;
87195972f6Sopenharmony_ci  const struct ip_hdr *iphdr_in;
88195972f6Sopenharmony_ci  u16_t hlen;
89195972f6Sopenharmony_ci  const ip4_addr_t *src;
90195972f6Sopenharmony_ci
91195972f6Sopenharmony_ci  ICMP_STATS_INC(icmp.recv);
92195972f6Sopenharmony_ci  MIB2_STATS_INC(mib2.icmpinmsgs);
93195972f6Sopenharmony_ci
94195972f6Sopenharmony_ci  iphdr_in = ip4_current_header();
95195972f6Sopenharmony_ci  hlen = IPH_HL_BYTES(iphdr_in);
96195972f6Sopenharmony_ci  if (hlen < IP_HLEN) {
97195972f6Sopenharmony_ci    LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short IP header (%"S16_F" bytes) received\n", hlen));
98195972f6Sopenharmony_ci    goto lenerr;
99195972f6Sopenharmony_ci  }
100195972f6Sopenharmony_ci  if (p->len < sizeof(u16_t) * 2) {
101195972f6Sopenharmony_ci    LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short ICMP (%"U16_F" bytes) received\n", p->tot_len));
102195972f6Sopenharmony_ci    goto lenerr;
103195972f6Sopenharmony_ci  }
104195972f6Sopenharmony_ci
105195972f6Sopenharmony_ci  type = *((u8_t *)p->payload);
106195972f6Sopenharmony_ci#ifdef LWIP_DEBUG
107195972f6Sopenharmony_ci  code = *(((u8_t *)p->payload) + 1);
108195972f6Sopenharmony_ci  /* if debug is enabled but debug statement below is somehow disabled: */
109195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(code);
110195972f6Sopenharmony_ci#endif /* LWIP_DEBUG */
111195972f6Sopenharmony_ci  switch (type) {
112195972f6Sopenharmony_ci    case ICMP_ER:
113195972f6Sopenharmony_ci      /* This is OK, echo reply might have been parsed by a raw PCB
114195972f6Sopenharmony_ci         (as obviously, an echo request has been sent, too). */
115195972f6Sopenharmony_ci      MIB2_STATS_INC(mib2.icmpinechoreps);
116195972f6Sopenharmony_ci      break;
117195972f6Sopenharmony_ci    case ICMP_ECHO:
118195972f6Sopenharmony_ci      MIB2_STATS_INC(mib2.icmpinechos);
119195972f6Sopenharmony_ci      src = ip4_current_dest_addr();
120195972f6Sopenharmony_ci      /* multicast destination address? */
121195972f6Sopenharmony_ci      if (ip4_addr_ismulticast(ip4_current_dest_addr())) {
122195972f6Sopenharmony_ci#if LWIP_MULTICAST_PING
123195972f6Sopenharmony_ci        /* For multicast, use address of receiving interface as source address */
124195972f6Sopenharmony_ci        src = netif_ip4_addr(inp);
125195972f6Sopenharmony_ci#else /* LWIP_MULTICAST_PING */
126195972f6Sopenharmony_ci        LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to multicast pings\n"));
127195972f6Sopenharmony_ci        goto icmperr;
128195972f6Sopenharmony_ci#endif /* LWIP_MULTICAST_PING */
129195972f6Sopenharmony_ci      }
130195972f6Sopenharmony_ci      /* broadcast destination address? */
131195972f6Sopenharmony_ci      if (ip4_addr_isbroadcast(ip4_current_dest_addr(), ip_current_netif())) {
132195972f6Sopenharmony_ci#if LWIP_BROADCAST_PING
133195972f6Sopenharmony_ci        /* For broadcast, use address of receiving interface as source address */
134195972f6Sopenharmony_ci        src = netif_ip4_addr(inp);
135195972f6Sopenharmony_ci#else /* LWIP_BROADCAST_PING */
136195972f6Sopenharmony_ci        LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to broadcast pings\n"));
137195972f6Sopenharmony_ci        goto icmperr;
138195972f6Sopenharmony_ci#endif /* LWIP_BROADCAST_PING */
139195972f6Sopenharmony_ci      }
140195972f6Sopenharmony_ci      LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n"));
141195972f6Sopenharmony_ci      if (p->tot_len < sizeof(struct icmp_echo_hdr)) {
142195972f6Sopenharmony_ci        LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n"));
143195972f6Sopenharmony_ci        goto lenerr;
144195972f6Sopenharmony_ci      }
145195972f6Sopenharmony_ci#if CHECKSUM_CHECK_ICMP
146195972f6Sopenharmony_ci      IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_ICMP) {
147195972f6Sopenharmony_ci        if (inet_chksum_pbuf(p) != 0) {
148195972f6Sopenharmony_ci          LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo\n"));
149195972f6Sopenharmony_ci          pbuf_free(p);
150195972f6Sopenharmony_ci          ICMP_STATS_INC(icmp.chkerr);
151195972f6Sopenharmony_ci          MIB2_STATS_INC(mib2.icmpinerrors);
152195972f6Sopenharmony_ci          return;
153195972f6Sopenharmony_ci        }
154195972f6Sopenharmony_ci      }
155195972f6Sopenharmony_ci#endif
156195972f6Sopenharmony_ci#if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
157195972f6Sopenharmony_ci      if (pbuf_add_header(p, hlen + PBUF_LINK_HLEN + PBUF_LINK_ENCAPSULATION_HLEN)) {
158195972f6Sopenharmony_ci        /* p is not big enough to contain link headers
159195972f6Sopenharmony_ci         * allocate a new one and copy p into it
160195972f6Sopenharmony_ci         */
161195972f6Sopenharmony_ci        struct pbuf *r;
162195972f6Sopenharmony_ci        u16_t alloc_len = (u16_t)(p->tot_len + hlen);
163195972f6Sopenharmony_ci        if (alloc_len < p->tot_len) {
164195972f6Sopenharmony_ci          LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: allocating new pbuf failed (tot_len overflow)\n"));
165195972f6Sopenharmony_ci          goto icmperr;
166195972f6Sopenharmony_ci        }
167195972f6Sopenharmony_ci        /* allocate new packet buffer with space for link headers */
168195972f6Sopenharmony_ci        r = pbuf_alloc(PBUF_LINK, alloc_len, PBUF_RAM);
169195972f6Sopenharmony_ci        if (r == NULL) {
170195972f6Sopenharmony_ci          LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: allocating new pbuf failed\n"));
171195972f6Sopenharmony_ci          goto icmperr;
172195972f6Sopenharmony_ci        }
173195972f6Sopenharmony_ci        if (r->len < hlen + sizeof(struct icmp_echo_hdr)) {
174195972f6Sopenharmony_ci          LWIP_DEBUGF(ICMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("first pbuf cannot hold the ICMP header"));
175195972f6Sopenharmony_ci          pbuf_free(r);
176195972f6Sopenharmony_ci          goto icmperr;
177195972f6Sopenharmony_ci        }
178195972f6Sopenharmony_ci        /* copy the ip header */
179195972f6Sopenharmony_ci        MEMCPY(r->payload, iphdr_in, hlen);
180195972f6Sopenharmony_ci        /* switch r->payload back to icmp header (cannot fail) */
181195972f6Sopenharmony_ci        if (pbuf_remove_header(r, hlen)) {
182195972f6Sopenharmony_ci          LWIP_ASSERT("icmp_input: moving r->payload to icmp header failed\n", 0);
183195972f6Sopenharmony_ci          pbuf_free(r);
184195972f6Sopenharmony_ci          goto icmperr;
185195972f6Sopenharmony_ci        }
186195972f6Sopenharmony_ci        /* copy the rest of the packet without ip header */
187195972f6Sopenharmony_ci        if (pbuf_copy(r, p) != ERR_OK) {
188195972f6Sopenharmony_ci          LWIP_DEBUGF(ICMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("icmp_input: copying to new pbuf failed"));
189195972f6Sopenharmony_ci          pbuf_free(r);
190195972f6Sopenharmony_ci          goto icmperr;
191195972f6Sopenharmony_ci        }
192195972f6Sopenharmony_ci        /* free the original p */
193195972f6Sopenharmony_ci        pbuf_free(p);
194195972f6Sopenharmony_ci        /* we now have an identical copy of p that has room for link headers */
195195972f6Sopenharmony_ci        p = r;
196195972f6Sopenharmony_ci      } else {
197195972f6Sopenharmony_ci        /* restore p->payload to point to icmp header (cannot fail) */
198195972f6Sopenharmony_ci        if (pbuf_remove_header(p, hlen + PBUF_LINK_HLEN + PBUF_LINK_ENCAPSULATION_HLEN)) {
199195972f6Sopenharmony_ci          LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0);
200195972f6Sopenharmony_ci          goto icmperr;
201195972f6Sopenharmony_ci        }
202195972f6Sopenharmony_ci      }
203195972f6Sopenharmony_ci#endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
204195972f6Sopenharmony_ci      /* At this point, all checks are OK. */
205195972f6Sopenharmony_ci      /* We generate an answer by switching the dest and src ip addresses,
206195972f6Sopenharmony_ci       * setting the icmp type to ECHO_RESPONSE and updating the checksum. */
207195972f6Sopenharmony_ci      iecho = (struct icmp_echo_hdr *)p->payload;
208195972f6Sopenharmony_ci      if (pbuf_add_header(p, hlen)) {
209195972f6Sopenharmony_ci        LWIP_DEBUGF(ICMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Can't move over header in packet"));
210195972f6Sopenharmony_ci      } else {
211195972f6Sopenharmony_ci        err_t ret;
212195972f6Sopenharmony_ci        struct ip_hdr *iphdr = (struct ip_hdr *)p->payload;
213195972f6Sopenharmony_ci        ip4_addr_copy(iphdr->src, *src);
214195972f6Sopenharmony_ci        ip4_addr_copy(iphdr->dest, *ip4_current_src_addr());
215195972f6Sopenharmony_ci        ICMPH_TYPE_SET(iecho, ICMP_ER);
216195972f6Sopenharmony_ci#if CHECKSUM_GEN_ICMP
217195972f6Sopenharmony_ci        IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_GEN_ICMP) {
218195972f6Sopenharmony_ci          /* adjust the checksum */
219195972f6Sopenharmony_ci          if (iecho->chksum > PP_HTONS(0xffffU - (ICMP_ECHO << 8))) {
220195972f6Sopenharmony_ci            iecho->chksum = (u16_t)(iecho->chksum + PP_HTONS((u16_t)(ICMP_ECHO << 8)) + 1);
221195972f6Sopenharmony_ci          } else {
222195972f6Sopenharmony_ci            iecho->chksum = (u16_t)(iecho->chksum + PP_HTONS(ICMP_ECHO << 8));
223195972f6Sopenharmony_ci          }
224195972f6Sopenharmony_ci        }
225195972f6Sopenharmony_ci#if LWIP_CHECKSUM_CTRL_PER_NETIF
226195972f6Sopenharmony_ci        else {
227195972f6Sopenharmony_ci          iecho->chksum = 0;
228195972f6Sopenharmony_ci        }
229195972f6Sopenharmony_ci#endif /* LWIP_CHECKSUM_CTRL_PER_NETIF */
230195972f6Sopenharmony_ci#else /* CHECKSUM_GEN_ICMP */
231195972f6Sopenharmony_ci        iecho->chksum = 0;
232195972f6Sopenharmony_ci#endif /* CHECKSUM_GEN_ICMP */
233195972f6Sopenharmony_ci
234195972f6Sopenharmony_ci        /* Set the correct TTL and recalculate the header checksum. */
235195972f6Sopenharmony_ci        IPH_TTL_SET(iphdr, ICMP_TTL);
236195972f6Sopenharmony_ci        IPH_CHKSUM_SET(iphdr, 0);
237195972f6Sopenharmony_ci#if CHECKSUM_GEN_IP
238195972f6Sopenharmony_ci        IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_GEN_IP) {
239195972f6Sopenharmony_ci          IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, hlen));
240195972f6Sopenharmony_ci        }
241195972f6Sopenharmony_ci#endif /* CHECKSUM_GEN_IP */
242195972f6Sopenharmony_ci
243195972f6Sopenharmony_ci        ICMP_STATS_INC(icmp.xmit);
244195972f6Sopenharmony_ci        /* increase number of messages attempted to send */
245195972f6Sopenharmony_ci        MIB2_STATS_INC(mib2.icmpoutmsgs);
246195972f6Sopenharmony_ci        /* increase number of echo replies attempted to send */
247195972f6Sopenharmony_ci        MIB2_STATS_INC(mib2.icmpoutechoreps);
248195972f6Sopenharmony_ci
249195972f6Sopenharmony_ci        /* send an ICMP packet */
250195972f6Sopenharmony_ci        ret = ip4_output_if(p, src, LWIP_IP_HDRINCL,
251195972f6Sopenharmony_ci                            ICMP_TTL, 0, IP_PROTO_ICMP, inp);
252195972f6Sopenharmony_ci        if (ret != ERR_OK) {
253195972f6Sopenharmony_ci          LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ip_output_if returned an error: %s\n", lwip_strerr(ret)));
254195972f6Sopenharmony_ci        }
255195972f6Sopenharmony_ci      }
256195972f6Sopenharmony_ci      break;
257195972f6Sopenharmony_ci    default:
258195972f6Sopenharmony_ci      if (type == ICMP_DUR) {
259195972f6Sopenharmony_ci        MIB2_STATS_INC(mib2.icmpindestunreachs);
260195972f6Sopenharmony_ci      } else if (type == ICMP_TE) {
261195972f6Sopenharmony_ci        MIB2_STATS_INC(mib2.icmpintimeexcds);
262195972f6Sopenharmony_ci      } else if (type == ICMP_PP) {
263195972f6Sopenharmony_ci        MIB2_STATS_INC(mib2.icmpinparmprobs);
264195972f6Sopenharmony_ci      } else if (type == ICMP_SQ) {
265195972f6Sopenharmony_ci        MIB2_STATS_INC(mib2.icmpinsrcquenchs);
266195972f6Sopenharmony_ci      } else if (type == ICMP_RD) {
267195972f6Sopenharmony_ci        MIB2_STATS_INC(mib2.icmpinredirects);
268195972f6Sopenharmony_ci      } else if (type == ICMP_TS) {
269195972f6Sopenharmony_ci        MIB2_STATS_INC(mib2.icmpintimestamps);
270195972f6Sopenharmony_ci      } else if (type == ICMP_TSR) {
271195972f6Sopenharmony_ci        MIB2_STATS_INC(mib2.icmpintimestampreps);
272195972f6Sopenharmony_ci      } else if (type == ICMP_AM) {
273195972f6Sopenharmony_ci        MIB2_STATS_INC(mib2.icmpinaddrmasks);
274195972f6Sopenharmony_ci      } else if (type == ICMP_AMR) {
275195972f6Sopenharmony_ci        MIB2_STATS_INC(mib2.icmpinaddrmaskreps);
276195972f6Sopenharmony_ci      }
277195972f6Sopenharmony_ci      LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" code %"S16_F" not supported.\n",
278195972f6Sopenharmony_ci                               (s16_t)type, (s16_t)code));
279195972f6Sopenharmony_ci      ICMP_STATS_INC(icmp.proterr);
280195972f6Sopenharmony_ci      ICMP_STATS_INC(icmp.drop);
281195972f6Sopenharmony_ci  }
282195972f6Sopenharmony_ci  pbuf_free(p);
283195972f6Sopenharmony_ci  return;
284195972f6Sopenharmony_cilenerr:
285195972f6Sopenharmony_ci  pbuf_free(p);
286195972f6Sopenharmony_ci  ICMP_STATS_INC(icmp.lenerr);
287195972f6Sopenharmony_ci  MIB2_STATS_INC(mib2.icmpinerrors);
288195972f6Sopenharmony_ci  return;
289195972f6Sopenharmony_ci#if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN || !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING
290195972f6Sopenharmony_ciicmperr:
291195972f6Sopenharmony_ci  pbuf_free(p);
292195972f6Sopenharmony_ci  ICMP_STATS_INC(icmp.err);
293195972f6Sopenharmony_ci  MIB2_STATS_INC(mib2.icmpinerrors);
294195972f6Sopenharmony_ci  return;
295195972f6Sopenharmony_ci#endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN || !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING */
296195972f6Sopenharmony_ci}
297195972f6Sopenharmony_ci
298195972f6Sopenharmony_ci/**
299195972f6Sopenharmony_ci * Send an icmp 'destination unreachable' packet, called from ip_input() if
300195972f6Sopenharmony_ci * the transport layer protocol is unknown and from udp_input() if the local
301195972f6Sopenharmony_ci * port is not bound.
302195972f6Sopenharmony_ci *
303195972f6Sopenharmony_ci * @param p the input packet for which the 'unreachable' should be sent,
304195972f6Sopenharmony_ci *          p->payload pointing to the IP header
305195972f6Sopenharmony_ci * @param t type of the 'unreachable' packet
306195972f6Sopenharmony_ci */
307195972f6Sopenharmony_civoid
308195972f6Sopenharmony_ciicmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t)
309195972f6Sopenharmony_ci{
310195972f6Sopenharmony_ci  MIB2_STATS_INC(mib2.icmpoutdestunreachs);
311195972f6Sopenharmony_ci  icmp_send_response(p, ICMP_DUR, t);
312195972f6Sopenharmony_ci}
313195972f6Sopenharmony_ci
314195972f6Sopenharmony_ci#if IP_FORWARD || IP_REASSEMBLY
315195972f6Sopenharmony_ci/**
316195972f6Sopenharmony_ci * Send a 'time exceeded' packet, called from ip_forward() if TTL is 0.
317195972f6Sopenharmony_ci *
318195972f6Sopenharmony_ci * @param p the input packet for which the 'time exceeded' should be sent,
319195972f6Sopenharmony_ci *          p->payload pointing to the IP header
320195972f6Sopenharmony_ci * @param t type of the 'time exceeded' packet
321195972f6Sopenharmony_ci */
322195972f6Sopenharmony_civoid
323195972f6Sopenharmony_ciicmp_time_exceeded(struct pbuf *p, enum icmp_te_type t)
324195972f6Sopenharmony_ci{
325195972f6Sopenharmony_ci  MIB2_STATS_INC(mib2.icmpouttimeexcds);
326195972f6Sopenharmony_ci  icmp_send_response(p, ICMP_TE, t);
327195972f6Sopenharmony_ci}
328195972f6Sopenharmony_ci
329195972f6Sopenharmony_ci#endif /* IP_FORWARD || IP_REASSEMBLY */
330195972f6Sopenharmony_ci
331195972f6Sopenharmony_ci/**
332195972f6Sopenharmony_ci * Send an icmp packet in response to an incoming packet.
333195972f6Sopenharmony_ci *
334195972f6Sopenharmony_ci * @param p the input packet for which the 'unreachable' should be sent,
335195972f6Sopenharmony_ci *          p->payload pointing to the IP header
336195972f6Sopenharmony_ci * @param type Type of the ICMP header
337195972f6Sopenharmony_ci * @param code Code of the ICMP header
338195972f6Sopenharmony_ci */
339195972f6Sopenharmony_cistatic void
340195972f6Sopenharmony_ciicmp_send_response(struct pbuf *p, u8_t type, u8_t code)
341195972f6Sopenharmony_ci{
342195972f6Sopenharmony_ci  struct pbuf *q;
343195972f6Sopenharmony_ci  struct ip_hdr *iphdr;
344195972f6Sopenharmony_ci  /* we can use the echo header here */
345195972f6Sopenharmony_ci  struct icmp_echo_hdr *icmphdr;
346195972f6Sopenharmony_ci  ip4_addr_t iphdr_src;
347195972f6Sopenharmony_ci  struct netif *netif;
348195972f6Sopenharmony_ci  u16_t response_pkt_len;
349195972f6Sopenharmony_ci
350195972f6Sopenharmony_ci  /* increase number of messages attempted to send */
351195972f6Sopenharmony_ci  MIB2_STATS_INC(mib2.icmpoutmsgs);
352195972f6Sopenharmony_ci
353195972f6Sopenharmony_ci  /* Keep IP header + up to 8 bytes */
354195972f6Sopenharmony_ci  response_pkt_len = IP_HLEN + ICMP_DEST_UNREACH_DATASIZE;
355195972f6Sopenharmony_ci  if (p->tot_len < response_pkt_len) {
356195972f6Sopenharmony_ci    response_pkt_len = p->tot_len;
357195972f6Sopenharmony_ci  }
358195972f6Sopenharmony_ci
359195972f6Sopenharmony_ci  /* ICMP header + part of original packet */
360195972f6Sopenharmony_ci  q = pbuf_alloc(PBUF_IP, sizeof(struct icmp_echo_hdr) + response_pkt_len, PBUF_RAM);
361195972f6Sopenharmony_ci  if (q == NULL) {
362195972f6Sopenharmony_ci    LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded: failed to allocate pbuf for ICMP packet.\n"));
363195972f6Sopenharmony_ci    MIB2_STATS_INC(mib2.icmpouterrors);
364195972f6Sopenharmony_ci    return;
365195972f6Sopenharmony_ci  }
366195972f6Sopenharmony_ci  LWIP_ASSERT("check that first pbuf can hold icmp message",
367195972f6Sopenharmony_ci              (q->len >= (sizeof(struct icmp_echo_hdr) + response_pkt_len)));
368195972f6Sopenharmony_ci
369195972f6Sopenharmony_ci  iphdr = (struct ip_hdr *)p->payload;
370195972f6Sopenharmony_ci  LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded from "));
371195972f6Sopenharmony_ci  ip4_addr_debug_print_val(ICMP_DEBUG, iphdr->src);
372195972f6Sopenharmony_ci  LWIP_DEBUGF(ICMP_DEBUG, (" to "));
373195972f6Sopenharmony_ci  ip4_addr_debug_print_val(ICMP_DEBUG, iphdr->dest);
374195972f6Sopenharmony_ci  LWIP_DEBUGF(ICMP_DEBUG, ("\n"));
375195972f6Sopenharmony_ci
376195972f6Sopenharmony_ci  icmphdr = (struct icmp_echo_hdr *)q->payload;
377195972f6Sopenharmony_ci  icmphdr->type = type;
378195972f6Sopenharmony_ci  icmphdr->code = code;
379195972f6Sopenharmony_ci  icmphdr->id = 0;
380195972f6Sopenharmony_ci  icmphdr->seqno = 0;
381195972f6Sopenharmony_ci
382195972f6Sopenharmony_ci  /* copy fields from original packet */
383195972f6Sopenharmony_ci  SMEMCPY((u8_t *)q->payload + sizeof(struct icmp_echo_hdr), (u8_t *)p->payload,
384195972f6Sopenharmony_ci          response_pkt_len);
385195972f6Sopenharmony_ci
386195972f6Sopenharmony_ci  ip4_addr_copy(iphdr_src, iphdr->src);
387195972f6Sopenharmony_ci#ifdef LWIP_HOOK_IP4_ROUTE_SRC
388195972f6Sopenharmony_ci  {
389195972f6Sopenharmony_ci    ip4_addr_t iphdr_dst;
390195972f6Sopenharmony_ci    ip4_addr_copy(iphdr_dst, iphdr->dest);
391195972f6Sopenharmony_ci#ifdef LOSCFG_NET_CONTAINER
392195972f6Sopenharmony_ci    netif = ip4_route_src(&iphdr_dst, &iphdr_src, get_root_net_group());
393195972f6Sopenharmony_ci#else
394195972f6Sopenharmony_ci    netif = ip4_route_src(&iphdr_dst, &iphdr_src);
395195972f6Sopenharmony_ci#endif
396195972f6Sopenharmony_ci  }
397195972f6Sopenharmony_ci#else
398195972f6Sopenharmony_ci#ifdef LOSCFG_NET_CONTAINER
399195972f6Sopenharmony_ci  netif = ip4_route(&iphdr_src, get_root_net_group());
400195972f6Sopenharmony_ci#else
401195972f6Sopenharmony_ci  netif = ip4_route(&iphdr_src);
402195972f6Sopenharmony_ci#endif
403195972f6Sopenharmony_ci#endif
404195972f6Sopenharmony_ci  if (netif != NULL) {
405195972f6Sopenharmony_ci    /* calculate checksum */
406195972f6Sopenharmony_ci    icmphdr->chksum = 0;
407195972f6Sopenharmony_ci#if CHECKSUM_GEN_ICMP
408195972f6Sopenharmony_ci    IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP) {
409195972f6Sopenharmony_ci      icmphdr->chksum = inet_chksum(icmphdr, q->len);
410195972f6Sopenharmony_ci    }
411195972f6Sopenharmony_ci#endif
412195972f6Sopenharmony_ci    ICMP_STATS_INC(icmp.xmit);
413195972f6Sopenharmony_ci    ip4_output_if(q, NULL, &iphdr_src, ICMP_TTL, 0, IP_PROTO_ICMP, netif);
414195972f6Sopenharmony_ci  }
415195972f6Sopenharmony_ci  pbuf_free(q);
416195972f6Sopenharmony_ci}
417195972f6Sopenharmony_ci
418195972f6Sopenharmony_ci#endif /* LWIP_IPV4 && LWIP_ICMP */
419