nshlib/ifconfig: Add support for add/del a single IPv6 address

Note: We're using similar error codes as Linux:
```
linux> sudo ifconfig eth0 inet6 add fc00::2/64
linux> sudo ifconfig eth0 inet6 del fc00::2/112
SIOCDIFADDR: Cannot assign requested address
linux> sudo ifconfig eth0 inet6 del fc00::3/64
SIOCDIFADDR: Cannot assign requested address
linux> sudo ifconfig eth0 inet6 add fc00::2/64
SIOCSIFADDR: File exists

nuttx> ifconfig eth0 inet6 add fc00::2/64
nuttx> ifconfig eth0 inet6 del fc00::2/112
Failed to manage IPv6 address: Cannot assign requested address
nuttx> ifconfig eth0 inet6 del fc00::3/112
Failed to manage IPv6 address: Cannot assign requested address
nuttx> ifconfig eth0 inet6 add fc00::2/64
Failed to manage IPv6 address: File exists
```

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
This commit is contained in:
Zhe Weng 2023-10-25 15:18:00 +08:00 committed by Xiang Xiao
parent 2f9d98cab2
commit 56ef320d6f
2 changed files with 43 additions and 7 deletions

View File

@ -278,7 +278,7 @@ static const struct cmdmap_s g_cmdmap[] =
#ifdef CONFIG_NET
# ifndef CONFIG_NSH_DISABLE_IFCONFIG
CMD_MAP("ifconfig", cmd_ifconfig, 1, 12,
"[interface [address_family] [mtu <len>] | [<ip-address>|dhcp]]"
"[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>]"),
# endif

View File

@ -555,6 +555,9 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
struct in6_addr addr6;
struct in6_addr gip6 = IN6ADDR_ANY_INIT;
FAR char *preflen = NULL;
# ifdef CONFIG_NETDEV_MULTIPLE_IPv6
bool remove = false;
# endif
#endif
int i;
FAR char *ifname = NULL;
@ -717,8 +720,14 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
#endif
else if (!strcmp(tmp, "add"))
{
/* Compatible with linux IPv6 command, do nothing. */
#if defined(CONFIG_NET_IPv6) && defined(CONFIG_NETDEV_MULTIPLE_IPv6)
remove = false;
continue;
}
else if (!strcmp(tmp, "del"))
{
remove = true;
#endif
continue;
}
else if (!strcmp(tmp, "mtu"))
@ -805,7 +814,9 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
inet_pton(AF_INET6, hostip, &addr6);
}
#ifndef CONFIG_NETDEV_MULTIPLE_IPv6
netlib_set_ipv6addr(ifname, &addr6);
#endif
}
#endif /* CONFIG_NET_IPv6 */
@ -899,23 +910,48 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
if (inet6)
#endif
{
struct in6_addr mask6;
#ifdef CONFIG_NETDEV_MULTIPLE_IPv6
uint8_t plen;
#endif
if (mask != NULL)
{
ninfo("Netmask: %s\n", mask);
inet_pton(AF_INET6, mask, &addr6);
inet_pton(AF_INET6, mask, &mask6);
}
else if (preflen != NULL)
{
ninfo("Prefixlen: %s\n", preflen);
netlib_prefix2ipv6netmask(atoi(preflen), &addr6);
netlib_prefix2ipv6netmask(atoi(preflen), &mask6);
}
else
{
ninfo("Netmask: Default\n");
inet_pton(AF_INET6, "ffff:ffff:ffff:ffff::", &addr6);
inet_pton(AF_INET6, "ffff:ffff:ffff:ffff::", &mask6);
}
netlib_set_ipv6netmask(ifname, &addr6);
#ifdef CONFIG_NETDEV_MULTIPLE_IPv6
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");
/* REVISIT: Should we return ERROR or just let it go? */
return ERROR;
}
#else
netlib_set_ipv6netmask(ifname, &mask6);
#endif /* CONFIG_NETDEV_MULTIPLE_IPv6 */
}
#endif /* CONFIG_NET_IPv6 */