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