snats.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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")] = {};
  216. char ip[INET_ADDRSTRLEN], portcntbuf[6], *p = buf;
  217. size_t rem = sizeof(buf);
  218. int len;
  219. if (snat->target == FW3_FLAG_SNAT)
  220. {
  221. if (snat->ip_snat.set)
  222. {
  223. inet_ntop(AF_INET, &snat->ip_snat.address.v4, ip, sizeof(ip));
  224. len = snprintf(p, rem, "%s", ip);
  225. if (len < 0 || len >= rem)
  226. return;
  227. rem -= len;
  228. p += len;
  229. }
  230. if (snat->port_snat.set && proto && !proto->any &&
  231. (proto->protocol == 6 || proto->protocol == 17 || proto->protocol == 1))
  232. {
  233. if (snat->port_snat.port_min == snat->port_snat.port_max)
  234. snprintf(p, rem, ":%u", snat->port_snat.port_min);
  235. else
  236. snprintf(p, rem, ":%u-%u",
  237. snat->port_snat.port_min, snat->port_snat.port_max);
  238. if (snat->connlimit_ports) {
  239. snprintf(portcntbuf, sizeof(portcntbuf), "%u",
  240. 1 + snat->port_snat.port_max - snat->port_snat.port_min);
  241. fw3_ipt_rule_addarg(r, false, "-m", "connlimit");
  242. fw3_ipt_rule_addarg(r, false, "--connlimit-daddr", NULL);
  243. fw3_ipt_rule_addarg(r, false, "--connlimit-upto", portcntbuf);
  244. }
  245. }
  246. fw3_ipt_rule_target(r, "SNAT");
  247. fw3_ipt_rule_addarg(r, false, "--to-source", buf);
  248. }
  249. else if (snat->target == FW3_FLAG_ACCEPT)
  250. {
  251. fw3_ipt_rule_target(r, "ACCEPT");
  252. }
  253. else
  254. {
  255. fw3_ipt_rule_target(r, "MASQUERADE");
  256. }
  257. }
  258. static void
  259. set_comment(struct fw3_ipt_rule *r, const char *name, int num)
  260. {
  261. if (name)
  262. fw3_ipt_rule_comment(r, name);
  263. else
  264. fw3_ipt_rule_comment(r, "@nat[%u]", num);
  265. }
  266. static void
  267. print_snat(struct fw3_ipt_handle *h, struct fw3_state *state,
  268. struct fw3_snat *snat, int num, struct fw3_protocol *proto)
  269. {
  270. struct fw3_ipt_rule *r;
  271. struct fw3_address *src, *dst;
  272. struct fw3_port *spt, *dpt;
  273. switch (h->table)
  274. {
  275. case FW3_TABLE_NAT:
  276. src = &snat->ip_src;
  277. dst = &snat->ip_dest;
  278. spt = &snat->port_src;
  279. dpt = &snat->port_dest;
  280. r = fw3_ipt_rule_create(h, proto, NULL, NULL, src, dst);
  281. fw3_ipt_rule_sport_dport(r, spt, dpt);
  282. fw3_ipt_rule_device(r, snat->device, true);
  283. fw3_ipt_rule_ipset(r, &snat->ipset);
  284. fw3_ipt_rule_limit(r, &snat->limit);
  285. fw3_ipt_rule_time(r, &snat->time);
  286. fw3_ipt_rule_mark(r, &snat->mark);
  287. set_target(r, snat, proto);
  288. fw3_ipt_rule_extra(r, snat->extra);
  289. set_comment(r, snat->name, num);
  290. append_chain(r, snat);
  291. break;
  292. default:
  293. break;
  294. }
  295. }
  296. static void
  297. expand_snat(struct fw3_ipt_handle *handle, struct fw3_state *state,
  298. struct fw3_snat *snat, int num)
  299. {
  300. struct fw3_protocol *proto;
  301. if (snat->name)
  302. info(" * NAT '%s'", snat->name);
  303. else
  304. info(" * NAT #%u", num);
  305. if (!fw3_is_family(snat->_src, handle->family))
  306. {
  307. info(" ! Skipping due to different family of zone");
  308. return;
  309. }
  310. if (!fw3_is_family(&snat->ip_src, handle->family) ||
  311. !fw3_is_family(&snat->ip_dest, handle->family) ||
  312. !fw3_is_family(&snat->ip_snat, handle->family))
  313. {
  314. if (!snat->ip_src.resolved ||
  315. !snat->ip_dest.resolved ||
  316. !snat->ip_snat.resolved)
  317. info(" ! Skipping due to different family of ip address");
  318. return;
  319. }
  320. if (snat->ipset.ptr)
  321. {
  322. if (!fw3_is_family(snat->ipset.ptr, handle->family))
  323. {
  324. info(" ! Skipping due to different family in ipset");
  325. return;
  326. }
  327. if (!fw3_check_ipset(snat->ipset.ptr))
  328. {
  329. info(" ! Skipping due to missing ipset '%s'",
  330. snat->ipset.ptr->external ?
  331. snat->ipset.ptr->external : snat->ipset.ptr->name);
  332. return;
  333. }
  334. set(snat->ipset.ptr->flags, handle->family, handle->family);
  335. }
  336. fw3_foreach(proto, &snat->proto)
  337. print_snat(handle, state, snat, num, proto);
  338. }
  339. void
  340. fw3_print_snats(struct fw3_ipt_handle *handle, struct fw3_state *state)
  341. {
  342. int num = 0;
  343. struct fw3_snat *snat;
  344. if (handle->family == FW3_FAMILY_V6)
  345. return;
  346. if (handle->table != FW3_TABLE_NAT)
  347. return;
  348. list_for_each_entry(snat, &state->snats, list)
  349. expand_snat(handle, state, snat, num++);
  350. }