net_add_ramroute.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /****************************************************************************
  2. * net/route/net_add_ramroute.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 <debug.h>
  43. #include <nuttx/net/net.h>
  44. #include <nuttx/net/ip.h>
  45. #include <arch/irq.h>
  46. #include "route/ramroute.h"
  47. #include "route/route.h"
  48. #if defined(CONFIG_ROUTE_IPv4_RAMROUTE) || defined(CONFIG_ROUTE_IPv6_RAMROUTE)
  49. /****************************************************************************
  50. * Public Functions
  51. ****************************************************************************/
  52. /****************************************************************************
  53. * Name: net_addroute_ipv4 and net_addroute_ipv6
  54. *
  55. * Description:
  56. * Add a new route to the routing table
  57. *
  58. * Input Parameters:
  59. *
  60. * Returned Value:
  61. * OK on success; Negated errno on failure.
  62. *
  63. ****************************************************************************/
  64. #ifdef CONFIG_ROUTE_IPv4_RAMROUTE
  65. int net_addroute_ipv4(in_addr_t target, in_addr_t netmask, in_addr_t router)
  66. {
  67. FAR struct net_route_ipv4_s *route;
  68. /* Allocate a route entry */
  69. route = net_allocroute_ipv4();
  70. if (!route)
  71. {
  72. nerr("ERROR: Failed to allocate a route\n");
  73. return -ENOMEM;
  74. }
  75. /* Format the new routing table entry */
  76. net_ipv4addr_copy(route->target, target);
  77. net_ipv4addr_copy(route->netmask, netmask);
  78. net_ipv4addr_copy(route->router, router);
  79. net_ipv4_dumproute("New route", route);
  80. /* Get exclusive address to the networking data structures */
  81. net_lock();
  82. /* Then add the new entry to the table */
  83. ramroute_ipv4_addlast((FAR struct net_route_ipv4_entry_s *)route,
  84. &g_ipv4_routes);
  85. net_unlock();
  86. return OK;
  87. }
  88. #endif
  89. #ifdef CONFIG_ROUTE_IPv6_RAMROUTE
  90. int net_addroute_ipv6(net_ipv6addr_t target, net_ipv6addr_t netmask,
  91. net_ipv6addr_t router)
  92. {
  93. FAR struct net_route_ipv6_s *route;
  94. /* Allocate a route entry */
  95. route = net_allocroute_ipv6();
  96. if (!route)
  97. {
  98. nerr("ERROR: Failed to allocate a route\n");
  99. return -ENOMEM;
  100. }
  101. /* Format the new routing table entry */
  102. net_ipv6addr_copy(route->target, target);
  103. net_ipv6addr_copy(route->netmask, netmask);
  104. net_ipv6addr_copy(route->router, router);
  105. net_ipv6_dumproute("New route", route);
  106. /* Get exclusive address to the networking data structures */
  107. net_lock();
  108. /* Then add the new entry to the table */
  109. ramroute_ipv6_addlast((FAR struct net_route_ipv6_entry_s *)route,
  110. &g_ipv6_routes);
  111. net_unlock();
  112. return OK;
  113. }
  114. #endif
  115. #endif /* CONFIG_ROUTE_IPv4_RAMROUTE || CONFIG_ROUTE_IPv6_RAMROUTE */