net_ipv4_route.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * @file net_ipv4_route.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  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: net.ipv4.route(string dest, string dest_prefix, string gateway, string metric, string ifname)
  27. */
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <ncd/NCDModule.h>
  31. #include <ncd/NCDIfConfig.h>
  32. #include <generated/blog_channel_ncd_net_ipv4_route.h>
  33. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  34. struct instance {
  35. NCDModuleInst *i;
  36. struct ipv4_ifaddr dest;
  37. int have_gateway;
  38. uint32_t gateway;
  39. int metric;
  40. const char *ifname;
  41. };
  42. static void func_new (NCDModuleInst *i)
  43. {
  44. // allocate instance
  45. struct instance *o = malloc(sizeof(*o));
  46. if (!o) {
  47. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  48. goto fail0;
  49. }
  50. NCDModuleInst_Backend_SetUser(i, o);
  51. // init arguments
  52. o->i = i;
  53. // read arguments
  54. NCDValue *dest_arg;
  55. NCDValue *dest_prefix_arg;
  56. NCDValue *gateway_arg;
  57. NCDValue *metric_arg;
  58. NCDValue *ifname_arg;
  59. if (!NCDValue_ListRead(o->i->args, 5, &dest_arg, &dest_prefix_arg, &gateway_arg, &metric_arg, &ifname_arg)) {
  60. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  61. goto fail1;
  62. }
  63. if (NCDValue_Type(dest_arg) != NCDVALUE_STRING || NCDValue_Type(dest_prefix_arg) != NCDVALUE_STRING || NCDValue_Type(gateway_arg) != NCDVALUE_STRING ||
  64. NCDValue_Type(metric_arg) != NCDVALUE_STRING || NCDValue_Type(ifname_arg) != NCDVALUE_STRING) {
  65. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  66. goto fail1;
  67. }
  68. // read dest
  69. if (!ipaddr_parse_ipv4_addr(NCDValue_StringValue(dest_arg), &o->dest.addr)) {
  70. ModuleLog(o->i, BLOG_ERROR, "wrong dest addr");
  71. goto fail1;
  72. }
  73. if (!ipaddr_parse_ipv4_prefix(NCDValue_StringValue(dest_prefix_arg), &o->dest.prefix)) {
  74. ModuleLog(o->i, BLOG_ERROR, "wrong dest prefix");
  75. goto fail1;
  76. }
  77. // read gateway
  78. char *gateway_str = NCDValue_StringValue(gateway_arg);
  79. if (!strcmp(gateway_str, "none")) {
  80. o->have_gateway = 0;
  81. } else {
  82. if (!ipaddr_parse_ipv4_addr(gateway_str, &o->gateway)) {
  83. ModuleLog(o->i, BLOG_ERROR, "wrong gateway");
  84. goto fail1;
  85. }
  86. o->have_gateway = 1;
  87. }
  88. // read metric
  89. o->metric = atoi(NCDValue_StringValue(metric_arg));
  90. // read ifname
  91. o->ifname = NCDValue_StringValue(ifname_arg);
  92. // add route
  93. if (!NCDIfConfig_add_ipv4_route(o->dest, (o->have_gateway ? &o->gateway : NULL), o->metric, o->ifname)) {
  94. ModuleLog(o->i, BLOG_ERROR, "failed to add route");
  95. goto fail1;
  96. }
  97. // signal up
  98. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  99. return;
  100. fail1:
  101. free(o);
  102. fail0:
  103. NCDModuleInst_Backend_SetError(i);
  104. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  105. }
  106. static void func_die (void *vo)
  107. {
  108. struct instance *o = vo;
  109. NCDModuleInst *i = o->i;
  110. // remove route
  111. if (!NCDIfConfig_remove_ipv4_route(o->dest, (o->have_gateway ? &o->gateway : NULL), o->metric, o->ifname)) {
  112. ModuleLog(o->i, BLOG_ERROR, "failed to remove route");
  113. }
  114. // free instance
  115. free(o);
  116. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  117. }
  118. static const struct NCDModule modules[] = {
  119. {
  120. .type = "net.ipv4.route",
  121. .func_new = func_new,
  122. .func_die = func_die
  123. }, {
  124. .type = NULL
  125. }
  126. };
  127. const struct NCDModuleGroup ncdmodule_net_ipv4_route = {
  128. .modules = modules
  129. };