rules.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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->set_mark.invert || r->set_xmark.invert || r->set_dscp.invert)
  142. {
  143. warn_section("rule", r, e, "must not have inverted 'set_mark', "
  144. "'set_xmark' or 'set_dscp'");
  145. return false;
  146. }
  147. if (!r->set_helper.set && r->target == FW3_FLAG_HELPER)
  148. {
  149. warn_section("rule", r, e, "is set to target HELPER but specifies "
  150. "no 'set_helper' option");
  151. return false;
  152. }
  153. if (r->set_helper.invert && r->target == FW3_FLAG_HELPER)
  154. {
  155. warn_section("rule", r, e, "must not have inverted 'set_helper' option");
  156. return false;
  157. }
  158. if (!r->_src && !r->_dest && !r->src.any && !r->dest.any)
  159. {
  160. warn_section("rule", r, e, "has neither a source nor a destination zone assigned "
  161. "- assuming an output rule");
  162. }
  163. if (list_empty(&r->proto))
  164. {
  165. warn_section("rule", r, e, "does not specify a protocol, assuming TCP+UDP");
  166. fw3_parse_protocol(&r->proto, "tcpudp", true);
  167. }
  168. if (r->target == FW3_FLAG_UNSPEC)
  169. {
  170. warn_section("rule", r, e, "has no target specified, defaulting to REJECT");
  171. r->target = FW3_FLAG_REJECT;
  172. }
  173. else if (r->target > FW3_FLAG_DSCP)
  174. {
  175. warn_section("rule", r, e, "has invalid target specified, defaulting to REJECT");
  176. r->target = FW3_FLAG_REJECT;
  177. }
  178. /* NB: r family... */
  179. if (r->_dest)
  180. {
  181. fw3_setbit(r->_dest->flags[0], r->target);
  182. fw3_setbit(r->_dest->flags[1], r->target);
  183. }
  184. else if (need_src_action_chain(r))
  185. {
  186. fw3_setbit(r->_src->flags[0], fw3_to_src_target(r->target));
  187. fw3_setbit(r->_src->flags[1], fw3_to_src_target(r->target));
  188. }
  189. return true;
  190. }
  191. void
  192. fw3_load_rules(struct fw3_state *state, struct uci_package *p,
  193. struct blob_attr *a)
  194. {
  195. struct uci_section *s;
  196. struct uci_element *e;
  197. struct fw3_rule *rule;
  198. struct blob_attr *entry;
  199. unsigned rem;
  200. INIT_LIST_HEAD(&state->rules);
  201. blob_for_each_attr(entry, a, rem) {
  202. const char *type;
  203. const char *name = "ubus rule";
  204. if (!fw3_attr_parse_name_type(entry, &name, &type))
  205. continue;
  206. if (strcmp(type, "rule"))
  207. continue;
  208. if (!(rule = alloc_rule(state)))
  209. continue;
  210. if (!fw3_parse_blob_options(rule, fw3_rule_opts, entry, name))
  211. {
  212. warn_section("rule", rule, NULL, "skipped due to invalid options");
  213. fw3_free_rule(rule);
  214. continue;
  215. }
  216. if (!check_rule(state, rule, NULL))
  217. fw3_free_rule(rule);
  218. }
  219. uci_foreach_element(&p->sections, e)
  220. {
  221. s = uci_to_section(e);
  222. if (strcmp(s->type, "rule"))
  223. continue;
  224. if (!(rule = alloc_rule(state)))
  225. continue;
  226. if (!fw3_parse_options(rule, fw3_rule_opts, s))
  227. {
  228. warn_elem(e, "skipped due to invalid options");
  229. fw3_free_rule(rule);
  230. continue;
  231. }
  232. if (!check_rule(state, rule, e))
  233. fw3_free_rule(rule);
  234. }
  235. }
  236. static void
  237. append_chain(struct fw3_ipt_rule *r, struct fw3_rule *rule)
  238. {
  239. char chain[32];
  240. snprintf(chain, sizeof(chain), "OUTPUT");
  241. if (rule->target == FW3_FLAG_NOTRACK)
  242. {
  243. snprintf(chain, sizeof(chain), "zone_%s_notrack", rule->src.name);
  244. }
  245. else if (rule->target == FW3_FLAG_HELPER)
  246. {
  247. snprintf(chain, sizeof(chain), "zone_%s_helper", rule->src.name);
  248. }
  249. else if (rule->target == FW3_FLAG_MARK || rule->target == FW3_FLAG_DSCP)
  250. {
  251. if ((rule->_dest && rule->_src) ||
  252. (rule->dest.any && rule->src.any))
  253. snprintf(chain, sizeof(chain), "FORWARD");
  254. else if (rule->src.any && rule->_dest)
  255. snprintf(chain, sizeof(chain), "POSTROUTING");
  256. else if (rule->dest.any && rule->_src)
  257. snprintf(chain, sizeof(chain), "PREROUTING");
  258. else if (!rule->dest.set && rule->src.set)
  259. snprintf(chain, sizeof(chain), "INPUT");
  260. else /* if (!rule->src.set) */
  261. snprintf(chain, sizeof(chain), "OUTPUT");
  262. }
  263. else
  264. {
  265. if (rule->src.set)
  266. {
  267. if (!rule->src.any)
  268. {
  269. if (rule->dest.set)
  270. snprintf(chain, sizeof(chain), "zone_%s_forward",
  271. rule->src.name);
  272. else
  273. snprintf(chain, sizeof(chain), "zone_%s_input",
  274. rule->src.name);
  275. }
  276. else
  277. {
  278. if (rule->dest.set)
  279. snprintf(chain, sizeof(chain), "FORWARD");
  280. else
  281. snprintf(chain, sizeof(chain), "INPUT");
  282. }
  283. }
  284. if (rule->dest.set && !rule->src.set)
  285. {
  286. if (rule->dest.any)
  287. snprintf(chain, sizeof(chain), "OUTPUT");
  288. else
  289. snprintf(chain, sizeof(chain), "zone_%s_output",
  290. rule->dest.name);
  291. }
  292. }
  293. fw3_ipt_rule_append(r, chain);
  294. }
  295. static void set_target(struct fw3_ipt_rule *r, struct fw3_rule *rule)
  296. {
  297. const char *name;
  298. struct fw3_mark *mark;
  299. char buf[sizeof("0xFFFFFFFF/0xFFFFFFFF")];
  300. switch(rule->target)
  301. {
  302. case FW3_FLAG_MARK:
  303. name = rule->set_mark.set ? "--set-mark" : "--set-xmark";
  304. mark = rule->set_mark.set ? &rule->set_mark : &rule->set_xmark;
  305. snprintf(buf, sizeof(buf), "0x%x/0x%x", mark->mark, mark->mask);
  306. fw3_ipt_rule_target(r, "MARK");
  307. fw3_ipt_rule_addarg(r, false, name, buf);
  308. return;
  309. case FW3_FLAG_DSCP:
  310. snprintf(buf, sizeof(buf), "0x%x", rule->set_dscp.dscp);
  311. fw3_ipt_rule_target(r, "DSCP");
  312. fw3_ipt_rule_addarg(r, false, "--set-dscp", buf);
  313. return;
  314. case FW3_FLAG_NOTRACK:
  315. fw3_ipt_rule_target(r, "CT");
  316. fw3_ipt_rule_addarg(r, false, "--notrack", NULL);
  317. return;
  318. case FW3_FLAG_HELPER:
  319. fw3_ipt_rule_target(r, "CT");
  320. fw3_ipt_rule_addarg(r, false, "--helper", rule->set_helper.ptr->name);
  321. return;
  322. case FW3_FLAG_ACCEPT:
  323. case FW3_FLAG_DROP:
  324. name = fw3_flag_names[rule->target];
  325. break;
  326. default:
  327. name = fw3_flag_names[FW3_FLAG_REJECT];
  328. break;
  329. }
  330. if (rule->dest.set && !rule->dest.any)
  331. fw3_ipt_rule_target(r, "zone_%s_dest_%s", rule->dest.name, name);
  332. else if (need_src_action_chain(rule))
  333. fw3_ipt_rule_target(r, "zone_%s_src_%s", rule->src.name, name);
  334. else if (strcmp(name, "REJECT"))
  335. fw3_ipt_rule_target(r, name);
  336. else
  337. fw3_ipt_rule_target(r, "reject");
  338. }
  339. static void
  340. set_comment(struct fw3_ipt_rule *r, const char *name, int num)
  341. {
  342. if (name)
  343. fw3_ipt_rule_comment(r, name);
  344. else
  345. fw3_ipt_rule_comment(r, "@rule[%u]", num);
  346. }
  347. static void
  348. print_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
  349. struct fw3_rule *rule, int num, struct fw3_protocol *proto,
  350. struct fw3_address *sip, struct fw3_address *dip,
  351. struct fw3_port *sport, struct fw3_port *dport,
  352. struct fw3_mac *mac, struct fw3_icmptype *icmptype)
  353. {
  354. struct fw3_ipt_rule *r;
  355. struct fw3_device *idev, *odev;
  356. struct list_head empty, *idevices, *odevices;
  357. INIT_LIST_HEAD(&empty);
  358. idevices = odevices = &empty;
  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 (!fw3_is_family(sip, handle->family) ||
  374. !fw3_is_family(dip, handle->family))
  375. {
  376. if ((sip && !sip->resolved) || (dip && !dip->resolved))
  377. info(" ! Skipping due to different family of ip address");
  378. return;
  379. }
  380. if (proto->protocol == 58 && handle->family == FW3_FAMILY_V4)
  381. {
  382. info(" ! Skipping protocol %s due to different family",
  383. fw3_protoname(proto));
  384. return;
  385. }
  386. if (rule->helper.ptr &&
  387. !fw3_cthelper_check_proto(rule->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. if (rule->set_helper.ptr &&
  394. !fw3_cthelper_check_proto(rule->set_helper.ptr, proto))
  395. {
  396. info(" ! Skipping protocol %s since helper '%s' does not support it",
  397. fw3_protoname(proto), rule->helper.ptr->name);
  398. return;
  399. }
  400. if (rule->target == FW3_FLAG_DSCP || rule->target == FW3_FLAG_MARK)
  401. {
  402. if (rule->_src)
  403. idevices = &rule->_src->devices;
  404. if (rule->_dest)
  405. odevices = &rule->_dest->devices;
  406. }
  407. fw3_foreach(idev, idevices)
  408. fw3_foreach(odev, odevices)
  409. {
  410. r = fw3_ipt_rule_create(handle, proto, idev, odev, sip, dip);
  411. fw3_ipt_rule_sport_dport(r, sport, dport);
  412. fw3_ipt_rule_device(r, rule->device, rule->direction_out);
  413. fw3_ipt_rule_icmptype(r, icmptype);
  414. fw3_ipt_rule_mac(r, mac);
  415. fw3_ipt_rule_ipset(r, &rule->ipset);
  416. fw3_ipt_rule_helper(r, &rule->helper);
  417. fw3_ipt_rule_limit(r, &rule->limit);
  418. fw3_ipt_rule_time(r, &rule->time);
  419. fw3_ipt_rule_mark(r, &rule->mark);
  420. fw3_ipt_rule_dscp(r, &rule->dscp);
  421. set_target(r, rule);
  422. fw3_ipt_rule_extra(r, rule->extra);
  423. set_comment(r, rule->name, num);
  424. append_chain(r, rule);
  425. }
  426. }
  427. static void
  428. expand_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
  429. struct fw3_rule *rule, int num)
  430. {
  431. struct fw3_protocol *proto;
  432. struct fw3_address *sip;
  433. struct fw3_address *dip;
  434. struct fw3_port *sport;
  435. struct fw3_port *dport;
  436. struct fw3_mac *mac;
  437. struct fw3_icmptype *icmptype;
  438. struct list_head *sports = NULL;
  439. struct list_head *dports = NULL;
  440. struct list_head *icmptypes = NULL;
  441. struct list_head empty;
  442. INIT_LIST_HEAD(&empty);
  443. if (!fw3_is_family(rule, handle->family))
  444. return;
  445. if ((rule->target == FW3_FLAG_NOTRACK && handle->table != FW3_TABLE_RAW) ||
  446. (rule->target == FW3_FLAG_HELPER && handle->table != FW3_TABLE_RAW) ||
  447. (rule->target == FW3_FLAG_MARK && handle->table != FW3_TABLE_MANGLE) ||
  448. (rule->target == FW3_FLAG_DSCP && handle->table != FW3_TABLE_MANGLE) ||
  449. (rule->target < FW3_FLAG_NOTRACK && handle->table != FW3_TABLE_FILTER))
  450. return;
  451. if (rule->name)
  452. info(" * Rule '%s'", rule->name);
  453. else
  454. info(" * Rule #%u", num);
  455. if (!fw3_is_family(rule->_src, handle->family) ||
  456. !fw3_is_family(rule->_dest, handle->family))
  457. {
  458. info(" ! Skipping due to different family of zone");
  459. return;
  460. }
  461. if (rule->ipset.ptr)
  462. {
  463. if (!fw3_is_family(rule->ipset.ptr, handle->family))
  464. {
  465. info(" ! Skipping due to different family in ipset");
  466. return;
  467. }
  468. if (!fw3_check_ipset(rule->ipset.ptr))
  469. {
  470. info(" ! Skipping due to missing ipset '%s'",
  471. rule->ipset.ptr->external
  472. ? rule->ipset.ptr->external : rule->ipset.ptr->name);
  473. return;
  474. }
  475. set(rule->ipset.ptr->flags, handle->family, handle->family);
  476. }
  477. if (rule->helper.ptr && !fw3_is_family(rule->helper.ptr, handle->family))
  478. {
  479. info(" ! Skipping due to unsupported family of CT helper");
  480. return;
  481. }
  482. if (rule->set_helper.ptr && !fw3_is_family(rule->set_helper.ptr, handle->family))
  483. {
  484. info(" ! Skipping due to unsupported family of CT helper");
  485. return;
  486. }
  487. list_for_each_entry(proto, &rule->proto, list)
  488. {
  489. /* icmp / ipv6-icmp */
  490. if (proto->protocol == 1 || proto->protocol == 58)
  491. {
  492. sports = &empty;
  493. dports = &empty;
  494. icmptypes = &rule->icmp_type;
  495. }
  496. else
  497. {
  498. sports = &rule->port_src;
  499. dports = &rule->port_dest;
  500. icmptypes = &empty;
  501. }
  502. fw3_foreach(sip, &rule->ip_src)
  503. fw3_foreach(dip, &rule->ip_dest)
  504. fw3_foreach(sport, sports)
  505. fw3_foreach(dport, dports)
  506. fw3_foreach(mac, &rule->mac_src)
  507. fw3_foreach(icmptype, icmptypes)
  508. print_rule(handle, state, rule, num, proto, sip, dip,
  509. sport, dport, mac, icmptype);
  510. }
  511. }
  512. void
  513. fw3_print_rules(struct fw3_ipt_handle *handle, struct fw3_state *state)
  514. {
  515. int num = 0;
  516. struct fw3_rule *rule;
  517. list_for_each_entry(rule, &state->rules, list)
  518. expand_rule(handle, state, rule, num++);
  519. }