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.
21 static struct blob_attr *interfaces = NULL;
22 static struct blob_attr *procd_data;
25 static void dump_cb(struct ubus_request *req, int type, struct blob_attr *msg)
27 static const struct blobmsg_policy policy = { "interface", BLOBMSG_TYPE_ARRAY };
28 struct blob_attr *cur;
30 blobmsg_parse(&policy, 1, &cur, blob_data(msg), blob_len(msg));
32 interfaces = blob_memdup(cur);
35 static void procd_data_cb(struct ubus_request *req, int type, struct blob_attr *msg)
37 procd_data = blob_memdup(msg);
41 fw3_ubus_connect(void)
45 struct ubus_context *ctx = ubus_connect(NULL);
46 struct blob_buf b = { };
51 if (ubus_lookup_id(ctx, "network.interface", &id))
54 if (ubus_invoke(ctx, id, "dump", NULL, dump_cb, NULL, 2000))
59 if (ubus_lookup_id(ctx, "service", &id))
63 blobmsg_add_string(&b, "type", "firewall");
64 ubus_invoke(ctx, id, "get_data", b.head, procd_data_cb, NULL, 2000);
74 fw3_ubus_disconnect(void)
80 static struct fw3_address *
81 parse_subnet(enum fw3_family family, struct blob_attr *dict, int rem)
83 struct blob_attr *cur;
84 struct fw3_address *addr;
86 addr = calloc(1, sizeof(*addr));
91 addr->family = family;
93 __blob_for_each_attr(cur, dict, rem)
95 if (!strcmp(blobmsg_name(cur), "address"))
96 inet_pton(family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
97 blobmsg_get_string(cur), &addr->address.v6);
99 else if (!strcmp(blobmsg_name(cur), "mask"))
100 fw3_bitlen2netmask(family, blobmsg_get_u32(cur), &addr->mask.v6);
107 parse_subnets(struct list_head *head, enum fw3_family family,
108 struct blob_attr *list)
110 struct blob_attr *cur;
111 struct fw3_address *addr;
117 rem = blobmsg_data_len(list);
119 __blob_for_each_attr(cur, blobmsg_data(list), rem)
121 addr = parse_subnet(family, blobmsg_data(cur), blobmsg_data_len(cur));
125 list_add_tail(&addr->list, head);
134 fw3_ubus_device(const char *net)
142 static const struct blobmsg_policy policy[__DEV_MAX] = {
143 [DEV_INTERFACE] = { "interface", BLOBMSG_TYPE_STRING },
144 [DEV_DEVICE] = { "device", BLOBMSG_TYPE_STRING },
145 [DEV_L3_DEVICE] = { "l3_device", BLOBMSG_TYPE_STRING },
147 struct fw3_device *dev = NULL;
148 struct blob_attr *tb[__DEV_MAX];
149 struct blob_attr *cur;
153 if (!net || !interfaces)
156 blobmsg_for_each_attr(cur, interfaces, rem) {
157 blobmsg_parse(policy, __DEV_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
158 if (!tb[DEV_INTERFACE] ||
159 strcmp(blobmsg_data(tb[DEV_INTERFACE]), net) != 0)
162 if (tb[DEV_L3_DEVICE])
163 name = blobmsg_data(tb[DEV_L3_DEVICE]);
164 else if (tb[DEV_DEVICE])
165 name = blobmsg_data(tb[DEV_DEVICE]);
175 dev = calloc(1, sizeof(*dev));
180 snprintf(dev->name, sizeof(dev->name), "%s", name);
187 fw3_ubus_address(struct list_head *list, const char *net)
196 static const struct blobmsg_policy policy[__ADDR_MAX] = {
197 [ADDR_INTERFACE] = { "interface", BLOBMSG_TYPE_STRING },
198 [ADDR_IPV4] = { "ipv4-address", BLOBMSG_TYPE_ARRAY },
199 [ADDR_IPV6] = { "ipv6-address", BLOBMSG_TYPE_ARRAY },
200 [ADDR_IPV6_PREFIX] = { "ipv6-prefix-assignment", BLOBMSG_TYPE_ARRAY },
202 struct blob_attr *tb[__ADDR_MAX];
203 struct blob_attr *cur;
206 if (!net || !interfaces)
209 blobmsg_for_each_attr(cur, interfaces, rem) {
210 blobmsg_parse(policy, __ADDR_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
212 if (!tb[ADDR_INTERFACE] ||
213 strcmp(blobmsg_data(tb[ADDR_INTERFACE]), net) != 0)
216 n += parse_subnets(list, FW3_FAMILY_V4, tb[ADDR_IPV4]);
217 n += parse_subnets(list, FW3_FAMILY_V6, tb[ADDR_IPV6]);
218 n += parse_subnets(list, FW3_FAMILY_V6, tb[ADDR_IPV6_PREFIX]);
225 fw3_ubus_zone_devices(struct fw3_zone *zone)
227 struct blob_attr *c, *cur, *dcur;
228 unsigned r, rem, drem;
232 blobmsg_for_each_attr(c, interfaces, r) {
236 blobmsg_for_each_attr(cur, c, rem) {
237 if (!strcmp(blobmsg_name(cur), "interface"))
238 name = blobmsg_get_string(cur);
239 else if (!strcmp(blobmsg_name(cur), "data"))
240 blobmsg_for_each_attr(dcur, cur, drem)
241 if (!strcmp(blobmsg_name(dcur), "zone"))
242 matches = !strcmp(blobmsg_get_string(dcur), zone->name);
246 fw3_parse_device(&zone->networks, name, true);
250 static void fw3_ubus_rules_add(struct blob_buf *b, const char *service,
251 const char *instance, const char *device,
252 const struct blob_attr *rule, unsigned n)
254 void *k = blobmsg_open_table(b, "");
255 struct blob_attr *ropt;
260 blobmsg_for_each_attr(ropt, rule, orem) {
261 if (!strcmp(blobmsg_name(ropt), "type"))
262 type = blobmsg_data(ropt);
263 if (device && !strcmp(blobmsg_name(ropt), "device"))
264 device = blobmsg_get_string(ropt);
265 else if (strcmp(blobmsg_name(ropt), "name"))
266 blobmsg_add_blob(b, ropt);
270 snprintf(comment, sizeof(comment), "ubus:%s[%s] %s %d",
271 service, instance, type ? type : "rule", n);
273 snprintf(comment, sizeof(comment), "ubus:%s %s %d",
274 service, type ? type : "rule", n);
276 blobmsg_add_string(b, "name", comment);
279 blobmsg_add_string(b, "device", device);
281 blobmsg_close_table(b, k);
285 fw3_ubus_rules(struct blob_buf *b)
289 struct blob_attr *c, *cur, *dcur, *rule;
290 unsigned n, r, rem, drem, rrem;
292 blobmsg_for_each_attr(c, interfaces, r) {
293 const char *l3_device = NULL;
294 const char *iface_proto = "unknown";
295 const char *iface_name = "unknown";
296 struct blob_attr *data = NULL;
298 blobmsg_for_each_attr(cur, c, rem) {
299 if (!strcmp(blobmsg_name(cur), "l3_device"))
300 l3_device = blobmsg_get_string(cur);
301 else if (!strcmp(blobmsg_name(cur), "interface"))
302 iface_name = blobmsg_get_string(cur);
303 else if (!strcmp(blobmsg_name(cur), "proto"))
304 iface_proto = blobmsg_get_string(cur);
305 else if (!strcmp(blobmsg_name(cur), "data"))
309 if (!data || !l3_device)
312 blobmsg_for_each_attr(dcur, data, drem) {
313 if (strcmp(blobmsg_name(dcur), "firewall"))
318 blobmsg_for_each_attr(rule, dcur, rrem)
319 fw3_ubus_rules_add(b, iface_name, iface_proto,
320 l3_device, rule, n++);
328 blobmsg_for_each_attr(c, procd_data, r) {
329 if (!blobmsg_check_attr(c, true))
333 blobmsg_for_each_attr(cur, c, rem) {
334 if (!blobmsg_check_attr(cur, true))
337 /* fw rules within the service itself */
338 if (!strcmp(blobmsg_name(cur), "firewall")) {
341 blobmsg_for_each_attr(rule, cur, rrem)
342 fw3_ubus_rules_add(b, blobmsg_name(c),
343 NULL, NULL, rule, n++);
349 blobmsg_for_each_attr(dcur, cur, drem) {
350 if (!blobmsg_check_attr(dcur, true))
353 if (strcmp(blobmsg_name(dcur), "firewall"))
358 blobmsg_for_each_attr(rule, dcur, rrem)
359 fw3_ubus_rules_add(b, blobmsg_name(c),
360 blobmsg_name(cur), NULL, rule, n++);