2 * firewall3 - 3rd OpenWrt UCI firewall implementation
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
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.
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.
22 const struct fw3_option fw3_ipset_opts[] = {
23 FW3_OPT("enabled", bool, ipset, enabled),
25 FW3_OPT("name", string, ipset, name),
26 FW3_OPT("family", family, ipset, family),
28 FW3_OPT("storage", ipset_method, ipset, method),
29 FW3_LIST("match", ipset_datatype, ipset, datatypes),
31 FW3_OPT("iprange", address, ipset, iprange),
32 FW3_OPT("portrange", port, ipset, portrange),
34 FW3_OPT("netmask", int, ipset, netmask),
35 FW3_OPT("maxelem", int, ipset, maxelem),
36 FW3_OPT("hashsize", int, ipset, hashsize),
37 FW3_OPT("timeout", int, ipset, timeout),
39 FW3_OPT("external", string, ipset, external),
44 #define T(m, t1, t2, t3, r, o) \
45 { FW3_IPSET_METHOD_##m, \
46 FW3_IPSET_TYPE_##t1 | (FW3_IPSET_TYPE_##t2 << 8) | (FW3_IPSET_TYPE_##t3 << 16), \
50 OPT_IPRANGE = (1 << 0),
51 OPT_PORTRANGE = (1 << 1),
52 OPT_NETMASK = (1 << 2),
53 OPT_HASHSIZE = (1 << 3),
54 OPT_MAXELEM = (1 << 4),
55 OPT_FAMILY = (1 << 5),
59 enum fw3_ipset_method method;
65 static struct ipset_type ipset_types[] = {
66 T(BITMAP, IP, UNSPEC, UNSPEC, OPT_IPRANGE, OPT_NETMASK),
67 T(BITMAP, IP, MAC, UNSPEC, OPT_IPRANGE, 0),
68 T(BITMAP, PORT, UNSPEC, UNSPEC, OPT_PORTRANGE, 0),
70 T(HASH, IP, UNSPEC, UNSPEC, 0,
71 OPT_FAMILY | OPT_HASHSIZE | OPT_MAXELEM | OPT_NETMASK),
72 T(HASH, NET, UNSPEC, UNSPEC, 0,
73 OPT_FAMILY | OPT_HASHSIZE | OPT_MAXELEM),
74 T(HASH, IP, PORT, UNSPEC, 0,
75 OPT_FAMILY | OPT_HASHSIZE | OPT_MAXELEM),
76 T(HASH, NET, PORT, UNSPEC, 0,
77 OPT_FAMILY | OPT_HASHSIZE | OPT_MAXELEM),
78 T(HASH, IP, PORT, IP, 0,
79 OPT_FAMILY | OPT_HASHSIZE | OPT_MAXELEM),
80 T(HASH, IP, PORT, NET, 0,
81 OPT_FAMILY | OPT_HASHSIZE | OPT_MAXELEM),
83 T(LIST, SET, UNSPEC, UNSPEC, 0, OPT_MAXELEM),
88 check_types(struct uci_element *e, struct fw3_ipset *ipset)
91 uint32_t typelist = 0;
92 struct fw3_ipset_datatype *type;
94 list_for_each_entry(type, &ipset->datatypes, list)
98 warn_elem(e, "must not have more than 3 datatypes assigned");
102 typelist |= (type->type << (i++ * 8));
105 /* find a suitable storage method if none specified */
106 if (ipset->method == FW3_IPSET_METHOD_UNSPEC)
108 for (i = 0; i < ARRAY_SIZE(ipset_types); i++)
110 if (ipset_types[i].types == typelist)
112 ipset->method = ipset_types[i].method;
114 warn_elem(e, "defines no storage method, assuming '%s'",
115 fw3_ipset_method_names[ipset->method]);
122 //typelist |= ipset->method;
124 for (i = 0; i < ARRAY_SIZE(ipset_types); i++)
126 if (ipset_types[i].method == ipset->method &&
127 ipset_types[i].types == typelist)
129 if (!ipset->external || !*ipset->external)
131 if ((ipset_types[i].required & OPT_IPRANGE) &&
134 warn_elem(e, "requires an ip range");
138 if ((ipset_types[i].required & OPT_PORTRANGE) &&
139 !ipset->portrange.set)
141 warn_elem(e, "requires a port range");
145 if (!(ipset_types[i].required & OPT_IPRANGE) &&
148 warn_elem(e, "iprange ignored");
149 ipset->iprange.set = false;
152 if (!(ipset_types[i].required & OPT_PORTRANGE) &&
153 ipset->portrange.set)
155 warn_elem(e, "portrange ignored");
156 ipset->portrange.set = false;
159 if (!(ipset_types[i].optional & OPT_NETMASK) &&
162 warn_elem(e, "netmask ignored");
166 if (!(ipset_types[i].optional & OPT_HASHSIZE) &&
169 warn_elem(e, "hashsize ignored");
173 if (!(ipset_types[i].optional & OPT_MAXELEM) &&
176 warn_elem(e, "maxelem ignored");
180 if (!(ipset_types[i].optional & OPT_FAMILY) &&
181 ipset->family != FW3_FAMILY_ANY)
183 warn_elem(e, "family ignored");
184 ipset->family = FW3_FAMILY_ANY;
192 warn_elem(e, "has an invalid combination of storage method and matches");
197 fw3_alloc_ipset(void)
199 struct fw3_ipset *ipset;
201 ipset = malloc(sizeof(*ipset));
206 memset(ipset, 0, sizeof(*ipset));
208 INIT_LIST_HEAD(&ipset->datatypes);
210 ipset->enabled = true;
216 fw3_load_ipsets(struct fw3_state *state, struct uci_package *p)
218 struct uci_section *s;
219 struct uci_element *e;
220 struct fw3_ipset *ipset;
222 INIT_LIST_HEAD(&state->ipsets);
224 if (state->disable_ipsets)
227 uci_foreach_element(&p->sections, e)
229 s = uci_to_section(e);
231 if (strcmp(s->type, "ipset"))
234 ipset = fw3_alloc_ipset();
239 fw3_parse_options(ipset, fw3_ipset_opts, s);
241 if (!ipset->name || !*ipset->name)
243 warn_elem(e, "must have a name assigned");
245 //else if (fw3_lookup_ipset(state, ipset->name) != NULL)
247 // warn_elem(e, "has duplicated set name '%s'", ipset->name);
249 else if (list_empty(&ipset->datatypes))
251 warn_elem(e, "has no datatypes assigned");
253 else if (check_types(e, ipset))
255 list_add_tail(&ipset->list, &state->ipsets);
259 fw3_free_ipset(ipset);
265 create_ipset(struct fw3_ipset *ipset, struct fw3_state *state)
269 struct fw3_ipset_datatype *type;
271 if (ipset->external && *ipset->external)
274 info(" * Creating ipset %s", ipset->name);
277 fw3_pr("create %s %s", ipset->name, fw3_ipset_method_names[ipset->method]);
279 list_for_each_entry(type, &ipset->datatypes, list)
281 fw3_pr("%c%s", first ? ':' : ',', fw3_ipset_type_names[type->type]);
285 if (ipset->iprange.set)
287 fw3_pr(" range %s", fw3_address_to_string(&ipset->iprange, false));
289 else if (ipset->portrange.set)
291 fw3_pr(" range %u-%u",
292 ipset->portrange.port_min, ipset->portrange.port_max);
295 if (ipset->family != FW3_FAMILY_ANY)
296 fw3_pr(" family inet%s", (ipset->family == FW3_FAMILY_V4) ? "" : "6");
298 if (ipset->timeout > 0)
299 fw3_pr(" timeout %u", ipset->timeout);
301 if (ipset->maxelem > 0)
302 fw3_pr(" maxelem %u", ipset->maxelem);
304 if (ipset->netmask > 0)
305 fw3_pr(" netmask %u", ipset->netmask);
307 if (ipset->hashsize > 0)
308 fw3_pr(" hashsize %u", ipset->hashsize);
314 fw3_create_ipsets(struct fw3_state *state)
316 struct fw3_ipset *ipset;
318 if (state->disable_ipsets)
321 list_for_each_entry(ipset, &state->ipsets, list)
322 create_ipset(ipset, state);
328 fw3_destroy_ipsets(struct fw3_state *state)
332 list_for_each_entry(s, &state->ipsets, list)
334 info(" * Deleting ipset %s", s->name);
336 fw3_pr("flush %s\n", s->name);
337 fw3_pr("destroy %s\n", s->name);
344 fw3_lookup_ipset(struct fw3_state *state, const char *name)
348 if (list_empty(&state->ipsets))
351 list_for_each_entry(s, &state->ipsets, list)
353 if (strcmp(s->name, name))