]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/ip6_id.c
2 * Copyright (c) 2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (C) 2003 WIDE Project.
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
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.
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
57 * $KAME: ip6_id.c,v 1.13 2003/09/16 09:11:19 itojun Exp $
61 * Copyright 1998 Niels Provos <provos@citi.umich.edu>
62 * All rights reserved.
64 * Theo de Raadt <deraadt@openbsd.org> came up with the idea of using
65 * such a mathematical system to generate more random (yet non-repeating)
66 * ids to solve the resolver/named problem. But Niels designed the
67 * actual system based on the constraints.
69 * Redistribution and use in source and binary forms, with or without
70 * modification, are permitted provided that the following conditions
72 * 1. Redistributions of source code must retain the above copyright
73 * notice, this list of conditions and the following disclaimer.
74 * 2. Redistributions in binary form must reproduce the above copyright
75 * notice, this list of conditions and the following disclaimer in the
76 * documentation and/or other materials provided with the distribution.
77 * 3. All advertising materials mentioning features or use of this software
78 * must display the following acknowledgement:
79 * This product includes software developed by Niels Provos.
80 * 4. The name of the author may not be used to endorse or promote products
81 * derived from this software without specific prior written permission.
83 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
84 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
85 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
86 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
87 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
88 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
89 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
90 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
91 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
92 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
94 * $OpenBSD: ip_id.c,v 1.6 2002/03/15 18:19:52 millert Exp $
97 #include <sys/cdefs.h>
100 * seed = random (bits - 1) bit
101 * n = prime, g0 = generator to n,
102 * j = random so that gcd(j,n-1) == 1
103 * g = g0^j mod n will be a generator again.
105 * X[0] = random seed.
106 * X[n] = a*X[n-1]+b mod m is a Linear Congruential Generator
107 * with a = 7^(even random) mod m,
108 * b = random with gcd(b,m) == 1
109 * m = constant and a maximal period of m-1.
111 * The transaction id is determined by:
112 * id[n] = seed xor (g^X[n] mod n)
114 * Effectivly the id is restricted to the lower (bits - 1) bits, thus
115 * yielding two different cycles by toggling the msb on and off.
116 * This avoids reuse issues caused by reseeding.
119 #include <sys/types.h>
120 #include <sys/socket.h>
121 #include <sys/param.h>
122 #include <sys/time.h>
123 #include <sys/kernel.h>
124 #include <sys/random.h>
125 #include <libkern/libkern.h>
128 #include <net/route.h>
129 #include <netinet/in.h>
130 #include <netinet/ip6.h>
131 #include <netinet6/ip6_var.h>
134 #define INT32_MAX 0x7fffffffU
138 const int ru_bits
; /* resulting bits */
139 const long ru_out
; /* Time after wich will be reseeded */
140 const u_int32_t ru_max
; /* Uniq cycle, avoid blackjack prediction */
141 const u_int32_t ru_gen
; /* Starting generator */
142 const u_int32_t ru_n
; /* ru_n: prime, ru_n - 1: product of pfacts[] */
143 const u_int32_t ru_agen
; /* determine ru_a as ru_agen^(2*rand) */
144 const u_int32_t ru_m
; /* ru_m = 2^x*3^y */
145 const u_int32_t pfacts
[4]; /* factors of ru_n */
147 u_int32_t ru_counter
;
151 u_int32_t ru_seed
, ru_seed2
;
152 u_int32_t ru_a
, ru_b
;
157 static struct randomtab randomtab_32
= {
158 32, /* resulting bits */
159 180, /* Time after wich will be reseeded */
160 1000000000, /* Uniq cycle, avoid blackjack prediction */
161 2, /* Starting generator */
162 2147483629, /* RU_N-1 = 2^2*3^2*59652323 */
163 7, /* determine ru_a as RU_AGEN^(2*rand) */
164 1836660096, /* RU_M = 2^7*3^15 - don't change */
165 { 2, 3, 59652323, 0 }, /* factors of ru_n */
166 0, 0, 0, 0, 0, 0, 0, 0, 0
169 static struct randomtab randomtab_20
= {
170 20, /* resulting bits */
171 180, /* Time after wich will be reseeded */
172 200000, /* Uniq cycle, avoid blackjack prediction */
173 2, /* Starting generator */
174 524269, /* RU_N-1 = 2^2*3^2*14563 */
175 7, /* determine ru_a as RU_AGEN^(2*rand) */
176 279936, /* RU_M = 2^7*3^7 - don't change */
177 { 2, 3, 14563, 0 }, /* factors of ru_n */
178 0, 0, 0, 0, 0, 0, 0, 0, 0
181 static u_int32_t
pmod(u_int32_t
, u_int32_t
, u_int32_t
);
182 static void initid(struct randomtab
*);
183 static u_int32_t
randomid(struct randomtab
*);
186 * Do a fast modular exponation, returned value will be in the range
190 pmod(u_int32_t gen
, u_int32_t expo
, u_int32_t mod
)
208 * Initalizes the seed and chooses a suitable generator. Also toggles
209 * the msb flag. The msb flag is used to generate two distinct
210 * cycles of random numbers and thus avoiding reuse of ids.
212 * This function is called from id_randomid() when needed, an
213 * application does not have to worry about it.
216 initid(struct randomtab
*p
)
220 struct timeval timenow
;
222 getmicrotime(&timenow
);
224 p
->ru_x
= random() % p
->ru_m
;
226 /* (bits - 1) bits of random seed */
227 p
->ru_seed
= random() & (~0U >> (32 - p
->ru_bits
+ 1));
228 p
->ru_seed2
= random() & (~0U >> (32 - p
->ru_bits
+ 1));
230 /* Determine the LCG we use */
231 p
->ru_b
= (random() & (~0U >> (32 - p
->ru_bits
))) | 1;
232 p
->ru_a
= pmod(p
->ru_agen
,
233 (random() & (~0U >> (32 - p
->ru_bits
))) & (~1U), p
->ru_m
);
234 while (p
->ru_b
% 3 == 0)
237 j
= random() % p
->ru_n
;
240 * Do a fast gcd(j, RU_N - 1), so we can find a j with
241 * gcd(j, RU_N - 1) == 1, giving a new generator for
245 for (i
= 0; p
->pfacts
[i
] > 0; i
++)
246 if (j
% p
->pfacts
[i
] == 0)
249 if (p
->pfacts
[i
] == 0)
252 j
= (j
+ 1) % p
->ru_n
;
255 p
->ru_g
= pmod(p
->ru_gen
, j
, p
->ru_n
);
258 p
->ru_reseed
= timenow
.tv_sec
+ p
->ru_out
;
259 p
->ru_msb
= p
->ru_msb
? 0 : (1U << (p
->ru_bits
- 1));
263 randomid(struct randomtab
*p
)
267 struct timeval timenow
;
269 getmicrotime(&timenow
);
271 if (p
->ru_counter
>= p
->ru_max
|| timenow
.tv_sec
> p
->ru_reseed
)
276 /* Skip a random number of ids */
277 n
= tmp
& 0x3; tmp
= tmp
>> 2;
278 if (p
->ru_counter
+ n
>= p
->ru_max
)
281 for (i
= 0; i
<= n
; i
++) {
282 /* Linear Congruential Generator */
283 p
->ru_x
= (u_int32_t
)((u_int64_t
)p
->ru_a
* p
->ru_x
+ p
->ru_b
) % p
->ru_m
;
288 return (p
->ru_seed
^ pmod(p
->ru_g
, p
->ru_seed2
^ p
->ru_x
, p
->ru_n
)) |
296 return randomid(&randomtab_32
);
300 ip6_randomflowlabel(void)
303 return randomid(&randomtab_20
) & 0xfffff;