Debian Codegraph @

src/network/networkd-bridge-fdb.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /***
3 Copyright © 2014 Intel Corporation. All rights reserved.
4 ***/
5
6 #include "sd-netlink.h"
7
8 #include "alloc-util.h"
9 #include "conf-parser.h"
10 #include "hashmap.h"
11 #include "netlink-util.h"
12 #include "networkd-bridge-fdb.h"
13 #include "networkd-link.h"
14 #include "networkd-manager.h"
15 #include "networkd-network.h"
16 #include "networkd-queue.h"
17 #include "networkd-util.h"
18 #include "parse-util.h"
19 #include "socket-util.h"
20 #include "string-table.h"
21 #include "string-util.h"
22 #include "vlan-util.h"
23 #include "vxlan.h"
24
25 #define STATIC_BRIDGE_FDB_ENTRIES_PER_NETWORK_MAX 1024U
26
27 static BridgeFDB* bridge_fdb_free(BridgeFDB *fdb) {
28 if (!fdb)
29 return NULL;
30
31 if (fdb->network) {
32 assert(fdb->section);
33 hashmap_remove(fdb->network->bridge_fdb_entries_by_section, fdb->section);
34 }
35
36 config_section_free(fdb->section);
37
38 free(fdb->outgoing_ifname);
39 return mfree(fdb);
40 }
41
42 DEFINE_SECTION_CLEANUP_FUNCTIONS(BridgeFDB, bridge_fdb_free);
43
44 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
45 bridge_fdb_hash_ops_by_section,
46 ConfigSection, config_section_hash_func, config_section_compare_func,
47 BridgeFDB, bridge_fdb_free);
48
49 static int bridge_fdb_new_static(
50 Network *network,
51 const char *filename,
52 unsigned section_line,
53 BridgeFDB **ret) {
54
55 _cleanup_(config_section_freep) ConfigSection *n = NULL;
56 _cleanup_(bridge_fdb_freep) BridgeFDB *fdb = NULL;
57 int r;
58
59 assert(network);
60 assert(ret);
61 assert(filename);
62 assert(section_line > 0);
63
64 r = config_section_new(filename, section_line, &n);
65 if (r < 0)
66 return r;
67
68 /* search entry in hashmap first. */
69 fdb = hashmap_get(network->bridge_fdb_entries_by_section, n);
70 if (fdb) {
71 *ret = TAKE_PTR(fdb);
72 return 0;
73 }
74
75 if (hashmap_size(network->bridge_fdb_entries_by_section) >= STATIC_BRIDGE_FDB_ENTRIES_PER_NETWORK_MAX)
76 return -E2BIG;
77
78 /* allocate space for and FDB entry. */
79 fdb = new(BridgeFDB, 1);
80 if (!fdb)
81 return -ENOMEM;
82
83 /* init FDB structure. */
84 *fdb = (BridgeFDB) {
85 .network = network,
86 .section = TAKE_PTR(n),
87 .vni = VXLAN_VID_MAX + 1,
88 .ntf_flags = NEIGHBOR_CACHE_ENTRY_FLAGS_SELF,
89 };
90
91 r = hashmap_ensure_put(&network->bridge_fdb_entries_by_section, &bridge_fdb_hash_ops_by_section, fdb->section, fdb);
92 if (r < 0)
93 return r;
94
95 /* return allocated FDB structure. */
96 *ret = TAKE_PTR(fdb);
97 return 0;
98 }
99
100 static int bridge_fdb_configure_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, void *userdata) {
101 int r;
102
103 assert(m);
104 assert(link);
105
106 r = sd_netlink_message_get_errno(m);
107 if (r < 0 && r != -EEXIST) {
108 log_link_message_warning_errno(link, m, r, "Could not add bridge FDB entry");
109 link_enter_failed(link);
110 return 0;
111 }
112
113 if (link->static_bridge_fdb_messages == 0) {
114 log_link_debug(link, "Bridge FDB entries set");
115 link->static_bridge_fdb_configured = true;
116 link_check_ready(link);
117 }
118
119 return 0;
120 }
121
122 /* send a request to the kernel to add a FDB entry in its static MAC table. */
123 static int bridge_fdb_configure_message(const BridgeFDB *fdb, Link *link, sd_netlink_message *req) {
124 int r;
125
126 assert(fdb);
127 assert(link);
128
129 r = sd_rtnl_message_neigh_set_flags(req, fdb->ntf_flags);
130 if (r < 0)
131 return r;
132
133 /* only NUD_PERMANENT state supported. */
134 r = sd_rtnl_message_neigh_set_state(req, NUD_NOARP | NUD_PERMANENT);
135 if (r < 0)
136 return r;
137
138 r = sd_netlink_message_append_data(req, NDA_LLADDR, &fdb->mac_addr, sizeof(fdb->mac_addr));
139 if (r < 0)
140 return r;
141
142 /* VLAN ID is optional. We'll add VLAN ID only if it's specified. */
143 if (fdb->vlan_id > 0) {
144 r = sd_netlink_message_append_u16(req, NDA_VLAN, fdb->vlan_id);
145 if (r < 0)
146 return r;
147 }
148
149 if (fdb->outgoing_ifindex > 0) {
150 r = sd_netlink_message_append_u32(req, NDA_IFINDEX, fdb->outgoing_ifindex);
151 if (r < 0)
152 return r;
153 }
154
155 if (in_addr_is_set(fdb->family, &fdb->destination_addr)) {
156 r = netlink_message_append_in_addr_union(req, NDA_DST, fdb->family, &fdb->destination_addr);
157 if (r < 0)
158 return r;
159 }
160
161 if (fdb->vni <= VXLAN_VID_MAX) {
162 r = sd_netlink_message_append_u32(req, NDA_VNI, fdb->vni);
163 if (r < 0)
164 return r;
165 }
166
167 return 0;
168 }
169
170 static int bridge_fdb_configure(BridgeFDB *fdb, Link *link, Request *req) {
171 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
172 int r;
173
174 assert(fdb);
175 assert(link);
176 assert(link->manager);
177 assert(req);
178
179 r = sd_rtnl_message_new_neigh(link->manager->rtnl, &m, RTM_NEWNEIGH, link->ifindex, AF_BRIDGE);
180 if (r < 0)
181 return r;
182
183 r = bridge_fdb_configure_message(fdb, link, m);
184 if (r < 0)
185 return r;
186
187 return request_call_netlink_async(link->manager->rtnl, m, req);
188 }
189
190 static bool bridge_fdb_is_ready_to_configure(BridgeFDB *fdb, Link *link) {
191 Link *out = NULL;
192
193 assert(fdb);
194 assert(link);
195 assert(link->manager);
196
197 if (!link_is_ready_to_configure(link, false))
198 return false;
199
200 if (fdb->outgoing_ifname) {
201 if (link_get_by_name(link->manager, fdb->outgoing_ifname, &out) < 0)
202 return false;
203
204 fdb->outgoing_ifindex = out->ifindex;
205 } else if (fdb->outgoing_ifindex > 0) {
206 if (link_get_by_index(link->manager, fdb->outgoing_ifindex, &out) < 0)
207 return false;
208 }
209 if (out && !link_is_ready_to_configure(out, false))
210 return false;
211
212 return true;
213 }
214
215 static int bridge_fdb_process_request(Request *req, Link *link, void *userdata) {
216 BridgeFDB *fdb = ASSERT_PTR(userdata);
217 int r;
218
219 assert(req);
220 assert(link);
221
222 if (!bridge_fdb_is_ready_to_configure(fdb, link))
223 return 0;
224
225 r = bridge_fdb_configure(fdb, link, req);
226 if (r < 0)
227 return log_link_warning_errno(link, r, "Failed to configure bridge FDB: %m");
228
229 return 1;
230 }
231
232 int link_request_static_bridge_fdb(Link *link) {
233 BridgeFDB *fdb;
234 int r;
235
236 assert(link);
237 assert(link->network);
238
239 link->static_bridge_fdb_configured = false;
240
241 HASHMAP_FOREACH(fdb, link->network->bridge_fdb_entries_by_section) {
242 r = link_queue_request_full(link, REQUEST_TYPE_BRIDGE_FDB,
243 fdb, NULL,
244 trivial_hash_func,
245 trivial_compare_func,
246 bridge_fdb_process_request,
247 &link->static_bridge_fdb_messages,
248 bridge_fdb_configure_handler,
249 NULL);
250 if (r < 0)
251 return log_link_error_errno(link, r, "Failed to request static bridge FDB entry: %m");
252 }
253
254 if (link->static_bridge_fdb_messages == 0) {
255 link->static_bridge_fdb_configured = true;
256 link_check_ready(link);
257 } else {
258 log_link_debug(link, "Setting bridge FDB entries");
259 link_set_state(link, LINK_STATE_CONFIGURING);
260 }
261
262 return 0;
263 }
264
265 void network_drop_invalid_bridge_fdb_entries(Network *network) {
266 BridgeFDB *fdb;
267
268 assert(network);
269
270 HASHMAP_FOREACH(fdb, network->bridge_fdb_entries_by_section)
271 if (section_is_invalid(fdb->section))
272 bridge_fdb_free(fdb);
273 }
274
275 /* parse the HW address from config files. */
276 int config_parse_fdb_hwaddr(
277 const char *unit,
278 const char *filename,
279 unsigned line,
280 const char *section,
281 unsigned section_line,
282 const char *lvalue,
283 int ltype,
284 const char *rvalue,
285 void *data,
286 void *userdata) {
287
288 _cleanup_(bridge_fdb_free_or_set_invalidp) BridgeFDB *fdb = NULL;
289 Network *network = userdata;
290 int r;
291
292 assert(filename);
293 assert(section);
294 assert(lvalue);
295 assert(rvalue);
296 assert(data);
297
298 r = bridge_fdb_new_static(network, filename, section_line, &fdb);
299 if (r < 0)
300 return log_oom();
301
302 r = parse_ether_addr(rvalue, &fdb->mac_addr);
303 if (r < 0) {
304 log_syntax(unit, LOG_WARNING, filename, line, r, "Not a valid MAC address, ignoring assignment: %s", rvalue);
305 return 0;
306 }
307
308 TAKE_PTR(fdb);
309 return 0;
310 }
311
312 /* parse the VLAN Id from config files. */
313 int config_parse_fdb_vlan_id(
314 const char *unit,
315 const char *filename,
316 unsigned line,
317 const char *section,
318 unsigned section_line,
319 const char *lvalue,
320 int ltype,
321 const char *rvalue,
322 void *data,
323 void *userdata) {
324
325 _cleanup_(bridge_fdb_free_or_set_invalidp) BridgeFDB *fdb = NULL;
326 Network *network = userdata;
327 int r;
328
329 assert(filename);
330 assert(section);
331 assert(lvalue);
332 assert(rvalue);
333 assert(data);
334
335 r = bridge_fdb_new_static(network, filename, section_line, &fdb);
336 if (r < 0)
337 return log_oom();
338
339 r = config_parse_vlanid(unit, filename, line, section,
340 section_line, lvalue, ltype,
341 rvalue, &fdb->vlan_id, userdata);
342 if (r < 0)
343 return r;
344
345 TAKE_PTR(fdb);
346 return 0;
347 }
348
349 int config_parse_fdb_destination(
350 const char *unit,
351 const char *filename,
352 unsigned line,
353 const char *section,
354 unsigned section_line,
355 const char *lvalue,
356 int ltype,
357 const char *rvalue,
358 void *data,
359 void *userdata) {
360
361 _cleanup_(bridge_fdb_free_or_set_invalidp) BridgeFDB *fdb = NULL;
362 Network *network = userdata;
363 int r;
364
365 assert(filename);
366 assert(section);
367 assert(lvalue);
368 assert(rvalue);
369 assert(data);
370
371 r = bridge_fdb_new_static(network, filename, section_line, &fdb);
372 if (r < 0)
373 return log_oom();
374
375 r = in_addr_from_string_auto(rvalue, &fdb->family, &fdb->destination_addr);
376 if (r < 0) {
377 log_syntax(unit, LOG_WARNING, filename, line, r,
378 "FDB destination IP address is invalid, ignoring assignment: %s",
379 rvalue);
380 return 0;
381 }
382
383 TAKE_PTR(fdb);
384 return 0;
385 }
386
387 int config_parse_fdb_vxlan_vni(
388 const char *unit,
389 const char *filename,
390 unsigned line,
391 const char *section,
392 unsigned section_line,
393 const char *lvalue,
394 int ltype,
395 const char *rvalue,
396 void *data,
397 void *userdata) {
398
399 _cleanup_(bridge_fdb_free_or_set_invalidp) BridgeFDB *fdb = NULL;
400 Network *network = userdata;
401 uint32_t vni;
402 int r;
403
404 assert(filename);
405 assert(section);
406 assert(lvalue);
407 assert(rvalue);
408 assert(data);
409
410 r = bridge_fdb_new_static(network, filename, section_line, &fdb);
411 if (r < 0)
412 return log_oom();
413
414 r = safe_atou32(rvalue, &vni);
415 if (r < 0) {
416 log_syntax(unit, LOG_WARNING, filename, line, r,
417 "Failed to parse VXLAN Network Identifier (VNI), ignoring assignment: %s",
418 rvalue);
419 return 0;
420 }
421
422 if (vni > VXLAN_VID_MAX) {
423 log_syntax(unit, LOG_WARNING, filename, line, 0,
424 "FDB invalid VXLAN Network Identifier (VNI), ignoring assignment: %s",
425 rvalue);
426 return 0;
427 }
428
429 fdb->vni = vni;
430
431 TAKE_PTR(fdb);
432 return 0;
433 }
434
435 static const char* const ntf_flags_table[_NEIGHBOR_CACHE_ENTRY_FLAGS_MAX] = {
436 [NEIGHBOR_CACHE_ENTRY_FLAGS_USE] = "use",
437 [NEIGHBOR_CACHE_ENTRY_FLAGS_SELF] = "self",
438 [NEIGHBOR_CACHE_ENTRY_FLAGS_MASTER] = "master",
439 [NEIGHBOR_CACHE_ENTRY_FLAGS_ROUTER] = "router",
440 };
441
442 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(ntf_flags, NeighborCacheEntryFlags);
443
444 int config_parse_fdb_ntf_flags(
445 const char *unit,
446 const char *filename,
447 unsigned line,
448 const char *section,
449 unsigned section_line,
450 const char *lvalue,
451 int ltype,
452 const char *rvalue,
453 void *data,
454 void *userdata) {
455
456 _cleanup_(bridge_fdb_free_or_set_invalidp) BridgeFDB *fdb = NULL;
457 Network *network = userdata;
458 NeighborCacheEntryFlags f;
459 int r;
460
461 assert(filename);
462 assert(section);
463 assert(lvalue);
464 assert(rvalue);
465 assert(data);
466
467 r = bridge_fdb_new_static(network, filename, section_line, &fdb);
468 if (r < 0)
469 return log_oom();
470
471 f = ntf_flags_from_string(rvalue);
472 if (f < 0) {
473 log_syntax(unit, LOG_WARNING, filename, line, f,
474 "FDB failed to parse AssociatedWith=, ignoring assignment: %s",
475 rvalue);
476 return 0;
477 }
478
479 fdb->ntf_flags = f;
480
481 TAKE_PTR(fdb);
482 return 0;
483 }
484
485 int config_parse_fdb_interface(
486 const char *unit,
487 const char *filename,
488 unsigned line,
489 const char *section,
490 unsigned section_line,
491 const char *lvalue,
492 int ltype,
493 const char *rvalue,
494 void *data,
495 void *userdata) {
496
497 _cleanup_(bridge_fdb_free_or_set_invalidp) BridgeFDB *fdb = NULL;
498 Network *network = userdata;
499 int r;
500
501 assert(filename);
502 assert(section);
503 assert(lvalue);
504 assert(rvalue);
505 assert(data);
506
507 r = bridge_fdb_new_static(network, filename, section_line, &fdb);
508 if (r < 0)
509 return log_oom();
510
511 if (isempty(rvalue)) {
512 fdb->outgoing_ifname = mfree(fdb->outgoing_ifname);
513 fdb->outgoing_ifindex = 0;
514 TAKE_PTR(fdb);
515 return 0;
516 }
517
518 r = parse_ifindex(rvalue);
519 if (r > 0) {
520 fdb->outgoing_ifname = mfree(fdb->outgoing_ifname);
521 fdb->outgoing_ifindex = r;
522 TAKE_PTR(fdb);
523 return 0;
524 }
525
526 if (!ifname_valid_full(rvalue, IFNAME_VALID_ALTERNATIVE)) {
527 log_syntax(unit, LOG_WARNING, filename, line, 0,
528 "Invalid interface name in %s=, ignoring assignment: %s", lvalue, rvalue);
529 return 0;
530 }
531
532 r = free_and_strdup(&fdb->outgoing_ifname, rvalue);
533 if (r < 0)
534 return log_oom();
535 fdb->outgoing_ifindex = 0;
536
537 TAKE_PTR(fdb);
538 return 0;
539 }