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 (__Userspace_os_Windows)
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 static int
read_random_phony(void * buf,int count)69 read_random_phony(void *buf, int count)
70 {
71 	memset(buf, 'A', count);
72 	return (count);
73 }
74 #else
75 #if defined(__Userspace_os_FreeBSD) || defined(__Userspace_os_Darwin)
76 static int
read_random_phony(void * buf,int count)77 read_random_phony(void *buf, int count)
78 {
79 	if (count >= 0) {
80 		arc4random_buf(buf, count);
81 	}
82 	return (count);
83 }
84 #else
85 static int
read_random_phony(void * buf,int count)86 read_random_phony(void *buf, int count)
87 {
88 	uint32_t randval;
89 	int size, i;
90 
91 	/* srandom() is called in kern/init_main.c:proc0_post() */
92 
93 	/* Fill buf[] with random(9) output */
94 	for (i = 0; i < count; i+= (int)sizeof(uint32_t)) {
95 		randval = random();
96 		size = MIN(count - i, (int)sizeof(uint32_t));
97 		memcpy(&((char *)buf)[i], &randval, (size_t)size);
98 	}
99 
100 	return (count);
101 }
102 #endif
103 #endif
104 
105 static int (*read_func)(void *, int) = read_random_phony;
106 
107 /* Userland-visible version of read_random */
108 int
read_random(void * buf,int count)109 read_random(void *buf, int count)
110 {
111 	return ((*read_func)(buf, count));
112 }
113 
114