X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/bd504ef0e0b883cdd7917b73b3574eb9ce669905..39236c6e673c41db228275375ab7fdb0f837b292:/bsd/netinet6/ip6_id.c diff --git a/bsd/netinet6/ip6_id.c b/bsd/netinet6/ip6_id.c index 26fffd286..eab70a6c0 100644 --- a/bsd/netinet6/ip6_id.c +++ b/bsd/netinet6/ip6_id.c @@ -1,8 +1,8 @@ /* - * 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 @@ -11,10 +11,10 @@ * 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, @@ -22,7 +22,7 @@ * 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@ */ @@ -53,8 +53,6 @@ * 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 $ */ /*- @@ -90,8 +88,6 @@ * 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 @@ -122,7 +118,9 @@ #include #include #include +#include #include +#include #include #include @@ -130,10 +128,6 @@ #include #include -#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 */ @@ -215,26 +209,24 @@ pmod(u_int32_t gen, u_int32_t expo, u_int32_t mod) 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 @@ -255,23 +247,21 @@ initid(struct randomtab *p) 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; @@ -280,25 +270,25 @@ randomid(struct randomtab *p) 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); }