randomdev.c revision 72667
1/*-
2 * Copyright (c) 2000 Mark R V Murray
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer
10 *    in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/dev/random/randomdev.c 72667 2001-02-18 17:40:47Z markm $
27 */
28
29#include <sys/param.h>
30#include <sys/queue.h>
31#include <sys/systm.h>
32#include <sys/conf.h>
33#include <sys/fcntl.h>
34#include <sys/uio.h>
35#include <sys/kernel.h>
36#include <sys/malloc.h>
37#include <sys/module.h>
38#include <sys/bus.h>
39#include <sys/poll.h>
40#include <sys/selinfo.h>
41#include <sys/random.h>
42#include <sys/sysctl.h>
43#include <sys/vnode.h>
44#include <machine/bus.h>
45#include <machine/resource.h>
46#include <crypto/blowfish/blowfish.h>
47
48#include <dev/random/hash.h>
49#include <dev/random/yarrow.h>
50
51static d_open_t random_open;
52static d_close_t random_close;
53static d_read_t random_read;
54static d_write_t random_write;
55static d_ioctl_t random_ioctl;
56static d_poll_t random_poll;
57
58#define CDEV_MAJOR	2
59#define RANDOM_MINOR	3
60#define URANDOM_MINOR	4
61
62static struct cdevsw random_cdevsw = {
63	/* open */	random_open,
64	/* close */	random_close,
65	/* read */	random_read,
66	/* write */	random_write,
67	/* ioctl */	random_ioctl,
68	/* poll */	random_poll,
69	/* mmap */	nommap,
70	/* strategy */	nostrategy,
71	/* name */	"random",
72	/* maj */	CDEV_MAJOR,
73	/* dump */	nodump,
74	/* psize */	nopsize,
75	/* flags */	0,
76	/* bmaj */	-1
77};
78
79/* For use with make_dev(9)/destroy_dev(9). */
80static dev_t random_dev;
81static dev_t urandom_dev; /* XXX Temporary */
82
83/* To stash the sysctl's until they are removed */
84static struct sysctl_oid *random_sysctl[12]; /* magic # is sysctl count */
85static int sysctlcount = 0;
86
87static int
88random_open(dev_t dev, int flags, int fmt, struct proc *p)
89{
90	if ((flags & FWRITE) && (securelevel > 0 || suser(p)))
91		return EPERM;
92	else
93		return 0;
94}
95
96static int
97random_close(dev_t dev, int flags, int fmt, struct proc *p)
98{
99	if ((flags & FWRITE) && !(securelevel > 0 || suser(p)))
100		random_reseed();
101	return 0;
102}
103
104static int
105random_read(dev_t dev, struct uio *uio, int flag)
106{
107	u_int c, ret;
108	int error = 0;
109	void *random_buf;
110
111	while (!random_state.seeded) {
112		if (flag & IO_NDELAY)
113			error =  EWOULDBLOCK;
114		else
115			error = tsleep(&random_state, PUSER|PCATCH, "rndblk", 0);
116		if (error != 0)
117			return error;
118	}
119	c = min(uio->uio_resid, PAGE_SIZE);
120	random_buf = (void *)malloc(c, M_TEMP, M_WAITOK);
121	while (uio->uio_resid > 0 && error == 0) {
122		ret = read_random_real(random_buf, c);
123		error = uiomove(random_buf, ret, uio);
124	}
125	free(random_buf, M_TEMP);
126	return error;
127}
128
129static int
130random_write(dev_t dev, struct uio *uio, int flag)
131{
132	u_int c;
133	int error = 0;
134	void *random_buf;
135
136	random_buf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
137	while (uio->uio_resid > 0) {
138		c = min(uio->uio_resid, PAGE_SIZE);
139		error = uiomove(random_buf, c, uio);
140		if (error)
141			break;
142		write_random(random_buf, c);
143	}
144	free(random_buf, M_TEMP);
145	return error;
146}
147
148static int
149random_ioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct proc *p)
150{
151	return ENOTTY;
152}
153
154static int
155random_poll(dev_t dev, int events, struct proc *p)
156{
157	int revents;
158
159	revents = 0;
160	if (events & (POLLIN | POLLRDNORM)) {
161		if (random_state.seeded)
162			revents = events & (POLLIN | POLLRDNORM);
163		else
164			selrecord(p, &random_state.rsel);
165	}
166	return revents;
167}
168
169static int
170random_modevent(module_t mod, int type, void *data)
171{
172	struct sysctl_oid *node_base, *node1, *node2;
173	int error, i;
174
175	switch(type) {
176	case MOD_LOAD:
177		error = random_init();
178		if (error != 0)
179			return error;
180
181		random_sysctl[sysctlcount++] = node_base =
182			SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_kern),
183				OID_AUTO, "random", CTLFLAG_RW, 0,
184				"Random Number Generator");
185		random_sysctl[sysctlcount++] = node1 =
186			SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(node_base),
187				OID_AUTO, "sys", CTLFLAG_RW, 0,
188				"Entropy Device Parameters");
189		random_sysctl[sysctlcount++] =
190			SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(node1),
191				OID_AUTO, "seeded", CTLFLAG_RW,
192				&random_state.seeded, 0, "Seeded State");
193		random_sysctl[sysctlcount++] =
194			SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(node1),
195				OID_AUTO, "harvest_ethernet", CTLFLAG_RW,
196				&harvest.ethernet, 0, "Harvest NIC entropy");
197		random_sysctl[sysctlcount++] =
198			SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(node1),
199				OID_AUTO, "harvest_point_to_point", CTLFLAG_RW,
200				&harvest.point_to_point, 0, "Harvest serial net entropy");
201		random_sysctl[sysctlcount++] =
202			SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(node1),
203				OID_AUTO, "harvest_interrupt", CTLFLAG_RW,
204				&harvest.interrupt, 0, "Harvest IRQ entropy");
205		random_sysctl[sysctlcount++] = node2 =
206			SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(node_base),
207				OID_AUTO, "yarrow", CTLFLAG_RW, 0,
208				"Yarrow Parameters");
209		random_sysctl[sysctlcount++] =
210			SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(node2),
211				OID_AUTO, "gengateinterval", CTLFLAG_RW,
212				&random_state.gengateinterval, 0,
213				"Generator Gate Interval");
214		random_sysctl[sysctlcount++] =
215			SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(node2),
216				OID_AUTO, "bins", CTLFLAG_RW,
217				&random_state.bins, 0,
218				"Execution time tuner");
219		random_sysctl[sysctlcount++] =
220			SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(node2),
221				OID_AUTO, "fastthresh", CTLFLAG_RW,
222				&random_state.pool[0].thresh, 0,
223				"Fast pool reseed threshhold");
224		random_sysctl[sysctlcount++] =
225			SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(node2),
226				OID_AUTO, "slowthresh", CTLFLAG_RW,
227				&random_state.pool[1].thresh, 0,
228				"Slow pool reseed threshhold");
229		random_sysctl[sysctlcount++] =
230			SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(node2),
231				OID_AUTO, "slowoverthresh", CTLFLAG_RW,
232				&random_state.slowoverthresh, 0,
233				"Slow pool over-threshhold reseed");
234
235		if (bootverbose)
236			printf("random: <entropy source>\n");
237		random_dev = make_dev(&random_cdevsw, RANDOM_MINOR, UID_ROOT,
238			GID_WHEEL, 0666, "random");
239		urandom_dev = make_dev(&random_cdevsw, URANDOM_MINOR, UID_ROOT,
240			GID_WHEEL, 0666, "urandom"); /* XXX Temporary */
241		return 0;
242
243	case MOD_UNLOAD:
244		random_deinit();
245		destroy_dev(random_dev);
246		destroy_dev(urandom_dev); /* XXX Temporary */
247		for (i = sysctlcount - 1; i >= 0; i--)
248			if (sysctl_remove_oid(random_sysctl[i], 1, 0) == EINVAL)
249				panic("random: removing sysctl");
250		return 0;
251
252	case MOD_SHUTDOWN:
253		return 0;
254
255	default:
256		return EOPNOTSUPP;
257	}
258}
259
260DEV_MODULE(random, random_modevent, NULL);
261