From 3e4faffedebe1a3ee786e9f73e5f6487d679594f Mon Sep 17 00:00:00 2001 From: june li Date: Sat, 22 Apr 2017 14:13:23 +0800 Subject: [PATCH] net: l2: Clear arp cache when disable interface. When connect to diffrent router with the same gateway ip address, need to clear arp cache when disable interface, or it will use the wrong gateway mac address. Call net_arp_clear_cache function replace to set arp_table 0. Change-Id: Ib403a0c0030832ba48824db4d2d3fcb8add63d16 Signed-off-by: june li --- include/net/arp.h | 2 ++ subsys/net/ip/l2/arp.c | 15 ++++++++++++++- subsys/net/ip/l2/ethernet.c | 14 +++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/include/net/arp.h b/include/net/arp.h index 4e8ebadf1f0..2a2673699c1 100644 --- a/include/net/arp.h +++ b/include/net/arp.h @@ -39,10 +39,12 @@ struct net_arp_hdr { struct net_pkt *net_arp_prepare(struct net_pkt *pkt); enum net_verdict net_arp_input(struct net_pkt *pkt); +void net_arp_clear_cache(void); void net_arp_init(void); #else /* CONFIG_NET_ARP */ +#define net_arp_clear_cache(...) #define net_arp_init(...) #endif /* CONFIG_NET_ARP */ diff --git a/subsys/net/ip/l2/arp.c b/subsys/net/ip/l2/arp.c index 940501d6c67..7e78fd5ad64 100644 --- a/subsys/net/ip/l2/arp.c +++ b/subsys/net/ip/l2/arp.c @@ -483,7 +483,20 @@ enum net_verdict net_arp_input(struct net_pkt *pkt) return NET_OK; } -void net_arp_init(void) +void net_arp_clear_cache(void) { + int i; + + for (i = 0; i < CONFIG_NET_ARP_TABLE_SIZE; i++) { + if (arp_table[i].pending) { + net_pkt_unref(arp_table[i].pending); + } + } + memset(&arp_table, 0, sizeof(arp_table)); } + +void net_arp_init(void) +{ + net_arp_clear_cache(); +} diff --git a/subsys/net/ip/l2/ethernet.c b/subsys/net/ip/l2/ethernet.c index b255349e5d8..304220da43c 100644 --- a/subsys/net/ip/l2/ethernet.c +++ b/subsys/net/ip/l2/ethernet.c @@ -310,4 +310,16 @@ static inline u16_t ethernet_reserve(struct net_if *iface, void *unused) return sizeof(struct net_eth_hdr); } -NET_L2_INIT(ETHERNET_L2, ethernet_recv, ethernet_send, ethernet_reserve, NULL); +static inline int ethernet_enable(struct net_if *iface, bool state) +{ + ARG_UNUSED(iface); + + if (!state) { + net_arp_clear_cache(); + } + + return 0; +} + +NET_L2_INIT(ETHERNET_L2, ethernet_recv, ethernet_send, ethernet_reserve, + ethernet_enable);