3 #include <libubox/uloop.h>
4 #include <netinet/in.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;
16 static struct ubus_request req_dump = { .list = LIST_HEAD_INIT(req_dump.list) };
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)
22 struct interface *iface;
23 time_t now = odhcpd_time();
27 a = blobmsg_open_table(&b, "device");
29 list_for_each_entry(iface, &interfaces, head) {
30 if (iface->dhcpv4 != RELAYD_SERVER || iface->dhcpv4_assignments.next == NULL)
33 void *i = blobmsg_open_table(&b, iface->ifname);
34 void *j = blobmsg_open_array(&b, "leases");
36 struct dhcpv4_assignment *c;
37 list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
38 if (!INFINITE_VALID(c->valid_until) && c->valid_until < now)
41 void *m, *l = blobmsg_open_table(&b, NULL);
42 char *buf = blobmsg_alloc_string_buffer(&b, "mac", 13);
44 odhcpd_hexlify(buf, c->hwaddr, sizeof(c->hwaddr));
45 blobmsg_add_string_buffer(&b);
47 blobmsg_add_string(&b, "hostname", (c->hostname) ? c->hostname : "");
49 m = blobmsg_open_array(&b, "flags");
50 if (c->flags & OAF_BOUND)
51 blobmsg_add_string(&b, NULL, "bound");
53 if (c->flags & OAF_STATIC)
54 blobmsg_add_string(&b, NULL, "static");
55 blobmsg_close_array(&b, m);
57 buf = blobmsg_alloc_string_buffer(&b, "ip", INET_ADDRSTRLEN);
58 struct in_addr addr = {htonl(c->addr)};
59 inet_ntop(AF_INET, &addr, buf, INET_ADDRSTRLEN);
60 blobmsg_add_string_buffer(&b);
62 blobmsg_add_u32(&b, "valid", INFINITE_VALID(c->valid_until) ?
63 (uint32_t)-1 : (uint32_t)(c->valid_until - now));
65 blobmsg_close_table(&b, l);
68 blobmsg_close_array(&b, j);
69 blobmsg_close_table(&b, i);
72 blobmsg_close_table(&b, a);
73 ubus_send_reply(ctx, req, b.head);
78 static void dhcpv6_blobmsg_ia_addr(struct in6_addr *addr, int prefix, uint32_t pref,
79 uint32_t valid, _unused void *arg)
81 void *a = blobmsg_open_table(&b, NULL);
82 char *buf = blobmsg_alloc_string_buffer(&b, NULL, INET6_ADDRSTRLEN);
84 inet_ntop(AF_INET6, addr, buf, INET6_ADDRSTRLEN);
85 blobmsg_add_string_buffer(&b);
86 blobmsg_add_u32(&b, "preferred-lifetime",
87 pref == UINT32_MAX ? (uint32_t)-1 : pref);
88 blobmsg_add_u32(&b, "valid-lifetime",
89 valid == UINT32_MAX ? (uint32_t)-1 : valid);
92 blobmsg_add_u32(&b, "prefix-length", prefix);
94 blobmsg_close_table(&b, a);
97 static int handle_dhcpv6_leases(_unused struct ubus_context *ctx, _unused struct ubus_object *obj,
98 _unused struct ubus_request_data *req, _unused const char *method,
99 _unused struct blob_attr *msg)
101 struct interface *iface;
102 time_t now = odhcpd_time();
105 blob_buf_init(&b, 0);
106 a = blobmsg_open_table(&b, "device");
108 list_for_each_entry(iface, &interfaces, head) {
109 if (iface->dhcpv6 != RELAYD_SERVER || iface->ia_assignments.next == NULL)
112 void *i = blobmsg_open_table(&b, iface->ifname);
113 void *j = blobmsg_open_array(&b, "leases");
115 struct dhcpv6_assignment *a, *border = list_last_entry(
116 &iface->ia_assignments, struct dhcpv6_assignment, head);
118 list_for_each_entry(a, &iface->ia_assignments, head) {
119 if (a == border || (!INFINITE_VALID(a->valid_until) &&
120 a->valid_until < now))
123 void *m, *l = blobmsg_open_table(&b, NULL);
124 char *buf = blobmsg_alloc_string_buffer(&b, "duid", 264);
126 odhcpd_hexlify(buf, a->clid_data, a->clid_len);
127 blobmsg_add_string_buffer(&b);
129 blobmsg_add_u32(&b, "iaid", ntohl(a->iaid));
130 blobmsg_add_string(&b, "hostname", (a->hostname) ? a->hostname : "");
131 blobmsg_add_u32(&b, "assigned", a->assigned);
133 m = blobmsg_open_array(&b, "flags");
134 if (a->flags & OAF_BOUND)
135 blobmsg_add_string(&b, NULL, "bound");
137 if (a->flags & OAF_STATIC)
138 blobmsg_add_string(&b, NULL, "static");
139 blobmsg_close_array(&b, m);
141 m = blobmsg_open_array(&b, a->length == 128 ? "ipv6-addr": "ipv6-prefix");
142 dhcpv6_enum_ia_addrs(iface, a, now, dhcpv6_blobmsg_ia_addr, NULL);
143 blobmsg_close_table(&b, m);
145 blobmsg_add_u32(&b, "valid", INFINITE_VALID(a->valid_until) ?
146 (uint32_t)-1 : (uint32_t)(a->valid_until - now));
148 blobmsg_close_table(&b, l);
151 blobmsg_close_array(&b, j);
152 blobmsg_close_table(&b, i);
155 blobmsg_close_table(&b, a);
156 ubus_send_reply(ctx, req, b.head);
161 static struct ubus_method main_object_methods[] = {
162 {.name = "ipv4leases", .handler = handle_dhcpv4_leases},
163 {.name = "ipv6leases", .handler = handle_dhcpv6_leases},
166 static struct ubus_object_type main_object_type =
167 UBUS_OBJECT_TYPE("dhcp", main_object_methods);
169 static struct ubus_object main_object = {
171 .type = &main_object_type,
172 .methods = main_object_methods,
173 .n_methods = ARRAY_SIZE(main_object_methods),
182 static const struct blobmsg_policy dump_attrs[DUMP_ATTR_MAX] = {
183 [DUMP_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_ARRAY },
188 IFACE_ATTR_INTERFACE,
197 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
198 [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
199 [IFACE_ATTR_IFNAME] = { .name = "l3_device", .type = BLOBMSG_TYPE_STRING },
200 [IFACE_ATTR_UP] = { .name = "up", .type = BLOBMSG_TYPE_BOOL },
201 [IFACE_ATTR_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
202 [IFACE_ATTR_PREFIX] = { .name = "ipv6-prefix", .type = BLOBMSG_TYPE_ARRAY },
203 [IFACE_ATTR_ADDRESS] = { .name = "ipv6-address", .type = BLOBMSG_TYPE_ARRAY },
206 static void handle_dump(_unused struct ubus_request *req, _unused int type, struct blob_attr *msg)
208 struct blob_attr *tb[DUMP_ATTR_MAX];
209 blobmsg_parse(dump_attrs, DUMP_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
211 if (!tb[DUMP_ATTR_INTERFACE])
215 dump = blob_memdup(tb[DUMP_ATTR_INTERFACE]);
220 static void update_netifd(bool subscribe)
223 ubus_subscribe(ubus, &netifd, objid);
225 ubus_abort_request(ubus, &req_dump);
226 if (!ubus_invoke_async(ubus, objid, "dump", NULL, &req_dump)) {
227 req_dump.data_cb = handle_dump;
228 ubus_complete_request_async(ubus, &req_dump);
233 static int handle_update(_unused struct ubus_context *ctx, _unused struct ubus_object *obj,
234 _unused struct ubus_request_data *req, _unused const char *method,
235 struct blob_attr *msg)
237 struct blob_attr *tb[IFACE_ATTR_MAX];
238 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
240 const char *interface = (tb[IFACE_ATTR_INTERFACE]) ?
241 blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]) : "";
242 const char *ifname = (tb[IFACE_ATTR_IFNAME]) ?
243 blobmsg_get_string(tb[IFACE_ATTR_IFNAME]) : "";
245 struct interface *c, *iface = NULL;
246 list_for_each_entry(c, &interfaces, head)
247 if (!strcmp(interface, c->name) || !strcmp(ifname, c->ifname))
250 if (iface && iface->ignore)
253 update_netifd(false);
258 void ubus_apply_network(void)
266 blobmsg_for_each_attr(a, dump, rem) {
267 struct blob_attr *tb[IFACE_ATTR_MAX];
268 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(a), blobmsg_data_len(a));
270 if (!tb[IFACE_ATTR_INTERFACE] || !tb[IFACE_ATTR_DATA])
273 const char *interface = (tb[IFACE_ATTR_INTERFACE]) ?
274 blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]) : "";
275 const char *ifname = (tb[IFACE_ATTR_IFNAME]) ?
276 blobmsg_get_string(tb[IFACE_ATTR_IFNAME]) : "";
278 bool matched = false;
279 struct interface *c, *n;
280 list_for_each_entry_safe(c, n, &interfaces, head) {
281 char *f = memmem(c->upstream, c->upstream_len,
282 interface, strlen(interface) + 1);
283 bool cmatched = !strcmp(interface, c->name) || !strcmp(ifname, c->ifname);
286 if (!cmatched && (!c->upstream_len || !f || (f != c->upstream && f[-1] != 0)))
290 config_parse_interface(blobmsg_data(tb[IFACE_ATTR_DATA]),
291 blobmsg_data_len(tb[IFACE_ATTR_DATA]), c->name, false);
295 config_parse_interface(blobmsg_data(tb[IFACE_ATTR_DATA]),
296 blobmsg_data_len(tb[IFACE_ATTR_DATA]), interface, false);
307 static const struct blobmsg_policy obj_attrs[OBJ_ATTR_MAX] = {
308 [OBJ_ATTR_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
309 [OBJ_ATTR_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
313 static void handle_event(_unused struct ubus_context *ctx, _unused struct ubus_event_handler *ev,
314 _unused const char *type, struct blob_attr *msg)
316 struct blob_attr *tb[OBJ_ATTR_MAX];
317 blobmsg_parse(obj_attrs, OBJ_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
319 if (!tb[OBJ_ATTR_ID] || !tb[OBJ_ATTR_PATH])
322 if (strcmp(blobmsg_get_string(tb[OBJ_ATTR_PATH]), "network.interface"))
325 objid = blobmsg_get_u32(tb[OBJ_ATTR_ID]);
329 static struct ubus_event_handler event_handler = { .cb = handle_event };
332 const char* ubus_get_ifname(const char *name)
340 blobmsg_for_each_attr(c, dump, rem) {
341 struct blob_attr *tb[IFACE_ATTR_MAX];
342 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(c), blobmsg_data_len(c));
344 if (!tb[IFACE_ATTR_INTERFACE] || strcmp(name,
345 blobmsg_get_string(tb[IFACE_ATTR_INTERFACE])))
348 if (tb[IFACE_ATTR_IFNAME])
349 return blobmsg_get_string(tb[IFACE_ATTR_IFNAME]);
356 bool ubus_has_prefix(const char *name, const char *ifname)
358 struct blob_attr *c, *cur;
364 blobmsg_for_each_attr(c, dump, rem) {
365 struct blob_attr *tb[IFACE_ATTR_MAX];
366 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(c), blobmsg_data_len(c));
368 if (!tb[IFACE_ATTR_INTERFACE] || !tb[IFACE_ATTR_IFNAME])
371 if (strcmp(name, blobmsg_get_string(tb[IFACE_ATTR_INTERFACE])) ||
372 strcmp(ifname, blobmsg_get_string(tb[IFACE_ATTR_IFNAME])))
375 if ((cur = tb[IFACE_ATTR_PREFIX])) {
376 if (blobmsg_type(cur) != BLOBMSG_TYPE_ARRAY || !blobmsg_check_attr(cur, false))
381 blobmsg_for_each_attr(d, cur, drem) {
393 if (!(ubus = ubus_connect(NULL))) {
394 syslog(LOG_ERR, "Unable to connect to ubus: %s", strerror(errno));
398 netifd.cb = handle_update;
399 ubus_register_subscriber(ubus, &netifd);
401 ubus_add_uloop(ubus);
402 ubus_add_object(ubus, &main_object);
403 ubus_register_event_handler(ubus, &event_handler, "ubus.object.add");
404 if (!ubus_lookup_id(ubus, "network.interface", &objid))