defaults.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. struct fw3_protocol any = {};
  207. r = fw3_ipt_rule_new(handle);
  208. fw3_ipt_rule_proto(r, &any);
  209. fw3_ipt_rule_comment(r, "Traffic offloading");
  210. fw3_ipt_rule_extra(r, "-m conntrack --ctstate RELATED,ESTABLISHED");
  211. fw3_ipt_rule_target(r, "FLOWOFFLOAD");
  212. if (defs->flow_offloading_hw)
  213. fw3_ipt_rule_addarg(r, false, "--hw", NULL);
  214. fw3_ipt_rule_append(r, "FORWARD");
  215. }
  216. for (i = 0; i < ARRAY_SIZE(chains); i += 2)
  217. {
  218. r = fw3_ipt_rule_new(handle);
  219. fw3_ipt_rule_extra(r, "-m conntrack --ctstate RELATED,ESTABLISHED");
  220. fw3_ipt_rule_target(r, "ACCEPT");
  221. fw3_ipt_rule_append(r, chains[i]);
  222. if (defs->drop_invalid)
  223. {
  224. r = fw3_ipt_rule_new(handle);
  225. fw3_ipt_rule_extra(r, "-m conntrack --ctstate INVALID");
  226. fw3_ipt_rule_target(r, "DROP");
  227. fw3_ipt_rule_append(r, chains[i]);
  228. }
  229. }
  230. if (defs->syn_flood)
  231. {
  232. r = fw3_ipt_rule_create(handle, NULL, NULL, NULL, NULL, NULL);
  233. fw3_ipt_rule_limit(r, &defs->syn_flood_rate);
  234. fw3_ipt_rule_target(r, "RETURN");
  235. fw3_ipt_rule_append(r, "syn_flood");
  236. r = fw3_ipt_rule_new(handle);
  237. fw3_ipt_rule_target(r, "DROP");
  238. fw3_ipt_rule_append(r, "syn_flood");
  239. r = fw3_ipt_rule_create(handle, &tcp, NULL, NULL, NULL, NULL);
  240. fw3_ipt_rule_extra(r, "--syn");
  241. fw3_ipt_rule_target(r, "syn_flood");
  242. fw3_ipt_rule_append(r, "INPUT");
  243. }
  244. r = fw3_ipt_rule_create(handle, &tcp, NULL, NULL, NULL, NULL);
  245. fw3_ipt_rule_target(r, "REJECT");
  246. fw3_ipt_rule_addarg(r, false, "--reject-with", get_reject_code(handle->family, defs->tcp_reject_code));
  247. fw3_ipt_rule_append(r, "reject");
  248. r = fw3_ipt_rule_new(handle);
  249. fw3_ipt_rule_target(r, "REJECT");
  250. fw3_ipt_rule_addarg(r, false, "--reject-with", get_reject_code(handle->family, defs->any_reject_code));
  251. fw3_ipt_rule_append(r, "reject");
  252. break;
  253. case FW3_TABLE_NAT:
  254. if (defs->custom_chains)
  255. {
  256. r = fw3_ipt_rule_new(handle);
  257. fw3_ipt_rule_comment(r, "Custom prerouting rule chain");
  258. fw3_ipt_rule_target(r, "prerouting_rule");
  259. fw3_ipt_rule_append(r, "PREROUTING");
  260. r = fw3_ipt_rule_new(handle);
  261. fw3_ipt_rule_comment(r, "Custom postrouting rule chain");
  262. fw3_ipt_rule_target(r, "postrouting_rule");
  263. fw3_ipt_rule_append(r, "POSTROUTING");
  264. }
  265. break;
  266. default:
  267. break;
  268. }
  269. }
  270. void
  271. fw3_print_default_tail_rules(struct fw3_ipt_handle *handle,
  272. struct fw3_state *state, bool reload)
  273. {
  274. struct fw3_defaults *defs = &state->defaults;
  275. struct fw3_ipt_rule *r;
  276. if (handle->table != FW3_TABLE_FILTER)
  277. return;
  278. if (defs->policy_input == FW3_FLAG_REJECT)
  279. {
  280. r = fw3_ipt_rule_new(handle);
  281. if (!r)
  282. return;
  283. fw3_ipt_rule_target(r, "reject");
  284. fw3_ipt_rule_append(r, "INPUT");
  285. }
  286. if (defs->policy_output == 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, "OUTPUT");
  293. }
  294. if (defs->policy_forward == 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, "FORWARD");
  301. }
  302. }
  303. static void
  304. set_default(const char *name, int set)
  305. {
  306. FILE *f;
  307. char path[sizeof("/proc/sys/net/ipv4/tcp_window_scaling")];
  308. snprintf(path, sizeof(path), "/proc/sys/net/ipv4/tcp_%s", name);
  309. info(" * Set tcp_%s to %s", name, set ? "on" : "off");
  310. if (!(f = fopen(path, "w")))
  311. {
  312. info(" ! Unable to write value: %s", strerror(errno));
  313. return;
  314. }
  315. fprintf(f, "%u\n", set);
  316. fclose(f);
  317. }
  318. void
  319. fw3_set_defaults(struct fw3_state *state)
  320. {
  321. set_default("ecn", state->defaults.tcp_ecn);
  322. set_default("syncookies", state->defaults.tcp_syncookies);
  323. set_default("window_scaling", state->defaults.tcp_window_scaling);
  324. }
  325. void
  326. fw3_flush_rules(struct fw3_ipt_handle *handle, struct fw3_state *state,
  327. bool reload)
  328. {
  329. enum fw3_flag policy = reload ? FW3_FLAG_DROP : FW3_FLAG_ACCEPT;
  330. struct fw3_defaults *defs = &state->defaults;
  331. const struct fw3_chain_spec *c;
  332. if (!has(defs->flags, handle->family, handle->table))
  333. return;
  334. if (handle->table == FW3_TABLE_FILTER)
  335. {
  336. fw3_ipt_set_policy(handle, "INPUT", policy);
  337. fw3_ipt_set_policy(handle, "OUTPUT", policy);
  338. fw3_ipt_set_policy(handle, "FORWARD", policy);
  339. }
  340. fw3_ipt_delete_id_rules(handle, "INPUT");
  341. fw3_ipt_delete_id_rules(handle, "OUTPUT");
  342. fw3_ipt_delete_id_rules(handle, "FORWARD");
  343. fw3_ipt_delete_id_rules(handle, "PREROUTING");
  344. fw3_ipt_delete_id_rules(handle, "POSTROUTING");
  345. /* first flush all the rules ... */
  346. for (c = default_chains; c->format; c++)
  347. {
  348. /* don't touch user chains on selective stop */
  349. if (reload && c->flag == FW3_FLAG_CUSTOM_CHAINS)
  350. continue;
  351. if (!fw3_is_family(c, handle->family))
  352. continue;
  353. if (c->table != handle->table)
  354. continue;
  355. if (c->flag && !has(defs->flags, handle->family, c->flag))
  356. continue;
  357. fw3_ipt_flush_chain(handle, c->format);
  358. }
  359. /* ... then remove the chains */
  360. for (c = default_chains; c->format; c++)
  361. {
  362. if (!fw3_is_family(c, handle->family))
  363. continue;
  364. if (c->table != handle->table)
  365. continue;
  366. if (c->flag && !has(defs->flags, handle->family, c->flag))
  367. continue;
  368. fw3_ipt_delete_chain(handle, reload, c->format);
  369. }
  370. del(defs->flags, handle->family, handle->table);
  371. }
  372. void
  373. fw3_flush_all(struct fw3_ipt_handle *handle)
  374. {
  375. if (handle->table == FW3_TABLE_FILTER)
  376. {
  377. fw3_ipt_set_policy(handle, "INPUT", FW3_FLAG_ACCEPT);
  378. fw3_ipt_set_policy(handle, "OUTPUT", FW3_FLAG_ACCEPT);
  379. fw3_ipt_set_policy(handle, "FORWARD", FW3_FLAG_ACCEPT);
  380. }
  381. fw3_ipt_flush(handle);
  382. }