3 #include <libubox/uloop.h>
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;
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)
23 void *a = blobmsg_open_table(&b, "device");
24 time_t now = odhcpd_time();
26 struct interface *iface;
27 list_for_each_entry(iface, &interfaces, head) {
28 if (iface->dhcpv4 != RELAYD_SERVER)
31 void *i = blobmsg_open_table(&b, iface->ifname);
32 void *j = blobmsg_open_array(&b, "leases");
34 struct dhcpv4_assignment *lease;
35 list_for_each_entry(lease, &iface->dhcpv4_assignments, head) {
36 if (lease->valid_until < now)
39 void *l = blobmsg_open_table(&b, NULL);
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);
45 blobmsg_add_string(&b, "hostname", lease->hostname);
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);
52 blobmsg_add_u32(&b, "valid", now - lease->valid_until);
54 blobmsg_close_table(&b, l);
57 blobmsg_close_array(&b, j);
58 blobmsg_close_table(&b, i);
61 blobmsg_close_table(&b, a);
62 ubus_send_reply(ctx, req, b.head);
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)
72 void *a = blobmsg_open_table(&b, "device");
73 time_t now = odhcpd_time();
75 struct interface *iface;
76 list_for_each_entry(iface, &interfaces, head) {
77 if (iface->dhcpv6 != RELAYD_SERVER)
80 void *i = blobmsg_open_table(&b, iface->ifname);
81 void *j = blobmsg_open_array(&b, "leases");
83 struct dhcpv6_assignment *lease;
84 list_for_each_entry(lease, &iface->ia_assignments, head) {
85 if (lease->valid_until < now)
88 void *l = blobmsg_open_table(&b, NULL);
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);
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);
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)
105 addr = iface->ia_addr[i].addr;
106 if (lease->length == 128)
107 addr.s6_addr32[3] = htonl(lease->assigned);
109 addr.s6_addr32[1] |= htonl(lease->assigned);
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);
115 blobmsg_close_table(&b, m);
117 blobmsg_add_u32(&b, "valid", now - lease->valid_until);
119 blobmsg_close_table(&b, l);
122 blobmsg_close_array(&b, j);
123 blobmsg_close_table(&b, i);
126 blobmsg_close_table(&b, a);
127 ubus_send_reply(ctx, req, b.head);
132 static struct ubus_method main_object_methods[] = {
133 {.name = "ipv4leases", .handler = handle_dhcpv4_leases},
134 {.name = "ipv6leases", .handler = handle_dhcpv6_leases},
137 static struct ubus_object_type main_object_type =
138 UBUS_OBJECT_TYPE("dhcp", main_object_methods);
140 static struct ubus_object main_object = {
142 .type = &main_object_type,
143 .methods = main_object_methods,
144 .n_methods = ARRAY_SIZE(main_object_methods),
153 static const struct blobmsg_policy dump_attrs[DUMP_ATTR_MAX] = {
154 [DUMP_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_ARRAY },
159 IFACE_ATTR_INTERFACE,
168 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
169 [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
170 [IFACE_ATTR_IFNAME] = { .name = "l3_device", .type = BLOBMSG_TYPE_STRING },
171 [IFACE_ATTR_UP] = { .name = "up", .type = BLOBMSG_TYPE_BOOL },
172 [IFACE_ATTR_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
173 [IFACE_ATTR_PREFIX] = { .name = "ipv6-prefix", .type = BLOBMSG_TYPE_ARRAY },
174 [IFACE_ATTR_ADDRESS] = { .name = "ipv6-address", .type = BLOBMSG_TYPE_ARRAY },
177 static void handle_dump(_unused struct ubus_request *req, _unused int type, struct blob_attr *msg)
179 struct blob_attr *tb[DUMP_ATTR_INTERFACE];
180 blobmsg_parse(dump_attrs, DUMP_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
182 if (!tb[DUMP_ATTR_INTERFACE])
186 dump = blob_memdup(tb[DUMP_ATTR_INTERFACE]);
191 static int handle_update(_unused struct ubus_context *ctx, _unused struct ubus_object *obj,
192 _unused struct ubus_request_data *req, _unused const char *method,
193 struct blob_attr *msg)
195 struct blob_attr *tb[IFACE_ATTR_MAX];
196 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
198 const char *interface = (tb[IFACE_ATTR_INTERFACE]) ?
199 blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]) : "";
200 const char *ifname = (tb[IFACE_ATTR_IFNAME]) ?
201 blobmsg_get_string(tb[IFACE_ATTR_IFNAME]) : "";
203 struct interface *c, *iface = NULL;
204 list_for_each_entry(c, &interfaces, head)
205 if (!strcmp(interface, c->name) || !strcmp(ifname, c->ifname))
208 if (iface && iface->ignore)
211 ubus_invoke(ubus, objid, "dump", NULL, handle_dump, NULL, 0);
216 static void subscribe_netifd(void)
218 ubus_subscribe(ubus, &netifd, objid);
219 ubus_invoke(ubus, objid, "dump", NULL, handle_dump, NULL, 0);
223 void ubus_apply_network(void)
231 blobmsg_for_each_attr(c, dump, rem) {
232 struct blob_attr *tb[IFACE_ATTR_MAX];
233 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(c), blobmsg_data_len(c));
235 if (!tb[IFACE_ATTR_INTERFACE] || !tb[IFACE_ATTR_DATA])
238 const char *interface = (tb[IFACE_ATTR_INTERFACE]) ?
239 blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]) : "";
240 const char *ifname = (tb[IFACE_ATTR_IFNAME]) ?
241 blobmsg_get_string(tb[IFACE_ATTR_IFNAME]) : "";
243 bool matched = false;
245 list_for_each_entry(c, &interfaces, head) {
246 char *f = memmem(c->upstream, c->upstream_len,
247 interface, strlen(interface) + 1);
248 bool cmatched = !strcmp(interface, c->name) || !strcmp(ifname, c->ifname);
251 if (!cmatched && (!c->upstream_len || !f || (f != c->upstream && f[-1] != 0)))
255 config_parse_interface(blobmsg_data(tb[IFACE_ATTR_DATA]),
256 blobmsg_data_len(tb[IFACE_ATTR_DATA]), c->name, false);
260 config_parse_interface(blobmsg_data(tb[IFACE_ATTR_DATA]),
261 blobmsg_data_len(tb[IFACE_ATTR_DATA]), interface, false);
272 static const struct blobmsg_policy obj_attrs[OBJ_ATTR_MAX] = {
273 [OBJ_ATTR_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
274 [OBJ_ATTR_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
278 static void handle_event(_unused struct ubus_context *ctx, _unused struct ubus_event_handler *ev,
279 _unused const char *type, struct blob_attr *msg)
281 struct blob_attr *tb[OBJ_ATTR_MAX];
282 blobmsg_parse(obj_attrs, OBJ_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
284 if (!tb[OBJ_ATTR_ID] || !tb[OBJ_ATTR_PATH])
287 if (strcmp(blobmsg_get_string(tb[OBJ_ATTR_PATH]), "network.interface"))
290 objid = blobmsg_get_u32(tb[OBJ_ATTR_ID]);
294 static struct ubus_event_handler event_handler = { .cb = handle_event };
297 const char* ubus_get_ifname(const char *name)
305 blobmsg_for_each_attr(c, dump, rem) {
306 struct blob_attr *tb[IFACE_ATTR_MAX];
307 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(c), blobmsg_data_len(c));
309 if (!tb[IFACE_ATTR_INTERFACE] || strcmp(name,
310 blobmsg_get_string(tb[IFACE_ATTR_INTERFACE])))
313 if (tb[IFACE_ATTR_IFNAME])
314 return blobmsg_get_string(tb[IFACE_ATTR_IFNAME]);
321 bool ubus_has_prefix(const char *name, const char *ifname)
323 struct blob_attr *c, *cur;
329 blobmsg_for_each_attr(c, dump, rem) {
330 struct blob_attr *tb[IFACE_ATTR_MAX];
331 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(c), blobmsg_data_len(c));
333 if (!tb[IFACE_ATTR_INTERFACE] || !tb[IFACE_ATTR_IFNAME])
336 if (strcmp(name, blobmsg_get_string(tb[IFACE_ATTR_INTERFACE])) ||
337 strcmp(ifname, blobmsg_get_string(tb[IFACE_ATTR_IFNAME])))
340 if ((cur = tb[IFACE_ATTR_PREFIX])) {
341 if (blobmsg_type(cur) != BLOBMSG_TYPE_ARRAY || !blobmsg_check_attr(cur, NULL))
346 blobmsg_for_each_attr(d, cur, drem) {
362 static const struct blobmsg_policy addr_attrs[ADDR_ATTR_MAX] = {
363 [ADDR_ATTR_ADDR] = { .name = "address", .type = BLOBMSG_TYPE_STRING },
364 [ADDR_ATTR_CLASS] = { .name = "class", .type = BLOBMSG_TYPE_STRING },
367 bool ubus_get_class(const char *ifname, const struct in6_addr *addr, uint16_t *pclass)
369 struct blob_attr *c, *cur;
375 blobmsg_for_each_attr(c, dump, rem) {
376 struct blob_attr *tb[IFACE_ATTR_MAX];
377 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(c), blobmsg_data_len(c));
379 if (!tb[IFACE_ATTR_IFNAME])
382 if (strcmp(ifname, blobmsg_get_string(tb[IFACE_ATTR_IFNAME])))
385 if ((cur = tb[IFACE_ATTR_ADDRESS])) {
386 if (blobmsg_type(cur) != BLOBMSG_TYPE_ARRAY || !blobmsg_check_attr(cur, NULL))
391 blobmsg_for_each_attr(d, cur, drem) {
392 struct blob_attr *t[ADDR_ATTR_MAX];
393 blobmsg_parse(addr_attrs, ADDR_ATTR_MAX, t, blobmsg_data(d), blobmsg_data_len(d));
395 if (!t[ADDR_ATTR_ADDR] || !t[ADDR_ATTR_CLASS])
398 const char *addrs = blobmsg_get_string(t[ADDR_ATTR_ADDR]);
399 const char *class = blobmsg_get_string(t[ADDR_ATTR_CLASS]);
401 struct in6_addr ip6addr;
402 inet_pton(AF_INET6, addrs, &ip6addr);
404 if (IN6_ARE_ADDR_EQUAL(&ip6addr, addr)) {
405 *pclass = atoi(class);
420 if (!(ubus = ubus_connect(NULL))) {
421 syslog(LOG_ERR, "Unable to connect to ubus: %s", strerror(errno));
425 netifd.cb = handle_update;
426 ubus_register_subscriber(ubus, &netifd);
428 ubus_add_uloop(ubus);
429 ubus_add_object(ubus, &main_object);
430 ubus_register_event_handler(ubus, &event_handler, "ubus.object.add");
431 if (!ubus_lookup_id(ubus, "network.interface", &objid))