Contiki-NG
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  * \addtogroup uip
35  * @{
36  */
37 
38 /**
39  * \file
40  * Header file for ICMPv6 message and error handing (RFC 4443)
41  * \author Julien Abeille <jabeille@cisco.com>
42  * \author Mathilde Durvy <mdurvy@cisco.com>
43  */
44 
45 #ifndef ICMP6_H_
46 #define ICMP6_H_
47 
48 #include "net/ipv6/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 #define ICMP6_ESMRF ICMP6_PRIV_EXP_201 /**< ESMRF Multicast */
73 /** @} */
74 
75 
76 /** \name ICMPv6 Destination Unreachable message codes*/
77 /** @{ */
78 #define ICMP6_DST_UNREACH_NOROUTE 0 /**< no route to destination */
79 #define ICMP6_DST_UNREACH_ADMIN 1 /**< administratively prohibited */
80 #define ICMP6_DST_UNREACH_NOTNEIGHBOR 2 /**< not a neighbor(obsolete) */
81 #define ICMP6_DST_UNREACH_BEYONDSCOPE 2 /**< beyond scope of source address */
82 #define ICMP6_DST_UNREACH_ADDR 3 /**< address unreachable */
83 #define ICMP6_DST_UNREACH_NOPORT 4 /**< port unreachable */
84 /** @} */
85 
86 /** \name ICMPv6 Time Exceeded message codes*/
87 /** @{ */
88 #define ICMP6_TIME_EXCEED_TRANSIT 0 /**< ttl==0 in transit */
89 #define ICMP6_TIME_EXCEED_REASSEMBLY 1 /**< ttl==0 in reass */
90 /** @} */
91 
92 /** \name ICMPv6 Parameter Problem message codes*/
93 /** @{ */
94 #define ICMP6_PARAMPROB_HEADER 0 /**< erroneous header field */
95 #define ICMP6_PARAMPROB_NEXTHEADER 1 /**< unrecognized next header */
96 #define ICMP6_PARAMPROB_OPTION 2 /**< unrecognized option */
97 /** @} */
98 
99 /** \brief Echo Request constant part length */
100 #define UIP_ICMP6_ECHO_REQUEST_LEN 4
101 
102 /** \brief ICMPv6 Error message constant part length */
103 #define UIP_ICMP6_ERROR_LEN 4
104 
105 /** \brief ICMPv6 Error message constant part */
106 typedef struct uip_icmp6_error{
107  uint32_t param;
109 
110 /** \name ICMPv6 RFC4443 Message processing and sending */
111 /** @{ */
112 /**
113  * \brief Send an icmpv6 error message
114  * \param type type of the error message
115  * \param code of the error message
116  * \param param 32 bit parameter of the error message, semantic depends on error
117  */
118 void
119 uip_icmp6_error_output(uint8_t type, uint8_t code, uint32_t param);
120 
121 /**
122  * \brief Send an icmpv6 message
123  * \param dest destination address of the message
124  * \param type type of the message
125  * \param code of the message
126  * \param payload_len length of the payload
127  */
128 void
129 uip_icmp6_send(const uip_ipaddr_t *dest, int type, int code, int payload_len);
130 
131 
132 
133 typedef void (* uip_icmp6_echo_reply_callback_t)(uip_ipaddr_t *source,
134  uint8_t ttl,
135  uint8_t *data,
136  uint16_t datalen);
137 struct uip_icmp6_echo_reply_notification {
138  struct uip_icmp6_echo_reply_notification *next;
139  uip_icmp6_echo_reply_callback_t callback;
140 };
141 
142 /**
143  * \brief Add a callback function for ping replies
144  * \param n A struct uip_icmp6_echo_reply_notification that must have been allocated by the caller
145  * \param c A pointer to the callback function to be called
146  *
147  * This function adds a callback function to the list of
148  * callback functions that are called when an ICMP echo
149  * reply (ping reply) is received. This is used when
150  * implementing a ping protocol: attach a callback
151  * function to the ping reply, then send out a ping packet
152  * with uip_icmp6_send().
153  *
154  * The caller must have statically allocated a struct
155  * uip_icmp6_echo_reply_notification to hold the internal
156  * state of the callback function.
157  *
158  * When a ping reply packet is received, all registered
159  * callback functions are called. The callback functions
160  * must not modify the contents of the uIP buffer.
161  */
162 void
163 uip_icmp6_echo_reply_callback_add(struct uip_icmp6_echo_reply_notification *n,
164  uip_icmp6_echo_reply_callback_t c);
165 
166 /**
167  * \brief Remove a callback function for ping replies
168  * \param n A struct uip_icmp6_echo_reply_notification that must have been previously added with uip_icmp6_echo_reply_callback_add()
169  *
170  * This function removes a callback function from the list of
171  * callback functions that are called when an ICMP echo
172  * reply (ping reply) is received.
173  */
174 void
175 uip_icmp6_echo_reply_callback_rm(struct uip_icmp6_echo_reply_notification *n);
176 
177 /* Generic ICMPv6 input handers */
178 typedef struct uip_icmp6_input_handler {
179  struct uip_icmp6_input_handler *next;
180  uint8_t type;
181  uint8_t icode;
182  void (*handler)(void);
183 } uip_icmp6_input_handler_t;
184 
185 #define UIP_ICMP6_INPUT_SUCCESS 0
186 #define UIP_ICMP6_INPUT_ERROR 1
187 
188 #define UIP_ICMP6_HANDLER_CODE_ANY 0xFF /* Handle all codes for this type */
189 
190 /*
191  * Initialise a variable of type uip_icmp6_input_handler, to be used later as
192  * the argument to uip_icmp6_register_input_handler
193  *
194  * The function pointer stored in this variable will get called and will be
195  * expected to handle incoming ICMPv6 datagrams of the specified type/code
196  *
197  * If code has a value of UIP_ICMP6_HANDLER_CODE_ANY, the same function
198  * will handle all codes for this type. In other words, the ICMPv6
199  * message's code is "don't care"
200  */
201 #define UIP_ICMP6_HANDLER(name, type, code, func) \
202  static uip_icmp6_input_handler_t name = { NULL, type, code, func }
203 
204 /**
205  * \brief Handle an incoming ICMPv6 message
206  * \param type The ICMPv6 message type
207  * \param icode The ICMPv6 message code
208  * \return Success: UIP_ICMP6_INPUT_SUCCESS, Error: UIP_ICMP6_INPUT_ERROR
209  *
210  * Generic handler for unknown ICMPv6 types. It will lookup for a registered
211  * function capable of handing this message type. The function must have first
212  * been registered with uip_icmp6_register_input_handler. The function is in
213  * charge of setting uip_len to 0, otherwise the uIPv6 core will attempt to
214  * send whatever remains in the UIP_IP_BUF.
215  *
216  * A return value of UIP_ICMP6_INPUT_ERROR means that a handler could not be
217  * invoked. This is most likely because the ICMPv6 type does not have a valid
218  * handler associated with it.
219 
220  * UIP_ICMP6_INPUT_SUCCESS signifies that a handler was found for this ICMPv6
221  * type and that it was invoked. It does NOT provide any indication whatsoever
222  * regarding whether the handler itself succeeded.
223  */
224 uint8_t uip_icmp6_input(uint8_t type, uint8_t icode);
225 
226 /**
227  * \brief Register a handler which can handle a specific ICMPv6 message type
228  * \param handler A pointer to the handler
229  */
230 void uip_icmp6_register_input_handler(uip_icmp6_input_handler_t *handler);
231 
232 
233 /**
234  * \brief Initialise the uIP ICMPv6 core
235  */
236 void uip_icmp6_init(void);
237 
238 /** @} */
239 
240 #endif /*ICMP6_H_*/
241 /** @} */
242 
ICMPv6 Error message constant part.
Definition: uip-icmp6.h:106
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:347
void uip_icmp6_error_output(uint8_t type, uint8_t code, uint32_t param)
Send an icmpv6 error message.
Definition: uip-icmp6.c:174
uint8_t uip_icmp6_input(uint8_t type, uint8_t icode)
Handle an incoming ICMPv6 message.
Definition: uip-icmp6.c:89
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:337
Header file for the uIP TCP/IP stack.
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:106
void uip_icmp6_init()
Initialise the uIP ICMPv6 core.
Definition: uip-icmp6.c:358
void uip_icmp6_send(const uip_ipaddr_t *dest, int type, int code, int payload_len)
Send an icmpv6 message.
Definition: uip-icmp6.c:254