From f61c12e008043e002cc224ca75ef213031435ce1 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Fri, 25 Feb 2022 17:20:17 +0100 Subject: [PATCH 1/3] netdev_tap: make 'wired' property configurable `netdev_tap` is a virtual interface, make it possible to simulate both a wired and a wireless interface. --- cpu/native/include/netdev_tap.h | 6 +++++- cpu/native/netdev_tap/netdev_tap.c | 17 +++++++++++++++++ cpu/native/startup.c | 1 + 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/cpu/native/include/netdev_tap.h b/cpu/native/include/netdev_tap.h index e94cd6f861..4e8ff0e287 100644 --- a/cpu/native/include/netdev_tap.h +++ b/cpu/native/include/netdev_tap.h @@ -25,6 +25,7 @@ extern "C" { #endif #include +#include #include "net/netdev.h" #include "net/ethernet/hdr.h" @@ -43,7 +44,8 @@ typedef struct netdev_tap { char tap_name[IFNAMSIZ]; /**< host dev file name */ int tap_fd; /**< host file descriptor for the TAP */ uint8_t addr[ETHERNET_ADDR_LEN]; /**< The MAC address of the TAP */ - uint8_t promiscuous; /**< Flag for promiscuous mode */ + bool promiscuous; /**< Flag for promiscuous mode */ + bool wired; /**< Flag for wired mode */ } netdev_tap_t; /** @@ -52,6 +54,8 @@ typedef struct netdev_tap { typedef struct { char **tap_name; /**< Name of the host system's tap interface to bind to. */ + bool wired; /**< Interface should behave like a + wired interface. */ } netdev_tap_params_t; /** diff --git a/cpu/native/netdev_tap/netdev_tap.c b/cpu/native/netdev_tap/netdev_tap.c index 70755e9176..5c413677cc 100644 --- a/cpu/native/netdev_tap/netdev_tap.c +++ b/cpu/native/netdev_tap/netdev_tap.c @@ -94,6 +94,12 @@ static inline int _set_promiscuous(netdev_t *netdev, int value) return value; } +static inline int _get_wired(netdev_t *netdev) +{ + netdev_tap_t *dev = container_of(netdev, netdev_tap_t, netdev); + return dev->wired; +} + static inline void _isr(netdev_t *netdev) { if (netdev->event_callback) { @@ -124,6 +130,16 @@ static int _get(netdev_t *dev, netopt_t opt, void *value, size_t max_len) *((bool*)value) = (bool)_get_promiscuous(dev); res = sizeof(bool); break; + case NETOPT_IS_WIRED: + if (!_get_wired(dev)) { + res = -ENOTSUP; + } else { + if (value) { + *((bool*)value) = true; + } + res = sizeof(bool); + } + break; default: res = netdev_eth_get(dev, opt, value, max_len); break; @@ -294,6 +310,7 @@ void netdev_tap_setup(netdev_tap_t *dev, const netdev_tap_params_t *params, int dev->netdev.driver = &netdev_driver_tap; strncpy(dev->tap_name, *(params->tap_name), IFNAMSIZ - 1); dev->tap_name[IFNAMSIZ - 1] = '\0'; + dev->wired = params->wired; netdev_register(&dev->netdev, NETDEV_TAP, index); } diff --git a/cpu/native/startup.c b/cpu/native/startup.c index 9009487290..262f706b05 100644 --- a/cpu/native/startup.c +++ b/cpu/native/startup.c @@ -655,6 +655,7 @@ __attribute__((constructor)) static void startup(int argc, char **argv, char **e #ifdef MODULE_NETDEV_TAP for (int i = 0; i < NETDEV_TAP_MAX; i++) { netdev_tap_params[i].tap_name = &argv[optind + i]; + netdev_tap_params[i].wired = true; } #endif From 2520aaf1e8f960fd891dbc9c320d2a5e4318ca58 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sat, 26 Feb 2022 15:28:08 +0100 Subject: [PATCH 2/3] cpu/native: add -w command line parameter to simulate wireless tap --- cpu/native/startup.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/cpu/native/startup.c b/cpu/native/startup.c index 262f706b05..d760ebf747 100644 --- a/cpu/native/startup.c +++ b/cpu/native/startup.c @@ -115,6 +115,9 @@ static const char short_opts[] = ":hi:s:deEoc:" #endif #ifdef MODULE_PERIPH_SPIDEV_LINUX "p:" +#endif +#ifdef MODULE_NETDEV_TAP + "w:" #endif ""; @@ -367,6 +370,11 @@ void usage_exit(int status) " -M , --eeprom=\n" " Specify the file path where the EEPROM content is stored\n" " Example: --eeprom=/tmp/riot_native.eeprom\n"); +#endif +#ifdef MODULE_NETDEV_TAP + real_printf( +" -w \n" +" Add a tap interface as a wireless interface\n"); #endif real_exit(status); } @@ -468,6 +476,9 @@ __attribute__((constructor)) static void startup(int argc, char **argv, char **e _native_id = _native_pid; int c, opt_idx = 0, uart = 0; +#ifdef MODULE_NETDEV_TAP + unsigned taps = 0; +#endif #ifdef MODULE_SOCKET_ZEP unsigned zeps = 0; #endif @@ -575,6 +586,13 @@ __attribute__((constructor)) static void startup(int argc, char **argv, char **e strncpy(eeprom_file, optarg, EEPROM_FILEPATH_MAX_LEN); break; } +#endif +#ifdef MODULE_NETDEV_TAP + case 'w': + netdev_tap_params[taps].tap_name = &argv[optind - 1]; + netdev_tap_params[taps].wired = false; + ++taps; + break; #endif default: usage_exit(EXIT_FAILURE); @@ -582,7 +600,7 @@ __attribute__((constructor)) static void startup(int argc, char **argv, char **e } } #ifdef MODULE_NETDEV_TAP - for (int i = 0; i < NETDEV_TAP_MAX; i++) { + for (unsigned i = 0; i < NETDEV_TAP_MAX - taps; i++) { if (argv[optind + i] == NULL) { /* no tap parameter left */ usage_exit(EXIT_FAILURE); @@ -653,9 +671,9 @@ __attribute__((constructor)) static void startup(int argc, char **argv, char **e native_cpu_init(); native_interrupt_init(); #ifdef MODULE_NETDEV_TAP - for (int i = 0; i < NETDEV_TAP_MAX; i++) { - netdev_tap_params[i].tap_name = &argv[optind + i]; - netdev_tap_params[i].wired = true; + for (unsigned i = 0; taps < NETDEV_TAP_MAX; ++taps, ++i) { + netdev_tap_params[taps].tap_name = &argv[optind + i]; + netdev_tap_params[taps].wired = true; } #endif From fad65011b76f78aa37a59f84459a71ea8a83c80e Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sat, 26 Feb 2022 15:50:31 +0100 Subject: [PATCH 3/3] netdev_tap: make NETDEV_TAP_MAX an upper bound --- cpu/native/startup.c | 12 ++++-------- pkg/lwip/init_devs/auto_init_netdev_tap.c | 3 +++ sys/net/gnrc/netif/init_devs/auto_init_netdev_tap.c | 4 ++++ 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/cpu/native/startup.c b/cpu/native/startup.c index d760ebf747..61f0f2297b 100644 --- a/cpu/native/startup.c +++ b/cpu/native/startup.c @@ -478,6 +478,7 @@ __attribute__((constructor)) static void startup(int argc, char **argv, char **e int c, opt_idx = 0, uart = 0; #ifdef MODULE_NETDEV_TAP unsigned taps = 0; + memset(netdev_tap_params, 0, sizeof(netdev_tap_params)); #endif #ifdef MODULE_SOCKET_ZEP unsigned zeps = 0; @@ -599,14 +600,6 @@ __attribute__((constructor)) static void startup(int argc, char **argv, char **e break; } } -#ifdef MODULE_NETDEV_TAP - for (unsigned i = 0; i < NETDEV_TAP_MAX - taps; i++) { - if (argv[optind + i] == NULL) { - /* no tap parameter left */ - usage_exit(EXIT_FAILURE); - } - } -#endif #ifdef MODULE_SOCKET_ZEP if (zeps != SOCKET_ZEP_MAX) { /* not enough ZEPs given */ @@ -672,6 +665,9 @@ __attribute__((constructor)) static void startup(int argc, char **argv, char **e native_interrupt_init(); #ifdef MODULE_NETDEV_TAP for (unsigned i = 0; taps < NETDEV_TAP_MAX; ++taps, ++i) { + if (argv[optind + i] == NULL) { + break; + } netdev_tap_params[taps].tap_name = &argv[optind + i]; netdev_tap_params[taps].wired = true; } diff --git a/pkg/lwip/init_devs/auto_init_netdev_tap.c b/pkg/lwip/init_devs/auto_init_netdev_tap.c index effebbf637..e81e230978 100644 --- a/pkg/lwip/init_devs/auto_init_netdev_tap.c +++ b/pkg/lwip/init_devs/auto_init_netdev_tap.c @@ -33,6 +33,9 @@ static netdev_tap_t netdev_taps[NETIF_TAP_NUMOF]; static void auto_init_netdev_tap(void) { for (unsigned i = 0; i < NETIF_TAP_NUMOF; i++) { + if (netdev_tap_params[i].tap_name == NULL) { + continue; + } netdev_tap_setup(&netdev_taps[i], &netdev_tap_params[i], i); if (lwip_add_ethernet(&netif[i], &netdev_taps[i].netdev) == NULL) { DEBUG("Could not add netdev_tap device\n"); diff --git a/sys/net/gnrc/netif/init_devs/auto_init_netdev_tap.c b/sys/net/gnrc/netif/init_devs/auto_init_netdev_tap.c index 93a4195590..ea34968d45 100644 --- a/sys/net/gnrc/netif/init_devs/auto_init_netdev_tap.c +++ b/sys/net/gnrc/netif/init_devs/auto_init_netdev_tap.c @@ -35,6 +35,10 @@ void auto_init_netdev_tap(void) for (unsigned i = 0; i < NETDEV_TAP_MAX; i++) { const netdev_tap_params_t *p = &netdev_tap_params[i]; + if (p->tap_name == NULL) { + continue; + } + LOG_DEBUG("[auto_init_netif] initializing netdev_tap #%u on TAP %s\n", i, *(p->tap_name));