ip6_id.c revision 139826
1120639Sume/*	$KAME: ip6_id.c,v 1.13 2003/09/16 09:11:19 itojun Exp $	*/
2120639Sume/*	$OpenBSD: ip_id.c,v 1.6 2002/03/15 18:19:52 millert Exp $	*/
3120639Sume/* $FreeBSD: head/sys/netinet6/ip6_id.c 139826 2005-01-07 02:30:35Z imp $ */
4120639Sume
5139826Simp/*-
6120639Sume * Copyright (C) 2003 WIDE Project.
7120639Sume * All rights reserved.
8120639Sume *
9120639Sume * Redistribution and use in source and binary forms, with or without
10120639Sume * modification, are permitted provided that the following conditions
11120639Sume * are met:
12120639Sume * 1. Redistributions of source code must retain the above copyright
13120639Sume *    notice, this list of conditions and the following disclaimer.
14120639Sume * 2. Redistributions in binary form must reproduce the above copyright
15120639Sume *    notice, this list of conditions and the following disclaimer in the
16120639Sume *    documentation and/or other materials provided with the distribution.
17120639Sume * 3. Neither the name of the project nor the names of its contributors
18120639Sume *    may be used to endorse or promote products derived from this software
19120639Sume *    without specific prior written permission.
20120639Sume *
21120639Sume * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22120639Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23120639Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24120639Sume * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25120639Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26120639Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27120639Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28120639Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29120639Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30120639Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31120639Sume * SUCH DAMAGE.
32120639Sume */
33120639Sume
34139826Simp/*-
35120639Sume * Copyright 1998 Niels Provos <provos@citi.umich.edu>
36120639Sume * All rights reserved.
37120639Sume *
38120639Sume * Theo de Raadt <deraadt@openbsd.org> came up with the idea of using
39120639Sume * such a mathematical system to generate more random (yet non-repeating)
40120639Sume * ids to solve the resolver/named problem.  But Niels designed the
41120639Sume * actual system based on the constraints.
42120639Sume *
43120639Sume * Redistribution and use in source and binary forms, with or without
44120639Sume * modification, are permitted provided that the following conditions
45120639Sume * are met:
46120639Sume * 1. Redistributions of source code must retain the above copyright
47120639Sume *    notice, this list of conditions and the following disclaimer.
48120639Sume * 2. Redistributions in binary form must reproduce the above copyright
49120639Sume *    notice, this list of conditions and the following disclaimer in the
50120639Sume *    documentation and/or other materials provided with the distribution.
51120639Sume * 3. All advertising materials mentioning features or use of this software
52120639Sume *    must display the following acknowledgement:
53120639Sume *      This product includes software developed by Niels Provos.
54120639Sume * 4. The name of the author may not be used to endorse or promote products
55120639Sume *    derived from this software without specific prior written permission.
56120639Sume *
57120639Sume * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
58120639Sume * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59120639Sume * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60120639Sume * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
61120639Sume * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
62120639Sume * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
63120639Sume * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
64120639Sume * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65120639Sume * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
66120639Sume * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67120639Sume */
68120639Sume
69120639Sume/*
70120639Sume * seed = random (bits - 1) bit
71120639Sume * n = prime, g0 = generator to n,
72120639Sume * j = random so that gcd(j,n-1) == 1
73120639Sume * g = g0^j mod n will be a generator again.
74120639Sume *
75120639Sume * X[0] = random seed.
76120639Sume * X[n] = a*X[n-1]+b mod m is a Linear Congruential Generator
77120639Sume * with a = 7^(even random) mod m,
78120639Sume *      b = random with gcd(b,m) == 1
79120639Sume *      m = constant and a maximal period of m-1.
80120639Sume *
81120639Sume * The transaction id is determined by:
82120639Sume * id[n] = seed xor (g^X[n] mod n)
83120639Sume *
84120639Sume * Effectivly the id is restricted to the lower (bits - 1) bits, thus
85120639Sume * yielding two different cycles by toggling the msb on and off.
86120639Sume * This avoids reuse issues caused by reseeding.
87120639Sume */
88120639Sume
89120639Sume#include <sys/types.h>
90120639Sume#include <sys/param.h>
91120639Sume#include <sys/kernel.h>
92120639Sume#include <sys/socket.h>
93120639Sume#include <sys/libkern.h>
94120639Sume
95120639Sume#include <net/if.h>
96120639Sume#include <net/route.h>
97120639Sume#include <netinet/in.h>
98120639Sume#include <netinet/ip6.h>
99120639Sume#include <netinet6/ip6_var.h>
100120639Sume
101120639Sume#ifndef INT32_MAX
102120639Sume#define INT32_MAX	0x7fffffffU
103120639Sume#endif
104120639Sume
105120639Sumestruct randomtab {
106120639Sume	const int	ru_bits; /* resulting bits */
107120639Sume	const long	ru_out;	/* Time after wich will be reseeded */
108120639Sume	const u_int32_t ru_max;	/* Uniq cycle, avoid blackjack prediction */
109120639Sume	const u_int32_t ru_gen;	/* Starting generator */
110120639Sume	const u_int32_t ru_n;	/* ru_n: prime, ru_n - 1: product of pfacts[] */
111120639Sume	const u_int32_t ru_agen; /* determine ru_a as ru_agen^(2*rand) */
112120639Sume	const u_int32_t ru_m;	/* ru_m = 2^x*3^y */
113120639Sume	const u_int32_t pfacts[4];	/* factors of ru_n */
114120639Sume
115120639Sume	u_int32_t ru_counter;
116120639Sume	u_int32_t ru_msb;
117120639Sume
118120639Sume	u_int32_t ru_x;
119120639Sume	u_int32_t ru_seed, ru_seed2;
120120639Sume	u_int32_t ru_a, ru_b;
121120639Sume	u_int32_t ru_g;
122120639Sume	long ru_reseed;
123120639Sume};
124120639Sume
125120639Sumestatic struct randomtab randomtab_32 = {
126120639Sume	32,			/* resulting bits */
127120639Sume	180,			/* Time after wich will be reseeded */
128120639Sume	1000000000,		/* Uniq cycle, avoid blackjack prediction */
129120639Sume	2,			/* Starting generator */
130120639Sume	2147483629,		/* RU_N-1 = 2^2*3^2*59652323 */
131120639Sume	7,			/* determine ru_a as RU_AGEN^(2*rand) */
132120639Sume	1836660096,		/* RU_M = 2^7*3^15 - don't change */
133120639Sume	{ 2, 3, 59652323, 0 },	/* factors of ru_n */
134120639Sume};
135120639Sume
136120652Sumestatic struct randomtab randomtab_20 = {
137120652Sume	20,			/* resulting bits */
138120652Sume	180,			/* Time after wich will be reseeded */
139120652Sume	200000,			/* Uniq cycle, avoid blackjack prediction */
140120652Sume	2,			/* Starting generator */
141120652Sume	524269,			/* RU_N-1 = 2^2*3^2*14563 */
142120652Sume	7,			/* determine ru_a as RU_AGEN^(2*rand) */
143120652Sume	279936,			/* RU_M = 2^7*3^7 - don't change */
144120652Sume	{ 2, 3, 14563, 0 },	/* factors of ru_n */
145120652Sume};
146120652Sume
147120639Sumestatic u_int32_t pmod(u_int32_t, u_int32_t, u_int32_t);
148120639Sumestatic void initid(struct randomtab *);
149120639Sumestatic u_int32_t randomid(struct randomtab *);
150120639Sume
151120639Sume/*
152120639Sume * Do a fast modular exponation, returned value will be in the range
153120639Sume * of 0 - (mod-1)
154120639Sume */
155120639Sume
156120639Sumestatic u_int32_t
157120639Sumepmod(u_int32_t gen, u_int32_t expo, u_int32_t mod)
158120639Sume{
159120639Sume	u_int64_t s, t, u;
160120639Sume
161120639Sume	s = 1;
162120639Sume	t = gen;
163120639Sume	u = expo;
164120639Sume
165120639Sume	while (u) {
166120639Sume		if (u & 1)
167120639Sume			s = (s * t) % mod;
168120639Sume		u >>= 1;
169120639Sume		t = (t * t) % mod;
170120639Sume	}
171120639Sume	return (s);
172120639Sume}
173120639Sume
174120639Sume/*
175120639Sume * Initalizes the seed and chooses a suitable generator. Also toggles
176120639Sume * the msb flag. The msb flag is used to generate two distinct
177120639Sume * cycles of random numbers and thus avoiding reuse of ids.
178120639Sume *
179120639Sume * This function is called from id_randomid() when needed, an
180120639Sume * application does not have to worry about it.
181120639Sume */
182120639Sumestatic void
183120639Sumeinitid(struct randomtab *p)
184120639Sume{
185120639Sume	u_int32_t j, i;
186120639Sume	int noprime = 1;
187120639Sume
188120639Sume	p->ru_x = arc4random() % p->ru_m;
189120639Sume
190120639Sume	/* (bits - 1) bits of random seed */
191120639Sume	p->ru_seed = arc4random() & (~0U >> (32 - p->ru_bits + 1));
192120639Sume	p->ru_seed2 = arc4random() & (~0U >> (32 - p->ru_bits + 1));
193120639Sume
194120639Sume	/* Determine the LCG we use */
195120639Sume	p->ru_b = (arc4random() & (~0U >> (32 - p->ru_bits))) | 1;
196120639Sume	p->ru_a = pmod(p->ru_agen,
197120639Sume	    (arc4random() & (~0U >> (32 - p->ru_bits))) & (~1U), p->ru_m);
198120639Sume	while (p->ru_b % 3 == 0)
199120639Sume		p->ru_b += 2;
200120639Sume
201120639Sume	j = arc4random() % p->ru_n;
202120639Sume
203120639Sume	/*
204120639Sume	 * Do a fast gcd(j, RU_N - 1), so we can find a j with
205120639Sume	 * gcd(j, RU_N - 1) == 1, giving a new generator for
206120639Sume	 * RU_GEN^j mod RU_N
207120639Sume	 */
208120639Sume	while (noprime) {
209120639Sume		for (i = 0; p->pfacts[i] > 0; i++)
210120639Sume			if (j % p->pfacts[i] == 0)
211120639Sume				break;
212120639Sume
213120639Sume		if (p->pfacts[i] == 0)
214120639Sume			noprime = 0;
215120639Sume		else
216120639Sume			j = (j + 1) % p->ru_n;
217120639Sume	}
218120639Sume
219120639Sume	p->ru_g = pmod(p->ru_gen, j, p->ru_n);
220120639Sume	p->ru_counter = 0;
221120639Sume
222120639Sume	p->ru_reseed = time_second + p->ru_out;
223120639Sume	p->ru_msb = p->ru_msb ? 0 : (1U << (p->ru_bits - 1));
224120639Sume}
225120639Sume
226120639Sumestatic u_int32_t
227120639Sumerandomid(struct randomtab *p)
228120639Sume{
229120639Sume	int i, n;
230120639Sume	u_int32_t tmp;
231120639Sume
232120639Sume	if (p->ru_counter >= p->ru_max || time_second > p->ru_reseed)
233120639Sume		initid(p);
234120639Sume
235120639Sume	tmp = arc4random();
236120639Sume
237120639Sume	/* Skip a random number of ids */
238120639Sume	n = tmp & 0x3; tmp = tmp >> 2;
239120639Sume	if (p->ru_counter + n >= p->ru_max)
240120639Sume		initid(p);
241120639Sume
242120639Sume	for (i = 0; i <= n; i++) {
243120639Sume		/* Linear Congruential Generator */
244120639Sume		p->ru_x = (u_int32_t)((u_int64_t)p->ru_a * p->ru_x + p->ru_b) % p->ru_m;
245120639Sume	}
246120639Sume
247120639Sume	p->ru_counter += i;
248120639Sume
249120639Sume	return (p->ru_seed ^ pmod(p->ru_g, p->ru_seed2 ^ p->ru_x, p->ru_n)) |
250120639Sume	    p->ru_msb;
251120639Sume}
252120639Sume
253120639Sumeu_int32_t
254120639Sumeip6_randomid(void)
255120639Sume{
256120639Sume
257120639Sume	return randomid(&randomtab_32);
258120639Sume}
259120641Sume
260120649Sumeu_int32_t
261120649Sumeip6_randomflowlabel(void)
262120649Sume{
263120649Sume
264120649Sume	return randomid(&randomtab_20) & 0xfffff;
265120649Sume}
266