From c724cf99f4d40a056dc1aaef594c55754b43e99a Mon Sep 17 00:00:00 2001 From: Robert Lubos Date: Wed, 8 Nov 2023 10:18:01 +0100 Subject: [PATCH] net: pkt: Add explicit flag to indicate packet is IP reassembled The current logic to determine whether a packet is IP reassembled is flawed, as it only worked in certain conditions (which was ok, as the conditions were satisfied for the current use case, but now it's a public function). Therefore, add an explicit flag that indicates whether a packet is IP reassembled or not. Signed-off-by: Robert Lubos --- include/zephyr/net/net_pkt.h | 33 ++++++++++++++++++++++++--------- subsys/net/ip/ipv4_fragment.c | 1 + subsys/net/ip/ipv6_fragment.c | 1 + subsys/net/ip/net_pkt.c | 1 + 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/include/zephyr/net/net_pkt.h b/include/zephyr/net/net_pkt.h index 591002a9d6..fb00d7b980 100644 --- a/include/zephyr/net/net_pkt.h +++ b/include/zephyr/net/net_pkt.h @@ -197,7 +197,9 @@ struct net_pkt { uint8_t chksum_done : 1; /* Checksum has already been computed for * the packet. */ - +#if defined(CONFIG_NET_IPV4_FRAGMENT) || defined(CONFIG_NET_IPV6_FRAGMENT) + uint8_t ip_reassembled : 1; /* Packet is a reassembled IP packet. */ +#endif /* bitfield byte alignment boundary */ #if defined(CONFIG_NET_IP) @@ -836,20 +838,33 @@ static inline void net_pkt_set_ipv6_fragment_id(struct net_pkt *pkt, } #endif /* CONFIG_NET_IPV6_FRAGMENT */ +#if defined(CONFIG_NET_IPV4_FRAGMENT) || defined(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 !!(pkt->ip_reassembled); +} + +static inline void net_pkt_set_ip_reassembled(struct net_pkt *pkt, + bool reassembled) +{ + pkt->ip_reassembled = reassembled; +} +#else /* CONFIG_NET_IPV4_FRAGMENT || CONFIG_NET_IPV6_FRAGMENT */ +static inline bool net_pkt_is_ip_reassembled(struct net_pkt *pkt) +{ + ARG_UNUSED(pkt); return false; } +static inline void net_pkt_set_ip_reassembled(struct net_pkt *pkt, + bool reassembled) +{ + ARG_UNUSED(pkt); + ARG_UNUSED(reassembled); +} +#endif /* CONFIG_NET_IPV4_FRAGMENT || CONFIG_NET_IPV6_FRAGMENT */ + static inline uint8_t net_pkt_priority(struct net_pkt *pkt) { return pkt->priority; diff --git a/subsys/net/ip/ipv4_fragment.c b/subsys/net/ip/ipv4_fragment.c index 5bcf4e0026..ee6e43c83d 100644 --- a/subsys/net/ip/ipv4_fragment.c +++ b/subsys/net/ip/ipv4_fragment.c @@ -203,6 +203,7 @@ static void reassemble_packet(struct net_ipv4_reassembly *reass) ipv4_hdr->chksum = net_calc_chksum_ipv4(pkt); net_pkt_set_data(pkt, &ipv4_access); + net_pkt_set_ip_reassembled(pkt, true); LOG_DBG("New pkt %p IPv4 len is %d bytes", pkt, net_pkt_get_len(pkt)); diff --git a/subsys/net/ip/ipv6_fragment.c b/subsys/net/ip/ipv6_fragment.c index 6d391b3b82..fda00c59f0 100644 --- a/subsys/net/ip/ipv6_fragment.c +++ b/subsys/net/ip/ipv6_fragment.c @@ -338,6 +338,7 @@ static void reassemble_packet(struct net_ipv6_reassembly *reass) ipv6.hdr->len = htons(len); net_pkt_set_data(pkt, &ipv6_access); + net_pkt_set_ip_reassembled(pkt, true); NET_DBG("New pkt %p IPv6 len is %d bytes", pkt, len + NET_IPV6H_LEN); diff --git a/subsys/net/ip/net_pkt.c b/subsys/net/ip/net_pkt.c index cef7cbcd10..ea60060abe 100644 --- a/subsys/net/ip/net_pkt.c +++ b/subsys/net/ip/net_pkt.c @@ -1813,6 +1813,7 @@ static void clone_pkt_attributes(struct net_pkt *pkt, struct net_pkt *clone_pkt) net_pkt_set_ptp(clone_pkt, net_pkt_is_ptp(pkt)); net_pkt_set_forwarding(clone_pkt, net_pkt_forwarding(pkt)); net_pkt_set_chksum_done(clone_pkt, net_pkt_is_chksum_done(pkt)); + net_pkt_set_ip_reassembled(pkt, net_pkt_is_ip_reassembled(pkt)); net_pkt_set_l2_bridged(clone_pkt, net_pkt_is_l2_bridged(pkt)); net_pkt_set_l2_processed(clone_pkt, net_pkt_is_l2_processed(pkt));