icmp6_api.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**
  2. * Copyright (c) 2014 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /** @file icmp6_api.h
  41. *
  42. * @defgroup iot_icmp6 ICMP6 Application Interface for Nordic's IPv6 stack
  43. * @ingroup iot_sdk_stack
  44. * @{
  45. * @brief Nordic Internet Control Message Protocol Application Interface for Nordic's IPv6 stack.
  46. *
  47. * @details This module provides basic features related to ICMPv6 support.
  48. */
  49. #ifndef ICMP6_API_H__
  50. #define ICMP6_API_H__
  51. #include "sdk_config.h"
  52. #include "sdk_common.h"
  53. #include "iot_defines.h"
  54. #include "ipv6_api.h"
  55. #ifdef __cplusplus
  56. extern "C" {
  57. #endif
  58. #define ICMP6_ECHO_REQUEST_PAYLOAD_OFFSET 8 /**< Offset of echo request payload from ICMPv6 header. */
  59. /**@defgroup icmp6_code ICMPv6 codes per message type as defined in RFC 4443.
  60. * @ingroup iot_icmp6
  61. * @{
  62. */
  63. #define ICMP6_DU_CODE_NO_ROUTE_TO_DESTINATION 0 /**< Code for Destination Unreachable Message when there is no route to destination. */
  64. #define ICMP6_DU_CODE_ADMINISTRATIVELY_PROHIBITED 1 /**< Code for Destination Unreachable Message when the communication to destination administratively prohibited. */
  65. #define ICMP6_DU_CODE_BEYOND_SCOPE_OF_SOURCE 2 /**< Code for Destination Unreachable Message when the destination is beyond the scope of source address. */
  66. #define ICMP6_DU_CODE_ADDRESS_UNREACHABLE 3 /**< Code for Destination Unreachable Message when the destination address is unreachable. */
  67. #define ICMP6_DU_CODE_PORT_UNREACHABLE 4 /**< Code for Destination Unreachable Message when the destination port is unreachable. */
  68. #define ICMP6_DU_CODE_FAILED_INGRESS_EGRESS_POLICY 5 /**< Code for Destination Unreachable Message when the source address failed on ingress/egress policy. */
  69. #define ICMP6_DU_CODE_REJECT_ROUTE_TO_DESTINATION 6 /**< Code for Destination Unreachable Message when the route to destination is rejected. */
  70. #define ICMP6_TE_CODE_EXCEEDED_HOP_LIMIT_TRANSIT 0 /**< Code for Time Exceeded Message when the packet received had hop limit of zero. */
  71. #define ICMP6_TE_CODE_FAR_TIME_EXCEEDED 1 /**< Code for Time Exceeded Message to report fragmentation and reassembly timeout. */
  72. #define ICMP6_PP_CODE_INVALID_HEADER 0 /**< Code for Parameter Problem Message when the header of the incoming packet was erroneous. */
  73. #define ICMP6_PP_CODE_UNKNOWN_NEXT_HEADER 1 /**< Code for Parameter Problem Message when the next header of the incoming packet was unrecognized. */
  74. #define ICMP6_PP_CODE_UNKNOWN_IPV6_OPTION 2 /**< Code for Parameter Problem Message when the option of the incoming packet was unrecognized. */
  75. #define ICMP6_ERROR_MESSAGE_INVOKING_PKT_OFFSET 8 /**< Offset in the received ICMPv6 payload where the packet (partial or complete) that invoked the error message is found. */
  76. /* @} */
  77. /** Neighbor solicitation parameters. */
  78. typedef struct
  79. {
  80. ipv6_addr_t target_addr; /**< The IPv6 address of the target of the solicitation. MUST NOT be a multi-cast address. */
  81. bool add_aro; /**< Indicates if ARO option should be added. */
  82. uint16_t aro_lifetime; /**< The amount of time in units of 60 seconds that the router should retain the NCE for the sender of the NS. */
  83. } icmp6_ns_param_t;
  84. /**@brief Parameters associated with error message in receive and transmit paths. */
  85. typedef struct
  86. {
  87. uint8_t type; /**< Identifies error message type, valid values as described in RFC 4443. See @ref icmp6_error_type for possible values. */
  88. uint8_t code; /**< Identifies code, if any associated with the error. See \ref icmp6_code and RFC 4443 for details. */
  89. union /**< Additional field like MTU, pointer or unused based on message type. See RFC 4443 for more details. If unused, application may ignore this field. */
  90. {
  91. uint32_t mtu; /**< MTU of next hop limit, used only with ICMP6_TYPE_PACKET_TOO_LONG type. */
  92. uint32_t offset; /**< Offset pointing to the parameter that resulted in the ICMP6_TYPE_PARAMETER_PROBLEM error. Used only with ICMP6_TYPE_PARAMETER_PROBLEM. */
  93. uint32_t unused; /**< Any other error message. Is always zero. */
  94. } error_field;
  95. uint8_t * p_packet; /**< Points to the start of IPv6 packet that has resulted in the error message. */
  96. uint16_t packet_len; /**< Length of the packet that resulted in error. The module may truncate the packet and pack only partially the packet based on configuration of ICMP6_ERROR_MESSAGE_MAX_SIZE. */
  97. } icmp6_error_message_param_t;
  98. /**@brief ICMPv6 data RX callback.
  99. *
  100. * @details Asynchronous callback used to notify the application of ICMP packets received.
  101. * By default, the application is not notified through ICMP of messages related to ECHO
  102. * requests or any errors. However, these notifications can easily be enabled by defining
  103. * either the ICMP6_ENABLE_ND6_MESSAGES_TO_APPLICATION or the
  104. * ICMP6_ENABLE_ALL_MESSAGES_TO_APPLICATION if the application should handle them.
  105. *
  106. * @param[in] p_interface Pointer to the IPv6 interface from where the ICMP packet was received.
  107. * @param[in] p_ip_header Pointer to the IP header of the ICMP packet received.
  108. * @param[in] p_icmp_header Pointer to the ICMP header of the received packet.
  109. * @param[in] process_result Notifies the application if the ICMP packet was processed successfully or if
  110. * an error occurred, for example, if the packet was malformed.
  111. * @param[in] p_rx_packet Packet buffer containing the packet received. p_rx_packet->p_payload
  112. * contains the ICMP payload.
  113. *
  114. * @retval A provision for the application to notify the module of whether the received packet was
  115. * processed successfully by application. The application may take ownership of the received
  116. * packet by returning IOT_IPV6_ERR_PENDING, in which case the application must take care to
  117. * free it using @ref iot_pbuffer_free.
  118. */
  119. typedef uint32_t (*icmp6_receive_callback_t)(iot_interface_t * p_interface,
  120. ipv6_header_t * p_ip_header,
  121. icmp6_header_t * p_icmp_header,
  122. uint32_t process_result,
  123. iot_pbuffer_t * p_rx_packet);
  124. /**@brief Sends ICMPv6 Error Message as defined in RFC 4443.
  125. *
  126. * @details API to send messages categorized under error messages. See @ref icmp6_error_type and
  127. * RFC 4443 for valid types.
  128. *
  129. * @param[in] p_interface Identifies the interface on which the procedure was requested.
  130. * Shall not be NULL.
  131. * @param[in] p_src_addr Source IPv6 address to be used for the request. Shall not be NULL.
  132. * @param[in] p_dest_addr Destination IPv6 address to which the message send is requested.
  133. * Shall not be NULL.
  134. * @param[in] p_param Parameters describing Type, code, invoking packet information any
  135. * additional details associated with the error message.
  136. *
  137. * @retval NRF_SUCCESS If the send request was successful, else, an error code indicating reason for
  138. * failure.
  139. */
  140. uint32_t icmp6_error_message(const iot_interface_t * p_interface,
  141. const ipv6_addr_t * p_src_addr,
  142. const ipv6_addr_t * p_dest_addr,
  143. const icmp6_error_message_param_t * p_param);
  144. /**@brief Sends ICMPv6 echo request as defined in RFC4443.
  145. *
  146. * @details API used to send an ICMPv6 echo request packet to a specific destination address.
  147. * The user can decide how much additional data must be sent.
  148. *
  149. * The application calling the function should allocate a packet before, with the type set to
  150. * ICMP6_PACKET_TYPE in the allocation parameter.
  151. *
  152. * The application should pack the payload at ICMP6_ECHO_REQUEST_PAYLOAD_OFFSET. ID,
  153. * Sequence number, ICMP Code, type checksum, etc. are filled in by the module.
  154. *
  155. * The application shall not free the allocated packet buffer if the procedure was successful,
  156. * to ensure that no data copies are needed when transmitting a packet.
  157. *
  158. * @param[in] p_interface Pointer to the IPv6 interface to send the ICMP packet.
  159. * @param[in] p_src_addr IPv6 source address from where the echo request is sent.
  160. * @param[in] p_dest_addr IPv6 destination address to where the echo request is sent.
  161. * @param[in] p_request Packet buffer containing the echo request.
  162. *
  163. * @retval NRF_SUCCESS If the send request was successful.
  164. */
  165. uint32_t icmp6_echo_request(const iot_interface_t * p_interface,
  166. const ipv6_addr_t * p_src_addr,
  167. const ipv6_addr_t * p_dest_addr,
  168. iot_pbuffer_t * p_request);
  169. /**@brief Sends router solicitation message defined in RFC6775.
  170. *
  171. * @details API used to send a neighbor discovery message of type Router Solicitation to a specific
  172. * destination address. If no address is known, the user should send the message to all
  173. * routers' address (FF02::1).
  174. *
  175. * The function internally tries to allocate a packet buffer. EUI-64 used in the SLLAO option is
  176. * taken from the interface parameter defined in the @ref ipv6_init() function.
  177. *
  178. * @param[in] p_interface Pointer to the IPv6 interface to send the ICMP packet.
  179. * @param[in] p_src_addr IPv6 source address from where the router solicitation message is
  180. * sent.
  181. * @param[in] p_dest_addr IPv6 destination address to where the router solicitation message is
  182. * sent.
  183. *
  184. * @retval NRF_SUCCESS If the send request was successful.
  185. */
  186. uint32_t icmp6_rs_send(const iot_interface_t * p_interface,
  187. const ipv6_addr_t * p_src_addr,
  188. const ipv6_addr_t * p_dest_addr);
  189. /**@brief Sends neighbour solicitation message defined in RFC6775.
  190. *
  191. * @details API used to send a neighbor discovery message of type Neighbor Solicitation to a
  192. * specific destination address.
  193. *
  194. * The function internally tries to allocate a packet buffer. EUI-64 used in the SLLAO and ARO
  195. * options is taken from the interface parameter defined in the @ref ipv6_init() function.
  196. *
  197. * @param[in] p_interface Pointer to the IPv6 interface to send the ICMP packet.
  198. * @param[in] p_src_addr IPv6 source address from where the neighbor solicitation message is
  199. * sent.
  200. * @param[in] p_dest_addr IPv6 destination address to where the neighbor solicitation message
  201. * is sent.
  202. * @param[in] p_ns_param Neighbor discovery parameters.
  203. *
  204. * @retval NRF_SUCCESS If the send request was successful.
  205. */
  206. uint32_t icmp6_ns_send(const iot_interface_t * p_interface,
  207. const ipv6_addr_t * p_src_addr,
  208. const ipv6_addr_t * p_dest_addr,
  209. const icmp6_ns_param_t * p_ns_param);
  210. /**@brief Registers the callback function for echo reply.
  211. *
  212. * @details API used to register callback to indicate the ICMP echo reply packet. Could be not used.
  213. *
  214. * Neighbor discovery related messages are not relayed to the application by default.
  215. * However, this can be enabled by using the ICMP6_ENABLE_ND6_MESSAGES_TO_APPLICATION
  216. * configuration parameter.
  217. *
  218. * @param[in] cb Handler called when an ICMP packet is received.
  219. *
  220. * @retval NRF_SUCCESS If the registration was successful.
  221. */
  222. uint32_t icmp6_receive_register(icmp6_receive_callback_t cb);
  223. #ifdef __cplusplus
  224. }
  225. #endif
  226. #endif //ICMP6_API_H__
  227. /**@} */