Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: arp: Add support for gratuitous ARP transmission #71873

Merged
merged 2 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions subsys/net/l2/ethernet/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ config NET_ARP_GRATUITOUS
ff:ff:ff:ff:ff:ff. Ordinarily, no reply packet will occur.
A gratuitous ARP reply is a reply to which no request has been made.

config NET_ARP_GRATUITOUS_TRANSMISSION
bool "Transmit gratuitous ARP requests"
depends on NET_ARP_GRATUITOUS
depends on NET_MGMT_EVENT
depends on NET_MGMT_EVENT_INFO
help
Transmit gratuitous ARP requests, as defined in RFC 5227.

config NET_ARP_GRATUITOUS_INTERVAL
int "Time interval (in seconds) between sending gratuitous ARP requests"
depends on NET_ARP_GRATUITOUS_TRANSMISSION
default 60

if NET_ARP
module = NET_ARP
module-dep = NET_LOG
Expand Down
157 changes: 157 additions & 0 deletions subsys/net/l2/ethernet/arp.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ LOG_MODULE_REGISTER(net_arp, CONFIG_NET_ARP_LOG_LEVEL);
#include <zephyr/net/net_pkt.h>
#include <zephyr/net/net_if.h>
#include <zephyr/net/net_stats.h>
#include <zephyr/net/net_mgmt.h>

#include "arp.h"
#include "net_private.h"
Expand All @@ -34,6 +35,12 @@ static struct k_work_delayable arp_request_timer;

static struct k_mutex arp_mutex;

#if defined(CONFIG_NET_ARP_GRATUITOUS_TRANSMISSION)
static struct net_mgmt_event_callback iface_event_cb;
static struct net_mgmt_event_callback ipv4_event_cb;
static struct k_work_delayable arp_gratuitous_work;
#endif /* defined(CONFIG_NET_ARP_GRATUITOUS_TRANSMISSION) */

static void arp_entry_cleanup(struct arp_entry *entry, bool pending)
{
NET_DBG("entry %p", entry);
Expand Down Expand Up @@ -466,6 +473,141 @@ static void arp_gratuitous(struct net_if *iface,
}
}

#if defined(CONFIG_NET_ARP_GRATUITOUS_TRANSMISSION)
static void arp_gratuitous_send(struct net_if *iface,
struct in_addr *ipaddr)
{
struct net_arp_hdr *hdr;
struct net_pkt *pkt;

pkt = net_pkt_alloc_with_buffer(iface, sizeof(struct net_arp_hdr),
AF_UNSPEC, 0, NET_BUF_TIMEOUT);
if (!pkt) {
return;
}

net_buf_add(pkt->buffer, sizeof(struct net_arp_hdr));
net_pkt_set_vlan_tag(pkt, net_eth_get_vlan_tag(iface));

hdr = NET_ARP_HDR(pkt);

hdr->hwtype = htons(NET_ARP_HTYPE_ETH);
hdr->protocol = htons(NET_ETH_PTYPE_IP);
hdr->hwlen = sizeof(struct net_eth_addr);
hdr->protolen = sizeof(struct in_addr);
hdr->opcode = htons(NET_ARP_REQUEST);

memcpy(&hdr->dst_hwaddr.addr, net_eth_broadcast_addr(),
sizeof(struct net_eth_addr));
memcpy(&hdr->src_hwaddr.addr, net_if_get_link_addr(iface)->addr,
sizeof(struct net_eth_addr));

net_ipv4_addr_copy_raw(hdr->dst_ipaddr, (uint8_t *)ipaddr);
net_ipv4_addr_copy_raw(hdr->src_ipaddr, (uint8_t *)ipaddr);

net_pkt_lladdr_src(pkt)->addr = net_if_get_link_addr(iface)->addr;
net_pkt_lladdr_src(pkt)->len = sizeof(struct net_eth_addr);

net_pkt_lladdr_dst(pkt)->addr = (uint8_t *)net_eth_broadcast_addr();
net_pkt_lladdr_dst(pkt)->len = sizeof(struct net_eth_addr);

NET_DBG("Sending gratuitous ARP pkt %p", pkt);

if (net_if_send_data(iface, pkt) == NET_DROP) {
net_pkt_unref(pkt);
}
}

static void notify_all_ipv4_addr(struct net_if *iface)
{
struct net_if_ipv4 *ipv4 = iface->config.ip.ipv4;
int i;

if (!ipv4) {
return;
}

for (i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) {
if (ipv4->unicast[i].ipv4.is_used &&
ipv4->unicast[i].ipv4.address.family == AF_INET &&
ipv4->unicast[i].ipv4.addr_state == NET_ADDR_PREFERRED) {
arp_gratuitous_send(iface,
&ipv4->unicast[i].ipv4.address.in_addr);
}
}
}

static void iface_event_handler(struct net_mgmt_event_callback *cb,
uint32_t mgmt_event, struct net_if *iface)
{
ARG_UNUSED(cb);

if (!(net_if_l2(iface) == &NET_L2_GET_NAME(ETHERNET) ||
net_eth_is_vlan_interface(iface))) {
return;
}

if (mgmt_event != NET_EVENT_IF_UP) {
return;
}

notify_all_ipv4_addr(iface);
}

static void ipv4_event_handler(struct net_mgmt_event_callback *cb,
uint32_t mgmt_event, struct net_if *iface)
{
struct in_addr *ipaddr;

if (!(net_if_l2(iface) == &NET_L2_GET_NAME(ETHERNET) ||
net_eth_is_vlan_interface(iface))) {
return;
}

if (!net_if_is_up(iface)) {
return;
}

if (mgmt_event != NET_EVENT_IPV4_ADDR_ADD) {
return;
}

if (cb->info_length != sizeof(struct in_addr)) {
return;
}

ipaddr = (struct in_addr *)cb->info;

arp_gratuitous_send(iface, ipaddr);
}

static void iface_cb(struct net_if *iface, void *user_data)
{
ARG_UNUSED(user_data);

if (!(net_if_l2(iface) == &NET_L2_GET_NAME(ETHERNET) ||
net_eth_is_vlan_interface(iface))) {
return;
}

if (!net_if_is_up(iface)) {
return;
}

notify_all_ipv4_addr(iface);
}

static void arp_gratuitous_work_handler(struct k_work *work)
{
ARG_UNUSED(work);

net_if_foreach(iface_cb, NULL);

k_work_reschedule(&arp_gratuitous_work,
K_SECONDS(CONFIG_NET_ARP_GRATUITOUS_INTERVAL));
}
#endif /* defined(CONFIG_NET_ARP_GRATUITOUS_TRANSMISSION) */

void net_arp_update(struct net_if *iface,
struct in_addr *src,
struct net_eth_addr *hwaddr,
Expand Down Expand Up @@ -837,4 +979,19 @@ void net_arp_init(void)
k_mutex_init(&arp_mutex);

arp_cache_initialized = true;

#if defined(CONFIG_NET_ARP_GRATUITOUS_TRANSMISSION)
net_mgmt_init_event_callback(&iface_event_cb, iface_event_handler,
NET_EVENT_IF_UP);
net_mgmt_init_event_callback(&ipv4_event_cb, ipv4_event_handler,
NET_EVENT_IPV4_ADDR_ADD);

net_mgmt_add_event_callback(&iface_event_cb);
net_mgmt_add_event_callback(&ipv4_event_cb);

k_work_init_delayable(&arp_gratuitous_work,
arp_gratuitous_work_handler);
k_work_reschedule(&arp_gratuitous_work,
K_SECONDS(CONFIG_NET_ARP_GRATUITOUS_INTERVAL));
#endif /* defined(CONFIG_NET_ARP_GRATUITOUS_TRANSMISSION) */
}
1 change: 1 addition & 0 deletions tests/net/all/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ CONFIG_NET_ARP=y
CONFIG_NET_ARP_TABLE_SIZE=3
CONFIG_NET_ARP_LOG_LEVEL_DBG=y
CONFIG_NET_ARP_GRATUITOUS=y
CONFIG_NET_ARP_GRATUITOUS_TRANSMISSION=y

# Logging
CONFIG_NET_LOG=y
Expand Down