Skip to content

[nrf fromtree] net: arp: Add support for gratuitous ARP transmission #1665

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

Merged
merged 3 commits into from
May 13, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions include/zephyr/net/ethernet.h
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,24 @@ static inline bool net_eth_get_vlan_status(struct net_if *iface)
}
#endif

/**
* @brief Check if the given interface is a VLAN interface.
*
* @param iface Network interface
*
* @return True if this network interface is VLAN one, false if not.
*/
#if defined(CONFIG_NET_VLAN)
bool net_eth_is_vlan_interface(struct net_if *iface);
#else
static inline bool net_eth_is_vlan_interface(struct net_if *iface)
{
ARG_UNUSED(iface);

return false;
}
#endif

#if !defined(CONFIG_ETH_DRIVER_RAW_MODE)

#define Z_ETH_NET_DEVICE_INIT_INSTANCE(node_id, dev_id, name, instance, \
Expand Down
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) */
}
33 changes: 24 additions & 9 deletions subsys/net/l2/ethernet/vlan.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,18 +317,17 @@ bool net_eth_is_vlan_enabled(struct ethernet_context *ctx,
uint16_t net_eth_get_vlan_tag(struct net_if *iface)
{
uint16_t tag = NET_VLAN_TAG_UNSPEC;
struct vlan_context *ctx;

k_mutex_lock(&lock, K_FOREVER);

ctx = get_vlan_ctx(iface, tag, true);
if (ctx != NULL) {
/* The Ethernet interface does not have a tag so if user
* tried to use the main interface, then do not return
* the tag.
*/
if (ctx->attached_to != iface) {
tag = ctx->tag;
ARRAY_FOR_EACH(vlan_ctx, i) {
if (vlan_ctx[i] == NULL || !vlan_ctx[i]->is_used) {
continue;
}

if (vlan_ctx[i]->iface == iface) {
tag = vlan_ctx[i]->tag;
break;
}
}

Expand All @@ -337,6 +336,22 @@ uint16_t net_eth_get_vlan_tag(struct net_if *iface)
return tag;
}

bool net_eth_is_vlan_interface(struct net_if *iface)
{
enum virtual_interface_caps caps;

if (net_if_l2(iface) != &NET_L2_GET_NAME(VIRTUAL)) {
return false;
}

caps = net_virtual_get_iface_capabilities(iface);
if (!(caps & VIRTUAL_INTERFACE_VLAN)) {
return false;
}

return true;
}

bool net_eth_get_vlan_status(struct net_if *iface)
{
bool status = false;
Expand Down
30 changes: 26 additions & 4 deletions tests/net/vlan/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,28 @@ static void test_vlan_enable(void)
ret = net_eth_vlan_enable(iface, VLAN_TAG_1);
zassert_equal(ret, -EALREADY, "VLAN tag %d enabled for iface 1 (%d)",
VLAN_TAG_1, ret);

for (int i = VLAN_TAG_1; i <= VLAN_TAG_5; i += 100) {
iface = net_eth_get_vlan_iface(NULL, i);

ARRAY_FOR_EACH_PTR(vlan_interfaces, vlan_iface) {
uint16_t tag;

ret = net_eth_is_vlan_interface(*vlan_iface);
zassert_equal(ret, true,
"Not identified as VLAN interface %d",
net_if_get_by_iface(*vlan_iface));

if (*vlan_iface == iface) {
tag = net_eth_get_vlan_tag(*vlan_iface);

zassert_equal(tag, i,
"Could not get the VLAN interface (%d)",
net_if_get_by_iface(*vlan_iface));
break;
}
}
}
}

static void test_vlan_disable(void)
Expand Down Expand Up @@ -628,13 +650,13 @@ static void test_vlan_enable_all(void)
int ret;

ret = net_eth_vlan_enable(eth_interfaces[0], VLAN_TAG_1);
zassert_equal(ret, 0, "Cannot enable %d", VLAN_TAG_1);
zassert_true(ret == 0 || ret == -EALREADY, "Cannot enable %d", VLAN_TAG_1);
ret = net_eth_vlan_enable(eth_interfaces[0], VLAN_TAG_2);
zassert_equal(ret, 0, "Cannot enable %d", VLAN_TAG_2);
zassert_true(ret == 0 || ret == -EALREADY, "Cannot enable %d", VLAN_TAG_2);
ret = net_eth_vlan_enable(eth_interfaces[0], VLAN_TAG_3);
zassert_equal(ret, 0, "Cannot enable %d", VLAN_TAG_3);
zassert_true(ret == 0 || ret == -EALREADY, "Cannot enable %d", VLAN_TAG_3);
ret = net_eth_vlan_enable(eth_interfaces[0], VLAN_TAG_4);
zassert_equal(ret, 0, "Cannot enable %d", VLAN_TAG_4);
zassert_true(ret == 0 || ret == -EALREADY, "Cannot enable %d", VLAN_TAG_4);

eth_ctx = net_if_l2_data(eth_interfaces[0]);

Expand Down
Loading