]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/random/random_adaptors.c
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.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 struct random_adaptor *random_adaptor;
57
58 MALLOC_DEFINE(M_RANDOM_ADAPTORS, "random_adaptors", "Random adaptors buffers");
59
60 int
61 random_adaptor_register(const char *name, struct random_adaptor *rsp)
62 {
63         struct random_adaptors *rpp;
64
65         KASSERT(name != NULL && rsp != NULL, ("invalid input to %s", __func__));
66
67         rpp = malloc(sizeof(struct random_adaptors), M_RANDOM_ADAPTORS,
68             M_WAITOK);
69         rpp->name = name;
70         rpp->rsp = rsp;
71
72         sx_xlock(&adaptors_lock);
73         LIST_INSERT_HEAD(&adaptors, rpp, entries);
74         sx_xunlock(&adaptors_lock);
75
76         return (0);
77 }
78
79 struct random_adaptor *
80 random_adaptor_get(const char *name)
81 {
82         struct random_adaptors  *rpp;
83         struct random_adaptor   *rsp;
84
85         rsp = NULL;
86
87         sx_slock(&adaptors_lock);
88
89         LIST_FOREACH(rpp, &adaptors, entries)
90                 if (strcmp(rpp->name, name) == 0)
91                         rsp = rpp->rsp;
92
93         sx_sunlock(&adaptors_lock);
94
95         return (rsp);
96 }
97
98 /*
99  * In the past, the logic of the random_adaptor selection was inverted, such
100  * that hardware RNGs would be chosen unless disabled. This routine is here to
101  * preserve that functionality to avoid folks losing their hardware RNGs by
102  * upgrading to newer kernel.
103  */
104 static void
105 random_adaptor_choose_legacy(struct random_adaptor **adaptor)
106 {
107         struct random_adaptor *tmp;
108         int enable;
109
110         /* Then go looking for hardware */
111         enable = 1;
112         TUNABLE_INT_FETCH("hw.nehemiah_rng_enable", &enable);
113         if (enable && (tmp = random_adaptor_get("nehemiah")))
114                 *adaptor = tmp;
115
116         enable = 1;
117         TUNABLE_INT_FETCH("hw.ivy_rng_enable", &enable);
118         if (enable && (tmp = random_adaptor_get("rdrand")))
119                 *adaptor = tmp;
120 }
121
122 /*
123  * Walk a list of registered random(4) adaptors and pick the last non-selected
124  * one.
125  *
126  * If none are selected, use yarrow if available.
127  */
128 void
129 random_adaptor_choose(struct random_adaptor **adaptor)
130 {
131         char                     rngs[128], *token, *cp;
132         struct random_adaptors  *rpp;
133
134         KASSERT(adaptor != NULL, ("pre-conditions failed"));
135
136         *adaptor = NULL;
137
138         random_adaptor_choose_legacy(adaptor);
139
140         if (*adaptor != NULL)
141                 return;
142
143         if (TUNABLE_STR_FETCH("rngs_want", rngs, sizeof(rngs))) {
144                 cp = rngs;
145
146                 while ((token = strsep(&cp, ",")) != NULL) {
147                         if ((*adaptor = random_adaptor_get(token)) != NULL)
148                                 break;
149                         else if (bootverbose)
150                                 printf(
151                             "%s random adaptor is not available, skipping\n",
152                                     token);
153                 }
154         }
155
156         if (*adaptor == NULL) {
157                 /*
158                  * Either no RNGs are prefered via rngs_want tunable, or
159                  * no prefered RNGs are registered.
160                  * Fallback to Yarrow.
161                  */
162                 *adaptor = random_adaptor_get("yarrow");
163
164                 if (*adaptor == NULL) {
165                         /*
166                          * Yarrow doesn't seem to be available.
167                          * Fallback to the first thing that's on the list of
168                          * available RNGs.
169                          */
170                         sx_slock(&adaptors_lock);
171
172                         rpp = LIST_FIRST(&adaptors);
173                         if (rpp != NULL)
174                                 *adaptor = rpp->rsp;
175
176                         sx_sunlock(&adaptors_lock);
177                 }
178
179                 if (bootverbose && *adaptor)
180                         printf("Falling back to <%s> random adaptor\n",
181                             (*adaptor)->ident);
182         }
183 }
184
185 static void
186 random_adaptors_deinit(void *unused)
187 {
188
189         sx_destroy(&adaptors_lock);
190         sysctl_ctx_free(&random_clist);
191 }
192
193 static int
194 random_sysctl_adaptors_handler(SYSCTL_HANDLER_ARGS)
195 {
196         struct random_adaptors  *rpp;
197         int error, count;
198
199         count = error = 0;
200
201         sx_slock(&adaptors_lock);
202
203         if (LIST_EMPTY(&adaptors)) {
204                 error = SYSCTL_OUT(req, "", 0);
205         } else {
206
207                 LIST_FOREACH(rpp, &adaptors, entries) {
208
209                         error = SYSCTL_OUT(req, ",", count++ ? 1 : 0);
210
211                         if (error)
212                                 break;
213
214                         error = SYSCTL_OUT(req, rpp->name, strlen(rpp->name));
215
216                         if (error)
217                                 break;
218                 }
219         }
220
221         sx_sunlock(&adaptors_lock);
222
223         return (error);
224 }
225
226 static int
227 random_sysctl_active_adaptor_handler(SYSCTL_HANDLER_ARGS)
228 {
229         struct random_adaptor   *rsp;
230         struct random_adaptors  *rpp;
231         const char              *name;
232         int error;
233
234         name = NULL;
235         rsp = random_adaptor;
236
237         if (rsp != NULL) {
238                 sx_slock(&adaptors_lock);
239
240                 LIST_FOREACH(rpp, &adaptors, entries) {
241                         if (rpp->rsp == rsp)
242                                 name = rpp->name;
243                 }
244
245                 sx_sunlock(&adaptors_lock);
246         }
247
248         if (rsp == NULL || name == NULL) {
249                 error = SYSCTL_OUT(req, "", 0);
250         } else {
251                 error = SYSCTL_OUT(req, name, strlen(name));
252         }
253
254         return (error);
255 }
256
257 static void
258 random_adaptors_init(void *unused)
259 {
260
261         SYSCTL_PROC(_kern_random, OID_AUTO, adaptors,
262             CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
263             NULL, 0, random_sysctl_adaptors_handler, "",
264             "Random Number Generator adaptors");
265
266         SYSCTL_PROC(_kern_random, OID_AUTO, active_adaptor,
267             CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
268             NULL, 0, random_sysctl_active_adaptor_handler, "",
269             "Active Random Number Generator Adaptor");
270
271         sx_init(&adaptors_lock, "random_adaptors");
272 }
273
274 SYSCTL_NODE(_kern, OID_AUTO, random, CTLFLAG_RW, 0, "Random Number Generator");
275
276 SYSINIT(random_adaptors, SI_SUB_DRIVERS, SI_ORDER_FIRST, random_adaptors_init,
277     NULL);
278 SYSUNINIT(random_adaptors, SI_SUB_DRIVERS, SI_ORDER_FIRST,
279     random_adaptors_deinit, NULL);