ipsets.c 12 KB

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