ipsets.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. * firewall3 - 3rd OpenWrt UCI firewall implementation
  3. *
  4. * Copyright (C) 2013 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 <ctype.h>
  19. #include "ipsets.h"
  20. const struct fw3_option fw3_ipset_opts[] = {
  21. FW3_OPT("enabled", bool, ipset, enabled),
  22. FW3_OPT("reload_set", bool, ipset, reload_set),
  23. FW3_OPT("counters", bool, ipset, counters),
  24. FW3_OPT("comment", bool, ipset, comment),
  25. FW3_OPT("name", string, ipset, name),
  26. FW3_OPT("family", family, ipset, family),
  27. FW3_OPT("storage", ipset_method, ipset, method),
  28. FW3_LIST("match", ipset_datatype, ipset, datatypes),
  29. FW3_OPT("iprange", address, ipset, iprange),
  30. FW3_OPT("portrange", port, ipset, portrange),
  31. FW3_OPT("netmask", int, ipset, netmask),
  32. FW3_OPT("maxelem", int, ipset, maxelem),
  33. FW3_OPT("hashsize", int, ipset, hashsize),
  34. FW3_OPT("timeout", int, ipset, timeout),
  35. FW3_OPT("external", string, ipset, external),
  36. FW3_LIST("entry", setentry, ipset, entries),
  37. FW3_OPT("loadfile", string, ipset, loadfile),
  38. { }
  39. };
  40. #define T(m, t1, t2, t3, r, o) \
  41. { FW3_IPSET_METHOD_##m, \
  42. FW3_IPSET_TYPE_##t1 | (FW3_IPSET_TYPE_##t2 << 8) | (FW3_IPSET_TYPE_##t3 << 16), \
  43. r, o }
  44. enum ipset_optflag {
  45. OPT_IPRANGE = (1 << 0),
  46. OPT_PORTRANGE = (1 << 1),
  47. OPT_NETMASK = (1 << 2),
  48. OPT_HASHSIZE = (1 << 3),
  49. OPT_MAXELEM = (1 << 4),
  50. OPT_FAMILY = (1 << 5),
  51. };
  52. struct ipset_type {
  53. enum fw3_ipset_method method;
  54. uint32_t types;
  55. uint8_t required;
  56. uint8_t optional;
  57. };
  58. static struct ipset_type ipset_types[] = {
  59. T(BITMAP, IP, UNSPEC, UNSPEC, OPT_IPRANGE, OPT_NETMASK),
  60. T(BITMAP, IP, MAC, UNSPEC, OPT_IPRANGE, 0),
  61. T(BITMAP, PORT, UNSPEC, UNSPEC, OPT_PORTRANGE, 0),
  62. T(HASH, IP, UNSPEC, UNSPEC, 0,
  63. OPT_FAMILY | OPT_HASHSIZE | OPT_MAXELEM | OPT_NETMASK),
  64. T(HASH, NET, UNSPEC, UNSPEC, 0,
  65. OPT_FAMILY | OPT_HASHSIZE | OPT_MAXELEM),
  66. T(HASH, IP, PORT, UNSPEC, 0,
  67. OPT_FAMILY | OPT_HASHSIZE | OPT_MAXELEM),
  68. T(HASH, NET, PORT, UNSPEC, 0,
  69. OPT_FAMILY | OPT_HASHSIZE | OPT_MAXELEM),
  70. T(HASH, IP, PORT, IP, 0,
  71. OPT_FAMILY | OPT_HASHSIZE | OPT_MAXELEM),
  72. T(HASH, IP, PORT, NET, 0,
  73. OPT_FAMILY | OPT_HASHSIZE | OPT_MAXELEM),
  74. T(LIST, SET, UNSPEC, UNSPEC, 0, OPT_MAXELEM),
  75. };
  76. static bool
  77. check_types(struct uci_element *e, struct fw3_ipset *ipset)
  78. {
  79. int i = 0;
  80. uint32_t typelist = 0;
  81. struct fw3_ipset_datatype *type;
  82. list_for_each_entry(type, &ipset->datatypes, list)
  83. {
  84. if (i >= 3)
  85. {
  86. warn_section("ipset", ipset, e, "must not have more than 3 datatypes assigned");
  87. return false;
  88. }
  89. typelist |= (type->type << (i++ * 8));
  90. }
  91. /* find a suitable storage method if none specified */
  92. if (ipset->method == FW3_IPSET_METHOD_UNSPEC)
  93. {
  94. for (i = 0; i < ARRAY_SIZE(ipset_types); i++)
  95. {
  96. /* skip type for v6 if it does not support family */
  97. if (ipset->family != FW3_FAMILY_V4 &&
  98. !(ipset_types[i].optional & OPT_FAMILY))
  99. continue;
  100. if (ipset_types[i].types == typelist)
  101. {
  102. ipset->method = ipset_types[i].method;
  103. warn_section("ipset", ipset, e, "defines no storage method, assuming '%s'",
  104. fw3_ipset_method_names[ipset->method]);
  105. break;
  106. }
  107. }
  108. }
  109. //typelist |= ipset->method;
  110. for (i = 0; i < ARRAY_SIZE(ipset_types); i++)
  111. {
  112. if (ipset_types[i].method == ipset->method &&
  113. ipset_types[i].types == typelist)
  114. {
  115. if (!ipset->external)
  116. {
  117. if ((ipset_types[i].required & OPT_IPRANGE) &&
  118. !ipset->iprange.set)
  119. {
  120. warn_section("ipset", ipset, e, "requires an ip range");
  121. return false;
  122. }
  123. if ((ipset_types[i].required & OPT_PORTRANGE) &&
  124. !ipset->portrange.set)
  125. {
  126. warn_section("ipset", ipset, e, "requires a port range");
  127. return false;
  128. }
  129. if (!(ipset_types[i].required & OPT_IPRANGE) &&
  130. ipset->iprange.set)
  131. {
  132. warn_section("ipset", ipset, e, "iprange ignored");
  133. ipset->iprange.set = false;
  134. }
  135. if (!(ipset_types[i].required & OPT_PORTRANGE) &&
  136. ipset->portrange.set)
  137. {
  138. warn_section("ipset", ipset, e, "portrange ignored");
  139. ipset->portrange.set = false;
  140. }
  141. if (!(ipset_types[i].optional & OPT_NETMASK) &&
  142. ipset->netmask > 0)
  143. {
  144. warn_section("ipset", ipset, e, "netmask ignored");
  145. ipset->netmask = 0;
  146. }
  147. if (!(ipset_types[i].optional & OPT_HASHSIZE) &&
  148. ipset->hashsize > 0)
  149. {
  150. warn_section("ipset", ipset, e, "hashsize ignored");
  151. ipset->hashsize = 0;
  152. }
  153. if (!(ipset_types[i].optional & OPT_MAXELEM) &&
  154. ipset->maxelem > 0)
  155. {
  156. warn_section("ipset", ipset, e, "maxelem ignored");
  157. ipset->maxelem = 0;
  158. }
  159. if (!(ipset_types[i].optional & OPT_FAMILY) &&
  160. ipset->family != FW3_FAMILY_V4)
  161. {
  162. warn_section("ipset", ipset, e, "family ignored");
  163. ipset->family = FW3_FAMILY_V4;
  164. }
  165. }
  166. return true;
  167. }
  168. }
  169. warn_section("ipset", ipset, e, "has an invalid combination of storage method and matches");
  170. return false;
  171. }
  172. static bool
  173. check_ipset(struct fw3_state *state, struct fw3_ipset *ipset, struct uci_element *e)
  174. {
  175. if (!ipset->enabled) {
  176. return false;
  177. }
  178. if (ipset->external)
  179. {
  180. if (!*ipset->external)
  181. ipset->external = NULL;
  182. else if (!ipset->name)
  183. ipset->name = ipset->external;
  184. }
  185. if (!ipset->name || !*ipset->name)
  186. {
  187. warn_section("ipset", ipset, e, "ipset must have a name assigned");
  188. }
  189. //else if (fw3_lookup_ipset(state, ipset->name) != NULL)
  190. //{
  191. // warn_section("ipset", ipset, e, "has duplicated set name", ipset->name);
  192. //}
  193. else if (ipset->family == FW3_FAMILY_ANY)
  194. {
  195. warn_section("ipset", ipset, e, "must not have family 'any'");
  196. }
  197. else if (ipset->iprange.set && ipset->family != ipset->iprange.family)
  198. {
  199. warn_section("ipset", ipset, e, "has iprange of wrong address family");
  200. }
  201. else if (list_empty(&ipset->datatypes))
  202. {
  203. warn_section("ipset", ipset, e, "has no datatypes assigned");
  204. }
  205. else if (check_types(e, ipset))
  206. {
  207. return true;
  208. }
  209. return false;
  210. }
  211. static struct fw3_ipset *
  212. fw3_alloc_ipset(struct fw3_state *state)
  213. {
  214. struct fw3_ipset *ipset;
  215. ipset = calloc(1, sizeof(*ipset));
  216. if (!ipset)
  217. return NULL;
  218. INIT_LIST_HEAD(&ipset->datatypes);
  219. INIT_LIST_HEAD(&ipset->entries);
  220. ipset->comment = false;
  221. ipset->counters = false;
  222. ipset->enabled = true;
  223. ipset->family = FW3_FAMILY_V4;
  224. ipset->reload_set = false;
  225. ipset->timeout = -1; /* no timeout by default */
  226. list_add_tail(&ipset->list, &state->ipsets);
  227. return ipset;
  228. }
  229. void
  230. fw3_load_ipsets(struct fw3_state *state, struct uci_package *p,
  231. struct blob_attr *a)
  232. {
  233. struct uci_section *s;
  234. struct uci_element *e;
  235. struct fw3_ipset *ipset;
  236. struct blob_attr *entry;
  237. unsigned rem;
  238. INIT_LIST_HEAD(&state->ipsets);
  239. if (state->disable_ipsets)
  240. return;
  241. blob_for_each_attr(entry, a, rem)
  242. {
  243. const char *type;
  244. const char *name = "ubus ipset";
  245. if (!fw3_attr_parse_name_type(entry, &name, &type))
  246. continue;
  247. if (strcmp(type, "ipset"))
  248. continue;
  249. ipset = fw3_alloc_ipset(state);
  250. if (!ipset)
  251. continue;
  252. if (!fw3_parse_blob_options(ipset, fw3_ipset_opts, entry, name))
  253. {
  254. warn_section("ipset", ipset, NULL, "skipped due to invalid options");
  255. fw3_free_ipset(ipset);
  256. continue;
  257. }
  258. if (!check_ipset(state, ipset, NULL))
  259. fw3_free_ipset(ipset);
  260. }
  261. uci_foreach_element(&p->sections, e)
  262. {
  263. s = uci_to_section(e);
  264. if (strcmp(s->type, "ipset"))
  265. continue;
  266. ipset = fw3_alloc_ipset(state);
  267. if (!ipset)
  268. continue;
  269. if (!fw3_parse_options(ipset, fw3_ipset_opts, s))
  270. warn_elem(e, "has invalid options");
  271. if (!check_ipset(state, ipset, e))
  272. fw3_free_ipset(ipset);
  273. }
  274. }
  275. static void
  276. load_file(struct fw3_ipset *ipset)
  277. {
  278. FILE *f;
  279. char line[128];
  280. char *p;
  281. if (!ipset->loadfile)
  282. return;
  283. info(" * Loading file %s", ipset->loadfile);
  284. f = fopen(ipset->loadfile, "r");
  285. if (!f) {
  286. info(" ! Skipping due to open error: %s", strerror(errno));
  287. return;
  288. }
  289. while (fgets(line, sizeof(line), f)) {
  290. p = line;
  291. while (isspace(*p))
  292. p++;
  293. if (*p && *p != '#')
  294. fw3_pr("add %s %s", ipset->name, line);
  295. }
  296. fclose(f);
  297. }
  298. static void
  299. create_ipset(struct fw3_ipset *ipset, struct fw3_state *state)
  300. {
  301. bool first = true;
  302. struct fw3_setentry *entry;
  303. struct fw3_ipset_datatype *type;
  304. info(" * Creating ipset %s", ipset->name);
  305. first = true;
  306. fw3_pr("create %s %s", ipset->name, fw3_ipset_method_names[ipset->method]);
  307. list_for_each_entry(type, &ipset->datatypes, list)
  308. {
  309. fw3_pr("%c%s", first ? ':' : ',', fw3_ipset_type_names[type->type]);
  310. first = false;
  311. }
  312. if (ipset->method == FW3_IPSET_METHOD_HASH)
  313. fw3_pr(" family inet%s", (ipset->family == FW3_FAMILY_V4) ? "" : "6");
  314. if (ipset->iprange.set)
  315. {
  316. fw3_pr(" range %s", fw3_address_to_string(&ipset->iprange, false, true));
  317. }
  318. else if (ipset->portrange.set)
  319. {
  320. fw3_pr(" range %u-%u",
  321. ipset->portrange.port_min, ipset->portrange.port_max);
  322. }
  323. if (ipset->timeout >= 0)
  324. fw3_pr(" timeout %u", ipset->timeout);
  325. if (ipset->maxelem > 0)
  326. fw3_pr(" maxelem %u", ipset->maxelem);
  327. if (ipset->netmask > 0)
  328. fw3_pr(" netmask %u", ipset->netmask);
  329. if (ipset->hashsize > 0)
  330. fw3_pr(" hashsize %u", ipset->hashsize);
  331. if (ipset->counters)
  332. fw3_pr(" counters");
  333. if (ipset->comment)
  334. fw3_pr(" comment");
  335. fw3_pr("\n");
  336. list_for_each_entry(entry, &ipset->entries, list)
  337. fw3_pr("add %s %s\n", ipset->name, entry->value);
  338. load_file(ipset);
  339. }
  340. void
  341. fw3_create_ipsets(struct fw3_state *state, enum fw3_family family,
  342. bool reload_set)
  343. {
  344. unsigned int delay, tries;
  345. bool exec = false;
  346. struct fw3_ipset *ipset;
  347. if (state->disable_ipsets)
  348. return;
  349. /* spawn ipsets */
  350. list_for_each_entry(ipset, &state->ipsets, list)
  351. {
  352. if (ipset->family != family)
  353. continue;
  354. if (ipset->external)
  355. continue;
  356. if (fw3_check_ipset(ipset) &&
  357. (reload_set && !ipset->reload_set))
  358. continue;
  359. if (!exec)
  360. {
  361. exec = fw3_command_pipe(false, "ipset", "-exist", "-");
  362. if (!exec)
  363. return;
  364. }
  365. create_ipset(ipset, state);
  366. }
  367. if (exec)
  368. {
  369. fw3_pr("quit\n");
  370. fw3_command_close();
  371. }
  372. /* wait a little expontially for ipsets to appear */
  373. list_for_each_entry(ipset, &state->ipsets, list)
  374. {
  375. if (ipset->external)
  376. continue;
  377. delay = 5;
  378. for (tries = 0; !fw3_check_ipset(ipset) && tries < 10; tries++)
  379. usleep(delay<<1);
  380. }
  381. }
  382. void
  383. fw3_destroy_ipsets(struct fw3_state *state, enum fw3_family family,
  384. bool reload_set)
  385. {
  386. unsigned int delay, tries;
  387. bool exec = false;
  388. struct fw3_ipset *ipset;
  389. if (state->disable_ipsets)
  390. return;
  391. /* destroy ipsets */
  392. list_for_each_entry(ipset, &state->ipsets, list)
  393. {
  394. if (ipset->family != family ||
  395. (reload_set && !ipset->reload_set))
  396. continue;
  397. if (!exec)
  398. {
  399. exec = fw3_command_pipe(false, "ipset", "-exist", "-");
  400. if (!exec)
  401. return;
  402. }
  403. info(" * Deleting ipset %s", ipset->name);
  404. fw3_pr("flush %s\n", ipset->name);
  405. fw3_pr("destroy %s\n", ipset->name);
  406. }
  407. if (exec)
  408. {
  409. fw3_pr("quit\n");
  410. fw3_command_close();
  411. }
  412. /* wait for ipsets to disappear */
  413. list_for_each_entry(ipset, &state->ipsets, list)
  414. {
  415. if (ipset->external)
  416. continue;
  417. delay = 5;
  418. for (tries = 0; fw3_check_ipset(ipset) && tries < 10; tries++)
  419. usleep(delay<<1);
  420. }
  421. }
  422. struct fw3_ipset *
  423. fw3_lookup_ipset(struct fw3_state *state, const char *name)
  424. {
  425. struct fw3_ipset *s;
  426. if (list_empty(&state->ipsets))
  427. return NULL;
  428. list_for_each_entry(s, &state->ipsets, list)
  429. {
  430. if (strcmp(s->name, name))
  431. continue;
  432. return s;
  433. }
  434. return NULL;
  435. }
  436. bool
  437. fw3_check_ipset(struct fw3_ipset *set)
  438. {
  439. bool rv = false;
  440. socklen_t sz;
  441. int s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
  442. struct ip_set_req_version req_ver;
  443. struct ip_set_req_get_set req_name;
  444. if (s < 0 || fcntl(s, F_SETFD, FD_CLOEXEC))
  445. goto out;
  446. sz = sizeof(req_ver);
  447. req_ver.op = IP_SET_OP_VERSION;
  448. if (getsockopt(s, SOL_IP, SO_IP_SET, &req_ver, &sz))
  449. goto out;
  450. sz = sizeof(req_name);
  451. req_name.op = IP_SET_OP_GET_BYNAME;
  452. req_name.version = req_ver.version;
  453. snprintf(req_name.set.name, IPSET_MAXNAMELEN - 1, "%s",
  454. set->external ? set->external : set->name);
  455. if (getsockopt(s, SOL_IP, SO_IP_SET, &req_name, &sz))
  456. goto out;
  457. rv = ((sz == sizeof(req_name)) && (req_name.set.index != IPSET_INVALID_ID));
  458. out:
  459. if (s >= 0)
  460. close(s);
  461. return rv;
  462. }
  463. void
  464. fw3_ipsets_update_run_state(enum fw3_family family, struct fw3_state *run_state,
  465. struct fw3_state *cfg_state)
  466. {
  467. struct fw3_ipset *ipset_run, *ipset_cfg;
  468. bool in_cfg;
  469. list_for_each_entry(ipset_run, &run_state->ipsets, list) {
  470. if (ipset_run->family != family)
  471. continue;
  472. in_cfg = false;
  473. list_for_each_entry(ipset_cfg, &cfg_state->ipsets, list) {
  474. if (ipset_cfg->family != family)
  475. continue;
  476. if (strlen(ipset_run->name) ==
  477. strlen(ipset_cfg->name) &&
  478. !strcmp(ipset_run->name, ipset_cfg->name)) {
  479. in_cfg = true;
  480. break;
  481. }
  482. }
  483. /* If a set is found in run_state, but not in cfg_state then the
  484. * set has been deleted/renamed. Set reload_set to true to force
  485. * the old set to be destroyed in the "stop" fase of the reload.
  486. * If the set is found, then copy the reload_set value from the
  487. * configuration state. This ensures that the elements are
  488. * always updated according to the configuration, and not the
  489. * runtime state (which the user might have forgotten).
  490. */
  491. if (!in_cfg)
  492. ipset_run->reload_set = true;
  493. else
  494. ipset_run->reload_set = ipset_cfg->reload_set;
  495. }
  496. }