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
+1 -1
View File
@@ -22,7 +22,7 @@ if(CONFIG_NET_ICMPv6 AND NOT CONFIG_NET_ICMPv6_NO_STACK)
# ICMPv6 source files
set(SRCS icmpv6_input.c icmpv6_solicit.c icmpv6_advertise.c
icmpv6_linkipaddr.c icmpv6_reply.c)
icmpv6_linkipaddr.c icmpv6_reply.c icmpv6_initialize.c)
if(CONFIG_NET_ICMPv6_SOCKET)
list(
+1
View File
@@ -8,6 +8,7 @@ menu "ICMPv6 Networking Support"
config NET_ICMPv6
bool "Enable ICMPv6 networking"
default n
select NET_MCASTGROUP
depends on NET
---help---
Enable minimal ICMPv6 support. Includes built-in support
+1 -1
View File
@@ -24,7 +24,7 @@ ifneq ($(CONFIG_NET_ICMPv6_NO_STACK),y)
# ICMPv6 source files
NET_CSRCS += icmpv6_input.c icmpv6_solicit.c icmpv6_advertise.c
NET_CSRCS += icmpv6_linkipaddr.c icmpv6_reply.c
NET_CSRCS += icmpv6_linkipaddr.c icmpv6_reply.c icmpv6_initialize.c
ifeq ($(CONFIG_NET_ICMPv6_SOCKET),y)
SOCK_CSRCS += icmpv6_sockif.c icmpv6_conn.c icmpv6_sendmsg.c
+20
View File
@@ -165,6 +165,26 @@ EXTERN const struct sock_intf_s g_icmpv6_sockif;
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: icmpv6_devinit
*
* Description:
* Called when a new network device is registered to configure that device
* for ICMPv6 support.
*
* Input Parameters:
* dev - The device driver structure to configure.
*
* Returned Value:
* None
*
* Assumptions:
* The network is locked.
*
****************************************************************************/
void icmpv6_devinit(FAR struct net_driver_s *dev);
/****************************************************************************
* Name: icmpv6_input
*
+86
View File
@@ -0,0 +1,86 @@
/****************************************************************************
* net/icmpv6/icmpv6_initialize.c
* ICMPv6 initialization logic
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <debug.h>
#include <nuttx/net/ethernet.h>
#include <nuttx/net/netdev.h>
#include "icmpv6/icmpv6.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: icmpv6_devinit
*
* Description:
* Called when a new network device is registered to configure that device
* for ICMPv6 support.
*
* Input Parameters:
* dev - The device driver structure to configure.
*
* Returned Value:
* None
*
* Assumptions:
* The network is locked.
*
****************************************************************************/
void icmpv6_devinit(FAR struct net_driver_s *dev)
{
ninfo("ICMPv6 initializing dev %p\n", dev);
#ifdef CONFIG_NET_ETHERNET
# ifdef CONFIG_NET_ICMPv6_AUTOCONF
/* Add the IPv6 all link-local nodes Ethernet address. This is the
* address that we expect to receive ICMPv6 Router Advertisement
* packets.
*/
if (dev->d_addmac != NULL)
{
dev->d_addmac(dev, g_ipv6_ethallnodes.ether_addr_octet);
}
# endif /* CONFIG_NET_ICMPv6_AUTOCONF */
# ifdef CONFIG_NET_ICMPv6_ROUTER
/* Add the IPv6 all link-local routers Ethernet address. This is the
* address that we expect to receive ICMPv6 Router Solicitation
* packets.
*/
if (dev->d_addmac != NULL)
{
dev->d_addmac(dev, g_ipv6_ethallrouters.ether_addr_octet);
}
# endif /* CONFIG_NET_ICMPv6_ROUTER */
#endif /* CONFIG_NET_ETHERNET */
}
+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)