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