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