rules.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*
  2. * firewall3 - 3rd OpenWrt UCI firewall implementation
  3. *
  4. * Copyright (C) 2013-2018 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 "rules.h"
  19. const struct fw3_option fw3_rule_opts[] = {
  20. FW3_OPT("enabled", bool, rule, enabled),
  21. FW3_OPT("name", string, rule, name),
  22. FW3_OPT("family", family, rule, family),
  23. FW3_OPT("src", device, rule, src),
  24. FW3_OPT("dest", device, rule, dest),
  25. FW3_OPT("device", string, rule, device),
  26. FW3_OPT("direction", direction, rule, direction_out),
  27. FW3_OPT("ipset", setmatch, rule, ipset),
  28. FW3_OPT("helper", cthelper, rule, helper),
  29. FW3_OPT("set_helper", cthelper, rule, set_helper),
  30. FW3_LIST("proto", protocol, rule, proto),
  31. FW3_LIST("src_ip", network, rule, ip_src),
  32. FW3_LIST("src_mac", mac, rule, mac_src),
  33. FW3_LIST("src_port", port, rule, port_src),
  34. FW3_LIST("dest_ip", network, rule, ip_dest),
  35. FW3_LIST("dest_port", port, rule, port_dest),
  36. FW3_LIST("icmp_type", icmptype, rule, icmp_type),
  37. FW3_OPT("extra", string, rule, extra),
  38. FW3_OPT("limit", limit, rule, limit),
  39. FW3_OPT("limit_burst", int, rule, limit.burst),
  40. FW3_OPT("utc_time", bool, rule, time.utc),
  41. FW3_OPT("start_date", date, rule, time.datestart),
  42. FW3_OPT("stop_date", date, rule, time.datestop),
  43. FW3_OPT("start_time", time, rule, time.timestart),
  44. FW3_OPT("stop_time", time, rule, time.timestop),
  45. FW3_OPT("weekdays", weekdays, rule, time.weekdays),
  46. FW3_OPT("monthdays", monthdays, rule, time.monthdays),
  47. FW3_OPT("mark", mark, rule, mark),
  48. FW3_OPT("set_mark", mark, rule, set_mark),
  49. FW3_OPT("set_xmark", mark, rule, set_xmark),
  50. FW3_OPT("dscp", dscp, rule, dscp),
  51. FW3_OPT("set_dscp", dscp, rule, set_dscp),
  52. FW3_OPT("target", target, rule, target),
  53. { }
  54. };
  55. static bool
  56. need_src_action_chain(struct fw3_rule *r)
  57. {
  58. return (r->_src && r->_src->log && (r->target > FW3_FLAG_ACCEPT));
  59. }
  60. static struct fw3_rule*
  61. alloc_rule(struct fw3_state *state)
  62. {
  63. struct fw3_rule *rule = calloc(1, sizeof(*rule));
  64. if (rule) {
  65. INIT_LIST_HEAD(&rule->proto);
  66. INIT_LIST_HEAD(&rule->ip_src);
  67. INIT_LIST_HEAD(&rule->mac_src);
  68. INIT_LIST_HEAD(&rule->port_src);
  69. INIT_LIST_HEAD(&rule->ip_dest);
  70. INIT_LIST_HEAD(&rule->port_dest);
  71. INIT_LIST_HEAD(&rule->icmp_type);
  72. list_add_tail(&rule->list, &state->rules);
  73. rule->enabled = true;
  74. }
  75. return rule;
  76. }
  77. static bool
  78. check_rule(struct fw3_state *state, struct fw3_rule *r, struct uci_element *e)
  79. {
  80. if (!r->enabled)
  81. return false;
  82. if (r->src.invert || r->dest.invert)
  83. {
  84. warn_section("rule", r, e, "must not have inverted 'src' or 'dest' options");
  85. return false;
  86. }
  87. else if (r->src.set && !r->src.any &&
  88. !(r->_src = fw3_lookup_zone(state, r->src.name)))
  89. {
  90. warn_section("rule", r, e, "refers to not existing zone '%s'", r->src.name);
  91. return false;
  92. }
  93. else if (r->dest.set && !r->dest.any &&
  94. !(r->_dest = fw3_lookup_zone(state, r->dest.name)))
  95. {
  96. warn_section("rule", r, e, "refers to not existing zone '%s'", r->dest.name);
  97. return false;
  98. }
  99. else if (r->ipset.set && state->disable_ipsets)
  100. {
  101. warn_section("rule", r, e, "skipped due to disabled ipset support");
  102. return false;
  103. }
  104. else if (r->ipset.set &&
  105. !(r->ipset.ptr = fw3_lookup_ipset(state, r->ipset.name)))
  106. {
  107. warn_section("rule", r, e, "refers to unknown ipset '%s'", r->ipset.name);
  108. return false;
  109. }
  110. else if (r->helper.set &&
  111. !(r->helper.ptr = fw3_lookup_cthelper(state, r->helper.name)))
  112. {
  113. warn_section("rule", r, e, "refers to unknown CT helper '%s'", r->helper.name);
  114. return false;
  115. }
  116. else if (r->set_helper.set &&
  117. !(r->set_helper.ptr = fw3_lookup_cthelper(state, r->set_helper.name)))
  118. {
  119. warn_section("rule", r, e, "refers to unknown CT helper '%s'", r->set_helper.name);
  120. return false;
  121. }
  122. if (!r->_src && (r->target == FW3_FLAG_NOTRACK || r->target == FW3_FLAG_HELPER))
  123. {
  124. warn_section("rule", r, e, "is set to target %s but has no source assigned",
  125. fw3_flag_names[r->target]);
  126. return false;
  127. }
  128. if (!r->set_mark.set && !r->set_xmark.set &&
  129. r->target == FW3_FLAG_MARK)
  130. {
  131. warn_section("rule", r, e, "is set to target MARK but specifies neither "
  132. "'set_mark' nor 'set_xmark' option");
  133. return false;
  134. }
  135. if (!r->set_dscp.set && r->target == FW3_FLAG_DSCP)
  136. {
  137. warn_section("rule", r, e, "is set to target DSCP but specifies no "
  138. "'set_dscp' option");
  139. return false;
  140. }
  141. if (r->_dest && (r->target == FW3_FLAG_MARK || r->target == FW3_FLAG_DSCP))
  142. {
  143. warn_section("rule", r, e, "must not specify 'dest' for %s target",
  144. fw3_flag_names[r->target]);
  145. return false;
  146. }
  147. if (r->set_mark.invert || r->set_xmark.invert || r->set_dscp.invert)
  148. {
  149. warn_section("rule", r, e, "must not have inverted 'set_mark', "
  150. "'set_xmark' or 'set_dscp'");
  151. return false;
  152. }
  153. if (!r->set_helper.set && r->target == FW3_FLAG_HELPER)
  154. {
  155. warn_section("rule", r, e, "is set to target HELPER but specifies "
  156. "no 'set_helper' option");
  157. return false;
  158. }
  159. if (r->set_helper.invert && r->target == FW3_FLAG_HELPER)
  160. {
  161. warn_section("rule", r, e, "must not have inverted 'set_helper' option");
  162. return false;
  163. }
  164. if (!r->_src && !r->_dest && !r->src.any && !r->dest.any)
  165. {
  166. warn_section("rule", r, e, "has neither a source nor a destination zone assigned "
  167. "- assuming an output r");
  168. }
  169. if (list_empty(&r->proto))
  170. {
  171. warn_section("rule", r, e, "does not specify a protocol, assuming TCP+UDP");
  172. fw3_parse_protocol(&r->proto, "tcpudp", true);
  173. }
  174. if (r->target == FW3_FLAG_UNSPEC)
  175. {
  176. warn_section("rule", r, e, "has no target specified, defaulting to REJECT");
  177. r->target = FW3_FLAG_REJECT;
  178. }
  179. else if (r->target > FW3_FLAG_DSCP)
  180. {
  181. warn_section("rule", r, e, "has invalid target specified, defaulting to REJECT");
  182. r->target = FW3_FLAG_REJECT;
  183. }
  184. /* NB: r family... */
  185. if (r->_dest)
  186. {
  187. fw3_setbit(r->_dest->flags[0], r->target);
  188. fw3_setbit(r->_dest->flags[1], r->target);
  189. }
  190. else if (need_src_action_chain(r))
  191. {
  192. fw3_setbit(r->_src->flags[0], fw3_to_src_target(r->target));
  193. fw3_setbit(r->_src->flags[1], fw3_to_src_target(r->target));
  194. }
  195. return true;
  196. }
  197. void
  198. fw3_load_rules(struct fw3_state *state, struct uci_package *p,
  199. struct blob_attr *a)
  200. {
  201. struct uci_section *s;
  202. struct uci_element *e;
  203. struct fw3_rule *rule;
  204. struct blob_attr *entry;
  205. unsigned rem;
  206. INIT_LIST_HEAD(&state->rules);
  207. blob_for_each_attr(entry, a, rem) {
  208. const char *type;
  209. const char *name = "ubus rule";
  210. if (!fw3_attr_parse_name_type(entry, &name, &type))
  211. continue;
  212. if (strcmp(type, "rule"))
  213. continue;
  214. if (!(rule = alloc_rule(state)))
  215. continue;
  216. if (!fw3_parse_blob_options(rule, fw3_rule_opts, entry, name))
  217. {
  218. warn_section("rule", rule, NULL, "skipped due to invalid options");
  219. fw3_free_rule(rule);
  220. continue;
  221. }
  222. if (!check_rule(state, rule, NULL))
  223. fw3_free_rule(rule);
  224. }
  225. uci_foreach_element(&p->sections, e)
  226. {
  227. s = uci_to_section(e);
  228. if (strcmp(s->type, "rule"))
  229. continue;
  230. if (!(rule = alloc_rule(state)))
  231. continue;
  232. if (!fw3_parse_options(rule, fw3_rule_opts, s))
  233. {
  234. warn_elem(e, "skipped due to invalid options");
  235. fw3_free_rule(rule);
  236. continue;
  237. }
  238. if (!check_rule(state, rule, e))
  239. fw3_free_rule(rule);
  240. }
  241. }
  242. static void
  243. append_chain(struct fw3_ipt_rule *r, struct fw3_rule *rule)
  244. {
  245. char chain[32];
  246. snprintf(chain, sizeof(chain), "OUTPUT");
  247. if (rule->target == FW3_FLAG_NOTRACK)
  248. {
  249. snprintf(chain, sizeof(chain), "zone_%s_notrack", rule->src.name);
  250. }
  251. else if (rule->target == FW3_FLAG_HELPER)
  252. {
  253. snprintf(chain, sizeof(chain), "zone_%s_helper", rule->src.name);
  254. }
  255. else if ((rule->target == FW3_FLAG_MARK || rule->target == FW3_FLAG_DSCP) &&
  256. (rule->_src || rule->src.any))
  257. {
  258. snprintf(chain, sizeof(chain), "PREROUTING");
  259. }
  260. else
  261. {
  262. if (rule->src.set)
  263. {
  264. if (!rule->src.any)
  265. {
  266. if (rule->dest.set)
  267. snprintf(chain, sizeof(chain), "zone_%s_forward",
  268. rule->src.name);
  269. else
  270. snprintf(chain, sizeof(chain), "zone_%s_input",
  271. rule->src.name);
  272. }
  273. else
  274. {
  275. if (rule->dest.set)
  276. snprintf(chain, sizeof(chain), "FORWARD");
  277. else
  278. snprintf(chain, sizeof(chain), "INPUT");
  279. }
  280. }
  281. if (rule->dest.set && !rule->src.set)
  282. {
  283. if (rule->dest.any)
  284. snprintf(chain, sizeof(chain), "OUTPUT");
  285. else
  286. snprintf(chain, sizeof(chain), "zone_%s_output",
  287. rule->dest.name);
  288. }
  289. }
  290. fw3_ipt_rule_append(r, chain);
  291. }
  292. static void set_target(struct fw3_ipt_rule *r, struct fw3_rule *rule)
  293. {
  294. const char *name;
  295. struct fw3_mark *mark;
  296. char buf[sizeof("0xFFFFFFFF/0xFFFFFFFF\0")];
  297. switch(rule->target)
  298. {
  299. case FW3_FLAG_MARK:
  300. name = rule->set_mark.set ? "--set-mark" : "--set-xmark";
  301. mark = rule->set_mark.set ? &rule->set_mark : &rule->set_xmark;
  302. sprintf(buf, "0x%x/0x%x", mark->mark, mark->mask);
  303. fw3_ipt_rule_target(r, "MARK");
  304. fw3_ipt_rule_addarg(r, false, name, buf);
  305. return;
  306. case FW3_FLAG_DSCP:
  307. sprintf(buf, "0x%x", rule->set_dscp.dscp);
  308. fw3_ipt_rule_target(r, "DSCP");
  309. fw3_ipt_rule_addarg(r, false, "--set-dscp", buf);
  310. return;
  311. case FW3_FLAG_NOTRACK:
  312. fw3_ipt_rule_target(r, "CT");
  313. fw3_ipt_rule_addarg(r, false, "--notrack", NULL);
  314. return;
  315. case FW3_FLAG_HELPER:
  316. fw3_ipt_rule_target(r, "CT");
  317. fw3_ipt_rule_addarg(r, false, "--helper", rule->set_helper.ptr->name);
  318. return;
  319. case FW3_FLAG_ACCEPT:
  320. case FW3_FLAG_DROP:
  321. name = fw3_flag_names[rule->target];
  322. break;
  323. default:
  324. name = fw3_flag_names[FW3_FLAG_REJECT];
  325. break;
  326. }
  327. if (rule->dest.set && !rule->dest.any)
  328. fw3_ipt_rule_target(r, "zone_%s_dest_%s", rule->dest.name, name);
  329. else if (need_src_action_chain(rule))
  330. fw3_ipt_rule_target(r, "zone_%s_src_%s", rule->src.name, name);
  331. else if (strcmp(name, "REJECT"))
  332. fw3_ipt_rule_target(r, name);
  333. else
  334. fw3_ipt_rule_target(r, "reject");
  335. }
  336. static void
  337. set_comment(struct fw3_ipt_rule *r, const char *name, int num)
  338. {
  339. if (name)
  340. fw3_ipt_rule_comment(r, name);
  341. else
  342. fw3_ipt_rule_comment(r, "@rule[%u]", num);
  343. }
  344. static void
  345. print_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
  346. struct fw3_rule *rule, int num, struct fw3_protocol *proto,
  347. struct fw3_address *sip, struct fw3_address *dip,
  348. struct fw3_port *sport, struct fw3_port *dport,
  349. struct fw3_mac *mac, struct fw3_icmptype *icmptype)
  350. {
  351. struct fw3_ipt_rule *r;
  352. if (!fw3_is_family(sip, handle->family) ||
  353. !fw3_is_family(dip, handle->family))
  354. {
  355. if ((sip && !sip->resolved) || (dip && !dip->resolved))
  356. info(" ! Skipping due to different family of ip address");
  357. return;
  358. }
  359. if (!fw3_is_family(sip, handle->family) ||
  360. !fw3_is_family(dip, handle->family))
  361. {
  362. if ((sip && !sip->resolved) || (dip && !dip->resolved))
  363. info(" ! Skipping due to different family of ip address");
  364. return;
  365. }
  366. if (!fw3_is_family(sip, handle->family) ||
  367. !fw3_is_family(dip, handle->family))
  368. {
  369. if ((sip && !sip->resolved) || (dip && !dip->resolved))
  370. info(" ! Skipping due to different family of ip address");
  371. return;
  372. }
  373. if (proto->protocol == 58 && handle->family == FW3_FAMILY_V4)
  374. {
  375. info(" ! Skipping protocol %s due to different family",
  376. fw3_protoname(proto));
  377. return;
  378. }
  379. if (rule->helper.ptr &&
  380. !fw3_cthelper_check_proto(rule->helper.ptr, proto))
  381. {
  382. info(" ! Skipping protocol %s since helper '%s' does not support it",
  383. fw3_protoname(proto), rule->helper.ptr->name);
  384. return;
  385. }
  386. if (rule->set_helper.ptr &&
  387. !fw3_cthelper_check_proto(rule->set_helper.ptr, proto))
  388. {
  389. info(" ! Skipping protocol %s since helper '%s' does not support it",
  390. fw3_protoname(proto), rule->helper.ptr->name);
  391. return;
  392. }
  393. r = fw3_ipt_rule_create(handle, proto, NULL, NULL, sip, dip);
  394. fw3_ipt_rule_sport_dport(r, sport, dport);
  395. fw3_ipt_rule_device(r, rule->device, rule->direction_out);
  396. fw3_ipt_rule_icmptype(r, icmptype);
  397. fw3_ipt_rule_mac(r, mac);
  398. fw3_ipt_rule_ipset(r, &rule->ipset);
  399. fw3_ipt_rule_helper(r, &rule->helper);
  400. fw3_ipt_rule_limit(r, &rule->limit);
  401. fw3_ipt_rule_time(r, &rule->time);
  402. fw3_ipt_rule_mark(r, &rule->mark);
  403. fw3_ipt_rule_dscp(r, &rule->dscp);
  404. set_target(r, rule);
  405. fw3_ipt_rule_extra(r, rule->extra);
  406. set_comment(r, rule->name, num);
  407. append_chain(r, rule);
  408. }
  409. static void
  410. expand_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
  411. struct fw3_rule *rule, int num)
  412. {
  413. struct fw3_protocol *proto;
  414. struct fw3_address *sip;
  415. struct fw3_address *dip;
  416. struct fw3_port *sport;
  417. struct fw3_port *dport;
  418. struct fw3_mac *mac;
  419. struct fw3_icmptype *icmptype;
  420. struct list_head *sports = NULL;
  421. struct list_head *dports = NULL;
  422. struct list_head *icmptypes = NULL;
  423. struct list_head empty;
  424. INIT_LIST_HEAD(&empty);
  425. if (!fw3_is_family(rule, handle->family))
  426. return;
  427. if ((rule->target == FW3_FLAG_NOTRACK && handle->table != FW3_TABLE_RAW) ||
  428. (rule->target == FW3_FLAG_HELPER && handle->table != FW3_TABLE_RAW) ||
  429. (rule->target == FW3_FLAG_MARK && handle->table != FW3_TABLE_MANGLE) ||
  430. (rule->target == FW3_FLAG_DSCP && handle->table != FW3_TABLE_MANGLE) ||
  431. (rule->target < FW3_FLAG_NOTRACK && handle->table != FW3_TABLE_FILTER))
  432. return;
  433. if (rule->name)
  434. info(" * Rule '%s'", rule->name);
  435. else
  436. info(" * Rule #%u", num);
  437. if (!fw3_is_family(rule->_src, handle->family) ||
  438. !fw3_is_family(rule->_dest, handle->family))
  439. {
  440. info(" ! Skipping due to different family of zone");
  441. return;
  442. }
  443. if (rule->ipset.ptr)
  444. {
  445. if (!fw3_is_family(rule->ipset.ptr, handle->family))
  446. {
  447. info(" ! Skipping due to different family in ipset");
  448. return;
  449. }
  450. if (!fw3_check_ipset(rule->ipset.ptr))
  451. {
  452. info(" ! Skipping due to missing ipset '%s'",
  453. rule->ipset.ptr->external
  454. ? rule->ipset.ptr->external : rule->ipset.ptr->name);
  455. return;
  456. }
  457. set(rule->ipset.ptr->flags, handle->family, handle->family);
  458. }
  459. if (rule->helper.ptr && !fw3_is_family(rule->helper.ptr, handle->family))
  460. {
  461. info(" ! Skipping due to unsupported family of CT helper");
  462. return;
  463. }
  464. if (rule->set_helper.ptr && !fw3_is_family(rule->set_helper.ptr, handle->family))
  465. {
  466. info(" ! Skipping due to unsupported family of CT helper");
  467. return;
  468. }
  469. list_for_each_entry(proto, &rule->proto, list)
  470. {
  471. /* icmp / ipv6-icmp */
  472. if (proto->protocol == 1 || proto->protocol == 58)
  473. {
  474. sports = &empty;
  475. dports = &empty;
  476. icmptypes = &rule->icmp_type;
  477. }
  478. else
  479. {
  480. sports = &rule->port_src;
  481. dports = &rule->port_dest;
  482. icmptypes = &empty;
  483. }
  484. fw3_foreach(sip, &rule->ip_src)
  485. fw3_foreach(dip, &rule->ip_dest)
  486. fw3_foreach(sport, sports)
  487. fw3_foreach(dport, dports)
  488. fw3_foreach(mac, &rule->mac_src)
  489. fw3_foreach(icmptype, icmptypes)
  490. print_rule(handle, state, rule, num, proto, sip, dip,
  491. sport, dport, mac, icmptype);
  492. }
  493. }
  494. void
  495. fw3_print_rules(struct fw3_ipt_handle *handle, struct fw3_state *state)
  496. {
  497. int num = 0;
  498. struct fw3_rule *rule;
  499. list_for_each_entry(rule, &state->rules, list)
  500. expand_rule(handle, state, rule, num++);
  501. }