xref: /freebsd/sys/netinet/ip_id.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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 
38 /*-
39  * seed = random 15bit
40  * n = prime, g0 = generator to n,
41  * j = random so that gcd(j,n-1) == 1
42  * g = g0^j mod n will be a generator again.
43  *
44  * X[0] = random seed.
45  * X[n] = a*X[n-1]+b mod m is a Linear Congruential Generator
46  * with a = 7^(even random) mod m,
47  *      b = random with gcd(b,m) == 1
48  *      m = 31104 and a maximal period of m-1.
49  *
50  * The transaction id is determined by:
51  * id[n] = seed xor (g^X[n] mod n)
52  *
53  * Effectivly the id is restricted to the lower 15 bits, thus yielding two
54  * different cycles by toggling the msb on and off.  This avoids reuse issues
55  * caused by reseeding.
56  */
57 
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
60 
61 #include "opt_pf.h"
62 #include <sys/param.h>
63 #include <sys/time.h>
64 #include <sys/kernel.h>
65 #include <sys/random.h>
66 
67 #define	RU_OUT  180		/* Time after wich will be reseeded */
68 #define	RU_MAX	30000		/* Uniq cycle, avoid blackjack prediction */
69 #define	RU_GEN	2		/* Starting generator */
70 #define	RU_N	32749		/* RU_N-1 = 2*2*3*2729 */
71 #define	RU_AGEN	7		/* determine ru_a as RU_AGEN^(2*rand) */
72 #define	RU_M	31104		/* RU_M = 2^7*3^5 - don't change */
73 
74 #define	PFAC_N 3
75 const static u_int16_t pfacts[PFAC_N] = {
76 	2,
77 	3,
78 	2729
79 };
80 
81 static u_int16_t ru_x;
82 static u_int16_t ru_seed, ru_seed2;
83 static u_int16_t ru_a, ru_b;
84 static u_int16_t ru_g;
85 static u_int16_t ru_counter = 0;
86 static u_int16_t ru_msb = 0;
87 static long ru_reseed;
88 static u_int32_t tmp;		/* Storage for unused random */
89 
90 static u_int16_t	pmod(u_int16_t, u_int16_t, u_int16_t);
91 static void		ip_initid(void);
92 u_int16_t		ip_randomid(void);
93 
94 /*
95  * Do a fast modular exponation, returned value will be in the range of 0 -
96  * (mod-1).
97  */
98 
99 static u_int16_t
100 pmod(u_int16_t gen, u_int16_t exp, u_int16_t mod)
101 {
102 	u_int16_t s, t, u;
103 
104 	s = 1;
105 	t = gen;
106 	u = exp;
107 
108 	while (u) {
109 		if (u & 1)
110 			s = (s*t) % mod;
111 		u >>= 1;
112 		t = (t*t) % mod;
113 	}
114 	return (s);
115 }
116 
117 /*
118  * Initalizes the seed and chooses a suitable generator.  Also toggles the
119  * msb flag.  The msb flag is used to generate two distinct cycles of random
120  * numbers and thus avoiding reuse of ids.
121  *
122  * This function is called from id_randomid() when needed, an application
123  * does not have to worry about it.
124  */
125 static void
126 ip_initid(void)
127 {
128 	u_int16_t j, i;
129 	int noprime = 1;
130 	struct timeval time;
131 
132 	getmicrotime(&time);
133 	read_random((void *) &tmp, sizeof(tmp));
134 	ru_x = (tmp & 0xFFFF) % RU_M;
135 
136 	/* 15 bits of random seed. */
137 	ru_seed = (tmp >> 16) & 0x7FFF;
138 	read_random((void *) &tmp, sizeof(tmp));
139 	ru_seed2 = tmp & 0x7FFF;
140 
141 	read_random((void *) &tmp, sizeof(tmp));
142 
143 	/* Determine the LCG we use. */
144 	ru_b = (tmp & 0xfffe) | 1;
145 	ru_a = pmod(RU_AGEN, (tmp >> 16) & 0xfffe, RU_M);
146 	while (ru_b % 3 == 0)
147 	  ru_b += 2;
148 
149 	read_random((void *) &tmp, sizeof(tmp));
150 	j = tmp % RU_N;
151 	tmp = tmp >> 16;
152 
153 	/*
154 	 * Do a fast gcd(j,RU_N-1), so we can find a j with gcd(j, RU_N-1) ==
155 	 * 1, giving a new generator for RU_GEN^j mod RU_N.
156 	 */
157 	while (noprime) {
158 		for (i=0; i<PFAC_N; i++)
159 			if (j%pfacts[i] == 0)
160 				break;
161 
162 		if (i>=PFAC_N)
163 			noprime = 0;
164 		else
165 			j = (j+1) % RU_N;
166 	}
167 
168 	ru_g = pmod(RU_GEN,j,RU_N);
169 	ru_counter = 0;
170 
171 	ru_reseed = time.tv_sec + RU_OUT;
172 	ru_msb = ru_msb == 0x8000 ? 0 : 0x8000;
173 }
174 
175 u_int16_t
176 ip_randomid(void)
177 {
178 	int i, n;
179 	struct timeval time;
180 
181 	/* XXX not reentrant */
182 	getmicrotime(&time);
183 	if (ru_counter >= RU_MAX || time.tv_sec > ru_reseed)
184 		ip_initid();
185 
186 	if (!tmp)
187 		read_random((void *) &tmp, sizeof(tmp));
188 
189 	/* Skip a random number of ids. */
190 	n = tmp & 0x3; tmp = tmp >> 2;
191 	if (ru_counter + n >= RU_MAX)
192 		ip_initid();
193 
194 	for (i = 0; i <= n; i++)
195 		/* Linear Congruential Generator. */
196 		ru_x = (ru_a*ru_x + ru_b) % RU_M;
197 
198 	ru_counter += i;
199 
200 	return (ru_seed ^ pmod(ru_g,ru_seed2 ^ ru_x,RU_N)) | ru_msb;
201 }
202