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