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