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"
10 #include "conf-parser.h"
11 #include "netdev/bridge.h"
12 #include "netdev/vxlan.h"
13 #include "netlink-util.h"
14 #include "networkd-fdb.h"
15 #include "networkd-manager.h"
16 #include "parse-util.h"
17 #include "string-util.h"
18 #include "string-table.h"
20 #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 /* create a new FDB entry or get an existing one. */
34 static int fdb_entry_new_static(
37 unsigned section_line
,
40 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
41 _cleanup_(fdb_entry_freep
) FdbEntry
*fdb_entry
= NULL
;
46 assert(!!filename
== (section_line
> 0));
48 /* search entry in hashmap first. */
50 r
= network_config_section_new(filename
, section_line
, &n
);
54 fdb_entry
= hashmap_get(network
->fdb_entries_by_section
, n
);
56 *ret
= TAKE_PTR(fdb_entry
);
62 if (network
->n_static_fdb_entries
>= STATIC_FDB_ENTRIES_PER_NETWORK_MAX
)
65 /* allocate space for and FDB entry. */
66 fdb_entry
= new(FdbEntry
, 1);
70 /* init FDB structure. */
71 *fdb_entry
= (FdbEntry
) {
73 .vni
= VXLAN_VID_MAX
+ 1,
74 .fdb_ntf_flags
= NEIGHBOR_CACHE_ENTRY_FLAGS_SELF
,
77 LIST_PREPEND(static_fdb_entries
, network
->static_fdb_entries
, fdb_entry
);
78 network
->n_static_fdb_entries
++;
81 fdb_entry
->section
= TAKE_PTR(n
);
83 r
= hashmap_ensure_allocated(&network
->fdb_entries_by_section
, &network_config_hash_ops
);
87 r
= hashmap_put(network
->fdb_entries_by_section
, fdb_entry
->section
, fdb_entry
);
92 /* return allocated FDB structure. */
93 *ret
= TAKE_PTR(fdb_entry
);
98 static int set_fdb_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
103 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
106 r
= sd_netlink_message_get_errno(m
);
107 if (r
< 0 && r
!= -EEXIST
) {
108 log_link_error_errno(link
, r
, "Could not add FDB entry: %m");
109 link_enter_failed(link
);
116 /* send a request to the kernel to add a FDB entry in its static MAC table. */
117 int fdb_entry_configure(Link
*link
, FdbEntry
*fdb_entry
) {
118 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
122 assert(link
->network
);
123 assert(link
->manager
);
126 if (fdb_entry
->family
== AF_INET6
&& link_sysctl_ipv6_enabled(link
) == 0) {
127 log_link_warning(link
, "An IPv6 fdb entry is requested, but IPv6 is disabled by sysctl, ignoring.");
131 /* create new RTM message */
132 r
= sd_rtnl_message_new_neigh(link
->manager
->rtnl
, &req
, RTM_NEWNEIGH
, link
->ifindex
, PF_BRIDGE
);
134 return rtnl_log_create_error(r
);
136 r
= sd_rtnl_message_neigh_set_flags(req
, fdb_entry
->fdb_ntf_flags
);
138 return rtnl_log_create_error(r
);
140 /* only NUD_PERMANENT state supported. */
141 r
= sd_rtnl_message_neigh_set_state(req
, NUD_NOARP
| NUD_PERMANENT
);
143 return rtnl_log_create_error(r
);
145 r
= sd_netlink_message_append_data(req
, NDA_LLADDR
, &fdb_entry
->mac_addr
, sizeof(fdb_entry
->mac_addr
));
147 return rtnl_log_create_error(r
);
149 /* VLAN Id is optional. We'll add VLAN Id only if it's specified. */
150 if (fdb_entry
->vlan_id
> 0) {
151 r
= sd_netlink_message_append_u16(req
, NDA_VLAN
, fdb_entry
->vlan_id
);
153 return rtnl_log_create_error(r
);
156 if (!in_addr_is_null(fdb_entry
->family
, &fdb_entry
->destination_addr
)) {
157 r
= netlink_message_append_in_addr_union(req
, NDA_DST
, fdb_entry
->family
, &fdb_entry
->destination_addr
);
159 return log_link_error_errno(link
, r
, "Could not append NDA_DST attribute: %m");
162 if (fdb_entry
->vni
<= VXLAN_VID_MAX
) {
163 r
= sd_netlink_message_append_u32(req
, NDA_VNI
, fdb_entry
->vni
);
165 return log_link_error_errno(link
, r
, "Could not append NDA_VNI attribute: %m");
168 /* send message to the kernel to update its internal static MAC table. */
169 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_fdb_handler
,
170 link_netlink_destroy_callback
, link
);
172 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
179 /* remove and FDB entry. */
180 void fdb_entry_free(FdbEntry
*fdb_entry
) {
184 if (fdb_entry
->network
) {
185 LIST_REMOVE(static_fdb_entries
, fdb_entry
->network
->static_fdb_entries
, fdb_entry
);
186 assert(fdb_entry
->network
->n_static_fdb_entries
> 0);
187 fdb_entry
->network
->n_static_fdb_entries
--;
189 if (fdb_entry
->section
)
190 hashmap_remove(fdb_entry
->network
->fdb_entries_by_section
, fdb_entry
->section
);
193 network_config_section_free(fdb_entry
->section
);
197 /* parse the HW address from config files. */
198 int config_parse_fdb_hwaddr(
200 const char *filename
,
203 unsigned section_line
,
210 Network
*network
= userdata
;
211 _cleanup_(fdb_entry_free_or_set_invalidp
) FdbEntry
*fdb_entry
= NULL
;
220 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
224 r
= ether_addr_from_string(rvalue
, &fdb_entry
->mac_addr
);
226 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Not a valid MAC address, ignoring assignment: %s", rvalue
);
235 /* parse the VLAN Id from config files. */
236 int config_parse_fdb_vlan_id(
238 const char *filename
,
241 unsigned section_line
,
248 Network
*network
= userdata
;
249 _cleanup_(fdb_entry_free_or_set_invalidp
) FdbEntry
*fdb_entry
= NULL
;
258 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
262 r
= config_parse_vlanid(unit
, filename
, line
, section
,
263 section_line
, lvalue
, ltype
,
264 rvalue
, &fdb_entry
->vlan_id
, userdata
);
273 int config_parse_fdb_destination(
275 const char *filename
,
278 unsigned section_line
,
285 _cleanup_(fdb_entry_free_or_set_invalidp
) FdbEntry
*fdb_entry
= NULL
;
286 Network
*network
= userdata
;
295 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
299 r
= in_addr_from_string_auto(rvalue
, &fdb_entry
->family
, &fdb_entry
->destination_addr
);
301 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
302 "FDB destination IP address is invalid, ignoring assignment: %s",
310 int config_parse_fdb_vxlan_vni(
312 const char *filename
,
315 unsigned section_line
,
322 _cleanup_(fdb_entry_free_or_set_invalidp
) FdbEntry
*fdb_entry
= NULL
;
323 Network
*network
= userdata
;
333 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
337 r
= safe_atou32(rvalue
, &vni
);
339 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
340 "Failed to parse VXLAN Network Identifier (VNI), ignoring assignment: %s",
345 if (vni
> VXLAN_VID_MAX
) {
346 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
347 "FDB invalid VXLAN Network Identifier (VNI), ignoring assignment: %s",
352 fdb_entry
->vni
= vni
;
358 int config_parse_fdb_ntf_flags(
360 const char *filename
,
363 unsigned section_line
,
370 _cleanup_(fdb_entry_free_or_set_invalidp
) FdbEntry
*fdb_entry
= NULL
;
371 Network
*network
= userdata
;
372 NeighborCacheEntryFlags f
;
381 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
385 f
= fdb_ntf_flags_from_string(rvalue
);
387 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
388 "FDB failed to parse AssociatedWith=, ignoring assignment: %s",
393 fdb_entry
->fdb_ntf_flags
= f
;