2000-06-25 08:38:58 +00:00
|
|
|
/*-
|
2004-04-09 15:47:10 +00:00
|
|
|
* Copyright (c) 2000-2004 Mark R V Murray
|
2000-06-25 08:38:58 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer
|
|
|
|
* in this position and unchanged.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2003-08-24 17:55:58 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2000-06-25 08:38:58 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/bus.h>
|
2000-06-25 08:38:58 +00:00
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/fcntl.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/filio.h>
|
2000-06-25 08:38:58 +00:00
|
|
|
#include <sys/kernel.h>
|
2001-03-10 12:51:55 +00:00
|
|
|
#include <sys/kthread.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/lock.h>
|
2000-06-25 08:38:58 +00:00
|
|
|
#include <sys/malloc.h>
|
2004-05-30 17:57:46 +00:00
|
|
|
#include <sys/module.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/mutex.h>
|
2000-10-14 10:59:56 +00:00
|
|
|
#include <sys/poll.h>
|
2006-11-06 13:42:10 +00:00
|
|
|
#include <sys/priv.h>
|
2001-09-12 08:38:13 +00:00
|
|
|
#include <sys/proc.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/selinfo.h>
|
|
|
|
#include <sys/uio.h>
|
2001-03-10 12:51:55 +00:00
|
|
|
#include <sys/unistd.h>
|
|
|
|
|
2000-06-25 08:38:58 +00:00
|
|
|
#include <machine/bus.h>
|
2001-03-10 12:51:55 +00:00
|
|
|
#include <machine/cpu.h>
|
2000-06-25 08:38:58 +00:00
|
|
|
|
2001-03-10 12:51:55 +00:00
|
|
|
#include <dev/random/randomdev.h>
|
2000-06-25 08:38:58 +00:00
|
|
|
|
2003-11-17 23:02:21 +00:00
|
|
|
#define RANDOM_MINOR 0
|
|
|
|
|
2004-04-09 15:47:10 +00:00
|
|
|
static d_close_t random_close;
|
|
|
|
static d_read_t random_read;
|
|
|
|
static d_write_t random_write;
|
|
|
|
static d_ioctl_t random_ioctl;
|
|
|
|
static d_poll_t random_poll;
|
2000-06-25 08:38:58 +00:00
|
|
|
|
|
|
|
static struct cdevsw random_cdevsw = {
|
2004-04-09 15:47:10 +00:00
|
|
|
.d_version = D_VERSION,
|
|
|
|
.d_close = random_close,
|
|
|
|
.d_read = random_read,
|
|
|
|
.d_write = random_write,
|
|
|
|
.d_ioctl = random_ioctl,
|
|
|
|
.d_poll = random_poll,
|
|
|
|
.d_name = "random",
|
2003-11-20 15:35:48 +00:00
|
|
|
};
|
|
|
|
|
2004-04-09 15:47:10 +00:00
|
|
|
struct random_systat random_systat;
|
2001-03-10 12:51:55 +00:00
|
|
|
|
2000-06-25 08:38:58 +00:00
|
|
|
/* For use with make_dev(9)/destroy_dev(9). */
|
2004-06-16 09:47:26 +00:00
|
|
|
static struct cdev *random_dev;
|
2001-03-10 12:51:55 +00:00
|
|
|
|
2004-04-09 15:47:10 +00:00
|
|
|
/* Used to fake out unused random calls in random_systat */
|
|
|
|
void
|
|
|
|
random_null_func(void)
|
2001-03-10 12:51:55 +00:00
|
|
|
{
|
|
|
|
}
|
2000-06-25 08:38:58 +00:00
|
|
|
|
2002-03-03 19:44:22 +00:00
|
|
|
/* ARGSUSED */
|
2000-11-25 19:13:29 +00:00
|
|
|
static int
|
2004-06-16 09:47:26 +00:00
|
|
|
random_close(struct cdev *dev __unused, int flags, int fmt __unused,
|
2004-04-09 15:47:10 +00:00
|
|
|
struct thread *td)
|
2000-11-25 19:13:29 +00:00
|
|
|
{
|
2006-11-06 13:42:10 +00:00
|
|
|
if ((flags & FWRITE) && (priv_check(td, PRIV_RANDOM_RESEED) == 0)
|
2004-04-09 15:47:10 +00:00
|
|
|
&& (securelevel_gt(td->td_ucred, 0) == 0)) {
|
|
|
|
(*random_systat.reseed)();
|
|
|
|
random_systat.seeded = 1;
|
2008-11-24 17:39:39 +00:00
|
|
|
arc4rand(NULL, 0, 1); /* Reseed arc4random as well. */
|
2001-09-26 20:15:42 +00:00
|
|
|
}
|
2004-04-16 17:10:54 +00:00
|
|
|
|
2004-04-09 15:47:10 +00:00
|
|
|
return (0);
|
2000-11-25 19:13:29 +00:00
|
|
|
}
|
|
|
|
|
2002-03-03 19:44:22 +00:00
|
|
|
/* ARGSUSED */
|
2000-06-25 08:38:58 +00:00
|
|
|
static int
|
2004-06-16 09:47:26 +00:00
|
|
|
random_read(struct cdev *dev __unused, struct uio *uio, int flag)
|
2000-06-25 08:38:58 +00:00
|
|
|
{
|
2004-04-09 15:47:10 +00:00
|
|
|
int c, error = 0;
|
2004-04-17 19:23:15 +00:00
|
|
|
void *random_buf;
|
2000-06-25 08:38:58 +00:00
|
|
|
|
2004-04-09 15:47:10 +00:00
|
|
|
/* Blocking logic */
|
2005-12-20 21:41:52 +00:00
|
|
|
if (!random_systat.seeded)
|
|
|
|
error = (*random_systat.block)(flag);
|
2004-04-09 15:47:10 +00:00
|
|
|
|
|
|
|
/* The actual read */
|
|
|
|
if (!error) {
|
2004-04-17 19:23:15 +00:00
|
|
|
|
|
|
|
random_buf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
|
|
|
|
|
2004-04-09 15:47:10 +00:00
|
|
|
while (uio->uio_resid > 0 && !error) {
|
|
|
|
c = MIN(uio->uio_resid, PAGE_SIZE);
|
|
|
|
c = (*random_systat.read)(random_buf, c);
|
|
|
|
error = uiomove(random_buf, c, uio);
|
|
|
|
}
|
2004-04-17 19:23:15 +00:00
|
|
|
|
|
|
|
free(random_buf, M_TEMP);
|
|
|
|
|
2000-06-25 08:38:58 +00:00
|
|
|
}
|
2004-04-12 09:13:24 +00:00
|
|
|
|
2004-04-09 15:47:10 +00:00
|
|
|
return (error);
|
2000-06-25 08:38:58 +00:00
|
|
|
}
|
|
|
|
|
2002-03-03 19:44:22 +00:00
|
|
|
/* ARGSUSED */
|
2000-06-25 08:38:58 +00:00
|
|
|
static int
|
2004-06-16 09:47:26 +00:00
|
|
|
random_write(struct cdev *dev __unused, struct uio *uio, int flag __unused)
|
2000-06-25 08:38:58 +00:00
|
|
|
{
|
2004-04-09 15:47:10 +00:00
|
|
|
int c, error = 0;
|
2004-04-17 19:23:15 +00:00
|
|
|
void *random_buf;
|
|
|
|
|
|
|
|
random_buf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
|
2000-06-25 08:38:58 +00:00
|
|
|
|
|
|
|
while (uio->uio_resid > 0) {
|
2004-04-09 15:47:10 +00:00
|
|
|
c = MIN((int)uio->uio_resid, PAGE_SIZE);
|
2000-07-07 09:03:59 +00:00
|
|
|
error = uiomove(random_buf, c, uio);
|
2000-06-25 08:38:58 +00:00
|
|
|
if (error)
|
|
|
|
break;
|
2004-04-09 15:47:10 +00:00
|
|
|
(*random_systat.write)(random_buf, c);
|
2000-06-25 08:38:58 +00:00
|
|
|
}
|
2004-04-16 17:10:54 +00:00
|
|
|
|
2004-04-17 19:23:15 +00:00
|
|
|
free(random_buf, M_TEMP);
|
|
|
|
|
2004-04-09 15:47:10 +00:00
|
|
|
return (error);
|
2000-06-25 08:38:58 +00:00
|
|
|
}
|
|
|
|
|
2002-03-03 19:44:22 +00:00
|
|
|
/* ARGSUSED */
|
2000-09-10 13:52:19 +00:00
|
|
|
static int
|
2004-06-16 09:47:26 +00:00
|
|
|
random_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t addr __unused,
|
2002-03-03 19:44:22 +00:00
|
|
|
int flags __unused, struct thread *td __unused)
|
2000-09-10 13:52:19 +00:00
|
|
|
{
|
2004-04-09 15:47:10 +00:00
|
|
|
int error = 0;
|
|
|
|
|
2001-03-25 06:55:19 +00:00
|
|
|
switch (cmd) {
|
2004-04-09 15:47:10 +00:00
|
|
|
/* Really handled in upper layer */
|
2001-03-25 06:55:19 +00:00
|
|
|
case FIOASYNC:
|
|
|
|
case FIONBIO:
|
2004-04-09 15:47:10 +00:00
|
|
|
break;
|
2001-03-25 06:55:19 +00:00
|
|
|
default:
|
2004-04-09 15:47:10 +00:00
|
|
|
error = ENOTTY;
|
2001-03-25 06:55:19 +00:00
|
|
|
}
|
2004-04-09 15:47:10 +00:00
|
|
|
return (error);
|
2000-09-10 13:52:19 +00:00
|
|
|
}
|
|
|
|
|
2002-03-03 19:44:22 +00:00
|
|
|
/* ARGSUSED */
|
2000-10-14 10:59:56 +00:00
|
|
|
static int
|
2004-06-16 09:47:26 +00:00
|
|
|
random_poll(struct cdev *dev __unused, int events, struct thread *td)
|
2000-10-14 10:59:56 +00:00
|
|
|
{
|
2004-04-09 15:47:10 +00:00
|
|
|
int revents = 0;
|
2000-10-14 10:59:56 +00:00
|
|
|
|
|
|
|
if (events & (POLLIN | POLLRDNORM)) {
|
2001-03-10 12:51:55 +00:00
|
|
|
if (random_systat.seeded)
|
2000-10-14 10:59:56 +00:00
|
|
|
revents = events & (POLLIN | POLLRDNORM);
|
|
|
|
else
|
2005-12-20 21:41:52 +00:00
|
|
|
revents = (*random_systat.poll) (events,td);
|
2000-10-14 10:59:56 +00:00
|
|
|
}
|
2004-04-09 15:47:10 +00:00
|
|
|
return (revents);
|
2000-10-14 10:59:56 +00:00
|
|
|
}
|
|
|
|
|
2002-03-03 19:44:22 +00:00
|
|
|
/* ARGSUSED */
|
2000-06-25 08:38:58 +00:00
|
|
|
static int
|
2002-03-03 19:44:22 +00:00
|
|
|
random_modevent(module_t mod __unused, int type, void *data __unused)
|
2000-06-25 08:38:58 +00:00
|
|
|
{
|
2004-04-09 15:47:10 +00:00
|
|
|
int error = 0;
|
2000-09-10 13:52:19 +00:00
|
|
|
|
2004-04-09 15:47:10 +00:00
|
|
|
switch (type) {
|
2000-06-25 08:38:58 +00:00
|
|
|
case MOD_LOAD:
|
2004-04-09 15:47:10 +00:00
|
|
|
random_ident_hardware(&random_systat);
|
|
|
|
(*random_systat.init)();
|
2001-03-10 12:51:55 +00:00
|
|
|
|
2004-07-01 07:46:29 +00:00
|
|
|
if (bootverbose)
|
|
|
|
printf("random: <entropy source, %s>\n",
|
|
|
|
random_systat.ident);
|
2001-03-10 12:51:55 +00:00
|
|
|
|
2011-01-04 10:59:38 +00:00
|
|
|
random_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &random_cdevsw,
|
|
|
|
RANDOM_MINOR, NULL, UID_ROOT, GID_WHEEL, 0666, "random");
|
2004-04-09 15:47:10 +00:00
|
|
|
make_dev_alias(random_dev, "urandom"); /* XXX Deprecated */
|
2001-03-10 12:51:55 +00:00
|
|
|
|
2004-04-09 15:47:10 +00:00
|
|
|
break;
|
2000-06-25 08:38:58 +00:00
|
|
|
|
|
|
|
case MOD_UNLOAD:
|
2004-04-09 15:47:10 +00:00
|
|
|
(*random_systat.deinit)();
|
2001-03-10 12:51:55 +00:00
|
|
|
|
2000-07-07 09:03:59 +00:00
|
|
|
destroy_dev(random_dev);
|
2004-04-09 15:47:10 +00:00
|
|
|
|
|
|
|
break;
|
2000-06-25 08:38:58 +00:00
|
|
|
|
|
|
|
case MOD_SHUTDOWN:
|
2004-04-09 15:47:10 +00:00
|
|
|
break;
|
2000-06-25 08:38:58 +00:00
|
|
|
|
2004-07-15 08:26:07 +00:00
|
|
|
default:
|
|
|
|
error = EOPNOTSUPP;
|
|
|
|
break;
|
|
|
|
|
2000-06-25 08:38:58 +00:00
|
|
|
}
|
2004-04-09 15:47:10 +00:00
|
|
|
return (error);
|
2000-06-25 08:38:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEV_MODULE(random, random_modevent, NULL);
|
2004-08-02 20:42:28 +00:00
|
|
|
MODULE_VERSION(random, 1);
|