firewall3: ubus: parse the firewall data within the service itself
[project/firewall3.git] / ubus.c
1 /*
2 * firewall3 - 3rd OpenWrt UCI firewall implementation
3 *
4 * Copyright (C) 2013 Jo-Philipp Wich <jo@mein.io>
5 *
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.
9 *
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.
17 */
18
19 #include "ubus.h"
20
21 static struct blob_attr *interfaces = NULL;
22 static struct blob_attr *procd_data;
23
24
25 static void dump_cb(struct ubus_request *req, int type, struct blob_attr *msg)
26 {
27 static const struct blobmsg_policy policy = { "interface", BLOBMSG_TYPE_ARRAY };
28 struct blob_attr *cur;
29
30 blobmsg_parse(&policy, 1, &cur, blob_data(msg), blob_len(msg));
31 if (cur)
32 interfaces = blob_memdup(cur);
33 }
34
35 static void procd_data_cb(struct ubus_request *req, int type, struct blob_attr *msg)
36 {
37 procd_data = blob_memdup(msg);
38 }
39
40 bool
41 fw3_ubus_connect(void)
42 {
43 bool status = false;
44 uint32_t id;
45 struct ubus_context *ctx = ubus_connect(NULL);
46 struct blob_buf b = { };
47
48 if (!ctx)
49 goto out;
50
51 if (ubus_lookup_id(ctx, "network.interface", &id))
52 goto out;
53
54 if (ubus_invoke(ctx, id, "dump", NULL, dump_cb, NULL, 2000))
55 goto out;
56
57 status = true;
58
59 if (ubus_lookup_id(ctx, "service", &id))
60 goto out;
61
62 blob_buf_init(&b, 0);
63 blobmsg_add_string(&b, "type", "firewall");
64 ubus_invoke(ctx, id, "get_data", b.head, procd_data_cb, NULL, 2000);
65 blob_buf_free(&b);
66
67 out:
68 if (ctx)
69 ubus_free(ctx);
70 return status;
71 }
72
73 void
74 fw3_ubus_disconnect(void)
75 {
76 free(interfaces);
77 interfaces = NULL;
78 }
79
80 static struct fw3_address *
81 parse_subnet(enum fw3_family family, struct blob_attr *dict, int rem)
82 {
83 struct blob_attr *cur;
84 struct fw3_address *addr;
85
86 addr = calloc(1, sizeof(*addr));
87 if (!addr)
88 return NULL;
89
90 addr->set = true;
91 addr->family = family;
92
93 __blob_for_each_attr(cur, dict, rem)
94 {
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);
98
99 else if (!strcmp(blobmsg_name(cur), "mask"))
100 fw3_bitlen2netmask(family, blobmsg_get_u32(cur), &addr->mask.v6);
101 }
102
103 return addr;
104 }
105
106 static void
107 parse_subnets(struct list_head *head, enum fw3_family family,
108 struct blob_attr *list)
109 {
110 struct blob_attr *cur;
111 struct fw3_address *addr;
112 int rem;
113
114 if (!list)
115 return;
116
117 rem = blobmsg_data_len(list);
118
119 __blob_for_each_attr(cur, blobmsg_data(list), rem)
120 {
121 addr = parse_subnet(family, blobmsg_data(cur), blobmsg_data_len(cur));
122
123 if (addr)
124 list_add_tail(&addr->list, head);
125 }
126 }
127
128 struct fw3_device *
129 fw3_ubus_device(const char *net)
130 {
131 enum {
132 DEV_INTERFACE,
133 DEV_DEVICE,
134 DEV_L3_DEVICE,
135 __DEV_MAX
136 };
137 static const struct blobmsg_policy policy[__DEV_MAX] = {
138 [DEV_INTERFACE] = { "interface", BLOBMSG_TYPE_STRING },
139 [DEV_DEVICE] = { "device", BLOBMSG_TYPE_STRING },
140 [DEV_L3_DEVICE] = { "l3_device", BLOBMSG_TYPE_STRING },
141 };
142 struct fw3_device *dev = NULL;
143 struct blob_attr *tb[__DEV_MAX];
144 struct blob_attr *cur;
145 char *name = NULL;
146 int rem;
147
148 if (!net || !interfaces)
149 return NULL;
150
151 blobmsg_for_each_attr(cur, interfaces, rem) {
152 blobmsg_parse(policy, __DEV_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
153 if (!tb[DEV_INTERFACE] ||
154 strcmp(blobmsg_data(tb[DEV_INTERFACE]), net) != 0)
155 continue;
156
157 if (tb[DEV_L3_DEVICE])
158 name = blobmsg_data(tb[DEV_L3_DEVICE]);
159 else if (tb[DEV_DEVICE])
160 name = blobmsg_data(tb[DEV_DEVICE]);
161 else
162 continue;
163
164 break;
165 }
166
167 if (!name)
168 return NULL;
169
170 dev = calloc(1, sizeof(*dev));
171
172 if (!dev)
173 return NULL;
174
175 snprintf(dev->name, sizeof(dev->name), "%s", name);
176 dev->set = true;
177
178 return dev;
179 }
180
181 void
182 fw3_ubus_address(struct list_head *list, const char *net)
183 {
184 enum {
185 ADDR_INTERFACE,
186 ADDR_IPV4,
187 ADDR_IPV6,
188 ADDR_IPV6_PREFIX,
189 __ADDR_MAX
190 };
191 static const struct blobmsg_policy policy[__ADDR_MAX] = {
192 [ADDR_INTERFACE] = { "interface", BLOBMSG_TYPE_STRING },
193 [ADDR_IPV4] = { "ipv4-address", BLOBMSG_TYPE_ARRAY },
194 [ADDR_IPV6] = { "ipv6-address", BLOBMSG_TYPE_ARRAY },
195 [ADDR_IPV6_PREFIX] = { "ipv6-prefix-assignment", BLOBMSG_TYPE_ARRAY },
196 };
197 struct blob_attr *tb[__ADDR_MAX];
198 struct blob_attr *cur;
199 int rem;
200
201 if (!net || !interfaces)
202 return;
203
204 blobmsg_for_each_attr(cur, interfaces, rem) {
205 blobmsg_parse(policy, __ADDR_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
206
207 if (!tb[ADDR_INTERFACE] ||
208 strcmp(blobmsg_data(tb[ADDR_INTERFACE]), net) != 0)
209 continue;
210
211 parse_subnets(list, FW3_FAMILY_V4, tb[ADDR_IPV4]);
212 parse_subnets(list, FW3_FAMILY_V6, tb[ADDR_IPV6]);
213 parse_subnets(list, FW3_FAMILY_V6, tb[ADDR_IPV6_PREFIX]);
214 }
215 }
216
217 void
218 fw3_ubus_zone_devices(struct fw3_zone *zone)
219 {
220 struct blob_attr *c, *cur, *dcur;
221 unsigned r, rem, drem;
222 const char *name;
223 bool matches;
224
225 blobmsg_for_each_attr(c, interfaces, r) {
226 name = NULL;
227 matches = false;
228
229 blobmsg_for_each_attr(cur, c, rem) {
230 if (!strcmp(blobmsg_name(cur), "interface"))
231 name = blobmsg_get_string(cur);
232 else if (!strcmp(blobmsg_name(cur), "data"))
233 blobmsg_for_each_attr(dcur, cur, drem)
234 if (!strcmp(blobmsg_name(dcur), "zone"))
235 matches = !strcmp(blobmsg_get_string(dcur), zone->name);
236 }
237
238 if (name && matches)
239 fw3_parse_device(&zone->networks, name, true);
240 }
241 }
242
243 static void fw3_ubus_rules_add(struct blob_buf *b, const char *service,
244 const char *instance, const char *device,
245 const struct blob_attr *rule, unsigned n)
246 {
247 void *k = blobmsg_open_table(b, "");
248 struct blob_attr *ropt;
249 unsigned orem;
250 char *type = NULL;
251 char comment[256];
252
253 blobmsg_for_each_attr(ropt, rule, orem) {
254 if (!strcmp(blobmsg_name(ropt), "type"))
255 type = blobmsg_data(ropt);
256 if (device && !strcmp(blobmsg_name(ropt), "device"))
257 device = blobmsg_get_string(ropt);
258 else if (strcmp(blobmsg_name(ropt), "name"))
259 blobmsg_add_blob(b, ropt);
260 }
261
262 if (instance)
263 snprintf(comment, sizeof(comment), "ubus:%s[%s] %s %d",
264 service, instance, type ? type : "rule", n);
265 else
266 snprintf(comment, sizeof(comment), "ubus:%s %s %d",
267 service, type ? type : "rule", n);
268
269 blobmsg_add_string(b, "name", comment);
270
271 if (device)
272 blobmsg_add_string(b, "device", device);
273
274 blobmsg_close_table(b, k);
275 }
276
277 void
278 fw3_ubus_rules(struct blob_buf *b)
279 {
280 blob_buf_init(b, 0);
281
282 struct blob_attr *c, *cur, *dcur, *rule;
283 unsigned n, r, rem, drem, rrem;
284
285 blobmsg_for_each_attr(c, interfaces, r) {
286 const char *l3_device = NULL;
287 const char *iface_proto = "unknown";
288 const char *iface_name = "unknown";
289 struct blob_attr *data = NULL;
290
291 blobmsg_for_each_attr(cur, c, rem) {
292 if (!strcmp(blobmsg_name(cur), "l3_device"))
293 l3_device = blobmsg_get_string(cur);
294 else if (!strcmp(blobmsg_name(cur), "interface"))
295 iface_name = blobmsg_get_string(cur);
296 else if (!strcmp(blobmsg_name(cur), "proto"))
297 iface_proto = blobmsg_get_string(cur);
298 else if (!strcmp(blobmsg_name(cur), "data"))
299 data = cur;
300 }
301
302 if (!data || !l3_device)
303 continue;
304
305 blobmsg_for_each_attr(dcur, data, drem) {
306 if (strcmp(blobmsg_name(dcur), "firewall"))
307 continue;
308
309 n = 0;
310
311 blobmsg_for_each_attr(rule, dcur, rrem)
312 fw3_ubus_rules_add(b, iface_name, iface_proto,
313 l3_device, rule, n++);
314 }
315 }
316
317 if (!procd_data)
318 return;
319
320 /* service */
321 blobmsg_for_each_attr(c, procd_data, r) {
322 if (!blobmsg_check_attr(c, true))
323 continue;
324
325 /* instance */
326 blobmsg_for_each_attr(cur, c, rem) {
327 if (!blobmsg_check_attr(cur, true))
328 continue;
329
330 /* fw rules within the service itself */
331 if (!strcmp(blobmsg_name(cur), "firewall")) {
332 n = 0;
333
334 blobmsg_for_each_attr(rule, cur, rrem)
335 fw3_ubus_rules_add(b, blobmsg_name(c),
336 NULL, NULL, rule, n++);
337
338 continue;
339 }
340
341 /* type */
342 blobmsg_for_each_attr(dcur, cur, drem) {
343 if (!blobmsg_check_attr(dcur, true))
344 continue;
345
346 if (strcmp(blobmsg_name(dcur), "firewall"))
347 continue;
348
349 n = 0;
350
351 blobmsg_for_each_attr(rule, dcur, rrem)
352 fw3_ubus_rules_add(b, blobmsg_name(c),
353 blobmsg_name(cur), NULL, rule, n++);
354 }
355 }
356 }
357 }