]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
7de12ae7 TG |
2 | |
3 | #include <netinet/ether.h> | |
4 | #include <linux/if.h> | |
5d8689d7 | 5 | #include <fnmatch.h> |
7de12ae7 | 6 | |
b5efdb8a | 7 | #include "alloc-util.h" |
c5fcf6e4 TG |
8 | #include "link.h" |
9 | #include "manager.h" | |
1c4baffc | 10 | #include "netlink-util.h" |
7de12ae7 | 11 | #include "network-internal.h" |
51517f9e | 12 | #include "strv.h" |
e56cdb7a | 13 | #include "time-util.h" |
b5efdb8a | 14 | #include "util.h" |
7de12ae7 | 15 | |
79b1f37d TG |
16 | bool manager_ignore_link(Manager *m, Link *link) { |
17 | assert(m); | |
18 | assert(link); | |
19 | ||
be0c1370 | 20 | /* always ignore the loopback interface */ |
79b1f37d TG |
21 | if (link->flags & IFF_LOOPBACK) |
22 | return true; | |
23 | ||
be0c1370 TG |
24 | /* if interfaces are given on the command line, ignore all others */ |
25 | if (m->interfaces && !strv_contains(m->interfaces, link->ifname)) | |
26 | return true; | |
27 | ||
c1a38904 MTL |
28 | if (!link->required_for_online) |
29 | return true; | |
30 | ||
be0c1370 | 31 | /* ignore interfaces we explicitly are asked to ignore */ |
03fb3334 | 32 | return strv_fnmatch(m->ignore, link->ifname, 0); |
79b1f37d TG |
33 | } |
34 | ||
7de12ae7 TG |
35 | bool manager_all_configured(Manager *m) { |
36 | Iterator i; | |
37 | Link *l; | |
38 | char **ifname; | |
39 | bool one_ready = false; | |
40 | ||
3f85ef0f | 41 | /* wait for all the links given on the command line to appear */ |
7de12ae7 TG |
42 | STRV_FOREACH(ifname, m->interfaces) { |
43 | l = hashmap_get(m->links_by_name, *ifname); | |
44 | if (!l) { | |
45 | log_debug("still waiting for %s", *ifname); | |
46 | return false; | |
47 | } | |
48 | } | |
49 | ||
50 | /* wait for all links networkd manages to be in admin state 'configured' | |
51 | and at least one link to gain a carrier */ | |
52 | HASHMAP_FOREACH(l, m->links, i) { | |
79b1f37d TG |
53 | if (manager_ignore_link(m, l)) { |
54 | log_info("ignoring: %s", l->ifname); | |
7de12ae7 TG |
55 | continue; |
56 | } | |
57 | ||
58 | if (!l->state) { | |
59 | log_debug("link %s has not yet been processed by udev", | |
60 | l->ifname); | |
61 | return false; | |
62 | } | |
63 | ||
79ac8ba9 | 64 | if (STR_IN_SET(l->state, "configuring", "pending")) { |
7de12ae7 TG |
65 | log_debug("link %s is being processed by networkd", |
66 | l->ifname); | |
67 | return false; | |
68 | } | |
69 | ||
70 | if (l->operational_state && | |
71 | STR_IN_SET(l->operational_state, "degraded", "routable")) | |
72 | /* we wait for at least one link to be ready, | |
73 | regardless of who manages it */ | |
74 | one_ready = true; | |
75 | } | |
76 | ||
77 | return one_ready; | |
78 | } | |
79 | ||
1c4baffc | 80 | static int manager_process_link(sd_netlink *rtnl, sd_netlink_message *mm, void *userdata) { |
7de12ae7 TG |
81 | Manager *m = userdata; |
82 | uint16_t type; | |
83 | Link *l; | |
d2437732 | 84 | const char *ifname; |
7de12ae7 TG |
85 | int ifindex, r; |
86 | ||
87 | assert(rtnl); | |
88 | assert(m); | |
89 | assert(mm); | |
90 | ||
1c4baffc | 91 | r = sd_netlink_message_get_type(mm, &type); |
7de12ae7 TG |
92 | if (r < 0) |
93 | goto fail; | |
94 | ||
95 | r = sd_rtnl_message_link_get_ifindex(mm, &ifindex); | |
96 | if (r < 0) | |
97 | goto fail; | |
98 | ||
1c4baffc | 99 | r = sd_netlink_message_read_string(mm, IFLA_IFNAME, &ifname); |
7de12ae7 TG |
100 | if (r < 0) |
101 | goto fail; | |
102 | ||
103 | l = hashmap_get(m->links, INT_TO_PTR(ifindex)); | |
104 | ||
105 | switch (type) { | |
106 | ||
107 | case RTM_NEWLINK: | |
108 | if (!l) { | |
109 | log_debug("Found link %i", ifindex); | |
110 | ||
111 | r = link_new(m, &l, ifindex, ifname); | |
112 | if (r < 0) | |
113 | goto fail; | |
114 | ||
115 | r = link_update_monitor(l); | |
116 | if (r < 0) | |
117 | goto fail; | |
118 | } | |
119 | ||
120 | r = link_update_rtnl(l, mm); | |
121 | if (r < 0) | |
122 | goto fail; | |
123 | ||
124 | break; | |
125 | ||
126 | case RTM_DELLINK: | |
127 | if (l) { | |
128 | log_debug("Removing link %i", l->ifindex); | |
129 | link_free(l); | |
130 | } | |
131 | ||
132 | break; | |
133 | } | |
134 | ||
135 | return 0; | |
136 | ||
137 | fail: | |
da927ba9 | 138 | log_warning_errno(r, "Failed to process RTNL link message: %m"); |
7de12ae7 TG |
139 | return 0; |
140 | } | |
141 | ||
1c4baffc | 142 | static int on_rtnl_event(sd_netlink *rtnl, sd_netlink_message *mm, void *userdata) { |
7de12ae7 TG |
143 | Manager *m = userdata; |
144 | int r; | |
145 | ||
146 | r = manager_process_link(rtnl, mm, m); | |
147 | if (r < 0) | |
148 | return r; | |
149 | ||
150 | if (manager_all_configured(m)) | |
151 | sd_event_exit(m->event, 0); | |
152 | ||
153 | return 1; | |
154 | } | |
155 | ||
156 | static int manager_rtnl_listen(Manager *m) { | |
4afd3348 | 157 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
1c4baffc | 158 | sd_netlink_message *i; |
7de12ae7 TG |
159 | int r; |
160 | ||
161 | assert(m); | |
162 | ||
6fb2f9ab | 163 | /* First, subscribe to interfaces coming and going */ |
1c4baffc | 164 | r = sd_netlink_open(&m->rtnl); |
7de12ae7 TG |
165 | if (r < 0) |
166 | return r; | |
167 | ||
1c4baffc | 168 | r = sd_netlink_attach_event(m->rtnl, m->event, 0); |
7de12ae7 TG |
169 | if (r < 0) |
170 | return r; | |
171 | ||
8190a388 | 172 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWLINK, on_rtnl_event, NULL, m, "wait-online-on-NEWLINK"); |
7de12ae7 TG |
173 | if (r < 0) |
174 | return r; | |
175 | ||
8190a388 | 176 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELLINK, on_rtnl_event, NULL, m, "wait-online-on-DELLINK"); |
7de12ae7 TG |
177 | if (r < 0) |
178 | return r; | |
179 | ||
180 | /* Then, enumerate all links */ | |
181 | r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0); | |
182 | if (r < 0) | |
183 | return r; | |
184 | ||
1c4baffc | 185 | r = sd_netlink_message_request_dump(req, true); |
7de12ae7 TG |
186 | if (r < 0) |
187 | return r; | |
188 | ||
1c4baffc | 189 | r = sd_netlink_call(m->rtnl, req, 0, &reply); |
7de12ae7 TG |
190 | if (r < 0) |
191 | return r; | |
192 | ||
1c4baffc | 193 | for (i = reply; i; i = sd_netlink_message_next(i)) { |
7de12ae7 TG |
194 | r = manager_process_link(m->rtnl, i, m); |
195 | if (r < 0) | |
196 | return r; | |
197 | } | |
198 | ||
199 | return r; | |
200 | } | |
201 | ||
202 | static int on_network_event(sd_event_source *s, int fd, uint32_t revents, void *userdata) { | |
203 | Manager *m = userdata; | |
204 | Iterator i; | |
205 | Link *l; | |
206 | int r; | |
207 | ||
208 | assert(m); | |
209 | ||
210 | sd_network_monitor_flush(m->network_monitor); | |
211 | ||
212 | HASHMAP_FOREACH(l, m->links, i) { | |
213 | r = link_update_monitor(l); | |
214 | if (r < 0) | |
da927ba9 | 215 | log_warning_errno(r, "Failed to update monitor information for %i: %m", l->ifindex); |
7de12ae7 TG |
216 | } |
217 | ||
218 | if (manager_all_configured(m)) | |
219 | sd_event_exit(m->event, 0); | |
220 | ||
221 | return 0; | |
222 | } | |
223 | ||
224 | static int manager_network_monitor_listen(Manager *m) { | |
225 | int r, fd, events; | |
226 | ||
227 | assert(m); | |
228 | ||
229 | r = sd_network_monitor_new(&m->network_monitor, NULL); | |
230 | if (r < 0) | |
231 | return r; | |
232 | ||
233 | fd = sd_network_monitor_get_fd(m->network_monitor); | |
234 | if (fd < 0) | |
235 | return fd; | |
236 | ||
237 | events = sd_network_monitor_get_events(m->network_monitor); | |
238 | if (events < 0) | |
239 | return events; | |
240 | ||
241 | r = sd_event_add_io(m->event, &m->network_monitor_event_source, | |
242 | fd, events, &on_network_event, m); | |
243 | if (r < 0) | |
244 | return r; | |
245 | ||
246 | return 0; | |
247 | } | |
248 | ||
e56cdb7a | 249 | int manager_new(Manager **ret, char **interfaces, char **ignore, usec_t timeout) { |
7de12ae7 TG |
250 | _cleanup_(manager_freep) Manager *m = NULL; |
251 | int r; | |
252 | ||
253 | assert(ret); | |
254 | ||
255 | m = new0(Manager, 1); | |
256 | if (!m) | |
257 | return -ENOMEM; | |
258 | ||
259 | m->interfaces = interfaces; | |
79b1f37d | 260 | m->ignore = ignore; |
7de12ae7 TG |
261 | |
262 | r = sd_event_default(&m->event); | |
263 | if (r < 0) | |
264 | return r; | |
265 | ||
0b2c35b8 LP |
266 | (void) sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL); |
267 | (void) sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL); | |
7de12ae7 | 268 | |
e56cdb7a TG |
269 | if (timeout > 0) { |
270 | usec_t usec; | |
271 | ||
272 | usec = now(clock_boottime_or_monotonic()) + timeout; | |
273 | ||
274 | r = sd_event_add_time(m->event, NULL, clock_boottime_or_monotonic(), usec, 0, NULL, INT_TO_PTR(-ETIMEDOUT)); | |
275 | if (r < 0) | |
276 | return r; | |
277 | } | |
278 | ||
7de12ae7 TG |
279 | sd_event_set_watchdog(m->event, true); |
280 | ||
281 | r = manager_network_monitor_listen(m); | |
282 | if (r < 0) | |
283 | return r; | |
284 | ||
285 | r = manager_rtnl_listen(m); | |
286 | if (r < 0) | |
287 | return r; | |
288 | ||
1cc6c93a | 289 | *ret = TAKE_PTR(m); |
7de12ae7 TG |
290 | |
291 | return 0; | |
292 | } | |
293 | ||
294 | void manager_free(Manager *m) { | |
295 | Link *l; | |
296 | ||
297 | if (!m) | |
298 | return; | |
299 | ||
300 | while ((l = hashmap_first(m->links))) | |
301 | link_free(l); | |
302 | hashmap_free(m->links); | |
303 | hashmap_free(m->links_by_name); | |
304 | ||
305 | sd_event_source_unref(m->network_monitor_event_source); | |
306 | sd_network_monitor_unref(m->network_monitor); | |
307 | ||
308 | sd_event_source_unref(m->rtnl_event_source); | |
1c4baffc | 309 | sd_netlink_unref(m->rtnl); |
7de12ae7 TG |
310 | |
311 | sd_event_unref(m->event); | |
312 | free(m); | |
313 | ||
314 | return; | |
315 | } |