netdev_findbyaddr.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /****************************************************************************
  2. * net/netdev/netdev_findbyaddr.c
  3. *
  4. * Copyright (C) 2007-2009, 2014-2015, 2017-2018 Gregory Nutt. All rights
  5. * reserved.
  6. * Author: Gregory Nutt <gnutt@nuttx.org>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * 3. Neither the name NuttX nor the names of its contributors may be
  19. * used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ****************************************************************************/
  36. /****************************************************************************
  37. * Included Files
  38. ****************************************************************************/
  39. #include <nuttx/config.h>
  40. #include <stdbool.h>
  41. #include <string.h>
  42. #include <errno.h>
  43. #include <debug.h>
  44. #include <netinet/in.h>
  45. #include <nuttx/net/netdev.h>
  46. #include <nuttx/net/ip.h>
  47. #include "utils/utils.h"
  48. #include "devif/devif.h"
  49. #include "inet/inet.h"
  50. #include "route/route.h"
  51. #include "netdev/netdev.h"
  52. /****************************************************************************
  53. * Public Functions
  54. ****************************************************************************/
  55. /****************************************************************************
  56. * Name: netdev_findby_lipv4addr
  57. *
  58. * Description:
  59. * Find a previously registered network device by matching a local address
  60. * with the subnet served by the device. Only "up" devices are considered
  61. * (since a "down" device has no meaningful address).
  62. *
  63. * Input Parameters:
  64. * lipaddr - Local, IPv4 address assigned to the network device. Or any
  65. * IPv4 address on the sub-net served by the network device.
  66. *
  67. * Returned Value:
  68. * Pointer to driver on success; null on failure
  69. *
  70. ****************************************************************************/
  71. #ifdef CONFIG_NET_IPv4
  72. FAR struct net_driver_s *netdev_findby_lipv4addr(in_addr_t lipaddr)
  73. {
  74. FAR struct net_driver_s *dev;
  75. /* Examine each registered network device */
  76. net_lock();
  77. for (dev = g_netdevices; dev; dev = dev->flink)
  78. {
  79. /* Is the interface in the "up" state? */
  80. if ((dev->d_flags & IFF_UP) != 0)
  81. {
  82. /* Yes.. check for an address match (under the netmask) */
  83. if (net_ipv4addr_maskcmp(dev->d_ipaddr, lipaddr,
  84. dev->d_netmask))
  85. {
  86. /* Its a match */
  87. net_unlock();
  88. return dev;
  89. }
  90. }
  91. }
  92. /* No device with the matching address found */
  93. net_unlock();
  94. return NULL;
  95. }
  96. #endif /* CONFIG_NET_IPv4 */
  97. /****************************************************************************
  98. * Name: netdev_findby_lipv6addr
  99. *
  100. * Description:
  101. * Find a previously registered network device by matching a local address
  102. * with the subnet served by the device. Only "up" devices are considered
  103. * (since a "down" device has no meaningful address).
  104. *
  105. * Input Parameters:
  106. * lipaddr - Local, IPv6 address assigned to the network device. Or any
  107. * IPv6 address on the sub-net served by the network device.
  108. *
  109. * Returned Value:
  110. * Pointer to driver on success; null on failure
  111. *
  112. ****************************************************************************/
  113. #ifdef CONFIG_NET_IPv6
  114. FAR struct net_driver_s *netdev_findby_lipv6addr(const net_ipv6addr_t lipaddr)
  115. {
  116. FAR struct net_driver_s *dev;
  117. /* Examine each registered network device */
  118. net_lock();
  119. for (dev = g_netdevices; dev; dev = dev->flink)
  120. {
  121. /* Is the interface in the "up" state? */
  122. if ((dev->d_flags & IFF_UP) != 0)
  123. {
  124. /* Yes.. check for an address match (under the netmask) */
  125. if (net_ipv6addr_maskcmp(dev->d_ipv6addr, lipaddr,
  126. dev->d_ipv6netmask))
  127. {
  128. /* Its a match */
  129. net_unlock();
  130. return dev;
  131. }
  132. }
  133. }
  134. /* No device with the matching address found */
  135. net_unlock();
  136. return NULL;
  137. }
  138. #endif /* CONFIG_NET_IPv6 */
  139. /****************************************************************************
  140. * Name: netdev_findby_ripv4addr
  141. *
  142. * Description:
  143. * Find a previously registered network device by matching the remote
  144. * IPv4 address that can be reached by the device.
  145. *
  146. * Input Parameters:
  147. * lipaddr - Local, bound address of a connection (used only if ripaddr is
  148. * the broadcast address).
  149. * ripaddr - Remote address of a connection to use in the lookup
  150. *
  151. * Returned Value:
  152. * Pointer to driver on success; null on failure
  153. *
  154. ****************************************************************************/
  155. #ifdef CONFIG_NET_IPv4
  156. FAR struct net_driver_s *netdev_findby_ripv4addr(in_addr_t lipaddr,
  157. in_addr_t ripaddr)
  158. {
  159. struct net_driver_s *dev;
  160. #ifdef CONFIG_NET_ROUTE
  161. in_addr_t router;
  162. int ret;
  163. #endif
  164. /* First, check if this is the broadcast IP address */
  165. if (net_ipv4addr_cmp(ripaddr, INADDR_BROADCAST))
  166. {
  167. /* Yes.. Check the local, bound address. Is it INADDR_ANY? */
  168. if (net_ipv4addr_cmp(lipaddr, INADDR_ANY))
  169. {
  170. /* Yes.. In this case, I think we are supposed to send the
  171. * broadcast packet out ALL locally available networks. I am not
  172. * sure of that and, in any event, there is nothing we can do
  173. * about that here.
  174. */
  175. return netdev_default();
  176. }
  177. else
  178. {
  179. /* Return the device associated with the local address */
  180. return netdev_findby_lipv4addr(lipaddr);
  181. }
  182. }
  183. /* Check if the address maps to a locally available network */
  184. dev = netdev_findby_lipv4addr(ripaddr);
  185. if (dev)
  186. {
  187. return dev;
  188. }
  189. /* No.. The address lies on an external network */
  190. #ifdef CONFIG_NET_ROUTE
  191. /* If we have a routing table, then perhaps we can find the local
  192. * address of a router that can forward packets to the external network.
  193. */
  194. ret = net_ipv4_router(ripaddr, &router);
  195. if (ret >= 0)
  196. {
  197. /* Success... try to find the network device associated with the local
  198. * router address
  199. */
  200. dev = netdev_findby_lipv4addr(router);
  201. if (dev)
  202. {
  203. return dev;
  204. }
  205. }
  206. #endif /* CONFIG_NET_ROUTE */
  207. /* The above lookup will fail if the packet is being sent out of our
  208. * out subnet to a router and there is no routing information. Let's
  209. * try the default network device.
  210. */
  211. return netdev_default();
  212. }
  213. #endif /* CONFIG_NET_IPv4 */
  214. /****************************************************************************
  215. * Name: netdev_findby_ripv6addr
  216. *
  217. * Description:
  218. * Find a previously registered network device by matching the remote
  219. * IPv6 address that can be reached by the device.
  220. *
  221. * Input Parameters:
  222. * lipaddr - Local, bound address of a connection (used only if ripaddr is
  223. * a multicast address).
  224. * ripaddr - Remote address of a connection to use in the lookup
  225. *
  226. * Returned Value:
  227. * Pointer to driver on success; null on failure
  228. *
  229. ****************************************************************************/
  230. #ifdef CONFIG_NET_IPv6
  231. FAR struct net_driver_s *netdev_findby_ripv6addr(const net_ipv6addr_t lipaddr,
  232. const net_ipv6addr_t ripaddr)
  233. {
  234. struct net_driver_s *dev;
  235. #ifdef CONFIG_NET_ROUTE
  236. net_ipv6addr_t router;
  237. int ret;
  238. #endif
  239. /* First, check if this is the multicast IP address */
  240. if (net_is_addr_mcast(ripaddr))
  241. {
  242. /* Yes.. Check the local, bound address. Is it the IPv6 unspecified
  243. * address?
  244. */
  245. if (net_ipv6addr_cmp(lipaddr, g_ipv6_unspecaddr))
  246. {
  247. /* Yes.. In this case, I think we are supposed to send the
  248. * broadcast packet out ALL locally available networks. I am not
  249. * sure of that and, in any event, there is nothing we can do
  250. * about that here.
  251. */
  252. return netdev_default();
  253. }
  254. else
  255. {
  256. /* Return the device associated with the local address */
  257. return netdev_findby_lipv6addr(lipaddr);
  258. }
  259. }
  260. /* Check if the address maps to a locally available network */
  261. dev = netdev_findby_lipv6addr(ripaddr);
  262. if (dev)
  263. {
  264. return dev;
  265. }
  266. /* No.. The address lies on an external network */
  267. #ifdef CONFIG_NET_ROUTE
  268. /* If we have a routing table, then perhaps we can find the local
  269. * address of a router that can forward packets to the external network.
  270. */
  271. ret = net_ipv6_router(ripaddr, router);
  272. if (ret >= 0)
  273. {
  274. /* Success... try to find the network device associated with the local
  275. * router address
  276. */
  277. dev = netdev_findby_lipv6addr(router);
  278. if (dev)
  279. {
  280. return dev;
  281. }
  282. }
  283. #endif /* CONFIG_NET_ROUTE */
  284. /* The above lookup will fail if the packet is being sent out of our
  285. * out subnet to a router and there is no routing information. Let's
  286. * try the default network device.
  287. */
  288. return netdev_default();
  289. }
  290. #endif /* CONFIG_NET_IPv6 */