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