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(
26 _cleanup_(fdb_entry_freep
) FdbEntry
*fdb_entry
= NULL
;
27 struct ether_addr
*mac_addr
= NULL
;
32 /* search entry in hashmap first. */
34 fdb_entry
= hashmap_get(network
->fdb_entries_by_section
, UINT_TO_PTR(section
));
36 *ret
= TAKE_PTR(fdb_entry
);
42 if (network
->n_static_fdb_entries
>= STATIC_FDB_ENTRIES_PER_NETWORK_MAX
)
45 /* allocate space for MAC address. */
46 mac_addr
= new0(struct ether_addr
, 1);
50 /* allocate space for and FDB entry. */
51 fdb_entry
= new0(FdbEntry
, 1);
53 /* free previously allocated space for mac_addr. */
58 /* init FDB structure. */
59 fdb_entry
->network
= network
;
60 fdb_entry
->mac_addr
= mac_addr
;
62 LIST_PREPEND(static_fdb_entries
, network
->static_fdb_entries
, fdb_entry
);
63 network
->n_static_fdb_entries
++;
66 fdb_entry
->section
= section
;
67 hashmap_put(network
->fdb_entries_by_section
,
68 UINT_TO_PTR(fdb_entry
->section
), fdb_entry
);
71 /* return allocated FDB structure. */
72 *ret
= TAKE_PTR(fdb_entry
);
77 static int set_fdb_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
78 Link
*link
= userdata
;
83 r
= sd_netlink_message_get_errno(m
);
84 if (r
< 0 && r
!= -EEXIST
)
85 log_link_error_errno(link
, r
, "Could not add FDB entry: %m");
90 /* send a request to the kernel to add a FDB entry in its static MAC table. */
91 int fdb_entry_configure(Link
*link
, FdbEntry
*fdb_entry
) {
92 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
99 assert(link
->network
);
100 assert(link
->manager
);
103 rtnl
= link
->manager
->rtnl
;
104 bridge
= BRIDGE(link
->network
->bridge
);
106 /* create new RTM message */
107 r
= sd_rtnl_message_new_neigh(rtnl
, &req
, RTM_NEWNEIGH
, link
->ifindex
, PF_BRIDGE
);
109 return rtnl_log_create_error(r
);
116 r
= sd_rtnl_message_neigh_set_flags(req
, flags
);
118 return rtnl_log_create_error(r
);
120 /* only NUD_PERMANENT state supported. */
121 r
= sd_rtnl_message_neigh_set_state(req
, NUD_NOARP
| NUD_PERMANENT
);
123 return rtnl_log_create_error(r
);
125 r
= sd_netlink_message_append_ether_addr(req
, NDA_LLADDR
, fdb_entry
->mac_addr
);
127 return rtnl_log_create_error(r
);
129 /* VLAN Id is optional. We'll add VLAN Id only if it's specified. */
130 if (0 != fdb_entry
->vlan_id
) {
131 r
= sd_netlink_message_append_u16(req
, NDA_VLAN
, fdb_entry
->vlan_id
);
133 return rtnl_log_create_error(r
);
136 /* send message to the kernel to update its internal static MAC table. */
137 r
= sd_netlink_call_async(rtnl
, NULL
, req
, set_fdb_handler
,
138 link_netlink_destroy_callback
, link
, 0, __func__
);
140 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
147 /* remove and FDB entry. */
148 void fdb_entry_free(FdbEntry
*fdb_entry
) {
152 if (fdb_entry
->network
) {
153 LIST_REMOVE(static_fdb_entries
, fdb_entry
->network
->static_fdb_entries
, fdb_entry
);
155 assert(fdb_entry
->network
->n_static_fdb_entries
> 0);
156 fdb_entry
->network
->n_static_fdb_entries
--;
158 if (fdb_entry
->section
)
159 hashmap_remove(fdb_entry
->network
->fdb_entries_by_section
, UINT_TO_PTR(fdb_entry
->section
));
162 free(fdb_entry
->mac_addr
);
167 /* parse the HW address from config files. */
168 int config_parse_fdb_hwaddr(
170 const char *filename
,
173 unsigned section_line
,
180 Network
*network
= userdata
;
181 _cleanup_(fdb_entry_freep
) FdbEntry
*fdb_entry
= NULL
;
190 r
= fdb_entry_new_static(network
, section_line
, &fdb_entry
);
194 /* read in the MAC address for the FDB table. */
195 r
= sscanf(rvalue
, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
196 &fdb_entry
->mac_addr
->ether_addr_octet
[0],
197 &fdb_entry
->mac_addr
->ether_addr_octet
[1],
198 &fdb_entry
->mac_addr
->ether_addr_octet
[2],
199 &fdb_entry
->mac_addr
->ether_addr_octet
[3],
200 &fdb_entry
->mac_addr
->ether_addr_octet
[4],
201 &fdb_entry
->mac_addr
->ether_addr_octet
[5]);
203 if (ETHER_ADDR_LEN
!= r
) {
204 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Not a valid MAC address, ignoring assignment: %s", rvalue
);
213 /* parse the VLAN Id from config files. */
214 int config_parse_fdb_vlan_id(
216 const char *filename
,
219 unsigned section_line
,
226 Network
*network
= userdata
;
227 _cleanup_(fdb_entry_freep
) FdbEntry
*fdb_entry
= NULL
;
236 r
= fdb_entry_new_static(network
, section_line
, &fdb_entry
);
240 r
= config_parse_vlanid(unit
, filename
, line
, section
,
241 section_line
, lvalue
, ltype
,
242 rvalue
, &fdb_entry
->vlan_id
, userdata
);