icmpv6_neighbor.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /****************************************************************************
  2. * net/icmpv6/icmpv6_neighbor.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 <unistd.h>
  40. #include <string.h>
  41. #include <semaphore.h>
  42. #include <time.h>
  43. #include <debug.h>
  44. #include <netinet/in.h>
  45. #include <net/if.h>
  46. #include <nuttx/semaphore.h>
  47. #include <nuttx/net/net.h>
  48. #include <nuttx/net/netdev.h>
  49. #include <nuttx/net/ip.h>
  50. #include <nuttx/net/icmpv6.h>
  51. #include "netdev/netdev.h"
  52. #include "devif/devif.h"
  53. #include "inet/inet.h"
  54. #include "neighbor/neighbor.h"
  55. #include "route/route.h"
  56. #include "icmpv6/icmpv6.h"
  57. #ifdef CONFIG_NET_ICMPv6_NEIGHBOR
  58. /****************************************************************************
  59. * Pre-processor Definitions
  60. ****************************************************************************/
  61. #define CONFIG_ICMPv6_NEIGHBOR_DELAYSEC \
  62. (CONFIG_ICMPv6_NEIGHBOR_DELAYMSEC / 1000)
  63. #define CONFIG_ICMPv6_NEIGHBOR_DELAYNSEC \
  64. ((CONFIG_ICMPv6_NEIGHBOR_DELAYMSEC - 1000*CONFIG_ICMPv6_NEIGHBOR_DELAYSEC) * 1000000)
  65. /****************************************************************************
  66. * Private Types
  67. ****************************************************************************/
  68. /* This structure holds the state of the send operation until it can be
  69. * operated upon from the event handler.
  70. */
  71. struct icmpv6_neighbor_s
  72. {
  73. FAR struct devif_callback_s *snd_cb; /* Reference to callback instance */
  74. sem_t snd_sem; /* Used to wake up the waiting thread */
  75. uint8_t snd_retries; /* Retry count */
  76. volatile bool snd_sent; /* True: if request sent */
  77. uint8_t snd_ifname[IFNAMSIZ]; /* Interface name */
  78. net_ipv6addr_t snd_ipaddr; /* The IPv6 address to be queried */
  79. };
  80. /****************************************************************************
  81. * Private Functions
  82. ****************************************************************************/
  83. /****************************************************************************
  84. * Name: icmpv6_neighbor_eventhandler
  85. ****************************************************************************/
  86. static uint16_t icmpv6_neighbor_eventhandler(FAR struct net_driver_s *dev,
  87. FAR void *pvconn,
  88. FAR void *priv, uint16_t flags)
  89. {
  90. FAR struct icmpv6_neighbor_s *state = (FAR struct icmpv6_neighbor_s *)priv;
  91. ninfo("flags: %04x sent: %d\n", flags, state->snd_sent);
  92. if (state)
  93. {
  94. /* Is this the device that we need to route this request? */
  95. if (strncmp((FAR const char *)dev->d_ifname,
  96. (FAR const char *)state->snd_ifname, IFNAMSIZ) != 0)
  97. {
  98. /* No... pass on this one and wait for the device that we want */
  99. return flags;
  100. }
  101. /* Check if the outgoing packet is available. It may have been claimed
  102. * by a send event handler serving a different thread -OR- if the output
  103. * buffer currently contains unprocessed incoming data. In these cases
  104. * we will just have to wait for the next polling cycle.
  105. */
  106. if (dev->d_sndlen > 0 || (flags & ICMPv6_NEWDATA) != 0)
  107. {
  108. /* Another thread has beat us sending data or the buffer is busy,
  109. * Check for a timeout. If not timed out, wait for the next
  110. * polling cycle and check again.
  111. */
  112. /* REVISIT: No timeout. Just wait for the next polling cycle */
  113. return flags;
  114. }
  115. /* It looks like we are good to send the data.
  116. *
  117. * Copy the packet data into the device packet buffer and send it.
  118. */
  119. icmpv6_solicit(dev, state->snd_ipaddr);
  120. IFF_SET_IPv6(dev->d_flags);
  121. /* Don't allow any further call backs. */
  122. state->snd_sent = true;
  123. state->snd_cb->flags = 0;
  124. state->snd_cb->priv = NULL;
  125. state->snd_cb->event = NULL;
  126. /* Wake up the waiting thread */
  127. nxsem_post(&state->snd_sem);
  128. }
  129. return flags;
  130. }
  131. /****************************************************************************
  132. * Public Functions
  133. ****************************************************************************/
  134. /****************************************************************************
  135. * Name: icmpv6_neighbor
  136. *
  137. * Description:
  138. * The icmpv6_solicit() call may be to send an ICMPv6 Neighbor
  139. * Solicitation to resolve an IPv6 address. This function first checks if
  140. * the IPv6 address is already in the Neighbor Table. If so, then it
  141. * returns success immediately.
  142. *
  143. * If the requested IPv6 address in not in the Neighbor Table, then this
  144. * function will send the Neighbor Solicitation, delay, then check if the
  145. * IP address is now in the Neighbor able. It will repeat this sequence
  146. * until either (1) the IPv6 address mapping is now in the Neighbor table,
  147. * or (2) a configurable number of timeouts occur without receiving the
  148. * ICMPv6 Neighbor Advertisement.
  149. *
  150. * Input Parameters:
  151. * ipaddr The IPv6 address to be queried.
  152. *
  153. * Returned Value:
  154. * Zero (OK) is returned on success and the IP address mapping can now be
  155. * found in the Neighbor Table. On error a negated errno value is returned:
  156. *
  157. * -ETIMEDOUT: The number or retry counts has been exceed.
  158. * -EHOSTUNREACH: Could not find a route to the host
  159. *
  160. * Assumptions:
  161. * This function is called from the normal tasking context.
  162. *
  163. ****************************************************************************/
  164. int icmpv6_neighbor(const net_ipv6addr_t ipaddr)
  165. {
  166. FAR struct net_driver_s *dev;
  167. struct icmpv6_notify_s notify;
  168. struct timespec delay;
  169. struct icmpv6_neighbor_s state;
  170. net_ipv6addr_t lookup;
  171. int ret;
  172. /* First check if destination is a local broadcast or a multicast address.
  173. *
  174. * - IPv6 multicast addresses are have the high-order octet of the
  175. * addresses=0xff (ff00::/8.)
  176. */
  177. if (net_ipv6addr_cmp(ipaddr, g_ipv6_unspecaddr) ||
  178. net_is_addr_mcast(ipaddr))
  179. {
  180. /* We don't need to send the Neighbor Solicitation. But for the case
  181. * of the Multicast address, a routing able entry will be required.
  182. */
  183. return OK;
  184. }
  185. /* Get the device that can route this request */
  186. dev = netdev_findby_ripv6addr(g_ipv6_unspecaddr, ipaddr);
  187. if (!dev)
  188. {
  189. nerr("ERROR: Unreachable: %08lx\n", (unsigned long)ipaddr);
  190. ret = -EHOSTUNREACH;
  191. goto errout;
  192. }
  193. /* Check if the destination address is on the local network. */
  194. if (net_ipv6addr_maskcmp(ipaddr, dev->d_ipv6addr, dev->d_ipv6netmask))
  195. {
  196. /* Yes.. use the input address for the lookup */
  197. net_ipv6addr_copy(lookup, ipaddr);
  198. }
  199. else
  200. {
  201. /* Destination address is not on the local network */
  202. #ifdef CONFIG_NET_ROUTE
  203. /* We have a routing table.. find the correct router to use in
  204. * this case (or, as a fall-back, use the device's default router
  205. * address). We will use the router IP address instead of the
  206. * destination address when determining the MAC address.
  207. */
  208. netdev_ipv6_router(dev, ipaddr, lookup);
  209. #else
  210. /* Use the device's default router IP address instead of the
  211. * destination address when determining the MAC address.
  212. */
  213. net_ipv6addr_copy(lookup, dev->d_ipv6draddr);
  214. #endif
  215. }
  216. /* Allocate resources to receive a callback. This and the following
  217. * initialization is performed with the network lock because we don't
  218. * want anything to happen until we are ready.
  219. */
  220. net_lock();
  221. state.snd_cb = icmpv6_callback_alloc(dev);
  222. if (!state.snd_cb)
  223. {
  224. nerr("ERROR: Failed to allocate a callback\n");
  225. ret = -ENOMEM;
  226. goto errout_with_lock;
  227. }
  228. /* Initialize the state structure with the network locked.
  229. *
  230. * This semaphore is used for signaling and, hence, should not have
  231. * priority inheritance enabled.
  232. */
  233. (void)nxsem_init(&state.snd_sem, 0, 0); /* Doesn't really fail */
  234. nxsem_setprotocol(&state.snd_sem, SEM_PRIO_NONE);
  235. state.snd_retries = 0; /* No retries yet */
  236. net_ipv6addr_copy(state.snd_ipaddr, lookup); /* IP address to query */
  237. /* Remember the routing device name */
  238. strncpy((FAR char *)state.snd_ifname, (FAR const char *)dev->d_ifname,
  239. IFNAMSIZ);
  240. /* Now loop, testing if the address mapping is in the Neighbor Table and
  241. * re-sending the Neighbor Solicitation if it is not.
  242. */
  243. /* The optimal delay would be the worst case round trip time. */
  244. delay.tv_sec = CONFIG_ICMPv6_NEIGHBOR_DELAYSEC;
  245. delay.tv_nsec = CONFIG_ICMPv6_NEIGHBOR_DELAYNSEC;
  246. ret = -ETIMEDOUT; /* Assume a timeout failure */
  247. while (state.snd_retries < CONFIG_ICMPv6_NEIGHBOR_MAXTRIES)
  248. {
  249. /* Check if the address mapping is present in the Neighbor Table. This
  250. * is only really meaningful on the first time through the loop.
  251. *
  252. * NOTE: If the Neighbor Table is large than this could be a performance
  253. * issue.
  254. */
  255. if (neighbor_lookup(lookup, NULL) >= 0)
  256. {
  257. /* We have it! Break out with success */
  258. ret = OK;
  259. break;
  260. }
  261. /* Set up the Neighbor Advertisement wait BEFORE we send the Neighbor
  262. * Solicitation.
  263. */
  264. icmpv6_wait_setup(lookup, &notify);
  265. /* Arm/re-arm the callback */
  266. state.snd_sent = false;
  267. state.snd_cb->flags = ICMPv6_POLL;
  268. state.snd_cb->priv = (FAR void *)&state;
  269. state.snd_cb->event = icmpv6_neighbor_eventhandler;
  270. /* Notify the device driver that new TX data is available. */
  271. netdev_txnotify_dev(dev);
  272. /* Wait for the send to complete or an error to occur.
  273. * net_lockedwait will also terminate if a signal is received.
  274. */
  275. do
  276. {
  277. (void)net_lockedwait(&state.snd_sem);
  278. }
  279. while (!state.snd_sent);
  280. /* Now wait for response to the Neighbor Advertisement to be received. */
  281. ret = icmpv6_wait(&notify, &delay);
  282. /* icmpv6_wait will return OK if and only if the matching Neighbor
  283. * Advertisement is received. Otherwise, it will return -ETIMEDOUT.
  284. */
  285. if (ret == OK)
  286. {
  287. break;
  288. }
  289. /* Increment the retry count and double the delay time */
  290. clock_timespec_add(&delay, &delay, &delay);
  291. state.snd_retries++;
  292. }
  293. nxsem_destroy(&state.snd_sem);
  294. icmpv6_callback_free(dev, state.snd_cb);
  295. errout_with_lock:
  296. net_unlock();
  297. errout:
  298. return ret;
  299. }
  300. #endif /* CONFIG_NET_ICMPv6_NEIGHBOR */