Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without modification, |
| 6 | * are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 19 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 21 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 24 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
| 25 | * OF SUCH DAMAGE. |
| 26 | * |
| 27 | * This file is part of the lwIP TCP/IP stack. |
| 28 | * |
| 29 | * Author: Adam Dunkels <adam@sics.se> |
| 30 | * |
| 31 | */ |
| 32 | |
| 33 | /* Some ICMP messages should be passed to the transport protocols. This |
| 34 | is not implemented. */ |
| 35 | |
| 36 | #include "lwip/opt.h" |
| 37 | |
| 38 | #include "lwip/icmp.h" |
| 39 | #include "lwip/inet.h" |
| 40 | #include "lwip/ip.h" |
| 41 | #include "lwip/def.h" |
| 42 | |
| 43 | #include "lwip/stats.h" |
| 44 | |
| 45 | |
| 46 | void |
| 47 | icmp_input(struct pbuf *p, struct netif *inp) |
| 48 | { |
| 49 | u8_t type; |
| 50 | struct icmp_echo_hdr *iecho; |
| 51 | struct ip_hdr *iphdr; |
| 52 | struct ip_addr tmpaddr; |
| 53 | |
| 54 | #ifdef ICMP_STATS |
| 55 | ++lwip_stats.icmp.recv; |
| 56 | #endif /* ICMP_STATS */ |
| 57 | |
| 58 | /* TODO: check length before accessing payload! */ |
| 59 | |
| 60 | type = ((u8_t *)p->payload)[0]; |
| 61 | |
| 62 | switch (type) { |
| 63 | case ICMP6_ECHO: |
| 64 | LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n")); |
| 65 | |
| 66 | if (p->tot_len < sizeof(struct icmp_echo_hdr)) { |
| 67 | LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n")); |
| 68 | |
| 69 | pbuf_free(p); |
| 70 | #ifdef ICMP_STATS |
| 71 | ++lwip_stats.icmp.lenerr; |
| 72 | #endif /* ICMP_STATS */ |
| 73 | |
| 74 | return; |
| 75 | } |
| 76 | iecho = p->payload; |
| 77 | iphdr = (struct ip_hdr *)((u8_t *)p->payload - IP_HLEN); |
| 78 | if (inet_chksum_pbuf(p) != 0) { |
| 79 | 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))); |
| 80 | |
| 81 | #ifdef ICMP_STATS |
| 82 | ++lwip_stats.icmp.chkerr; |
| 83 | #endif /* ICMP_STATS */ |
| 84 | /* return;*/ |
| 85 | } |
| 86 | LWIP_DEBUGF(ICMP_DEBUG, ("icmp: p->len %"S16_F" p->tot_len %"S16_F"\n", p->len, p->tot_len)); |
| 87 | ip_addr_set(&tmpaddr, &(iphdr->src)); |
| 88 | ip_addr_set(&(iphdr->src), &(iphdr->dest)); |
| 89 | ip_addr_set(&(iphdr->dest), &tmpaddr); |
| 90 | iecho->type = ICMP6_ER; |
| 91 | /* adjust the checksum */ |
| 92 | if (iecho->chksum >= htons(0xffff - (ICMP6_ECHO << 8))) { |
| 93 | iecho->chksum += htons(ICMP6_ECHO << 8) + 1; |
| 94 | } else { |
| 95 | iecho->chksum += htons(ICMP6_ECHO << 8); |
| 96 | } |
| 97 | 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))); |
| 98 | #ifdef ICMP_STATS |
| 99 | ++lwip_stats.icmp.xmit; |
| 100 | #endif /* ICMP_STATS */ |
| 101 | |
| 102 | /* LWIP_DEBUGF("icmp: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len);*/ |
| 103 | ip_output_if (p, &(iphdr->src), IP_HDRINCL, |
| 104 | iphdr->hoplim, IP_PROTO_ICMP, inp); |
| 105 | break; |
| 106 | default: |
| 107 | LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" not supported.\n", (s16_t)type)); |
| 108 | #ifdef ICMP_STATS |
| 109 | ++lwip_stats.icmp.proterr; |
| 110 | ++lwip_stats.icmp.drop; |
| 111 | #endif /* ICMP_STATS */ |
| 112 | } |
| 113 | |
| 114 | pbuf_free(p); |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | icmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t) |
| 119 | { |
| 120 | struct pbuf *q; |
| 121 | struct ip_hdr *iphdr; |
| 122 | struct icmp_dur_hdr *idur; |
| 123 | |
| 124 | q = pbuf_alloc(PBUF_IP, 8 + IP_HLEN + 8, PBUF_RAM); |
| 125 | /* ICMP header + IP header + 8 bytes of data */ |
| 126 | |
| 127 | iphdr = p->payload; |
| 128 | |
| 129 | idur = q->payload; |
| 130 | idur->type = (u8_t)ICMP6_DUR; |
| 131 | idur->icode = (u8_t)t; |
| 132 | |
| 133 | memcpy((u8_t *)q->payload + 8, p->payload, IP_HLEN + 8); |
| 134 | |
| 135 | /* calculate checksum */ |
| 136 | idur->chksum = 0; |
| 137 | idur->chksum = inet_chksum(idur, q->len); |
| 138 | #ifdef ICMP_STATS |
| 139 | ++lwip_stats.icmp.xmit; |
| 140 | #endif /* ICMP_STATS */ |
| 141 | |
| 142 | ip_output(q, NULL, |
| 143 | (struct ip_addr *)&(iphdr->src), ICMP_TTL, IP_PROTO_ICMP); |
| 144 | pbuf_free(q); |
| 145 | } |
| 146 | |
| 147 | void |
| 148 | icmp_time_exceeded(struct pbuf *p, enum icmp_te_type t) |
| 149 | { |
| 150 | struct pbuf *q; |
| 151 | struct ip_hdr *iphdr; |
| 152 | struct icmp_te_hdr *tehdr; |
| 153 | |
| 154 | LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded\n")); |
| 155 | |
| 156 | q = pbuf_alloc(PBUF_IP, 8 + IP_HLEN + 8, PBUF_RAM); |
| 157 | |
| 158 | iphdr = p->payload; |
| 159 | |
| 160 | tehdr = q->payload; |
| 161 | tehdr->type = (u8_t)ICMP6_TE; |
| 162 | tehdr->icode = (u8_t)t; |
| 163 | |
| 164 | /* copy fields from original packet */ |
| 165 | memcpy((u8_t *)q->payload + 8, (u8_t *)p->payload, IP_HLEN + 8); |
| 166 | |
| 167 | /* calculate checksum */ |
| 168 | tehdr->chksum = 0; |
| 169 | tehdr->chksum = inet_chksum(tehdr, q->len); |
| 170 | #ifdef ICMP_STATS |
| 171 | ++lwip_stats.icmp.xmit; |
| 172 | #endif /* ICMP_STATS */ |
| 173 | ip_output(q, NULL, |
| 174 | (struct ip_addr *)&(iphdr->src), ICMP_TTL, IP_PROTO_ICMP); |
| 175 | pbuf_free(q); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |