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