Skip to content

Commit ced7293

Browse files
Antonio Quartulliordex
authored andcommitted
batman-adv: use CRC32C instead of CRC16 in TT code
CRC32C has to be preferred to CRC16 because of its possible HW native support and because of the reduced collision probability. With this change the Translation Table component now uses CRC32C to compute the local and global table checksum. Signed-off-by: Antonio Quartulli <[email protected]> Signed-off-by: Marek Lindner <[email protected]>
1 parent 122edaa commit ced7293

File tree

3 files changed

+45
-38
lines changed

3 files changed

+45
-38
lines changed

net/batman-adv/packet.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,12 +345,14 @@ struct batadv_tvlv_gateway_data {
345345
* struct batadv_tvlv_tt_data - tt data propagated through the tt tvlv container
346346
* @flags: translation table flags (see batadv_tt_data_flags)
347347
* @ttvn: translation table version number
348-
* @crc: crc16 checksum of the local translation table
348+
* @reserved: field reserved for future use
349+
* @crc: crc32 checksum of the local translation table
349350
*/
350351
struct batadv_tvlv_tt_data {
351352
uint8_t flags;
352353
uint8_t ttvn;
353-
__be16 crc;
354+
uint16_t reserved;
355+
__be32 crc;
354356
};
355357

356358
/**

net/batman-adv/translation-table.c

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include "routing.h"
2828
#include "bridge_loop_avoidance.h"
2929

30-
#include <linux/crc16.h>
30+
#include <linux/crc32c.h>
3131

3232
/* hash class keys */
3333
static struct lock_class_key batadv_tt_local_hash_lock_class_key;
@@ -409,7 +409,7 @@ static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
409409

410410
tt_data->flags = BATADV_TT_OGM_DIFF;
411411
tt_data->ttvn = atomic_read(&bat_priv->tt.vn);
412-
tt_data->crc = htons(bat_priv->tt.local_crc);
412+
tt_data->crc = htonl(bat_priv->tt.local_crc);
413413

414414
if (tt_diff_len == 0)
415415
goto container_register;
@@ -481,7 +481,7 @@ int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
481481
goto out;
482482

483483
seq_printf(seq,
484-
"Locally retrieved addresses (from %s) announced via TT (TTVN: %u CRC: %#.4x):\n",
484+
"Locally retrieved addresses (from %s) announced via TT (TTVN: %u CRC: %#.8x):\n",
485485
net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn),
486486
bat_priv->tt.local_crc);
487487
seq_printf(seq, " %-13s %-7s %-10s\n", "Client", "Flags",
@@ -993,7 +993,7 @@ batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
993993
if (best_entry) {
994994
last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
995995
seq_printf(seq,
996-
" %c %pM (%3u) via %pM (%3u) (%#.4x) [%c%c%c]\n",
996+
" %c %pM (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
997997
'*', tt_global_entry->common.addr,
998998
best_entry->ttvn, best_entry->orig_node->orig,
999999
last_ttvn, best_entry->orig_node->tt_crc,
@@ -1037,7 +1037,7 @@ int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
10371037
seq_printf(seq,
10381038
"Globally announced TT entries received via the mesh %s\n",
10391039
net_dev->name);
1040-
seq_printf(seq, " %-13s %s %-15s %s (%-6s) %s\n",
1040+
seq_printf(seq, " %-13s %s %-15s %s (%-10s) %s\n",
10411041
"Client", "(TTVN)", "Originator", "(Curr TTVN)", "CRC",
10421042
"Flags");
10431043

@@ -1394,17 +1394,19 @@ struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
13941394
return orig_node;
13951395
}
13961396

1397-
/* Calculates the checksum of the local table of a given orig_node */
1398-
static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1397+
/**
1398+
* batadv_tt_global_crc - calculates the checksum of the local table belonging
1399+
* to the given orig_node
1400+
* @bat_priv: the bat priv with all the soft interface information
1401+
*/
1402+
static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
13991403
struct batadv_orig_node *orig_node)
14001404
{
1401-
uint16_t total = 0, total_one;
14021405
struct batadv_hashtable *hash = bat_priv->tt.global_hash;
14031406
struct batadv_tt_common_entry *tt_common;
14041407
struct batadv_tt_global_entry *tt_global;
14051408
struct hlist_head *head;
1406-
uint32_t i;
1407-
int j;
1409+
uint32_t i, crc = 0;
14081410

14091411
for (i = 0; i < hash->size; i++) {
14101412
head = &hash->table[i];
@@ -1435,27 +1437,24 @@ static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
14351437
orig_node))
14361438
continue;
14371439

1438-
total_one = 0;
1439-
for (j = 0; j < ETH_ALEN; j++)
1440-
total_one = crc16_byte(total_one,
1441-
tt_common->addr[j]);
1442-
total ^= total_one;
1440+
crc ^= crc32c(0, tt_common->addr, ETH_ALEN);
14431441
}
14441442
rcu_read_unlock();
14451443
}
14461444

1447-
return total;
1445+
return crc;
14481446
}
14491447

1450-
/* Calculates the checksum of the local table */
1451-
static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
1448+
/**
1449+
* batadv_tt_local_crc - calculates the checksum of the local table
1450+
* @bat_priv: the bat priv with all the soft interface information
1451+
*/
1452+
static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
14521453
{
1453-
uint16_t total = 0, total_one;
14541454
struct batadv_hashtable *hash = bat_priv->tt.local_hash;
14551455
struct batadv_tt_common_entry *tt_common;
14561456
struct hlist_head *head;
1457-
uint32_t i;
1458-
int j;
1457+
uint32_t i, crc = 0;
14591458

14601459
for (i = 0; i < hash->size; i++) {
14611460
head = &hash->table[i];
@@ -1467,16 +1466,13 @@ static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
14671466
*/
14681467
if (tt_common->flags & BATADV_TT_CLIENT_NEW)
14691468
continue;
1470-
total_one = 0;
1471-
for (j = 0; j < ETH_ALEN; j++)
1472-
total_one = crc16_byte(total_one,
1473-
tt_common->addr[j]);
1474-
total ^= total_one;
1469+
1470+
crc ^= crc32c(0, tt_common->addr, ETH_ALEN);
14751471
}
14761472
rcu_read_unlock();
14771473
}
14781474

1479-
return total;
1475+
return crc;
14801476
}
14811477

14821478
static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
@@ -1662,9 +1658,18 @@ batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
16621658
return tvlv_tt_data;
16631659
}
16641660

1661+
/**
1662+
* batadv_send_tt_request - send a TT Request message to a given node
1663+
* @bat_priv: the bat priv with all the soft interface information
1664+
* @dst_orig_node: the destination of the message
1665+
* @ttvn: the version number that the source of the message is looking for
1666+
* @tt_crc: the CRC associated with the version number
1667+
* @full_table: ask for the entire translation table if true, while only for the
1668+
* last TT diff otherwise
1669+
*/
16651670
static int batadv_send_tt_request(struct batadv_priv *bat_priv,
16661671
struct batadv_orig_node *dst_orig_node,
1667-
uint8_t ttvn, uint16_t tt_crc,
1672+
uint8_t ttvn, uint32_t tt_crc,
16681673
bool full_table)
16691674
{
16701675
struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
@@ -1689,7 +1694,7 @@ static int batadv_send_tt_request(struct batadv_priv *bat_priv,
16891694

16901695
tvlv_tt_data->flags = BATADV_TT_REQUEST;
16911696
tvlv_tt_data->ttvn = ttvn;
1692-
tvlv_tt_data->crc = htons(tt_crc);
1697+
tvlv_tt_data->crc = htonl(tt_crc);
16931698

16941699
if (full_table)
16951700
tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
@@ -1756,7 +1761,7 @@ static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
17561761

17571762
/* this node doesn't have the requested data */
17581763
if (orig_ttvn != req_ttvn ||
1759-
tt_data->crc != htons(req_dst_orig_node->tt_crc))
1764+
tt_data->crc != htonl(req_dst_orig_node->tt_crc))
17601765
goto out;
17611766

17621767
/* If the full table has been explicitly requested */
@@ -2404,13 +2409,13 @@ bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
24042409
* @tt_buff: buffer holding the tt information
24052410
* @tt_num_changes: number of tt changes inside the tt buffer
24062411
* @ttvn: translation table version number of this changeset
2407-
* @tt_crc: crc16 checksum of orig node's translation table
2412+
* @tt_crc: crc32 checksum of orig node's translation table
24082413
*/
24092414
static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
24102415
struct batadv_orig_node *orig_node,
24112416
const unsigned char *tt_buff,
24122417
uint16_t tt_num_changes, uint8_t ttvn,
2413-
uint16_t tt_crc)
2418+
uint32_t tt_crc)
24142419
{
24152420
uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
24162421
bool full_table = true;
@@ -2464,7 +2469,7 @@ static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
24642469
orig_node->tt_crc != tt_crc) {
24652470
request_table:
24662471
batadv_dbg(BATADV_DBG_TT, bat_priv,
2467-
"TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %#.4x last_crc: %#.4x num_changes: %u)\n",
2472+
"TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %#.8x last_crc: %#.8x num_changes: %u)\n",
24682473
orig_node->orig, ttvn, orig_ttvn, tt_crc,
24692474
orig_node->tt_crc, tt_num_changes);
24702475
batadv_send_tt_request(bat_priv, orig_node, ttvn,
@@ -2572,7 +2577,7 @@ static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
25722577

25732578
batadv_tt_update_orig(bat_priv, orig,
25742579
(unsigned char *)(tt_data + 1),
2575-
num_entries, tt_data->ttvn, ntohs(tt_data->crc));
2580+
num_entries, tt_data->ttvn, ntohl(tt_data->crc));
25762581
}
25772582

25782583
/**

net/batman-adv/types.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ struct batadv_orig_node {
150150
uint8_t flags;
151151
uint8_t capabilities;
152152
atomic_t last_ttvn;
153-
uint16_t tt_crc;
153+
uint32_t tt_crc;
154154
unsigned char *tt_buff;
155155
int16_t tt_buff_len;
156156
spinlock_t tt_buff_lock; /* protects tt_buff & tt_buff_len */
@@ -377,7 +377,7 @@ struct batadv_priv_tt {
377377
spinlock_t req_list_lock; /* protects req_list */
378378
spinlock_t roam_list_lock; /* protects roam_list */
379379
atomic_t local_entry_num;
380-
uint16_t local_crc;
380+
uint32_t local_crc;
381381
unsigned char *last_changeset;
382382
int16_t last_changeset_len;
383383
/* protects last_changeset & last_changeset_len */

0 commit comments

Comments
 (0)