10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 52712Snn35248 * Common Development and Distribution License (the "License"). 62712Snn35248 * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 222712Snn35248 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include <sys/param.h> 290Sstevel@tonic-gate #include <sys/types.h> 300Sstevel@tonic-gate #include <sys/user.h> 310Sstevel@tonic-gate #include <sys/vfs.h> 320Sstevel@tonic-gate #include <sys/vnode.h> 330Sstevel@tonic-gate #include <sys/file.h> 340Sstevel@tonic-gate #include <sys/stream.h> 350Sstevel@tonic-gate #include <sys/stropts.h> 360Sstevel@tonic-gate #include <sys/strsubr.h> 370Sstevel@tonic-gate #include <sys/dlpi.h> 380Sstevel@tonic-gate #include <sys/vnode.h> 390Sstevel@tonic-gate #include <sys/socket.h> 400Sstevel@tonic-gate #include <sys/sockio.h> 410Sstevel@tonic-gate #include <sys/cmn_err.h> 420Sstevel@tonic-gate #include <net/if.h> 430Sstevel@tonic-gate #include <sys/sad.h> 440Sstevel@tonic-gate #include <sys/kstr.h> 450Sstevel@tonic-gate #include <sys/ddi.h> 460Sstevel@tonic-gate #include <sys/sunddi.h> 470Sstevel@tonic-gate #include <sys/sunldi.h> 480Sstevel@tonic-gate 490Sstevel@tonic-gate #include <sys/cred.h> 500Sstevel@tonic-gate #include <sys/sysmacros.h> 510Sstevel@tonic-gate 520Sstevel@tonic-gate #include <sys/modctl.h> 530Sstevel@tonic-gate 540Sstevel@tonic-gate /* 550Sstevel@tonic-gate * Routines to allow strplumb() legitimate access 560Sstevel@tonic-gate * to the kernel. 570Sstevel@tonic-gate */ 580Sstevel@tonic-gate int 590Sstevel@tonic-gate kstr_open(major_t maj, minor_t min, vnode_t **vpp, int *fd) 600Sstevel@tonic-gate { 610Sstevel@tonic-gate vnode_t *vp; 620Sstevel@tonic-gate int error; 630Sstevel@tonic-gate 640Sstevel@tonic-gate vp = makespecvp(makedevice(maj, min), VCHR); 650Sstevel@tonic-gate 660Sstevel@tonic-gate /* 670Sstevel@tonic-gate * Fix for 4170365: only allocate file descriptor entry 680Sstevel@tonic-gate * if file descriptor is to be returned; otherwise VOP_OPEN. 690Sstevel@tonic-gate */ 700Sstevel@tonic-gate if (fd != NULL) 710Sstevel@tonic-gate error = fassign(&vp, FREAD|FWRITE, fd); 720Sstevel@tonic-gate else 730Sstevel@tonic-gate error = VOP_OPEN(&vp, FREAD|FWRITE, CRED()); 740Sstevel@tonic-gate 750Sstevel@tonic-gate /* 760Sstevel@tonic-gate * Must set vpp after calling fassign()/VOP_OPEN() 770Sstevel@tonic-gate * since `vp' might change if it's a clone driver. 780Sstevel@tonic-gate */ 790Sstevel@tonic-gate if (vpp != NULL) 800Sstevel@tonic-gate *vpp = vp; 810Sstevel@tonic-gate 820Sstevel@tonic-gate return (error); 830Sstevel@tonic-gate } 840Sstevel@tonic-gate 850Sstevel@tonic-gate int 860Sstevel@tonic-gate kstr_plink(vnode_t *vp, int fd, int *mux_id) 870Sstevel@tonic-gate { 880Sstevel@tonic-gate int id; 890Sstevel@tonic-gate int error; 900Sstevel@tonic-gate 910Sstevel@tonic-gate if (error = strioctl(vp, I_PLINK, (intptr_t)fd, 0, K_TO_K, CRED(), &id)) 920Sstevel@tonic-gate return (error); 930Sstevel@tonic-gate if (mux_id) 940Sstevel@tonic-gate *mux_id = id; 950Sstevel@tonic-gate return (0); 960Sstevel@tonic-gate } 970Sstevel@tonic-gate 980Sstevel@tonic-gate int 990Sstevel@tonic-gate kstr_unplink(vnode_t *vp, int mux_id) 1000Sstevel@tonic-gate { 1010Sstevel@tonic-gate int rval; 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate return (strioctl(vp, I_PUNLINK, (intptr_t)mux_id, 0, 1040Sstevel@tonic-gate K_TO_K, CRED(), &rval)); 1050Sstevel@tonic-gate } 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate int 1080Sstevel@tonic-gate kstr_push(vnode_t *vp, char *mod) 1090Sstevel@tonic-gate { 1100Sstevel@tonic-gate int rval; 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate return (strioctl(vp, I_PUSH, (intptr_t)mod, 0, K_TO_K, CRED(), &rval)); 1130Sstevel@tonic-gate } 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate int 1160Sstevel@tonic-gate kstr_pop(vnode_t *vp) 1170Sstevel@tonic-gate { 1180Sstevel@tonic-gate int rval; 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate return (strioctl(vp, I_POP, 0, 0, K_TO_K, CRED(), &rval)); 1210Sstevel@tonic-gate } 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate int 1240Sstevel@tonic-gate kstr_close(vnode_t *vp, int fd) 1250Sstevel@tonic-gate { 1260Sstevel@tonic-gate int ret; 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate if (vp == (vnode_t *)NULL && fd == -1) 1290Sstevel@tonic-gate return (EINVAL); 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate if (fd != -1) { 1320Sstevel@tonic-gate if (closeandsetf(fd, NULL) == 0) { 1330Sstevel@tonic-gate return (0); 1340Sstevel@tonic-gate } else { 1350Sstevel@tonic-gate return (EINVAL); 1360Sstevel@tonic-gate } 1370Sstevel@tonic-gate } else { 1380Sstevel@tonic-gate ret = VOP_CLOSE(vp, FREAD|FWRITE, 1, (offset_t)0, CRED()); 1390Sstevel@tonic-gate VN_RELE(vp); 1400Sstevel@tonic-gate return (ret); 1410Sstevel@tonic-gate } 1420Sstevel@tonic-gate } 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate int 1450Sstevel@tonic-gate kstr_ioctl(struct vnode *vp, int cmd, intptr_t arg) 1460Sstevel@tonic-gate { 1470Sstevel@tonic-gate int rval; 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate return (strioctl(vp, cmd, arg, 0, K_TO_K, CRED(), &rval)); 1500Sstevel@tonic-gate } 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate /* 1530Sstevel@tonic-gate * Optionally send data (if smp set) and optionally receive data (if rmp is 1540Sstevel@tonic-gate * set). If timeo is NULL the reception will sleep until a message is 1550Sstevel@tonic-gate * received; otherwise the sleep is limited to the specified amount of time. 1560Sstevel@tonic-gate */ 1570Sstevel@tonic-gate int 1580Sstevel@tonic-gate kstr_msg(vnode_t *vp, mblk_t *smp, mblk_t **rmp, timestruc_t *timeo) 1590Sstevel@tonic-gate { 1600Sstevel@tonic-gate int error; 1610Sstevel@tonic-gate clock_t timout; /* milliseconds */ 1620Sstevel@tonic-gate uchar_t pri; 1630Sstevel@tonic-gate int pflag; 1640Sstevel@tonic-gate rval_t rval; 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate if (rmp == NULL && timeo != NULL && 1670Sstevel@tonic-gate (timeo->tv_sec != 0 || timeo->tv_nsec != 0)) 1680Sstevel@tonic-gate return (EINVAL); 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate if (smp == NULL && rmp == NULL) 1710Sstevel@tonic-gate return (EINVAL); 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate if (smp != NULL) { 1740Sstevel@tonic-gate /* Send message while honoring flow control */ 1750Sstevel@tonic-gate (void) kstrputmsg(vp, smp, NULL, 0, 0, 1760Sstevel@tonic-gate MSG_BAND | MSG_HOLDSIG | MSG_IGNERROR, 0); 1770Sstevel@tonic-gate } 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate if (rmp == NULL) { 1800Sstevel@tonic-gate /* No reply wanted by caller */ 1810Sstevel@tonic-gate return (0); 1820Sstevel@tonic-gate } 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate /* 1850Sstevel@tonic-gate * Convert from nanoseconds to milliseconds. 1860Sstevel@tonic-gate */ 1870Sstevel@tonic-gate if (timeo != NULL) { 1880Sstevel@tonic-gate timout = timeo->tv_sec * 1000 + timeo->tv_nsec / 1000000; 1890Sstevel@tonic-gate if (timout > INT_MAX) 1900Sstevel@tonic-gate return (EINVAL); 1910Sstevel@tonic-gate } else 1920Sstevel@tonic-gate timout = -1; 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate /* Wait for timeout millseconds for a message */ 1950Sstevel@tonic-gate pflag = MSG_ANY; 1960Sstevel@tonic-gate pri = 0; 1970Sstevel@tonic-gate *rmp = NULL; 1980Sstevel@tonic-gate error = kstrgetmsg(vp, rmp, NULL, &pri, &pflag, timout, &rval); 1990Sstevel@tonic-gate /* Callers use *rmp == NULL to determine that there was a timeout */ 2000Sstevel@tonic-gate if (error == ETIME) 2010Sstevel@tonic-gate error = 0; 2020Sstevel@tonic-gate return (error); 2030Sstevel@tonic-gate } 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate #define SAD_ADM "/devices/pseudo/sad@0:admin" 2060Sstevel@tonic-gate #define SAD_USR "/devices/pseudo/sad@0:user" 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate /* 2090Sstevel@tonic-gate * It is the callers responsibility to make sure that "mods" 2100Sstevel@tonic-gate * conforms to what is required. We do not check it here. 2110Sstevel@tonic-gate * 2120Sstevel@tonic-gate * "maj", "min", and "lastmin" are value-result parameters. 2130Sstevel@tonic-gate * for SET_AUTOPUSH, "anchor" should be set to the place in the stream 2140Sstevel@tonic-gate * to put the anchor, or NULL if no anchor needs to be set. 2150Sstevel@tonic-gate * for GET_AUTOPUSH, "anchor" should point to a uint_t to store the 2160Sstevel@tonic-gate * position of the anchor at, or NULL if the caller is not interested. 2170Sstevel@tonic-gate */ 2180Sstevel@tonic-gate int 2190Sstevel@tonic-gate kstr_autopush(int op, major_t *maj, minor_t *min, minor_t *lastmin, 2200Sstevel@tonic-gate uint_t *anchor, char *mods[]) 2210Sstevel@tonic-gate { 2220Sstevel@tonic-gate ldi_handle_t lh; 2230Sstevel@tonic-gate ldi_ident_t li; 2240Sstevel@tonic-gate struct strapush push; 2250Sstevel@tonic-gate int i, error, rval; 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate li = ldi_ident_from_anon(); 2280Sstevel@tonic-gate if (op == SET_AUTOPUSH || op == CLR_AUTOPUSH) { 2290Sstevel@tonic-gate error = ldi_open_by_name(SAD_ADM, FREAD|FWRITE, 2302712Snn35248 kcred, &lh, li); 2310Sstevel@tonic-gate if (error) { 2320Sstevel@tonic-gate printf("kstr_autopush: open failed error %d\n", error); 2330Sstevel@tonic-gate ldi_ident_release(li); 2340Sstevel@tonic-gate return (error); 2350Sstevel@tonic-gate } 2360Sstevel@tonic-gate } else { 2370Sstevel@tonic-gate error = ldi_open_by_name(SAD_USR, FREAD|FWRITE, 2382712Snn35248 kcred, &lh, li); 2390Sstevel@tonic-gate if (error) { 2400Sstevel@tonic-gate printf("kstr_autopush: open failed error %d\n", error); 2410Sstevel@tonic-gate ldi_ident_release(li); 2420Sstevel@tonic-gate return (error); 2430Sstevel@tonic-gate } 2440Sstevel@tonic-gate } 2450Sstevel@tonic-gate ldi_ident_release(li); 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate switch (op) { 2480Sstevel@tonic-gate case GET_AUTOPUSH: 2490Sstevel@tonic-gate /* Get autopush information */ 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate push.sap_major = *maj; 2520Sstevel@tonic-gate push.sap_minor = *min; 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate error = ldi_ioctl(lh, SAD_GAP, (intptr_t)&push, 2552712Snn35248 FKIOCTL, kcred, &rval); 2560Sstevel@tonic-gate if (error) { 257*3042Sedp printf("kstr_autopush: " 258*3042Sedp "ioctl(GET_AUTOPUSH) failed, error %d\n", error); 2592712Snn35248 (void) ldi_close(lh, FREAD|FWRITE, kcred); 2600Sstevel@tonic-gate return (error); 2610Sstevel@tonic-gate } 2620Sstevel@tonic-gate switch (push.sap_cmd) { 2630Sstevel@tonic-gate case SAP_ONE: 2640Sstevel@tonic-gate *maj = push.sap_major; 2650Sstevel@tonic-gate *min = push.sap_minor; 2660Sstevel@tonic-gate *lastmin = 0; 2670Sstevel@tonic-gate break; 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate case SAP_RANGE: 2700Sstevel@tonic-gate *maj = push.sap_major; 2710Sstevel@tonic-gate *min = push.sap_minor; 2720Sstevel@tonic-gate *lastmin = push.sap_lastminor; 2730Sstevel@tonic-gate break; 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate case SAP_ALL: 2760Sstevel@tonic-gate *maj = push.sap_major; 2770Sstevel@tonic-gate *min = (minor_t)-1; 2780Sstevel@tonic-gate break; 2790Sstevel@tonic-gate } 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate if (anchor != NULL) 2820Sstevel@tonic-gate *anchor = push.sap_anchor; 2830Sstevel@tonic-gate 2840Sstevel@tonic-gate if (push.sap_npush > 1) { 2850Sstevel@tonic-gate for (i = 0; i < push.sap_npush && 2860Sstevel@tonic-gate mods[i] != NULL; i++) 2870Sstevel@tonic-gate (void) strcpy(mods[i], push.sap_list[i]); 2880Sstevel@tonic-gate mods[i] = NULL; 2890Sstevel@tonic-gate } 2902712Snn35248 (void) ldi_close(lh, FREAD|FWRITE, kcred); 2910Sstevel@tonic-gate return (0); 2920Sstevel@tonic-gate 2930Sstevel@tonic-gate case CLR_AUTOPUSH: 2940Sstevel@tonic-gate /* Remove autopush information */ 2950Sstevel@tonic-gate 2960Sstevel@tonic-gate push.sap_cmd = SAP_CLEAR; 2970Sstevel@tonic-gate push.sap_minor = *min; 2980Sstevel@tonic-gate push.sap_major = *maj; 2990Sstevel@tonic-gate 3000Sstevel@tonic-gate error = ldi_ioctl(lh, SAD_SAP, (intptr_t)&push, 3012712Snn35248 FKIOCTL, kcred, &rval); 3020Sstevel@tonic-gate if (error) { 303*3042Sedp printf("kstr_autopush: " 304*3042Sedp "ioctl(CLR_AUTOPUSH) failed, error %d\n", error); 3050Sstevel@tonic-gate } 3062712Snn35248 (void) ldi_close(lh, FREAD|FWRITE, kcred); 3070Sstevel@tonic-gate return (error); 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate case SET_AUTOPUSH: 3100Sstevel@tonic-gate /* Set autopush information */ 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate if (*min == (minor_t)-1) { 3130Sstevel@tonic-gate push.sap_cmd = SAP_ALL; 3140Sstevel@tonic-gate } else if (*lastmin == 0) { 3150Sstevel@tonic-gate push.sap_cmd = SAP_ONE; 3160Sstevel@tonic-gate } else { 3170Sstevel@tonic-gate push.sap_cmd = SAP_RANGE; 3180Sstevel@tonic-gate } 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate if (anchor != NULL) 3210Sstevel@tonic-gate push.sap_anchor = *anchor; 3220Sstevel@tonic-gate else 3230Sstevel@tonic-gate push.sap_anchor = 0; 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate push.sap_minor = *min; 3260Sstevel@tonic-gate push.sap_major = *maj; 3270Sstevel@tonic-gate if (lastmin) 3280Sstevel@tonic-gate push.sap_lastminor = *lastmin; 3290Sstevel@tonic-gate else 3300Sstevel@tonic-gate push.sap_lastminor = 0; 3310Sstevel@tonic-gate 3320Sstevel@tonic-gate /* pain */ 3330Sstevel@tonic-gate for (i = 0; i < MAXAPUSH && mods[i] != (char *)NULL; i++) { 3340Sstevel@tonic-gate (void) strcpy(push.sap_list[i], mods[i]); 3350Sstevel@tonic-gate } 3360Sstevel@tonic-gate push.sap_npush = i; 3370Sstevel@tonic-gate push.sap_list[i][0] = '\0'; 3380Sstevel@tonic-gate 3390Sstevel@tonic-gate error = ldi_ioctl(lh, SAD_SAP, (intptr_t)&push, 3402712Snn35248 FKIOCTL, kcred, &rval); 3410Sstevel@tonic-gate if (error) { 342*3042Sedp printf("kstr_autopush: " 343*3042Sedp "ioctl(SET_AUTOPUSH) failed, error %d\n", error); 3440Sstevel@tonic-gate } 3452712Snn35248 (void) ldi_close(lh, FREAD|FWRITE, kcred); 3460Sstevel@tonic-gate return (error); 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate default: 3492712Snn35248 (void) ldi_close(lh, FREAD|FWRITE, kcred); 3500Sstevel@tonic-gate return (EINVAL); 3510Sstevel@tonic-gate } 3520Sstevel@tonic-gate } 353