1 /*- 2 * Copyright (c) 2000-2013 Mark R V Murray 3 * Copyright (c) 2013 Arthur Mesh <[email protected]> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer 11 * in this position and unchanged. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/bus.h> 35 #include <sys/conf.h> 36 #include <sys/fcntl.h> 37 #include <sys/filio.h> 38 #include <sys/kernel.h> 39 #include <sys/kthread.h> 40 #include <sys/lock.h> 41 #include <sys/malloc.h> 42 #include <sys/module.h> 43 #include <sys/mutex.h> 44 #include <sys/poll.h> 45 #include <sys/priv.h> 46 #include <sys/proc.h> 47 #include <sys/random.h> 48 #include <sys/selinfo.h> 49 #include <sys/uio.h> 50 #include <sys/unistd.h> 51 52 #include <machine/bus.h> 53 #include <machine/cpu.h> 54 55 #include <dev/random/random_adaptors.h> 56 #include <dev/random/live_entropy_sources.h> 57 #include <dev/random/randomdev.h> 58 59 #define RANDOM_MINOR 0 60 61 static d_read_t random_read; 62 static d_write_t random_write; 63 static d_ioctl_t random_ioctl; 64 static d_poll_t random_poll; 65 66 static struct cdevsw random_cdevsw = { 67 .d_version = D_VERSION, 68 .d_read = random_read, 69 .d_write = random_write, 70 .d_ioctl = random_ioctl, 71 .d_poll = random_poll, 72 .d_name = "random", 73 }; 74 75 /* For use with make_dev(9)/destroy_dev(9). */ 76 static struct cdev *random_dev; 77 78 /* ARGSUSED */ 79 static int 80 random_read(struct cdev *dev __unused, struct uio *uio, int flag) 81 { 82 int c, error = 0; 83 void *random_buf; 84 85 /* XXX: Harvest some entropy from live entropy sources, if available */ 86 live_entropy_sources_feed(65); /* 65 is meaningless -- 87 need to decide appropriate value */ 88 89 /* Blocking logic */ 90 if (!random_adaptor->seeded) 91 error = (*random_adaptor->block)(flag); 92 93 /* The actual read */ 94 if (!error) { 95 96 random_buf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK); 97 98 while (uio->uio_resid > 0 && !error) { 99 c = MIN(uio->uio_resid, PAGE_SIZE); 100 c = (*random_adaptor->read)(random_buf, c); 101 error = uiomove(random_buf, c, uio); 102 } 103 /* Finished reading; let the source know so it can do some 104 * optional housekeeping */ 105 (*random_adaptor->read)(NULL, 0); 106 107 free(random_buf, M_TEMP); 108 109 } 110 111 return (error); 112 } 113 114 /* ARGSUSED */ 115 static int 116 random_write(struct cdev *dev __unused, struct uio *uio, int flag __unused) 117 { 118 119 /* We used to allow this to insert userland entropy. 120 * We don't any more because (1) this so-called entropy 121 * is usually lousy and (b) its vaguely possible to 122 * mess with entropy harvesting by overdoing a write. 123 * Now we just ignore input like /dev/null does. 124 */ 125 uio->uio_resid = 0; 126 127 return (0); 128 } 129 130 /* ARGSUSED */ 131 static int 132 random_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t addr __unused, 133 int flags __unused, struct thread *td __unused) 134 { 135 int error = 0; 136 137 switch (cmd) { 138 /* Really handled in upper layer */ 139 case FIOASYNC: 140 case FIONBIO: 141 break; 142 default: 143 error = ENOTTY; 144 } 145 return (error); 146 } 147 148 /* ARGSUSED */ 149 static int 150 random_poll(struct cdev *dev __unused, int events, struct thread *td) 151 { 152 int revents = 0; 153 154 if (events & (POLLIN | POLLRDNORM)) { 155 if (random_adaptor->seeded) 156 revents = events & (POLLIN | POLLRDNORM); 157 else 158 revents = (*random_adaptor->poll)(events, td); 159 } 160 return (revents); 161 } 162 163 static void 164 random_initialize(void *p, struct random_adaptor *s) 165 { 166 static int random_inited = 0; 167 168 if (random_inited) { 169 printf("random: <%s> already initialized\n", 170 random_adaptor->ident); 171 return; 172 } 173 174 random_adaptor = s; 175 176 (s->init)(); 177 178 printf("random: <%s> initialized\n", s->ident); 179 180 /* Use an appropriately evil mode for those who are concerned 181 * with daemons */ 182 random_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &random_cdevsw, 183 RANDOM_MINOR, NULL, UID_ROOT, GID_WHEEL, 0666, "random"); 184 185 /* mark random(4) as initialized, to avoid being called again */ 186 random_inited = 1; 187 } 188 189 /* ARGSUSED */ 190 static int 191 random_modevent(module_t mod __unused, int type, void *data __unused) 192 { 193 static eventhandler_tag attach_tag = NULL; 194 int error = 0; 195 196 switch (type) { 197 case MOD_LOAD: 198 random_adaptor_choose(&random_adaptor); 199 200 if (random_adaptor == NULL) { 201 printf("random: No random adaptor attached, " 202 "postponing initialization\n"); 203 attach_tag = EVENTHANDLER_REGISTER(random_adaptor_attach, 204 random_initialize, NULL, EVENTHANDLER_PRI_ANY); 205 } else 206 random_initialize(NULL, random_adaptor); 207 208 break; 209 210 case MOD_UNLOAD: 211 if (random_adaptor != NULL) { 212 (*random_adaptor->deinit)(); 213 destroy_dev(random_dev); 214 } 215 /* Unregister the event handler */ 216 if (attach_tag != NULL) 217 EVENTHANDLER_DEREGISTER(random_adaptor_attach, 218 attach_tag); 219 220 break; 221 222 case MOD_SHUTDOWN: 223 break; 224 225 default: 226 error = EOPNOTSUPP; 227 break; 228 229 } 230 return (error); 231 } 232 233 DEV_MODULE(random, random_modevent, NULL); 234 MODULE_VERSION(random, 1); 235