1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 Copyright © 2014 Intel Corporation. All rights reserved.
6 #include <net/ethernet.h>
9 #include "alloc-util.h"
11 #include "netlink-util.h"
12 #include "networkd-fdb.h"
13 #include "networkd-link.h"
14 #include "networkd-manager.h"
15 #include "networkd-network.h"
16 #include "parse-util.h"
17 #include "string-table.h"
18 #include "vlan-util.h"
21 #define STATIC_FDB_ENTRIES_PER_NETWORK_MAX 1024U
23 /* remove and FDB entry. */
24 FdbEntry
*fdb_entry_free(FdbEntry
*fdb_entry
) {
28 if (fdb_entry
->network
) {
29 assert(fdb_entry
->section
);
30 hashmap_remove(fdb_entry
->network
->fdb_entries_by_section
, fdb_entry
->section
);
33 network_config_section_free(fdb_entry
->section
);
34 return mfree(fdb_entry
);
37 DEFINE_NETWORK_SECTION_FUNCTIONS(FdbEntry
, fdb_entry_free
);
39 /* create a new FDB entry or get an existing one. */
40 static int fdb_entry_new_static(
43 unsigned section_line
,
46 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
47 _cleanup_(fdb_entry_freep
) FdbEntry
*fdb_entry
= NULL
;
53 assert(section_line
> 0);
55 r
= network_config_section_new(filename
, section_line
, &n
);
59 /* search entry in hashmap first. */
60 fdb_entry
= hashmap_get(network
->fdb_entries_by_section
, n
);
62 *ret
= TAKE_PTR(fdb_entry
);
66 if (hashmap_size(network
->fdb_entries_by_section
) >= STATIC_FDB_ENTRIES_PER_NETWORK_MAX
)
69 /* allocate space for and FDB entry. */
70 fdb_entry
= new(FdbEntry
, 1);
74 /* init FDB structure. */
75 *fdb_entry
= (FdbEntry
) {
77 .section
= TAKE_PTR(n
),
78 .vni
= VXLAN_VID_MAX
+ 1,
79 .fdb_ntf_flags
= NEIGHBOR_CACHE_ENTRY_FLAGS_SELF
,
82 r
= hashmap_ensure_put(&network
->fdb_entries_by_section
, &network_config_hash_ops
, fdb_entry
->section
, fdb_entry
);
86 /* return allocated FDB structure. */
87 *ret
= TAKE_PTR(fdb_entry
);
92 static int set_fdb_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
97 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
100 r
= sd_netlink_message_get_errno(m
);
101 if (r
< 0 && r
!= -EEXIST
) {
102 log_link_message_warning_errno(link
, m
, r
, "Could not add FDB entry");
103 link_enter_failed(link
);
110 /* send a request to the kernel to add a FDB entry in its static MAC table. */
111 static int fdb_entry_configure(Link
*link
, FdbEntry
*fdb_entry
) {
112 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
116 assert(link
->network
);
117 assert(link
->manager
);
120 /* create new RTM message */
121 r
= sd_rtnl_message_new_neigh(link
->manager
->rtnl
, &req
, RTM_NEWNEIGH
, link
->ifindex
, AF_BRIDGE
);
123 return log_link_error_errno(link
, r
, "Could not create RTM_NEWNEIGH message: %m");
125 r
= sd_rtnl_message_neigh_set_flags(req
, fdb_entry
->fdb_ntf_flags
);
127 return log_link_error_errno(link
, r
, "Could not set neighbor flags: %m");
129 /* only NUD_PERMANENT state supported. */
130 r
= sd_rtnl_message_neigh_set_state(req
, NUD_NOARP
| NUD_PERMANENT
);
132 return log_link_error_errno(link
, r
, "Could not set neighbor state: %m");
134 r
= sd_netlink_message_append_data(req
, NDA_LLADDR
, &fdb_entry
->mac_addr
, sizeof(fdb_entry
->mac_addr
));
136 return log_link_error_errno(link
, r
, "Could not append NDA_LLADDR attribute: %m");
138 /* VLAN Id is optional. We'll add VLAN Id only if it's specified. */
139 if (fdb_entry
->vlan_id
> 0) {
140 r
= sd_netlink_message_append_u16(req
, NDA_VLAN
, fdb_entry
->vlan_id
);
142 return log_link_error_errno(link
, r
, "Could not append NDA_VLAN attribute: %m");
145 if (!in_addr_is_null(fdb_entry
->family
, &fdb_entry
->destination_addr
)) {
146 r
= netlink_message_append_in_addr_union(req
, NDA_DST
, fdb_entry
->family
, &fdb_entry
->destination_addr
);
148 return log_link_error_errno(link
, r
, "Could not append NDA_DST attribute: %m");
151 if (fdb_entry
->vni
<= VXLAN_VID_MAX
) {
152 r
= sd_netlink_message_append_u32(req
, NDA_VNI
, fdb_entry
->vni
);
154 return log_link_error_errno(link
, r
, "Could not append NDA_VNI attribute: %m");
157 /* send message to the kernel to update its internal static MAC table. */
158 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_fdb_handler
,
159 link_netlink_destroy_callback
, link
);
161 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
168 int link_set_bridge_fdb(Link
*link
) {
173 assert(link
->network
);
175 HASHMAP_FOREACH(fdb_entry
, link
->network
->fdb_entries_by_section
) {
176 r
= fdb_entry_configure(link
, fdb_entry
);
178 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
184 void network_drop_invalid_fdb_entries(Network
*network
) {
189 HASHMAP_FOREACH(fdb_entry
, network
->fdb_entries_by_section
)
190 if (section_is_invalid(fdb_entry
->section
))
191 fdb_entry_free(fdb_entry
);
194 /* parse the HW address from config files. */
195 int config_parse_fdb_hwaddr(
197 const char *filename
,
200 unsigned section_line
,
207 Network
*network
= userdata
;
208 _cleanup_(fdb_entry_free_or_set_invalidp
) FdbEntry
*fdb_entry
= NULL
;
217 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
221 r
= ether_addr_from_string(rvalue
, &fdb_entry
->mac_addr
);
223 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Not a valid MAC address, ignoring assignment: %s", rvalue
);
232 /* parse the VLAN Id from config files. */
233 int config_parse_fdb_vlan_id(
235 const char *filename
,
238 unsigned section_line
,
245 Network
*network
= userdata
;
246 _cleanup_(fdb_entry_free_or_set_invalidp
) FdbEntry
*fdb_entry
= NULL
;
255 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
259 r
= config_parse_vlanid(unit
, filename
, line
, section
,
260 section_line
, lvalue
, ltype
,
261 rvalue
, &fdb_entry
->vlan_id
, userdata
);
270 int config_parse_fdb_destination(
272 const char *filename
,
275 unsigned section_line
,
282 _cleanup_(fdb_entry_free_or_set_invalidp
) FdbEntry
*fdb_entry
= NULL
;
283 Network
*network
= userdata
;
292 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
296 r
= in_addr_from_string_auto(rvalue
, &fdb_entry
->family
, &fdb_entry
->destination_addr
);
298 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
299 "FDB destination IP address is invalid, ignoring assignment: %s",
309 int config_parse_fdb_vxlan_vni(
311 const char *filename
,
314 unsigned section_line
,
321 _cleanup_(fdb_entry_free_or_set_invalidp
) FdbEntry
*fdb_entry
= NULL
;
322 Network
*network
= userdata
;
332 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
336 r
= safe_atou32(rvalue
, &vni
);
338 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
339 "Failed to parse VXLAN Network Identifier (VNI), ignoring assignment: %s",
344 if (vni
> VXLAN_VID_MAX
) {
345 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
346 "FDB invalid VXLAN Network Identifier (VNI), ignoring assignment: %s",
351 fdb_entry
->vni
= vni
;
357 static const char* const fdb_ntf_flags_table
[_NEIGHBOR_CACHE_ENTRY_FLAGS_MAX
] = {
358 [NEIGHBOR_CACHE_ENTRY_FLAGS_USE
] = "use",
359 [NEIGHBOR_CACHE_ENTRY_FLAGS_SELF
] = "self",
360 [NEIGHBOR_CACHE_ENTRY_FLAGS_MASTER
] = "master",
361 [NEIGHBOR_CACHE_ENTRY_FLAGS_ROUTER
] = "router",
364 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(fdb_ntf_flags
, NeighborCacheEntryFlags
);
366 int config_parse_fdb_ntf_flags(
368 const char *filename
,
371 unsigned section_line
,
378 _cleanup_(fdb_entry_free_or_set_invalidp
) FdbEntry
*fdb_entry
= NULL
;
379 Network
*network
= userdata
;
380 NeighborCacheEntryFlags f
;
389 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
393 f
= fdb_ntf_flags_from_string(rvalue
);
395 log_syntax(unit
, LOG_WARNING
, filename
, line
, SYNTHETIC_ERRNO(EINVAL
),
396 "FDB failed to parse AssociatedWith=, ignoring assignment: %s",
401 fdb_entry
->fdb_ntf_flags
= f
;