snats.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * firewall3 - 3rd OpenWrt UCI firewall implementation
  3. *
  4. * Copyright (C) 2014 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 "snats.h"
  19. const struct fw3_option fw3_snat_opts[] = {
  20. FW3_OPT("enabled", bool, snat, enabled),
  21. FW3_OPT("name", string, snat, name),
  22. FW3_OPT("family", family, snat, family),
  23. FW3_OPT("src", device, snat, src),
  24. FW3_OPT("device", string, snat, device),
  25. FW3_OPT("ipset", setmatch, snat, ipset),
  26. FW3_LIST("proto", protocol, snat, proto),
  27. FW3_OPT("src_ip", network, snat, ip_src),
  28. FW3_OPT("src_port", port, snat, port_src),
  29. FW3_OPT("snat_ip", network, snat, ip_snat),
  30. FW3_OPT("snat_port", port, snat, port_snat),
  31. FW3_OPT("dest_ip", network, snat, ip_dest),
  32. FW3_OPT("dest_port", port, snat, port_dest),
  33. FW3_OPT("extra", string, snat, extra),
  34. FW3_OPT("limit", limit, snat, limit),
  35. FW3_OPT("limit_burst", int, snat, limit.burst),
  36. FW3_OPT("connlimit_ports", bool, snat, connlimit_ports),
  37. FW3_OPT("utc_time", bool, snat, time.utc),
  38. FW3_OPT("start_date", date, snat, time.datestart),
  39. FW3_OPT("stop_date", date, snat, time.datestop),
  40. FW3_OPT("start_time", time, snat, time.timestart),
  41. FW3_OPT("stop_time", time, snat, time.timestop),
  42. FW3_OPT("weekdays", weekdays, snat, time.weekdays),
  43. FW3_OPT("monthdays", monthdays, snat, time.monthdays),
  44. FW3_OPT("mark", mark, snat, mark),
  45. FW3_OPT("target", target, snat, target),
  46. { }
  47. };
  48. static bool
  49. check_families(struct uci_element *e, struct fw3_snat *r)
  50. {
  51. if (r->family == FW3_FAMILY_ANY)
  52. return true;
  53. if (r->_src && r->_src->family && r->_src->family != r->family)
  54. {
  55. warn_section("nat", r, e, "refers to source zone with different family");
  56. return false;
  57. }
  58. if (r->ipset.ptr && r->ipset.ptr->family &&
  59. r->ipset.ptr->family != r->family)
  60. {
  61. warn_section("nat", r, e, "refers to ipset with different family");
  62. return false;
  63. }
  64. if (r->ip_src.family && r->ip_src.family != r->family)
  65. {
  66. warn_section("nat", r, e, "uses source ip with different family");
  67. return false;
  68. }
  69. if (r->ip_dest.family && r->ip_dest.family != r->family)
  70. {
  71. warn_section("nat", r, e, "uses destination ip with different family");
  72. return false;
  73. }
  74. if (r->ip_snat.family && r->ip_snat.family != r->family)
  75. {
  76. warn_section("nat", r, e, "uses snat ip with different family");
  77. return false;
  78. }
  79. return true;
  80. }
  81. static struct fw3_snat*
  82. alloc_snat(struct fw3_state *state)
  83. {
  84. struct fw3_snat *snat = calloc(1, sizeof(*snat));
  85. if (snat) {
  86. INIT_LIST_HEAD(&snat->proto);
  87. list_add_tail(&snat->list, &state->snats);
  88. snat->enabled = true;
  89. }
  90. return snat;
  91. }
  92. static bool
  93. check_snat(struct fw3_state *state, struct fw3_snat *snat, struct uci_element *e)
  94. {
  95. if (!snat->enabled)
  96. return false;
  97. if (snat->src.invert)
  98. {
  99. warn_section("nat", snat, e, "must not have an inverted source");
  100. return false;
  101. }
  102. else if (snat->src.set && !snat->src.any &&
  103. !(snat->_src = fw3_lookup_zone(state, snat->src.name)))
  104. {
  105. warn_section("nat", snat, e, "refers to not existing zone '%s'", snat->src.name);
  106. return false;
  107. }
  108. else if (snat->ipset.set && state->disable_ipsets)
  109. {
  110. warn_section("nat", snat, e, "skipped due to disabled ipset support");
  111. return false;
  112. }
  113. else if (snat->ipset.set &&
  114. !(snat->ipset.ptr = fw3_lookup_ipset(state, snat->ipset.name)))
  115. {
  116. warn_section("nat", snat, e, "refers to unknown ipset '%s'", snat->ipset.name);
  117. return false;
  118. }
  119. if (!check_families(e, snat))
  120. return false;
  121. if (snat->target == FW3_FLAG_UNSPEC)
  122. {
  123. warn_section("nat", snat, e, "has no target specified, defaulting to MASQUERADE");
  124. snat->target = FW3_FLAG_MASQUERADE;
  125. }
  126. else if (snat->target != FW3_FLAG_ACCEPT && snat->target != FW3_FLAG_SNAT &&
  127. snat->target != FW3_FLAG_MASQUERADE)
  128. {
  129. warn_section("nat", snat, e, "has invalid target specified, defaulting to MASQUERADE");
  130. snat->target = FW3_FLAG_MASQUERADE;
  131. }
  132. if (snat->target == FW3_FLAG_SNAT &&
  133. !snat->ip_snat.set && !snat->port_snat.set)
  134. {
  135. warn_section("nat", snat, e, "needs either 'snat_ip' or 'snat_port' for SNAT");
  136. return false;
  137. }
  138. else if (snat->target != FW3_FLAG_SNAT && snat->ip_snat.set)
  139. {
  140. warn_section("nat", snat, e, "must not use 'snat_ip' for non-SNAT");
  141. return false;
  142. }
  143. else if (snat->target != FW3_FLAG_SNAT && snat->port_snat.set)
  144. {
  145. warn_section("nat", snat, e, "must not use 'snat_port' for non-SNAT");
  146. return false;
  147. }
  148. if (list_empty(&snat->proto))
  149. {
  150. warn_section("nat", snat, e, "does not specify a protocol, assuming all");
  151. fw3_parse_protocol(&snat->proto, "all", true);
  152. }
  153. if (snat->_src)
  154. set(snat->_src->flags, FW3_FAMILY_V4, FW3_FLAG_SNAT);
  155. return true;
  156. }
  157. void
  158. fw3_load_snats(struct fw3_state *state, struct uci_package *p, struct blob_attr *a)
  159. {
  160. struct uci_section *s;
  161. struct uci_element *e;
  162. struct fw3_snat *snat;
  163. struct blob_attr *entry;
  164. unsigned rem;
  165. INIT_LIST_HEAD(&state->snats);
  166. blob_for_each_attr(entry, a, rem) {
  167. const char *type = NULL;
  168. const char *name = "ubus rule";
  169. if (!fw3_attr_parse_name_type(entry, &name, &type))
  170. continue;
  171. if (strcmp(type, "nat"))
  172. continue;
  173. snat = alloc_snat(state);
  174. if (!snat)
  175. continue;
  176. if (!fw3_parse_blob_options(snat, fw3_snat_opts, entry, name))
  177. {
  178. warn_section("nat", snat, NULL, "skipped due to invalid options");
  179. fw3_free_snat(snat);
  180. continue;
  181. }
  182. if (!check_snat(state, snat, NULL))
  183. fw3_free_snat(snat);
  184. }
  185. uci_foreach_element(&p->sections, e)
  186. {
  187. s = uci_to_section(e);
  188. if (strcmp(s->type, "nat"))
  189. continue;
  190. snat = alloc_snat(state);
  191. if (!snat)
  192. continue;
  193. if (!fw3_parse_options(snat, fw3_snat_opts, s))
  194. {
  195. warn_elem(e, "skipped due to invalid options");
  196. fw3_free_snat(snat);
  197. continue;
  198. }
  199. if (!check_snat(state, snat, e))
  200. fw3_free_snat(snat);
  201. }
  202. }
  203. static void
  204. append_chain(struct fw3_ipt_rule *r, struct fw3_snat *snat)
  205. {
  206. if (snat->_src)
  207. fw3_ipt_rule_append(r, "zone_%s_postrouting", snat->src.name);
  208. else
  209. fw3_ipt_rule_append(r, "POSTROUTING");
  210. }
  211. static void
  212. set_target(struct fw3_ipt_rule *r, struct fw3_snat *snat,
  213. struct fw3_protocol *proto)
  214. {
  215. char buf[sizeof("255.255.255.255:65535-65535\0")];
  216. if (snat->target == FW3_FLAG_SNAT)
  217. {
  218. buf[0] = '\0';
  219. if (snat->ip_snat.set)
  220. {
  221. inet_ntop(AF_INET, &snat->ip_snat.address.v4, buf, sizeof(buf));
  222. }
  223. if (snat->port_snat.set && proto && !proto->any &&
  224. (proto->protocol == 6 || proto->protocol == 17 || proto->protocol == 1))
  225. {
  226. if (snat->port_snat.port_min == snat->port_snat.port_max)
  227. sprintf(buf + strlen(buf), ":%u", snat->port_snat.port_min);
  228. else
  229. sprintf(buf + strlen(buf), ":%u-%u",
  230. snat->port_snat.port_min, snat->port_snat.port_max);
  231. if (snat->connlimit_ports) {
  232. char portcntbuf[6];
  233. snprintf(portcntbuf, sizeof(portcntbuf), "%u",
  234. 1 + snat->port_snat.port_max - snat->port_snat.port_min);
  235. fw3_ipt_rule_addarg(r, false, "-m", "connlimit");
  236. fw3_ipt_rule_addarg(r, false, "--connlimit-daddr", NULL);
  237. fw3_ipt_rule_addarg(r, false, "--connlimit-upto", portcntbuf);
  238. }
  239. }
  240. fw3_ipt_rule_target(r, "SNAT");
  241. fw3_ipt_rule_addarg(r, false, "--to-source", buf);
  242. }
  243. else if (snat->target == FW3_FLAG_ACCEPT)
  244. {
  245. fw3_ipt_rule_target(r, "ACCEPT");
  246. }
  247. else
  248. {
  249. fw3_ipt_rule_target(r, "MASQUERADE");
  250. }
  251. }
  252. static void
  253. set_comment(struct fw3_ipt_rule *r, const char *name, int num)
  254. {
  255. if (name)
  256. fw3_ipt_rule_comment(r, name);
  257. else
  258. fw3_ipt_rule_comment(r, "@nat[%u]", num);
  259. }
  260. static void
  261. print_snat(struct fw3_ipt_handle *h, struct fw3_state *state,
  262. struct fw3_snat *snat, int num, struct fw3_protocol *proto)
  263. {
  264. struct fw3_ipt_rule *r;
  265. struct fw3_address *src, *dst;
  266. struct fw3_port *spt, *dpt;
  267. switch (h->table)
  268. {
  269. case FW3_TABLE_NAT:
  270. src = &snat->ip_src;
  271. dst = &snat->ip_dest;
  272. spt = &snat->port_src;
  273. dpt = &snat->port_dest;
  274. r = fw3_ipt_rule_create(h, proto, NULL, NULL, src, dst);
  275. fw3_ipt_rule_sport_dport(r, spt, dpt);
  276. fw3_ipt_rule_device(r, snat->device, true);
  277. fw3_ipt_rule_ipset(r, &snat->ipset);
  278. fw3_ipt_rule_limit(r, &snat->limit);
  279. fw3_ipt_rule_time(r, &snat->time);
  280. fw3_ipt_rule_mark(r, &snat->mark);
  281. set_target(r, snat, proto);
  282. fw3_ipt_rule_extra(r, snat->extra);
  283. set_comment(r, snat->name, num);
  284. append_chain(r, snat);
  285. break;
  286. default:
  287. break;
  288. }
  289. }
  290. static void
  291. expand_snat(struct fw3_ipt_handle *handle, struct fw3_state *state,
  292. struct fw3_snat *snat, int num)
  293. {
  294. struct fw3_protocol *proto;
  295. if (snat->name)
  296. info(" * NAT '%s'", snat->name);
  297. else
  298. info(" * NAT #%u", num);
  299. if (!fw3_is_family(snat->_src, handle->family))
  300. {
  301. info(" ! Skipping due to different family of zone");
  302. return;
  303. }
  304. if (!fw3_is_family(&snat->ip_src, handle->family) ||
  305. !fw3_is_family(&snat->ip_dest, handle->family) ||
  306. !fw3_is_family(&snat->ip_snat, handle->family))
  307. {
  308. if (!snat->ip_src.resolved ||
  309. !snat->ip_dest.resolved ||
  310. !snat->ip_snat.resolved)
  311. info(" ! Skipping due to different family of ip address");
  312. return;
  313. }
  314. if (snat->ipset.ptr)
  315. {
  316. if (!fw3_is_family(snat->ipset.ptr, handle->family))
  317. {
  318. info(" ! Skipping due to different family in ipset");
  319. return;
  320. }
  321. if (!fw3_check_ipset(snat->ipset.ptr))
  322. {
  323. info(" ! Skipping due to missing ipset '%s'",
  324. snat->ipset.ptr->external ?
  325. snat->ipset.ptr->external : snat->ipset.ptr->name);
  326. return;
  327. }
  328. set(snat->ipset.ptr->flags, handle->family, handle->family);
  329. }
  330. fw3_foreach(proto, &snat->proto)
  331. print_snat(handle, state, snat, num, proto);
  332. }
  333. void
  334. fw3_print_snats(struct fw3_ipt_handle *handle, struct fw3_state *state)
  335. {
  336. int num = 0;
  337. struct fw3_snat *snat;
  338. if (handle->family == FW3_FAMILY_V6)
  339. return;
  340. if (handle->table != FW3_TABLE_NAT)
  341. return;
  342. list_for_each_entry(snat, &state->snats, list)
  343. expand_snat(handle, state, snat, num++);
  344. }