1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

gnrc_static: add static network configuration

This commit is contained in:
Benjamin Valentin 2022-08-19 13:52:15 +02:00
parent 15c8ad2e8e
commit 249901bc96
8 changed files with 202 additions and 1 deletions

View File

@ -327,6 +327,11 @@ extern void auto_init_sock_dns(void);
AUTO_INIT(auto_init_sock_dns,
AUTO_INIT_PRIO_MOD_DOCK_DNS);
#endif
#if IS_USED(MODULE_GNRC_IPV6_STATIC_ADDR)
extern void auto_init_gnrc_ipv6_static_addr(void);
AUTO_INIT(auto_init_gnrc_ipv6_static_addr,
AUTO_INIT_PRIO_MOD_GNRC_IPV6_STATIC_ADDR);
#endif
void auto_init(void)
{

View File

@ -353,6 +353,12 @@ extern "C" {
*/
#define AUTO_INIT_PRIO_MOD_DOCK_DNS 1550
#endif
#ifndef AUTO_INIT_PRIO_MOD_GNRC_IPV6_STATIC_ADDR
/**
* @brief Static network configuration priority
*/
#define AUTO_INIT_PRIO_MOD_GNRC_IPV6_STATIC_ADDR 1560
#endif
#ifdef __cplusplus
}

View File

@ -136,7 +136,7 @@ extern "C" {
#if CONFIG_GNRC_IPV6_NIB_ROUTER && \
(!CONFIG_GNRC_IPV6_NIB_6LR || CONFIG_GNRC_IPV6_NIB_6LBR) && \
!(IS_USED(MODULE_DHCPV6_CLIENT_IA_PD) || IS_USED(MODULE_GNRC_UHCPC) || \
IS_USED(MODULE_GNRC_IPV6_AUTO_SUBNETS))
IS_USED(MODULE_GNRC_IPV6_AUTO_SUBNETS) || IS_USED(MODULE_GNRC_IPV6_STATIC_ADDR))
#define CONFIG_GNRC_IPV6_NIB_ADV_ROUTER 1
#else
#define CONFIG_GNRC_IPV6_NIB_ADV_ROUTER 0

View File

@ -88,6 +88,9 @@ endif
ifneq (,$(filter gnrc_rpl_p2p,$(USEMODULE)))
DIRS += routing/rpl/p2p
endif
ifneq (,$(filter gnrc_ipv6_static_addr,$(USEMODULE)))
DIRS += network_layer/ipv6/static_addr
endif
ifneq (,$(filter gnrc_ipv6_auto_subnets,$(USEMODULE)))
DIRS += routing/ipv6_auto_subnets
endif

View File

@ -28,5 +28,6 @@ rsource "blacklist/Kconfig"
rsource "ext/frag/Kconfig"
rsource "nib/Kconfig"
rsource "whitelist/Kconfig"
rsource "static_addr/Kconfig"
endmenu # IPv6

View File

@ -0,0 +1,35 @@
# Copyright (c) 2022 ML!PA Consulting GmbH
#
# 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_USEMODULE_GNRC_IPV6_STATIC_ADDR
bool "Configure static IPv6 addresses and routes"
depends on USEMODULE_GNRC_IPV6_STATIC_ADDR
help
Configure GNRC IPv6 Whitelisting module using Kconfig.
if KCONFIG_USEMODULE_GNRC_IPV6_STATIC_ADDR
config GNRC_IPV6_STATIC_ADDR
string "Static IPv6 address of the upstream interface"
default "2001:db8::100"
config GNRC_IPV6_STATIC_ADDR_UPSTREAM
int "ID of the upstream interface, use 0 to auto-select wired interface"
default 0
config GNRC_IPV6_STATIC_PREFIX
string "Static IPv6 prefix for the downstream network"
default "2001:db8:8000::/48"
config GNRC_IPV6_STATIC_ADDR_DOWNSTREAM
int "ID of the downstream interface, use 0 to auto-select wireless interface"
default 0
config GNRC_IPV6_STATIC_DEFAULT_ROUTER
string "Static IPv6 address of the default router"
default "2001:db8::1"
endif # KCONFIG_USEMODULE_GNRC_IPV6_STATIC_ADDR

View File

@ -0,0 +1,3 @@
MODULE = gnrc_ipv6_static_addr
include $(RIOTBASE)/Makefile.base

View File

@ -0,0 +1,148 @@
/*
* Copyright (C) 2022 ML!PA Consulting GmbH
*
* 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.
*/
/**
* @{
*
* @file
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
*/
#include "net/gnrc/ipv6/nib.h"
#include "net/gnrc/ipv6.h"
#include "net/gnrc/netapi.h"
#include "net/gnrc/netif.h"
#include "net/gnrc/rpl.h"
#include "net/ipv6/addr.h"
#include "net/netdev.h"
#include "net/netopt.h"
#define ENABLE_DEBUG 0
#include "debug.h"
/**
* @brief ID of the upstream interface, set to 0 to attempt auto-select
*/
#ifndef CONFIG_GNRC_IPV6_STATIC_ADDR_UPSTREAM
#define CONFIG_GNRC_IPV6_STATIC_ADDR_UPSTREAM 0
#endif
/**
* @brief ID of the downstream interface, set to 0 to attempt auto-select
*/
#ifndef CONFIG_GNRC_IPV6_STATIC_ADDR_DOWNSTREAM
#define CONFIG_GNRC_IPV6_STATIC_ADDR_DOWNSTREAM 0
#endif
static void _config_upstream(gnrc_netif_t *upstream)
{
ipv6_addr_t addr;
const char *static_addr =
#ifdef CONFIG_GNRC_IPV6_STATIC_ADDR
CONFIG_GNRC_IPV6_STATIC_ADDR;
#else
NULL;
#endif
const char *static_route =
#ifdef CONFIG_GNRC_IPV6_STATIC_DEFAULT_ROUTER
CONFIG_GNRC_IPV6_STATIC_DEFAULT_ROUTER;
#else
NULL;
#endif
DEBUG("gnrc_ipv6_static_addr: interface %u selected as upstream\n", upstream->pid);
/* configure static address */
int addr_len;
if (static_addr != NULL &&
(addr_len = ipv6_prefix_from_str(&addr, static_addr)) > 0) {
gnrc_netif_ipv6_addr_add_internal(upstream, &addr, addr_len,
GNRC_NETIF_IPV6_ADDRS_FLAGS_STATE_VALID);
}
/* configure static route */
if (static_route != NULL &&
ipv6_addr_from_str(&addr, static_route) != NULL) {
gnrc_ipv6_nib_ft_add(&addr, 0, &addr, upstream->pid, 0);
}
}
static void _config_downstream(gnrc_netif_t *downstream)
{
const char *static_prefix =
#ifdef CONFIG_GNRC_IPV6_STATIC_PREFIX
CONFIG_GNRC_IPV6_STATIC_PREFIX;
#else
NULL;
#endif
DEBUG("gnrc_ipv6_static_addr: interface %u selected as downstream\n", downstream->pid);
if (CONFIG_GNRC_IPV6_STATIC_PREFIX == NULL) {
return;
}
ipv6_addr_t prefix;
int len = ipv6_prefix_from_str(&prefix, static_prefix);
if (len <= 0) {
return;
}
/* configure subnet on downstream interface */
int idx = gnrc_netif_ipv6_add_prefix(downstream, &prefix, len,
UINT32_MAX, UINT32_MAX);
if (idx < 0) {
DEBUG("gnrc_ipv6_static_addr: adding prefix to %u failed\n", downstream->pid);
return;
}
/* start advertising subnet */
gnrc_ipv6_nib_change_rtr_adv_iface(downstream, true);
/* configure RPL root if applicable */
if (IS_ACTIVE(CONFIG_GNRC_IPV6_NIB_6LBR)) {
gnrc_rpl_configure_root(downstream, &downstream->ipv6.addrs[idx]);
}
}
void auto_init_gnrc_ipv6_static_addr(void)
{
gnrc_netif_t *netif = NULL;
gnrc_netif_t *upstream = NULL;
gnrc_netif_t *downstream = NULL;
if (IS_ACTIVE(CONFIG_GNRC_IPV6_STATIC_ADDR_UPSTREAM)) {
upstream = gnrc_netif_get_by_pid(CONFIG_GNRC_IPV6_STATIC_ADDR_UPSTREAM);
}
if (IS_ACTIVE(CONFIG_GNRC_IPV6_STATIC_ADDR_DOWNSTREAM)) {
downstream = gnrc_netif_get_by_pid(CONFIG_GNRC_IPV6_STATIC_ADDR_DOWNSTREAM);
}
while ((netif = gnrc_netif_iter(netif))) {
bool is_wired = gnrc_netapi_get(netif->pid, NETOPT_IS_WIRED, 0, NULL, 0) == 1;
if (!upstream && is_wired) {
upstream = netif;
} else if (!downstream && !is_wired) {
downstream = netif;
}
}
if (upstream) {
_config_upstream(upstream);
}
if (downstream) {
_config_downstream(downstream);
}
}
/** @} */