From 56ef320d6fed6676654a67097a6950d6868ce433 Mon Sep 17 00:00:00 2001 From: Zhe Weng Date: Wed, 25 Oct 2023 15:18:00 +0800 Subject: [PATCH] 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 --- nshlib/nsh_command.c | 2 +- nshlib/nsh_netcmds.c | 48 ++++++++++++++++++++++++++++++++++++++------ 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/nshlib/nsh_command.c b/nshlib/nsh_command.c index a1f5445d0..e4ce17a3d 100644 --- a/nshlib/nsh_command.c +++ b/nshlib/nsh_command.c @@ -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 ] | [|dhcp]]" + "[interface [mtu ]|[address_family] [[add|del] |dhcp]]" "[dr|gw|gateway ] [netmask |prefixlen ] " "[dns ] [hw ]"), # endif diff --git a/nshlib/nsh_netcmds.c b/nshlib/nsh_netcmds.c index e19841dc3..ddd2b7ea8 100644 --- a/nshlib/nsh_netcmds.c +++ b/nshlib/nsh_netcmds.c @@ -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 */