net_iptables.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**
  2. * @file net_iptables.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. * iptables module.
  25. *
  26. * Synopsis: net.iptables.append(string table, string chain, string arg1, ...)
  27. * Synopsis: net.iptables.policy(string table, string chain, string target, string revert_target)
  28. */
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <system/BEventLock.h>
  32. #include <ncd/modules/command_template.h>
  33. #include <generated/blog_channel_ncd_net_iptables.h>
  34. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  35. #define IPTABLES_PATH "/sbin/iptables"
  36. BEventLock iptables_lock;
  37. struct instance {
  38. command_template_instance cti;
  39. };
  40. static int build_append_cmdline (NCDModuleInst *i, int remove, char **exec, CmdLine *cl)
  41. {
  42. // read arguments
  43. NCDValue *table_arg;
  44. NCDValue *chain_arg;
  45. if (!NCDValue_ListReadHead(i->args, 2, &table_arg, &chain_arg)) {
  46. ModuleLog(i, BLOG_ERROR, "wrong arity");
  47. goto fail0;
  48. }
  49. if (NCDValue_Type(table_arg) != NCDVALUE_STRING || NCDValue_Type(chain_arg) != NCDVALUE_STRING) {
  50. ModuleLog(i, BLOG_ERROR, "wrong type");
  51. goto fail0;
  52. }
  53. char *table = NCDValue_StringValue(table_arg);
  54. char *chain = NCDValue_StringValue(chain_arg);
  55. // alloc exec
  56. if (!(*exec = strdup(IPTABLES_PATH))) {
  57. ModuleLog(i, BLOG_ERROR, "strdup failed");
  58. goto fail0;
  59. }
  60. // start cmdline
  61. if (!CmdLine_Init(cl)) {
  62. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  63. goto fail1;
  64. }
  65. // add header
  66. if (!CmdLine_Append(cl, IPTABLES_PATH) || !CmdLine_Append(cl, "-t") || !CmdLine_Append(cl, table) || !CmdLine_Append(cl, (remove ? "-D" : "-A")) || !CmdLine_Append(cl, chain)) {
  67. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  68. goto fail2;
  69. }
  70. // add additional arguments
  71. NCDValue *arg = NCDValue_ListNext(i->args, chain_arg);
  72. while (arg) {
  73. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  74. ModuleLog(i, BLOG_ERROR, "wrong type");
  75. goto fail2;
  76. }
  77. if (!CmdLine_Append(cl, NCDValue_StringValue(arg))) {
  78. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  79. goto fail2;
  80. }
  81. arg = NCDValue_ListNext(i->args, arg);
  82. }
  83. // finish
  84. if (!CmdLine_Finish(cl)) {
  85. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  86. goto fail2;
  87. }
  88. return 1;
  89. fail2:
  90. CmdLine_Free(cl);
  91. fail1:
  92. free(*exec);
  93. fail0:
  94. return 0;
  95. }
  96. static int build_policy_cmdline (NCDModuleInst *i, int remove, char **exec, CmdLine *cl)
  97. {
  98. // read arguments
  99. NCDValue *table_arg;
  100. NCDValue *chain_arg;
  101. NCDValue *target_arg;
  102. NCDValue *revert_target_arg;
  103. if (!NCDValue_ListRead(i->args, 4, &table_arg, &chain_arg, &target_arg, &revert_target_arg)) {
  104. ModuleLog(i, BLOG_ERROR, "wrong arity");
  105. goto fail0;
  106. }
  107. if (NCDValue_Type(table_arg) != NCDVALUE_STRING || NCDValue_Type(chain_arg) != NCDVALUE_STRING ||
  108. NCDValue_Type(target_arg) != NCDVALUE_STRING || NCDValue_Type(revert_target_arg) != NCDVALUE_STRING
  109. ) {
  110. ModuleLog(i, BLOG_ERROR, "wrong type");
  111. goto fail0;
  112. }
  113. char *table = NCDValue_StringValue(table_arg);
  114. char *chain = NCDValue_StringValue(chain_arg);
  115. char *target = NCDValue_StringValue(target_arg);
  116. char *revert_target = NCDValue_StringValue(revert_target_arg);
  117. // alloc exec
  118. if (!(*exec = strdup(IPTABLES_PATH))) {
  119. ModuleLog(i, BLOG_ERROR, "strdup failed");
  120. goto fail0;
  121. }
  122. // start cmdline
  123. if (!CmdLine_Init(cl)) {
  124. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  125. goto fail1;
  126. }
  127. // add arguments
  128. if (!CmdLine_Append(cl, IPTABLES_PATH) || !CmdLine_Append(cl, "-t") || !CmdLine_Append(cl, table) ||
  129. !CmdLine_Append(cl, "-P") || !CmdLine_Append(cl, chain) || !CmdLine_Append(cl, (remove ? revert_target : target))) {
  130. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  131. goto fail2;
  132. }
  133. // finish
  134. if (!CmdLine_Finish(cl)) {
  135. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  136. goto fail2;
  137. }
  138. return 1;
  139. fail2:
  140. CmdLine_Free(cl);
  141. fail1:
  142. free(*exec);
  143. fail0:
  144. return 0;
  145. }
  146. static int func_globalinit (struct NCDModuleInitParams params)
  147. {
  148. // init iptables lock
  149. BEventLock_Init(&iptables_lock, BReactor_PendingGroup(params.reactor));
  150. return 1;
  151. }
  152. static void func_globalfree (void)
  153. {
  154. // free iptables lock
  155. BEventLock_Free(&iptables_lock);
  156. }
  157. static void * func_new (NCDModuleInst *i, command_template_build_cmdline build_cmdline)
  158. {
  159. struct instance *o = malloc(sizeof(*o));
  160. if (!o) {
  161. BLog(BLOG_ERROR, "malloc failed");
  162. goto fail0;
  163. }
  164. if (!command_template_new(&o->cti, i, build_cmdline, BLOG_CURRENT_CHANNEL, &iptables_lock)) {
  165. goto fail1;
  166. }
  167. return o;
  168. fail1:
  169. free(o);
  170. fail0:
  171. return NULL;
  172. }
  173. static void * append_func_new (NCDModuleInst *i)
  174. {
  175. return func_new(i, build_append_cmdline);
  176. }
  177. static void * policy_func_new (NCDModuleInst *i)
  178. {
  179. return func_new(i, build_policy_cmdline);
  180. }
  181. static void func_free (void *vo)
  182. {
  183. struct instance *o = vo;
  184. command_template_free(&o->cti);
  185. free(o);
  186. }
  187. static void func_die (void *vo)
  188. {
  189. struct instance *o = vo;
  190. command_template_die(&o->cti);
  191. }
  192. static const struct NCDModule modules[] = {
  193. {
  194. .type = "net.iptables.append",
  195. .func_new = append_func_new,
  196. .func_free = func_free,
  197. .func_die = func_die
  198. }, {
  199. .type = "net.iptables.policy",
  200. .func_new = policy_func_new,
  201. .func_free = func_free,
  202. .func_die = func_die
  203. }, {
  204. .type = NULL
  205. }
  206. };
  207. const struct NCDModuleGroup ncdmodule_net_iptables = {
  208. .modules = modules,
  209. .func_globalinit = func_globalinit,
  210. .func_globalfree = func_globalfree
  211. };