On Wed, Mar 12, 2025 at 02:45:36PM +0100, Oleksij Rempel wrote: > This patch introduces the `ip` command, providing functionality similar > to Linux’s `ip` tool, catering to users with strong Linux muscle memory. > It supports: > > - `ip l` / `ip link` - Show network interfaces (state, MAC address) > - `ip a` / `ip addr` - Show assigned IP addresses and netmasks > - `ip r` / `ip route` - Show routing table, including default gateway, > interface-specific routes, DNS, and server IP. > > Signed-off-by: Oleksij Rempel <o.rempel@xxxxxxxxxxxxxx> > --- > commands/Kconfig | 9 +++ > commands/Makefile | 1 + > commands/ip.c | 157 ++++++++++++++++++++++++++++++++++++++++++++++ > 3 files changed, 167 insertions(+) > create mode 100644 commands/ip.c > > diff --git a/commands/Kconfig b/commands/Kconfig > index e41625e294c9..528b30b8fc90 100644 > --- a/commands/Kconfig > +++ b/commands/Kconfig > @@ -1397,6 +1397,15 @@ config CMD_TFTP > Options: > -p push to TFTP server > > +config CMD_IP > + tristate > + prompt "ip" > + default y > + help > + This command provides information about network interfaces, similar > + to the Linux `ip addr show` command. It lists interface names, states > + (UP/DOWN), MAC addresses, and assigned IP addresses. Whitespace damage before "This..." > + > config CMD_IP_ROUTE_GET > tristate > prompt "ip-route-get" > diff --git a/commands/Makefile b/commands/Makefile > index e152e4148b04..c202cdef5703 100644 > --- a/commands/Makefile > +++ b/commands/Makefile > @@ -146,6 +146,7 @@ obj-$(CONFIG_CMD_MMC) += mmc.o > obj-$(CONFIG_CMD_MMC_EXTCSD) += mmc_extcsd.o > obj-$(CONFIG_CMD_NAND_BITFLIP) += nand-bitflip.o > obj-$(CONFIG_CMD_SEED) += seed.o > +obj-$(CONFIG_CMD_IP) += ip.o > obj-$(CONFIG_CMD_IP_ROUTE_GET) += ip-route-get.o > obj-$(CONFIG_CMD_BTHREAD) += bthread.o > obj-$(CONFIG_CMD_UBSAN) += ubsan.o > diff --git a/commands/ip.c b/commands/ip.c > new file mode 100644 > index 000000000000..35fc5f4d300f > --- /dev/null > +++ b/commands/ip.c > @@ -0,0 +1,157 @@ SPDX? > +#include <common.h> > +#include <command.h> > +#include <net.h> > +#include <linux/list.h> > +#include <complete.h> > + > +/* Function to calculate CIDR prefix from netmask */ > +static int netmask_to_prefix(IPaddr_t netmask) > +{ > + int prefix = 0; > + > + while (netmask) { > + prefix += netmask & 1; > + netmask >>= 1; > + } > + > + return prefix; > +} > + > +/* Function to display network links (`ip l`) */ > +static int do_ip_link(int argc, char *argv[]) > +{ > + struct eth_device *edev; > + > + for_each_netdev(edev) { > + const char *phy_status = edev->phydev ? > + (edev->phydev->link ? ",LOWER_UP" : ",NO-CARRIER") : ""; > + > + printf("%s: <%s%s>\n", > + eth_name(edev), > + edev->active ? "UP" : "DOWN", > + phy_status); > + > + printf(" link/ether %pM brd ff:ff:ff:ff:ff:ff\n", > + edev->ethaddr); > + } > + > + return 0; > +} > + > +/* Function to display network addresses (`ip a`) */ > +static int do_ip_addr(int argc, char *argv[]) > +{ > + struct eth_device *edev; > + > + for_each_netdev(edev) { > + IPaddr_t ipaddr = net_get_ip(edev); > + int prefix_length = netmask_to_prefix(edev->netmask); > + const char *phy_status = edev->phydev ? > + (edev->phydev->link ? ",LOWER_UP" : ",NO-CARRIER") : ""; > + > + printf("%s: <%s%s>\n", eth_name(edev), > + edev->active ? "UP" : "DOWN", phy_status); > + > + printf(" link/ether %pM brd ff:ff:ff:ff:ff:ff\n", > + edev->ethaddr); > + > + if (ipaddr) { > + printf(" inet %pI4/%d scope global src %pI4\n", > + &ipaddr, prefix_length, &ipaddr); > + } > + } > + > + return 0; > +} > + > +/* Function to display network routes (`ip r`) */ > +static int do_ip_route(int argc, char *argv[]) > +{ > + const char *domainname = net_get_domainname(); > + IPaddr_t nameserver = net_get_nameserver(); > + const char *serverip = net_get_server(); > + IPaddr_t gateway = net_get_gateway(); > + struct eth_device *edev; > + > + for_each_netdev(edev) { > + int prefix_length = netmask_to_prefix(edev->netmask); > + IPaddr_t ipaddr = net_get_ip(edev); > + const char *proto; > + > + /* Skip interfaces without an IP address */ > + if (!ipaddr) > + continue; > + > + /* Determine protocol: DHCP or Static */ > + if (edev->global_mode == ETH_MODE_DHCP) > + proto = "dhcp"; > + else > + proto = "static"; > + > + /* Show default gateway */ > + if (gateway) { > + printf("default via %pI4 dev %s proto %s src %pI4\n", > + &gateway, eth_name(edev), proto, &ipaddr); > + } > + > + /* Show per-interface network route */ > + if (prefix_length > 0) { > + printf("%pI4/%d dev %s proto kernel scope link src %pI4\n", > + &ipaddr, prefix_length, eth_name(edev), > + &ipaddr); > + } > + } > + > + /* Show boot server */ > + if (serverip && *serverip) { > + printf("serverip %s\n", serverip); > + } > + > + /* Show DNS server */ > + if (nameserver) { > + printf("nameserver %pI4\n", &nameserver); > + } > + > + /* Show search domain */ > + if (domainname && *domainname) { > + printf("search %s\n", domainname); > + } > + > + return 0; > +} > + > +/* Main IP command dispatcher */ > +static int do_ip(int argc, char *argv[]) > +{ > + if (argc < 2) { > + return do_ip_addr(argc, argv); /* Default: `ip a` */ > + } > + > + if (!strcmp(argv[1], "l") || !strcmp(argv[1], "link")) > + return do_ip_link(argc, argv); > + else if (!strcmp(argv[1], "a") || !strcmp(argv[1], "addr")) > + return do_ip_addr(argc, argv); > + else if (!strcmp(argv[1], "r") || !strcmp(argv[1], "route")) > + return do_ip_route(argc, argv); The Linux ip tool not only accepts "l" and "link", but also "li" and "lin", so maybe something like strncmp(argv[1], "link", strlen(argv[1]))? > + > + printf("Usage: ip [l|link] | [a|addr] | [r|route]\n"); > + return 1; return COMMAND_ERROR_USAGE instead? Sascha -- Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |