net_iptables.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. * Synopsis:
  41. * net.iptables.append(string table, string chain, string arg1 ...)
  42. * Description:
  43. * init: iptables -t table -A chain arg1 ...
  44. * deinit: iptables -t table -D chain arg1 ...
  45. *
  46. * Synopsis:
  47. * net.iptables.policy(string table, string chain, string target, string revert_target)
  48. * Description:
  49. * init: iptables -t table -P chain target
  50. * deinit: iptables -t table -P chain revert_target
  51. *
  52. * Synopsis:
  53. * net.iptables.newchain(string table, string chain)
  54. * net.iptables.newchain(string chain) // DEPRECATED, defaults to table="filter"
  55. * Description:
  56. * init: iptables -t table -N chain
  57. * deinit: iptables -t table -X chain
  58. *
  59. * Synopsis:
  60. * net.ebtables.append(string table, string chain, string arg1 ...)
  61. * Description:
  62. * init: ebtables -t table -A chain arg1 ...
  63. * deinit: ebtables -t table -D chain arg1 ...
  64. *
  65. * Synopsis:
  66. * net.ebtables.policy(string table, string chain, string target, string revert_target)
  67. * Description:
  68. * init: ebtables -t table -P chain target
  69. * deinit: ebtables -t table -P chain revert_target
  70. *
  71. * Synopsis:
  72. * net.ebtables.newchain(string table, string chain)
  73. * Description:
  74. * init: ebtables -t table -N chain
  75. * deinit: ebtables -t table -X chain
  76. *
  77. * Synopsis:
  78. * net.iptables.lock()
  79. * Description:
  80. * Use at the beginning of a block of custom iptables/ebtables commands to make sure
  81. * they do not interfere with other iptables/ebtables commands.
  82. * WARNING: improper usage of the lock can lead to deadlock. In particular:
  83. * - Do not call any of the iptables/ebtables wrappers above from a lock section;
  84. * those will attempt to aquire the lock themselves.
  85. * - Do not enter another lock section from a lock section.
  86. * - Do not perform any potentially long wait from a lock section.
  87. *
  88. * Synopsis:
  89. * net.iptables.lock::unlock()
  90. * Description:
  91. * Use at the end of a block of custom iptables/ebtables commands to make sure
  92. * they do not interfere with other iptables/ebtables commands.
  93. */
  94. #include <stdlib.h>
  95. #include <string.h>
  96. #include <unistd.h>
  97. #include <misc/debug.h>
  98. #include <misc/find_program.h>
  99. #include <ncd/BEventLock.h>
  100. #include <ncd/modules/command_template.h>
  101. #include <generated/blog_channel_ncd_net_iptables.h>
  102. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  103. static void template_free_func (void *vo, int is_error);
  104. static BEventLock iptables_lock;
  105. struct instance {
  106. NCDModuleInst *i;
  107. command_template_instance cti;
  108. };
  109. struct unlock_instance;
  110. #define LOCK_STATE_LOCKING 1
  111. #define LOCK_STATE_LOCKED 2
  112. #define LOCK_STATE_UNLOCKED 3
  113. #define LOCK_STATE_RELOCKING 4
  114. struct lock_instance {
  115. NCDModuleInst *i;
  116. BEventLockJob lock_job;
  117. struct unlock_instance *unlock;
  118. int state;
  119. };
  120. struct unlock_instance {
  121. NCDModuleInst *i;
  122. struct lock_instance *lock;
  123. };
  124. static void unlock_free (struct unlock_instance *o);
  125. static int build_append_cmdline (NCDModuleInst *i, const char *prog, int remove, char **exec, CmdLine *cl)
  126. {
  127. // read arguments
  128. NCDValRef table_arg;
  129. NCDValRef chain_arg;
  130. if (!NCDVal_ListReadHead(i->args, 2, &table_arg, &chain_arg)) {
  131. ModuleLog(i, BLOG_ERROR, "wrong arity");
  132. goto fail0;
  133. }
  134. if (!NCDVal_IsStringNoNulls(table_arg) || !NCDVal_IsStringNoNulls(chain_arg)) {
  135. ModuleLog(i, BLOG_ERROR, "wrong type");
  136. goto fail0;
  137. }
  138. const char *table = NCDVal_StringValue(table_arg);
  139. const char *chain = NCDVal_StringValue(chain_arg);
  140. // find program
  141. if (!(*exec = badvpn_find_program(prog))) {
  142. ModuleLog(i, BLOG_ERROR, "failed to find program: %s", prog);
  143. goto fail0;
  144. }
  145. // start cmdline
  146. if (!CmdLine_Init(cl)) {
  147. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  148. goto fail1;
  149. }
  150. // add header
  151. if (!CmdLine_Append(cl, *exec) || !CmdLine_Append(cl, "-t") || !CmdLine_Append(cl, table) || !CmdLine_Append(cl, (remove ? "-D" : "-A")) || !CmdLine_Append(cl, chain)) {
  152. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  153. goto fail2;
  154. }
  155. // add additional arguments
  156. size_t count = NCDVal_ListCount(i->args);
  157. for (size_t j = 2; j < count; j++) {
  158. NCDValRef arg = NCDVal_ListGet(i->args, j);
  159. if (!NCDVal_IsStringNoNulls(arg)) {
  160. ModuleLog(i, BLOG_ERROR, "wrong type");
  161. goto fail2;
  162. }
  163. if (!CmdLine_Append(cl, NCDVal_StringValue(arg))) {
  164. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  165. goto fail2;
  166. }
  167. }
  168. // finish
  169. if (!CmdLine_Finish(cl)) {
  170. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  171. goto fail2;
  172. }
  173. return 1;
  174. fail2:
  175. CmdLine_Free(cl);
  176. fail1:
  177. free(*exec);
  178. fail0:
  179. return 0;
  180. }
  181. static int build_policy_cmdline (NCDModuleInst *i, const char *prog, int remove, char **exec, CmdLine *cl)
  182. {
  183. // read arguments
  184. NCDValRef table_arg;
  185. NCDValRef chain_arg;
  186. NCDValRef target_arg;
  187. NCDValRef revert_target_arg;
  188. if (!NCDVal_ListRead(i->args, 4, &table_arg, &chain_arg, &target_arg, &revert_target_arg)) {
  189. ModuleLog(i, BLOG_ERROR, "wrong arity");
  190. goto fail0;
  191. }
  192. if (!NCDVal_IsStringNoNulls(table_arg) || !NCDVal_IsStringNoNulls(chain_arg) ||
  193. !NCDVal_IsStringNoNulls(target_arg) || !NCDVal_IsStringNoNulls(revert_target_arg)
  194. ) {
  195. ModuleLog(i, BLOG_ERROR, "wrong type");
  196. goto fail0;
  197. }
  198. const char *table = NCDVal_StringValue(table_arg);
  199. const char *chain = NCDVal_StringValue(chain_arg);
  200. const char *target = NCDVal_StringValue(target_arg);
  201. const char *revert_target = NCDVal_StringValue(revert_target_arg);
  202. // find program
  203. if (!(*exec = badvpn_find_program(prog))) {
  204. ModuleLog(i, BLOG_ERROR, "failed to find program: %s", prog);
  205. goto fail0;
  206. }
  207. // start cmdline
  208. if (!CmdLine_Init(cl)) {
  209. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  210. goto fail1;
  211. }
  212. // add arguments
  213. if (!CmdLine_Append(cl, *exec) || !CmdLine_Append(cl, "-t") || !CmdLine_Append(cl, table) ||
  214. !CmdLine_Append(cl, "-P") || !CmdLine_Append(cl, chain) || !CmdLine_Append(cl, (remove ? revert_target : target))) {
  215. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  216. goto fail2;
  217. }
  218. // finish
  219. if (!CmdLine_Finish(cl)) {
  220. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  221. goto fail2;
  222. }
  223. return 1;
  224. fail2:
  225. CmdLine_Free(cl);
  226. fail1:
  227. free(*exec);
  228. fail0:
  229. return 0;
  230. }
  231. static int build_newchain_cmdline (NCDModuleInst *i, const char *prog, int remove, char **exec, CmdLine *cl)
  232. {
  233. // read arguments
  234. NCDValRef table_arg = NCDVal_NewInvalid();
  235. NCDValRef chain_arg;
  236. if (!NCDVal_ListRead(i->args, 1, &chain_arg) && !NCDVal_ListRead(i->args, 2, &table_arg, &chain_arg)) {
  237. ModuleLog(i, BLOG_ERROR, "wrong arity");
  238. goto fail0;
  239. }
  240. if ((!NCDVal_IsInvalid(table_arg) && !NCDVal_IsStringNoNulls(table_arg)) || !NCDVal_IsStringNoNulls(chain_arg)) {
  241. ModuleLog(i, BLOG_ERROR, "wrong type");
  242. goto fail0;
  243. }
  244. const char *table = (NCDVal_IsInvalid(table_arg) ? "filter" : NCDVal_StringValue(table_arg));
  245. const char *chain = NCDVal_StringValue(chain_arg);
  246. // find program
  247. if (!(*exec = badvpn_find_program(prog))) {
  248. ModuleLog(i, BLOG_ERROR, "failed to find program: %s", prog);
  249. goto fail0;
  250. }
  251. // start cmdline
  252. if (!CmdLine_Init(cl)) {
  253. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  254. goto fail1;
  255. }
  256. // add arguments
  257. if (!CmdLine_AppendMulti(cl, 5, *exec, "-t", table, (remove ? "-X" : "-N"), chain)) {
  258. ModuleLog(i, BLOG_ERROR, "CmdLine_AppendMulti failed");
  259. goto fail2;
  260. }
  261. // finish
  262. if (!CmdLine_Finish(cl)) {
  263. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  264. goto fail2;
  265. }
  266. return 1;
  267. fail2:
  268. CmdLine_Free(cl);
  269. fail1:
  270. free(*exec);
  271. fail0:
  272. return 0;
  273. }
  274. static int build_iptables_append_cmdline (NCDModuleInst *i, int remove, char **exec, CmdLine *cl)
  275. {
  276. return build_append_cmdline(i, "iptables", remove, exec, cl);
  277. }
  278. static int build_iptables_policy_cmdline (NCDModuleInst *i, int remove, char **exec, CmdLine *cl)
  279. {
  280. return build_policy_cmdline(i, "iptables", remove, exec, cl);
  281. }
  282. static int build_iptables_newchain_cmdline (NCDModuleInst *i, int remove, char **exec, CmdLine *cl)
  283. {
  284. return build_newchain_cmdline(i, "iptables", remove, exec, cl);
  285. }
  286. static int build_ebtables_append_cmdline (NCDModuleInst *i, int remove, char **exec, CmdLine *cl)
  287. {
  288. return build_append_cmdline(i, "ebtables", remove, exec, cl);
  289. }
  290. static int build_ebtables_policy_cmdline (NCDModuleInst *i, int remove, char **exec, CmdLine *cl)
  291. {
  292. return build_policy_cmdline(i, "ebtables", remove, exec, cl);
  293. }
  294. static int build_ebtables_newchain_cmdline (NCDModuleInst *i, int remove, char **exec, CmdLine *cl)
  295. {
  296. return build_newchain_cmdline(i, "ebtables", remove, exec, cl);
  297. }
  298. static void lock_job_handler (struct lock_instance *o)
  299. {
  300. ASSERT(o->state == LOCK_STATE_LOCKING || o->state == LOCK_STATE_RELOCKING)
  301. if (o->state == LOCK_STATE_LOCKING) {
  302. ASSERT(!o->unlock)
  303. // up
  304. NCDModuleInst_Backend_Up(o->i);
  305. // set state locked
  306. o->state = LOCK_STATE_LOCKED;
  307. }
  308. else if (o->state == LOCK_STATE_RELOCKING) {
  309. ASSERT(o->unlock)
  310. ASSERT(o->unlock->lock == o)
  311. // die unlock
  312. unlock_free(o->unlock);
  313. o->unlock = NULL;
  314. // set state locked
  315. o->state = LOCK_STATE_LOCKED;
  316. }
  317. }
  318. static int func_globalinit (struct NCDModuleInitParams params)
  319. {
  320. // init iptables lock
  321. BEventLock_Init(&iptables_lock, BReactor_PendingGroup(params.reactor));
  322. return 1;
  323. }
  324. static void func_globalfree (void)
  325. {
  326. // free iptables lock
  327. BEventLock_Free(&iptables_lock);
  328. }
  329. static void func_new (void *vo, NCDModuleInst *i, command_template_build_cmdline build_cmdline)
  330. {
  331. struct instance *o = vo;
  332. o->i = i;
  333. command_template_new(&o->cti, i, build_cmdline, template_free_func, o, BLOG_CURRENT_CHANNEL, &iptables_lock);
  334. }
  335. void template_free_func (void *vo, int is_error)
  336. {
  337. struct instance *o = vo;
  338. if (is_error) {
  339. NCDModuleInst_Backend_SetError(o->i);
  340. }
  341. NCDModuleInst_Backend_Dead(o->i);
  342. }
  343. static void append_iptables_func_new (void *vo, NCDModuleInst *i)
  344. {
  345. func_new(vo, i, build_iptables_append_cmdline);
  346. }
  347. static void policy_iptables_func_new (void *vo, NCDModuleInst *i)
  348. {
  349. func_new(vo, i, build_iptables_policy_cmdline);
  350. }
  351. static void newchain_iptables_func_new (void *vo, NCDModuleInst *i)
  352. {
  353. func_new(vo, i, build_iptables_newchain_cmdline);
  354. }
  355. static void append_ebtables_func_new (void *vo, NCDModuleInst *i)
  356. {
  357. func_new(vo, i, build_ebtables_append_cmdline);
  358. }
  359. static void policy_ebtables_func_new (void *vo, NCDModuleInst *i)
  360. {
  361. func_new(vo, i, build_ebtables_policy_cmdline);
  362. }
  363. static void newchain_ebtables_func_new (void *vo, NCDModuleInst *i)
  364. {
  365. func_new(vo, i, build_ebtables_newchain_cmdline);
  366. }
  367. static void func_die (void *vo)
  368. {
  369. struct instance *o = vo;
  370. command_template_die(&o->cti);
  371. }
  372. static void lock_func_new (void *vo, NCDModuleInst *i)
  373. {
  374. struct lock_instance *o = vo;
  375. o->i = i;
  376. // init lock job
  377. BEventLockJob_Init(&o->lock_job, &iptables_lock, (BEventLock_handler)lock_job_handler, o);
  378. BEventLockJob_Wait(&o->lock_job);
  379. // set no unlock
  380. o->unlock = NULL;
  381. // set state locking
  382. o->state = LOCK_STATE_LOCKING;
  383. }
  384. static void lock_func_die (void *vo)
  385. {
  386. struct lock_instance *o = vo;
  387. if (o->state == LOCK_STATE_UNLOCKED) {
  388. ASSERT(o->unlock)
  389. ASSERT(o->unlock->lock == o)
  390. o->unlock->lock = NULL;
  391. }
  392. else if (o->state == LOCK_STATE_RELOCKING) {
  393. ASSERT(o->unlock)
  394. ASSERT(o->unlock->lock == o)
  395. unlock_free(o->unlock);
  396. }
  397. else {
  398. ASSERT(!o->unlock)
  399. }
  400. // free lock job
  401. BEventLockJob_Free(&o->lock_job);
  402. // dead
  403. NCDModuleInst_Backend_Dead(o->i);
  404. }
  405. static void unlock_func_new (void *vo, NCDModuleInst *i)
  406. {
  407. struct unlock_instance *o = vo;
  408. o->i = i;
  409. // get lock lock
  410. struct lock_instance *lock = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  411. // make sure lock doesn't already have an unlock
  412. if (lock->unlock) {
  413. BLog(BLOG_ERROR, "lock already has an unlock");
  414. goto fail0;
  415. }
  416. // make sure lock is locked
  417. if (lock->state != LOCK_STATE_LOCKED) {
  418. BLog(BLOG_ERROR, "lock is not locked");
  419. goto fail0;
  420. }
  421. // set lock
  422. o->lock = lock;
  423. // set unlock in lock
  424. lock->unlock = o;
  425. // up
  426. NCDModuleInst_Backend_Up(o->i);
  427. // release lock
  428. BEventLockJob_Release(&lock->lock_job);
  429. // set lock state unlocked
  430. lock->state = LOCK_STATE_UNLOCKED;
  431. return;
  432. fail0:
  433. NCDModuleInst_Backend_SetError(i);
  434. NCDModuleInst_Backend_Dead(i);
  435. }
  436. static void unlock_func_die (void *vo)
  437. {
  438. struct unlock_instance *o = vo;
  439. // if lock is gone, die right away
  440. if (!o->lock) {
  441. unlock_free(o);
  442. return;
  443. }
  444. ASSERT(o->lock->unlock == o)
  445. ASSERT(o->lock->state == LOCK_STATE_UNLOCKED)
  446. // wait lock
  447. BEventLockJob_Wait(&o->lock->lock_job);
  448. // set lock state relocking
  449. o->lock->state = LOCK_STATE_RELOCKING;
  450. }
  451. static void unlock_free (struct unlock_instance *o)
  452. {
  453. NCDModuleInst_Backend_Dead(o->i);
  454. }
  455. static const struct NCDModule modules[] = {
  456. {
  457. .type = "net.iptables.append",
  458. .func_new2 = append_iptables_func_new,
  459. .func_die = func_die,
  460. .alloc_size = sizeof(struct instance)
  461. }, {
  462. .type = "net.iptables.policy",
  463. .func_new2 = policy_iptables_func_new,
  464. .func_die = func_die,
  465. .alloc_size = sizeof(struct instance)
  466. }, {
  467. .type = "net.iptables.newchain",
  468. .func_new2 = newchain_iptables_func_new,
  469. .func_die = func_die,
  470. .alloc_size = sizeof(struct instance)
  471. }, {
  472. .type = "net.ebtables.append",
  473. .func_new2 = append_ebtables_func_new,
  474. .func_die = func_die,
  475. .alloc_size = sizeof(struct instance)
  476. }, {
  477. .type = "net.ebtables.policy",
  478. .func_new2 = policy_ebtables_func_new,
  479. .func_die = func_die,
  480. .alloc_size = sizeof(struct instance)
  481. }, {
  482. .type = "net.ebtables.newchain",
  483. .func_new2 = newchain_ebtables_func_new,
  484. .func_die = func_die,
  485. .alloc_size = sizeof(struct instance)
  486. }, {
  487. .type = "net.iptables.lock",
  488. .func_new2 = lock_func_new,
  489. .func_die = lock_func_die,
  490. .alloc_size = sizeof(struct lock_instance)
  491. }, {
  492. .type = "net.iptables.lock::unlock",
  493. .func_new2 = unlock_func_new,
  494. .func_die = unlock_func_die,
  495. .alloc_size = sizeof(struct unlock_instance)
  496. }, {
  497. .type = NULL
  498. }
  499. };
  500. const struct NCDModuleGroup ncdmodule_net_iptables = {
  501. .modules = modules,
  502. .func_globalinit = func_globalinit,
  503. .func_globalfree = func_globalfree
  504. };