ip6_id.c revision 120649
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 120649 2003-10-01 21:24:28Z ume $ */
4120639Sume
5120639Sume/*
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
34120639Sume/*
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
89120642Sume#include "opt_random_ip_id.h"
90120642Sume
91120639Sume#include <sys/types.h>
92120639Sume#include <sys/param.h>
93120639Sume#include <sys/kernel.h>
94120639Sume#include <sys/socket.h>
95120639Sume#include <sys/libkern.h>
96120639Sume
97120639Sume#include <net/if.h>
98120639Sume#include <net/route.h>
99120639Sume#include <netinet/in.h>
100120639Sume#include <netinet/ip6.h>
101120639Sume#include <netinet6/ip6_var.h>
102120639Sume
103120641Sume#ifdef RANDOM_IP_ID
104120641Sume
105120639Sume#ifndef INT32_MAX
106120639Sume#define INT32_MAX	0x7fffffffU
107120639Sume#endif
108120639Sume
109120639Sumestruct randomtab {
110120639Sume	const int	ru_bits; /* resulting bits */
111120639Sume	const long	ru_out;	/* Time after wich will be reseeded */
112120639Sume	const u_int32_t ru_max;	/* Uniq cycle, avoid blackjack prediction */
113120639Sume	const u_int32_t ru_gen;	/* Starting generator */
114120639Sume	const u_int32_t ru_n;	/* ru_n: prime, ru_n - 1: product of pfacts[] */
115120639Sume	const u_int32_t ru_agen; /* determine ru_a as ru_agen^(2*rand) */
116120639Sume	const u_int32_t ru_m;	/* ru_m = 2^x*3^y */
117120639Sume	const u_int32_t pfacts[4];	/* factors of ru_n */
118120639Sume
119120639Sume	u_int32_t ru_counter;
120120639Sume	u_int32_t ru_msb;
121120639Sume
122120639Sume	u_int32_t ru_x;
123120639Sume	u_int32_t ru_seed, ru_seed2;
124120639Sume	u_int32_t ru_a, ru_b;
125120639Sume	u_int32_t ru_g;
126120639Sume	long ru_reseed;
127120639Sume};
128120639Sume
129120639Sumestatic struct randomtab randomtab_32 = {
130120639Sume	32,			/* resulting bits */
131120639Sume	180,			/* Time after wich will be reseeded */
132120639Sume	1000000000,		/* Uniq cycle, avoid blackjack prediction */
133120639Sume	2,			/* Starting generator */
134120639Sume	2147483629,		/* RU_N-1 = 2^2*3^2*59652323 */
135120639Sume	7,			/* determine ru_a as RU_AGEN^(2*rand) */
136120639Sume	1836660096,		/* RU_M = 2^7*3^15 - don't change */
137120639Sume	{ 2, 3, 59652323, 0 },	/* factors of ru_n */
138120639Sume};
139120639Sume
140120639Sumestatic u_int32_t pmod(u_int32_t, u_int32_t, u_int32_t);
141120639Sumestatic void initid(struct randomtab *);
142120639Sumestatic u_int32_t randomid(struct randomtab *);
143120639Sume
144120639Sume/*
145120639Sume * Do a fast modular exponation, returned value will be in the range
146120639Sume * of 0 - (mod-1)
147120639Sume */
148120639Sume
149120639Sumestatic u_int32_t
150120639Sumepmod(u_int32_t gen, u_int32_t expo, u_int32_t mod)
151120639Sume{
152120639Sume	u_int64_t s, t, u;
153120639Sume
154120639Sume	s = 1;
155120639Sume	t = gen;
156120639Sume	u = expo;
157120639Sume
158120639Sume	while (u) {
159120639Sume		if (u & 1)
160120639Sume			s = (s * t) % mod;
161120639Sume		u >>= 1;
162120639Sume		t = (t * t) % mod;
163120639Sume	}
164120639Sume	return (s);
165120639Sume}
166120639Sume
167120639Sume/*
168120639Sume * Initalizes the seed and chooses a suitable generator. Also toggles
169120639Sume * the msb flag. The msb flag is used to generate two distinct
170120639Sume * cycles of random numbers and thus avoiding reuse of ids.
171120639Sume *
172120639Sume * This function is called from id_randomid() when needed, an
173120639Sume * application does not have to worry about it.
174120639Sume */
175120639Sumestatic void
176120639Sumeinitid(struct randomtab *p)
177120639Sume{
178120639Sume	u_int32_t j, i;
179120639Sume	int noprime = 1;
180120639Sume
181120639Sume	p->ru_x = arc4random() % p->ru_m;
182120639Sume
183120639Sume	/* (bits - 1) bits of random seed */
184120639Sume	p->ru_seed = arc4random() & (~0U >> (32 - p->ru_bits + 1));
185120639Sume	p->ru_seed2 = arc4random() & (~0U >> (32 - p->ru_bits + 1));
186120639Sume
187120639Sume	/* Determine the LCG we use */
188120639Sume	p->ru_b = (arc4random() & (~0U >> (32 - p->ru_bits))) | 1;
189120639Sume	p->ru_a = pmod(p->ru_agen,
190120639Sume	    (arc4random() & (~0U >> (32 - p->ru_bits))) & (~1U), p->ru_m);
191120639Sume	while (p->ru_b % 3 == 0)
192120639Sume		p->ru_b += 2;
193120639Sume
194120639Sume	j = arc4random() % p->ru_n;
195120639Sume
196120639Sume	/*
197120639Sume	 * Do a fast gcd(j, RU_N - 1), so we can find a j with
198120639Sume	 * gcd(j, RU_N - 1) == 1, giving a new generator for
199120639Sume	 * RU_GEN^j mod RU_N
200120639Sume	 */
201120639Sume	while (noprime) {
202120639Sume		for (i = 0; p->pfacts[i] > 0; i++)
203120639Sume			if (j % p->pfacts[i] == 0)
204120639Sume				break;
205120639Sume
206120639Sume		if (p->pfacts[i] == 0)
207120639Sume			noprime = 0;
208120639Sume		else
209120639Sume			j = (j + 1) % p->ru_n;
210120639Sume	}
211120639Sume
212120639Sume	p->ru_g = pmod(p->ru_gen, j, p->ru_n);
213120639Sume	p->ru_counter = 0;
214120639Sume
215120639Sume	p->ru_reseed = time_second + p->ru_out;
216120639Sume	p->ru_msb = p->ru_msb ? 0 : (1U << (p->ru_bits - 1));
217120639Sume}
218120639Sume
219120639Sumestatic u_int32_t
220120639Sumerandomid(struct randomtab *p)
221120639Sume{
222120639Sume	int i, n;
223120639Sume	u_int32_t tmp;
224120639Sume
225120639Sume	if (p->ru_counter >= p->ru_max || time_second > p->ru_reseed)
226120639Sume		initid(p);
227120639Sume
228120639Sume	tmp = arc4random();
229120639Sume
230120639Sume	/* Skip a random number of ids */
231120639Sume	n = tmp & 0x3; tmp = tmp >> 2;
232120639Sume	if (p->ru_counter + n >= p->ru_max)
233120639Sume		initid(p);
234120639Sume
235120639Sume	for (i = 0; i <= n; i++) {
236120639Sume		/* Linear Congruential Generator */
237120639Sume		p->ru_x = (u_int32_t)((u_int64_t)p->ru_a * p->ru_x + p->ru_b) % p->ru_m;
238120639Sume	}
239120639Sume
240120639Sume	p->ru_counter += i;
241120639Sume
242120639Sume	return (p->ru_seed ^ pmod(p->ru_g, p->ru_seed2 ^ p->ru_x, p->ru_n)) |
243120639Sume	    p->ru_msb;
244120639Sume}
245120639Sume
246120639Sumeu_int32_t
247120639Sumeip6_randomid(void)
248120639Sume{
249120639Sume
250120639Sume	return randomid(&randomtab_32);
251120639Sume}
252120641Sume
253120649Sumeu_int32_t
254120649Sumeip6_randomflowlabel(void)
255120649Sume{
256120649Sume
257120649Sume	return randomid(&randomtab_20) & 0xfffff;
258120649Sume}
259120649Sume
260120641Sume#endif /* RANDOM_IP_ID */
261