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"
19 #include "vlan-util.h"
21 #define STATIC_FDB_ENTRIES_PER_NETWORK_MAX 1024U
23 /* create a new FDB entry or get an existing one. */
24 static int fdb_entry_new_static(
27 unsigned section_line
,
30 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
31 _cleanup_(fdb_entry_freep
) FdbEntry
*fdb_entry
= NULL
;
32 _cleanup_free_
struct ether_addr
*mac_addr
= NULL
;
37 assert(!!filename
== (section_line
> 0));
39 /* search entry in hashmap first. */
41 r
= network_config_section_new(filename
, section_line
, &n
);
45 fdb_entry
= hashmap_get(network
->fdb_entries_by_section
, n
);
47 *ret
= TAKE_PTR(fdb_entry
);
53 if (network
->n_static_fdb_entries
>= STATIC_FDB_ENTRIES_PER_NETWORK_MAX
)
56 /* allocate space for MAC address. */
57 mac_addr
= new0(struct ether_addr
, 1);
61 /* allocate space for and FDB entry. */
62 fdb_entry
= new(FdbEntry
, 1);
66 /* init FDB structure. */
67 *fdb_entry
= (FdbEntry
) {
69 .mac_addr
= TAKE_PTR(mac_addr
),
70 .vni
= VXLAN_VID_MAX
+ 1,
73 LIST_PREPEND(static_fdb_entries
, network
->static_fdb_entries
, fdb_entry
);
74 network
->n_static_fdb_entries
++;
77 fdb_entry
->section
= TAKE_PTR(n
);
79 r
= hashmap_ensure_allocated(&network
->fdb_entries_by_section
, &network_config_hash_ops
);
83 r
= hashmap_put(network
->fdb_entries_by_section
, fdb_entry
->section
, fdb_entry
);
88 /* return allocated FDB structure. */
89 *ret
= TAKE_PTR(fdb_entry
);
94 static int set_fdb_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
99 r
= sd_netlink_message_get_errno(m
);
100 if (r
< 0 && r
!= -EEXIST
)
101 log_link_error_errno(link
, r
, "Could not add FDB entry: %m");
106 /* send a request to the kernel to add a FDB entry in its static MAC table. */
107 int fdb_entry_configure(Link
*link
, FdbEntry
*fdb_entry
) {
108 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
115 assert(link
->network
);
116 assert(link
->manager
);
119 rtnl
= link
->manager
->rtnl
;
120 bridge
= BRIDGE(link
->network
->bridge
);
122 /* create new RTM message */
123 r
= sd_rtnl_message_new_neigh(rtnl
, &req
, RTM_NEWNEIGH
, link
->ifindex
, PF_BRIDGE
);
125 return rtnl_log_create_error(r
);
132 r
= sd_rtnl_message_neigh_set_flags(req
, flags
);
134 return rtnl_log_create_error(r
);
136 /* only NUD_PERMANENT state supported. */
137 r
= sd_rtnl_message_neigh_set_state(req
, NUD_NOARP
| NUD_PERMANENT
);
139 return rtnl_log_create_error(r
);
141 r
= sd_netlink_message_append_ether_addr(req
, NDA_LLADDR
, fdb_entry
->mac_addr
);
143 return rtnl_log_create_error(r
);
145 /* VLAN Id is optional. We'll add VLAN Id only if it's specified. */
146 if (fdb_entry
->vlan_id
> 0) {
147 r
= sd_netlink_message_append_u16(req
, NDA_VLAN
, fdb_entry
->vlan_id
);
149 return rtnl_log_create_error(r
);
152 if (!in_addr_is_null(fdb_entry
->family
, &fdb_entry
->destination_addr
)) {
153 r
= netlink_message_append_in_addr_union(req
, NDA_DST
, fdb_entry
->family
, &fdb_entry
->destination_addr
);
155 return log_link_error_errno(link
, r
, "Could not append NDA_DST attribute: %m");
158 if (fdb_entry
->vni
<= VXLAN_VID_MAX
) {
159 r
= sd_netlink_message_append_u32(req
, NDA_VNI
, fdb_entry
->vni
);
161 return log_link_error_errno(link
, r
, "Could not append NDA_VNI attribute: %m");
164 /* send message to the kernel to update its internal static MAC table. */
165 r
= netlink_call_async(rtnl
, NULL
, req
, set_fdb_handler
,
166 link_netlink_destroy_callback
, link
);
168 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
175 /* remove and FDB entry. */
176 void fdb_entry_free(FdbEntry
*fdb_entry
) {
180 if (fdb_entry
->network
) {
181 LIST_REMOVE(static_fdb_entries
, fdb_entry
->network
->static_fdb_entries
, fdb_entry
);
182 assert(fdb_entry
->network
->n_static_fdb_entries
> 0);
183 fdb_entry
->network
->n_static_fdb_entries
--;
185 if (fdb_entry
->section
)
186 hashmap_remove(fdb_entry
->network
->fdb_entries_by_section
, fdb_entry
->section
);
189 network_config_section_free(fdb_entry
->section
);
190 free(fdb_entry
->mac_addr
);
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 /* read in the MAC address for the FDB table. */
222 r
= sscanf(rvalue
, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
223 &fdb_entry
->mac_addr
->ether_addr_octet
[0],
224 &fdb_entry
->mac_addr
->ether_addr_octet
[1],
225 &fdb_entry
->mac_addr
->ether_addr_octet
[2],
226 &fdb_entry
->mac_addr
->ether_addr_octet
[3],
227 &fdb_entry
->mac_addr
->ether_addr_octet
[4],
228 &fdb_entry
->mac_addr
->ether_addr_octet
[5]);
230 if (r
!= ETHER_ADDR_LEN
) {
231 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Not a valid MAC address, ignoring assignment: %s", rvalue
);
240 /* parse the VLAN Id from config files. */
241 int config_parse_fdb_vlan_id(
243 const char *filename
,
246 unsigned section_line
,
253 Network
*network
= userdata
;
254 _cleanup_(fdb_entry_free_or_set_invalidp
) FdbEntry
*fdb_entry
= NULL
;
263 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
267 r
= config_parse_vlanid(unit
, filename
, line
, section
,
268 section_line
, lvalue
, ltype
,
269 rvalue
, &fdb_entry
->vlan_id
, userdata
);
278 int config_parse_fdb_destination(
280 const char *filename
,
283 unsigned section_line
,
290 _cleanup_(fdb_entry_free_or_set_invalidp
) FdbEntry
*fdb_entry
= NULL
;
291 Network
*network
= userdata
;
300 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
304 r
= in_addr_from_string_auto(rvalue
, &fdb_entry
->family
, &fdb_entry
->destination_addr
);
306 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
307 "FDB destination IP address is invalid, ignoring assignment: %s",
315 int config_parse_fdb_vxlan_vni(
317 const char *filename
,
320 unsigned section_line
,
327 _cleanup_(fdb_entry_free_or_set_invalidp
) FdbEntry
*fdb_entry
= NULL
;
328 Network
*network
= userdata
;
338 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
342 r
= safe_atou32(rvalue
, &vni
);
344 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
345 "Failed to parse VXLAN Network Identifier (VNI), ignoring assignment: %s",
350 if (vni
> VXLAN_VID_MAX
) {
351 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
352 "FDB invalid VXLAN Network Identifier (VNI), ignoring assignment: %s",
357 fdb_entry
->vni
= vni
;