net_iptables.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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. 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 (NCDModuleInst *i, command_template_build_cmdline build_cmdline)
  330. {
  331. // allocate instance
  332. struct instance *o = malloc(sizeof(*o));
  333. if (!o) {
  334. BLog(BLOG_ERROR, "malloc failed");
  335. goto fail0;
  336. }
  337. o->i = i;
  338. NCDModuleInst_Backend_SetUser(i, o);
  339. command_template_new(&o->cti, i, build_cmdline, template_free_func, o, BLOG_CURRENT_CHANNEL, &iptables_lock);
  340. return;
  341. fail0:
  342. NCDModuleInst_Backend_SetError(i);
  343. NCDModuleInst_Backend_Dead(i);
  344. }
  345. void template_free_func (void *vo, int is_error)
  346. {
  347. struct instance *o = vo;
  348. NCDModuleInst *i = o->i;
  349. // free instance
  350. free(o);
  351. if (is_error) {
  352. NCDModuleInst_Backend_SetError(i);
  353. }
  354. NCDModuleInst_Backend_Dead(i);
  355. }
  356. static void append_iptables_func_new (NCDModuleInst *i)
  357. {
  358. func_new(i, build_iptables_append_cmdline);
  359. }
  360. static void policy_iptables_func_new (NCDModuleInst *i)
  361. {
  362. func_new(i, build_iptables_policy_cmdline);
  363. }
  364. static void newchain_iptables_func_new (NCDModuleInst *i)
  365. {
  366. func_new(i, build_iptables_newchain_cmdline);
  367. }
  368. static void append_ebtables_func_new (NCDModuleInst *i)
  369. {
  370. func_new(i, build_ebtables_append_cmdline);
  371. }
  372. static void policy_ebtables_func_new (NCDModuleInst *i)
  373. {
  374. func_new(i, build_ebtables_policy_cmdline);
  375. }
  376. static void newchain_ebtables_func_new (NCDModuleInst *i)
  377. {
  378. func_new(i, build_ebtables_newchain_cmdline);
  379. }
  380. static void func_die (void *vo)
  381. {
  382. struct instance *o = vo;
  383. command_template_die(&o->cti);
  384. }
  385. static void lock_func_new (NCDModuleInst *i)
  386. {
  387. // allocate instance
  388. struct lock_instance *o = malloc(sizeof(*o));
  389. if (!o) {
  390. BLog(BLOG_ERROR, "malloc failed");
  391. goto fail0;
  392. }
  393. NCDModuleInst_Backend_SetUser(i, o);
  394. // init arguments
  395. o->i = i;
  396. // init lock job
  397. BEventLockJob_Init(&o->lock_job, &iptables_lock, (BEventLock_handler)lock_job_handler, o);
  398. BEventLockJob_Wait(&o->lock_job);
  399. // set no unlock
  400. o->unlock = NULL;
  401. // set state locking
  402. o->state = LOCK_STATE_LOCKING;
  403. return;
  404. fail0:
  405. NCDModuleInst_Backend_SetError(i);
  406. NCDModuleInst_Backend_Dead(i);
  407. }
  408. static void lock_func_die (void *vo)
  409. {
  410. struct lock_instance *o = vo;
  411. NCDModuleInst *i = o->i;
  412. if (o->state == LOCK_STATE_UNLOCKED) {
  413. ASSERT(o->unlock)
  414. ASSERT(o->unlock->lock == o)
  415. o->unlock->lock = NULL;
  416. }
  417. else if (o->state == LOCK_STATE_RELOCKING) {
  418. ASSERT(o->unlock)
  419. ASSERT(o->unlock->lock == o)
  420. unlock_free(o->unlock);
  421. }
  422. else {
  423. ASSERT(!o->unlock)
  424. }
  425. // free lock job
  426. BEventLockJob_Free(&o->lock_job);
  427. // free instance
  428. free(o);
  429. // dead
  430. NCDModuleInst_Backend_Dead(i);
  431. }
  432. static void unlock_func_new (NCDModuleInst *i)
  433. {
  434. // allocate instance
  435. struct unlock_instance *o = malloc(sizeof(*o));
  436. if (!o) {
  437. BLog(BLOG_ERROR, "malloc failed");
  438. goto fail0;
  439. }
  440. NCDModuleInst_Backend_SetUser(i, o);
  441. // init arguments
  442. o->i = i;
  443. // get lock lock
  444. struct lock_instance *lock = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  445. // make sure lock doesn't already have an unlock
  446. if (lock->unlock) {
  447. BLog(BLOG_ERROR, "lock already has an unlock");
  448. goto fail1;
  449. }
  450. // make sure lock is locked
  451. if (lock->state != LOCK_STATE_LOCKED) {
  452. BLog(BLOG_ERROR, "lock is not locked");
  453. goto fail1;
  454. }
  455. // set lock
  456. o->lock = lock;
  457. // set unlock in lock
  458. lock->unlock = o;
  459. // up
  460. NCDModuleInst_Backend_Up(o->i);
  461. // release lock
  462. BEventLockJob_Release(&lock->lock_job);
  463. // set lock state unlocked
  464. lock->state = LOCK_STATE_UNLOCKED;
  465. return;
  466. fail1:
  467. free(o);
  468. fail0:
  469. NCDModuleInst_Backend_SetError(i);
  470. NCDModuleInst_Backend_Dead(i);
  471. }
  472. static void unlock_func_die (void *vo)
  473. {
  474. struct unlock_instance *o = vo;
  475. // if lock is gone, die right away
  476. if (!o->lock) {
  477. unlock_free(o);
  478. return;
  479. }
  480. ASSERT(o->lock->unlock == o)
  481. ASSERT(o->lock->state == LOCK_STATE_UNLOCKED)
  482. // wait lock
  483. BEventLockJob_Wait(&o->lock->lock_job);
  484. // set lock state relocking
  485. o->lock->state = LOCK_STATE_RELOCKING;
  486. }
  487. static void unlock_free (struct unlock_instance *o)
  488. {
  489. NCDModuleInst *i = o->i;
  490. // free instance
  491. free(o);
  492. NCDModuleInst_Backend_Dead(i);
  493. }
  494. static const struct NCDModule modules[] = {
  495. {
  496. .type = "net.iptables.append",
  497. .func_new = append_iptables_func_new,
  498. .func_die = func_die
  499. }, {
  500. .type = "net.iptables.policy",
  501. .func_new = policy_iptables_func_new,
  502. .func_die = func_die
  503. }, {
  504. .type = "net.iptables.newchain",
  505. .func_new = newchain_iptables_func_new,
  506. .func_die = func_die
  507. }, {
  508. .type = "net.ebtables.append",
  509. .func_new = append_ebtables_func_new,
  510. .func_die = func_die
  511. }, {
  512. .type = "net.ebtables.policy",
  513. .func_new = policy_ebtables_func_new,
  514. .func_die = func_die
  515. }, {
  516. .type = "net.ebtables.newchain",
  517. .func_new = newchain_ebtables_func_new,
  518. .func_die = func_die
  519. }, {
  520. .type = "net.iptables.lock",
  521. .func_new = lock_func_new,
  522. .func_die = lock_func_die
  523. }, {
  524. .type = "net.iptables.lock::unlock",
  525. .func_new = unlock_func_new,
  526. .func_die = unlock_func_die
  527. }, {
  528. .type = NULL
  529. }
  530. };
  531. const struct NCDModuleGroup ncdmodule_net_iptables = {
  532. .modules = modules,
  533. .func_globalinit = func_globalinit,
  534. .func_globalfree = func_globalfree
  535. };