* [PATCH v2] commands: Add `ip` command for listing interfaces, addresses, and routes
@ 2025-03-15 13:52 Oleksij Rempel
2025-03-17 9:05 ` Sascha Hauer
2025-03-17 9:07 ` Sascha Hauer
0 siblings, 2 replies; 3+ messages in thread
From: Oleksij Rempel @ 2025-03-15 13:52 UTC (permalink / raw)
To: barebox; +Cc: Oleksij Rempel
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@pengutronix.de>
---
changes v2:
- add SPDX
- fix help in Kconfig
- add support for range of commands
- use COMMAND_ERROR_USAGE
---
commands/Kconfig | 9 +++
commands/Makefile | 1 +
commands/ip.c | 159 ++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 169 insertions(+)
create mode 100644 commands/ip.c
diff --git a/commands/Kconfig b/commands/Kconfig
index fe459fa862f5..466afa7fa479 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..76a84804c002
--- /dev/null
+++ b/commands/ip.c
@@ -0,0 +1,159 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// SPDX-FileCopyrightText: © 2025 Oleksij Rempel <o.rempel@pengutronix.de>
+
+#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[])
+{
+ size_t arglen;
+
+ if (argc < 2)
+ return do_ip_addr(argc, argv); /* Default: `ip a` */
+
+ arglen = strlen(argv[1]);
+
+ if ((arglen <= strlen("link")) && !strncmp(argv[1], "link", arglen))
+ return do_ip_link(argc, argv);
+ else if ((arglen <= strlen("addr")) && !strncmp(argv[1], "addr", arglen))
+ return do_ip_addr(argc, argv);
+ else if ((arglen <= strlen("route")) && !strncmp(argv[1], "route", arglen))
+ return do_ip_route(argc, argv);
+
+ return COMMAND_ERROR_USAGE;
+}
+
+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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] commands: Add `ip` command for listing interfaces, addresses, and routes
2025-03-15 13:52 [PATCH v2] commands: Add `ip` command for listing interfaces, addresses, and routes Oleksij Rempel
@ 2025-03-17 9:05 ` Sascha Hauer
2025-03-17 9:07 ` Sascha Hauer
1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2025-03-17 9:05 UTC (permalink / raw)
To: barebox, Oleksij Rempel
On Sat, 15 Mar 2025 14:52:45 +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.
>
> [...]
Applied, thanks!
[1/1] commands: Add `ip` command for listing interfaces, addresses, and routes
https://git.pengutronix.de/cgit/barebox/commit/?id=35e565ec63ab (link may not be stable)
Best regards,
--
Sascha Hauer <s.hauer@pengutronix.de>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] commands: Add `ip` command for listing interfaces, addresses, and routes
2025-03-15 13:52 [PATCH v2] commands: Add `ip` command for listing interfaces, addresses, and routes Oleksij Rempel
2025-03-17 9:05 ` Sascha Hauer
@ 2025-03-17 9:07 ` Sascha Hauer
1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2025-03-17 9:07 UTC (permalink / raw)
To: Oleksij Rempel; +Cc: barebox
On Sat, Mar 15, 2025 at 02:52:45PM +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@pengutronix.de>
> ---
> changes v2:
> - add SPDX
> - fix help in Kconfig
> - add support for range of commands
> - use COMMAND_ERROR_USAGE
> ---
> commands/Kconfig | 9 +++
> commands/Makefile | 1 +
> commands/ip.c | 159 ++++++++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 169 insertions(+)
> create mode 100644 commands/ip.c
>
> diff --git a/commands/Kconfig b/commands/Kconfig
> index fe459fa862f5..466afa7fa479 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
Dropped the default y while applying.
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 |
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-03-17 9:07 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-03-15 13:52 [PATCH v2] commands: Add `ip` command for listing interfaces, addresses, and routes Oleksij Rempel
2025-03-17 9:05 ` Sascha Hauer
2025-03-17 9:07 ` Sascha Hauer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox