Also read addresses from "ipv6-prefix-assignment" ifstatus table
[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 !strcmp(blobmsg_name(cur), "ipv6-prefix-assignment")))
118 parse_subnets(da->addr, FW3_FAMILY_V6,
119 blobmsg_data(cur), blobmsg_data_len(cur));
120 }
121
122 if (dev)
123 dev->set = !!dev->name[0];
124 }
125
126 static void *
127 invoke_common(const char *net, bool dev)
128 {
129 uint32_t id;
130 char path[128];
131 static struct dev_addr da;
132
133 if (!net)
134 return NULL;
135
136 memset(&da, 0, sizeof(da));
137
138 if (dev)
139 da.dev = malloc(sizeof(*da.dev));
140 else
141 da.addr = malloc(sizeof(*da.addr));
142
143 if ((dev && !da.dev) || (!dev && !da.addr))
144 goto fail;
145
146 if (dev)
147 memset(da.dev, 0, sizeof(*da.dev));
148 else
149 INIT_LIST_HEAD(da.addr);
150
151 snprintf(path, sizeof(path), "network.interface.%s", net);
152
153 if (ubus_lookup_id(ctx, path, &id))
154 goto fail;
155
156 if (ubus_invoke(ctx, id, "status", NULL, invoke_cb, &da, 500))
157 goto fail;
158
159 if (dev && da.dev->set)
160 return da.dev;
161 else if (!dev && !list_empty(da.addr))
162 return da.addr;
163
164 fail:
165 if (da.dev)
166 free(da.dev);
167
168 if (da.addr)
169 free(da.addr);
170
171 return NULL;
172 }
173
174 struct fw3_device *
175 fw3_ubus_device(const char *net)
176 {
177 return invoke_common(net, true);
178 }
179
180 struct list_head *
181 fw3_ubus_address(const char *net)
182 {
183 return invoke_common(net, false);
184 }
185
186 void
187 fw3_ubus_address_free(struct list_head *list)
188 {
189 struct fw3_address *addr, *tmp;
190
191 list_for_each_entry_safe(addr, tmp, list, list)
192 {
193 list_del(&addr->list);
194 free(addr);
195 }
196
197 free(list);
198 }