1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright (C) 2014 Intel Corporation. All rights reserved.
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include <net/ethernet.h>
24 #include "alloc-util.h"
25 #include "conf-parser.h"
26 #include "netdev/bridge.h"
27 #include "netlink-util.h"
28 #include "networkd-fdb.h"
29 #include "networkd-manager.h"
31 #include "vlan-util.h"
33 #define STATIC_FDB_ENTRIES_PER_NETWORK_MAX 1024U
35 /* create a new FDB entry or get an existing one. */
36 int fdb_entry_new_static(
41 _cleanup_fdbentry_free_ FdbEntry
*fdb_entry
= NULL
;
42 struct ether_addr
*mac_addr
= NULL
;
47 /* search entry in hashmap first. */
49 fdb_entry
= hashmap_get(network
->fdb_entries_by_section
, UINT_TO_PTR(section
));
51 *ret
= TAKE_PTR(fdb_entry
);
57 if (network
->n_static_fdb_entries
>= STATIC_FDB_ENTRIES_PER_NETWORK_MAX
)
60 /* allocate space for MAC address. */
61 mac_addr
= new0(struct ether_addr
, 1);
65 /* allocate space for and FDB entry. */
66 fdb_entry
= new0(FdbEntry
, 1);
68 /* free previously allocated space for mac_addr. */
73 /* init FDB structure. */
74 fdb_entry
->network
= network
;
75 fdb_entry
->mac_addr
= mac_addr
;
77 LIST_PREPEND(static_fdb_entries
, network
->static_fdb_entries
, fdb_entry
);
78 network
->n_static_fdb_entries
++;
81 fdb_entry
->section
= section
;
82 hashmap_put(network
->fdb_entries_by_section
,
83 UINT_TO_PTR(fdb_entry
->section
), fdb_entry
);
86 /* return allocated FDB structure. */
87 *ret
= TAKE_PTR(fdb_entry
);
92 static int set_fdb_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
93 Link
*link
= userdata
;
98 r
= sd_netlink_message_get_errno(m
);
99 if (r
< 0 && r
!= -EEXIST
)
100 log_link_error_errno(link
, r
, "Could not add FDB entry: %m");
105 /* send a request to the kernel to add a FDB entry in its static MAC table. */
106 int fdb_entry_configure(Link
*link
, FdbEntry
*fdb_entry
) {
107 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
114 assert(link
->network
);
115 assert(link
->manager
);
118 rtnl
= link
->manager
->rtnl
;
119 bridge
= BRIDGE(link
->network
->bridge
);
121 /* create new RTM message */
122 r
= sd_rtnl_message_new_neigh(rtnl
, &req
, RTM_NEWNEIGH
, link
->ifindex
, PF_BRIDGE
);
124 return rtnl_log_create_error(r
);
131 r
= sd_rtnl_message_neigh_set_flags(req
, flags
);
133 return rtnl_log_create_error(r
);
135 /* only NUD_PERMANENT state supported. */
136 r
= sd_rtnl_message_neigh_set_state(req
, NUD_NOARP
| NUD_PERMANENT
);
138 return rtnl_log_create_error(r
);
140 r
= sd_netlink_message_append_ether_addr(req
, NDA_LLADDR
, fdb_entry
->mac_addr
);
142 return rtnl_log_create_error(r
);
144 /* VLAN Id is optional. We'll add VLAN Id only if it's specified. */
145 if (0 != fdb_entry
->vlan_id
) {
146 r
= sd_netlink_message_append_u16(req
, NDA_VLAN
, fdb_entry
->vlan_id
);
148 return rtnl_log_create_error(r
);
151 /* send message to the kernel to update its internal static MAC table. */
152 r
= sd_netlink_call_async(rtnl
, req
, set_fdb_handler
, link
, 0, NULL
);
154 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
);
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
, UINT_TO_PTR(fdb_entry
->section
));
174 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_fdbentry_free_ FdbEntry
*fdb_entry
= NULL
;
202 r
= fdb_entry_new_static(network
, 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 (ETHER_ADDR_LEN
!= r
) {
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_fdbentry_free_ FdbEntry
*fdb_entry
= NULL
;
248 r
= fdb_entry_new_static(network
, 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
);