• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-
2  * Copyright (c) 2009-2010 Brad Penoff
3  * Copyright (c) 2009-2010 Humaira Kamal
4  * Copyright (c) 2011-2012 Irene Ruengeler
5  * Copyright (c) 2011-2012 Michael Tuexen
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 /* __Userspace__ */
32 
33 #include <stdlib.h>
34 #if !defined(_WIN32)
35 #include <stdint.h>
36 #include <netinet/sctp_os_userspace.h>
37 #endif
38 #include <user_environment.h>
39 #include <sys/types.h>
40 /* #include <sys/param.h> defines MIN */
41 #if !defined(MIN)
42 #define MIN(arg1,arg2) ((arg1) < (arg2) ? (arg1) : (arg2))
43 #endif
44 #include <string.h>
45 
46 #define uHZ 1000
47 
48 /* See user_include/user_environment.h for comments about these variables */
49 int maxsockets = 25600;
50 int hz = uHZ;
51 int ip_defttl = 64;
52 int ipport_firstauto = 49152, ipport_lastauto = 65535;
53 int nmbclusters = 65536;
54 
55 /* Source ip_output.c. extern'd in ip_var.h */
56 u_short ip_id = 0; /*__Userspace__ TODO Should it be initialized to zero? */
57 
58 /* used in user_include/user_atomic.h in order to make the operations
59  * defined there truly atomic
60  */
61 userland_mutex_t atomic_mtx;
62 
63 /* If the entropy device is not loaded, make a token effort to
64  * provide _some_ kind of randomness. This should only be used
65  * inside other RNG's, like arc4random(9).
66  */
67 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
68 void
init_random(void)69 init_random(void)
70 {
71 	return;
72 }
73 
74 int
read_random(void * buf,int count)75 read_random(void *buf, int count)
76 {
77 	memset(buf, 'A', count);
78 	return (count);
79 }
80 #elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
81 void
init_random(void)82 init_random(void)
83 {
84 	return;
85 }
86 
87 int
read_random(void * buf,int count)88 read_random(void *buf, int count)
89 {
90 	if (count >= 0) {
91 		arc4random_buf(buf, count);
92 	}
93 	return (count);
94 }
95 #else
96 void
init_random(void)97 init_random(void)
98 {
99 	struct timeval now;
100 	unsigned int seed;
101 
102 	(void)SCTP_GETTIME_TIMEVAL(&now);
103 	seed = 0;
104 	seed |= (unsigned int)now.tv_sec;
105 	seed |= (unsigned int)now.tv_usec;
106 #if !defined(_WIN32) &&! defined(__native_client__)
107 	seed |= getpid();
108 #endif
109 #if defined(_WIN32) || defined(__native_client__)
110 	srand(seed);
111 #else
112 	srandom(seed);
113 #endif
114 	return;
115 }
116 
117 int
read_random(void * buf,int count)118 read_random(void *buf, int count)
119 {
120 	uint32_t randval;
121 	int size, i;
122 
123 	/* Fill buf[] with random(9) output */
124 	for (i = 0; i < count; i+= (int)sizeof(uint32_t)) {
125 		randval = random();
126 		size = MIN(count - i, (int)sizeof(uint32_t));
127 		memcpy(&((char *)buf)[i], &randval, (size_t)size);
128 	}
129 
130 	return (count);
131 }
132 #endif
133