/*
subnet.c -- handle subnet lookups and lists
- Copyright (C) 2000-2017 Guus Sliepen <guus@tinc-vpn.org>,
+ Copyright (C) 2000-2022 Guus Sliepen <guus@tinc-vpn.org>,
2000-2005 Ivo Timmermans
This program is free software; you can redistribute it and/or modify
#include "splay_tree.h"
#include "control_common.h"
+#include "crypto.h"
#include "hash.h"
#include "logger.h"
#include "net.h"
/* Subnet lookup cache */
+static uint32_t wrapping_add32(uint32_t a, uint32_t b) {
+ return (uint32_t)((uint64_t)a + b);
+}
+
+static uint32_t wrapping_mul32(uint32_t a, uint32_t b) {
+ return (uint32_t)((uint64_t)a * b);
+}
+
static uint32_t hash_function_ipv4_t(const ipv4_t *p) {
/*
This basic hash works because
#if __BYTE_ORDER == __LITTLE_ENDIAN
// 10.0.x.x/16 part
- hash += halfwidth[1] * 0x9e370001UL;
+ hash = wrapping_add32(hash, wrapping_mul32(halfwidth[1], 0x9e370001U));
// x.x.0.[0-255] part
#if SUBNET_HASH_SIZE >= 0x10000
#endif // _____LP64_____
#else
// 10.0.x.x/16 part
- hash += halfwidth[0] * 0x9e370001UL;
+ hash = wrapping_add32(hash, wrapping_mul32(halfwidth[0], 0x9e370001U));
// x.x.0.[0-255] part (ntohs is nop on big endian)
return hash ^ halfwidth[1];
uint32_t hash = hash_seed;
for(int i = 0; i < 4; i++) {
- hash += fullwidth[i];
- hash *= 0x9e370001UL;
+ hash = wrapping_add32(hash, fullwidth[i]);
+ hash = wrapping_mul32(hash, 0x9e370001U);
}
return hash;
uint32_t hash = hash_seed;
for(int i = 0; i < 3; i++) {
- hash += halfwidth[i];
- hash *= 0x9e370001UL;
+ hash = wrapping_add32(hash, halfwidth[i]);
+ hash = wrapping_mul32(hash, 0x9e370001U);
}
return hash;
/* Initialising trees */
void init_subnets(void) {
- hash_seed = (uint32_t)rand();
+ hash_seed = prng(UINT32_MAX);
+
+ // tables need to be cleared on startup
+ subnet_cache_flush_tables();
}
void exit_subnets(void) {
hash_clear(mac_t, &mac_cache);
}
-void subnet_cache_flush(subnet_t *subnet) {
+static void subnet_cache_flush(subnet_t *subnet) {
switch(subnet->type) {
case SUBNET_IPV4:
if(subnet->net.ipv4.prefixlength == 32) {
void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
char netstr[MAXNETSTR];
- char *name, *address, *port;
+ char *address, *port;
char empty[] = "";
// Prepare environment variables to be passed to the script
int env_subnet = environment_add(&env, NULL);
int env_weight = environment_add(&env, NULL);
- name = up ? "subnet-up" : "subnet-down";
+ const char *name = up ? "subnet-up" : "subnet-down";
if(!subnet) {
for splay_each(subnet_t, subnet, &owner->subnet_tree) {