From ab7e0660d59adc131e1dff5ea41a8729aefd4634 Mon Sep 17 00:00:00 2001 From: wangjianyu3 Date: Mon, 10 Aug 2026 16:24:37 +0800 Subject: [PATCH] nshlib/ifconfig: support "ifconfig up|down" Previously "ifconfig eth0 up" fell through to the host-IP parsing branch, where inet_addr("up") returns INADDR_NONE, so the address was silently set to 255.255.255.255 and the interface was never brought up. Users had to run the separate "ifup"/"ifdown" commands. Recognize the "up" and "down" tokens explicitly and apply them once the rest of the configuration is in place, so up/down is just another keyword in the argument list as it is in Linux ifconfig. A bare "ifconfig up|down" needs no special case: the preceding patch made the address, netmask, gateway, DNS and DHCP blocks check whether they were asked for, so the command falls through them without touching anything. The up/down handling calls netlib_ifup()/netlib_ifdown() from netlib, so it stays available regardless of CONFIG_NSH_DISABLE_IFUPDOWN (which only strips the standalone ifup/ifdown commands). Assisted-by: GitHubCopilot:claude-opus-5 Signed-off-by: wangjianyu3 --- nshlib/nsh_command.c | 2 +- nshlib/nsh_netcmds.c | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/nshlib/nsh_command.c b/nshlib/nsh_command.c index 098abaa76..8e136edc7 100644 --- a/nshlib/nsh_command.c +++ b/nshlib/nsh_command.c @@ -296,7 +296,7 @@ static const struct cmdmap_s g_cmdmap[] = CMD_MAP("ifconfig", cmd_ifconfig, 1, 12, "[interface [mtu ]|[address_family] [[add|del] |dhcp]]" "[dr|gw|gateway ] [netmask |prefixlen ] " - "[dns ] [hw ]"), + "[dns ] [hw ] [up|down]"), # endif # if defined(CONFIG_NET_VLAN) && !defined(CONFIG_NSH_DISABLE_VCONFIG) CMD_MAP("vconfig", cmd_vconfig, 3, 5, diff --git a/nshlib/nsh_netcmds.c b/nshlib/nsh_netcmds.c index defcb717f..c281650d2 100644 --- a/nshlib/nsh_netcmds.c +++ b/nshlib/nsh_netcmds.c @@ -584,6 +584,8 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv) #endif bool missingarg = true; bool badarg = false; + bool ifup = false; + bool ifdown = false; #ifdef CONFIG_NET_ARP arp_cfg_e arpflag = ARP_DEFAULT; #endif @@ -766,6 +768,14 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv) arpflag = ARP_DISABLE; } #endif + else if (!strcmp(tmp, "up")) + { + ifup = true; + } + else if (!strcmp(tmp, "down")) + { + ifdown = true; + } else if (hostip == NULL && i <= 4) { /* Let first non-option be host ip, to support inet/inet6 @@ -1079,6 +1089,17 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv) } #endif + /* Bring the interface up or down once everything else is in place. */ + + if (ifup) + { + netlib_ifup(ifname); + } + else if (ifdown) + { + netlib_ifdown(ifname); + } + #if !defined(CONFIG_NET_IPv4) && !defined(CONFIG_NET_IPv6) UNUSED(hostip); UNUSED(mask);