dhcpv4: update ubus DHCPv4 events/methods
authorDavid Härdeman <[email protected]>
Mon, 10 Nov 2025 22:36:27 +0000 (23:36 +0100)
committerÁlvaro Fernández Rojas <[email protected]>
Fri, 14 Nov 2025 16:25:31 +0000 (17:25 +0100)
This is based on the assumption that we don't really have any consumers
of ubus DHCPv4 events (yet).

With that in mind, rename and simplify the event function (yes, we
should add a sibling function for DHCPv6 leases later).

Also, take the chance to improve some naming, and introduce additional
attributes to the ubus event/method (the interface name and the
DUID/IAID, if known).

Signed-off-by: David Härdeman <[email protected]>
Link: https://github.com/openwrt/odhcpd/pull/306
Signed-off-by: Álvaro Fernández Rojas <[email protected]>
src/dhcpv4.c
src/odhcpd.h
src/ubus.c

index 5aa23099b4d7cf92c4df5437df6984ecccf4597e..d1811ec5ed037d7bec95c8cc2f55c555eeefa9ad 100644 (file)
@@ -558,8 +558,7 @@ dhcpv4_lease(struct interface *iface, enum dhcpv4_msg req_msg, const uint8_t *re
                if (!lease)
                        return NULL;
 
-               ubus_bcast_dhcp_event("dhcp.release", req_mac, lease->ipv4,
-                                     lease->hostname, iface->ifname);
+               ubus_bcast_dhcpv4_event("dhcp.release4", iface->ifname, lease);
                 dhcpv4_free_lease(lease);
                 lease = NULL;
                break;
@@ -1319,8 +1318,7 @@ void dhcpv4_handle_msg(void *src_addr, void *data, size_t len,
        }
 
        if (reply_msg.data == DHCPV4_MSG_ACK && lease)
-               ubus_bcast_dhcp_event("dhcp.ack", req->chaddr, lease->ipv4,
-                                     lease->hostname, iface->ifname);
+               ubus_bcast_dhcpv4_event("dhcp.lease4", iface->ifname, lease);
 }
 
 /* Handler for DHCPv4 messages */
@@ -1585,8 +1583,7 @@ static void dhcpv4_valid_until_cb(struct uloop_timeout *event)
 
                avl_for_each_element_safe(&iface->dhcpv4_leases, lease, iface_avl, tmp) {
                        if (!INFINITE_VALID(lease->valid_until) && lease->valid_until < now) {
-                               ubus_bcast_dhcp_event("dhcp.expire", lease->hwaddr, lease->ipv4,
-                                                     lease->hostname, iface->ifname);
+                               ubus_bcast_dhcpv4_event("dhcp.expire4", iface->ifname, lease);
                                dhcpv4_free_lease(lease);
                                update_statefile = true;
                        }
index d9860804d6f709eeddb3a52e7675bbdcab6c8483..5daf5100f1cc55ea361e590ee4bf114899bfa706 100644 (file)
@@ -563,9 +563,8 @@ int ubus_init(void);
 const char* ubus_get_ifname(const char *name);
 void ubus_apply_network(void);
 bool ubus_has_prefix(const char *name, const char *ifname);
-void ubus_bcast_dhcp_event(const char *type, const uint8_t *mac,
-                          const struct in_addr ipv4, const char *name,
-                          const char *interface);
+void ubus_bcast_dhcpv4_event(const char *type, const char *iface,
+                            const struct dhcpv4_lease *lease);
 #else
 static inline int ubus_init(void)
 {
@@ -578,9 +577,8 @@ static inline void ubus_apply_network(void)
 }
 
 static inline
-void ubus_bcast_dhcp_event(const char *type, const uint8_t *mac,
-                          const struct in_addr ipv4, const char *name,
-                          const char *interface)
+void ubus_bcast_dhcpv4_event(const char *type, const char *iface,
+                            const struct dhcpv4_lease *lease)
 {
        return;
 }
index ca15d37b6622c2bf4cafc6cdd391d30f0d640056..bc8e456dadd2d9895610cf3df7226011932f1e82 100644 (file)
@@ -43,11 +43,18 @@ static int handle_dhcpv4_leases(struct ubus_context *ctx, _unused struct ubus_ob
                                continue;
 
                        void *m, *l = blobmsg_open_table(&b, NULL);
-                       char *buf = blobmsg_alloc_string_buffer(&b, "mac", 13);
+                       char *buf = blobmsg_alloc_string_buffer(&b, "mac", sizeof(c->hwaddr) * 2 + 1);
 
                        odhcpd_hexlify(buf, c->hwaddr, sizeof(c->hwaddr));
                        blobmsg_add_string_buffer(&b);
 
+                       if (c->duid_len > 0) {
+                               buf = blobmsg_alloc_string_buffer(&b, "duid", DUID_HEXSTRLEN + 1);
+                               odhcpd_hexlify(buf, c->duid, c->duid_len);
+                               blobmsg_add_string_buffer(&b);
+                               blobmsg_add_u32(&b, "iaid", ntohl(c->iaid));
+                       }
+
                        blobmsg_add_string(&b, "hostname", (c->hostname) ? c->hostname : "");
                        blobmsg_add_u8(&b, "accept-reconf", c->accept_fr_nonce);
 
@@ -130,7 +137,7 @@ static int handle_dhcpv6_leases(_unused struct ubus_context *ctx, _unused struct
                                continue;
 
                        void *m, *l = blobmsg_open_table(&b, NULL);
-                       char *buf = blobmsg_alloc_string_buffer(&b, "duid", DUID_HEXSTRLEN);
+                       char *buf = blobmsg_alloc_string_buffer(&b, "duid", DUID_HEXSTRLEN + 1);
 
                        odhcpd_hexlify(buf, a->duid, a->duid_len);
                        blobmsg_add_string_buffer(&b);
@@ -392,23 +399,26 @@ static const struct blobmsg_policy obj_attrs[OBJ_ATTR_MAX] = {
        [OBJ_ATTR_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
 };
 
-void ubus_bcast_dhcp_event(const char *type, const uint8_t *mac,
-                          const struct in_addr ipv4, const char *name,
-                          const char *interface)
+void ubus_bcast_dhcpv4_event(const char *type, const char *iface,
+                            const struct dhcpv4_lease *lease)
 {
        char ipv4_str[INET_ADDRSTRLEN];
 
-       if (!ubus || !main_object.has_subscribers)
+       if (!ubus || !main_object.has_subscribers || !iface)
                return;
 
        blob_buf_init(&b, 0);
-       if (mac)
-               blobmsg_add_string(&b, "mac", odhcpd_print_mac(mac, ETH_ALEN));
-       blobmsg_add_string(&b, "ip", inet_ntop(AF_INET, &ipv4, ipv4_str, sizeof(ipv4_str)));
-       if (name)
-               blobmsg_add_string(&b, "name", name);
-       if (interface)
-               blobmsg_add_string(&b, "interface", interface);
+       blobmsg_add_string(&b, "interface", iface);
+       blobmsg_add_string(&b, "ipv4", inet_ntop(AF_INET, &lease->ipv4, ipv4_str, sizeof(ipv4_str)));
+       blobmsg_add_string(&b, "mac", odhcpd_print_mac(lease->hwaddr, sizeof(lease->hwaddr)));
+       if (lease->hostname)
+               blobmsg_add_string(&b, "hostname", lease->hostname);
+       if (lease->duid_len > 0) {
+               char *buf = blobmsg_alloc_string_buffer(&b, "duid", DUID_HEXSTRLEN + 1);
+               odhcpd_hexlify(buf, lease->duid, lease->duid_len);
+               blobmsg_add_string_buffer(&b);
+               blobmsg_add_u32(&b, "iaid", lease->iaid);
+       }
 
        ubus_notify(ubus, &main_object, type, b.head, -1);
 }