]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/random/randomdev.c
Update to OpenPAM Nummularia.
[FreeBSD/FreeBSD.git] / sys / dev / random / randomdev.c
1 /*-
2  * Copyright (c) 2013 Arthur Mesh <arthurmesh@gmail.com>
3  * Copyright (c) 2000-2004 Mark R V Murray
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/selinfo.h>
48 #include <sys/uio.h>
49 #include <sys/unistd.h>
50
51 #include <machine/bus.h>
52 #include <machine/cpu.h>
53
54 #include <dev/random/random_adaptors.h>
55 #include <dev/random/randomdev.h>
56
57 #define RANDOM_MINOR    0
58
59 static d_close_t random_close;
60 static d_read_t random_read;
61 static d_write_t random_write;
62 static d_ioctl_t random_ioctl;
63 static d_poll_t random_poll;
64
65 static struct cdevsw random_cdevsw = {
66         .d_version = D_VERSION,
67         .d_close = random_close,
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 static struct random_adaptor *random_adaptor;
76 static eventhandler_tag attach_tag;
77 static int random_inited;
78
79
80 /* For use with make_dev(9)/destroy_dev(9). */
81 static struct cdev *random_dev;
82
83 /* Used to fake out unused random calls in random_adaptor */
84 void
85 random_null_func(void)
86 {
87 }
88
89 struct random_adaptor *
90 random_get_active_adaptor(void)
91
92
93         return (random_adaptor);
94 }
95
96 /* ARGSUSED */
97 static int
98 random_close(struct cdev *dev __unused, int flags, int fmt __unused,
99     struct thread *td)
100 {
101         if ((flags & FWRITE) && (priv_check(td, PRIV_RANDOM_RESEED) == 0)
102             && (securelevel_gt(td->td_ucred, 0) == 0)) {
103                 (*random_adaptor->reseed)();
104                 random_adaptor->seeded = 1;
105                 arc4rand(NULL, 0, 1);   /* Reseed arc4random as well. */
106         }
107
108         return (0);
109 }
110
111 /* ARGSUSED */
112 static int
113 random_read(struct cdev *dev __unused, struct uio *uio, int flag)
114 {
115         int c, error = 0;
116         void *random_buf;
117
118         /* Blocking logic */
119         if (!random_adaptor->seeded)
120                 error = (*random_adaptor->block)(flag);
121
122         /* The actual read */
123         if (!error) {
124
125                 random_buf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
126
127                 while (uio->uio_resid > 0 && !error) {
128                         c = MIN(uio->uio_resid, PAGE_SIZE);
129                         c = (*random_adaptor->read)(random_buf, c);
130                         error = uiomove(random_buf, c, uio);
131                 }
132
133                 free(random_buf, M_TEMP);
134
135         }
136
137         return (error);
138 }
139
140 /* ARGSUSED */
141 static int
142 random_write(struct cdev *dev __unused, struct uio *uio, int flag __unused)
143 {
144         int c, error = 0;
145         void *random_buf;
146
147         random_buf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
148
149         while (uio->uio_resid > 0) {
150                 c = MIN((int)uio->uio_resid, PAGE_SIZE);
151                 error = uiomove(random_buf, c, uio);
152                 if (error)
153                         break;
154                 (*random_adaptor->write)(random_buf, c);
155         }
156
157         free(random_buf, M_TEMP);
158
159         return (error);
160 }
161
162 /* ARGSUSED */
163 static int
164 random_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t addr __unused,
165     int flags __unused, struct thread *td __unused)
166 {
167         int error = 0;
168
169         switch (cmd) {
170                 /* Really handled in upper layer */
171         case FIOASYNC:
172         case FIONBIO:
173                 break;
174         default:
175                 error = ENOTTY;
176         }
177         return (error);
178 }
179
180 /* ARGSUSED */
181 static int
182 random_poll(struct cdev *dev __unused, int events, struct thread *td)
183 {
184         int revents = 0;
185
186         if (events & (POLLIN | POLLRDNORM)) {
187                 if (random_adaptor->seeded)
188                         revents = events & (POLLIN | POLLRDNORM);
189                 else
190                         revents = (*random_adaptor->poll) (events,td);
191         }
192         return (revents);
193 }
194
195 static void
196 random_initialize(void *p, struct random_adaptor *s)
197 {
198         if (random_inited) {
199                 printf("random: <%s> already initialized\n",
200                     random_adaptor->ident);
201                 return;
202         }
203
204         random_adaptor = s;
205
206         (s->init)();
207
208         printf("random: <%s> initialized\n", s->ident);
209
210         random_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &random_cdevsw,
211             RANDOM_MINOR, NULL, UID_ROOT, GID_WHEEL, 0666, "random");
212         make_dev_alias(random_dev, "urandom");  /* XXX Deprecated */
213
214         /* mark random(4) as initialized, to avoid being called again */
215         random_inited = 1;
216 }
217
218 /* ARGSUSED */
219 static int
220 random_modevent(module_t mod __unused, int type, void *data __unused)
221 {
222         int error = 0;
223
224         switch (type) {
225         case MOD_LOAD:
226                 random_adaptor_choose(&random_adaptor);
227
228                 if (random_adaptor == NULL) {
229                         printf(
230        "random: No random adaptor attached, postponing initialization\n");
231                         attach_tag = EVENTHANDLER_REGISTER(random_adaptor_attach,
232                             random_initialize, NULL, EVENTHANDLER_PRI_ANY);
233                 } else {
234                         random_initialize(NULL, random_adaptor);
235                 }
236
237                 break;
238
239         case MOD_UNLOAD:
240                 if (random_adaptor != NULL) {
241                         (*random_adaptor->deinit)();
242                         destroy_dev(random_dev);
243                 }
244                 /* Unregister the event handler */
245                 if (attach_tag != NULL) {
246                         EVENTHANDLER_DEREGISTER(random_adaptor_attach,
247                             attach_tag);
248                 }
249
250                 break;
251
252         case MOD_SHUTDOWN:
253                 break;
254
255         default:
256                 error = EOPNOTSUPP;
257                 break;
258
259         }
260         return (error);
261 }
262
263 DEV_MODULE(random, random_modevent, NULL);
264 MODULE_VERSION(random, 1);