redirects.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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 "redirects.h"
  19. const struct fw3_option fw3_redirect_opts[] = {
  20. FW3_OPT("enabled", bool, redirect, enabled),
  21. FW3_OPT("name", string, redirect, name),
  22. FW3_OPT("family", family, redirect, family),
  23. FW3_OPT("src", device, redirect, src),
  24. FW3_OPT("dest", device, redirect, dest),
  25. FW3_OPT("ipset", setmatch, redirect, ipset),
  26. FW3_OPT("helper", cthelper, redirect, helper),
  27. FW3_LIST("proto", protocol, redirect, proto),
  28. FW3_OPT("src_ip", network, redirect, ip_src),
  29. FW3_LIST("src_mac", mac, redirect, mac_src),
  30. FW3_OPT("src_port", port, redirect, port_src),
  31. FW3_OPT("src_dip", network, redirect, ip_dest),
  32. FW3_OPT("src_dport", port, redirect, port_dest),
  33. FW3_OPT("dest_ip", network, redirect, ip_redir),
  34. FW3_OPT("dest_port", port, redirect, port_redir),
  35. FW3_OPT("extra", string, redirect, extra),
  36. FW3_OPT("limit", limit, redirect, limit),
  37. FW3_OPT("limit_burst", int, redirect, limit.burst),
  38. FW3_OPT("utc_time", bool, redirect, time.utc),
  39. FW3_OPT("start_date", date, redirect, time.datestart),
  40. FW3_OPT("stop_date", date, redirect, time.datestop),
  41. FW3_OPT("start_time", time, redirect, time.timestart),
  42. FW3_OPT("stop_time", time, redirect, time.timestop),
  43. FW3_OPT("weekdays", weekdays, redirect, time.weekdays),
  44. FW3_OPT("monthdays", monthdays, redirect, time.monthdays),
  45. FW3_OPT("mark", mark, redirect, mark),
  46. FW3_OPT("reflection", bool, redirect, reflection),
  47. FW3_OPT("reflection_src", reflection_source,
  48. redirect, reflection_src),
  49. FW3_LIST("reflection_zone", device, redirect, reflection_zones),
  50. FW3_OPT("target", target, redirect, target),
  51. { }
  52. };
  53. static bool
  54. check_families(struct uci_element *e, struct fw3_redirect *r)
  55. {
  56. if (r->family == FW3_FAMILY_ANY)
  57. return true;
  58. if (r->_src && r->_src->family && r->_src->family != r->family)
  59. {
  60. warn_elem(e, "refers to source zone with different family");
  61. return false;
  62. }
  63. if (r->_dest && r->_dest->family && r->_dest->family != r->family)
  64. {
  65. warn_elem(e, "refers to destination zone with different family");
  66. return false;
  67. }
  68. if (r->ipset.ptr && r->ipset.ptr->family &&
  69. r->ipset.ptr->family != r->family)
  70. {
  71. warn_elem(e, "refers to ipset with different family");
  72. return false;
  73. }
  74. if (r->helper.ptr && r->helper.ptr->family &&
  75. r->helper.ptr->family != r->family)
  76. {
  77. warn_elem(e, "refers to CT helper not supporting family");
  78. return false;
  79. }
  80. if (r->ip_src.family && r->ip_src.family != r->family)
  81. {
  82. warn_elem(e, "uses source ip with different family");
  83. return false;
  84. }
  85. if (r->ip_dest.family && r->ip_dest.family != r->family)
  86. {
  87. warn_elem(e, "uses destination ip with different family");
  88. return false;
  89. }
  90. if (r->ip_redir.family && r->ip_redir.family != r->family)
  91. {
  92. warn_elem(e, "uses redirect ip with different family");
  93. return false;
  94. }
  95. return true;
  96. }
  97. static bool
  98. compare_addr(struct fw3_address *a, struct fw3_address *b)
  99. {
  100. if (a->family != FW3_FAMILY_V4 || b->family != FW3_FAMILY_V4)
  101. return false;
  102. return ((a->address.v4.s_addr & a->mask.v4.s_addr) ==
  103. (b->address.v4.s_addr & a->mask.v4.s_addr));
  104. }
  105. static bool
  106. resolve_dest(struct uci_element *e, struct fw3_redirect *redir,
  107. struct fw3_state *state)
  108. {
  109. struct fw3_zone *zone;
  110. struct fw3_address *addr;
  111. struct list_head *addrs;
  112. if (!redir->ip_redir.set)
  113. return false;
  114. list_for_each_entry(zone, &state->zones, list)
  115. {
  116. addrs = fw3_resolve_zone_addresses(zone, NULL);
  117. if (!addrs)
  118. continue;
  119. list_for_each_entry(addr, addrs, list)
  120. {
  121. if (!compare_addr(addr, &redir->ip_redir))
  122. continue;
  123. snprintf(redir->dest.name, sizeof(redir->dest.name), "%s", zone->name);
  124. redir->dest.set = true;
  125. redir->_dest = zone;
  126. break;
  127. }
  128. fw3_free_list(addrs);
  129. if (redir->_dest)
  130. return true;
  131. }
  132. return false;
  133. }
  134. static bool
  135. check_local(struct uci_element *e, struct fw3_redirect *redir,
  136. struct fw3_state *state)
  137. {
  138. if (redir->target != FW3_FLAG_DNAT)
  139. return false;
  140. if (!redir->ip_redir.set)
  141. redir->local = true;
  142. return redir->local;
  143. }
  144. static void
  145. select_helper(struct fw3_state *state, struct fw3_redirect *redir)
  146. {
  147. struct fw3_protocol *proto;
  148. struct fw3_cthelper *helper;
  149. int n_matches = 0;
  150. if (!state->defaults.auto_helper)
  151. return;
  152. if (!redir->_src || redir->target != FW3_FLAG_DNAT)
  153. return;
  154. if (!redir->port_redir.set || redir->port_redir.invert)
  155. return;
  156. if (redir->helper.set || redir->helper.ptr)
  157. return;
  158. if (list_empty(&redir->proto))
  159. return;
  160. list_for_each_entry(proto, &redir->proto, list)
  161. {
  162. helper = fw3_lookup_cthelper_by_proto_port(state, proto, &redir->port_redir);
  163. if (helper)
  164. n_matches++;
  165. }
  166. if (n_matches != 1)
  167. return;
  168. /* store pointer to auto-selected helper but set ".set" flag to false,
  169. * to allow later code to decide between configured or auto-selected
  170. * helpers */
  171. redir->helper.set = false;
  172. redir->helper.ptr = helper;
  173. set(redir->_src->flags, FW3_FAMILY_V4, FW3_FLAG_HELPER);
  174. }
  175. static bool
  176. check_redirect(struct fw3_state *state, struct fw3_redirect *redir, struct uci_element *e)
  177. {
  178. bool valid;
  179. if (!redir->enabled)
  180. return false;
  181. if (redir->src.invert)
  182. {
  183. warn_section("redirect", redir, e, "must not have an inverted source");
  184. return false;
  185. }
  186. else if (redir->src.set && !redir->src.any &&
  187. !(redir->_src = fw3_lookup_zone(state, redir->src.name)))
  188. {
  189. warn_section("redirect", redir, e, "refers to not existing zone '%s'",
  190. redir->src.name);
  191. return false;
  192. }
  193. else if (redir->dest.set && !redir->dest.any &&
  194. !(redir->_dest = fw3_lookup_zone(state, redir->dest.name)))
  195. {
  196. warn_section("redirect", redir, e, "refers to not existing zone '%s'",
  197. redir->dest.name);
  198. return false;
  199. }
  200. else if (redir->ipset.set && state->disable_ipsets)
  201. {
  202. warn_section("redirect", redir, e, "skipped due to disabled ipset support");
  203. return false;
  204. }
  205. else if (redir->ipset.set &&
  206. !(redir->ipset.ptr = fw3_lookup_ipset(state, redir->ipset.name)))
  207. {
  208. warn_section("redirect", redir, e, "refers to unknown ipset '%s'",
  209. redir->ipset.name);
  210. return false;
  211. }
  212. else if (redir->helper.set &&
  213. !(redir->helper.ptr = fw3_lookup_cthelper(state, redir->helper.name)))
  214. {
  215. warn_section("redirect", redir, e, "refers to unknown CT helper '%s'",
  216. redir->helper.name);
  217. return false;
  218. }
  219. if (!check_families(e, redir))
  220. return false;
  221. if (redir->target == FW3_FLAG_UNSPEC)
  222. {
  223. warn_section("redirect", redir, e, "has no target specified, defaulting to DNAT");
  224. redir->target = FW3_FLAG_DNAT;
  225. }
  226. else if (redir->target < FW3_FLAG_DNAT || redir->target > FW3_FLAG_SNAT)
  227. {
  228. warn_section("redirect", redir, e, "has invalid target specified, defaulting to DNAT");
  229. redir->target = FW3_FLAG_DNAT;
  230. }
  231. valid = false;
  232. if (redir->target == FW3_FLAG_DNAT)
  233. {
  234. if (redir->src.any)
  235. warn_section("redirect", redir, e, "must not have source '*' for DNAT target");
  236. else if (!redir->_src)
  237. warn_section("redirect", redir, e, "has no source specified");
  238. else if (redir->helper.invert)
  239. warn_section("redirect", redir, e, "must not use a negated helper match");
  240. else
  241. {
  242. set(redir->_src->flags, FW3_FAMILY_V4, redir->target);
  243. valid = true;
  244. if (!check_local(e, redir, state) && !redir->dest.set &&
  245. resolve_dest(e, redir, state))
  246. {
  247. warn_section("redirect", redir, e,
  248. "does not specify a destination, assuming '%s'",
  249. redir->dest.name);
  250. }
  251. if (redir->reflection && redir->_dest && redir->_src->masq)
  252. {
  253. set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_ACCEPT);
  254. set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_DNAT);
  255. set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_SNAT);
  256. }
  257. if (redir->helper.ptr)
  258. set(redir->_src->flags, FW3_FAMILY_V4, FW3_FLAG_HELPER);
  259. }
  260. }
  261. else
  262. {
  263. if (redir->dest.any)
  264. warn_section("redirect", redir, e,
  265. "must not have destination '*' for SNAT target");
  266. else if (!redir->_dest)
  267. warn_section("redirect", redir, e, "has no destination specified");
  268. else if (!redir->ip_dest.set)
  269. warn_section("redirect", redir, e, "has no src_dip option specified");
  270. else if (!list_empty(&redir->mac_src))
  271. warn_section("redirect", redir, e, "must not use 'src_mac' option for SNAT target");
  272. else if (redir->helper.set)
  273. warn_section("redirect", redir, e, "must not use 'helper' option for SNAT target");
  274. else
  275. {
  276. set(redir->_dest->flags, FW3_FAMILY_V4, redir->target);
  277. valid = true;
  278. }
  279. }
  280. if (list_empty(&redir->proto))
  281. {
  282. warn_section("redirect", redir, e, "does not specify a protocol, assuming TCP+UDP");
  283. fw3_parse_protocol(&redir->proto, "tcpudp", true);
  284. }
  285. if (!valid)
  286. return false;
  287. if (redir->target == FW3_FLAG_DNAT && !redir->port_redir.set)
  288. redir->port_redir = redir->port_dest;
  289. return true;
  290. }
  291. static struct fw3_redirect *
  292. fw3_alloc_redirect(struct fw3_state *state)
  293. {
  294. struct fw3_redirect *redir;
  295. redir = calloc(1, sizeof(*redir));
  296. if (!redir)
  297. return NULL;
  298. INIT_LIST_HEAD(&redir->proto);
  299. INIT_LIST_HEAD(&redir->mac_src);
  300. INIT_LIST_HEAD(&redir->reflection_zones);
  301. redir->enabled = true;
  302. redir->reflection = true;
  303. list_add_tail(&redir->list, &state->redirects);
  304. return redir;
  305. }
  306. void
  307. fw3_load_redirects(struct fw3_state *state, struct uci_package *p,
  308. struct blob_attr *a)
  309. {
  310. struct uci_section *s;
  311. struct uci_element *e;
  312. struct fw3_redirect *redir;
  313. struct blob_attr *entry;
  314. unsigned rem;
  315. INIT_LIST_HEAD(&state->redirects);
  316. blob_for_each_attr(entry, a, rem)
  317. {
  318. const char *type;
  319. const char *name = "ubus redirect";
  320. if (!fw3_attr_parse_name_type(entry, &name, &type))
  321. continue;
  322. if (strcmp(type, "redirect"))
  323. continue;
  324. redir = fw3_alloc_redirect(state);
  325. if (!redir)
  326. continue;
  327. if (!fw3_parse_blob_options(redir, fw3_redirect_opts, entry, name))
  328. {
  329. warn_section("redirect", redir, NULL, "skipped due to invalid options");
  330. fw3_free_redirect(redir);
  331. continue;
  332. }
  333. if (!check_redirect(state, redir, NULL)) {
  334. fw3_free_redirect(redir);
  335. continue;
  336. }
  337. select_helper(state, redir);
  338. }
  339. uci_foreach_element(&p->sections, e)
  340. {
  341. s = uci_to_section(e);
  342. if (strcmp(s->type, "redirect"))
  343. continue;
  344. redir = fw3_alloc_redirect(state);
  345. if (!redir)
  346. continue;
  347. if (!fw3_parse_options(redir, fw3_redirect_opts, s))
  348. {
  349. warn_elem(e, "skipped due to invalid options");
  350. fw3_free_redirect(redir);
  351. continue;
  352. }
  353. if (!check_redirect(state, redir, e)) {
  354. fw3_free_redirect(redir);
  355. continue;
  356. }
  357. select_helper(state, redir);
  358. }
  359. }
  360. static void
  361. append_chain_nat(struct fw3_ipt_rule *r, struct fw3_redirect *redir)
  362. {
  363. if (redir->target == FW3_FLAG_DNAT)
  364. fw3_ipt_rule_append(r, "zone_%s_prerouting", redir->src.name);
  365. else
  366. fw3_ipt_rule_append(r, "zone_%s_postrouting", redir->dest.name);
  367. }
  368. static void
  369. set_redirect(struct fw3_ipt_rule *r, struct fw3_port *port)
  370. {
  371. char buf[sizeof("65535-65535")];
  372. fw3_ipt_rule_target(r, "REDIRECT");
  373. if (port && port->set)
  374. {
  375. if (port->port_min == port->port_max)
  376. snprintf(buf, sizeof(buf), "%u", port->port_min);
  377. else
  378. snprintf(buf, sizeof(buf), "%u-%u", port->port_min, port->port_max);
  379. fw3_ipt_rule_addarg(r, false, "--to-ports", buf);
  380. }
  381. }
  382. static void
  383. set_snat_dnat(struct fw3_ipt_rule *r, enum fw3_flag target,
  384. struct fw3_address *addr, struct fw3_port *port)
  385. {
  386. char buf[sizeof("255.255.255.255:65535-65535")] = {};
  387. char ip[INET_ADDRSTRLEN], *p = buf;
  388. size_t rem = sizeof(buf);
  389. int len;
  390. if (addr && addr->set)
  391. {
  392. inet_ntop(AF_INET, &addr->address.v4, ip, sizeof(ip));
  393. len = snprintf(p, rem, "%s", ip);
  394. if (len < 0 || len >= rem)
  395. return;
  396. rem -= len;
  397. p += len;
  398. }
  399. if (port && port->set)
  400. {
  401. if (port->port_min == port->port_max)
  402. snprintf(p, rem, ":%u", port->port_min);
  403. else
  404. snprintf(p, rem, ":%u-%u", port->port_min, port->port_max);
  405. }
  406. if (target == FW3_FLAG_DNAT)
  407. {
  408. fw3_ipt_rule_target(r, "DNAT");
  409. fw3_ipt_rule_addarg(r, false, "--to-destination", buf);
  410. }
  411. else
  412. {
  413. fw3_ipt_rule_target(r, "SNAT");
  414. fw3_ipt_rule_addarg(r, false, "--to-source", buf);
  415. }
  416. }
  417. static void
  418. set_target_nat(struct fw3_ipt_rule *r, struct fw3_redirect *redir)
  419. {
  420. if (redir->local)
  421. set_redirect(r, &redir->port_redir);
  422. else if (redir->target == FW3_FLAG_DNAT)
  423. set_snat_dnat(r, redir->target, &redir->ip_redir, &redir->port_redir);
  424. else
  425. set_snat_dnat(r, redir->target, &redir->ip_dest, &redir->port_dest);
  426. }
  427. static void
  428. set_comment(struct fw3_ipt_rule *r, const char *name, int num, const char *suffix)
  429. {
  430. if (name)
  431. {
  432. if (suffix)
  433. fw3_ipt_rule_comment(r, "%s (%s)", name, suffix);
  434. else
  435. fw3_ipt_rule_comment(r, name);
  436. }
  437. else
  438. {
  439. if (suffix)
  440. fw3_ipt_rule_comment(r, "@redirect[%u] (%s)", num, suffix);
  441. else
  442. fw3_ipt_rule_comment(r, "@redirect[%u]", num);
  443. }
  444. }
  445. static void
  446. print_redirect(struct fw3_ipt_handle *h, struct fw3_state *state,
  447. struct fw3_redirect *redir, int num,
  448. struct fw3_protocol *proto, struct fw3_mac *mac)
  449. {
  450. struct fw3_ipt_rule *r;
  451. struct fw3_address *src, *dst;
  452. struct fw3_port *spt, *dpt;
  453. switch (h->table)
  454. {
  455. case FW3_TABLE_NAT:
  456. src = &redir->ip_src;
  457. dst = &redir->ip_dest;
  458. spt = &redir->port_src;
  459. dpt = &redir->port_dest;
  460. if (redir->target == FW3_FLAG_SNAT)
  461. {
  462. dst = &redir->ip_redir;
  463. dpt = &redir->port_redir;
  464. }
  465. r = fw3_ipt_rule_create(h, proto, NULL, NULL, src, dst);
  466. fw3_ipt_rule_sport_dport(r, spt, dpt);
  467. fw3_ipt_rule_mac(r, mac);
  468. fw3_ipt_rule_ipset(r, &redir->ipset);
  469. fw3_ipt_rule_helper(r, &redir->helper);
  470. fw3_ipt_rule_limit(r, &redir->limit);
  471. fw3_ipt_rule_time(r, &redir->time);
  472. fw3_ipt_rule_mark(r, &redir->mark);
  473. set_target_nat(r, redir);
  474. fw3_ipt_rule_extra(r, redir->extra);
  475. set_comment(r, redir->name, num, NULL);
  476. append_chain_nat(r, redir);
  477. break;
  478. case FW3_TABLE_RAW:
  479. if (redir->target == FW3_FLAG_DNAT && redir->helper.ptr)
  480. {
  481. if (!fw3_cthelper_check_proto(redir->helper.ptr, proto))
  482. {
  483. info(" ! Skipping protocol %s since helper '%s' does not support it",
  484. fw3_protoname(proto), redir->helper.ptr->name);
  485. return;
  486. }
  487. if (!redir->helper.set)
  488. info(" - Auto-selected conntrack helper '%s' based on proto/port",
  489. redir->helper.ptr->name);
  490. r = fw3_ipt_rule_create(h, proto, NULL, NULL, &redir->ip_src, &redir->ip_redir);
  491. fw3_ipt_rule_sport_dport(r, &redir->port_src, &redir->port_redir);
  492. fw3_ipt_rule_mac(r, mac);
  493. fw3_ipt_rule_ipset(r, &redir->ipset);
  494. fw3_ipt_rule_limit(r, &redir->limit);
  495. fw3_ipt_rule_time(r, &redir->time);
  496. fw3_ipt_rule_mark(r, &redir->mark);
  497. fw3_ipt_rule_addarg(r, false, "-m", "conntrack");
  498. fw3_ipt_rule_addarg(r, false, "--ctstate", "DNAT");
  499. fw3_ipt_rule_target(r, "CT");
  500. fw3_ipt_rule_addarg(r, false, "--helper", redir->helper.ptr->name);
  501. set_comment(r, redir->name, num, "CT helper");
  502. fw3_ipt_rule_append(r, "zone_%s_helper", redir->_src->name);
  503. }
  504. break;
  505. default:
  506. break;
  507. }
  508. }
  509. static void
  510. print_reflection(struct fw3_ipt_handle *h, struct fw3_state *state,
  511. struct fw3_redirect *redir, int num,
  512. struct fw3_protocol *proto, struct fw3_address *ra,
  513. struct fw3_address *ia, struct fw3_address *ea, struct fw3_device *rz)
  514. {
  515. struct fw3_ipt_rule *r;
  516. switch (h->table)
  517. {
  518. case FW3_TABLE_NAT:
  519. r = fw3_ipt_rule_create(h, proto, NULL, NULL, ia, ea);
  520. fw3_ipt_rule_sport_dport(r, NULL, &redir->port_dest);
  521. fw3_ipt_rule_limit(r, &redir->limit);
  522. fw3_ipt_rule_time(r, &redir->time);
  523. set_comment(r, redir->name, num, "reflection");
  524. set_snat_dnat(r, FW3_FLAG_DNAT, &redir->ip_redir, &redir->port_redir);
  525. fw3_ipt_rule_replace(r, "zone_%s_prerouting", rz->name);
  526. r = fw3_ipt_rule_create(h, proto, NULL, NULL, ia, &redir->ip_redir);
  527. fw3_ipt_rule_sport_dport(r, NULL, &redir->port_redir);
  528. fw3_ipt_rule_limit(r, &redir->limit);
  529. fw3_ipt_rule_time(r, &redir->time);
  530. set_comment(r, redir->name, num, "reflection");
  531. set_snat_dnat(r, FW3_FLAG_SNAT, ra, NULL);
  532. fw3_ipt_rule_replace(r, "zone_%s_postrouting", rz->name);
  533. break;
  534. default:
  535. break;
  536. }
  537. }
  538. static void
  539. expand_redirect(struct fw3_ipt_handle *handle, struct fw3_state *state,
  540. struct fw3_redirect *redir, int num)
  541. {
  542. struct list_head *ext_addrs, *int_addrs;
  543. struct fw3_address *ext_addr, *int_addr, ref_addr;
  544. struct fw3_protocol *proto;
  545. struct fw3_mac *mac;
  546. struct fw3_device *reflection_zone;
  547. struct fw3_zone *zone;
  548. if (redir->name)
  549. info(" * Redirect '%s'", redir->name);
  550. else
  551. info(" * Redirect #%u", num);
  552. if (!fw3_is_family(redir->_src, handle->family) ||
  553. !fw3_is_family(redir->_dest, handle->family))
  554. {
  555. info(" ! Skipping due to different family of zone");
  556. return;
  557. }
  558. if (!fw3_is_family(&redir->ip_src, handle->family) ||
  559. !fw3_is_family(&redir->ip_dest, handle->family) ||
  560. !fw3_is_family(&redir->ip_redir, handle->family))
  561. {
  562. if (!redir->ip_src.resolved ||
  563. !redir->ip_dest.resolved ||
  564. !redir->ip_redir.resolved)
  565. info(" ! Skipping due to different family of ip address");
  566. return;
  567. }
  568. if (redir->ipset.ptr)
  569. {
  570. if (!fw3_is_family(redir->ipset.ptr, handle->family))
  571. {
  572. info(" ! Skipping due to different family in ipset");
  573. return;
  574. }
  575. if (!fw3_check_ipset(redir->ipset.ptr))
  576. {
  577. info(" ! Skipping due to missing ipset '%s'",
  578. redir->ipset.ptr->external ?
  579. redir->ipset.ptr->external : redir->ipset.ptr->name);
  580. return;
  581. }
  582. set(redir->ipset.ptr->flags, handle->family, handle->family);
  583. }
  584. fw3_foreach(proto, &redir->proto)
  585. fw3_foreach(mac, &redir->mac_src)
  586. print_redirect(handle, state, redir, num, proto, mac);
  587. /* reflection rules */
  588. if (redir->target != FW3_FLAG_DNAT || !redir->reflection || redir->local)
  589. return;
  590. if (!redir->_dest || !redir->_src->masq)
  591. return;
  592. ext_addrs = fw3_resolve_zone_addresses(redir->_src, &redir->ip_dest);
  593. if (!ext_addrs)
  594. return;
  595. list_for_each_entry(ext_addr, ext_addrs, list)
  596. {
  597. if (!fw3_is_family(ext_addr, handle->family))
  598. continue;
  599. for (reflection_zone = list_empty(&redir->reflection_zones)
  600. ? &redir->dest
  601. : list_first_entry(&redir->reflection_zones, struct fw3_device, list);
  602. list_empty(&redir->reflection_zones)
  603. ? (reflection_zone == &redir->dest)
  604. : (&reflection_zone->list != &redir->reflection_zones);
  605. reflection_zone = list_empty(&redir->reflection_zones)
  606. ? NULL
  607. : list_entry(reflection_zone->list.next, struct fw3_device, list))
  608. {
  609. zone = fw3_lookup_zone(state, reflection_zone->name);
  610. if (!zone)
  611. continue;
  612. int_addrs = fw3_resolve_zone_addresses(zone, NULL);
  613. if (!int_addrs)
  614. continue;
  615. list_for_each_entry(int_addr, int_addrs, list)
  616. {
  617. if (!fw3_is_family(int_addr, handle->family))
  618. continue;
  619. fw3_foreach(proto, &redir->proto)
  620. {
  621. if (!proto)
  622. continue;
  623. if (redir->reflection_src == FW3_REFLECTION_INTERNAL)
  624. ref_addr = *int_addr;
  625. else
  626. ref_addr = *ext_addr;
  627. ref_addr.mask.v4.s_addr = 0xFFFFFFFF;
  628. ext_addr->mask.v4.s_addr = 0xFFFFFFFF;
  629. print_reflection(handle, state, redir, num, proto,
  630. &ref_addr, int_addr, ext_addr, reflection_zone);
  631. }
  632. }
  633. fw3_free_list(int_addrs);
  634. }
  635. }
  636. fw3_free_list(ext_addrs);
  637. }
  638. void
  639. fw3_print_redirects(struct fw3_ipt_handle *handle, struct fw3_state *state)
  640. {
  641. int num = 0;
  642. struct fw3_redirect *redir;
  643. if (handle->family == FW3_FAMILY_V6)
  644. return;
  645. if (handle->table != FW3_TABLE_FILTER &&
  646. handle->table != FW3_TABLE_NAT &&
  647. handle->table != FW3_TABLE_RAW)
  648. return;
  649. list_for_each_entry(redir, &state->redirects, list)
  650. {
  651. if (handle->table == FW3_TABLE_RAW && !redir->helper.ptr)
  652. continue;
  653. expand_redirect(handle, state, redir, num++);
  654. }
  655. }