net_ipv4_route.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. // init arguments
  51. o->i = i;
  52. // read arguments
  53. NCDValue *dest_arg;
  54. NCDValue *dest_prefix_arg;
  55. NCDValue *gateway_arg;
  56. NCDValue *metric_arg;
  57. NCDValue *ifname_arg;
  58. if (!NCDValue_ListRead(o->i->args, 5, &dest_arg, &dest_prefix_arg, &gateway_arg, &metric_arg, &ifname_arg)) {
  59. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  60. goto fail1;
  61. }
  62. if (NCDValue_Type(dest_arg) != NCDVALUE_STRING || NCDValue_Type(dest_prefix_arg) != NCDVALUE_STRING || NCDValue_Type(gateway_arg) != NCDVALUE_STRING ||
  63. NCDValue_Type(metric_arg) != NCDVALUE_STRING || NCDValue_Type(ifname_arg) != NCDVALUE_STRING) {
  64. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  65. goto fail1;
  66. }
  67. // read dest
  68. if (!ipaddr_parse_ipv4_addr(NCDValue_StringValue(dest_arg), &o->dest.addr)) {
  69. ModuleLog(o->i, BLOG_ERROR, "wrong dest addr");
  70. goto fail1;
  71. }
  72. if (!ipaddr_parse_ipv4_prefix(NCDValue_StringValue(dest_prefix_arg), &o->dest.prefix)) {
  73. ModuleLog(o->i, BLOG_ERROR, "wrong dest prefix");
  74. goto fail1;
  75. }
  76. // read gateway
  77. char *gateway_str = NCDValue_StringValue(gateway_arg);
  78. if (!strcmp(gateway_str, "none")) {
  79. o->have_gateway = 0;
  80. } else {
  81. if (!ipaddr_parse_ipv4_addr(gateway_str, &o->gateway)) {
  82. ModuleLog(o->i, BLOG_ERROR, "wrong gateway");
  83. goto fail1;
  84. }
  85. o->have_gateway = 1;
  86. }
  87. // read metric
  88. o->metric = atoi(NCDValue_StringValue(metric_arg));
  89. // read ifname
  90. o->ifname = NCDValue_StringValue(ifname_arg);
  91. // add route
  92. if (!NCDIfConfig_add_ipv4_route(o->dest, (o->have_gateway ? &o->gateway : NULL), o->metric, o->ifname)) {
  93. ModuleLog(o->i, BLOG_ERROR, "failed to add route");
  94. goto fail1;
  95. }
  96. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  97. return o;
  98. fail1:
  99. free(o);
  100. fail0:
  101. return NULL;
  102. }
  103. static void func_free (void *vo)
  104. {
  105. struct instance *o = vo;
  106. // remove route
  107. if (!NCDIfConfig_remove_ipv4_route(o->dest, (o->have_gateway ? &o->gateway : NULL), o->metric, o->ifname)) {
  108. ModuleLog(o->i, BLOG_ERROR, "failed to remove route");
  109. }
  110. // free instance
  111. free(o);
  112. }
  113. static const struct NCDModule modules[] = {
  114. {
  115. .type = "net.ipv4.route",
  116. .func_new = func_new,
  117. .func_free = func_free
  118. }, {
  119. .type = NULL
  120. }
  121. };
  122. const struct NCDModuleGroup ncdmodule_net_ipv4_route = {
  123. .modules = modules
  124. };