net_iptables.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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. MemRef table = NCDVal_StringMemRef(table_arg);
  160. MemRef chain = NCDVal_StringMemRef(chain_arg);
  161. // find program
  162. if (!(*exec = badvpn_find_program(prog))) {
  163. ModuleLog(i, BLOG_ERROR, "failed to find program: %s", prog);
  164. goto fail0;
  165. }
  166. // start cmdline
  167. if (!CmdLine_Init(cl)) {
  168. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  169. goto fail1;
  170. }
  171. // add header
  172. if (!CmdLine_Append(cl, *exec) || !CmdLine_Append(cl, "-t") || !CmdLine_AppendNoNullMr(cl, table) || !CmdLine_Append(cl, (remove ? "-D" : type)) || !CmdLine_AppendNoNullMr(cl, chain)) {
  173. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  174. goto fail2;
  175. }
  176. // add additional arguments
  177. size_t count = NCDVal_ListCount(args);
  178. for (size_t j = 2; j < count; j++) {
  179. NCDValRef arg = NCDVal_ListGet(args, j);
  180. if (!NCDVal_IsStringNoNulls(arg)) {
  181. ModuleLog(i, BLOG_ERROR, "wrong type");
  182. goto fail2;
  183. }
  184. if (!CmdLine_AppendNoNullMr(cl, NCDVal_StringMemRef(arg))) {
  185. ModuleLog(i, BLOG_ERROR, "CmdLine_AppendNoNullMr failed");
  186. goto fail2;
  187. }
  188. }
  189. // finish
  190. if (!CmdLine_Finish(cl)) {
  191. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  192. goto fail2;
  193. }
  194. return 1;
  195. fail2:
  196. CmdLine_Free(cl);
  197. fail1:
  198. free(*exec);
  199. fail0:
  200. return 0;
  201. }
  202. static int build_append_cmdline (NCDModuleInst *i, NCDValRef args, const char *prog, int remove, char **exec, CmdLine *cl)
  203. {
  204. return build_append_or_insert_cmdline(i, args, prog, remove, exec, cl, "-A");
  205. }
  206. static int build_insert_cmdline (NCDModuleInst *i, NCDValRef args, const char *prog, int remove, char **exec, CmdLine *cl)
  207. {
  208. return build_append_or_insert_cmdline(i, args, prog, remove, exec, cl, "-I");
  209. }
  210. static int build_policy_cmdline (NCDModuleInst *i, NCDValRef args, const char *prog, int remove, char **exec, CmdLine *cl)
  211. {
  212. // read arguments
  213. NCDValRef table_arg;
  214. NCDValRef chain_arg;
  215. NCDValRef target_arg;
  216. NCDValRef revert_target_arg;
  217. if (!NCDVal_ListRead(args, 4, &table_arg, &chain_arg, &target_arg, &revert_target_arg)) {
  218. ModuleLog(i, BLOG_ERROR, "wrong arity");
  219. goto fail0;
  220. }
  221. if (!NCDVal_IsStringNoNulls(table_arg) || !NCDVal_IsStringNoNulls(chain_arg) ||
  222. !NCDVal_IsStringNoNulls(target_arg) || !NCDVal_IsStringNoNulls(revert_target_arg)
  223. ) {
  224. ModuleLog(i, BLOG_ERROR, "wrong type");
  225. goto fail0;
  226. }
  227. MemRef table = NCDVal_StringMemRef(table_arg);
  228. MemRef chain = NCDVal_StringMemRef(chain_arg);
  229. MemRef target = NCDVal_StringMemRef(target_arg);
  230. MemRef revert_target = NCDVal_StringMemRef(revert_target_arg);
  231. // find program
  232. if (!(*exec = badvpn_find_program(prog))) {
  233. ModuleLog(i, BLOG_ERROR, "failed to find program: %s", prog);
  234. goto fail0;
  235. }
  236. // start cmdline
  237. if (!CmdLine_Init(cl)) {
  238. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  239. goto fail1;
  240. }
  241. // add arguments
  242. if (!CmdLine_Append(cl, *exec) || !CmdLine_Append(cl, "-t") || !CmdLine_AppendNoNullMr(cl, table) ||
  243. !CmdLine_Append(cl, "-P") || !CmdLine_AppendNoNullMr(cl, chain) ||
  244. !CmdLine_AppendNoNullMr(cl, (remove ? revert_target : target))
  245. ) {
  246. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  247. goto fail2;
  248. }
  249. // finish
  250. if (!CmdLine_Finish(cl)) {
  251. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  252. goto fail2;
  253. }
  254. return 1;
  255. fail2:
  256. CmdLine_Free(cl);
  257. fail1:
  258. free(*exec);
  259. fail0:
  260. return 0;
  261. }
  262. static int build_newchain_cmdline (NCDModuleInst *i, NCDValRef args, const char *prog, int remove, char **exec, CmdLine *cl)
  263. {
  264. // read arguments
  265. NCDValRef table_arg = NCDVal_NewInvalid();
  266. NCDValRef chain_arg;
  267. if (!NCDVal_ListRead(args, 1, &chain_arg) && !NCDVal_ListRead(args, 2, &table_arg, &chain_arg)) {
  268. ModuleLog(i, BLOG_ERROR, "wrong arity");
  269. goto fail0;
  270. }
  271. if ((!NCDVal_IsInvalid(table_arg) && !NCDVal_IsStringNoNulls(table_arg)) || !NCDVal_IsStringNoNulls(chain_arg)) {
  272. ModuleLog(i, BLOG_ERROR, "wrong type");
  273. goto fail0;
  274. }
  275. MemRef table = NCDVal_IsInvalid(table_arg) ? MemRef_MakeCstr("filter") : NCDVal_StringMemRef(table_arg);
  276. MemRef chain = NCDVal_StringMemRef(chain_arg);
  277. // find program
  278. if (!(*exec = badvpn_find_program(prog))) {
  279. ModuleLog(i, BLOG_ERROR, "failed to find program: %s", prog);
  280. goto fail0;
  281. }
  282. // start cmdline
  283. if (!CmdLine_Init(cl)) {
  284. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  285. goto fail1;
  286. }
  287. // add arguments
  288. if (!CmdLine_AppendMulti(cl, 2, *exec, "-t") ||
  289. !CmdLine_AppendNoNullMr(cl, table) ||
  290. !CmdLine_Append(cl, (remove ? "-X" : "-N")) ||
  291. !CmdLine_AppendNoNullMr(cl, chain)
  292. ) {
  293. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  294. goto fail2;
  295. }
  296. // finish
  297. if (!CmdLine_Finish(cl)) {
  298. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  299. goto fail2;
  300. }
  301. return 1;
  302. fail2:
  303. CmdLine_Free(cl);
  304. fail1:
  305. free(*exec);
  306. fail0:
  307. return 0;
  308. }
  309. static int build_iptables_append_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  310. {
  311. return build_append_cmdline(i, args, "iptables", remove, exec, cl);
  312. }
  313. static int build_iptables_insert_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  314. {
  315. return build_insert_cmdline(i, args, "iptables", remove, exec, cl);
  316. }
  317. static int build_iptables_policy_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  318. {
  319. return build_policy_cmdline(i, args, "iptables", remove, exec, cl);
  320. }
  321. static int build_iptables_newchain_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  322. {
  323. return build_newchain_cmdline(i, args, "iptables", remove, exec, cl);
  324. }
  325. static int build_ip6tables_append_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  326. {
  327. return build_append_cmdline(i, args, "ip6tables", remove, exec, cl);
  328. }
  329. static int build_ip6tables_insert_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  330. {
  331. return build_insert_cmdline(i, args, "ip6tables", remove, exec, cl);
  332. }
  333. static int build_ip6tables_policy_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  334. {
  335. return build_policy_cmdline(i, args, "ip6tables", remove, exec, cl);
  336. }
  337. static int build_ip6tables_newchain_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  338. {
  339. return build_newchain_cmdline(i, args, "ip6tables", remove, exec, cl);
  340. }
  341. static int build_ebtables_append_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  342. {
  343. return build_append_cmdline(i, args, "ebtables", remove, exec, cl);
  344. }
  345. static int build_ebtables_insert_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  346. {
  347. return build_insert_cmdline(i, args, "ebtables", remove, exec, cl);
  348. }
  349. static int build_ebtables_policy_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  350. {
  351. return build_policy_cmdline(i, args, "ebtables", remove, exec, cl);
  352. }
  353. static int build_ebtables_newchain_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  354. {
  355. return build_newchain_cmdline(i, args, "ebtables", remove, exec, cl);
  356. }
  357. static void lock_job_handler (struct lock_instance *o)
  358. {
  359. ASSERT(o->state == LOCK_STATE_LOCKING || o->state == LOCK_STATE_RELOCKING)
  360. if (o->state == LOCK_STATE_LOCKING) {
  361. ASSERT(!o->unlock)
  362. // up
  363. NCDModuleInst_Backend_Up(o->i);
  364. // set state locked
  365. o->state = LOCK_STATE_LOCKED;
  366. }
  367. else if (o->state == LOCK_STATE_RELOCKING) {
  368. ASSERT(o->unlock)
  369. ASSERT(o->unlock->lock == o)
  370. // die unlock
  371. unlock_free(o->unlock);
  372. o->unlock = NULL;
  373. // set state locked
  374. o->state = LOCK_STATE_LOCKED;
  375. }
  376. }
  377. static int func_globalinit (struct NCDInterpModuleGroup *group, const struct NCDModuleInst_iparams *params)
  378. {
  379. // allocate global state structure
  380. struct global *g = BAlloc(sizeof(*g));
  381. if (!g) {
  382. BLog(BLOG_ERROR, "BAlloc failed");
  383. return 0;
  384. }
  385. // set group state pointer
  386. group->group_state = g;
  387. // init iptables lock
  388. BEventLock_Init(&g->iptables_lock, BReactor_PendingGroup(params->reactor));
  389. return 1;
  390. }
  391. static void func_globalfree (struct NCDInterpModuleGroup *group)
  392. {
  393. struct global *g = group->group_state;
  394. // free iptables lock
  395. BEventLock_Free(&g->iptables_lock);
  396. // free global state structure
  397. BFree(g);
  398. }
  399. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, command_template_build_cmdline build_cmdline)
  400. {
  401. struct global *g = ModuleGlobal(i);
  402. struct instance *o = vo;
  403. o->i = i;
  404. command_template_new(&o->cti, i, params, build_cmdline, template_free_func, o, BLOG_CURRENT_CHANNEL, &g->iptables_lock);
  405. }
  406. void template_free_func (void *vo, int is_error)
  407. {
  408. struct instance *o = vo;
  409. if (is_error) {
  410. NCDModuleInst_Backend_DeadError(o->i);
  411. } else {
  412. NCDModuleInst_Backend_Dead(o->i);
  413. }
  414. }
  415. static void append_iptables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  416. {
  417. func_new(vo, i, params, build_iptables_append_cmdline);
  418. }
  419. static void insert_iptables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  420. {
  421. func_new(vo, i, params, build_iptables_insert_cmdline);
  422. }
  423. static void policy_iptables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  424. {
  425. func_new(vo, i, params, build_iptables_policy_cmdline);
  426. }
  427. static void newchain_iptables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  428. {
  429. func_new(vo, i, params, build_iptables_newchain_cmdline);
  430. }
  431. static void append_ip6tables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  432. {
  433. func_new(vo, i, params, build_ip6tables_append_cmdline);
  434. }
  435. static void insert_ip6tables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  436. {
  437. func_new(vo, i, params, build_ip6tables_insert_cmdline);
  438. }
  439. static void policy_ip6tables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  440. {
  441. func_new(vo, i, params, build_ip6tables_policy_cmdline);
  442. }
  443. static void newchain_ip6tables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  444. {
  445. func_new(vo, i, params, build_ip6tables_newchain_cmdline);
  446. }
  447. static void append_ebtables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  448. {
  449. func_new(vo, i, params, build_ebtables_append_cmdline);
  450. }
  451. static void insert_ebtables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  452. {
  453. func_new(vo, i, params, build_ebtables_insert_cmdline);
  454. }
  455. static void policy_ebtables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  456. {
  457. func_new(vo, i, params, build_ebtables_policy_cmdline);
  458. }
  459. static void newchain_ebtables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  460. {
  461. func_new(vo, i, params, build_ebtables_newchain_cmdline);
  462. }
  463. static void func_die (void *vo)
  464. {
  465. struct instance *o = vo;
  466. command_template_die(&o->cti);
  467. }
  468. static void lock_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  469. {
  470. struct global *g = ModuleGlobal(i);
  471. struct lock_instance *o = vo;
  472. o->i = i;
  473. // init lock job
  474. BEventLockJob_Init(&o->lock_job, &g->iptables_lock, (BEventLock_handler)lock_job_handler, o);
  475. BEventLockJob_Wait(&o->lock_job);
  476. // set no unlock
  477. o->unlock = NULL;
  478. // set state locking
  479. o->state = LOCK_STATE_LOCKING;
  480. }
  481. static void lock_func_die (void *vo)
  482. {
  483. struct lock_instance *o = vo;
  484. if (o->state == LOCK_STATE_UNLOCKED) {
  485. ASSERT(o->unlock)
  486. ASSERT(o->unlock->lock == o)
  487. o->unlock->lock = NULL;
  488. }
  489. else if (o->state == LOCK_STATE_RELOCKING) {
  490. ASSERT(o->unlock)
  491. ASSERT(o->unlock->lock == o)
  492. unlock_free(o->unlock);
  493. }
  494. else {
  495. ASSERT(!o->unlock)
  496. }
  497. // free lock job
  498. BEventLockJob_Free(&o->lock_job);
  499. // dead
  500. NCDModuleInst_Backend_Dead(o->i);
  501. }
  502. static void unlock_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  503. {
  504. struct unlock_instance *o = vo;
  505. o->i = i;
  506. // get lock lock
  507. struct lock_instance *lock = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  508. // make sure lock doesn't already have an unlock
  509. if (lock->unlock) {
  510. BLog(BLOG_ERROR, "lock already has an unlock");
  511. goto fail0;
  512. }
  513. // make sure lock is locked
  514. if (lock->state != LOCK_STATE_LOCKED) {
  515. BLog(BLOG_ERROR, "lock is not locked");
  516. goto fail0;
  517. }
  518. // set lock
  519. o->lock = lock;
  520. // set unlock in lock
  521. lock->unlock = o;
  522. // up
  523. NCDModuleInst_Backend_Up(o->i);
  524. // release lock
  525. BEventLockJob_Release(&lock->lock_job);
  526. // set lock state unlocked
  527. lock->state = LOCK_STATE_UNLOCKED;
  528. return;
  529. fail0:
  530. NCDModuleInst_Backend_DeadError(i);
  531. }
  532. static void unlock_func_die (void *vo)
  533. {
  534. struct unlock_instance *o = vo;
  535. // if lock is gone, die right away
  536. if (!o->lock) {
  537. unlock_free(o);
  538. return;
  539. }
  540. ASSERT(o->lock->unlock == o)
  541. ASSERT(o->lock->state == LOCK_STATE_UNLOCKED)
  542. // wait lock
  543. BEventLockJob_Wait(&o->lock->lock_job);
  544. // set lock state relocking
  545. o->lock->state = LOCK_STATE_RELOCKING;
  546. }
  547. static void unlock_free (struct unlock_instance *o)
  548. {
  549. NCDModuleInst_Backend_Dead(o->i);
  550. }
  551. static struct NCDModule modules[] = {
  552. {
  553. .type = "net.iptables.append",
  554. .func_new2 = append_iptables_func_new,
  555. .func_die = func_die,
  556. .alloc_size = sizeof(struct instance)
  557. }, {
  558. .type = "net.iptables.insert",
  559. .func_new2 = insert_iptables_func_new,
  560. .func_die = func_die,
  561. .alloc_size = sizeof(struct instance)
  562. }, {
  563. .type = "net.iptables.policy",
  564. .func_new2 = policy_iptables_func_new,
  565. .func_die = func_die,
  566. .alloc_size = sizeof(struct instance)
  567. }, {
  568. .type = "net.iptables.newchain",
  569. .func_new2 = newchain_iptables_func_new,
  570. .func_die = func_die,
  571. .alloc_size = sizeof(struct instance)
  572. }, {
  573. .type = "net.ip6tables.append",
  574. .func_new2 = append_ip6tables_func_new,
  575. .func_die = func_die,
  576. .alloc_size = sizeof(struct instance)
  577. }, {
  578. .type = "net.ip6tables.insert",
  579. .func_new2 = insert_ip6tables_func_new,
  580. .func_die = func_die,
  581. .alloc_size = sizeof(struct instance)
  582. }, {
  583. .type = "net.ip6tables.policy",
  584. .func_new2 = policy_ip6tables_func_new,
  585. .func_die = func_die,
  586. .alloc_size = sizeof(struct instance)
  587. }, {
  588. .type = "net.ip6tables.newchain",
  589. .func_new2 = newchain_ip6tables_func_new,
  590. .func_die = func_die,
  591. .alloc_size = sizeof(struct instance)
  592. }, {
  593. .type = "net.ebtables.append",
  594. .func_new2 = append_ebtables_func_new,
  595. .func_die = func_die,
  596. .alloc_size = sizeof(struct instance)
  597. }, {
  598. .type = "net.ebtables.insert",
  599. .func_new2 = insert_ebtables_func_new,
  600. .func_die = func_die,
  601. .alloc_size = sizeof(struct instance)
  602. }, {
  603. .type = "net.ebtables.policy",
  604. .func_new2 = policy_ebtables_func_new,
  605. .func_die = func_die,
  606. .alloc_size = sizeof(struct instance)
  607. }, {
  608. .type = "net.ebtables.newchain",
  609. .func_new2 = newchain_ebtables_func_new,
  610. .func_die = func_die,
  611. .alloc_size = sizeof(struct instance)
  612. }, {
  613. .type = "net.iptables.lock",
  614. .func_new2 = lock_func_new,
  615. .func_die = lock_func_die,
  616. .alloc_size = sizeof(struct lock_instance)
  617. }, {
  618. .type = "net.iptables.lock::unlock",
  619. .func_new2 = unlock_func_new,
  620. .func_die = unlock_func_die,
  621. .alloc_size = sizeof(struct unlock_instance)
  622. }, {
  623. .type = NULL
  624. }
  625. };
  626. const struct NCDModuleGroup ncdmodule_net_iptables = {
  627. .modules = modules,
  628. .func_globalinit = func_globalinit,
  629. .func_globalfree = func_globalfree
  630. };