• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/odhcpd/src/ubus.c

  1 #include <syslog.h>
  2 #include <libubus.h>
  3 #include <libubox/uloop.h>
  4 #include <netinet/in.h>
  5 #include <arpa/inet.h>
  6 
  7 #include <libubox/utils.h>
  8 
  9 #include "odhcpd.h"
 10 #include "dhcpv6.h"
 11 #include "dhcpv4.h"
 12 
 13 static struct ubus_context *ubus = NULL;
 14 static struct ubus_subscriber netifd;
 15 static struct blob_buf b;
 16 static struct blob_attr *dump = NULL;
 17 static uint32_t objid = 0;
 18 static struct ubus_request req_dump = { .list = LIST_HEAD_INIT(req_dump.list) };
 19 
 20 static int handle_dhcpv4_leases(struct ubus_context *ctx, _unused struct ubus_object *obj,
 21                 struct ubus_request_data *req, _unused const char *method,
 22                 _unused struct blob_attr *msg)
 23 {
 24         struct interface *iface;
 25         time_t now = odhcpd_time();
 26         void *a;
 27 
 28         blob_buf_init(&b, 0);
 29         a = blobmsg_open_table(&b, "device");
 30 
 31         avl_for_each_element(&interfaces, iface, avl) {
 32                 if (iface->dhcpv4 != MODE_SERVER)
 33                         continue;
 34 
 35                 void *i = blobmsg_open_table(&b, iface->ifname);
 36                 void *j = blobmsg_open_array(&b, "leases");
 37 
 38                 struct dhcp_assignment *c;
 39                 list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
 40                         if (!INFINITE_VALID(c->valid_until) && c->valid_until < now)
 41                                 continue;
 42 
 43                         void *m, *l = blobmsg_open_table(&b, NULL);
 44                         char *buf = blobmsg_alloc_string_buffer(&b, "mac", 13);
 45 
 46                         odhcpd_hexlify(buf, c->hwaddr, sizeof(c->hwaddr));
 47                         blobmsg_add_string_buffer(&b);
 48 
 49                         blobmsg_add_string(&b, "hostname", (c->hostname) ? c->hostname : "");
 50                         blobmsg_add_u8(&b, "accept-reconf-nonce", c->accept_fr_nonce);
 51 
 52                         if (c->reqopts) {
 53                                 int opt = 0;
 54                                 int chars = 0;
 55                                 buf = blobmsg_alloc_string_buffer(&b, "reqopts", strlen(c->reqopts) * 4 + 1);
 56                                 for(; c->reqopts[opt]; opt++)
 57                                         chars += snprintf(buf + chars, 6, "%u,", (uint8_t)c->reqopts[opt]);
 58                                 buf[chars - 1] = '\0';
 59                                 blobmsg_add_string_buffer(&b);
 60                         }
 61 
 62                         m = blobmsg_open_array(&b, "flags");
 63                         if (c->flags & OAF_BOUND)
 64                                 blobmsg_add_string(&b, NULL, "bound");
 65 
 66                         if (c->flags & OAF_STATIC)
 67                                 blobmsg_add_string(&b, NULL, "static");
 68 
 69                         if (c->flags & OAF_BROKEN_HOSTNAME)
 70                                 blobmsg_add_string(&b, NULL, "broken-hostname");
 71                         blobmsg_close_array(&b, m);
 72 
 73                         buf = blobmsg_alloc_string_buffer(&b, "address", INET_ADDRSTRLEN);
 74                         struct in_addr addr = {.s_addr = c->addr};
 75                         inet_ntop(AF_INET, &addr, buf, INET_ADDRSTRLEN);
 76                         blobmsg_add_string_buffer(&b);
 77 
 78                         blobmsg_add_u32(&b, "valid", INFINITE_VALID(c->valid_until) ?
 79                                                 (uint32_t)-1 : (uint32_t)(c->valid_until - now));
 80 
 81                         blobmsg_close_table(&b, l);
 82                 }
 83 
 84                 blobmsg_close_array(&b, j);
 85                 blobmsg_close_table(&b, i);
 86         }
 87 
 88         blobmsg_close_table(&b, a);
 89         ubus_send_reply(ctx, req, b.head);
 90 
 91         return 0;
 92 }
 93 
 94 static void dhcpv6_blobmsg_ia_addr(struct in6_addr *addr, int prefix, uint32_t pref,
 95                                         uint32_t valid, _unused void *arg)
 96 {
 97         void *a = blobmsg_open_table(&b, NULL);
 98         char *buf = blobmsg_alloc_string_buffer(&b, "address", INET6_ADDRSTRLEN);
 99 
100         inet_ntop(AF_INET6, addr, buf, INET6_ADDRSTRLEN);
101         blobmsg_add_string_buffer(&b);
102         blobmsg_add_u32(&b, "preferred-lifetime",
103                         pref == UINT32_MAX ? (uint32_t)-1 : pref);
104         blobmsg_add_u32(&b, "valid-lifetime",
105                         valid == UINT32_MAX ? (uint32_t)-1 : valid);
106 
107         if (prefix != 128)
108                 blobmsg_add_u32(&b, "prefix-length", prefix);
109 
110         blobmsg_close_table(&b, a);
111 }
112 
113 static int handle_dhcpv6_leases(_unused struct ubus_context *ctx, _unused struct ubus_object *obj,
114                 _unused struct ubus_request_data *req, _unused const char *method,
115                 _unused struct blob_attr *msg)
116 {
117         struct interface *iface;
118         time_t now = odhcpd_time();
119         void *a;
120 
121         blob_buf_init(&b, 0);
122         a = blobmsg_open_table(&b, "device");
123 
124         avl_for_each_element(&interfaces, iface, avl) {
125                 if (iface->dhcpv6 != MODE_SERVER)
126                         continue;
127 
128                 void *i = blobmsg_open_table(&b, iface->ifname);
129                 void *j = blobmsg_open_array(&b, "leases");
130 
131                 struct dhcp_assignment *a, *border = list_last_entry(
132                                 &iface->ia_assignments, struct dhcp_assignment, head);
133 
134                 list_for_each_entry(a, &iface->ia_assignments, head) {
135                         if (a == border || (!INFINITE_VALID(a->valid_until) &&
136                                                 a->valid_until < now))
137                                 continue;
138 
139                         void *m, *l = blobmsg_open_table(&b, NULL);
140                         char *buf = blobmsg_alloc_string_buffer(&b, "duid", 264);
141 
142                         odhcpd_hexlify(buf, a->clid_data, a->clid_len);
143                         blobmsg_add_string_buffer(&b);
144 
145                         blobmsg_add_u32(&b, "iaid", ntohl(a->iaid));
146                         blobmsg_add_string(&b, "hostname", (a->hostname) ? a->hostname : "");
147                         blobmsg_add_u8(&b, "accept-reconf", a->accept_reconf);
148                         blobmsg_add_u32(&b, "assigned", a->assigned);
149 
150                         m = blobmsg_open_array(&b, "flags");
151                         if (a->flags & OAF_BOUND)
152                                 blobmsg_add_string(&b, NULL, "bound");
153 
154                         if (a->flags & OAF_STATIC)
155                                 blobmsg_add_string(&b, NULL, "static");
156                         blobmsg_close_array(&b, m);
157 
158                         m = blobmsg_open_array(&b, a->flags & OAF_DHCPV6_NA ? "ipv6-addr": "ipv6-prefix");
159                         dhcpv6_ia_enum_addrs(iface, a, now, dhcpv6_blobmsg_ia_addr, NULL);
160                         blobmsg_close_table(&b, m);
161 
162                         blobmsg_add_u32(&b, "valid", INFINITE_VALID(a->valid_until) ?
163                                                 (uint32_t)-1 : (uint32_t)(a->valid_until - now));
164 
165                         blobmsg_close_table(&b, l);
166                 }
167 
168                 blobmsg_close_array(&b, j);
169                 blobmsg_close_table(&b, i);
170         }
171 
172         blobmsg_close_table(&b, a);
173         ubus_send_reply(ctx, req, b.head);
174         return 0;
175 }
176 
177 
178 static struct ubus_method main_object_methods[] = {
179         {.name = "ipv4leases", .handler = handle_dhcpv4_leases},
180         {.name = "ipv6leases", .handler = handle_dhcpv6_leases},
181 };
182 
183 static struct ubus_object_type main_object_type =
184                 UBUS_OBJECT_TYPE("dhcp", main_object_methods);
185 
186 static struct ubus_object main_object = {
187         .name = "dhcp",
188         .type = &main_object_type,
189         .methods = main_object_methods,
190         .n_methods = ARRAY_SIZE(main_object_methods),
191 };
192 
193 
194 enum {
195         DUMP_ATTR_INTERFACE,
196         DUMP_ATTR_MAX
197 };
198 
199 static const struct blobmsg_policy dump_attrs[DUMP_ATTR_MAX] = {
200         [DUMP_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_ARRAY },
201 };
202 
203 
204 enum {
205         IFACE_ATTR_INTERFACE,
206         IFACE_ATTR_IFNAME,
207         IFACE_ATTR_UP,
208         IFACE_ATTR_DATA,
209         IFACE_ATTR_PREFIX,
210         IFACE_ATTR_ADDRESS,
211         IFACE_ATTR_MAX,
212 };
213 
214 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
215         [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
216         [IFACE_ATTR_IFNAME] = { .name = "l3_device", .type = BLOBMSG_TYPE_STRING },
217         [IFACE_ATTR_UP] = { .name = "up", .type = BLOBMSG_TYPE_BOOL },
218         [IFACE_ATTR_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
219         [IFACE_ATTR_PREFIX] = { .name = "ipv6-prefix", .type = BLOBMSG_TYPE_ARRAY },
220         [IFACE_ATTR_ADDRESS] = { .name = "ipv6-address", .type = BLOBMSG_TYPE_ARRAY },
221 };
222 
223 static void handle_dump(_unused struct ubus_request *req, _unused int type, struct blob_attr *msg)
224 {
225         struct blob_attr *tb[DUMP_ATTR_MAX];
226         blobmsg_parse(dump_attrs, DUMP_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
227 
228         if (!tb[DUMP_ATTR_INTERFACE])
229                 return;
230 
231         free(dump);
232         dump = blob_memdup(tb[DUMP_ATTR_INTERFACE]);
233         odhcpd_reload();
234 }
235 
236 
237 static void update_netifd(bool subscribe)
238 {
239         if (subscribe)
240                 ubus_subscribe(ubus, &netifd, objid);
241 
242         ubus_abort_request(ubus, &req_dump);
243         blob_buf_init(&b, 0);
244 
245         if (!ubus_invoke_async(ubus, objid, "dump", b.head, &req_dump)) {
246                 req_dump.data_cb = handle_dump;
247                 ubus_complete_request_async(ubus, &req_dump);
248         }
249 }
250 
251 
252 static int handle_update(_unused struct ubus_context *ctx, _unused struct ubus_object *obj,
253                 _unused struct ubus_request_data *req, _unused const char *method,
254                 struct blob_attr *msg)
255 {
256         struct blob_attr *tb[IFACE_ATTR_MAX];
257         struct interface *c;
258         bool update = true;
259 
260         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
261         const char *interface = (tb[IFACE_ATTR_INTERFACE]) ?
262                         blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]) : "";
263 
264         avl_for_each_element(&interfaces, c, avl) {
265                 if (!strcmp(interface, c->name) && c->ignore) {
266                         update = false;
267                         break;
268                 }
269         }
270 
271         if (update)
272                 update_netifd(false);
273 
274         return 0;
275 }
276 
277 
278 void ubus_apply_network(void)
279 {
280         struct blob_attr *a;
281         unsigned rem;
282 
283         if (!dump)
284                 return;
285 
286         blobmsg_for_each_attr(a, dump, rem) {
287                 struct blob_attr *tb[IFACE_ATTR_MAX];
288                 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(a), blobmsg_data_len(a));
289 
290                 if (!tb[IFACE_ATTR_INTERFACE] || !tb[IFACE_ATTR_DATA])
291                         continue;
292 
293                 const char *interface = (tb[IFACE_ATTR_INTERFACE]) ?
294                                 blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]) : "";
295 
296                 bool matched = false;
297                 struct interface *c, *tmp;
298                 avl_for_each_element_safe(&interfaces, c, avl, tmp) {
299                         char *f = memmem(c->upstream, c->upstream_len,
300                                         interface, strlen(interface) + 1);
301                         bool cmatched = !strcmp(interface, c->name);
302                         matched |= cmatched;
303 
304                         if (!cmatched && (!c->upstream_len || !f || (f != c->upstream && f[-1] != 0)))
305                                 continue;
306 
307                         if (!c->ignore)
308                                 config_parse_interface(blobmsg_data(tb[IFACE_ATTR_DATA]),
309                                                 blobmsg_data_len(tb[IFACE_ATTR_DATA]), c->name, false);
310                 }
311 
312                 if (!matched)
313                         config_parse_interface(blobmsg_data(tb[IFACE_ATTR_DATA]),
314                                         blobmsg_data_len(tb[IFACE_ATTR_DATA]), interface, false);
315         }
316 }
317 
318 
319 enum {
320         OBJ_ATTR_ID,
321         OBJ_ATTR_PATH,
322         OBJ_ATTR_MAX
323 };
324 
325 static const struct blobmsg_policy obj_attrs[OBJ_ATTR_MAX] = {
326         [OBJ_ATTR_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
327         [OBJ_ATTR_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
328 };
329 
330 void ubus_bcast_dhcp_event(const char *type, const uint8_t *mac,
331                 const size_t mlen, const struct in_addr *addr, const char *name,
332                 const char *interface)
333 {
334         if (!ubus || !main_object.has_subscribers)
335                 return;
336 
337         blob_buf_init(&b, 0);
338         if (mac)
339                 blobmsg_add_string(&b, "mac", odhcpd_print_mac(mac, mlen));
340         if (addr)
341                 blobmsg_add_string(&b, "ip", inet_ntoa(*addr));
342         if (name)
343                 blobmsg_add_string(&b, "name", name);
344         if (interface)
345                 blobmsg_add_string(&b, "interface", interface);
346 
347         ubus_notify(ubus, &main_object, type, b.head, -1);
348 }
349 
350 static void handle_event(_unused struct ubus_context *ctx, _unused struct ubus_event_handler *ev,
351                 _unused const char *type, struct blob_attr *msg)
352 {
353         struct blob_attr *tb[OBJ_ATTR_MAX];
354         blobmsg_parse(obj_attrs, OBJ_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
355 
356         if (!tb[OBJ_ATTR_ID] || !tb[OBJ_ATTR_PATH])
357                 return;
358 
359         if (strcmp(blobmsg_get_string(tb[OBJ_ATTR_PATH]), "network.interface"))
360                 return;
361 
362         objid = blobmsg_get_u32(tb[OBJ_ATTR_ID]);
363         update_netifd(true);
364 }
365 
366 static struct ubus_event_handler event_handler = { .cb = handle_event };
367 
368 
369 const char* ubus_get_ifname(const char *name)
370 {
371         struct blob_attr *c;
372         unsigned rem;
373 
374         if (!dump)
375                 return NULL;
376 
377         blobmsg_for_each_attr(c, dump, rem) {
378                 struct blob_attr *tb[IFACE_ATTR_MAX];
379                 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(c), blobmsg_data_len(c));
380 
381                 if (!tb[IFACE_ATTR_INTERFACE] || strcmp(name,
382                                 blobmsg_get_string(tb[IFACE_ATTR_INTERFACE])))
383                         continue;
384 
385                 if (tb[IFACE_ATTR_IFNAME])
386                         return blobmsg_get_string(tb[IFACE_ATTR_IFNAME]);
387         }
388 
389         return NULL;
390 }
391 
392 
393 bool ubus_has_prefix(const char *name, const char *ifname)
394 {
395         struct blob_attr *c, *cur;
396         unsigned rem;
397 
398         if (!dump)
399                 return NULL;
400 
401         blobmsg_for_each_attr(c, dump, rem) {
402                 struct blob_attr *tb[IFACE_ATTR_MAX];
403                 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(c), blobmsg_data_len(c));
404 
405                 if (!tb[IFACE_ATTR_INTERFACE] || !tb[IFACE_ATTR_IFNAME])
406                         continue;
407 
408                 if (strcmp(name, blobmsg_get_string(tb[IFACE_ATTR_INTERFACE])) ||
409                                 strcmp(ifname, blobmsg_get_string(tb[IFACE_ATTR_IFNAME])))
410                         continue;
411 
412                 if ((cur = tb[IFACE_ATTR_PREFIX])) {
413                         if (blobmsg_type(cur) != BLOBMSG_TYPE_ARRAY || !blobmsg_check_attr(cur, false))
414                                 continue;
415 
416                         struct blob_attr *d;
417                         unsigned drem;
418                         blobmsg_for_each_attr(d, cur, drem) {
419                                 return true;
420                         }
421                 }
422         }
423 
424         return false;
425 }
426 
427 
428 int ubus_init(void)
429 {
430         if (!(ubus = ubus_connect(NULL))) {
431                 syslog(LOG_ERR, "Unable to connect to ubus: %m");
432                 return -1;
433         }
434 
435         netifd.cb = handle_update;
436         ubus_register_subscriber(ubus, &netifd);
437 
438         ubus_add_uloop(ubus);
439         ubus_add_object(ubus, &main_object);
440         ubus_register_event_handler(ubus, &event_handler, "ubus.object.add");
441         if (!ubus_lookup_id(ubus, "network.interface", &objid))
442                 update_netifd(true);
443 
444         return 0;
445 }
446 
447 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt