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