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.
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_section("ipset", ipset
, 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_section("ipset", ipset
, 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_section("ipset", ipset
, e
, "requires an ip range");
143 if ((ipset_types
[i
].required
& OPT_PORTRANGE
) &&
144 !ipset
->portrange
.set
)
146 warn_section("ipset", ipset
, e
, "requires a port range");
150 if (!(ipset_types
[i
].required
& OPT_IPRANGE
) &&
153 warn_section("ipset", ipset
, e
, "iprange ignored");
154 ipset
->iprange
.set
= false;
157 if (!(ipset_types
[i
].required
& OPT_PORTRANGE
) &&
158 ipset
->portrange
.set
)
160 warn_section("ipset", ipset
, e
, "portrange ignored");
161 ipset
->portrange
.set
= false;
164 if (!(ipset_types
[i
].optional
& OPT_NETMASK
) &&
167 warn_section("ipset", ipset
, e
, "netmask ignored");
171 if (!(ipset_types
[i
].optional
& OPT_HASHSIZE
) &&
174 warn_section("ipset", ipset
, e
, "hashsize ignored");
178 if (!(ipset_types
[i
].optional
& OPT_MAXELEM
) &&
181 warn_section("ipset", ipset
, e
, "maxelem ignored");
185 if (!(ipset_types
[i
].optional
& OPT_FAMILY
) &&
186 ipset
->family
!= FW3_FAMILY_V4
)
188 warn_section("ipset", ipset
, e
, "family ignored");
189 ipset
->family
= FW3_FAMILY_V4
;
197 warn_section("ipset", ipset
, e
, "has an invalid combination of storage method and matches");
202 check_ipset(struct fw3_state
*state
, struct fw3_ipset
*ipset
, struct uci_element
*e
)
206 if (!*ipset
->external
)
207 ipset
->external
= NULL
;
208 else if (!ipset
->name
)
209 ipset
->name
= ipset
->external
;
212 if (!ipset
->name
|| !*ipset
->name
)
214 warn_section("ipset", ipset
, e
, "ipset must have a name assigned");
216 //else if (fw3_lookup_ipset(state, ipset->name) != NULL)
218 // warn_section("ipset", ipset, e, "has duplicated set name", ipset->name);
220 else if (ipset
->family
== FW3_FAMILY_ANY
)
222 warn_section("ipset", ipset
, e
, "must not have family 'any'");
224 else if (ipset
->iprange
.set
&& ipset
->family
!= ipset
->iprange
.family
)
226 warn_section("ipset", ipset
, e
, "has iprange of wrong address family");
228 else if (list_empty(&ipset
->datatypes
))
230 warn_section("ipset", ipset
, e
, "has no datatypes assigned");
232 else if (check_types(e
, ipset
))
240 static struct fw3_ipset
*
241 fw3_alloc_ipset(struct fw3_state
*state
)
243 struct fw3_ipset
*ipset
;
245 ipset
= calloc(1, sizeof(*ipset
));
249 INIT_LIST_HEAD(&ipset
->datatypes
);
251 ipset
->enabled
= true;
252 ipset
->family
= FW3_FAMILY_V4
;
254 list_add_tail(&ipset
->list
, &state
->ipsets
);
260 fw3_load_ipsets(struct fw3_state
*state
, struct uci_package
*p
,
263 struct uci_section
*s
;
264 struct uci_element
*e
;
265 struct fw3_ipset
*ipset
;
266 struct blob_attr
*entry
;
269 INIT_LIST_HEAD(&state
->ipsets
);
271 if (state
->disable_ipsets
)
274 blob_for_each_attr(entry
, a
, rem
)
277 const char *name
= "ubus ipset";
279 if (!fw3_attr_parse_name_type(entry
, &name
, &type
))
282 if (strcmp(type
, "ipset"))
285 ipset
= fw3_alloc_ipset(state
);
289 if (!fw3_parse_blob_options(ipset
, fw3_ipset_opts
, entry
, name
))
291 warn_section("ipset", ipset
, NULL
, "skipped due to invalid options");
292 fw3_free_ipset(ipset
);
296 if (!check_ipset(state
, ipset
, NULL
))
297 fw3_free_ipset(ipset
);
300 uci_foreach_element(&p
->sections
, e
)
302 s
= uci_to_section(e
);
304 if (strcmp(s
->type
, "ipset"))
307 ipset
= fw3_alloc_ipset(state
);
312 if (!fw3_parse_options(ipset
, fw3_ipset_opts
, s
))
313 warn_elem(e
, "has invalid options");
315 if (!check_ipset(state
, ipset
, e
))
316 fw3_free_ipset(ipset
);
322 create_ipset(struct fw3_ipset
*ipset
, struct fw3_state
*state
)
326 struct fw3_ipset_datatype
*type
;
328 info(" * Creating ipset %s", ipset
->name
);
331 fw3_pr("create %s %s", ipset
->name
, fw3_ipset_method_names
[ipset
->method
]);
333 list_for_each_entry(type
, &ipset
->datatypes
, list
)
335 fw3_pr("%c%s", first
? ':' : ',', fw3_ipset_type_names
[type
->type
]);
339 if (ipset
->method
== FW3_IPSET_METHOD_HASH
)
340 fw3_pr(" family inet%s", (ipset
->family
== FW3_FAMILY_V4
) ? "" : "6");
342 if (ipset
->iprange
.set
)
344 fw3_pr(" range %s", fw3_address_to_string(&ipset
->iprange
, false, true));
346 else if (ipset
->portrange
.set
)
348 fw3_pr(" range %u-%u",
349 ipset
->portrange
.port_min
, ipset
->portrange
.port_max
);
352 if (ipset
->timeout
> 0)
353 fw3_pr(" timeout %u", ipset
->timeout
);
355 if (ipset
->maxelem
> 0)
356 fw3_pr(" maxelem %u", ipset
->maxelem
);
358 if (ipset
->netmask
> 0)
359 fw3_pr(" netmask %u", ipset
->netmask
);
361 if (ipset
->hashsize
> 0)
362 fw3_pr(" hashsize %u", ipset
->hashsize
);
368 fw3_create_ipsets(struct fw3_state
*state
)
372 struct fw3_ipset
*ipset
;
374 if (state
->disable_ipsets
)
378 list_for_each_entry(ipset
, &state
->ipsets
, list
)
385 exec
= fw3_command_pipe(false, "ipset", "-exist", "-");
391 create_ipset(ipset
, state
);
400 /* wait for ipsets to appear */
401 list_for_each_entry(ipset
, &state
->ipsets
, list
)
406 for (tries
= 0; !fw3_check_ipset(ipset
) && tries
< 10; tries
++)
412 fw3_destroy_ipsets(struct fw3_state
*state
)
416 struct fw3_ipset
*ipset
;
419 list_for_each_entry(ipset
, &state
->ipsets
, list
)
423 exec
= fw3_command_pipe(false, "ipset", "-exist", "-");
429 info(" * Deleting ipset %s", ipset
->name
);
431 fw3_pr("flush %s\n", ipset
->name
);
432 fw3_pr("destroy %s\n", ipset
->name
);
441 /* wait for ipsets to disappear */
442 list_for_each_entry(ipset
, &state
->ipsets
, list
)
447 for (tries
= 0; fw3_check_ipset(ipset
) && tries
< 10; tries
++)
453 fw3_lookup_ipset(struct fw3_state
*state
, const char *name
)
457 if (list_empty(&state
->ipsets
))
460 list_for_each_entry(s
, &state
->ipsets
, list
)
462 if (strcmp(s
->name
, name
))
472 fw3_check_ipset(struct fw3_ipset
*set
)
477 int s
= socket(AF_INET
, SOCK_RAW
, IPPROTO_RAW
);
478 struct ip_set_req_version req_ver
;
479 struct ip_set_req_get_set req_name
;
481 if (s
< 0 || fcntl(s
, F_SETFD
, FD_CLOEXEC
))
484 sz
= sizeof(req_ver
);
485 req_ver
.op
= IP_SET_OP_VERSION
;
487 if (getsockopt(s
, SOL_IP
, SO_IP_SET
, &req_ver
, &sz
))
490 sz
= sizeof(req_name
);
491 req_name
.op
= IP_SET_OP_GET_BYNAME
;
492 req_name
.version
= req_ver
.version
;
493 snprintf(req_name
.set
.name
, IPSET_MAXNAMELEN
- 1, "%s",
494 set
->external
? set
->external
: set
->name
);
496 if (getsockopt(s
, SOL_IP
, SO_IP_SET
, &req_name
, &sz
))
499 rv
= ((sz
== sizeof(req_name
)) && (req_name
.set
.index
!= IPSET_INVALID_ID
));