]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet6/ip6_id.c
xnu-6153.11.26.tar.gz
[apple/xnu.git] / bsd / netinet6 / ip6_id.c
CommitLineData
6d2010ae 1/*
cb323159 2 * Copyright (c) 2009-2019 Apple Inc. All rights reserved.
6d2010ae
A
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
39236c6e 5 *
6d2010ae
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
39236c6e 14 *
6d2010ae
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
39236c6e 17 *
6d2010ae
A
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
39236c6e 25 *
6d2010ae
A
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29/*-
30 * Copyright (C) 2003 WIDE Project.
31 * All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the project nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
6d2010ae
A
56 */
57
58/*-
59 * Copyright 1998 Niels Provos <provos@citi.umich.edu>
60 * All rights reserved.
61 *
62 * Theo de Raadt <deraadt@openbsd.org> came up with the idea of using
63 * such a mathematical system to generate more random (yet non-repeating)
64 * ids to solve the resolver/named problem. But Niels designed the
65 * actual system based on the constraints.
66 *
67 * Redistribution and use in source and binary forms, with or without
68 * modification, are permitted provided that the following conditions
69 * are met:
70 * 1. Redistributions of source code must retain the above copyright
71 * notice, this list of conditions and the following disclaimer.
72 * 2. Redistributions in binary form must reproduce the above copyright
73 * notice, this list of conditions and the following disclaimer in the
74 * documentation and/or other materials provided with the distribution.
75 * 3. All advertising materials mentioning features or use of this software
76 * must display the following acknowledgement:
77 * This product includes software developed by Niels Provos.
78 * 4. The name of the author may not be used to endorse or promote products
79 * derived from this software without specific prior written permission.
80 *
81 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
82 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
83 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
84 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
85 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
86 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
87 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
88 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
89 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
90 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6d2010ae
A
91 */
92
93#include <sys/cdefs.h>
94
95/*
96 * seed = random (bits - 1) bit
97 * n = prime, g0 = generator to n,
98 * j = random so that gcd(j,n-1) == 1
99 * g = g0^j mod n will be a generator again.
100 *
101 * X[0] = random seed.
102 * X[n] = a*X[n-1]+b mod m is a Linear Congruential Generator
103 * with a = 7^(even random) mod m,
104 * b = random with gcd(b,m) == 1
105 * m = constant and a maximal period of m-1.
106 *
107 * The transaction id is determined by:
108 * id[n] = seed xor (g^X[n] mod n)
109 *
110 * Effectivly the id is restricted to the lower (bits - 1) bits, thus
111 * yielding two different cycles by toggling the msb on and off.
112 * This avoids reuse issues caused by reseeding.
113 */
114
115#include <sys/types.h>
116#include <sys/socket.h>
117#include <sys/param.h>
118#include <sys/time.h>
119#include <sys/kernel.h>
120#include <sys/random.h>
39236c6e 121#include <sys/protosw.h>
6d2010ae 122#include <libkern/libkern.h>
39236c6e 123#include <dev/random/randomdev.h>
6d2010ae
A
124
125#include <net/if.h>
126#include <net/route.h>
127#include <netinet/in.h>
128#include <netinet/ip6.h>
129#include <netinet6/ip6_var.h>
130
6d2010ae 131struct randomtab {
0a7de745
A
132 const int ru_bits; /* resulting bits */
133 const long ru_out; /* Time after wich will be reseeded */
134 const u_int32_t ru_max; /* Uniq cycle, avoid blackjack prediction */
135 const u_int32_t ru_gen; /* Starting generator */
136 const u_int32_t ru_n; /* ru_n: prime, ru_n - 1: product of pfacts[] */
6d2010ae 137 const u_int32_t ru_agen; /* determine ru_a as ru_agen^(2*rand) */
0a7de745
A
138 const u_int32_t ru_m; /* ru_m = 2^x*3^y */
139 const u_int32_t pfacts[4]; /* factors of ru_n */
6d2010ae
A
140
141 u_int32_t ru_counter;
142 u_int32_t ru_msb;
143
144 u_int32_t ru_x;
145 u_int32_t ru_seed, ru_seed2;
146 u_int32_t ru_a, ru_b;
147 u_int32_t ru_g;
148 long ru_reseed;
149};
150
151static struct randomtab randomtab_32 = {
cb323159
A
152 .ru_bits = 32, /* resulting bits */
153 .ru_out = 180, /* Time after wich will be reseeded */
154 .ru_max = 1000000000, /* Uniq cycle, avoid blackjack prediction */
155 .ru_gen = 2, /* Starting generator */
156 .ru_n = 2147483629, /* RU_N-1 = 2^2*3^2*59652323 */
157 .ru_agen = 7, /* determine ru_a as RU_AGEN^(2*rand) */
158 .ru_m = 1836660096, /* RU_M = 2^7*3^15 - don't change */
159 .pfacts = { 2, 3, 59652323, 0 }, /* factors of ru_n */
160 .ru_counter = 0,
161 .ru_msb = 0,
162 .ru_x = 0,
163 .ru_seed = 0,
164 .ru_seed2 = 0,
165 .ru_a = 0,
166 .ru_b = 0,
167 .ru_g = 0,
168 .ru_reseed = 0
6d2010ae
A
169};
170
171static struct randomtab randomtab_20 = {
cb323159
A
172 .ru_bits = 20, /* resulting bits */
173 .ru_out = 180, /* Time after wich will be reseeded */
174 .ru_max = 200000, /* Uniq cycle, avoid blackjack prediction */
175 .ru_gen = 2, /* Starting generator */
176 .ru_n = 524269, /* RU_N-1 = 2^2*3^2*14563 */
177 .ru_agen = 7, /* determine ru_a as RU_AGEN^(2*rand) */
178 .ru_m = 279936, /* RU_M = 2^7*3^7 - don't change */
179 .pfacts = { 2, 3, 14563, 0 }, /* factors of ru_n */
180 .ru_counter = 0,
181 .ru_msb = 0,
182 .ru_x = 0,
183 .ru_seed = 0,
184 .ru_seed2 = 0,
185 .ru_a = 0,
186 .ru_b = 0,
187 .ru_g = 0,
188 .ru_reseed = 0
6d2010ae
A
189};
190
191static u_int32_t pmod(u_int32_t, u_int32_t, u_int32_t);
192static void initid(struct randomtab *);
193static u_int32_t randomid(struct randomtab *);
194
195/*
196 * Do a fast modular exponation, returned value will be in the range
197 * of 0 - (mod-1)
198 */
199static u_int32_t
200pmod(u_int32_t gen, u_int32_t expo, u_int32_t mod)
201{
202 u_int64_t s, t, u;
203
204 s = 1;
205 t = gen;
206 u = expo;
207
208 while (u) {
0a7de745 209 if (u & 1) {
6d2010ae 210 s = (s * t) % mod;
0a7de745 211 }
6d2010ae
A
212 u >>= 1;
213 t = (t * t) % mod;
214 }
0a7de745 215 return s;
6d2010ae
A
216}
217
218/*
219 * Initalizes the seed and chooses a suitable generator. Also toggles
220 * the msb flag. The msb flag is used to generate two distinct
221 * cycles of random numbers and thus avoiding reuse of ids.
222 *
223 * This function is called from id_randomid() when needed, an
224 * application does not have to worry about it.
225 */
226static void
227initid(struct randomtab *p)
228{
39236c6e 229 time_t curtime = (time_t)net_uptime();
6d2010ae
A
230 u_int32_t j, i;
231 int noprime = 1;
6d2010ae 232
39236c6e 233 p->ru_x = RandomULong() % p->ru_m;
6d2010ae
A
234
235 /* (bits - 1) bits of random seed */
39236c6e
A
236 p->ru_seed = RandomULong() & (~0U >> (32 - p->ru_bits + 1));
237 p->ru_seed2 = RandomULong() & (~0U >> (32 - p->ru_bits + 1));
6d2010ae
A
238
239 /* Determine the LCG we use */
39236c6e 240 p->ru_b = (RandomULong() & (~0U >> (32 - p->ru_bits))) | 1;
6d2010ae 241 p->ru_a = pmod(p->ru_agen,
39236c6e 242 (RandomULong() & (~0U >> (32 - p->ru_bits))) & (~1U), p->ru_m);
0a7de745 243 while (p->ru_b % 3 == 0) {
6d2010ae 244 p->ru_b += 2;
0a7de745 245 }
6d2010ae 246
39236c6e 247 j = RandomULong() % p->ru_n;
6d2010ae
A
248
249 /*
250 * Do a fast gcd(j, RU_N - 1), so we can find a j with
251 * gcd(j, RU_N - 1) == 1, giving a new generator for
252 * RU_GEN^j mod RU_N
253 */
254 while (noprime) {
0a7de745
A
255 for (i = 0; p->pfacts[i] > 0; i++) {
256 if (j % p->pfacts[i] == 0) {
6d2010ae 257 break;
0a7de745
A
258 }
259 }
6d2010ae 260
0a7de745 261 if (p->pfacts[i] == 0) {
6d2010ae 262 noprime = 0;
0a7de745 263 } else {
6d2010ae 264 j = (j + 1) % p->ru_n;
0a7de745 265 }
6d2010ae
A
266 }
267
268 p->ru_g = pmod(p->ru_gen, j, p->ru_n);
269 p->ru_counter = 0;
270
39236c6e 271 p->ru_reseed = curtime + p->ru_out;
6d2010ae
A
272 p->ru_msb = p->ru_msb ? 0 : (1U << (p->ru_bits - 1));
273}
274
275static u_int32_t
276randomid(struct randomtab *p)
277{
39236c6e 278 time_t curtime = (time_t)net_uptime();
6d2010ae
A
279 int i, n;
280 u_int32_t tmp;
6d2010ae 281
0a7de745 282 if (p->ru_counter >= p->ru_max || curtime > p->ru_reseed) {
6d2010ae 283 initid(p);
0a7de745 284 }
6d2010ae 285
39236c6e 286 tmp = RandomULong();
6d2010ae
A
287
288 /* Skip a random number of ids */
289 n = tmp & 0x3; tmp = tmp >> 2;
0a7de745 290 if (p->ru_counter + n >= p->ru_max) {
6d2010ae 291 initid(p);
0a7de745 292 }
6d2010ae
A
293
294 for (i = 0; i <= n; i++) {
295 /* Linear Congruential Generator */
39236c6e 296 p->ru_x = ((u_int64_t)p->ru_a * p->ru_x + p->ru_b) % p->ru_m;
6d2010ae
A
297 }
298
299 p->ru_counter += i;
300
0a7de745
A
301 return (p->ru_seed ^ pmod(p->ru_g, p->ru_seed2 ^ p->ru_x, p->ru_n)) |
302 p->ru_msb;
6d2010ae
A
303}
304
305u_int32_t
306ip6_randomid(void)
307{
0a7de745 308 return randomid(&randomtab_32);
6d2010ae
A
309}
310
311u_int32_t
312ip6_randomflowlabel(void)
313{
0a7de745 314 return randomid(&randomtab_20) & 0xfffff;
6d2010ae 315}