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 "conf-parser.h"
12 #include "netlink-util.h"
13 #include "networkd-fdb.h"
14 #include "networkd-manager.h"
15 #include "parse-util.h"
16 #include "string-util.h"
17 #include "string-table.h"
19 #include "vlan-util.h"
22 #define STATIC_FDB_ENTRIES_PER_NETWORK_MAX 1024U
24 static const char* const fdb_ntf_flags_table
[_NEIGHBOR_CACHE_ENTRY_FLAGS_MAX
] = {
25 [NEIGHBOR_CACHE_ENTRY_FLAGS_USE
] = "use",
26 [NEIGHBOR_CACHE_ENTRY_FLAGS_SELF
] = "self",
27 [NEIGHBOR_CACHE_ENTRY_FLAGS_MASTER
] = "master",
28 [NEIGHBOR_CACHE_ENTRY_FLAGS_ROUTER
] = "router",
31 DEFINE_STRING_TABLE_LOOKUP(fdb_ntf_flags
, NeighborCacheEntryFlags
);
33 /* remove and FDB entry. */
34 FdbEntry
*fdb_entry_free(FdbEntry
*fdb_entry
) {
38 if (fdb_entry
->network
) {
39 LIST_REMOVE(static_fdb_entries
, fdb_entry
->network
->static_fdb_entries
, fdb_entry
);
40 assert(fdb_entry
->network
->n_static_fdb_entries
> 0);
41 fdb_entry
->network
->n_static_fdb_entries
--;
43 if (fdb_entry
->section
)
44 hashmap_remove(fdb_entry
->network
->fdb_entries_by_section
, fdb_entry
->section
);
47 network_config_section_free(fdb_entry
->section
);
48 return mfree(fdb_entry
);
51 DEFINE_NETWORK_SECTION_FUNCTIONS(FdbEntry
, fdb_entry_free
);
53 /* create a new FDB entry or get an existing one. */
54 static int fdb_entry_new_static(
57 unsigned section_line
,
60 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
61 _cleanup_(fdb_entry_freep
) FdbEntry
*fdb_entry
= NULL
;
66 assert(!!filename
== (section_line
> 0));
68 /* search entry in hashmap first. */
70 r
= network_config_section_new(filename
, section_line
, &n
);
74 fdb_entry
= hashmap_get(network
->fdb_entries_by_section
, n
);
76 *ret
= TAKE_PTR(fdb_entry
);
82 if (network
->n_static_fdb_entries
>= STATIC_FDB_ENTRIES_PER_NETWORK_MAX
)
85 /* allocate space for and FDB entry. */
86 fdb_entry
= new(FdbEntry
, 1);
90 /* init FDB structure. */
91 *fdb_entry
= (FdbEntry
) {
93 .vni
= VXLAN_VID_MAX
+ 1,
94 .fdb_ntf_flags
= NEIGHBOR_CACHE_ENTRY_FLAGS_SELF
,
97 LIST_PREPEND(static_fdb_entries
, network
->static_fdb_entries
, fdb_entry
);
98 network
->n_static_fdb_entries
++;
101 fdb_entry
->section
= TAKE_PTR(n
);
103 r
= hashmap_ensure_allocated(&network
->fdb_entries_by_section
, &network_config_hash_ops
);
107 r
= hashmap_put(network
->fdb_entries_by_section
, fdb_entry
->section
, fdb_entry
);
112 /* return allocated FDB structure. */
113 *ret
= TAKE_PTR(fdb_entry
);
118 static int set_fdb_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
123 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
126 r
= sd_netlink_message_get_errno(m
);
127 if (r
< 0 && r
!= -EEXIST
) {
128 log_link_message_warning_errno(link
, m
, r
, "Could not add FDB entry");
129 link_enter_failed(link
);
136 /* send a request to the kernel to add a FDB entry in its static MAC table. */
137 int fdb_entry_configure(Link
*link
, FdbEntry
*fdb_entry
) {
138 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
142 assert(link
->network
);
143 assert(link
->manager
);
146 /* create new RTM message */
147 r
= sd_rtnl_message_new_neigh(link
->manager
->rtnl
, &req
, RTM_NEWNEIGH
, link
->ifindex
, AF_BRIDGE
);
149 return log_link_error_errno(link
, r
, "Could not create RTM_NEWNEIGH message: %m");
151 r
= sd_rtnl_message_neigh_set_flags(req
, fdb_entry
->fdb_ntf_flags
);
153 return log_link_error_errno(link
, r
, "Could not set neighbor flags: %m");
155 /* only NUD_PERMANENT state supported. */
156 r
= sd_rtnl_message_neigh_set_state(req
, NUD_NOARP
| NUD_PERMANENT
);
158 return log_link_error_errno(link
, r
, "Could not set neighbor state: %m");
160 r
= sd_netlink_message_append_data(req
, NDA_LLADDR
, &fdb_entry
->mac_addr
, sizeof(fdb_entry
->mac_addr
));
162 return log_link_error_errno(link
, r
, "Could not append NDA_LLADDR attribute: %m");
164 /* VLAN Id is optional. We'll add VLAN Id only if it's specified. */
165 if (fdb_entry
->vlan_id
> 0) {
166 r
= sd_netlink_message_append_u16(req
, NDA_VLAN
, fdb_entry
->vlan_id
);
168 return log_link_error_errno(link
, r
, "Could not append NDA_VLAN attribute: %m");
171 if (!in_addr_is_null(fdb_entry
->family
, &fdb_entry
->destination_addr
)) {
172 r
= netlink_message_append_in_addr_union(req
, NDA_DST
, fdb_entry
->family
, &fdb_entry
->destination_addr
);
174 return log_link_error_errno(link
, r
, "Could not append NDA_DST attribute: %m");
177 if (fdb_entry
->vni
<= VXLAN_VID_MAX
) {
178 r
= sd_netlink_message_append_u32(req
, NDA_VNI
, fdb_entry
->vni
);
180 return log_link_error_errno(link
, r
, "Could not append NDA_VNI attribute: %m");
183 /* send message to the kernel to update its internal static MAC table. */
184 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_fdb_handler
,
185 link_netlink_destroy_callback
, link
);
187 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
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 int config_parse_fdb_ntf_flags(
359 const char *filename
,
362 unsigned section_line
,
369 _cleanup_(fdb_entry_free_or_set_invalidp
) FdbEntry
*fdb_entry
= NULL
;
370 Network
*network
= userdata
;
371 NeighborCacheEntryFlags f
;
380 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
384 f
= fdb_ntf_flags_from_string(rvalue
);
386 log_syntax(unit
, LOG_WARNING
, filename
, line
, SYNTHETIC_ERRNO(EINVAL
),
387 "FDB failed to parse AssociatedWith=, ignoring assignment: %s",
392 fdb_entry
->fdb_ntf_flags
= f
;