]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
02b59d57 TG |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2013 Tom Gundersen <teg@jklm.no> | |
02b59d57 TG |
6 | ***/ |
7 | ||
987efa17 TG |
8 | #include <net/if.h> |
9 | ||
b5efdb8a | 10 | #include "alloc-util.h" |
02b59d57 TG |
11 | #include "conf-files.h" |
12 | #include "conf-parser.h" | |
3ffd4af2 | 13 | #include "fd-util.h" |
02b59d57 | 14 | #include "list.h" |
fc2f9534 LP |
15 | #include "netlink-util.h" |
16 | #include "network-internal.h" | |
441e9ae4 | 17 | #include "netdev/netdev.h" |
23f53b99 | 18 | #include "networkd-manager.h" |
14b6bb77 | 19 | #include "networkd-link.h" |
07630cea | 20 | #include "siphash24.h" |
8fcde012 | 21 | #include "stat-util.h" |
8b43440b | 22 | #include "string-table.h" |
07630cea | 23 | #include "string-util.h" |
02b59d57 | 24 | |
23f53b99 TG |
25 | #include "netdev/bridge.h" |
26 | #include "netdev/bond.h" | |
6598e046 | 27 | #include "netdev/geneve.h" |
23f53b99 TG |
28 | #include "netdev/vlan.h" |
29 | #include "netdev/macvlan.h" | |
30 | #include "netdev/ipvlan.h" | |
31 | #include "netdev/vxlan.h" | |
32 | #include "netdev/tunnel.h" | |
33 | #include "netdev/tuntap.h" | |
34 | #include "netdev/veth.h" | |
35 | #include "netdev/dummy.h" | |
36 | #include "netdev/vrf.h" | |
37 | #include "netdev/vcan.h" | |
d6df583c | 38 | #include "netdev/vxcan.h" |
e5719363 | 39 | #include "netdev/wireguard.h" |
23f53b99 | 40 | |
3be1d7e0 TG |
41 | const NetDevVTable * const netdev_vtable[_NETDEV_KIND_MAX] = { |
42 | [NETDEV_KIND_BRIDGE] = &bridge_vtable, | |
43 | [NETDEV_KIND_BOND] = &bond_vtable, | |
44 | [NETDEV_KIND_VLAN] = &vlan_vtable, | |
45 | [NETDEV_KIND_MACVLAN] = &macvlan_vtable, | |
f33ff02b | 46 | [NETDEV_KIND_MACVTAP] = &macvtap_vtable, |
c4a5ddc9 | 47 | [NETDEV_KIND_IPVLAN] = &ipvlan_vtable, |
3be1d7e0 TG |
48 | [NETDEV_KIND_VXLAN] = &vxlan_vtable, |
49 | [NETDEV_KIND_IPIP] = &ipip_vtable, | |
50 | [NETDEV_KIND_GRE] = &gre_vtable, | |
1af2536a | 51 | [NETDEV_KIND_GRETAP] = &gretap_vtable, |
b16492f8 SS |
52 | [NETDEV_KIND_IP6GRE] = &ip6gre_vtable, |
53 | [NETDEV_KIND_IP6GRETAP] = &ip6gretap_vtable, | |
3be1d7e0 TG |
54 | [NETDEV_KIND_SIT] = &sit_vtable, |
55 | [NETDEV_KIND_VTI] = &vti_vtable, | |
9011ce77 | 56 | [NETDEV_KIND_VTI6] = &vti6_vtable, |
3be1d7e0 TG |
57 | [NETDEV_KIND_VETH] = &veth_vtable, |
58 | [NETDEV_KIND_DUMMY] = &dummy_vtable, | |
59 | [NETDEV_KIND_TUN] = &tun_vtable, | |
60 | [NETDEV_KIND_TAP] = &tap_vtable, | |
855ee1a1 | 61 | [NETDEV_KIND_IP6TNL] = &ip6tnl_vtable, |
20897a0d | 62 | [NETDEV_KIND_VRF] = &vrf_vtable, |
92c918b0 | 63 | [NETDEV_KIND_VCAN] = &vcan_vtable, |
6598e046 | 64 | [NETDEV_KIND_GENEVE] = &geneve_vtable, |
d6df583c | 65 | [NETDEV_KIND_VXCAN] = &vxcan_vtable, |
e5719363 | 66 | [NETDEV_KIND_WIREGUARD] = &wireguard_vtable, |
3be1d7e0 TG |
67 | }; |
68 | ||
2c5859af | 69 | static const char* const netdev_kind_table[_NETDEV_KIND_MAX] = { |
52433f6b | 70 | [NETDEV_KIND_BRIDGE] = "bridge", |
54abf461 TG |
71 | [NETDEV_KIND_BOND] = "bond", |
72 | [NETDEV_KIND_VLAN] = "vlan", | |
fe6b2d55 | 73 | [NETDEV_KIND_MACVLAN] = "macvlan", |
f33ff02b | 74 | [NETDEV_KIND_MACVTAP] = "macvtap", |
c4a5ddc9 | 75 | [NETDEV_KIND_IPVLAN] = "ipvlan", |
326cb406 | 76 | [NETDEV_KIND_VXLAN] = "vxlan", |
7951dea2 SS |
77 | [NETDEV_KIND_IPIP] = "ipip", |
78 | [NETDEV_KIND_GRE] = "gre", | |
1af2536a | 79 | [NETDEV_KIND_GRETAP] = "gretap", |
b16492f8 SS |
80 | [NETDEV_KIND_IP6GRE] = "ip6gre", |
81 | [NETDEV_KIND_IP6GRETAP] = "ip6gretap", | |
7951dea2 | 82 | [NETDEV_KIND_SIT] = "sit", |
10142d75 | 83 | [NETDEV_KIND_VETH] = "veth", |
9e358851 | 84 | [NETDEV_KIND_VTI] = "vti", |
9011ce77 | 85 | [NETDEV_KIND_VTI6] = "vti6", |
9e358851 | 86 | [NETDEV_KIND_DUMMY] = "dummy", |
30ae9dfd SS |
87 | [NETDEV_KIND_TUN] = "tun", |
88 | [NETDEV_KIND_TAP] = "tap", | |
855ee1a1 | 89 | [NETDEV_KIND_IP6TNL] = "ip6tnl", |
20897a0d | 90 | [NETDEV_KIND_VRF] = "vrf", |
92c918b0 | 91 | [NETDEV_KIND_VCAN] = "vcan", |
6598e046 | 92 | [NETDEV_KIND_GENEVE] = "geneve", |
d6df583c | 93 | [NETDEV_KIND_VXCAN] = "vxcan", |
e5719363 | 94 | [NETDEV_KIND_WIREGUARD] = "wireguard", |
52433f6b | 95 | }; |
02b59d57 | 96 | |
1a436809 TG |
97 | DEFINE_STRING_TABLE_LOOKUP(netdev_kind, NetDevKind); |
98 | DEFINE_CONFIG_PARSE_ENUM(config_parse_netdev_kind, netdev_kind, NetDevKind, "Failed to parse netdev kind"); | |
52433f6b | 99 | |
59cb64e6 | 100 | static void netdev_cancel_callbacks(NetDev *netdev) { |
4afd3348 | 101 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
3f265037 | 102 | netdev_join_callback *callback; |
52433f6b | 103 | |
22699541 | 104 | if (!netdev || !netdev->manager) |
02b59d57 TG |
105 | return; |
106 | ||
05d0c2e3 | 107 | rtnl_message_new_synthetic_error(netdev->manager->rtnl, -ENODEV, 0, &m); |
59cb64e6 | 108 | |
52433f6b | 109 | while ((callback = netdev->callbacks)) { |
59cb64e6 TG |
110 | if (m) { |
111 | assert(callback->link); | |
112 | assert(callback->callback); | |
113 | assert(netdev->manager); | |
114 | assert(netdev->manager->rtnl); | |
115 | ||
b024a9cf | 116 | callback->callback(netdev->manager->rtnl, m, callback->link); |
59cb64e6 TG |
117 | } |
118 | ||
52433f6b | 119 | LIST_REMOVE(callbacks, netdev->callbacks, callback); |
62e2d5bb | 120 | link_unref(callback->link); |
02b59d57 TG |
121 | free(callback); |
122 | } | |
59cb64e6 TG |
123 | } |
124 | ||
125 | static void netdev_free(NetDev *netdev) { | |
126 | if (!netdev) | |
127 | return; | |
128 | ||
129 | netdev_cancel_callbacks(netdev); | |
02b59d57 | 130 | |
281bb5c1 | 131 | if (netdev->ifname && netdev->manager) |
af4e9e2c | 132 | hashmap_remove(netdev->manager->netdevs, netdev->ifname); |
02b59d57 | 133 | |
52433f6b | 134 | free(netdev->filename); |
02b59d57 | 135 | |
52433f6b | 136 | free(netdev->description); |
af4e9e2c | 137 | free(netdev->ifname); |
96c90742 | 138 | free(netdev->mac); |
02b59d57 | 139 | |
79e16ce3 LP |
140 | condition_free_list(netdev->match_host); |
141 | condition_free_list(netdev->match_virt); | |
5022f08a LP |
142 | condition_free_list(netdev->match_kernel_cmdline); |
143 | condition_free_list(netdev->match_kernel_version); | |
79e16ce3 LP |
144 | condition_free_list(netdev->match_arch); |
145 | ||
f3c33b23 LP |
146 | /* Invoke the per-kind done() destructor, but only if the state field is initialized. We conditionalize that |
147 | * because we parse .netdev files twice: once to determine the kind (with a short, minimal NetDev structure | |
148 | * allocation, with no room for per-kind fields), and once to read the kind's properties (with a full, | |
149 | * comprehensive NetDev structure allocation with enough space for whatever the specific kind needs). Now, in | |
150 | * the first case we shouldn't try to destruct the per-kind NetDev fields on destruction, in the second case we | |
151 | * should. We use the state field to discern the two cases: it's _NETDEV_STATE_INVALID on the first "raw" | |
152 | * call. */ | |
153 | if (netdev->state != _NETDEV_STATE_INVALID && | |
154 | NETDEV_VTABLE(netdev) && | |
aa9f1140 TG |
155 | NETDEV_VTABLE(netdev)->done) |
156 | NETDEV_VTABLE(netdev)->done(netdev); | |
157 | ||
52433f6b | 158 | free(netdev); |
02b59d57 TG |
159 | } |
160 | ||
14b746f7 TG |
161 | NetDev *netdev_unref(NetDev *netdev) { |
162 | if (netdev && (-- netdev->n_ref <= 0)) | |
163 | netdev_free(netdev); | |
164 | ||
165 | return NULL; | |
166 | } | |
167 | ||
168 | NetDev *netdev_ref(NetDev *netdev) { | |
169 | if (netdev) | |
170 | assert_se(++ netdev->n_ref >= 2); | |
171 | ||
172 | return netdev; | |
173 | } | |
174 | ||
2cc7e981 TG |
175 | void netdev_drop(NetDev *netdev) { |
176 | if (!netdev || netdev->state == NETDEV_STATE_LINGER) | |
177 | return; | |
178 | ||
179 | netdev->state = NETDEV_STATE_LINGER; | |
180 | ||
79008bdd | 181 | log_netdev_debug(netdev, "netdev removed"); |
370e9930 | 182 | |
2cc7e981 TG |
183 | netdev_cancel_callbacks(netdev); |
184 | ||
185 | netdev_unref(netdev); | |
186 | ||
187 | return; | |
188 | } | |
189 | ||
1a436809 TG |
190 | int netdev_get(Manager *manager, const char *name, NetDev **ret) { |
191 | NetDev *netdev; | |
02b59d57 TG |
192 | |
193 | assert(manager); | |
194 | assert(name); | |
195 | assert(ret); | |
196 | ||
52433f6b TG |
197 | netdev = hashmap_get(manager->netdevs, name); |
198 | if (!netdev) { | |
02b59d57 TG |
199 | *ret = NULL; |
200 | return -ENOENT; | |
201 | } | |
202 | ||
52433f6b | 203 | *ret = netdev; |
02b59d57 TG |
204 | |
205 | return 0; | |
206 | } | |
207 | ||
1a436809 | 208 | static int netdev_enter_failed(NetDev *netdev) { |
52433f6b | 209 | netdev->state = NETDEV_STATE_FAILED; |
02b59d57 | 210 | |
62e2d5bb TG |
211 | netdev_cancel_callbacks(netdev); |
212 | ||
02b59d57 TG |
213 | return 0; |
214 | } | |
215 | ||
1c4baffc | 216 | static int netdev_enslave_ready(NetDev *netdev, Link* link, sd_netlink_message_handler_t callback) { |
4afd3348 | 217 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
02b59d57 TG |
218 | int r; |
219 | ||
52433f6b TG |
220 | assert(netdev); |
221 | assert(netdev->state == NETDEV_STATE_READY); | |
4fb7242c TG |
222 | assert(netdev->manager); |
223 | assert(netdev->manager->rtnl); | |
6cb955c6 | 224 | assert(IN_SET(netdev->kind, NETDEV_KIND_BRIDGE, NETDEV_KIND_BOND, NETDEV_KIND_VRF)); |
02b59d57 TG |
225 | assert(link); |
226 | assert(callback); | |
227 | ||
14b6bb77 RW |
228 | if (link->flags & IFF_UP) { |
229 | log_netdev_debug(netdev, "Link '%s' was up when attempting to enslave it. Bringing link down.", link->ifname); | |
230 | r = link_down(link); | |
231 | if (r < 0) | |
232 | return log_netdev_error_errno(netdev, r, "Could not bring link down: %m"); | |
233 | } | |
234 | ||
6a7a4e4d LP |
235 | r = sd_rtnl_message_new_link(netdev->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
236 | if (r < 0) | |
237 | return log_netdev_error_errno(netdev, r, "Could not allocate RTM_SETLINK message: %m"); | |
02b59d57 | 238 | |
1c4baffc | 239 | r = sd_netlink_message_append_u32(req, IFLA_MASTER, netdev->ifindex); |
6a7a4e4d LP |
240 | if (r < 0) |
241 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_MASTER attribute: %m"); | |
02b59d57 | 242 | |
1c4baffc | 243 | r = sd_netlink_call_async(netdev->manager->rtnl, req, callback, link, 0, NULL); |
6a7a4e4d LP |
244 | if (r < 0) |
245 | return log_netdev_error(netdev, "Could not send rtnetlink message: %m"); | |
02b59d57 | 246 | |
563c69c6 TG |
247 | link_ref(link); |
248 | ||
6a7a4e4d | 249 | log_netdev_debug(netdev, "Enslaving link '%s'", link->ifname); |
ab47d620 | 250 | |
02b59d57 TG |
251 | return 0; |
252 | } | |
253 | ||
1a436809 | 254 | static int netdev_enter_ready(NetDev *netdev) { |
3f265037 | 255 | netdev_join_callback *callback, *callback_next; |
ad0774e6 | 256 | int r; |
02b59d57 | 257 | |
52433f6b | 258 | assert(netdev); |
af4e9e2c | 259 | assert(netdev->ifname); |
924fe430 | 260 | |
ba5596ec TG |
261 | if (netdev->state != NETDEV_STATE_CREATING) |
262 | return 0; | |
263 | ||
52433f6b | 264 | netdev->state = NETDEV_STATE_READY; |
02b59d57 | 265 | |
98b32556 | 266 | log_netdev_info(netdev, "netdev ready"); |
02b59d57 | 267 | |
ad0774e6 | 268 | LIST_FOREACH_SAFE(callbacks, callback, callback_next, netdev->callbacks) { |
b226d99b | 269 | /* enslave the links that were attempted to be enslaved before the |
02b59d57 | 270 | * link was ready */ |
ad0774e6 TG |
271 | r = netdev_enslave_ready(netdev, callback->link, callback->callback); |
272 | if (r < 0) | |
273 | return r; | |
274 | ||
275 | LIST_REMOVE(callbacks, netdev->callbacks, callback); | |
276 | link_unref(callback->link); | |
277 | free(callback); | |
02b59d57 TG |
278 | } |
279 | ||
540eb5f0 SS |
280 | if (NETDEV_VTABLE(netdev)->post_create) |
281 | NETDEV_VTABLE(netdev)->post_create(netdev, NULL, NULL); | |
282 | ||
02b59d57 TG |
283 | return 0; |
284 | } | |
8469c1d3 | 285 | |
d5b3d845 | 286 | /* callback for netdev's created without a backing Link */ |
1c4baffc | 287 | static int netdev_create_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
8900367c | 288 | _cleanup_netdev_unref_ NetDev *netdev = userdata; |
172f6635 | 289 | int r; |
02b59d57 | 290 | |
52433f6b | 291 | assert(netdev->state != _NETDEV_STATE_INVALID); |
02b59d57 | 292 | |
1c4baffc | 293 | r = sd_netlink_message_get_errno(m); |
e09826dc | 294 | if (r == -EEXIST) |
98b32556 | 295 | log_netdev_info(netdev, "netdev exists, using existing without changing its parameters"); |
505f8da7 | 296 | else if (r < 0) { |
6a7a4e4d | 297 | log_netdev_warning_errno(netdev, r, "netdev could not be created: %m"); |
37ebeb77 | 298 | netdev_drop(netdev); |
dd3efc09 TG |
299 | |
300 | return 1; | |
02b59d57 TG |
301 | } |
302 | ||
6a7a4e4d | 303 | log_netdev_debug(netdev, "Created"); |
aa9f1140 | 304 | |
dd3efc09 | 305 | return 1; |
02b59d57 TG |
306 | } |
307 | ||
1c4baffc | 308 | int netdev_enslave(NetDev *netdev, Link *link, sd_netlink_message_handler_t callback) { |
b226d99b TG |
309 | int r; |
310 | ||
3f265037 | 311 | assert(netdev); |
62e2d5bb TG |
312 | assert(netdev->manager); |
313 | assert(netdev->manager->rtnl); | |
6cb955c6 | 314 | assert(IN_SET(netdev->kind, NETDEV_KIND_BRIDGE, NETDEV_KIND_BOND, NETDEV_KIND_VRF)); |
7951dea2 | 315 | |
52433f6b | 316 | if (netdev->state == NETDEV_STATE_READY) { |
b226d99b TG |
317 | r = netdev_enslave_ready(netdev, link, callback); |
318 | if (r < 0) | |
319 | return r; | |
62e2d5bb | 320 | } else if (IN_SET(netdev->state, NETDEV_STATE_LINGER, NETDEV_STATE_FAILED)) { |
4afd3348 | 321 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
62e2d5bb | 322 | |
05d0c2e3 | 323 | r = rtnl_message_new_synthetic_error(netdev->manager->rtnl, -ENODEV, 0, &m); |
62e2d5bb TG |
324 | if (r >= 0) |
325 | callback(netdev->manager->rtnl, m, link); | |
02b59d57 | 326 | } else { |
d076c6f9 | 327 | /* the netdev is not yet read, save this request for when it is */ |
3f265037 | 328 | netdev_join_callback *cb; |
02b59d57 | 329 | |
3f265037 | 330 | cb = new0(netdev_join_callback, 1); |
02b59d57 TG |
331 | if (!cb) |
332 | return log_oom(); | |
333 | ||
334 | cb->callback = callback; | |
335 | cb->link = link; | |
563c69c6 | 336 | link_ref(link); |
02b59d57 | 337 | |
52433f6b | 338 | LIST_PREPEND(callbacks, netdev->callbacks, cb); |
3be1d7e0 | 339 | |
6a7a4e4d | 340 | log_netdev_debug(netdev, "Will enslave '%s', when ready", link->ifname); |
aa9f1140 | 341 | } |
3f265037 TG |
342 | |
343 | return 0; | |
344 | } | |
345 | ||
1c4baffc | 346 | int netdev_set_ifindex(NetDev *netdev, sd_netlink_message *message) { |
c3ebdce3 | 347 | uint16_t type; |
d39edfc7 | 348 | const char *kind; |
ca4e095a LP |
349 | const char *received_kind; |
350 | const char *received_name; | |
d39edfc7 TG |
351 | int r, ifindex; |
352 | ||
50add290 | 353 | assert(netdev); |
c3ebdce3 | 354 | assert(message); |
02b59d57 | 355 | |
1c4baffc | 356 | r = sd_netlink_message_get_type(message, &type); |
6a7a4e4d LP |
357 | if (r < 0) |
358 | return log_netdev_error_errno(netdev, r, "Could not get rtnl message type: %m"); | |
c3ebdce3 | 359 | |
ba5596ec | 360 | if (type != RTM_NEWLINK) { |
6a7a4e4d | 361 | log_netdev_error(netdev, "Cannot set ifindex from unexpected rtnl message type."); |
c3ebdce3 | 362 | return -EINVAL; |
ba5596ec | 363 | } |
d39edfc7 | 364 | |
a21df104 TG |
365 | r = sd_rtnl_message_link_get_ifindex(message, &ifindex); |
366 | if (r < 0) { | |
6a7a4e4d | 367 | log_netdev_error_errno(netdev, r, "Could not get ifindex: %m"); |
a21df104 TG |
368 | netdev_enter_failed(netdev); |
369 | return r; | |
370 | } else if (ifindex <= 0) { | |
79008bdd | 371 | log_netdev_error(netdev, "Got invalid ifindex: %d", ifindex); |
a21df104 | 372 | netdev_enter_failed(netdev); |
6a7a4e4d | 373 | return -EINVAL; |
a21df104 TG |
374 | } |
375 | ||
a21df104 TG |
376 | if (netdev->ifindex > 0) { |
377 | if (netdev->ifindex != ifindex) { | |
79008bdd | 378 | log_netdev_error(netdev, "Could not set ifindex to %d, already set to %d", |
a21df104 TG |
379 | ifindex, netdev->ifindex); |
380 | netdev_enter_failed(netdev); | |
381 | return -EEXIST; | |
382 | } else | |
383 | /* ifindex already set to the same for this netdev */ | |
384 | return 0; | |
385 | } | |
386 | ||
1c4baffc | 387 | r = sd_netlink_message_read_string(message, IFLA_IFNAME, &received_name); |
6a7a4e4d LP |
388 | if (r < 0) |
389 | return log_netdev_error_errno(netdev, r, "Could not get IFNAME: %m"); | |
c6315a7a | 390 | |
af4e9e2c | 391 | if (!streq(netdev->ifname, received_name)) { |
6a7a4e4d | 392 | log_netdev_error(netdev, "Received newlink with wrong IFNAME %s", received_name); |
c6315a7a TG |
393 | netdev_enter_failed(netdev); |
394 | return r; | |
395 | } | |
396 | ||
1c4baffc | 397 | r = sd_netlink_message_enter_container(message, IFLA_LINKINFO); |
6a7a4e4d LP |
398 | if (r < 0) |
399 | return log_netdev_error_errno(netdev, r, "Could not get LINKINFO: %m"); | |
d39edfc7 | 400 | |
1c4baffc | 401 | r = sd_netlink_message_read_string(message, IFLA_INFO_KIND, &received_kind); |
6a7a4e4d LP |
402 | if (r < 0) |
403 | return log_netdev_error_errno(netdev, r, "Could not get KIND: %m"); | |
d39edfc7 | 404 | |
1c4baffc | 405 | r = sd_netlink_message_exit_container(message); |
6a7a4e4d LP |
406 | if (r < 0) |
407 | return log_netdev_error_errno(netdev, r, "Could not exit container: %m"); | |
505f8da7 | 408 | |
30ae9dfd SS |
409 | if (netdev->kind == NETDEV_KIND_TAP) |
410 | /* the kernel does not distinguish between tun and tap */ | |
411 | kind = "tun"; | |
412 | else { | |
413 | kind = netdev_kind_to_string(netdev->kind); | |
414 | if (!kind) { | |
79008bdd | 415 | log_netdev_error(netdev, "Could not get kind"); |
30ae9dfd SS |
416 | netdev_enter_failed(netdev); |
417 | return -EINVAL; | |
418 | } | |
c3ebdce3 TG |
419 | } |
420 | ||
d39edfc7 | 421 | if (!streq(kind, received_kind)) { |
79008bdd | 422 | log_netdev_error(netdev, |
30ae9dfd | 423 | "Received newlink with wrong KIND %s, " |
c6315a7a | 424 | "expected %s", received_kind, kind); |
d39edfc7 TG |
425 | netdev_enter_failed(netdev); |
426 | return r; | |
427 | } | |
428 | ||
50add290 | 429 | netdev->ifindex = ifindex; |
52433f6b | 430 | |
79008bdd | 431 | log_netdev_debug(netdev, "netdev has index %d", netdev->ifindex); |
5261692f | 432 | |
52433f6b | 433 | netdev_enter_ready(netdev); |
02b59d57 TG |
434 | |
435 | return 0; | |
436 | } | |
437 | ||
5c8f858d TG |
438 | #define HASH_KEY SD_ID128_MAKE(52,e1,45,bd,00,6f,29,96,21,c6,30,6d,83,71,04,48) |
439 | ||
3be1d7e0 | 440 | int netdev_get_mac(const char *ifname, struct ether_addr **ret) { |
5c8f858d | 441 | _cleanup_free_ struct ether_addr *mac = NULL; |
dbe81cbd | 442 | uint64_t result; |
5c8f858d TG |
443 | size_t l, sz; |
444 | uint8_t *v; | |
445 | int r; | |
446 | ||
447 | assert(ifname); | |
448 | assert(ret); | |
449 | ||
450 | mac = new0(struct ether_addr, 1); | |
451 | if (!mac) | |
452 | return -ENOMEM; | |
453 | ||
454 | l = strlen(ifname); | |
455 | sz = sizeof(sd_id128_t) + l; | |
456 | v = alloca(sz); | |
457 | ||
458 | /* fetch some persistent data unique to the machine */ | |
459 | r = sd_id128_get_machine((sd_id128_t*) v); | |
460 | if (r < 0) | |
461 | return r; | |
462 | ||
463 | /* combine with some data unique (on this machine) to this | |
464 | * netdev */ | |
465 | memcpy(v + sizeof(sd_id128_t), ifname, l); | |
466 | ||
467 | /* Let's hash the host machine ID plus the container name. We | |
468 | * use a fixed, but originally randomly created hash key here. */ | |
933f9cae | 469 | result = siphash24(v, sz, HASH_KEY.bytes); |
5c8f858d TG |
470 | |
471 | assert_cc(ETH_ALEN <= sizeof(result)); | |
dbe81cbd | 472 | memcpy(mac->ether_addr_octet, &result, ETH_ALEN); |
5c8f858d TG |
473 | |
474 | /* see eth_random_addr in the kernel */ | |
475 | mac->ether_addr_octet[0] &= 0xfe; /* clear multicast bit */ | |
476 | mac->ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */ | |
477 | ||
ae2a15bc | 478 | *ret = TAKE_PTR(mac); |
5c8f858d TG |
479 | |
480 | return 0; | |
481 | } | |
482 | ||
6c3e68e7 | 483 | static int netdev_create(NetDev *netdev, Link *link, |
1c4baffc | 484 | sd_netlink_message_handler_t callback) { |
aa9f1140 TG |
485 | int r; |
486 | ||
487 | assert(netdev); | |
6c3e68e7 | 488 | assert(!link || callback); |
aa9f1140 TG |
489 | |
490 | /* create netdev */ | |
491 | if (NETDEV_VTABLE(netdev)->create) { | |
6c3e68e7 TG |
492 | assert(!link); |
493 | ||
aa9f1140 TG |
494 | r = NETDEV_VTABLE(netdev)->create(netdev); |
495 | if (r < 0) | |
496 | return r; | |
497 | ||
6a7a4e4d | 498 | log_netdev_debug(netdev, "Created"); |
aa9f1140 | 499 | } else { |
4afd3348 | 500 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
aa9f1140 TG |
501 | |
502 | r = sd_rtnl_message_new_link(netdev->manager->rtnl, &m, RTM_NEWLINK, 0); | |
6a7a4e4d LP |
503 | if (r < 0) |
504 | return log_netdev_error_errno(netdev, r, "Could not allocate RTM_NEWLINK message: %m"); | |
aa9f1140 | 505 | |
1c4baffc | 506 | r = sd_netlink_message_append_string(m, IFLA_IFNAME, netdev->ifname); |
6a7a4e4d LP |
507 | if (r < 0) |
508 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_IFNAME, attribute: %m"); | |
aa9f1140 TG |
509 | |
510 | if (netdev->mac) { | |
1c4baffc | 511 | r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, netdev->mac); |
6a7a4e4d LP |
512 | if (r < 0) |
513 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_ADDRESS attribute: %m"); | |
aa9f1140 TG |
514 | } |
515 | ||
516 | if (netdev->mtu) { | |
1c4baffc | 517 | r = sd_netlink_message_append_u32(m, IFLA_MTU, netdev->mtu); |
6a7a4e4d LP |
518 | if (r < 0) |
519 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_MTU attribute: %m"); | |
aa9f1140 TG |
520 | } |
521 | ||
522 | if (link) { | |
1c4baffc | 523 | r = sd_netlink_message_append_u32(m, IFLA_LINK, link->ifindex); |
6a7a4e4d LP |
524 | if (r < 0) |
525 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_LINK attribute: %m"); | |
aa9f1140 TG |
526 | } |
527 | ||
1c4baffc | 528 | r = sd_netlink_message_open_container(m, IFLA_LINKINFO); |
6a7a4e4d LP |
529 | if (r < 0) |
530 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_LINKINFO attribute: %m"); | |
aa9f1140 | 531 | |
1c4baffc | 532 | r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, netdev_kind_to_string(netdev->kind)); |
6a7a4e4d LP |
533 | if (r < 0) |
534 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_INFO_DATA attribute: %m"); | |
aa9f1140 TG |
535 | |
536 | if (NETDEV_VTABLE(netdev)->fill_message_create) { | |
537 | r = NETDEV_VTABLE(netdev)->fill_message_create(netdev, link, m); | |
538 | if (r < 0) | |
539 | return r; | |
540 | } | |
541 | ||
1c4baffc | 542 | r = sd_netlink_message_close_container(m); |
6a7a4e4d | 543 | if (r < 0) |
9b53e129 | 544 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_INFO_DATA attribute: %m"); |
aa9f1140 | 545 | |
1c4baffc | 546 | r = sd_netlink_message_close_container(m); |
6a7a4e4d LP |
547 | if (r < 0) |
548 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_LINKINFO attribute: %m"); | |
aa9f1140 | 549 | |
6c3e68e7 | 550 | if (link) { |
1c4baffc | 551 | r = sd_netlink_call_async(netdev->manager->rtnl, m, callback, link, 0, NULL); |
6a7a4e4d LP |
552 | if (r < 0) |
553 | return log_netdev_error_errno(netdev, r, "Could not send rtnetlink message: %m"); | |
aa9f1140 | 554 | |
6c3e68e7 TG |
555 | link_ref(link); |
556 | } else { | |
1c4baffc | 557 | r = sd_netlink_call_async(netdev->manager->rtnl, m, netdev_create_handler, netdev, 0, NULL); |
6a7a4e4d LP |
558 | if (r < 0) |
559 | return log_netdev_error_errno(netdev, r, "Could not send rtnetlink message: %m"); | |
6c3e68e7 TG |
560 | |
561 | netdev_ref(netdev); | |
562 | } | |
aa9f1140 TG |
563 | |
564 | netdev->state = NETDEV_STATE_CREATING; | |
565 | ||
6a7a4e4d | 566 | log_netdev_debug(netdev, "Creating"); |
aa9f1140 TG |
567 | } |
568 | ||
569 | return 0; | |
570 | } | |
571 | ||
572 | /* the callback must be called, possibly after a timeout, as otherwise the Link will hang */ | |
1c4baffc | 573 | int netdev_join(NetDev *netdev, Link *link, sd_netlink_message_handler_t callback) { |
aa9f1140 TG |
574 | int r; |
575 | ||
576 | assert(netdev); | |
577 | assert(netdev->manager); | |
578 | assert(netdev->manager->rtnl); | |
579 | assert(NETDEV_VTABLE(netdev)); | |
580 | ||
581 | switch (NETDEV_VTABLE(netdev)->create_type) { | |
582 | case NETDEV_CREATE_MASTER: | |
583 | r = netdev_enslave(netdev, link, callback); | |
584 | if (r < 0) | |
585 | return r; | |
586 | ||
587 | break; | |
588 | case NETDEV_CREATE_STACKED: | |
6c3e68e7 | 589 | r = netdev_create(netdev, link, callback); |
aa9f1140 TG |
590 | if (r < 0) |
591 | return r; | |
592 | ||
593 | break; | |
594 | default: | |
595 | assert_not_reached("Can not join independent netdev"); | |
596 | } | |
597 | ||
598 | return 0; | |
599 | } | |
600 | ||
52433f6b | 601 | static int netdev_load_one(Manager *manager, const char *filename) { |
281bb5c1 | 602 | _cleanup_netdev_unref_ NetDev *netdev_raw = NULL, *netdev = NULL; |
02b59d57 | 603 | _cleanup_fclose_ FILE *file = NULL; |
2cc34d5b | 604 | const char *dropin_dirname; |
4d7fa6de | 605 | bool independent = false; |
02b59d57 TG |
606 | int r; |
607 | ||
bf1bc670 TA |
608 | assert(manager); |
609 | assert(filename); | |
610 | ||
02b59d57 TG |
611 | file = fopen(filename, "re"); |
612 | if (!file) { | |
613 | if (errno == ENOENT) | |
614 | return 0; | |
f3c33b23 LP |
615 | |
616 | return -errno; | |
02b59d57 TG |
617 | } |
618 | ||
ed88bcfb ZJS |
619 | if (null_or_empty_fd(fileno(file))) { |
620 | log_debug("Skipping empty file: %s", filename); | |
621 | return 0; | |
622 | } | |
623 | ||
aa9f1140 TG |
624 | netdev_raw = new0(NetDev, 1); |
625 | if (!netdev_raw) | |
02b59d57 TG |
626 | return log_oom(); |
627 | ||
281bb5c1 | 628 | netdev_raw->n_ref = 1; |
aa9f1140 | 629 | netdev_raw->kind = _NETDEV_KIND_INVALID; |
f3c33b23 | 630 | netdev_raw->state = _NETDEV_STATE_INVALID; /* an invalid state means done() of the implementation won't be called on destruction */ |
02b59d57 | 631 | |
281bb5c1 | 632 | dropin_dirname = strjoina(basename(filename), ".d"); |
2cc34d5b ZJS |
633 | r = config_parse_many(filename, network_dirs, dropin_dirname, |
634 | "Match\0NetDev\0", | |
635 | config_item_perf_lookup, network_netdev_gperf_lookup, | |
5360d771 | 636 | CONFIG_PARSE_WARN|CONFIG_PARSE_RELAXED, netdev_raw); |
36f822c4 | 637 | if (r < 0) |
02b59d57 | 638 | return r; |
02b59d57 | 639 | |
2023dc8a TG |
640 | /* skip out early if configuration does not match the environment */ |
641 | if (net_match_config(NULL, NULL, NULL, NULL, NULL, | |
aa9f1140 | 642 | netdev_raw->match_host, netdev_raw->match_virt, |
5022f08a LP |
643 | netdev_raw->match_kernel_cmdline, netdev_raw->match_kernel_version, |
644 | netdev_raw->match_arch, | |
32bc8adc | 645 | NULL, NULL, NULL, NULL, NULL, NULL) <= 0) |
2023dc8a TG |
646 | return 0; |
647 | ||
2f27e2c5 | 648 | if (netdev_raw->kind == _NETDEV_KIND_INVALID) { |
881e6b5e | 649 | log_warning("NetDev has no Kind configured in %s. Ignoring", filename); |
52433f6b | 650 | return 0; |
3be1d7e0 | 651 | } |
2023dc8a | 652 | |
aa9f1140 | 653 | if (!netdev_raw->ifname) { |
1a436809 | 654 | log_warning("NetDev without Name configured in %s. Ignoring", filename); |
326cb406 SS |
655 | return 0; |
656 | } | |
657 | ||
281bb5c1 ZJS |
658 | r = fseek(file, 0, SEEK_SET); |
659 | if (r < 0) | |
660 | return -errno; | |
661 | ||
aa9f1140 TG |
662 | netdev = malloc0(NETDEV_VTABLE(netdev_raw)->object_size); |
663 | if (!netdev) | |
664 | return log_oom(); | |
fe6b2d55 | 665 | |
aa9f1140 TG |
666 | netdev->n_ref = 1; |
667 | netdev->manager = manager; | |
aa9f1140 | 668 | netdev->kind = netdev_raw->kind; |
f3c33b23 | 669 | netdev->state = NETDEV_STATE_LOADING; /* we initialize the state here for the first time, so that done() will be called on destruction */ |
326cb406 | 670 | |
aa9f1140 TG |
671 | if (NETDEV_VTABLE(netdev)->init) |
672 | NETDEV_VTABLE(netdev)->init(netdev); | |
673 | ||
674 | r = config_parse(NULL, filename, file, | |
675 | NETDEV_VTABLE(netdev)->sections, | |
676 | config_item_perf_lookup, network_netdev_gperf_lookup, | |
bcde742e | 677 | CONFIG_PARSE_WARN, netdev); |
aa9f1140 TG |
678 | if (r < 0) |
679 | return r; | |
680 | ||
681 | /* verify configuration */ | |
682 | if (NETDEV_VTABLE(netdev)->config_verify) { | |
683 | r = NETDEV_VTABLE(netdev)->config_verify(netdev, filename); | |
684 | if (r < 0) | |
685 | return 0; | |
fe6b2d55 TG |
686 | } |
687 | ||
52433f6b TG |
688 | netdev->filename = strdup(filename); |
689 | if (!netdev->filename) | |
02b59d57 TG |
690 | return log_oom(); |
691 | ||
bc48c51f | 692 | if (!netdev->mac && netdev->kind != NETDEV_KIND_VLAN) { |
5c8f858d | 693 | r = netdev_get_mac(netdev->ifname, &netdev->mac); |
6a7a4e4d LP |
694 | if (r < 0) |
695 | return log_error_errno(r, "Failed to generate predictable MAC address for %s: %m", netdev->ifname); | |
5c8f858d TG |
696 | } |
697 | ||
af4e9e2c | 698 | r = hashmap_put(netdev->manager->netdevs, netdev->ifname, netdev); |
02b59d57 TG |
699 | if (r < 0) |
700 | return r; | |
701 | ||
52433f6b | 702 | LIST_HEAD_INIT(netdev->callbacks); |
02b59d57 | 703 | |
79008bdd | 704 | log_netdev_debug(netdev, "loaded %s", netdev_kind_to_string(netdev->kind)); |
3be1d7e0 | 705 | |
aa9f1140 TG |
706 | switch (NETDEV_VTABLE(netdev)->create_type) { |
707 | case NETDEV_CREATE_MASTER: | |
708 | case NETDEV_CREATE_INDEPENDENT: | |
6c3e68e7 | 709 | r = netdev_create(netdev, NULL, NULL); |
9e358851 | 710 | if (r < 0) |
4d7fa6de SS |
711 | return r; |
712 | ||
713 | break; | |
714 | default: | |
715 | break; | |
716 | } | |
9e358851 | 717 | |
4d7fa6de SS |
718 | switch (netdev->kind) { |
719 | case NETDEV_KIND_IPIP: | |
720 | independent = IPIP(netdev)->independent; | |
721 | break; | |
722 | case NETDEV_KIND_GRE: | |
723 | independent = GRE(netdev)->independent; | |
724 | break; | |
725 | case NETDEV_KIND_GRETAP: | |
726 | independent = GRETAP(netdev)->independent; | |
727 | break; | |
728 | case NETDEV_KIND_IP6GRE: | |
729 | independent = IP6GRE(netdev)->independent; | |
730 | break; | |
731 | case NETDEV_KIND_IP6GRETAP: | |
732 | independent = IP6GRETAP(netdev)->independent; | |
733 | break; | |
734 | case NETDEV_KIND_SIT: | |
735 | independent = SIT(netdev)->independent; | |
736 | break; | |
737 | case NETDEV_KIND_VTI: | |
738 | independent = VTI(netdev)->independent; | |
739 | break; | |
740 | case NETDEV_KIND_VTI6: | |
741 | independent = VTI6(netdev)->independent; | |
742 | break; | |
743 | case NETDEV_KIND_IP6TNL: | |
744 | independent = IP6TNL(netdev)->independent; | |
aa9f1140 TG |
745 | break; |
746 | default: | |
747 | break; | |
54abf461 | 748 | } |
02b59d57 | 749 | |
4d7fa6de SS |
750 | if (independent) { |
751 | r = netdev_create(netdev, NULL, NULL); | |
752 | if (r < 0) | |
753 | return r; | |
754 | } | |
755 | ||
52433f6b | 756 | netdev = NULL; |
02b59d57 TG |
757 | |
758 | return 0; | |
759 | } | |
760 | ||
52433f6b | 761 | int netdev_load(Manager *manager) { |
6a7a4e4d | 762 | _cleanup_strv_free_ char **files = NULL; |
1a436809 | 763 | NetDev *netdev; |
6a7a4e4d | 764 | char **f; |
02b59d57 TG |
765 | int r; |
766 | ||
767 | assert(manager); | |
768 | ||
52433f6b | 769 | while ((netdev = hashmap_first(manager->netdevs))) |
14b746f7 | 770 | netdev_unref(netdev); |
02b59d57 | 771 | |
b5084605 | 772 | r = conf_files_list_strv(&files, ".netdev", NULL, 0, network_dirs); |
f647962d MS |
773 | if (r < 0) |
774 | return log_error_errno(r, "Failed to enumerate netdev files: %m"); | |
02b59d57 TG |
775 | |
776 | STRV_FOREACH_BACKWARDS(f, files) { | |
52433f6b | 777 | r = netdev_load_one(manager, *f); |
02b59d57 TG |
778 | if (r < 0) |
779 | return r; | |
780 | } | |
781 | ||
02b59d57 TG |
782 | return 0; |
783 | } |