net_iptables.c 6.9 KB

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