1 /*        $NetBSD: rnd.h,v 1.7 2019/06/27 02:44:54 christos Exp $     */
2 
3 /*-
4  * Copyright (c) 1997,2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Michael Graff <explorer@flame.org>.  This code uses ideas and
9  * algorithms from the Linux driver written by Ted Ts'o.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef _COMPAT_SYS_RND_H_
34 #define   _COMPAT_SYS_RND_H_
35 
36 #if defined(_KERNEL_OPT)
37 #include "opt_compat_netbsd.h"
38 #include "opt_compat_netbsd32.h"
39 #endif
40 
41 #include <sys/types.h>
42 #include <sys/ioctl.h>
43 
44 #ifdef COMPAT_NETBSD32
45 #include <compat/netbsd32/netbsd32.h>
46 #endif /* COMPAT_NETBSD32 */
47 
48 #include <sys/rndio.h>
49 
50 /*
51  * NetBSD-5 used "void *state" in the rndsource_t struct.  rndsource_t
52  * was used in rnstat_t and rnstat_name_t, which were used by
53  * the NetBSD-5 RNDGETSRCNUM and RNDGETSRCNAME ioctls.
54  *
55  */
56 
57 /* Sanitized random source view for userspace. */
58 typedef struct {
59           char                name[16]; /* device name */
60           uint32_t  unused_time;        /* was: last time recorded */
61           uint32_t  unused_delta;       /* was: last delta value */
62           uint32_t  unused_delta2;      /* was: last delta2 value */
63           uint32_t  total;              /* entropy from this source */
64           uint32_t  type;               /* type */
65           uint32_t  flags;              /* flags */
66           void                *unused_state;      /* was: internal state */
67 } rndsource50_t;
68 
69 #ifdef COMPAT_NETBSD32
70 typedef struct {
71           char                name[16]; /* device name */
72           uint32_t  unused_time;        /* was: last time recorded */
73           uint32_t  unused_delta;       /* was: last delta value */
74           uint32_t  unused_delta2;      /* was: last delta2 value */
75           uint32_t  total;              /* entropy from this source */
76           uint32_t  type;               /* type */
77           uint32_t  flags;              /* flags */
78           netbsd32_voidp      unused_state;       /* was: internal state */
79 } rndsource50_32_t;
80 #endif /* COMPAT_NETBSD32 */
81 
82 /*
83  * NetBSD-5 defined RND_MAXSTATCOUNT as 10.  We define RND_MAXSTATCOUNT50
84  * here, and check that the native RND_MAXSTATCOUNT is not smaller.
85  */
86 #define   RND_MAXSTATCOUNT50  10        /* 10 sources at once max */
87 #if (RND_MAXSTATCOUNT50 > RND_MAXSTATCOUNT)
88  #error "RND_MAXSTATCOUNT50 is too large"
89 #endif
90 
91 /*
92  * return "count" random entries, starting at "start"
93  */
94 typedef struct {
95           uint32_t  start;
96           uint32_t  count;
97           rndsource50_t source[RND_MAXSTATCOUNT50];
98 } rndstat50_t;
99 
100 #ifdef COMPAT_NETBSD32
101 typedef struct {
102           uint32_t  start;
103           uint32_t  count;
104           rndsource50_32_t source[RND_MAXSTATCOUNT50];
105 } rndstat50_32_t;
106 #endif /* COMPAT_NETBSD32 */
107 
108 /*
109  * return information on a specific source by name
110  */
111 typedef struct {
112           char                name[16];
113           rndsource50_t source;
114 } rndstat_name50_t;
115 
116 #ifdef COMPAT_NETBSD32
117 typedef struct {
118           char                name[16];
119           rndsource50_32_t source;
120 } rndstat_name50_32_t;
121 #endif /* COMPAT_NETBSD32 */
122 
123 /*
124  * NetBSD-5 defined RND_POOLWORDS as 128.  In NetBSD-6, the value
125  * exposed to userland via the rnddata_t type was renamed to
126  * RND_SAVEWORDS.  As long as RND_SAVEWORDS remains equal to 128, then
127  * rnddata_t (used by ioctl RNDADDATA), and rndpoolstat_t (used by ioctl
128  * RNDGETPOOLSTAT) remain ABI compatible without any extra effort, even
129  * though the declarations in the source code have changed.
130  */
131 #if (RND_SAVEWORDS != 128)
132  #error "RND_SAVEWORDS must be 128 for NetBSD-5 compatibility"
133 #endif
134 
135 /*
136  * Compatibility with NetBSD-5 ioctls.
137  */
138 #ifdef _KERNEL
139 int compat_50_rnd_ioctl(struct file *, u_long, void *);
140 int compat32_50_rnd_ioctl(struct file *, u_long, void *);
141 #endif
142 
143 #define   RNDGETSRCNUM50                _IOWR('R', 102, rndstat50_t)
144 #define   RNDGETSRCNAME50               _IOWR('R', 103, rndstat_name50_t)
145 
146 #ifdef COMPAT_NETBSD32
147 #define   RNDGETSRCNUM50_32   _IOWR('R', 102, rndstat50_32_t)
148 #define   RNDGETSRCNAME50_32  _IOWR('R', 103, rndstat_name50_32_t)
149 #endif /* COMPAT_NETBSD32 */
150 
151 #endif /* !_COMPAT_SYS_RND_H_ */
152