ipv6.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. /**
  2. * Copyright (c) 2013 - 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. #include <stdio.h>
  41. #include <stdint.h>
  42. #include <string.h>
  43. #include "ble_6lowpan.h"
  44. #include "mem_manager.h"
  45. #include "sdk_os.h"
  46. #include "sdk_config.h"
  47. #include "iot_common.h"
  48. #include "iot_context_manager.h"
  49. #include "ipv6_api.h"
  50. #include "icmp6_api.h"
  51. #include "udp_api.h"
  52. #include "icmp6.h"
  53. #include "udp.h"
  54. #if IPV6_CONFIG_LOG_ENABLED
  55. #define NRF_LOG_MODULE_NAME ipv6
  56. #define NRF_LOG_LEVEL IPV6_CONFIG_LOG_LEVEL
  57. #define NRF_LOG_INFO_COLOR IPV6_CONFIG_INFO_COLOR
  58. #define NRF_LOG_DEBUG_COLOR IPV6_CONFIG_DEBUG_COLOR
  59. #include "nrf_log.h"
  60. NRF_LOG_MODULE_REGISTER();
  61. #define IPV6_TRC NRF_LOG_DEBUG /**< Used for getting trace of execution in the module. */
  62. #define IPV6_ERR NRF_LOG_ERROR /**< Used for logging errors in the module. */
  63. #define IPV6_DUMP NRF_LOG_HEXDUMP_DEBUG /**< Used for dumping octet information to get details of bond information etc. */
  64. #define IPV6_ENTRY() IPV6_TRC(">> %s", __func__)
  65. #define IPV6_EXIT() IPV6_TRC("<< %s", __func__)
  66. #else // IPV6_CONFIG_LOG_ENABLED
  67. #define IPV6_TRC(...) /**< Disables traces. */
  68. #define IPV6_DUMP(...) /**< Disables dumping of octet streams. */
  69. #define IPV6_ERR(...) /**< Disables error logs. */
  70. #define IPV6_ENTRY(...)
  71. #define IPV6_EXIT(...)
  72. #endif // IPV6_CONFIG_LOG_ENABLED
  73. /**
  74. * @defgroup ipv6_mutex_lock_unlock Module's Mutex Lock/Unlock Macros.
  75. *
  76. * @details Macros used to lock and unlock modules. Currently, SDK does not use mutexes but
  77. * framework is provided in case need arises to use an alternative architecture.
  78. * @{
  79. */
  80. #define IPV6_MUTEX_LOCK() SDK_MUTEX_LOCK(m_ipv6_mutex) /**< Lock module using mutex */
  81. #define IPV6_MUTEX_UNLOCK() SDK_MUTEX_UNLOCK(m_ipv6_mutex) /**< Unlock module using mutex */
  82. /** @} */
  83. /**
  84. * @defgroup api_param_check API Parameters check macros.
  85. *
  86. * @details Macros that verify parameters passed to the module in the APIs. These macros
  87. * could be mapped to nothing in final versions of code to save execution and size.
  88. * IPV6_DISABLE_API_PARAM_CHECK should be set to 0 to enable these checks.
  89. *
  90. * @{
  91. */
  92. #if (IPV6_DISABLE_API_PARAM_CHECK == 0)
  93. /**@brief Macro to check is module is initialized before requesting one of the module procedures. */
  94. #define VERIFY_MODULE_IS_INITIALIZED() \
  95. if (m_event_handler == NULL) \
  96. { \
  97. return (SDK_ERR_MODULE_NOT_INITIALIZED | IOT_IPV6_ERR_BASE); \
  98. }
  99. /**@brief Verify NULL parameters are not passed to API by application. */
  100. #define NULL_PARAM_CHECK(PARAM) \
  101. if ((PARAM) == NULL) \
  102. { \
  103. return (NRF_ERROR_NULL | IOT_IPV6_ERR_BASE); \
  104. }
  105. #else // IPV6_DISABLE_API_PARAM_CHECK
  106. #define VERIFY_MODULE_IS_INITIALIZED()
  107. #define NULL_PARAM_CHECK(PARAM)
  108. #endif // IPV6_DISABLE_API_PARAM_CHECK
  109. /** @} */
  110. #define PBUFFER_ICMP_PAYLOAD_OFFSET IPV6_IP_HEADER_SIZE + ICMP6_HEADER_SIZE /**< ICMP payload offset. */
  111. #define PBUFFER_UDP_PAYLOAD_OFFSET IPV6_IP_HEADER_SIZE + UDP_HEADER_SIZE /**< UDP payload offset. */
  112. #define PBUFFER_OTHER_PAYLOAD_OFFSET IPV6_IP_HEADER_SIZE /**< Raw IPv6 payload offset. */
  113. #define IPV6_MAX_ADDRESS_COUNT (IPV6_MAX_ADDRESS_PER_INTERFACE * IPV6_MAX_INTERFACE) /**< Maximum number of addresses. */
  114. #define IPV6_INVALID_ADDR_INDEX 0xFF /**< Invalid address representation. */
  115. #define DEST_ADDR_OFFSET 24 /**< Offset of destination address in IPv6 packet. */
  116. /**@brief Internal interface structure. */
  117. typedef struct
  118. {
  119. iot_interface_t * p_interface; /**< Pointer to driver interface */
  120. uint8_t addr_range[IPV6_MAX_ADDRESS_PER_INTERFACE]; /**< Indexes to m_address_table indicating the address. If an index is IPV6_INVALID_ADDR_INDEX, it means there is no address entry. */
  121. } ipv6_interface_t;
  122. /**@brief Application Event Handler. */
  123. static ipv6_evt_handler_t m_event_handler = NULL;
  124. /**@brief Table of addresses */
  125. static ipv6_addr_conf_t m_address_table[IPV6_MAX_ADDRESS_COUNT];
  126. /**@brief Network interfaces table. */
  127. static ipv6_interface_t m_interfaces[IPV6_MAX_INTERFACE];
  128. /**@brief Number of network interfaces. */
  129. static uint32_t m_interfaces_count = 0;
  130. /**@brief Global address for IPv6 any. */
  131. ipv6_addr_t ipv6_addr_any;
  132. /**@brief Mutex variable. Currently unused, this declaration does not occupy any space in RAM. */
  133. SDK_MUTEX_DEFINE(m_ipv6_mutex)
  134. /**@brief Function for finding specific address in address table.
  135. *
  136. * @param[in] p_addr Checked address.
  137. * @param[out] p_index Index of address.
  138. *
  139. * @return NRF_SUCCESS if success, NRF_ERROR_NOT_FOUND otherwise.
  140. */
  141. static uint32_t addr_find(const ipv6_addr_t * p_addr, uint32_t * p_index)
  142. {
  143. uint32_t index;
  144. uint32_t err_code = (IOT_IPV6_ERR_BASE | NRF_ERROR_NOT_FOUND);
  145. for (index = 0; index < IPV6_MAX_ADDRESS_COUNT; index++)
  146. {
  147. if ((m_address_table[index].state != IPV6_ADDR_STATE_UNUSED) &&
  148. (0 == IPV6_ADDRESS_CMP(&m_address_table[index].addr, p_addr)))
  149. {
  150. *p_index = index;
  151. err_code = NRF_SUCCESS;
  152. break;
  153. }
  154. }
  155. return err_code;
  156. }
  157. /**@brief Function for finding free place in address table.
  158. *
  159. * @param[out] p_index Index of address.
  160. *
  161. * @return NRF_SUCCESS if success, NRF_ERROR_NOT_FOUND otherwise.
  162. */
  163. static uint32_t addr_find_free(uint32_t * p_index)
  164. {
  165. uint32_t index;
  166. uint32_t err_code = (IOT_IPV6_ERR_BASE | NRF_ERROR_NO_MEM);
  167. for (index = 0; index < IPV6_MAX_ADDRESS_COUNT; index++)
  168. {
  169. if (m_address_table[index].state == IPV6_ADDR_STATE_UNUSED)
  170. {
  171. *p_index = index;
  172. err_code = NRF_SUCCESS;
  173. break;
  174. }
  175. }
  176. return err_code;
  177. }
  178. /**@brief Function for freeing an address configuration entry in m_address_table.
  179. *
  180. * @param[in] index Index of address.
  181. * @param[in] check_references Indicate that before remove references should be counted.
  182. *
  183. * @return None.
  184. */
  185. static void addr_free(uint32_t addr_index, bool check_references)
  186. {
  187. uint32_t if_index;
  188. uint32_t index;
  189. if (check_references)
  190. {
  191. for (if_index = 0; if_index < IPV6_MAX_INTERFACE; if_index++)
  192. {
  193. for (index = 0; index < IPV6_MAX_ADDRESS_PER_INTERFACE; index++)
  194. {
  195. if (m_interfaces[if_index].addr_range[index] == addr_index)
  196. {
  197. return;
  198. }
  199. }
  200. }
  201. }
  202. m_address_table[addr_index].state = IPV6_ADDR_STATE_UNUSED;
  203. IPV6_ADDRESS_INITIALIZE(&m_address_table[addr_index].addr);
  204. }
  205. /**@brief Function for checking if received packet is for us.
  206. * Currently only all-node, MLDv2 and solicited-node
  207. * multicast addresses are accepted.
  208. *
  209. * @param[in] interface_id Index of the interface.
  210. * @param[in] p_addr Checked address.
  211. * @param[in] check_multicast Define if multicast addresses have to be checked.
  212. *
  213. * @return NRF_SUCCESS if packet can be processing to IPv6 multiplexer.
  214. */
  215. static uint32_t addr_check(uint32_t interface_id, const ipv6_addr_t * p_addr, bool check_multicast)
  216. {
  217. ipv6_addr_conf_t * p_addr_conf;
  218. uint32_t index;
  219. uint32_t err_code = NRF_ERROR_NOT_FOUND;
  220. // Check basic Multicast addresses.
  221. if (check_multicast && (IPV6_ADDRESS_IS_MLDV2_MCAST(p_addr) || IPV6_ADDRESS_IS_ALL_NODE(p_addr)))
  222. {
  223. return NRF_SUCCESS;
  224. }
  225. for (index = 0; m_interfaces[interface_id].addr_range[index] != IPV6_INVALID_ADDR_INDEX; index++)
  226. {
  227. p_addr_conf = &m_address_table[m_interfaces[interface_id].addr_range[index]];
  228. if (check_multicast && IPV6_ADDRESS_IS_MULTICAST_SOLICITED_NODE(p_addr))
  229. {
  230. // Solicited-node multicast address is formed by taking the low-order 24 bits of an address (unicast or anycast).
  231. if (0 == memcmp(&p_addr_conf->addr.u8[13], &p_addr->u8[13], 3))
  232. {
  233. err_code = NRF_SUCCESS;
  234. break;
  235. }
  236. }
  237. else if (0 == IPV6_ADDRESS_CMP(&p_addr_conf->addr, p_addr))
  238. {
  239. err_code = NRF_SUCCESS;
  240. break;
  241. }
  242. }
  243. return err_code;
  244. }
  245. /**@brief Function for adding/updating IPv6 address in table.
  246. *
  247. * @param[in] interface_id Index of interface.
  248. * @param[in] p_addr Given address.
  249. *
  250. * @return NRF_SUCCESS if operation successful, NRF_ERROR_NO_MEM otherwise.
  251. */
  252. static uint32_t addr_set(const iot_interface_t * p_interface,
  253. const ipv6_addr_conf_t * p_addr)
  254. {
  255. uint32_t index;
  256. uint32_t addr_index;
  257. uint32_t err_code;
  258. uint32_t interface_id = (uint32_t)p_interface->p_upper_stack;
  259. // Try to find address.
  260. err_code = addr_find(&p_addr->addr, &addr_index);
  261. if (err_code != NRF_SUCCESS)
  262. {
  263. // Find first empty one.
  264. err_code = addr_find_free(&addr_index);
  265. }
  266. if (err_code == NRF_SUCCESS)
  267. {
  268. err_code = IOT_IPV6_ERR_ADDR_IF_MISMATCH;
  269. // Check if this index entry exists in the p_interface for which API is requested.
  270. for (index = 0; index < IPV6_MAX_ADDRESS_PER_INTERFACE; index++)
  271. {
  272. if (m_interfaces[interface_id].addr_range[index] == addr_index)
  273. {
  274. m_address_table[index].state = p_addr->state;
  275. err_code = NRF_SUCCESS;
  276. break;
  277. }
  278. }
  279. if (err_code == IOT_IPV6_ERR_ADDR_IF_MISMATCH)
  280. {
  281. err_code = (IOT_IPV6_ERR_BASE | NRF_ERROR_NO_MEM);
  282. for (index = 0; index < IPV6_MAX_ADDRESS_PER_INTERFACE; index++)
  283. {
  284. if (m_interfaces[interface_id].addr_range[index] == IPV6_INVALID_ADDR_INDEX)
  285. {
  286. m_address_table[index].state = p_addr->state;
  287. memcpy(&m_address_table[index].addr, p_addr, IPV6_ADDR_SIZE);
  288. m_interfaces[interface_id].addr_range[index] = addr_index;
  289. err_code = NRF_SUCCESS;
  290. break;
  291. }
  292. }
  293. }
  294. }
  295. return err_code;
  296. }
  297. /**@brief Function for calculating how many bits of addresses are equal.
  298. *
  299. * @param[in] p_addr1 Base address.
  300. * @param[in] p_addr2 Base address.
  301. *
  302. * @return Number of same bits.
  303. */
  304. static uint32_t addr_bit_equal(const ipv6_addr_t * p_addr1,
  305. const ipv6_addr_t * p_addr2)
  306. {
  307. uint32_t index;
  308. uint32_t match = 0;
  309. uint8_t temp;
  310. uint32_t index_tab;
  311. for (index = 0; index < IPV6_ADDR_SIZE; index++)
  312. {
  313. if (p_addr1->u8[index] == p_addr2->u8[index])
  314. {
  315. // Add full 8bits to match
  316. match += 8;
  317. }
  318. else
  319. {
  320. // Operation of XOR to detect differences
  321. temp = p_addr1->u8[index] ^ p_addr2->u8[index];
  322. // Check all single bits
  323. for (index_tab = 0; index_tab < 8; index_tab++)
  324. {
  325. if ((temp & 0x80) == 0)
  326. {
  327. // If the oldest bits matched, add one more.
  328. match++;
  329. // Check next bit.
  330. temp = temp << 1;
  331. }
  332. else
  333. {
  334. break;
  335. }
  336. }
  337. break;
  338. }
  339. }
  340. return match;
  341. }
  342. /**@brief Function for searching specific network interface by given address.
  343. *
  344. * @param[in] p_interface Pointer to IPv6 network interface.
  345. * @param[in] p_dest_addr IPv6 address to be matched.
  346. *
  347. * @return NRF_SUCCESS if operation successful, NRF_ERROR_NOT_FOUND otherwise.
  348. */
  349. static uint32_t interface_find(iot_interface_t ** pp_interface, const ipv6_addr_t * p_dest_addr)
  350. {
  351. // Currently only host role is implemented, though no need to match addresses.
  352. UNUSED_VARIABLE(p_dest_addr);
  353. uint32_t index;
  354. uint32_t err_code = (IOT_IPV6_ERR_BASE | NRF_ERROR_NOT_FOUND);
  355. if (m_interfaces_count == 1)
  356. {
  357. for (index = 0; index < IPV6_MAX_INTERFACE; index++)
  358. {
  359. if (m_interfaces[index].p_interface != NULL)
  360. {
  361. *pp_interface = m_interfaces[index].p_interface;
  362. err_code = NRF_SUCCESS;
  363. break;
  364. }
  365. }
  366. }
  367. else if (m_interfaces_count == 0)
  368. {
  369. err_code = (IOT_IPV6_ERR_BASE | NRF_ERROR_NOT_FOUND);
  370. }
  371. else
  372. {
  373. // Not supported now.
  374. err_code = (IOT_IPV6_ERR_BASE | NRF_ERROR_NOT_SUPPORTED);
  375. }
  376. return err_code;
  377. }
  378. /**@brief Function for resetting specific network interface.
  379. *
  380. * @param[in] p_interface Pointer to IPv6 network interface.
  381. *
  382. * @return None.
  383. */
  384. static void interface_reset(ipv6_interface_t * p_interface)
  385. {
  386. uint32_t index;
  387. uint8_t addr_index;
  388. p_interface->p_interface = NULL;
  389. for (index = 0; index < IPV6_MAX_ADDRESS_PER_INTERFACE; index++)
  390. {
  391. addr_index = p_interface->addr_range[index];
  392. if (addr_index != IPV6_INVALID_ADDR_INDEX)
  393. {
  394. p_interface->addr_range[index] = IPV6_INVALID_ADDR_INDEX;
  395. addr_free(index, true);
  396. }
  397. }
  398. }
  399. /**@brief Function for getting specific network interface by 6LoWPAN interface.
  400. *
  401. * @param[in] p_6lo_interface Pointer to 6LoWPAN interface.
  402. *
  403. * @return Pointer to internal network interface on success, otherwise NULL.
  404. */
  405. static uint32_t interface_get_by_6lo(iot_interface_t * p_6lo_interface)
  406. {
  407. return (uint32_t)(p_6lo_interface->p_upper_stack);
  408. }
  409. /**@brief Function for adding new 6lowpan interface to interface table.
  410. *
  411. * @param[in] p_6lo_interface Pointer to 6LoWPAN interface.
  412. * @param[out] p_index Pointer to index of internal network interface.
  413. *
  414. * @return NRF_SUCCESS on success, otherwise NRF_ERROR_NO_MEM error.
  415. */
  416. static uint32_t interface_add(iot_interface_t * p_interface,
  417. uint32_t * p_index )
  418. {
  419. uint32_t index;
  420. uint32_t err_code;
  421. ipv6_addr_conf_t linklocal_addr;
  422. for (index = 0; index < IPV6_MAX_INTERFACE; index++)
  423. {
  424. if (m_interfaces[index].p_interface == NULL)
  425. {
  426. m_interfaces[index].p_interface = p_interface;
  427. p_interface->p_upper_stack = (void *) index;
  428. (*p_index) = index;
  429. // Add link local address.
  430. IPV6_CREATE_LINK_LOCAL_FROM_EUI64(&linklocal_addr.addr, p_interface->local_addr.identifier);
  431. linklocal_addr.state = IPV6_ADDR_STATE_PREFERRED;
  432. err_code = addr_set(p_interface, &linklocal_addr);
  433. if (err_code != NRF_SUCCESS)
  434. {
  435. IPV6_ERR("Cannot add link-local address to interface!");
  436. }
  437. return NRF_SUCCESS;
  438. }
  439. }
  440. return NRF_ERROR_NO_MEM;
  441. }
  442. /**@brief Function for removing 6lowpan interface from interface table.
  443. *
  444. * @param[in] p_interface Pointer to internal network interface.
  445. *
  446. * @return None.
  447. */
  448. static void interface_delete(uint32_t index)
  449. {
  450. interface_reset(&m_interfaces[index]);
  451. }
  452. /**@brief Function for notifying application of the new interface established.
  453. *
  454. * @param[in] p_interface Pointer to internal network interface.
  455. *
  456. * @return None.
  457. */
  458. static void app_notify_interface_add(iot_interface_t * p_interface)
  459. {
  460. ipv6_event_t event;
  461. event.event_id = IPV6_EVT_INTERFACE_ADD;
  462. IPV6_MUTEX_UNLOCK();
  463. m_event_handler(p_interface, &event);
  464. IPV6_MUTEX_LOCK();
  465. }
  466. /**@brief Function for notifying application of the interface disconnection.
  467. *
  468. * @param[in] p_interface Pointer to internal network interface.
  469. *
  470. * @return None.
  471. */
  472. static void app_notify_interface_delete(iot_interface_t * p_interface)
  473. {
  474. ipv6_event_t event;
  475. event.event_id = IPV6_EVT_INTERFACE_DELETE;
  476. IPV6_MUTEX_UNLOCK();
  477. m_event_handler(p_interface, &event);
  478. IPV6_MUTEX_LOCK();
  479. }
  480. #if (IPV6_ENABLE_USNUPORTED_PROTOCOLS_TO_APPLICATION == 1)
  481. /**@brief Function for notifying application of the received packet (e.g. with unsupported protocol).
  482. *
  483. * @param[in] p_interface Pointer to external interface from which packet come.
  484. * @param[in] p_pbuffer Pointer to packet buffer.
  485. *
  486. * @return None.
  487. */
  488. static void app_notify_rx_data(iot_interface_t * p_interface, iot_pbuffer_t * p_pbuffer)
  489. {
  490. ipv6_event_t event;
  491. event.event_id = IPV6_EVT_INTERFACE_RX_DATA;
  492. // RX Event parameter.
  493. event.event_param.rx_event_param.p_rx_packet = p_pbuffer;
  494. event.event_param.rx_event_param.p_ip_header = (ipv6_header_t *)p_pbuffer->p_memory;
  495. IPV6_MUTEX_UNLOCK();
  496. m_event_handler(p_interface, &event);
  497. IPV6_MUTEX_LOCK();
  498. }
  499. #endif
  500. /**@brief Function for multiplexing transport protocol to different modules.
  501. *
  502. * @param[in] p_interface Pointer to external interface from which packet come.
  503. * @param[in] p_pbuffer Pointer to packet buffer.
  504. *
  505. * @return NRF_SUCCESS if success, otherwise an error code.
  506. */
  507. static uint32_t ipv6_input(iot_interface_t * p_interface, iot_pbuffer_t * p_pbuffer)
  508. {
  509. uint32_t err_code = NRF_SUCCESS;
  510. ipv6_header_t * p_iphdr = (ipv6_header_t *)(p_pbuffer->p_payload - IPV6_IP_HEADER_SIZE);
  511. // Change byte order of IP header given to application.
  512. p_iphdr->length = NTOHS(p_iphdr->length);
  513. p_iphdr->flowlabel = NTOHS(p_iphdr->flowlabel);
  514. switch (p_iphdr->next_header)
  515. {
  516. case IPV6_NEXT_HEADER_ICMP6:
  517. IPV6_TRC("Got ICMPv6 packet.");
  518. IPV6_MUTEX_UNLOCK();
  519. err_code = icmp6_input(p_interface, p_iphdr, p_pbuffer);
  520. IPV6_MUTEX_LOCK();
  521. break;
  522. case IPV6_NEXT_HEADER_UDP:
  523. IPV6_TRC("Got UDP packet.");
  524. IPV6_MUTEX_UNLOCK();
  525. err_code = udp_input(p_interface, p_iphdr, p_pbuffer);
  526. IPV6_MUTEX_LOCK();
  527. break;
  528. default:
  529. IPV6_ERR("Got unsupported protocol packet. Protocol ID = 0x%x!",
  530. p_iphdr->next_header);
  531. #if (IPV6_ENABLE_USNUPORTED_PROTOCOLS_TO_APPLICATION == 1)
  532. app_notify_rx_data(p_interface, p_pbuffer);
  533. #endif
  534. break;
  535. }
  536. // Free packet buffer unless marked explicitly as pending
  537. if (err_code != IOT_IPV6_ERR_PENDING)
  538. {
  539. UNUSED_VARIABLE(iot_pbuffer_free(p_pbuffer, true));
  540. }
  541. return err_code;
  542. }
  543. /**@brief Function for receiving 6LoWPAN module events.
  544. *
  545. * @param[in] p_6lo_interface Pointer to 6LoWPAN interface.
  546. * @param[in] p_6lo_event Pointer to 6LoWPAN related event.
  547. *
  548. * @return None.
  549. */
  550. static void ble_6lowpan_evt_handler(iot_interface_t * p_interface,
  551. ble_6lowpan_event_t * p_6lo_event)
  552. {
  553. bool rx_failure = false;
  554. uint32_t err_code;
  555. uint32_t interface_id;
  556. iot_pbuffer_t * p_pbuffer;
  557. iot_pbuffer_alloc_param_t pbuff_param;
  558. IPV6_MUTEX_LOCK();
  559. IPV6_ENTRY();
  560. IPV6_TRC("In 6LoWPAN Handler:");
  561. interface_id = interface_get_by_6lo(p_interface);
  562. switch (p_6lo_event->event_id)
  563. {
  564. case BLE_6LO_EVT_ERROR:
  565. {
  566. IPV6_ERR("Got error, with result %08lx", p_6lo_event->event_result);
  567. break;
  568. }
  569. case BLE_6LO_EVT_INTERFACE_ADD:
  570. {
  571. IPV6_TRC("New interface established!");
  572. // Add interface to internal table.
  573. err_code = interface_add(p_interface, &interface_id);
  574. if (NRF_SUCCESS == err_code)
  575. {
  576. IPV6_TRC("Added new network interface to internal table.");
  577. err_code = iot_context_manager_table_alloc(p_interface);
  578. if (err_code == NRF_SUCCESS)
  579. {
  580. IPV6_TRC("Successfully allocated context table!");
  581. }
  582. else
  583. {
  584. IPV6_ERR("Failed to allocate context table!");
  585. }
  586. // Increase number of up interfaces.
  587. m_interfaces_count++;
  588. // Notify application.
  589. app_notify_interface_add(p_interface);
  590. }
  591. else
  592. {
  593. IPV6_ERR("Cannot add new interface. Table is full.");
  594. }
  595. break;
  596. }
  597. case BLE_6LO_EVT_INTERFACE_DELETE:
  598. {
  599. IPV6_TRC("Interface disconnected!");
  600. if (interface_id < IPV6_MAX_INTERFACE)
  601. {
  602. IPV6_TRC("Removed network interface.");
  603. // Notify application.
  604. app_notify_interface_delete(p_interface);
  605. err_code = iot_context_manager_table_free(p_interface);
  606. if (err_code == NRF_SUCCESS)
  607. {
  608. IPV6_TRC("Successfully freed context table!");
  609. }
  610. // Decrease number of up interfaces.
  611. m_interfaces_count--;
  612. // Remove interface from internal table.
  613. interface_delete(interface_id);
  614. }
  615. break;
  616. }
  617. case BLE_6LO_EVT_INTERFACE_DATA_RX:
  618. {
  619. IPV6_TRC("Got data with size = %d!",
  620. p_6lo_event->event_param.rx_event_param.packet_len);
  621. IPV6_TRC("Data: ");
  622. IPV6_DUMP(p_6lo_event->event_param.rx_event_param.p_packet,
  623. p_6lo_event->event_param.rx_event_param.packet_len);
  624. if (interface_id < IPV6_MAX_INTERFACE)
  625. {
  626. if (p_6lo_event->event_result == NRF_ERROR_NOT_FOUND)
  627. {
  628. IPV6_ERR("Cannot restore IPv6 addresses!");
  629. IPV6_ERR("Source CID = 0x%x, Destination CID = 0x%x",
  630. p_6lo_event->event_param.rx_event_param.rx_contexts.src_cntxt_id,
  631. p_6lo_event->event_param.rx_event_param.rx_contexts.dest_cntxt_id);
  632. // Indicates failure.
  633. rx_failure = true;
  634. break;
  635. }
  636. // Check if packet is for us.
  637. ipv6_addr_t * p_addr =
  638. (ipv6_addr_t *)&p_6lo_event->event_param.rx_event_param.p_packet[DEST_ADDR_OFFSET];
  639. err_code = addr_check(interface_id, p_addr, true);
  640. // If no address found - drop message.
  641. if (err_code != NRF_SUCCESS)
  642. {
  643. IPV6_ERR("Packet received on unknown address!");
  644. rx_failure = true;
  645. break;
  646. }
  647. // Try to allocate pbuffer, with no memory.
  648. pbuff_param.flags = PBUFFER_FLAG_NO_MEM_ALLOCATION;
  649. pbuff_param.type = RAW_PACKET_TYPE;
  650. pbuff_param.length = p_6lo_event->event_param.rx_event_param.packet_len;
  651. // Try to allocate pbuffer for receiving data.
  652. err_code = iot_pbuffer_allocate(&pbuff_param, &p_pbuffer);
  653. if (err_code == NRF_SUCCESS)
  654. {
  655. p_pbuffer->p_memory = p_6lo_event->event_param.rx_event_param.p_packet;
  656. p_pbuffer->p_payload = p_pbuffer->p_memory + IPV6_IP_HEADER_SIZE;
  657. p_pbuffer->length -= IPV6_IP_HEADER_SIZE;
  658. // Execute multiplexer.
  659. err_code = ipv6_input(p_interface, p_pbuffer);
  660. if (err_code != NRF_SUCCESS)
  661. {
  662. IPV6_ERR("Failed while processing packet, error = 0x%08lX!", err_code);
  663. }
  664. }
  665. else
  666. {
  667. IPV6_ERR("Failed to allocate packet buffer!");
  668. rx_failure = true;
  669. }
  670. }
  671. else
  672. {
  673. IPV6_ERR("[6LOWPAN]: Got data to unknown interface!");
  674. rx_failure = true;
  675. }
  676. break;
  677. }
  678. default:
  679. break;
  680. }
  681. if (rx_failure == true)
  682. {
  683. UNUSED_VARIABLE(nrf_free(p_6lo_event->event_param.rx_event_param.p_packet));
  684. }
  685. IPV6_EXIT();
  686. IPV6_MUTEX_UNLOCK();
  687. }
  688. uint32_t ipv6_init(const ipv6_init_t * p_init)
  689. {
  690. uint32_t index;
  691. uint32_t err_code;
  692. ble_6lowpan_init_t init_params;
  693. NULL_PARAM_CHECK(p_init);
  694. NULL_PARAM_CHECK(p_init->p_eui64);
  695. NULL_PARAM_CHECK(p_init->event_handler);
  696. SDK_MUTEX_INIT(m_ipv6_mutex);
  697. IPV6_MUTEX_LOCK();
  698. IPV6_ENTRY();
  699. // Initialize related modules.
  700. UNUSED_VARIABLE(nrf_mem_init());
  701. UNUSED_VARIABLE(iot_pbuffer_init());
  702. // Initialize submodules of IPv6 stack.
  703. UNUSED_VARIABLE(udp_init());
  704. UNUSED_VARIABLE(icmp6_init());
  705. // Initialize context manager.
  706. UNUSED_VARIABLE(iot_context_manager_init());
  707. IPV6_ADDRESS_INITIALIZE(IPV6_ADDR_ANY);
  708. // Set application event handler.
  709. m_event_handler = p_init->event_handler;
  710. // Clear number of interfaces.
  711. m_interfaces_count = 0;
  712. // Clear network interfaces.
  713. for (index = 0; index < IPV6_MAX_INTERFACE; index++)
  714. {
  715. interface_reset(&m_interfaces[index]);
  716. }
  717. // Clear all addresses.
  718. for (index = 0; index < IPV6_MAX_ADDRESS_COUNT; index++)
  719. {
  720. addr_free(index, false);
  721. }
  722. // 6LoWPAN module initialization.
  723. init_params.p_eui64 = p_init->p_eui64;
  724. init_params.event_handler = ble_6lowpan_evt_handler;
  725. err_code = ble_6lowpan_init(&init_params);
  726. IPV6_EXIT();
  727. IPV6_MUTEX_UNLOCK();
  728. return err_code;
  729. }
  730. uint32_t ipv6_address_set(const iot_interface_t * p_interface,
  731. const ipv6_addr_conf_t * p_addr)
  732. {
  733. VERIFY_MODULE_IS_INITIALIZED();
  734. NULL_PARAM_CHECK(p_addr);
  735. NULL_PARAM_CHECK(p_interface);
  736. uint32_t err_code;
  737. IPV6_MUTEX_LOCK();
  738. IPV6_ENTRY();
  739. err_code = addr_set(p_interface, p_addr);
  740. IPV6_EXIT();
  741. IPV6_MUTEX_UNLOCK();
  742. return err_code;
  743. }
  744. uint32_t ipv6_address_check(const iot_interface_t * p_interface,
  745. const ipv6_addr_t * p_addr)
  746. {
  747. VERIFY_MODULE_IS_INITIALIZED();
  748. NULL_PARAM_CHECK(p_addr);
  749. NULL_PARAM_CHECK(p_interface);
  750. uint32_t err_code;
  751. IPV6_MUTEX_LOCK();
  752. IPV6_ENTRY();
  753. uint32_t interface_id = (uint32_t)p_interface->p_upper_stack;
  754. err_code = addr_check(interface_id, p_addr, false);
  755. IPV6_EXIT();
  756. IPV6_MUTEX_UNLOCK();
  757. return err_code;
  758. }
  759. uint32_t ipv6_address_find_best_match(iot_interface_t ** pp_interface,
  760. ipv6_addr_t * p_addr_r,
  761. const ipv6_addr_t * p_addr_f)
  762. {
  763. VERIFY_MODULE_IS_INITIALIZED();
  764. NULL_PARAM_CHECK(p_addr_f);
  765. NULL_PARAM_CHECK(pp_interface);
  766. uint32_t index;
  767. uint32_t err_code;
  768. uint32_t addr_index;
  769. uint32_t match_temp = 0;
  770. uint32_t match_best = 0;
  771. ipv6_addr_t * p_best_addr = NULL;
  772. IPV6_MUTEX_LOCK();
  773. err_code = interface_find(pp_interface, p_addr_f);
  774. if (err_code == NRF_SUCCESS && p_addr_r)
  775. {
  776. uint32_t interface_id = (uint32_t)(*pp_interface)->p_upper_stack;
  777. for (index = 0; index < IPV6_MAX_ADDRESS_PER_INTERFACE; index++)
  778. {
  779. addr_index = m_interfaces[interface_id].addr_range[index];
  780. if (addr_index != IPV6_INVALID_ADDR_INDEX)
  781. {
  782. if (m_address_table[addr_index].state == IPV6_ADDR_STATE_PREFERRED)
  783. {
  784. match_temp = addr_bit_equal(p_addr_f, &m_address_table[addr_index].addr);
  785. if (match_temp >= match_best)
  786. {
  787. match_best = match_temp;
  788. p_best_addr = &m_address_table[addr_index].addr;
  789. }
  790. }
  791. }
  792. }
  793. // No address found.
  794. if (p_best_addr == NULL)
  795. {
  796. // Set undefined :: address.
  797. IPV6_ADDRESS_INITIALIZE(p_addr_r);
  798. }
  799. else
  800. {
  801. memcpy(p_addr_r->u8, p_best_addr->u8, IPV6_ADDR_SIZE);
  802. }
  803. }
  804. IPV6_MUTEX_UNLOCK();
  805. return err_code;
  806. }
  807. uint32_t ipv6_address_remove(const iot_interface_t * p_interface,
  808. const ipv6_addr_t * p_addr)
  809. {
  810. VERIFY_MODULE_IS_INITIALIZED();
  811. NULL_PARAM_CHECK(p_addr);
  812. NULL_PARAM_CHECK(p_interface);
  813. uint32_t index;
  814. uint32_t err_code;
  815. uint32_t addr_index;
  816. IPV6_MUTEX_LOCK();
  817. IPV6_ENTRY();
  818. uint32_t interface_id = (uint32_t)p_interface->p_upper_stack;
  819. err_code = (IOT_IPV6_ERR_BASE | NRF_ERROR_NOT_FOUND);
  820. for (index = 0; index < IPV6_MAX_ADDRESS_PER_INTERFACE; index++)
  821. {
  822. addr_index = m_interfaces[interface_id].addr_range[index];
  823. if (addr_index != IPV6_INVALID_ADDR_INDEX)
  824. {
  825. if (0 == IPV6_ADDRESS_CMP(&m_address_table[addr_index].addr, p_addr))
  826. {
  827. m_interfaces[interface_id].addr_range[index] = IPV6_INVALID_ADDR_INDEX;
  828. // Remove address if no reference to interface found.
  829. addr_free(index, true);
  830. err_code = NRF_SUCCESS;
  831. break;
  832. }
  833. }
  834. }
  835. IPV6_EXIT();
  836. IPV6_MUTEX_UNLOCK();
  837. return err_code;
  838. }
  839. uint32_t ipv6_send(const iot_interface_t * p_interface, iot_pbuffer_t * p_packet)
  840. {
  841. VERIFY_MODULE_IS_INITIALIZED();
  842. NULL_PARAM_CHECK(p_packet);
  843. NULL_PARAM_CHECK(p_interface);
  844. uint32_t err_code;
  845. IPV6_MUTEX_LOCK();
  846. IPV6_ENTRY();
  847. err_code = ble_6lowpan_interface_send(p_interface,
  848. p_packet->p_payload,
  849. p_packet->length);
  850. if (err_code != NRF_SUCCESS)
  851. {
  852. IPV6_ERR("Cannot send packet!");
  853. }
  854. // Free pbuffer, without freeing memory.
  855. UNUSED_VARIABLE(iot_pbuffer_free(p_packet, false));
  856. IPV6_EXIT();
  857. IPV6_MUTEX_UNLOCK();
  858. return err_code;
  859. }