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