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 /* skip type for v6 if it does not support family */
111 if (ipset->family != FW3_FAMILY_V4 &&
112 !(ipset_types[i].optional & OPT_FAMILY))
115 if (ipset_types[i].types == typelist)
117 ipset->method = ipset_types[i].method;
119 warn_elem(e, "defines no storage method, assuming '%s'",
120 fw3_ipset_method_names[ipset->method]);
127 //typelist |= ipset->method;
129 for (i = 0; i < ARRAY_SIZE(ipset_types); i++)
131 if (ipset_types[i].method == ipset->method &&
132 ipset_types[i].types == typelist)
134 if (!ipset->external)
136 if ((ipset_types[i].required & OPT_IPRANGE) &&
139 warn_elem(e, "requires an ip range");
143 if ((ipset_types[i].required & OPT_PORTRANGE) &&
144 !ipset->portrange.set)
146 warn_elem(e, "requires a port range");
150 if (!(ipset_types[i].required & OPT_IPRANGE) &&
153 warn_elem(e, "iprange ignored");
154 ipset->iprange.set = false;
157 if (!(ipset_types[i].required & OPT_PORTRANGE) &&
158 ipset->portrange.set)
160 warn_elem(e, "portrange ignored");
161 ipset->portrange.set = false;
164 if (!(ipset_types[i].optional & OPT_NETMASK) &&
167 warn_elem(e, "netmask ignored");
171 if (!(ipset_types[i].optional & OPT_HASHSIZE) &&
174 warn_elem(e, "hashsize ignored");
178 if (!(ipset_types[i].optional & OPT_MAXELEM) &&
181 warn_elem(e, "maxelem ignored");
185 if (!(ipset_types[i].optional & OPT_FAMILY) &&
186 ipset->family != FW3_FAMILY_V4)
188 warn_elem(e, "family ignored");
189 ipset->family = FW3_FAMILY_V4;
197 warn_elem(e, "has an invalid combination of storage method and matches");
202 fw3_alloc_ipset(void)
204 struct fw3_ipset *ipset;
206 ipset = calloc(1, sizeof(*ipset));
210 INIT_LIST_HEAD(&ipset->datatypes);
212 ipset->enabled = true;
213 ipset->family = FW3_FAMILY_V4;
219 fw3_load_ipsets(struct fw3_state *state, struct uci_package *p)
221 struct uci_section *s;
222 struct uci_element *e;
223 struct fw3_ipset *ipset;
225 INIT_LIST_HEAD(&state->ipsets);
227 if (state->disable_ipsets)
230 uci_foreach_element(&p->sections, e)
232 s = uci_to_section(e);
234 if (strcmp(s->type, "ipset"))
237 ipset = fw3_alloc_ipset();
242 fw3_parse_options(ipset, fw3_ipset_opts, s);
246 if (!*ipset->external)
247 ipset->external = NULL;
248 else if (!ipset->name)
249 ipset->name = ipset->external;
252 if (!ipset->name || !*ipset->name)
254 warn_elem(e, "must have a name assigned");
256 //else if (fw3_lookup_ipset(state, ipset->name) != NULL)
258 // warn_elem(e, "has duplicated set name '%s'", ipset->name);
260 else if (ipset->family == FW3_FAMILY_ANY)
262 warn_elem(e, "must not have family 'any'");
264 else if (ipset->iprange.set && ipset->family != ipset->iprange.family)
266 warn_elem(e, "has iprange of wrong address family");
268 else if (list_empty(&ipset->datatypes))
270 warn_elem(e, "has no datatypes assigned");
272 else if (check_types(e, ipset))
274 list_add_tail(&ipset->list, &state->ipsets);
278 fw3_free_ipset(ipset);
284 create_ipset(struct fw3_ipset *ipset, struct fw3_state *state)
288 struct fw3_ipset_datatype *type;
290 info(" * Creating ipset %s", ipset->name);
293 fw3_pr("create %s %s", ipset->name, fw3_ipset_method_names[ipset->method]);
295 list_for_each_entry(type, &ipset->datatypes, list)
297 fw3_pr("%c%s", first ? ':' : ',', fw3_ipset_type_names[type->type]);
301 if (ipset->method == FW3_IPSET_METHOD_HASH)
302 fw3_pr(" family inet%s", (ipset->family == FW3_FAMILY_V4) ? "" : "6");
304 if (ipset->iprange.set)
306 fw3_pr(" range %s", fw3_address_to_string(&ipset->iprange, false));
308 else if (ipset->portrange.set)
310 fw3_pr(" range %u-%u",
311 ipset->portrange.port_min, ipset->portrange.port_max);
314 if (ipset->timeout > 0)
315 fw3_pr(" timeout %u", ipset->timeout);
317 if (ipset->maxelem > 0)
318 fw3_pr(" maxelem %u", ipset->maxelem);
320 if (ipset->netmask > 0)
321 fw3_pr(" netmask %u", ipset->netmask);
323 if (ipset->hashsize > 0)
324 fw3_pr(" hashsize %u", ipset->hashsize);
330 fw3_create_ipsets(struct fw3_state *state)
334 struct fw3_ipset *ipset;
336 if (state->disable_ipsets)
340 list_for_each_entry(ipset, &state->ipsets, list)
347 exec = fw3_command_pipe(false, "ipset", "-exist", "-");
353 create_ipset(ipset, state);
362 /* wait for ipsets to appear */
363 list_for_each_entry(ipset, &state->ipsets, list)
368 for (tries = 0; !fw3_check_ipset(ipset) && tries < 10; tries++)
374 fw3_destroy_ipsets(struct fw3_state *state)
378 struct fw3_ipset *ipset;
381 list_for_each_entry(ipset, &state->ipsets, list)
385 exec = fw3_command_pipe(false, "ipset", "-exist", "-");
391 info(" * Deleting ipset %s", ipset->name);
393 fw3_pr("flush %s\n", ipset->name);
394 fw3_pr("destroy %s\n", ipset->name);
403 /* wait for ipsets to disappear */
404 list_for_each_entry(ipset, &state->ipsets, list)
409 for (tries = 0; fw3_check_ipset(ipset) && tries < 10; tries++)
415 fw3_lookup_ipset(struct fw3_state *state, const char *name)
419 if (list_empty(&state->ipsets))
422 list_for_each_entry(s, &state->ipsets, list)
424 if (strcmp(s->name, name))
434 fw3_check_ipset(struct fw3_ipset *set)
439 int s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
440 struct ip_set_req_version req_ver;
441 struct ip_set_req_get_set req_name;
443 if (s < 0 || fcntl(s, F_SETFD, FD_CLOEXEC))
446 sz = sizeof(req_ver);
447 req_ver.op = IP_SET_OP_VERSION;
449 if (getsockopt(s, SOL_IP, SO_IP_SET, &req_ver, &sz))
452 sz = sizeof(req_name);
453 req_name.op = IP_SET_OP_GET_BYNAME;
454 req_name.version = req_ver.version;
455 snprintf(req_name.set.name, IPSET_MAXNAMELEN - 1, "%s",
456 set->external ? set->external : set->name);
458 if (getsockopt(s, SOL_IP, SO_IP_SET, &req_name, &sz))
461 rv = ((sz == sizeof(req_name)) && (req_name.set.index != IPSET_INVALID_ID));