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. + 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 @@ +#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); + + printf("Usage: ip [l|link] | [a|addr] | [r|route]\n"); + return 1; +} + +BAREBOX_CMD_HELP_START(ip) +BAREBOX_CMD_HELP_TEXT("Show network interfaces, addresses, and routes") +BAREBOX_CMD_HELP_TEXT("") +BAREBOX_CMD_HELP_TEXT("Options:") +BAREBOX_CMD_HELP_OPT("l, link", "Show only network links (interface state and MAC)") +BAREBOX_CMD_HELP_OPT("a, addr", "Show only network addresses (default behavior)") +BAREBOX_CMD_HELP_OPT("r, route", "Show routing information (default gateway, network routes, server IP, DNS)") +BAREBOX_CMD_HELP_END + +BAREBOX_CMD_START(ip) + .cmd = do_ip, + BAREBOX_CMD_DESC("Show network interfaces, addresses, and routes") + BAREBOX_CMD_OPTS("[l|link] | [a|addr] | [r|route]") + BAREBOX_CMD_GROUP(CMD_GRP_MISC) + BAREBOX_CMD_COMPLETE(empty_complete) + BAREBOX_CMD_HELP(cmd_ip_help) +BAREBOX_CMD_END -- 2.39.5