1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include "alloc-util.h"
4 #include "bus-common-errors.h"
5 #include "bus-util.h"
6 #include "dhcp-server-internal.h"
7 #include "networkd-dhcp-server-bus.h"
8 #include "networkd-link-bus.h"
9 #include "networkd-manager.h"
10 #include "strv.h"
11 
property_get_leases(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * reply,void * userdata,sd_bus_error * error)12 static int property_get_leases(
13                 sd_bus *bus,
14                 const char *path,
15                 const char *interface,
16                 const char *property,
17                 sd_bus_message *reply,
18                 void *userdata,
19                 sd_bus_error *error) {
20         Link *l = userdata;
21         sd_dhcp_server *s;
22         DHCPLease *lease;
23         int r;
24 
25         assert(reply);
26         assert(l);
27 
28         s = l->dhcp_server;
29         if (!s)
30                 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Link %s has no DHCP server.", l->ifname);
31 
32         if (sd_dhcp_server_is_in_relay_mode(s))
33                 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Link %s has DHCP relay agent active.", l->ifname);
34 
35         r = sd_bus_message_open_container(reply, 'a', "(uayayayayt)");
36         if (r < 0)
37                 return r;
38 
39         HASHMAP_FOREACH(lease, s->bound_leases_by_client_id) {
40                 r = sd_bus_message_open_container(reply, 'r', "uayayayayt");
41                 if (r < 0)
42                         return r;
43 
44                 r = sd_bus_message_append(reply, "u", (uint32_t)AF_INET);
45                 if (r < 0)
46                         return r;
47 
48                 r = sd_bus_message_append_array(reply, 'y', lease->client_id.data, lease->client_id.length);
49                 if (r < 0)
50                         return r;
51 
52                 r = sd_bus_message_append_array(reply, 'y', &lease->address, sizeof(lease->address));
53                 if (r < 0)
54                         return r;
55 
56                 r = sd_bus_message_append_array(reply, 'y', &lease->gateway, sizeof(lease->gateway));
57                 if (r < 0)
58                         return r;
59 
60                 r = sd_bus_message_append_array(reply, 'y', &lease->chaddr, sizeof(lease->chaddr));
61                 if (r < 0)
62                         return r;
63 
64                 r = sd_bus_message_append_basic(reply, 't', &lease->expiration);
65                 if (r < 0)
66                         return r;
67 
68                 r = sd_bus_message_close_container(reply);
69                 if (r < 0)
70                         return r;
71         }
72 
73         return sd_bus_message_close_container(reply);
74 }
75 
dhcp_server_emit_changed(Link * link,const char * property,...)76 static int dhcp_server_emit_changed(Link *link, const char *property, ...) {
77         _cleanup_free_ char *path = NULL;
78         char **l;
79 
80         assert(link);
81 
82         if (sd_bus_is_ready(link->manager->bus) <= 0)
83                 return 0;
84 
85         path = link_bus_path(link);
86         if (!path)
87                 return log_oom();
88 
89         l = strv_from_stdarg_alloca(property);
90 
91         return sd_bus_emit_properties_changed_strv(
92                         link->manager->bus,
93                         path,
94                         "org.freedesktop.network1.DHCPServer",
95                         l);
96 }
97 
dhcp_server_callback(sd_dhcp_server * s,uint64_t event,void * data)98 void dhcp_server_callback(sd_dhcp_server *s, uint64_t event, void *data) {
99         Link *l = data;
100 
101         assert(l);
102 
103         if (event & SD_DHCP_SERVER_EVENT_LEASE_CHANGED)
104                 (void) dhcp_server_emit_changed(l, "Leases", NULL);
105 }
106 
107 static const sd_bus_vtable dhcp_server_vtable[] = {
108         SD_BUS_VTABLE_START(0),
109 
110         SD_BUS_PROPERTY("Leases", "a(uayayayayt)", property_get_leases, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
111 
112         SD_BUS_VTABLE_END
113 };
114 
115 const BusObjectImplementation dhcp_server_object = {
116         "/org/freedesktop/network1/link",
117         "org.freedesktop.network1.DHCPServer",
118         .fallback_vtables = BUS_FALLBACK_VTABLES({dhcp_server_vtable, link_object_find}),
119         .node_enumerator = link_node_enumerator,
120 };
121