net_iptables.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /**
  2. * @file net_iptables.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * iptables and ebtables module.
  32. *
  33. * Note that all iptables/ebtables commands (in general) must be issued synchronously, or
  34. * the kernel may randomly report errors if there is another iptables/ebtables command in
  35. * progress. To solve this, the NCD process contains a single "iptables lock". All
  36. * iptables/ebtables commands exposed here go through that lock.
  37. * In case you wish to call iptables/ebtables directly, the lock is exposed via
  38. * net.iptables.lock().
  39. *
  40. * The append and insert commands, instead of using the variable-argument form below
  41. * as documented below, may alternatively be called with a single list argument.
  42. *
  43. * Synopsis:
  44. * net.iptables.append(string table, string chain, string arg1 ...)
  45. * Description:
  46. * init: iptables -t table -A chain arg1 ...
  47. * deinit: iptables -t table -D chain arg1 ...
  48. *
  49. * Synopsis:
  50. * net.iptables.insert(string table, string chain, string arg1 ...)
  51. * Description:
  52. * init: iptables -t table -I chain arg1 ...
  53. * deinit: iptables -t table -D chain arg1 ...
  54. *
  55. * Synopsis:
  56. * net.iptables.policy(string table, string chain, string target, string revert_target)
  57. * Description:
  58. * init: iptables -t table -P chain target
  59. * deinit: iptables -t table -P chain revert_target
  60. *
  61. * Synopsis:
  62. * net.iptables.newchain(string table, string chain)
  63. * net.iptables.newchain(string chain) // DEPRECATED, defaults to table="filter"
  64. * Description:
  65. * init: iptables -t table -N chain
  66. * deinit: iptables -t table -X chain
  67. *
  68. * Synopsis:
  69. * net.ebtables.append(string table, string chain, string arg1 ...)
  70. * Description:
  71. * init: ebtables -t table -A chain arg1 ...
  72. * deinit: ebtables -t table -D chain arg1 ...
  73. *
  74. * Synopsis:
  75. * net.ebtables.insert(string table, string chain, string arg1 ...)
  76. * Description:
  77. * init: ebtables -t table -I chain arg1 ...
  78. * deinit: ebtables -t table -D chain arg1 ...
  79. *
  80. * Synopsis:
  81. * net.ebtables.policy(string table, string chain, string target, string revert_target)
  82. * Description:
  83. * init: ebtables -t table -P chain target
  84. * deinit: ebtables -t table -P chain revert_target
  85. *
  86. * Synopsis:
  87. * net.ebtables.newchain(string table, string chain)
  88. * Description:
  89. * init: ebtables -t table -N chain
  90. * deinit: ebtables -t table -X chain
  91. *
  92. * Synopsis:
  93. * net.iptables.lock()
  94. * Description:
  95. * Use at the beginning of a block of custom iptables/ebtables commands to make sure
  96. * they do not interfere with other iptables/ebtables commands.
  97. * WARNING: improper usage of the lock can lead to deadlock. In particular:
  98. * - Do not call any of the iptables/ebtables wrappers above from a lock section;
  99. * those will attempt to aquire the lock themselves.
  100. * - Do not enter another lock section from a lock section.
  101. * - Do not perform any potentially long wait from a lock section.
  102. *
  103. * Synopsis:
  104. * net.iptables.lock::unlock()
  105. * Description:
  106. * Use at the end of a block of custom iptables/ebtables commands to make sure
  107. * they do not interfere with other iptables/ebtables commands.
  108. */
  109. #include <stdlib.h>
  110. #include <string.h>
  111. #include <unistd.h>
  112. #include <misc/debug.h>
  113. #include <misc/find_program.h>
  114. #include <misc/balloc.h>
  115. #include <ncd/modules/command_template.h>
  116. #include <ncd/module_common.h>
  117. #include <generated/blog_channel_ncd_net_iptables.h>
  118. static void template_free_func (void *vo, int is_error);
  119. struct global {
  120. BEventLock iptables_lock;
  121. };
  122. struct instance {
  123. NCDModuleInst *i;
  124. command_template_instance cti;
  125. };
  126. struct unlock_instance;
  127. #define LOCK_STATE_LOCKING 1
  128. #define LOCK_STATE_LOCKED 2
  129. #define LOCK_STATE_UNLOCKED 3
  130. #define LOCK_STATE_RELOCKING 4
  131. struct lock_instance {
  132. NCDModuleInst *i;
  133. BEventLockJob lock_job;
  134. struct unlock_instance *unlock;
  135. int state;
  136. };
  137. struct unlock_instance {
  138. NCDModuleInst *i;
  139. struct lock_instance *lock;
  140. };
  141. static void unlock_free (struct unlock_instance *o);
  142. static int build_append_or_insert_cmdline (NCDModuleInst *i, NCDValRef args, const char *prog, int remove, char **exec, CmdLine *cl, const char *type)
  143. {
  144. if (NCDVal_ListRead(args, 1, &args) && !NCDVal_IsList(args)) {
  145. ModuleLog(i, BLOG_ERROR, "in one-argument form a list is expected");
  146. goto fail0;
  147. }
  148. // read arguments
  149. NCDValRef table_arg;
  150. NCDValRef chain_arg;
  151. if (!NCDVal_ListReadHead(args, 2, &table_arg, &chain_arg)) {
  152. ModuleLog(i, BLOG_ERROR, "wrong arity");
  153. goto fail0;
  154. }
  155. if (!NCDVal_IsStringNoNulls(table_arg) || !NCDVal_IsStringNoNulls(chain_arg)) {
  156. ModuleLog(i, BLOG_ERROR, "wrong type");
  157. goto fail0;
  158. }
  159. const char *table = NCDVal_StringData(table_arg);
  160. size_t table_len = NCDVal_StringLength(table_arg);
  161. const char *chain = NCDVal_StringData(chain_arg);
  162. size_t chain_len = NCDVal_StringLength(chain_arg);
  163. // find program
  164. if (!(*exec = badvpn_find_program(prog))) {
  165. ModuleLog(i, BLOG_ERROR, "failed to find program: %s", prog);
  166. goto fail0;
  167. }
  168. // start cmdline
  169. if (!CmdLine_Init(cl)) {
  170. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  171. goto fail1;
  172. }
  173. // add header
  174. if (!CmdLine_Append(cl, *exec) || !CmdLine_Append(cl, "-t") || !CmdLine_AppendNoNull(cl, table, table_len) || !CmdLine_Append(cl, (remove ? "-D" : type)) || !CmdLine_AppendNoNull(cl, chain, chain_len)) {
  175. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  176. goto fail2;
  177. }
  178. // add additional arguments
  179. size_t count = NCDVal_ListCount(args);
  180. for (size_t j = 2; j < count; j++) {
  181. NCDValRef arg = NCDVal_ListGet(args, j);
  182. if (!NCDVal_IsStringNoNulls(arg)) {
  183. ModuleLog(i, BLOG_ERROR, "wrong type");
  184. goto fail2;
  185. }
  186. if (!CmdLine_AppendNoNull(cl, NCDVal_StringData(arg), NCDVal_StringLength(arg))) {
  187. ModuleLog(i, BLOG_ERROR, "CmdLine_AppendNoNull failed");
  188. goto fail2;
  189. }
  190. }
  191. // finish
  192. if (!CmdLine_Finish(cl)) {
  193. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  194. goto fail2;
  195. }
  196. return 1;
  197. fail2:
  198. CmdLine_Free(cl);
  199. fail1:
  200. free(*exec);
  201. fail0:
  202. return 0;
  203. }
  204. static int build_append_cmdline (NCDModuleInst *i, NCDValRef args, const char *prog, int remove, char **exec, CmdLine *cl)
  205. {
  206. return build_append_or_insert_cmdline(i, args, prog, remove, exec, cl, "-A");
  207. }
  208. static int build_insert_cmdline (NCDModuleInst *i, NCDValRef args, const char *prog, int remove, char **exec, CmdLine *cl)
  209. {
  210. return build_append_or_insert_cmdline(i, args, prog, remove, exec, cl, "-I");
  211. }
  212. static int build_policy_cmdline (NCDModuleInst *i, NCDValRef args, const char *prog, int remove, char **exec, CmdLine *cl)
  213. {
  214. // read arguments
  215. NCDValRef table_arg;
  216. NCDValRef chain_arg;
  217. NCDValRef target_arg;
  218. NCDValRef revert_target_arg;
  219. if (!NCDVal_ListRead(args, 4, &table_arg, &chain_arg, &target_arg, &revert_target_arg)) {
  220. ModuleLog(i, BLOG_ERROR, "wrong arity");
  221. goto fail0;
  222. }
  223. if (!NCDVal_IsStringNoNulls(table_arg) || !NCDVal_IsStringNoNulls(chain_arg) ||
  224. !NCDVal_IsStringNoNulls(target_arg) || !NCDVal_IsStringNoNulls(revert_target_arg)
  225. ) {
  226. ModuleLog(i, BLOG_ERROR, "wrong type");
  227. goto fail0;
  228. }
  229. const char *table = NCDVal_StringData(table_arg);
  230. size_t table_len = NCDVal_StringLength(table_arg);
  231. const char *chain = NCDVal_StringData(chain_arg);
  232. size_t chain_len = NCDVal_StringLength(chain_arg);
  233. const char *target = NCDVal_StringData(target_arg);
  234. size_t target_len = NCDVal_StringLength(target_arg);
  235. const char *revert_target = NCDVal_StringData(revert_target_arg);
  236. size_t revert_target_len = NCDVal_StringLength(revert_target_arg);
  237. // find program
  238. if (!(*exec = badvpn_find_program(prog))) {
  239. ModuleLog(i, BLOG_ERROR, "failed to find program: %s", prog);
  240. goto fail0;
  241. }
  242. // start cmdline
  243. if (!CmdLine_Init(cl)) {
  244. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  245. goto fail1;
  246. }
  247. // add arguments
  248. if (!CmdLine_Append(cl, *exec) || !CmdLine_Append(cl, "-t") || !CmdLine_AppendNoNull(cl, table, table_len) ||
  249. !CmdLine_Append(cl, "-P") || !CmdLine_AppendNoNull(cl, chain, chain_len) ||
  250. !CmdLine_AppendNoNull(cl, (remove ? revert_target : target), (remove ? revert_target_len : target_len))
  251. ) {
  252. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  253. goto fail2;
  254. }
  255. // finish
  256. if (!CmdLine_Finish(cl)) {
  257. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  258. goto fail2;
  259. }
  260. return 1;
  261. fail2:
  262. CmdLine_Free(cl);
  263. fail1:
  264. free(*exec);
  265. fail0:
  266. return 0;
  267. }
  268. static int build_newchain_cmdline (NCDModuleInst *i, NCDValRef args, const char *prog, int remove, char **exec, CmdLine *cl)
  269. {
  270. // read arguments
  271. NCDValRef table_arg = NCDVal_NewInvalid();
  272. NCDValRef chain_arg;
  273. if (!NCDVal_ListRead(args, 1, &chain_arg) && !NCDVal_ListRead(args, 2, &table_arg, &chain_arg)) {
  274. ModuleLog(i, BLOG_ERROR, "wrong arity");
  275. goto fail0;
  276. }
  277. if ((!NCDVal_IsInvalid(table_arg) && !NCDVal_IsStringNoNulls(table_arg)) || !NCDVal_IsStringNoNulls(chain_arg)) {
  278. ModuleLog(i, BLOG_ERROR, "wrong type");
  279. goto fail0;
  280. }
  281. const char *table = (NCDVal_IsInvalid(table_arg) ? "filter" : NCDVal_StringData(table_arg));
  282. size_t table_len = (NCDVal_IsInvalid(table_arg) ? 6 : NCDVal_StringLength(table_arg));
  283. const char *chain = NCDVal_StringData(chain_arg);
  284. size_t chain_len = NCDVal_StringLength(chain_arg);
  285. // find program
  286. if (!(*exec = badvpn_find_program(prog))) {
  287. ModuleLog(i, BLOG_ERROR, "failed to find program: %s", prog);
  288. goto fail0;
  289. }
  290. // start cmdline
  291. if (!CmdLine_Init(cl)) {
  292. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  293. goto fail1;
  294. }
  295. // add arguments
  296. if (!CmdLine_AppendMulti(cl, 2, *exec, "-t") ||
  297. !CmdLine_AppendNoNull(cl, table, table_len) ||
  298. !CmdLine_Append(cl, (remove ? "-X" : "-N")) ||
  299. !CmdLine_AppendNoNull(cl, chain, chain_len)
  300. ) {
  301. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  302. goto fail2;
  303. }
  304. // finish
  305. if (!CmdLine_Finish(cl)) {
  306. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  307. goto fail2;
  308. }
  309. return 1;
  310. fail2:
  311. CmdLine_Free(cl);
  312. fail1:
  313. free(*exec);
  314. fail0:
  315. return 0;
  316. }
  317. static int build_iptables_append_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  318. {
  319. return build_append_cmdline(i, args, "iptables", remove, exec, cl);
  320. }
  321. static int build_iptables_insert_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  322. {
  323. return build_insert_cmdline(i, args, "iptables", remove, exec, cl);
  324. }
  325. static int build_iptables_policy_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  326. {
  327. return build_policy_cmdline(i, args, "iptables", remove, exec, cl);
  328. }
  329. static int build_iptables_newchain_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  330. {
  331. return build_newchain_cmdline(i, args, "iptables", remove, exec, cl);
  332. }
  333. static int build_ip6tables_append_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  334. {
  335. return build_append_cmdline(i, args, "ip6tables", remove, exec, cl);
  336. }
  337. static int build_ip6tables_insert_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  338. {
  339. return build_insert_cmdline(i, args, "ip6tables", remove, exec, cl);
  340. }
  341. static int build_ip6tables_policy_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  342. {
  343. return build_policy_cmdline(i, args, "ip6tables", remove, exec, cl);
  344. }
  345. static int build_ip6tables_newchain_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  346. {
  347. return build_newchain_cmdline(i, args, "ip6tables", remove, exec, cl);
  348. }
  349. static int build_ebtables_append_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  350. {
  351. return build_append_cmdline(i, args, "ebtables", remove, exec, cl);
  352. }
  353. static int build_ebtables_insert_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  354. {
  355. return build_insert_cmdline(i, args, "ebtables", remove, exec, cl);
  356. }
  357. static int build_ebtables_policy_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  358. {
  359. return build_policy_cmdline(i, args, "ebtables", remove, exec, cl);
  360. }
  361. static int build_ebtables_newchain_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  362. {
  363. return build_newchain_cmdline(i, args, "ebtables", remove, exec, cl);
  364. }
  365. static void lock_job_handler (struct lock_instance *o)
  366. {
  367. ASSERT(o->state == LOCK_STATE_LOCKING || o->state == LOCK_STATE_RELOCKING)
  368. if (o->state == LOCK_STATE_LOCKING) {
  369. ASSERT(!o->unlock)
  370. // up
  371. NCDModuleInst_Backend_Up(o->i);
  372. // set state locked
  373. o->state = LOCK_STATE_LOCKED;
  374. }
  375. else if (o->state == LOCK_STATE_RELOCKING) {
  376. ASSERT(o->unlock)
  377. ASSERT(o->unlock->lock == o)
  378. // die unlock
  379. unlock_free(o->unlock);
  380. o->unlock = NULL;
  381. // set state locked
  382. o->state = LOCK_STATE_LOCKED;
  383. }
  384. }
  385. static int func_globalinit (struct NCDInterpModuleGroup *group, const struct NCDModuleInst_iparams *params)
  386. {
  387. // allocate global state structure
  388. struct global *g = BAlloc(sizeof(*g));
  389. if (!g) {
  390. BLog(BLOG_ERROR, "BAlloc failed");
  391. return 0;
  392. }
  393. // set group state pointer
  394. group->group_state = g;
  395. // init iptables lock
  396. BEventLock_Init(&g->iptables_lock, BReactor_PendingGroup(params->reactor));
  397. return 1;
  398. }
  399. static void func_globalfree (struct NCDInterpModuleGroup *group)
  400. {
  401. struct global *g = group->group_state;
  402. // free iptables lock
  403. BEventLock_Free(&g->iptables_lock);
  404. // free global state structure
  405. BFree(g);
  406. }
  407. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, command_template_build_cmdline build_cmdline)
  408. {
  409. struct global *g = ModuleGlobal(i);
  410. struct instance *o = vo;
  411. o->i = i;
  412. command_template_new(&o->cti, i, params, build_cmdline, template_free_func, o, BLOG_CURRENT_CHANNEL, &g->iptables_lock);
  413. }
  414. void template_free_func (void *vo, int is_error)
  415. {
  416. struct instance *o = vo;
  417. if (is_error) {
  418. NCDModuleInst_Backend_DeadError(o->i);
  419. } else {
  420. NCDModuleInst_Backend_Dead(o->i);
  421. }
  422. }
  423. static void append_iptables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  424. {
  425. func_new(vo, i, params, build_iptables_append_cmdline);
  426. }
  427. static void insert_iptables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  428. {
  429. func_new(vo, i, params, build_iptables_insert_cmdline);
  430. }
  431. static void policy_iptables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  432. {
  433. func_new(vo, i, params, build_iptables_policy_cmdline);
  434. }
  435. static void newchain_iptables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  436. {
  437. func_new(vo, i, params, build_iptables_newchain_cmdline);
  438. }
  439. static void append_ip6tables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  440. {
  441. func_new(vo, i, params, build_ip6tables_append_cmdline);
  442. }
  443. static void insert_ip6tables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  444. {
  445. func_new(vo, i, params, build_ip6tables_insert_cmdline);
  446. }
  447. static void policy_ip6tables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  448. {
  449. func_new(vo, i, params, build_ip6tables_policy_cmdline);
  450. }
  451. static void newchain_ip6tables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  452. {
  453. func_new(vo, i, params, build_ip6tables_newchain_cmdline);
  454. }
  455. static void append_ebtables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  456. {
  457. func_new(vo, i, params, build_ebtables_append_cmdline);
  458. }
  459. static void insert_ebtables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  460. {
  461. func_new(vo, i, params, build_ebtables_insert_cmdline);
  462. }
  463. static void policy_ebtables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  464. {
  465. func_new(vo, i, params, build_ebtables_policy_cmdline);
  466. }
  467. static void newchain_ebtables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  468. {
  469. func_new(vo, i, params, build_ebtables_newchain_cmdline);
  470. }
  471. static void func_die (void *vo)
  472. {
  473. struct instance *o = vo;
  474. command_template_die(&o->cti);
  475. }
  476. static void lock_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  477. {
  478. struct global *g = ModuleGlobal(i);
  479. struct lock_instance *o = vo;
  480. o->i = i;
  481. // init lock job
  482. BEventLockJob_Init(&o->lock_job, &g->iptables_lock, (BEventLock_handler)lock_job_handler, o);
  483. BEventLockJob_Wait(&o->lock_job);
  484. // set no unlock
  485. o->unlock = NULL;
  486. // set state locking
  487. o->state = LOCK_STATE_LOCKING;
  488. }
  489. static void lock_func_die (void *vo)
  490. {
  491. struct lock_instance *o = vo;
  492. if (o->state == LOCK_STATE_UNLOCKED) {
  493. ASSERT(o->unlock)
  494. ASSERT(o->unlock->lock == o)
  495. o->unlock->lock = NULL;
  496. }
  497. else if (o->state == LOCK_STATE_RELOCKING) {
  498. ASSERT(o->unlock)
  499. ASSERT(o->unlock->lock == o)
  500. unlock_free(o->unlock);
  501. }
  502. else {
  503. ASSERT(!o->unlock)
  504. }
  505. // free lock job
  506. BEventLockJob_Free(&o->lock_job);
  507. // dead
  508. NCDModuleInst_Backend_Dead(o->i);
  509. }
  510. static void unlock_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  511. {
  512. struct unlock_instance *o = vo;
  513. o->i = i;
  514. // get lock lock
  515. struct lock_instance *lock = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  516. // make sure lock doesn't already have an unlock
  517. if (lock->unlock) {
  518. BLog(BLOG_ERROR, "lock already has an unlock");
  519. goto fail0;
  520. }
  521. // make sure lock is locked
  522. if (lock->state != LOCK_STATE_LOCKED) {
  523. BLog(BLOG_ERROR, "lock is not locked");
  524. goto fail0;
  525. }
  526. // set lock
  527. o->lock = lock;
  528. // set unlock in lock
  529. lock->unlock = o;
  530. // up
  531. NCDModuleInst_Backend_Up(o->i);
  532. // release lock
  533. BEventLockJob_Release(&lock->lock_job);
  534. // set lock state unlocked
  535. lock->state = LOCK_STATE_UNLOCKED;
  536. return;
  537. fail0:
  538. NCDModuleInst_Backend_DeadError(i);
  539. }
  540. static void unlock_func_die (void *vo)
  541. {
  542. struct unlock_instance *o = vo;
  543. // if lock is gone, die right away
  544. if (!o->lock) {
  545. unlock_free(o);
  546. return;
  547. }
  548. ASSERT(o->lock->unlock == o)
  549. ASSERT(o->lock->state == LOCK_STATE_UNLOCKED)
  550. // wait lock
  551. BEventLockJob_Wait(&o->lock->lock_job);
  552. // set lock state relocking
  553. o->lock->state = LOCK_STATE_RELOCKING;
  554. }
  555. static void unlock_free (struct unlock_instance *o)
  556. {
  557. NCDModuleInst_Backend_Dead(o->i);
  558. }
  559. static struct NCDModule modules[] = {
  560. {
  561. .type = "net.iptables.append",
  562. .func_new2 = append_iptables_func_new,
  563. .func_die = func_die,
  564. .alloc_size = sizeof(struct instance)
  565. }, {
  566. .type = "net.iptables.insert",
  567. .func_new2 = insert_iptables_func_new,
  568. .func_die = func_die,
  569. .alloc_size = sizeof(struct instance)
  570. }, {
  571. .type = "net.iptables.policy",
  572. .func_new2 = policy_iptables_func_new,
  573. .func_die = func_die,
  574. .alloc_size = sizeof(struct instance)
  575. }, {
  576. .type = "net.iptables.newchain",
  577. .func_new2 = newchain_iptables_func_new,
  578. .func_die = func_die,
  579. .alloc_size = sizeof(struct instance)
  580. }, {
  581. .type = "net.ip6tables.append",
  582. .func_new2 = append_ip6tables_func_new,
  583. .func_die = func_die,
  584. .alloc_size = sizeof(struct instance)
  585. }, {
  586. .type = "net.ip6tables.insert",
  587. .func_new2 = insert_ip6tables_func_new,
  588. .func_die = func_die,
  589. .alloc_size = sizeof(struct instance)
  590. }, {
  591. .type = "net.ip6tables.policy",
  592. .func_new2 = policy_ip6tables_func_new,
  593. .func_die = func_die,
  594. .alloc_size = sizeof(struct instance)
  595. }, {
  596. .type = "net.ip6tables.newchain",
  597. .func_new2 = newchain_ip6tables_func_new,
  598. .func_die = func_die,
  599. .alloc_size = sizeof(struct instance)
  600. }, {
  601. .type = "net.ebtables.append",
  602. .func_new2 = append_ebtables_func_new,
  603. .func_die = func_die,
  604. .alloc_size = sizeof(struct instance)
  605. }, {
  606. .type = "net.ebtables.insert",
  607. .func_new2 = insert_ebtables_func_new,
  608. .func_die = func_die,
  609. .alloc_size = sizeof(struct instance)
  610. }, {
  611. .type = "net.ebtables.policy",
  612. .func_new2 = policy_ebtables_func_new,
  613. .func_die = func_die,
  614. .alloc_size = sizeof(struct instance)
  615. }, {
  616. .type = "net.ebtables.newchain",
  617. .func_new2 = newchain_ebtables_func_new,
  618. .func_die = func_die,
  619. .alloc_size = sizeof(struct instance)
  620. }, {
  621. .type = "net.iptables.lock",
  622. .func_new2 = lock_func_new,
  623. .func_die = lock_func_die,
  624. .alloc_size = sizeof(struct lock_instance)
  625. }, {
  626. .type = "net.iptables.lock::unlock",
  627. .func_new2 = unlock_func_new,
  628. .func_die = unlock_func_die,
  629. .alloc_size = sizeof(struct unlock_instance)
  630. }, {
  631. .type = NULL
  632. }
  633. };
  634. const struct NCDModuleGroup ncdmodule_net_iptables = {
  635. .modules = modules,
  636. .func_globalinit = func_globalinit,
  637. .func_globalfree = func_globalfree
  638. };