1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright (C) 2014 Intel Corporation. All rights reserved.
8 #include <net/ethernet.h>
11 #include "alloc-util.h"
12 #include "conf-parser.h"
13 #include "netdev/bridge.h"
14 #include "netlink-util.h"
15 #include "networkd-fdb.h"
16 #include "networkd-manager.h"
18 #include "vlan-util.h"
20 #define STATIC_FDB_ENTRIES_PER_NETWORK_MAX 1024U
22 /* create a new FDB entry or get an existing one. */
23 int fdb_entry_new_static(
28 _cleanup_fdbentry_free_ FdbEntry
*fdb_entry
= NULL
;
29 struct ether_addr
*mac_addr
= NULL
;
34 /* search entry in hashmap first. */
36 fdb_entry
= hashmap_get(network
->fdb_entries_by_section
, UINT_TO_PTR(section
));
38 *ret
= TAKE_PTR(fdb_entry
);
44 if (network
->n_static_fdb_entries
>= STATIC_FDB_ENTRIES_PER_NETWORK_MAX
)
47 /* allocate space for MAC address. */
48 mac_addr
= new0(struct ether_addr
, 1);
52 /* allocate space for and FDB entry. */
53 fdb_entry
= new0(FdbEntry
, 1);
55 /* free previously allocated space for mac_addr. */
60 /* init FDB structure. */
61 fdb_entry
->network
= network
;
62 fdb_entry
->mac_addr
= mac_addr
;
64 LIST_PREPEND(static_fdb_entries
, network
->static_fdb_entries
, fdb_entry
);
65 network
->n_static_fdb_entries
++;
68 fdb_entry
->section
= section
;
69 hashmap_put(network
->fdb_entries_by_section
,
70 UINT_TO_PTR(fdb_entry
->section
), fdb_entry
);
73 /* return allocated FDB structure. */
74 *ret
= TAKE_PTR(fdb_entry
);
79 static int set_fdb_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
80 Link
*link
= userdata
;
85 r
= sd_netlink_message_get_errno(m
);
86 if (r
< 0 && r
!= -EEXIST
)
87 log_link_error_errno(link
, r
, "Could not add FDB entry: %m");
92 /* send a request to the kernel to add a FDB entry in its static MAC table. */
93 int fdb_entry_configure(Link
*link
, FdbEntry
*fdb_entry
) {
94 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
101 assert(link
->network
);
102 assert(link
->manager
);
105 rtnl
= link
->manager
->rtnl
;
106 bridge
= BRIDGE(link
->network
->bridge
);
108 /* create new RTM message */
109 r
= sd_rtnl_message_new_neigh(rtnl
, &req
, RTM_NEWNEIGH
, link
->ifindex
, PF_BRIDGE
);
111 return rtnl_log_create_error(r
);
118 r
= sd_rtnl_message_neigh_set_flags(req
, flags
);
120 return rtnl_log_create_error(r
);
122 /* only NUD_PERMANENT state supported. */
123 r
= sd_rtnl_message_neigh_set_state(req
, NUD_NOARP
| NUD_PERMANENT
);
125 return rtnl_log_create_error(r
);
127 r
= sd_netlink_message_append_ether_addr(req
, NDA_LLADDR
, fdb_entry
->mac_addr
);
129 return rtnl_log_create_error(r
);
131 /* VLAN Id is optional. We'll add VLAN Id only if it's specified. */
132 if (0 != fdb_entry
->vlan_id
) {
133 r
= sd_netlink_message_append_u16(req
, NDA_VLAN
, fdb_entry
->vlan_id
);
135 return rtnl_log_create_error(r
);
138 /* send message to the kernel to update its internal static MAC table. */
139 r
= sd_netlink_call_async(rtnl
, req
, set_fdb_handler
, link
, 0, NULL
);
141 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
146 /* remove and FDB entry. */
147 void fdb_entry_free(FdbEntry
*fdb_entry
) {
151 if (fdb_entry
->network
) {
152 LIST_REMOVE(static_fdb_entries
, fdb_entry
->network
->static_fdb_entries
, fdb_entry
);
154 assert(fdb_entry
->network
->n_static_fdb_entries
> 0);
155 fdb_entry
->network
->n_static_fdb_entries
--;
157 if (fdb_entry
->section
)
158 hashmap_remove(fdb_entry
->network
->fdb_entries_by_section
, UINT_TO_PTR(fdb_entry
->section
));
161 free(fdb_entry
->mac_addr
);
166 /* parse the HW address from config files. */
167 int config_parse_fdb_hwaddr(
169 const char *filename
,
172 unsigned section_line
,
179 Network
*network
= userdata
;
180 _cleanup_fdbentry_free_ FdbEntry
*fdb_entry
= NULL
;
189 r
= fdb_entry_new_static(network
, section_line
, &fdb_entry
);
193 /* read in the MAC address for the FDB table. */
194 r
= sscanf(rvalue
, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
195 &fdb_entry
->mac_addr
->ether_addr_octet
[0],
196 &fdb_entry
->mac_addr
->ether_addr_octet
[1],
197 &fdb_entry
->mac_addr
->ether_addr_octet
[2],
198 &fdb_entry
->mac_addr
->ether_addr_octet
[3],
199 &fdb_entry
->mac_addr
->ether_addr_octet
[4],
200 &fdb_entry
->mac_addr
->ether_addr_octet
[5]);
202 if (ETHER_ADDR_LEN
!= r
) {
203 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Not a valid MAC address, ignoring assignment: %s", rvalue
);
212 /* parse the VLAN Id from config files. */
213 int config_parse_fdb_vlan_id(
215 const char *filename
,
218 unsigned section_line
,
225 Network
*network
= userdata
;
226 _cleanup_fdbentry_free_ FdbEntry
*fdb_entry
= NULL
;
235 r
= fdb_entry_new_static(network
, section_line
, &fdb_entry
);
239 r
= config_parse_vlanid(unit
, filename
, line
, section
,
240 section_line
, lvalue
, ltype
,
241 rvalue
, &fdb_entry
->vlan_id
, userdata
);