net_ipv4_route.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /**
  2. * @file net_ipv4_route.c
  3. * @author Ambroz Bizjak <[email protected]>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * IPv4 route module.
  25. *
  26. * Synopsis:
  27. * net.ipv4.route(string dest, string dest_prefix, string gateway, string metric, string ifname)
  28. * Description:
  29. * Adds an IPv4 route to the system's routing table on initiailzation, and removes it on
  30. * deinitialization.
  31. * If 'gateway' is "none", the route will only be associated with an interface.
  32. * If 'gateway' is "blackhole", the route will be a blackhole route (and 'ifname' is unused).
  33. */
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <ncd/NCDModule.h>
  37. #include <ncd/NCDIfConfig.h>
  38. #include <generated/blog_channel_ncd_net_ipv4_route.h>
  39. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  40. #define TYPE_NORMAL 1
  41. #define TYPE_IFONLY 2
  42. #define TYPE_BLACKHOLE 3
  43. struct instance {
  44. NCDModuleInst *i;
  45. struct ipv4_ifaddr dest;
  46. int type;
  47. uint32_t gateway;
  48. int metric;
  49. const char *ifname;
  50. };
  51. static void func_new (NCDModuleInst *i)
  52. {
  53. // allocate instance
  54. struct instance *o = malloc(sizeof(*o));
  55. if (!o) {
  56. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  57. goto fail0;
  58. }
  59. NCDModuleInst_Backend_SetUser(i, o);
  60. // init arguments
  61. o->i = i;
  62. // read arguments
  63. NCDValue *dest_arg;
  64. NCDValue *dest_prefix_arg;
  65. NCDValue *gateway_arg;
  66. NCDValue *metric_arg;
  67. NCDValue *ifname_arg;
  68. if (!NCDValue_ListRead(o->i->args, 5, &dest_arg, &dest_prefix_arg, &gateway_arg, &metric_arg, &ifname_arg)) {
  69. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  70. goto fail1;
  71. }
  72. if (NCDValue_Type(dest_arg) != NCDVALUE_STRING || NCDValue_Type(dest_prefix_arg) != NCDVALUE_STRING || NCDValue_Type(gateway_arg) != NCDVALUE_STRING ||
  73. NCDValue_Type(metric_arg) != NCDVALUE_STRING || NCDValue_Type(ifname_arg) != NCDVALUE_STRING) {
  74. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  75. goto fail1;
  76. }
  77. // read dest
  78. if (!ipaddr_parse_ipv4_addr(NCDValue_StringValue(dest_arg), &o->dest.addr)) {
  79. ModuleLog(o->i, BLOG_ERROR, "wrong dest addr");
  80. goto fail1;
  81. }
  82. if (!ipaddr_parse_ipv4_prefix(NCDValue_StringValue(dest_prefix_arg), &o->dest.prefix)) {
  83. ModuleLog(o->i, BLOG_ERROR, "wrong dest prefix");
  84. goto fail1;
  85. }
  86. // read gateway and choose type
  87. char *gateway_str = NCDValue_StringValue(gateway_arg);
  88. if (!strcmp(gateway_str, "none")) {
  89. o->type = TYPE_IFONLY;
  90. }
  91. else if (!strcmp(gateway_str, "blackhole")) {
  92. o->type = TYPE_BLACKHOLE;
  93. } else {
  94. if (!ipaddr_parse_ipv4_addr(gateway_str, &o->gateway)) {
  95. ModuleLog(o->i, BLOG_ERROR, "wrong gateway");
  96. goto fail1;
  97. }
  98. o->type = TYPE_NORMAL;
  99. }
  100. // read metric
  101. o->metric = atoi(NCDValue_StringValue(metric_arg));
  102. // read ifname
  103. o->ifname = NCDValue_StringValue(ifname_arg);
  104. // add route
  105. int res;
  106. switch (o->type) {
  107. case TYPE_NORMAL:
  108. res = NCDIfConfig_add_ipv4_route(o->dest, &o->gateway, o->metric, o->ifname);
  109. break;
  110. case TYPE_IFONLY:
  111. res = NCDIfConfig_add_ipv4_route(o->dest, NULL, o->metric, o->ifname);
  112. break;
  113. case TYPE_BLACKHOLE:
  114. res = NCDIfConfig_add_ipv4_blackhole_route(o->dest, o->metric);
  115. break;
  116. default: ASSERT(0);
  117. }
  118. if (!res) {
  119. ModuleLog(o->i, BLOG_ERROR, "failed to add route");
  120. goto fail1;
  121. }
  122. // signal up
  123. NCDModuleInst_Backend_Up(o->i);
  124. return;
  125. fail1:
  126. free(o);
  127. fail0:
  128. NCDModuleInst_Backend_SetError(i);
  129. NCDModuleInst_Backend_Dead(i);
  130. }
  131. static void func_die (void *vo)
  132. {
  133. struct instance *o = vo;
  134. NCDModuleInst *i = o->i;
  135. // remove route
  136. int res;
  137. switch (o->type) {
  138. case TYPE_NORMAL:
  139. res = NCDIfConfig_remove_ipv4_route(o->dest, &o->gateway, o->metric, o->ifname);
  140. break;
  141. case TYPE_IFONLY:
  142. res = NCDIfConfig_remove_ipv4_route(o->dest, NULL, o->metric, o->ifname);
  143. break;
  144. case TYPE_BLACKHOLE:
  145. res = NCDIfConfig_remove_ipv4_blackhole_route(o->dest, o->metric);
  146. break;
  147. default: ASSERT(0);
  148. }
  149. if (!res) {
  150. ModuleLog(o->i, BLOG_ERROR, "failed to remove route");
  151. }
  152. // free instance
  153. free(o);
  154. NCDModuleInst_Backend_Dead(i);
  155. }
  156. static const struct NCDModule modules[] = {
  157. {
  158. .type = "net.ipv4.route",
  159. .func_new = func_new,
  160. .func_die = func_die
  161. }, {
  162. .type = NULL
  163. }
  164. };
  165. const struct NCDModuleGroup ncdmodule_net_ipv4_route = {
  166. .modules = modules
  167. };