net_ipv4_route.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * @file net_ipv4_route.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * IPv4 route module.
  32. *
  33. * Synopsis:
  34. * net.ipv4.route(string dest, string dest_prefix, string gateway, string metric, string ifname)
  35. * Description:
  36. * Adds an IPv4 route to the system's routing table on initiailzation, and removes it on
  37. * deinitialization.
  38. * If 'gateway' is "none", the route will only be associated with an interface.
  39. * If 'gateway' is "blackhole", the route will be a blackhole route (and 'ifname' is unused).
  40. */
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <ncd/NCDModule.h>
  44. #include <ncd/NCDIfConfig.h>
  45. #include <generated/blog_channel_ncd_net_ipv4_route.h>
  46. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  47. #define TYPE_NORMAL 1
  48. #define TYPE_IFONLY 2
  49. #define TYPE_BLACKHOLE 3
  50. struct instance {
  51. NCDModuleInst *i;
  52. struct ipv4_ifaddr dest;
  53. int type;
  54. uint32_t gateway;
  55. int metric;
  56. const char *ifname;
  57. };
  58. static void func_new (NCDModuleInst *i)
  59. {
  60. // allocate instance
  61. struct instance *o = malloc(sizeof(*o));
  62. if (!o) {
  63. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  64. goto fail0;
  65. }
  66. NCDModuleInst_Backend_SetUser(i, o);
  67. // init arguments
  68. o->i = i;
  69. // read arguments
  70. NCDValue *dest_arg;
  71. NCDValue *dest_prefix_arg;
  72. NCDValue *gateway_arg;
  73. NCDValue *metric_arg;
  74. NCDValue *ifname_arg;
  75. if (!NCDValue_ListRead(o->i->args, 5, &dest_arg, &dest_prefix_arg, &gateway_arg, &metric_arg, &ifname_arg)) {
  76. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  77. goto fail1;
  78. }
  79. if (NCDValue_Type(dest_arg) != NCDVALUE_STRING || NCDValue_Type(dest_prefix_arg) != NCDVALUE_STRING || NCDValue_Type(gateway_arg) != NCDVALUE_STRING ||
  80. NCDValue_Type(metric_arg) != NCDVALUE_STRING || NCDValue_Type(ifname_arg) != NCDVALUE_STRING) {
  81. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  82. goto fail1;
  83. }
  84. // read dest
  85. if (!ipaddr_parse_ipv4_addr(NCDValue_StringValue(dest_arg), &o->dest.addr)) {
  86. ModuleLog(o->i, BLOG_ERROR, "wrong dest addr");
  87. goto fail1;
  88. }
  89. if (!ipaddr_parse_ipv4_prefix(NCDValue_StringValue(dest_prefix_arg), &o->dest.prefix)) {
  90. ModuleLog(o->i, BLOG_ERROR, "wrong dest prefix");
  91. goto fail1;
  92. }
  93. // read gateway and choose type
  94. char *gateway_str = NCDValue_StringValue(gateway_arg);
  95. if (!strcmp(gateway_str, "none")) {
  96. o->type = TYPE_IFONLY;
  97. }
  98. else if (!strcmp(gateway_str, "blackhole")) {
  99. o->type = TYPE_BLACKHOLE;
  100. } else {
  101. if (!ipaddr_parse_ipv4_addr(gateway_str, &o->gateway)) {
  102. ModuleLog(o->i, BLOG_ERROR, "wrong gateway");
  103. goto fail1;
  104. }
  105. o->type = TYPE_NORMAL;
  106. }
  107. // read metric
  108. o->metric = atoi(NCDValue_StringValue(metric_arg));
  109. // read ifname
  110. o->ifname = NCDValue_StringValue(ifname_arg);
  111. // add route
  112. int res;
  113. switch (o->type) {
  114. case TYPE_NORMAL:
  115. res = NCDIfConfig_add_ipv4_route(o->dest, &o->gateway, o->metric, o->ifname);
  116. break;
  117. case TYPE_IFONLY:
  118. res = NCDIfConfig_add_ipv4_route(o->dest, NULL, o->metric, o->ifname);
  119. break;
  120. case TYPE_BLACKHOLE:
  121. res = NCDIfConfig_add_ipv4_blackhole_route(o->dest, o->metric);
  122. break;
  123. default: ASSERT(0);
  124. }
  125. if (!res) {
  126. ModuleLog(o->i, BLOG_ERROR, "failed to add route");
  127. goto fail1;
  128. }
  129. // signal up
  130. NCDModuleInst_Backend_Up(o->i);
  131. return;
  132. fail1:
  133. free(o);
  134. fail0:
  135. NCDModuleInst_Backend_SetError(i);
  136. NCDModuleInst_Backend_Dead(i);
  137. }
  138. static void func_die (void *vo)
  139. {
  140. struct instance *o = vo;
  141. NCDModuleInst *i = o->i;
  142. // remove route
  143. int res;
  144. switch (o->type) {
  145. case TYPE_NORMAL:
  146. res = NCDIfConfig_remove_ipv4_route(o->dest, &o->gateway, o->metric, o->ifname);
  147. break;
  148. case TYPE_IFONLY:
  149. res = NCDIfConfig_remove_ipv4_route(o->dest, NULL, o->metric, o->ifname);
  150. break;
  151. case TYPE_BLACKHOLE:
  152. res = NCDIfConfig_remove_ipv4_blackhole_route(o->dest, o->metric);
  153. break;
  154. default: ASSERT(0);
  155. }
  156. if (!res) {
  157. ModuleLog(o->i, BLOG_ERROR, "failed to remove route");
  158. }
  159. // free instance
  160. free(o);
  161. NCDModuleInst_Backend_Dead(i);
  162. }
  163. static const struct NCDModule modules[] = {
  164. {
  165. .type = "net.ipv4.route",
  166. .func_new = func_new,
  167. .func_die = func_die
  168. }, {
  169. .type = NULL
  170. }
  171. };
  172. const struct NCDModuleGroup ncdmodule_net_ipv4_route = {
  173. .modules = modules
  174. };