icmpv6_neighbor.c 11 KB

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