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
;
42 _cleanup_free_
struct ether_addr
*mac_addr
= NULL
;
47 assert(!!filename
== (section_line
> 0));
49 /* search entry in hashmap first. */
51 r
= network_config_section_new(filename
, section_line
, &n
);
55 fdb_entry
= hashmap_get(network
->fdb_entries_by_section
, n
);
57 *ret
= TAKE_PTR(fdb_entry
);
63 if (network
->n_static_fdb_entries
>= STATIC_FDB_ENTRIES_PER_NETWORK_MAX
)
66 /* allocate space for MAC address. */
67 mac_addr
= new0(struct ether_addr
, 1);
71 /* allocate space for and FDB entry. */
72 fdb_entry
= new(FdbEntry
, 1);
76 /* init FDB structure. */
77 *fdb_entry
= (FdbEntry
) {
79 .mac_addr
= TAKE_PTR(mac_addr
),
80 .vni
= VXLAN_VID_MAX
+ 1,
81 .fdb_ntf_flags
= NEIGHBOR_CACHE_ENTRY_FLAGS_SELF
,
84 LIST_PREPEND(static_fdb_entries
, network
->static_fdb_entries
, fdb_entry
);
85 network
->n_static_fdb_entries
++;
88 fdb_entry
->section
= TAKE_PTR(n
);
90 r
= hashmap_ensure_allocated(&network
->fdb_entries_by_section
, &network_config_hash_ops
);
94 r
= hashmap_put(network
->fdb_entries_by_section
, fdb_entry
->section
, fdb_entry
);
99 /* return allocated FDB structure. */
100 *ret
= TAKE_PTR(fdb_entry
);
105 static int set_fdb_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
110 r
= sd_netlink_message_get_errno(m
);
111 if (r
< 0 && r
!= -EEXIST
)
112 log_link_error_errno(link
, r
, "Could not add FDB entry: %m");
117 /* send a request to the kernel to add a FDB entry in its static MAC table. */
118 int fdb_entry_configure(Link
*link
, FdbEntry
*fdb_entry
) {
119 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
123 assert(link
->network
);
124 assert(link
->manager
);
127 if (fdb_entry
->family
== AF_INET6
&& link_sysctl_ipv6_enabled(link
) == 0) {
128 log_link_warning(link
, "An IPv6 fdb entry is requested, but IPv6 is disabled by sysctl, ignoring.");
132 /* create new RTM message */
133 r
= sd_rtnl_message_new_neigh(link
->manager
->rtnl
, &req
, RTM_NEWNEIGH
, link
->ifindex
, PF_BRIDGE
);
135 return rtnl_log_create_error(r
);
137 r
= sd_rtnl_message_neigh_set_flags(req
, fdb_entry
->fdb_ntf_flags
);
139 return rtnl_log_create_error(r
);
141 /* only NUD_PERMANENT state supported. */
142 r
= sd_rtnl_message_neigh_set_state(req
, NUD_NOARP
| NUD_PERMANENT
);
144 return rtnl_log_create_error(r
);
146 r
= sd_netlink_message_append_ether_addr(req
, NDA_LLADDR
, fdb_entry
->mac_addr
);
148 return rtnl_log_create_error(r
);
150 /* VLAN Id is optional. We'll add VLAN Id only if it's specified. */
151 if (fdb_entry
->vlan_id
> 0) {
152 r
= sd_netlink_message_append_u16(req
, NDA_VLAN
, fdb_entry
->vlan_id
);
154 return rtnl_log_create_error(r
);
157 if (!in_addr_is_null(fdb_entry
->family
, &fdb_entry
->destination_addr
)) {
158 r
= netlink_message_append_in_addr_union(req
, NDA_DST
, fdb_entry
->family
, &fdb_entry
->destination_addr
);
160 return log_link_error_errno(link
, r
, "Could not append NDA_DST attribute: %m");
163 if (fdb_entry
->vni
<= VXLAN_VID_MAX
) {
164 r
= sd_netlink_message_append_u32(req
, NDA_VNI
, fdb_entry
->vni
);
166 return log_link_error_errno(link
, r
, "Could not append NDA_VNI attribute: %m");
169 /* send message to the kernel to update its internal static MAC table. */
170 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_fdb_handler
,
171 link_netlink_destroy_callback
, link
);
173 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
180 /* remove and FDB entry. */
181 void fdb_entry_free(FdbEntry
*fdb_entry
) {
185 if (fdb_entry
->network
) {
186 LIST_REMOVE(static_fdb_entries
, fdb_entry
->network
->static_fdb_entries
, fdb_entry
);
187 assert(fdb_entry
->network
->n_static_fdb_entries
> 0);
188 fdb_entry
->network
->n_static_fdb_entries
--;
190 if (fdb_entry
->section
)
191 hashmap_remove(fdb_entry
->network
->fdb_entries_by_section
, fdb_entry
->section
);
194 network_config_section_free(fdb_entry
->section
);
195 free(fdb_entry
->mac_addr
);
199 /* parse the HW address from config files. */
200 int config_parse_fdb_hwaddr(
202 const char *filename
,
205 unsigned section_line
,
212 Network
*network
= userdata
;
213 _cleanup_(fdb_entry_free_or_set_invalidp
) FdbEntry
*fdb_entry
= NULL
;
222 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
226 /* read in the MAC address for the FDB table. */
227 r
= sscanf(rvalue
, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
228 &fdb_entry
->mac_addr
->ether_addr_octet
[0],
229 &fdb_entry
->mac_addr
->ether_addr_octet
[1],
230 &fdb_entry
->mac_addr
->ether_addr_octet
[2],
231 &fdb_entry
->mac_addr
->ether_addr_octet
[3],
232 &fdb_entry
->mac_addr
->ether_addr_octet
[4],
233 &fdb_entry
->mac_addr
->ether_addr_octet
[5]);
235 if (r
!= ETHER_ADDR_LEN
) {
236 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Not a valid MAC address, ignoring assignment: %s", rvalue
);
245 /* parse the VLAN Id from config files. */
246 int config_parse_fdb_vlan_id(
248 const char *filename
,
251 unsigned section_line
,
258 Network
*network
= userdata
;
259 _cleanup_(fdb_entry_free_or_set_invalidp
) FdbEntry
*fdb_entry
= NULL
;
268 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
272 r
= config_parse_vlanid(unit
, filename
, line
, section
,
273 section_line
, lvalue
, ltype
,
274 rvalue
, &fdb_entry
->vlan_id
, userdata
);
283 int config_parse_fdb_destination(
285 const char *filename
,
288 unsigned section_line
,
295 _cleanup_(fdb_entry_free_or_set_invalidp
) FdbEntry
*fdb_entry
= NULL
;
296 Network
*network
= userdata
;
305 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
309 r
= in_addr_from_string_auto(rvalue
, &fdb_entry
->family
, &fdb_entry
->destination_addr
);
311 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
312 "FDB destination IP address is invalid, ignoring assignment: %s",
320 int config_parse_fdb_vxlan_vni(
322 const char *filename
,
325 unsigned section_line
,
332 _cleanup_(fdb_entry_free_or_set_invalidp
) FdbEntry
*fdb_entry
= NULL
;
333 Network
*network
= userdata
;
343 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
347 r
= safe_atou32(rvalue
, &vni
);
349 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
350 "Failed to parse VXLAN Network Identifier (VNI), ignoring assignment: %s",
355 if (vni
> VXLAN_VID_MAX
) {
356 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
357 "FDB invalid VXLAN Network Identifier (VNI), ignoring assignment: %s",
362 fdb_entry
->vni
= vni
;
369 int config_parse_fdb_ntf_flags(
371 const char *filename
,
374 unsigned section_line
,
381 _cleanup_(fdb_entry_free_or_set_invalidp
) FdbEntry
*fdb_entry
= NULL
;
382 Network
*network
= userdata
;
383 NeighborCacheEntryFlags f
;
392 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
396 f
= fdb_ntf_flags_from_string(rvalue
);
398 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
399 "FDB failed to parse AssociatedWith=, ignoring assignment: %s",
404 fdb_entry
->fdb_ntf_flags
= f
;