2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2014-12-17 16:35:36 +01:00
|
|
|
/***
|
2018-06-12 17:15:23 +02:00
|
|
|
Copyright © 2014 Intel Corporation. All rights reserved.
|
2014-12-17 16:35:36 +01:00
|
|
|
***/
|
|
|
|
|
|
|
|
#include <net/ethernet.h>
|
2015-11-16 22:09:36 +01:00
|
|
|
#include <net/if.h>
|
2014-12-17 16:35:36 +01:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2014-12-17 16:35:36 +01:00
|
|
|
#include "conf-parser.h"
|
2016-11-13 04:59:06 +01:00
|
|
|
#include "netdev/bridge.h"
|
2015-08-27 13:59:06 +02:00
|
|
|
#include "netlink-util.h"
|
|
|
|
#include "networkd-fdb.h"
|
2016-11-13 04:59:06 +01:00
|
|
|
#include "networkd-manager.h"
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "util.h"
|
2016-06-12 20:01:14 +02:00
|
|
|
#include "vlan-util.h"
|
2014-12-17 16:35:36 +01:00
|
|
|
|
2016-06-03 19:14:12 +02:00
|
|
|
#define STATIC_FDB_ENTRIES_PER_NETWORK_MAX 1024U
|
|
|
|
|
2014-12-17 16:35:36 +01:00
|
|
|
/* create a new FDB entry or get an existing one. */
|
2019-03-01 05:19:53 +01:00
|
|
|
static int fdb_entry_new_static(
|
2016-06-03 19:14:12 +02:00
|
|
|
Network *network,
|
2018-11-12 06:54:04 +01:00
|
|
|
const char *filename,
|
|
|
|
unsigned section_line,
|
2016-06-03 19:14:12 +02:00
|
|
|
FdbEntry **ret) {
|
|
|
|
|
2018-11-12 06:54:04 +01:00
|
|
|
_cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL;
|
tree-wide: drop redundant _cleanup_ macros (#8810)
This drops a good number of type-specific _cleanup_ macros, and patches
all users to just use the generic ones.
In most recent code we abstained from defining type-specific macros, and
this basically removes all those added already, with the exception of
the really low-level ones.
Having explicit macros for this is not too useful, as the expression
without the extra macro is generally just 2ch wider. We should generally
emphesize generic code, unless there are really good reasons for
specific code, hence let's follow this in this case too.
Note that _cleanup_free_ and similar really low-level, libc'ish, Linux
API'ish macros continue to be defined, only the really high-level OO
ones are dropped. From now on this should really be the rule: for really
low-level stuff, such as memory allocation, fd handling and so one, go
ahead and define explicit per-type macros, but for high-level, specific
program code, just use the generic _cleanup_() macro directly, in order
to keep things simple and as readable as possible for the uninitiated.
Note that before this patch some of the APIs (notable libudev ones) were
already used with the high-level macros at some places and with the
generic _cleanup_ macro at others. With this patch we hence unify on the
latter.
2018-04-25 12:31:45 +02:00
|
|
|
_cleanup_(fdb_entry_freep) FdbEntry *fdb_entry = NULL;
|
2018-11-12 06:54:04 +01:00
|
|
|
_cleanup_free_ struct ether_addr *mac_addr = NULL;
|
|
|
|
int r;
|
2014-12-17 16:35:36 +01:00
|
|
|
|
|
|
|
assert(network);
|
2016-06-03 19:14:12 +02:00
|
|
|
assert(ret);
|
2018-11-12 06:54:04 +01:00
|
|
|
assert(!!filename == (section_line > 0));
|
2014-12-17 16:35:36 +01:00
|
|
|
|
|
|
|
/* search entry in hashmap first. */
|
2018-11-12 06:54:04 +01:00
|
|
|
if (filename) {
|
|
|
|
r = network_config_section_new(filename, section_line, &n);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
fdb_entry = hashmap_get(network->fdb_entries_by_section, n);
|
2014-12-17 16:35:36 +01:00
|
|
|
if (fdb_entry) {
|
2018-04-05 07:26:26 +02:00
|
|
|
*ret = TAKE_PTR(fdb_entry);
|
2014-12-17 16:35:36 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 19:14:12 +02:00
|
|
|
if (network->n_static_fdb_entries >= STATIC_FDB_ENTRIES_PER_NETWORK_MAX)
|
|
|
|
return -E2BIG;
|
|
|
|
|
2014-12-17 16:35:36 +01:00
|
|
|
/* allocate space for MAC address. */
|
|
|
|
mac_addr = new0(struct ether_addr, 1);
|
|
|
|
if (!mac_addr)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/* allocate space for and FDB entry. */
|
2018-11-12 06:54:04 +01:00
|
|
|
fdb_entry = new(FdbEntry, 1);
|
|
|
|
if (!fdb_entry)
|
2014-12-17 16:35:36 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/* init FDB structure. */
|
2018-11-12 06:54:04 +01:00
|
|
|
*fdb_entry = (FdbEntry) {
|
|
|
|
.network = network,
|
|
|
|
.mac_addr = TAKE_PTR(mac_addr),
|
|
|
|
};
|
2014-12-17 16:35:36 +01:00
|
|
|
|
|
|
|
LIST_PREPEND(static_fdb_entries, network->static_fdb_entries, fdb_entry);
|
2016-06-03 19:14:12 +02:00
|
|
|
network->n_static_fdb_entries++;
|
2014-12-17 16:35:36 +01:00
|
|
|
|
2018-11-12 06:54:04 +01:00
|
|
|
if (filename) {
|
|
|
|
fdb_entry->section = TAKE_PTR(n);
|
|
|
|
|
2018-11-12 07:24:11 +01:00
|
|
|
r = hashmap_ensure_allocated(&network->fdb_entries_by_section, &network_config_hash_ops);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2018-11-12 06:54:04 +01:00
|
|
|
r = hashmap_put(network->fdb_entries_by_section, fdb_entry->section, fdb_entry);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-12-17 16:35:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* return allocated FDB structure. */
|
2018-04-05 07:26:26 +02:00
|
|
|
*ret = TAKE_PTR(fdb_entry);
|
2014-12-17 16:35:36 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-28 21:06:52 +01:00
|
|
|
static int set_fdb_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
|
2014-12-17 16:35:36 +01:00
|
|
|
int r;
|
|
|
|
|
2015-02-10 20:40:41 +01:00
|
|
|
assert(link);
|
2014-12-17 16:35:36 +01:00
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_get_errno(m);
|
2015-02-10 20:40:41 +01:00
|
|
|
if (r < 0 && r != -EEXIST)
|
2015-04-21 17:40:18 +02:00
|
|
|
log_link_error_errno(link, r, "Could not add FDB entry: %m");
|
2014-12-17 16:35:36 +01:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send a request to the kernel to add a FDB entry in its static MAC table. */
|
2016-06-03 19:20:46 +02:00
|
|
|
int fdb_entry_configure(Link *link, FdbEntry *fdb_entry) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
|
2015-06-12 16:31:33 +02:00
|
|
|
sd_netlink *rtnl;
|
2014-12-17 16:35:36 +01:00
|
|
|
int r;
|
2016-10-05 17:06:40 +02:00
|
|
|
uint8_t flags;
|
|
|
|
Bridge *bridge;
|
2014-12-17 16:35:36 +01:00
|
|
|
|
2015-02-10 20:40:41 +01:00
|
|
|
assert(link);
|
2016-10-05 17:06:40 +02:00
|
|
|
assert(link->network);
|
2015-02-10 20:40:41 +01:00
|
|
|
assert(link->manager);
|
2014-12-17 16:35:36 +01:00
|
|
|
assert(fdb_entry);
|
2015-02-10 20:40:41 +01:00
|
|
|
|
|
|
|
rtnl = link->manager->rtnl;
|
2016-10-05 17:06:40 +02:00
|
|
|
bridge = BRIDGE(link->network->bridge);
|
2014-12-17 16:35:36 +01:00
|
|
|
|
|
|
|
/* create new RTM message */
|
2015-02-10 20:40:41 +01:00
|
|
|
r = sd_rtnl_message_new_neigh(rtnl, &req, RTM_NEWNEIGH, link->ifindex, PF_BRIDGE);
|
2014-12-17 16:35:36 +01:00
|
|
|
if (r < 0)
|
|
|
|
return rtnl_log_create_error(r);
|
|
|
|
|
2016-10-05 17:06:40 +02:00
|
|
|
if (bridge)
|
|
|
|
flags = NTF_MASTER;
|
|
|
|
else
|
|
|
|
flags = NTF_SELF;
|
|
|
|
|
|
|
|
r = sd_rtnl_message_neigh_set_flags(req, flags);
|
2014-12-17 16:35:36 +01:00
|
|
|
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);
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_append_ether_addr(req, NDA_LLADDR, fdb_entry->mac_addr);
|
2014-12-17 16:35:36 +01:00
|
|
|
if (r < 0)
|
|
|
|
return rtnl_log_create_error(r);
|
|
|
|
|
|
|
|
/* VLAN Id is optional. We'll add VLAN Id only if it's specified. */
|
2019-04-30 09:36:10 +02:00
|
|
|
if (fdb_entry->vlan_id > 0) {
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_append_u16(req, NDA_VLAN, fdb_entry->vlan_id);
|
2014-12-17 16:35:36 +01:00
|
|
|
if (r < 0)
|
|
|
|
return rtnl_log_create_error(r);
|
|
|
|
}
|
|
|
|
|
2019-04-30 11:23:39 +02:00
|
|
|
if (!in_addr_is_null(fdb_entry->family, &fdb_entry->destination_addr)) {
|
|
|
|
r = netlink_message_append_in_addr_union(req, NDA_DST, fdb_entry->family, &fdb_entry->destination_addr);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Could not append NDA_DST attribute: %m");
|
|
|
|
}
|
|
|
|
|
2014-12-17 16:35:36 +01:00
|
|
|
/* send message to the kernel to update its internal static MAC table. */
|
2018-11-28 21:06:52 +01:00
|
|
|
r = netlink_call_async(rtnl, NULL, req, set_fdb_handler,
|
|
|
|
link_netlink_destroy_callback, link);
|
2015-04-21 17:40:18 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
|
2014-12-17 16:35:36 +01:00
|
|
|
|
2018-10-06 06:55:19 +02:00
|
|
|
link_ref(link);
|
|
|
|
|
2014-12-17 16:35:36 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove and FDB entry. */
|
|
|
|
void fdb_entry_free(FdbEntry *fdb_entry) {
|
2016-02-23 18:52:52 +01:00
|
|
|
if (!fdb_entry)
|
2014-12-17 16:35:36 +01:00
|
|
|
return;
|
|
|
|
|
2016-02-23 18:52:52 +01:00
|
|
|
if (fdb_entry->network) {
|
2016-06-03 19:14:12 +02:00
|
|
|
LIST_REMOVE(static_fdb_entries, fdb_entry->network->static_fdb_entries, fdb_entry);
|
|
|
|
assert(fdb_entry->network->n_static_fdb_entries > 0);
|
|
|
|
fdb_entry->network->n_static_fdb_entries--;
|
2014-12-17 16:35:36 +01:00
|
|
|
|
2015-04-21 17:43:31 +02:00
|
|
|
if (fdb_entry->section)
|
2018-11-12 06:54:04 +01:00
|
|
|
hashmap_remove(fdb_entry->network->fdb_entries_by_section, fdb_entry->section);
|
2014-12-17 16:35:36 +01:00
|
|
|
}
|
|
|
|
|
2018-11-12 06:54:04 +01:00
|
|
|
network_config_section_free(fdb_entry->section);
|
2014-12-17 16:35:36 +01:00
|
|
|
free(fdb_entry->mac_addr);
|
|
|
|
free(fdb_entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse the HW address from config files. */
|
2015-04-21 17:43:31 +02:00
|
|
|
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) {
|
|
|
|
|
2014-12-17 16:35:36 +01:00
|
|
|
Network *network = userdata;
|
2019-03-01 05:27:47 +01:00
|
|
|
_cleanup_(fdb_entry_free_or_set_invalidp) FdbEntry *fdb_entry = NULL;
|
2014-12-17 16:35:36 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(filename);
|
|
|
|
assert(section);
|
|
|
|
assert(lvalue);
|
|
|
|
assert(rvalue);
|
|
|
|
assert(data);
|
|
|
|
|
2018-11-12 06:54:04 +01:00
|
|
|
r = fdb_entry_new_static(network, filename, section_line, &fdb_entry);
|
2015-04-21 17:40:18 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_oom();
|
2014-12-17 16:35:36 +01:00
|
|
|
|
|
|
|
/* 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]);
|
|
|
|
|
2018-11-01 19:13:03 +01:00
|
|
|
if (r != ETHER_ADDR_LEN) {
|
2015-09-30 18:22:42 +02:00
|
|
|
log_syntax(unit, LOG_ERR, filename, line, 0, "Not a valid MAC address, ignoring assignment: %s", rvalue);
|
2014-12-17 16:35:36 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fdb_entry = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse the VLAN Id from config files. */
|
2015-04-21 17:43:31 +02:00
|
|
|
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) {
|
|
|
|
|
2014-12-17 16:35:36 +01:00
|
|
|
Network *network = userdata;
|
2019-03-01 05:27:47 +01:00
|
|
|
_cleanup_(fdb_entry_free_or_set_invalidp) FdbEntry *fdb_entry = NULL;
|
2014-12-17 16:35:36 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(filename);
|
|
|
|
assert(section);
|
|
|
|
assert(lvalue);
|
|
|
|
assert(rvalue);
|
|
|
|
assert(data);
|
|
|
|
|
2018-11-12 06:54:04 +01:00
|
|
|
r = fdb_entry_new_static(network, filename, section_line, &fdb_entry);
|
2015-04-21 17:40:18 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_oom();
|
2014-12-17 16:35:36 +01:00
|
|
|
|
2016-06-12 20:01:14 +02:00
|
|
|
r = config_parse_vlanid(unit, filename, line, section,
|
|
|
|
section_line, lvalue, ltype,
|
|
|
|
rvalue, &fdb_entry->vlan_id, userdata);
|
2015-04-21 17:40:18 +02:00
|
|
|
if (r < 0)
|
2014-12-17 16:35:36 +01:00
|
|
|
return r;
|
|
|
|
|
|
|
|
fdb_entry = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-04-30 11:23:39 +02:00
|
|
|
|
|
|
|
int config_parse_fdb_destination(
|
|
|
|
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) {
|
|
|
|
|
|
|
|
_cleanup_(fdb_entry_free_or_set_invalidp) FdbEntry *fdb_entry = NULL;
|
|
|
|
Network *network = userdata;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(filename);
|
|
|
|
assert(section);
|
|
|
|
assert(lvalue);
|
|
|
|
assert(rvalue);
|
|
|
|
assert(data);
|
|
|
|
|
|
|
|
r = fdb_entry_new_static(network, filename, section_line, &fdb_entry);
|
|
|
|
if (r < 0)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
r = in_addr_from_string_auto(rvalue, &fdb_entry->family, &fdb_entry->destination_addr);
|
|
|
|
if (r < 0)
|
|
|
|
return log_syntax(unit, LOG_ERR, filename, line, r,
|
|
|
|
"FDB destination IP address is invalid, ignoring assignment: %s",
|
|
|
|
rvalue);
|
|
|
|
|
|
|
|
fdb_entry = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|