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