2 * firewall3 - 3rd OpenWrt UCI firewall implementation
4 * Copyright (C) 2013 Jo-Philipp Wich <jo@mein.io>
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.
24 const struct fw3_option fw3_ipset_opts
[] = {
25 FW3_OPT("enabled", bool, ipset
, enabled
),
26 FW3_OPT("reload_set", bool, ipset
, reload_set
),
27 FW3_OPT("counters", bool, ipset
, counters
),
28 FW3_OPT("comment", bool, ipset
, comment
),
30 FW3_OPT("name", string
, ipset
, name
),
31 FW3_OPT("family", family
, ipset
, family
),
33 FW3_OPT("storage", ipset_method
, ipset
, method
),
34 FW3_LIST("match", ipset_datatype
, ipset
, datatypes
),
36 FW3_OPT("iprange", address
, ipset
, iprange
),
37 FW3_OPT("portrange", port
, ipset
, portrange
),
39 FW3_OPT("netmask", int, ipset
, netmask
),
40 FW3_OPT("maxelem", int, ipset
, maxelem
),
41 FW3_OPT("hashsize", int, ipset
, hashsize
),
42 FW3_OPT("timeout", int, ipset
, timeout
),
44 FW3_OPT("external", string
, ipset
, external
),
46 FW3_LIST("entry", setentry
, ipset
, entries
),
47 FW3_OPT("loadfile", string
, ipset
, loadfile
),
52 #define T(m, t1, t2, t3, r, o) \
53 { FW3_IPSET_METHOD_##m, \
54 FW3_IPSET_TYPE_##t1 | (FW3_IPSET_TYPE_##t2 << 8) | (FW3_IPSET_TYPE_##t3 << 16), \
58 OPT_IPRANGE
= (1 << 0),
59 OPT_PORTRANGE
= (1 << 1),
60 OPT_NETMASK
= (1 << 2),
61 OPT_HASHSIZE
= (1 << 3),
62 OPT_MAXELEM
= (1 << 4),
63 OPT_FAMILY
= (1 << 5),
67 enum fw3_ipset_method method
;
73 static struct ipset_type ipset_types
[] = {
74 T(BITMAP
, IP
, UNSPEC
, UNSPEC
, OPT_IPRANGE
, OPT_NETMASK
),
75 T(BITMAP
, IP
, MAC
, UNSPEC
, OPT_IPRANGE
, 0),
76 T(BITMAP
, PORT
, UNSPEC
, UNSPEC
, OPT_PORTRANGE
, 0),
78 T(HASH
, IP
, UNSPEC
, UNSPEC
, 0,
79 OPT_FAMILY
| OPT_HASHSIZE
| OPT_MAXELEM
| OPT_NETMASK
),
80 T(HASH
, NET
, UNSPEC
, UNSPEC
, 0,
81 OPT_FAMILY
| OPT_HASHSIZE
| OPT_MAXELEM
),
82 T(HASH
, IP
, PORT
, UNSPEC
, 0,
83 OPT_FAMILY
| OPT_HASHSIZE
| OPT_MAXELEM
),
84 T(HASH
, NET
, PORT
, UNSPEC
, 0,
85 OPT_FAMILY
| OPT_HASHSIZE
| OPT_MAXELEM
),
86 T(HASH
, IP
, PORT
, IP
, 0,
87 OPT_FAMILY
| OPT_HASHSIZE
| OPT_MAXELEM
),
88 T(HASH
, IP
, PORT
, NET
, 0,
89 OPT_FAMILY
| OPT_HASHSIZE
| OPT_MAXELEM
),
91 T(LIST
, SET
, UNSPEC
, UNSPEC
, 0, OPT_MAXELEM
),
96 check_types(struct uci_element
*e
, struct fw3_ipset
*ipset
)
99 uint32_t typelist
= 0;
100 struct fw3_ipset_datatype
*type
;
102 list_for_each_entry(type
, &ipset
->datatypes
, list
)
106 warn_section("ipset", ipset
, e
, "must not have more than 3 datatypes assigned");
110 typelist
|= (type
->type
<< (i
++ * 8));
113 /* find a suitable storage method if none specified */
114 if (ipset
->method
== FW3_IPSET_METHOD_UNSPEC
)
116 for (i
= 0; i
< ARRAY_SIZE(ipset_types
); i
++)
118 /* skip type for v6 if it does not support family */
119 if (ipset
->family
!= FW3_FAMILY_V4
&&
120 !(ipset_types
[i
].optional
& OPT_FAMILY
))
123 if (ipset_types
[i
].types
== typelist
)
125 ipset
->method
= ipset_types
[i
].method
;
127 warn_section("ipset", ipset
, e
, "defines no storage method, assuming '%s'",
128 fw3_ipset_method_names
[ipset
->method
]);
135 //typelist |= ipset->method;
137 for (i
= 0; i
< ARRAY_SIZE(ipset_types
); i
++)
139 if (ipset_types
[i
].method
== ipset
->method
&&
140 ipset_types
[i
].types
== typelist
)
142 if (!ipset
->external
)
144 if ((ipset_types
[i
].required
& OPT_IPRANGE
) &&
147 warn_section("ipset", ipset
, e
, "requires an ip range");
151 if ((ipset_types
[i
].required
& OPT_PORTRANGE
) &&
152 !ipset
->portrange
.set
)
154 warn_section("ipset", ipset
, e
, "requires a port range");
158 if (!(ipset_types
[i
].required
& OPT_IPRANGE
) &&
161 warn_section("ipset", ipset
, e
, "iprange ignored");
162 ipset
->iprange
.set
= false;
165 if (!(ipset_types
[i
].required
& OPT_PORTRANGE
) &&
166 ipset
->portrange
.set
)
168 warn_section("ipset", ipset
, e
, "portrange ignored");
169 ipset
->portrange
.set
= false;
172 if (!(ipset_types
[i
].optional
& OPT_NETMASK
) &&
175 warn_section("ipset", ipset
, e
, "netmask ignored");
179 if (!(ipset_types
[i
].optional
& OPT_HASHSIZE
) &&
182 warn_section("ipset", ipset
, e
, "hashsize ignored");
186 if (!(ipset_types
[i
].optional
& OPT_MAXELEM
) &&
189 warn_section("ipset", ipset
, e
, "maxelem ignored");
193 if (!(ipset_types
[i
].optional
& OPT_FAMILY
) &&
194 ipset
->family
!= FW3_FAMILY_V4
)
196 warn_section("ipset", ipset
, e
, "family ignored");
197 ipset
->family
= FW3_FAMILY_V4
;
205 warn_section("ipset", ipset
, e
, "has an invalid combination of storage method and matches");
210 check_ipset(struct fw3_state
*state
, struct fw3_ipset
*ipset
, struct uci_element
*e
)
212 if (!ipset
->enabled
) {
218 if (!*ipset
->external
)
219 ipset
->external
= NULL
;
220 else if (!ipset
->name
)
221 ipset
->name
= ipset
->external
;
224 if (!ipset
->name
|| !*ipset
->name
)
226 warn_section("ipset", ipset
, e
, "ipset must have a name assigned");
228 //else if (fw3_lookup_ipset(state, ipset->name) != NULL)
230 // warn_section("ipset", ipset, e, "has duplicated set name", ipset->name);
232 else if (ipset
->family
== FW3_FAMILY_ANY
)
234 warn_section("ipset", ipset
, e
, "must not have family 'any'");
236 else if (ipset
->iprange
.set
&& ipset
->family
!= ipset
->iprange
.family
)
238 warn_section("ipset", ipset
, e
, "has iprange of wrong address family");
240 else if (list_empty(&ipset
->datatypes
))
242 warn_section("ipset", ipset
, e
, "has no datatypes assigned");
244 else if (check_types(e
, ipset
))
252 static struct fw3_ipset
*
253 fw3_alloc_ipset(struct fw3_state
*state
)
255 struct fw3_ipset
*ipset
;
257 ipset
= calloc(1, sizeof(*ipset
));
261 INIT_LIST_HEAD(&ipset
->datatypes
);
262 INIT_LIST_HEAD(&ipset
->entries
);
264 ipset
->comment
= false;
265 ipset
->counters
= false;
266 ipset
->enabled
= true;
267 ipset
->family
= FW3_FAMILY_V4
;
268 ipset
->reload_set
= false;
270 list_add_tail(&ipset
->list
, &state
->ipsets
);
276 fw3_load_ipsets(struct fw3_state
*state
, struct uci_package
*p
,
279 struct uci_section
*s
;
280 struct uci_element
*e
;
281 struct fw3_ipset
*ipset
;
282 struct blob_attr
*entry
;
285 INIT_LIST_HEAD(&state
->ipsets
);
287 if (state
->disable_ipsets
)
290 blob_for_each_attr(entry
, a
, rem
)
293 const char *name
= "ubus ipset";
295 if (!fw3_attr_parse_name_type(entry
, &name
, &type
))
298 if (strcmp(type
, "ipset"))
301 ipset
= fw3_alloc_ipset(state
);
305 if (!fw3_parse_blob_options(ipset
, fw3_ipset_opts
, entry
, name
))
307 warn_section("ipset", ipset
, NULL
, "skipped due to invalid options");
308 fw3_free_ipset(ipset
);
312 if (!check_ipset(state
, ipset
, NULL
))
313 fw3_free_ipset(ipset
);
316 uci_foreach_element(&p
->sections
, e
)
318 s
= uci_to_section(e
);
320 if (strcmp(s
->type
, "ipset"))
323 ipset
= fw3_alloc_ipset(state
);
328 if (!fw3_parse_options(ipset
, fw3_ipset_opts
, s
))
329 warn_elem(e
, "has invalid options");
331 if (!check_ipset(state
, ipset
, e
))
332 fw3_free_ipset(ipset
);
338 load_file(struct fw3_ipset
*ipset
)
344 if (!ipset
->loadfile
)
347 info(" * Loading file %s", ipset
->loadfile
);
349 f
= fopen(ipset
->loadfile
, "r");
352 info(" ! Skipping due to open error: %s", strerror(errno
));
356 while (fgets(line
, sizeof(line
), f
)) {
361 fw3_pr("add %s %s", ipset
->name
, line
);
368 create_ipset(struct fw3_ipset
*ipset
, struct fw3_state
*state
)
371 struct fw3_setentry
*entry
;
372 struct fw3_ipset_datatype
*type
;
374 info(" * Creating ipset %s", ipset
->name
);
377 fw3_pr("create %s %s", ipset
->name
, fw3_ipset_method_names
[ipset
->method
]);
379 list_for_each_entry(type
, &ipset
->datatypes
, list
)
381 fw3_pr("%c%s", first
? ':' : ',', fw3_ipset_type_names
[type
->type
]);
385 if (ipset
->method
== FW3_IPSET_METHOD_HASH
)
386 fw3_pr(" family inet%s", (ipset
->family
== FW3_FAMILY_V4
) ? "" : "6");
388 if (ipset
->iprange
.set
)
390 fw3_pr(" range %s", fw3_address_to_string(&ipset
->iprange
, false, true));
392 else if (ipset
->portrange
.set
)
394 fw3_pr(" range %u-%u",
395 ipset
->portrange
.port_min
, ipset
->portrange
.port_max
);
398 if (ipset
->timeout
> 0)
399 fw3_pr(" timeout %u", ipset
->timeout
);
401 if (ipset
->maxelem
> 0)
402 fw3_pr(" maxelem %u", ipset
->maxelem
);
404 if (ipset
->netmask
> 0)
405 fw3_pr(" netmask %u", ipset
->netmask
);
407 if (ipset
->hashsize
> 0)
408 fw3_pr(" hashsize %u", ipset
->hashsize
);
418 list_for_each_entry(entry
, &ipset
->entries
, list
)
419 fw3_pr("add %s %s\n", ipset
->name
, entry
->value
);
425 fw3_create_ipsets(struct fw3_state
*state
, enum fw3_family family
,
428 unsigned int delay
, tries
;
430 struct fw3_ipset
*ipset
;
432 if (state
->disable_ipsets
)
436 list_for_each_entry(ipset
, &state
->ipsets
, list
)
438 if (ipset
->family
!= family
)
444 if (fw3_check_ipset(ipset
) &&
445 (reload_set
&& !ipset
->reload_set
))
450 exec
= fw3_command_pipe(false, "ipset", "-exist", "-");
456 create_ipset(ipset
, state
);
465 /* wait a little expontially for ipsets to appear */
466 list_for_each_entry(ipset
, &state
->ipsets
, list
)
472 for (tries
= 0; !fw3_check_ipset(ipset
) && tries
< 10; tries
++)
478 fw3_destroy_ipsets(struct fw3_state
*state
, enum fw3_family family
,
481 unsigned int delay
, tries
;
483 struct fw3_ipset
*ipset
;
485 if (state
->disable_ipsets
)
489 list_for_each_entry(ipset
, &state
->ipsets
, list
)
491 if (ipset
->family
!= family
||
492 (reload_set
&& !ipset
->reload_set
))
497 exec
= fw3_command_pipe(false, "ipset", "-exist", "-");
503 info(" * Deleting ipset %s", ipset
->name
);
505 fw3_pr("flush %s\n", ipset
->name
);
506 fw3_pr("destroy %s\n", ipset
->name
);
515 /* wait for ipsets to disappear */
516 list_for_each_entry(ipset
, &state
->ipsets
, list
)
522 for (tries
= 0; fw3_check_ipset(ipset
) && tries
< 10; tries
++)
528 fw3_lookup_ipset(struct fw3_state
*state
, const char *name
)
532 if (list_empty(&state
->ipsets
))
535 list_for_each_entry(s
, &state
->ipsets
, list
)
537 if (strcmp(s
->name
, name
))
547 fw3_check_ipset(struct fw3_ipset
*set
)
552 int s
= socket(AF_INET
, SOCK_RAW
, IPPROTO_RAW
);
553 struct ip_set_req_version req_ver
;
554 struct ip_set_req_get_set req_name
;
556 if (s
< 0 || fcntl(s
, F_SETFD
, FD_CLOEXEC
))
559 sz
= sizeof(req_ver
);
560 req_ver
.op
= IP_SET_OP_VERSION
;
562 if (getsockopt(s
, SOL_IP
, SO_IP_SET
, &req_ver
, &sz
))
565 sz
= sizeof(req_name
);
566 req_name
.op
= IP_SET_OP_GET_BYNAME
;
567 req_name
.version
= req_ver
.version
;
568 snprintf(req_name
.set
.name
, IPSET_MAXNAMELEN
- 1, "%s",
569 set
->external
? set
->external
: set
->name
);
571 if (getsockopt(s
, SOL_IP
, SO_IP_SET
, &req_name
, &sz
))
574 rv
= ((sz
== sizeof(req_name
)) && (req_name
.set
.index
!= IPSET_INVALID_ID
));
584 fw3_ipsets_update_run_state(enum fw3_family family
, struct fw3_state
*run_state
,
585 struct fw3_state
*cfg_state
)
587 struct fw3_ipset
*ipset_run
, *ipset_cfg
;
590 list_for_each_entry(ipset_run
, &run_state
->ipsets
, list
) {
591 if (ipset_run
->family
!= family
)
596 list_for_each_entry(ipset_cfg
, &cfg_state
->ipsets
, list
) {
597 if (ipset_cfg
->family
!= family
)
600 if (strlen(ipset_run
->name
) ==
601 strlen(ipset_cfg
->name
) &&
602 !strcmp(ipset_run
->name
, ipset_cfg
->name
)) {
608 /* If a set is found in run_state, but not in cfg_state then the
609 * set has been deleted/renamed. Set reload_set to true to force
610 * the old set to be destroyed in the "stop" fase of the reload.
611 * If the set is found, then copy the reload_set value from the
612 * configuration state. This ensures that the elements are
613 * always updated according to the configuration, and not the
614 * runtime state (which the user might have forgotten).
617 ipset_run
->reload_set
= true;
619 ipset_run
->reload_set
= ipset_cfg
->reload_set
;