/*-1* SPDX-License-Identifier: (BSD-3-Clause AND BSD-2-Clause)2*3* Copyright (C) 2003 WIDE Project.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. Neither the name of the project nor the names of its contributors15* may be used to endorse or promote products derived from this software16* without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*30* $KAME: ip6_id.c,v 1.13 2003/09/16 09:11:19 itojun Exp $31*/3233/*-34* Copyright 1998 Niels Provos <[email protected]>35* All rights reserved.36*37* Theo de Raadt <[email protected]> came up with the idea of using38* such a mathematical system to generate more random (yet non-repeating)39* ids to solve the resolver/named problem. But Niels designed the40* actual system based on the constraints.41*42* Redistribution and use in source and binary forms, with or without43* modification, are permitted provided that the following conditions44* are met:45* 1. Redistributions of source code must retain the above copyright46* notice, this list of conditions and the following disclaimer.47* 2. Redistributions in binary form must reproduce the above copyright48* notice, this list of conditions and the following disclaimer in the49* documentation and/or other materials provided with the distribution.50*51* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR52* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES53* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.54* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,55* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT56* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,57* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY58* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT59* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF60* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.61*62* $OpenBSD: ip6_id.c,v 1.3 2003/12/12 06:57:12 itojun Exp $63*/6465/*66* seed = random (bits - 1) bit67* n = prime, g0 = generator to n,68* j = random so that gcd(j,n-1) == 169* g = g0^j mod n will be a generator again.70*71* X[0] = random seed.72* X[n] = a*X[n-1]+b mod m is a Linear Congruential Generator73* with a = 7^(even random) mod m,74* b = random with gcd(b,m) == 175* m = constant and a maximal period of m-1.76*77* The transaction id is determined by:78* id[n] = seed xor (g^X[n] mod n)79*80* Effectively the id is restricted to the lower (bits - 1) bits, thus81* yielding two different cycles by toggling the msb on and off.82* This avoids reuse issues caused by reseeding.83*/8485#include <sys/types.h>86#include <sys/param.h>87#include <sys/kernel.h>88#include <sys/random.h>89#include <sys/socket.h>90#include <sys/libkern.h>9192#include <net/if.h>93#include <net/route.h>94#include <net/vnet.h>95#include <netinet/in.h>96#include <netinet/ip6.h>97#include <netinet6/ip6_var.h>9899#ifndef INT32_MAX100#define INT32_MAX 0x7fffffffU101#endif102103struct randomtab {104const int ru_bits; /* resulting bits */105const long ru_out; /* Time after which will be reseeded */106const u_int32_t ru_max; /* Uniq cycle, avoid blackjack prediction */107const u_int32_t ru_gen; /* Starting generator */108const u_int32_t ru_n; /* ru_n: prime, ru_n - 1: product of pfacts[] */109const u_int32_t ru_agen; /* determine ru_a as ru_agen^(2*rand) */110const u_int32_t ru_m; /* ru_m = 2^x*3^y */111const u_int32_t pfacts[4]; /* factors of ru_n */112113u_int32_t ru_counter;114u_int32_t ru_msb;115116u_int32_t ru_x;117u_int32_t ru_seed, ru_seed2;118u_int32_t ru_a, ru_b;119u_int32_t ru_g;120long ru_reseed;121};122123static struct randomtab randomtab_32 = {12432, /* resulting bits */125180, /* Time after which will be reseeded */1261000000000, /* Uniq cycle, avoid blackjack prediction */1272, /* Starting generator */1282147483629, /* RU_N-1 = 2^2*3^2*59652323 */1297, /* determine ru_a as RU_AGEN^(2*rand) */1301836660096, /* RU_M = 2^7*3^15 - don't change */131{ 2, 3, 59652323, 0 }, /* factors of ru_n */132};133134static struct randomtab randomtab_20 = {13520, /* resulting bits */136180, /* Time after which will be reseeded */137200000, /* Uniq cycle, avoid blackjack prediction */1382, /* Starting generator */139524269, /* RU_N-1 = 2^2*3^2*14563 */1407, /* determine ru_a as RU_AGEN^(2*rand) */141279936, /* RU_M = 2^7*3^7 - don't change */142{ 2, 3, 14563, 0 }, /* factors of ru_n */143};144145static u_int32_t pmod(u_int32_t, u_int32_t, u_int32_t);146static void initid(struct randomtab *);147static u_int32_t randomid(struct randomtab *);148149/*150* Do a fast modular exponation, returned value will be in the range151* of 0 - (mod-1)152*/153static u_int32_t154pmod(u_int32_t gen, u_int32_t expo, u_int32_t mod)155{156u_int64_t s, t, u;157158s = 1;159t = gen;160u = expo;161162while (u) {163if (u & 1)164s = (s * t) % mod;165u >>= 1;166t = (t * t) % mod;167}168return (s);169}170171/*172* Initializes the seed and chooses a suitable generator. Also toggles173* the msb flag. The msb flag is used to generate two distinct174* cycles of random numbers and thus avoiding reuse of ids.175*176* This function is called from id_randomid() when needed, an177* application does not have to worry about it.178*/179static void180initid(struct randomtab *p)181{182u_int32_t j, i;183int noprime = 1;184185p->ru_x = arc4random() % p->ru_m;186187/* (bits - 1) bits of random seed */188p->ru_seed = arc4random() & (~0U >> (32 - p->ru_bits + 1));189p->ru_seed2 = arc4random() & (~0U >> (32 - p->ru_bits + 1));190191/* Determine the LCG we use */192p->ru_b = (arc4random() & (~0U >> (32 - p->ru_bits))) | 1;193p->ru_a = pmod(p->ru_agen,194(arc4random() & (~0U >> (32 - p->ru_bits))) & (~1U), p->ru_m);195while (p->ru_b % 3 == 0)196p->ru_b += 2;197198j = arc4random() % p->ru_n;199200/*201* Do a fast gcd(j, RU_N - 1), so we can find a j with202* gcd(j, RU_N - 1) == 1, giving a new generator for203* RU_GEN^j mod RU_N204*/205while (noprime) {206for (i = 0; p->pfacts[i] > 0; i++)207if (j % p->pfacts[i] == 0)208break;209210if (p->pfacts[i] == 0)211noprime = 0;212else213j = (j + 1) % p->ru_n;214}215216p->ru_g = pmod(p->ru_gen, j, p->ru_n);217p->ru_counter = 0;218219p->ru_reseed = time_uptime + p->ru_out;220p->ru_msb = p->ru_msb ? 0 : (1U << (p->ru_bits - 1));221}222223static u_int32_t224randomid(struct randomtab *p)225{226int i, n;227228if (p->ru_counter >= p->ru_max || time_uptime > p->ru_reseed)229initid(p);230231/* Skip a random number of ids */232n = arc4random() & 0x3;233if (p->ru_counter + n >= p->ru_max)234initid(p);235236for (i = 0; i <= n; i++) {237/* Linear Congruential Generator */238p->ru_x = (u_int32_t)((u_int64_t)p->ru_a * p->ru_x + p->ru_b) % p->ru_m;239}240241p->ru_counter += i;242243return (p->ru_seed ^ pmod(p->ru_g, p->ru_seed2 + p->ru_x, p->ru_n)) |244p->ru_msb;245}246247u_int32_t248ip6_randomid(void)249{250251return randomid(&randomtab_32);252}253254u_int32_t255ip6_randomflowlabel(void)256{257258/*259* It's ok to emit zero flow labels early, before random is available260* (seeded). RFC 6437:261*262* "A Flow Label of zero is used to indicate packets that have not been263* labeled."264*/265if (__predict_false(!is_random_seeded()))266return (0);267268return randomid(&randomtab_20) & 0xfffff;269}270271272