Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
2 :
3 : #include <net/if.h>
4 :
5 : #include "alloc-util.h"
6 : #include "bus-common-errors.h"
7 : #include "bus-util.h"
8 : #include "networkd-link-bus.h"
9 : #include "networkd-link.h"
10 : #include "networkd-manager-bus.h"
11 : #include "networkd-manager.h"
12 : #include "path-util.h"
13 : #include "strv.h"
14 :
15 0 : static int method_list_links(sd_bus_message *message, void *userdata, sd_bus_error *error) {
16 0 : _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
17 0 : Manager *manager = userdata;
18 : Iterator i;
19 : Link *link;
20 : int r;
21 :
22 0 : r = sd_bus_message_new_method_return(message, &reply);
23 0 : if (r < 0)
24 0 : return r;
25 :
26 0 : r = sd_bus_message_open_container(reply, 'a', "(iso)");
27 0 : if (r < 0)
28 0 : return r;
29 :
30 0 : HASHMAP_FOREACH(link, manager->links, i) {
31 0 : _cleanup_free_ char *path = NULL;
32 :
33 0 : path = link_bus_path(link);
34 0 : if (!path)
35 0 : return -ENOMEM;
36 :
37 0 : r = sd_bus_message_append(
38 : reply, "(iso)",
39 0 : link->ifindex,
40 0 : link->ifname,
41 : empty_to_root(path));
42 0 : if (r < 0)
43 0 : return r;
44 : }
45 :
46 0 : r = sd_bus_message_close_container(reply);
47 0 : if (r < 0)
48 0 : return r;
49 :
50 0 : return sd_bus_send(NULL, reply, NULL);
51 : }
52 :
53 0 : static int method_get_link_by_name(sd_bus_message *message, void *userdata, sd_bus_error *error) {
54 0 : _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
55 0 : _cleanup_free_ char *path = NULL;
56 0 : Manager *manager = userdata;
57 : const char *name;
58 : int index, r;
59 : Link *link;
60 :
61 0 : r = sd_bus_message_read(message, "s", &name);
62 0 : if (r < 0)
63 0 : return r;
64 :
65 0 : index = if_nametoindex(name);
66 0 : if (index <= 0)
67 0 : return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %s not known", name);
68 :
69 0 : link = hashmap_get(manager->links, INT_TO_PTR(index));
70 0 : if (!link)
71 0 : return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %s not known", name);
72 :
73 0 : r = sd_bus_message_new_method_return(message, &reply);
74 0 : if (r < 0)
75 0 : return r;
76 :
77 0 : path = link_bus_path(link);
78 0 : if (!path)
79 0 : return -ENOMEM;
80 :
81 0 : r = sd_bus_message_append(reply, "io", link->ifindex, empty_to_root(path));
82 0 : if (r < 0)
83 0 : return r;
84 :
85 0 : return sd_bus_send(NULL, reply, NULL);
86 : }
87 :
88 0 : static int method_get_link_by_index(sd_bus_message *message, void *userdata, sd_bus_error *error) {
89 0 : _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
90 0 : _cleanup_free_ char *path = NULL;
91 0 : Manager *manager = userdata;
92 : int32_t index;
93 : Link *link;
94 : int r;
95 :
96 0 : r = sd_bus_message_read(message, "i", &index);
97 0 : if (r < 0)
98 0 : return r;
99 :
100 0 : link = hashmap_get(manager->links, INT_TO_PTR((int) index));
101 0 : if (!link)
102 0 : return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %" PRIi32 " not known", index);
103 :
104 0 : r = sd_bus_message_new_method_return(message, &reply);
105 0 : if (r < 0)
106 0 : return r;
107 :
108 0 : path = link_bus_path(link);
109 0 : if (!path)
110 0 : return -ENOMEM;
111 :
112 0 : r = sd_bus_message_append(reply, "so", link->ifname, empty_to_root(path));
113 0 : if (r < 0)
114 0 : return r;
115 :
116 0 : return sd_bus_send(NULL, reply, NULL);
117 : }
118 :
119 0 : static int call_link_method(Manager *m, sd_bus_message *message, sd_bus_message_handler_t handler, sd_bus_error *error) {
120 : int ifindex, r;
121 : Link *l;
122 :
123 0 : assert(m);
124 0 : assert(message);
125 0 : assert(handler);
126 :
127 : assert_cc(sizeof(int) == sizeof(int32_t));
128 0 : r = sd_bus_message_read(message, "i", &ifindex);
129 0 : if (r < 0)
130 0 : return r;
131 :
132 0 : if (ifindex <= 0)
133 0 : return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
134 :
135 0 : l = hashmap_get(m->links, INT_TO_PTR(ifindex));
136 0 : if (!l)
137 0 : return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %i not known", ifindex);
138 :
139 0 : return handler(message, l, error);
140 : }
141 :
142 0 : static int bus_method_set_link_ntp_servers(sd_bus_message *message, void *userdata, sd_bus_error *error) {
143 0 : return call_link_method(userdata, message, bus_link_method_set_ntp_servers, error);
144 : }
145 :
146 0 : static int bus_method_set_link_dns_servers(sd_bus_message *message, void *userdata, sd_bus_error *error) {
147 0 : return call_link_method(userdata, message, bus_link_method_set_dns_servers, error);
148 : }
149 :
150 0 : static int bus_method_set_link_domains(sd_bus_message *message, void *userdata, sd_bus_error *error) {
151 0 : return call_link_method(userdata, message, bus_link_method_set_domains, error);
152 : }
153 :
154 0 : static int bus_method_set_link_default_route(sd_bus_message *message, void *userdata, sd_bus_error *error) {
155 0 : return call_link_method(userdata, message, bus_link_method_set_default_route, error);
156 : }
157 :
158 0 : static int bus_method_set_link_llmnr(sd_bus_message *message, void *userdata, sd_bus_error *error) {
159 0 : return call_link_method(userdata, message, bus_link_method_set_llmnr, error);
160 : }
161 :
162 0 : static int bus_method_set_link_mdns(sd_bus_message *message, void *userdata, sd_bus_error *error) {
163 0 : return call_link_method(userdata, message, bus_link_method_set_mdns, error);
164 : }
165 :
166 0 : static int bus_method_set_link_dns_over_tls(sd_bus_message *message, void *userdata, sd_bus_error *error) {
167 0 : return call_link_method(userdata, message, bus_link_method_set_dns_over_tls, error);
168 : }
169 :
170 0 : static int bus_method_set_link_dnssec(sd_bus_message *message, void *userdata, sd_bus_error *error) {
171 0 : return call_link_method(userdata, message, bus_link_method_set_dnssec, error);
172 : }
173 :
174 0 : static int bus_method_set_link_dnssec_negative_trust_anchors(sd_bus_message *message, void *userdata, sd_bus_error *error) {
175 0 : return call_link_method(userdata, message, bus_link_method_set_dnssec_negative_trust_anchors, error);
176 : }
177 :
178 0 : static int bus_method_revert_link_ntp(sd_bus_message *message, void *userdata, sd_bus_error *error) {
179 0 : return call_link_method(userdata, message, bus_link_method_revert_ntp, error);
180 : }
181 :
182 0 : static int bus_method_revert_link_dns(sd_bus_message *message, void *userdata, sd_bus_error *error) {
183 0 : return call_link_method(userdata, message, bus_link_method_revert_dns, error);
184 : }
185 :
186 : const sd_bus_vtable manager_vtable[] = {
187 : SD_BUS_VTABLE_START(0),
188 :
189 : SD_BUS_PROPERTY("OperationalState", "s", property_get_operational_state, offsetof(Manager, operational_state), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
190 : SD_BUS_PROPERTY("CarrierState", "s", property_get_carrier_state, offsetof(Manager, carrier_state), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
191 : SD_BUS_PROPERTY("AddressState", "s", property_get_address_state, offsetof(Manager, address_state), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
192 :
193 : SD_BUS_METHOD("ListLinks", NULL, "a(iso)", method_list_links, SD_BUS_VTABLE_UNPRIVILEGED),
194 : SD_BUS_METHOD("GetLinkByName", "s", "io", method_get_link_by_name, SD_BUS_VTABLE_UNPRIVILEGED),
195 : SD_BUS_METHOD("GetLinkByIndex", "i", "so", method_get_link_by_index, SD_BUS_VTABLE_UNPRIVILEGED),
196 : SD_BUS_METHOD("SetLinkNTP", "ias", NULL, bus_method_set_link_ntp_servers, SD_BUS_VTABLE_UNPRIVILEGED),
197 : SD_BUS_METHOD("SetLinkDNS", "ia(iay)", NULL, bus_method_set_link_dns_servers, SD_BUS_VTABLE_UNPRIVILEGED),
198 : SD_BUS_METHOD("SetLinkDomains", "ia(sb)", NULL, bus_method_set_link_domains, SD_BUS_VTABLE_UNPRIVILEGED),
199 : SD_BUS_METHOD("SetLinkDefaultRoute", "ib", NULL, bus_method_set_link_default_route, SD_BUS_VTABLE_UNPRIVILEGED),
200 : SD_BUS_METHOD("SetLinkLLMNR", "is", NULL, bus_method_set_link_llmnr, SD_BUS_VTABLE_UNPRIVILEGED),
201 : SD_BUS_METHOD("SetLinkMulticastDNS", "is", NULL, bus_method_set_link_mdns, SD_BUS_VTABLE_UNPRIVILEGED),
202 : SD_BUS_METHOD("SetLinkDNSOverTLS", "is", NULL, bus_method_set_link_dns_over_tls, SD_BUS_VTABLE_UNPRIVILEGED),
203 : SD_BUS_METHOD("SetLinkDNSSEC", "is", NULL, bus_method_set_link_dnssec, SD_BUS_VTABLE_UNPRIVILEGED),
204 : SD_BUS_METHOD("SetLinkDNSSECNegativeTrustAnchors", "ias", NULL, bus_method_set_link_dnssec_negative_trust_anchors, SD_BUS_VTABLE_UNPRIVILEGED),
205 : SD_BUS_METHOD("RevertLinkNTP", "i", NULL, bus_method_revert_link_ntp, SD_BUS_VTABLE_UNPRIVILEGED),
206 : SD_BUS_METHOD("RevertLinkDNS", "i", NULL, bus_method_revert_link_dns, SD_BUS_VTABLE_UNPRIVILEGED),
207 :
208 : SD_BUS_VTABLE_END
209 : };
210 :
211 0 : int manager_send_changed_strv(Manager *manager, char **properties) {
212 0 : assert(manager);
213 0 : assert(properties);
214 :
215 0 : if (!manager->bus)
216 0 : return 0;
217 :
218 0 : return sd_bus_emit_properties_changed_strv(
219 : manager->bus,
220 : "/org/freedesktop/network1",
221 : "org.freedesktop.network1.Manager",
222 : properties);
223 : }
|