net: shell: Join IPv6 mcast group if needed

If user adds IPv6 address to the network interface, check
if the address is a multicast one and add it to multicast
group if it is.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
This commit is contained in:
Jukka Rissanen 2023-10-26 10:46:51 +03:00 committed by Carles Cufí
parent 1467d7fa7b
commit e44de3e381
2 changed files with 29 additions and 5 deletions

View File

@ -159,6 +159,7 @@ config NET_SHELL
bool "Network shell utilities"
select SHELL
select NET_IPV4_IGMP if NET_IPV4
select NET_IPV6_MLD if NET_IPV6
help
Activate shell module that provides network commands like
ping to the console.

View File

@ -9,6 +9,7 @@
LOG_MODULE_DECLARE(net_shell);
#include "common.h"
#include "../ip/ipv6.h"
#if defined(CONFIG_NET_IPV6_FRAGMENT)
void ipv6_frag_cb(struct net_ipv6_reassembly *reass, void *user_data)
@ -211,8 +212,19 @@ static int cmd_net_ip6_add(const struct shell *sh, size_t argc, char *argv[])
return -EINVAL;
}
if (!net_if_ipv6_addr_add(iface, &addr, NET_ADDR_MANUAL, 0)) {
PR_ERROR("Failed to add %s address to interface %p\n", argv[2], iface);
if (net_ipv6_is_addr_mcast(&addr)) {
int ret;
ret = net_ipv6_mld_join(iface, &addr);
if (ret < 0) {
PR_ERROR("Cannot %s multicast group %s for interface %d (%d)\n",
"join", net_sprint_ipv6_addr(&addr), idx, ret);
return ret;
}
} else {
if (!net_if_ipv6_addr_add(iface, &addr, NET_ADDR_MANUAL, 0)) {
PR_ERROR("Failed to add %s address to interface %p\n", argv[2], iface);
}
}
#else /* CONFIG_NET_NATIVE_IPV6 */
@ -250,9 +262,20 @@ static int cmd_net_ip6_del(const struct shell *sh, size_t argc, char *argv[])
return -EINVAL;
}
if (!net_if_ipv6_addr_rm(iface, &addr)) {
PR_ERROR("Failed to delete %s\n", argv[2]);
return -1;
if (net_ipv6_is_addr_mcast(&addr)) {
int ret;
ret = net_ipv6_mld_leave(iface, &addr);
if (ret < 0) {
PR_ERROR("Cannot %s multicast group %s for interface %d (%d)\n",
"leave", net_sprint_ipv6_addr(&addr), idx, ret);
return ret;
}
} else {
if (!net_if_ipv6_addr_rm(iface, &addr)) {
PR_ERROR("Failed to delete %s\n", argv[2]);
return -1;
}
}
#else /* CONFIG_NET_NATIVE_IPV6 */