Make batadv_bla_check_duplist() just use the new function skb_crc32c(),
instead of calling skb_seq_read() with crc32c(). This is faster and
simpler.

Suggested-by: Eric Biggers <[email protected]>
Signed-off-by: Sven Eckelmann <[email protected]>
---
 compat-include/linux/skbuff.h          | 45 ++++++++++++++++++++++++++++++
 net/batman-adv/Kconfig                 |  1 +
 net/batman-adv/bridge_loop_avoidance.c | 51 ++++++----------------------------
 net/batman-adv/types.h                 |  2 +-
 4 files changed, 55 insertions(+), 44 deletions(-)

diff --git a/compat-include/linux/skbuff.h b/compat-include/linux/skbuff.h
new file mode 100644
index 
0000000000000000000000000000000000000000..96a2e86f3f0cd694344a9614661de8de730a797c
--- /dev/null
+++ b/compat-include/linux/skbuff.h
@@ -0,0 +1,45 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) B.A.T.M.A.N. contributors:
+ *
+ * Marek Lindner, Simon Wunderlich
+ *
+ * This file contains macros for maintaining compatibility with older versions
+ * of the Linux kernel.
+ */
+
+#ifndef _NET_BATMAN_ADV_COMPAT_LINUX_SKBUFF_H_
+#define _NET_BATMAN_ADV_COMPAT_LINUX_SKBUFF_H_
+
+#include <linux/version.h>
+#include_next <linux/skbuff.h>
+
+#if LINUX_VERSION_IS_LESS(6, 16, 0) || !defined(CONFIG_NET_CRC32C)
+
+#include <linux/crc32.h>
+
+static inline u32 batadv_skb_crc32c(struct sk_buff *skb, int offset,
+                                   int len, u32 crc)
+{
+       unsigned int to = offset + len;
+       unsigned int consumed = 0;
+       struct skb_seq_state st;
+       unsigned int l;
+       const u8 *data;
+
+       if (len <= 0)
+              return crc;
+
+       skb_prepare_seq_read(skb, offset, to, &st);
+       while ((l = skb_seq_read(consumed, &data, &st)) != 0) {
+               crc = crc32c(crc, data, l);
+               consumed += l;
+       }
+
+       return crc;
+}
+
+#define skb_crc32c batadv_skb_crc32c
+
+#endif /* LINUX_VERSION_IS_LESS(6, 16, 0) || !defined(CONFIG_NET_CRC32C) */
+
+#endif /* _NET_BATMAN_ADV_COMPAT_LINUX_TIMER_H_ */
diff --git a/net/batman-adv/Kconfig b/net/batman-adv/Kconfig
index 
c299e2bc87eda118f35c4d9b6a718927951e09b2..58c408b7a7d9c3f78394bc038dbdaad287126013
 100644
--- a/net/batman-adv/Kconfig
+++ b/net/batman-adv/Kconfig
@@ -35,6 +35,7 @@ config BATMAN_ADV_BLA
        bool "Bridge Loop Avoidance"
        depends on BATMAN_ADV && INET
        select CRC16
+       select NET_CRC32C
        default y
        help
          This option enables BLA (Bridge Loop Avoidance), a mechanism
diff --git a/net/batman-adv/bridge_loop_avoidance.c 
b/net/batman-adv/bridge_loop_avoidance.c
index 
b992ba12aa2478a338155d2fe46a75e970306bd5..3dc791c15bf72edd4884c1c41f90a729846df41b
 100644
--- a/net/batman-adv/bridge_loop_avoidance.c
+++ b/net/batman-adv/bridge_loop_avoidance.c
@@ -12,7 +12,6 @@
 #include <linux/compiler.h>
 #include <linux/container_of.h>
 #include <linux/crc16.h>
-#include <linux/crc32.h>
 #include <linux/err.h>
 #include <linux/errno.h>
 #include <linux/etherdevice.h>
@@ -1585,45 +1584,11 @@ int batadv_bla_init(struct batadv_priv *bat_priv)
        return 0;
 }
 
-/**
- * batadv_skb_crc32() - calculate CRC32 of the whole packet and skip bytes in
- *  the header
- * @skb: skb pointing to fragmented socket buffers
- * @payload_ptr: Pointer to position inside the head buffer of the skb
- *  marking the start of the data to be CRC'ed
- *
- * payload_ptr must always point to an address in the skb head buffer and not 
to
- * a fragment.
- *
- * Return: big endian crc32c of the checksummed data
- */
-static __be32 batadv_skb_crc32(struct sk_buff *skb, u8 *payload_ptr)
-{
-       unsigned int to = skb->len;
-       unsigned int consumed = 0;
-       struct skb_seq_state st;
-       unsigned int from;
-       unsigned int len;
-       const u8 *data;
-       u32 crc = 0;
-
-       from = (unsigned int)(payload_ptr - skb->data);
-
-       skb_prepare_seq_read(skb, from, to, &st);
-       while ((len = skb_seq_read(consumed, &data, &st)) != 0) {
-               crc = crc32c(crc, data, len);
-               consumed += len;
-       }
-
-       return htonl(crc);
-}
-
 /**
  * batadv_bla_check_duplist() - Check if a frame is in the broadcast dup.
  * @bat_priv: the bat priv with all the mesh interface information
  * @skb: contains the multicast packet to be checked
- * @payload_ptr: pointer to position inside the head buffer of the skb
- *  marking the start of the data to be CRC'ed
+ * @payload_offset: offset in the skb, marking the start of the data to be 
CRC'ed
  * @orig: originator mac address, NULL if unknown
  *
  * Check if it is on our broadcast list. Another gateway might have sent the
@@ -1638,16 +1603,18 @@ static __be32 batadv_skb_crc32(struct sk_buff *skb, u8 
*payload_ptr)
  * Return: true if a packet is in the duplicate list, false otherwise.
  */
 static bool batadv_bla_check_duplist(struct batadv_priv *bat_priv,
-                                    struct sk_buff *skb, u8 *payload_ptr,
+                                    struct sk_buff *skb, int payload_offset,
                                     const u8 *orig)
 {
        struct batadv_bcast_duplist_entry *entry;
        bool ret = false;
+       int payload_len;
        int i, curr;
-       __be32 crc;
+       u32 crc;
 
        /* calculate the crc ... */
-       crc = batadv_skb_crc32(skb, payload_ptr);
+       payload_len = skb->len - payload_offset;
+       crc = skb_crc32c(skb, payload_offset, payload_len, 0);
 
        spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
 
@@ -1727,7 +1694,7 @@ static bool batadv_bla_check_duplist(struct batadv_priv 
*bat_priv,
 static bool batadv_bla_check_ucast_duplist(struct batadv_priv *bat_priv,
                                           struct sk_buff *skb)
 {
-       return batadv_bla_check_duplist(bat_priv, skb, (u8 *)skb->data, NULL);
+       return batadv_bla_check_duplist(bat_priv, skb, 0, NULL);
 }
 
 /**
@@ -1745,12 +1712,10 @@ bool batadv_bla_check_bcast_duplist(struct batadv_priv 
*bat_priv,
                                    struct sk_buff *skb)
 {
        struct batadv_bcast_packet *bcast_packet;
-       u8 *payload_ptr;
 
        bcast_packet = (struct batadv_bcast_packet *)skb->data;
-       payload_ptr = (u8 *)(bcast_packet + 1);
 
-       return batadv_bla_check_duplist(bat_priv, skb, payload_ptr,
+       return batadv_bla_check_duplist(bat_priv, skb, sizeof(*bcast_packet),
                                        bcast_packet->orig);
 }
 
diff --git a/net/batman-adv/types.h b/net/batman-adv/types.h
index 
ae1d7a8dc480fd6b49bb8d5ec02359b8aa21d5c2..8fc5fe0e9b05398c9e1af6417453f4a09bc74e14
 100644
--- a/net/batman-adv/types.h
+++ b/net/batman-adv/types.h
@@ -734,7 +734,7 @@ struct batadv_bcast_duplist_entry {
        u8 orig[ETH_ALEN];
 
        /** @crc: crc32 checksum of broadcast payload */
-       __be32 crc;
+       u32 crc;
 
        /** @entrytime: time when the broadcast packet was received */
        unsigned long entrytime;

---
base-commit: def64eeace3150b88d5823fcb733dadd79d7562a
change-id: 20250928-skb_crc32c-042889bf2de5

Best regards,
-- 
Sven Eckelmann <[email protected]>

Reply via email to