]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
f579559b | 2 | |
9aa5d8ba | 3 | #include <netinet/in.h> |
091a364c | 4 | #include <sys/socket.h> |
9aa5d8ba | 5 | #include <unistd.h> |
bbf7c048 | 6 | #include <linux/if.h> |
bce67bbe | 7 | #include <linux/fib_rules.h> |
c16c7808 | 8 | #include <linux/nexthop.h> |
3bef724f | 9 | |
fc2f9534 | 10 | #include "sd-daemon.h" |
07630cea | 11 | #include "sd-netlink.h" |
fc2f9534 | 12 | |
b5efdb8a | 13 | #include "alloc-util.h" |
269e4d2d | 14 | #include "bus-polkit.h" |
a97dcc12 | 15 | #include "bus-util.h" |
07630cea | 16 | #include "conf-parser.h" |
a97dcc12 | 17 | #include "def.h" |
a7f95575 | 18 | #include "device-private.h" |
21486d9e | 19 | #include "device-util.h" |
482d1aeb | 20 | #include "dns-domain.h" |
3ffd4af2 | 21 | #include "fd-util.h" |
0d39fa9c | 22 | #include "fileio.h" |
4f5f911e | 23 | #include "local-addresses.h" |
07630cea | 24 | #include "netlink-util.h" |
dc0d4078 | 25 | #include "network-internal.h" |
ca5ad760 | 26 | #include "networkd-dhcp6.h" |
6a1af3d4 | 27 | #include "networkd-link-bus.h" |
79a59fa5 | 28 | #include "networkd-manager-bus.h" |
23f53b99 | 29 | #include "networkd-manager.h" |
ceac2c2b | 30 | #include "networkd-network-bus.h" |
a879e1a4 | 31 | #include "networkd-speed-meter.h" |
00616955 | 32 | #include "ordered-set.h" |
07630cea LP |
33 | #include "path-util.h" |
34 | #include "set.h" | |
ab76be55 | 35 | #include "signal-util.h" |
21486d9e | 36 | #include "strv.h" |
4b600505 | 37 | #include "sysctl-util.h" |
e4de7287 | 38 | #include "tmpfile-util.h" |
299ad32d | 39 | #include "udev-util.h" |
07630cea | 40 | #include "virt.h" |
505f8da7 | 41 | |
48d0248e YW |
42 | /* use 128 MB for receive socket kernel queue. */ |
43 | #define RCVBUF_SIZE (128*1024*1024) | |
be660c37 | 44 | |
5ecb131d YW |
45 | static int log_message_warning_errno(sd_netlink_message *m, int err, const char *msg) { |
46 | const char *err_msg = NULL; | |
47 | ||
48 | (void) sd_netlink_message_read_string(m, NLMSGERR_ATTR_MSG, &err_msg); | |
49 | return log_warning_errno(err, "%s: %s%s%m", msg, strempty(err_msg), err_msg ? " " : ""); | |
50 | } | |
51 | ||
11bf3cce LP |
52 | static int setup_default_address_pool(Manager *m) { |
53 | AddressPool *p; | |
54 | int r; | |
55 | ||
56 | assert(m); | |
57 | ||
58 | /* Add in the well-known private address ranges. */ | |
59 | ||
f2efbeaf | 60 | r = address_pool_new_from_string(m, &p, AF_INET6, "fd00::", 8); |
11bf3cce LP |
61 | if (r < 0) |
62 | return r; | |
63 | ||
b085cd37 | 64 | r = address_pool_new_from_string(m, &p, AF_INET, "10.0.0.0", 8); |
11bf3cce LP |
65 | if (r < 0) |
66 | return r; | |
67 | ||
68 | r = address_pool_new_from_string(m, &p, AF_INET, "172.16.0.0", 12); | |
69 | if (r < 0) | |
70 | return r; | |
71 | ||
b085cd37 | 72 | r = address_pool_new_from_string(m, &p, AF_INET, "192.168.0.0", 16); |
11bf3cce LP |
73 | if (r < 0) |
74 | return r; | |
75 | ||
76 | return 0; | |
77 | } | |
78 | ||
9c0a72f9 TG |
79 | static int manager_reset_all(Manager *m) { |
80 | Link *link; | |
81 | Iterator i; | |
82 | int r; | |
83 | ||
84 | assert(m); | |
85 | ||
86 | HASHMAP_FOREACH(link, m->links, i) { | |
87 | r = link_carrier_reset(link); | |
88 | if (r < 0) | |
2f3cf1f9 | 89 | log_link_warning_errno(link, r, "Could not reset carrier: %m"); |
9c0a72f9 TG |
90 | } |
91 | ||
92 | return 0; | |
93 | } | |
94 | ||
19070062 | 95 | static int match_prepare_for_sleep(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) { |
9c0a72f9 TG |
96 | Manager *m = userdata; |
97 | int b, r; | |
98 | ||
19070062 | 99 | assert(message); |
d7afd945 | 100 | assert(m); |
9c0a72f9 TG |
101 | |
102 | r = sd_bus_message_read(message, "b", &b); | |
103 | if (r < 0) { | |
104 | log_debug_errno(r, "Failed to parse PrepareForSleep signal: %m"); | |
105 | return 0; | |
106 | } | |
107 | ||
108 | if (b) | |
109 | return 0; | |
110 | ||
111 | log_debug("Coming back from suspend, resetting all connections..."); | |
112 | ||
e1694a75 | 113 | (void) manager_reset_all(m); |
9c0a72f9 TG |
114 | |
115 | return 0; | |
116 | } | |
117 | ||
d7afd945 LP |
118 | static int on_connected(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) { |
119 | Manager *m = userdata; | |
9c0a72f9 | 120 | |
d7afd945 | 121 | assert(message); |
9c0a72f9 TG |
122 | assert(m); |
123 | ||
d7afd945 LP |
124 | /* Did we get a timezone or transient hostname from DHCP while D-Bus wasn't up yet? */ |
125 | if (m->dynamic_hostname) | |
126 | (void) manager_set_hostname(m, m->dynamic_hostname); | |
127 | if (m->dynamic_timezone) | |
128 | (void) manager_set_timezone(m, m->dynamic_timezone); | |
27dfc982 YW |
129 | if (m->links_requesting_uuid) |
130 | (void) manager_request_product_uuid(m, NULL); | |
9c0a72f9 | 131 | |
d7afd945 LP |
132 | return 0; |
133 | } | |
9c0a72f9 | 134 | |
d7afd945 LP |
135 | int manager_connect_bus(Manager *m) { |
136 | int r; | |
137 | ||
138 | assert(m); | |
9c0a72f9 | 139 | |
d7afd945 | 140 | if (m->bus) |
9c0a72f9 | 141 | return 0; |
7d6884b6 | 142 | |
621e4509 | 143 | r = bus_open_system_watch_bind_with_description(&m->bus, "bus-api-network"); |
9c0a72f9 | 144 | if (r < 0) |
d7afd945 | 145 | return log_error_errno(r, "Failed to connect to bus: %m"); |
9c0a72f9 | 146 | |
e331e246 TG |
147 | r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/network1", "org.freedesktop.network1.Manager", manager_vtable, m); |
148 | if (r < 0) | |
149 | return log_error_errno(r, "Failed to add manager object vtable: %m"); | |
150 | ||
151 | r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/network1/link", "org.freedesktop.network1.Link", link_vtable, link_object_find, m); | |
152 | if (r < 0) | |
153 | return log_error_errno(r, "Failed to add link object vtable: %m"); | |
154 | ||
155 | r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/network1/link", link_node_enumerator, m); | |
156 | if (r < 0) | |
157 | return log_error_errno(r, "Failed to add link enumerator: %m"); | |
3175fcde TG |
158 | |
159 | r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/network1/network", "org.freedesktop.network1.Network", network_vtable, network_object_find, m); | |
160 | if (r < 0) | |
161 | return log_error_errno(r, "Failed to add network object vtable: %m"); | |
162 | ||
163 | r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/network1/network", network_node_enumerator, m); | |
164 | if (r < 0) | |
165 | return log_error_errno(r, "Failed to add network enumerator: %m"); | |
e331e246 | 166 | |
696fc836 | 167 | r = sd_bus_request_name_async(m->bus, NULL, "org.freedesktop.network1", 0, NULL, NULL); |
e331e246 | 168 | if (r < 0) |
0c0b9306 | 169 | return log_error_errno(r, "Failed to request name: %m"); |
e331e246 TG |
170 | |
171 | r = sd_bus_attach_event(m->bus, m->event, 0); | |
172 | if (r < 0) | |
173 | return log_error_errno(r, "Failed to attach bus to event loop: %m"); | |
174 | ||
d7afd945 LP |
175 | r = sd_bus_match_signal_async( |
176 | m->bus, | |
cad43595 | 177 | NULL, |
d7afd945 LP |
178 | "org.freedesktop.DBus.Local", |
179 | NULL, | |
180 | "org.freedesktop.DBus.Local", | |
181 | "Connected", | |
182 | on_connected, NULL, m); | |
183 | if (r < 0) | |
184 | return log_error_errno(r, "Failed to request match on Connected signal: %m"); | |
185 | ||
186 | r = sd_bus_match_signal_async( | |
187 | m->bus, | |
cad43595 | 188 | NULL, |
d7afd945 LP |
189 | "org.freedesktop.login1", |
190 | "/org/freedesktop/login1", | |
191 | "org.freedesktop.login1.Manager", | |
192 | "PrepareForSleep", | |
193 | match_prepare_for_sleep, NULL, m); | |
194 | if (r < 0) | |
195 | log_warning_errno(r, "Failed to request match for PrepareForSleep, ignoring: %m"); | |
7901cea1 | 196 | |
9c0a72f9 TG |
197 | return 0; |
198 | } | |
199 | ||
d2ebf952 YW |
200 | static int manager_udev_process_link(sd_device_monitor *monitor, sd_device *device, void *userdata) { |
201 | Manager *m = userdata; | |
a7f95575 | 202 | DeviceAction action; |
5fae368b TG |
203 | Link *link = NULL; |
204 | int r, ifindex; | |
5544ee85 | 205 | |
5fae368b TG |
206 | assert(m); |
207 | assert(device); | |
5544ee85 | 208 | |
a7f95575 | 209 | r = device_get_action(device, &action); |
21486d9e | 210 | if (r < 0) { |
a7f95575 | 211 | log_device_debug_errno(device, r, "Failed to get udev action, ignoring device: %m"); |
5fae368b | 212 | return 0; |
21486d9e YW |
213 | } |
214 | ||
a7f95575 YW |
215 | if (!IN_SET(action, DEVICE_ACTION_ADD, DEVICE_ACTION_CHANGE, DEVICE_ACTION_MOVE)) { |
216 | log_device_debug(device, "Ignoring udev %s event for device.", device_action_to_string(action)); | |
21486d9e YW |
217 | return 0; |
218 | } | |
5544ee85 | 219 | |
51517f9e | 220 | r = sd_device_get_ifindex(device, &ifindex); |
7606377e | 221 | if (r < 0) { |
21486d9e | 222 | log_device_debug_errno(device, r, "Ignoring udev ADD event for device without ifindex or with invalid ifindex: %m"); |
5fae368b | 223 | return 0; |
5544ee85 TG |
224 | } |
225 | ||
299ad32d YW |
226 | r = device_is_renaming(device); |
227 | if (r < 0) { | |
a7f95575 YW |
228 | log_device_error_errno(device, r, "Failed to determine the device is renamed or not, ignoring '%s' uevent: %m", |
229 | device_action_to_string(action)); | |
299ad32d YW |
230 | return 0; |
231 | } | |
232 | if (r > 0) { | |
233 | log_device_debug(device, "Interface is under renaming, wait for the interface to be renamed: %m"); | |
234 | return 0; | |
235 | } | |
236 | ||
5fae368b | 237 | r = link_get(m, ifindex, &link); |
d2ebf952 YW |
238 | if (r < 0) { |
239 | if (r != -ENODEV) | |
240 | log_debug_errno(r, "Failed to get link from ifindex %i, ignoring: %m", ifindex); | |
5fae368b | 241 | return 0; |
d2ebf952 | 242 | } |
02b59d57 | 243 | |
d2ebf952 | 244 | (void) link_initialized(link, device); |
e1694a75 | 245 | |
f579559b TG |
246 | return 0; |
247 | } | |
248 | ||
5fae368b TG |
249 | static int manager_connect_udev(Manager *m) { |
250 | int r; | |
f579559b | 251 | |
5fae368b TG |
252 | /* udev does not initialize devices inside containers, |
253 | * so we rely on them being already initialized before | |
254 | * entering the container */ | |
75f86906 | 255 | if (detect_container() > 0) |
5fae368b | 256 | return 0; |
f579559b | 257 | |
d2ebf952 | 258 | r = sd_device_monitor_new(&m->device_monitor); |
02b59d57 | 259 | if (r < 0) |
d2ebf952 | 260 | return log_error_errno(r, "Failed to initialize device monitor: %m"); |
02b59d57 | 261 | |
d2ebf952 YW |
262 | r = sd_device_monitor_filter_add_match_subsystem_devtype(m->device_monitor, "net", NULL); |
263 | if (r < 0) | |
264 | return log_error_errno(r, "Could not add device monitor filter: %m"); | |
505f8da7 | 265 | |
deb2b734 | 266 | r = sd_device_monitor_attach_event(m->device_monitor, m->event); |
5fae368b | 267 | if (r < 0) |
d2ebf952 | 268 | return log_error_errno(r, "Failed to attach event to device monitor: %m"); |
505f8da7 | 269 | |
deb2b734 | 270 | r = sd_device_monitor_start(m->device_monitor, manager_udev_process_link, m); |
505f8da7 | 271 | if (r < 0) |
d2ebf952 | 272 | return log_error_errno(r, "Failed to start device monitor: %m"); |
11a7f229 | 273 | |
505f8da7 TG |
274 | return 0; |
275 | } | |
f579559b | 276 | |
1c8e710c | 277 | int manager_rtnl_process_route(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) { |
f1368755 YW |
278 | _cleanup_(route_freep) Route *tmp = NULL; |
279 | Route *route = NULL; | |
1c8e710c TG |
280 | Manager *m = userdata; |
281 | Link *link = NULL; | |
f1368755 | 282 | uint32_t ifindex; |
1c8e710c | 283 | uint16_t type; |
f1368755 | 284 | unsigned char table; |
1c8e710c TG |
285 | int r; |
286 | ||
287 | assert(rtnl); | |
288 | assert(message); | |
289 | assert(m); | |
290 | ||
291 | if (sd_netlink_message_is_error(message)) { | |
292 | r = sd_netlink_message_get_errno(message); | |
293 | if (r < 0) | |
5ecb131d | 294 | log_message_warning_errno(message, r, "rtnl: failed to receive route message, ignoring"); |
1c8e710c TG |
295 | |
296 | return 0; | |
297 | } | |
298 | ||
299 | r = sd_netlink_message_get_type(message, &type); | |
300 | if (r < 0) { | |
e1694a75 | 301 | log_warning_errno(r, "rtnl: could not get message type, ignoring: %m"); |
1c8e710c | 302 | return 0; |
25a1bffc | 303 | } else if (!IN_SET(type, RTM_NEWROUTE, RTM_DELROUTE)) { |
7f474ed7 | 304 | log_warning("rtnl: received unexpected message type %u when processing route, ignoring.", type); |
1c8e710c TG |
305 | return 0; |
306 | } | |
307 | ||
308 | r = sd_netlink_message_read_u32(message, RTA_OIF, &ifindex); | |
309 | if (r == -ENODATA) { | |
39f26098 | 310 | log_debug("rtnl: received route message without ifindex, ignoring"); |
1c8e710c TG |
311 | return 0; |
312 | } else if (r < 0) { | |
7f474ed7 | 313 | log_warning_errno(r, "rtnl: could not get ifindex from route message, ignoring: %m"); |
1c8e710c TG |
314 | return 0; |
315 | } else if (ifindex <= 0) { | |
7f474ed7 YW |
316 | log_warning("rtnl: received route message with invalid ifindex %d, ignoring.", ifindex); |
317 | return 0; | |
318 | } | |
319 | ||
320 | r = link_get(m, ifindex, &link); | |
321 | if (r < 0 || !link) { | |
322 | /* when enumerating we might be out of sync, but we will | |
323 | * get the route again, so just ignore it */ | |
324 | if (!m->enumerating) | |
39f26098 | 325 | log_warning("rtnl: received route message for link (%d) we do not know about, ignoring", ifindex); |
7f474ed7 | 326 | return 0; |
1c8e710c TG |
327 | } |
328 | ||
f1368755 YW |
329 | r = route_new(&tmp); |
330 | if (r < 0) | |
331 | return log_oom(); | |
332 | ||
333 | r = sd_rtnl_message_route_get_family(message, &tmp->family); | |
509b06ff YW |
334 | if (r < 0) { |
335 | log_link_warning(link, "rtnl: received route message without family, ignoring"); | |
336 | return 0; | |
337 | } else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) { | |
338 | log_link_debug(link, "rtnl: received route message with invalid family '%i', ignoring", tmp->family); | |
1c8e710c TG |
339 | return 0; |
340 | } | |
341 | ||
f1368755 | 342 | r = sd_rtnl_message_route_get_protocol(message, &tmp->protocol); |
1c8e710c | 343 | if (r < 0) { |
509b06ff | 344 | log_warning_errno(r, "rtnl: received route message without route protocol: %m"); |
1c8e710c TG |
345 | return 0; |
346 | } | |
347 | ||
f1368755 | 348 | switch (tmp->family) { |
1c8e710c | 349 | case AF_INET: |
f1368755 | 350 | r = sd_netlink_message_read_in_addr(message, RTA_DST, &tmp->dst.in); |
1c8e710c | 351 | if (r < 0 && r != -ENODATA) { |
7f474ed7 | 352 | log_link_warning_errno(link, r, "rtnl: received route message without valid destination, ignoring: %m"); |
1c8e710c TG |
353 | return 0; |
354 | } | |
355 | ||
f1368755 | 356 | r = sd_netlink_message_read_in_addr(message, RTA_GATEWAY, &tmp->gw.in); |
1c8e710c | 357 | if (r < 0 && r != -ENODATA) { |
7f474ed7 | 358 | log_link_warning_errno(link, r, "rtnl: received route message without valid gateway, ignoring: %m"); |
1c8e710c TG |
359 | return 0; |
360 | } | |
361 | ||
f1368755 | 362 | r = sd_netlink_message_read_in_addr(message, RTA_SRC, &tmp->src.in); |
1c8e710c | 363 | if (r < 0 && r != -ENODATA) { |
7f474ed7 | 364 | log_link_warning_errno(link, r, "rtnl: received route message without valid source, ignoring: %m"); |
1c8e710c TG |
365 | return 0; |
366 | } | |
367 | ||
f1368755 | 368 | r = sd_netlink_message_read_in_addr(message, RTA_PREFSRC, &tmp->prefsrc.in); |
1c8e710c | 369 | if (r < 0 && r != -ENODATA) { |
7f474ed7 | 370 | log_link_warning_errno(link, r, "rtnl: received route message without valid preferred source, ignoring: %m"); |
1c8e710c TG |
371 | return 0; |
372 | } | |
373 | ||
374 | break; | |
375 | ||
376 | case AF_INET6: | |
f1368755 | 377 | r = sd_netlink_message_read_in6_addr(message, RTA_DST, &tmp->dst.in6); |
1c8e710c | 378 | if (r < 0 && r != -ENODATA) { |
7f474ed7 | 379 | log_link_warning_errno(link, r, "rtnl: received route message without valid destination, ignoring: %m"); |
1c8e710c TG |
380 | return 0; |
381 | } | |
382 | ||
f1368755 | 383 | r = sd_netlink_message_read_in6_addr(message, RTA_GATEWAY, &tmp->gw.in6); |
1c8e710c | 384 | if (r < 0 && r != -ENODATA) { |
7f474ed7 | 385 | log_link_warning_errno(link, r, "rtnl: received route message without valid gateway, ignoring: %m"); |
1c8e710c TG |
386 | return 0; |
387 | } | |
388 | ||
f1368755 | 389 | r = sd_netlink_message_read_in6_addr(message, RTA_SRC, &tmp->src.in6); |
1c8e710c | 390 | if (r < 0 && r != -ENODATA) { |
7f474ed7 | 391 | log_link_warning_errno(link, r, "rtnl: received route message without valid source, ignoring: %m"); |
1c8e710c TG |
392 | return 0; |
393 | } | |
394 | ||
f1368755 | 395 | r = sd_netlink_message_read_in6_addr(message, RTA_PREFSRC, &tmp->prefsrc.in6); |
1c8e710c | 396 | if (r < 0 && r != -ENODATA) { |
7f474ed7 | 397 | log_link_warning_errno(link, r, "rtnl: received route message without valid preferred source, ignoring: %m"); |
1c8e710c TG |
398 | return 0; |
399 | } | |
400 | ||
401 | break; | |
402 | ||
403 | default: | |
7f474ed7 | 404 | assert_not_reached("Received route message with unsupported address family"); |
1c8e710c TG |
405 | return 0; |
406 | } | |
407 | ||
f1368755 | 408 | r = sd_rtnl_message_route_get_dst_prefixlen(message, &tmp->dst_prefixlen); |
1c8e710c | 409 | if (r < 0) { |
39f26098 | 410 | log_link_warning_errno(link, r, "rtnl: received route message with invalid destination prefixlen, ignoring: %m"); |
1c8e710c TG |
411 | return 0; |
412 | } | |
413 | ||
f1368755 | 414 | r = sd_rtnl_message_route_get_src_prefixlen(message, &tmp->src_prefixlen); |
1c8e710c | 415 | if (r < 0) { |
39f26098 | 416 | log_link_warning_errno(link, r, "rtnl: received route message with invalid source prefixlen, ignoring: %m"); |
1c8e710c TG |
417 | return 0; |
418 | } | |
419 | ||
f1368755 | 420 | r = sd_rtnl_message_route_get_scope(message, &tmp->scope); |
1c8e710c | 421 | if (r < 0) { |
39f26098 | 422 | log_link_warning_errno(link, r, "rtnl: received route message with invalid scope, ignoring: %m"); |
1c8e710c TG |
423 | return 0; |
424 | } | |
425 | ||
f1368755 | 426 | r = sd_rtnl_message_route_get_tos(message, &tmp->tos); |
1c8e710c | 427 | if (r < 0) { |
39f26098 | 428 | log_link_warning_errno(link, r, "rtnl: received route message with invalid tos, ignoring: %m"); |
1c8e710c TG |
429 | return 0; |
430 | } | |
431 | ||
f1368755 | 432 | r = sd_rtnl_message_route_get_type(message, &tmp->type); |
983226f3 | 433 | if (r < 0) { |
39f26098 | 434 | log_link_warning_errno(link, r, "rtnl: received route message with invalid type, ignoring: %m"); |
983226f3 SS |
435 | return 0; |
436 | } | |
437 | ||
1c8e710c TG |
438 | r = sd_rtnl_message_route_get_table(message, &table); |
439 | if (r < 0) { | |
39f26098 | 440 | log_link_warning_errno(link, r, "rtnl: received route message with invalid table, ignoring: %m"); |
1c8e710c TG |
441 | return 0; |
442 | } | |
f1368755 | 443 | tmp->table = table; |
1c8e710c | 444 | |
f1368755 | 445 | r = sd_netlink_message_read_u32(message, RTA_PRIORITY, &tmp->priority); |
1c8e710c | 446 | if (r < 0 && r != -ENODATA) { |
39f26098 | 447 | log_link_warning_errno(link, r, "rtnl: received route message with invalid priority, ignoring: %m"); |
1c8e710c TG |
448 | return 0; |
449 | } | |
450 | ||
fa3e401a YW |
451 | r = sd_netlink_message_enter_container(message, RTA_METRICS); |
452 | if (r < 0 && r != -ENODATA) { | |
453 | log_link_error_errno(link, r, "rtnl: Could not enter RTA_METRICS container: %m"); | |
454 | return 0; | |
455 | } | |
456 | if (r >= 0) { | |
457 | r = sd_netlink_message_read_u32(message, RTAX_INITCWND, &tmp->initcwnd); | |
458 | if (r < 0 && r != -ENODATA) { | |
459 | log_link_warning_errno(link, r, "rtnl: received route message with invalid initcwnd, ignoring: %m"); | |
460 | return 0; | |
461 | } | |
462 | ||
463 | r = sd_netlink_message_read_u32(message, RTAX_INITRWND, &tmp->initrwnd); | |
464 | if (r < 0 && r != -ENODATA) { | |
465 | log_link_warning_errno(link, r, "rtnl: received route message with invalid initrwnd, ignoring: %m"); | |
466 | return 0; | |
467 | } | |
468 | ||
469 | r = sd_netlink_message_exit_container(message); | |
470 | if (r < 0) { | |
471 | log_link_error_errno(link, r, "rtnl: Could not exit from RTA_METRICS container: %m"); | |
472 | return 0; | |
473 | } | |
474 | } | |
475 | ||
f1368755 | 476 | (void) route_get(link, tmp, &route); |
1c8e710c | 477 | |
156ed65e YW |
478 | if (DEBUG_LOGGING) { |
479 | _cleanup_free_ char *buf_dst = NULL, *buf_dst_prefixlen = NULL, | |
480 | *buf_src = NULL, *buf_gw = NULL, *buf_prefsrc = NULL; | |
d3e291fd YW |
481 | char buf_scope[ROUTE_SCOPE_STR_MAX], buf_table[ROUTE_TABLE_STR_MAX], |
482 | buf_protocol[ROUTE_PROTOCOL_STR_MAX]; | |
156ed65e | 483 | |
f1368755 YW |
484 | if (!in_addr_is_null(tmp->family, &tmp->dst)) { |
485 | (void) in_addr_to_string(tmp->family, &tmp->dst, &buf_dst); | |
486 | (void) asprintf(&buf_dst_prefixlen, "/%u", tmp->dst_prefixlen); | |
156ed65e | 487 | } |
f1368755 YW |
488 | if (!in_addr_is_null(tmp->family, &tmp->src)) |
489 | (void) in_addr_to_string(tmp->family, &tmp->src, &buf_src); | |
490 | if (!in_addr_is_null(tmp->family, &tmp->gw)) | |
491 | (void) in_addr_to_string(tmp->family, &tmp->gw, &buf_gw); | |
492 | if (!in_addr_is_null(tmp->family, &tmp->prefsrc)) | |
493 | (void) in_addr_to_string(tmp->family, &tmp->prefsrc, &buf_prefsrc); | |
156ed65e YW |
494 | |
495 | log_link_debug(link, | |
d3e291fd | 496 | "%s route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s", |
f1368755 | 497 | type == RTM_DELROUTE ? "Forgetting" : route ? "Received remembered" : "Remembering", |
156ed65e | 498 | strna(buf_dst), strempty(buf_dst_prefixlen), |
b297e0a7 | 499 | strna(buf_src), strna(buf_gw), strna(buf_prefsrc), |
f1368755 YW |
500 | format_route_scope(tmp->scope, buf_scope, sizeof buf_scope), |
501 | format_route_table(tmp->table, buf_table, sizeof buf_table), | |
502 | format_route_protocol(tmp->protocol, buf_protocol, sizeof buf_protocol), | |
503 | strna(route_type_to_string(tmp->type))); | |
156ed65e YW |
504 | } |
505 | ||
1c8e710c TG |
506 | switch (type) { |
507 | case RTM_NEWROUTE: | |
508 | if (!route) { | |
509 | /* A route appeared that we did not request */ | |
f1368755 | 510 | r = route_add_foreign(link, tmp, &route); |
e1694a75 | 511 | if (r < 0) { |
7f474ed7 | 512 | log_link_warning_errno(link, r, "Failed to remember foreign route, ignoring: %m"); |
1c8e710c | 513 | return 0; |
e1694a75 | 514 | } |
1c8e710c TG |
515 | } |
516 | ||
1c8e710c TG |
517 | break; |
518 | ||
519 | case RTM_DELROUTE: | |
b9642f41 | 520 | route_free(route); |
1c8e710c | 521 | break; |
b9642f41 | 522 | |
1c8e710c | 523 | default: |
7f474ed7 | 524 | assert_not_reached("Received route message with invalid RTNL message type"); |
1c8e710c TG |
525 | } |
526 | ||
527 | return 1; | |
528 | } | |
529 | ||
d1bdafd2 WKI |
530 | static int manager_rtnl_process_neighbor_lladdr(sd_netlink_message *message, union lladdr_union *lladdr, size_t *size, char **str) { |
531 | int r; | |
532 | ||
533 | assert(message); | |
534 | assert(lladdr); | |
535 | assert(size); | |
536 | assert(str); | |
537 | ||
538 | *str = NULL; | |
539 | ||
540 | r = sd_netlink_message_read(message, NDA_LLADDR, sizeof(lladdr->ip.in6), &lladdr->ip.in6); | |
541 | if (r >= 0) { | |
542 | *size = sizeof(lladdr->ip.in6); | |
543 | if (in_addr_to_string(AF_INET6, &lladdr->ip, str) < 0) | |
544 | log_warning_errno(r, "Could not print lower address: %m"); | |
545 | return r; | |
546 | } | |
547 | ||
548 | r = sd_netlink_message_read(message, NDA_LLADDR, sizeof(lladdr->mac), &lladdr->mac); | |
549 | if (r >= 0) { | |
550 | *size = sizeof(lladdr->mac); | |
551 | *str = new(char, ETHER_ADDR_TO_STRING_MAX); | |
552 | if (!*str) { | |
553 | log_oom(); | |
554 | return r; | |
555 | } | |
556 | ether_addr_to_string(&lladdr->mac, *str); | |
557 | return r; | |
558 | } | |
559 | ||
560 | r = sd_netlink_message_read(message, NDA_LLADDR, sizeof(lladdr->ip.in), &lladdr->ip.in); | |
561 | if (r >= 0) { | |
562 | *size = sizeof(lladdr->ip.in); | |
563 | if (in_addr_to_string(AF_INET, &lladdr->ip, str) < 0) | |
564 | log_warning_errno(r, "Could not print lower address: %m"); | |
565 | return r; | |
566 | } | |
567 | ||
568 | return r; | |
569 | } | |
570 | ||
571 | int manager_rtnl_process_neighbor(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) { | |
572 | Manager *m = userdata; | |
573 | Link *link = NULL; | |
574 | Neighbor *neighbor = NULL; | |
575 | int ifindex, family, r; | |
576 | uint16_t type, state; | |
577 | union in_addr_union in_addr = IN_ADDR_NULL; | |
578 | _cleanup_free_ char *addr_str = NULL; | |
579 | union lladdr_union lladdr; | |
580 | size_t lladdr_size = 0; | |
581 | _cleanup_free_ char *lladdr_str = NULL; | |
582 | ||
583 | assert(rtnl); | |
584 | assert(message); | |
585 | assert(m); | |
586 | ||
587 | if (sd_netlink_message_is_error(message)) { | |
588 | r = sd_netlink_message_get_errno(message); | |
589 | if (r < 0) | |
5ecb131d | 590 | log_message_warning_errno(message, r, "rtnl: failed to receive neighbor message, ignoring"); |
d1bdafd2 WKI |
591 | |
592 | return 0; | |
593 | } | |
594 | ||
595 | r = sd_netlink_message_get_type(message, &type); | |
596 | if (r < 0) { | |
597 | log_warning_errno(r, "rtnl: could not get message type, ignoring: %m"); | |
598 | return 0; | |
599 | } else if (!IN_SET(type, RTM_NEWNEIGH, RTM_DELNEIGH)) { | |
600 | log_warning("rtnl: received unexpected message type %u when processing neighbor, ignoring.", type); | |
601 | return 0; | |
602 | } | |
603 | ||
604 | r = sd_rtnl_message_neigh_get_state(message, &state); | |
605 | if (r < 0) { | |
606 | log_link_warning_errno(link, r, "rtnl: received neighbor message with invalid state, ignoring: %m"); | |
607 | return 0; | |
608 | } else if (!FLAGS_SET(state, NUD_PERMANENT)) { | |
609 | log_debug("rtnl: received non-static neighbor, ignoring."); | |
610 | return 0; | |
611 | } | |
612 | ||
613 | r = sd_rtnl_message_neigh_get_ifindex(message, &ifindex); | |
614 | if (r < 0) { | |
615 | log_warning_errno(r, "rtnl: could not get ifindex from message, ignoring: %m"); | |
616 | return 0; | |
617 | } else if (ifindex <= 0) { | |
618 | log_warning("rtnl: received neighbor message with invalid ifindex %d, ignoring.", ifindex); | |
619 | return 0; | |
620 | } | |
621 | ||
622 | r = link_get(m, ifindex, &link); | |
623 | if (r < 0 || !link) { | |
624 | /* when enumerating we might be out of sync, but we will get the neighbor again, so just | |
625 | * ignore it */ | |
626 | if (!m->enumerating) | |
627 | log_warning("rtnl: received neighbor for link '%d' we don't know about, ignoring.", ifindex); | |
628 | return 0; | |
629 | } | |
630 | ||
631 | r = sd_rtnl_message_neigh_get_family(message, &family); | |
509b06ff | 632 | if (r < 0) { |
7c6d95ea | 633 | log_link_warning(link, "rtnl: received neighbor message without family, ignoring."); |
509b06ff YW |
634 | return 0; |
635 | } else if (!IN_SET(family, AF_INET, AF_INET6)) { | |
636 | log_link_debug(link, "rtnl: received neighbor message with invalid family '%i', ignoring.", family); | |
d1bdafd2 WKI |
637 | return 0; |
638 | } | |
639 | ||
640 | switch (family) { | |
641 | case AF_INET: | |
642 | r = sd_netlink_message_read_in_addr(message, NDA_DST, &in_addr.in); | |
643 | if (r < 0) { | |
644 | log_link_warning_errno(link, r, "rtnl: received neighbor message without valid address, ignoring: %m"); | |
645 | return 0; | |
646 | } | |
647 | ||
648 | break; | |
649 | ||
650 | case AF_INET6: | |
651 | r = sd_netlink_message_read_in6_addr(message, NDA_DST, &in_addr.in6); | |
652 | if (r < 0) { | |
653 | log_link_warning_errno(link, r, "rtnl: received neighbor message without valid address, ignoring: %m"); | |
654 | return 0; | |
655 | } | |
656 | ||
657 | break; | |
658 | ||
659 | default: | |
660 | assert_not_reached("Received unsupported address family"); | |
661 | } | |
662 | ||
663 | if (in_addr_to_string(family, &in_addr, &addr_str) < 0) | |
664 | log_link_warning_errno(link, r, "Could not print address: %m"); | |
665 | ||
666 | r = manager_rtnl_process_neighbor_lladdr(message, &lladdr, &lladdr_size, &lladdr_str); | |
667 | if (r < 0) { | |
668 | log_link_warning_errno(link, r, "rtnl: received neighbor message with invalid lladdr, ignoring: %m"); | |
669 | return 0; | |
670 | } | |
671 | ||
672 | (void) neighbor_get(link, family, &in_addr, &lladdr, lladdr_size, &neighbor); | |
673 | ||
674 | switch (type) { | |
675 | case RTM_NEWNEIGH: | |
676 | if (neighbor) | |
677 | log_link_debug(link, "Remembering neighbor: %s->%s", | |
678 | strnull(addr_str), strnull(lladdr_str)); | |
679 | else { | |
680 | /* A neighbor appeared that we did not request */ | |
681 | r = neighbor_add_foreign(link, family, &in_addr, &lladdr, lladdr_size, &neighbor); | |
682 | if (r < 0) { | |
683 | log_link_warning_errno(link, r, "Failed to remember foreign neighbor %s->%s, ignoring: %m", | |
684 | strnull(addr_str), strnull(lladdr_str)); | |
685 | return 0; | |
686 | } else | |
687 | log_link_debug(link, "Remembering foreign neighbor: %s->%s", | |
688 | strnull(addr_str), strnull(lladdr_str)); | |
689 | } | |
690 | ||
691 | break; | |
692 | ||
693 | case RTM_DELNEIGH: | |
694 | if (neighbor) { | |
695 | log_link_debug(link, "Forgetting neighbor: %s->%s", | |
696 | strnull(addr_str), strnull(lladdr_str)); | |
697 | (void) neighbor_free(neighbor); | |
698 | } else | |
d6246fd4 YW |
699 | log_link_debug(link, "Kernel removed a neighbor we don't remember: %s->%s, ignoring.", |
700 | strnull(addr_str), strnull(lladdr_str)); | |
d1bdafd2 WKI |
701 | |
702 | break; | |
703 | ||
704 | default: | |
705 | assert_not_reached("Received invalid RTNL message type"); | |
706 | } | |
707 | ||
708 | return 1; | |
709 | } | |
710 | ||
200a0868 | 711 | int manager_rtnl_process_address(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) { |
57e44707 | 712 | _cleanup_free_ char *buf = NULL; |
200a0868 TG |
713 | Manager *m = userdata; |
714 | Link *link = NULL; | |
715 | uint16_t type; | |
57e44707 | 716 | unsigned char flags, prefixlen, scope; |
67a46833 | 717 | union in_addr_union in_addr = IN_ADDR_NULL; |
054f0db4 TG |
718 | struct ifa_cacheinfo cinfo; |
719 | Address *address = NULL; | |
57e44707 | 720 | char valid_buf[FORMAT_TIMESPAN_MAX]; |
200a0868 | 721 | const char *valid_str = NULL; |
57e44707 | 722 | int ifindex, family, r; |
200a0868 TG |
723 | |
724 | assert(rtnl); | |
725 | assert(message); | |
726 | assert(m); | |
727 | ||
728 | if (sd_netlink_message_is_error(message)) { | |
729 | r = sd_netlink_message_get_errno(message); | |
730 | if (r < 0) | |
5ecb131d | 731 | log_message_warning_errno(message, r, "rtnl: failed to receive address message, ignoring"); |
200a0868 TG |
732 | |
733 | return 0; | |
734 | } | |
735 | ||
736 | r = sd_netlink_message_get_type(message, &type); | |
737 | if (r < 0) { | |
e1694a75 | 738 | log_warning_errno(r, "rtnl: could not get message type, ignoring: %m"); |
200a0868 | 739 | return 0; |
25a1bffc | 740 | } else if (!IN_SET(type, RTM_NEWADDR, RTM_DELADDR)) { |
7f474ed7 | 741 | log_warning("rtnl: received unexpected message type %u when processing address, ignoring.", type); |
200a0868 TG |
742 | return 0; |
743 | } | |
744 | ||
745 | r = sd_rtnl_message_addr_get_ifindex(message, &ifindex); | |
746 | if (r < 0) { | |
44e891bb | 747 | log_warning_errno(r, "rtnl: could not get ifindex from message, ignoring: %m"); |
200a0868 TG |
748 | return 0; |
749 | } else if (ifindex <= 0) { | |
7f474ed7 | 750 | log_warning("rtnl: received address message with invalid ifindex %d, ignoring.", ifindex); |
200a0868 | 751 | return 0; |
44e891bb ZJS |
752 | } |
753 | ||
754 | r = link_get(m, ifindex, &link); | |
755 | if (r < 0 || !link) { | |
756 | /* when enumerating we might be out of sync, but we will get the address again, so just | |
757 | * ignore it */ | |
758 | if (!m->enumerating) | |
7f474ed7 | 759 | log_warning("rtnl: received address for link '%d' we don't know about, ignoring.", ifindex); |
44e891bb | 760 | return 0; |
200a0868 TG |
761 | } |
762 | ||
054f0db4 | 763 | r = sd_rtnl_message_addr_get_family(message, &family); |
509b06ff YW |
764 | if (r < 0) { |
765 | log_link_warning(link, "rtnl: received address message without family, ignoring."); | |
766 | return 0; | |
767 | } else if (!IN_SET(family, AF_INET, AF_INET6)) { | |
768 | log_link_debug(link, "rtnl: received address message with invalid family '%i', ignoring.", family); | |
200a0868 TG |
769 | return 0; |
770 | } | |
771 | ||
054f0db4 | 772 | r = sd_rtnl_message_addr_get_prefixlen(message, &prefixlen); |
200a0868 | 773 | if (r < 0) { |
44e891bb | 774 | log_link_warning_errno(link, r, "rtnl: received address message with invalid prefixlen, ignoring: %m"); |
200a0868 TG |
775 | return 0; |
776 | } | |
777 | ||
054f0db4 | 778 | r = sd_rtnl_message_addr_get_scope(message, &scope); |
200a0868 | 779 | if (r < 0) { |
44e891bb | 780 | log_link_warning_errno(link, r, "rtnl: received address message with invalid scope, ignoring: %m"); |
200a0868 TG |
781 | return 0; |
782 | } | |
783 | ||
784 | r = sd_rtnl_message_addr_get_flags(message, &flags); | |
785 | if (r < 0) { | |
44e891bb | 786 | log_link_warning_errno(link, r, "rtnl: received address message with invalid flags, ignoring: %m"); |
200a0868 TG |
787 | return 0; |
788 | } | |
200a0868 | 789 | |
054f0db4 | 790 | switch (family) { |
200a0868 | 791 | case AF_INET: |
054f0db4 | 792 | r = sd_netlink_message_read_in_addr(message, IFA_LOCAL, &in_addr.in); |
200a0868 | 793 | if (r < 0) { |
44e891bb | 794 | log_link_warning_errno(link, r, "rtnl: received address message without valid address, ignoring: %m"); |
200a0868 TG |
795 | return 0; |
796 | } | |
797 | ||
798 | break; | |
799 | ||
800 | case AF_INET6: | |
054f0db4 | 801 | r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &in_addr.in6); |
200a0868 | 802 | if (r < 0) { |
44e891bb | 803 | log_link_warning_errno(link, r, "rtnl: received address message without valid address, ignoring: %m"); |
200a0868 TG |
804 | return 0; |
805 | } | |
806 | ||
807 | break; | |
808 | ||
809 | default: | |
e1694a75 | 810 | assert_not_reached("Received unsupported address family"); |
200a0868 TG |
811 | } |
812 | ||
57e44707 | 813 | r = in_addr_to_string(family, &in_addr, &buf); |
44e891bb ZJS |
814 | if (r < 0) |
815 | log_link_warning_errno(link, r, "Could not print address: %m"); | |
200a0868 | 816 | |
054f0db4 | 817 | r = sd_netlink_message_read_cache_info(message, IFA_CACHEINFO, &cinfo); |
e1694a75 YW |
818 | if (r < 0 && r != -ENODATA) { |
819 | log_link_warning_errno(link, r, "rtnl: cannot get IFA_CACHEINFO attribute, ignoring: %m"); | |
820 | return 0; | |
57e44707 YW |
821 | } else if (r >= 0 && cinfo.ifa_valid != CACHE_INFO_INFINITY_LIFE_TIME) |
822 | valid_str = format_timespan(valid_buf, FORMAT_TIMESPAN_MAX, | |
823 | cinfo.ifa_valid * USEC_PER_SEC, | |
824 | USEC_PER_SEC); | |
200a0868 | 825 | |
e1694a75 | 826 | (void) address_get(link, family, &in_addr, prefixlen, &address); |
200a0868 TG |
827 | |
828 | switch (type) { | |
829 | case RTM_NEWADDR: | |
36c32f61 | 830 | if (address) |
44e891bb ZJS |
831 | log_link_debug(link, "Remembering updated address: %s/%u (valid %s%s)", |
832 | strnull(buf), prefixlen, | |
87e4e28d | 833 | valid_str ? "for " : "forever", strempty(valid_str)); |
36c32f61 | 834 | else { |
adda1ed9 TG |
835 | /* An address appeared that we did not request */ |
836 | r = address_add_foreign(link, family, &in_addr, prefixlen, &address); | |
cf1d700d | 837 | if (r < 0) { |
44e891bb ZJS |
838 | log_link_warning_errno(link, r, "Failed to remember foreign address %s/%u, ignoring: %m", |
839 | strnull(buf), prefixlen); | |
cf1d700d TG |
840 | return 0; |
841 | } else | |
44e891bb ZJS |
842 | log_link_debug(link, "Remembering foreign address: %s/%u (valid %s%s)", |
843 | strnull(buf), prefixlen, | |
87e4e28d | 844 | valid_str ? "for " : "forever", strempty(valid_str)); |
200a0868 TG |
845 | } |
846 | ||
44e891bb ZJS |
847 | /* address_update() logs internally, so we don't need to. */ |
848 | (void) address_update(address, flags, scope, &cinfo); | |
36c32f61 | 849 | |
200a0868 TG |
850 | break; |
851 | ||
852 | case RTM_DELADDR: | |
054f0db4 | 853 | if (address) { |
44e891bb ZJS |
854 | log_link_debug(link, "Forgetting address: %s/%u (valid %s%s)", |
855 | strnull(buf), prefixlen, | |
87e4e28d | 856 | valid_str ? "for " : "forever", strempty(valid_str)); |
e1694a75 | 857 | (void) address_drop(address); |
200a0868 | 858 | } else |
d6246fd4 YW |
859 | log_link_debug(link, "Kernel removed an address we don't remember: %s/%u (valid %s%s), ignoring.", |
860 | strnull(buf), prefixlen, | |
861 | valid_str ? "for " : "forever", strempty(valid_str)); | |
200a0868 TG |
862 | |
863 | break; | |
44e891bb | 864 | |
200a0868 TG |
865 | default: |
866 | assert_not_reached("Received invalid RTNL message type"); | |
867 | } | |
868 | ||
869 | return 1; | |
870 | } | |
871 | ||
1c4baffc | 872 | static int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) { |
505f8da7 TG |
873 | Manager *m = userdata; |
874 | Link *link = NULL; | |
4d473d5d | 875 | NetDev *netdev = NULL; |
f2236469 | 876 | uint16_t type; |
ca4e095a | 877 | const char *name; |
505f8da7 | 878 | int r, ifindex; |
f579559b | 879 | |
505f8da7 TG |
880 | assert(rtnl); |
881 | assert(message); | |
f579559b TG |
882 | assert(m); |
883 | ||
1c4baffc TG |
884 | if (sd_netlink_message_is_error(message)) { |
885 | r = sd_netlink_message_get_errno(message); | |
45af44d4 | 886 | if (r < 0) |
5ecb131d | 887 | log_message_warning_errno(message, r, "rtnl: Could not receive link message, ignoring"); |
45af44d4 TG |
888 | |
889 | return 0; | |
890 | } | |
891 | ||
1c4baffc | 892 | r = sd_netlink_message_get_type(message, &type); |
f2236469 | 893 | if (r < 0) { |
e1694a75 | 894 | log_warning_errno(r, "rtnl: Could not get message type, ignoring: %m"); |
f2236469 | 895 | return 0; |
25a1bffc | 896 | } else if (!IN_SET(type, RTM_NEWLINK, RTM_DELLINK)) { |
7f474ed7 | 897 | log_warning("rtnl: Received unexpected message type %u when processing link, ignoring.", type); |
cdfee943 | 898 | return 0; |
f2236469 TG |
899 | } |
900 | ||
505f8da7 | 901 | r = sd_rtnl_message_link_get_ifindex(message, &ifindex); |
45af44d4 | 902 | if (r < 0) { |
7f474ed7 | 903 | log_warning_errno(r, "rtnl: Could not get ifindex from link message, ignoring: %m"); |
45af44d4 TG |
904 | return 0; |
905 | } else if (ifindex <= 0) { | |
7f474ed7 | 906 | log_warning("rtnl: received link message with invalid ifindex %d, ignoring.", ifindex); |
505f8da7 | 907 | return 0; |
a0db8e46 | 908 | } |
f579559b | 909 | |
1c4baffc | 910 | r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name); |
45af44d4 | 911 | if (r < 0) { |
e1694a75 | 912 | log_warning_errno(r, "rtnl: Received link message without ifname, ignoring: %m"); |
4d473d5d | 913 | return 0; |
a0db8e46 ZJS |
914 | } |
915 | ||
916 | (void) link_get(m, ifindex, &link); | |
917 | (void) netdev_get(m, name, &netdev); | |
4d473d5d TG |
918 | |
919 | switch (type) { | |
920 | case RTM_NEWLINK: | |
921 | if (!link) { | |
922 | /* link is new, so add it */ | |
923 | r = link_add(m, message, &link); | |
924 | if (r < 0) { | |
7f474ed7 | 925 | log_warning_errno(r, "Could not process new link message, ignoring: %m"); |
4d473d5d TG |
926 | return 0; |
927 | } | |
928 | } | |
929 | ||
930 | if (netdev) { | |
931 | /* netdev exists, so make sure the ifindex matches */ | |
505f8da7 TG |
932 | r = netdev_set_ifindex(netdev, message); |
933 | if (r < 0) { | |
7f474ed7 | 934 | log_warning_errno(r, "Could not process new link message for netdev, ignoring: %m"); |
505f8da7 TG |
935 | return 0; |
936 | } | |
937 | } | |
e1202047 | 938 | |
f2236469 | 939 | r = link_update(link, message); |
e1694a75 | 940 | if (r < 0) { |
7f474ed7 | 941 | log_warning_errno(r, "Could not process link message, ignoring: %m"); |
f2236469 | 942 | return 0; |
e1694a75 | 943 | } |
4d473d5d TG |
944 | |
945 | break; | |
946 | ||
947 | case RTM_DELLINK: | |
948 | link_drop(link); | |
949 | netdev_drop(netdev); | |
950 | ||
951 | break; | |
952 | ||
953 | default: | |
7f474ed7 | 954 | assert_not_reached("Received link message with invalid RTNL message type."); |
f2236469 | 955 | } |
505f8da7 TG |
956 | |
957 | return 1; | |
958 | } | |
959 | ||
bce67bbe | 960 | int manager_rtnl_process_rule(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) { |
b80a511b | 961 | _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *tmp = NULL; |
15201512 | 962 | _cleanup_free_ char *from = NULL, *to = NULL; |
bce67bbe | 963 | RoutingPolicyRule *rule = NULL; |
eeab051b | 964 | const char *iif = NULL, *oif = NULL; |
bce67bbe | 965 | Manager *m = userdata; |
b80a511b | 966 | unsigned flags; |
bce67bbe | 967 | uint16_t type; |
bce67bbe SS |
968 | int r; |
969 | ||
970 | assert(rtnl); | |
971 | assert(message); | |
972 | assert(m); | |
973 | ||
974 | if (sd_netlink_message_is_error(message)) { | |
975 | r = sd_netlink_message_get_errno(message); | |
976 | if (r < 0) | |
5ecb131d | 977 | log_message_warning_errno(message, r, "rtnl: failed to receive rule message, ignoring"); |
bce67bbe SS |
978 | |
979 | return 0; | |
980 | } | |
981 | ||
982 | r = sd_netlink_message_get_type(message, &type); | |
983 | if (r < 0) { | |
e1694a75 | 984 | log_warning_errno(r, "rtnl: could not get message type, ignoring: %m"); |
bce67bbe SS |
985 | return 0; |
986 | } else if (!IN_SET(type, RTM_NEWRULE, RTM_DELRULE)) { | |
7f474ed7 | 987 | log_warning("rtnl: received unexpected message type %u when processing rule, ignoring.", type); |
bce67bbe SS |
988 | return 0; |
989 | } | |
990 | ||
b80a511b YW |
991 | r = routing_policy_rule_new(&tmp); |
992 | if (r < 0) { | |
993 | log_oom(); | |
994 | return 0; | |
995 | } | |
996 | ||
997 | r = sd_rtnl_message_get_family(message, &tmp->family); | |
615ded62 | 998 | if (r < 0) { |
e1694a75 | 999 | log_warning_errno(r, "rtnl: could not get rule family, ignoring: %m"); |
615ded62 | 1000 | return 0; |
b80a511b | 1001 | } else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) { |
7f474ed7 | 1002 | log_debug("rtnl: received rule message with invalid family %d, ignoring.", tmp->family); |
bce67bbe SS |
1003 | return 0; |
1004 | } | |
1005 | ||
b80a511b | 1006 | switch (tmp->family) { |
bce67bbe | 1007 | case AF_INET: |
b80a511b | 1008 | r = sd_netlink_message_read_in_addr(message, FRA_SRC, &tmp->from.in); |
e1694a75 YW |
1009 | if (r < 0 && r != -ENODATA) { |
1010 | log_warning_errno(r, "rtnl: could not get FRA_SRC attribute, ignoring: %m"); | |
1011 | return 0; | |
1012 | } else if (r >= 0) { | |
b80a511b | 1013 | r = sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(message, &tmp->from_prefixlen); |
e1694a75 | 1014 | if (r < 0) { |
7f474ed7 | 1015 | log_warning_errno(r, "rtnl: received rule message without valid source prefix length, ignoring: %m"); |
e1694a75 YW |
1016 | return 0; |
1017 | } | |
bce67bbe SS |
1018 | } |
1019 | ||
b80a511b | 1020 | r = sd_netlink_message_read_in_addr(message, FRA_DST, &tmp->to.in); |
e1694a75 YW |
1021 | if (r < 0 && r != -ENODATA) { |
1022 | log_warning_errno(r, "rtnl: could not get FRA_DST attribute, ignoring: %m"); | |
1023 | return 0; | |
1024 | } else if (r >= 0) { | |
b80a511b | 1025 | r = sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(message, &tmp->to_prefixlen); |
e1694a75 | 1026 | if (r < 0) { |
7f474ed7 | 1027 | log_warning_errno(r, "rtnl: received rule message without valid destination prefix length, ignoring: %m"); |
e1694a75 YW |
1028 | return 0; |
1029 | } | |
bce67bbe SS |
1030 | } |
1031 | ||
1032 | break; | |
1033 | ||
1034 | case AF_INET6: | |
b80a511b | 1035 | r = sd_netlink_message_read_in6_addr(message, FRA_SRC, &tmp->from.in6); |
e1694a75 YW |
1036 | if (r < 0 && r != -ENODATA) { |
1037 | log_warning_errno(r, "rtnl: could not get FRA_SRC attribute, ignoring: %m"); | |
1038 | return 0; | |
1039 | } else if (r >= 0) { | |
b80a511b | 1040 | r = sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(message, &tmp->from_prefixlen); |
e1694a75 | 1041 | if (r < 0) { |
7f474ed7 | 1042 | log_warning_errno(r, "rtnl: received rule message without valid source prefix length, ignoring: %m"); |
e1694a75 YW |
1043 | return 0; |
1044 | } | |
bce67bbe SS |
1045 | } |
1046 | ||
b80a511b | 1047 | r = sd_netlink_message_read_in6_addr(message, FRA_DST, &tmp->to.in6); |
e1694a75 YW |
1048 | if (r < 0 && r != -ENODATA) { |
1049 | log_warning_errno(r, "rtnl: could not get FRA_DST attribute, ignoring: %m"); | |
1050 | return 0; | |
1051 | } else if (r >= 0) { | |
b80a511b | 1052 | r = sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(message, &tmp->to_prefixlen); |
e1694a75 | 1053 | if (r < 0) { |
7f474ed7 | 1054 | log_warning_errno(r, "rtnl: received rule message without valid destination prefix length, ignoring: %m"); |
e1694a75 YW |
1055 | return 0; |
1056 | } | |
bce67bbe SS |
1057 | } |
1058 | ||
1059 | break; | |
1060 | ||
1061 | default: | |
7f474ed7 | 1062 | assert_not_reached("Received rule message with unsupported address family"); |
bce67bbe SS |
1063 | } |
1064 | ||
b80a511b | 1065 | if (tmp->from_prefixlen == 0 && tmp->to_prefixlen == 0) |
bce67bbe SS |
1066 | return 0; |
1067 | ||
b80a511b YW |
1068 | r = sd_rtnl_message_routing_policy_rule_get_flags(message, &flags); |
1069 | if (r < 0) { | |
7f474ed7 | 1070 | log_warning_errno(r, "rtnl: received rule message without valid flag, ignoring: %m"); |
b80a511b YW |
1071 | return 0; |
1072 | } | |
1073 | tmp->invert_rule = flags & FIB_RULE_INVERT; | |
1074 | ||
1075 | r = sd_netlink_message_read_u32(message, FRA_FWMARK, &tmp->fwmark); | |
e1694a75 YW |
1076 | if (r < 0 && r != -ENODATA) { |
1077 | log_warning_errno(r, "rtnl: could not get FRA_FWMARK attribute, ignoring: %m"); | |
1078 | return 0; | |
1079 | } | |
1080 | ||
b80a511b YW |
1081 | r = sd_netlink_message_read_u32(message, FRA_FWMASK, &tmp->fwmask); |
1082 | if (r < 0 && r != -ENODATA) { | |
1083 | log_warning_errno(r, "rtnl: could not get FRA_FWMASK attribute, ignoring: %m"); | |
1084 | return 0; | |
1085 | } | |
1086 | ||
1087 | r = sd_netlink_message_read_u32(message, FRA_PRIORITY, &tmp->priority); | |
1088 | if (r < 0 && r != -ENODATA) { | |
1089 | log_warning_errno(r, "rtnl: could not get FRA_PRIORITY attribute, ignoring: %m"); | |
1090 | return 0; | |
1091 | } | |
1092 | ||
1093 | r = sd_netlink_message_read_u32(message, FRA_TABLE, &tmp->table); | |
e1694a75 YW |
1094 | if (r < 0 && r != -ENODATA) { |
1095 | log_warning_errno(r, "rtnl: could not get FRA_TABLE attribute, ignoring: %m"); | |
1096 | return 0; | |
1097 | } | |
1098 | ||
b80a511b | 1099 | r = sd_rtnl_message_routing_policy_rule_get_tos(message, &tmp->tos); |
e1694a75 YW |
1100 | if (r < 0 && r != -ENODATA) { |
1101 | log_warning_errno(r, "rtnl: could not get ip rule TOS, ignoring: %m"); | |
1102 | return 0; | |
1103 | } | |
1104 | ||
eeab051b | 1105 | r = sd_netlink_message_read_string(message, FRA_IIFNAME, &iif); |
e1694a75 YW |
1106 | if (r < 0 && r != -ENODATA) { |
1107 | log_warning_errno(r, "rtnl: could not get FRA_IIFNAME attribute, ignoring: %m"); | |
1108 | return 0; | |
1109 | } | |
b80a511b YW |
1110 | r = free_and_strdup(&tmp->iif, iif); |
1111 | if (r < 0) | |
1112 | return log_oom(); | |
e1694a75 | 1113 | |
eeab051b | 1114 | r = sd_netlink_message_read_string(message, FRA_OIFNAME, &oif); |
e1694a75 YW |
1115 | if (r < 0 && r != -ENODATA) { |
1116 | log_warning_errno(r, "rtnl: could not get FRA_OIFNAME attribute, ignoring: %m"); | |
1117 | return 0; | |
1118 | } | |
b80a511b YW |
1119 | r = free_and_strdup(&tmp->oif, oif); |
1120 | if (r < 0) | |
1121 | return log_oom(); | |
bce67bbe | 1122 | |
b80a511b | 1123 | r = sd_netlink_message_read_u8(message, FRA_IP_PROTO, &tmp->protocol); |
926062f0 SS |
1124 | if (r < 0 && r != -ENODATA) { |
1125 | log_warning_errno(r, "rtnl: could not get FRA_IP_PROTO attribute, ignoring: %m"); | |
1126 | return 0; | |
1127 | } | |
1128 | ||
b80a511b | 1129 | r = sd_netlink_message_read(message, FRA_SPORT_RANGE, sizeof(tmp->sport), &tmp->sport); |
926062f0 SS |
1130 | if (r < 0 && r != -ENODATA) { |
1131 | log_warning_errno(r, "rtnl: could not get FRA_SPORT_RANGE attribute, ignoring: %m"); | |
1132 | return 0; | |
1133 | } | |
1134 | ||
b80a511b | 1135 | r = sd_netlink_message_read(message, FRA_DPORT_RANGE, sizeof(tmp->dport), &tmp->dport); |
926062f0 SS |
1136 | if (r < 0 && r != -ENODATA) { |
1137 | log_warning_errno(r, "rtnl: could not get FRA_DPORT_RANGE attribute, ignoring: %m"); | |
1138 | return 0; | |
1139 | } | |
1140 | ||
b80a511b | 1141 | (void) routing_policy_rule_get(m, tmp, &rule); |
bce67bbe | 1142 | |
15201512 YW |
1143 | if (DEBUG_LOGGING) { |
1144 | (void) in_addr_to_string(tmp->family, &tmp->from, &from); | |
1145 | (void) in_addr_to_string(tmp->family, &tmp->to, &to); | |
1146 | } | |
1147 | ||
bce67bbe SS |
1148 | switch (type) { |
1149 | case RTM_NEWRULE: | |
508f63b4 | 1150 | if (!rule) { |
15201512 YW |
1151 | log_debug("Remembering foreign routing policy rule: %s/%u -> %s/%u, iif: %s, oif: %s, table: %u", |
1152 | from, tmp->from_prefixlen, to, tmp->to_prefixlen, strna(tmp->iif), strna(tmp->oif), tmp->table); | |
b80a511b | 1153 | r = routing_policy_rule_add_foreign(m, tmp, &rule); |
bce67bbe | 1154 | if (r < 0) { |
7f474ed7 | 1155 | log_warning_errno(r, "Could not remember foreign rule, ignoring: %m"); |
bce67bbe SS |
1156 | return 0; |
1157 | } | |
1158 | } | |
1159 | break; | |
1160 | case RTM_DELRULE: | |
15201512 YW |
1161 | log_debug("Forgetting routing policy rule: %s/%u -> %s/%u, iif: %s, oif: %s, table: %u", |
1162 | from, tmp->from_prefixlen, to, tmp->to_prefixlen, strna(tmp->iif), strna(tmp->oif), tmp->table); | |
bce67bbe SS |
1163 | routing_policy_rule_free(rule); |
1164 | ||
1165 | break; | |
1166 | ||
1167 | default: | |
1168 | assert_not_reached("Received invalid RTNL message type"); | |
1169 | } | |
1170 | ||
1171 | return 1; | |
1172 | } | |
1173 | ||
c16c7808 SS |
1174 | int manager_rtnl_process_nexthop(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) { |
1175 | _cleanup_(nexthop_freep) NextHop *tmp = NULL; | |
1176 | _cleanup_free_ char *gateway = NULL; | |
1177 | NextHop *nexthop = NULL; | |
1178 | Manager *m = userdata; | |
1179 | Link *link = NULL; | |
1180 | uint16_t type; | |
1181 | int r; | |
1182 | ||
1183 | assert(rtnl); | |
1184 | assert(message); | |
1185 | assert(m); | |
1186 | ||
1187 | if (sd_netlink_message_is_error(message)) { | |
1188 | r = sd_netlink_message_get_errno(message); | |
1189 | if (r < 0) | |
5ecb131d | 1190 | log_message_warning_errno(message, r, "rtnl: failed to receive rule message, ignoring"); |
c16c7808 SS |
1191 | |
1192 | return 0; | |
1193 | } | |
1194 | ||
1195 | r = sd_netlink_message_get_type(message, &type); | |
1196 | if (r < 0) { | |
1197 | log_warning_errno(r, "rtnl: could not get message type, ignoring: %m"); | |
1198 | return 0; | |
1199 | } else if (!IN_SET(type, RTM_NEWNEXTHOP, RTM_DELNEXTHOP)) { | |
1200 | log_warning("rtnl: received unexpected message type %u when processing nexthop, ignoring.", type); | |
1201 | return 0; | |
1202 | } | |
1203 | ||
1204 | r = nexthop_new(&tmp); | |
1205 | if (r < 0) | |
1206 | return log_oom(); | |
1207 | ||
1208 | r = sd_rtnl_message_get_family(message, &tmp->family); | |
1209 | if (r < 0) { | |
1210 | log_warning_errno(r, "rtnl: could not get nexthop family, ignoring: %m"); | |
1211 | return 0; | |
1212 | } else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) { | |
1213 | log_debug("rtnl: received nexthop message with invalid family %d, ignoring.", tmp->family); | |
1214 | return 0; | |
1215 | } | |
1216 | ||
1217 | switch (tmp->family) { | |
1218 | case AF_INET: | |
1219 | r = sd_netlink_message_read_in_addr(message, NHA_GATEWAY, &tmp->gw.in); | |
1220 | if (r < 0 && r != -ENODATA) { | |
1221 | log_warning_errno(r, "rtnl: could not get NHA_GATEWAY attribute, ignoring: %m"); | |
1222 | return 0; | |
1223 | } | |
1224 | break; | |
1225 | ||
1226 | case AF_INET6: | |
1227 | r = sd_netlink_message_read_in6_addr(message, NHA_GATEWAY, &tmp->gw.in6); | |
1228 | if (r < 0 && r != -ENODATA) { | |
1229 | log_warning_errno(r, "rtnl: could not get NHA_GATEWAY attribute, ignoring: %m"); | |
1230 | return 0; | |
1231 | } | |
1232 | break; | |
1233 | ||
1234 | default: | |
1235 | assert_not_reached("Received rule message with unsupported address family"); | |
1236 | } | |
1237 | ||
1238 | r = sd_netlink_message_read_u32(message, NHA_ID, &tmp->id); | |
1239 | if (r < 0 && r != -ENODATA) { | |
1240 | log_warning_errno(r, "rtnl: could not get NHA_ID attribute, ignoring: %m"); | |
1241 | return 0; | |
1242 | } | |
1243 | ||
1244 | r = sd_netlink_message_read_u32(message, NHA_OIF, &tmp->oif); | |
1245 | if (r < 0 && r != -ENODATA) { | |
1246 | log_warning_errno(r, "rtnl: could not get NHA_OIF attribute, ignoring: %m"); | |
1247 | return 0; | |
1248 | } | |
1249 | ||
1250 | r = link_get(m, tmp->oif, &link); | |
1251 | if (r < 0 || !link) { | |
1252 | if (!m->enumerating) | |
1253 | log_warning("rtnl: received nexthop message for link (%d) we do not know about, ignoring", tmp->oif); | |
1254 | return 0; | |
1255 | } | |
1256 | ||
1257 | (void) nexthop_get(link, tmp, &nexthop); | |
1258 | ||
1259 | if (DEBUG_LOGGING) | |
1260 | (void) in_addr_to_string(tmp->family, &tmp->gw, &gateway); | |
1261 | ||
1262 | switch (type) { | |
1263 | case RTM_NEWNEXTHOP: | |
1264 | if (!nexthop) { | |
1265 | log_debug("Remembering foreign nexthop: %s, oif: %d, id: %d", gateway, tmp->oif, tmp->id); | |
1266 | r = nexthop_add_foreign(link, tmp, &nexthop); | |
1267 | if (r < 0) { | |
1268 | log_warning_errno(r, "Could not remember foreign nexthop, ignoring: %m"); | |
1269 | return 0; | |
1270 | } | |
1271 | } | |
1272 | break; | |
1273 | case RTM_DELNEXTHOP: | |
1274 | log_debug("Forgetting foreign nexthop: %s, oif: %d, id: %d", gateway, tmp->oif, tmp->id); | |
1275 | nexthop_free(nexthop); | |
1276 | ||
1277 | break; | |
1278 | ||
1279 | default: | |
1280 | assert_not_reached("Received invalid RTNL message type"); | |
1281 | } | |
1282 | ||
1283 | return 1; | |
1284 | } | |
1285 | ||
5fae368b TG |
1286 | static int systemd_netlink_fd(void) { |
1287 | int n, fd, rtnl_fd = -EINVAL; | |
1288 | ||
1289 | n = sd_listen_fds(true); | |
1290 | if (n <= 0) | |
1291 | return -EINVAL; | |
1292 | ||
1293 | for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) { | |
1294 | if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) { | |
1295 | if (rtnl_fd >= 0) | |
1296 | return -EINVAL; | |
1297 | ||
1298 | rtnl_fd = fd; | |
1299 | } | |
1300 | } | |
1301 | ||
1302 | return rtnl_fd; | |
1303 | } | |
1304 | ||
05d0c2e3 JT |
1305 | static int manager_connect_genl(Manager *m) { |
1306 | int r; | |
1307 | ||
1308 | assert(m); | |
1309 | ||
1310 | r = sd_genl_socket_open(&m->genl); | |
1311 | if (r < 0) | |
1312 | return r; | |
1313 | ||
1314 | r = sd_netlink_inc_rcvbuf(m->genl, RCVBUF_SIZE); | |
1315 | if (r < 0) | |
1316 | return r; | |
1317 | ||
1318 | r = sd_netlink_attach_event(m->genl, m->event, 0); | |
1319 | if (r < 0) | |
1320 | return r; | |
1321 | ||
1322 | return 0; | |
1323 | } | |
1324 | ||
5fae368b TG |
1325 | static int manager_connect_rtnl(Manager *m) { |
1326 | int fd, r; | |
505f8da7 TG |
1327 | |
1328 | assert(m); | |
505f8da7 | 1329 | |
5fae368b TG |
1330 | fd = systemd_netlink_fd(); |
1331 | if (fd < 0) | |
1c4baffc | 1332 | r = sd_netlink_open(&m->rtnl); |
5fae368b | 1333 | else |
1c4baffc | 1334 | r = sd_netlink_open_fd(&m->rtnl, fd); |
505f8da7 TG |
1335 | if (r < 0) |
1336 | return r; | |
1337 | ||
1c4baffc | 1338 | r = sd_netlink_inc_rcvbuf(m->rtnl, RCVBUF_SIZE); |
f579559b | 1339 | if (r < 0) |
bf5332d2 | 1340 | return r; |
f579559b | 1341 | |
1c4baffc | 1342 | r = sd_netlink_attach_event(m->rtnl, m->event, 0); |
505f8da7 TG |
1343 | if (r < 0) |
1344 | return r; | |
f579559b | 1345 | |
8190a388 | 1346 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWLINK, &manager_rtnl_process_link, NULL, m, "network-rtnl_process_link"); |
5fae368b TG |
1347 | if (r < 0) |
1348 | return r; | |
505f8da7 | 1349 | |
8190a388 | 1350 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELLINK, &manager_rtnl_process_link, NULL, m, "network-rtnl_process_link"); |
5fae368b TG |
1351 | if (r < 0) |
1352 | return r; | |
45af44d4 | 1353 | |
8190a388 | 1354 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWADDR, &manager_rtnl_process_address, NULL, m, "network-rtnl_process_address"); |
5fae368b TG |
1355 | if (r < 0) |
1356 | return r; | |
1357 | ||
8190a388 | 1358 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELADDR, &manager_rtnl_process_address, NULL, m, "network-rtnl_process_address"); |
5fae368b TG |
1359 | if (r < 0) |
1360 | return r; | |
1361 | ||
d1bdafd2 WKI |
1362 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWNEIGH, &manager_rtnl_process_neighbor, NULL, m, "network-rtnl_process_neighbor"); |
1363 | if (r < 0) | |
1364 | return r; | |
1365 | ||
1366 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELNEIGH, &manager_rtnl_process_neighbor, NULL, m, "network-rtnl_process_neighbor"); | |
1367 | if (r < 0) | |
1368 | return r; | |
1369 | ||
8190a388 | 1370 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWROUTE, &manager_rtnl_process_route, NULL, m, "network-rtnl_process_route"); |
1c8e710c TG |
1371 | if (r < 0) |
1372 | return r; | |
1373 | ||
8190a388 | 1374 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELROUTE, &manager_rtnl_process_route, NULL, m, "network-rtnl_process_route"); |
1c8e710c TG |
1375 | if (r < 0) |
1376 | return r; | |
1377 | ||
8190a388 | 1378 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWRULE, &manager_rtnl_process_rule, NULL, m, "network-rtnl_process_rule"); |
bce67bbe SS |
1379 | if (r < 0) |
1380 | return r; | |
1381 | ||
8190a388 | 1382 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELRULE, &manager_rtnl_process_rule, NULL, m, "network-rtnl_process_rule"); |
bce67bbe SS |
1383 | if (r < 0) |
1384 | return r; | |
1385 | ||
c16c7808 SS |
1386 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWNEXTHOP, &manager_rtnl_process_nexthop, NULL, m, "network-rtnl_process_nexthop"); |
1387 | if (r < 0) | |
1388 | return r; | |
1389 | ||
1390 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELNEXTHOP, &manager_rtnl_process_nexthop, NULL, m, "network-rtnl_process_nexthop"); | |
1391 | if (r < 0) | |
1392 | return r; | |
1393 | ||
5fae368b | 1394 | return 0; |
45af44d4 | 1395 | } |
505f8da7 | 1396 | |
5512a963 | 1397 | static int ordered_set_put_in_addr_data(OrderedSet *s, const struct in_addr_data *address) { |
84de38c5 TG |
1398 | char *p; |
1399 | int r; | |
1400 | ||
1401 | assert(s); | |
5512a963 LP |
1402 | assert(address); |
1403 | ||
1404 | r = in_addr_to_string(address->family, &address->address, &p); | |
1405 | if (r < 0) | |
1406 | return r; | |
1407 | ||
1408 | r = ordered_set_consume(s, p); | |
1409 | if (r == -EEXIST) | |
1410 | return 0; | |
1411 | ||
1412 | return r; | |
1413 | } | |
1414 | ||
1415 | static int ordered_set_put_in_addr_datav(OrderedSet *s, const struct in_addr_data *addresses, unsigned n) { | |
1416 | int r, c = 0; | |
1417 | unsigned i; | |
1418 | ||
1419 | assert(s); | |
1420 | assert(addresses || n == 0); | |
1421 | ||
1422 | for (i = 0; i < n; i++) { | |
1423 | r = ordered_set_put_in_addr_data(s, addresses+i); | |
1424 | if (r < 0) | |
1425 | return r; | |
1426 | ||
1427 | c += r; | |
1428 | } | |
1429 | ||
1430 | return c; | |
1431 | } | |
1432 | ||
1433 | static int ordered_set_put_in4_addr(OrderedSet *s, const struct in_addr *address) { | |
1434 | char *p; | |
1435 | int r; | |
1436 | ||
1437 | assert(s); | |
1438 | assert(address); | |
84de38c5 TG |
1439 | |
1440 | r = in_addr_to_string(AF_INET, (const union in_addr_union*) address, &p); | |
1441 | if (r < 0) | |
1442 | return r; | |
1443 | ||
00616955 | 1444 | r = ordered_set_consume(s, p); |
84de38c5 TG |
1445 | if (r == -EEXIST) |
1446 | return 0; | |
1447 | ||
1448 | return r; | |
1449 | } | |
1450 | ||
072320ea TH |
1451 | static int ordered_set_put_in4_addrv(OrderedSet *s, |
1452 | const struct in_addr *addresses, | |
1453 | size_t n, | |
1454 | bool (*predicate)(const struct in_addr *addr)) { | |
5512a963 | 1455 | int r, c = 0; |
072320ea | 1456 | size_t i; |
84de38c5 TG |
1457 | |
1458 | assert(s); | |
5512a963 | 1459 | assert(n == 0 || addresses); |
84de38c5 TG |
1460 | |
1461 | for (i = 0; i < n; i++) { | |
072320ea TH |
1462 | if (predicate && !predicate(&addresses[i])) |
1463 | continue; | |
5512a963 | 1464 | r = ordered_set_put_in4_addr(s, addresses+i); |
84de38c5 TG |
1465 | if (r < 0) |
1466 | return r; | |
1467 | ||
1468 | c += r; | |
1469 | } | |
1470 | ||
1471 | return c; | |
1472 | } | |
1473 | ||
84de38c5 | 1474 | static int manager_save(Manager *m) { |
299d578f SS |
1475 | _cleanup_ordered_set_free_free_ OrderedSet *dns = NULL, *ntp = NULL, *sip = NULL, *search_domains = NULL, *route_domains = NULL; |
1476 | const char *operstate_str, *carrier_state_str, *address_state_str; | |
84de38c5 | 1477 | LinkOperationalState operstate = LINK_OPERSTATE_OFF; |
7f3c07ad YW |
1478 | LinkCarrierState carrier_state = LINK_CARRIER_STATE_OFF; |
1479 | LinkAddressState address_state = LINK_ADDRESS_STATE_OFF; | |
299d578f SS |
1480 | _cleanup_free_ char *temp_path = NULL; |
1481 | _cleanup_strv_free_ char **p = NULL; | |
1482 | _cleanup_fclose_ FILE *f = NULL; | |
1483 | Link *link; | |
1484 | Iterator i; | |
84de38c5 TG |
1485 | int r; |
1486 | ||
1487 | assert(m); | |
1488 | assert(m->state_file); | |
1489 | ||
1490 | /* We add all NTP and DNS server to a set, to filter out duplicates */ | |
00616955 | 1491 | dns = ordered_set_new(&string_hash_ops); |
84de38c5 TG |
1492 | if (!dns) |
1493 | return -ENOMEM; | |
1494 | ||
00616955 | 1495 | ntp = ordered_set_new(&string_hash_ops); |
84de38c5 TG |
1496 | if (!ntp) |
1497 | return -ENOMEM; | |
1498 | ||
299d578f SS |
1499 | sip = ordered_set_new(&string_hash_ops); |
1500 | if (!sip) | |
1501 | return -ENOMEM; | |
1502 | ||
482d1aeb | 1503 | search_domains = ordered_set_new(&dns_name_hash_ops); |
3df9bec5 LP |
1504 | if (!search_domains) |
1505 | return -ENOMEM; | |
1506 | ||
482d1aeb | 1507 | route_domains = ordered_set_new(&dns_name_hash_ops); |
3df9bec5 | 1508 | if (!route_domains) |
84de38c5 TG |
1509 | return -ENOMEM; |
1510 | ||
1511 | HASHMAP_FOREACH(link, m->links, i) { | |
1512 | if (link->flags & IFF_LOOPBACK) | |
1513 | continue; | |
1514 | ||
1515 | if (link->operstate > operstate) | |
1516 | operstate = link->operstate; | |
1517 | ||
7f3c07ad YW |
1518 | if (link->carrier_state > carrier_state) |
1519 | carrier_state = link->carrier_state; | |
1520 | ||
1521 | if (link->address_state > address_state) | |
1522 | address_state = link->address_state; | |
1523 | ||
84de38c5 TG |
1524 | if (!link->network) |
1525 | continue; | |
1526 | ||
1527 | /* First add the static configured entries */ | |
5512a963 | 1528 | r = ordered_set_put_in_addr_datav(dns, link->network->dns, link->network->n_dns); |
84de38c5 TG |
1529 | if (r < 0) |
1530 | return r; | |
1531 | ||
15761549 | 1532 | r = ordered_set_put_strdupv(ntp, link->ntp ?: link->network->ntp); |
84de38c5 TG |
1533 | if (r < 0) |
1534 | return r; | |
1535 | ||
15761549 | 1536 | r = ordered_set_put_string_set(search_domains, link->search_domains ?: link->network->search_domains); |
3df9bec5 LP |
1537 | if (r < 0) |
1538 | return r; | |
1539 | ||
15761549 | 1540 | r = ordered_set_put_string_set(route_domains, link->route_domains ?: link->network->route_domains); |
84de38c5 TG |
1541 | if (r < 0) |
1542 | return r; | |
1543 | ||
1544 | if (!link->dhcp_lease) | |
1545 | continue; | |
1546 | ||
1547 | /* Secondly, add the entries acquired via DHCP */ | |
27cb34f5 | 1548 | if (link->network->dhcp_use_dns) { |
84de38c5 TG |
1549 | const struct in_addr *addresses; |
1550 | ||
1551 | r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses); | |
1552 | if (r > 0) { | |
072320ea | 1553 | r = ordered_set_put_in4_addrv(dns, addresses, r, in4_addr_is_non_local); |
84de38c5 TG |
1554 | if (r < 0) |
1555 | return r; | |
1556 | } else if (r < 0 && r != -ENODATA) | |
1557 | return r; | |
1558 | } | |
1559 | ||
27cb34f5 | 1560 | if (link->network->dhcp_use_ntp) { |
84de38c5 TG |
1561 | const struct in_addr *addresses; |
1562 | ||
1563 | r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses); | |
1564 | if (r > 0) { | |
072320ea | 1565 | r = ordered_set_put_in4_addrv(ntp, addresses, r, in4_addr_is_non_local); |
84de38c5 TG |
1566 | if (r < 0) |
1567 | return r; | |
1568 | } else if (r < 0 && r != -ENODATA) | |
1569 | return r; | |
1570 | } | |
1571 | ||
299d578f SS |
1572 | if (link->network->dhcp_use_sip) { |
1573 | const struct in_addr *addresses; | |
1574 | ||
1575 | r = sd_dhcp_lease_get_sip(link->dhcp_lease, &addresses); | |
1576 | if (r > 0) { | |
1577 | r = ordered_set_put_in4_addrv(sip, addresses, r, in4_addr_is_non_local); | |
1578 | if (r < 0) | |
1579 | return r; | |
1580 | } else if (r < 0 && r != -ENODATA) | |
1581 | return r; | |
1582 | } | |
1583 | ||
b2a81c0b | 1584 | if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) { |
84de38c5 | 1585 | const char *domainname; |
b85bc551 | 1586 | char **domains = NULL; |
84de38c5 | 1587 | |
b85bc551 | 1588 | OrderedSet *target_domains = (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES) ? search_domains : route_domains; |
84de38c5 TG |
1589 | r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname); |
1590 | if (r >= 0) { | |
b85bc551 DW |
1591 | r = ordered_set_put_strdup(target_domains, domainname); |
1592 | if (r < 0) | |
1593 | return r; | |
1594 | } else if (r != -ENODATA) | |
1595 | return r; | |
b2a81c0b | 1596 | |
b85bc551 DW |
1597 | r = sd_dhcp_lease_get_search_domains(link->dhcp_lease, &domains); |
1598 | if (r >= 0) { | |
1599 | r = ordered_set_put_strdupv(target_domains, domains); | |
84de38c5 TG |
1600 | if (r < 0) |
1601 | return r; | |
1602 | } else if (r != -ENODATA) | |
1603 | return r; | |
1604 | } | |
1605 | } | |
1606 | ||
7f3c07ad YW |
1607 | if (carrier_state >= LINK_CARRIER_STATE_ENSLAVED) |
1608 | carrier_state = LINK_CARRIER_STATE_CARRIER; | |
1609 | ||
84de38c5 TG |
1610 | operstate_str = link_operstate_to_string(operstate); |
1611 | assert(operstate_str); | |
1612 | ||
ac999bf0 YW |
1613 | carrier_state_str = link_carrier_state_to_string(carrier_state); |
1614 | assert(carrier_state_str); | |
1615 | ||
1616 | address_state_str = link_address_state_to_string(address_state); | |
1617 | assert(address_state_str); | |
1618 | ||
84de38c5 TG |
1619 | r = fopen_temporary(m->state_file, &f, &temp_path); |
1620 | if (r < 0) | |
1621 | return r; | |
1622 | ||
5512a963 | 1623 | (void) fchmod(fileno(f), 0644); |
84de38c5 TG |
1624 | |
1625 | fprintf(f, | |
1626 | "# This is private data. Do not parse.\n" | |
ac999bf0 YW |
1627 | "OPER_STATE=%s\n" |
1628 | "CARRIER_STATE=%s\n" | |
1629 | "ADDRESS_STATE=%s\n", | |
1630 | operstate_str, carrier_state_str, address_state_str); | |
84de38c5 | 1631 | |
53ae3f64 ZJS |
1632 | ordered_set_print(f, "DNS=", dns); |
1633 | ordered_set_print(f, "NTP=", ntp); | |
299d578f | 1634 | ordered_set_print(f, "SIP=", sip); |
53ae3f64 ZJS |
1635 | ordered_set_print(f, "DOMAINS=", search_domains); |
1636 | ordered_set_print(f, "ROUTE_DOMAINS=", route_domains); | |
84de38c5 | 1637 | |
458d8ae3 ZJS |
1638 | r = routing_policy_serialize_rules(m->rules, f); |
1639 | if (r < 0) | |
1640 | goto fail; | |
bce67bbe | 1641 | |
84de38c5 TG |
1642 | r = fflush_and_check(f); |
1643 | if (r < 0) | |
1644 | goto fail; | |
1645 | ||
1646 | if (rename(temp_path, m->state_file) < 0) { | |
1647 | r = -errno; | |
1648 | goto fail; | |
1649 | } | |
1650 | ||
1651 | if (m->operational_state != operstate) { | |
1652 | m->operational_state = operstate; | |
7f3c07ad YW |
1653 | if (strv_extend(&p, "OperationalState") < 0) |
1654 | log_oom(); | |
1655 | } | |
1656 | ||
1657 | if (m->carrier_state != carrier_state) { | |
1658 | m->carrier_state = carrier_state; | |
1659 | if (strv_extend(&p, "CarrierState") < 0) | |
1660 | log_oom(); | |
1661 | } | |
1662 | ||
1663 | if (m->address_state != address_state) { | |
1664 | m->address_state = address_state; | |
1665 | if (strv_extend(&p, "AddressState") < 0) | |
1666 | log_oom(); | |
1667 | } | |
1668 | ||
1669 | if (p) { | |
1670 | r = manager_send_changed_strv(m, p); | |
84de38c5 | 1671 | if (r < 0) |
7f3c07ad | 1672 | log_error_errno(r, "Could not emit changed properties: %m"); |
84de38c5 TG |
1673 | } |
1674 | ||
1675 | m->dirty = false; | |
1676 | ||
1677 | return 0; | |
1678 | ||
1679 | fail: | |
1680 | (void) unlink(m->state_file); | |
1681 | (void) unlink(temp_path); | |
1682 | ||
1683 | return log_error_errno(r, "Failed to save network state to %s: %m", m->state_file); | |
1684 | } | |
1685 | ||
1686 | static int manager_dirty_handler(sd_event_source *s, void *userdata) { | |
1687 | Manager *m = userdata; | |
1688 | Link *link; | |
1689 | Iterator i; | |
84de38c5 TG |
1690 | |
1691 | assert(m); | |
1692 | ||
1693 | if (m->dirty) | |
1694 | manager_save(m); | |
1695 | ||
a0e8e4cf YW |
1696 | SET_FOREACH(link, m->dirty_links, i) |
1697 | if (link_save(link) >= 0) | |
84de38c5 | 1698 | link_clean(link); |
84de38c5 TG |
1699 | |
1700 | return 1; | |
1701 | } | |
1702 | ||
ab76be55 ZJS |
1703 | static int signal_terminate_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
1704 | Manager *m = userdata; | |
1705 | ||
1706 | assert(m); | |
1707 | m->restarting = false; | |
1708 | ||
1709 | log_debug("Terminate operation initiated."); | |
1710 | ||
1711 | return sd_event_exit(sd_event_source_get_event(s), 0); | |
1712 | } | |
1713 | ||
1714 | static int signal_restart_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { | |
1715 | Manager *m = userdata; | |
1716 | ||
1717 | assert(m); | |
1718 | m->restarting = true; | |
1719 | ||
1720 | log_debug("Restart operation initiated."); | |
1721 | ||
1722 | return sd_event_exit(sd_event_source_get_event(s), 0); | |
1723 | } | |
1724 | ||
3534a043 | 1725 | int manager_new(Manager **ret) { |
8e766630 | 1726 | _cleanup_(manager_freep) Manager *m = NULL; |
45af44d4 | 1727 | int r; |
f579559b | 1728 | |
a879e1a4 | 1729 | m = new(Manager, 1); |
5fae368b TG |
1730 | if (!m) |
1731 | return -ENOMEM; | |
45af44d4 | 1732 | |
a879e1a4 YW |
1733 | *m = (Manager) { |
1734 | .speed_meter_interval_usec = SPEED_METER_DEFAULT_TIME_INTERVAL, | |
1735 | }; | |
1736 | ||
5fae368b TG |
1737 | m->state_file = strdup("/run/systemd/netif/state"); |
1738 | if (!m->state_file) | |
1739 | return -ENOMEM; | |
1740 | ||
3534a043 YW |
1741 | r = sd_event_default(&m->event); |
1742 | if (r < 0) | |
1743 | return r; | |
1744 | ||
ab76be55 ZJS |
1745 | assert_se(sigprocmask_many(SIG_SETMASK, NULL, SIGINT, SIGTERM, SIGUSR2, -1) >= 0); |
1746 | ||
05e21627 | 1747 | (void) sd_event_set_watchdog(m->event, true); |
ab76be55 ZJS |
1748 | (void) sd_event_add_signal(m->event, NULL, SIGTERM, signal_terminate_callback, m); |
1749 | (void) sd_event_add_signal(m->event, NULL, SIGINT, signal_terminate_callback, m); | |
1750 | (void) sd_event_add_signal(m->event, NULL, SIGUSR2, signal_restart_callback, m); | |
5fae368b | 1751 | |
84de38c5 TG |
1752 | r = sd_event_add_post(m->event, NULL, manager_dirty_handler, m); |
1753 | if (r < 0) | |
1754 | return r; | |
1755 | ||
5fae368b | 1756 | r = manager_connect_rtnl(m); |
45af44d4 TG |
1757 | if (r < 0) |
1758 | return r; | |
1759 | ||
05d0c2e3 JT |
1760 | r = manager_connect_genl(m); |
1761 | if (r < 0) | |
1762 | return r; | |
1763 | ||
5fae368b TG |
1764 | r = manager_connect_udev(m); |
1765 | if (r < 0) | |
1766 | return r; | |
45af44d4 | 1767 | |
05d0c2e3 JT |
1768 | r = sd_resolve_default(&m->resolve); |
1769 | if (r < 0) | |
1770 | return r; | |
1771 | ||
1772 | r = sd_resolve_attach_event(m->resolve, m->event, 0); | |
1773 | if (r < 0) | |
1774 | return r; | |
1775 | ||
5fae368b TG |
1776 | r = setup_default_address_pool(m); |
1777 | if (r < 0) | |
1778 | return r; | |
f579559b | 1779 | |
8341a5c3 | 1780 | m->duid.type = DUID_TYPE_EN; |
413708d1 | 1781 | |
458d8ae3 | 1782 | (void) routing_policy_load_rules(m->state_file, &m->rules_saved); |
bce67bbe | 1783 | |
1cc6c93a | 1784 | *ret = TAKE_PTR(m); |
f579559b | 1785 | |
f579559b TG |
1786 | return 0; |
1787 | } | |
1788 | ||
5fae368b | 1789 | void manager_free(Manager *m) { |
2c448c8a | 1790 | struct in6_addr *a; |
c4397d94 | 1791 | AddressPool *pool; |
5fae368b | 1792 | Link *link; |
f579559b | 1793 | |
5fae368b TG |
1794 | if (!m) |
1795 | return; | |
505f8da7 | 1796 | |
5fae368b | 1797 | free(m->state_file); |
505f8da7 | 1798 | |
2c448c8a | 1799 | while ((a = hashmap_first_key(m->dhcp6_prefixes))) |
04ed9949 | 1800 | (void) dhcp6_prefix_remove(m, a); |
26a65470 | 1801 | m->dhcp6_prefixes = hashmap_free(m->dhcp6_prefixes); |
e133b289 | 1802 | |
f61365e5 | 1803 | while ((link = hashmap_steal_first(m->links))) { |
65dd5e31 | 1804 | if (link->dhcp6_client) |
f61365e5 | 1805 | (void) dhcp6_lease_pd_prefix_lost(link->dhcp6_client, link); |
946f8e14 | 1806 | |
95355a28 | 1807 | (void) link_stop_clients(link, true); |
946f8e14 | 1808 | |
5fae368b | 1809 | link_unref(link); |
65dd5e31 | 1810 | } |
f579559b | 1811 | |
c4397d94 | 1812 | m->dirty_links = set_free_with_destructor(m->dirty_links, link_unref); |
5f707e12 YW |
1813 | m->links_requesting_uuid = set_free_with_destructor(m->links_requesting_uuid, link_unref); |
1814 | m->links = hashmap_free_with_destructor(m->links, link_unref); | |
27dfc982 | 1815 | |
5f707e12 | 1816 | m->duids_requesting_uuid = set_free(m->duids_requesting_uuid); |
715d398e | 1817 | m->networks = ordered_hashmap_free_with_destructor(m->networks, network_unref); |
dbffab87 | 1818 | |
c4397d94 | 1819 | m->netdevs = hashmap_free_with_destructor(m->netdevs, netdev_unref); |
5fae368b TG |
1820 | |
1821 | while ((pool = m->address_pools)) | |
1822 | address_pool_free(pool); | |
1823 | ||
f19ee681 YW |
1824 | /* routing_policy_rule_free() access m->rules and m->rules_foreign. |
1825 | * So, it is necessary to set NULL after the sets are freed. */ | |
1826 | m->rules = set_free_with_destructor(m->rules, routing_policy_rule_free); | |
1827 | m->rules_foreign = set_free_with_destructor(m->rules_foreign, routing_policy_rule_free); | |
b921fcb2 | 1828 | set_free_with_destructor(m->rules_saved, routing_policy_rule_free); |
bce67bbe | 1829 | |
d4df6326 SS |
1830 | sd_netlink_unref(m->rtnl); |
1831 | sd_netlink_unref(m->genl); | |
1832 | sd_resolve_unref(m->resolve); | |
1833 | ||
a879e1a4 | 1834 | sd_event_source_unref(m->speed_meter_event_source); |
2f5b4a77 | 1835 | sd_event_unref(m->event); |
5fae368b | 1836 | |
d2ebf952 | 1837 | sd_device_monitor_unref(m->device_monitor); |
7d20d375 | 1838 | |
15761549 | 1839 | bus_verify_polkit_async_registry_free(m->polkit_registry); |
92e31da1 | 1840 | sd_bus_flush_close_unref(m->bus); |
7d20d375 | 1841 | |
7901cea1 MP |
1842 | free(m->dynamic_timezone); |
1843 | free(m->dynamic_hostname); | |
1844 | ||
5fae368b TG |
1845 | free(m); |
1846 | } | |
1847 | ||
b76d99d9 | 1848 | int manager_start(Manager *m) { |
84de38c5 TG |
1849 | Link *link; |
1850 | Iterator i; | |
a879e1a4 | 1851 | int r; |
84de38c5 | 1852 | |
a97dcc12 TG |
1853 | assert(m); |
1854 | ||
a879e1a4 YW |
1855 | r = manager_start_speed_meter(m); |
1856 | if (r < 0) | |
1857 | return log_error_errno(r, "Failed to initialize speed meter: %m"); | |
1858 | ||
84de38c5 TG |
1859 | /* The dirty handler will deal with future serialization, but the first one |
1860 | must be done explicitly. */ | |
1861 | ||
1862 | manager_save(m); | |
1863 | ||
1864 | HASHMAP_FOREACH(link, m->links, i) | |
1865 | link_save(link); | |
1866 | ||
b76d99d9 | 1867 | return 0; |
a97dcc12 TG |
1868 | } |
1869 | ||
5fae368b TG |
1870 | int manager_load_config(Manager *m) { |
1871 | int r; | |
1872 | ||
1873 | /* update timestamp */ | |
dc0d4078 | 1874 | paths_check_timestamp(NETWORK_DIRS, &m->network_dirs_ts_usec, true); |
5fae368b | 1875 | |
e272b621 | 1876 | r = netdev_load(m, false); |
f579559b TG |
1877 | if (r < 0) |
1878 | return r; | |
1879 | ||
7f06b3e1 | 1880 | r = network_load(m, &m->networks); |
9021bb9f TG |
1881 | if (r < 0) |
1882 | return r; | |
1883 | ||
f579559b TG |
1884 | return 0; |
1885 | } | |
f882c247 | 1886 | |
5fae368b | 1887 | bool manager_should_reload(Manager *m) { |
dc0d4078 | 1888 | return paths_check_timestamp(NETWORK_DIRS, &m->network_dirs_ts_usec, false); |
5fae368b TG |
1889 | } |
1890 | ||
1891 | int manager_rtnl_enumerate_links(Manager *m) { | |
4afd3348 | 1892 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
1c4baffc | 1893 | sd_netlink_message *link; |
f882c247 TG |
1894 | int r; |
1895 | ||
5da8149f | 1896 | assert(m); |
5fae368b | 1897 | assert(m->rtnl); |
5da8149f | 1898 | |
5fae368b | 1899 | r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0); |
f882c247 TG |
1900 | if (r < 0) |
1901 | return r; | |
1902 | ||
1c4baffc | 1903 | r = sd_netlink_message_request_dump(req, true); |
dd3efc09 TG |
1904 | if (r < 0) |
1905 | return r; | |
1906 | ||
1c4baffc | 1907 | r = sd_netlink_call(m->rtnl, req, 0, &reply); |
f2236469 TG |
1908 | if (r < 0) |
1909 | return r; | |
1910 | ||
1c4baffc | 1911 | for (link = reply; link; link = sd_netlink_message_next(link)) { |
5fae368b | 1912 | int k; |
2e9f08ea | 1913 | |
6a24f148 TG |
1914 | m->enumerating = true; |
1915 | ||
5fae368b TG |
1916 | k = manager_rtnl_process_link(m->rtnl, link, m); |
1917 | if (k < 0) | |
1918 | r = k; | |
6a24f148 TG |
1919 | |
1920 | m->enumerating = false; | |
5fae368b | 1921 | } |
2e9f08ea | 1922 | |
5fae368b | 1923 | return r; |
f882c247 | 1924 | } |
3bef724f | 1925 | |
5fae368b | 1926 | int manager_rtnl_enumerate_addresses(Manager *m) { |
4afd3348 | 1927 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
1c4baffc | 1928 | sd_netlink_message *addr; |
1346b1f0 TG |
1929 | int r; |
1930 | ||
5fae368b TG |
1931 | assert(m); |
1932 | assert(m->rtnl); | |
bcbca829 | 1933 | |
5fae368b TG |
1934 | r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, 0); |
1935 | if (r < 0) | |
1936 | return r; | |
bcbca829 | 1937 | |
1c4baffc | 1938 | r = sd_netlink_message_request_dump(req, true); |
1346b1f0 TG |
1939 | if (r < 0) |
1940 | return r; | |
1941 | ||
1c4baffc | 1942 | r = sd_netlink_call(m->rtnl, req, 0, &reply); |
5fae368b TG |
1943 | if (r < 0) |
1944 | return r; | |
1945 | ||
1c4baffc | 1946 | for (addr = reply; addr; addr = sd_netlink_message_next(addr)) { |
5fae368b TG |
1947 | int k; |
1948 | ||
6a24f148 TG |
1949 | m->enumerating = true; |
1950 | ||
200a0868 | 1951 | k = manager_rtnl_process_address(m->rtnl, addr, m); |
5fae368b TG |
1952 | if (k < 0) |
1953 | r = k; | |
6a24f148 TG |
1954 | |
1955 | m->enumerating = false; | |
5fae368b TG |
1956 | } |
1957 | ||
1958 | return r; | |
1346b1f0 | 1959 | } |
d1bdafd2 WKI |
1960 | |
1961 | int manager_rtnl_enumerate_neighbors(Manager *m) { | |
1962 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; | |
1963 | sd_netlink_message *neigh; | |
1964 | int r; | |
1965 | ||
1966 | assert(m); | |
1967 | assert(m->rtnl); | |
1968 | ||
1969 | r = sd_rtnl_message_new_neigh(m->rtnl, &req, RTM_GETNEIGH, 0, AF_UNSPEC); | |
1970 | if (r < 0) | |
1971 | return r; | |
1972 | ||
1973 | r = sd_netlink_message_request_dump(req, true); | |
1974 | if (r < 0) | |
1975 | return r; | |
1976 | ||
1977 | r = sd_netlink_call(m->rtnl, req, 0, &reply); | |
1978 | if (r < 0) | |
1979 | return r; | |
1980 | ||
1981 | for (neigh = reply; neigh; neigh = sd_netlink_message_next(neigh)) { | |
1982 | int k; | |
1983 | ||
1984 | m->enumerating = true; | |
1985 | ||
1986 | k = manager_rtnl_process_neighbor(m->rtnl, neigh, m); | |
1987 | if (k < 0) | |
1988 | r = k; | |
1989 | ||
1990 | m->enumerating = false; | |
1991 | } | |
1992 | ||
1993 | return r; | |
1994 | } | |
1346b1f0 | 1995 | |
1c8e710c | 1996 | int manager_rtnl_enumerate_routes(Manager *m) { |
4afd3348 | 1997 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
1c8e710c TG |
1998 | sd_netlink_message *route; |
1999 | int r; | |
2000 | ||
2001 | assert(m); | |
2002 | assert(m->rtnl); | |
2003 | ||
2004 | r = sd_rtnl_message_new_route(m->rtnl, &req, RTM_GETROUTE, 0, 0); | |
2005 | if (r < 0) | |
2006 | return r; | |
2007 | ||
2008 | r = sd_netlink_message_request_dump(req, true); | |
2009 | if (r < 0) | |
2010 | return r; | |
2011 | ||
2012 | r = sd_netlink_call(m->rtnl, req, 0, &reply); | |
2013 | if (r < 0) | |
2014 | return r; | |
2015 | ||
2016 | for (route = reply; route; route = sd_netlink_message_next(route)) { | |
2017 | int k; | |
2018 | ||
2019 | m->enumerating = true; | |
2020 | ||
2021 | k = manager_rtnl_process_route(m->rtnl, route, m); | |
2022 | if (k < 0) | |
2023 | r = k; | |
2024 | ||
2025 | m->enumerating = false; | |
2026 | } | |
2027 | ||
2028 | return r; | |
2029 | } | |
2030 | ||
bce67bbe SS |
2031 | int manager_rtnl_enumerate_rules(Manager *m) { |
2032 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; | |
2033 | sd_netlink_message *rule; | |
2034 | int r; | |
2035 | ||
2036 | assert(m); | |
2037 | assert(m->rtnl); | |
2038 | ||
2039 | r = sd_rtnl_message_new_routing_policy_rule(m->rtnl, &req, RTM_GETRULE, 0); | |
2040 | if (r < 0) | |
2041 | return r; | |
2042 | ||
2043 | r = sd_netlink_message_request_dump(req, true); | |
2044 | if (r < 0) | |
2045 | return r; | |
2046 | ||
2047 | r = sd_netlink_call(m->rtnl, req, 0, &reply); | |
6acbbdd4 SS |
2048 | if (r < 0) { |
2049 | if (r == -EOPNOTSUPP) { | |
2050 | log_debug("FIB Rules are not supported by the kernel. Ignoring."); | |
2051 | return 0; | |
2052 | } | |
2053 | ||
bce67bbe | 2054 | return r; |
6acbbdd4 | 2055 | } |
bce67bbe SS |
2056 | |
2057 | for (rule = reply; rule; rule = sd_netlink_message_next(rule)) { | |
2058 | int k; | |
2059 | ||
2060 | m->enumerating = true; | |
2061 | ||
2062 | k = manager_rtnl_process_rule(m->rtnl, rule, m); | |
2063 | if (k < 0) | |
2064 | r = k; | |
2065 | ||
2066 | m->enumerating = false; | |
2067 | } | |
2068 | ||
2069 | return r; | |
2070 | } | |
2071 | ||
c16c7808 SS |
2072 | int manager_rtnl_enumerate_nexthop(Manager *m) { |
2073 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; | |
2074 | sd_netlink_message *nexthop; | |
2075 | int r; | |
2076 | ||
2077 | assert(m); | |
2078 | assert(m->rtnl); | |
2079 | ||
2080 | r = sd_rtnl_message_new_nexthop(m->rtnl, &req, RTM_GETNEXTHOP, 0, 0); | |
2081 | if (r < 0) | |
2082 | return r; | |
2083 | ||
2084 | r = sd_netlink_message_request_dump(req, true); | |
2085 | if (r < 0) | |
2086 | return r; | |
2087 | ||
2088 | r = sd_netlink_call(m->rtnl, req, 0, &reply); | |
2089 | if (r < 0) { | |
2090 | if (r == -EOPNOTSUPP) { | |
2091 | log_debug("Nexthop are not supported by the kernel. Ignoring."); | |
2092 | return 0; | |
2093 | } | |
2094 | ||
2095 | return r; | |
2096 | } | |
2097 | ||
2098 | for (nexthop = reply; nexthop; nexthop = sd_netlink_message_next(nexthop)) { | |
2099 | int k; | |
2100 | ||
2101 | m->enumerating = true; | |
2102 | ||
2103 | k = manager_rtnl_process_nexthop(m->rtnl, nexthop, m); | |
2104 | if (k < 0) | |
2105 | r = k; | |
2106 | ||
2107 | m->enumerating = false; | |
2108 | } | |
2109 | ||
2110 | return r; | |
2111 | } | |
2112 | ||
0dd25fb9 | 2113 | int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found) { |
11bf3cce LP |
2114 | AddressPool *p; |
2115 | int r; | |
2116 | ||
2117 | assert(m); | |
2118 | assert(prefixlen > 0); | |
2119 | assert(found); | |
2120 | ||
2121 | LIST_FOREACH(address_pools, p, m->address_pools) { | |
2122 | if (p->family != family) | |
2123 | continue; | |
2124 | ||
2125 | r = address_pool_acquire(p, prefixlen, found); | |
2126 | if (r != 0) | |
2127 | return r; | |
2128 | } | |
2129 | ||
2130 | return 0; | |
2131 | } | |
4f5f911e LP |
2132 | |
2133 | Link* manager_find_uplink(Manager *m, Link *exclude) { | |
2134 | _cleanup_free_ struct local_address *gateways = NULL; | |
2135 | int n, i; | |
2136 | ||
2137 | assert(m); | |
2138 | ||
2139 | /* Looks for a suitable "uplink", via black magic: an | |
2140 | * interface that is up and where the default route with the | |
2141 | * highest priority points to. */ | |
2142 | ||
2143 | n = local_gateways(m->rtnl, 0, AF_UNSPEC, &gateways); | |
2144 | if (n < 0) { | |
2145 | log_warning_errno(n, "Failed to determine list of default gateways: %m"); | |
2146 | return NULL; | |
2147 | } | |
2148 | ||
2149 | for (i = 0; i < n; i++) { | |
2150 | Link *link; | |
2151 | ||
2152 | link = hashmap_get(m->links, INT_TO_PTR(gateways[i].ifindex)); | |
2153 | if (!link) { | |
c2c940bd | 2154 | log_debug("Weird, found a gateway for a link we don't know. Ignoring."); |
4f5f911e LP |
2155 | continue; |
2156 | } | |
2157 | ||
2158 | if (link == exclude) | |
2159 | continue; | |
2160 | ||
2161 | if (link->operstate < LINK_OPERSTATE_ROUTABLE) | |
2162 | continue; | |
2163 | ||
2164 | return link; | |
2165 | } | |
2166 | ||
2167 | return NULL; | |
2168 | } | |
84de38c5 TG |
2169 | |
2170 | void manager_dirty(Manager *manager) { | |
2171 | assert(manager); | |
2172 | ||
2173 | /* the serialized state in /run is no longer up-to-date */ | |
2174 | manager->dirty = true; | |
2175 | } | |
59eb33e0 MP |
2176 | |
2177 | static int set_hostname_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { | |
2178 | Manager *manager = userdata; | |
2179 | const sd_bus_error *e; | |
2180 | ||
2181 | assert(m); | |
2182 | assert(manager); | |
2183 | ||
2184 | e = sd_bus_message_get_error(m); | |
2185 | if (e) | |
2186 | log_warning_errno(sd_bus_error_get_errno(e), "Could not set hostname: %s", e->message); | |
2187 | ||
2188 | return 1; | |
2189 | } | |
2190 | ||
2191 | int manager_set_hostname(Manager *m, const char *hostname) { | |
2192 | int r; | |
2193 | ||
2194 | log_debug("Setting transient hostname: '%s'", strna(hostname)); | |
d7afd945 | 2195 | |
7901cea1 MP |
2196 | if (free_and_strdup(&m->dynamic_hostname, hostname) < 0) |
2197 | return log_oom(); | |
59eb33e0 | 2198 | |
d7afd945 | 2199 | if (!m->bus || sd_bus_is_ready(m->bus) <= 0) { |
e0d95f03 | 2200 | log_debug("Not connected to system bus, setting hostname later."); |
59eb33e0 MP |
2201 | return 0; |
2202 | } | |
2203 | ||
2204 | r = sd_bus_call_method_async( | |
2205 | m->bus, | |
2206 | NULL, | |
2207 | "org.freedesktop.hostname1", | |
2208 | "/org/freedesktop/hostname1", | |
2209 | "org.freedesktop.hostname1", | |
2210 | "SetHostname", | |
2211 | set_hostname_handler, | |
2212 | m, | |
2213 | "sb", | |
2214 | hostname, | |
2215 | false); | |
2216 | ||
2217 | if (r < 0) | |
2218 | return log_error_errno(r, "Could not set transient hostname: %m"); | |
2219 | ||
2220 | return 0; | |
2221 | } | |
2222 | ||
2223 | static int set_timezone_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { | |
2224 | Manager *manager = userdata; | |
2225 | const sd_bus_error *e; | |
2226 | ||
2227 | assert(m); | |
2228 | assert(manager); | |
2229 | ||
2230 | e = sd_bus_message_get_error(m); | |
2231 | if (e) | |
2232 | log_warning_errno(sd_bus_error_get_errno(e), "Could not set timezone: %s", e->message); | |
2233 | ||
2234 | return 1; | |
2235 | } | |
2236 | ||
2237 | int manager_set_timezone(Manager *m, const char *tz) { | |
2238 | int r; | |
2239 | ||
2240 | assert(m); | |
2241 | assert(tz); | |
2242 | ||
2243 | log_debug("Setting system timezone: '%s'", tz); | |
7901cea1 MP |
2244 | if (free_and_strdup(&m->dynamic_timezone, tz) < 0) |
2245 | return log_oom(); | |
59eb33e0 | 2246 | |
d7afd945 | 2247 | if (!m->bus || sd_bus_is_ready(m->bus) <= 0) { |
e0d95f03 | 2248 | log_debug("Not connected to system bus, setting timezone later."); |
59eb33e0 MP |
2249 | return 0; |
2250 | } | |
2251 | ||
2252 | r = sd_bus_call_method_async( | |
2253 | m->bus, | |
2254 | NULL, | |
2255 | "org.freedesktop.timedate1", | |
2256 | "/org/freedesktop/timedate1", | |
2257 | "org.freedesktop.timedate1", | |
2258 | "SetTimezone", | |
2259 | set_timezone_handler, | |
2260 | m, | |
2261 | "sb", | |
2262 | tz, | |
2263 | false); | |
2264 | if (r < 0) | |
2265 | return log_error_errno(r, "Could not set timezone: %m"); | |
2266 | ||
2267 | return 0; | |
2268 | } | |
27dfc982 YW |
2269 | |
2270 | int manager_request_product_uuid(Manager *m, Link *link) { | |
2271 | int r; | |
2272 | ||
2273 | assert(m); | |
2274 | ||
2275 | if (m->has_product_uuid) | |
2276 | return 0; | |
2277 | ||
2278 | log_debug("Requesting product UUID"); | |
2279 | ||
2280 | if (link) { | |
2281 | DUID *duid; | |
2282 | ||
2283 | assert_se(duid = link_get_duid(link)); | |
2284 | ||
2285 | r = set_ensure_allocated(&m->links_requesting_uuid, NULL); | |
2286 | if (r < 0) | |
2287 | return log_oom(); | |
2288 | ||
2289 | r = set_ensure_allocated(&m->duids_requesting_uuid, NULL); | |
2290 | if (r < 0) | |
2291 | return log_oom(); | |
2292 | ||
2293 | r = set_put(m->links_requesting_uuid, link); | |
2294 | if (r < 0) | |
2295 | return log_oom(); | |
2296 | ||
2297 | r = set_put(m->duids_requesting_uuid, duid); | |
2298 | if (r < 0) | |
2299 | return log_oom(); | |
3dc2e7af YW |
2300 | |
2301 | link_ref(link); | |
27dfc982 YW |
2302 | } |
2303 | ||
2304 | if (!m->bus || sd_bus_is_ready(m->bus) <= 0) { | |
2305 | log_debug("Not connected to system bus, requesting product UUID later."); | |
2306 | return 0; | |
2307 | } | |
2308 | ||
2309 | r = sd_bus_call_method_async( | |
2310 | m->bus, | |
2311 | NULL, | |
2312 | "org.freedesktop.hostname1", | |
2313 | "/org/freedesktop/hostname1", | |
2314 | "org.freedesktop.hostname1", | |
2315 | "GetProductUUID", | |
2316 | get_product_uuid_handler, | |
2317 | m, | |
2318 | "b", | |
2319 | false); | |
2320 | if (r < 0) | |
2321 | return log_warning_errno(r, "Failed to get product UUID: %m"); | |
2322 | ||
2323 | return 0; | |
2324 | } |