From 76a256ea576a41544c9b64665ad2a5a80ddbf2db Mon Sep 17 00:00:00 2001 From: Robert Lubos Date: Mon, 30 Oct 2023 13:17:51 +0100 Subject: [PATCH] net: pkt: Add function to check if packet was reassembled at IP level Move the existing code verifying that the net_pkt was reassembled at IP level to a helper function, as it will be needed in other places as well. Additionally, add packet family check before accessing union fields. Signed-off-by: Robert Lubos --- include/zephyr/net/net_pkt.h | 14 ++++++++++++++ subsys/net/ip/net_core.c | 3 +-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/include/zephyr/net/net_pkt.h b/include/zephyr/net/net_pkt.h index 98646b25b26..591002a9d66 100644 --- a/include/zephyr/net/net_pkt.h +++ b/include/zephyr/net/net_pkt.h @@ -836,6 +836,20 @@ static inline void net_pkt_set_ipv6_fragment_id(struct net_pkt *pkt, } #endif /* CONFIG_NET_IPV6_FRAGMENT */ +static inline bool net_pkt_is_ip_reassembled(struct net_pkt *pkt) +{ + if ((IS_ENABLED(CONFIG_NET_IPV4_FRAGMENT) && + net_pkt_family(pkt) == AF_INET && + net_pkt_ipv4_fragment_more(pkt)) || + (IS_ENABLED(CONFIG_NET_IPV6_FRAGMENT) && + net_pkt_family(pkt) == AF_INET6 && + net_pkt_ipv6_fragment_start(pkt))) { + return true; + } + + return false; +} + static inline uint8_t net_pkt_priority(struct net_pkt *pkt) { return pkt->priority; diff --git a/subsys/net/ip/net_core.c b/subsys/net/ip/net_core.c index 3d37743a39d..baa8cd695c5 100644 --- a/subsys/net/ip/net_core.c +++ b/subsys/net/ip/net_core.c @@ -76,8 +76,7 @@ static inline enum net_verdict process_data(struct net_pkt *pkt, /* If the packet is routed back to us when we have reassembled an IPv4 or IPv6 packet, * then do not pass it to L2 as the packet does not have link layer headers in it. */ - if ((IS_ENABLED(CONFIG_NET_IPV4_FRAGMENT) && net_pkt_ipv4_fragment_more(pkt)) || - (IS_ENABLED(CONFIG_NET_IPV6_FRAGMENT) && net_pkt_ipv6_fragment_start(pkt))) { + if (net_pkt_is_ip_reassembled(pkt)) { locally_routed = true; }