]> git.ipfire.org Git - thirdparty/knot-dns.git/commitdiff
XDP-TCP: refactor Siphash secret handling
authorDaniel Salzman <daniel.salzman@nic.cz>
Tue, 6 Jul 2021 20:31:25 +0000 (22:31 +0200)
committerDaniel Salzman <daniel.salzman@nic.cz>
Sat, 10 Jul 2021 20:46:37 +0000 (22:46 +0200)
src/libdnssec/random.h
src/libknot/xdp/tcp.c
src/libknot/xdp/tcp.h

index f6abb94e9d750c1ef6c51960d131b6702f434266..3a575333b3b401e2e22989988f552cf32acc320e 100644 (file)
@@ -1,4 +1,4 @@
-/*  Copyright (C) 2020 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
+/*  Copyright (C) 2021 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
 
     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -73,6 +73,7 @@ static inline uint32_t dnssec_random_uint32_t(void);
 /*! \cond */
 dnssec_register_random_type(uint16_t);
 dnssec_register_random_type(uint32_t);
+dnssec_register_random_type(uint64_t);
 /*! \endcond */
 
 /*! @} */
index 93c12a9946c541470b2d7e683a3e0fa3e147cda6..49c7081a32946fdaa7f12da0e93b5a40ff6d47b4 100644 (file)
@@ -49,14 +49,11 @@ static size_t sockaddr_data_len(const struct sockaddr_in6 *rem, const struct soc
 }
 
 static uint64_t hash_four_tuple(const struct sockaddr_in6 *rem, const struct sockaddr_in6 *loc,
-                                uint32_t hash_secret[4])
+                                knot_tcp_table_t *table)
 {
        size_t socka_data_len = sockaddr_data_len(rem, loc);
-       SIPHASH_KEY key;
-       //assert(sizeof(key) == sizeof(hash_secret)); // beware, sizeof(hash_secret) == sizeof(uint32_t*)
-       memcpy(&key, hash_secret, sizeof(key));
        SIPHASH_CTX ctx;
-       SipHash24_Init(&ctx, &key);
+       SipHash24_Init(&ctx, (const SIPHASH_KEY *)(table->hash_secret));
        SipHash24_Update(&ctx, rem, socka_data_len);
        SipHash24_Update(&ctx, loc, socka_data_len);
        return SipHash24_End(&ctx);
@@ -84,9 +81,9 @@ knot_tcp_table_t *knot_tcp_table_new(size_t size)
        table->size = size;
        init_list(tcp_table_timeout(table));
 
-       for (size_t i = 0; i < sizeof(table->hash_secret) / sizeof(*table->hash_secret); i++) {
-               table->hash_secret[i] = dnssec_random_uint32_t();
-       }
+       assert(sizeof(table->hash_secret) == sizeof(SIPHASH_KEY));
+       table->hash_secret[0] = dnssec_random_uint64_t();
+       table->hash_secret[1] = dnssec_random_uint64_t();
 
        return table;
 }
@@ -107,7 +104,7 @@ static knot_tcp_conn_t **tcp_table_lookup(const struct sockaddr_in6 *rem,
                                           const struct sockaddr_in6 *loc,
                                           uint64_t *hash, knot_tcp_table_t *table)
 {
-       *hash = hash_four_tuple(rem, loc, table->hash_secret);
+       *hash = hash_four_tuple(rem, loc, table);
        size_t sdl = sockaddr_data_len(rem, loc);
        knot_tcp_conn_t **res = table->conns + (*hash % table->size);
        while (*res != NULL) {
index 8c15b528dde73c713350cf1b5015cb1b06dcc0bf..48c77160fb63dfffaaffdc3b0a60b2dddcd56649 100644 (file)
@@ -74,7 +74,7 @@ typedef struct {
        size_t size;
        size_t usage;
        size_t inbufs_total;
-       uint32_t hash_secret[4];
+       uint64_t hash_secret[2];
        knot_tcp_conn_t *conns[];
 } knot_tcp_table_t;