1 /* $NetBSD: rndpseudo_50.c,v 1.7 2020/04/30 03:30:10 riastradh 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.7 2020/04/30 03:30:10 riastradh 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 #include <sys/module_hook.h> 42 #include <sys/compat_stub.h> 43 44 #include <compat/sys/rnd.h> 45 #include <compat/common/compat_mod.h> 46 47 /* 48 * Convert from rndsource_t to rndsource50_t, for the results from 49 * RNDGETNUM50 and RNDGETNAME50. 50 */ 51 static void 52 rndsource_to_rndsource50(rndsource_t *r, rndsource50_t *r50) 53 { 54 memset(r50, 0, sizeof(*r50)); 55 strlcpy(r50->name, r->name, sizeof(r50->name)); 56 r50->total = r->total; 57 r50->type = r->type; 58 r50->flags = r->flags; 59 } 60 61 /* 62 * COMPAT_50 handling for rnd_ioctl. This is called from rnd_ioctl. 63 * 64 * It also handles the case of (COMPAT_50 && COMPAT_NETBSD32). 65 */ 66 int 67 compat_50_rnd_ioctl(struct file *fp, u_long cmd, void *addr) 68 { 69 int ret = 0; 70 71 switch (cmd) { 72 73 case RNDGETSRCNUM50: 74 { 75 rndstat_t rstbuf = {.start = 0}; 76 rndstat50_t *rst50 = (rndstat50_t *)addr; 77 size_t count; 78 79 if (rst50->count > RND_MAXSTATCOUNT50) 80 return EINVAL; 81 82 rstbuf.start = rst50->start; 83 rstbuf.count = rst50->count; 84 85 ret = (fp->f_ops->fo_ioctl)(fp, RNDGETSRCNUM, &rstbuf); 86 if (ret != 0) 87 return ret; 88 89 for (count = 0; count < rst50->count; count++) { 90 rndsource_to_rndsource50(&rstbuf.source[count], 91 &rst50->source[count]); 92 } 93 rst50->count = rstbuf.count; 94 95 break; 96 } 97 98 case RNDGETSRCNAME50: 99 { 100 rndstat_name_t rstnmbuf = {.name[0] = 0}; 101 rndstat_name50_t *rstnm50; 102 rstnm50 = (rndstat_name50_t *)addr; 103 104 strlcpy(rstnmbuf.name, rstnm50->name, sizeof(rstnmbuf.name)); 105 106 ret = (fp->f_ops->fo_ioctl)(fp, RNDGETSRCNAME, &rstnmbuf); 107 if (ret != 0) 108 return ret; 109 110 rndsource_to_rndsource50(&rstnmbuf.source, &rstnm50->source); 111 112 break; 113 } 114 115 default: 116 return ENOTTY; 117 } 118 119 return ret; 120 } 121 122 void 123 rndpseudo_50_init(void) 124 { 125 126 MODULE_HOOK_SET(rnd_ioctl_50_hook, compat_50_rnd_ioctl); 127 } 128 129 void 130 rndpseudo_50_fini(void) 131 { 132 133 MODULE_HOOK_UNSET(rnd_ioctl_50_hook); 134 } 135