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