Remove src_flags and running_src_flags from fw3_zone struct, rename dst_flags and...
[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
22 static struct ubus_context *ctx = NULL;
23
24 bool
25 fw3_ubus_connect(void)
26 {
27         ctx = ubus_connect(NULL);
28         return !!ctx;
29 }
30
31 void
32 fw3_ubus_disconnect(void)
33 {
34         if (!ctx)
35                 return;
36
37         ubus_free(ctx);
38         ctx = NULL;
39 }
40
41 static struct fw3_address *
42 parse_subnet(enum fw3_family family, struct blob_attr *dict, int rem)
43 {
44         struct blob_attr *cur;
45         struct fw3_address *addr;
46
47         addr = malloc(sizeof(*addr));
48
49         if (!addr)
50                 return NULL;
51
52         memset(addr, 0, sizeof(*addr));
53
54         addr->set = true;
55         addr->family = family;
56
57         __blob_for_each_attr(cur, dict, rem)
58         {
59                 if (!strcmp(blobmsg_name(cur), "address"))
60                         inet_pton(family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
61                                   blobmsg_data(cur), &addr->address.v6);
62
63                 else if (!strcmp(blobmsg_name(cur), "mask"))
64                         addr->mask = be32_to_cpu(*(uint32_t *)blobmsg_data(cur));
65         }
66
67         return addr;
68 }
69
70 static void
71 parse_subnets(struct list_head *head, enum fw3_family family,
72               struct blob_attr *list, int rem)
73 {
74         struct blob_attr *cur;
75         struct fw3_address *addr;
76
77         __blob_for_each_attr(cur, list, rem)
78         {
79                 addr = parse_subnet(family, blobmsg_data(cur), blobmsg_data_len(cur));
80
81                 if (addr)
82                         list_add_tail(&addr->list, head);
83         }
84 }
85
86 struct dev_addr
87 {
88         struct fw3_device *dev;
89         struct list_head *addr;
90 };
91
92 static void
93 invoke_cb(struct ubus_request *req, int type, struct blob_attr *msg)
94 {
95         int rem;
96         char *data;
97         struct blob_attr *cur;
98         struct dev_addr *da = (struct dev_addr *)req->priv;
99         struct fw3_device *dev = da->dev;
100
101         if (!msg)
102                 return;
103
104         rem = blob_len(msg);
105         __blob_for_each_attr(cur, blob_data(msg), rem)
106         {
107                 data = blobmsg_data(cur);
108
109                 if (dev && !strcmp(blobmsg_name(cur), "device") && !dev->name[0])
110                         snprintf(dev->name, sizeof(dev->name), "%s", data);
111                 else if (dev && !strcmp(blobmsg_name(cur), "l3_device"))
112                         snprintf(dev->name, sizeof(dev->name), "%s", data);
113                 else if (!dev && !strcmp(blobmsg_name(cur), "ipv4-address"))
114                         parse_subnets(da->addr, FW3_FAMILY_V4,
115                                       blobmsg_data(cur), blobmsg_data_len(cur));
116                 else if (!dev && !strcmp(blobmsg_name(cur), "ipv6-address"))
117                         parse_subnets(da->addr, FW3_FAMILY_V6,
118                                       blobmsg_data(cur), blobmsg_data_len(cur));
119         }
120
121         if (dev)
122                 dev->set = !!dev->name[0];
123 }
124
125 static void *
126 invoke_common(const char *net, bool dev)
127 {
128         uint32_t id;
129         char path[128];
130         static struct dev_addr da;
131
132         if (!net)
133                 return NULL;
134
135         memset(&da, 0, sizeof(da));
136
137         if (dev)
138                 da.dev = malloc(sizeof(*da.dev));
139         else
140                 da.addr = malloc(sizeof(*da.addr));
141
142         if ((dev && !da.dev) || (!dev && !da.addr))
143                 goto fail;
144
145         if (dev)
146                 memset(da.dev, 0, sizeof(*da.dev));
147         else
148                 INIT_LIST_HEAD(da.addr);
149
150         snprintf(path, sizeof(path), "network.interface.%s", net);
151
152         if (ubus_lookup_id(ctx, path, &id))
153                 goto fail;
154
155         if (ubus_invoke(ctx, id, "status", NULL, invoke_cb, &da, 500))
156                 goto fail;
157
158         if (dev && da.dev->set)
159                 return da.dev;
160         else if (!dev && !list_empty(da.addr))
161                 return da.addr;
162
163 fail:
164         if (da.dev)
165                 free(da.dev);
166
167         if (da.addr)
168                 free(da.addr);
169
170         return NULL;
171 }
172
173 struct fw3_device *
174 fw3_ubus_device(const char *net)
175 {
176         return invoke_common(net, true);
177 }
178
179 struct list_head *
180 fw3_ubus_address(const char *net)
181 {
182         return invoke_common(net, false);
183 }
184
185 void
186 fw3_ubus_address_free(struct list_head *list)
187 {
188         struct fw3_address *addr, *tmp;
189
190         list_for_each_entry_safe(addr, tmp, list, list)
191         {
192                 list_del(&addr->list);
193                 free(addr);
194         }
195
196         free(list);
197 }