]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
e331e246 | 2 | |
071712b2 | 3 | #include <net/if.h> |
f39dbf28 | 4 | #include <netinet/in.h> |
7f06b3e1 | 5 | #include <sys/capability.h> |
071712b2 | 6 | |
b5efdb8a | 7 | #include "alloc-util.h" |
071712b2 | 8 | #include "bus-common-errors.h" |
269e4d2d | 9 | #include "bus-polkit.h" |
7f3c07ad | 10 | #include "networkd-link-bus.h" |
37d577c8 | 11 | #include "networkd-link.h" |
79a59fa5 | 12 | #include "networkd-manager-bus.h" |
23f53b99 | 13 | #include "networkd-manager.h" |
37d577c8 | 14 | #include "path-util.h" |
fc2ea97a | 15 | #include "socket-netlink.h" |
51517f9e | 16 | #include "strv.h" |
7f06b3e1 | 17 | #include "user-util.h" |
e331e246 | 18 | |
37d577c8 YW |
19 | static int method_list_links(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
20 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
21 | Manager *manager = userdata; | |
22 | Iterator i; | |
23 | Link *link; | |
24 | int r; | |
25 | ||
26 | r = sd_bus_message_new_method_return(message, &reply); | |
27 | if (r < 0) | |
28 | return r; | |
29 | ||
30 | r = sd_bus_message_open_container(reply, 'a', "(iso)"); | |
31 | if (r < 0) | |
32 | return r; | |
33 | ||
34 | HASHMAP_FOREACH(link, manager->links, i) { | |
35 | _cleanup_free_ char *path = NULL; | |
36 | ||
37 | path = link_bus_path(link); | |
38 | if (!path) | |
39 | return -ENOMEM; | |
40 | ||
41 | r = sd_bus_message_append( | |
42 | reply, "(iso)", | |
43 | link->ifindex, | |
44 | link->ifname, | |
45 | empty_to_root(path)); | |
46 | if (r < 0) | |
47 | return r; | |
48 | } | |
49 | ||
50 | r = sd_bus_message_close_container(reply); | |
51 | if (r < 0) | |
52 | return r; | |
53 | ||
54 | return sd_bus_send(NULL, reply, NULL); | |
55 | } | |
56 | ||
071712b2 YW |
57 | static int method_get_link_by_name(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
58 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
59 | _cleanup_free_ char *path = NULL; | |
60 | Manager *manager = userdata; | |
61 | const char *name; | |
62 | int index, r; | |
63 | Link *link; | |
64 | ||
65 | r = sd_bus_message_read(message, "s", &name); | |
66 | if (r < 0) | |
67 | return r; | |
68 | ||
fc2ea97a ZJS |
69 | index = resolve_ifname(&manager->rtnl, name); |
70 | if (index < 0) | |
71 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %s cannot be resolved", name); | |
071712b2 YW |
72 | |
73 | link = hashmap_get(manager->links, INT_TO_PTR(index)); | |
74 | if (!link) | |
75 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %s not known", name); | |
76 | ||
77 | r = sd_bus_message_new_method_return(message, &reply); | |
78 | if (r < 0) | |
79 | return r; | |
80 | ||
81 | path = link_bus_path(link); | |
82 | if (!path) | |
83 | return -ENOMEM; | |
84 | ||
85 | r = sd_bus_message_append(reply, "io", link->ifindex, empty_to_root(path)); | |
86 | if (r < 0) | |
87 | return r; | |
88 | ||
89 | return sd_bus_send(NULL, reply, NULL); | |
90 | } | |
91 | ||
92 | static int method_get_link_by_index(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
93 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
94 | _cleanup_free_ char *path = NULL; | |
95 | Manager *manager = userdata; | |
96 | int32_t index; | |
97 | Link *link; | |
98 | int r; | |
99 | ||
100 | r = sd_bus_message_read(message, "i", &index); | |
101 | if (r < 0) | |
102 | return r; | |
103 | ||
104 | link = hashmap_get(manager->links, INT_TO_PTR((int) index)); | |
105 | if (!link) | |
106 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %" PRIi32 " not known", index); | |
107 | ||
108 | r = sd_bus_message_new_method_return(message, &reply); | |
109 | if (r < 0) | |
110 | return r; | |
111 | ||
112 | path = link_bus_path(link); | |
113 | if (!path) | |
114 | return -ENOMEM; | |
115 | ||
116 | r = sd_bus_message_append(reply, "so", link->ifname, empty_to_root(path)); | |
117 | if (r < 0) | |
118 | return r; | |
119 | ||
120 | return sd_bus_send(NULL, reply, NULL); | |
121 | } | |
122 | ||
15761549 YW |
123 | static int call_link_method(Manager *m, sd_bus_message *message, sd_bus_message_handler_t handler, sd_bus_error *error) { |
124 | int ifindex, r; | |
125 | Link *l; | |
126 | ||
127 | assert(m); | |
128 | assert(message); | |
129 | assert(handler); | |
130 | ||
131 | assert_cc(sizeof(int) == sizeof(int32_t)); | |
132 | r = sd_bus_message_read(message, "i", &ifindex); | |
133 | if (r < 0) | |
134 | return r; | |
135 | ||
136 | if (ifindex <= 0) | |
137 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index"); | |
138 | ||
139 | l = hashmap_get(m->links, INT_TO_PTR(ifindex)); | |
140 | if (!l) | |
141 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %i not known", ifindex); | |
142 | ||
143 | return handler(message, l, error); | |
144 | } | |
145 | ||
146 | static int bus_method_set_link_ntp_servers(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
147 | return call_link_method(userdata, message, bus_link_method_set_ntp_servers, error); | |
148 | } | |
149 | ||
150 | static int bus_method_set_link_dns_servers(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
151 | return call_link_method(userdata, message, bus_link_method_set_dns_servers, error); | |
152 | } | |
153 | ||
154 | static int bus_method_set_link_domains(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
155 | return call_link_method(userdata, message, bus_link_method_set_domains, error); | |
156 | } | |
157 | ||
158 | static int bus_method_set_link_default_route(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
159 | return call_link_method(userdata, message, bus_link_method_set_default_route, error); | |
160 | } | |
161 | ||
162 | static int bus_method_set_link_llmnr(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
163 | return call_link_method(userdata, message, bus_link_method_set_llmnr, error); | |
164 | } | |
165 | ||
166 | static int bus_method_set_link_mdns(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
167 | return call_link_method(userdata, message, bus_link_method_set_mdns, error); | |
168 | } | |
169 | ||
170 | static int bus_method_set_link_dns_over_tls(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
171 | return call_link_method(userdata, message, bus_link_method_set_dns_over_tls, error); | |
172 | } | |
173 | ||
174 | static int bus_method_set_link_dnssec(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
175 | return call_link_method(userdata, message, bus_link_method_set_dnssec, error); | |
176 | } | |
177 | ||
178 | static int bus_method_set_link_dnssec_negative_trust_anchors(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
179 | return call_link_method(userdata, message, bus_link_method_set_dnssec_negative_trust_anchors, error); | |
180 | } | |
181 | ||
182 | static int bus_method_revert_link_ntp(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
183 | return call_link_method(userdata, message, bus_link_method_revert_ntp, error); | |
184 | } | |
185 | ||
186 | static int bus_method_revert_link_dns(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
187 | return call_link_method(userdata, message, bus_link_method_revert_dns, error); | |
188 | } | |
189 | ||
ae65d7db YW |
190 | static int bus_method_renew_link(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
191 | return call_link_method(userdata, message, bus_link_method_renew, error); | |
192 | } | |
193 | ||
90867f6a SS |
194 | static int bus_method_force_renew_link(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
195 | return call_link_method(userdata, message, bus_link_method_force_renew, error); | |
196 | } | |
197 | ||
99b8517c YW |
198 | static int bus_method_reconfigure_link(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
199 | return call_link_method(userdata, message, bus_link_method_reconfigure, error); | |
200 | } | |
201 | ||
7f06b3e1 YW |
202 | static int bus_method_reload(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
203 | Manager *manager = userdata; | |
204 | Iterator i; | |
205 | Link *link; | |
206 | int r; | |
207 | ||
208 | r = bus_verify_polkit_async(message, CAP_NET_ADMIN, | |
209 | "org.freedesktop.network1.reload", | |
210 | NULL, true, UID_INVALID, | |
211 | &manager->polkit_registry, error); | |
212 | if (r < 0) | |
213 | return r; | |
214 | if (r == 0) | |
215 | return 1; /* Polkit will call us back */ | |
216 | ||
e272b621 YW |
217 | r = netdev_load(manager, true); |
218 | if (r < 0) | |
219 | return r; | |
220 | ||
7f06b3e1 YW |
221 | r = network_reload(manager); |
222 | if (r < 0) | |
223 | return r; | |
224 | ||
225 | HASHMAP_FOREACH(link, manager->links, i) { | |
99b8517c | 226 | r = link_reconfigure(link, false); |
7f06b3e1 YW |
227 | if (r < 0) |
228 | return r; | |
229 | } | |
230 | ||
231 | return sd_bus_reply_method_return(message, NULL); | |
232 | } | |
233 | ||
e331e246 TG |
234 | const sd_bus_vtable manager_vtable[] = { |
235 | SD_BUS_VTABLE_START(0), | |
236 | ||
237 | SD_BUS_PROPERTY("OperationalState", "s", property_get_operational_state, offsetof(Manager, operational_state), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
7f3c07ad YW |
238 | SD_BUS_PROPERTY("CarrierState", "s", property_get_carrier_state, offsetof(Manager, carrier_state), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
239 | SD_BUS_PROPERTY("AddressState", "s", property_get_address_state, offsetof(Manager, address_state), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
e331e246 | 240 | |
37d577c8 | 241 | SD_BUS_METHOD("ListLinks", NULL, "a(iso)", method_list_links, SD_BUS_VTABLE_UNPRIVILEGED), |
071712b2 YW |
242 | SD_BUS_METHOD("GetLinkByName", "s", "io", method_get_link_by_name, SD_BUS_VTABLE_UNPRIVILEGED), |
243 | SD_BUS_METHOD("GetLinkByIndex", "i", "so", method_get_link_by_index, SD_BUS_VTABLE_UNPRIVILEGED), | |
15761549 YW |
244 | SD_BUS_METHOD("SetLinkNTP", "ias", NULL, bus_method_set_link_ntp_servers, SD_BUS_VTABLE_UNPRIVILEGED), |
245 | SD_BUS_METHOD("SetLinkDNS", "ia(iay)", NULL, bus_method_set_link_dns_servers, SD_BUS_VTABLE_UNPRIVILEGED), | |
246 | SD_BUS_METHOD("SetLinkDomains", "ia(sb)", NULL, bus_method_set_link_domains, SD_BUS_VTABLE_UNPRIVILEGED), | |
247 | SD_BUS_METHOD("SetLinkDefaultRoute", "ib", NULL, bus_method_set_link_default_route, SD_BUS_VTABLE_UNPRIVILEGED), | |
248 | SD_BUS_METHOD("SetLinkLLMNR", "is", NULL, bus_method_set_link_llmnr, SD_BUS_VTABLE_UNPRIVILEGED), | |
249 | SD_BUS_METHOD("SetLinkMulticastDNS", "is", NULL, bus_method_set_link_mdns, SD_BUS_VTABLE_UNPRIVILEGED), | |
250 | SD_BUS_METHOD("SetLinkDNSOverTLS", "is", NULL, bus_method_set_link_dns_over_tls, SD_BUS_VTABLE_UNPRIVILEGED), | |
251 | SD_BUS_METHOD("SetLinkDNSSEC", "is", NULL, bus_method_set_link_dnssec, SD_BUS_VTABLE_UNPRIVILEGED), | |
252 | SD_BUS_METHOD("SetLinkDNSSECNegativeTrustAnchors", "ias", NULL, bus_method_set_link_dnssec_negative_trust_anchors, SD_BUS_VTABLE_UNPRIVILEGED), | |
253 | SD_BUS_METHOD("RevertLinkNTP", "i", NULL, bus_method_revert_link_ntp, SD_BUS_VTABLE_UNPRIVILEGED), | |
254 | SD_BUS_METHOD("RevertLinkDNS", "i", NULL, bus_method_revert_link_dns, SD_BUS_VTABLE_UNPRIVILEGED), | |
ae65d7db | 255 | SD_BUS_METHOD("RenewLink", "i", NULL, bus_method_renew_link, SD_BUS_VTABLE_UNPRIVILEGED), |
90867f6a | 256 | SD_BUS_METHOD("ForceRenewLink", "i", NULL, bus_method_force_renew_link, SD_BUS_VTABLE_UNPRIVILEGED), |
99b8517c | 257 | SD_BUS_METHOD("ReconfigureLink", "i", NULL, bus_method_reconfigure_link, SD_BUS_VTABLE_UNPRIVILEGED), |
7f06b3e1 | 258 | SD_BUS_METHOD("Reload", NULL, NULL, bus_method_reload, SD_BUS_VTABLE_UNPRIVILEGED), |
37d577c8 | 259 | |
e331e246 TG |
260 | SD_BUS_VTABLE_END |
261 | }; | |
262 | ||
46606fdd | 263 | int manager_send_changed_strv(Manager *manager, char **properties) { |
e331e246 | 264 | assert(manager); |
46606fdd | 265 | assert(properties); |
e331e246 | 266 | |
4852e159 | 267 | if (!manager->bus) |
46606fdd | 268 | return 0; |
e331e246 TG |
269 | |
270 | return sd_bus_emit_properties_changed_strv( | |
271 | manager->bus, | |
272 | "/org/freedesktop/network1", | |
273 | "org.freedesktop.network1.Manager", | |
46606fdd YW |
274 | properties); |
275 | } |