1 /*- 2 * SPDX-License-Identifier: (BSD-3-Clause AND BSD-2-Clause) 3 * 4 * Copyright (C) 2003 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * $KAME: ip6_id.c,v 1.13 2003/09/16 09:11:19 itojun Exp $ 32 */ 33 34 /*- 35 * Copyright 1998 Niels Provos <provos@citi.umich.edu> 36 * All rights reserved. 37 * 38 * Theo de Raadt <deraadt@openbsd.org> came up with the idea of using 39 * such a mathematical system to generate more random (yet non-repeating) 40 * ids to solve the resolver/named problem. But Niels designed the 41 * actual system based on the constraints. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 52 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 53 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 54 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 55 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 56 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 57 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 58 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 61 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 62 * 63 * $OpenBSD: ip6_id.c,v 1.3 2003/12/12 06:57:12 itojun Exp $ 64 */ 65 66 #include <sys/cdefs.h> 67 /* 68 * seed = random (bits - 1) bit 69 * n = prime, g0 = generator to n, 70 * j = random so that gcd(j,n-1) == 1 71 * g = g0^j mod n will be a generator again. 72 * 73 * X[0] = random seed. 74 * X[n] = a*X[n-1]+b mod m is a Linear Congruential Generator 75 * with a = 7^(even random) mod m, 76 * b = random with gcd(b,m) == 1 77 * m = constant and a maximal period of m-1. 78 * 79 * The transaction id is determined by: 80 * id[n] = seed xor (g^X[n] mod n) 81 * 82 * Effectively the id is restricted to the lower (bits - 1) bits, thus 83 * yielding two different cycles by toggling the msb on and off. 84 * This avoids reuse issues caused by reseeding. 85 */ 86 87 #include <sys/types.h> 88 #include <sys/param.h> 89 #include <sys/kernel.h> 90 #include <sys/random.h> 91 #include <sys/socket.h> 92 #include <sys/libkern.h> 93 94 #include <net/if.h> 95 #include <net/route.h> 96 #include <net/vnet.h> 97 #include <netinet/in.h> 98 #include <netinet/ip6.h> 99 #include <netinet6/ip6_var.h> 100 101 #ifndef INT32_MAX 102 #define INT32_MAX 0x7fffffffU 103 #endif 104 105 struct randomtab { 106 const int ru_bits; /* resulting bits */ 107 const long ru_out; /* Time after which will be reseeded */ 108 const u_int32_t ru_max; /* Uniq cycle, avoid blackjack prediction */ 109 const u_int32_t ru_gen; /* Starting generator */ 110 const u_int32_t ru_n; /* ru_n: prime, ru_n - 1: product of pfacts[] */ 111 const u_int32_t ru_agen; /* determine ru_a as ru_agen^(2*rand) */ 112 const u_int32_t ru_m; /* ru_m = 2^x*3^y */ 113 const u_int32_t pfacts[4]; /* factors of ru_n */ 114 115 u_int32_t ru_counter; 116 u_int32_t ru_msb; 117 118 u_int32_t ru_x; 119 u_int32_t ru_seed, ru_seed2; 120 u_int32_t ru_a, ru_b; 121 u_int32_t ru_g; 122 long ru_reseed; 123 }; 124 125 static struct randomtab randomtab_32 = { 126 32, /* resulting bits */ 127 180, /* Time after which will be reseeded */ 128 1000000000, /* Uniq cycle, avoid blackjack prediction */ 129 2, /* Starting generator */ 130 2147483629, /* RU_N-1 = 2^2*3^2*59652323 */ 131 7, /* determine ru_a as RU_AGEN^(2*rand) */ 132 1836660096, /* RU_M = 2^7*3^15 - don't change */ 133 { 2, 3, 59652323, 0 }, /* factors of ru_n */ 134 }; 135 136 static struct randomtab randomtab_20 = { 137 20, /* resulting bits */ 138 180, /* Time after which will be reseeded */ 139 200000, /* Uniq cycle, avoid blackjack prediction */ 140 2, /* Starting generator */ 141 524269, /* RU_N-1 = 2^2*3^2*14563 */ 142 7, /* determine ru_a as RU_AGEN^(2*rand) */ 143 279936, /* RU_M = 2^7*3^7 - don't change */ 144 { 2, 3, 14563, 0 }, /* factors of ru_n */ 145 }; 146 147 static u_int32_t pmod(u_int32_t, u_int32_t, u_int32_t); 148 static void initid(struct randomtab *); 149 static u_int32_t randomid(struct randomtab *); 150 151 /* 152 * Do a fast modular exponation, returned value will be in the range 153 * of 0 - (mod-1) 154 */ 155 static u_int32_t 156 pmod(u_int32_t gen, u_int32_t expo, u_int32_t mod) 157 { 158 u_int64_t s, t, u; 159 160 s = 1; 161 t = gen; 162 u = expo; 163 164 while (u) { 165 if (u & 1) 166 s = (s * t) % mod; 167 u >>= 1; 168 t = (t * t) % mod; 169 } 170 return (s); 171 } 172 173 /* 174 * Initializes the seed and chooses a suitable generator. Also toggles 175 * the msb flag. The msb flag is used to generate two distinct 176 * cycles of random numbers and thus avoiding reuse of ids. 177 * 178 * This function is called from id_randomid() when needed, an 179 * application does not have to worry about it. 180 */ 181 static void 182 initid(struct randomtab *p) 183 { 184 u_int32_t j, i; 185 int noprime = 1; 186 187 p->ru_x = arc4random() % p->ru_m; 188 189 /* (bits - 1) bits of random seed */ 190 p->ru_seed = arc4random() & (~0U >> (32 - p->ru_bits + 1)); 191 p->ru_seed2 = arc4random() & (~0U >> (32 - p->ru_bits + 1)); 192 193 /* Determine the LCG we use */ 194 p->ru_b = (arc4random() & (~0U >> (32 - p->ru_bits))) | 1; 195 p->ru_a = pmod(p->ru_agen, 196 (arc4random() & (~0U >> (32 - p->ru_bits))) & (~1U), p->ru_m); 197 while (p->ru_b % 3 == 0) 198 p->ru_b += 2; 199 200 j = arc4random() % p->ru_n; 201 202 /* 203 * Do a fast gcd(j, RU_N - 1), so we can find a j with 204 * gcd(j, RU_N - 1) == 1, giving a new generator for 205 * RU_GEN^j mod RU_N 206 */ 207 while (noprime) { 208 for (i = 0; p->pfacts[i] > 0; i++) 209 if (j % p->pfacts[i] == 0) 210 break; 211 212 if (p->pfacts[i] == 0) 213 noprime = 0; 214 else 215 j = (j + 1) % p->ru_n; 216 } 217 218 p->ru_g = pmod(p->ru_gen, j, p->ru_n); 219 p->ru_counter = 0; 220 221 p->ru_reseed = time_uptime + p->ru_out; 222 p->ru_msb = p->ru_msb ? 0 : (1U << (p->ru_bits - 1)); 223 } 224 225 static u_int32_t 226 randomid(struct randomtab *p) 227 { 228 int i, n; 229 230 if (p->ru_counter >= p->ru_max || time_uptime > p->ru_reseed) 231 initid(p); 232 233 /* Skip a random number of ids */ 234 n = arc4random() & 0x3; 235 if (p->ru_counter + n >= p->ru_max) 236 initid(p); 237 238 for (i = 0; i <= n; i++) { 239 /* Linear Congruential Generator */ 240 p->ru_x = (u_int32_t)((u_int64_t)p->ru_a * p->ru_x + p->ru_b) % p->ru_m; 241 } 242 243 p->ru_counter += i; 244 245 return (p->ru_seed ^ pmod(p->ru_g, p->ru_seed2 + p->ru_x, p->ru_n)) | 246 p->ru_msb; 247 } 248 249 u_int32_t 250 ip6_randomid(void) 251 { 252 253 return randomid(&randomtab_32); 254 } 255 256 u_int32_t 257 ip6_randomflowlabel(void) 258 { 259 260 /* 261 * It's ok to emit zero flow labels early, before random is available 262 * (seeded). RFC 6437: 263 * 264 * "A Flow Label of zero is used to indicate packets that have not been 265 * labeled." 266 */ 267 if (__predict_false(!is_random_seeded())) 268 return (0); 269 270 return randomid(&randomtab_20) & 0xfffff; 271 } 272