mirror of
https://github.com/opnsense/src.git
synced 2026-03-27 05:03:09 -04:00
I've been meaning to commit this for months. Implement select()
for /dev/random and /dev/urandom. Both are always writable, urandom is always readable, and /dev/random is readable when >= 8 bits are in the pool.
This commit is contained in:
parent
78a660dc23
commit
983febf3dc
4 changed files with 96 additions and 6 deletions
|
|
@ -38,7 +38,7 @@
|
|||
*
|
||||
* from: Utah $Hdr: mem.c 1.13 89/10/08$
|
||||
* from: @(#)mem.c 7.2 (Berkeley) 5/9/91
|
||||
* $Id: mem.c,v 1.36 1996/07/15 05:23:04 bde Exp $
|
||||
* $Id: mem.c,v 1.37 1996/07/15 18:22:07 nate Exp $
|
||||
*/
|
||||
|
||||
/*
|
||||
|
|
@ -80,12 +80,13 @@ static d_close_t mmclose;
|
|||
static d_read_t mmrw;
|
||||
static d_ioctl_t mmioctl;
|
||||
static d_mmap_t memmmap;
|
||||
static d_select_t mmselect;
|
||||
|
||||
#define CDEV_MAJOR 2
|
||||
static struct cdevsw mem_cdevsw =
|
||||
{ mmopen, mmclose, mmrw, mmrw, /*2*/
|
||||
mmioctl, nullstop, nullreset, nodevtotty,/* memory */
|
||||
seltrue, memmmap, NULL, "mem", NULL, -1 };
|
||||
mmselect, memmmap, NULL, "mem", NULL, -1 };
|
||||
|
||||
static caddr_t zbuf;
|
||||
|
||||
|
|
@ -474,6 +475,21 @@ mmioctl(dev, cmd, cmdarg, flags, p)
|
|||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
mmselect(dev, rw, p)
|
||||
dev_t dev;
|
||||
int rw;
|
||||
struct proc *p;
|
||||
{
|
||||
switch (minor(dev)) {
|
||||
case 3: /* /dev/random */
|
||||
return random_select(dev, rw, p);
|
||||
case 4: /* /dev/urandom */
|
||||
default:
|
||||
return seltrue(dev, rw, p);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Routine that identifies /dev/mem and /dev/kmem.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@
|
|||
*
|
||||
* from: Utah $Hdr: mem.c 1.13 89/10/08$
|
||||
* from: @(#)mem.c 7.2 (Berkeley) 5/9/91
|
||||
* $Id: mem.c,v 1.36 1996/07/15 05:23:04 bde Exp $
|
||||
* $Id: mem.c,v 1.37 1996/07/15 18:22:07 nate Exp $
|
||||
*/
|
||||
|
||||
/*
|
||||
|
|
@ -80,12 +80,13 @@ static d_close_t mmclose;
|
|||
static d_read_t mmrw;
|
||||
static d_ioctl_t mmioctl;
|
||||
static d_mmap_t memmmap;
|
||||
static d_select_t mmselect;
|
||||
|
||||
#define CDEV_MAJOR 2
|
||||
static struct cdevsw mem_cdevsw =
|
||||
{ mmopen, mmclose, mmrw, mmrw, /*2*/
|
||||
mmioctl, nullstop, nullreset, nodevtotty,/* memory */
|
||||
seltrue, memmmap, NULL, "mem", NULL, -1 };
|
||||
mmselect, memmmap, NULL, "mem", NULL, -1 };
|
||||
|
||||
static caddr_t zbuf;
|
||||
|
||||
|
|
@ -474,6 +475,21 @@ mmioctl(dev, cmd, cmdarg, flags, p)
|
|||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
mmselect(dev, rw, p)
|
||||
dev_t dev;
|
||||
int rw;
|
||||
struct proc *p;
|
||||
{
|
||||
switch (minor(dev)) {
|
||||
case 3: /* /dev/random */
|
||||
return random_select(dev, rw, p);
|
||||
case 4: /* /dev/urandom */
|
||||
default:
|
||||
return seltrue(dev, rw, p);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Routine that identifies /dev/mem and /dev/kmem.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
* random_machdep.c -- A strong random number generator
|
||||
*
|
||||
* $Id: random_machdep.c,v 1.9 1996/06/17 16:47:43 bde Exp $
|
||||
* $Id: random_machdep.c,v 1.10 1996/08/02 21:16:27 bde Exp $
|
||||
*
|
||||
* Version 0.95, last modified 18-Oct-95
|
||||
*
|
||||
|
|
@ -44,6 +44,8 @@
|
|||
#include <sys/param.h>
|
||||
#include <sys/systm.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/fcntl.h>
|
||||
|
||||
#include <machine/clock.h>
|
||||
#include <machine/random.h>
|
||||
|
|
@ -85,6 +87,7 @@ struct random_bucket {
|
|||
u_int entropy_count;
|
||||
int input_rotate;
|
||||
u_int32_t *pool;
|
||||
struct selinfo rsel;
|
||||
};
|
||||
|
||||
/* There is one of these per entropy source */
|
||||
|
|
@ -118,6 +121,8 @@ rand_initialize(void)
|
|||
random_state.entropy_count = 0;
|
||||
random_state.pool = random_pool;
|
||||
random_wait = NULL;
|
||||
random_state.rsel.si_flags = 0;
|
||||
random_state.rsel.si_pid = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -225,6 +230,9 @@ add_timer_randomness(struct random_bucket *r, struct timer_rand_state *state,
|
|||
/* Prevent overflow */
|
||||
if (r->entropy_count > POOLBITS)
|
||||
r->entropy_count = POOLBITS;
|
||||
|
||||
if (r->entropy_count >= 8)
|
||||
selwakeup(&random_state.rsel);
|
||||
}
|
||||
|
||||
void
|
||||
|
|
@ -479,3 +487,24 @@ write_random(const char *buf, u_int nbytes)
|
|||
return nbytes;
|
||||
}
|
||||
#endif /* notused */
|
||||
|
||||
int
|
||||
random_select(dev_t dev, int rw, struct proc *p)
|
||||
{
|
||||
int s, ret;
|
||||
|
||||
if (rw == FWRITE)
|
||||
return 1; /* heh. */
|
||||
|
||||
s = splhigh();
|
||||
if (random_state.entropy_count >= 8)
|
||||
ret = 1;
|
||||
else {
|
||||
selrecord(p, &random_state.rsel);
|
||||
ret = 0;
|
||||
}
|
||||
splx(s);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
* random_machdep.c -- A strong random number generator
|
||||
*
|
||||
* $Id: random_machdep.c,v 1.9 1996/06/17 16:47:43 bde Exp $
|
||||
* $Id: random_machdep.c,v 1.10 1996/08/02 21:16:27 bde Exp $
|
||||
*
|
||||
* Version 0.95, last modified 18-Oct-95
|
||||
*
|
||||
|
|
@ -44,6 +44,8 @@
|
|||
#include <sys/param.h>
|
||||
#include <sys/systm.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/fcntl.h>
|
||||
|
||||
#include <machine/clock.h>
|
||||
#include <machine/random.h>
|
||||
|
|
@ -85,6 +87,7 @@ struct random_bucket {
|
|||
u_int entropy_count;
|
||||
int input_rotate;
|
||||
u_int32_t *pool;
|
||||
struct selinfo rsel;
|
||||
};
|
||||
|
||||
/* There is one of these per entropy source */
|
||||
|
|
@ -118,6 +121,8 @@ rand_initialize(void)
|
|||
random_state.entropy_count = 0;
|
||||
random_state.pool = random_pool;
|
||||
random_wait = NULL;
|
||||
random_state.rsel.si_flags = 0;
|
||||
random_state.rsel.si_pid = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -225,6 +230,9 @@ add_timer_randomness(struct random_bucket *r, struct timer_rand_state *state,
|
|||
/* Prevent overflow */
|
||||
if (r->entropy_count > POOLBITS)
|
||||
r->entropy_count = POOLBITS;
|
||||
|
||||
if (r->entropy_count >= 8)
|
||||
selwakeup(&random_state.rsel);
|
||||
}
|
||||
|
||||
void
|
||||
|
|
@ -479,3 +487,24 @@ write_random(const char *buf, u_int nbytes)
|
|||
return nbytes;
|
||||
}
|
||||
#endif /* notused */
|
||||
|
||||
int
|
||||
random_select(dev_t dev, int rw, struct proc *p)
|
||||
{
|
||||
int s, ret;
|
||||
|
||||
if (rw == FWRITE)
|
||||
return 1; /* heh. */
|
||||
|
||||
s = splhigh();
|
||||
if (random_state.entropy_count >= 8)
|
||||
ret = 1;
|
||||
else {
|
||||
selrecord(p, &random_state.rsel);
|
||||
ret = 0;
|
||||
}
|
||||
splx(s);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue