defaults.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * firewall3 - 3rd OpenWrt UCI firewall implementation
  3. *
  4. * Copyright (C) 2013 Jo-Philipp Wich <jo@mein.io>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include "defaults.h"
  19. #define C(f, tbl, def, fmt) \
  20. { FW3_FAMILY_##f, FW3_TABLE_##tbl, FW3_FLAG_##def, fmt }
  21. static const struct fw3_chain_spec default_chains[] = {
  22. C(ANY, FILTER, UNSPEC, "reject"),
  23. C(ANY, FILTER, CUSTOM_CHAINS, "input_rule"),
  24. C(ANY, FILTER, CUSTOM_CHAINS, "output_rule"),
  25. C(ANY, FILTER, CUSTOM_CHAINS, "forwarding_rule"),
  26. C(ANY, FILTER, SYN_FLOOD, "syn_flood"),
  27. C(V4, NAT, CUSTOM_CHAINS, "prerouting_rule"),
  28. C(V4, NAT, CUSTOM_CHAINS, "postrouting_rule"),
  29. { }
  30. };
  31. const struct fw3_option fw3_flag_opts[] = {
  32. FW3_OPT("input", target, defaults, policy_input),
  33. FW3_OPT("forward", target, defaults, policy_forward),
  34. FW3_OPT("output", target, defaults, policy_output),
  35. FW3_OPT("drop_invalid", bool, defaults, drop_invalid),
  36. FW3_OPT("tcp_reject_code", reject_code, defaults, tcp_reject_code),
  37. FW3_OPT("any_reject_code", reject_code, defaults, any_reject_code),
  38. FW3_OPT("syn_flood", bool, defaults, syn_flood),
  39. FW3_OPT("synflood_protect", bool, defaults, syn_flood),
  40. FW3_OPT("synflood_rate", limit, defaults, syn_flood_rate),
  41. FW3_OPT("synflood_burst", int, defaults, syn_flood_rate.burst),
  42. FW3_OPT("tcp_syncookies", bool, defaults, tcp_syncookies),
  43. FW3_OPT("tcp_ecn", int, defaults, tcp_ecn),
  44. FW3_OPT("tcp_window_scaling", bool, defaults, tcp_window_scaling),
  45. FW3_OPT("accept_redirects", bool, defaults, accept_redirects),
  46. FW3_OPT("accept_source_route", bool, defaults, accept_source_route),
  47. FW3_OPT("auto_helper", bool, defaults, auto_helper),
  48. FW3_OPT("custom_chains", bool, defaults, custom_chains),
  49. FW3_OPT("disable_ipv6", bool, defaults, disable_ipv6),
  50. FW3_OPT("flow_offloading", bool, defaults, flow_offloading),
  51. FW3_OPT("flow_offloading_hw", bool, defaults, flow_offloading_hw),
  52. FW3_OPT("__flags_v4", int, defaults, flags[0]),
  53. FW3_OPT("__flags_v6", int, defaults, flags[1]),
  54. { }
  55. };
  56. static void
  57. check_policy(struct uci_element *e, enum fw3_flag *pol, const char *name)
  58. {
  59. if (*pol == FW3_FLAG_UNSPEC)
  60. {
  61. warn_elem(e, "has no %s policy specified, defaulting to DROP", name);
  62. *pol = FW3_FLAG_DROP;
  63. }
  64. else if (*pol > FW3_FLAG_DROP)
  65. {
  66. warn_elem(e, "has invalid %s policy, defaulting to DROP", name);
  67. *pol = FW3_FLAG_DROP;
  68. }
  69. }
  70. static void
  71. check_kmod(struct uci_element *e, bool *module, const char *name)
  72. {
  73. FILE *f;
  74. char buf[128];
  75. if (!*module)
  76. return;
  77. snprintf(buf, sizeof(buf), "/sys/module/%s/refcnt", name);
  78. f = fopen(buf, "r");
  79. if (f)
  80. {
  81. fclose(f);
  82. return;
  83. }
  84. warn_elem(e, "requires not available kernel module %s, disabling", name);
  85. *module = false;
  86. }
  87. static void
  88. check_any_reject_code(struct uci_element *e, enum fw3_reject_code *any_reject_code)
  89. {
  90. if (*any_reject_code == FW3_REJECT_CODE_TCP_RESET) {
  91. warn_elem(e, "tcp-reset not valid for any_reject_code, defaulting to port-unreach");
  92. *any_reject_code = FW3_REJECT_CODE_PORT_UNREACH;
  93. }
  94. }
  95. static const char*
  96. get_reject_code(enum fw3_family family, enum fw3_reject_code reject_code)
  97. {
  98. switch (reject_code) {
  99. case FW3_REJECT_CODE_TCP_RESET:
  100. return "tcp-reset";
  101. case FW3_REJECT_CODE_PORT_UNREACH:
  102. return "port-unreach";
  103. case FW3_REJECT_CODE_ADM_PROHIBITED:
  104. return family == FW3_FAMILY_V6 ? "adm-prohibited": "admin-prohib";
  105. default:
  106. return "unknown";
  107. }
  108. }
  109. void
  110. fw3_load_defaults(struct fw3_state *state, struct uci_package *p)
  111. {
  112. struct uci_section *s;
  113. struct uci_element *e;
  114. struct fw3_defaults *defs = &state->defaults;
  115. bool seen = false;
  116. defs->tcp_reject_code = FW3_REJECT_CODE_TCP_RESET;
  117. defs->any_reject_code = FW3_REJECT_CODE_PORT_UNREACH;
  118. defs->syn_flood_rate.rate = 25;
  119. defs->syn_flood_rate.burst = 50;
  120. defs->tcp_syncookies = true;
  121. defs->tcp_window_scaling = true;
  122. defs->custom_chains = true;
  123. defs->auto_helper = true;
  124. uci_foreach_element(&p->sections, e)
  125. {
  126. s = uci_to_section(e);
  127. if (strcmp(s->type, "defaults"))
  128. continue;
  129. if (seen)
  130. {
  131. warn_elem(e, "ignoring duplicate section");
  132. continue;
  133. }
  134. if(!fw3_parse_options(&state->defaults, fw3_flag_opts, s))
  135. warn_elem(e, "has invalid options");
  136. check_policy(e, &defs->policy_input, "input");
  137. check_policy(e, &defs->policy_output, "output");
  138. check_policy(e, &defs->policy_forward, "forward");
  139. check_any_reject_code(e, &defs->any_reject_code);
  140. check_kmod(e, &defs->flow_offloading, "xt_FLOWOFFLOAD");
  141. }
  142. }
  143. void
  144. fw3_print_default_chains(struct fw3_ipt_handle *handle, struct fw3_state *state,
  145. bool reload)
  146. {
  147. struct fw3_defaults *defs = &state->defaults;
  148. const struct fw3_chain_spec *c;
  149. #define policy(t) \
  150. ((t == FW3_FLAG_REJECT) ? FW3_FLAG_DROP : t)
  151. if (handle->family == FW3_FAMILY_V6 && defs->disable_ipv6)
  152. return;
  153. if (handle->table == FW3_TABLE_FILTER)
  154. {
  155. fw3_ipt_set_policy(handle, "INPUT", policy(defs->policy_input));
  156. fw3_ipt_set_policy(handle, "OUTPUT", policy(defs->policy_output));
  157. fw3_ipt_set_policy(handle, "FORWARD", policy(defs->policy_forward));
  158. }
  159. if (defs->custom_chains)
  160. set(defs->flags, handle->family, FW3_FLAG_CUSTOM_CHAINS);
  161. if (defs->syn_flood)
  162. set(defs->flags, handle->family, FW3_FLAG_SYN_FLOOD);
  163. for (c = default_chains; c->format; c++)
  164. {
  165. /* don't touch user chains on selective stop */
  166. if (reload && c->flag == FW3_FLAG_CUSTOM_CHAINS)
  167. continue;
  168. if (!fw3_is_family(c, handle->family))
  169. continue;
  170. if (c->table != handle->table)
  171. continue;
  172. if (c->flag &&
  173. !fw3_hasbit(defs->flags[handle->family == FW3_FAMILY_V6], c->flag))
  174. continue;
  175. fw3_ipt_create_chain(handle, c->format);
  176. }
  177. set(defs->flags, handle->family, handle->table);
  178. }
  179. void
  180. fw3_print_default_head_rules(struct fw3_ipt_handle *handle,
  181. struct fw3_state *state, bool reload)
  182. {
  183. int i;
  184. struct fw3_defaults *defs = &state->defaults;
  185. struct fw3_device lodev = { .set = true };
  186. struct fw3_protocol tcp = { .protocol = 6 };
  187. struct fw3_ipt_rule *r;
  188. const char *chains[] = {
  189. "INPUT", "input",
  190. "OUTPUT", "output",
  191. "FORWARD", "forwarding",
  192. };
  193. switch (handle->table)
  194. {
  195. case FW3_TABLE_FILTER:
  196. sprintf(lodev.name, "lo");
  197. r = fw3_ipt_rule_create(handle, NULL, &lodev, NULL, NULL, NULL);
  198. fw3_ipt_rule_target(r, "ACCEPT");
  199. fw3_ipt_rule_append(r, "INPUT");
  200. r = fw3_ipt_rule_create(handle, NULL, NULL, &lodev, NULL, NULL);
  201. fw3_ipt_rule_target(r, "ACCEPT");
  202. fw3_ipt_rule_append(r, "OUTPUT");
  203. if (defs->custom_chains)
  204. {
  205. for (i = 0; i < ARRAY_SIZE(chains); i += 2)
  206. {
  207. r = fw3_ipt_rule_new(handle);
  208. fw3_ipt_rule_comment(r, "Custom %s rule chain", chains[i+1]);
  209. fw3_ipt_rule_target(r, "%s_rule", chains[i+1]);
  210. fw3_ipt_rule_append(r, chains[i]);
  211. }
  212. }
  213. if (defs->flow_offloading)
  214. {
  215. r = fw3_ipt_rule_new(handle);
  216. fw3_ipt_rule_comment(r, "Traffic offloading");
  217. fw3_ipt_rule_extra(r, "-m conntrack --ctstate RELATED,ESTABLISHED");
  218. fw3_ipt_rule_target(r, "FLOWOFFLOAD");
  219. if (defs->flow_offloading_hw)
  220. fw3_ipt_rule_addarg(r, false, "--hw", NULL);
  221. fw3_ipt_rule_append(r, "FORWARD");
  222. }
  223. for (i = 0; i < ARRAY_SIZE(chains); i += 2)
  224. {
  225. r = fw3_ipt_rule_new(handle);
  226. fw3_ipt_rule_extra(r, "-m conntrack --ctstate RELATED,ESTABLISHED");
  227. fw3_ipt_rule_target(r, "ACCEPT");
  228. fw3_ipt_rule_append(r, chains[i]);
  229. if (defs->drop_invalid)
  230. {
  231. r = fw3_ipt_rule_new(handle);
  232. fw3_ipt_rule_extra(r, "-m conntrack --ctstate INVALID");
  233. fw3_ipt_rule_target(r, "DROP");
  234. fw3_ipt_rule_append(r, chains[i]);
  235. }
  236. }
  237. if (defs->syn_flood)
  238. {
  239. r = fw3_ipt_rule_create(handle, &tcp, NULL, NULL, NULL, NULL);
  240. fw3_ipt_rule_extra(r, "--syn");
  241. fw3_ipt_rule_limit(r, &defs->syn_flood_rate);
  242. fw3_ipt_rule_target(r, "RETURN");
  243. fw3_ipt_rule_append(r, "syn_flood");
  244. r = fw3_ipt_rule_new(handle);
  245. fw3_ipt_rule_target(r, "DROP");
  246. fw3_ipt_rule_append(r, "syn_flood");
  247. r = fw3_ipt_rule_create(handle, &tcp, NULL, NULL, NULL, NULL);
  248. fw3_ipt_rule_extra(r, "--syn");
  249. fw3_ipt_rule_target(r, "syn_flood");
  250. fw3_ipt_rule_append(r, "INPUT");
  251. }
  252. r = fw3_ipt_rule_create(handle, &tcp, NULL, NULL, NULL, NULL);
  253. fw3_ipt_rule_target(r, "REJECT");
  254. fw3_ipt_rule_addarg(r, false, "--reject-with", get_reject_code(handle->family, defs->tcp_reject_code));
  255. fw3_ipt_rule_append(r, "reject");
  256. r = fw3_ipt_rule_new(handle);
  257. fw3_ipt_rule_target(r, "REJECT");
  258. fw3_ipt_rule_addarg(r, false, "--reject-with", get_reject_code(handle->family, defs->any_reject_code));
  259. fw3_ipt_rule_append(r, "reject");
  260. break;
  261. case FW3_TABLE_NAT:
  262. if (defs->custom_chains)
  263. {
  264. r = fw3_ipt_rule_new(handle);
  265. fw3_ipt_rule_comment(r, "Custom prerouting rule chain");
  266. fw3_ipt_rule_target(r, "prerouting_rule");
  267. fw3_ipt_rule_append(r, "PREROUTING");
  268. r = fw3_ipt_rule_new(handle);
  269. fw3_ipt_rule_comment(r, "Custom postrouting rule chain");
  270. fw3_ipt_rule_target(r, "postrouting_rule");
  271. fw3_ipt_rule_append(r, "POSTROUTING");
  272. }
  273. break;
  274. default:
  275. break;
  276. }
  277. }
  278. void
  279. fw3_print_default_tail_rules(struct fw3_ipt_handle *handle,
  280. struct fw3_state *state, bool reload)
  281. {
  282. struct fw3_defaults *defs = &state->defaults;
  283. struct fw3_ipt_rule *r;
  284. if (handle->table != FW3_TABLE_FILTER)
  285. return;
  286. if (defs->policy_input == FW3_FLAG_REJECT)
  287. {
  288. r = fw3_ipt_rule_new(handle);
  289. if (!r)
  290. return;
  291. fw3_ipt_rule_target(r, "reject");
  292. fw3_ipt_rule_append(r, "INPUT");
  293. }
  294. if (defs->policy_output == FW3_FLAG_REJECT)
  295. {
  296. r = fw3_ipt_rule_new(handle);
  297. if (!r)
  298. return;
  299. fw3_ipt_rule_target(r, "reject");
  300. fw3_ipt_rule_append(r, "OUTPUT");
  301. }
  302. if (defs->policy_forward == FW3_FLAG_REJECT)
  303. {
  304. r = fw3_ipt_rule_new(handle);
  305. if (!r)
  306. return;
  307. fw3_ipt_rule_target(r, "reject");
  308. fw3_ipt_rule_append(r, "FORWARD");
  309. }
  310. }
  311. static void
  312. set_default(const char *name, int set)
  313. {
  314. FILE *f;
  315. char path[sizeof("/proc/sys/net/ipv4/tcp_window_scaling\0")];
  316. snprintf(path, sizeof(path), "/proc/sys/net/ipv4/tcp_%s", name);
  317. info(" * Set tcp_%s to %s", name, set ? "on" : "off", name);
  318. if (!(f = fopen(path, "w")))
  319. {
  320. info(" ! Unable to write value: %s", strerror(errno));
  321. return;
  322. }
  323. fprintf(f, "%u\n", set);
  324. fclose(f);
  325. }
  326. void
  327. fw3_set_defaults(struct fw3_state *state)
  328. {
  329. set_default("ecn", state->defaults.tcp_ecn);
  330. set_default("syncookies", state->defaults.tcp_syncookies);
  331. set_default("window_scaling", state->defaults.tcp_window_scaling);
  332. }
  333. void
  334. fw3_flush_rules(struct fw3_ipt_handle *handle, struct fw3_state *state,
  335. bool reload)
  336. {
  337. enum fw3_flag policy = reload ? FW3_FLAG_DROP : FW3_FLAG_ACCEPT;
  338. struct fw3_defaults *defs = &state->defaults;
  339. const struct fw3_chain_spec *c;
  340. if (!has(defs->flags, handle->family, handle->table))
  341. return;
  342. if (handle->table == FW3_TABLE_FILTER)
  343. {
  344. fw3_ipt_set_policy(handle, "INPUT", policy);
  345. fw3_ipt_set_policy(handle, "OUTPUT", policy);
  346. fw3_ipt_set_policy(handle, "FORWARD", policy);
  347. }
  348. fw3_ipt_delete_id_rules(handle, "INPUT");
  349. fw3_ipt_delete_id_rules(handle, "OUTPUT");
  350. fw3_ipt_delete_id_rules(handle, "FORWARD");
  351. fw3_ipt_delete_id_rules(handle, "PREROUTING");
  352. fw3_ipt_delete_id_rules(handle, "POSTROUTING");
  353. for (c = default_chains; c->format; c++)
  354. {
  355. /* don't touch user chains on selective stop */
  356. if (reload && c->flag == FW3_FLAG_CUSTOM_CHAINS)
  357. continue;
  358. if (!fw3_is_family(c, handle->family))
  359. continue;
  360. if (c->table != handle->table)
  361. continue;
  362. if (c->flag && !has(defs->flags, handle->family, c->flag))
  363. continue;
  364. fw3_ipt_flush_chain(handle, c->format);
  365. /* keep certain basic chains that do not depend on any settings to
  366. avoid purging unrelated user rules pointing to them */
  367. if (reload && !c->flag)
  368. continue;
  369. fw3_ipt_delete_chain(handle, c->format);
  370. }
  371. del(defs->flags, handle->family, handle->table);
  372. }
  373. void
  374. fw3_flush_all(struct fw3_ipt_handle *handle)
  375. {
  376. if (handle->table == FW3_TABLE_FILTER)
  377. {
  378. fw3_ipt_set_policy(handle, "INPUT", FW3_FLAG_ACCEPT);
  379. fw3_ipt_set_policy(handle, "OUTPUT", FW3_FLAG_ACCEPT);
  380. fw3_ipt_set_policy(handle, "FORWARD", FW3_FLAG_ACCEPT);
  381. }
  382. fw3_ipt_flush(handle);
  383. }