1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <sys/param.h> 29 #include <sys/types.h> 30 #include <sys/user.h> 31 #include <sys/vfs.h> 32 #include <sys/vnode.h> 33 #include <sys/file.h> 34 #include <sys/stream.h> 35 #include <sys/stropts.h> 36 #include <sys/strsubr.h> 37 #include <sys/dlpi.h> 38 #include <sys/vnode.h> 39 #include <sys/socket.h> 40 #include <sys/sockio.h> 41 #include <sys/cmn_err.h> 42 #include <net/if.h> 43 #include <sys/sad.h> 44 #include <sys/kstr.h> 45 #include <sys/ddi.h> 46 #include <sys/sunddi.h> 47 #include <sys/sunldi.h> 48 49 #include <sys/cred.h> 50 #include <sys/sysmacros.h> 51 52 #include <sys/modctl.h> 53 54 /* 55 * Routines to allow strplumb() legitimate access 56 * to the kernel. 57 */ 58 int 59 kstr_open(major_t maj, minor_t min, vnode_t **vpp, int *fd) 60 { 61 vnode_t *vp; 62 int error; 63 64 vp = makespecvp(makedevice(maj, min), VCHR); 65 66 /* 67 * Fix for 4170365: only allocate file descriptor entry 68 * if file descriptor is to be returned; otherwise VOP_OPEN. 69 */ 70 if (fd != NULL) 71 error = fassign(&vp, FREAD|FWRITE, fd); 72 else 73 error = VOP_OPEN(&vp, FREAD|FWRITE, CRED(), NULL); 74 75 /* 76 * Must set vpp after calling fassign()/VOP_OPEN() 77 * since `vp' might change if it's a clone driver. 78 */ 79 if (vpp != NULL) 80 *vpp = vp; 81 82 return (error); 83 } 84 85 int 86 kstr_plink(vnode_t *vp, int fd, int *mux_id) 87 { 88 int id; 89 int error; 90 91 if (error = strioctl(vp, I_PLINK, (intptr_t)fd, 0, K_TO_K, CRED(), &id)) 92 return (error); 93 if (mux_id) 94 *mux_id = id; 95 return (0); 96 } 97 98 int 99 kstr_unplink(vnode_t *vp, int mux_id) 100 { 101 int rval; 102 103 return (strioctl(vp, I_PUNLINK, (intptr_t)mux_id, 0, 104 K_TO_K, CRED(), &rval)); 105 } 106 107 int 108 kstr_push(vnode_t *vp, char *mod) 109 { 110 int rval; 111 112 return (strioctl(vp, I_PUSH, (intptr_t)mod, 0, K_TO_K, CRED(), &rval)); 113 } 114 115 int 116 kstr_pop(vnode_t *vp) 117 { 118 int rval; 119 120 return (strioctl(vp, I_POP, 0, 0, K_TO_K, CRED(), &rval)); 121 } 122 123 int 124 kstr_close(vnode_t *vp, int fd) 125 { 126 int ret; 127 128 if (vp == (vnode_t *)NULL && fd == -1) 129 return (EINVAL); 130 131 if (fd != -1) { 132 if (closeandsetf(fd, NULL) == 0) { 133 return (0); 134 } else { 135 return (EINVAL); 136 } 137 } else { 138 ret = VOP_CLOSE(vp, FREAD|FWRITE, 1, (offset_t)0, CRED(), NULL); 139 VN_RELE(vp); 140 return (ret); 141 } 142 } 143 144 int 145 kstr_ioctl(struct vnode *vp, int cmd, intptr_t arg) 146 { 147 int rval; 148 149 return (strioctl(vp, cmd, arg, 0, K_TO_K, CRED(), &rval)); 150 } 151 152 /* 153 * Optionally send data (if smp set) and optionally receive data (if rmp is 154 * set). If timeo is NULL the reception will sleep until a message is 155 * received; otherwise the sleep is limited to the specified amount of time. 156 */ 157 int 158 kstr_msg(vnode_t *vp, mblk_t *smp, mblk_t **rmp, timestruc_t *timeo) 159 { 160 int error; 161 clock_t timout; /* milliseconds */ 162 uchar_t pri; 163 int pflag; 164 rval_t rval; 165 166 if (rmp == NULL && timeo != NULL && 167 (timeo->tv_sec != 0 || timeo->tv_nsec != 0)) 168 return (EINVAL); 169 170 if (smp == NULL && rmp == NULL) 171 return (EINVAL); 172 173 if (smp != NULL) { 174 /* Send message while honoring flow control */ 175 (void) kstrputmsg(vp, smp, NULL, 0, 0, 176 MSG_BAND | MSG_HOLDSIG | MSG_IGNERROR, 0); 177 } 178 179 if (rmp == NULL) { 180 /* No reply wanted by caller */ 181 return (0); 182 } 183 184 /* 185 * Convert from nanoseconds to milliseconds. 186 */ 187 if (timeo != NULL) { 188 timout = timeo->tv_sec * 1000 + timeo->tv_nsec / 1000000; 189 if (timout > INT_MAX) 190 return (EINVAL); 191 } else 192 timout = -1; 193 194 /* Wait for timeout millseconds for a message */ 195 pflag = MSG_ANY; 196 pri = 0; 197 *rmp = NULL; 198 error = kstrgetmsg(vp, rmp, NULL, &pri, &pflag, timout, &rval); 199 /* Callers use *rmp == NULL to determine that there was a timeout */ 200 if (error == ETIME) 201 error = 0; 202 return (error); 203 } 204 205 #define SAD_ADM "/devices/pseudo/sad@0:admin" 206 #define SAD_USR "/devices/pseudo/sad@0:user" 207 208 /* 209 * It is the callers responsibility to make sure that "mods" 210 * conforms to what is required. We do not check it here. 211 * 212 * "maj", "min", and "lastmin" are value-result parameters. 213 * for SET_AUTOPUSH, "anchor" should be set to the place in the stream 214 * to put the anchor, or NULL if no anchor needs to be set. 215 * for GET_AUTOPUSH, "anchor" should point to a uint_t to store the 216 * position of the anchor at, or NULL if the caller is not interested. 217 */ 218 int 219 kstr_autopush(int op, major_t *maj, minor_t *min, minor_t *lastmin, 220 uint_t *anchor, char *mods[]) 221 { 222 ldi_handle_t lh; 223 ldi_ident_t li; 224 struct strapush push; 225 int i, error, rval; 226 227 li = ldi_ident_from_anon(); 228 if (op == SET_AUTOPUSH || op == CLR_AUTOPUSH) { 229 error = ldi_open_by_name(SAD_ADM, FREAD|FWRITE, 230 kcred, &lh, li); 231 if (error) { 232 printf("kstr_autopush: open failed error %d\n", error); 233 ldi_ident_release(li); 234 return (error); 235 } 236 } else { 237 error = ldi_open_by_name(SAD_USR, FREAD|FWRITE, 238 kcred, &lh, li); 239 if (error) { 240 printf("kstr_autopush: open failed error %d\n", error); 241 ldi_ident_release(li); 242 return (error); 243 } 244 } 245 ldi_ident_release(li); 246 247 switch (op) { 248 case GET_AUTOPUSH: 249 /* Get autopush information */ 250 251 push.sap_major = *maj; 252 push.sap_minor = *min; 253 254 error = ldi_ioctl(lh, SAD_GAP, (intptr_t)&push, 255 FKIOCTL, kcred, &rval); 256 if (error) { 257 printf("kstr_autopush: " 258 "ioctl(GET_AUTOPUSH) failed, error %d\n", error); 259 (void) ldi_close(lh, FREAD|FWRITE, kcred); 260 return (error); 261 } 262 switch (push.sap_cmd) { 263 case SAP_ONE: 264 *maj = push.sap_major; 265 *min = push.sap_minor; 266 *lastmin = 0; 267 break; 268 269 case SAP_RANGE: 270 *maj = push.sap_major; 271 *min = push.sap_minor; 272 *lastmin = push.sap_lastminor; 273 break; 274 275 case SAP_ALL: 276 *maj = push.sap_major; 277 *min = (minor_t)-1; 278 break; 279 } 280 281 if (anchor != NULL) 282 *anchor = push.sap_anchor; 283 284 if (push.sap_npush > 1) { 285 for (i = 0; i < push.sap_npush && 286 mods[i] != NULL; i++) 287 (void) strcpy(mods[i], push.sap_list[i]); 288 mods[i] = NULL; 289 } 290 (void) ldi_close(lh, FREAD|FWRITE, kcred); 291 return (0); 292 293 case CLR_AUTOPUSH: 294 /* Remove autopush information */ 295 296 push.sap_cmd = SAP_CLEAR; 297 push.sap_minor = *min; 298 push.sap_major = *maj; 299 300 error = ldi_ioctl(lh, SAD_SAP, (intptr_t)&push, 301 FKIOCTL, kcred, &rval); 302 if (error) { 303 printf("kstr_autopush: " 304 "ioctl(CLR_AUTOPUSH) failed, error %d\n", error); 305 } 306 (void) ldi_close(lh, FREAD|FWRITE, kcred); 307 return (error); 308 309 case SET_AUTOPUSH: 310 /* Set autopush information */ 311 312 if (*min == (minor_t)-1) { 313 push.sap_cmd = SAP_ALL; 314 } else if (*lastmin == 0) { 315 push.sap_cmd = SAP_ONE; 316 } else { 317 push.sap_cmd = SAP_RANGE; 318 } 319 320 if (anchor != NULL) 321 push.sap_anchor = *anchor; 322 else 323 push.sap_anchor = 0; 324 325 push.sap_minor = *min; 326 push.sap_major = *maj; 327 if (lastmin) 328 push.sap_lastminor = *lastmin; 329 else 330 push.sap_lastminor = 0; 331 332 /* pain */ 333 for (i = 0; i < MAXAPUSH && mods[i] != (char *)NULL; i++) { 334 (void) strcpy(push.sap_list[i], mods[i]); 335 } 336 push.sap_npush = i; 337 push.sap_list[i][0] = '\0'; 338 339 error = ldi_ioctl(lh, SAD_SAP, (intptr_t)&push, 340 FKIOCTL, kcred, &rval); 341 if (error) { 342 printf("kstr_autopush: " 343 "ioctl(SET_AUTOPUSH) failed, error %d\n", error); 344 } 345 (void) ldi_close(lh, FREAD|FWRITE, kcred); 346 return (error); 347 348 default: 349 (void) ldi_close(lh, FREAD|FWRITE, kcred); 350 return (EINVAL); 351 } 352 } 353