randomdev.c revision 139194
1/*-
2 * Copyright (c) 2000-2004 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 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: head/sys/dev/random/randomdev.c 139194 2004-12-22 17:29:37Z phk $");
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/bus.h>
34#include <sys/conf.h>
35#include <sys/fcntl.h>
36#include <sys/filio.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/selinfo.h>
46#include <sys/uio.h>
47#include <sys/unistd.h>
48
49#include <machine/bus.h>
50#include <machine/cpu.h>
51
52#include <dev/random/randomdev.h>
53
54#define RANDOM_MINOR	0
55
56static d_close_t random_close;
57static d_read_t random_read;
58static d_write_t random_write;
59static d_ioctl_t random_ioctl;
60static d_poll_t random_poll;
61
62static struct cdevsw random_cdevsw = {
63	.d_version = D_VERSION,
64	.d_flags = D_NEEDGIANT,
65	.d_close = random_close,
66	.d_read = random_read,
67	.d_write = random_write,
68	.d_ioctl = random_ioctl,
69	.d_poll = random_poll,
70	.d_name = "random",
71};
72
73struct random_systat random_systat;
74
75/* For use with make_dev(9)/destroy_dev(9). */
76static struct cdev *random_dev;
77
78/* Used to fake out unused random calls in random_systat */
79void
80random_null_func(void)
81{
82}
83
84/* ARGSUSED */
85static int
86random_close(struct cdev *dev __unused, int flags, int fmt __unused,
87    struct thread *td)
88{
89	if ((flags & FWRITE) && (suser(td) == 0)
90	    && (securelevel_gt(td->td_ucred, 0) == 0)) {
91		(*random_systat.reseed)();
92		random_systat.seeded = 1;
93	}
94
95	return (0);
96}
97
98/* ARGSUSED */
99static int
100random_read(struct cdev *dev __unused, struct uio *uio, int flag)
101{
102	int c, error = 0;
103	void *random_buf;
104
105	/* Blocking logic */
106	while (!random_systat.seeded && !error) {
107		if (flag & O_NONBLOCK)
108			error = EWOULDBLOCK;
109		else {
110			printf("Entropy device is blocking.\n");
111			error = tsleep(&random_systat,
112			    PUSER | PCATCH, "block", 0);
113		}
114	}
115
116	/* The actual read */
117	if (!error) {
118
119		random_buf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
120
121		while (uio->uio_resid > 0 && !error) {
122			c = MIN(uio->uio_resid, PAGE_SIZE);
123			c = (*random_systat.read)(random_buf, c);
124			error = uiomove(random_buf, c, uio);
125		}
126
127		free(random_buf, M_TEMP);
128
129	}
130
131	return (error);
132}
133
134/* ARGSUSED */
135static int
136random_write(struct cdev *dev __unused, struct uio *uio, int flag __unused)
137{
138	int c, error = 0;
139	void *random_buf;
140
141	random_buf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
142
143	while (uio->uio_resid > 0) {
144		c = MIN((int)uio->uio_resid, PAGE_SIZE);
145		error = uiomove(random_buf, c, uio);
146		if (error)
147			break;
148		(*random_systat.write)(random_buf, c);
149	}
150
151	free(random_buf, M_TEMP);
152
153	return (error);
154}
155
156/* ARGSUSED */
157static int
158random_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t addr __unused,
159    int flags __unused, struct thread *td __unused)
160{
161	int error = 0;
162
163	switch (cmd) {
164		/* Really handled in upper layer */
165	case FIOASYNC:
166	case FIONBIO:
167		break;
168	default:
169		error = ENOTTY;
170	}
171	return (error);
172}
173
174/* ARGSUSED */
175static int
176random_poll(struct cdev *dev __unused, int events, struct thread *td)
177{
178	int revents = 0;
179
180	if (events & (POLLIN | POLLRDNORM)) {
181		if (random_systat.seeded)
182			revents = events & (POLLIN | POLLRDNORM);
183		else
184			selrecord(td, &random_systat.rsel);
185	}
186	return (revents);
187}
188
189/* ARGSUSED */
190static int
191random_modevent(module_t mod __unused, int type, void *data __unused)
192{
193	int error = 0;
194
195	switch (type) {
196	case MOD_LOAD:
197		random_ident_hardware(&random_systat);
198		(*random_systat.init)();
199
200		if (bootverbose)
201			printf("random: <entropy source, %s>\n",
202			    random_systat.ident);
203
204		random_dev = make_dev(&random_cdevsw, RANDOM_MINOR,
205		    UID_ROOT, GID_WHEEL, 0666, "random");
206		make_dev_alias(random_dev, "urandom");	/* XXX Deprecated */
207
208		break;
209
210	case MOD_UNLOAD:
211		(*random_systat.deinit)();
212
213		destroy_dev(random_dev);
214
215		break;
216
217	case MOD_SHUTDOWN:
218		break;
219
220	default:
221		error = EOPNOTSUPP;
222		break;
223
224	}
225	return (error);
226}
227
228DEV_MODULE(random, random_modevent, NULL);
229MODULE_VERSION(random, 1);
230