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