defaults.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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_target(struct uci_element *e, bool *available, const char *target, const bool ipv6)
  72. {
  73. const bool b = fw3_has_target(ipv6, target);
  74. if (!b)
  75. {
  76. warn_elem(e, "requires unavailable target extension %s, disabling", target);
  77. *available = false;
  78. }
  79. }
  80. static void
  81. check_any_reject_code(struct uci_element *e, enum fw3_reject_code *any_reject_code)
  82. {
  83. if (*any_reject_code == FW3_REJECT_CODE_TCP_RESET) {
  84. warn_elem(e, "tcp-reset not valid for any_reject_code, defaulting to port-unreach");
  85. *any_reject_code = FW3_REJECT_CODE_PORT_UNREACH;
  86. }
  87. }
  88. static const char*
  89. get_reject_code(enum fw3_family family, enum fw3_reject_code reject_code)
  90. {
  91. switch (reject_code) {
  92. case FW3_REJECT_CODE_TCP_RESET:
  93. return "tcp-reset";
  94. case FW3_REJECT_CODE_PORT_UNREACH:
  95. return "port-unreach";
  96. case FW3_REJECT_CODE_ADM_PROHIBITED:
  97. return family == FW3_FAMILY_V6 ? "adm-prohibited": "admin-prohib";
  98. default:
  99. return "unknown";
  100. }
  101. }
  102. void
  103. fw3_load_defaults(struct fw3_state *state, struct uci_package *p)
  104. {
  105. struct uci_section *s;
  106. struct uci_element *e;
  107. struct fw3_defaults *defs = &state->defaults;
  108. bool seen = false;
  109. defs->tcp_reject_code = FW3_REJECT_CODE_TCP_RESET;
  110. defs->any_reject_code = FW3_REJECT_CODE_PORT_UNREACH;
  111. defs->syn_flood_rate.rate = 25;
  112. defs->syn_flood_rate.burst = 50;
  113. defs->tcp_syncookies = true;
  114. defs->tcp_window_scaling = true;
  115. defs->custom_chains = true;
  116. defs->auto_helper = true;
  117. uci_foreach_element(&p->sections, e)
  118. {
  119. s = uci_to_section(e);
  120. if (strcmp(s->type, "defaults"))
  121. continue;
  122. if (seen)
  123. {
  124. warn_elem(e, "ignoring duplicate section");
  125. continue;
  126. }
  127. seen = true;
  128. if(!fw3_parse_options(&state->defaults, fw3_flag_opts, s))
  129. warn_elem(e, "has invalid options");
  130. check_policy(e, &defs->policy_input, "input");
  131. check_policy(e, &defs->policy_output, "output");
  132. check_policy(e, &defs->policy_forward, "forward");
  133. check_any_reject_code(e, &defs->any_reject_code);
  134. /* exists in both ipv4 and ipv6, if at all, so only check ipv4 */
  135. check_target(e, &defs->flow_offloading, "FLOWOFFLOAD", false);
  136. }
  137. }
  138. void
  139. fw3_print_default_chains(struct fw3_ipt_handle *handle, struct fw3_state *state,
  140. bool reload)
  141. {
  142. struct fw3_defaults *defs = &state->defaults;
  143. const struct fw3_chain_spec *c;
  144. #define policy(t) \
  145. ((t == FW3_FLAG_REJECT) ? FW3_FLAG_DROP : t)
  146. if (handle->family == FW3_FAMILY_V6 && defs->disable_ipv6)
  147. return;
  148. if (handle->table == FW3_TABLE_FILTER)
  149. {
  150. fw3_ipt_set_policy(handle, "INPUT", policy(defs->policy_input));
  151. fw3_ipt_set_policy(handle, "OUTPUT", policy(defs->policy_output));
  152. fw3_ipt_set_policy(handle, "FORWARD", policy(defs->policy_forward));
  153. }
  154. if (defs->custom_chains)
  155. set(defs->flags, handle->family, FW3_FLAG_CUSTOM_CHAINS);
  156. if (defs->syn_flood)
  157. set(defs->flags, handle->family, FW3_FLAG_SYN_FLOOD);
  158. for (c = default_chains; c->format; c++)
  159. {
  160. if (!fw3_is_family(c, handle->family))
  161. continue;
  162. if (c->table != handle->table)
  163. continue;
  164. if (c->flag &&
  165. !fw3_hasbit(defs->flags[handle->family == FW3_FAMILY_V6], c->flag))
  166. continue;
  167. fw3_ipt_create_chain(handle, reload, c->format);
  168. }
  169. set(defs->flags, handle->family, handle->table);
  170. }
  171. void
  172. fw3_print_default_head_rules(struct fw3_ipt_handle *handle,
  173. struct fw3_state *state, bool reload)
  174. {
  175. int i;
  176. struct fw3_defaults *defs = &state->defaults;
  177. struct fw3_device lodev = { .set = true, .name = "lo" };
  178. struct fw3_protocol tcp = { .protocol = 6 };
  179. struct fw3_ipt_rule *r;
  180. const char *chains[] = {
  181. "INPUT", "input",
  182. "OUTPUT", "output",
  183. "FORWARD", "forwarding",
  184. };
  185. switch (handle->table)
  186. {
  187. case FW3_TABLE_FILTER:
  188. r = fw3_ipt_rule_create(handle, NULL, &lodev, NULL, NULL, NULL);
  189. fw3_ipt_rule_target(r, "ACCEPT");
  190. fw3_ipt_rule_append(r, "INPUT");
  191. r = fw3_ipt_rule_create(handle, NULL, NULL, &lodev, NULL, NULL);
  192. fw3_ipt_rule_target(r, "ACCEPT");
  193. fw3_ipt_rule_append(r, "OUTPUT");
  194. if (defs->custom_chains)
  195. {
  196. for (i = 0; i < ARRAY_SIZE(chains); i += 2)
  197. {
  198. r = fw3_ipt_rule_new(handle);
  199. fw3_ipt_rule_comment(r, "Custom %s rule chain", chains[i+1]);
  200. fw3_ipt_rule_target(r, "%s_rule", chains[i+1]);
  201. fw3_ipt_rule_append(r, chains[i]);
  202. }
  203. }
  204. if (defs->flow_offloading)
  205. {
  206. r = fw3_ipt_rule_new(handle);
  207. fw3_ipt_rule_comment(r, "Traffic offloading");
  208. fw3_ipt_rule_extra(r, "-m conntrack --ctstate RELATED,ESTABLISHED");
  209. fw3_ipt_rule_target(r, "FLOWOFFLOAD");
  210. if (defs->flow_offloading_hw)
  211. fw3_ipt_rule_addarg(r, false, "--hw", NULL);
  212. fw3_ipt_rule_append(r, "FORWARD");
  213. }
  214. for (i = 0; i < ARRAY_SIZE(chains); i += 2)
  215. {
  216. r = fw3_ipt_rule_new(handle);
  217. fw3_ipt_rule_extra(r, "-m conntrack --ctstate RELATED,ESTABLISHED");
  218. fw3_ipt_rule_target(r, "ACCEPT");
  219. fw3_ipt_rule_append(r, chains[i]);
  220. if (defs->drop_invalid)
  221. {
  222. r = fw3_ipt_rule_new(handle);
  223. fw3_ipt_rule_extra(r, "-m conntrack --ctstate INVALID");
  224. fw3_ipt_rule_target(r, "DROP");
  225. fw3_ipt_rule_append(r, chains[i]);
  226. }
  227. }
  228. if (defs->syn_flood)
  229. {
  230. r = fw3_ipt_rule_create(handle, NULL, NULL, NULL, NULL, NULL);
  231. fw3_ipt_rule_limit(r, &defs->syn_flood_rate);
  232. fw3_ipt_rule_target(r, "RETURN");
  233. fw3_ipt_rule_append(r, "syn_flood");
  234. r = fw3_ipt_rule_new(handle);
  235. fw3_ipt_rule_target(r, "DROP");
  236. fw3_ipt_rule_append(r, "syn_flood");
  237. r = fw3_ipt_rule_create(handle, &tcp, NULL, NULL, NULL, NULL);
  238. fw3_ipt_rule_extra(r, "--syn");
  239. fw3_ipt_rule_target(r, "syn_flood");
  240. fw3_ipt_rule_append(r, "INPUT");
  241. }
  242. r = fw3_ipt_rule_create(handle, &tcp, NULL, NULL, NULL, NULL);
  243. fw3_ipt_rule_target(r, "REJECT");
  244. fw3_ipt_rule_addarg(r, false, "--reject-with", get_reject_code(handle->family, defs->tcp_reject_code));
  245. fw3_ipt_rule_append(r, "reject");
  246. r = fw3_ipt_rule_new(handle);
  247. fw3_ipt_rule_target(r, "REJECT");
  248. fw3_ipt_rule_addarg(r, false, "--reject-with", get_reject_code(handle->family, defs->any_reject_code));
  249. fw3_ipt_rule_append(r, "reject");
  250. break;
  251. case FW3_TABLE_NAT:
  252. if (defs->custom_chains)
  253. {
  254. r = fw3_ipt_rule_new(handle);
  255. fw3_ipt_rule_comment(r, "Custom prerouting rule chain");
  256. fw3_ipt_rule_target(r, "prerouting_rule");
  257. fw3_ipt_rule_append(r, "PREROUTING");
  258. r = fw3_ipt_rule_new(handle);
  259. fw3_ipt_rule_comment(r, "Custom postrouting rule chain");
  260. fw3_ipt_rule_target(r, "postrouting_rule");
  261. fw3_ipt_rule_append(r, "POSTROUTING");
  262. }
  263. break;
  264. default:
  265. break;
  266. }
  267. }
  268. void
  269. fw3_print_default_tail_rules(struct fw3_ipt_handle *handle,
  270. struct fw3_state *state, bool reload)
  271. {
  272. struct fw3_defaults *defs = &state->defaults;
  273. struct fw3_ipt_rule *r;
  274. if (handle->table != FW3_TABLE_FILTER)
  275. return;
  276. if (defs->policy_input == FW3_FLAG_REJECT)
  277. {
  278. r = fw3_ipt_rule_new(handle);
  279. if (!r)
  280. return;
  281. fw3_ipt_rule_target(r, "reject");
  282. fw3_ipt_rule_append(r, "INPUT");
  283. }
  284. if (defs->policy_output == FW3_FLAG_REJECT)
  285. {
  286. r = fw3_ipt_rule_new(handle);
  287. if (!r)
  288. return;
  289. fw3_ipt_rule_target(r, "reject");
  290. fw3_ipt_rule_append(r, "OUTPUT");
  291. }
  292. if (defs->policy_forward == FW3_FLAG_REJECT)
  293. {
  294. r = fw3_ipt_rule_new(handle);
  295. if (!r)
  296. return;
  297. fw3_ipt_rule_target(r, "reject");
  298. fw3_ipt_rule_append(r, "FORWARD");
  299. }
  300. }
  301. static void
  302. set_default(const char *name, int set)
  303. {
  304. FILE *f;
  305. char path[sizeof("/proc/sys/net/ipv4/tcp_window_scaling")];
  306. snprintf(path, sizeof(path), "/proc/sys/net/ipv4/tcp_%s", name);
  307. info(" * Set tcp_%s to %s", name, set ? "on" : "off");
  308. if (!(f = fopen(path, "w")))
  309. {
  310. info(" ! Unable to write value: %s", strerror(errno));
  311. return;
  312. }
  313. fprintf(f, "%u\n", set);
  314. fclose(f);
  315. }
  316. void
  317. fw3_set_defaults(struct fw3_state *state)
  318. {
  319. set_default("ecn", state->defaults.tcp_ecn);
  320. set_default("syncookies", state->defaults.tcp_syncookies);
  321. set_default("window_scaling", state->defaults.tcp_window_scaling);
  322. }
  323. void
  324. fw3_flush_rules(struct fw3_ipt_handle *handle, struct fw3_state *state,
  325. bool reload)
  326. {
  327. enum fw3_flag policy = reload ? FW3_FLAG_DROP : FW3_FLAG_ACCEPT;
  328. struct fw3_defaults *defs = &state->defaults;
  329. const struct fw3_chain_spec *c;
  330. if (!has(defs->flags, handle->family, handle->table))
  331. return;
  332. if (handle->table == FW3_TABLE_FILTER)
  333. {
  334. fw3_ipt_set_policy(handle, "INPUT", policy);
  335. fw3_ipt_set_policy(handle, "OUTPUT", policy);
  336. fw3_ipt_set_policy(handle, "FORWARD", policy);
  337. }
  338. fw3_ipt_delete_id_rules(handle, "INPUT");
  339. fw3_ipt_delete_id_rules(handle, "OUTPUT");
  340. fw3_ipt_delete_id_rules(handle, "FORWARD");
  341. fw3_ipt_delete_id_rules(handle, "PREROUTING");
  342. fw3_ipt_delete_id_rules(handle, "POSTROUTING");
  343. /* first flush all the rules ... */
  344. for (c = default_chains; c->format; c++)
  345. {
  346. /* don't touch user chains on selective stop */
  347. if (reload && c->flag == FW3_FLAG_CUSTOM_CHAINS)
  348. continue;
  349. if (!fw3_is_family(c, handle->family))
  350. continue;
  351. if (c->table != handle->table)
  352. continue;
  353. if (c->flag && !has(defs->flags, handle->family, c->flag))
  354. continue;
  355. fw3_ipt_flush_chain(handle, c->format);
  356. }
  357. /* ... then remove the chains */
  358. for (c = default_chains; c->format; c++)
  359. {
  360. if (!fw3_is_family(c, handle->family))
  361. continue;
  362. if (c->table != handle->table)
  363. continue;
  364. if (c->flag && !has(defs->flags, handle->family, c->flag))
  365. continue;
  366. fw3_ipt_delete_chain(handle, reload, c->format);
  367. }
  368. del(defs->flags, handle->family, handle->table);
  369. }
  370. void
  371. fw3_flush_all(struct fw3_ipt_handle *handle)
  372. {
  373. if (handle->table == FW3_TABLE_FILTER)
  374. {
  375. fw3_ipt_set_policy(handle, "INPUT", FW3_FLAG_ACCEPT);
  376. fw3_ipt_set_policy(handle, "OUTPUT", FW3_FLAG_ACCEPT);
  377. fw3_ipt_set_policy(handle, "FORWARD", FW3_FLAG_ACCEPT);
  378. }
  379. fw3_ipt_flush(handle);
  380. }