ipsets.c 14 KB

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