]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/random/random_adaptors.c
Update to OpenPAM Nummularia.
[FreeBSD/FreeBSD.git] / sys / dev / random / random_adaptors.c
1 /*-
2  * Copyright (c) 2013 Arthur Mesh <arthurmesh@gmail.com>
3  * Copyright (c) 2013 David E. O'Brien <obrien@NUXI.org>
4  * Copyright (c) 2004 Mark R V Murray
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in this position and unchanged.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <sys/param.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
34 #include <sys/kthread.h>
35 #include <sys/lock.h>
36 #include <sys/random.h>
37 #include <sys/selinfo.h>
38 #include <sys/sysctl.h>
39 #include <sys/sx.h>
40 #include <sys/malloc.h>
41 #include <sys/queue.h>
42 #include <sys/libkern.h>
43 #include <sys/unistd.h>
44
45 #include <dev/random/randomdev.h>
46 #include <dev/random/randomdev_soft.h>
47 #include <dev/random/random_adaptors.h>
48
49 LIST_HEAD(adaptors_head, random_adaptors);
50 static struct adaptors_head adaptors = LIST_HEAD_INITIALIZER(adaptors);
51 static struct sx adaptors_lock; /* need a sleepable lock */
52
53 /* List for the dynamic sysctls */
54 static struct sysctl_ctx_list random_clist;
55
56 MALLOC_DEFINE(M_RANDOM_ADAPTORS, "random_adaptors", "Random adaptors buffers");
57
58 int
59 random_adaptor_register(const char *name, struct random_adaptor *rsp)
60 {
61         struct random_adaptors *rpp;
62
63         KASSERT(name != NULL && rsp != NULL, ("invalid input to %s", __func__));
64
65         rpp = malloc(sizeof(struct random_adaptors), M_RANDOM_ADAPTORS,
66             M_WAITOK);
67         rpp->name = name;
68         rpp->rsp = rsp;
69
70         sx_xlock(&adaptors_lock);
71         LIST_INSERT_HEAD(&adaptors, rpp, entries);
72         sx_xunlock(&adaptors_lock);
73
74         return (0);
75 }
76
77 struct random_adaptor *
78 random_adaptor_get(const char *name)
79 {
80         struct random_adaptors  *rpp;
81         struct random_adaptor   *rsp;
82
83         rsp = NULL;
84
85         sx_slock(&adaptors_lock);
86
87         LIST_FOREACH(rpp, &adaptors, entries)
88                 if (strcmp(rpp->name, name) == 0)
89                         rsp = rpp->rsp;
90
91         sx_sunlock(&adaptors_lock);
92
93         return (rsp);
94 }
95
96 /*
97  * In the past, the logic of the random_adaptor selection was inverted, such
98  * that hardware RNGs would be chosen unless disabled. This routine is here to
99  * preserve that functionality to avoid folks losing their hardware RNGs by
100  * upgrading to newer kernel.
101  */
102 static void
103 random_adaptor_choose_legacy(struct random_adaptor **adaptor)
104 {
105         struct random_adaptor *tmp;
106         int enable;
107
108         /* Then go looking for hardware */
109         enable = 1;
110         TUNABLE_INT_FETCH("hw.nehemiah_rng_enable", &enable);
111         if (enable && (tmp = random_adaptor_get("nehemiah")))
112                 *adaptor = tmp;
113
114         enable = 1;
115         TUNABLE_INT_FETCH("hw.ivy_rng_enable", &enable);
116         if (enable && (tmp = random_adaptor_get("rdrand")))
117                 *adaptor = tmp;
118 }
119
120 /*
121  * Walk a list of registered random(4) adaptors and pick the last non-selected
122  * one.
123  *
124  * If none are selected, use yarrow if available.
125  */
126 void
127 random_adaptor_choose(struct random_adaptor **adaptor)
128 {
129         char                     rngs[128], *token, *cp;
130         struct random_adaptors  *rpp;
131
132         KASSERT(adaptor != NULL, ("pre-conditions failed"));
133
134         *adaptor = NULL;
135
136         random_adaptor_choose_legacy(adaptor);
137
138         if (*adaptor != NULL)
139                 return;
140
141         if (TUNABLE_STR_FETCH("rngs_want", rngs, sizeof(rngs))) {
142                 cp = rngs;
143
144                 while ((token = strsep(&cp, ",")) != NULL) {
145                         if ((*adaptor = random_adaptor_get(token)) != NULL)
146                                 break;
147                         else if (bootverbose)
148                                 printf(
149                             "%s random adaptor is not available, skipping\n",
150                                     token);
151                 }
152         }
153
154         if (*adaptor == NULL) {
155                 /*
156                  * Either no RNGs are prefered via rngs_want tunable, or
157                  * no prefered RNGs are registered.
158                  * Fallback to Yarrow.
159                  */
160                 *adaptor = random_adaptor_get("yarrow");
161
162                 if (*adaptor == NULL) {
163                         /*
164                          * Yarrow doesn't seem to be available.
165                          * Fallback to the first thing that's on the list of
166                          * available RNGs.
167                          */
168                         sx_slock(&adaptors_lock);
169
170                         rpp = LIST_FIRST(&adaptors);
171                         if (rpp != NULL)
172                                 *adaptor = rpp->rsp;
173
174                         sx_sunlock(&adaptors_lock);
175                 }
176
177                 if (bootverbose && *adaptor)
178                         printf("Falling back to <%s> random adaptor",
179                             (*adaptor)->ident);
180         }
181 }
182
183 static void
184 random_adaptors_deinit(void *unused)
185 {
186
187         sx_destroy(&adaptors_lock);
188         sysctl_ctx_free(&random_clist);
189 }
190
191 static int
192 random_sysctl_adaptors_handler(SYSCTL_HANDLER_ARGS)
193 {
194         struct random_adaptors  *rpp;
195         int error, count;
196
197         count = error = 0;
198
199         sx_slock(&adaptors_lock);
200
201         if (LIST_EMPTY(&adaptors)) {
202                 error = SYSCTL_OUT(req, "", 0);
203         } else {
204
205                 LIST_FOREACH(rpp, &adaptors, entries) {
206
207                         error = SYSCTL_OUT(req, ",", count++ ? 1 : 0);
208
209                         if (error)
210                                 break;
211
212                         error = SYSCTL_OUT(req, rpp->name, strlen(rpp->name));
213
214                         if (error)
215                                 break;
216                 }
217         }
218
219         sx_sunlock(&adaptors_lock);
220
221         return (error);
222 }
223
224 static int
225 random_sysctl_active_adaptor_handler(SYSCTL_HANDLER_ARGS)
226 {
227         struct random_adaptor   *rsp;
228         struct random_adaptors  *rpp;
229         const char              *name;
230         int error;
231
232         name = NULL;
233         rsp = random_get_active_adaptor();
234
235         if (rsp != NULL) {
236                 sx_slock(&adaptors_lock);
237
238                 LIST_FOREACH(rpp, &adaptors, entries) {
239                         if (rpp->rsp == rsp)
240                                 name = rpp->name;
241                 }
242
243                 sx_sunlock(&adaptors_lock);
244         }
245
246         if (rsp == NULL || name == NULL) {
247                 error = SYSCTL_OUT(req, "", 0);
248         } else {
249                 error = SYSCTL_OUT(req, name, strlen(name));
250         }
251
252         return (error);
253 }
254
255 static void
256 random_adaptors_init(void *unused)
257 {
258
259         SYSCTL_PROC(_kern_random, OID_AUTO, adaptors,
260             CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
261             NULL, 0, random_sysctl_adaptors_handler, "",
262             "Random Number Generator adaptors");
263
264         SYSCTL_PROC(_kern_random, OID_AUTO, active_adaptor,
265             CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
266             NULL, 0, random_sysctl_active_adaptor_handler, "",
267             "Active Random Number Generator Adaptor");
268
269         sx_init(&adaptors_lock, "random_adaptors");
270 }
271
272 SYSCTL_NODE(_kern, OID_AUTO, random, CTLFLAG_RW, 0, "Random Number Generator");
273
274 SYSINIT(random_adaptors, SI_SUB_DRIVERS, SI_ORDER_FIRST, random_adaptors_init,
275     NULL);
276 SYSUNINIT(random_adaptors, SI_SUB_DRIVERS, SI_ORDER_FIRST,
277     random_adaptors_deinit, NULL);