net_iptables.c 21 KB

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