• 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 (__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 /* Source: /usr/src/sys/dev/random/harvest.c */
64 static int read_random_phony(void *, int);
65 
66 static int (*read_func)(void *, int) = read_random_phony;
67 
68 /* Userland-visible version of read_random */
69 int
read_random(void * buf,int count)70 read_random(void *buf, int count)
71 {
72 	return ((*read_func)(buf, count));
73 }
74 
75 /* If the entropy device is not loaded, make a token effort to
76  * provide _some_ kind of randomness. This should only be used
77  * inside other RNG's, like arc4random(9).
78  */
79 static int
read_random_phony(void * buf,int count)80 read_random_phony(void *buf, int count)
81 {
82 	uint32_t randval;
83 	int size, i;
84 
85 	/* srandom() is called in kern/init_main.c:proc0_post() */
86 
87 	/* Fill buf[] with random(9) output */
88 	for (i = 0; i < count; i+= (int)sizeof(uint32_t)) {
89 		randval = random();
90 		size = MIN(count - i, (int)sizeof(uint32_t));
91 		memcpy(&((char *)buf)[i], &randval, (size_t)size);
92 	}
93 
94 	return (count);
95 }
96 
97