1 /* $NetBSD: bio.c,v 1.4 2007/12/05 07:06:50 ad Exp $ */ 2 /* $OpenBSD: bio.c,v 1.9 2007/03/20 02:35:55 marco Exp $ */ 3 4 /* 5 * Copyright (c) 2002 Niklas Hallqvist. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 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 /* A device controller ioctl tunnelling device. */ 29 30 #include <sys/cdefs.h> 31 __KERNEL_RCSID(0, "$NetBSD: bio.c,v 1.4 2007/12/05 07:06:50 ad Exp $"); 32 33 #include <sys/param.h> 34 #include <sys/conf.h> 35 #include <sys/device.h> 36 #include <sys/event.h> 37 #include <sys/ioctl.h> 38 #include <sys/malloc.h> 39 #include <sys/queue.h> 40 #include <sys/systm.h> 41 #include <sys/mutex.h> 42 #include <sys/proc.h> 43 #include <sys/kauth.h> 44 45 #include <dev/biovar.h> 46 47 struct bio_mapping { 48 LIST_ENTRY(bio_mapping) bm_link; 49 struct device *bm_dev; 50 int (*bm_ioctl)(struct device *, u_long, void *); 51 }; 52 53 LIST_HEAD(, bio_mapping) bios = LIST_HEAD_INITIALIZER(bios); 54 static kmutex_t bio_lock; 55 static bool bio_lock_initialized = false; 56 57 static void bio_initialize(void); 58 void bioattach(int); 59 int bioclose(dev_t, int, int, struct lwp *); 60 int bioioctl(dev_t, u_long, void *, int, struct lwp *); 61 int bioopen(dev_t, int, int, struct lwp *); 62 63 int bio_delegate_ioctl(struct bio_mapping *, u_long, void *); 64 struct bio_mapping *bio_lookup(char *); 65 int bio_validate(void *); 66 67 const struct cdevsw bio_cdevsw = { 68 bioopen, bioclose, noread, nowrite, bioioctl, 69 nostop, notty, nopoll, nommap, nokqfilter, 0 70 }; 71 72 73 void 74 bio_initialize(void) 75 { 76 if (bio_lock_initialized) 77 return; 78 79 mutex_init(&bio_lock, MUTEX_DEFAULT, IPL_VM); 80 bio_lock_initialized = true; 81 } 82 83 void 84 bioattach(int nunits) 85 { 86 if (!bio_lock_initialized) 87 bio_initialize(); 88 } 89 90 int 91 bioopen(dev_t dev, int flags, int mode, struct lwp *l) 92 { 93 return 0; 94 } 95 96 int 97 bioclose(dev_t dev, int flags, int mode, struct lwp *l) 98 { 99 return 0; 100 } 101 102 int 103 bioioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l) 104 { 105 struct bio_locate *locate; 106 struct bio_common *common; 107 char name[16]; 108 int error; 109 110 switch(cmd) { 111 case BIOCLOCATE: 112 case BIOCINQ: 113 case BIOCDISK: 114 case BIOCVOL: 115 error = kauth_authorize_device_passthru(l->l_cred, dev, 116 KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF, addr); 117 if (error) 118 return error; 119 break; 120 case BIOCBLINK: 121 case BIOCSETSTATE: 122 error = kauth_authorize_device_passthru(l->l_cred, dev, 123 KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITECONF, addr); 124 if (error) 125 return error; 126 break; 127 case BIOCALARM: { 128 struct bioc_alarm *alarm = (struct bioc_alarm *)addr; 129 switch (alarm->ba_opcode) { 130 case BIOC_SADISABLE: 131 case BIOC_SAENABLE: 132 case BIOC_SASILENCE: 133 case BIOC_SATEST: 134 error = kauth_authorize_device_passthru(l->l_cred, dev, 135 KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITECONF, addr); 136 if (error) 137 return error; 138 break; 139 case BIOC_GASTATUS: 140 error = kauth_authorize_device_passthru(l->l_cred, dev, 141 KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF, addr); 142 if (error) 143 return error; 144 break; 145 default: 146 return EINVAL; 147 } 148 break; 149 } 150 default: 151 return ENOTTY; 152 } 153 154 switch (cmd) { 155 case BIOCLOCATE: 156 locate = (struct bio_locate *)addr; 157 error = copyinstr(locate->bl_name, name, 16, NULL); 158 if (error != 0) 159 return error; 160 locate->bl_cookie = bio_lookup(name); 161 if (locate->bl_cookie == NULL) 162 return ENOENT; 163 break; 164 165 default: 166 common = (struct bio_common *)addr; 167 mutex_enter(&bio_lock); 168 if (!bio_validate(common->bc_cookie)) { 169 mutex_exit(&bio_lock); 170 return ENOENT; 171 } 172 mutex_exit(&bio_lock); 173 error = bio_delegate_ioctl( 174 (struct bio_mapping *)common->bc_cookie, cmd, addr); 175 return error; 176 } 177 return 0; 178 } 179 180 int 181 bio_register(struct device *dev, int (*ioctl)(struct device *, u_long, void *)) 182 { 183 struct bio_mapping *bm; 184 185 if (!bio_lock_initialized) 186 bio_initialize(); 187 188 bm = (struct bio_mapping *)malloc(sizeof(*bm), M_DEVBUF, 189 M_NOWAIT|M_ZERO); 190 if (bm == NULL) 191 return ENOMEM; 192 bm->bm_dev = dev; 193 bm->bm_ioctl = ioctl; 194 mutex_enter(&bio_lock); 195 LIST_INSERT_HEAD(&bios, bm, bm_link); 196 mutex_exit(&bio_lock); 197 return 0; 198 } 199 200 void 201 bio_unregister(struct device *dev) 202 { 203 struct bio_mapping *bm, *next; 204 205 mutex_enter(&bio_lock); 206 for (bm = LIST_FIRST(&bios); bm != NULL; bm = next) { 207 next = LIST_NEXT(bm, bm_link); 208 209 if (dev == bm->bm_dev) { 210 LIST_REMOVE(bm, bm_link); 211 free(bm, M_DEVBUF); 212 } 213 } 214 mutex_exit(&bio_lock); 215 } 216 217 struct bio_mapping * 218 bio_lookup(char *name) 219 { 220 struct bio_mapping *bm; 221 222 mutex_enter(&bio_lock); 223 LIST_FOREACH(bm, &bios, bm_link) { 224 if (strcmp(name, bm->bm_dev->dv_xname) == 0) { 225 mutex_exit(&bio_lock); 226 return bm; 227 } 228 } 229 mutex_exit(&bio_lock); 230 return NULL; 231 } 232 233 int 234 bio_validate(void *cookie) 235 { 236 struct bio_mapping *bm; 237 238 LIST_FOREACH(bm, &bios, bm_link) 239 if (bm == cookie) 240 return 1; 241 242 return 0; 243 } 244 245 int 246 bio_delegate_ioctl(struct bio_mapping *bm, u_long cmd, void *addr) 247 { 248 249 return bm->bm_ioctl(bm->bm_dev, cmd, addr); 250 } 251