net/arp: clean the arp table when netdev carrier off

Fix the arp address changed if netdev renew, since the
arp table should be cleared when the netdev carrier off

Signed-off-by: songlinzhang <songlinzhang@xiaomi.com>
This commit is contained in:
songlinzhang
2021-11-18 13:29:25 +08:00
committed by Xiang Xiao
parent 4db9513658
commit 22df553443
7 changed files with 104 additions and 18 deletions
+1
View File
@@ -85,6 +85,7 @@ struct arp_entry_s
in_addr_t at_ipaddr; /* IP address */ in_addr_t at_ipaddr; /* IP address */
struct ether_addr at_ethaddr; /* Hardware address */ struct ether_addr at_ethaddr; /* Hardware address */
clock_t at_time; /* Time of last usage */ clock_t at_time; /* Time of last usage */
FAR struct net_driver_s *at_dev; /* The device driver structure */
}; };
/**************************************************************************** /****************************************************************************
+25 -4
View File
@@ -358,6 +358,22 @@ int arp_find(in_addr_t ipaddr, FAR struct ether_addr *ethaddr);
void arp_delete(in_addr_t ipaddr); void arp_delete(in_addr_t ipaddr);
/****************************************************************************
* Name: arp_cleanup
*
* Description:
* Clear the ARP table on the network device
*
* Input Parameters:
* dev - The device driver structure
*
* Assumptions
* The network is locked to assure exclusive access to the ARP table.
*
****************************************************************************/
void arp_cleanup(FAR struct net_driver_s *dev);
/**************************************************************************** /****************************************************************************
* Name: arp_update * Name: arp_update
* *
@@ -366,6 +382,7 @@ void arp_delete(in_addr_t ipaddr);
* address of an existing association. * address of an existing association.
* *
* Input Parameters: * Input Parameters:
* dev - The device driver structure
* ipaddr - The IP address as an inaddr_t * ipaddr - The IP address as an inaddr_t
* ethaddr - Refers to a HW address uint8_t[IFHWADDRLEN] * ethaddr - Refers to a HW address uint8_t[IFHWADDRLEN]
* *
@@ -378,7 +395,8 @@ void arp_delete(in_addr_t ipaddr);
* *
****************************************************************************/ ****************************************************************************/
int arp_update(in_addr_t ipaddr, FAR uint8_t *ethaddr); int arp_update(FAR struct net_driver_s *dev, in_addr_t ipaddr,
FAR uint8_t *ethaddr);
/**************************************************************************** /****************************************************************************
* Name: arp_hdr_update * Name: arp_hdr_update
@@ -388,6 +406,7 @@ int arp_update(in_addr_t ipaddr, FAR uint8_t *ethaddr);
* address of an existing association. * address of an existing association.
* *
* Input Parameters: * Input Parameters:
* dev - The device driver structure
* pipaddr - Refers to an IP address uint16_t[2] in network order * pipaddr - Refers to an IP address uint16_t[2] in network order
* ethaddr - Refers to a HW address uint8_t[IFHWADDRLEN] * ethaddr - Refers to a HW address uint8_t[IFHWADDRLEN]
* *
@@ -400,7 +419,8 @@ int arp_update(in_addr_t ipaddr, FAR uint8_t *ethaddr);
* *
****************************************************************************/ ****************************************************************************/
void arp_hdr_update(FAR uint16_t *pipaddr, FAR uint8_t *ethaddr); void arp_hdr_update(FAR struct net_driver_s *dev, FAR uint16_t *pipaddr,
FAR uint8_t *ethaddr);
/**************************************************************************** /****************************************************************************
* Name: arp_snapshot * Name: arp_snapshot
@@ -462,8 +482,9 @@ void arp_dump(FAR struct arp_hdr_s *arp);
# define arp_notify(i) # define arp_notify(i)
# define arp_find(i,e) (-ENOSYS) # define arp_find(i,e) (-ENOSYS)
# define arp_delete(i) # define arp_delete(i)
# define arp_update(i,m); # define arp_cleanup(d)
# define arp_hdr_update(i,m); # define arp_update(d,i,m);
# define arp_hdr_update(d,i,m);
# define arp_snapshot(s,n) (0) # define arp_snapshot(s,n) (0)
# define arp_dump(arp) # define arp_dump(arp)
+2 -2
View File
@@ -121,7 +121,7 @@ void arp_arpin(FAR struct net_driver_s *dev)
* with this host in the future. * with this host in the future.
*/ */
arp_hdr_update(arp->ah_sipaddr, arp->ah_shwaddr); arp_hdr_update(dev, arp->ah_sipaddr, arp->ah_shwaddr);
arp->ah_opcode = HTONS(ARP_REPLY); arp->ah_opcode = HTONS(ARP_REPLY);
memcpy(arp->ah_dhwaddr, arp->ah_shwaddr, ETHER_ADDR_LEN); memcpy(arp->ah_dhwaddr, arp->ah_shwaddr, ETHER_ADDR_LEN);
@@ -152,7 +152,7 @@ void arp_arpin(FAR struct net_driver_s *dev)
{ {
/* Yes... Insert the address mapping in the ARP table */ /* Yes... Insert the address mapping in the ARP table */
arp_hdr_update(arp->ah_sipaddr, arp->ah_shwaddr); arp_hdr_update(dev, arp->ah_sipaddr, arp->ah_shwaddr);
/* Then notify any logic waiting for the ARP result */ /* Then notify any logic waiting for the ARP result */
+1 -1
View File
@@ -90,7 +90,7 @@ void arp_ipin(FAR struct net_driver_s *dev)
srcipaddr = net_ip4addr_conv32(IPBUF->eh_srcipaddr); srcipaddr = net_ip4addr_conv32(IPBUF->eh_srcipaddr);
if (net_ipv4addr_maskcmp(srcipaddr, dev->d_ipaddr, dev->d_netmask)) if (net_ipv4addr_maskcmp(srcipaddr, dev->d_ipaddr, dev->d_netmask))
{ {
arp_hdr_update(IPBUF->eh_srcipaddr, ETHBUF->src); arp_hdr_update(dev, IPBUF->eh_srcipaddr, ETHBUF->src);
} }
} }
+35 -3
View File
@@ -181,6 +181,7 @@ arp_return_old_entry(FAR struct arp_entry_s *e1, FAR struct arp_entry_s *e2)
* address of an existing association. * address of an existing association.
* *
* Input Parameters: * Input Parameters:
* dev - The device driver structure
* ipaddr - The IP address as an inaddr_t * ipaddr - The IP address as an inaddr_t
* ethaddr - Refers to a HW address uint8_t[IFHWADDRLEN] * ethaddr - Refers to a HW address uint8_t[IFHWADDRLEN]
* *
@@ -193,7 +194,8 @@ arp_return_old_entry(FAR struct arp_entry_s *e1, FAR struct arp_entry_s *e2)
* *
****************************************************************************/ ****************************************************************************/
int arp_update(in_addr_t ipaddr, FAR uint8_t *ethaddr) int arp_update(FAR struct net_driver_s *dev, in_addr_t ipaddr,
FAR uint8_t *ethaddr)
{ {
FAR struct arp_entry_s *tabptr = &g_arptable[0]; FAR struct arp_entry_s *tabptr = &g_arptable[0];
int i; int i;
@@ -231,6 +233,7 @@ int arp_update(in_addr_t ipaddr, FAR uint8_t *ethaddr)
tabptr->at_ipaddr = ipaddr; tabptr->at_ipaddr = ipaddr;
memcpy(tabptr->at_ethaddr.ether_addr_octet, ethaddr, ETHER_ADDR_LEN); memcpy(tabptr->at_ethaddr.ether_addr_octet, ethaddr, ETHER_ADDR_LEN);
tabptr->at_dev = dev;
tabptr->at_time = clock_systime_ticks(); tabptr->at_time = clock_systime_ticks();
return OK; return OK;
} }
@@ -243,6 +246,7 @@ int arp_update(in_addr_t ipaddr, FAR uint8_t *ethaddr)
* address of an existing association. * address of an existing association.
* *
* Input Parameters: * Input Parameters:
* dev - The device driver structure
* pipaddr - Refers to an IP address uint16_t[2] in network order * pipaddr - Refers to an IP address uint16_t[2] in network order
* ethaddr - Refers to a HW address uint8_t[IFHWADDRLEN] * ethaddr - Refers to a HW address uint8_t[IFHWADDRLEN]
* *
@@ -255,13 +259,14 @@ int arp_update(in_addr_t ipaddr, FAR uint8_t *ethaddr)
* *
****************************************************************************/ ****************************************************************************/
void arp_hdr_update(FAR uint16_t *pipaddr, FAR uint8_t *ethaddr) void arp_hdr_update(FAR struct net_driver_s *dev, FAR uint16_t *pipaddr,
FAR uint8_t *ethaddr)
{ {
in_addr_t ipaddr = net_ip4addr_conv32(pipaddr); in_addr_t ipaddr = net_ip4addr_conv32(pipaddr);
/* Update the ARP table */ /* Update the ARP table */
arp_update(ipaddr, ethaddr); arp_update(dev, ipaddr, ethaddr);
} }
/**************************************************************************** /****************************************************************************
@@ -393,6 +398,33 @@ void arp_delete(in_addr_t ipaddr)
} }
} }
/****************************************************************************
* Name: arp_cleanup
*
* Description:
* Clear the ARP table on the network device
*
* Input Parameters:
* dev - The device driver structure
*
* Assumptions
* The network is locked to assure exclusive access to the ARP table.
*
****************************************************************************/
void arp_cleanup(FAR struct net_driver_s *dev)
{
int i;
for (i = 0; i < CONFIG_NET_ARPTAB_SIZE; ++i)
{
if (dev == g_arptable[i].at_dev)
{
memset(&g_arptable[i], 0, sizeof(g_arptable[i]));
}
}
}
/**************************************************************************** /****************************************************************************
* Name: arp_snapshot * Name: arp_snapshot
* *
+2
View File
@@ -37,6 +37,7 @@
#include "netdev/netdev.h" #include "netdev/netdev.h"
#include "netlink/netlink.h" #include "netlink/netlink.h"
#include "arp/arp.h"
/**************************************************************************** /****************************************************************************
* Public Functions * Public Functions
@@ -94,6 +95,7 @@ int netdev_carrier_off(FAR struct net_driver_s *dev)
/* Notify clients that the network has been taken down */ /* Notify clients that the network has been taken down */
devif_dev_event(dev, NULL, NETDEV_DOWN); devif_dev_event(dev, NULL, NETDEV_DOWN);
arp_cleanup(dev);
return OK; return OK;
} }
+35 -5
View File
@@ -1222,6 +1222,40 @@ static int netdev_imsf_ioctl(FAR struct socket *psock, int cmd,
} }
#endif #endif
/****************************************************************************
* Name: netdev_arp_callback
*
* Description:
* This is a callback that checks if the Ethernet network device has the
* indicated name
*
* Input Parameters:
* dev Ethernet driver device structure
* req The argument of the ioctl cmd
*
* Returned Value:
* 1 on success
* 0 on error
****************************************************************************/
#ifdef CONFIG_NET_ARP
static int netdev_arp_callback(FAR struct net_driver_s *dev, FAR void *arg)
{
FAR struct arpreq *req = arg;
FAR struct sockaddr_in *addr = (FAR struct sockaddr_in *)&req->arp_pa;
if (strncmp(dev->d_ifname, (FAR const char *)req->arp_dev,
sizeof(dev->d_ifname)))
{
return 0;
}
arp_update(dev, addr->sin_addr.s_addr,
(FAR uint8_t *)req->arp_ha.sa_data);
return 1;
}
#endif
/**************************************************************************** /****************************************************************************
* Name: netdev_arp_ioctl * Name: netdev_arp_ioctl
* *
@@ -1256,15 +1290,11 @@ static int netdev_arp_ioctl(FAR struct socket *psock, int cmd,
req->arp_pa.sa_family == AF_INET && req->arp_pa.sa_family == AF_INET &&
req->arp_ha.sa_family == ARPHRD_ETHER) req->arp_ha.sa_family == ARPHRD_ETHER)
{ {
FAR struct sockaddr_in *addr =
(FAR struct sockaddr_in *)&req->arp_pa;
/* Update any existing ARP table entry for this protocol /* Update any existing ARP table entry for this protocol
* address -OR- add a new ARP table entry if there is not. * address -OR- add a new ARP table entry if there is not.
*/ */
ret = arp_update(addr->sin_addr.s_addr, ret = netdev_foreach(netdev_arp_callback, req) ? OK : -EINVAL;
(FAR uint8_t *)req->arp_ha.sa_data);
} }
else else
{ {