Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/l2filter : Expose configurations to Kconfig #14567

Merged
merged 4 commits into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions drivers/include/net/netdev.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,14 @@ typedef void (*netdev_event_cb_t)(netdev_t *dev, netdev_event_t event);
* be used by upper layers to store reference information.
*/
struct netdev {
const struct netdev_driver *driver; /**< ptr to that driver's interface. */
netdev_event_cb_t event_callback; /**< callback for device events */
void *context; /**< ptr to network stack context */
const struct netdev_driver *driver; /**< ptr to that driver's interface. */
netdev_event_cb_t event_callback; /**< callback for device events */
void *context; /**< ptr to network stack context */
#ifdef MODULE_NETDEV_LAYER
netdev_t *lower; /**< ptr to the lower netdev layer */
netdev_t *lower; /**< ptr to the lower netdev layer */
#endif
#ifdef MODULE_L2FILTER
l2filter_t filter[L2FILTER_LISTSIZE]; /**< link layer address filters */
l2filter_t filter[CONFIG_L2FILTER_LISTSIZE]; /**< link layer address filters */
akshaim marked this conversation as resolved.
Show resolved Hide resolved
#endif
};

Expand Down
24 changes: 15 additions & 9 deletions sys/include/net/l2filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,25 @@
extern "C" {
#endif

/**
* @defgroup net_l2filter_conf Link layer address filter compile configurations
* @ingroup config
* @{
*/
/**
* @brief Maximal length of addresses that can be stored in the filter list
*/
#ifndef L2FILTER_ADDR_MAXLEN
#define L2FILTER_ADDR_MAXLEN (8U)
#ifndef CONFIG_L2FILTER_ADDR_MAXLEN
#define CONFIG_L2FILTER_ADDR_MAXLEN (8U)
#endif

/**
* @brief Number of slots in each filter list (filter entries per device)
*/
#ifndef L2FILTER_LISTSIZE
#define L2FILTER_LISTSIZE (8U)
#ifndef CONFIG_L2FILTER_LISTSIZE
#define CONFIG_L2FILTER_LISTSIZE (8U)
#endif
/** @} */

/**
* @brief Filter list entries
Expand All @@ -62,8 +68,8 @@ extern "C" {
* addresses at the same time.
*/
typedef struct {
uint8_t addr[L2FILTER_ADDR_MAXLEN]; /**< link layer address */
size_t addr_len; /**< address length in byte */
uint8_t addr[CONFIG_L2FILTER_ADDR_MAXLEN]; /**< link layer address */
size_t addr_len; /**< address length in byte */
} l2filter_t;

/**
Expand All @@ -75,7 +81,7 @@ typedef struct {
*
* @pre @p list != NULL
* @pre @p addr != NULL
* @pre @p addr_maxlen <= @ref L2FILTER_ADDR_MAXLEN
* @pre @p addr_maxlen <= @ref CONFIG_L2FILTER_ADDR_MAXLEN
*
* @return 0 on success
* @return -ENOMEM if no empty slot left in list
Expand All @@ -91,7 +97,7 @@ int l2filter_add(l2filter_t *list, const void *addr, size_t addr_len);
*
* @pre @p list != NULL
* @pre @p addr != NULL
* @pre @p addr_maxlen <= @ref L2FILTER_ADDR_MAXLEN
* @pre @p addr_maxlen <= @ref CONFIG_L2FILTER_ADDR_MAXLEN
*
* @return 0 on success
* @return -ENOENT if @p addr was not found in @p list
Expand All @@ -112,7 +118,7 @@ int l2filter_rm(l2filter_t *list, const void *addr, size_t addr_len);
*
* @pre @p list != NULL
* @pre @p addr != NULL
* @pre @p addr_maxlen <= @ref L2FILTER_ADDR_MAXLEN
* @pre @p addr_maxlen <= @ref CONFIG_L2FILTER_ADDR_MAXLEN
*
* @return in whitelist mode: true if @p addr is in @p list
* @return in whitelist mode: false if @p addr is not in @p list
Expand Down
1 change: 1 addition & 0 deletions sys/net/link_layer/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@

rsource "csma_sender/Kconfig"
rsource "ieee802154/Kconfig"
rsource "l2filter/Kconfig"
24 changes: 24 additions & 0 deletions sys/net/link_layer/l2filter/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright (c) 2020 Freie Universitaet Berlin
#
# This file is subject to the terms and conditions of the GNU Lesser
# General Public License v2.1. See the file LICENSE in the top level
# directory for more details.
#
menuconfig KCONFIG_MODULE_L2FILTER
bool "Configure L2filter"
depends on MODULE_L2FILTER
help
Configure L2filter using Kconfig. This module is used for filtering
(allowlisting or denylisting) link layer addresses.

if KCONFIG_MODULE_L2FILTER

config L2FILTER_ADDR_MAXLEN
int "Maximum length of addresses that can be stored in the filter list"
default 8

config L2FILTER_LISTSIZE
int "Number of slots in each filter list (filter entries per device)"
default 8

endif # KCONFIG_MODULE_L2FILTER
16 changes: 8 additions & 8 deletions sys/net/link_layer/l2filter/l2filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ void l2filter_init(l2filter_t *list)
{
assert(list);

for (unsigned i = 0; i < L2FILTER_LISTSIZE; i++) {
for (unsigned i = 0; i < CONFIG_L2FILTER_LISTSIZE; i++) {
list[i].addr_len = 0;
}
}

int l2filter_add(l2filter_t *list, const void *addr, size_t addr_len)
{
assert(list && addr && (addr_len <= L2FILTER_ADDR_MAXLEN));
assert(list && addr && (addr_len <= CONFIG_L2FILTER_ADDR_MAXLEN));

int res = -ENOMEM;

for (unsigned i = 0; i < L2FILTER_LISTSIZE; i++) {
for (unsigned i = 0; i < CONFIG_L2FILTER_LISTSIZE; i++) {
if (list[i].addr_len == 0) {
list[i].addr_len = addr_len;
memcpy(list[i].addr, addr, addr_len);
Expand All @@ -62,11 +62,11 @@ int l2filter_add(l2filter_t *list, const void *addr, size_t addr_len)

int l2filter_rm(l2filter_t *list, const void *addr, size_t addr_len)
{
assert(list && addr && (addr_len <= L2FILTER_ADDR_MAXLEN));
assert(list && addr && (addr_len <= CONFIG_L2FILTER_ADDR_MAXLEN));

int res = -ENOENT;

for (unsigned i = 0; i < L2FILTER_LISTSIZE; i++) {
for (unsigned i = 0; i < CONFIG_L2FILTER_LISTSIZE; i++) {
if (match(&list[i], addr, addr_len)) {
list[i].addr_len = 0;
res = 0;
Expand All @@ -79,11 +79,11 @@ int l2filter_rm(l2filter_t *list, const void *addr, size_t addr_len)

bool l2filter_pass(const l2filter_t *list, const void *addr, size_t addr_len)
{
assert(list && addr && (addr_len <= L2FILTER_ADDR_MAXLEN));
assert(list && addr && (addr_len <= CONFIG_L2FILTER_ADDR_MAXLEN));

#ifdef MODULE_L2FILTER_WHITELIST
bool res = false;
for (unsigned i = 0; i < L2FILTER_LISTSIZE; i++) {
for (unsigned i = 0; i < CONFIG_L2FILTER_LISTSIZE; i++) {
if (match(&list[i], addr, addr_len)) {
DEBUG("[l2filter] whitelist: address match -> packet passes\n");
res = true;
Expand All @@ -93,7 +93,7 @@ bool l2filter_pass(const l2filter_t *list, const void *addr, size_t addr_len)
DEBUG("[l2filter] whitelist: no match -> packet dropped\n");
#else
bool res = true;
for (unsigned i = 0; i < L2FILTER_LISTSIZE; i++) {
for (unsigned i = 0; i < CONFIG_L2FILTER_LISTSIZE; i++) {
if (match(&list[i], addr, addr_len)) {
DEBUG("[l2filter] blacklist: address match -> packet dropped\n");
res = false;
Expand Down
4 changes: 2 additions & 2 deletions sys/shell/commands/sc_gnrc_netif.c
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ static void _netif_list(netif_t *iface)
puts("\n Black-listed link layer addresses:");
#endif
int count = 0;
for (unsigned i = 0; i < L2FILTER_LISTSIZE; i++) {
for (unsigned i = 0; i < CONFIG_L2FILTER_LISTSIZE; i++) {
if (filter[i].addr_len > 0) {
char hwaddr_str[filter[i].addr_len * 3];
gnrc_netif_addr_to_str(filter[i].addr, filter[i].addr_len,
Expand Down Expand Up @@ -1242,7 +1242,7 @@ static int _netif_addrm_l2filter(netif_t *iface, char *val, bool add)
uint8_t addr[GNRC_NETIF_L2ADDR_MAXLEN];
size_t addr_len = gnrc_netif_addr_from_str(val, addr);

if ((addr_len == 0) || (addr_len > L2FILTER_ADDR_MAXLEN)) {
if ((addr_len == 0) || (addr_len > CONFIG_L2FILTER_ADDR_MAXLEN)) {
puts("error: given address is invalid");
return 1;
}
Expand Down