net_iptables.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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_ip6tables_append_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  336. {
  337. return build_append_cmdline(i, args, "ip6tables", remove, exec, cl);
  338. }
  339. static int build_ip6tables_insert_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  340. {
  341. return build_insert_cmdline(i, args, "ip6tables", remove, exec, cl);
  342. }
  343. static int build_ip6tables_policy_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  344. {
  345. return build_policy_cmdline(i, args, "ip6tables", remove, exec, cl);
  346. }
  347. static int build_ip6tables_newchain_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  348. {
  349. return build_newchain_cmdline(i, args, "ip6tables", remove, exec, cl);
  350. }
  351. static int build_ebtables_append_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  352. {
  353. return build_append_cmdline(i, args, "ebtables", remove, exec, cl);
  354. }
  355. static int build_ebtables_insert_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  356. {
  357. return build_insert_cmdline(i, args, "ebtables", remove, exec, cl);
  358. }
  359. static int build_ebtables_policy_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  360. {
  361. return build_policy_cmdline(i, args, "ebtables", remove, exec, cl);
  362. }
  363. static int build_ebtables_newchain_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  364. {
  365. return build_newchain_cmdline(i, args, "ebtables", remove, exec, cl);
  366. }
  367. static void lock_job_handler (struct lock_instance *o)
  368. {
  369. ASSERT(o->state == LOCK_STATE_LOCKING || o->state == LOCK_STATE_RELOCKING)
  370. if (o->state == LOCK_STATE_LOCKING) {
  371. ASSERT(!o->unlock)
  372. // up
  373. NCDModuleInst_Backend_Up(o->i);
  374. // set state locked
  375. o->state = LOCK_STATE_LOCKED;
  376. }
  377. else if (o->state == LOCK_STATE_RELOCKING) {
  378. ASSERT(o->unlock)
  379. ASSERT(o->unlock->lock == o)
  380. // die unlock
  381. unlock_free(o->unlock);
  382. o->unlock = NULL;
  383. // set state locked
  384. o->state = LOCK_STATE_LOCKED;
  385. }
  386. }
  387. static int func_globalinit (struct NCDInterpModuleGroup *group, const struct NCDModuleInst_iparams *params)
  388. {
  389. // allocate global state structure
  390. struct global *g = BAlloc(sizeof(*g));
  391. if (!g) {
  392. BLog(BLOG_ERROR, "BAlloc failed");
  393. return 0;
  394. }
  395. // set group state pointer
  396. group->group_state = g;
  397. // init iptables lock
  398. BEventLock_Init(&g->iptables_lock, BReactor_PendingGroup(params->reactor));
  399. return 1;
  400. }
  401. static void func_globalfree (struct NCDInterpModuleGroup *group)
  402. {
  403. struct global *g = group->group_state;
  404. // free iptables lock
  405. BEventLock_Free(&g->iptables_lock);
  406. // free global state structure
  407. BFree(g);
  408. }
  409. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, command_template_build_cmdline build_cmdline)
  410. {
  411. struct global *g = ModuleGlobal(i);
  412. struct instance *o = vo;
  413. o->i = i;
  414. command_template_new(&o->cti, i, params, build_cmdline, template_free_func, o, BLOG_CURRENT_CHANNEL, &g->iptables_lock);
  415. }
  416. void template_free_func (void *vo, int is_error)
  417. {
  418. struct instance *o = vo;
  419. if (is_error) {
  420. NCDModuleInst_Backend_DeadError(o->i);
  421. } else {
  422. NCDModuleInst_Backend_Dead(o->i);
  423. }
  424. }
  425. static void append_iptables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  426. {
  427. func_new(vo, i, params, build_iptables_append_cmdline);
  428. }
  429. static void insert_iptables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  430. {
  431. func_new(vo, i, params, build_iptables_insert_cmdline);
  432. }
  433. static void policy_iptables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  434. {
  435. func_new(vo, i, params, build_iptables_policy_cmdline);
  436. }
  437. static void newchain_iptables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  438. {
  439. func_new(vo, i, params, build_iptables_newchain_cmdline);
  440. }
  441. static void append_ip6tables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  442. {
  443. func_new(vo, i, params, build_ip6tables_append_cmdline);
  444. }
  445. static void insert_ip6tables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  446. {
  447. func_new(vo, i, params, build_ip6tables_insert_cmdline);
  448. }
  449. static void policy_ip6tables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  450. {
  451. func_new(vo, i, params, build_ip6tables_policy_cmdline);
  452. }
  453. static void newchain_ip6tables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  454. {
  455. func_new(vo, i, params, build_ip6tables_newchain_cmdline);
  456. }
  457. static void append_ebtables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  458. {
  459. func_new(vo, i, params, build_ebtables_append_cmdline);
  460. }
  461. static void insert_ebtables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  462. {
  463. func_new(vo, i, params, build_ebtables_insert_cmdline);
  464. }
  465. static void policy_ebtables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  466. {
  467. func_new(vo, i, params, build_ebtables_policy_cmdline);
  468. }
  469. static void newchain_ebtables_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  470. {
  471. func_new(vo, i, params, build_ebtables_newchain_cmdline);
  472. }
  473. static void func_die (void *vo)
  474. {
  475. struct instance *o = vo;
  476. command_template_die(&o->cti);
  477. }
  478. static void lock_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  479. {
  480. struct global *g = ModuleGlobal(i);
  481. struct lock_instance *o = vo;
  482. o->i = i;
  483. // init lock job
  484. BEventLockJob_Init(&o->lock_job, &g->iptables_lock, (BEventLock_handler)lock_job_handler, o);
  485. BEventLockJob_Wait(&o->lock_job);
  486. // set no unlock
  487. o->unlock = NULL;
  488. // set state locking
  489. o->state = LOCK_STATE_LOCKING;
  490. }
  491. static void lock_func_die (void *vo)
  492. {
  493. struct lock_instance *o = vo;
  494. if (o->state == LOCK_STATE_UNLOCKED) {
  495. ASSERT(o->unlock)
  496. ASSERT(o->unlock->lock == o)
  497. o->unlock->lock = NULL;
  498. }
  499. else if (o->state == LOCK_STATE_RELOCKING) {
  500. ASSERT(o->unlock)
  501. ASSERT(o->unlock->lock == o)
  502. unlock_free(o->unlock);
  503. }
  504. else {
  505. ASSERT(!o->unlock)
  506. }
  507. // free lock job
  508. BEventLockJob_Free(&o->lock_job);
  509. // dead
  510. NCDModuleInst_Backend_Dead(o->i);
  511. }
  512. static void unlock_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  513. {
  514. struct unlock_instance *o = vo;
  515. o->i = i;
  516. // get lock lock
  517. struct lock_instance *lock = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  518. // make sure lock doesn't already have an unlock
  519. if (lock->unlock) {
  520. BLog(BLOG_ERROR, "lock already has an unlock");
  521. goto fail0;
  522. }
  523. // make sure lock is locked
  524. if (lock->state != LOCK_STATE_LOCKED) {
  525. BLog(BLOG_ERROR, "lock is not locked");
  526. goto fail0;
  527. }
  528. // set lock
  529. o->lock = lock;
  530. // set unlock in lock
  531. lock->unlock = o;
  532. // up
  533. NCDModuleInst_Backend_Up(o->i);
  534. // release lock
  535. BEventLockJob_Release(&lock->lock_job);
  536. // set lock state unlocked
  537. lock->state = LOCK_STATE_UNLOCKED;
  538. return;
  539. fail0:
  540. NCDModuleInst_Backend_DeadError(i);
  541. }
  542. static void unlock_func_die (void *vo)
  543. {
  544. struct unlock_instance *o = vo;
  545. // if lock is gone, die right away
  546. if (!o->lock) {
  547. unlock_free(o);
  548. return;
  549. }
  550. ASSERT(o->lock->unlock == o)
  551. ASSERT(o->lock->state == LOCK_STATE_UNLOCKED)
  552. // wait lock
  553. BEventLockJob_Wait(&o->lock->lock_job);
  554. // set lock state relocking
  555. o->lock->state = LOCK_STATE_RELOCKING;
  556. }
  557. static void unlock_free (struct unlock_instance *o)
  558. {
  559. NCDModuleInst_Backend_Dead(o->i);
  560. }
  561. static struct NCDModule modules[] = {
  562. {
  563. .type = "net.iptables.append",
  564. .func_new2 = append_iptables_func_new,
  565. .func_die = func_die,
  566. .alloc_size = sizeof(struct instance)
  567. }, {
  568. .type = "net.iptables.insert",
  569. .func_new2 = insert_iptables_func_new,
  570. .func_die = func_die,
  571. .alloc_size = sizeof(struct instance)
  572. }, {
  573. .type = "net.iptables.policy",
  574. .func_new2 = policy_iptables_func_new,
  575. .func_die = func_die,
  576. .alloc_size = sizeof(struct instance)
  577. }, {
  578. .type = "net.iptables.newchain",
  579. .func_new2 = newchain_iptables_func_new,
  580. .func_die = func_die,
  581. .alloc_size = sizeof(struct instance)
  582. }, {
  583. .type = "net.ip6tables.append",
  584. .func_new2 = append_ip6tables_func_new,
  585. .func_die = func_die,
  586. .alloc_size = sizeof(struct instance)
  587. }, {
  588. .type = "net.ip6tables.insert",
  589. .func_new2 = insert_ip6tables_func_new,
  590. .func_die = func_die,
  591. .alloc_size = sizeof(struct instance)
  592. }, {
  593. .type = "net.ip6tables.policy",
  594. .func_new2 = policy_ip6tables_func_new,
  595. .func_die = func_die,
  596. .alloc_size = sizeof(struct instance)
  597. }, {
  598. .type = "net.ip6tables.newchain",
  599. .func_new2 = newchain_ip6tables_func_new,
  600. .func_die = func_die,
  601. .alloc_size = sizeof(struct instance)
  602. }, {
  603. .type = "net.ebtables.append",
  604. .func_new2 = append_ebtables_func_new,
  605. .func_die = func_die,
  606. .alloc_size = sizeof(struct instance)
  607. }, {
  608. .type = "net.ebtables.insert",
  609. .func_new2 = insert_ebtables_func_new,
  610. .func_die = func_die,
  611. .alloc_size = sizeof(struct instance)
  612. }, {
  613. .type = "net.ebtables.policy",
  614. .func_new2 = policy_ebtables_func_new,
  615. .func_die = func_die,
  616. .alloc_size = sizeof(struct instance)
  617. }, {
  618. .type = "net.ebtables.newchain",
  619. .func_new2 = newchain_ebtables_func_new,
  620. .func_die = func_die,
  621. .alloc_size = sizeof(struct instance)
  622. }, {
  623. .type = "net.iptables.lock",
  624. .func_new2 = lock_func_new,
  625. .func_die = lock_func_die,
  626. .alloc_size = sizeof(struct lock_instance)
  627. }, {
  628. .type = "net.iptables.lock::unlock",
  629. .func_new2 = unlock_func_new,
  630. .func_die = unlock_func_die,
  631. .alloc_size = sizeof(struct unlock_instance)
  632. }, {
  633. .type = NULL
  634. }
  635. };
  636. const struct NCDModuleGroup ncdmodule_net_iptables = {
  637. .modules = modules,
  638. .func_globalinit = func_globalinit,
  639. .func_globalfree = func_globalfree
  640. };