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