net_iptables.c 15 KB

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