xref: /netbsd-src/sys/compat/common/rndpseudo_50.c (revision f3cfa6f6ce31685c6c4a758bc430e69eb99f50a4)
1 /*	$NetBSD: rndpseudo_50.c,v 1.4 2019/03/01 11:06:56 pgoyette 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> and Thor Lancelot Simon.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: rndpseudo_50.c,v 1.4 2019/03/01 11:06:56 pgoyette Exp $");
34 
35 #if defined(_KERNEL_OPT)
36 #include "opt_compat_netbsd.h"
37 #endif
38 
39 #include <sys/param.h>
40 #include <sys/file.h>
41 
42 #include <sys/rnd.h>
43 #include <sys/module_hook.h>
44 #include <sys/compat_stub.h>
45 
46 #include <compat/sys/rnd.h>
47 #include <compat/common/compat_mod.h>
48 
49 /*
50  * Convert from rndsource_t to rndsource50_t, for the results from
51  * RNDGETNUM50 and RNDGETNAME50.
52  */
53 static void
54 rndsource_to_rndsource50(rndsource_t *r, rndsource50_t *r50)
55 {
56 	memset(r50, 0, sizeof(*r50));
57 	strlcpy(r50->name, r->name, sizeof(r50->name));
58 	r50->total = r->total;
59 	r50->type = r->type;
60 	r50->flags = r->flags;
61 }
62 
63 /*
64  * COMPAT_50 handling for rnd_ioctl.  This is called from rnd_ioctl.
65  *
66  * It also handles the case of (COMPAT_50 && COMPAT_NETBSD32).
67  */
68 int
69 compat_50_rnd_ioctl(struct file *fp, u_long cmd, void *addr)
70 {
71 	int ret = 0;
72 
73 	switch (cmd) {
74 
75 	case RNDGETSRCNUM50:
76 	{
77 		rndstat_t rstbuf = {.start = 0};
78 		rndstat50_t *rst50 = (rndstat50_t *)addr;
79 		int count;
80 
81 		if (rst50->count > RND_MAXSTATCOUNT50)
82 			return EINVAL;
83 
84 		rstbuf.start = rst50->start;
85 		rstbuf.count = rst50->count;
86 
87 		ret = (fp->f_ops->fo_ioctl)(fp, RNDGETSRCNUM, &rstbuf);
88 		if (ret != 0)
89 			return ret;
90 
91 		for (count = 0; count < rst50->count; count++) {
92 			rndsource_to_rndsource50(&rstbuf.source[count],
93 			    &rst50->source[count]);
94 		}
95 		rst50->count = rstbuf.count;
96 
97 		break;
98 	}
99 
100 	case RNDGETSRCNAME50:
101 	{
102 		rndstat_name_t rstnmbuf = {.name[0] = 0};
103 		rndstat_name50_t *rstnm50;
104 		rstnm50 = (rndstat_name50_t *)addr;
105 
106 		strlcpy(rstnmbuf.name, rstnm50->name, sizeof(rstnmbuf.name));
107 
108 		ret = (fp->f_ops->fo_ioctl)(fp, RNDGETSRCNAME, &rstnmbuf);
109 		if (ret != 0)
110 			return ret;
111 
112 		rndsource_to_rndsource50(&rstnmbuf.source, &rstnm50->source);
113 
114 		break;
115 	}
116 
117 	default:
118 		return ENOTTY;
119 	}
120 
121 	return ret;
122 }
123 
124 void
125 rndpseudo_50_init(void)
126 {
127 
128 	MODULE_HOOK_SET(rnd_ioctl_50_hook, "rnd_50", compat_50_rnd_ioctl);
129 }
130 
131 void
132 rndpseudo_50_fini(void)
133 {
134 
135 	MODULE_HOOK_UNSET(rnd_ioctl_50_hook);
136 }
137