1 /* SPDX-License-Identifier: LGPL-2.1+ */
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_allocated(&network
->fdb_entries_by_section
, &network_config_hash_ops
);
86 r
= hashmap_put(network
->fdb_entries_by_section
, fdb_entry
->section
, fdb_entry
);
90 /* return allocated FDB structure. */
91 *ret
= TAKE_PTR(fdb_entry
);
96 static int set_fdb_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
101 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
104 r
= sd_netlink_message_get_errno(m
);
105 if (r
< 0 && r
!= -EEXIST
) {
106 log_link_message_warning_errno(link
, m
, r
, "Could not add FDB entry");
107 link_enter_failed(link
);
114 /* send a request to the kernel to add a FDB entry in its static MAC table. */
115 static int fdb_entry_configure(Link
*link
, FdbEntry
*fdb_entry
) {
116 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
120 assert(link
->network
);
121 assert(link
->manager
);
124 /* create new RTM message */
125 r
= sd_rtnl_message_new_neigh(link
->manager
->rtnl
, &req
, RTM_NEWNEIGH
, link
->ifindex
, AF_BRIDGE
);
127 return log_link_error_errno(link
, r
, "Could not create RTM_NEWNEIGH message: %m");
129 r
= sd_rtnl_message_neigh_set_flags(req
, fdb_entry
->fdb_ntf_flags
);
131 return log_link_error_errno(link
, r
, "Could not set neighbor flags: %m");
133 /* only NUD_PERMANENT state supported. */
134 r
= sd_rtnl_message_neigh_set_state(req
, NUD_NOARP
| NUD_PERMANENT
);
136 return log_link_error_errno(link
, r
, "Could not set neighbor state: %m");
138 r
= sd_netlink_message_append_data(req
, NDA_LLADDR
, &fdb_entry
->mac_addr
, sizeof(fdb_entry
->mac_addr
));
140 return log_link_error_errno(link
, r
, "Could not append NDA_LLADDR attribute: %m");
142 /* VLAN Id is optional. We'll add VLAN Id only if it's specified. */
143 if (fdb_entry
->vlan_id
> 0) {
144 r
= sd_netlink_message_append_u16(req
, NDA_VLAN
, fdb_entry
->vlan_id
);
146 return log_link_error_errno(link
, r
, "Could not append NDA_VLAN attribute: %m");
149 if (!in_addr_is_null(fdb_entry
->family
, &fdb_entry
->destination_addr
)) {
150 r
= netlink_message_append_in_addr_union(req
, NDA_DST
, fdb_entry
->family
, &fdb_entry
->destination_addr
);
152 return log_link_error_errno(link
, r
, "Could not append NDA_DST attribute: %m");
155 if (fdb_entry
->vni
<= VXLAN_VID_MAX
) {
156 r
= sd_netlink_message_append_u32(req
, NDA_VNI
, fdb_entry
->vni
);
158 return log_link_error_errno(link
, r
, "Could not append NDA_VNI attribute: %m");
161 /* send message to the kernel to update its internal static MAC table. */
162 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_fdb_handler
,
163 link_netlink_destroy_callback
, link
);
165 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
172 int link_set_bridge_fdb(Link
*link
) {
177 assert(link
->network
);
179 HASHMAP_FOREACH(fdb_entry
, link
->network
->fdb_entries_by_section
) {
180 r
= fdb_entry_configure(link
, fdb_entry
);
182 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
188 void network_drop_invalid_fdb_entries(Network
*network
) {
193 HASHMAP_FOREACH(fdb_entry
, network
->fdb_entries_by_section
)
194 if (section_is_invalid(fdb_entry
->section
))
195 fdb_entry_free(fdb_entry
);
198 /* parse the HW address from config files. */
199 int config_parse_fdb_hwaddr(
201 const char *filename
,
204 unsigned section_line
,
211 Network
*network
= userdata
;
212 _cleanup_(fdb_entry_free_or_set_invalidp
) FdbEntry
*fdb_entry
= NULL
;
221 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
225 r
= ether_addr_from_string(rvalue
, &fdb_entry
->mac_addr
);
227 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Not a valid MAC address, ignoring assignment: %s", rvalue
);
236 /* parse the VLAN Id from config files. */
237 int config_parse_fdb_vlan_id(
239 const char *filename
,
242 unsigned section_line
,
249 Network
*network
= userdata
;
250 _cleanup_(fdb_entry_free_or_set_invalidp
) FdbEntry
*fdb_entry
= NULL
;
259 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
263 r
= config_parse_vlanid(unit
, filename
, line
, section
,
264 section_line
, lvalue
, ltype
,
265 rvalue
, &fdb_entry
->vlan_id
, userdata
);
274 int config_parse_fdb_destination(
276 const char *filename
,
279 unsigned section_line
,
286 _cleanup_(fdb_entry_free_or_set_invalidp
) FdbEntry
*fdb_entry
= NULL
;
287 Network
*network
= userdata
;
296 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
300 r
= in_addr_from_string_auto(rvalue
, &fdb_entry
->family
, &fdb_entry
->destination_addr
);
302 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
303 "FDB destination IP address is invalid, ignoring assignment: %s",
313 int config_parse_fdb_vxlan_vni(
315 const char *filename
,
318 unsigned section_line
,
325 _cleanup_(fdb_entry_free_or_set_invalidp
) FdbEntry
*fdb_entry
= NULL
;
326 Network
*network
= userdata
;
336 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
340 r
= safe_atou32(rvalue
, &vni
);
342 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
343 "Failed to parse VXLAN Network Identifier (VNI), ignoring assignment: %s",
348 if (vni
> VXLAN_VID_MAX
) {
349 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
350 "FDB invalid VXLAN Network Identifier (VNI), ignoring assignment: %s",
355 fdb_entry
->vni
= vni
;
361 static const char* const fdb_ntf_flags_table
[_NEIGHBOR_CACHE_ENTRY_FLAGS_MAX
] = {
362 [NEIGHBOR_CACHE_ENTRY_FLAGS_USE
] = "use",
363 [NEIGHBOR_CACHE_ENTRY_FLAGS_SELF
] = "self",
364 [NEIGHBOR_CACHE_ENTRY_FLAGS_MASTER
] = "master",
365 [NEIGHBOR_CACHE_ENTRY_FLAGS_ROUTER
] = "router",
368 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(fdb_ntf_flags
, NeighborCacheEntryFlags
);
370 int config_parse_fdb_ntf_flags(
372 const char *filename
,
375 unsigned section_line
,
382 _cleanup_(fdb_entry_free_or_set_invalidp
) FdbEntry
*fdb_entry
= NULL
;
383 Network
*network
= userdata
;
384 NeighborCacheEntryFlags f
;
393 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
397 f
= fdb_ntf_flags_from_string(rvalue
);
399 log_syntax(unit
, LOG_WARNING
, filename
, line
, SYNTHETIC_ERRNO(EINVAL
),
400 "FDB failed to parse AssociatedWith=, ignoring assignment: %s",
405 fdb_entry
->fdb_ntf_flags
= f
;