net_router.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /****************************************************************************
  2. * net/route/net_router.c
  3. *
  4. * Copyright (C) 2013-2015, 2017 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 <stdint.h>
  40. #include <string.h>
  41. #include <errno.h>
  42. #include <netinet/in.h>
  43. #include <nuttx/net/ip.h>
  44. #include "devif/devif.h"
  45. #include "route/cacheroute.h"
  46. #include "route/route.h"
  47. #if defined(CONFIG_NET) && defined(CONFIG_NET_ROUTE)
  48. /****************************************************************************
  49. * Pre-processor defintions
  50. ****************************************************************************/
  51. #ifdef CONFIG_ROUTE_IPv6_CACHEROUTE
  52. # define IPv4_ROUTER entry.router
  53. #else
  54. # define IPv4_ROUTER router
  55. #endif
  56. #ifdef CONFIG_ROUTE_IPv6_CACHEROUTE
  57. # define IPv6_ROUTER entry.router
  58. #else
  59. # define IPv6_ROUTER router
  60. #endif
  61. /****************************************************************************
  62. * Public Types
  63. ****************************************************************************/
  64. #ifdef CONFIG_NET_IPv4
  65. struct route_ipv4_match_s
  66. {
  67. in_addr_t target; /* Target IPv4 address on remote network */
  68. #ifdef CONFIG_ROUTE_IPv4_CACHEROUTE
  69. struct net_route_ipv4_s entry; /* Full entry from the IPv4 routing table */
  70. #else
  71. in_addr_t router; /* IPv4 address of router a local networks */
  72. #endif
  73. };
  74. #endif
  75. #ifdef CONFIG_NET_IPv6
  76. struct route_ipv6_match_s
  77. {
  78. net_ipv6addr_t target; /* Target IPv6 address on remote network */
  79. #ifdef CONFIG_ROUTE_IPv6_CACHEROUTE
  80. struct net_route_ipv6_s entry; /* Full entry from the IPv6 routing table */
  81. #else
  82. net_ipv6addr_t router; /* IPv6 address of router a local networks */
  83. #endif
  84. };
  85. #endif
  86. /****************************************************************************
  87. * Private Functions
  88. ****************************************************************************/
  89. /****************************************************************************
  90. * Name: net_ipv4_match
  91. *
  92. * Description:
  93. * Return 1 if the IPv4 route is available
  94. *
  95. * Input Parameters:
  96. * route - The next route to examine
  97. * arg - The match values (cast to void*)
  98. *
  99. * Returned Value:
  100. * 0 if the entry is not a match; 1 if the entry matched and was cleared.
  101. *
  102. ****************************************************************************/
  103. #ifdef CONFIG_NET_IPv4
  104. static int net_ipv4_match(FAR struct net_route_ipv4_s *route, FAR void *arg)
  105. {
  106. FAR struct route_ipv4_match_s *match = (FAR struct route_ipv4_match_s *)arg;
  107. /* To match, the masked target addresses must be the same. In the event
  108. * of multiple matches, only the first is returned. There is not (yet) any
  109. * concept for the precedence of networks.
  110. */
  111. if (net_ipv4addr_maskcmp(route->target, match->target, route->netmask))
  112. {
  113. #ifdef CONFIG_ROUTE_IPv4_CACHEROUTE
  114. /* They match.. Copy the entire routing table entry */
  115. memcpy(&match->entry, route, sizeof(struct net_route_ipv4_s));
  116. #else
  117. /* They match.. Copy the router address */
  118. net_ipv4addr_copy(match->router, route->router);
  119. #endif
  120. return 1;
  121. }
  122. return 0;
  123. }
  124. #endif /* CONFIG_NET_IPv4 */
  125. /****************************************************************************
  126. * Name: net_ipv6_match
  127. *
  128. * Description:
  129. * Return 1 if the IPv6 route is available
  130. *
  131. * Input Parameters:
  132. * route - The next route to examine
  133. * arg - The match values (cast to void*)
  134. *
  135. * Returned Value:
  136. * 0 if the entry is not a match; 1 if the entry matched and was cleared.
  137. *
  138. ****************************************************************************/
  139. #ifdef CONFIG_NET_IPv6
  140. static int net_ipv6_match(FAR struct net_route_ipv6_s *route, FAR void *arg)
  141. {
  142. FAR struct route_ipv6_match_s *match = (FAR struct route_ipv6_match_s *)arg;
  143. /* To match, the masked target addresses must be the same. In the event
  144. * of multiple matches, only the first is returned. There is not (yet) any
  145. * concept for the precedence of networks.
  146. */
  147. if (net_ipv6addr_maskcmp(route->target, match->target, route->netmask))
  148. {
  149. #ifdef CONFIG_ROUTE_IPv6_CACHEROUTE
  150. /* They match.. Copy the entire routing table entry */
  151. memcpy(&match->entry, route, sizeof(struct net_route_ipv6_s));
  152. #else
  153. /* They match.. Copy the router address */
  154. net_ipv6addr_copy(match->router, route->router);
  155. #endif
  156. return 1;
  157. }
  158. return 0;
  159. }
  160. #endif /* CONFIG_NET_IPv6 */
  161. /****************************************************************************
  162. * Public Functions
  163. ****************************************************************************/
  164. /****************************************************************************
  165. * Name: net_ipv4_router
  166. *
  167. * Description:
  168. * Given an IPv4 address on a external network, return the address of the
  169. * router on a local network that can forward to the external network.
  170. *
  171. * Input Parameters:
  172. * target - An IPv4 address on a remote network to use in the lookup.
  173. * router - The address of router on a local network that can forward our
  174. * packets to the target.
  175. *
  176. * Returned Value:
  177. * OK on success; Negated errno on failure.
  178. *
  179. ****************************************************************************/
  180. #ifdef CONFIG_NET_IPv4
  181. int net_ipv4_router(in_addr_t target, FAR in_addr_t *router)
  182. {
  183. struct route_ipv4_match_s match;
  184. int ret;
  185. /* Do not route the special broadcast IP address */
  186. if (net_ipv4addr_cmp(target, INADDR_BROADCAST))
  187. {
  188. return -ENOENT;
  189. }
  190. /* Set up the comparison structure */
  191. memset(&match, 0, sizeof(struct route_ipv4_match_s));
  192. net_ipv4addr_copy(match.target, target);
  193. #ifdef CONFIG_ROUTE_IPv4_CACHEROUTE
  194. /* First see if we can find a router entry in the cache */
  195. ret = net_foreachcache_ipv4(net_ipv4_match, &match);
  196. if (ret <= 0)
  197. #endif
  198. {
  199. /* Not found in the cache. Try to find a router entry with the
  200. * routing table that can forward to this address
  201. */
  202. ret = net_foreachroute_ipv4(net_ipv4_match, &match);
  203. }
  204. /* Did we find a route? */
  205. if (ret <= 0)
  206. {
  207. /* No.. there is no route for this address */
  208. return -ENOENT;
  209. }
  210. /* We found a route. */
  211. #ifdef CONFIG_ROUTE_IPv4_CACHEROUTE
  212. /* Add the route to the cache. If the route is already in the cache, this
  213. * will update it to the most recently accessed.
  214. */
  215. (void)net_addcache_ipv4(&match.entry);
  216. #endif
  217. /* Return the router address. */
  218. net_ipv4addr_copy(*router, match.IPv4_ROUTER);
  219. return OK;
  220. }
  221. #endif /* CONFIG_NET_IPv4 */
  222. /****************************************************************************
  223. * Name: net_ipv6_router
  224. *
  225. * Description:
  226. * Given an IPv6 address on a external network, return the address of the
  227. * router on a local network that can forward to the external network.
  228. *
  229. * Input Parameters:
  230. * target - An IPv6 address on a remote network to use in the lookup.
  231. * router - The address of router on a local network that can forward our
  232. * packets to the target.
  233. *
  234. * Returned Value:
  235. * OK on success; Negated errno on failure.
  236. *
  237. ****************************************************************************/
  238. #ifdef CONFIG_NET_IPv6
  239. int net_ipv6_router(const net_ipv6addr_t target, net_ipv6addr_t router)
  240. {
  241. struct route_ipv6_match_s match;
  242. int ret;
  243. /* Do not route to any the special IPv6 multicast addresses */
  244. if (target[0] == HTONS(0xff02))
  245. {
  246. return -ENOENT;
  247. }
  248. /* Set up the comparison structure */
  249. memset(&match, 0, sizeof(struct route_ipv6_match_s));
  250. net_ipv6addr_copy(match.target, target);
  251. #ifdef CONFIG_ROUTE_IPv6_CACHEROUTE
  252. /* First see if we can find a router entry in the cache */
  253. ret = net_foreachcache_ipv6(net_ipv6_match, &match);
  254. if (ret <= 0)
  255. #endif
  256. {
  257. /* Not found in the cache. Try to find a router entry with the
  258. * routing table that can forward to this address
  259. */
  260. ret = net_foreachroute_ipv6(net_ipv6_match, &match);
  261. }
  262. /* Did we find a route? */
  263. if (ret <= 0)
  264. {
  265. /* No.. there is no route for this address */
  266. return -ENOENT;
  267. }
  268. /* We found a route. */
  269. #ifdef CONFIG_ROUTE_IPv6_CACHEROUTE
  270. /* Add the route to the cache. If the route is already in the cache, this
  271. * will update it to the most recently accessed.
  272. */
  273. (void)net_addcache_ipv6(&match.entry);
  274. #endif
  275. /* Return the router address. */
  276. net_ipv6addr_copy(router, match.IPv6_ROUTER);
  277. return OK;
  278. }
  279. #endif /* CONFIG_NET_IPv6 */
  280. #endif /* CONFIG_NET && CONFIG_NET_ROUTE */