]>
Commit | Line | Data |
---|---|---|
02b59d57 TG |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2013 Tom Gundersen <teg@jklm.no> | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
987efa17 TG |
20 | #include <net/if.h> |
21 | ||
b5efdb8a | 22 | #include "alloc-util.h" |
02b59d57 TG |
23 | #include "conf-files.h" |
24 | #include "conf-parser.h" | |
3ffd4af2 | 25 | #include "fd-util.h" |
02b59d57 | 26 | #include "list.h" |
fc2f9534 LP |
27 | #include "netlink-util.h" |
28 | #include "network-internal.h" | |
3ffd4af2 | 29 | #include "networkd-netdev.h" |
fc2f9534 | 30 | #include "networkd.h" |
07630cea | 31 | #include "siphash24.h" |
8fcde012 | 32 | #include "stat-util.h" |
8b43440b | 33 | #include "string-table.h" |
07630cea | 34 | #include "string-util.h" |
02b59d57 | 35 | |
3be1d7e0 | 36 | const NetDevVTable * const netdev_vtable[_NETDEV_KIND_MAX] = { |
fc2f9534 | 37 | |
3be1d7e0 TG |
38 | [NETDEV_KIND_BRIDGE] = &bridge_vtable, |
39 | [NETDEV_KIND_BOND] = &bond_vtable, | |
40 | [NETDEV_KIND_VLAN] = &vlan_vtable, | |
41 | [NETDEV_KIND_MACVLAN] = &macvlan_vtable, | |
f33ff02b | 42 | [NETDEV_KIND_MACVTAP] = &macvtap_vtable, |
c4a5ddc9 | 43 | [NETDEV_KIND_IPVLAN] = &ipvlan_vtable, |
3be1d7e0 TG |
44 | [NETDEV_KIND_VXLAN] = &vxlan_vtable, |
45 | [NETDEV_KIND_IPIP] = &ipip_vtable, | |
46 | [NETDEV_KIND_GRE] = &gre_vtable, | |
1af2536a | 47 | [NETDEV_KIND_GRETAP] = &gretap_vtable, |
b16492f8 SS |
48 | [NETDEV_KIND_IP6GRE] = &ip6gre_vtable, |
49 | [NETDEV_KIND_IP6GRETAP] = &ip6gretap_vtable, | |
3be1d7e0 TG |
50 | [NETDEV_KIND_SIT] = &sit_vtable, |
51 | [NETDEV_KIND_VTI] = &vti_vtable, | |
9011ce77 | 52 | [NETDEV_KIND_VTI6] = &vti6_vtable, |
3be1d7e0 TG |
53 | [NETDEV_KIND_VETH] = &veth_vtable, |
54 | [NETDEV_KIND_DUMMY] = &dummy_vtable, | |
55 | [NETDEV_KIND_TUN] = &tun_vtable, | |
56 | [NETDEV_KIND_TAP] = &tap_vtable, | |
855ee1a1 | 57 | [NETDEV_KIND_IP6TNL] = &ip6tnl_vtable, |
20897a0d | 58 | [NETDEV_KIND_VRF] = &vrf_vtable, |
92c918b0 | 59 | [NETDEV_KIND_VCAN] = &vcan_vtable, |
3be1d7e0 TG |
60 | }; |
61 | ||
2c5859af | 62 | static const char* const netdev_kind_table[_NETDEV_KIND_MAX] = { |
52433f6b | 63 | [NETDEV_KIND_BRIDGE] = "bridge", |
54abf461 TG |
64 | [NETDEV_KIND_BOND] = "bond", |
65 | [NETDEV_KIND_VLAN] = "vlan", | |
fe6b2d55 | 66 | [NETDEV_KIND_MACVLAN] = "macvlan", |
f33ff02b | 67 | [NETDEV_KIND_MACVTAP] = "macvtap", |
c4a5ddc9 | 68 | [NETDEV_KIND_IPVLAN] = "ipvlan", |
326cb406 | 69 | [NETDEV_KIND_VXLAN] = "vxlan", |
7951dea2 SS |
70 | [NETDEV_KIND_IPIP] = "ipip", |
71 | [NETDEV_KIND_GRE] = "gre", | |
1af2536a | 72 | [NETDEV_KIND_GRETAP] = "gretap", |
b16492f8 SS |
73 | [NETDEV_KIND_IP6GRE] = "ip6gre", |
74 | [NETDEV_KIND_IP6GRETAP] = "ip6gretap", | |
7951dea2 | 75 | [NETDEV_KIND_SIT] = "sit", |
10142d75 | 76 | [NETDEV_KIND_VETH] = "veth", |
9e358851 | 77 | [NETDEV_KIND_VTI] = "vti", |
9011ce77 | 78 | [NETDEV_KIND_VTI6] = "vti6", |
9e358851 | 79 | [NETDEV_KIND_DUMMY] = "dummy", |
30ae9dfd SS |
80 | [NETDEV_KIND_TUN] = "tun", |
81 | [NETDEV_KIND_TAP] = "tap", | |
855ee1a1 | 82 | [NETDEV_KIND_IP6TNL] = "ip6tnl", |
20897a0d | 83 | [NETDEV_KIND_VRF] = "vrf", |
92c918b0 | 84 | [NETDEV_KIND_VCAN] = "vcan", |
52433f6b | 85 | }; |
02b59d57 | 86 | |
1a436809 TG |
87 | DEFINE_STRING_TABLE_LOOKUP(netdev_kind, NetDevKind); |
88 | DEFINE_CONFIG_PARSE_ENUM(config_parse_netdev_kind, netdev_kind, NetDevKind, "Failed to parse netdev kind"); | |
52433f6b | 89 | |
59cb64e6 | 90 | static void netdev_cancel_callbacks(NetDev *netdev) { |
4afd3348 | 91 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
3f265037 | 92 | netdev_join_callback *callback; |
52433f6b TG |
93 | |
94 | if (!netdev) | |
02b59d57 TG |
95 | return; |
96 | ||
59cb64e6 TG |
97 | rtnl_message_new_synthetic_error(-ENODEV, 0, &m); |
98 | ||
52433f6b | 99 | while ((callback = netdev->callbacks)) { |
59cb64e6 TG |
100 | if (m) { |
101 | assert(callback->link); | |
102 | assert(callback->callback); | |
103 | assert(netdev->manager); | |
104 | assert(netdev->manager->rtnl); | |
105 | ||
b024a9cf | 106 | callback->callback(netdev->manager->rtnl, m, callback->link); |
59cb64e6 TG |
107 | } |
108 | ||
52433f6b | 109 | LIST_REMOVE(callbacks, netdev->callbacks, callback); |
62e2d5bb | 110 | link_unref(callback->link); |
02b59d57 TG |
111 | free(callback); |
112 | } | |
59cb64e6 TG |
113 | } |
114 | ||
115 | static void netdev_free(NetDev *netdev) { | |
116 | if (!netdev) | |
117 | return; | |
118 | ||
119 | netdev_cancel_callbacks(netdev); | |
02b59d57 | 120 | |
af4e9e2c TG |
121 | if (netdev->ifname) |
122 | hashmap_remove(netdev->manager->netdevs, netdev->ifname); | |
02b59d57 | 123 | |
52433f6b | 124 | free(netdev->filename); |
02b59d57 | 125 | |
52433f6b | 126 | free(netdev->description); |
af4e9e2c | 127 | free(netdev->ifname); |
96c90742 | 128 | free(netdev->mac); |
02b59d57 | 129 | |
79e16ce3 LP |
130 | condition_free_list(netdev->match_host); |
131 | condition_free_list(netdev->match_virt); | |
132 | condition_free_list(netdev->match_kernel); | |
133 | condition_free_list(netdev->match_arch); | |
134 | ||
aa9f1140 TG |
135 | if (NETDEV_VTABLE(netdev) && |
136 | NETDEV_VTABLE(netdev)->done) | |
137 | NETDEV_VTABLE(netdev)->done(netdev); | |
138 | ||
52433f6b | 139 | free(netdev); |
02b59d57 TG |
140 | } |
141 | ||
14b746f7 TG |
142 | NetDev *netdev_unref(NetDev *netdev) { |
143 | if (netdev && (-- netdev->n_ref <= 0)) | |
144 | netdev_free(netdev); | |
145 | ||
146 | return NULL; | |
147 | } | |
148 | ||
149 | NetDev *netdev_ref(NetDev *netdev) { | |
150 | if (netdev) | |
151 | assert_se(++ netdev->n_ref >= 2); | |
152 | ||
153 | return netdev; | |
154 | } | |
155 | ||
2cc7e981 TG |
156 | void netdev_drop(NetDev *netdev) { |
157 | if (!netdev || netdev->state == NETDEV_STATE_LINGER) | |
158 | return; | |
159 | ||
160 | netdev->state = NETDEV_STATE_LINGER; | |
161 | ||
79008bdd | 162 | log_netdev_debug(netdev, "netdev removed"); |
370e9930 | 163 | |
2cc7e981 TG |
164 | netdev_cancel_callbacks(netdev); |
165 | ||
166 | netdev_unref(netdev); | |
167 | ||
168 | return; | |
169 | } | |
170 | ||
1a436809 TG |
171 | int netdev_get(Manager *manager, const char *name, NetDev **ret) { |
172 | NetDev *netdev; | |
02b59d57 TG |
173 | |
174 | assert(manager); | |
175 | assert(name); | |
176 | assert(ret); | |
177 | ||
52433f6b TG |
178 | netdev = hashmap_get(manager->netdevs, name); |
179 | if (!netdev) { | |
02b59d57 TG |
180 | *ret = NULL; |
181 | return -ENOENT; | |
182 | } | |
183 | ||
52433f6b | 184 | *ret = netdev; |
02b59d57 TG |
185 | |
186 | return 0; | |
187 | } | |
188 | ||
1a436809 | 189 | static int netdev_enter_failed(NetDev *netdev) { |
52433f6b | 190 | netdev->state = NETDEV_STATE_FAILED; |
02b59d57 | 191 | |
62e2d5bb TG |
192 | netdev_cancel_callbacks(netdev); |
193 | ||
02b59d57 TG |
194 | return 0; |
195 | } | |
196 | ||
1c4baffc | 197 | static int netdev_enslave_ready(NetDev *netdev, Link* link, sd_netlink_message_handler_t callback) { |
4afd3348 | 198 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
02b59d57 TG |
199 | int r; |
200 | ||
52433f6b TG |
201 | assert(netdev); |
202 | assert(netdev->state == NETDEV_STATE_READY); | |
4fb7242c TG |
203 | assert(netdev->manager); |
204 | assert(netdev->manager->rtnl); | |
6cb955c6 | 205 | assert(IN_SET(netdev->kind, NETDEV_KIND_BRIDGE, NETDEV_KIND_BOND, NETDEV_KIND_VRF)); |
02b59d57 TG |
206 | assert(link); |
207 | assert(callback); | |
208 | ||
6a7a4e4d LP |
209 | r = sd_rtnl_message_new_link(netdev->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
210 | if (r < 0) | |
211 | return log_netdev_error_errno(netdev, r, "Could not allocate RTM_SETLINK message: %m"); | |
02b59d57 | 212 | |
1c4baffc | 213 | r = sd_netlink_message_append_u32(req, IFLA_MASTER, netdev->ifindex); |
6a7a4e4d LP |
214 | if (r < 0) |
215 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_MASTER attribute: %m"); | |
02b59d57 | 216 | |
1c4baffc | 217 | r = sd_netlink_call_async(netdev->manager->rtnl, req, callback, link, 0, NULL); |
6a7a4e4d LP |
218 | if (r < 0) |
219 | return log_netdev_error(netdev, "Could not send rtnetlink message: %m"); | |
02b59d57 | 220 | |
563c69c6 TG |
221 | link_ref(link); |
222 | ||
6a7a4e4d | 223 | log_netdev_debug(netdev, "Enslaving link '%s'", link->ifname); |
ab47d620 | 224 | |
02b59d57 TG |
225 | return 0; |
226 | } | |
227 | ||
1a436809 | 228 | static int netdev_enter_ready(NetDev *netdev) { |
3f265037 | 229 | netdev_join_callback *callback, *callback_next; |
ad0774e6 | 230 | int r; |
02b59d57 | 231 | |
52433f6b | 232 | assert(netdev); |
af4e9e2c | 233 | assert(netdev->ifname); |
924fe430 | 234 | |
ba5596ec TG |
235 | if (netdev->state != NETDEV_STATE_CREATING) |
236 | return 0; | |
237 | ||
52433f6b | 238 | netdev->state = NETDEV_STATE_READY; |
02b59d57 | 239 | |
98b32556 | 240 | log_netdev_info(netdev, "netdev ready"); |
02b59d57 | 241 | |
ad0774e6 | 242 | LIST_FOREACH_SAFE(callbacks, callback, callback_next, netdev->callbacks) { |
b226d99b | 243 | /* enslave the links that were attempted to be enslaved before the |
02b59d57 | 244 | * link was ready */ |
ad0774e6 TG |
245 | r = netdev_enslave_ready(netdev, callback->link, callback->callback); |
246 | if (r < 0) | |
247 | return r; | |
248 | ||
249 | LIST_REMOVE(callbacks, netdev->callbacks, callback); | |
250 | link_unref(callback->link); | |
251 | free(callback); | |
02b59d57 TG |
252 | } |
253 | ||
540eb5f0 SS |
254 | if (NETDEV_VTABLE(netdev)->post_create) |
255 | NETDEV_VTABLE(netdev)->post_create(netdev, NULL, NULL); | |
256 | ||
02b59d57 TG |
257 | return 0; |
258 | } | |
8469c1d3 | 259 | |
d5b3d845 | 260 | /* callback for netdev's created without a backing Link */ |
1c4baffc | 261 | static int netdev_create_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
8900367c | 262 | _cleanup_netdev_unref_ NetDev *netdev = userdata; |
172f6635 | 263 | int r; |
02b59d57 | 264 | |
52433f6b | 265 | assert(netdev->state != _NETDEV_STATE_INVALID); |
02b59d57 | 266 | |
1c4baffc | 267 | r = sd_netlink_message_get_errno(m); |
e09826dc | 268 | if (r == -EEXIST) |
98b32556 | 269 | log_netdev_info(netdev, "netdev exists, using existing without changing its parameters"); |
505f8da7 | 270 | else if (r < 0) { |
6a7a4e4d | 271 | log_netdev_warning_errno(netdev, r, "netdev could not be created: %m"); |
37ebeb77 | 272 | netdev_drop(netdev); |
dd3efc09 TG |
273 | |
274 | return 1; | |
02b59d57 TG |
275 | } |
276 | ||
6a7a4e4d | 277 | log_netdev_debug(netdev, "Created"); |
aa9f1140 | 278 | |
dd3efc09 | 279 | return 1; |
02b59d57 TG |
280 | } |
281 | ||
1c4baffc | 282 | int netdev_enslave(NetDev *netdev, Link *link, sd_netlink_message_handler_t callback) { |
b226d99b TG |
283 | int r; |
284 | ||
3f265037 | 285 | assert(netdev); |
62e2d5bb TG |
286 | assert(netdev->manager); |
287 | assert(netdev->manager->rtnl); | |
6cb955c6 | 288 | assert(IN_SET(netdev->kind, NETDEV_KIND_BRIDGE, NETDEV_KIND_BOND, NETDEV_KIND_VRF)); |
7951dea2 | 289 | |
52433f6b | 290 | if (netdev->state == NETDEV_STATE_READY) { |
b226d99b TG |
291 | r = netdev_enslave_ready(netdev, link, callback); |
292 | if (r < 0) | |
293 | return r; | |
62e2d5bb | 294 | } else if (IN_SET(netdev->state, NETDEV_STATE_LINGER, NETDEV_STATE_FAILED)) { |
4afd3348 | 295 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
62e2d5bb TG |
296 | |
297 | r = rtnl_message_new_synthetic_error(-ENODEV, 0, &m); | |
298 | if (r >= 0) | |
299 | callback(netdev->manager->rtnl, m, link); | |
02b59d57 | 300 | } else { |
d076c6f9 | 301 | /* the netdev is not yet read, save this request for when it is */ |
3f265037 | 302 | netdev_join_callback *cb; |
02b59d57 | 303 | |
3f265037 | 304 | cb = new0(netdev_join_callback, 1); |
02b59d57 TG |
305 | if (!cb) |
306 | return log_oom(); | |
307 | ||
308 | cb->callback = callback; | |
309 | cb->link = link; | |
563c69c6 | 310 | link_ref(link); |
02b59d57 | 311 | |
52433f6b | 312 | LIST_PREPEND(callbacks, netdev->callbacks, cb); |
3be1d7e0 | 313 | |
6a7a4e4d | 314 | log_netdev_debug(netdev, "Will enslave '%s', when ready", link->ifname); |
aa9f1140 | 315 | } |
3f265037 TG |
316 | |
317 | return 0; | |
318 | } | |
319 | ||
1c4baffc | 320 | int netdev_set_ifindex(NetDev *netdev, sd_netlink_message *message) { |
c3ebdce3 | 321 | uint16_t type; |
d39edfc7 | 322 | const char *kind; |
ca4e095a LP |
323 | const char *received_kind; |
324 | const char *received_name; | |
d39edfc7 TG |
325 | int r, ifindex; |
326 | ||
50add290 | 327 | assert(netdev); |
c3ebdce3 | 328 | assert(message); |
02b59d57 | 329 | |
1c4baffc | 330 | r = sd_netlink_message_get_type(message, &type); |
6a7a4e4d LP |
331 | if (r < 0) |
332 | return log_netdev_error_errno(netdev, r, "Could not get rtnl message type: %m"); | |
c3ebdce3 | 333 | |
ba5596ec | 334 | if (type != RTM_NEWLINK) { |
6a7a4e4d | 335 | log_netdev_error(netdev, "Cannot set ifindex from unexpected rtnl message type."); |
c3ebdce3 | 336 | return -EINVAL; |
ba5596ec | 337 | } |
d39edfc7 | 338 | |
a21df104 TG |
339 | r = sd_rtnl_message_link_get_ifindex(message, &ifindex); |
340 | if (r < 0) { | |
6a7a4e4d | 341 | log_netdev_error_errno(netdev, r, "Could not get ifindex: %m"); |
a21df104 TG |
342 | netdev_enter_failed(netdev); |
343 | return r; | |
344 | } else if (ifindex <= 0) { | |
79008bdd | 345 | log_netdev_error(netdev, "Got invalid ifindex: %d", ifindex); |
a21df104 | 346 | netdev_enter_failed(netdev); |
6a7a4e4d | 347 | return -EINVAL; |
a21df104 TG |
348 | } |
349 | ||
a21df104 TG |
350 | if (netdev->ifindex > 0) { |
351 | if (netdev->ifindex != ifindex) { | |
79008bdd | 352 | log_netdev_error(netdev, "Could not set ifindex to %d, already set to %d", |
a21df104 TG |
353 | ifindex, netdev->ifindex); |
354 | netdev_enter_failed(netdev); | |
355 | return -EEXIST; | |
356 | } else | |
357 | /* ifindex already set to the same for this netdev */ | |
358 | return 0; | |
359 | } | |
360 | ||
1c4baffc | 361 | r = sd_netlink_message_read_string(message, IFLA_IFNAME, &received_name); |
6a7a4e4d LP |
362 | if (r < 0) |
363 | return log_netdev_error_errno(netdev, r, "Could not get IFNAME: %m"); | |
c6315a7a | 364 | |
af4e9e2c | 365 | if (!streq(netdev->ifname, received_name)) { |
6a7a4e4d | 366 | log_netdev_error(netdev, "Received newlink with wrong IFNAME %s", received_name); |
c6315a7a TG |
367 | netdev_enter_failed(netdev); |
368 | return r; | |
369 | } | |
370 | ||
1c4baffc | 371 | r = sd_netlink_message_enter_container(message, IFLA_LINKINFO); |
6a7a4e4d LP |
372 | if (r < 0) |
373 | return log_netdev_error_errno(netdev, r, "Could not get LINKINFO: %m"); | |
d39edfc7 | 374 | |
1c4baffc | 375 | r = sd_netlink_message_read_string(message, IFLA_INFO_KIND, &received_kind); |
6a7a4e4d LP |
376 | if (r < 0) |
377 | return log_netdev_error_errno(netdev, r, "Could not get KIND: %m"); | |
d39edfc7 | 378 | |
1c4baffc | 379 | r = sd_netlink_message_exit_container(message); |
6a7a4e4d LP |
380 | if (r < 0) |
381 | return log_netdev_error_errno(netdev, r, "Could not exit container: %m"); | |
505f8da7 | 382 | |
30ae9dfd SS |
383 | if (netdev->kind == NETDEV_KIND_TAP) |
384 | /* the kernel does not distinguish between tun and tap */ | |
385 | kind = "tun"; | |
386 | else { | |
387 | kind = netdev_kind_to_string(netdev->kind); | |
388 | if (!kind) { | |
79008bdd | 389 | log_netdev_error(netdev, "Could not get kind"); |
30ae9dfd SS |
390 | netdev_enter_failed(netdev); |
391 | return -EINVAL; | |
392 | } | |
c3ebdce3 TG |
393 | } |
394 | ||
d39edfc7 | 395 | if (!streq(kind, received_kind)) { |
79008bdd | 396 | log_netdev_error(netdev, |
30ae9dfd | 397 | "Received newlink with wrong KIND %s, " |
c6315a7a | 398 | "expected %s", received_kind, kind); |
d39edfc7 TG |
399 | netdev_enter_failed(netdev); |
400 | return r; | |
401 | } | |
402 | ||
50add290 | 403 | netdev->ifindex = ifindex; |
52433f6b | 404 | |
79008bdd | 405 | log_netdev_debug(netdev, "netdev has index %d", netdev->ifindex); |
5261692f | 406 | |
52433f6b | 407 | netdev_enter_ready(netdev); |
02b59d57 TG |
408 | |
409 | return 0; | |
410 | } | |
411 | ||
5c8f858d TG |
412 | #define HASH_KEY SD_ID128_MAKE(52,e1,45,bd,00,6f,29,96,21,c6,30,6d,83,71,04,48) |
413 | ||
3be1d7e0 | 414 | int netdev_get_mac(const char *ifname, struct ether_addr **ret) { |
5c8f858d | 415 | _cleanup_free_ struct ether_addr *mac = NULL; |
dbe81cbd | 416 | uint64_t result; |
5c8f858d TG |
417 | size_t l, sz; |
418 | uint8_t *v; | |
419 | int r; | |
420 | ||
421 | assert(ifname); | |
422 | assert(ret); | |
423 | ||
424 | mac = new0(struct ether_addr, 1); | |
425 | if (!mac) | |
426 | return -ENOMEM; | |
427 | ||
428 | l = strlen(ifname); | |
429 | sz = sizeof(sd_id128_t) + l; | |
430 | v = alloca(sz); | |
431 | ||
432 | /* fetch some persistent data unique to the machine */ | |
433 | r = sd_id128_get_machine((sd_id128_t*) v); | |
434 | if (r < 0) | |
435 | return r; | |
436 | ||
437 | /* combine with some data unique (on this machine) to this | |
438 | * netdev */ | |
439 | memcpy(v + sizeof(sd_id128_t), ifname, l); | |
440 | ||
441 | /* Let's hash the host machine ID plus the container name. We | |
442 | * use a fixed, but originally randomly created hash key here. */ | |
933f9cae | 443 | result = siphash24(v, sz, HASH_KEY.bytes); |
5c8f858d TG |
444 | |
445 | assert_cc(ETH_ALEN <= sizeof(result)); | |
dbe81cbd | 446 | memcpy(mac->ether_addr_octet, &result, ETH_ALEN); |
5c8f858d TG |
447 | |
448 | /* see eth_random_addr in the kernel */ | |
449 | mac->ether_addr_octet[0] &= 0xfe; /* clear multicast bit */ | |
450 | mac->ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */ | |
451 | ||
452 | *ret = mac; | |
453 | mac = NULL; | |
454 | ||
455 | return 0; | |
456 | } | |
457 | ||
6c3e68e7 | 458 | static int netdev_create(NetDev *netdev, Link *link, |
1c4baffc | 459 | sd_netlink_message_handler_t callback) { |
aa9f1140 TG |
460 | int r; |
461 | ||
462 | assert(netdev); | |
6c3e68e7 | 463 | assert(!link || callback); |
aa9f1140 TG |
464 | |
465 | /* create netdev */ | |
466 | if (NETDEV_VTABLE(netdev)->create) { | |
6c3e68e7 TG |
467 | assert(!link); |
468 | ||
aa9f1140 TG |
469 | r = NETDEV_VTABLE(netdev)->create(netdev); |
470 | if (r < 0) | |
471 | return r; | |
472 | ||
6a7a4e4d | 473 | log_netdev_debug(netdev, "Created"); |
aa9f1140 | 474 | } else { |
4afd3348 | 475 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
aa9f1140 TG |
476 | |
477 | r = sd_rtnl_message_new_link(netdev->manager->rtnl, &m, RTM_NEWLINK, 0); | |
6a7a4e4d LP |
478 | if (r < 0) |
479 | return log_netdev_error_errno(netdev, r, "Could not allocate RTM_NEWLINK message: %m"); | |
aa9f1140 | 480 | |
1c4baffc | 481 | r = sd_netlink_message_append_string(m, IFLA_IFNAME, netdev->ifname); |
6a7a4e4d LP |
482 | if (r < 0) |
483 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_IFNAME, attribute: %m"); | |
aa9f1140 TG |
484 | |
485 | if (netdev->mac) { | |
1c4baffc | 486 | r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, netdev->mac); |
6a7a4e4d LP |
487 | if (r < 0) |
488 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_ADDRESS attribute: %m"); | |
aa9f1140 TG |
489 | } |
490 | ||
491 | if (netdev->mtu) { | |
1c4baffc | 492 | r = sd_netlink_message_append_u32(m, IFLA_MTU, netdev->mtu); |
6a7a4e4d LP |
493 | if (r < 0) |
494 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_MTU attribute: %m"); | |
aa9f1140 TG |
495 | } |
496 | ||
497 | if (link) { | |
1c4baffc | 498 | r = sd_netlink_message_append_u32(m, IFLA_LINK, link->ifindex); |
6a7a4e4d LP |
499 | if (r < 0) |
500 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_LINK attribute: %m"); | |
aa9f1140 TG |
501 | } |
502 | ||
1c4baffc | 503 | r = sd_netlink_message_open_container(m, IFLA_LINKINFO); |
6a7a4e4d LP |
504 | if (r < 0) |
505 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_LINKINFO attribute: %m"); | |
aa9f1140 | 506 | |
1c4baffc | 507 | r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, netdev_kind_to_string(netdev->kind)); |
6a7a4e4d LP |
508 | if (r < 0) |
509 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_INFO_DATA attribute: %m"); | |
aa9f1140 TG |
510 | |
511 | if (NETDEV_VTABLE(netdev)->fill_message_create) { | |
512 | r = NETDEV_VTABLE(netdev)->fill_message_create(netdev, link, m); | |
513 | if (r < 0) | |
514 | return r; | |
515 | } | |
516 | ||
1c4baffc | 517 | r = sd_netlink_message_close_container(m); |
6a7a4e4d LP |
518 | if (r < 0) |
519 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_LINKINFO attribute: %m"); | |
aa9f1140 | 520 | |
1c4baffc | 521 | r = sd_netlink_message_close_container(m); |
6a7a4e4d LP |
522 | if (r < 0) |
523 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_LINKINFO attribute: %m"); | |
aa9f1140 | 524 | |
6c3e68e7 | 525 | if (link) { |
1c4baffc | 526 | r = sd_netlink_call_async(netdev->manager->rtnl, m, callback, link, 0, NULL); |
6a7a4e4d LP |
527 | if (r < 0) |
528 | return log_netdev_error_errno(netdev, r, "Could not send rtnetlink message: %m"); | |
aa9f1140 | 529 | |
6c3e68e7 TG |
530 | link_ref(link); |
531 | } else { | |
1c4baffc | 532 | r = sd_netlink_call_async(netdev->manager->rtnl, m, netdev_create_handler, netdev, 0, NULL); |
6a7a4e4d LP |
533 | if (r < 0) |
534 | return log_netdev_error_errno(netdev, r, "Could not send rtnetlink message: %m"); | |
6c3e68e7 TG |
535 | |
536 | netdev_ref(netdev); | |
537 | } | |
aa9f1140 TG |
538 | |
539 | netdev->state = NETDEV_STATE_CREATING; | |
540 | ||
6a7a4e4d | 541 | log_netdev_debug(netdev, "Creating"); |
aa9f1140 TG |
542 | } |
543 | ||
544 | return 0; | |
545 | } | |
546 | ||
547 | /* the callback must be called, possibly after a timeout, as otherwise the Link will hang */ | |
1c4baffc | 548 | int netdev_join(NetDev *netdev, Link *link, sd_netlink_message_handler_t callback) { |
aa9f1140 TG |
549 | int r; |
550 | ||
551 | assert(netdev); | |
552 | assert(netdev->manager); | |
553 | assert(netdev->manager->rtnl); | |
554 | assert(NETDEV_VTABLE(netdev)); | |
555 | ||
556 | switch (NETDEV_VTABLE(netdev)->create_type) { | |
557 | case NETDEV_CREATE_MASTER: | |
558 | r = netdev_enslave(netdev, link, callback); | |
559 | if (r < 0) | |
560 | return r; | |
561 | ||
562 | break; | |
563 | case NETDEV_CREATE_STACKED: | |
6c3e68e7 | 564 | r = netdev_create(netdev, link, callback); |
aa9f1140 TG |
565 | if (r < 0) |
566 | return r; | |
567 | ||
568 | break; | |
569 | default: | |
570 | assert_not_reached("Can not join independent netdev"); | |
571 | } | |
572 | ||
573 | return 0; | |
574 | } | |
575 | ||
52433f6b | 576 | static int netdev_load_one(Manager *manager, const char *filename) { |
14b746f7 | 577 | _cleanup_netdev_unref_ NetDev *netdev = NULL; |
aa9f1140 | 578 | _cleanup_free_ NetDev *netdev_raw = NULL; |
02b59d57 TG |
579 | _cleanup_fclose_ FILE *file = NULL; |
580 | int r; | |
581 | ||
bf1bc670 TA |
582 | assert(manager); |
583 | assert(filename); | |
584 | ||
02b59d57 TG |
585 | file = fopen(filename, "re"); |
586 | if (!file) { | |
587 | if (errno == ENOENT) | |
588 | return 0; | |
589 | else | |
ecb08ec6 | 590 | return -errno; |
02b59d57 TG |
591 | } |
592 | ||
ed88bcfb ZJS |
593 | if (null_or_empty_fd(fileno(file))) { |
594 | log_debug("Skipping empty file: %s", filename); | |
595 | return 0; | |
596 | } | |
597 | ||
aa9f1140 TG |
598 | netdev_raw = new0(NetDev, 1); |
599 | if (!netdev_raw) | |
02b59d57 TG |
600 | return log_oom(); |
601 | ||
aa9f1140 | 602 | netdev_raw->kind = _NETDEV_KIND_INVALID; |
02b59d57 | 603 | |
326cb406 | 604 | r = config_parse(NULL, filename, file, |
aa9f1140 | 605 | "Match\0NetDev\0", |
e9f3d2d5 | 606 | config_item_perf_lookup, network_netdev_gperf_lookup, |
aa9f1140 | 607 | true, false, true, netdev_raw); |
36f822c4 | 608 | if (r < 0) |
02b59d57 | 609 | return r; |
02b59d57 | 610 | |
aa9f1140 TG |
611 | r = fseek(file, 0, SEEK_SET); |
612 | if (r < 0) | |
613 | return -errno; | |
614 | ||
2023dc8a TG |
615 | /* skip out early if configuration does not match the environment */ |
616 | if (net_match_config(NULL, NULL, NULL, NULL, NULL, | |
aa9f1140 TG |
617 | netdev_raw->match_host, netdev_raw->match_virt, |
618 | netdev_raw->match_kernel, netdev_raw->match_arch, | |
32bc8adc | 619 | NULL, NULL, NULL, NULL, NULL, NULL) <= 0) |
2023dc8a TG |
620 | return 0; |
621 | ||
2f27e2c5 | 622 | if (netdev_raw->kind == _NETDEV_KIND_INVALID) { |
e46eab86 | 623 | log_warning("NetDev with invalid Kind configured in %s. Ignoring", filename); |
52433f6b | 624 | return 0; |
3be1d7e0 | 625 | } |
2023dc8a | 626 | |
aa9f1140 | 627 | if (!netdev_raw->ifname) { |
1a436809 | 628 | log_warning("NetDev without Name configured in %s. Ignoring", filename); |
326cb406 SS |
629 | return 0; |
630 | } | |
631 | ||
aa9f1140 TG |
632 | netdev = malloc0(NETDEV_VTABLE(netdev_raw)->object_size); |
633 | if (!netdev) | |
634 | return log_oom(); | |
fe6b2d55 | 635 | |
aa9f1140 TG |
636 | netdev->n_ref = 1; |
637 | netdev->manager = manager; | |
638 | netdev->state = _NETDEV_STATE_INVALID; | |
639 | netdev->kind = netdev_raw->kind; | |
640 | netdev->ifname = netdev_raw->ifname; | |
326cb406 | 641 | |
aa9f1140 TG |
642 | if (NETDEV_VTABLE(netdev)->init) |
643 | NETDEV_VTABLE(netdev)->init(netdev); | |
644 | ||
645 | r = config_parse(NULL, filename, file, | |
646 | NETDEV_VTABLE(netdev)->sections, | |
647 | config_item_perf_lookup, network_netdev_gperf_lookup, | |
648 | false, false, false, netdev); | |
649 | if (r < 0) | |
650 | return r; | |
651 | ||
652 | /* verify configuration */ | |
653 | if (NETDEV_VTABLE(netdev)->config_verify) { | |
654 | r = NETDEV_VTABLE(netdev)->config_verify(netdev, filename); | |
655 | if (r < 0) | |
656 | return 0; | |
fe6b2d55 TG |
657 | } |
658 | ||
52433f6b TG |
659 | netdev->filename = strdup(filename); |
660 | if (!netdev->filename) | |
02b59d57 TG |
661 | return log_oom(); |
662 | ||
bc48c51f | 663 | if (!netdev->mac && netdev->kind != NETDEV_KIND_VLAN) { |
5c8f858d | 664 | r = netdev_get_mac(netdev->ifname, &netdev->mac); |
6a7a4e4d LP |
665 | if (r < 0) |
666 | return log_error_errno(r, "Failed to generate predictable MAC address for %s: %m", netdev->ifname); | |
5c8f858d TG |
667 | } |
668 | ||
af4e9e2c | 669 | r = hashmap_put(netdev->manager->netdevs, netdev->ifname, netdev); |
02b59d57 TG |
670 | if (r < 0) |
671 | return r; | |
672 | ||
52433f6b | 673 | LIST_HEAD_INIT(netdev->callbacks); |
02b59d57 | 674 | |
79008bdd | 675 | log_netdev_debug(netdev, "loaded %s", netdev_kind_to_string(netdev->kind)); |
3be1d7e0 | 676 | |
aa9f1140 TG |
677 | switch (NETDEV_VTABLE(netdev)->create_type) { |
678 | case NETDEV_CREATE_MASTER: | |
679 | case NETDEV_CREATE_INDEPENDENT: | |
6c3e68e7 | 680 | r = netdev_create(netdev, NULL, NULL); |
9e358851 | 681 | if (r < 0) |
1eb80875 | 682 | return 0; |
9e358851 | 683 | |
aa9f1140 TG |
684 | break; |
685 | default: | |
686 | break; | |
54abf461 | 687 | } |
02b59d57 | 688 | |
52433f6b | 689 | netdev = NULL; |
02b59d57 TG |
690 | |
691 | return 0; | |
692 | } | |
693 | ||
52433f6b | 694 | int netdev_load(Manager *manager) { |
6a7a4e4d | 695 | _cleanup_strv_free_ char **files = NULL; |
1a436809 | 696 | NetDev *netdev; |
6a7a4e4d | 697 | char **f; |
02b59d57 TG |
698 | int r; |
699 | ||
700 | assert(manager); | |
701 | ||
52433f6b | 702 | while ((netdev = hashmap_first(manager->netdevs))) |
14b746f7 | 703 | netdev_unref(netdev); |
02b59d57 | 704 | |
2ad8416d | 705 | r = conf_files_list_strv(&files, ".netdev", NULL, network_dirs); |
f647962d MS |
706 | if (r < 0) |
707 | return log_error_errno(r, "Failed to enumerate netdev files: %m"); | |
02b59d57 TG |
708 | |
709 | STRV_FOREACH_BACKWARDS(f, files) { | |
52433f6b | 710 | r = netdev_load_one(manager, *f); |
02b59d57 TG |
711 | if (r < 0) |
712 | return r; | |
713 | } | |
714 | ||
02b59d57 TG |
715 | return 0; |
716 | } |