net_iptables.c 6.0 KB

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