/*
- * Copyright (c) 2009 Apple Inc. All rights reserved.
+ * Copyright (c) 2009-2013 Apple Inc. All rights reserved.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_START@
- *
+ *
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* unlawful or unlicensed copies of an Apple operating system, or to
* circumvent, violate, or enable the circumvention or violation of, any
* terms of an Apple operating system software license agreement.
- *
+ *
* Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this file.
- *
+ *
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
- *
+ *
* @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
- *
- * $KAME: ip6_id.c,v 1.13 2003/09/16 09:11:19 itojun Exp $
*/
/*-
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * $OpenBSD: ip_id.c,v 1.6 2002/03/15 18:19:52 millert Exp $
*/
#include <sys/cdefs.h>
#include <sys/time.h>
#include <sys/kernel.h>
#include <sys/random.h>
+#include <sys/protosw.h>
#include <libkern/libkern.h>
+#include <dev/random/randomdev.h>
#include <net/if.h>
#include <net/route.h>
#include <netinet/ip6.h>
#include <netinet6/ip6_var.h>
-#ifndef INT32_MAX
-#define INT32_MAX 0x7fffffffU
-#endif
-
struct randomtab {
const int ru_bits; /* resulting bits */
const long ru_out; /* Time after wich will be reseeded */
static void
initid(struct randomtab *p)
{
+ time_t curtime = (time_t)net_uptime();
u_int32_t j, i;
int noprime = 1;
- struct timeval timenow;
-
- getmicrotime(&timenow);
- p->ru_x = random() % p->ru_m;
+ p->ru_x = RandomULong() % p->ru_m;
/* (bits - 1) bits of random seed */
- p->ru_seed = random() & (~0U >> (32 - p->ru_bits + 1));
- p->ru_seed2 = random() & (~0U >> (32 - p->ru_bits + 1));
+ p->ru_seed = RandomULong() & (~0U >> (32 - p->ru_bits + 1));
+ p->ru_seed2 = RandomULong() & (~0U >> (32 - p->ru_bits + 1));
/* Determine the LCG we use */
- p->ru_b = (random() & (~0U >> (32 - p->ru_bits))) | 1;
+ p->ru_b = (RandomULong() & (~0U >> (32 - p->ru_bits))) | 1;
p->ru_a = pmod(p->ru_agen,
- (random() & (~0U >> (32 - p->ru_bits))) & (~1U), p->ru_m);
+ (RandomULong() & (~0U >> (32 - p->ru_bits))) & (~1U), p->ru_m);
while (p->ru_b % 3 == 0)
p->ru_b += 2;
- j = random() % p->ru_n;
+ j = RandomULong() % p->ru_n;
/*
* Do a fast gcd(j, RU_N - 1), so we can find a j with
p->ru_g = pmod(p->ru_gen, j, p->ru_n);
p->ru_counter = 0;
- p->ru_reseed = timenow.tv_sec + p->ru_out;
+ p->ru_reseed = curtime + p->ru_out;
p->ru_msb = p->ru_msb ? 0 : (1U << (p->ru_bits - 1));
}
static u_int32_t
randomid(struct randomtab *p)
{
+ time_t curtime = (time_t)net_uptime();
int i, n;
u_int32_t tmp;
- struct timeval timenow;
-
- getmicrotime(&timenow);
- if (p->ru_counter >= p->ru_max || timenow.tv_sec > p->ru_reseed)
+ if (p->ru_counter >= p->ru_max || curtime > p->ru_reseed)
initid(p);
- tmp = random();
+ tmp = RandomULong();
/* Skip a random number of ids */
n = tmp & 0x3; tmp = tmp >> 2;
for (i = 0; i <= n; i++) {
/* Linear Congruential Generator */
- p->ru_x = (u_int32_t)((u_int64_t)p->ru_a * p->ru_x + p->ru_b) % p->ru_m;
+ p->ru_x = ((u_int64_t)p->ru_a * p->ru_x + p->ru_b) % p->ru_m;
}
p->ru_counter += i;
- return (p->ru_seed ^ pmod(p->ru_g, p->ru_seed2 ^ p->ru_x, p->ru_n)) |
- p->ru_msb;
+ return ((p->ru_seed ^ pmod(p->ru_g, p->ru_seed2 ^ p->ru_x, p->ru_n)) |
+ p->ru_msb);
}
u_int32_t
ip6_randomid(void)
{
- return randomid(&randomtab_32);
+ return (randomid(&randomtab_32));
}
u_int32_t
ip6_randomflowlabel(void)
{
- return randomid(&randomtab_20) & 0xfffff;
+ return (randomid(&randomtab_20) & 0xfffff);
}