Contiki 3.x
uip-icmp6.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006, Swedish Institute of Computer Science.
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. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * ICMPv6 echo request and error messages (RFC 4443)
36  * \author Julien Abeille <jabeille@cisco.com>
37  * \author Mathilde Durvy <mdurvy@cisco.com>
38  */
39 
40 /**
41  * \addtogroup uip6
42  * @{
43  */
44 
45 #ifndef ICMP6_H_
46 #define ICMP6_H_
47 
48 #include "net/ip/uip.h"
49 
50 
51 /** \name ICMPv6 message types */
52 /** @{ */
53 #define ICMP6_DST_UNREACH 1 /**< dest unreachable */
54 #define ICMP6_PACKET_TOO_BIG 2 /**< packet too big */
55 #define ICMP6_TIME_EXCEEDED 3 /**< time exceeded */
56 #define ICMP6_PARAM_PROB 4 /**< ip6 header bad */
57 #define ICMP6_ECHO_REQUEST 128 /**< Echo request */
58 #define ICMP6_ECHO_REPLY 129 /**< Echo reply */
59 
60 #define ICMP6_RS 133 /**< Router Solicitation */
61 #define ICMP6_RA 134 /**< Router Advertisement */
62 #define ICMP6_NS 135 /**< Neighbor Solicitation */
63 #define ICMP6_NA 136 /**< Neighbor advertisement */
64 #define ICMP6_REDIRECT 137 /**< Redirect */
65 
66 #define ICMP6_RPL 155 /**< RPL */
67 #define ICMP6_PRIV_EXP_100 100 /**< Private Experimentation */
68 #define ICMP6_PRIV_EXP_101 101 /**< Private Experimentation */
69 #define ICMP6_PRIV_EXP_200 200 /**< Private Experimentation */
70 #define ICMP6_PRIV_EXP_201 201 /**< Private Experimentation */
71 #define ICMP6_ROLL_TM ICMP6_PRIV_EXP_200 /**< ROLL Trickle Multicast */
72 /** @} */
73 
74 
75 /** \name ICMPv6 Destination Unreachable message codes*/
76 /** @{ */
77 #define ICMP6_DST_UNREACH_NOROUTE 0 /**< no route to destination */
78 #define ICMP6_DST_UNREACH_ADMIN 1 /**< administratively prohibited */
79 #define ICMP6_DST_UNREACH_NOTNEIGHBOR 2 /**< not a neighbor(obsolete) */
80 #define ICMP6_DST_UNREACH_BEYONDSCOPE 2 /**< beyond scope of source address */
81 #define ICMP6_DST_UNREACH_ADDR 3 /**< address unreachable */
82 #define ICMP6_DST_UNREACH_NOPORT 4 /**< port unreachable */
83 /** @} */
84 
85 /** \name ICMPv6 Time Exceeded message codes*/
86 /** @{ */
87 #define ICMP6_TIME_EXCEED_TRANSIT 0 /**< ttl==0 in transit */
88 #define ICMP6_TIME_EXCEED_REASSEMBLY 1 /**< ttl==0 in reass */
89 /** @} */
90 
91 /** \name ICMPv6 Parameter Problem message codes*/
92 /** @{ */
93 #define ICMP6_PARAMPROB_HEADER 0 /**< erroneous header field */
94 #define ICMP6_PARAMPROB_NEXTHEADER 1 /**< unrecognized next header */
95 #define ICMP6_PARAMPROB_OPTION 2 /**< unrecognized option */
96 /** @} */
97 
98 /** \brief Echo Request constant part length */
99 #define UIP_ICMP6_ECHO_REQUEST_LEN 4
100 
101 /** \brief ICMPv6 Error message constant part length */
102 #define UIP_ICMP6_ERROR_LEN 4
103 
104 /** \brief ICMPv6 Error message constant part */
105 typedef struct uip_icmp6_error{
106  uint32_t param;
108 
109 /** \name ICMPv6 RFC4443 Message processing and sending */
110 /** @{ */
111 /**
112  * \brief Send an icmpv6 error message
113  * \param type type of the error message
114  * \param code of the error message
115  * \param type 32 bit parameter of the error message, semantic depends on error
116  */
117 void
118 uip_icmp6_error_output(uint8_t type, uint8_t code, uint32_t param);
119 
120 /**
121  * \brief Send an icmpv6 message
122  * \param dest destination address of the message
123  * \param type type of the message
124  * \param code of the message
125  * \param payload_len length of the payload
126  */
127 void
128 uip_icmp6_send(const uip_ipaddr_t *dest, int type, int code, int payload_len);
129 
130 
131 
132 typedef void (* uip_icmp6_echo_reply_callback_t)(uip_ipaddr_t *source,
133  uint8_t ttl,
134  uint8_t *data,
135  uint16_t datalen);
136 struct uip_icmp6_echo_reply_notification {
137  struct uip_icmp6_echo_reply_notification *next;
138  uip_icmp6_echo_reply_callback_t callback;
139 };
140 
141 /**
142  * \brief Add a callback function for ping replies
143  * \param n A struct uip_icmp6_echo_reply_notification that must have been allocated by the caller
144  * \param c A pointer to the callback function to be called
145  *
146  * This function adds a callback function to the list of
147  * callback functions that are called when an ICMP echo
148  * reply (ping reply) is received. This is used when
149  * implementing a ping protocol: attach a callback
150  * function to the ping reply, then send out a ping packet
151  * with uip_icmp6_send().
152  *
153  * The caller must have statically allocated a struct
154  * uip_icmp6_echo_reply_notification to hold the internal
155  * state of the callback function.
156  *
157  * When a ping reply packet is received, all registered
158  * callback functions are called. The callback functions
159  * must not modify the contents of the uIP buffer.
160  */
161 void
162 uip_icmp6_echo_reply_callback_add(struct uip_icmp6_echo_reply_notification *n,
163  uip_icmp6_echo_reply_callback_t c);
164 
165 /**
166  * \brief Remove a callback function for ping replies
167  * \param n A struct uip_icmp6_echo_reply_notification that must have been previously added with uip_icmp6_echo_reply_callback_add()
168  *
169  * This function removes a callback function from the list of
170  * callback functions that are called when an ICMP echo
171  * reply (ping reply) is received.
172  */
173 void
174 uip_icmp6_echo_reply_callback_rm(struct uip_icmp6_echo_reply_notification *n);
175 
176 /* Generic ICMPv6 input handers */
177 typedef struct uip_icmp6_input_handler {
178  struct uip_icmp6_input_handler *next;
179  uint8_t type;
180  uint8_t icode;
181  void (*handler)(void);
182 } uip_icmp6_input_handler_t;
183 
184 #define UIP_ICMP6_INPUT_SUCCESS 0
185 #define UIP_ICMP6_INPUT_ERROR 1
186 
187 #define UIP_ICMP6_HANDLER_CODE_ANY 0xFF /* Handle all codes for this type */
188 
189 /*
190  * Initialise a variable of type uip_icmp6_input_handler, to be used later as
191  * the argument to uip_icmp6_register_input_handler
192  *
193  * The function pointer stored in this variable will get called and will be
194  * expected to handle incoming ICMPv6 datagrams of the specified type/code
195  *
196  * If code has a value of UIP_ICMP6_HANDLER_CODE_ANY, the same function
197  * will handle all codes for this type. In other words, the ICMPv6
198  * message's code is "don't care"
199  */
200 #define UIP_ICMP6_HANDLER(name, type, code, func) \
201  static uip_icmp6_input_handler_t name = { NULL, type, code, func }
202 
203 /**
204  * \brief Handle an incoming ICMPv6 message
205  * \param type The ICMPv6 message type
206  * \param icode The ICMPv6 message code
207  * \return Success: UIP_ICMP6_INPUT_SUCCESS, Error: UIP_ICMP6_INPUT_ERROR
208  *
209  * Generic handler for unknown ICMPv6 types. It will lookup for a registered
210  * function capable of handing this message type. The function must have first
211  * been registered with uip_icmp6_register_input_handler. The function is in
212  * charge of setting uip_len to 0, otherwise the uIPv6 core will attempt to
213  * send whatever remains in the UIP_IP_BUF.
214  *
215  * A return value of UIP_ICMP6_INPUT_ERROR means that a handler could not be
216  * invoked. This is most likely because the ICMPv6 type does not have a valid
217  * handler associated with it.
218 
219  * UIP_ICMP6_INPUT_SUCCESS signifies that a handler was found for this ICMPv6
220  * type and that it was invoked. It does NOT provide any indication whatsoever
221  * regarding whether the handler itself succeeded.
222  */
223 uint8_t uip_icmp6_input(uint8_t type, uint8_t icode);
224 
225 /**
226  * \brief Register a handler which can handle a specific ICMPv6 message type
227  * \param handler A pointer to the handler
228  */
229 void uip_icmp6_register_input_handler(uip_icmp6_input_handler_t *handler);
230 
231 
232 /**
233  * \brief Initialise the uIP ICMPv6 core
234  */
235 void uip_icmp6_init(void);
236 
237 /** @} */
238 
239 #endif /*ICMP6_H_*/
240 /** @} */
241 
Header file for the uIP TCP/IP stack.
void uip_icmp6_error_output(uint8_t type, uint8_t code, uint32_t param)
Send an icmpv6 error message.
Definition: uip-icmp6.c:210
struct uip_icmp6_error uip_icmp6_error
ICMPv6 Error message constant part.
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:117
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:405
void uip_icmp6_send(const uip_ipaddr_t *dest, int type, int code, int payload_len)
Send an icmpv6 message.
Definition: uip-icmp6.c:297
uint8_t uip_icmp6_input(uint8_t type, uint8_t icode)
Handle an incoming ICMPv6 message.
Definition: uip-icmp6.c:100
void uip_icmp6_init()
Initialise the uIP ICMPv6 core.
Definition: uip-icmp6.c:416
ICMPv6 Error message constant part.
Definition: uip-icmp6.h:105
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:395