From eab317749f82764c6e978ab13a6819b5320a031c Mon Sep 17 00:00:00 2001 From: Erik Ekman Date: Mon, 8 Feb 2021 22:35:20 +0100 Subject: [PATCH] sys/shell: Add lwIP ifconfig shell command Lists state, link type, v4/v6 addresses. Currently read-only. Using lwIP debug system to print addresses, to limit dependencies and work with dual stack setup. Most other code seems to only allow either v4 or v6 networking. For that to compile I had to change the `SZT_F` format string due to this error: ``` error: format '%lu' expects argument of type 'long unsigned int', but argument 2 has type 'size_t {aka unsigned int}' ``` Switching to the lwIP default format string here. Outputs the following on my ESP32 board with Ethernet, when both v4 and v6 are enabled in examples/paho-mqtt: ``` > ifconfig Iface ET0 HWaddr: 24:0a:c4:e6:0e:9f Link: up State: up Link type: wired inet addr: 10.4.4.81 mask: 255.255.254.0 gw: 10.4.4.1 inet6 addr: fe80:0:0:0:260a:c4ff:fee6:e9f scope: link inet6 addr: 2001:db8:1000:0:260a:c4ff:fee6:e9f scope: global Iface ET1 HWaddr: 24:0a:c4:e6:0e:9c Link: up State: up Link type: wireless inet addr: 10.4.4.82 mask: 255.255.254.0 gw: 10.4.4.1 inet6 addr: fe80:0:0:0:260a:c4ff:fee6:e9c scope: link inet6 addr: 2001:db8:1000:0:260a:c4ff:fee6:e9c scope: global > ``` --- pkg/lwip/include/arch/cc.h | 2 +- pkg/lwip/include/lwipopts.h | 3 + sys/shell/commands/Makefile | 4 ++ sys/shell/commands/sc_lwip_netif.c | 101 ++++++++++++++++++++++++++++ sys/shell/commands/shell_commands.c | 7 ++ 5 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 sys/shell/commands/sc_lwip_netif.c diff --git a/pkg/lwip/include/arch/cc.h b/pkg/lwip/include/arch/cc.h index 5d64b120ff..fc9eb7857d 100644 --- a/pkg/lwip/include/arch/cc.h +++ b/pkg/lwip/include/arch/cc.h @@ -58,7 +58,7 @@ extern "C" { #define S32_F PRId32 #define X32_F PRIx32 -#define SZT_F "lu" +#define SZT_F PRIuPTR /** @} */ /** diff --git a/pkg/lwip/include/lwipopts.h b/pkg/lwip/include/lwipopts.h index 9573866ae2..a7711e536c 100644 --- a/pkg/lwip/include/lwipopts.h +++ b/pkg/lwip/include/lwipopts.h @@ -137,6 +137,9 @@ extern "C" { #define LWIP_NETCONN 0 #endif /* MODULE_LWIP_SOCK */ +#ifdef MODULE_SHELL_COMMANDS +#define LWIP_DEBUG 1 +#endif #ifndef TCP_LISTEN_BACKLOG # if defined(MODULE_LWIP_SOCK_TCP) diff --git a/sys/shell/commands/Makefile b/sys/shell/commands/Makefile index d923c19ca5..55b2904645 100644 --- a/sys/shell/commands/Makefile +++ b/sys/shell/commands/Makefile @@ -93,6 +93,10 @@ ifneq (,$(filter openwsn,$(USEPKG))) SRC += sc_openwsn.c endif +ifneq (,$(filter lwip_netif,$(USEMODULE))) + SRC += sc_lwip_netif.c +endif + ifneq (,$(filter periph_rtc,$(USEMODULE))) SRC += sc_rtc.c endif diff --git a/sys/shell/commands/sc_lwip_netif.c b/sys/shell/commands/sc_lwip_netif.c new file mode 100644 index 0000000000..14c864821a --- /dev/null +++ b/sys/shell/commands/sc_lwip_netif.c @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2021 Google LLC + * + * 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. + */ + +/** + * @ingroup sys_shell_commands + * @{ + * + * @file + * @brief Shell command for printing lwIP network interface status + * + * @author Erik Ekman + * + * @} + */ + +#include +#include "lwip/netif.h" +#include "net/netdev.h" +#include "net/netopt.h" + +#ifdef MODULE_LWIP_IPV6 +static void _netif_list_ipv6(struct netif *netif, int addr_index) { + printf(" inet6 addr: "); + ip_addr_debug_print(LWIP_DBG_ON, netif_ip_addr6(netif, addr_index)); + printf(" scope: "); + if (ip6_addr_isglobal(netif_ip6_addr(netif, addr_index))) { + printf("global"); + } else if (ip6_addr_islinklocal(netif_ip6_addr(netif, addr_index))) { + printf("link"); + } else if (ip6_addr_issitelocal(netif_ip6_addr(netif, addr_index))) { + printf("site"); + } else { + printf("unknown"); + } + printf("\n"); +} +#endif + +static void _netif_list(struct netif *netif) { + int i; + struct netdev *dev = netif->state; + printf("Iface %c%c%u ", netif->name[0], netif->name[1], netif->num); + printf("HWaddr: "); + for (i = 0; i < netif->hwaddr_len; i++) { + printf("%02x", netif->hwaddr[i]); + if ((i+1) < netif->hwaddr_len) { + printf(":"); + } + } + printf(" Link: %s State: %s\n", + netif_is_link_up(netif) ? "up" : "down", + netif_is_up(netif) ? "up" : "down"); + printf(" Link type: %s\n", + (dev->driver->get(dev, NETOPT_IS_WIRED, &i, sizeof(i)) > 0) ? + "wired" : "wireless"); +#ifdef MODULE_LWIP_IPV4 + printf(" inet addr: "); + ip_addr_debug_print(LWIP_DBG_ON, netif_ip_addr4(netif)); + printf(" mask: "); + ip_addr_debug_print(LWIP_DBG_ON, netif_ip_netmask4(netif)); + printf(" gw: "); + ip_addr_debug_print(LWIP_DBG_ON, netif_ip_gw4(netif)); + printf("\n"); +#endif + +#ifdef MODULE_LWIP_IPV6 + for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) { + if (netif_ip6_addr_state(netif, i) != IP6_ADDR_INVALID) { + _netif_list_ipv6(netif, i); + } + } +#endif +} + +int _lwip_netif_config(int argc, char **argv) +{ + if (argc < 2) { + /* List in interface order, which is normally reverse of list order */ + struct netif *netif; + int netifs = 0; + int listed = 0; + u8_t i; + NETIF_FOREACH(netif) netifs++; + for (i = 0; listed < netifs; i++) { + NETIF_FOREACH(netif) { + if (i == netif->num) { + _netif_list(netif); + listed++; + } + } + } + return 0; + } + printf("%s takes no arguments.\n", argv[0]); + return 1; +} diff --git a/sys/shell/commands/shell_commands.c b/sys/shell/commands/shell_commands.c index 149b717597..f2d5a13a25 100644 --- a/sys/shell/commands/shell_commands.c +++ b/sys/shell/commands/shell_commands.c @@ -108,6 +108,10 @@ extern int _openwsn_ifconfig(int argc, char **argv); extern int _openwsn_handler(int argc, char **argv); #endif +#ifdef MODULE_LWIP_NETIF +extern int _lwip_netif_config(int argc, char **argv); +#endif + #ifdef MODULE_FIB extern int _fib_route_handler(int argc, char **argv); #endif @@ -262,6 +266,9 @@ const shell_command_t _shell_command_list[] = { {"ifconfig", "Shows assigned IPv6 addresses", _openwsn_ifconfig}, {"openwsn", "OpenWSN commands", _openwsn_handler}, #endif +#ifdef MODULE_LWIP_NETIF + {"ifconfig", "List network interfaces", _lwip_netif_config}, +#endif #ifdef MODULE_FIB {"fibroute", "Manipulate the FIB (info: 'fibroute [add|del]')", _fib_route_handler}, #endif