5161922144fb90287b7d453f84effd584b111938
[project/firewall3.git] / ubus.c
1 /*
2  * firewall3 - 3rd OpenWrt UCI firewall implementation
3  *
4  *   Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
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, 500))
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_data(cur), &addr->address.v6);
98
99                 else if (!strcmp(blobmsg_name(cur), "mask"))
100                         addr->mask = be32_to_cpu(*(uint32_t *)blobmsg_data(cur));
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, int rem)
109 {
110         struct blob_attr *cur;
111         struct fw3_address *addr;
112
113         if (!list)
114                 return;
115
116         __blob_for_each_attr(cur, list, rem)
117         {
118                 addr = parse_subnet(family, blobmsg_data(cur), blobmsg_data_len(cur));
119
120                 if (addr)
121                         list_add_tail(&addr->list, head);
122         }
123 }
124
125 struct fw3_device *
126 fw3_ubus_device(const char *net)
127 {
128         enum {
129                 DEV_INTERFACE,
130                 DEV_DEVICE,
131                 DEV_L3_DEVICE,
132                 __DEV_MAX
133         };
134         static const struct blobmsg_policy policy[__DEV_MAX] = {
135                 [DEV_INTERFACE] = { "interface", BLOBMSG_TYPE_STRING },
136                 [DEV_DEVICE] = { "device", BLOBMSG_TYPE_STRING },
137                 [DEV_L3_DEVICE] = { "l3_device", BLOBMSG_TYPE_STRING },
138         };
139         struct fw3_device *dev = NULL;
140         struct blob_attr *tb[__DEV_MAX];
141         struct blob_attr *cur;
142         char *name = NULL;
143         int rem;
144
145         if (!net || !interfaces)
146                 return NULL;
147
148         blobmsg_for_each_attr(cur, interfaces, rem) {
149                 blobmsg_parse(policy, __DEV_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
150                 if (!tb[DEV_INTERFACE] ||
151                     strcmp(blobmsg_data(tb[DEV_INTERFACE]), net) != 0)
152                         continue;
153
154                 if (tb[DEV_L3_DEVICE])
155                         name = blobmsg_data(tb[DEV_L3_DEVICE]);
156                 else if (tb[DEV_DEVICE])
157                         name = blobmsg_data(tb[DEV_DEVICE]);
158                 else
159                         continue;
160
161                 break;
162         }
163
164         if (!name)
165                 return NULL;
166
167         dev = calloc(1, sizeof(*dev));
168
169         if (!dev)
170                 return NULL;
171
172         snprintf(dev->name, sizeof(dev->name), "%s", name);
173         dev->set = true;
174
175         return dev;
176 }
177
178 void
179 fw3_ubus_address(struct list_head *list, const char *net)
180 {
181         enum {
182                 ADDR_INTERFACE,
183                 ADDR_IPV4,
184                 ADDR_IPV6,
185                 ADDR_IPV6_PREFIX,
186                 __ADDR_MAX
187         };
188         static const struct blobmsg_policy policy[__ADDR_MAX] = {
189                 [ADDR_INTERFACE] = { "interface", BLOBMSG_TYPE_STRING },
190                 [ADDR_IPV4] = { "ipv4-address", BLOBMSG_TYPE_ARRAY },
191                 [ADDR_IPV6] = { "ipv6-address", BLOBMSG_TYPE_ARRAY },
192                 [ADDR_IPV6_PREFIX] = { "ipv6-prefix-assignment", BLOBMSG_TYPE_ARRAY },
193         };
194         struct blob_attr *tb[__ADDR_MAX];
195         struct blob_attr *cur;
196         int rem;
197
198         if (!net || !interfaces)
199                 return;
200
201         blobmsg_for_each_attr(cur, interfaces, rem) {
202                 blobmsg_parse(policy, __ADDR_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
203
204                 if (!tb[ADDR_INTERFACE] ||
205                     strcmp(blobmsg_data(tb[ADDR_INTERFACE]), net) != 0)
206                         continue;
207
208                 parse_subnets(list, FW3_FAMILY_V4, blobmsg_data(tb[ADDR_IPV4]), blobmsg_data_len(tb[ADDR_IPV4]));
209                 parse_subnets(list, FW3_FAMILY_V6, blobmsg_data(tb[ADDR_IPV6]), blobmsg_data_len(tb[ADDR_IPV6]));
210                 parse_subnets(list, FW3_FAMILY_V6, blobmsg_data(tb[ADDR_IPV6_PREFIX]), blobmsg_data_len(tb[ADDR_IPV6_PREFIX]));
211         }
212 }
213
214 void
215 fw3_ubus_zone_devices(struct fw3_zone *zone)
216 {
217         struct blob_attr *c, *cur, *dcur;
218         unsigned r, rem, drem;
219         const char *name;
220         bool matches;
221
222         blobmsg_for_each_attr(c, interfaces, r) {
223                 name = NULL;
224                 matches = false;
225
226                 blobmsg_for_each_attr(cur, c, rem) {
227                         if (!strcmp(blobmsg_name(cur), "interface"))
228                                 name = blobmsg_get_string(cur);
229                         else if (!strcmp(blobmsg_name(cur), "data"))
230                                 blobmsg_for_each_attr(dcur, cur, drem)
231                                         if (!strcmp(blobmsg_name(dcur), "zone"))
232                                                 matches = !strcmp(blobmsg_get_string(dcur), zone->name);
233                 }
234
235                 if (name && matches)
236                         fw3_parse_device(&zone->networks, name, true);
237         }
238 }
239
240 void
241 fw3_ubus_rules(struct blob_buf *b)
242 {
243         blob_buf_init(b, 0);
244
245         struct blob_attr *c, *cur, *dcur, *rule, *ropt;
246         unsigned r, rem, drem, rrem, orem;
247
248         blobmsg_for_each_attr(c, interfaces, r) {
249                 const char *l3_device = NULL;
250                 struct blob_attr *data = NULL;
251
252                 blobmsg_for_each_attr(cur, c, rem) {
253                         if (!strcmp(blobmsg_name(cur), "l3_device"))
254                                 l3_device = blobmsg_get_string(cur);
255                         else if (!strcmp(blobmsg_name(cur), "data"))
256                                 data = cur;
257                 }
258
259                 if (!data || !l3_device)
260                         continue;
261
262                 blobmsg_for_each_attr(dcur, data, drem) {
263                         if (strcmp(blobmsg_name(dcur), "firewall"))
264                                 continue;
265
266                         blobmsg_for_each_attr(rule, dcur, rrem) {
267                                 void *k = blobmsg_open_table(b, "");
268
269                                 blobmsg_for_each_attr(ropt, rule, orem)
270                                         if (strcmp(blobmsg_name(ropt), "device"))
271                                                 blobmsg_add_blob(b, ropt);
272
273                                 blobmsg_add_string(b, "device", l3_device);
274                                 blobmsg_close_table(b, k);
275                         }
276                 }
277         }
278
279         if (!procd_data)
280                 return;
281
282         /* service */
283         blobmsg_for_each_attr(c, procd_data, r) {
284                 if (!blobmsg_check_attr(c, true))
285                         continue;
286
287                 /* instance */
288                 blobmsg_for_each_attr(cur, c, rem) {
289                         if (!blobmsg_check_attr(cur, true))
290                                 continue;
291
292                         /* type */
293                         blobmsg_for_each_attr(dcur, cur, drem) {
294                                 if (!blobmsg_check_attr(dcur, true))
295                                         continue;
296
297                                 blobmsg_for_each_attr(rule, dcur, rrem)
298                                         blobmsg_add_blob(b, rule);
299                         }
300                 }
301         }
302 }