1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright (C) 2014 Intel Corporation. All rights reserved.
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include <net/ethernet.h>
27 #include "networkd-netdev.h"
28 #include "networkd-link.h"
29 #include "network-internal.h"
30 #include "path-util.h"
31 #include "conf-files.h"
32 #include "conf-parser.h"
35 /* create a new FDB entry or get an existing one. */
36 int fdb_entry_new_static(Network *const network,
37 const unsigned section,
39 _cleanup_fdbentry_free_ FdbEntry *fdb_entry = NULL;
40 struct ether_addr *mac_addr = NULL;
44 /* search entry in hashmap first. */
46 fdb_entry = hashmap_get(network->fdb_entries_by_section, UINT_TO_PTR(section));
55 /* allocate space for MAC address. */
56 mac_addr = new0(struct ether_addr, 1);
60 /* allocate space for and FDB entry. */
61 fdb_entry = new0(FdbEntry, 1);
64 /* free previously allocated space for mac_addr. */
69 /* init FDB structure. */
70 fdb_entry->network = network;
71 fdb_entry->mac_addr = mac_addr;
73 LIST_PREPEND(static_fdb_entries, network->static_fdb_entries, fdb_entry);
76 fdb_entry->section = section;
77 hashmap_put(network->fdb_entries_by_section,
78 UINT_TO_PTR(fdb_entry->section), fdb_entry);
81 /* return allocated FDB structure. */
88 static int set_fdb_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
89 Link *link = userdata;
94 r = sd_rtnl_message_get_errno(m);
95 if (r < 0 && r != -EEXIST)
96 log_link_error(link, "Could not add FDB entry: %s", strerror(-r));
101 /* send a request to the kernel to add a FDB entry in its static MAC table. */
102 int fdb_entry_configure(Link *link,
103 FdbEntry *const fdb_entry) {
104 _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
109 assert(link->manager);
112 rtnl = link->manager->rtnl;
114 /* create new RTM message */
115 r = sd_rtnl_message_new_neigh(rtnl, &req, RTM_NEWNEIGH, link->ifindex, PF_BRIDGE);
117 return rtnl_log_create_error(r);
119 /* only NTF_SELF flag supported. */
120 r = sd_rtnl_message_neigh_set_flags(req, NTF_SELF);
122 return rtnl_log_create_error(r);
124 /* only NUD_PERMANENT state supported. */
125 r = sd_rtnl_message_neigh_set_state(req, NUD_NOARP | NUD_PERMANENT);
127 return rtnl_log_create_error(r);
129 r = sd_rtnl_message_append_ether_addr(req, NDA_LLADDR, fdb_entry->mac_addr);
131 return rtnl_log_create_error(r);
133 /* VLAN Id is optional. We'll add VLAN Id only if it's specified. */
134 if (0 != fdb_entry->vlan_id) {
135 r = sd_rtnl_message_append_u16(req, NDA_VLAN, fdb_entry->vlan_id);
137 return rtnl_log_create_error(r);
140 /* send message to the kernel to update its internal static MAC table. */
141 r = sd_rtnl_call_async(rtnl, req, set_fdb_handler, link, 0, NULL);
143 log_link_error(link, "Could not send rtnetlink message: %s", strerror(-r));
150 /* remove and FDB entry. */
151 void fdb_entry_free(FdbEntry *fdb_entry) {
155 if(fdb_entry->network) {
156 LIST_REMOVE(static_fdb_entries, fdb_entry->network->static_fdb_entries,
159 if(fdb_entry->section)
160 hashmap_remove(fdb_entry->network->fdb_entries_by_section,
161 UINT_TO_PTR(fdb_entry->section));
164 free(fdb_entry->mac_addr);
169 /* parse the HW address from config files. */
170 int config_parse_fdb_hwaddr(const char *unit,
171 const char *filename,
174 unsigned section_line,
180 Network *network = userdata;
181 _cleanup_fdbentry_free_ FdbEntry *fdb_entry = NULL;
190 r = fdb_entry_new_static(network, section_line, &fdb_entry);
192 log_error("Failed to allocate a new FDB entry: %s", strerror(-r));
196 /* read in the MAC address for the FDB table. */
197 r = sscanf(rvalue, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
198 &fdb_entry->mac_addr->ether_addr_octet[0],
199 &fdb_entry->mac_addr->ether_addr_octet[1],
200 &fdb_entry->mac_addr->ether_addr_octet[2],
201 &fdb_entry->mac_addr->ether_addr_octet[3],
202 &fdb_entry->mac_addr->ether_addr_octet[4],
203 &fdb_entry->mac_addr->ether_addr_octet[5]);
205 if (ETHER_ADDR_LEN != r) {
206 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
207 "Not a valid MAC address, ignoring assignment: %s", rvalue);
216 /* parse the VLAN Id from config files. */
217 int config_parse_fdb_vlan_id(const char *unit,
218 const char *filename,
221 unsigned section_line,
227 Network *network = userdata;
228 _cleanup_fdbentry_free_ FdbEntry *fdb_entry = NULL;
237 r = fdb_entry_new_static(network, section_line, &fdb_entry);
239 log_error("Failed to allocate a new FDB entry: %s", strerror(-r));
243 r = config_parse_unsigned(unit, filename, line, section,
244 section_line, lvalue, ltype,
245 rvalue, &fdb_entry->vlan_id, userdata);
247 log_error("Failed to parse the unsigned integer: %s", strerror(-r));