dc2f17135524b9e593e9fe233edf59d2a4b2eeb2
[project/odhcpd.git] / src / ubus.c
1 #include <syslog.h>
2 #include <libubus.h>
3 #include <libubox/uloop.h>
4 #include <arpa/inet.h>
5
6 #include "odhcpd.h"
7 #include "dhcpv6.h"
8 #include "dhcpv4.h"
9
10
11 static struct ubus_context *ubus = NULL;
12 static struct ubus_subscriber netifd;
13 static struct blob_buf b;
14 static struct blob_attr *dump = NULL;
15 static uint32_t objid = 0;
16
17
18 static int handle_dhcpv4_leases(struct ubus_context *ctx, _unused struct ubus_object *obj,
19                 struct ubus_request_data *req, _unused const char *method,
20                 _unused struct blob_attr *msg)
21 {
22         blob_buf_init(&b, 0);
23         void *a = blobmsg_open_table(&b, "device");
24         time_t now = odhcpd_time();
25
26         struct interface *iface;
27         list_for_each_entry(iface, &interfaces, head) {
28                 if (iface->dhcpv4 != RELAYD_SERVER)
29                         continue;
30
31                 void *i = blobmsg_open_table(&b, iface->ifname);
32                 void *j = blobmsg_open_array(&b, "leases");
33
34                 struct dhcpv4_assignment *lease;
35                 list_for_each_entry(lease, &iface->dhcpv4_assignments, head) {
36                         if (lease->valid_until < now)
37                                 continue;
38
39                         void *l = blobmsg_open_table(&b, NULL);
40
41                         char *buf = blobmsg_alloc_string_buffer(&b, "mac", 13);
42                         odhcpd_hexlify(buf, lease->hwaddr, sizeof(lease->hwaddr));
43                         blobmsg_add_string_buffer(&b);
44
45                         blobmsg_add_string(&b, "hostname", lease->hostname);
46
47                         buf = blobmsg_alloc_string_buffer(&b, "ip", INET_ADDRSTRLEN);
48                         struct in_addr addr = {htonl(lease->addr)};
49                         inet_ntop(AF_INET, &addr, buf, INET_ADDRSTRLEN);
50                         blobmsg_add_string_buffer(&b);
51
52                         blobmsg_add_u32(&b, "valid", now - lease->valid_until);
53
54                         blobmsg_close_table(&b, l);
55                 }
56
57                 blobmsg_close_array(&b, j);
58                 blobmsg_close_table(&b, i);
59         }
60
61         blobmsg_close_table(&b, a);
62         ubus_send_reply(ctx, req, b.head);
63         return 0;
64 }
65
66
67 static int handle_dhcpv6_leases(_unused struct ubus_context *ctx, _unused struct ubus_object *obj,
68                 _unused struct ubus_request_data *req, _unused const char *method,
69                 _unused struct blob_attr *msg)
70 {
71         blob_buf_init(&b, 0);
72         void *a = blobmsg_open_table(&b, "device");
73         time_t now = odhcpd_time();
74
75         struct interface *iface;
76         list_for_each_entry(iface, &interfaces, head) {
77                 if (iface->dhcpv6 != RELAYD_SERVER)
78                         continue;
79
80                 void *i = blobmsg_open_table(&b, iface->ifname);
81                 void *j = blobmsg_open_array(&b, "leases");
82
83                 struct dhcpv6_assignment *lease;
84                 list_for_each_entry(lease, &iface->ia_assignments, head) {
85                         if (lease->valid_until < now)
86                                 continue;
87
88                         void *l = blobmsg_open_table(&b, NULL);
89
90                         char *buf = blobmsg_alloc_string_buffer(&b, "duid", 264);
91                         odhcpd_hexlify(buf, lease->clid_data, lease->clid_len);
92                         blobmsg_add_string_buffer(&b);
93
94                         blobmsg_add_u32(&b, "iaid", ntohl(lease->iaid));
95                         blobmsg_add_string(&b, "hostname", (lease->hostname) ? lease->hostname : "");
96                         blobmsg_add_u32(&b, "assigned", lease->assigned);
97                         blobmsg_add_u32(&b, "length", lease->length);
98
99                         void *m = blobmsg_open_array(&b, "ipv6");
100                         struct in6_addr addr;
101                         for (size_t i = 0; i < iface->ia_addr_len; ++i) {
102                                 if (iface->ia_addr[i].prefix > 64)
103                                         continue;
104
105                                 addr = iface->ia_addr[i].addr;
106                                 if (lease->length == 128)
107                                         addr.s6_addr32[3] = htonl(lease->assigned);
108                                 else
109                                         addr.s6_addr32[1] |= htonl(lease->assigned);
110
111                                 char *c = blobmsg_alloc_string_buffer(&b, NULL, INET6_ADDRSTRLEN);
112                                 inet_ntop(AF_INET6, &addr, c, INET6_ADDRSTRLEN);
113                                 blobmsg_add_string_buffer(&b);
114                         }
115                         blobmsg_close_table(&b, m);
116
117                         blobmsg_add_u32(&b, "valid", now - lease->valid_until);
118
119                         blobmsg_close_table(&b, l);
120                 }
121
122                 blobmsg_close_array(&b, j);
123                 blobmsg_close_table(&b, i);
124         }
125
126         blobmsg_close_table(&b, a);
127         ubus_send_reply(ctx, req, b.head);
128         return 0;
129 }
130
131
132 static struct ubus_method main_object_methods[] = {
133         {.name = "ipv4leases", .handler = handle_dhcpv4_leases},
134         {.name = "ipv6leases", .handler = handle_dhcpv6_leases},
135 };
136
137 static struct ubus_object_type main_object_type =
138                 UBUS_OBJECT_TYPE("dhcp", main_object_methods);
139
140 static struct ubus_object main_object = {
141         .name = "dhcp",
142         .type = &main_object_type,
143         .methods = main_object_methods,
144         .n_methods = ARRAY_SIZE(main_object_methods),
145 };
146
147
148 enum {
149         DUMP_ATTR_INTERFACE,
150         DUMP_ATTR_MAX
151 };
152
153 static const struct blobmsg_policy dump_attrs[DUMP_ATTR_MAX] = {
154         [DUMP_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_ARRAY },
155 };
156
157
158 enum {
159         IFACE_ATTR_INTERFACE,
160         IFACE_ATTR_IFNAME,
161         IFACE_ATTR_UP,
162         IFACE_ATTR_DATA,
163         IFACE_ATTR_PREFIX,
164         IFACE_ATTR_MAX,
165 };
166
167 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
168         [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
169         [IFACE_ATTR_IFNAME] = { .name = "l3_device", .type = BLOBMSG_TYPE_STRING },
170         [IFACE_ATTR_UP] = { .name = "up", .type = BLOBMSG_TYPE_BOOL },
171         [IFACE_ATTR_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
172         [IFACE_ATTR_PREFIX] = { .name = "ipv6-prefix", .type = BLOBMSG_TYPE_ARRAY },
173 };
174
175 static void handle_dump(_unused struct ubus_request *req, _unused int type, struct blob_attr *msg)
176 {
177         struct blob_attr *tb[DUMP_ATTR_INTERFACE];
178         blobmsg_parse(dump_attrs, DUMP_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
179
180         if (!tb[DUMP_ATTR_INTERFACE])
181                 return;
182
183         free(dump);
184         dump = blob_memdup(tb[DUMP_ATTR_INTERFACE]);
185         odhcpd_reload();
186 }
187
188
189 static int handle_update(_unused struct ubus_context *ctx, _unused struct ubus_object *obj,
190                 _unused struct ubus_request_data *req, _unused const char *method,
191                 struct blob_attr *msg)
192 {
193         struct blob_attr *tb[IFACE_ATTR_MAX];
194         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
195
196         const char *interface = (tb[IFACE_ATTR_INTERFACE]) ?
197                         blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]) : "";
198         const char *ifname = (tb[IFACE_ATTR_IFNAME]) ?
199                         blobmsg_get_string(tb[IFACE_ATTR_IFNAME]) : "";
200
201         struct interface *c, *iface = NULL;
202         list_for_each_entry(c, &interfaces, head)
203                 if (!strcmp(interface, c->name) || !strcmp(ifname, c->ifname))
204                         iface = c;
205
206         if (iface && iface->ignore)
207                 return 0;
208
209         ubus_invoke(ubus, objid, "dump", NULL, handle_dump, NULL, 0);
210         return 0;
211 }
212
213
214 static void subscribe_netifd(void)
215 {
216         ubus_subscribe(ubus, &netifd, objid);
217         ubus_invoke(ubus, objid, "dump", NULL, handle_dump, NULL, 0);
218 }
219
220
221 void ubus_apply_network(void)
222 {
223         struct blob_attr *c;
224         unsigned rem;
225
226         if (!dump)
227                 return;
228
229         blobmsg_for_each_attr(c, dump, rem) {
230                 struct blob_attr *tb[IFACE_ATTR_MAX];
231                 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(c), blobmsg_data_len(c));
232
233                 if (!tb[IFACE_ATTR_INTERFACE] || !tb[IFACE_ATTR_DATA])
234                         continue;
235
236                 const char *interface = (tb[IFACE_ATTR_INTERFACE]) ?
237                                 blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]) : "";
238                 const char *ifname = (tb[IFACE_ATTR_IFNAME]) ?
239                                 blobmsg_get_string(tb[IFACE_ATTR_IFNAME]) : "";
240
241                 bool matched = false;
242                 struct interface *c;
243                 list_for_each_entry(c, &interfaces, head) {
244                         char *f = memmem(c->upstream, c->upstream_len,
245                                         interface, strlen(interface) + 1);
246                         bool cmatched = !strcmp(interface, c->name) || !strcmp(ifname, c->ifname);
247                         matched |= cmatched;
248
249                         if (!cmatched && (!c->upstream_len || !f || (f != c->upstream && f[-1] != 0)))
250                                 continue;
251
252                         if (!c->ignore)
253                                 config_parse_interface(blobmsg_data(tb[IFACE_ATTR_DATA]),
254                                                 blobmsg_data_len(tb[IFACE_ATTR_DATA]), c->name, false);
255                 }
256
257                 if (!matched)
258                         config_parse_interface(blobmsg_data(tb[IFACE_ATTR_DATA]),
259                                         blobmsg_data_len(tb[IFACE_ATTR_DATA]), interface, false);
260         }
261 }
262
263
264 enum {
265         OBJ_ATTR_ID,
266         OBJ_ATTR_PATH,
267         OBJ_ATTR_MAX
268 };
269
270 static const struct blobmsg_policy obj_attrs[OBJ_ATTR_MAX] = {
271         [OBJ_ATTR_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
272         [OBJ_ATTR_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
273 };
274
275
276 static void handle_event(_unused struct ubus_context *ctx, _unused struct ubus_event_handler *ev,
277                 _unused const char *type, struct blob_attr *msg)
278 {
279         struct blob_attr *tb[OBJ_ATTR_MAX];
280         blobmsg_parse(obj_attrs, OBJ_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
281
282         if (!tb[OBJ_ATTR_ID] || !tb[OBJ_ATTR_PATH])
283                 return;
284
285         if (strcmp(blobmsg_get_string(tb[OBJ_ATTR_PATH]), "network.interface"))
286                 return;
287
288         objid = blobmsg_get_u32(tb[OBJ_ATTR_ID]);
289         subscribe_netifd();
290 }
291
292 static struct ubus_event_handler event_handler = { .cb = handle_event };
293
294
295 const char* ubus_get_ifname(const char *name)
296 {
297         struct blob_attr *c;
298         unsigned rem;
299
300         if (!dump)
301                 return NULL;
302
303         blobmsg_for_each_attr(c, dump, rem) {
304                 struct blob_attr *tb[IFACE_ATTR_MAX];
305                 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(c), blobmsg_data_len(c));
306
307                 if (!tb[IFACE_ATTR_INTERFACE] || strcmp(name,
308                                 blobmsg_get_string(tb[IFACE_ATTR_INTERFACE])))
309                         continue;
310
311                 if (tb[IFACE_ATTR_IFNAME])
312                         return blobmsg_get_string(tb[IFACE_ATTR_IFNAME]);
313         }
314
315         return NULL;
316 }
317
318
319 bool ubus_has_prefix(const char *name, const char *ifname)
320 {
321         struct blob_attr *c, *cur;
322         unsigned rem;
323
324         if (!dump)
325                 return NULL;
326
327         blobmsg_for_each_attr(c, dump, rem) {
328                 struct blob_attr *tb[IFACE_ATTR_MAX];
329                 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(c), blobmsg_data_len(c));
330
331                 if (!tb[IFACE_ATTR_INTERFACE] || !tb[IFACE_ATTR_IFNAME])
332                         continue;
333
334                 if (!strcmp(name, blobmsg_get_string(tb[IFACE_ATTR_INTERFACE])) ||
335                                 !strcmp(ifname, blobmsg_get_string(tb[IFACE_ATTR_IFNAME])))
336                         continue;
337
338                 if ((cur = tb[IFACE_ATTR_PREFIX])) {
339                         if (blobmsg_type(cur) != BLOBMSG_TYPE_ARRAY || !blobmsg_check_attr(cur, NULL))
340                                 continue;
341
342                         struct blob_attr *d;
343                         unsigned drem;
344                         blobmsg_for_each_attr(d, cur, drem) {
345                                 return true;
346                         }
347                 }
348         }
349
350         return false;
351 }
352
353
354 int init_ubus(void)
355 {
356         if (!(ubus = ubus_connect(NULL))) {
357                 syslog(LOG_ERR, "Unable to connect to ubus: %s", strerror(errno));
358                 return -1;
359         }
360
361         netifd.cb = handle_update;
362         ubus_register_subscriber(ubus, &netifd);
363
364         ubus_add_uloop(ubus);
365         ubus_add_object(ubus, &main_object);
366         ubus_register_event_handler(ubus, &event_handler, "ubus.object.add");
367         if (!ubus_lookup_id(ubus, "network.interface", &objid))
368                 subscribe_netifd();
369
370         return 0;
371 }
372