1/* $OpenBSD: ip_id.c,v 1.2 1999/08/26 13:37:01 provos Exp $ */ 2 3/* 4 * Copyright 1998 Niels Provos <provos@citi.umich.edu> 5 * All rights reserved. 6 * 7 * Theo de Raadt <deraadt@openbsd.org> came up with the idea of using 8 * such a mathematical system to generate more random (yet non-repeating) 9 * ids to solve the resolver/named problem. But Niels designed the 10 * actual system based on the constraints. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by Niels Provos. 23 * 4. The name of the author may not be used to endorse or promote products 24 * derived from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 * 37 * $FreeBSD: src/sys/netinet/ip_id.c,v 1.1.2.1 2001/07/19 06:37:26 kris Exp $ 38 */ 39 40/* 41 * seed = random 15bit 42 * n = prime, g0 = generator to n, 43 * j = random so that gcd(j,n-1) == 1 44 * g = g0^j mod n will be a generator again. 45 * 46 * X[0] = random seed. 47 * X[n] = a*X[n-1]+b mod m is a Linear Congruential Generator 48 * with a = 7^(even random) mod m, 49 * b = random with gcd(b,m) == 1 50 * m = 31104 and a maximal period of m-1. 51 * 52 * The transaction id is determined by: 53 * id[n] = seed xor (g^X[n] mod n) 54 * 55 * Effectivly the id is restricted to the lower 15 bits, thus 56 * yielding two different cycles by toggling the msb on and off. 57 * This avoids reuse issues caused by reseeding. 58 */ 59 60#include <sys/param.h> 61#include <sys/time.h> 62#include <sys/kernel.h> 63#include <sys/random.h> 64 65#if RANDOM_IP_ID 66#define RU_OUT 180 /* Time after wich will be reseeded */ 67#define RU_MAX 30000 /* Uniq cycle, avoid blackjack prediction */ 68#define RU_GEN 2 /* Starting generator */ 69#define RU_N 32749 /* RU_N-1 = 2*2*3*2729 */ 70#define RU_AGEN 7 /* determine ru_a as RU_AGEN^(2*rand) */ 71#define RU_M 31104 /* RU_M = 2^7*3^5 - don't change */ 72 73#define PFAC_N 3 74static u_int16_t pfacts[PFAC_N] = { 75 2, 76 3, 77 2729 78}; 79 80static u_int16_t ru_x; 81static u_int16_t ru_seed, ru_seed2; 82static u_int16_t ru_a, ru_b; 83static u_int16_t ru_g; 84static u_int16_t ru_counter = 0; 85static u_int16_t ru_msb = 0; 86static time_t ru_reseed; 87static u_int32_t tmp; /* Storage for unused random */ 88 89static u_int16_t pmod(u_int16_t, u_int16_t, u_int16_t); 90static void ip_initid(void); 91u_int16_t ip_randomid(void); 92 93extern u_short ip_id; 94extern int ip_use_randomid; 95/* 96 * Do a fast modular exponation, returned value will be in the range 97 * of 0 - (mod-1) 98 */ 99 100#ifdef __STDC__ 101static u_int16_t 102pmod(u_int16_t gen, u_int16_t exp, u_int16_t mod) 103#else 104static u_int16_t 105pmod(gen, exp, mod) 106 u_int16_t gen, exp, mod; 107#endif 108{ 109 u_int16_t s, t, u; 110 111 s = 1; 112 t = gen; 113 u = exp; 114 115 while (u) { 116 if (u & 1) 117 s = (s*t) % mod; 118 u >>= 1; 119 t = (t*t) % mod; 120 } 121 return (s); 122} 123 124/* 125 * Initalizes the seed and chooses a suitable generator. Also toggles 126 * the msb flag. The msb flag is used to generate two distinct 127 * cycles of random numbers and thus avoiding reuse of ids. 128 * 129 * This function is called from id_randomid() when needed, an 130 * application does not have to worry about it. 131 */ 132static void 133ip_initid(void) 134{ 135 u_int16_t j, i; 136 int noprime = 1; 137 struct timeval timenow; 138 139 getmicrotime(&timenow); 140 read_random((void *) &tmp, sizeof(tmp)); 141 ru_x = (tmp & 0xFFFF) % RU_M; 142 143 /* 15 bits of random seed */ 144 ru_seed = (tmp >> 16) & 0x7FFF; 145 read_random((void *) &tmp, sizeof(tmp)); 146 ru_seed2 = tmp & 0x7FFF; 147 148 read_random((void *) &tmp, sizeof(tmp)); 149 150 /* Determine the LCG we use */ 151 ru_b = (tmp & 0xfffe) | 1; 152 ru_a = pmod(RU_AGEN, (tmp >> 16) & 0xfffe, RU_M); 153 while (ru_b % 3 == 0) 154 ru_b += 2; 155 156 read_random((void *) &tmp, sizeof(tmp)); 157 j = tmp % RU_N; 158 tmp = tmp >> 16; 159 160 /* 161 * Do a fast gcd(j,RU_N-1), so we can find a j with 162 * gcd(j, RU_N-1) == 1, giving a new generator for 163 * RU_GEN^j mod RU_N 164 */ 165 166 while (noprime) { 167 for (i=0; i<PFAC_N; i++) 168 if (j%pfacts[i] == 0) 169 break; 170 171 if (i>=PFAC_N) 172 noprime = 0; 173 else 174 j = (j+1) % RU_N; 175 } 176 177 ru_g = pmod(RU_GEN,j,RU_N); 178 ru_counter = 0; 179 180 ru_reseed = timenow.tv_sec + RU_OUT; 181 ru_msb = ru_msb == 0x8000 ? 0 : 0x8000; 182} 183 184u_int16_t 185ip_randomid(void) 186{ 187 int i, n; 188 struct timeval timenow; 189 190 /* if net.inet.ip.random_id is disabled, 191 * reverts to the incrementing ip_id 192 */ 193 194 if (ip_use_randomid == 0) 195 return htons(ip_id++); 196 197 198 getmicrotime(&timenow); 199 if (ru_counter >= RU_MAX || timenow.tv_sec > ru_reseed) 200 ip_initid(); 201 202 if (!tmp) 203 read_random((void *) &tmp, sizeof(tmp)); 204 205 /* Skip a random number of ids */ 206 n = tmp & 0x3; tmp = tmp >> 2; 207 if (ru_counter + n >= RU_MAX) 208 ip_initid(); 209 210 for (i = 0; i <= n; i++) 211 /* Linear Congruential Generator */ 212 ru_x = (ru_a*ru_x + ru_b) % RU_M; 213 214 ru_counter += i; 215 216 return (ru_seed ^ pmod(ru_g,ru_seed2 ^ ru_x,RU_N)) | ru_msb; 217} 218 219#endif /* RANDOM_IP_ID */ 220