diff options
Diffstat (limited to 'src/network')
-rw-r--r-- | src/network/networkd-fdb.c | 252 | ||||
-rw-r--r-- | src/network/networkd-link.c | 19 | ||||
-rw-r--r-- | src/network/networkd-network-gperf.gperf | 2 | ||||
-rw-r--r-- | src/network/networkd-network.c | 13 | ||||
-rw-r--r-- | src/network/networkd.h | 29 |
5 files changed, 314 insertions, 1 deletions
diff --git a/src/network/networkd-fdb.c b/src/network/networkd-fdb.c new file mode 100644 index 0000000000..9bb45e33d1 --- /dev/null +++ b/src/network/networkd-fdb.c @@ -0,0 +1,252 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright (C) 2014 Intel Corporation. All rights reserved. + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include <ctype.h> +#include <net/if.h> +#include <net/ethernet.h> + +#include "networkd.h" +#include "networkd-netdev.h" +#include "networkd-link.h" +#include "network-internal.h" +#include "path-util.h" +#include "conf-files.h" +#include "conf-parser.h" +#include "util.h" + +/* create a new FDB entry or get an existing one. */ +int fdb_entry_new_static(Network *const network, + const unsigned section, + FdbEntry **ret) { + _cleanup_fdbentry_free_ FdbEntry *fdb_entry = NULL; + struct ether_addr *mac_addr = NULL; + + assert(network); + + /* search entry in hashmap first. */ + if(section) { + fdb_entry = hashmap_get(network->fdb_entries_by_section, UINT_TO_PTR(section)); + if (fdb_entry) { + *ret = fdb_entry; + fdb_entry = NULL; + + return 0; + } + } + + /* allocate space for MAC address. */ + mac_addr = new0(struct ether_addr, 1); + if (!mac_addr) + return -ENOMEM; + + /* allocate space for and FDB entry. */ + fdb_entry = new0(FdbEntry, 1); + + if (!fdb_entry) { + /* free previously allocated space for mac_addr. */ + free(mac_addr); + return -ENOMEM; + } + + /* init FDB structure. */ + fdb_entry->network = network; + fdb_entry->mac_addr = mac_addr; + + LIST_PREPEND(static_fdb_entries, network->static_fdb_entries, fdb_entry); + + if (section) { + fdb_entry->section = section; + hashmap_put(network->fdb_entries_by_section, + UINT_TO_PTR(fdb_entry->section), fdb_entry); + } + + /* return allocated FDB structure. */ + *ret = fdb_entry; + fdb_entry = NULL; + + return 0; +} + +static int set_fdb_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { + _cleanup_fdbentry_free_ FdbEntry *fdb_entry = userdata; + int r; + + assert(fdb_entry); + + r = sd_rtnl_message_get_errno(m); + if ((r < 0) && (r != (-EEXIST))) + log_error("Could not add FDB entry for interface: %s error: %s", + fdb_entry->network->match_name, strerror(-r)); + + return 1; +} + +/* send a request to the kernel to add a FDB entry in its static MAC table. */ +int fdb_entry_configure(sd_rtnl *const rtnl, + FdbEntry *const fdb_entry, + const int ifindex) { + _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL; + int r; + + assert(fdb_entry); + assert(rtnl); + + /* create new RTM message */ + r = sd_rtnl_message_new_neigh(rtnl, &req, RTM_NEWNEIGH, ifindex, PF_BRIDGE); + if (r < 0) + return rtnl_log_create_error(r); + + /* only NTF_SELF flag supported. */ + r = sd_rtnl_message_neigh_set_flags(req, NTF_SELF); + if (r < 0) + return rtnl_log_create_error(r); + + /* only NUD_PERMANENT state supported. */ + r = sd_rtnl_message_neigh_set_state(req, NUD_NOARP | NUD_PERMANENT); + if (r < 0) + return rtnl_log_create_error(r); + + r = sd_rtnl_message_append_ether_addr(req, NDA_LLADDR, fdb_entry->mac_addr); + if (r < 0) + return rtnl_log_create_error(r); + + /* VLAN Id is optional. We'll add VLAN Id only if it's specified. */ + if (0 != fdb_entry->vlan_id) { + r = sd_rtnl_message_append_u16(req, NDA_VLAN, fdb_entry->vlan_id); + if (r < 0) + return rtnl_log_create_error(r); + } + + /* send message to the kernel to update its internal static MAC table. */ + r = sd_rtnl_call_async(rtnl, req, set_fdb_handler, fdb_entry, 0, NULL); + if (r < 0) { + log_error("Could not send rtnetlink message: %s", strerror(-r)); + return r; + } + + return 0; +} + +/* remove and FDB entry. */ +void fdb_entry_free(FdbEntry *fdb_entry) { + if(!fdb_entry) + return; + + if(fdb_entry->network) { + LIST_REMOVE(static_fdb_entries, fdb_entry->network->static_fdb_entries, + fdb_entry); + + if(fdb_entry->section) + hashmap_remove(fdb_entry->network->fdb_entries_by_section, + UINT_TO_PTR(fdb_entry->section)); + } + + free(fdb_entry->mac_addr); + + free(fdb_entry); +} + +/* parse the HW address from config files. */ +int config_parse_fdb_hwaddr(const char *unit, + const char *filename, + unsigned line, + const char *section, + unsigned section_line, + const char *lvalue, + int ltype, + const char *rvalue, + void *data, + void *userdata) { + Network *network = userdata; + _cleanup_fdbentry_free_ FdbEntry *fdb_entry = NULL; + int r; + + assert(filename); + assert(section); + assert(lvalue); + assert(rvalue); + assert(data); + + r = fdb_entry_new_static(network, section_line, &fdb_entry); + if (r < 0) { + log_error("Failed to allocate a new FDB entry: %s", strerror(-r)); + return r; + } + + /* read in the MAC address for the FDB table. */ + r = sscanf(rvalue, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", + &fdb_entry->mac_addr->ether_addr_octet[0], + &fdb_entry->mac_addr->ether_addr_octet[1], + &fdb_entry->mac_addr->ether_addr_octet[2], + &fdb_entry->mac_addr->ether_addr_octet[3], + &fdb_entry->mac_addr->ether_addr_octet[4], + &fdb_entry->mac_addr->ether_addr_octet[5]); + + if (ETHER_ADDR_LEN != r) { + log_syntax(unit, LOG_ERR, filename, line, EINVAL, + "Not a valid MAC address, ignoring assignment: %s", rvalue); + return 0; + } + + fdb_entry = NULL; + + return 0; +} + +/* parse the VLAN Id from config files. */ +int config_parse_fdb_vlan_id(const char *unit, + const char *filename, + unsigned line, + const char *section, + unsigned section_line, + const char *lvalue, + int ltype, + const char *rvalue, + void *data, + void *userdata) { + Network *network = userdata; + _cleanup_fdbentry_free_ FdbEntry *fdb_entry = NULL; + int r; + + assert(filename); + assert(section); + assert(lvalue); + assert(rvalue); + assert(data); + + r = fdb_entry_new_static(network, section_line, &fdb_entry); + if (r < 0) { + log_error("Failed to allocate a new FDB entry: %s", strerror(-r)); + return r; + } + + r = config_parse_unsigned(unit, filename, line, section, + section_line, lvalue, ltype, + rvalue, &fdb_entry->vlan_id, userdata); + if (r < 0) { + log_error("Failed to parse the unsigned integer: %s", strerror(-r)); + return r; + } + + fdb_entry = NULL; + + return 0; +} diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c index 08f724e127..341ae88ec1 100644 --- a/src/network/networkd-link.c +++ b/src/network/networkd-link.c @@ -656,6 +656,21 @@ int link_address_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) return 1; } +static int link_set_bridge_fdb(const Link *const link) { + FdbEntry *fdb_entry; + int r = 0; + + LIST_FOREACH(static_fdb_entries, fdb_entry, link->network->static_fdb_entries) { + r = fdb_entry_configure(link->manager->rtnl, fdb_entry, link->ifindex); + if(r < 0) { + log_link_error(link, "Failed to add MAC entry to static MAC table: %s", strerror(-r)); + break; + } + } + + return r; +} + static int link_set_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { _cleanup_link_unref_ Link *link = userdata; int r; @@ -1147,6 +1162,10 @@ static int link_configure(Link *link) { assert(link->network); assert(link->state == LINK_STATE_PENDING); + r = link_set_bridge_fdb(link); + if (r < 0) + return r; + if (link_ipv4ll_enabled(link)) { r = ipv4ll_configure(link); if (r < 0) diff --git a/src/network/networkd-network-gperf.gperf b/src/network/networkd-network-gperf.gperf index 640a3a20b8..fb0a209103 100644 --- a/src/network/networkd-network-gperf.gperf +++ b/src/network/networkd-network-gperf.gperf @@ -62,6 +62,8 @@ DHCP.CriticalConnection, config_parse_bool, 0, DHCP.VendorClassIdentifier, config_parse_string, 0, offsetof(Network, dhcp_vendor_class_identifier) DHCP.RouteMetric, config_parse_unsigned, 0, offsetof(Network, dhcp_route_metric) Bridge.Cost, config_parse_unsigned, 0, offsetof(Network, cost) +BridgeFDB.MACAddress, config_parse_fdb_hwaddr, 0, 0 +BridgeFDB.VLANId, config_parse_fdb_vlan_id, 0, 0 /* backwards compatibility: do not add new entries to this section */ DHCPv4.UseDNS, config_parse_bool, 0, offsetof(Network, dhcp_dns) DHCPv4.UseMTU, config_parse_bool, 0, offsetof(Network, dhcp_mtu) diff --git a/src/network/networkd-network.c b/src/network/networkd-network.c index ef9e0a8c35..966b59b878 100644 --- a/src/network/networkd-network.c +++ b/src/network/networkd-network.c @@ -62,6 +62,7 @@ static int network_load_one(Manager *manager, const char *filename) { LIST_HEAD_INIT(network->static_addresses); LIST_HEAD_INIT(network->static_routes); + LIST_HEAD_INIT(network->static_fdb_entries); network->stacked_netdevs = hashmap_new(&string_hash_ops); if (!network->stacked_netdevs) @@ -75,6 +76,10 @@ static int network_load_one(Manager *manager, const char *filename) { if (!network->routes_by_section) return log_oom(); + network->fdb_entries_by_section = hashmap_new(NULL); + if (!network->fdb_entries_by_section) + return log_oom(); + network->filename = strdup(filename); if (!network->filename) return log_oom(); @@ -97,7 +102,8 @@ static int network_load_one(Manager *manager, const char *filename) { "Route\0" "DHCP\0" "DHCPv4\0" - "Bridge\0", + "Bridge\0" + "BridgeFDB\0", config_item_perf_lookup, network_network_gperf_lookup, false, false, true, network); if (r < 0) @@ -154,6 +160,7 @@ void network_free(Network *network) { NetDev *netdev; Route *route; Address *address; + FdbEntry *fdb_entry; Iterator i; if (!network) @@ -192,8 +199,12 @@ void network_free(Network *network) { while ((address = network->static_addresses)) address_free(address); + while ((fdb_entry = network->static_fdb_entries)) + fdb_entry_free(fdb_entry); + hashmap_free(network->addresses_by_section); hashmap_free(network->routes_by_section); + hashmap_free(network->fdb_entries_by_section); if (network->manager && network->manager->networks) LIST_REMOVE(networks, network->manager->networks, network); diff --git a/src/network/networkd.h b/src/network/networkd.h index 4cdcd73c5d..a5c5b085ba 100644 --- a/src/network/networkd.h +++ b/src/network/networkd.h @@ -51,6 +51,7 @@ typedef struct Address Address; typedef struct Route Route; typedef struct Manager Manager; typedef struct AddressPool AddressPool; +typedef struct FdbEntry FdbEntry; typedef enum DHCPSupport { DHCP_SUPPORT_NONE, @@ -69,6 +70,16 @@ typedef enum LLMNRSupport { _LLMNR_SUPPORT_INVALID = -1, } LLMNRSupport; +struct FdbEntry { + Network *network; + unsigned section; + + struct ether_addr *mac_addr; + uint16_t vlan_id; + + LIST_FIELDS(FdbEntry, static_fdb_entries); +}; + struct Network { Manager *manager; @@ -113,9 +124,11 @@ struct Network { LIST_HEAD(Address, static_addresses); LIST_HEAD(Route, static_routes); + LIST_HEAD(FdbEntry, static_fdb_entries); Hashmap *addresses_by_section; Hashmap *routes_by_section; + Hashmap *fdb_entries_by_section; bool wildcard_domain; char **domains, **dns, **ntp; @@ -327,6 +340,22 @@ int config_parse_label(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata); +/* Forwarding database table. */ +int fdb_entry_configure(sd_rtnl *const rtnl, FdbEntry *const fdb_entry, const int ifindex); +void fdb_entry_free(FdbEntry *fdb_entry); +int fdb_entry_new_static(Network *const network, const unsigned section, FdbEntry **ret); + +DEFINE_TRIVIAL_CLEANUP_FUNC(FdbEntry*, fdb_entry_free); +#define _cleanup_fdbentry_free_ _cleanup_(fdb_entry_freep) + +int config_parse_fdb_hwaddr(const char *unit, const char *filename, unsigned line, + const char *section, unsigned section_line, const char *lvalue, + int ltype, const char *rvalue, void *data, void *userdata); + +int config_parse_fdb_vlan_id(const char *unit, const char *filename, unsigned line, + const char *section, unsigned section_line, const char *lvalue, + int ltype, const char *rvalue, void *data, void *userdata); + /* DHCP support */ const char* dhcp_support_to_string(DHCPSupport i) _const_; |