randomdev_soft.c revision 254147
1/*-
2 * Copyright (c) 2000-2009 Mark R V Murray
3 * Copyright (c) 2004 Robert N. M. Watson
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer
11 *    in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/dev/random/randomdev_soft.c 254147 2013-08-09 15:31:50Z obrien $");
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/bus.h>
35#include <sys/conf.h>
36#include <sys/fcntl.h>
37#include <sys/kernel.h>
38#include <sys/kthread.h>
39#include <sys/lock.h>
40#include <sys/malloc.h>
41#include <sys/module.h>
42#include <sys/mutex.h>
43#include <sys/poll.h>
44#include <sys/proc.h>
45#include <sys/random.h>
46#include <sys/selinfo.h>
47#include <sys/sysctl.h>
48#include <sys/uio.h>
49#include <sys/unistd.h>
50
51#include <machine/bus.h>
52#include <machine/cpu.h>
53
54#include <dev/random/random_adaptors.h>
55#include <dev/random/randomdev.h>
56#include <dev/random/randomdev_soft.h>
57
58#define RANDOM_FIFO_MAX	256	/* How many events to queue up */
59
60static void random_kthread(void *);
61static void
62random_harvest_internal(u_int64_t, const void *, u_int,
63    u_int, u_int, enum esource);
64static int random_yarrow_poll(int event,struct thread *td);
65static int random_yarrow_block(int flag);
66static void random_yarrow_flush_reseed(void);
67
68struct random_adaptor random_yarrow = {
69	.ident = "Software, Yarrow",
70	.init = random_yarrow_init,
71	.deinit = random_yarrow_deinit,
72	.block = random_yarrow_block,
73	.read = random_yarrow_read,
74	.write = random_yarrow_write,
75	.poll = random_yarrow_poll,
76	.reseed = random_yarrow_flush_reseed,
77	.seeded = 1,
78};
79
80MALLOC_DEFINE(M_ENTROPY, "entropy", "Entropy harvesting buffers");
81
82/*
83 * The harvest mutex protects the consistency of the entropy fifos and
84 * empty fifo.
85 */
86struct mtx	harvest_mtx;
87
88/* Lockable FIFO queue holding entropy buffers */
89struct entropyfifo {
90	int count;
91	STAILQ_HEAD(harvestlist, harvest) head;
92};
93
94/* Empty entropy buffers */
95static struct entropyfifo emptyfifo;
96
97#define EMPTYBUFFERS	1024
98
99/* Harvested entropy */
100static struct entropyfifo harvestfifo[ENTROPYSOURCE];
101
102/* <0 to end the kthread, 0 to let it run, 1 to flush the harvest queues */
103static int random_kthread_control = 0;
104
105static struct proc *random_kthread_proc;
106
107/* List for the dynamic sysctls */
108static struct sysctl_ctx_list random_clist;
109
110/* ARGSUSED */
111static int
112random_check_boolean(SYSCTL_HANDLER_ARGS)
113{
114	if (oidp->oid_arg1 != NULL && *(u_int *)(oidp->oid_arg1) != 0)
115		*(u_int *)(oidp->oid_arg1) = 1;
116	return sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
117}
118
119/* ARGSUSED */
120void
121random_yarrow_init(void)
122{
123	int error, i;
124	struct harvest *np;
125	struct sysctl_oid *random_sys_o, *random_sys_harvest_o;
126	enum esource e;
127
128	random_yarrow_init_alg(&random_clist);
129
130	random_sys_o = SYSCTL_ADD_NODE(&random_clist,
131	    SYSCTL_STATIC_CHILDREN(_kern_random),
132	    OID_AUTO, "sys", CTLFLAG_RW, 0,
133	    "Entropy Device Parameters");
134
135	SYSCTL_ADD_PROC(&random_clist,
136	    SYSCTL_CHILDREN(random_sys_o),
137	    OID_AUTO, "seeded", CTLTYPE_INT | CTLFLAG_RW,
138	    &random_yarrow.seeded, 1, random_check_boolean, "I",
139	    "Seeded State");
140
141	random_sys_harvest_o = SYSCTL_ADD_NODE(&random_clist,
142	    SYSCTL_CHILDREN(random_sys_o),
143	    OID_AUTO, "harvest", CTLFLAG_RW, 0,
144	    "Entropy Sources");
145
146	SYSCTL_ADD_PROC(&random_clist,
147	    SYSCTL_CHILDREN(random_sys_harvest_o),
148	    OID_AUTO, "ethernet", CTLTYPE_INT | CTLFLAG_RW,
149	    &harvest.ethernet, 1, random_check_boolean, "I",
150	    "Harvest NIC entropy");
151	SYSCTL_ADD_PROC(&random_clist,
152	    SYSCTL_CHILDREN(random_sys_harvest_o),
153	    OID_AUTO, "point_to_point", CTLTYPE_INT | CTLFLAG_RW,
154	    &harvest.point_to_point, 1, random_check_boolean, "I",
155	    "Harvest serial net entropy");
156	SYSCTL_ADD_PROC(&random_clist,
157	    SYSCTL_CHILDREN(random_sys_harvest_o),
158	    OID_AUTO, "interrupt", CTLTYPE_INT | CTLFLAG_RW,
159	    &harvest.interrupt, 1, random_check_boolean, "I",
160	    "Harvest IRQ entropy");
161	SYSCTL_ADD_PROC(&random_clist,
162	    SYSCTL_CHILDREN(random_sys_harvest_o),
163	    OID_AUTO, "swi", CTLTYPE_INT | CTLFLAG_RW,
164	    &harvest.swi, 0, random_check_boolean, "I",
165	    "Harvest SWI entropy");
166
167	/* Initialise the harvest fifos */
168	STAILQ_INIT(&emptyfifo.head);
169	emptyfifo.count = 0;
170	for (i = 0; i < EMPTYBUFFERS; i++) {
171		np = malloc(sizeof(struct harvest), M_ENTROPY, M_WAITOK);
172		STAILQ_INSERT_TAIL(&emptyfifo.head, np, next);
173	}
174	for (e = RANDOM_START; e < ENTROPYSOURCE; e++) {
175		STAILQ_INIT(&harvestfifo[e].head);
176		harvestfifo[e].count = 0;
177	}
178
179	mtx_init(&harvest_mtx, "entropy harvest mutex", NULL, MTX_SPIN);
180
181	/* Start the hash/reseed thread */
182	error = kproc_create(random_kthread, NULL,
183	    &random_kthread_proc, RFHIGHPID, 0, "yarrow");
184	if (error != 0)
185		panic("Cannot create entropy maintenance thread.");
186
187	/* Register the randomness harvesting routine */
188	random_yarrow_init_harvester(random_harvest_internal,
189	    random_yarrow_read);
190}
191
192/* ARGSUSED */
193void
194random_yarrow_deinit(void)
195{
196	struct harvest *np;
197	enum esource e;
198
199	/* Deregister the randomness harvesting routine */
200	random_yarrow_deinit_harvester();
201
202	/*
203	 * Command the hash/reseed thread to end and wait for it to finish
204	 */
205	random_kthread_control = -1;
206	tsleep((void *)&random_kthread_control, 0, "term", 0);
207
208	/* Destroy the harvest fifos */
209	while (!STAILQ_EMPTY(&emptyfifo.head)) {
210		np = STAILQ_FIRST(&emptyfifo.head);
211		STAILQ_REMOVE_HEAD(&emptyfifo.head, next);
212		free(np, M_ENTROPY);
213	}
214	for (e = RANDOM_START; e < ENTROPYSOURCE; e++) {
215		while (!STAILQ_EMPTY(&harvestfifo[e].head)) {
216			np = STAILQ_FIRST(&harvestfifo[e].head);
217			STAILQ_REMOVE_HEAD(&harvestfifo[e].head, next);
218			free(np, M_ENTROPY);
219		}
220	}
221
222	random_yarrow_deinit_alg();
223
224	mtx_destroy(&harvest_mtx);
225
226	sysctl_ctx_free(&random_clist);
227}
228
229/* ARGSUSED */
230static void
231random_kthread(void *arg __unused)
232{
233	STAILQ_HEAD(, harvest) local_queue;
234	struct harvest *event = NULL;
235	int local_count;
236	enum esource source;
237
238	STAILQ_INIT(&local_queue);
239	local_count = 0;
240
241	/* Process until told to stop */
242	mtx_lock_spin(&harvest_mtx);
243	for (; random_kthread_control >= 0;) {
244
245		/* Cycle through all the entropy sources */
246		for (source = RANDOM_START; source < ENTROPYSOURCE; source++) {
247			/*
248			 * Drain entropy source records into a thread-local
249			 * queue for processing while not holding the mutex.
250			 */
251			STAILQ_CONCAT(&local_queue, &harvestfifo[source].head);
252			local_count += harvestfifo[source].count;
253			harvestfifo[source].count = 0;
254		}
255
256		/*
257		 * Deal with events, if any, dropping the mutex as we process
258		 * each event.  Then push the events back into the empty
259		 * fifo.
260		 */
261		if (!STAILQ_EMPTY(&local_queue)) {
262			mtx_unlock_spin(&harvest_mtx);
263			STAILQ_FOREACH(event, &local_queue, next)
264				random_process_event(event);
265			mtx_lock_spin(&harvest_mtx);
266			STAILQ_CONCAT(&emptyfifo.head, &local_queue);
267			emptyfifo.count += local_count;
268			local_count = 0;
269		}
270
271		KASSERT(local_count == 0, ("random_kthread: local_count %d",
272		    local_count));
273
274		/*
275		 * If a queue flush was commanded, it has now happened,
276		 * and we can mark this by resetting the command.
277		 */
278		if (random_kthread_control == 1)
279			random_kthread_control = 0;
280
281		/* Work done, so don't belabour the issue */
282		msleep_spin_sbt(&random_kthread_control, &harvest_mtx,
283		    "-", SBT_1S / 10, 0, C_PREL(1));
284
285	}
286	mtx_unlock_spin(&harvest_mtx);
287
288	random_set_wakeup_exit(&random_kthread_control);
289	/* NOTREACHED */
290}
291
292/* Entropy harvesting routine. This is supposed to be fast; do
293 * not do anything slow in here!
294 */
295static void
296random_harvest_internal(u_int64_t somecounter, const void *entropy,
297    u_int count, u_int bits, u_int frac, enum esource origin)
298{
299	struct harvest *event;
300
301	KASSERT(origin == RANDOM_START || origin == RANDOM_WRITE ||
302            origin == RANDOM_KEYBOARD || origin == RANDOM_MOUSE ||
303            origin == RANDOM_NET || origin == RANDOM_INTERRUPT ||
304            origin == RANDOM_PURE,
305	    ("random_harvest_internal: origin %d invalid\n", origin));
306
307	/* Lockless read to avoid lock operations if fifo is full. */
308	if (harvestfifo[origin].count >= RANDOM_FIFO_MAX)
309		return;
310
311	mtx_lock_spin(&harvest_mtx);
312
313	/*
314	 * Don't make the harvest queues too big - help to prevent low-grade
315	 * entropy swamping
316	 */
317	if (harvestfifo[origin].count < RANDOM_FIFO_MAX) {
318		event = STAILQ_FIRST(&emptyfifo.head);
319		if (event != NULL) {
320			/* Add the harvested data to the fifo */
321			STAILQ_REMOVE_HEAD(&emptyfifo.head, next);
322			harvestfifo[origin].count++;
323			event->somecounter = somecounter;
324			event->size = count;
325			event->bits = bits;
326			event->frac = frac;
327			event->source = origin;
328
329			/* XXXX Come back and make this dynamic! */
330			count = MIN(count, HARVESTSIZE);
331			memcpy(event->entropy, entropy, count);
332
333			STAILQ_INSERT_TAIL(&harvestfifo[origin].head,
334			    event, next);
335		}
336	}
337	mtx_unlock_spin(&harvest_mtx);
338}
339
340void
341random_yarrow_write(void *buf, int count)
342{
343	int i;
344	u_int chunk;
345
346	/*
347	 * Break the input up into HARVESTSIZE chunks. The writer has too
348	 * much control here, so "estimate" the entropy as zero.
349	 */
350	for (i = 0; i < count; i += HARVESTSIZE) {
351		chunk = HARVESTSIZE;
352		if (i + chunk >= count)
353			chunk = (u_int)(count - i);
354		random_harvest_internal(get_cyclecount(), (char *)buf + i,
355		    chunk, 0, 0, RANDOM_WRITE);
356	}
357}
358
359void
360random_yarrow_unblock(void)
361{
362	if (!random_yarrow.seeded) {
363		random_yarrow.seeded = 1;
364		selwakeuppri(&random_yarrow.rsel, PUSER);
365		wakeup(&random_yarrow);
366	}
367	(void)atomic_cmpset_int(&arc4rand_iniseed_state, ARC4_ENTR_NONE,
368	    ARC4_ENTR_HAVE);
369}
370
371static int
372random_yarrow_poll(int events, struct thread *td)
373{
374	int revents = 0;
375	mtx_lock(&random_reseed_mtx);
376
377	if (random_yarrow.seeded)
378		revents = events & (POLLIN | POLLRDNORM);
379	else
380		selrecord(td, &random_yarrow.rsel);
381
382	mtx_unlock(&random_reseed_mtx);
383	return revents;
384}
385
386static int
387random_yarrow_block(int flag)
388{
389	int error = 0;
390
391	mtx_lock(&random_reseed_mtx);
392
393	/* Blocking logic */
394	while (!random_yarrow.seeded && !error) {
395		if (flag & O_NONBLOCK)
396			error = EWOULDBLOCK;
397		else {
398			printf("Entropy device is blocking.\n");
399			error = msleep(&random_yarrow,
400			    &random_reseed_mtx,
401			    PUSER | PCATCH, "block", 0);
402		}
403	}
404	mtx_unlock(&random_reseed_mtx);
405
406	return error;
407}
408
409/* Helper routine to perform explicit reseeds */
410static void
411random_yarrow_flush_reseed(void)
412{
413	/* Command a entropy queue flush and wait for it to finish */
414	random_kthread_control = 1;
415	while (random_kthread_control)
416		pause("-", hz / 10);
417
418	random_yarrow_reseed();
419}
420
421static int
422yarrow_modevent(module_t mod, int type, void *unused)
423{
424
425	switch (type) {
426	case MOD_LOAD:
427		random_adaptor_register("yarrow", &random_yarrow);
428		/*
429		 * For statically built kernels that contain both device
430		 * random and options PADLOCK_RNG/RDRAND_RNG/etc..,
431		 * this event handler will do nothing, since the random
432		 * driver-specific handlers are loaded after these HW
433		 * consumers, and hence hasn't yet registered for this event.
434		 *
435		 * In case where both the random driver and RNG's are built
436		 * as seperate modules, random.ko is loaded prior to *_rng.ko's
437		 * (by dependency). This event handler is there to delay
438		 * creation of /dev/{u,}random and attachment of this *_rng.ko.
439		 */
440		EVENTHANDLER_INVOKE(random_adaptor_attach, &random_yarrow);
441		return (0);
442	}
443
444	return (EINVAL);
445}
446
447RANDOM_ADAPTOR_MODULE(yarrow, yarrow_modevent, 1);
448