2 This file is part of systemd.
4 Copyright (C) 2014 Intel Corporation. All rights reserved.
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <net/ethernet.h>
23 #include "alloc-util.h"
24 #include "conf-parser.h"
25 #include "netlink-util.h"
26 #include "networkd-fdb.h"
30 /* create a new FDB entry or get an existing one. */
31 int fdb_entry_new_static(Network
*const network
,
32 const unsigned section
,
34 _cleanup_fdbentry_free_ FdbEntry
*fdb_entry
= NULL
;
35 struct ether_addr
*mac_addr
= NULL
;
39 /* search entry in hashmap first. */
41 fdb_entry
= hashmap_get(network
->fdb_entries_by_section
, UINT_TO_PTR(section
));
50 /* allocate space for MAC address. */
51 mac_addr
= new0(struct ether_addr
, 1);
55 /* allocate space for and FDB entry. */
56 fdb_entry
= new0(FdbEntry
, 1);
59 /* free previously allocated space for mac_addr. */
64 /* init FDB structure. */
65 fdb_entry
->network
= network
;
66 fdb_entry
->mac_addr
= mac_addr
;
68 LIST_PREPEND(static_fdb_entries
, network
->static_fdb_entries
, fdb_entry
);
71 fdb_entry
->section
= section
;
72 hashmap_put(network
->fdb_entries_by_section
,
73 UINT_TO_PTR(fdb_entry
->section
), fdb_entry
);
76 /* return allocated FDB structure. */
83 static int set_fdb_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
84 Link
*link
= userdata
;
89 r
= sd_netlink_message_get_errno(m
);
90 if (r
< 0 && r
!= -EEXIST
)
91 log_link_error_errno(link
, r
, "Could not add FDB entry: %m");
96 /* send a request to the kernel to add a FDB entry in its static MAC table. */
97 int fdb_entry_configure(Link
*const link
, FdbEntry
*const fdb_entry
) {
98 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
103 assert(link
->manager
);
106 rtnl
= link
->manager
->rtnl
;
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
);
113 /* only NTF_SELF flag supported. */
114 r
= sd_rtnl_message_neigh_set_flags(req
, NTF_SELF
);
116 return rtnl_log_create_error(r
);
118 /* only NUD_PERMANENT state supported. */
119 r
= sd_rtnl_message_neigh_set_state(req
, NUD_NOARP
| NUD_PERMANENT
);
121 return rtnl_log_create_error(r
);
123 r
= sd_netlink_message_append_ether_addr(req
, NDA_LLADDR
, fdb_entry
->mac_addr
);
125 return rtnl_log_create_error(r
);
127 /* VLAN Id is optional. We'll add VLAN Id only if it's specified. */
128 if (0 != fdb_entry
->vlan_id
) {
129 r
= sd_netlink_message_append_u16(req
, NDA_VLAN
, fdb_entry
->vlan_id
);
131 return rtnl_log_create_error(r
);
134 /* send message to the kernel to update its internal static MAC table. */
135 r
= sd_netlink_call_async(rtnl
, req
, set_fdb_handler
, link
, 0, NULL
);
137 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
142 /* remove and FDB entry. */
143 void fdb_entry_free(FdbEntry
*fdb_entry
) {
147 if (fdb_entry
->network
) {
148 LIST_REMOVE(static_fdb_entries
, fdb_entry
->network
->static_fdb_entries
,
151 if (fdb_entry
->section
)
152 hashmap_remove(fdb_entry
->network
->fdb_entries_by_section
,
153 UINT_TO_PTR(fdb_entry
->section
));
156 free(fdb_entry
->mac_addr
);
161 /* parse the HW address from config files. */
162 int config_parse_fdb_hwaddr(
164 const char *filename
,
167 unsigned section_line
,
174 Network
*network
= userdata
;
175 _cleanup_fdbentry_free_ FdbEntry
*fdb_entry
= NULL
;
184 r
= fdb_entry_new_static(network
, section_line
, &fdb_entry
);
188 /* read in the MAC address for the FDB table. */
189 r
= sscanf(rvalue
, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
190 &fdb_entry
->mac_addr
->ether_addr_octet
[0],
191 &fdb_entry
->mac_addr
->ether_addr_octet
[1],
192 &fdb_entry
->mac_addr
->ether_addr_octet
[2],
193 &fdb_entry
->mac_addr
->ether_addr_octet
[3],
194 &fdb_entry
->mac_addr
->ether_addr_octet
[4],
195 &fdb_entry
->mac_addr
->ether_addr_octet
[5]);
197 if (ETHER_ADDR_LEN
!= r
) {
198 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Not a valid MAC address, ignoring assignment: %s", rvalue
);
207 /* parse the VLAN Id from config files. */
208 int config_parse_fdb_vlan_id(
210 const char *filename
,
213 unsigned section_line
,
220 Network
*network
= userdata
;
221 _cleanup_fdbentry_free_ FdbEntry
*fdb_entry
= NULL
;
230 r
= fdb_entry_new_static(network
, section_line
, &fdb_entry
);
234 r
= config_parse_unsigned(unit
, filename
, line
, section
,
235 section_line
, lvalue
, ltype
,
236 rvalue
, &fdb_entry
->vlan_id
, userdata
);