1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Tom Gundersen <teg@jklm.no>
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "path-util.h"
#include "networkd.h"
#include "libudev-private.h"
#include "udev-util.h"
int manager_new(Manager **ret) {
_cleanup_manager_free_ Manager *m = NULL;
int r;
m = new0(Manager, 1);
if (!m)
return -ENOMEM;
r = sd_event_default(&m->event);
if (r < 0)
return r;
sd_event_set_watchdog(m->event, true);
r = sd_rtnl_open(RTMGRP_LINK | RTMGRP_IPV4_IFADDR, &m->rtnl);
if (r < 0)
return r;
m->udev = udev_new();
if (!m->udev)
return -ENOMEM;
m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
if (!m->udev_monitor)
return -ENOMEM;
m->links = hashmap_new(uint64_hash_func, uint64_compare_func);
if (!m->links)
return -ENOMEM;
m->bridges = hashmap_new(string_hash_func, string_compare_func);
if (!m->bridges)
return -ENOMEM;
LIST_HEAD_INIT(m->networks);
m->network_dirs = strv_new("/etc/systemd/network/",
"/run/systemd/network/",
"/usr/lib/systemd/network",
#ifdef HAVE_SPLIT_USER
"/lib/systemd/network",
#endif
NULL);
if (!m->network_dirs)
return -ENOMEM;
if (!path_strv_canonicalize_uniq(m->network_dirs))
return -ENOMEM;
*ret = m;
m = NULL;
return 0;
}
void manager_free(Manager *m) {
Network *network;
Bridge *bridge;
Link *link;
udev_monitor_unref(m->udev_monitor);
udev_unref(m->udev);
sd_event_source_unref(m->udev_event_source);
sd_event_unref(m->event);
while ((network = m->networks))
network_free(network);
while ((link = hashmap_first(m->links)))
link_free(link);
hashmap_free(m->links);
while ((bridge = hashmap_first(m->bridges)))
bridge_free(bridge);
hashmap_free(m->bridges);
strv_free(m->network_dirs);
sd_rtnl_unref(m->rtnl);
free(m);
}
int manager_load_config(Manager *m) {
int r;
/* update timestamp */
paths_check_timestamp(m->network_dirs, &m->network_dirs_ts_usec, true);
r = bridge_load(m);
if (r < 0)
return r;
r = network_load(m);
if (r < 0)
return r;
return 0;
}
bool manager_should_reload(Manager *m) {
return paths_check_timestamp(m->network_dirs, &m->network_dirs_ts_usec, false);
}
static int manager_process_link(Manager *m, struct udev_device *device) {
Link *link;
int r;
if (streq_ptr(udev_device_get_action(device), "remove")) {
uint64_t ifindex;
log_debug("%s: link removed", udev_device_get_sysname(device));
ifindex = udev_device_get_ifindex(device);
link = hashmap_get(m->links, &ifindex);
if (!link)
return 0;
link_free(link);
} else {
r = link_add(m, device);
if (r < 0) {
if (r == -EEXIST)
log_debug("%s: link already exists, ignoring",
udev_device_get_sysname(device));
else
log_error("%s: could not handle link: %s",
udev_device_get_sysname(device),
strerror(-r));
} else
log_debug("%s: link added", udev_device_get_sysname(device));
}
return 0;
}
int manager_udev_enumerate_links(Manager *m) {
_cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
struct udev_list_entry *item = NULL, *first = NULL;
int r;
assert(m);
e = udev_enumerate_new(m->udev);
if (!e)
return -ENOMEM;
r = udev_enumerate_add_match_subsystem(e, "net");
if (r < 0)
return r;
r = udev_enumerate_add_match_is_initialized(e);
if (r < 0)
return r;
r = udev_enumerate_scan_devices(e);
if (r < 0)
return r;
first = udev_enumerate_get_list_entry(e);
udev_list_entry_foreach(item, first) {
_cleanup_udev_device_unref_ struct udev_device *d = NULL;
int k;
d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item));
if (!d)
return -ENOMEM;
k = manager_process_link(m, d);
if (k < 0)
r = k;
}
return r;
}
static int manager_dispatch_link_udev(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
Manager *m = userdata;
struct udev_monitor *monitor = m->udev_monitor;
_cleanup_udev_device_unref_ struct udev_device *device = NULL;
device = udev_monitor_receive_device(monitor);
if (!device)
return -ENOMEM;
manager_process_link(m, device);
return 0;
}
int manager_udev_listen(Manager *m) {
int r;
r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_monitor, "net", NULL);
if (r < 0) {
log_error("Could not add udev monitor filter: %s", strerror(-r));
return r;
}
r = udev_monitor_enable_receiving(m->udev_monitor);
if (r < 0) {
log_error("Could not enable udev monitor");
return r;
}
r = sd_event_add_io(m->event,
udev_monitor_get_fd(m->udev_monitor),
EPOLLIN, manager_dispatch_link_udev,
m, &m->udev_event_source);
if (r < 0)
return r;
return 0;
}
static int manager_rtnl_process_link(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata) {
Manager *m = userdata;
Link *link;
int r, ifindex;
r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
if (r < 0) {
log_debug("received RTM_NEWLINK message without valid ifindex");
return 0;
}
link = hashmap_get(m->links, &ifindex);
if (!link) {
log_debug("received RTM_NEWLINK message for ifindex we are not tracking (%d)", ifindex);
return 0;
}
/* only track the status of links we want to manage */
if (link->network) {
r = link_update(link, message);
if (r < 0)
return 0;
} else
log_debug("received RTM_NEWLINK message for link we are not managing (%d)", ifindex);
return 1;
}
int manager_rtnl_listen(Manager *m) {
int r;
r = sd_rtnl_attach_event(m->rtnl, m->event, 0);
if (r < 0)
return r;
r = sd_rtnl_add_match(m->rtnl, RTM_NEWLINK, &manager_rtnl_process_link, m);
if (r < 0)
return r;
return 0;
}
|