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 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
, void *userdata
) {
91 Link
*link
= userdata
;
96 r
= sd_netlink_message_get_errno(m
);
97 if (r
< 0 && r
!= -EEXIST
)
98 log_link_error_errno(link
, r
, "Could not add FDB entry: %m");
103 /* send a request to the kernel to add a FDB entry in its static MAC table. */
104 int fdb_entry_configure(Link
*link
, FdbEntry
*fdb_entry
) {
105 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
112 assert(link
->network
);
113 assert(link
->manager
);
116 rtnl
= link
->manager
->rtnl
;
117 bridge
= BRIDGE(link
->network
->bridge
);
119 /* create new RTM message */
120 r
= sd_rtnl_message_new_neigh(rtnl
, &req
, RTM_NEWNEIGH
, link
->ifindex
, PF_BRIDGE
);
122 return rtnl_log_create_error(r
);
129 r
= sd_rtnl_message_neigh_set_flags(req
, flags
);
131 return rtnl_log_create_error(r
);
133 /* only NUD_PERMANENT state supported. */
134 r
= sd_rtnl_message_neigh_set_state(req
, NUD_NOARP
| NUD_PERMANENT
);
136 return rtnl_log_create_error(r
);
138 r
= sd_netlink_message_append_ether_addr(req
, NDA_LLADDR
, fdb_entry
->mac_addr
);
140 return rtnl_log_create_error(r
);
142 /* VLAN Id is optional. We'll add VLAN Id only if it's specified. */
143 if (0 != fdb_entry
->vlan_id
) {
144 r
= sd_netlink_message_append_u16(req
, NDA_VLAN
, fdb_entry
->vlan_id
);
146 return rtnl_log_create_error(r
);
149 /* send message to the kernel to update its internal static MAC table. */
150 r
= sd_netlink_call_async(rtnl
, NULL
, req
, set_fdb_handler
,
151 link_netlink_destroy_callback
, link
, 0, __func__
);
153 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
160 /* remove and FDB entry. */
161 void fdb_entry_free(FdbEntry
*fdb_entry
) {
165 if (fdb_entry
->network
) {
166 LIST_REMOVE(static_fdb_entries
, fdb_entry
->network
->static_fdb_entries
, fdb_entry
);
167 assert(fdb_entry
->network
->n_static_fdb_entries
> 0);
168 fdb_entry
->network
->n_static_fdb_entries
--;
170 if (fdb_entry
->section
)
171 hashmap_remove(fdb_entry
->network
->fdb_entries_by_section
, fdb_entry
->section
);
174 network_config_section_free(fdb_entry
->section
);
175 free(fdb_entry
->mac_addr
);
179 /* parse the HW address from config files. */
180 int config_parse_fdb_hwaddr(
182 const char *filename
,
185 unsigned section_line
,
192 Network
*network
= userdata
;
193 _cleanup_(fdb_entry_freep
) FdbEntry
*fdb_entry
= NULL
;
202 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
206 /* read in the MAC address for the FDB table. */
207 r
= sscanf(rvalue
, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
208 &fdb_entry
->mac_addr
->ether_addr_octet
[0],
209 &fdb_entry
->mac_addr
->ether_addr_octet
[1],
210 &fdb_entry
->mac_addr
->ether_addr_octet
[2],
211 &fdb_entry
->mac_addr
->ether_addr_octet
[3],
212 &fdb_entry
->mac_addr
->ether_addr_octet
[4],
213 &fdb_entry
->mac_addr
->ether_addr_octet
[5]);
215 if (r
!= ETHER_ADDR_LEN
) {
216 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Not a valid MAC address, ignoring assignment: %s", rvalue
);
225 /* parse the VLAN Id from config files. */
226 int config_parse_fdb_vlan_id(
228 const char *filename
,
231 unsigned section_line
,
238 Network
*network
= userdata
;
239 _cleanup_(fdb_entry_freep
) FdbEntry
*fdb_entry
= NULL
;
248 r
= fdb_entry_new_static(network
, filename
, section_line
, &fdb_entry
);
252 r
= config_parse_vlanid(unit
, filename
, line
, section
,
253 section_line
, lvalue
, ltype
,
254 rvalue
, &fdb_entry
->vlan_id
, userdata
);