route.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /****************************************************************************
  2. * net/route/route.h
  3. *
  4. * Copyright (C) 2013-2014, 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. #ifndef __NET_ROUTE_ROUTE_H
  36. #define __NET_ROUTE_ROUTE_H
  37. /****************************************************************************
  38. * Included Files
  39. ****************************************************************************/
  40. #include <nuttx/config.h>
  41. #include <net/if.h>
  42. #include <nuttx/net/ip.h>
  43. #ifdef CONFIG_NET_ROUTE
  44. /****************************************************************************
  45. * Public Types
  46. ****************************************************************************/
  47. /* This structure describes one entry in the routing table */
  48. #ifdef CONFIG_NET_IPv4
  49. struct net_route_ipv4_s
  50. {
  51. in_addr_t target; /* The destination network */
  52. in_addr_t netmask; /* The network address mask */
  53. in_addr_t router; /* Route packets via this router */
  54. };
  55. /* Type of the call out function pointer provided to net_foreachroute_ipv4() */
  56. typedef int (*route_handler_ipv4_t)(FAR struct net_route_ipv4_s *route,
  57. FAR void *arg);
  58. #endif /* CONFIG_NET_IPv4 */
  59. #ifdef CONFIG_NET_IPv6
  60. struct net_route_ipv6_s
  61. {
  62. net_ipv6addr_t target; /* The destination network */
  63. net_ipv6addr_t netmask; /* The network address mask */
  64. net_ipv6addr_t router; /* Route packets via this router */
  65. };
  66. /* Type of the call out function pointer provided to net_foreachroute_ipv6() */
  67. typedef int (*route_handler_ipv6_t)(FAR struct net_route_ipv6_s *route,
  68. FAR void *arg);
  69. #endif /* CONFIG_NET_IPv6 */
  70. /****************************************************************************
  71. * Public Data
  72. ****************************************************************************/
  73. #ifdef __cplusplus
  74. #define EXTERN extern "C"
  75. extern "C"
  76. {
  77. #else
  78. #define EXTERN extern
  79. #endif
  80. /****************************************************************************
  81. * Public Function Prototypes
  82. ****************************************************************************/
  83. /****************************************************************************
  84. * Name: net_init_route
  85. *
  86. * Description:
  87. * Initialize to the routing table
  88. *
  89. * Input Parameters:
  90. * None
  91. *
  92. * Returned Value:
  93. * None
  94. *
  95. ****************************************************************************/
  96. void net_init_route(void);
  97. /****************************************************************************
  98. * Name: net_addroute_ipv4 and net_addroute_ipv6
  99. *
  100. * Description:
  101. * Add a new route to the routing table
  102. *
  103. * Input Parameters:
  104. * target - The destination IP address on the destination network
  105. * netmask - The mask defining the destination sub-net
  106. * router - The IP address on one of our networks that provides the
  107. * router to the external network
  108. *
  109. * Returned Value:
  110. * OK on success; Negated errno on failure.
  111. *
  112. ****************************************************************************/
  113. #ifdef CONFIG_NET_IPv4
  114. int net_addroute_ipv4(in_addr_t target, in_addr_t netmask,
  115. in_addr_t router);
  116. #endif
  117. #ifdef CONFIG_NET_IPv6
  118. int net_addroute_ipv6(net_ipv6addr_t target, net_ipv6addr_t netmask,
  119. net_ipv6addr_t router);
  120. #endif
  121. /****************************************************************************
  122. * Name: net_delroute_ipv4 and net_delroute_ipv6
  123. *
  124. * Description:
  125. * Remove an existing route from the routing table
  126. *
  127. * Input Parameters:
  128. *
  129. * Returned Value:
  130. * OK on success; Negated errno on failure.
  131. *
  132. ****************************************************************************/
  133. #ifdef CONFIG_NET_IPv4
  134. int net_delroute_ipv4(in_addr_t target, in_addr_t netmask);
  135. #endif
  136. #ifdef CONFIG_NET_IPv6
  137. int net_delroute_ipv6(net_ipv6addr_t target, net_ipv6addr_t netmask);
  138. #endif
  139. /****************************************************************************
  140. * Name: net_ipv4_router
  141. *
  142. * Description:
  143. * Given an IPv4 address on a external network, return the address of the
  144. * router on a local network that can forward to the external network.
  145. *
  146. * Input Parameters:
  147. * target - An IPv4 address on a remote network to use in the lookup.
  148. * router - The address of router on a local network that can forward our
  149. * packets to the target.
  150. *
  151. * Returned Value:
  152. * OK on success; Negated errno on failure.
  153. *
  154. ****************************************************************************/
  155. #ifdef CONFIG_NET_IPv4
  156. int net_ipv4_router(in_addr_t target, FAR in_addr_t *router);
  157. #endif
  158. /****************************************************************************
  159. * Name: net_ipv6_router
  160. *
  161. * Description:
  162. * Given an IPv6 address on a external network, return the address of the
  163. * router on a local network that can forward to the external network.
  164. *
  165. * Input Parameters:
  166. * target - An IPv6 address on a remote network to use in the lookup.
  167. * router - The address of router on a local network that can forward our
  168. * packets to the target.
  169. *
  170. * Returned Value:
  171. * OK on success; Negated errno on failure.
  172. *
  173. ****************************************************************************/
  174. #ifdef CONFIG_NET_IPv6
  175. int net_ipv6_router(const net_ipv6addr_t target, net_ipv6addr_t router);
  176. #endif
  177. /****************************************************************************
  178. * Name: netdev_ipv4_router
  179. *
  180. * Description:
  181. * Given an IPv4 address on a external network, return the address of the
  182. * router on a local network that can forward to the external network.
  183. * This is similar to net_ipv4_router(). However, the set of routers is
  184. * constrained to those accessible by the specific device
  185. *
  186. * Input Parameters:
  187. * dev - We are committed to using this device.
  188. * target - An IPv4 address on a remote network to use in the lookup.
  189. * router - The address of router on a local network that can forward our
  190. * packets to the target.
  191. *
  192. * Returned Value:
  193. * A router address is always returned (which may just be, perhaps,
  194. * device's default router address)
  195. *
  196. ****************************************************************************/
  197. #ifdef CONFIG_NET_IPv4
  198. struct net_driver_s;
  199. void netdev_ipv4_router(FAR struct net_driver_s *dev, in_addr_t target,
  200. FAR in_addr_t *router);
  201. #endif
  202. /****************************************************************************
  203. * Name: netdev_ipv6_router
  204. *
  205. * Description:
  206. * Given an IPv6 address on a external network, return the address of the
  207. * router on a local network that can forward to the external network.
  208. * This is similar to net_ipv6_router(). However, the set of routers is
  209. * constrained to those accessible by the specific device
  210. *
  211. * Input Parameters:
  212. * dev - We are committed to using this device.
  213. * target - An IPv6 address on a remote network to use in the lookup.
  214. * router - The address of router on a local network that can forward our
  215. * packets to the target.
  216. *
  217. * Returned Value:
  218. * A router address is always returned (which may just be, perhaps,
  219. * device's default router address)
  220. *
  221. ****************************************************************************/
  222. #ifdef CONFIG_NET_IPv6
  223. struct net_driver_s;
  224. void netdev_ipv6_router(FAR struct net_driver_s *dev,
  225. FAR const net_ipv6addr_t target,
  226. FAR net_ipv6addr_t router);
  227. #endif
  228. /****************************************************************************
  229. * Name: net_foreachroute_ipv4/net_foreachroute_ipv6
  230. *
  231. * Description:
  232. * Traverse the routing table
  233. *
  234. * Input Parameters:
  235. * handler - Will be called for each route in the routing table.
  236. * arg - An arbitrary value that will be passed tot he handler.
  237. *
  238. * Returned Value:
  239. * Zero (OK) returned if the entire table was searched. A negated errno
  240. * value will be returned in the event of a failure. Handlers may also
  241. * terminate the search early with any non-zero value.
  242. *
  243. ****************************************************************************/
  244. #ifdef CONFIG_NET_IPv4
  245. int net_foreachroute_ipv4(route_handler_ipv4_t handler, FAR void *arg);
  246. #endif
  247. #ifdef CONFIG_NET_IPv6
  248. int net_foreachroute_ipv6(route_handler_ipv6_t handler, FAR void *arg);
  249. #endif
  250. /****************************************************************************
  251. * Name: net_ipv4_dumproute and net_ipv6_dumproute
  252. *
  253. * Description:
  254. * Dump a routing table entry
  255. *
  256. * Input Parameters:
  257. * route - The entry to be dumped
  258. *
  259. * Returned Value:
  260. * None
  261. *
  262. ****************************************************************************/
  263. #ifdef CONFIG_DEBUG_NET_INFO
  264. #ifdef CONFIG_NET_IPv4
  265. void net_ipv4_dumproute(FAR const char *msg,
  266. FAR struct net_route_ipv4_s *route);
  267. #else
  268. # define net_ipv4_dumproute(m,r)
  269. #endif
  270. #ifdef CONFIG_NET_IPv6
  271. void net_ipv6_dumproute(FAR const char *msg,
  272. FAR struct net_route_ipv6_s *route);
  273. #else
  274. # define net_ipv6_dumproute(m,r)
  275. #endif
  276. #else
  277. # define net_ipv4_dumproute(m,r)
  278. # define net_ipv6_dumproute(m,r)
  279. #endif
  280. #undef EXTERN
  281. #ifdef __cplusplus
  282. }
  283. #endif
  284. #endif /* CONFIG_NET_ROUTE */
  285. #endif /* __NET_ROUTE_ROUTE_H */