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 "netlink-util.h"
13 #include "networkd-fdb.h"
14 #include "networkd-manager.h"
16 #include "vlan-util.h"
18 #define STATIC_FDB_ENTRIES_PER_NETWORK_MAX 1024U
20 /* create a new FDB entry or get an existing one. */
21 static int fdb_entry_new_static(
24 unsigned section_line
,
27 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
28 _cleanup_(fdb_entry_freep
) FdbEntry
*fdb_entry
= NULL
;
29 _cleanup_free_
struct ether_addr
*mac_addr
= NULL
;
34 assert(!!filename
== (section_line
> 0));
36 /* search entry in hashmap first. */
38 r
= network_config_section_new(filename
, section_line
, &n
);
42 fdb_entry
= hashmap_get(network
->fdb_entries_by_section
, n
);
44 *ret
= TAKE_PTR(fdb_entry
);
50 if (network
->n_static_fdb_entries
>= STATIC_FDB_ENTRIES_PER_NETWORK_MAX
)
53 /* allocate space for MAC address. */
54 mac_addr
= new0(struct ether_addr
, 1);
58 /* allocate space for and FDB entry. */
59 fdb_entry
= new(FdbEntry
, 1);
63 /* init FDB structure. */
64 *fdb_entry
= (FdbEntry
) {
66 .mac_addr
= TAKE_PTR(mac_addr
),
69 LIST_PREPEND(static_fdb_entries
, network
->static_fdb_entries
, fdb_entry
);
70 network
->n_static_fdb_entries
++;
73 fdb_entry
->section
= TAKE_PTR(n
);
75 r
= hashmap_ensure_allocated(&network
->fdb_entries_by_section
, &network_config_hash_ops
);
79 r
= hashmap_put(network
->fdb_entries_by_section
, fdb_entry
->section
, fdb_entry
);
84 /* return allocated FDB structure. */
85 *ret
= TAKE_PTR(fdb_entry
);
90 static int set_fdb_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
95 r
= sd_netlink_message_get_errno(m
);
96 if (r
< 0 && r
!= -EEXIST
)
97 log_link_error_errno(link
, r
, "Could not add FDB entry: %m");
102 /* send a request to the kernel to add a FDB entry in its static MAC table. */
103 int fdb_entry_configure(Link
*link
, FdbEntry
*fdb_entry
) {
104 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
111 assert(link
->network
);
112 assert(link
->manager
);
115 rtnl
= link
->manager
->rtnl
;
116 bridge
= BRIDGE(link
->network
->bridge
);
118 /* create new RTM message */
119 r
= sd_rtnl_message_new_neigh(rtnl
, &req
, RTM_NEWNEIGH
, link
->ifindex
, PF_BRIDGE
);
121 return rtnl_log_create_error(r
);
128 r
= sd_rtnl_message_neigh_set_flags(req
, flags
);
130 return rtnl_log_create_error(r
);
132 /* only NUD_PERMANENT state supported. */
133 r
= sd_rtnl_message_neigh_set_state(req
, NUD_NOARP
| NUD_PERMANENT
);
135 return rtnl_log_create_error(r
);
137 r
= sd_netlink_message_append_ether_addr(req
, NDA_LLADDR
, fdb_entry
->mac_addr
);
139 return rtnl_log_create_error(r
);
141 /* VLAN Id is optional. We'll add VLAN Id only if it's specified. */
142 if (fdb_entry
->vlan_id
> 0) {
143 r
= sd_netlink_message_append_u16(req
, NDA_VLAN
, fdb_entry
->vlan_id
);
145 return rtnl_log_create_error(r
);
148 /* send message to the kernel to update its internal static MAC table. */
149 r
= netlink_call_async(rtnl
, NULL
, req
, set_fdb_handler
,
150 link_netlink_destroy_callback
, link
);
152 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
159 /* remove and FDB entry. */
160 void fdb_entry_free(FdbEntry
*fdb_entry
) {
164 if (fdb_entry
->network
) {
165 LIST_REMOVE(static_fdb_entries
, fdb_entry
->network
->static_fdb_entries
, fdb_entry
);
166 assert(fdb_entry
->network
->n_static_fdb_entries
> 0);
167 fdb_entry
->network
->n_static_fdb_entries
--;
169 if (fdb_entry
->section
)
170 hashmap_remove(fdb_entry
->network
->fdb_entries_by_section
, fdb_entry
->section
);
173 network_config_section_free(fdb_entry
->section
);
174 free(fdb_entry
->mac_addr
);
178 /* parse the HW address from config files. */
179 int config_parse_fdb_hwaddr(
181 const char *filename
,
184 unsigned section_line
,
191 Network
*network
= userdata
;
192 _cleanup_(fdb_entry_free_or_set_invalidp
) FdbEntry
*fdb_entry
= NULL
;
201 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
205 /* read in the MAC address for the FDB table. */
206 r
= sscanf(rvalue
, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
207 &fdb_entry
->mac_addr
->ether_addr_octet
[0],
208 &fdb_entry
->mac_addr
->ether_addr_octet
[1],
209 &fdb_entry
->mac_addr
->ether_addr_octet
[2],
210 &fdb_entry
->mac_addr
->ether_addr_octet
[3],
211 &fdb_entry
->mac_addr
->ether_addr_octet
[4],
212 &fdb_entry
->mac_addr
->ether_addr_octet
[5]);
214 if (r
!= ETHER_ADDR_LEN
) {
215 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Not a valid MAC address, ignoring assignment: %s", rvalue
);
224 /* parse the VLAN Id from config files. */
225 int config_parse_fdb_vlan_id(
227 const char *filename
,
230 unsigned section_line
,
237 Network
*network
= userdata
;
238 _cleanup_(fdb_entry_free_or_set_invalidp
) FdbEntry
*fdb_entry
= NULL
;
247 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
251 r
= config_parse_vlanid(unit
, filename
, line
, section
,
252 section_line
, lvalue
, ltype
,
253 rvalue
, &fdb_entry
->vlan_id
, userdata
);