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.
21 static struct blob_attr *interfaces = NULL;
24 static void dump_cb(struct ubus_request *req, int type, struct blob_attr *msg)
26 static const struct blobmsg_policy policy = { "interface", BLOBMSG_TYPE_ARRAY };
27 struct blob_attr *cur;
29 blobmsg_parse(&policy, 1, &cur, blob_data(msg), blob_len(msg));
31 interfaces = blob_memdup(cur);
35 fw3_ubus_connect(void)
39 struct ubus_context *ctx = ubus_connect(NULL);
44 if (ubus_lookup_id(ctx, "network.interface", &id))
47 if (ubus_invoke(ctx, id, "dump", NULL, dump_cb, NULL, 500))
59 fw3_ubus_disconnect(void)
65 static struct fw3_address *
66 parse_subnet(enum fw3_family family, struct blob_attr *dict, int rem)
68 struct blob_attr *cur;
69 struct fw3_address *addr;
71 addr = calloc(1, sizeof(*addr));
76 addr->family = family;
78 __blob_for_each_attr(cur, dict, rem)
80 if (!strcmp(blobmsg_name(cur), "address"))
81 inet_pton(family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
82 blobmsg_data(cur), &addr->address.v6);
84 else if (!strcmp(blobmsg_name(cur), "mask"))
85 addr->mask = be32_to_cpu(*(uint32_t *)blobmsg_data(cur));
92 parse_subnets(struct list_head *head, enum fw3_family family,
93 struct blob_attr *list)
95 struct blob_attr *cur;
96 struct fw3_address *addr;
102 blob_for_each_attr(cur, list, rem)
104 addr = parse_subnet(family, blobmsg_data(cur), blobmsg_data_len(cur));
107 list_add_tail(&addr->list, head);
112 fw3_ubus_device(const char *net)
120 static const struct blobmsg_policy policy[__DEV_MAX] = {
121 [DEV_INTERFACE] = { "interface", BLOBMSG_TYPE_STRING },
122 [DEV_DEVICE] = { "device", BLOBMSG_TYPE_STRING },
123 [DEV_L3_DEVICE] = { "l3_device", BLOBMSG_TYPE_STRING },
125 struct fw3_device *dev = NULL;
126 struct blob_attr *tb[__DEV_MAX];
127 struct blob_attr *cur;
130 if (!net || !interfaces)
133 dev = calloc(1, sizeof(*dev));
137 blobmsg_for_each_attr(cur, interfaces, rem) {
140 blobmsg_parse(policy, __DEV_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
141 if (!tb[DEV_INTERFACE] ||
142 strcmp(blobmsg_data(tb[DEV_INTERFACE]), net) != 0)
145 if (tb[DEV_L3_DEVICE])
146 name = blobmsg_data(tb[DEV_L3_DEVICE]);
147 else if (tb[DEV_DEVICE])
148 name = blobmsg_data(tb[DEV_DEVICE]);
152 snprintf(dev->name, sizeof(dev->name), "%s", name);
153 dev->set = !!dev->name[0];
161 fw3_ubus_address(struct list_head *list, const char *net)
170 static const struct blobmsg_policy policy[__ADDR_MAX] = {
171 [ADDR_INTERFACE] = { "interface", BLOBMSG_TYPE_STRING },
172 [ADDR_IPV4] = { "ipv4-address", BLOBMSG_TYPE_ARRAY },
173 [ADDR_IPV6] = { "ipv6-address", BLOBMSG_TYPE_ARRAY },
174 [ADDR_IPV6_PREFIX] = { "ipv6-prefix-assignment", BLOBMSG_TYPE_ARRAY },
176 struct blob_attr *tb[__ADDR_MAX];
177 struct blob_attr *cur;
180 if (!net || !interfaces)
183 blobmsg_for_each_attr(cur, interfaces, rem) {
184 blobmsg_parse(policy, __ADDR_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
186 if (!tb[ADDR_INTERFACE] ||
187 strcmp(blobmsg_data(tb[ADDR_INTERFACE]), net) != 0)
190 parse_subnets(list, FW3_FAMILY_V4, tb[ADDR_IPV4]);
191 parse_subnets(list, FW3_FAMILY_V6, tb[ADDR_IPV6]);
192 parse_subnets(list, FW3_FAMILY_V6, tb[ADDR_IPV6_PREFIX]);
197 fw3_ubus_zone_devices(struct fw3_zone *zone)
199 struct blob_attr *c, *cur, *dcur;
200 unsigned r, rem, drem;
204 blobmsg_for_each_attr(c, interfaces, r) {
208 blobmsg_for_each_attr(cur, c, rem) {
209 if (!strcmp(blobmsg_name(cur), "interface"))
210 name = blobmsg_get_string(cur);
211 else if (!strcmp(blobmsg_name(cur), "data"))
212 blobmsg_for_each_attr(dcur, cur, drem)
213 if (!strcmp(blobmsg_name(dcur), "zone"))
214 matches = !strcmp(blobmsg_get_string(dcur), zone->name);
218 fw3_parse_device(&zone->networks, name, true);
223 fw3_ubus_rules(struct blob_buf *b)
227 struct blob_attr *c, *cur, *dcur, *rule, *ropt;
228 unsigned r, rem, drem, rrem, orem;
230 blobmsg_for_each_attr(c, interfaces, r) {
231 const char *l3_device = NULL;
232 struct blob_attr *data = NULL;
234 blobmsg_for_each_attr(cur, c, rem) {
235 if (!strcmp(blobmsg_name(cur), "l3_device"))
236 l3_device = blobmsg_get_string(cur);
237 else if (!strcmp(blobmsg_name(cur), "data"))
241 if (!data || !l3_device)
244 blobmsg_for_each_attr(dcur, data, drem) {
245 if (strcmp(blobmsg_name(dcur), "firewall"))
248 blobmsg_for_each_attr(rule, dcur, rrem) {
249 void *k = blobmsg_open_table(b, "");
251 blobmsg_for_each_attr(ropt, rule, orem)
252 if (strcmp(blobmsg_name(ropt), "device"))
253 blobmsg_add_blob(b, ropt);
255 blobmsg_add_string(b, "device", l3_device);
256 blobmsg_close_table(b, k);