Contiki 3.x
uip-icmp6.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2003, Adam Dunkels.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote
14  * products derived from this software without specific prior
15  * written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * This file is part of the uIP TCP/IP stack.
30  *
31  */
32 
33 /**
34  * \addtogroup uip6
35  * @{
36  */
37 
38 /**
39  * \file
40  * ICMPv6 (RFC 4443) implementation, with message and error handling
41  * \author Julien Abeille <jabeille@cisco.com>
42  * \author Mathilde Durvy <mdurvy@cisco.com>
43  */
44 
45 #include <string.h>
46 #include "net/ipv6/uip-ds6.h"
47 #include "net/ipv6/uip-icmp6.h"
48 #include "contiki-default-conf.h"
49 
50 #define DEBUG 0
51 #if DEBUG
52 #include <stdio.h>
53 #define PRINTF(...) printf(__VA_ARGS__)
54 #define PRINT6ADDR(addr) PRINTF(" %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x ", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15])
55 #define PRINTLLADDR(lladdr) PRINTF(" %02x:%02x:%02x:%02x:%02x:%02x ",lladdr->addr[0], lladdr->addr[1], lladdr->addr[2], lladdr->addr[3],lladdr->addr[4], lladdr->addr[5])
56 #else
57 #define PRINTF(...)
58 #define PRINT6ADDR(addr)
59 #endif
60 
61 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
62 #define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len])
63 #define UIP_ICMP6_ERROR_BUF ((struct uip_icmp6_error *)&uip_buf[uip_l2_l3_icmp_hdr_len])
64 #define UIP_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[uip_l2_l3_hdr_len])
65 #define UIP_FIRST_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[UIP_LLIPH_LEN])
66 
67 #if UIP_CONF_IPV6_RPL
68 #include "rpl/rpl.h"
69 #endif /* UIP_CONF_IPV6_RPL */
70 
71 /** \brief temporary IP address */
72 static uip_ipaddr_t tmp_ipaddr;
73 
74 LIST(echo_reply_callback_list);
75 /*---------------------------------------------------------------------------*/
76 /* List of input handlers */
77 LIST(input_handler_list);
78 /*---------------------------------------------------------------------------*/
79 static uip_icmp6_input_handler_t *
80 input_handler_lookup(uint8_t type, uint8_t icode)
81 {
82  uip_icmp6_input_handler_t *handler = NULL;
83 
84  for(handler = list_head(input_handler_list);
85  handler != NULL;
86  handler = list_item_next(handler)) {
87  if(handler->type == type &&
88  (handler->icode == icode ||
89  handler->icode == UIP_ICMP6_HANDLER_CODE_ANY)) {
90  return handler;
91  }
92  }
93 
94  return NULL;
95 }
96 /*---------------------------------------------------------------------------*/
97 uint8_t
98 uip_icmp6_input(uint8_t type, uint8_t icode)
99 {
100  uip_icmp6_input_handler_t *handler = input_handler_lookup(type, icode);
101 
102  if(handler == NULL) {
103  return UIP_ICMP6_INPUT_ERROR;
104  }
105 
106  if(handler->handler == NULL) {
107  return UIP_ICMP6_INPUT_ERROR;
108  }
109 
110  handler->handler();
111  return UIP_ICMP6_INPUT_SUCCESS;
112 }
113 /*---------------------------------------------------------------------------*/
114 void
115 uip_icmp6_register_input_handler(uip_icmp6_input_handler_t *handler)
116 {
117  list_add(input_handler_list, handler);
118 }
119 /*---------------------------------------------------------------------------*/
120 static void
121 echo_request_input(void)
122 {
123  /*
124  * we send an echo reply. It is trivial if there was no extension
125  * headers in the request otherwise we need to remove the extension
126  * headers and change a few fields
127  */
128  PRINTF("Received Echo Request from ");
129  PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
130  PRINTF(" to ");
131  PRINT6ADDR(&UIP_IP_BUF->destipaddr);
132  PRINTF("\n");
133 
134  /* IP header */
135  UIP_IP_BUF->ttl = uip_ds6_if.cur_hop_limit;
136 
137  if(uip_is_addr_mcast(&UIP_IP_BUF->destipaddr)){
138  uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &UIP_IP_BUF->srcipaddr);
139  uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr);
140  } else {
141  uip_ipaddr_copy(&tmp_ipaddr, &UIP_IP_BUF->srcipaddr);
142  uip_ipaddr_copy(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr);
143  uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &tmp_ipaddr);
144  }
145 
146  if(uip_ext_len > 0) {
147  /* Remove extension headers if any */
148  UIP_IP_BUF->proto = UIP_PROTO_ICMP6;
149  uip_len -= uip_ext_len;
150  UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
151  UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
152  /* move the echo request payload (starting after the icmp header)
153  * to the new location in the reply.
154  * The shift is equal to the length of the extension headers present
155  * Note: UIP_ICMP_BUF still points to the echo request at this stage
156  */
157  memmove((uint8_t *)UIP_ICMP_BUF + UIP_ICMPH_LEN - uip_ext_len,
158  (uint8_t *)UIP_ICMP_BUF + UIP_ICMPH_LEN,
159  (uip_len - UIP_IPH_LEN - UIP_ICMPH_LEN));
160  uip_ext_len = 0;
161  }
162 
163  /* Insert RPL extension headers */
164 #if UIP_CONF_IPV6_RPL
165  rpl_insert_header();
166 #endif /* UIP_CONF_IPV6_RPL */
167 
168  /* Below is important for the correctness of UIP_ICMP_BUF and the
169  * checksum
170  */
171 
172  /* Note: now UIP_ICMP_BUF points to the beginning of the echo reply */
174  UIP_ICMP_BUF->icode = 0;
175  UIP_ICMP_BUF->icmpchksum = 0;
176  UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum();
177 
178  PRINTF("Sending Echo Reply to ");
179  PRINT6ADDR(&UIP_IP_BUF->destipaddr);
180  PRINTF(" from ");
181  PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
182  PRINTF("\n");
183  UIP_STAT(++uip_stat.icmp.sent);
184  return;
185 }
186 /*---------------------------------------------------------------------------*/
187 void
188 uip_icmp6_error_output(uint8_t type, uint8_t code, uint32_t param) {
189  /* check if originating packet is not an ICMP error */
190  if(uip_ext_len) {
191  if(UIP_EXT_BUF->next == UIP_PROTO_ICMP6 && UIP_ICMP_BUF->type < 128) {
192  uip_clear_buf();
193  return;
194  }
195  } else {
196  if(UIP_IP_BUF->proto == UIP_PROTO_ICMP6 && UIP_ICMP_BUF->type < 128) {
197  uip_clear_buf();
198  return;
199  }
200  }
201 
202 #if UIP_CONF_IPV6_RPL
203  rpl_remove_header();
204 #else
205  uip_ext_len = 0;
206 #endif /* UIP_CONF_IPV6_RPL */
207 
208  /* remember data of original packet before shifting */
209  uip_ipaddr_copy(&tmp_ipaddr, &UIP_IP_BUF->destipaddr);
210 
211  uip_len += UIP_IPICMPH_LEN + UIP_ICMP6_ERROR_LEN;
212 
213  if(uip_len > UIP_LINK_MTU) {
215  }
216 
217  memmove((uint8_t *)UIP_ICMP6_ERROR_BUF + uip_ext_len + UIP_ICMP6_ERROR_LEN,
218  (void *)UIP_IP_BUF, uip_len - UIP_IPICMPH_LEN - uip_ext_len - UIP_ICMP6_ERROR_LEN);
219 
220  UIP_IP_BUF->vtc = 0x60;
221  UIP_IP_BUF->tcflow = 0;
222  UIP_IP_BUF->flow = 0;
223  if (uip_ext_len) {
224  UIP_FIRST_EXT_BUF->next = UIP_PROTO_ICMP6;
225  } else {
226  UIP_IP_BUF->proto = UIP_PROTO_ICMP6;
227  }
228  UIP_IP_BUF->ttl = uip_ds6_if.cur_hop_limit;
229 
230  /* the source should not be unspecified nor multicast, the check for
231  multicast is done in uip_process */
232  if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)){
233  uip_clear_buf();
234  return;
235  }
236 
237  uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &UIP_IP_BUF->srcipaddr);
238 
240  if(type == ICMP6_PARAM_PROB && code == ICMP6_PARAMPROB_OPTION){
242  } else {
243  uip_clear_buf();
244  return;
245  }
246  } else {
247 #if UIP_CONF_ROUTER
248  /* need to pick a source that corresponds to this node */
250 #else
251  uip_ipaddr_copy(&UIP_IP_BUF->srcipaddr, &tmp_ipaddr);
252 #endif
253  }
254 
255  UIP_ICMP_BUF->type = type;
256  UIP_ICMP_BUF->icode = code;
257  UIP_ICMP6_ERROR_BUF->param = uip_htonl(param);
258  UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
259  UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
260  UIP_ICMP_BUF->icmpchksum = 0;
261  UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum();
262 
263 #if UIP_CONF_IPV6_RPL
264  rpl_insert_header();
265 #endif /* UIP_CONF_IPV6_RPL */
266 
267  UIP_STAT(++uip_stat.icmp.sent);
268 
269  PRINTF("Sending ICMPv6 ERROR message type %d code %d to ", type, code);
270  PRINT6ADDR(&UIP_IP_BUF->destipaddr);
271  PRINTF(" from ");
272  PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
273  PRINTF("\n");
274  return;
275 }
276 
277 /*---------------------------------------------------------------------------*/
278 void
279 uip_icmp6_send(const uip_ipaddr_t *dest, int type, int code, int payload_len)
280 {
281 
282  UIP_IP_BUF->vtc = 0x60;
283  UIP_IP_BUF->tcflow = 0;
284  UIP_IP_BUF->flow = 0;
285  UIP_IP_BUF->proto = UIP_PROTO_ICMP6;
286  UIP_IP_BUF->ttl = uip_ds6_if.cur_hop_limit;
287  UIP_IP_BUF->len[0] = (UIP_ICMPH_LEN + payload_len) >> 8;
288  UIP_IP_BUF->len[1] = (UIP_ICMPH_LEN + payload_len) & 0xff;
289 
290  memcpy(&UIP_IP_BUF->destipaddr, dest, sizeof(*dest));
291  uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr);
292 
293  UIP_ICMP_BUF->type = type;
294  UIP_ICMP_BUF->icode = code;
295 
296  UIP_ICMP_BUF->icmpchksum = 0;
297  UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum();
298 
299  uip_len = UIP_IPH_LEN + UIP_ICMPH_LEN + payload_len;
300 
301  UIP_STAT(++uip_stat.icmp.sent);
302  UIP_STAT(++uip_stat.ip.sent);
303 
304 #if UIP_CONF_IPV6_RPL
305  rpl_insert_header();
306 #endif /* UIP_CONF_IPV6_RPL */
308 }
309 /*---------------------------------------------------------------------------*/
310 static void
311 echo_reply_input(void)
312 {
313  int ttl;
314  uip_ipaddr_t sender;
315 
316  PRINTF("Received Echo Reply from ");
317  PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
318  PRINTF(" to ");
319  PRINT6ADDR(&UIP_IP_BUF->destipaddr);
320  PRINTF("\n");
321 
322  uip_ipaddr_copy(&sender, &UIP_IP_BUF->srcipaddr);
323  ttl = UIP_IP_BUF->ttl;
324 
325  if(uip_ext_len > 0) {
326  /* Remove extension headers if any */
327  UIP_IP_BUF->proto = UIP_PROTO_ICMP6;
328  uip_len -= uip_ext_len;
329  UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
330  UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
331  /* move the echo reply payload (starting after the icmp header)
332  * to the new location in the reply. The shift is equal to the
333  * length of the extension headers present Note: UIP_ICMP_BUF
334  * still points to the echo request at this stage
335  */
336  memmove((uint8_t *)UIP_ICMP_BUF + UIP_ICMPH_LEN - uip_ext_len,
337  (uint8_t *)UIP_ICMP_BUF + UIP_ICMPH_LEN,
338  (uip_len - UIP_IPH_LEN - UIP_ICMPH_LEN));
339  uip_ext_len = 0;
340  }
341 
342  /* Call all registered applications to let them know an echo reply
343  has been received. */
344  {
345  struct uip_icmp6_echo_reply_notification *n;
346  for(n = list_head(echo_reply_callback_list);
347  n != NULL;
348  n = list_item_next(n)) {
349  if(n->callback != NULL) {
350  n->callback(&sender, ttl,
351  (uint8_t *)&UIP_ICMP_BUF[sizeof(struct uip_icmp_hdr)],
352  uip_len - sizeof(struct uip_icmp_hdr) - UIP_IPH_LEN);
353  }
354  }
355  }
356 
357  uip_clear_buf();
358  return;
359 }
360 /*---------------------------------------------------------------------------*/
361 void
362 uip_icmp6_echo_reply_callback_add(struct uip_icmp6_echo_reply_notification *n,
363  uip_icmp6_echo_reply_callback_t c)
364 {
365  if(n != NULL && c != NULL) {
366  n->callback = c;
367  list_add(echo_reply_callback_list, n);
368  }
369 }
370 /*---------------------------------------------------------------------------*/
371 void
372 uip_icmp6_echo_reply_callback_rm(struct uip_icmp6_echo_reply_notification *n)
373 {
374  list_remove(echo_reply_callback_list, n);
375 }
376 /*---------------------------------------------------------------------------*/
377 UIP_ICMP6_HANDLER(echo_request_handler, ICMP6_ECHO_REQUEST,
378  UIP_ICMP6_HANDLER_CODE_ANY, echo_request_input);
379 UIP_ICMP6_HANDLER(echo_reply_handler, ICMP6_ECHO_REPLY,
380  UIP_ICMP6_HANDLER_CODE_ANY, echo_reply_input);
381 /*---------------------------------------------------------------------------*/
382 void
384 {
385  /* Register Echo Request and Reply handlers */
386  uip_icmp6_register_input_handler(&echo_request_handler);
387  uip_icmp6_register_input_handler(&echo_reply_handler);
388 }
389 /*---------------------------------------------------------------------------*/
390 /** @} */
#define ICMP6_ECHO_REPLY
Echo reply.
Definition: uip-icmp6.h:58
uip_len
The length of the packet in the uip_buf buffer.
Definition: tcp_loader.c:75
void list_remove(list_t list, void *item)
Remove a specific element from a list.
Definition: list.c:240
#define LIST(name)
Declare a linked list.
Definition: list.h:86
Header file for IPv6-related data structures.
void uip_icmp6_register_input_handler(uip_icmp6_input_handler_t *handler)
Register a handler which can handle a specific ICMPv6 message type.
Definition: uip-icmp6.c:115
static uip_ipaddr_t tmp_ipaddr
temporary IP address
Definition: uip-icmp6.c:72
#define ICMP6_ECHO_REQUEST
Echo request.
Definition: uip-icmp6.h:57
void * list_item_next(void *item)
Get the next item following this item.
Definition: list.c:325
uint16_t uip_icmp6chksum(void)
Calculate the ICMP checksum of the packet in uip_buf.
Definition: uip.c:340
#define uip_is_addr_unspecified(a)
Is IPv6 address a the unspecified address a is of type uip_ipaddr_t.
Definition: uip.h:1978
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1027
#define UIP_IP_BUF
Pointer to IP header.
Definition: uip-nd6.c:104
#define UIP_STAT(s)
The uIP TCP/IP statistics.
Definition: uip.h:1448
#define uip_is_addr_mcast(a)
is address a multicast address, see RFC 3513 a is of type uip_ipaddr_t*
Definition: uip.h:2103
void uip_icmp6_echo_reply_callback_rm(struct uip_icmp6_echo_reply_notification *n)
Remove a callback function for ping replies.
Definition: uip-icmp6.c:372
void uip_ds6_select_src(uip_ipaddr_t *src, uip_ipaddr_t *dst)
Source address selection, see RFC 3484.
Definition: uip-ds6.c:504
void * list_head(list_t list)
Get a pointer to the first element of a list.
Definition: list.c:83
uint8_t uip_ext_len
The length of the extension headers.
Definition: uip6.c:144
#define UIP_ICMP6_ERROR_LEN
ICMPv6 Error message constant part length.
Definition: uip-icmp6.h:102
void tcpip_ipv6_output(void)
This function does address resolution and then calls tcpip_output.
Definition: tcpip.c:530
#define NULL
The null pointer.
Header file for ICMPv6 message and error handing (RFC 4443)
void list_add(list_t list, void *item)
Add an item at the end of a list.
Definition: list.c:143
#define UIP_ICMP_BUF
Pointer to ICMP header.
Definition: uip-nd6.c:105
#define ICMP6_PARAM_PROB
ip6 header bad
Definition: uip-icmp6.h:56
void uip_icmp6_init()
Initialise the uIP ICMPv6 core.
Definition: uip-icmp6.c:383
void uip_icmp6_error_output(uint8_t type, uint8_t code, uint32_t param)
Send an icmpv6 error message.
Definition: uip-icmp6.c:188
uip_ds6_netif_t uip_ds6_if
The single interface.
Definition: uip-ds6.c:71
#define ICMP6_PARAMPROB_OPTION
unrecognized option
Definition: uip-icmp6.h:95
uint8_t uip_icmp6_input(uint8_t type, uint8_t icode)
Handle an incoming ICMPv6 message.
Definition: uip-icmp6.c:98
void uip_icmp6_send(const uip_ipaddr_t *dest, int type, int code, int payload_len)
Send an icmpv6 message.
Definition: uip-icmp6.c:279
void uip_icmp6_echo_reply_callback_add(struct uip_icmp6_echo_reply_notification *n, uip_icmp6_echo_reply_callback_t c)
Add a callback function for ping replies.
Definition: uip-icmp6.c:362
#define UIP_LINK_MTU
The maximum transmission unit at the IP Layer.
Definition: uipopt.h:283