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