icmpv6_notify.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /****************************************************************************
  2. * net/icmpv6/icmpv6_notify.c
  3. *
  4. * Copyright (C) 2015-2016 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <string.h>
  40. #include <time.h>
  41. #include <semaphore.h>
  42. #include <errno.h>
  43. #include <debug.h>
  44. #include <netinet/in.h>
  45. #include <nuttx/irq.h>
  46. #include <nuttx/semaphore.h>
  47. #include <nuttx/net/net.h>
  48. #include "icmpv6/icmpv6.h"
  49. #ifdef CONFIG_NET_ICMPv6_NEIGHBOR
  50. /****************************************************************************
  51. * Pre-processor Definitions
  52. ****************************************************************************/
  53. /****************************************************************************
  54. * Private Types
  55. ****************************************************************************/
  56. /****************************************************************************
  57. * Private Data
  58. ****************************************************************************/
  59. /* List of tasks waiting for Neighbor Discover events */
  60. static struct icmpv6_notify_s *g_icmpv6_waiters;
  61. /****************************************************************************
  62. * Private Functions
  63. ****************************************************************************/
  64. /****************************************************************************
  65. * Public Functions
  66. ****************************************************************************/
  67. /****************************************************************************
  68. * Name: icmpv6_wait_setup
  69. *
  70. * Description:
  71. * Called BEFORE an Neighbor Solicitation is sent. This function sets up
  72. * the Neighbor Advertisement timeout before the Neighbor Solicitation
  73. * is sent so that there is no race condition when icmpv6_wait() is called.
  74. *
  75. * Assumptions:
  76. * This function is called from icmpv6_neighbor() and executes in the normal
  77. * tasking environment.
  78. *
  79. ****************************************************************************/
  80. void icmpv6_wait_setup(const net_ipv6addr_t ipaddr,
  81. FAR struct icmpv6_notify_s *notify)
  82. {
  83. irqstate_t flags;
  84. /* Initialize the wait structure */
  85. net_ipv6addr_copy(notify->nt_ipaddr, ipaddr);
  86. notify->nt_result = -ETIMEDOUT;
  87. /* This semaphore is used for signaling and, hence, should not have
  88. * priority inheritance enabled.
  89. */
  90. (void)nxsem_init(&notify->nt_sem, 0, 0);
  91. nxsem_setprotocol(&notify->nt_sem, SEM_PRIO_NONE);
  92. /* Add the wait structure to the list with interrupts disabled */
  93. flags = enter_critical_section();
  94. notify->nt_flink = g_icmpv6_waiters;
  95. g_icmpv6_waiters = notify;
  96. leave_critical_section(flags);
  97. }
  98. /****************************************************************************
  99. * Name: icmpv6_wait_cancel
  100. *
  101. * Description:
  102. * Cancel any wait set after icmpv6_wait_setup is called but before
  103. * icmpv6_wait()is called (icmpv6_wait() will automatically cancel the
  104. * wait).
  105. *
  106. * Assumptions:
  107. * This function may execute in the interrupt context when called from
  108. * icmpv6_wait().
  109. *
  110. ****************************************************************************/
  111. int icmpv6_wait_cancel(FAR struct icmpv6_notify_s *notify)
  112. {
  113. FAR struct icmpv6_notify_s *curr;
  114. FAR struct icmpv6_notify_s *prev;
  115. irqstate_t flags;
  116. int ret = -ENOENT;
  117. /* Remove our wait structure from the list (we may no longer be at the
  118. * head of the list).
  119. */
  120. flags = enter_critical_section();
  121. for (prev = NULL, curr = g_icmpv6_waiters;
  122. curr && curr != notify;
  123. prev = curr, curr = curr->nt_flink);
  124. DEBUGASSERT(curr && curr == notify);
  125. if (curr)
  126. {
  127. if (prev)
  128. {
  129. prev->nt_flink = notify->nt_flink;
  130. }
  131. else
  132. {
  133. g_icmpv6_waiters = notify->nt_flink;
  134. }
  135. ret = OK;
  136. }
  137. leave_critical_section(flags);
  138. (void)nxsem_destroy(&notify->nt_sem);
  139. return ret;
  140. }
  141. /****************************************************************************
  142. * Name: icmpv6_wait
  143. *
  144. * Description:
  145. * Called each time that a Neighbor Solicitation is sent. This function
  146. * will sleep until either: (1) the matching Neighbor Advertisement is
  147. * received, or (2) a timeout occurs.
  148. *
  149. * Assumptions:
  150. * This function is called from icmpv6_neighbor() and must execute with
  151. * the network locked.
  152. *
  153. ****************************************************************************/
  154. int icmpv6_wait(FAR struct icmpv6_notify_s *notify,
  155. FAR struct timespec *timeout)
  156. {
  157. struct timespec abstime;
  158. irqstate_t flags;
  159. int ret;
  160. /* And wait for the Neighbor Advertisement (or a timeout). Interrupts will
  161. * be re-enabled while we wait.
  162. */
  163. flags = enter_critical_section();
  164. DEBUGVERIFY(clock_gettime(CLOCK_REALTIME, &abstime));
  165. abstime.tv_sec += timeout->tv_sec;
  166. abstime.tv_nsec += timeout->tv_nsec;
  167. if (abstime.tv_nsec >= 1000000000)
  168. {
  169. abstime.tv_sec++;
  170. abstime.tv_nsec -= 1000000000;
  171. }
  172. /* REVISIT: If net_timedwait() is awakened with signal, we will return
  173. * the wrong error code.
  174. */
  175. (void)net_timedwait(&notify->nt_sem, &abstime);
  176. ret = notify->nt_result;
  177. /* Remove our wait structure from the list (we may no longer be at the
  178. * head of the list).
  179. */
  180. (void)icmpv6_wait_cancel(notify);
  181. /* Re-enable interrupts and return the result of the wait */
  182. leave_critical_section(flags);
  183. return ret;
  184. }
  185. /****************************************************************************
  186. * Name: icmpv6_notify
  187. *
  188. * Description:
  189. * Called each time that a Neighbor Advertisement is received in order to
  190. * wake-up any threads that may be waiting for this particular Neighbor
  191. * Advertisement.
  192. *
  193. * Assumptions:
  194. * This function is called from the MAC device driver indirectly through
  195. * icmpv6_icmpv6in() will execute with the network locked.
  196. *
  197. ****************************************************************************/
  198. void icmpv6_notify(net_ipv6addr_t ipaddr)
  199. {
  200. FAR struct icmpv6_notify_s *curr;
  201. /* Find an entry with the matching IP address in the list of waiters */
  202. for (curr = g_icmpv6_waiters; curr; curr = curr->nt_flink)
  203. {
  204. /* Does this entry match? If the result is okay, then we have
  205. * already notified this waiter and it has not yet taken the
  206. * entry from the list.
  207. */
  208. if (curr->nt_result != OK && net_ipv6addr_cmp(curr->nt_ipaddr, ipaddr))
  209. {
  210. /* Yes.. Signal the waiting, returning success */
  211. curr->nt_result = OK;
  212. nxsem_post(&curr->nt_sem);
  213. break;
  214. }
  215. }
  216. }
  217. #endif /* CONFIG_NET_ICMPv6_NEIGHBOR */