]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
f579559b | 2 | |
091a364c | 3 | #include <sys/socket.h> |
bbf7c048 | 4 | #include <linux/if.h> |
bce67bbe | 5 | #include <linux/fib_rules.h> |
0d536673 | 6 | #include <stdio_ext.h> |
3bef724f | 7 | |
fc2f9534 | 8 | #include "sd-daemon.h" |
07630cea | 9 | #include "sd-netlink.h" |
fc2f9534 | 10 | |
b5efdb8a | 11 | #include "alloc-util.h" |
a97dcc12 | 12 | #include "bus-util.h" |
07630cea | 13 | #include "conf-parser.h" |
a97dcc12 | 14 | #include "def.h" |
21486d9e | 15 | #include "device-util.h" |
482d1aeb | 16 | #include "dns-domain.h" |
3ffd4af2 | 17 | #include "fd-util.h" |
0d39fa9c | 18 | #include "fileio.h" |
4f5f911e | 19 | #include "local-addresses.h" |
07630cea | 20 | #include "netlink-util.h" |
23f53b99 | 21 | #include "networkd-manager.h" |
00616955 | 22 | #include "ordered-set.h" |
07630cea LP |
23 | #include "path-util.h" |
24 | #include "set.h" | |
21486d9e | 25 | #include "strv.h" |
07630cea | 26 | #include "virt.h" |
505f8da7 | 27 | |
be660c37 AR |
28 | /* use 8 MB for receive socket kernel queue. */ |
29 | #define RCVBUF_SIZE (8*1024*1024) | |
30 | ||
2ad8416d ZJS |
31 | const char* const network_dirs[] = { |
32 | "/etc/systemd/network", | |
33 | "/run/systemd/network", | |
34 | "/usr/lib/systemd/network", | |
349cc4a5 | 35 | #if HAVE_SPLIT_USR |
2ad8416d ZJS |
36 | "/lib/systemd/network", |
37 | #endif | |
38 | NULL}; | |
39 | ||
11bf3cce LP |
40 | static int setup_default_address_pool(Manager *m) { |
41 | AddressPool *p; | |
42 | int r; | |
43 | ||
44 | assert(m); | |
45 | ||
46 | /* Add in the well-known private address ranges. */ | |
47 | ||
48 | r = address_pool_new_from_string(m, &p, AF_INET6, "fc00::", 7); | |
49 | if (r < 0) | |
50 | return r; | |
51 | ||
52 | r = address_pool_new_from_string(m, &p, AF_INET, "192.168.0.0", 16); | |
53 | if (r < 0) | |
54 | return r; | |
55 | ||
56 | r = address_pool_new_from_string(m, &p, AF_INET, "172.16.0.0", 12); | |
57 | if (r < 0) | |
58 | return r; | |
59 | ||
60 | r = address_pool_new_from_string(m, &p, AF_INET, "10.0.0.0", 8); | |
61 | if (r < 0) | |
62 | return r; | |
63 | ||
64 | return 0; | |
65 | } | |
66 | ||
9c0a72f9 TG |
67 | static int manager_reset_all(Manager *m) { |
68 | Link *link; | |
69 | Iterator i; | |
70 | int r; | |
71 | ||
72 | assert(m); | |
73 | ||
74 | HASHMAP_FOREACH(link, m->links, i) { | |
75 | r = link_carrier_reset(link); | |
76 | if (r < 0) | |
2f3cf1f9 | 77 | log_link_warning_errno(link, r, "Could not reset carrier: %m"); |
9c0a72f9 TG |
78 | } |
79 | ||
80 | return 0; | |
81 | } | |
82 | ||
19070062 | 83 | static int match_prepare_for_sleep(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) { |
9c0a72f9 TG |
84 | Manager *m = userdata; |
85 | int b, r; | |
86 | ||
19070062 | 87 | assert(message); |
d7afd945 | 88 | assert(m); |
9c0a72f9 TG |
89 | |
90 | r = sd_bus_message_read(message, "b", &b); | |
91 | if (r < 0) { | |
92 | log_debug_errno(r, "Failed to parse PrepareForSleep signal: %m"); | |
93 | return 0; | |
94 | } | |
95 | ||
96 | if (b) | |
97 | return 0; | |
98 | ||
99 | log_debug("Coming back from suspend, resetting all connections..."); | |
100 | ||
e1694a75 | 101 | (void) manager_reset_all(m); |
9c0a72f9 TG |
102 | |
103 | return 0; | |
104 | } | |
105 | ||
d7afd945 LP |
106 | static int on_connected(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) { |
107 | Manager *m = userdata; | |
9c0a72f9 | 108 | |
d7afd945 | 109 | assert(message); |
9c0a72f9 TG |
110 | assert(m); |
111 | ||
d7afd945 LP |
112 | /* Did we get a timezone or transient hostname from DHCP while D-Bus wasn't up yet? */ |
113 | if (m->dynamic_hostname) | |
114 | (void) manager_set_hostname(m, m->dynamic_hostname); | |
115 | if (m->dynamic_timezone) | |
116 | (void) manager_set_timezone(m, m->dynamic_timezone); | |
27dfc982 YW |
117 | if (m->links_requesting_uuid) |
118 | (void) manager_request_product_uuid(m, NULL); | |
9c0a72f9 | 119 | |
d7afd945 LP |
120 | return 0; |
121 | } | |
9c0a72f9 | 122 | |
d7afd945 LP |
123 | int manager_connect_bus(Manager *m) { |
124 | int r; | |
125 | ||
126 | assert(m); | |
9c0a72f9 | 127 | |
d7afd945 | 128 | if (m->bus) |
9c0a72f9 | 129 | return 0; |
7d6884b6 | 130 | |
621e4509 | 131 | r = bus_open_system_watch_bind_with_description(&m->bus, "bus-api-network"); |
9c0a72f9 | 132 | if (r < 0) |
d7afd945 | 133 | return log_error_errno(r, "Failed to connect to bus: %m"); |
9c0a72f9 | 134 | |
e331e246 TG |
135 | r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/network1", "org.freedesktop.network1.Manager", manager_vtable, m); |
136 | if (r < 0) | |
137 | return log_error_errno(r, "Failed to add manager object vtable: %m"); | |
138 | ||
139 | r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/network1/link", "org.freedesktop.network1.Link", link_vtable, link_object_find, m); | |
140 | if (r < 0) | |
141 | return log_error_errno(r, "Failed to add link object vtable: %m"); | |
142 | ||
143 | r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/network1/link", link_node_enumerator, m); | |
144 | if (r < 0) | |
145 | return log_error_errno(r, "Failed to add link enumerator: %m"); | |
3175fcde TG |
146 | |
147 | r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/network1/network", "org.freedesktop.network1.Network", network_vtable, network_object_find, m); | |
148 | if (r < 0) | |
149 | return log_error_errno(r, "Failed to add network object vtable: %m"); | |
150 | ||
151 | r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/network1/network", network_node_enumerator, m); | |
152 | if (r < 0) | |
153 | return log_error_errno(r, "Failed to add network enumerator: %m"); | |
e331e246 | 154 | |
696fc836 | 155 | r = sd_bus_request_name_async(m->bus, NULL, "org.freedesktop.network1", 0, NULL, NULL); |
e331e246 | 156 | if (r < 0) |
0c0b9306 | 157 | return log_error_errno(r, "Failed to request name: %m"); |
e331e246 TG |
158 | |
159 | r = sd_bus_attach_event(m->bus, m->event, 0); | |
160 | if (r < 0) | |
161 | return log_error_errno(r, "Failed to attach bus to event loop: %m"); | |
162 | ||
d7afd945 LP |
163 | r = sd_bus_match_signal_async( |
164 | m->bus, | |
cad43595 | 165 | NULL, |
d7afd945 LP |
166 | "org.freedesktop.DBus.Local", |
167 | NULL, | |
168 | "org.freedesktop.DBus.Local", | |
169 | "Connected", | |
170 | on_connected, NULL, m); | |
171 | if (r < 0) | |
172 | return log_error_errno(r, "Failed to request match on Connected signal: %m"); | |
173 | ||
174 | r = sd_bus_match_signal_async( | |
175 | m->bus, | |
cad43595 | 176 | NULL, |
d7afd945 LP |
177 | "org.freedesktop.login1", |
178 | "/org/freedesktop/login1", | |
179 | "org.freedesktop.login1.Manager", | |
180 | "PrepareForSleep", | |
181 | match_prepare_for_sleep, NULL, m); | |
182 | if (r < 0) | |
183 | log_warning_errno(r, "Failed to request match for PrepareForSleep, ignoring: %m"); | |
7901cea1 | 184 | |
9c0a72f9 TG |
185 | return 0; |
186 | } | |
187 | ||
d2ebf952 YW |
188 | static int manager_udev_process_link(sd_device_monitor *monitor, sd_device *device, void *userdata) { |
189 | Manager *m = userdata; | |
51517f9e | 190 | const char *action; |
5fae368b TG |
191 | Link *link = NULL; |
192 | int r, ifindex; | |
5544ee85 | 193 | |
5fae368b TG |
194 | assert(m); |
195 | assert(device); | |
5544ee85 | 196 | |
51517f9e | 197 | r = sd_device_get_property_value(device, "ACTION", &action); |
21486d9e YW |
198 | if (r < 0) { |
199 | log_device_debug_errno(device, r, "Failed to get 'ACTION' property, ignoring device: %m"); | |
5fae368b | 200 | return 0; |
21486d9e YW |
201 | } |
202 | ||
203 | if (!STR_IN_SET(action, "add", "change")) { | |
7946d57f | 204 | log_device_debug(device, "Ignoring udev %s event for device.", action); |
21486d9e YW |
205 | return 0; |
206 | } | |
5544ee85 | 207 | |
51517f9e | 208 | r = sd_device_get_ifindex(device, &ifindex); |
7606377e | 209 | if (r < 0) { |
21486d9e | 210 | log_device_debug_errno(device, r, "Ignoring udev ADD event for device without ifindex or with invalid ifindex: %m"); |
5fae368b | 211 | return 0; |
5544ee85 TG |
212 | } |
213 | ||
5fae368b | 214 | r = link_get(m, ifindex, &link); |
d2ebf952 YW |
215 | if (r < 0) { |
216 | if (r != -ENODEV) | |
217 | log_debug_errno(r, "Failed to get link from ifindex %i, ignoring: %m", ifindex); | |
5fae368b | 218 | return 0; |
d2ebf952 | 219 | } |
02b59d57 | 220 | |
d2ebf952 | 221 | (void) link_initialized(link, device); |
e1694a75 | 222 | |
f579559b TG |
223 | return 0; |
224 | } | |
225 | ||
5fae368b TG |
226 | static int manager_connect_udev(Manager *m) { |
227 | int r; | |
f579559b | 228 | |
5fae368b TG |
229 | /* udev does not initialize devices inside containers, |
230 | * so we rely on them being already initialized before | |
231 | * entering the container */ | |
75f86906 | 232 | if (detect_container() > 0) |
5fae368b | 233 | return 0; |
f579559b | 234 | |
d2ebf952 | 235 | r = sd_device_monitor_new(&m->device_monitor); |
02b59d57 | 236 | if (r < 0) |
d2ebf952 | 237 | return log_error_errno(r, "Failed to initialize device monitor: %m"); |
02b59d57 | 238 | |
d2ebf952 YW |
239 | r = sd_device_monitor_filter_add_match_subsystem_devtype(m->device_monitor, "net", NULL); |
240 | if (r < 0) | |
241 | return log_error_errno(r, "Could not add device monitor filter: %m"); | |
505f8da7 | 242 | |
d2ebf952 | 243 | r = sd_device_monitor_attach_event(m->device_monitor, m->event, 0); |
5fae368b | 244 | if (r < 0) |
d2ebf952 | 245 | return log_error_errno(r, "Failed to attach event to device monitor: %m"); |
505f8da7 | 246 | |
d2ebf952 | 247 | r = sd_device_monitor_start(m->device_monitor, manager_udev_process_link, m, "networkd-device-monitor"); |
505f8da7 | 248 | if (r < 0) |
d2ebf952 | 249 | return log_error_errno(r, "Failed to start device monitor: %m"); |
11a7f229 | 250 | |
505f8da7 TG |
251 | return 0; |
252 | } | |
f579559b | 253 | |
1c8e710c TG |
254 | int manager_rtnl_process_route(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) { |
255 | Manager *m = userdata; | |
256 | Link *link = NULL; | |
257 | uint16_t type; | |
258 | uint32_t ifindex, priority = 0; | |
983226f3 | 259 | unsigned char protocol, scope, tos, table, rt_type; |
1c8e710c TG |
260 | int family; |
261 | unsigned char dst_prefixlen, src_prefixlen; | |
262 | union in_addr_union dst = {}, gw = {}, src = {}, prefsrc = {}; | |
263 | Route *route = NULL; | |
264 | int r; | |
265 | ||
266 | assert(rtnl); | |
267 | assert(message); | |
268 | assert(m); | |
269 | ||
270 | if (sd_netlink_message_is_error(message)) { | |
271 | r = sd_netlink_message_get_errno(message); | |
272 | if (r < 0) | |
e1694a75 | 273 | log_warning_errno(r, "rtnl: failed to receive route, ignoring: %m"); |
1c8e710c TG |
274 | |
275 | return 0; | |
276 | } | |
277 | ||
278 | r = sd_netlink_message_get_type(message, &type); | |
279 | if (r < 0) { | |
e1694a75 | 280 | log_warning_errno(r, "rtnl: could not get message type, ignoring: %m"); |
1c8e710c | 281 | return 0; |
25a1bffc | 282 | } else if (!IN_SET(type, RTM_NEWROUTE, RTM_DELROUTE)) { |
e1694a75 | 283 | log_warning("rtnl: received unexpected message type when processing route, ignoring"); |
1c8e710c TG |
284 | return 0; |
285 | } | |
286 | ||
287 | r = sd_netlink_message_read_u32(message, RTA_OIF, &ifindex); | |
288 | if (r == -ENODATA) { | |
289 | log_debug("rtnl: received route without ifindex, ignoring"); | |
290 | return 0; | |
291 | } else if (r < 0) { | |
292 | log_warning_errno(r, "rtnl: could not get ifindex from route, ignoring: %m"); | |
293 | return 0; | |
294 | } else if (ifindex <= 0) { | |
295 | log_warning("rtnl: received route message with invalid ifindex, ignoring: %d", ifindex); | |
296 | return 0; | |
297 | } else { | |
298 | r = link_get(m, ifindex, &link); | |
299 | if (r < 0 || !link) { | |
300 | /* when enumerating we might be out of sync, but we will | |
301 | * get the route again, so just ignore it */ | |
302 | if (!m->enumerating) | |
303 | log_warning("rtnl: received route for nonexistent link (%d), ignoring", ifindex); | |
304 | return 0; | |
305 | } | |
306 | } | |
307 | ||
308 | r = sd_rtnl_message_route_get_family(message, &family); | |
309 | if (r < 0 || !IN_SET(family, AF_INET, AF_INET6)) { | |
e1694a75 | 310 | log_link_warning(link, "rtnl: received address with invalid family, ignoring"); |
1c8e710c TG |
311 | return 0; |
312 | } | |
313 | ||
314 | r = sd_rtnl_message_route_get_protocol(message, &protocol); | |
315 | if (r < 0) { | |
316 | log_warning_errno(r, "rtnl: could not get route protocol: %m"); | |
317 | return 0; | |
318 | } | |
319 | ||
320 | switch (family) { | |
321 | case AF_INET: | |
322 | r = sd_netlink_message_read_in_addr(message, RTA_DST, &dst.in); | |
323 | if (r < 0 && r != -ENODATA) { | |
324 | log_link_warning_errno(link, r, "rtnl: received route without valid destination, ignoring: %m"); | |
325 | return 0; | |
326 | } | |
327 | ||
328 | r = sd_netlink_message_read_in_addr(message, RTA_GATEWAY, &gw.in); | |
329 | if (r < 0 && r != -ENODATA) { | |
330 | log_link_warning_errno(link, r, "rtnl: received route with invalid gateway, ignoring: %m"); | |
331 | return 0; | |
332 | } | |
333 | ||
334 | r = sd_netlink_message_read_in_addr(message, RTA_SRC, &src.in); | |
335 | if (r < 0 && r != -ENODATA) { | |
336 | log_link_warning_errno(link, r, "rtnl: received route with invalid source, ignoring: %m"); | |
337 | return 0; | |
338 | } | |
339 | ||
340 | r = sd_netlink_message_read_in_addr(message, RTA_PREFSRC, &prefsrc.in); | |
341 | if (r < 0 && r != -ENODATA) { | |
342 | log_link_warning_errno(link, r, "rtnl: received route with invalid preferred source, ignoring: %m"); | |
343 | return 0; | |
344 | } | |
345 | ||
346 | break; | |
347 | ||
348 | case AF_INET6: | |
349 | r = sd_netlink_message_read_in6_addr(message, RTA_DST, &dst.in6); | |
350 | if (r < 0 && r != -ENODATA) { | |
351 | log_link_warning_errno(link, r, "rtnl: received route without valid destination, ignoring: %m"); | |
352 | return 0; | |
353 | } | |
354 | ||
355 | r = sd_netlink_message_read_in6_addr(message, RTA_GATEWAY, &gw.in6); | |
356 | if (r < 0 && r != -ENODATA) { | |
357 | log_link_warning_errno(link, r, "rtnl: received route with invalid gateway, ignoring: %m"); | |
358 | return 0; | |
359 | } | |
360 | ||
361 | r = sd_netlink_message_read_in6_addr(message, RTA_SRC, &src.in6); | |
362 | if (r < 0 && r != -ENODATA) { | |
363 | log_link_warning_errno(link, r, "rtnl: received route with invalid source, ignoring: %m"); | |
364 | return 0; | |
365 | } | |
366 | ||
367 | r = sd_netlink_message_read_in6_addr(message, RTA_PREFSRC, &prefsrc.in6); | |
368 | if (r < 0 && r != -ENODATA) { | |
369 | log_link_warning_errno(link, r, "rtnl: received route with invalid preferred source, ignoring: %m"); | |
370 | return 0; | |
371 | } | |
372 | ||
373 | break; | |
374 | ||
375 | default: | |
74dead34 | 376 | assert_not_reached("Received unsupported address family"); |
1c8e710c TG |
377 | return 0; |
378 | } | |
379 | ||
380 | r = sd_rtnl_message_route_get_dst_prefixlen(message, &dst_prefixlen); | |
381 | if (r < 0) { | |
382 | log_link_warning_errno(link, r, "rtnl: received route with invalid destination prefixlen, ignoring: %m"); | |
383 | return 0; | |
384 | } | |
385 | ||
386 | r = sd_rtnl_message_route_get_src_prefixlen(message, &src_prefixlen); | |
387 | if (r < 0) { | |
388 | log_link_warning_errno(link, r, "rtnl: received route with invalid source prefixlen, ignoring: %m"); | |
389 | return 0; | |
390 | } | |
391 | ||
392 | r = sd_rtnl_message_route_get_scope(message, &scope); | |
393 | if (r < 0) { | |
394 | log_link_warning_errno(link, r, "rtnl: received route with invalid scope, ignoring: %m"); | |
395 | return 0; | |
396 | } | |
397 | ||
398 | r = sd_rtnl_message_route_get_tos(message, &tos); | |
399 | if (r < 0) { | |
400 | log_link_warning_errno(link, r, "rtnl: received route with invalid tos, ignoring: %m"); | |
401 | return 0; | |
402 | } | |
403 | ||
983226f3 SS |
404 | r = sd_rtnl_message_route_get_type(message, &rt_type); |
405 | if (r < 0) { | |
406 | log_link_warning_errno(link, r, "rtnl: received route with invalid type, ignoring: %m"); | |
407 | return 0; | |
408 | } | |
409 | ||
1c8e710c TG |
410 | r = sd_rtnl_message_route_get_table(message, &table); |
411 | if (r < 0) { | |
412 | log_link_warning_errno(link, r, "rtnl: received route with invalid table, ignoring: %m"); | |
413 | return 0; | |
414 | } | |
415 | ||
416 | r = sd_netlink_message_read_u32(message, RTA_PRIORITY, &priority); | |
417 | if (r < 0 && r != -ENODATA) { | |
418 | log_link_warning_errno(link, r, "rtnl: received route with invalid priority, ignoring: %m"); | |
419 | return 0; | |
420 | } | |
421 | ||
e1694a75 | 422 | (void) route_get(link, family, &dst, dst_prefixlen, tos, priority, table, &route); |
1c8e710c TG |
423 | |
424 | switch (type) { | |
425 | case RTM_NEWROUTE: | |
426 | if (!route) { | |
427 | /* A route appeared that we did not request */ | |
428 | r = route_add_foreign(link, family, &dst, dst_prefixlen, tos, priority, table, &route); | |
e1694a75 YW |
429 | if (r < 0) { |
430 | log_link_warning_errno(link, r, "Failed to add route, ignoring: %m"); | |
1c8e710c | 431 | return 0; |
e1694a75 | 432 | } |
1c8e710c TG |
433 | } |
434 | ||
bbd15900 | 435 | route_update(route, &src, src_prefixlen, &gw, &prefsrc, scope, protocol, rt_type); |
1c8e710c TG |
436 | |
437 | break; | |
438 | ||
439 | case RTM_DELROUTE: | |
b9642f41 | 440 | route_free(route); |
1c8e710c | 441 | break; |
b9642f41 | 442 | |
1c8e710c TG |
443 | default: |
444 | assert_not_reached("Received invalid RTNL message type"); | |
445 | } | |
446 | ||
447 | return 1; | |
448 | } | |
449 | ||
200a0868 TG |
450 | int manager_rtnl_process_address(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) { |
451 | Manager *m = userdata; | |
452 | Link *link = NULL; | |
453 | uint16_t type; | |
200a0868 | 454 | unsigned char flags; |
054f0db4 TG |
455 | int family; |
456 | unsigned char prefixlen; | |
457 | unsigned char scope; | |
458 | union in_addr_union in_addr; | |
459 | struct ifa_cacheinfo cinfo; | |
460 | Address *address = NULL; | |
200a0868 TG |
461 | char buf[INET6_ADDRSTRLEN], valid_buf[FORMAT_TIMESPAN_MAX]; |
462 | const char *valid_str = NULL; | |
463 | int r, ifindex; | |
464 | ||
465 | assert(rtnl); | |
466 | assert(message); | |
467 | assert(m); | |
468 | ||
469 | if (sd_netlink_message_is_error(message)) { | |
470 | r = sd_netlink_message_get_errno(message); | |
471 | if (r < 0) | |
e1694a75 | 472 | log_warning_errno(r, "rtnl: failed to receive address, ignoring: %m"); |
200a0868 TG |
473 | |
474 | return 0; | |
475 | } | |
476 | ||
477 | r = sd_netlink_message_get_type(message, &type); | |
478 | if (r < 0) { | |
e1694a75 | 479 | log_warning_errno(r, "rtnl: could not get message type, ignoring: %m"); |
200a0868 | 480 | return 0; |
25a1bffc | 481 | } else if (!IN_SET(type, RTM_NEWADDR, RTM_DELADDR)) { |
e1694a75 | 482 | log_warning("rtnl: received unexpected message type when processing address, ignoring"); |
200a0868 TG |
483 | return 0; |
484 | } | |
485 | ||
486 | r = sd_rtnl_message_addr_get_ifindex(message, &ifindex); | |
487 | if (r < 0) { | |
e1694a75 | 488 | log_warning_errno(r, "rtnl: could not get ifindex from address, ignoring: %m"); |
200a0868 TG |
489 | return 0; |
490 | } else if (ifindex <= 0) { | |
e1694a75 | 491 | log_warning("rtnl: received address message with invalid ifindex, ignoring: %d", ifindex); |
200a0868 TG |
492 | return 0; |
493 | } else { | |
494 | r = link_get(m, ifindex, &link); | |
495 | if (r < 0 || !link) { | |
496 | /* when enumerating we might be out of sync, but we will | |
497 | * get the address again, so just ignore it */ | |
498 | if (!m->enumerating) | |
499 | log_warning("rtnl: received address for nonexistent link (%d), ignoring", ifindex); | |
500 | return 0; | |
501 | } | |
502 | } | |
503 | ||
054f0db4 TG |
504 | r = sd_rtnl_message_addr_get_family(message, &family); |
505 | if (r < 0 || !IN_SET(family, AF_INET, AF_INET6)) { | |
e1694a75 | 506 | log_link_warning(link, "rtnl: received address with invalid family, ignoring"); |
200a0868 TG |
507 | return 0; |
508 | } | |
509 | ||
054f0db4 | 510 | r = sd_rtnl_message_addr_get_prefixlen(message, &prefixlen); |
200a0868 TG |
511 | if (r < 0) { |
512 | log_link_warning_errno(link, r, "rtnl: received address with invalid prefixlen, ignoring: %m"); | |
513 | return 0; | |
514 | } | |
515 | ||
054f0db4 | 516 | r = sd_rtnl_message_addr_get_scope(message, &scope); |
200a0868 TG |
517 | if (r < 0) { |
518 | log_link_warning_errno(link, r, "rtnl: received address with invalid scope, ignoring: %m"); | |
519 | return 0; | |
520 | } | |
521 | ||
522 | r = sd_rtnl_message_addr_get_flags(message, &flags); | |
523 | if (r < 0) { | |
524 | log_link_warning_errno(link, r, "rtnl: received address with invalid flags, ignoring: %m"); | |
525 | return 0; | |
526 | } | |
200a0868 | 527 | |
054f0db4 | 528 | switch (family) { |
200a0868 | 529 | case AF_INET: |
054f0db4 | 530 | r = sd_netlink_message_read_in_addr(message, IFA_LOCAL, &in_addr.in); |
200a0868 TG |
531 | if (r < 0) { |
532 | log_link_warning_errno(link, r, "rtnl: received address without valid address, ignoring: %m"); | |
533 | return 0; | |
534 | } | |
535 | ||
536 | break; | |
537 | ||
538 | case AF_INET6: | |
054f0db4 | 539 | r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &in_addr.in6); |
200a0868 TG |
540 | if (r < 0) { |
541 | log_link_warning_errno(link, r, "rtnl: received address without valid address, ignoring: %m"); | |
542 | return 0; | |
543 | } | |
544 | ||
545 | break; | |
546 | ||
547 | default: | |
e1694a75 | 548 | assert_not_reached("Received unsupported address family"); |
200a0868 TG |
549 | } |
550 | ||
054f0db4 | 551 | if (!inet_ntop(family, &in_addr, buf, INET6_ADDRSTRLEN)) { |
e1694a75 | 552 | log_link_warning(link, "Could not print address, ignoring"); |
200a0868 TG |
553 | return 0; |
554 | } | |
555 | ||
054f0db4 | 556 | r = sd_netlink_message_read_cache_info(message, IFA_CACHEINFO, &cinfo); |
e1694a75 YW |
557 | if (r < 0 && r != -ENODATA) { |
558 | log_link_warning_errno(link, r, "rtnl: cannot get IFA_CACHEINFO attribute, ignoring: %m"); | |
559 | return 0; | |
560 | } else if (r >= 0) { | |
4058d339 | 561 | if (cinfo.ifa_valid != CACHE_INFO_INFINITY_LIFE_TIME) |
200a0868 | 562 | valid_str = format_timespan(valid_buf, FORMAT_TIMESPAN_MAX, |
054f0db4 | 563 | cinfo.ifa_valid * USEC_PER_SEC, |
200a0868 TG |
564 | USEC_PER_SEC); |
565 | } | |
566 | ||
e1694a75 | 567 | (void) address_get(link, family, &in_addr, prefixlen, &address); |
200a0868 TG |
568 | |
569 | switch (type) { | |
570 | case RTM_NEWADDR: | |
36c32f61 | 571 | if (address) |
4058d339 | 572 | log_link_debug(link, "Updating address: %s/%u (valid %s%s)", buf, prefixlen, |
87e4e28d | 573 | valid_str ? "for " : "forever", strempty(valid_str)); |
36c32f61 | 574 | else { |
adda1ed9 TG |
575 | /* An address appeared that we did not request */ |
576 | r = address_add_foreign(link, family, &in_addr, prefixlen, &address); | |
cf1d700d | 577 | if (r < 0) { |
e1694a75 | 578 | log_link_warning_errno(link, r, "Failed to add address %s/%u, ignoring: %m", buf, prefixlen); |
cf1d700d TG |
579 | return 0; |
580 | } else | |
4058d339 | 581 | log_link_debug(link, "Adding address: %s/%u (valid %s%s)", buf, prefixlen, |
87e4e28d | 582 | valid_str ? "for " : "forever", strempty(valid_str)); |
200a0868 TG |
583 | } |
584 | ||
e1694a75 YW |
585 | r = address_update(address, flags, scope, &cinfo); |
586 | if (r < 0) { | |
587 | log_link_warning_errno(link, r, "Failed to update address %s/%u, ignoring: %m", buf, prefixlen); | |
588 | return 0; | |
589 | } | |
36c32f61 | 590 | |
200a0868 TG |
591 | break; |
592 | ||
593 | case RTM_DELADDR: | |
594 | ||
054f0db4 | 595 | if (address) { |
4058d339 | 596 | log_link_debug(link, "Removing address: %s/%u (valid %s%s)", buf, prefixlen, |
87e4e28d | 597 | valid_str ? "for " : "forever", strempty(valid_str)); |
e1694a75 | 598 | (void) address_drop(address); |
200a0868 | 599 | } else |
e1694a75 | 600 | log_link_warning(link, "Removing non-existent address: %s/%u (valid %s%s), ignoring", buf, prefixlen, |
87e4e28d | 601 | valid_str ? "for " : "forever", strempty(valid_str)); |
200a0868 TG |
602 | |
603 | break; | |
604 | default: | |
605 | assert_not_reached("Received invalid RTNL message type"); | |
606 | } | |
607 | ||
608 | return 1; | |
609 | } | |
610 | ||
1c4baffc | 611 | static int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) { |
505f8da7 TG |
612 | Manager *m = userdata; |
613 | Link *link = NULL; | |
4d473d5d | 614 | NetDev *netdev = NULL; |
f2236469 | 615 | uint16_t type; |
ca4e095a | 616 | const char *name; |
505f8da7 | 617 | int r, ifindex; |
f579559b | 618 | |
505f8da7 TG |
619 | assert(rtnl); |
620 | assert(message); | |
f579559b TG |
621 | assert(m); |
622 | ||
1c4baffc TG |
623 | if (sd_netlink_message_is_error(message)) { |
624 | r = sd_netlink_message_get_errno(message); | |
45af44d4 | 625 | if (r < 0) |
e1694a75 | 626 | log_warning_errno(r, "rtnl: Could not receive link, ignoring: %m"); |
45af44d4 TG |
627 | |
628 | return 0; | |
629 | } | |
630 | ||
1c4baffc | 631 | r = sd_netlink_message_get_type(message, &type); |
f2236469 | 632 | if (r < 0) { |
e1694a75 | 633 | log_warning_errno(r, "rtnl: Could not get message type, ignoring: %m"); |
f2236469 | 634 | return 0; |
25a1bffc | 635 | } else if (!IN_SET(type, RTM_NEWLINK, RTM_DELLINK)) { |
e1694a75 | 636 | log_warning("rtnl: Received unexpected message type when processing link, ignoring"); |
cdfee943 | 637 | return 0; |
f2236469 TG |
638 | } |
639 | ||
505f8da7 | 640 | r = sd_rtnl_message_link_get_ifindex(message, &ifindex); |
45af44d4 | 641 | if (r < 0) { |
e1694a75 | 642 | log_warning_errno(r, "rtnl: Could not get ifindex from link, ignoring: %m"); |
45af44d4 TG |
643 | return 0; |
644 | } else if (ifindex <= 0) { | |
e1694a75 | 645 | log_warning("rtnl: received link message with invalid ifindex %d, ignoring", ifindex); |
505f8da7 | 646 | return 0; |
a0db8e46 | 647 | } |
f579559b | 648 | |
1c4baffc | 649 | r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name); |
45af44d4 | 650 | if (r < 0) { |
e1694a75 | 651 | log_warning_errno(r, "rtnl: Received link message without ifname, ignoring: %m"); |
4d473d5d | 652 | return 0; |
a0db8e46 ZJS |
653 | } |
654 | ||
655 | (void) link_get(m, ifindex, &link); | |
656 | (void) netdev_get(m, name, &netdev); | |
4d473d5d TG |
657 | |
658 | switch (type) { | |
659 | case RTM_NEWLINK: | |
660 | if (!link) { | |
661 | /* link is new, so add it */ | |
662 | r = link_add(m, message, &link); | |
663 | if (r < 0) { | |
e1694a75 | 664 | log_warning_errno(r, "Could not add new link, ignoring: %m"); |
4d473d5d TG |
665 | return 0; |
666 | } | |
667 | } | |
668 | ||
669 | if (netdev) { | |
670 | /* netdev exists, so make sure the ifindex matches */ | |
505f8da7 TG |
671 | r = netdev_set_ifindex(netdev, message); |
672 | if (r < 0) { | |
e1694a75 | 673 | log_warning_errno(r, "Could not set ifindex on netdev, ignoring: %m"); |
505f8da7 TG |
674 | return 0; |
675 | } | |
676 | } | |
e1202047 | 677 | |
f2236469 | 678 | r = link_update(link, message); |
e1694a75 YW |
679 | if (r < 0) { |
680 | log_warning_errno(r, "Could not update link, ignoring: %m"); | |
f2236469 | 681 | return 0; |
e1694a75 | 682 | } |
4d473d5d TG |
683 | |
684 | break; | |
685 | ||
686 | case RTM_DELLINK: | |
687 | link_drop(link); | |
688 | netdev_drop(netdev); | |
689 | ||
690 | break; | |
691 | ||
692 | default: | |
693 | assert_not_reached("Received invalid RTNL message type."); | |
f2236469 | 694 | } |
505f8da7 TG |
695 | |
696 | return 1; | |
697 | } | |
698 | ||
bce67bbe SS |
699 | int manager_rtnl_process_rule(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) { |
700 | uint8_t tos = 0, to_prefixlen = 0, from_prefixlen = 0; | |
36e6e28b | 701 | union in_addr_union to = {}, from = {}; |
bce67bbe | 702 | RoutingPolicyRule *rule = NULL; |
bce67bbe | 703 | uint32_t fwmark = 0, table = 0; |
eeab051b | 704 | const char *iif = NULL, *oif = NULL; |
bce67bbe SS |
705 | Manager *m = userdata; |
706 | uint16_t type; | |
707 | int family; | |
708 | int r; | |
709 | ||
710 | assert(rtnl); | |
711 | assert(message); | |
712 | assert(m); | |
713 | ||
714 | if (sd_netlink_message_is_error(message)) { | |
715 | r = sd_netlink_message_get_errno(message); | |
716 | if (r < 0) | |
e1694a75 | 717 | log_warning_errno(r, "rtnl: failed to receive rule, ignoring: %m"); |
bce67bbe SS |
718 | |
719 | return 0; | |
720 | } | |
721 | ||
722 | r = sd_netlink_message_get_type(message, &type); | |
723 | if (r < 0) { | |
e1694a75 | 724 | log_warning_errno(r, "rtnl: could not get message type, ignoring: %m"); |
bce67bbe SS |
725 | return 0; |
726 | } else if (!IN_SET(type, RTM_NEWRULE, RTM_DELRULE)) { | |
e1694a75 | 727 | log_warning("rtnl: received unexpected message type '%u' when processing rule, ignoring", type); |
bce67bbe SS |
728 | return 0; |
729 | } | |
730 | ||
731 | r = sd_rtnl_message_get_family(message, &family); | |
615ded62 | 732 | if (r < 0) { |
e1694a75 | 733 | log_warning_errno(r, "rtnl: could not get rule family, ignoring: %m"); |
615ded62 YW |
734 | return 0; |
735 | } else if (!IN_SET(family, AF_INET, AF_INET6)) { | |
e1694a75 | 736 | log_debug("rtnl: received address with invalid family %u, ignoring", family); |
bce67bbe SS |
737 | return 0; |
738 | } | |
739 | ||
740 | switch (family) { | |
741 | case AF_INET: | |
742 | r = sd_netlink_message_read_in_addr(message, FRA_SRC, &from.in); | |
e1694a75 YW |
743 | if (r < 0 && r != -ENODATA) { |
744 | log_warning_errno(r, "rtnl: could not get FRA_SRC attribute, ignoring: %m"); | |
745 | return 0; | |
746 | } else if (r >= 0) { | |
bce67bbe | 747 | r = sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(message, &from_prefixlen); |
e1694a75 | 748 | if (r < 0) { |
3fe91079 | 749 | log_warning_errno(r, "rtnl: failed to retrieve rule from prefix length, ignoring: %m"); |
e1694a75 YW |
750 | return 0; |
751 | } | |
bce67bbe SS |
752 | } |
753 | ||
754 | r = sd_netlink_message_read_in_addr(message, FRA_DST, &to.in); | |
e1694a75 YW |
755 | if (r < 0 && r != -ENODATA) { |
756 | log_warning_errno(r, "rtnl: could not get FRA_DST attribute, ignoring: %m"); | |
757 | return 0; | |
758 | } else if (r >= 0) { | |
bce67bbe | 759 | r = sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(message, &to_prefixlen); |
e1694a75 | 760 | if (r < 0) { |
3fe91079 | 761 | log_warning_errno(r, "rtnl: failed to retrieve rule to prefix length, ignoring: %m"); |
e1694a75 YW |
762 | return 0; |
763 | } | |
bce67bbe SS |
764 | } |
765 | ||
766 | break; | |
767 | ||
768 | case AF_INET6: | |
769 | r = sd_netlink_message_read_in6_addr(message, FRA_SRC, &from.in6); | |
e1694a75 YW |
770 | if (r < 0 && r != -ENODATA) { |
771 | log_warning_errno(r, "rtnl: could not get FRA_SRC attribute, ignoring: %m"); | |
772 | return 0; | |
773 | } else if (r >= 0) { | |
bce67bbe | 774 | r = sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(message, &from_prefixlen); |
e1694a75 | 775 | if (r < 0) { |
3fe91079 | 776 | log_warning_errno(r, "rtnl: failed to retrieve rule from prefix length, ignoring: %m"); |
e1694a75 YW |
777 | return 0; |
778 | } | |
bce67bbe SS |
779 | } |
780 | ||
781 | r = sd_netlink_message_read_in6_addr(message, FRA_DST, &to.in6); | |
e1694a75 YW |
782 | if (r < 0 && r != -ENODATA) { |
783 | log_warning_errno(r, "rtnl: could not get FRA_DST attribute, ignoring: %m"); | |
784 | return 0; | |
785 | } else if (r >= 0) { | |
bce67bbe | 786 | r = sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(message, &to_prefixlen); |
e1694a75 | 787 | if (r < 0) { |
3fe91079 | 788 | log_warning_errno(r, "rtnl: failed to retrieve rule to prefix length, ignoring: %m"); |
e1694a75 YW |
789 | return 0; |
790 | } | |
bce67bbe SS |
791 | } |
792 | ||
793 | break; | |
794 | ||
795 | default: | |
74dead34 | 796 | assert_not_reached("Received unsupported address family"); |
bce67bbe SS |
797 | } |
798 | ||
799 | if (from_prefixlen == 0 && to_prefixlen == 0) | |
800 | return 0; | |
801 | ||
e1694a75 YW |
802 | r = sd_netlink_message_read_u32(message, FRA_FWMARK, &fwmark); |
803 | if (r < 0 && r != -ENODATA) { | |
804 | log_warning_errno(r, "rtnl: could not get FRA_FWMARK attribute, ignoring: %m"); | |
805 | return 0; | |
806 | } | |
807 | ||
808 | r = sd_netlink_message_read_u32(message, FRA_TABLE, &table); | |
809 | if (r < 0 && r != -ENODATA) { | |
810 | log_warning_errno(r, "rtnl: could not get FRA_TABLE attribute, ignoring: %m"); | |
811 | return 0; | |
812 | } | |
813 | ||
814 | r = sd_rtnl_message_routing_policy_rule_get_tos(message, &tos); | |
815 | if (r < 0 && r != -ENODATA) { | |
816 | log_warning_errno(r, "rtnl: could not get ip rule TOS, ignoring: %m"); | |
817 | return 0; | |
818 | } | |
819 | ||
eeab051b | 820 | r = sd_netlink_message_read_string(message, FRA_IIFNAME, &iif); |
e1694a75 YW |
821 | if (r < 0 && r != -ENODATA) { |
822 | log_warning_errno(r, "rtnl: could not get FRA_IIFNAME attribute, ignoring: %m"); | |
823 | return 0; | |
824 | } | |
825 | ||
eeab051b | 826 | r = sd_netlink_message_read_string(message, FRA_OIFNAME, &oif); |
e1694a75 YW |
827 | if (r < 0 && r != -ENODATA) { |
828 | log_warning_errno(r, "rtnl: could not get FRA_OIFNAME attribute, ignoring: %m"); | |
829 | return 0; | |
830 | } | |
bce67bbe | 831 | |
762e2659 | 832 | (void) routing_policy_rule_get(m, family, &from, from_prefixlen, &to, to_prefixlen, tos, fwmark, table, iif, oif, &rule); |
bce67bbe SS |
833 | |
834 | switch (type) { | |
835 | case RTM_NEWRULE: | |
508f63b4 | 836 | if (!rule) { |
762e2659 | 837 | r = routing_policy_rule_add_foreign(m, family, &from, from_prefixlen, &to, to_prefixlen, tos, fwmark, table, iif, oif, &rule); |
bce67bbe | 838 | if (r < 0) { |
e1694a75 | 839 | log_warning_errno(r, "Could not add rule, ignoring: %m"); |
bce67bbe SS |
840 | return 0; |
841 | } | |
842 | } | |
843 | break; | |
844 | case RTM_DELRULE: | |
845 | routing_policy_rule_free(rule); | |
846 | ||
847 | break; | |
848 | ||
849 | default: | |
850 | assert_not_reached("Received invalid RTNL message type"); | |
851 | } | |
852 | ||
853 | return 1; | |
854 | } | |
855 | ||
5fae368b TG |
856 | static int systemd_netlink_fd(void) { |
857 | int n, fd, rtnl_fd = -EINVAL; | |
858 | ||
859 | n = sd_listen_fds(true); | |
860 | if (n <= 0) | |
861 | return -EINVAL; | |
862 | ||
863 | for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) { | |
864 | if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) { | |
865 | if (rtnl_fd >= 0) | |
866 | return -EINVAL; | |
867 | ||
868 | rtnl_fd = fd; | |
869 | } | |
870 | } | |
871 | ||
872 | return rtnl_fd; | |
873 | } | |
874 | ||
05d0c2e3 JT |
875 | static int manager_connect_genl(Manager *m) { |
876 | int r; | |
877 | ||
878 | assert(m); | |
879 | ||
880 | r = sd_genl_socket_open(&m->genl); | |
881 | if (r < 0) | |
882 | return r; | |
883 | ||
884 | r = sd_netlink_inc_rcvbuf(m->genl, RCVBUF_SIZE); | |
885 | if (r < 0) | |
886 | return r; | |
887 | ||
888 | r = sd_netlink_attach_event(m->genl, m->event, 0); | |
889 | if (r < 0) | |
890 | return r; | |
891 | ||
892 | return 0; | |
893 | } | |
894 | ||
5fae368b TG |
895 | static int manager_connect_rtnl(Manager *m) { |
896 | int fd, r; | |
505f8da7 TG |
897 | |
898 | assert(m); | |
505f8da7 | 899 | |
5fae368b TG |
900 | fd = systemd_netlink_fd(); |
901 | if (fd < 0) | |
1c4baffc | 902 | r = sd_netlink_open(&m->rtnl); |
5fae368b | 903 | else |
1c4baffc | 904 | r = sd_netlink_open_fd(&m->rtnl, fd); |
505f8da7 TG |
905 | if (r < 0) |
906 | return r; | |
907 | ||
1c4baffc | 908 | r = sd_netlink_inc_rcvbuf(m->rtnl, RCVBUF_SIZE); |
f579559b | 909 | if (r < 0) |
bf5332d2 | 910 | return r; |
f579559b | 911 | |
1c4baffc | 912 | r = sd_netlink_attach_event(m->rtnl, m->event, 0); |
505f8da7 TG |
913 | if (r < 0) |
914 | return r; | |
f579559b | 915 | |
8190a388 | 916 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWLINK, &manager_rtnl_process_link, NULL, m, "network-rtnl_process_link"); |
5fae368b TG |
917 | if (r < 0) |
918 | return r; | |
505f8da7 | 919 | |
8190a388 | 920 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELLINK, &manager_rtnl_process_link, NULL, m, "network-rtnl_process_link"); |
5fae368b TG |
921 | if (r < 0) |
922 | return r; | |
45af44d4 | 923 | |
8190a388 | 924 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWADDR, &manager_rtnl_process_address, NULL, m, "network-rtnl_process_address"); |
5fae368b TG |
925 | if (r < 0) |
926 | return r; | |
927 | ||
8190a388 | 928 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELADDR, &manager_rtnl_process_address, NULL, m, "network-rtnl_process_address"); |
5fae368b TG |
929 | if (r < 0) |
930 | return r; | |
931 | ||
8190a388 | 932 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWROUTE, &manager_rtnl_process_route, NULL, m, "network-rtnl_process_route"); |
1c8e710c TG |
933 | if (r < 0) |
934 | return r; | |
935 | ||
8190a388 | 936 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELROUTE, &manager_rtnl_process_route, NULL, m, "network-rtnl_process_route"); |
1c8e710c TG |
937 | if (r < 0) |
938 | return r; | |
939 | ||
8190a388 | 940 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWRULE, &manager_rtnl_process_rule, NULL, m, "network-rtnl_process_rule"); |
bce67bbe SS |
941 | if (r < 0) |
942 | return r; | |
943 | ||
8190a388 | 944 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELRULE, &manager_rtnl_process_rule, NULL, m, "network-rtnl_process_rule"); |
bce67bbe SS |
945 | if (r < 0) |
946 | return r; | |
947 | ||
5fae368b | 948 | return 0; |
45af44d4 | 949 | } |
505f8da7 | 950 | |
5512a963 | 951 | static int ordered_set_put_in_addr_data(OrderedSet *s, const struct in_addr_data *address) { |
84de38c5 TG |
952 | char *p; |
953 | int r; | |
954 | ||
955 | assert(s); | |
5512a963 LP |
956 | assert(address); |
957 | ||
958 | r = in_addr_to_string(address->family, &address->address, &p); | |
959 | if (r < 0) | |
960 | return r; | |
961 | ||
962 | r = ordered_set_consume(s, p); | |
963 | if (r == -EEXIST) | |
964 | return 0; | |
965 | ||
966 | return r; | |
967 | } | |
968 | ||
969 | static int ordered_set_put_in_addr_datav(OrderedSet *s, const struct in_addr_data *addresses, unsigned n) { | |
970 | int r, c = 0; | |
971 | unsigned i; | |
972 | ||
973 | assert(s); | |
974 | assert(addresses || n == 0); | |
975 | ||
976 | for (i = 0; i < n; i++) { | |
977 | r = ordered_set_put_in_addr_data(s, addresses+i); | |
978 | if (r < 0) | |
979 | return r; | |
980 | ||
981 | c += r; | |
982 | } | |
983 | ||
984 | return c; | |
985 | } | |
986 | ||
987 | static int ordered_set_put_in4_addr(OrderedSet *s, const struct in_addr *address) { | |
988 | char *p; | |
989 | int r; | |
990 | ||
991 | assert(s); | |
992 | assert(address); | |
84de38c5 TG |
993 | |
994 | r = in_addr_to_string(AF_INET, (const union in_addr_union*) address, &p); | |
995 | if (r < 0) | |
996 | return r; | |
997 | ||
00616955 | 998 | r = ordered_set_consume(s, p); |
84de38c5 TG |
999 | if (r == -EEXIST) |
1000 | return 0; | |
1001 | ||
1002 | return r; | |
1003 | } | |
1004 | ||
5512a963 LP |
1005 | static int ordered_set_put_in4_addrv(OrderedSet *s, const struct in_addr *addresses, unsigned n) { |
1006 | int r, c = 0; | |
1007 | unsigned i; | |
84de38c5 TG |
1008 | |
1009 | assert(s); | |
5512a963 | 1010 | assert(n == 0 || addresses); |
84de38c5 TG |
1011 | |
1012 | for (i = 0; i < n; i++) { | |
5512a963 | 1013 | r = ordered_set_put_in4_addr(s, addresses+i); |
84de38c5 TG |
1014 | if (r < 0) |
1015 | return r; | |
1016 | ||
1017 | c += r; | |
1018 | } | |
1019 | ||
1020 | return c; | |
1021 | } | |
1022 | ||
00616955 | 1023 | static void print_string_set(FILE *f, const char *field, OrderedSet *s) { |
84de38c5 TG |
1024 | bool space = false; |
1025 | Iterator i; | |
1026 | char *p; | |
1027 | ||
00616955 | 1028 | if (ordered_set_isempty(s)) |
84de38c5 TG |
1029 | return; |
1030 | ||
0d536673 | 1031 | fputs(field, f); |
84de38c5 | 1032 | |
d390f8ef LP |
1033 | ORDERED_SET_FOREACH(p, s, i) |
1034 | fputs_with_space(f, p, NULL, &space); | |
1035 | ||
0d536673 | 1036 | fputc('\n', f); |
84de38c5 TG |
1037 | } |
1038 | ||
1039 | static int manager_save(Manager *m) { | |
00616955 | 1040 | _cleanup_ordered_set_free_free_ OrderedSet *dns = NULL, *ntp = NULL, *search_domains = NULL, *route_domains = NULL; |
84de38c5 TG |
1041 | Link *link; |
1042 | Iterator i; | |
1043 | _cleanup_free_ char *temp_path = NULL; | |
1044 | _cleanup_fclose_ FILE *f = NULL; | |
1045 | LinkOperationalState operstate = LINK_OPERSTATE_OFF; | |
1046 | const char *operstate_str; | |
1047 | int r; | |
1048 | ||
1049 | assert(m); | |
1050 | assert(m->state_file); | |
1051 | ||
1052 | /* We add all NTP and DNS server to a set, to filter out duplicates */ | |
00616955 | 1053 | dns = ordered_set_new(&string_hash_ops); |
84de38c5 TG |
1054 | if (!dns) |
1055 | return -ENOMEM; | |
1056 | ||
00616955 | 1057 | ntp = ordered_set_new(&string_hash_ops); |
84de38c5 TG |
1058 | if (!ntp) |
1059 | return -ENOMEM; | |
1060 | ||
482d1aeb | 1061 | search_domains = ordered_set_new(&dns_name_hash_ops); |
3df9bec5 LP |
1062 | if (!search_domains) |
1063 | return -ENOMEM; | |
1064 | ||
482d1aeb | 1065 | route_domains = ordered_set_new(&dns_name_hash_ops); |
3df9bec5 | 1066 | if (!route_domains) |
84de38c5 TG |
1067 | return -ENOMEM; |
1068 | ||
1069 | HASHMAP_FOREACH(link, m->links, i) { | |
1070 | if (link->flags & IFF_LOOPBACK) | |
1071 | continue; | |
1072 | ||
1073 | if (link->operstate > operstate) | |
1074 | operstate = link->operstate; | |
1075 | ||
1076 | if (!link->network) | |
1077 | continue; | |
1078 | ||
1079 | /* First add the static configured entries */ | |
5512a963 | 1080 | r = ordered_set_put_in_addr_datav(dns, link->network->dns, link->network->n_dns); |
84de38c5 TG |
1081 | if (r < 0) |
1082 | return r; | |
1083 | ||
00616955 | 1084 | r = ordered_set_put_strdupv(ntp, link->network->ntp); |
84de38c5 TG |
1085 | if (r < 0) |
1086 | return r; | |
1087 | ||
00616955 | 1088 | r = ordered_set_put_strdupv(search_domains, link->network->search_domains); |
3df9bec5 LP |
1089 | if (r < 0) |
1090 | return r; | |
1091 | ||
00616955 | 1092 | r = ordered_set_put_strdupv(route_domains, link->network->route_domains); |
84de38c5 TG |
1093 | if (r < 0) |
1094 | return r; | |
1095 | ||
1096 | if (!link->dhcp_lease) | |
1097 | continue; | |
1098 | ||
1099 | /* Secondly, add the entries acquired via DHCP */ | |
27cb34f5 | 1100 | if (link->network->dhcp_use_dns) { |
84de38c5 TG |
1101 | const struct in_addr *addresses; |
1102 | ||
1103 | r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses); | |
1104 | if (r > 0) { | |
5512a963 | 1105 | r = ordered_set_put_in4_addrv(dns, addresses, r); |
84de38c5 TG |
1106 | if (r < 0) |
1107 | return r; | |
1108 | } else if (r < 0 && r != -ENODATA) | |
1109 | return r; | |
1110 | } | |
1111 | ||
27cb34f5 | 1112 | if (link->network->dhcp_use_ntp) { |
84de38c5 TG |
1113 | const struct in_addr *addresses; |
1114 | ||
1115 | r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses); | |
1116 | if (r > 0) { | |
5512a963 | 1117 | r = ordered_set_put_in4_addrv(ntp, addresses, r); |
84de38c5 TG |
1118 | if (r < 0) |
1119 | return r; | |
1120 | } else if (r < 0 && r != -ENODATA) | |
1121 | return r; | |
1122 | } | |
1123 | ||
b2a81c0b | 1124 | if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) { |
84de38c5 | 1125 | const char *domainname; |
b85bc551 | 1126 | char **domains = NULL; |
84de38c5 | 1127 | |
b85bc551 | 1128 | OrderedSet *target_domains = (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES) ? search_domains : route_domains; |
84de38c5 TG |
1129 | r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname); |
1130 | if (r >= 0) { | |
b85bc551 DW |
1131 | r = ordered_set_put_strdup(target_domains, domainname); |
1132 | if (r < 0) | |
1133 | return r; | |
1134 | } else if (r != -ENODATA) | |
1135 | return r; | |
b2a81c0b | 1136 | |
b85bc551 DW |
1137 | r = sd_dhcp_lease_get_search_domains(link->dhcp_lease, &domains); |
1138 | if (r >= 0) { | |
1139 | r = ordered_set_put_strdupv(target_domains, domains); | |
84de38c5 TG |
1140 | if (r < 0) |
1141 | return r; | |
1142 | } else if (r != -ENODATA) | |
1143 | return r; | |
1144 | } | |
1145 | } | |
1146 | ||
1147 | operstate_str = link_operstate_to_string(operstate); | |
1148 | assert(operstate_str); | |
1149 | ||
1150 | r = fopen_temporary(m->state_file, &f, &temp_path); | |
1151 | if (r < 0) | |
1152 | return r; | |
1153 | ||
0d536673 | 1154 | (void) __fsetlocking(f, FSETLOCKING_BYCALLER); |
5512a963 | 1155 | (void) fchmod(fileno(f), 0644); |
84de38c5 TG |
1156 | |
1157 | fprintf(f, | |
1158 | "# This is private data. Do not parse.\n" | |
1159 | "OPER_STATE=%s\n", operstate_str); | |
1160 | ||
1161 | print_string_set(f, "DNS=", dns); | |
1162 | print_string_set(f, "NTP=", ntp); | |
3df9bec5 LP |
1163 | print_string_set(f, "DOMAINS=", search_domains); |
1164 | print_string_set(f, "ROUTE_DOMAINS=", route_domains); | |
84de38c5 | 1165 | |
458d8ae3 ZJS |
1166 | r = routing_policy_serialize_rules(m->rules, f); |
1167 | if (r < 0) | |
1168 | goto fail; | |
bce67bbe | 1169 | |
84de38c5 TG |
1170 | r = fflush_and_check(f); |
1171 | if (r < 0) | |
1172 | goto fail; | |
1173 | ||
1174 | if (rename(temp_path, m->state_file) < 0) { | |
1175 | r = -errno; | |
1176 | goto fail; | |
1177 | } | |
1178 | ||
1179 | if (m->operational_state != operstate) { | |
1180 | m->operational_state = operstate; | |
1181 | r = manager_send_changed(m, "OperationalState", NULL); | |
1182 | if (r < 0) | |
1183 | log_error_errno(r, "Could not emit changed OperationalState: %m"); | |
1184 | } | |
1185 | ||
1186 | m->dirty = false; | |
1187 | ||
1188 | return 0; | |
1189 | ||
1190 | fail: | |
1191 | (void) unlink(m->state_file); | |
1192 | (void) unlink(temp_path); | |
1193 | ||
1194 | return log_error_errno(r, "Failed to save network state to %s: %m", m->state_file); | |
1195 | } | |
1196 | ||
1197 | static int manager_dirty_handler(sd_event_source *s, void *userdata) { | |
1198 | Manager *m = userdata; | |
1199 | Link *link; | |
1200 | Iterator i; | |
1201 | int r; | |
1202 | ||
1203 | assert(m); | |
1204 | ||
1205 | if (m->dirty) | |
1206 | manager_save(m); | |
1207 | ||
1208 | SET_FOREACH(link, m->dirty_links, i) { | |
1209 | r = link_save(link); | |
1210 | if (r >= 0) | |
1211 | link_clean(link); | |
1212 | } | |
1213 | ||
1214 | return 1; | |
1215 | } | |
1216 | ||
e133b289 PF |
1217 | Link *manager_dhcp6_prefix_get(Manager *m, struct in6_addr *addr) { |
1218 | assert_return(m, NULL); | |
1219 | assert_return(m->dhcp6_prefixes, NULL); | |
1220 | assert_return(addr, NULL); | |
1221 | ||
1222 | return hashmap_get(m->dhcp6_prefixes, addr); | |
1223 | } | |
1224 | ||
c8ee637e | 1225 | static int dhcp6_route_add_handler(sd_netlink *nl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 1226 | Link *link = userdata; |
9f386c6d | 1227 | int r; |
9f386c6d | 1228 | |
1046bf9b YW |
1229 | assert(link); |
1230 | ||
9f386c6d | 1231 | r = sd_netlink_message_get_errno(m); |
e1d737ef | 1232 | if (r < 0 && r != -EEXIST) |
1046bf9b | 1233 | log_link_debug_errno(link, r, "Received error adding DHCPv6 Prefix Delegation route: %m"); |
9f386c6d PF |
1234 | |
1235 | return 0; | |
1236 | } | |
1237 | ||
e133b289 | 1238 | int manager_dhcp6_prefix_add(Manager *m, struct in6_addr *addr, Link *link) { |
9f386c6d PF |
1239 | int r; |
1240 | Route *route; | |
e1d737ef | 1241 | _cleanup_free_ char *buf = NULL; |
9f386c6d | 1242 | |
e133b289 PF |
1243 | assert_return(m, -EINVAL); |
1244 | assert_return(m->dhcp6_prefixes, -ENODATA); | |
1245 | assert_return(addr, -EINVAL); | |
1246 | ||
9f386c6d PF |
1247 | r = route_add(link, AF_INET6, (union in_addr_union *) addr, 64, |
1248 | 0, 0, 0, &route); | |
1249 | if (r < 0) | |
1250 | return r; | |
1251 | ||
c8ee637e | 1252 | r = route_configure(route, link, dhcp6_route_add_handler); |
9f386c6d PF |
1253 | if (r < 0) |
1254 | return r; | |
1255 | ||
e1d737ef PF |
1256 | (void) in_addr_to_string(AF_INET6, (union in_addr_union *) addr, &buf); |
1257 | log_link_debug(link, "Adding prefix route %s/64", strnull(buf)); | |
1258 | ||
e133b289 PF |
1259 | return hashmap_put(m->dhcp6_prefixes, addr, link); |
1260 | } | |
1261 | ||
c8ee637e | 1262 | static int dhcp6_route_remove_handler(sd_netlink *nl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 1263 | Link *link = userdata; |
9f386c6d | 1264 | int r; |
9f386c6d | 1265 | |
1046bf9b YW |
1266 | assert(link); |
1267 | ||
9f386c6d | 1268 | r = sd_netlink_message_get_errno(m); |
e1d737ef | 1269 | if (r < 0) |
1046bf9b | 1270 | log_link_debug_errno(link, r, "Received error on DHCPv6 Prefix Delegation route removal: %m"); |
9f386c6d | 1271 | |
0ae286e6 | 1272 | return 1; |
9f386c6d PF |
1273 | } |
1274 | ||
fdb20b7c | 1275 | static int manager_dhcp6_prefix_remove(Manager *m, struct in6_addr *addr) { |
e133b289 | 1276 | Link *l; |
9f386c6d PF |
1277 | int r; |
1278 | Route *route; | |
e1d737ef | 1279 | _cleanup_free_ char *buf = NULL; |
e133b289 PF |
1280 | |
1281 | assert_return(m, -EINVAL); | |
1282 | assert_return(m->dhcp6_prefixes, -ENODATA); | |
1283 | assert_return(addr, -EINVAL); | |
1284 | ||
1285 | l = hashmap_remove(m->dhcp6_prefixes, addr); | |
1286 | if (!l) | |
1287 | return -EINVAL; | |
1288 | ||
1289 | (void) sd_radv_remove_prefix(l->radv, addr, 64); | |
9f386c6d PF |
1290 | r = route_get(l, AF_INET6, (union in_addr_union *) addr, 64, |
1291 | 0, 0, 0, &route); | |
e1d737ef PF |
1292 | if (r < 0) |
1293 | return r; | |
1294 | ||
c8ee637e | 1295 | r = route_remove(route, l, dhcp6_route_remove_handler); |
e1d737ef PF |
1296 | if (r < 0) |
1297 | return r; | |
1298 | ||
1299 | (void) in_addr_to_string(AF_INET6, (union in_addr_union *) addr, &buf); | |
1300 | log_link_debug(l, "Removing prefix route %s/64", strnull(buf)); | |
e133b289 PF |
1301 | |
1302 | return 0; | |
1303 | } | |
1304 | ||
1305 | int manager_dhcp6_prefix_remove_all(Manager *m, Link *link) { | |
1306 | Iterator i; | |
1307 | Link *l; | |
1308 | struct in6_addr *addr; | |
1309 | ||
1310 | assert_return(m, -EINVAL); | |
b7d16a91 | 1311 | assert_return(link, -EINVAL); |
e133b289 PF |
1312 | |
1313 | HASHMAP_FOREACH_KEY(l, addr, m->dhcp6_prefixes, i) { | |
1314 | if (l != link) | |
1315 | continue; | |
1316 | ||
9f386c6d | 1317 | (void) manager_dhcp6_prefix_remove(m, addr); |
e133b289 PF |
1318 | } |
1319 | ||
1320 | return 0; | |
1321 | } | |
1322 | ||
1323 | static void dhcp6_prefixes_hash_func(const void *p, struct siphash *state) { | |
1324 | const struct in6_addr *addr = p; | |
1325 | ||
1326 | assert(p); | |
1327 | ||
1328 | siphash24_compress(addr, sizeof(*addr), state); | |
1329 | } | |
1330 | ||
1331 | static int dhcp6_prefixes_compare_func(const void *_a, const void *_b) { | |
1332 | const struct in6_addr *a = _a, *b = _b; | |
1333 | ||
aa18944d | 1334 | return memcmp(a, b, sizeof(*a)); |
e133b289 PF |
1335 | } |
1336 | ||
1337 | static const struct hash_ops dhcp6_prefixes_hash_ops = { | |
1338 | .hash = dhcp6_prefixes_hash_func, | |
1339 | .compare = dhcp6_prefixes_compare_func, | |
1340 | }; | |
1341 | ||
3534a043 | 1342 | int manager_new(Manager **ret) { |
8e766630 | 1343 | _cleanup_(manager_freep) Manager *m = NULL; |
45af44d4 | 1344 | int r; |
f579559b | 1345 | |
5fae368b TG |
1346 | m = new0(Manager, 1); |
1347 | if (!m) | |
1348 | return -ENOMEM; | |
45af44d4 | 1349 | |
5fae368b TG |
1350 | m->state_file = strdup("/run/systemd/netif/state"); |
1351 | if (!m->state_file) | |
1352 | return -ENOMEM; | |
1353 | ||
3534a043 YW |
1354 | r = sd_event_default(&m->event); |
1355 | if (r < 0) | |
1356 | return r; | |
1357 | ||
05e21627 YW |
1358 | (void) sd_event_set_watchdog(m->event, true); |
1359 | (void) sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL); | |
1360 | (void) sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL); | |
5fae368b | 1361 | |
84de38c5 TG |
1362 | r = sd_event_add_post(m->event, NULL, manager_dirty_handler, m); |
1363 | if (r < 0) | |
1364 | return r; | |
1365 | ||
5fae368b | 1366 | r = manager_connect_rtnl(m); |
45af44d4 TG |
1367 | if (r < 0) |
1368 | return r; | |
1369 | ||
05d0c2e3 JT |
1370 | r = manager_connect_genl(m); |
1371 | if (r < 0) | |
1372 | return r; | |
1373 | ||
5fae368b TG |
1374 | r = manager_connect_udev(m); |
1375 | if (r < 0) | |
1376 | return r; | |
45af44d4 | 1377 | |
5fae368b TG |
1378 | m->netdevs = hashmap_new(&string_hash_ops); |
1379 | if (!m->netdevs) | |
1380 | return -ENOMEM; | |
f579559b | 1381 | |
5fae368b | 1382 | LIST_HEAD_INIT(m->networks); |
f579559b | 1383 | |
05d0c2e3 JT |
1384 | r = sd_resolve_default(&m->resolve); |
1385 | if (r < 0) | |
1386 | return r; | |
1387 | ||
1388 | r = sd_resolve_attach_event(m->resolve, m->event, 0); | |
1389 | if (r < 0) | |
1390 | return r; | |
1391 | ||
5fae368b TG |
1392 | r = setup_default_address_pool(m); |
1393 | if (r < 0) | |
1394 | return r; | |
f579559b | 1395 | |
e133b289 PF |
1396 | m->dhcp6_prefixes = hashmap_new(&dhcp6_prefixes_hash_ops); |
1397 | if (!m->dhcp6_prefixes) | |
1398 | return -ENOMEM; | |
1399 | ||
8341a5c3 | 1400 | m->duid.type = DUID_TYPE_EN; |
413708d1 | 1401 | |
458d8ae3 | 1402 | (void) routing_policy_load_rules(m->state_file, &m->rules_saved); |
bce67bbe | 1403 | |
1cc6c93a | 1404 | *ret = TAKE_PTR(m); |
f579559b | 1405 | |
f579559b TG |
1406 | return 0; |
1407 | } | |
1408 | ||
5fae368b | 1409 | void manager_free(Manager *m) { |
c4397d94 | 1410 | AddressPool *pool; |
5fae368b | 1411 | Network *network; |
5fae368b | 1412 | Link *link; |
f579559b | 1413 | |
5fae368b TG |
1414 | if (!m) |
1415 | return; | |
505f8da7 | 1416 | |
5fae368b | 1417 | free(m->state_file); |
505f8da7 | 1418 | |
1046bf9b YW |
1419 | sd_netlink_unref(m->rtnl); |
1420 | sd_netlink_unref(m->genl); | |
c195364d | 1421 | sd_resolve_unref(m->resolve); |
1046bf9b | 1422 | |
bce67bbe SS |
1423 | while ((network = m->networks)) |
1424 | network_free(network); | |
1425 | ||
e133b289 | 1426 | while ((link = hashmap_first(m->dhcp6_prefixes))) |
65dd5e31 | 1427 | manager_dhcp6_prefix_remove_all(m, link); |
e133b289 PF |
1428 | hashmap_free(m->dhcp6_prefixes); |
1429 | ||
65dd5e31 PF |
1430 | while ((link = hashmap_first(m->links))) { |
1431 | if (link->dhcp6_client) | |
1432 | (void) dhcp6_lease_pd_prefix_lost(link->dhcp6_client, | |
1433 | link); | |
1434 | ||
1435 | hashmap_remove(m->links, INT_TO_PTR(link->ifindex)); | |
1436 | ||
5fae368b | 1437 | link_unref(link); |
65dd5e31 | 1438 | } |
f579559b | 1439 | |
c4397d94 YW |
1440 | m->dirty_links = set_free_with_destructor(m->dirty_links, link_unref); |
1441 | m->links = hashmap_free(m->links); | |
1442 | m->links_requesting_uuid = set_free(m->links_requesting_uuid); | |
27dfc982 YW |
1443 | set_free(m->duids_requesting_uuid); |
1444 | ||
dbffab87 TG |
1445 | hashmap_free(m->networks_by_name); |
1446 | ||
c4397d94 | 1447 | m->netdevs = hashmap_free_with_destructor(m->netdevs, netdev_unref); |
5fae368b TG |
1448 | |
1449 | while ((pool = m->address_pools)) | |
1450 | address_pool_free(pool); | |
1451 | ||
f19ee681 YW |
1452 | /* routing_policy_rule_free() access m->rules and m->rules_foreign. |
1453 | * So, it is necessary to set NULL after the sets are freed. */ | |
1454 | m->rules = set_free_with_destructor(m->rules, routing_policy_rule_free); | |
1455 | m->rules_foreign = set_free_with_destructor(m->rules_foreign, routing_policy_rule_free); | |
b921fcb2 | 1456 | set_free_with_destructor(m->rules_saved, routing_policy_rule_free); |
bce67bbe | 1457 | |
2f5b4a77 | 1458 | sd_event_unref(m->event); |
5fae368b | 1459 | |
d2ebf952 | 1460 | sd_device_monitor_unref(m->device_monitor); |
7d20d375 TG |
1461 | |
1462 | sd_bus_unref(m->bus); | |
7d20d375 | 1463 | |
7901cea1 MP |
1464 | free(m->dynamic_timezone); |
1465 | free(m->dynamic_hostname); | |
1466 | ||
5fae368b TG |
1467 | free(m); |
1468 | } | |
1469 | ||
b76d99d9 | 1470 | int manager_start(Manager *m) { |
84de38c5 TG |
1471 | Link *link; |
1472 | Iterator i; | |
1473 | ||
a97dcc12 TG |
1474 | assert(m); |
1475 | ||
84de38c5 TG |
1476 | /* The dirty handler will deal with future serialization, but the first one |
1477 | must be done explicitly. */ | |
1478 | ||
1479 | manager_save(m); | |
1480 | ||
1481 | HASHMAP_FOREACH(link, m->links, i) | |
1482 | link_save(link); | |
1483 | ||
b76d99d9 | 1484 | return 0; |
a97dcc12 TG |
1485 | } |
1486 | ||
5fae368b TG |
1487 | int manager_load_config(Manager *m) { |
1488 | int r; | |
1489 | ||
1490 | /* update timestamp */ | |
1491 | paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, true); | |
1492 | ||
1493 | r = netdev_load(m); | |
f579559b TG |
1494 | if (r < 0) |
1495 | return r; | |
1496 | ||
5fae368b | 1497 | r = network_load(m); |
9021bb9f TG |
1498 | if (r < 0) |
1499 | return r; | |
1500 | ||
f579559b TG |
1501 | return 0; |
1502 | } | |
f882c247 | 1503 | |
5fae368b TG |
1504 | bool manager_should_reload(Manager *m) { |
1505 | return paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, false); | |
1506 | } | |
1507 | ||
1508 | int manager_rtnl_enumerate_links(Manager *m) { | |
4afd3348 | 1509 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
1c4baffc | 1510 | sd_netlink_message *link; |
f882c247 TG |
1511 | int r; |
1512 | ||
5da8149f | 1513 | assert(m); |
5fae368b | 1514 | assert(m->rtnl); |
5da8149f | 1515 | |
5fae368b | 1516 | r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0); |
f882c247 TG |
1517 | if (r < 0) |
1518 | return r; | |
1519 | ||
1c4baffc | 1520 | r = sd_netlink_message_request_dump(req, true); |
dd3efc09 TG |
1521 | if (r < 0) |
1522 | return r; | |
1523 | ||
1c4baffc | 1524 | r = sd_netlink_call(m->rtnl, req, 0, &reply); |
f2236469 TG |
1525 | if (r < 0) |
1526 | return r; | |
1527 | ||
1c4baffc | 1528 | for (link = reply; link; link = sd_netlink_message_next(link)) { |
5fae368b | 1529 | int k; |
2e9f08ea | 1530 | |
6a24f148 TG |
1531 | m->enumerating = true; |
1532 | ||
5fae368b TG |
1533 | k = manager_rtnl_process_link(m->rtnl, link, m); |
1534 | if (k < 0) | |
1535 | r = k; | |
6a24f148 TG |
1536 | |
1537 | m->enumerating = false; | |
5fae368b | 1538 | } |
2e9f08ea | 1539 | |
5fae368b | 1540 | return r; |
f882c247 | 1541 | } |
3bef724f | 1542 | |
5fae368b | 1543 | int manager_rtnl_enumerate_addresses(Manager *m) { |
4afd3348 | 1544 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
1c4baffc | 1545 | sd_netlink_message *addr; |
1346b1f0 TG |
1546 | int r; |
1547 | ||
5fae368b TG |
1548 | assert(m); |
1549 | assert(m->rtnl); | |
bcbca829 | 1550 | |
5fae368b TG |
1551 | r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, 0); |
1552 | if (r < 0) | |
1553 | return r; | |
bcbca829 | 1554 | |
1c4baffc | 1555 | r = sd_netlink_message_request_dump(req, true); |
1346b1f0 TG |
1556 | if (r < 0) |
1557 | return r; | |
1558 | ||
1c4baffc | 1559 | r = sd_netlink_call(m->rtnl, req, 0, &reply); |
5fae368b TG |
1560 | if (r < 0) |
1561 | return r; | |
1562 | ||
1c4baffc | 1563 | for (addr = reply; addr; addr = sd_netlink_message_next(addr)) { |
5fae368b TG |
1564 | int k; |
1565 | ||
6a24f148 TG |
1566 | m->enumerating = true; |
1567 | ||
200a0868 | 1568 | k = manager_rtnl_process_address(m->rtnl, addr, m); |
5fae368b TG |
1569 | if (k < 0) |
1570 | r = k; | |
6a24f148 TG |
1571 | |
1572 | m->enumerating = false; | |
5fae368b TG |
1573 | } |
1574 | ||
1575 | return r; | |
1346b1f0 TG |
1576 | } |
1577 | ||
1c8e710c | 1578 | int manager_rtnl_enumerate_routes(Manager *m) { |
4afd3348 | 1579 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
1c8e710c TG |
1580 | sd_netlink_message *route; |
1581 | int r; | |
1582 | ||
1583 | assert(m); | |
1584 | assert(m->rtnl); | |
1585 | ||
1586 | r = sd_rtnl_message_new_route(m->rtnl, &req, RTM_GETROUTE, 0, 0); | |
1587 | if (r < 0) | |
1588 | return r; | |
1589 | ||
1590 | r = sd_netlink_message_request_dump(req, true); | |
1591 | if (r < 0) | |
1592 | return r; | |
1593 | ||
1594 | r = sd_netlink_call(m->rtnl, req, 0, &reply); | |
1595 | if (r < 0) | |
1596 | return r; | |
1597 | ||
1598 | for (route = reply; route; route = sd_netlink_message_next(route)) { | |
1599 | int k; | |
1600 | ||
1601 | m->enumerating = true; | |
1602 | ||
1603 | k = manager_rtnl_process_route(m->rtnl, route, m); | |
1604 | if (k < 0) | |
1605 | r = k; | |
1606 | ||
1607 | m->enumerating = false; | |
1608 | } | |
1609 | ||
1610 | return r; | |
1611 | } | |
1612 | ||
bce67bbe SS |
1613 | int manager_rtnl_enumerate_rules(Manager *m) { |
1614 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; | |
1615 | sd_netlink_message *rule; | |
1616 | int r; | |
1617 | ||
1618 | assert(m); | |
1619 | assert(m->rtnl); | |
1620 | ||
1621 | r = sd_rtnl_message_new_routing_policy_rule(m->rtnl, &req, RTM_GETRULE, 0); | |
1622 | if (r < 0) | |
1623 | return r; | |
1624 | ||
1625 | r = sd_netlink_message_request_dump(req, true); | |
1626 | if (r < 0) | |
1627 | return r; | |
1628 | ||
1629 | r = sd_netlink_call(m->rtnl, req, 0, &reply); | |
6acbbdd4 SS |
1630 | if (r < 0) { |
1631 | if (r == -EOPNOTSUPP) { | |
1632 | log_debug("FIB Rules are not supported by the kernel. Ignoring."); | |
1633 | return 0; | |
1634 | } | |
1635 | ||
bce67bbe | 1636 | return r; |
6acbbdd4 | 1637 | } |
bce67bbe SS |
1638 | |
1639 | for (rule = reply; rule; rule = sd_netlink_message_next(rule)) { | |
1640 | int k; | |
1641 | ||
1642 | m->enumerating = true; | |
1643 | ||
1644 | k = manager_rtnl_process_rule(m->rtnl, rule, m); | |
1645 | if (k < 0) | |
1646 | r = k; | |
1647 | ||
1648 | m->enumerating = false; | |
1649 | } | |
1650 | ||
1651 | return r; | |
1652 | } | |
1653 | ||
0dd25fb9 | 1654 | int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found) { |
11bf3cce LP |
1655 | AddressPool *p; |
1656 | int r; | |
1657 | ||
1658 | assert(m); | |
1659 | assert(prefixlen > 0); | |
1660 | assert(found); | |
1661 | ||
1662 | LIST_FOREACH(address_pools, p, m->address_pools) { | |
1663 | if (p->family != family) | |
1664 | continue; | |
1665 | ||
1666 | r = address_pool_acquire(p, prefixlen, found); | |
1667 | if (r != 0) | |
1668 | return r; | |
1669 | } | |
1670 | ||
1671 | return 0; | |
1672 | } | |
4f5f911e LP |
1673 | |
1674 | Link* manager_find_uplink(Manager *m, Link *exclude) { | |
1675 | _cleanup_free_ struct local_address *gateways = NULL; | |
1676 | int n, i; | |
1677 | ||
1678 | assert(m); | |
1679 | ||
1680 | /* Looks for a suitable "uplink", via black magic: an | |
1681 | * interface that is up and where the default route with the | |
1682 | * highest priority points to. */ | |
1683 | ||
1684 | n = local_gateways(m->rtnl, 0, AF_UNSPEC, &gateways); | |
1685 | if (n < 0) { | |
1686 | log_warning_errno(n, "Failed to determine list of default gateways: %m"); | |
1687 | return NULL; | |
1688 | } | |
1689 | ||
1690 | for (i = 0; i < n; i++) { | |
1691 | Link *link; | |
1692 | ||
1693 | link = hashmap_get(m->links, INT_TO_PTR(gateways[i].ifindex)); | |
1694 | if (!link) { | |
c2c940bd | 1695 | log_debug("Weird, found a gateway for a link we don't know. Ignoring."); |
4f5f911e LP |
1696 | continue; |
1697 | } | |
1698 | ||
1699 | if (link == exclude) | |
1700 | continue; | |
1701 | ||
1702 | if (link->operstate < LINK_OPERSTATE_ROUTABLE) | |
1703 | continue; | |
1704 | ||
1705 | return link; | |
1706 | } | |
1707 | ||
1708 | return NULL; | |
1709 | } | |
84de38c5 TG |
1710 | |
1711 | void manager_dirty(Manager *manager) { | |
1712 | assert(manager); | |
1713 | ||
1714 | /* the serialized state in /run is no longer up-to-date */ | |
1715 | manager->dirty = true; | |
1716 | } | |
59eb33e0 MP |
1717 | |
1718 | static int set_hostname_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { | |
1719 | Manager *manager = userdata; | |
1720 | const sd_bus_error *e; | |
1721 | ||
1722 | assert(m); | |
1723 | assert(manager); | |
1724 | ||
1725 | e = sd_bus_message_get_error(m); | |
1726 | if (e) | |
1727 | log_warning_errno(sd_bus_error_get_errno(e), "Could not set hostname: %s", e->message); | |
1728 | ||
1729 | return 1; | |
1730 | } | |
1731 | ||
1732 | int manager_set_hostname(Manager *m, const char *hostname) { | |
1733 | int r; | |
1734 | ||
1735 | log_debug("Setting transient hostname: '%s'", strna(hostname)); | |
d7afd945 | 1736 | |
7901cea1 MP |
1737 | if (free_and_strdup(&m->dynamic_hostname, hostname) < 0) |
1738 | return log_oom(); | |
59eb33e0 | 1739 | |
d7afd945 | 1740 | if (!m->bus || sd_bus_is_ready(m->bus) <= 0) { |
e0d95f03 | 1741 | log_debug("Not connected to system bus, setting hostname later."); |
59eb33e0 MP |
1742 | return 0; |
1743 | } | |
1744 | ||
1745 | r = sd_bus_call_method_async( | |
1746 | m->bus, | |
1747 | NULL, | |
1748 | "org.freedesktop.hostname1", | |
1749 | "/org/freedesktop/hostname1", | |
1750 | "org.freedesktop.hostname1", | |
1751 | "SetHostname", | |
1752 | set_hostname_handler, | |
1753 | m, | |
1754 | "sb", | |
1755 | hostname, | |
1756 | false); | |
1757 | ||
1758 | if (r < 0) | |
1759 | return log_error_errno(r, "Could not set transient hostname: %m"); | |
1760 | ||
1761 | return 0; | |
1762 | } | |
1763 | ||
1764 | static int set_timezone_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { | |
1765 | Manager *manager = userdata; | |
1766 | const sd_bus_error *e; | |
1767 | ||
1768 | assert(m); | |
1769 | assert(manager); | |
1770 | ||
1771 | e = sd_bus_message_get_error(m); | |
1772 | if (e) | |
1773 | log_warning_errno(sd_bus_error_get_errno(e), "Could not set timezone: %s", e->message); | |
1774 | ||
1775 | return 1; | |
1776 | } | |
1777 | ||
1778 | int manager_set_timezone(Manager *m, const char *tz) { | |
1779 | int r; | |
1780 | ||
1781 | assert(m); | |
1782 | assert(tz); | |
1783 | ||
1784 | log_debug("Setting system timezone: '%s'", tz); | |
7901cea1 MP |
1785 | if (free_and_strdup(&m->dynamic_timezone, tz) < 0) |
1786 | return log_oom(); | |
59eb33e0 | 1787 | |
d7afd945 | 1788 | if (!m->bus || sd_bus_is_ready(m->bus) <= 0) { |
e0d95f03 | 1789 | log_debug("Not connected to system bus, setting timezone later."); |
59eb33e0 MP |
1790 | return 0; |
1791 | } | |
1792 | ||
1793 | r = sd_bus_call_method_async( | |
1794 | m->bus, | |
1795 | NULL, | |
1796 | "org.freedesktop.timedate1", | |
1797 | "/org/freedesktop/timedate1", | |
1798 | "org.freedesktop.timedate1", | |
1799 | "SetTimezone", | |
1800 | set_timezone_handler, | |
1801 | m, | |
1802 | "sb", | |
1803 | tz, | |
1804 | false); | |
1805 | if (r < 0) | |
1806 | return log_error_errno(r, "Could not set timezone: %m"); | |
1807 | ||
1808 | return 0; | |
1809 | } | |
27dfc982 YW |
1810 | |
1811 | int manager_request_product_uuid(Manager *m, Link *link) { | |
1812 | int r; | |
1813 | ||
1814 | assert(m); | |
1815 | ||
1816 | if (m->has_product_uuid) | |
1817 | return 0; | |
1818 | ||
1819 | log_debug("Requesting product UUID"); | |
1820 | ||
1821 | if (link) { | |
1822 | DUID *duid; | |
1823 | ||
1824 | assert_se(duid = link_get_duid(link)); | |
1825 | ||
1826 | r = set_ensure_allocated(&m->links_requesting_uuid, NULL); | |
1827 | if (r < 0) | |
1828 | return log_oom(); | |
1829 | ||
1830 | r = set_ensure_allocated(&m->duids_requesting_uuid, NULL); | |
1831 | if (r < 0) | |
1832 | return log_oom(); | |
1833 | ||
1834 | r = set_put(m->links_requesting_uuid, link); | |
1835 | if (r < 0) | |
1836 | return log_oom(); | |
1837 | ||
1838 | r = set_put(m->duids_requesting_uuid, duid); | |
1839 | if (r < 0) | |
1840 | return log_oom(); | |
1841 | } | |
1842 | ||
1843 | if (!m->bus || sd_bus_is_ready(m->bus) <= 0) { | |
1844 | log_debug("Not connected to system bus, requesting product UUID later."); | |
1845 | return 0; | |
1846 | } | |
1847 | ||
1848 | r = sd_bus_call_method_async( | |
1849 | m->bus, | |
1850 | NULL, | |
1851 | "org.freedesktop.hostname1", | |
1852 | "/org/freedesktop/hostname1", | |
1853 | "org.freedesktop.hostname1", | |
1854 | "GetProductUUID", | |
1855 | get_product_uuid_handler, | |
1856 | m, | |
1857 | "b", | |
1858 | false); | |
1859 | if (r < 0) | |
1860 | return log_warning_errno(r, "Failed to get product UUID: %m"); | |
1861 | ||
1862 | return 0; | |
1863 | } |