randomdev.c revision 128151
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 128151 2004-04-12 09:13:24Z 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/mutex.h> 42#include <sys/poll.h> 43#include <sys/proc.h> 44#include <sys/selinfo.h> 45#include <sys/uio.h> 46#include <sys/unistd.h> 47#include <sys/vnode.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_close = random_close, 65 .d_read = random_read, 66 .d_write = random_write, 67 .d_ioctl = random_ioctl, 68 .d_poll = random_poll, 69 .d_name = "random", 70}; 71 72static void *random_buf; 73 74struct random_systat random_systat; 75 76/* For use with make_dev(9)/destroy_dev(9). */ 77static dev_t 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(dev_t 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 mtx_lock(&random_systat.lock); 93 (*random_systat.reseed)(); 94 random_systat.seeded = 1; 95 mtx_unlock(&random_systat.lock); 96 } 97 return (0); 98} 99 100/* ARGSUSED */ 101static int 102random_read(dev_t dev __unused, struct uio *uio, int flag) 103{ 104 int c, error = 0; 105 106 mtx_lock(&random_systat.lock); 107 108 /* Blocking logic */ 109 while (!random_systat.seeded && !error) { 110 if (flag & IO_NDELAY) 111 error = EWOULDBLOCK; 112 else 113 error = msleep(&random_systat, &random_systat.lock, 114 PUSER | PCATCH, "block", 0); 115 } 116 117 /* The actual read */ 118 if (!error) { 119 while (uio->uio_resid > 0 && !error) { 120 c = MIN(uio->uio_resid, PAGE_SIZE); 121 c = (*random_systat.read)(random_buf, c); 122 error = uiomove(random_buf, c, uio); 123 } 124 } 125 126 mtx_unlock(&random_systat.lock); 127 128 return (error); 129} 130 131/* ARGSUSED */ 132static int 133random_write(dev_t dev __unused, struct uio *uio, int flag __unused) 134{ 135 int c, error = 0; 136 137 mtx_lock(&random_systat.lock); 138 while (uio->uio_resid > 0) { 139 c = MIN((int)uio->uio_resid, PAGE_SIZE); 140 error = uiomove(random_buf, c, uio); 141 if (error) 142 break; 143 (*random_systat.write)(random_buf, c); 144 } 145 mtx_unlock(&random_systat.lock); 146 return (error); 147} 148 149/* ARGSUSED */ 150static int 151random_ioctl(dev_t dev __unused, u_long cmd, caddr_t addr __unused, 152 int flags __unused, struct thread *td __unused) 153{ 154 int error = 0; 155 156 switch (cmd) { 157 /* Really handled in upper layer */ 158 case FIOASYNC: 159 case FIONBIO: 160 break; 161 default: 162 error = ENOTTY; 163 } 164 return (error); 165} 166 167/* ARGSUSED */ 168static int 169random_poll(dev_t dev __unused, int events, struct thread *td) 170{ 171 int revents = 0; 172 173 if (events & (POLLIN | POLLRDNORM)) { 174 if (random_systat.seeded) 175 revents = events & (POLLIN | POLLRDNORM); 176 else 177 selrecord(td, &random_systat.rsel); 178 } 179 return (revents); 180} 181 182/* ARGSUSED */ 183static int 184random_modevent(module_t mod __unused, int type, void *data __unused) 185{ 186 int error = 0; 187 188 switch (type) { 189 case MOD_LOAD: 190 random_buf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK); 191 random_ident_hardware(&random_systat); 192 mtx_init(&random_systat.lock, "entropy device lock", 193 NULL, MTX_DEF); 194 (*random_systat.init)(); 195 196 printf("random: <entropy source, %s>\n", random_systat.ident); 197 198 random_dev = make_dev(&random_cdevsw, RANDOM_MINOR, 199 UID_ROOT, GID_WHEEL, 0666, "random"); 200 make_dev_alias(random_dev, "urandom"); /* XXX Deprecated */ 201 202 break; 203 204 case MOD_UNLOAD: 205 (*random_systat.deinit)(); 206 free(random_buf, M_TEMP); 207 mtx_destroy(&random_systat.lock); 208 209 destroy_dev(random_dev); 210 211 break; 212 213 case MOD_SHUTDOWN: 214 break; 215 216 } 217 return (error); 218} 219 220DEV_MODULE(random, random_modevent, NULL); 221