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