From 1e42dda3135af5ca1c09cee7664a2f6d29960d1c Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Wed, 10 Apr 2024 11:26:15 +0300 Subject: [PATCH 1/3] [nrf fromtree] net: vlan: Fix net_eth_get_vlan_tag() to check correct interface The network interface parameter for net_eth_get_vlan_tag() should be the VLAN interface so use the search loop properly. Earlier the main interface could be checked. Add also test cases for this so that we can catch that the func works properly. Signed-off-by: Jukka Rissanen (cherry picked from commit d40abe8c0f880c53bdfb759895c2a5c910892646) Signed-off-by: Robert Lubos --- subsys/net/l2/ethernet/vlan.c | 17 ++++++++--------- tests/net/vlan/src/main.c | 25 +++++++++++++++++++++---- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/subsys/net/l2/ethernet/vlan.c b/subsys/net/l2/ethernet/vlan.c index 844a6bbd2ef..9a6d660aeb6 100644 --- a/subsys/net/l2/ethernet/vlan.c +++ b/subsys/net/l2/ethernet/vlan.c @@ -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; } } diff --git a/tests/net/vlan/src/main.c b/tests/net/vlan/src/main.c index e739af7dd12..a17aef644c7 100644 --- a/tests/net/vlan/src/main.c +++ b/tests/net/vlan/src/main.c @@ -574,6 +574,23 @@ 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; + + 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) @@ -628,13 +645,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]); From 3e9920f3ce5cca6b27d0214fb670728bd18e08fa Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Thu, 25 Apr 2024 09:47:23 +0300 Subject: [PATCH 2/3] [nrf fromtree] net: vlan: Add a function to check if interface is VLAN one We were missing a helper function that can be used to check whether the given function is the virtual VLAN interface. Signed-off-by: Jukka Rissanen (cherry picked from commit 07599e3a539f4e6563144cada8cfbb50eae3d93c) Signed-off-by: Robert Lubos --- include/zephyr/net/ethernet.h | 18 ++++++++++++++++++ subsys/net/l2/ethernet/vlan.c | 16 ++++++++++++++++ tests/net/vlan/src/main.c | 5 +++++ 3 files changed, 39 insertions(+) diff --git a/include/zephyr/net/ethernet.h b/include/zephyr/net/ethernet.h index e39fa7d170e..9ecc7340d03 100644 --- a/include/zephyr/net/ethernet.h +++ b/include/zephyr/net/ethernet.h @@ -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, \ diff --git a/subsys/net/l2/ethernet/vlan.c b/subsys/net/l2/ethernet/vlan.c index 9a6d660aeb6..f8e21efbbb0 100644 --- a/subsys/net/l2/ethernet/vlan.c +++ b/subsys/net/l2/ethernet/vlan.c @@ -336,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; diff --git a/tests/net/vlan/src/main.c b/tests/net/vlan/src/main.c index a17aef644c7..a7851cc2fc6 100644 --- a/tests/net/vlan/src/main.c +++ b/tests/net/vlan/src/main.c @@ -581,6 +581,11 @@ static void test_vlan_enable(void) 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); From 4193164e47bb47ac94112fb651160da7e4c15992 Mon Sep 17 00:00:00 2001 From: Robert Lubos Date: Mon, 15 May 2023 11:43:27 +0200 Subject: [PATCH 3/3] [nrf fromtree] net: arp: Add support for gratuitous ARP transmission Add support for gratuitous ARP transmission by Zephyr network stack. This allows to prematurely fill the peer ARP table, so there's no need to send an explicit request when peer needs to send an actual packet. The gratuitous ARP is send when the network interface is brought up (joins the network) or a new IP address is added. The gratuitous ARP request is also sent periodically, with a configurable interval time. The gratuitous ARP should also be sent whenever MAC address of the interface is changed, but as Zephyr only allows to do this when interface is down, this is already covered by the first case (interface brought up). Signed-off-by: Robert Lubos (cherry picked from commit 6551e6f5baea82cbf7f98cb5bbf5ca5c3c7a901f) --- subsys/net/l2/ethernet/Kconfig | 13 +++ subsys/net/l2/ethernet/arp.c | 157 +++++++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+) diff --git a/subsys/net/l2/ethernet/Kconfig b/subsys/net/l2/ethernet/Kconfig index 17588b17fd1..e6fd07e47e2 100644 --- a/subsys/net/l2/ethernet/Kconfig +++ b/subsys/net/l2/ethernet/Kconfig @@ -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 diff --git a/subsys/net/l2/ethernet/arp.c b/subsys/net/l2/ethernet/arp.c index 7c0a2ba74f2..85eafd0eedb 100644 --- a/subsys/net/l2/ethernet/arp.c +++ b/subsys/net/l2/ethernet/arp.c @@ -16,6 +16,7 @@ LOG_MODULE_REGISTER(net_arp, CONFIG_NET_ARP_LOG_LEVEL); #include #include #include +#include #include "arp.h" #include "net_private.h" @@ -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); @@ -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, @@ -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) */ }