make fw3_ubus_address take a list_head * argument instead of allocating & returning one
[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
23
24 static void dump_cb(struct ubus_request *req, int type, struct blob_attr *msg)
25 {
26         static const struct blobmsg_policy policy = { "interface", BLOBMSG_TYPE_ARRAY };
27         struct blob_attr *cur;
28
29         blobmsg_parse(&policy, 1, &cur, blob_data(msg), blob_len(msg));
30         if (cur)
31                 interfaces = blob_memdup(cur);
32 }
33
34 bool
35 fw3_ubus_connect(void)
36 {
37         bool status = false;
38         uint32_t id;
39         struct ubus_context *ctx = ubus_connect(NULL);
40
41         if (!ctx)
42                 goto out;
43
44         if (ubus_lookup_id(ctx, "network.interface", &id))
45                 goto out;
46
47         if (ubus_invoke(ctx, id, "dump", NULL, dump_cb, NULL, 500))
48                 goto out;
49
50         status = true;
51
52 out:
53         if (ctx)
54                 ubus_free(ctx);
55         return status;
56 }
57
58 void
59 fw3_ubus_disconnect(void)
60 {
61         free(interfaces);
62         interfaces = NULL;
63 }
64
65 static struct fw3_address *
66 parse_subnet(enum fw3_family family, struct blob_attr *dict, int rem)
67 {
68         struct blob_attr *cur;
69         struct fw3_address *addr;
70
71         addr = calloc(1, sizeof(*addr));
72         if (!addr)
73                 return NULL;
74
75         addr->set = true;
76         addr->family = family;
77
78         __blob_for_each_attr(cur, dict, rem)
79         {
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);
83
84                 else if (!strcmp(blobmsg_name(cur), "mask"))
85                         addr->mask = be32_to_cpu(*(uint32_t *)blobmsg_data(cur));
86         }
87
88         return addr;
89 }
90
91 static void
92 parse_subnets(struct list_head *head, enum fw3_family family,
93               struct blob_attr *list)
94 {
95         struct blob_attr *cur;
96         struct fw3_address *addr;
97         int rem;
98
99         if (!list)
100                 return;
101
102         blob_for_each_attr(cur, list, rem)
103         {
104                 addr = parse_subnet(family, blobmsg_data(cur), blobmsg_data_len(cur));
105
106                 if (addr)
107                         list_add_tail(&addr->list, head);
108         }
109 }
110
111 struct fw3_device *
112 fw3_ubus_device(const char *net)
113 {
114         struct fw3_device *dev = NULL;
115         struct blob_attr *c, *cur;
116         unsigned r, rem;
117         char *data;
118         bool matched;
119
120         if (!net || !interfaces)
121                 return NULL;
122
123         dev = calloc(1, sizeof(*dev));
124         if (!dev)
125                 return NULL;
126
127         blobmsg_for_each_attr(c, interfaces, r) {
128                 matched = false;
129                 blobmsg_for_each_attr(cur, c, rem)
130                         if (!strcmp(blobmsg_name(cur), "interface"))
131                                 matched = !strcmp(blobmsg_get_string(cur), net);
132
133                 if (!matched)
134                         continue;
135
136                 blobmsg_for_each_attr(cur, c, rem) {
137                         data = blobmsg_data(cur);
138
139                         if (!strcmp(blobmsg_name(cur), "device") && !dev->name[0])
140                                 snprintf(dev->name, sizeof(dev->name), "%s", data);
141                         else if (!strcmp(blobmsg_name(cur), "l3_device"))
142                                 snprintf(dev->name, sizeof(dev->name), "%s", data);
143                 }
144
145                 dev->set = !!dev->name[0];
146                 return dev;
147         }
148
149         return NULL;
150 }
151
152 void
153 fw3_ubus_address(struct list_head *list, const char *net)
154 {
155         enum {
156                 ADDR_INTERFACE,
157                 ADDR_IPV4,
158                 ADDR_IPV6,
159                 ADDR_IPV6_PREFIX,
160                 __ADDR_MAX
161         };
162         static const struct blobmsg_policy policy[__ADDR_MAX] = {
163                 [ADDR_INTERFACE] = { "interface", BLOBMSG_TYPE_STRING },
164                 [ADDR_IPV4] = { "ipv4-address", BLOBMSG_TYPE_ARRAY },
165                 [ADDR_IPV6] = { "ipv6-address", BLOBMSG_TYPE_ARRAY },
166                 [ADDR_IPV6_PREFIX] = { "ipv6-prefix-assignment", BLOBMSG_TYPE_ARRAY },
167         };
168         struct blob_attr *tb[__ADDR_MAX];
169         struct blob_attr *cur;
170         int rem;
171
172         if (!net || !interfaces)
173                 return;
174
175         blobmsg_for_each_attr(cur, interfaces, rem) {
176                 blobmsg_parse(policy, __ADDR_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
177
178                 if (!tb[ADDR_INTERFACE] ||
179                     strcmp(blobmsg_data(tb[ADDR_INTERFACE]), net) != 0)
180                         continue;
181
182                 parse_subnets(list, FW3_FAMILY_V4, tb[ADDR_IPV4]);
183                 parse_subnets(list, FW3_FAMILY_V6, tb[ADDR_IPV6]);
184                 parse_subnets(list, FW3_FAMILY_V6, tb[ADDR_IPV6_PREFIX]);
185         }
186 }
187
188 void
189 fw3_ubus_zone_devices(struct fw3_zone *zone)
190 {
191         struct blob_attr *c, *cur, *dcur;
192         unsigned r, rem, drem;
193         const char *name;
194         bool matches;
195
196         blobmsg_for_each_attr(c, interfaces, r) {
197                 name = NULL;
198                 matches = false;
199
200                 blobmsg_for_each_attr(cur, c, rem) {
201                         if (!strcmp(blobmsg_name(cur), "interface"))
202                                 name = blobmsg_get_string(cur);
203                         else if (!strcmp(blobmsg_name(cur), "data"))
204                                 blobmsg_for_each_attr(dcur, cur, drem)
205                                         if (!strcmp(blobmsg_name(dcur), "zone"))
206                                                 matches = !strcmp(blobmsg_get_string(dcur), zone->name);
207                 }
208
209                 if (name && matches)
210                         fw3_parse_device(&zone->networks, name, true);
211         }
212 }
213
214 void
215 fw3_ubus_rules(struct blob_buf *b)
216 {
217         blob_buf_init(b, 0);
218
219         struct blob_attr *c, *cur, *dcur, *rule, *ropt;
220         unsigned r, rem, drem, rrem, orem;
221
222         blobmsg_for_each_attr(c, interfaces, r) {
223                 const char *l3_device = NULL;
224                 struct blob_attr *data = NULL;
225
226                 blobmsg_for_each_attr(cur, c, rem) {
227                         if (!strcmp(blobmsg_name(cur), "l3_device"))
228                                 l3_device = blobmsg_get_string(cur);
229                         else if (!strcmp(blobmsg_name(cur), "data"))
230                                 data = cur;
231                 }
232
233                 if (!data || !l3_device)
234                         continue;
235
236                 blobmsg_for_each_attr(dcur, data, drem) {
237                         if (strcmp(blobmsg_name(dcur), "firewall"))
238                                 continue;
239
240                         blobmsg_for_each_attr(rule, dcur, rrem) {
241                                 void *k = blobmsg_open_table(b, "");
242
243                                 blobmsg_for_each_attr(ropt, rule, orem)
244                                         if (strcmp(blobmsg_name(ropt), "device"))
245                                                 blobmsg_add_blob(b, ropt);
246
247                                 blobmsg_add_string(b, "device", l3_device);
248                                 blobmsg_close_table(b, k);
249                         }
250                 }
251         }
252 }