mirror of
https://github.com/apache/nuttx-apps.git
synced 2026-08-12 07:55:42 +00:00
nshlib/ifconfig: only apply the settings that were asked for
cmd_ifconfig() pushed every setting to the device on each invocation,
whether or not the command line carried it, and stopped parsing as soon
as it saw "mtu". So a command meant to touch one thing quietly rewrote
the rest of the interface configuration:
ifconfig eth0 hw 00:11:22:33:44:55 # clears the address, netmask,
# gateway and resolver, and kicks
# off a DHCP request
ifconfig eth0 mtu 1500 # same, plus the default route
ifconfig eth0 mtu 1500 dns 8.8.8.8 # dns silently ignored
In detail:
- the address was written unconditionally. For IPv6 that read the
uninitialized addr6 off the stack and pushed whatever it held into
the device; for IPv4 it forced 0.0.0.0.
- the netmask fell back to a hard-coded 255.255.255.0, or to
ffff:ffff:ffff:ffff:: for IPv6.
- the IPv4 gateway was always written, unlike the IPv6 one, so it fell
back to INADDR_ANY.
- the resolver fell back to that gateway, i.e. to 0.0.0.0.
- the DHCP client was started whenever gip was left at zero instead of
when "dhcp" was asked for, and gip only becomes non-zero when an
address or a gateway is parsed.
- the "mtu" branch returned as soon as netlib_set_mtu() succeeded, so
every argument behind it was dropped.
Write each setting only when the user provided it, or when an address
is being assigned and the setting belongs to it. Keeping the address
case is deliberate: a freshly assigned address still needs a mask and
a route, so dropping the derived netmask and "x.x.x.1" gateway there
would be a regression of its own. DHCP now triggers on the "dhcp"
keyword, which is the only way to request it. And with nothing left
to clobber, the "mtu" branch no longer has to bail out early.
This is how ifconfig behaves elsewhere: Linux net-tools walks the
argument vector in a single loop where every keyword handler ends in
"continue" and none of them returns, so each keyword is an independent,
idempotent operation applied in the order it was written.
As a side effect mip is now only read along the paths that assign it,
since the IPv4 gateway fallback only derives an address from mip when
gip came from hostip.
Assisted-by: GitHubCopilot:claude-opus-5
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
This commit is contained in:
parent
413d2ddbc3
commit
a8d55f4a10
1 changed files with 45 additions and 44 deletions
|
|
@ -557,7 +557,7 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
|||
#ifdef CONFIG_NET_IPv4
|
||||
struct in_addr addr;
|
||||
in_addr_t gip = INADDR_ANY;
|
||||
in_addr_t mip;
|
||||
in_addr_t mip = INADDR_ANY;
|
||||
#endif
|
||||
#ifdef CONFIG_NET_IPv6
|
||||
struct in6_addr addr6;
|
||||
|
|
@ -809,7 +809,6 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
|||
if (mtu != 0)
|
||||
{
|
||||
netlib_set_mtu(ifname, mtu);
|
||||
return OK;
|
||||
}
|
||||
|
||||
/* Set IP address */
|
||||
|
|
@ -839,11 +838,11 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
|||
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
||||
return ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef CONFIG_NETDEV_MULTIPLE_IPv6
|
||||
netlib_set_ipv6addr(ifname, &addr6);
|
||||
netlib_set_ipv6addr(ifname, &addr6);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#endif /* CONFIG_NET_IPv6 */
|
||||
|
||||
|
|
@ -851,31 +850,25 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
|||
#ifdef CONFIG_NET_IPv6
|
||||
else
|
||||
#endif
|
||||
if (hostip != NULL)
|
||||
{
|
||||
if (hostip != NULL)
|
||||
{
|
||||
#if defined(CONFIG_NETUTILS_DHCPC)
|
||||
if (strcmp(hostip, "dhcp") == 0)
|
||||
{
|
||||
/* Set DHCP addr */
|
||||
if (strcmp(hostip, "dhcp") == 0)
|
||||
{
|
||||
/* Set DHCP addr */
|
||||
|
||||
ninfo("DHCPC Mode\n");
|
||||
addr.s_addr = 0;
|
||||
gip = 0;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
/* Set host IP address */
|
||||
|
||||
ninfo("Host IP: %s\n", hostip);
|
||||
addr.s_addr = inet_addr(hostip);
|
||||
gip = addr.s_addr;
|
||||
}
|
||||
ninfo("DHCPC Mode\n");
|
||||
addr.s_addr = 0;
|
||||
gip = 0;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
addr.s_addr = 0;
|
||||
/* Set host IP address */
|
||||
|
||||
ninfo("Host IP: %s\n", hostip);
|
||||
addr.s_addr = inet_addr(hostip);
|
||||
gip = addr.s_addr;
|
||||
}
|
||||
|
||||
netlib_set_ipv4addr(ifname, &addr);
|
||||
|
|
@ -908,33 +901,39 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
|||
ninfo("Prefixlen: %s\n", preflen);
|
||||
netlib_prefix2ipv6netmask(atoi(preflen), &mask6);
|
||||
}
|
||||
else
|
||||
else if (hostip != NULL)
|
||||
{
|
||||
ninfo("Netmask: Default\n");
|
||||
inet_pton(AF_INET6, "ffff:ffff:ffff:ffff::", &mask6);
|
||||
}
|
||||
|
||||
#ifdef CONFIG_NETDEV_MULTIPLE_IPv6
|
||||
plen = netlib_ipv6netmask2prefix(mask6.in6_u.u6_addr16);
|
||||
if (remove)
|
||||
if (hostip != NULL)
|
||||
{
|
||||
ret = netlib_del_ipv6addr(ifname, &addr6, plen);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = netlib_add_ipv6addr(ifname, &addr6, plen);
|
||||
}
|
||||
plen = netlib_ipv6netmask2prefix(mask6.in6_u.u6_addr16);
|
||||
if (remove)
|
||||
{
|
||||
ret = netlib_del_ipv6addr(ifname, &addr6, plen);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = netlib_add_ipv6addr(ifname, &addr6, plen);
|
||||
}
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
perror("Failed to manage IPv6 address");
|
||||
if (ret < 0)
|
||||
{
|
||||
perror("Failed to manage IPv6 address");
|
||||
|
||||
/* REVISIT: Should we return ERROR or just let it go? */
|
||||
/* REVISIT: Should we return ERROR or just let it go? */
|
||||
|
||||
return ERROR;
|
||||
return ERROR;
|
||||
}
|
||||
}
|
||||
#else
|
||||
netlib_set_ipv6netmask(ifname, &mask6);
|
||||
if (mask != NULL || preflen != NULL || hostip != NULL)
|
||||
{
|
||||
netlib_set_ipv6netmask(ifname, &mask6);
|
||||
}
|
||||
#endif /* CONFIG_NETDEV_MULTIPLE_IPv6 */
|
||||
}
|
||||
#endif /* CONFIG_NET_IPv6 */
|
||||
|
|
@ -943,6 +942,7 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
|||
#ifdef CONFIG_NET_IPv6
|
||||
else
|
||||
#endif
|
||||
if (mask != NULL || hostip != NULL)
|
||||
{
|
||||
if (mask != NULL)
|
||||
{
|
||||
|
|
@ -989,6 +989,7 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
|||
#ifdef CONFIG_NET_IPv6
|
||||
else
|
||||
#endif
|
||||
if (gwip != NULL || hostip != NULL)
|
||||
{
|
||||
if (gwip != NULL)
|
||||
{
|
||||
|
|
@ -1030,14 +1031,14 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
|||
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
netlib_set_ipv6dnsaddr(&addr6);
|
||||
}
|
||||
else
|
||||
else if (hostip != NULL)
|
||||
{
|
||||
ninfo("DNS: Default\n");
|
||||
addr6 = gip6;
|
||||
netlib_set_ipv6dnsaddr(&gip6);
|
||||
}
|
||||
|
||||
netlib_set_ipv6dnsaddr(&addr6);
|
||||
}
|
||||
#endif /* CONFIG_NET_IPv6 */
|
||||
|
||||
|
|
@ -1045,6 +1046,7 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
|||
#ifdef CONFIG_NET_IPv6
|
||||
else
|
||||
#endif
|
||||
if (dns != NULL || hostip != NULL)
|
||||
{
|
||||
if (dns != NULL)
|
||||
{
|
||||
|
|
@ -1063,8 +1065,7 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
|||
#endif /* CONFIG_NETDB_DNSCLIENT */
|
||||
|
||||
#if defined(CONFIG_NETUTILS_DHCPC)
|
||||
|
||||
if (!gip)
|
||||
if (hostip != NULL && strcmp(hostip, "dhcp") == 0)
|
||||
{
|
||||
netlib_obtain_ipv4addr(ifname);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue