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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt