nshlib/ifconfig: support "ifconfig <iface> 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 <iface> 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 <wangjianyu3@xiaomi.com>
This commit is contained in:
wangjianyu3 2026-08-10 16:24:37 +08:00 committed by Xiang Xiao
parent 198b89a81d
commit ab7e0660d5
2 changed files with 22 additions and 1 deletions

View file

@ -296,7 +296,7 @@ static const struct cmdmap_s g_cmdmap[] =
CMD_MAP("ifconfig", cmd_ifconfig, 1, 12,
"[interface [mtu <len>]|[address_family] [[add|del] <ip-address>|dhcp]]"
"[dr|gw|gateway <dr-address>] [netmask <net-mask>|prefixlen <len>] "
"[dns <dns-address>] [hw <hw-mac>]"),
"[dns <dns-address>] [hw <hw-mac>] [up|down]"),
# endif
# if defined(CONFIG_NET_VLAN) && !defined(CONFIG_NSH_DISABLE_VCONFIG)
CMD_MAP("vconfig", cmd_vconfig, 3, 5,

View file

@ -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);