void icmp_input(struct pbuf *p, struct netif *inp) { u8_t type; struct icmp_echo_hdr *iecho; struct ip_hdr *iphdr; struct ip_addr tmpaddr; ICMP_STATS_INC(icmp.recv); /* TODO: check length before accessing payload! */ type = ((u8_t *)p->payload)[0]; switch (type) { case ICMP6_ECHO: LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n")); if (p->tot_len < sizeof(struct icmp_echo_hdr)) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n")); pbuf_free(p); ICMP_STATS_INC(icmp.lenerr); return; } iecho = p->payload; iphdr = (struct ip_hdr *)((u8_t *)p->payload - IP_HLEN); if (inet_chksum_pbuf(p) != 0) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo (%"X16_F")\n", inet_chksum_pseudo(p, &(iphdr->src), &(iphdr->dest), IP_PROTO_ICMP, p->tot_len))); ICMP_STATS_INC(icmp.chkerr); /* return;*/ } LWIP_DEBUGF(ICMP_DEBUG, ("icmp: p->len %"S16_F" p->tot_len %"S16_F"\n", p->len, p->tot_len)); ip_addr_set(&tmpaddr, &(iphdr->src)); ip_addr_set(&(iphdr->src), &(iphdr->dest)); ip_addr_set(&(iphdr->dest), &tmpaddr); iecho->type = ICMP6_ER; /* adjust the checksum */ if (iecho->chksum >= htons(0xffff - (ICMP6_ECHO << 8))) { iecho->chksum += htons(ICMP6_ECHO << 8) + 1; } else { iecho->chksum += htons(ICMP6_ECHO << 8); } LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo (%"X16_F")\n", inet_chksum_pseudo(p, &(iphdr->src), &(iphdr->dest), IP_PROTO_ICMP, p->tot_len))); ICMP_STATS_INC(icmp.xmit); /* LWIP_DEBUGF("icmp: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len);*/ ip_output_if (p, &(iphdr->src), IP_HDRINCL, iphdr->hoplim, IP_PROTO_ICMP, inp); break; default: LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" not supported.\n", (s16_t)type)); ICMP_STATS_INC(icmp.proterr); ICMP_STATS_INC(icmp.drop); } pbuf_free(p); }
/** * Processes ICMP input packets, called from ip_input(). * * Currently only processes icmp echo requests and sends * out the echo response. * * @param p the icmp echo request packet, p->payload pointing to the icmp header * @param inp the netif on which this packet was received */ void icmp_input(struct pbuf *p, struct netif *inp) { u8_t type; #ifdef LWIP_DEBUG u8_t code; #endif /* LWIP_DEBUG */ struct icmp_echo_hdr *iecho; struct ip_hdr *iphdr; s16_t hlen; ICMP_STATS_INC(icmp.recv); snmp_inc_icmpinmsgs(); iphdr = (struct ip_hdr *)ip_current_header(); hlen = IPH_HL(iphdr) * 4; if (p->len < sizeof(u16_t)*2) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short ICMP (%"U16_F" bytes) received\n", p->tot_len)); goto lenerr; } type = *((u8_t *)p->payload); #ifdef LWIP_DEBUG code = *(((u8_t *)p->payload)+1); #endif /* LWIP_DEBUG */ switch (type) { case ICMP_ER: /* This is OK, echo reply might have been parsed by a raw PCB (as obviously, an echo request has been sent, too). */ break; case ICMP_ECHO: #if !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING { int accepted = 1; #if !LWIP_MULTICAST_PING /* multicast destination address? */ if (ip_addr_ismulticast(ip_current_dest_addr())) { accepted = 0; } #endif /* LWIP_MULTICAST_PING */ #if !LWIP_BROADCAST_PING /* broadcast destination address? */ if (ip_addr_isbroadcast(ip_current_dest_addr(), inp)) { accepted = 0; } #endif /* LWIP_BROADCAST_PING */ /* broadcast or multicast destination address not acceptd? */ if (!accepted) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to multicast or broadcast pings\n")); ICMP_STATS_INC(icmp.err); pbuf_free(p); return; } } #endif /* !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING */ LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n")); if (p->tot_len < sizeof(struct icmp_echo_hdr)) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n")); goto lenerr; } #if CHECKSUM_CHECK_ICMP if (inet_chksum_pbuf(p) != 0) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo\n")); pbuf_free(p); ICMP_STATS_INC(icmp.chkerr); snmp_inc_icmpinerrors(); return; } #endif #if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN if (pbuf_header(p, (PBUF_IP_HLEN + PBUF_LINK_HLEN))) { /* p is not big enough to contain link headers * allocate a new one and copy p into it */ struct pbuf *r; /* switch p->payload to ip header */ if (pbuf_header(p, hlen)) { LWIP_ASSERT("icmp_input: moving p->payload to ip header failed\n", 0); goto memerr; } /* allocate new packet buffer with space for link headers */ r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM); if (r == NULL) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: allocating new pbuf failed\n")); goto memerr; } LWIP_ASSERT("check that first pbuf can hold struct the ICMP header", (r->len >= hlen + sizeof(struct icmp_echo_hdr))); /* copy the whole packet including ip header */ if (pbuf_copy(r, p) != ERR_OK) { LWIP_ASSERT("icmp_input: copying to new pbuf failed\n", 0); goto memerr; } iphdr = (struct ip_hdr *)r->payload; /* switch r->payload back to icmp header */ if (pbuf_header(r, -hlen)) { LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0); goto memerr; } /* free the original p */ pbuf_free(p); /* we now have an identical copy of p that has room for link headers */ p = r; } else { /* restore p->payload to point to icmp header */ if (pbuf_header(p, -(s16_t)(PBUF_IP_HLEN + PBUF_LINK_HLEN))) { LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0); goto memerr; } } #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */ /* At this point, all checks are OK. */ /* We generate an answer by switching the dest and src ip addresses, * setting the icmp type to ECHO_RESPONSE and updating the checksum. */ iecho = (struct icmp_echo_hdr *)p->payload; ip_addr_copy(iphdr->src, *ip_current_dest_addr()); ip_addr_copy(iphdr->dest, *ip_current_src_addr()); ICMPH_TYPE_SET(iecho, ICMP_ER); #if CHECKSUM_GEN_ICMP /* adjust the checksum */ if (iecho->chksum > PP_HTONS(0xffffU - (ICMP_ECHO << 8))) { iecho->chksum += PP_HTONS(ICMP_ECHO << 8) + 1; } else { iecho->chksum += PP_HTONS(ICMP_ECHO << 8); } #else /* CHECKSUM_GEN_ICMP */ iecho->chksum = 0; #endif /* CHECKSUM_GEN_ICMP */ /* Set the correct TTL and recalculate the header checksum. */ IPH_TTL_SET(iphdr, ICMP_TTL); IPH_CHKSUM_SET(iphdr, 0); #if CHECKSUM_GEN_IP IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN)); #endif /* CHECKSUM_GEN_IP */ ICMP_STATS_INC(icmp.xmit); /* increase number of messages attempted to send */ snmp_inc_icmpoutmsgs(); /* increase number of echo replies attempted to send */ snmp_inc_icmpoutechoreps(); if(pbuf_header(p, hlen)) { LWIP_ASSERT("Can't move over header in packet", 0); } else { err_t ret; /* send an ICMP packet, src addr is the dest addr of the curren packet */ ret = ip_output_if(p, ip_current_dest_addr(), IP_HDRINCL, ICMP_TTL, 0, IP_PROTO_ICMP, inp); if (ret != ERR_OK) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ip_output_if returned an error: %c.\n", ret)); } } break; default: LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" code %"S16_F" not supported.\n", (s16_t)type, (s16_t)code)); ICMP_STATS_INC(icmp.proterr); ICMP_STATS_INC(icmp.drop); } pbuf_free(p); return; lenerr: pbuf_free(p); ICMP_STATS_INC(icmp.lenerr); snmp_inc_icmpinerrors(); return; #if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN memerr: pbuf_free(p); ICMP_STATS_INC(icmp.err); snmp_inc_icmpinerrors(); return; #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */ }
/** * Proxy ICMP input packets, called from ip_input(). * * @param p the icmp echo request packet, p->payload pointing to the icmp header * @param inp the netif on which this packet was received */ void icmp_proxy_input(struct pbuf *p, struct netif *inp) { u8_t type, code; # ifdef VBOX (void)inp; # endif ICMP_STATS_INC(icmp.recv); snmp_inc_icmpinmsgs(); if (p->tot_len < 4) { /* type(1), code(1), checksum(2) */ LWIP_DEBUGF(ICMP_DEBUG, ("icmp_proxy_input: short ICMP (%"U16_F" bytes) received\n", p->tot_len)); goto lenerr; } if (inet_chksum_pbuf(p) != 0) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_proxy_input: bad checksum\n")); pbuf_free(p); ICMP_STATS_INC(icmp.chkerr); snmp_inc_icmpinerrors(); return; } type = *((u8_t *)p->payload); code = *(((u8_t *)p->payload)+1); switch (type) { case ICMP_ER: /* ignore silently */ pbuf_free(p); break; case ICMP_DUR: /* TODO: anything useful we can do? */ pbuf_free(p); break; case ICMP_ECHO: if (code != 0) { goto proterr; } if (p->tot_len < 8) { goto lenerr; } if (ping_proxy_accept_callback != NULL) { (*ping_proxy_accept_callback)(ping_proxy_accept_arg, p); } break; default: goto proterr; } return; lenerr: ICMP_STATS_INC(icmp.lenerr); goto drop; proterr: LWIP_DEBUGF(ICMP_DEBUG, ("icmp_proxy_input: ICMP type %"S16_F" code %"S16_F " not supported.\n", (s16_t)type, (s16_t)code)); ICMP_STATS_INC(icmp.proterr); goto drop; drop: pbuf_free(p); ICMP_STATS_INC(icmp.drop); snmp_inc_icmpinerrors(); return; }
/** * Processes ICMP input packets, called from ip_input(). * * Currently only processes icmp echo requests and sends * out the echo response. * * @param p the icmp echo request packet, p->payload pointing to the ip header * @param inp the netif on which this packet was received */ void icmp_input(PBUF *p, NETIF *inp) { uint8 type; ICMP_ECHO_HDR *iecho; IP_HDR *iphdr; IP_ADDR tmpaddr; int16 hlen; iphdr = p->payload; hlen = IPH_HL(iphdr) * 4; if (pbuf_header(p, -hlen) == 0) return; type = *((uint8 *)p->payload); iecho = p->payload; printf("icmp_input %d %d\n", iecho->type, iecho->code); switch (type) { case ICMP_ECHO: { int accepted = 1; /* multicast destination address? */ if (ip_addr_ismulticast(&iphdr->dest)) { accepted = 0; } /* broadcast destination address? */ if (ip_addr_isbroadcast(&iphdr->dest, inp)) { accepted = 0; } /* broadcast or multicast destination address not acceptd? */ if (!accepted) { pbuf_free(p); return; } if (p->len < sizeof(ICMP_ECHO_HDR)) goto lenerr; if (inet_chksum_pbuf(p) != 0) { pbuf_free(p); return; } /* At this point, all checks are OK. */ /* We generate an answer by switching the dest and src ip addresses, * setting the icmp type to ECHO_RESPONSE and updating the checksum. */ tmpaddr.addr = iphdr->src.addr; iphdr->src.addr = iphdr->dest.addr; iphdr->dest.addr = tmpaddr.addr; iecho = p->payload; ICMPH_TYPE_SET(iecho, ICMP_ER); /* adjust the checksum */ if (iecho->chksum >= htons(0xffff - (ICMP_ECHO << 8))) { iecho->chksum += htons(ICMP_ECHO << 8) + 1; } else { iecho->chksum += htons(ICMP_ECHO << 8); } if (pbuf_header(p, hlen) == 0) return; ip_output_if(p, &(iphdr->src), IP_HDRINCL, ICMP_TTL, 0, IP_PROTO_ICMP, inp); break; } default: break; } lenerr: pbuf_free(p); return; }
/** * Processes ICMP input packets, called from ip_input(). * * Currently only processes icmp echo requests and sends * out the echo response. * * @param p the icmp echo request packet, p->payload pointing to the ip header * @param inp the netif on which this packet was received */ void icmp_input(struct pbuf *p, struct netif *inp) { u8_t type; #ifdef LWIP_DEBUG u8_t code; #endif /* LWIP_DEBUG */ struct icmp_echo_hdr *iecho; struct ip_hdr *iphdr; struct ip_addr tmpaddr; s16_t hlen; u8_t iptxt[20]; volatile u8_t iptab[4]; u32_t IPaddress; ICMP_STATS_INC(icmp.recv); snmp_inc_icmpinmsgs(); iphdr = p->payload; hlen = IPH_HL(iphdr) * 4; if (pbuf_header(p, -hlen) || (p->tot_len < sizeof(u16_t)*2)) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short ICMP (%"U16_F" bytes) received\n", p->tot_len)); goto lenerr; } IPaddress = iphdr->src.addr; /* read its IP address */ iptab[0] = (u8_t)(IPaddress >> 24); iptab[1] = (u8_t)(IPaddress >> 16); iptab[2] = (u8_t)(IPaddress >> 8); iptab[3] = (u8_t)(IPaddress); sprintf((char*)iptxt, "Ping: %d.%d.%d.%d ", iptab[3], iptab[2], iptab[1], iptab[0]); LCD_DisplayStringLine(Line4, iptxt); printf("\n\r%s", iptxt); type = *((u8_t *)p->payload); #ifdef LWIP_DEBUG code = *(((u8_t *)p->payload)+1); #endif /* LWIP_DEBUG */ switch (type) { case ICMP_ECHO: #if !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING { int accepted = 1; #if !LWIP_MULTICAST_PING /* multicast destination address? */ if (ip_addr_ismulticast(&iphdr->dest)) { accepted = 0; } #endif /* LWIP_MULTICAST_PING */ #if !LWIP_BROADCAST_PING /* broadcast destination address? */ if (ip_addr_isbroadcast(&iphdr->dest, inp)) { accepted = 0; } #endif /* LWIP_BROADCAST_PING */ /* broadcast or multicast destination address not acceptd? */ if (!accepted) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to multicast or broadcast pings\n")); ICMP_STATS_INC(icmp.err); pbuf_free(p); return; } } #endif /* !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING */ LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n")); if (p->tot_len < sizeof(struct icmp_echo_hdr)) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n")); goto lenerr; } if (inet_chksum_pbuf(p) != 0) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo\n")); pbuf_free(p); ICMP_STATS_INC(icmp.chkerr); snmp_inc_icmpinerrors(); return; } #if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN if (pbuf_header(p, (PBUF_IP_HLEN + PBUF_LINK_HLEN))) { /* p is not big enough to contain link headers * allocate a new one and copy p into it */ struct pbuf *r; /* switch p->payload to ip header */ if (pbuf_header(p, hlen)) { LWIP_ASSERT("icmp_input: moving p->payload to ip header failed\n", 0); goto memerr; } /* allocate new packet buffer with space for link headers */ r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM); if (r == NULL) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: allocating new pbuf failed\n")); goto memerr; } LWIP_ASSERT("check that first pbuf can hold struct the ICMP header", (r->len >= hlen + sizeof(struct icmp_echo_hdr))); /* copy the whole packet including ip header */ if (pbuf_copy(r, p) != ERR_OK) { LWIP_ASSERT("icmp_input: copying to new pbuf failed\n", 0); goto memerr; } iphdr = r->payload; /* switch r->payload back to icmp header */ if (pbuf_header(r, -hlen)) { LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0); goto memerr; } /* free the original p */ pbuf_free(p); /* we now have an identical copy of p that has room for link headers */ p = r; } else { /* restore p->payload to point to icmp header */ if (pbuf_header(p, -(s16_t)(PBUF_IP_HLEN + PBUF_LINK_HLEN))) { LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0); goto memerr; } } #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */ /* At this point, all checks are OK. */ /* We generate an answer by switching the dest and src ip addresses, * setting the icmp type to ECHO_RESPONSE and updating the checksum. */ iecho = p->payload; tmpaddr.addr = iphdr->src.addr; iphdr->src.addr = iphdr->dest.addr; iphdr->dest.addr = tmpaddr.addr; ICMPH_TYPE_SET(iecho, ICMP_ER); /* This part of code has been modified by ST's MCD Application Team */ /* To use the Checksum Offload Engine for the putgoing ICMP packets, the ICMP checksum field should be set to 0, this is required only for Tx ICMP*/ #ifdef CHECKSUM_BY_HARDWARE iecho->chksum = 0; #else /* adjust the checksum */ if (iecho->chksum >= htons(0xffff - (ICMP_ECHO << 8))) { iecho->chksum += htons(ICMP_ECHO << 8) + 1; } else { iecho->chksum += htons(ICMP_ECHO << 8); } #endif /* Set the correct TTL and recalculate the header checksum. */ IPH_TTL_SET(iphdr, ICMP_TTL); IPH_CHKSUM_SET(iphdr, 0); #if CHECKSUM_GEN_IP IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN)); #endif /* CHECKSUM_GEN_IP */ ICMP_STATS_INC(icmp.xmit); /* increase number of messages attempted to send */ snmp_inc_icmpoutmsgs(); /* increase number of echo replies attempted to send */ snmp_inc_icmpoutechoreps(); if(pbuf_header(p, hlen)) { LWIP_ASSERT("Can't move over header in packet", 0); } else { err_t ret; ret = ip_output_if(p, &(iphdr->src), IP_HDRINCL, ICMP_TTL, 0, IP_PROTO_ICMP, inp); if (ret != ERR_OK) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ip_output_if returned an error: %c.\n", ret)); } } break; default: LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" code %"S16_F" not supported.\n", (s16_t)type, (s16_t)code)); ICMP_STATS_INC(icmp.proterr); ICMP_STATS_INC(icmp.drop); } pbuf_free(p); return; lenerr: pbuf_free(p); ICMP_STATS_INC(icmp.lenerr); snmp_inc_icmpinerrors(); return; #if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN memerr: pbuf_free(p); ICMP_STATS_INC(icmp.err); snmp_inc_icmpinerrors(); return; #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */ }
/*-----------------------------------------------------------------------------------*/ void icmp_input(struct pbuf *p, struct netif *inp) { unsigned char type; struct icmp_echo_hdr *iecho; struct ip_hdr *iphdr; struct ip_addr tmpaddr; #ifdef ICMP_STATS ++stats.icmp.recv; #endif /* ICMP_STATS */ type = ((char *)p->payload)[0]; switch(type) { case ICMP6_ECHO: DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n")); if(p->tot_len < sizeof(struct icmp_echo_hdr)) { DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n")); pbuf_free(p); #ifdef ICMP_STATS ++stats.icmp.lenerr; #endif /* ICMP_STATS */ return; } iecho = p->payload; iphdr = (struct ip_hdr *)((char *)p->payload - IP_HLEN); if(inet_chksum_pbuf(p) != 0) { DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo (%x)\n", inet_chksum_pseudo(p, &(iphdr->src), &(iphdr->dest), IP_PROTO_ICMP, p->tot_len))); #ifdef ICMP_STATS ++stats.icmp.chkerr; #endif /* ICMP_STATS */ /* return;*/ } DEBUGF(ICMP_DEBUG, ("icmp: p->len %d p->tot_len %d\n", p->len, p->tot_len)); ip_addr_set(&tmpaddr, &(iphdr->src)); ip_addr_set(&(iphdr->src), &(iphdr->dest)); ip_addr_set(&(iphdr->dest), &tmpaddr); iecho->type = ICMP6_ER; /* adjust the checksum */ if(iecho->chksum >= htons(0xffff - (ICMP6_ECHO << 8))) { iecho->chksum += htons(ICMP6_ECHO << 8) + 1; } else { iecho->chksum += htons(ICMP6_ECHO << 8); } DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo (%x)\n", inet_chksum_pseudo(p, &(iphdr->src), &(iphdr->dest), IP_PROTO_ICMP, p->tot_len))); #ifdef ICMP_STATS ++stats.icmp.xmit; #endif /* ICMP_STATS */ /* DEBUGF("icmp: p->len %d p->tot_len %d\n", p->len, p->tot_len);*/ ip_output_if(p, &(iphdr->src), IP_HDRINCL, iphdr->hoplim, IP_PROTO_ICMP, inp); break; default: DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type not supported.\n")); #ifdef ICMP_STATS ++stats.icmp.proterr; ++stats.icmp.drop; #endif /* ICMP_STATS */ } pbuf_free(p); }
/*-----------------------------------------------------------------------------------*/ void icmp_input(struct pbuf *p, struct netif *inp) { unsigned char type; struct icmp_echo_hdr *iecho; struct ip_hdr *iphdr; struct ip_addr tmpaddr; uint16_t hlen; #ifdef ICMP_STATS ++stats.icmp.recv; #endif /* ICMP_STATS */ iphdr = p->payload; hlen = IPH_HL(iphdr) * 4/sizeof(uint8_t); pbuf_header(p, -hlen); type = *((uint8_t *)p->payload); switch(type) { case ICMP_ECHO: if(ip_addr_isbroadcast(&iphdr->dest, &inp->netmask) || ip_addr_ismulticast(&iphdr->dest)) { DEBUGF(ICMP_DEBUG, ("Smurf.\n")); #ifdef ICMP_STATS ++stats.icmp.err; #endif /* ICMP_STATS */ pbuf_free(p); return; } DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n")); DEBUGF(DEMO_DEBUG, ("Pong!\n")); if(p->tot_len < sizeof(struct icmp_echo_hdr)) { DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n")); pbuf_free(p); #ifdef ICMP_STATS ++stats.icmp.lenerr; #endif /* ICMP_STATS */ return; } iecho = p->payload; if(inet_chksum_pbuf(p) != 0) { DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo\n")); pbuf_free(p); #ifdef ICMP_STATS ++stats.icmp.chkerr; #endif /* ICMP_STATS */ return; } tmpaddr.addr = iphdr->src.addr; iphdr->src.addr = iphdr->dest.addr; iphdr->dest.addr = tmpaddr.addr; ICMPH_TYPE_SET(iecho, ICMP_ER); /* adjust the checksum */ if(iecho->chksum >= htons(0xffff - (ICMP_ECHO << 8))) { iecho->chksum += htons(ICMP_ECHO << 8) + 1; } else { iecho->chksum += htons(ICMP_ECHO << 8); } #ifdef ICMP_STATS ++stats.icmp.xmit; #endif /* ICMP_STATS */ pbuf_header(p, hlen); /* XXX .mc */ assert(0); /*sr_lwip_output(p, IP_HDRINCL, IP_PROTO_ICMP);*/ break; default: DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type not supported.\n")); #ifdef ICMP_STATS ++stats.icmp.proterr; ++stats.icmp.drop; #endif /* ICMP_STATS */ } pbuf_free(p); }
/* 调用途径:ethernetif_input() -> ethernet_input() -> ip_input() -> icmp_input()*/ void icmp_input(struct pbuf *p, struct netif *inp) { u8_t type; #ifdef LWIP_DEBUG u8_t code; #endif /* LWIP_DEBUG */ struct icmp_echo_hdr *iecho; struct ip_hdr *iphdr; /* IP 地址 */ struct ip_addr tmpaddr; s16_t hlen; ICMP_STATS_INC(icmp.recv); snmp_inc_icmpinmsgs(); /* 指向IP首部 */ iphdr = p->payload; /* 获得IP报头长度 */ hlen = IPH_HL(iphdr) * 4; /* pbuf的payload向后移动到IP载荷,即ICMP报头处 * 如果IP的载荷小于4字节,则跳到lenerr处,释放pbuf */ if (pbuf_header(p, -hlen) || (p->tot_len < sizeof(u16_t)*2)) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short ICMP (%"U16_F" bytes) received\n", p->tot_len)); goto lenerr; } /* 获取ICMP报头中的类型 */ type = *((u8_t *)p->payload); #ifdef LWIP_DEBUG code = *(((u8_t *)p->payload)+1); #endif /* LWIP_DEBUG */ switch (type) { /* 如果ICMP类型是回显请求 */ case ICMP_ECHO: /* 先检查目的IP地址是否合法 */ #if !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING { /* accepted表示是否对ICMP回显请求进行回应 */ int accepted = 1; #if !LWIP_MULTICAST_PING /* multicast destination address? */ /* 如果目的IP地址是多播地址,则不回应 */ if (ip_addr_ismulticast(&iphdr->dest)) { accepted = 0; } #endif /* LWIP_MULTICAST_PING */ #if !LWIP_BROADCAST_PING /* broadcast destination address? */ /* 如果目的IP地址是广播地址,则不回应 */ if (ip_addr_isbroadcast(&iphdr->dest, inp)) { accepted = 0; } #endif /* LWIP_BROADCAST_PING */ /* broadcast or multicast destination address not acceptd? */ /* 如果不回应,则释放pbuf后,返回 */ if (!accepted) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to multicast or broadcast pings\n")); ICMP_STATS_INC(icmp.err); pbuf_free(p); return; } } #endif /* !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING */ LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n")); /* 检查ICMP报文长度是否合法,ICMP报文总长度不能小于ICMP报头长度8字节 */ if (p->tot_len < sizeof(struct icmp_echo_hdr)) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n")); /* 跳到lenerr处执行返回操作 */ goto lenerr; } /* 计算ICMP的校验和是否正确 */ if (inet_chksum_pbuf(p) != 0) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo\n")); /* ICMP校验和错误,则释放pbuf,并返回 */ pbuf_free(p); ICMP_STATS_INC(icmp.chkerr); snmp_inc_icmpinerrors(); return; } #if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN if (pbuf_header(p, (PBUF_IP_HLEN + PBUF_LINK_HLEN))) { /* p is not big enough to contain link headers * allocate a new one and copy p into it */ struct pbuf *r; /* switch p->payload to ip header */ if (pbuf_header(p, hlen)) { LWIP_ASSERT("icmp_input: moving p->payload to ip header failed\n", 0); goto memerr; } /* allocate new packet buffer with space for link headers */ r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM); if (r == NULL) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: allocating new pbuf failed\n")); goto memerr; } LWIP_ASSERT("check that first pbuf can hold struct the ICMP header", (r->len >= hlen + sizeof(struct icmp_echo_hdr))); /* copy the whole packet including ip header */ if (pbuf_copy(r, p) != ERR_OK) { LWIP_ASSERT("icmp_input: copying to new pbuf failed\n", 0); goto memerr; } iphdr = r->payload; /* switch r->payload back to icmp header */ if (pbuf_header(r, -hlen)) { LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0); goto memerr; } /* free the original p */ pbuf_free(p); /* we now have an identical copy of p that has room for link headers */ p = r; } else { /* restore p->payload to point to icmp header */ if (pbuf_header(p, -(s16_t)(PBUF_IP_HLEN + PBUF_LINK_HLEN))) { LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0); goto memerr; } } #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */ /* At this point, all checks are OK. */ /* We generate an answer by switching the dest and src ip addresses, * setting the icmp type to ECHO_RESPONSE and updating the checksum. */ /* 校验完成,调整ICMP回显请求的相关字段,生成回显应答 * 交换IP数据报的源IP和目的IP地址,填写ICMP报文的类型字段,并重新计算ICMP的校验和 */ /* 获取ICMP报头指针 */ iecho = p->payload; /* 交换IP报头的源IP地址和目的IP地址 */ tmpaddr.addr = iphdr->src.addr; iphdr->src.addr = iphdr->dest.addr; iphdr->dest.addr = tmpaddr.addr; /* 设置ICMP报文类型为回显应答 */ ICMPH_TYPE_SET(iecho, ICMP_ER); /* adjust the checksum */ /* 调整ICMP的校验和 */ if (iecho->chksum >= htons(0xffff - (ICMP_ECHO << 8))) { iecho->chksum += htons(ICMP_ECHO << 8) + 1; } else { iecho->chksum += htons(ICMP_ECHO << 8); } /* Set the correct TTL and recalculate the header checksum. */ /* 设置IP首部的TTL */ IPH_TTL_SET(iphdr, ICMP_TTL); /* 计算IP首部校验和 */ IPH_CHKSUM_SET(iphdr, 0); #if CHECKSUM_GEN_IP IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN)); #endif /* CHECKSUM_GEN_IP */ /* 注意:这里没有修改IP首部的标识字段,所以ICMP回显应答的IP首部的标识字段和 * ICMP回显请求的标识字段是相同的。理论上来说ICMP回显应答的IP首部标识字段应该 * 被修改,但为什么不修改? * 对Linux主机进行ping,Linux主机回复的ICMP回显应答的IP首部的标识字段就修改了 */ ICMP_STATS_INC(icmp.xmit); /* increase number of messages attempted to send */ snmp_inc_icmpoutmsgs(); /* increase number of echo replies attempted to send */ snmp_inc_icmpoutechoreps(); /* pbuf的payload由ICMP的报头移动到IP的报头,hlen保存了IP报头的长度 */ if(pbuf_header(p, hlen)) { LWIP_ASSERT("Can't move over header in packet", 0); /* 移动失败 */ } else { /* 移动成功 */ err_t ret; /* 调用ip_output_if发送IP数据报,IP_HDRINCL表示IP首部已经填写好,并且 * pbuf的payload指向IP数据报首部,而不是IP载荷首部 */ ret = ip_output_if(p, &(iphdr->src), IP_HDRINCL, ICMP_TTL, 0, IP_PROTO_ICMP, inp); if (ret != ERR_OK) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ip_output_if returned an error: %c.\n", ret)); } } break; /* 如果ICMP类型不是ICMP回显请求,则直接忽略 */ default: LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" code %"S16_F" not supported.\n", (s16_t)type, (s16_t)code)); /* 更新统计量 */ ICMP_STATS_INC(icmp.proterr); ICMP_STATS_INC(icmp.drop); } /* 释放pbuf */ pbuf_free(p); return; lenerr: pbuf_free(p); ICMP_STATS_INC(icmp.lenerr); snmp_inc_icmpinerrors(); return; #if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN memerr: pbuf_free(p); ICMP_STATS_INC(icmp.err); snmp_inc_icmpinerrors(); return; #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */ }
void icmp_input(struct pbuf *p, struct netif *inp) { u8_t type; u8_t code; struct icmp_echo_hdr *iecho; struct ip_hdr *iphdr; struct ip_addr tmpaddr; u16_t hlen; u8_t *payload = NULL; ICMP_STATS_INC(icmp.recv); snmp_inc_icmpinmsgs(); iphdr = p->payload; hlen = IPH_HL(iphdr) * 4; if (p->tot_len < sizeof(u16_t)*2) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short ICMP (%"U16_F" bytes) received\n", p->tot_len)); pbuf_free(p); ICMP_STATS_INC(icmp.lenerr); snmp_inc_icmpinerrors(); return; } payload = (u8_t *)p->payload + hlen; type = *payload; code = *(payload+1); switch (type) { case ICMP_ECHO: /* broadcast or multicast destination address? */ if (ip_addr_isbroadcast(&iphdr->dest, inp) || ip_addr_ismulticast(&iphdr->dest)) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to multicast or broadcast pings\n")); ICMP_STATS_INC(icmp.err); pbuf_free(p); return; } LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n")); if (p->tot_len < sizeof(struct icmp_echo_hdr)) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n")); pbuf_free(p); ICMP_STATS_INC(icmp.lenerr); snmp_inc_icmpinerrors(); return; } iecho = (struct icmp_echo_hdr *)payload; if (inet_chksum_pbuf(p) != 0) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo\n")); pbuf_free(p); ICMP_STATS_INC(icmp.chkerr); snmp_inc_icmpinerrors(); return; } tmpaddr.addr = iphdr->src.addr; iphdr->src.addr = iphdr->dest.addr; iphdr->dest.addr = tmpaddr.addr; ICMPH_TYPE_SET(iecho, ICMP_ER); /* adjust the checksum */ if (iecho->chksum >= htons(0xffff - (ICMP_ECHO << 8))) { iecho->chksum += htons(ICMP_ECHO << 8) + 1; } else { iecho->chksum += htons(ICMP_ECHO << 8); } ICMP_STATS_INC(icmp.xmit); /* increase number of messages attempted to send */ snmp_inc_icmpoutmsgs(); /* increase number of echo replies attempted to send */ snmp_inc_icmpoutechoreps(); pbuf_header(p, hlen); ip_output_if(p, &(iphdr->src), IP_HDRINCL, IPH_TTL(iphdr), 0, IP_PROTO_ICMP, inp); break; default: LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" code %"S16_F" not supported.\n", (s16_t)type, (s16_t)code)); ICMP_STATS_INC(icmp.proterr); ICMP_STATS_INC(icmp.drop); } pbuf_free(p); }
/** * Processes ICMP input packets, called from ip_input(). * * Currently only processes icmp echo requests and sends * out the echo response. * * @param p the icmp echo request packet, p->payload pointing to the icmp header * @param inp the netif on which this packet was received */ void icmp_input(struct pbuf *p, struct netif *inp) { u8_t type; #ifdef LWIP_DEBUG u8_t code; #endif /* LWIP_DEBUG */ struct icmp_echo_hdr *iecho; const struct ip_hdr *iphdr_in; s16_t hlen; const ip4_addr_t* src; ICMP_STATS_INC(icmp.recv); MIB2_STATS_INC(mib2.icmpinmsgs); iphdr_in = ip4_current_header(); hlen = IPH_HL(iphdr_in) * 4; if (hlen < IP_HLEN) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short IP header (%"S16_F" bytes) received\n", hlen)); goto lenerr; } if (p->len < sizeof(u16_t)*2) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short ICMP (%"U16_F" bytes) received\n", p->tot_len)); goto lenerr; } type = *((u8_t *)p->payload); #ifdef LWIP_DEBUG code = *(((u8_t *)p->payload)+1); #endif /* LWIP_DEBUG */ switch (type) { case ICMP_ER: /* This is OK, echo reply might have been parsed by a raw PCB (as obviously, an echo request has been sent, too). */ MIB2_STATS_INC(mib2.icmpinechoreps); break; case ICMP_ECHO: MIB2_STATS_INC(mib2.icmpinechos); src = ip4_current_dest_addr(); /* multicast destination address? */ if (ip4_addr_ismulticast(ip4_current_dest_addr())) { #if LWIP_MULTICAST_PING /* For multicast, use address of receiving interface as source address */ src = netif_ip4_addr(inp); #else /* LWIP_MULTICAST_PING */ LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to multicast pings\n")); goto icmperr; #endif /* LWIP_MULTICAST_PING */ } /* broadcast destination address? */ if (ip4_addr_isbroadcast(ip4_current_dest_addr(), ip_current_netif())) { #if LWIP_BROADCAST_PING /* For broadcast, use address of receiving interface as source address */ src = netif_ip4_addr(inp); #else /* LWIP_BROADCAST_PING */ LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to broadcast pings\n")); goto icmperr; #endif /* LWIP_BROADCAST_PING */ } LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n")); if (p->tot_len < sizeof(struct icmp_echo_hdr)) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n")); goto lenerr; } #if CHECKSUM_CHECK_ICMP IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_ICMP) { if (inet_chksum_pbuf(p) != 0) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo\n")); pbuf_free(p); ICMP_STATS_INC(icmp.chkerr); MIB2_STATS_INC(mib2.icmpinerrors); return; } } #endif #if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN if (pbuf_header(p, (hlen + PBUF_LINK_HLEN + PBUF_LINK_ENCAPSULATION_HLEN))) { /* p is not big enough to contain link headers * allocate a new one and copy p into it */ struct pbuf *r; /* allocate new packet buffer with space for link headers */ r = pbuf_alloc(PBUF_LINK, p->tot_len + hlen, PBUF_RAM); if (r == NULL) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: allocating new pbuf failed\n")); goto icmperr; } if (r->len < hlen + sizeof(struct icmp_echo_hdr)) { LWIP_DEBUGF(ICMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("first pbuf cannot hold the ICMP header")); pbuf_free(r); goto icmperr; } /* copy the ip header */ MEMCPY(r->payload, iphdr_in, hlen); /* switch r->payload back to icmp header (cannot fail) */ if (pbuf_header(r, -hlen)) { LWIP_ASSERT("icmp_input: moving r->payload to icmp header failed\n", 0); pbuf_free(r); goto icmperr; } /* copy the rest of the packet without ip header */ if (pbuf_copy(r, p) != ERR_OK) { LWIP_DEBUGF(ICMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("icmp_input: copying to new pbuf failed")); pbuf_free(r); goto icmperr; } /* free the original p */ pbuf_free(p); /* we now have an identical copy of p that has room for link headers */ p = r; } else { /* restore p->payload to point to icmp header (cannot fail) */ if (pbuf_header(p, -(s16_t)(hlen + PBUF_LINK_HLEN + PBUF_LINK_ENCAPSULATION_HLEN))) { LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0); goto icmperr; } } #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */ /* At this point, all checks are OK. */ /* We generate an answer by switching the dest and src ip addresses, * setting the icmp type to ECHO_RESPONSE and updating the checksum. */ iecho = (struct icmp_echo_hdr *)p->payload; if (pbuf_header(p, hlen)) { LWIP_DEBUGF(ICMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Can't move over header in packet")); } else { err_t ret; struct ip_hdr *iphdr = (struct ip_hdr*)p->payload; ip4_addr_copy(iphdr->src, *src); ip4_addr_copy(iphdr->dest, *ip4_current_src_addr()); ICMPH_TYPE_SET(iecho, ICMP_ER); #if CHECKSUM_GEN_ICMP IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_GEN_ICMP) { /* adjust the checksum */ if (iecho->chksum > PP_HTONS(0xffffU - (ICMP_ECHO << 8))) { iecho->chksum += PP_HTONS(ICMP_ECHO << 8) + 1; } else { iecho->chksum += PP_HTONS(ICMP_ECHO << 8); } } #if LWIP_CHECKSUM_CTRL_PER_NETIF else { iecho->chksum = 0; } #endif /* LWIP_CHECKSUM_CTRL_PER_NETIF */ #else /* CHECKSUM_GEN_ICMP */ iecho->chksum = 0; #endif /* CHECKSUM_GEN_ICMP */ /* Set the correct TTL and recalculate the header checksum. */ IPH_TTL_SET(iphdr, ICMP_TTL); IPH_CHKSUM_SET(iphdr, 0); #if CHECKSUM_GEN_IP IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_GEN_IP) { IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, hlen)); } #endif /* CHECKSUM_GEN_IP */ ICMP_STATS_INC(icmp.xmit); /* increase number of messages attempted to send */ MIB2_STATS_INC(mib2.icmpoutmsgs); /* increase number of echo replies attempted to send */ MIB2_STATS_INC(mib2.icmpoutechoreps); /* send an ICMP packet */ ret = ip4_output_if(p, src, IP_HDRINCL, ICMP_TTL, 0, IP_PROTO_ICMP, inp); if (ret != ERR_OK) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ip_output_if returned an error: %s\n", lwip_strerr(ret))); } }