netdev/ipv6: Move xxx_ipv6multicast from arch to common code

The `xxx_ipv6multicast` function in each driver is not adapted to
multiple IPv6 addresses yet, and they're redundant, so try to take them
into common code.

Change:
1. Add MAC `g_ipv6_ethallnodes` and `g_ipv6_ethallrouters` in
   `icmpv6_devinit` and call them in `netdev_register`
2. Add multicast MAC for Neighbor Solicitation when adding any IPv6
   address, and remove them when IPv6 address is removed
3. Select `NET_MCASTGROUP` when `NET_ICMPv6` because now we need
   `d_addmac` when we have ICMPv6

Note:
We want modules outside net stack to call functions like
`netdev_ipv6_add` and never touch the related MAC address, so these MAC
functions are added as internal functions to `net/netdev/netdev.h`

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
This commit is contained in:
Zhe Weng
2023-12-13 12:39:43 +08:00
committed by Xiang Xiao
parent 06e5b661ed
commit 5aeb15469a
37 changed files with 290 additions and 2066 deletions
+29
View File
@@ -495,6 +495,35 @@ void netdown_notifier_teardown(int key);
void netdown_notifier_signal(FAR struct net_driver_s *dev);
#endif
/****************************************************************************
* Name: netdev_ipv6_addmcastmac/removemcastmac
*
* Description:
* Add / Remove an MAC address corresponds to the IPv6 address to / from
* the device's MAC filter table.
*
* Input Parameters:
* dev - The device driver structure to be modified
* addr - The IPv6 address whose related MAC will be added or removed
*
* Returned Value:
* None
*
* Assumptions:
* The caller has locked the network.
*
****************************************************************************/
#ifdef CONFIG_NET_ICMPv6
void netdev_ipv6_addmcastmac(FAR struct net_driver_s *dev,
const net_ipv6addr_t addr);
void netdev_ipv6_removemcastmac(FAR struct net_driver_s *dev,
const net_ipv6addr_t addr);
#else
# define netdev_ipv6_addmcastmac(dev,addr)
# define netdev_ipv6_removemcastmac(dev,addr)
#endif
#undef EXTERN
#ifdef __cplusplus
}
+4
View File
@@ -913,7 +913,11 @@ static int netdev_ifr_ioctl(FAR struct socket *psock, int cmd,
{
FAR struct lifreq *lreq = (FAR struct lifreq *)req;
idx = MIN(idx, CONFIG_NETDEV_MAX_IPv6_ADDR - 1);
netdev_ipv6_removemcastmac(dev, dev->d_ipv6[idx].addr);
ioctl_set_ipv6addr(dev->d_ipv6[idx].addr, &lreq->lifr_addr);
netdev_ipv6_addmcastmac(dev, dev->d_ipv6[idx].addr);
netlink_device_notify_ipaddr(dev, RTM_NEWADDR, AF_INET6,
dev->d_ipv6[idx].addr, net_ipv6_mask2pref(dev->d_ipv6[idx].mask));
}
+134
View File
@@ -31,6 +31,7 @@
#include <nuttx/net/netdev.h>
#include "inet/inet.h"
#include "netdev/netdev.h"
#include "utils/utils.h"
/****************************************************************************
@@ -50,6 +51,41 @@
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: netdev_ipv6_mcastmac
*
* Description:
* Given an IPv6 address (in network order), create a IPv6 multicast MAC
* address for ICMPv6 Neighbor Solicitation message.
*
****************************************************************************/
#ifdef CONFIG_NET_ICMPv6
static void netdev_ipv6_mcastmac(const net_ipv6addr_t addr, FAR uint8_t *mac)
{
FAR const uint8_t *ipaddr8 = (FAR const uint8_t *)addr;
/* For ICMPv6, we need to add the IPv6 multicast address
*
* For IPv6 multicast addresses, the Ethernet MAC is derived by
* the four low-order octets OR'ed with the MAC 33:33:00:00:00:00,
* so for example the IPv6 address FF02:DEAD:BEEF::1:3 would map
* to the Ethernet MAC address 33:33:00:01:00:03.
*
* NOTES: This appears correct for the ICMPv6 Router Solicitation
* Message, but the ICMPv6 Neighbor Solicitation message seems to
* use 33:33:ff:01:00:03.
*/
mac[0] = 0x33;
mac[1] = 0x33;
mac[2] = 0xff;
mac[3] = ipaddr8[13]; /* Bits: 104-111 */
mac[4] = ipaddr8[14]; /* Bits: 112-119 */
mac[5] = ipaddr8[15]; /* Bits: 120-127 */
}
#endif
/****************************************************************************
* Name: netdev_ipv6_get_scope
****************************************************************************/
@@ -176,6 +212,8 @@ int netdev_ipv6_add(FAR struct net_driver_s *dev, const net_ipv6addr_t addr,
net_ipv6addr_copy(ifaddr->addr, addr);
net_ipv6_pref2mask(ifaddr->mask, preflen);
netdev_ipv6_addmcastmac(dev, addr);
return OK;
}
@@ -213,9 +251,105 @@ int netdev_ipv6_del(FAR struct net_driver_s *dev, const net_ipv6addr_t addr,
net_ipv6addr_copy(ifaddr->addr, g_ipv6_unspecaddr);
net_ipv6addr_copy(ifaddr->mask, g_ipv6_unspecaddr);
netdev_ipv6_removemcastmac(dev, addr);
return OK;
}
/****************************************************************************
* Name: netdev_ipv6_addmcastmac/removemcastmac
*
* Description:
* Add / Remove an MAC address corresponds to the IPv6 address to / from
* the device's MAC filter table.
*
* Input Parameters:
* dev - The device driver structure to be modified
* addr - The IPv6 address whose related MAC will be added or removed
*
* Returned Value:
* None
*
* Assumptions:
* The caller has locked the network.
*
****************************************************************************/
#ifdef CONFIG_NET_ICMPv6
void netdev_ipv6_addmcastmac(FAR struct net_driver_s *dev,
const net_ipv6addr_t addr)
{
uint8_t mcastmac[ETHER_ADDR_LEN];
if (net_ipv6addr_cmp(addr, g_ipv6_unspecaddr))
{
return;
}
if (dev->d_addmac != NULL)
{
netdev_ipv6_mcastmac(addr, mcastmac);
ninfo("Add IPv6 Multicast: %02x:%02x:%02x:%02x:%02x:%02x\n",
mcastmac[0], mcastmac[1], mcastmac[2],
mcastmac[3], mcastmac[4], mcastmac[5]);
dev->d_addmac(dev, mcastmac);
}
}
void netdev_ipv6_removemcastmac(FAR struct net_driver_s *dev,
const net_ipv6addr_t addr)
{
uint8_t mcastmac[ETHER_ADDR_LEN];
#ifdef CONFIG_NETDEV_MULTIPLE_IPv6
int i;
#endif
if (net_ipv6addr_cmp(addr, g_ipv6_unspecaddr))
{
return;
}
if (dev->d_rmmac != NULL)
{
netdev_ipv6_mcastmac(addr, mcastmac);
#ifdef CONFIG_NETDEV_MULTIPLE_IPv6
/* Avoid removing mac needed by other addresses. */
for (i = 0; i < CONFIG_NETDEV_MAX_IPv6_ADDR; i++)
{
FAR struct netdev_ifaddr6_s *current = &dev->d_ipv6[i];
uint8_t currentmac[ETHER_ADDR_LEN];
/* Skip empty address and target address */
if (net_ipv6addr_cmp(current->addr, g_ipv6_unspecaddr) ||
net_ipv6addr_cmp(current->addr, addr))
{
continue;
}
/* Generate multicast MAC for this address. */
netdev_ipv6_mcastmac(current->addr, currentmac);
/* We don't remove the MAC if any other IPv6 address needs it. */
if (memcmp(currentmac, mcastmac, ETHER_ADDR_LEN) == 0)
{
return;
}
}
#endif /* CONFIG_NETDEV_MULTIPLE_IPv6 */
ninfo("Remove IPv6 Multicast: %02x:%02x:%02x:%02x:%02x:%02x\n",
mcastmac[0], mcastmac[1], mcastmac[2],
mcastmac[3], mcastmac[4], mcastmac[5]);
dev->d_rmmac(dev, mcastmac);
}
}
#endif
/****************************************************************************
* Name: netdev_ipv6_srcaddr/srcifaddr
*
+7
View File
@@ -40,6 +40,7 @@
#include <nuttx/net/can.h>
#include "utils/utils.h"
#include "icmpv6/icmpv6.h"
#include "igmp/igmp.h"
#include "mld/mld.h"
#include "netdev/netdev.h"
@@ -475,6 +476,12 @@ int netdev_register(FAR struct net_driver_s *dev, enum net_lltype_e lltype)
mld_devinit(dev);
#endif
#ifdef NET_ICMPv6_HAVE_STACK
/* Configure the device for ICMPv6 support */
icmpv6_devinit(dev);
#endif
net_unlock();
#if defined(CONFIG_NET_ETHERNET) || defined(CONFIG_DRIVERS_IEEE80211)