1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/stream.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/fs/fifonode.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/socketvar.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate /*
47*7c478bd9Sstevel@tonic-gate  * STREAMS system calls.
48*7c478bd9Sstevel@tonic-gate  */
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate int getmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int *flagsp);
51*7c478bd9Sstevel@tonic-gate int putmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int flags);
52*7c478bd9Sstevel@tonic-gate int getpmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int *prip,
53*7c478bd9Sstevel@tonic-gate     int *flagsp);
54*7c478bd9Sstevel@tonic-gate int putpmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int pri,
55*7c478bd9Sstevel@tonic-gate     int flags);
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate static int msgio(int fdes, struct strbuf *ctl, struct strbuf *data, int *rval,
58*7c478bd9Sstevel@tonic-gate     int mode, unsigned char *prip, int *flagsp);
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate int
getmsg(int fdes,struct strbuf * ctl,struct strbuf * data,int * flagsp)61*7c478bd9Sstevel@tonic-gate getmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int *flagsp)
62*7c478bd9Sstevel@tonic-gate {
63*7c478bd9Sstevel@tonic-gate 	int error;
64*7c478bd9Sstevel@tonic-gate 	int localflags;
65*7c478bd9Sstevel@tonic-gate 	int realflags = 0;
66*7c478bd9Sstevel@tonic-gate 	unsigned char pri = 0;
67*7c478bd9Sstevel@tonic-gate 	int rv = 0;
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	/*
70*7c478bd9Sstevel@tonic-gate 	 * Convert between old flags (localflags) and new flags (realflags).
71*7c478bd9Sstevel@tonic-gate 	 */
72*7c478bd9Sstevel@tonic-gate 	if (copyin(flagsp, &localflags, sizeof (*flagsp)))
73*7c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
74*7c478bd9Sstevel@tonic-gate 	switch (localflags) {
75*7c478bd9Sstevel@tonic-gate 	case 0:
76*7c478bd9Sstevel@tonic-gate 		realflags = MSG_ANY;
77*7c478bd9Sstevel@tonic-gate 		break;
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	case RS_HIPRI:
80*7c478bd9Sstevel@tonic-gate 		realflags = MSG_HIPRI;
81*7c478bd9Sstevel@tonic-gate 		break;
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 	default:
84*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
85*7c478bd9Sstevel@tonic-gate 	}
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	if ((error = msgio(fdes, ctl, data, &rv, FREAD, &pri,
88*7c478bd9Sstevel@tonic-gate 	    &realflags)) == 0) {
89*7c478bd9Sstevel@tonic-gate 		/*
90*7c478bd9Sstevel@tonic-gate 		 * massage realflags based on localflags.
91*7c478bd9Sstevel@tonic-gate 		 */
92*7c478bd9Sstevel@tonic-gate 		if (realflags == MSG_HIPRI)
93*7c478bd9Sstevel@tonic-gate 			localflags = RS_HIPRI;
94*7c478bd9Sstevel@tonic-gate 		else
95*7c478bd9Sstevel@tonic-gate 			localflags = 0;
96*7c478bd9Sstevel@tonic-gate 		if (copyout(&localflags, flagsp, sizeof (*flagsp)))
97*7c478bd9Sstevel@tonic-gate 			error = EFAULT;
98*7c478bd9Sstevel@tonic-gate 	}
99*7c478bd9Sstevel@tonic-gate 	if (error != 0)
100*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
101*7c478bd9Sstevel@tonic-gate 	return (rv);
102*7c478bd9Sstevel@tonic-gate }
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate int
putmsg(int fdes,struct strbuf * ctl,struct strbuf * data,int flags)105*7c478bd9Sstevel@tonic-gate putmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int flags)
106*7c478bd9Sstevel@tonic-gate {
107*7c478bd9Sstevel@tonic-gate 	unsigned char pri = 0;
108*7c478bd9Sstevel@tonic-gate 	int realflags;
109*7c478bd9Sstevel@tonic-gate 	int error;
110*7c478bd9Sstevel@tonic-gate 	int rv = 0;
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	switch (flags) {
113*7c478bd9Sstevel@tonic-gate 	case RS_HIPRI:
114*7c478bd9Sstevel@tonic-gate 		realflags = MSG_HIPRI;
115*7c478bd9Sstevel@tonic-gate 		break;
116*7c478bd9Sstevel@tonic-gate 	case (RS_HIPRI|MSG_XPG4):
117*7c478bd9Sstevel@tonic-gate 		realflags = MSG_HIPRI|MSG_XPG4;
118*7c478bd9Sstevel@tonic-gate 		break;
119*7c478bd9Sstevel@tonic-gate 	case MSG_XPG4:
120*7c478bd9Sstevel@tonic-gate 		realflags = MSG_BAND|MSG_XPG4;
121*7c478bd9Sstevel@tonic-gate 		break;
122*7c478bd9Sstevel@tonic-gate 	case 0:
123*7c478bd9Sstevel@tonic-gate 		realflags = MSG_BAND;
124*7c478bd9Sstevel@tonic-gate 		break;
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	default:
127*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
128*7c478bd9Sstevel@tonic-gate 	}
129*7c478bd9Sstevel@tonic-gate 	error = msgio(fdes, ctl, data, &rv, FWRITE, &pri, &realflags);
130*7c478bd9Sstevel@tonic-gate 	if (error != 0)
131*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
132*7c478bd9Sstevel@tonic-gate 	return (rv);
133*7c478bd9Sstevel@tonic-gate }
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate int
getpmsg(int fdes,struct strbuf * ctl,struct strbuf * data,int * prip,int * flagsp)137*7c478bd9Sstevel@tonic-gate getpmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int *prip,
138*7c478bd9Sstevel@tonic-gate     int *flagsp)
139*7c478bd9Sstevel@tonic-gate {
140*7c478bd9Sstevel@tonic-gate 	int error;
141*7c478bd9Sstevel@tonic-gate 	int flags;
142*7c478bd9Sstevel@tonic-gate 	int intpri;
143*7c478bd9Sstevel@tonic-gate 	unsigned char pri;
144*7c478bd9Sstevel@tonic-gate 	int rv = 0;
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 	if (copyin(flagsp, &flags, sizeof (flags)))
147*7c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
148*7c478bd9Sstevel@tonic-gate 	if (copyin(prip, &intpri, sizeof (intpri)))
149*7c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
150*7c478bd9Sstevel@tonic-gate 	if ((intpri > 255) || (intpri < 0))
151*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
152*7c478bd9Sstevel@tonic-gate 	pri = (unsigned char)intpri;
153*7c478bd9Sstevel@tonic-gate 	error = msgio(fdes, ctl, data, &rv, FREAD, &pri, &flags);
154*7c478bd9Sstevel@tonic-gate 	if (error != 0)
155*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
156*7c478bd9Sstevel@tonic-gate 	if (copyout(&flags, flagsp, sizeof (flags)))
157*7c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
158*7c478bd9Sstevel@tonic-gate 	intpri = (int)pri;
159*7c478bd9Sstevel@tonic-gate 	if (copyout(&intpri, prip, sizeof (intpri)))
160*7c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
161*7c478bd9Sstevel@tonic-gate 	return (rv);
162*7c478bd9Sstevel@tonic-gate }
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate int
putpmsg(int fdes,struct strbuf * ctl,struct strbuf * data,int intpri,int flags)165*7c478bd9Sstevel@tonic-gate putpmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int intpri,
166*7c478bd9Sstevel@tonic-gate     int flags)
167*7c478bd9Sstevel@tonic-gate {
168*7c478bd9Sstevel@tonic-gate 	unsigned char pri;
169*7c478bd9Sstevel@tonic-gate 	int rv = 0;
170*7c478bd9Sstevel@tonic-gate 	int error;
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 	if ((intpri > 255) || (intpri < 0))
173*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
174*7c478bd9Sstevel@tonic-gate 	pri = (unsigned char)intpri;
175*7c478bd9Sstevel@tonic-gate 	error = msgio(fdes, ctl, data, &rv, FWRITE, &pri, &flags);
176*7c478bd9Sstevel@tonic-gate 	if (error != 0)
177*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
178*7c478bd9Sstevel@tonic-gate 	return (rv);
179*7c478bd9Sstevel@tonic-gate }
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate /*
182*7c478bd9Sstevel@tonic-gate  * Common code for getmsg and putmsg calls: check permissions,
183*7c478bd9Sstevel@tonic-gate  * copy in args, do preliminary setup, and switch to
184*7c478bd9Sstevel@tonic-gate  * appropriate stream routine.
185*7c478bd9Sstevel@tonic-gate  */
186*7c478bd9Sstevel@tonic-gate static int
msgio(int fdes,struct strbuf * ctl,struct strbuf * data,int * rval,int mode,unsigned char * prip,int * flagsp)187*7c478bd9Sstevel@tonic-gate msgio(int fdes, struct strbuf *ctl, struct strbuf *data, int *rval,
188*7c478bd9Sstevel@tonic-gate     int mode, unsigned char *prip, int *flagsp)
189*7c478bd9Sstevel@tonic-gate {
190*7c478bd9Sstevel@tonic-gate 	file_t *fp;
191*7c478bd9Sstevel@tonic-gate 	vnode_t *vp;
192*7c478bd9Sstevel@tonic-gate 	struct strbuf msgctl, msgdata;
193*7c478bd9Sstevel@tonic-gate 	int error;
194*7c478bd9Sstevel@tonic-gate 	int flag;
195*7c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
196*7c478bd9Sstevel@tonic-gate 	rval_t rv;
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 	if ((fp = getf(fdes)) == NULL)
199*7c478bd9Sstevel@tonic-gate 		return (EBADF);
200*7c478bd9Sstevel@tonic-gate 	if ((fp->f_flag & mode) == 0) {
201*7c478bd9Sstevel@tonic-gate 		releasef(fdes);
202*7c478bd9Sstevel@tonic-gate 		return (EBADF);
203*7c478bd9Sstevel@tonic-gate 	}
204*7c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
205*7c478bd9Sstevel@tonic-gate 	if (vp->v_type == VFIFO) {
206*7c478bd9Sstevel@tonic-gate 		if (vp->v_stream) {
207*7c478bd9Sstevel@tonic-gate 			/*
208*7c478bd9Sstevel@tonic-gate 			 * must use sd_vnode, could be named pipe
209*7c478bd9Sstevel@tonic-gate 			 */
210*7c478bd9Sstevel@tonic-gate 			(void) fifo_vfastoff(vp->v_stream->sd_vnode);
211*7c478bd9Sstevel@tonic-gate 		} else {
212*7c478bd9Sstevel@tonic-gate 			releasef(fdes);
213*7c478bd9Sstevel@tonic-gate 			return (ENOSTR);
214*7c478bd9Sstevel@tonic-gate 		}
215*7c478bd9Sstevel@tonic-gate 	} else if ((vp->v_type != VCHR && vp->v_type != VSOCK) ||
216*7c478bd9Sstevel@tonic-gate 		    vp->v_stream == NULL) {
217*7c478bd9Sstevel@tonic-gate 		releasef(fdes);
218*7c478bd9Sstevel@tonic-gate 		return (ENOSTR);
219*7c478bd9Sstevel@tonic-gate 	}
220*7c478bd9Sstevel@tonic-gate 	if ((ctl != NULL) &&
221*7c478bd9Sstevel@tonic-gate 	    copyin(ctl, &msgctl, sizeof (struct strbuf))) {
222*7c478bd9Sstevel@tonic-gate 		releasef(fdes);
223*7c478bd9Sstevel@tonic-gate 		return (EFAULT);
224*7c478bd9Sstevel@tonic-gate 	}
225*7c478bd9Sstevel@tonic-gate 	if ((data != NULL) &&
226*7c478bd9Sstevel@tonic-gate 	    copyin(data, &msgdata, sizeof (struct strbuf))) {
227*7c478bd9Sstevel@tonic-gate 		releasef(fdes);
228*7c478bd9Sstevel@tonic-gate 		return (EFAULT);
229*7c478bd9Sstevel@tonic-gate 	}
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate 	if (mode == FREAD) {
232*7c478bd9Sstevel@tonic-gate 		if (ctl == NULL)
233*7c478bd9Sstevel@tonic-gate 			msgctl.maxlen = -1;
234*7c478bd9Sstevel@tonic-gate 		if (data == NULL)
235*7c478bd9Sstevel@tonic-gate 			msgdata.maxlen = -1;
236*7c478bd9Sstevel@tonic-gate 		flag = fp->f_flag;
237*7c478bd9Sstevel@tonic-gate 		rv.r_val1 = 0;
238*7c478bd9Sstevel@tonic-gate 		if (vp->v_type == VSOCK) {
239*7c478bd9Sstevel@tonic-gate 			error = sock_getmsg(vp, &msgctl, &msgdata, prip,
240*7c478bd9Sstevel@tonic-gate 			    flagsp, flag, &rv);
241*7c478bd9Sstevel@tonic-gate 		} else {
242*7c478bd9Sstevel@tonic-gate 			error = strgetmsg(vp, &msgctl, &msgdata, prip,
243*7c478bd9Sstevel@tonic-gate 			    flagsp, flag, &rv);
244*7c478bd9Sstevel@tonic-gate 		}
245*7c478bd9Sstevel@tonic-gate 		*rval = rv.r_val1;
246*7c478bd9Sstevel@tonic-gate 		if (error != 0) {
247*7c478bd9Sstevel@tonic-gate 			releasef(fdes);
248*7c478bd9Sstevel@tonic-gate 			return (error);
249*7c478bd9Sstevel@tonic-gate 		}
250*7c478bd9Sstevel@tonic-gate 		if (lwp != NULL)
251*7c478bd9Sstevel@tonic-gate 			lwp->lwp_ru.msgrcv++;
252*7c478bd9Sstevel@tonic-gate 		if (((ctl != NULL) &&
253*7c478bd9Sstevel@tonic-gate 		    copyout(&msgctl, ctl, sizeof (struct strbuf))) ||
254*7c478bd9Sstevel@tonic-gate 		    ((data != NULL) &&
255*7c478bd9Sstevel@tonic-gate 		    copyout(&msgdata, data, sizeof (struct strbuf)))) {
256*7c478bd9Sstevel@tonic-gate 			releasef(fdes);
257*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
258*7c478bd9Sstevel@tonic-gate 		}
259*7c478bd9Sstevel@tonic-gate 		releasef(fdes);
260*7c478bd9Sstevel@tonic-gate 		return (0);
261*7c478bd9Sstevel@tonic-gate 	}
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 	/*
264*7c478bd9Sstevel@tonic-gate 	 * FWRITE case
265*7c478bd9Sstevel@tonic-gate 	 */
266*7c478bd9Sstevel@tonic-gate 	if (ctl == NULL)
267*7c478bd9Sstevel@tonic-gate 		msgctl.len = -1;
268*7c478bd9Sstevel@tonic-gate 	if (data == NULL)
269*7c478bd9Sstevel@tonic-gate 		msgdata.len = -1;
270*7c478bd9Sstevel@tonic-gate 	flag = fp->f_flag;
271*7c478bd9Sstevel@tonic-gate 	if (vp->v_type == VSOCK) {
272*7c478bd9Sstevel@tonic-gate 		error = sock_putmsg(vp, &msgctl, &msgdata, *prip, *flagsp,
273*7c478bd9Sstevel@tonic-gate 		    flag);
274*7c478bd9Sstevel@tonic-gate 	} else {
275*7c478bd9Sstevel@tonic-gate 		error = strputmsg(vp, &msgctl, &msgdata, *prip, *flagsp, flag);
276*7c478bd9Sstevel@tonic-gate 	}
277*7c478bd9Sstevel@tonic-gate 	releasef(fdes);
278*7c478bd9Sstevel@tonic-gate 	if (error == 0 && lwp != NULL)
279*7c478bd9Sstevel@tonic-gate 		lwp->lwp_ru.msgsnd++;
280*7c478bd9Sstevel@tonic-gate 	return (error);
281*7c478bd9Sstevel@tonic-gate }
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate 
284*7c478bd9Sstevel@tonic-gate #if defined(_LP64) && defined(_SYSCALL32)
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate static int msgio32(int fdes, struct strbuf32 *ctl, struct strbuf32 *data,
287*7c478bd9Sstevel@tonic-gate     int *rval, int mode, unsigned char *prip, int *flagsp);
288*7c478bd9Sstevel@tonic-gate 
289*7c478bd9Sstevel@tonic-gate int
getmsg32(int fdes,struct strbuf32 * ctl,struct strbuf32 * data,int32_t * flagsp)290*7c478bd9Sstevel@tonic-gate getmsg32(int fdes, struct strbuf32 *ctl, struct strbuf32 *data, int32_t *flagsp)
291*7c478bd9Sstevel@tonic-gate {
292*7c478bd9Sstevel@tonic-gate 	int error;
293*7c478bd9Sstevel@tonic-gate 	int32_t localflags;
294*7c478bd9Sstevel@tonic-gate 	int realflags = 0;
295*7c478bd9Sstevel@tonic-gate 	unsigned char pri = 0;
296*7c478bd9Sstevel@tonic-gate 	int rv = 0;
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 	/*
299*7c478bd9Sstevel@tonic-gate 	 * Convert between old flags (localflags) and new flags (realflags).
300*7c478bd9Sstevel@tonic-gate 	 */
301*7c478bd9Sstevel@tonic-gate 	if (copyin(flagsp, &localflags, sizeof (*flagsp)))
302*7c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
303*7c478bd9Sstevel@tonic-gate 	switch (localflags) {
304*7c478bd9Sstevel@tonic-gate 	case 0:
305*7c478bd9Sstevel@tonic-gate 		realflags = MSG_ANY;
306*7c478bd9Sstevel@tonic-gate 		break;
307*7c478bd9Sstevel@tonic-gate 
308*7c478bd9Sstevel@tonic-gate 	case RS_HIPRI:
309*7c478bd9Sstevel@tonic-gate 		realflags = MSG_HIPRI;
310*7c478bd9Sstevel@tonic-gate 		break;
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate 	default:
313*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
314*7c478bd9Sstevel@tonic-gate 	}
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate 	if ((error = msgio32(fdes, ctl, data, &rv, FREAD, &pri,
317*7c478bd9Sstevel@tonic-gate 	    &realflags)) == 0) {
318*7c478bd9Sstevel@tonic-gate 		/*
319*7c478bd9Sstevel@tonic-gate 		 * massage realflags based on localflags.
320*7c478bd9Sstevel@tonic-gate 		 */
321*7c478bd9Sstevel@tonic-gate 		if (realflags == MSG_HIPRI)
322*7c478bd9Sstevel@tonic-gate 			localflags = RS_HIPRI;
323*7c478bd9Sstevel@tonic-gate 		else
324*7c478bd9Sstevel@tonic-gate 			localflags = 0;
325*7c478bd9Sstevel@tonic-gate 		if (copyout(&localflags, flagsp, sizeof (*flagsp)))
326*7c478bd9Sstevel@tonic-gate 			error = EFAULT;
327*7c478bd9Sstevel@tonic-gate 	}
328*7c478bd9Sstevel@tonic-gate 	if (error != 0)
329*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
330*7c478bd9Sstevel@tonic-gate 	return (rv);
331*7c478bd9Sstevel@tonic-gate }
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate int
putmsg32(int fdes,struct strbuf32 * ctl,struct strbuf32 * data,int32_t flags)334*7c478bd9Sstevel@tonic-gate putmsg32(int fdes, struct strbuf32 *ctl, struct strbuf32 *data, int32_t flags)
335*7c478bd9Sstevel@tonic-gate {
336*7c478bd9Sstevel@tonic-gate 	unsigned char pri = 0;
337*7c478bd9Sstevel@tonic-gate 	int realflags;
338*7c478bd9Sstevel@tonic-gate 	int error;
339*7c478bd9Sstevel@tonic-gate 	int rv = 0;
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 	switch (flags) {
342*7c478bd9Sstevel@tonic-gate 	case RS_HIPRI:
343*7c478bd9Sstevel@tonic-gate 		realflags = MSG_HIPRI;
344*7c478bd9Sstevel@tonic-gate 		break;
345*7c478bd9Sstevel@tonic-gate 	case (RS_HIPRI|MSG_XPG4):
346*7c478bd9Sstevel@tonic-gate 		realflags = MSG_HIPRI|MSG_XPG4;
347*7c478bd9Sstevel@tonic-gate 		break;
348*7c478bd9Sstevel@tonic-gate 	case MSG_XPG4:
349*7c478bd9Sstevel@tonic-gate 		realflags = MSG_BAND|MSG_XPG4;
350*7c478bd9Sstevel@tonic-gate 		break;
351*7c478bd9Sstevel@tonic-gate 	case 0:
352*7c478bd9Sstevel@tonic-gate 		realflags = MSG_BAND;
353*7c478bd9Sstevel@tonic-gate 		break;
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate 	default:
356*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
357*7c478bd9Sstevel@tonic-gate 	}
358*7c478bd9Sstevel@tonic-gate 	error = msgio32(fdes, ctl, data, &rv, FWRITE, &pri, &realflags);
359*7c478bd9Sstevel@tonic-gate 	if (error != 0)
360*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
361*7c478bd9Sstevel@tonic-gate 	return (rv);
362*7c478bd9Sstevel@tonic-gate }
363*7c478bd9Sstevel@tonic-gate 
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate int
getpmsg32(int fdes,struct strbuf32 * ctl,struct strbuf32 * data,int32_t * prip,int32_t * flagsp)366*7c478bd9Sstevel@tonic-gate getpmsg32(int fdes, struct strbuf32 *ctl, struct strbuf32 *data, int32_t *prip,
367*7c478bd9Sstevel@tonic-gate     int32_t *flagsp)
368*7c478bd9Sstevel@tonic-gate {
369*7c478bd9Sstevel@tonic-gate 	int error;
370*7c478bd9Sstevel@tonic-gate 	int32_t flags;
371*7c478bd9Sstevel@tonic-gate 	int32_t intpri;
372*7c478bd9Sstevel@tonic-gate 	unsigned char pri;
373*7c478bd9Sstevel@tonic-gate 	int rv = 0;
374*7c478bd9Sstevel@tonic-gate 
375*7c478bd9Sstevel@tonic-gate 	if (copyin(flagsp, &flags, sizeof (*flagsp)))
376*7c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
377*7c478bd9Sstevel@tonic-gate 	if (copyin(prip, &intpri, sizeof (intpri)))
378*7c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
379*7c478bd9Sstevel@tonic-gate 	if ((intpri > 255) || (intpri < 0))
380*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
381*7c478bd9Sstevel@tonic-gate 	pri = (unsigned char)intpri;
382*7c478bd9Sstevel@tonic-gate 	error = msgio32(fdes, ctl, data, &rv, FREAD, &pri, &flags);
383*7c478bd9Sstevel@tonic-gate 	if (error != 0)
384*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
385*7c478bd9Sstevel@tonic-gate 	if (copyout(&flags, flagsp, sizeof (flags)))
386*7c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
387*7c478bd9Sstevel@tonic-gate 	intpri = (int)pri;
388*7c478bd9Sstevel@tonic-gate 	if (copyout(&intpri, prip, sizeof (intpri)))
389*7c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
390*7c478bd9Sstevel@tonic-gate 	return (rv);
391*7c478bd9Sstevel@tonic-gate }
392*7c478bd9Sstevel@tonic-gate 
393*7c478bd9Sstevel@tonic-gate int
putpmsg32(int fdes,struct strbuf32 * ctl,struct strbuf32 * data,int32_t intpri,int32_t flags)394*7c478bd9Sstevel@tonic-gate putpmsg32(int fdes, struct strbuf32 *ctl, struct strbuf32 *data, int32_t intpri,
395*7c478bd9Sstevel@tonic-gate     int32_t flags)
396*7c478bd9Sstevel@tonic-gate {
397*7c478bd9Sstevel@tonic-gate 	unsigned char pri;
398*7c478bd9Sstevel@tonic-gate 	int rv = 0;
399*7c478bd9Sstevel@tonic-gate 	int error;
400*7c478bd9Sstevel@tonic-gate 
401*7c478bd9Sstevel@tonic-gate 	if ((intpri > 255) || (intpri < 0))
402*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
403*7c478bd9Sstevel@tonic-gate 	pri = (unsigned char)intpri;
404*7c478bd9Sstevel@tonic-gate 	error = msgio32(fdes, ctl, data, &rv, FWRITE, &pri, &flags);
405*7c478bd9Sstevel@tonic-gate 	if (error != 0)
406*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
407*7c478bd9Sstevel@tonic-gate 	return (rv);
408*7c478bd9Sstevel@tonic-gate }
409*7c478bd9Sstevel@tonic-gate 
410*7c478bd9Sstevel@tonic-gate /*
411*7c478bd9Sstevel@tonic-gate  * Common code for getmsg and putmsg calls: check permissions,
412*7c478bd9Sstevel@tonic-gate  * copy in args, do preliminary setup, and switch to
413*7c478bd9Sstevel@tonic-gate  * appropriate stream routine.
414*7c478bd9Sstevel@tonic-gate  */
415*7c478bd9Sstevel@tonic-gate static int
msgio32(int fdes,struct strbuf32 * ctl,struct strbuf32 * data,int * rval,int mode,unsigned char * prip,int * flagsp)416*7c478bd9Sstevel@tonic-gate msgio32(int fdes, struct strbuf32 *ctl, struct strbuf32 *data, int *rval,
417*7c478bd9Sstevel@tonic-gate     int mode, unsigned char *prip, int *flagsp)
418*7c478bd9Sstevel@tonic-gate {
419*7c478bd9Sstevel@tonic-gate 	file_t *fp;
420*7c478bd9Sstevel@tonic-gate 	vnode_t *vp;
421*7c478bd9Sstevel@tonic-gate 	struct strbuf32 msgctl32, msgdata32;
422*7c478bd9Sstevel@tonic-gate 	struct strbuf msgctl, msgdata;
423*7c478bd9Sstevel@tonic-gate 	int error;
424*7c478bd9Sstevel@tonic-gate 	int flag;
425*7c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
426*7c478bd9Sstevel@tonic-gate 	rval_t rv;
427*7c478bd9Sstevel@tonic-gate 
428*7c478bd9Sstevel@tonic-gate 	if ((fp = getf(fdes)) == NULL)
429*7c478bd9Sstevel@tonic-gate 		return (EBADF);
430*7c478bd9Sstevel@tonic-gate 	if ((fp->f_flag & mode) == 0) {
431*7c478bd9Sstevel@tonic-gate 		releasef(fdes);
432*7c478bd9Sstevel@tonic-gate 		return (EBADF);
433*7c478bd9Sstevel@tonic-gate 	}
434*7c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
435*7c478bd9Sstevel@tonic-gate 	if (vp->v_type == VFIFO) {
436*7c478bd9Sstevel@tonic-gate 		if (vp->v_stream) {
437*7c478bd9Sstevel@tonic-gate 			/*
438*7c478bd9Sstevel@tonic-gate 			 * must use sd_vnode, could be named pipe
439*7c478bd9Sstevel@tonic-gate 			 */
440*7c478bd9Sstevel@tonic-gate 			(void) fifo_vfastoff(vp->v_stream->sd_vnode);
441*7c478bd9Sstevel@tonic-gate 		} else {
442*7c478bd9Sstevel@tonic-gate 			releasef(fdes);
443*7c478bd9Sstevel@tonic-gate 			return (ENOSTR);
444*7c478bd9Sstevel@tonic-gate 		}
445*7c478bd9Sstevel@tonic-gate 	} else if ((vp->v_type != VCHR && vp->v_type != VSOCK) ||
446*7c478bd9Sstevel@tonic-gate 		    vp->v_stream == NULL) {
447*7c478bd9Sstevel@tonic-gate 		releasef(fdes);
448*7c478bd9Sstevel@tonic-gate 		return (ENOSTR);
449*7c478bd9Sstevel@tonic-gate 	}
450*7c478bd9Sstevel@tonic-gate 	if (ctl != NULL) {
451*7c478bd9Sstevel@tonic-gate 		if (copyin(ctl, &msgctl32, sizeof (msgctl32))) {
452*7c478bd9Sstevel@tonic-gate 			releasef(fdes);
453*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
454*7c478bd9Sstevel@tonic-gate 		}
455*7c478bd9Sstevel@tonic-gate 		msgctl.len = msgctl32.len;
456*7c478bd9Sstevel@tonic-gate 		msgctl.maxlen = msgctl32.maxlen;
457*7c478bd9Sstevel@tonic-gate 		msgctl.buf = (caddr_t)(uintptr_t)msgctl32.buf;
458*7c478bd9Sstevel@tonic-gate 	}
459*7c478bd9Sstevel@tonic-gate 	if (data != NULL) {
460*7c478bd9Sstevel@tonic-gate 		if (copyin(data, &msgdata32, sizeof (msgdata32))) {
461*7c478bd9Sstevel@tonic-gate 			releasef(fdes);
462*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
463*7c478bd9Sstevel@tonic-gate 		}
464*7c478bd9Sstevel@tonic-gate 		msgdata.len = msgdata32.len;
465*7c478bd9Sstevel@tonic-gate 		msgdata.maxlen = msgdata32.maxlen;
466*7c478bd9Sstevel@tonic-gate 		msgdata.buf = (caddr_t)(uintptr_t)msgdata32.buf;
467*7c478bd9Sstevel@tonic-gate 	}
468*7c478bd9Sstevel@tonic-gate 
469*7c478bd9Sstevel@tonic-gate 	if (mode == FREAD) {
470*7c478bd9Sstevel@tonic-gate 		if (ctl == NULL)
471*7c478bd9Sstevel@tonic-gate 			msgctl.maxlen = -1;
472*7c478bd9Sstevel@tonic-gate 		if (data == NULL)
473*7c478bd9Sstevel@tonic-gate 			msgdata.maxlen = -1;
474*7c478bd9Sstevel@tonic-gate 		flag = fp->f_flag;
475*7c478bd9Sstevel@tonic-gate 		rv.r_val1 = 0;
476*7c478bd9Sstevel@tonic-gate 		if (vp->v_type == VSOCK) {
477*7c478bd9Sstevel@tonic-gate 			error = sock_getmsg(vp, &msgctl, &msgdata, prip,
478*7c478bd9Sstevel@tonic-gate 			    flagsp, flag, &rv);
479*7c478bd9Sstevel@tonic-gate 		} else {
480*7c478bd9Sstevel@tonic-gate 			error = strgetmsg(vp, &msgctl, &msgdata, prip,
481*7c478bd9Sstevel@tonic-gate 			    flagsp, flag, &rv);
482*7c478bd9Sstevel@tonic-gate 		}
483*7c478bd9Sstevel@tonic-gate 		*rval = rv.r_val1;
484*7c478bd9Sstevel@tonic-gate 		if (error != 0) {
485*7c478bd9Sstevel@tonic-gate 			releasef(fdes);
486*7c478bd9Sstevel@tonic-gate 			return (error);
487*7c478bd9Sstevel@tonic-gate 		}
488*7c478bd9Sstevel@tonic-gate 		if (lwp != NULL)
489*7c478bd9Sstevel@tonic-gate 			lwp->lwp_ru.msgrcv++;
490*7c478bd9Sstevel@tonic-gate 		if (ctl != NULL) {
491*7c478bd9Sstevel@tonic-gate 			/* XX64 - range check */
492*7c478bd9Sstevel@tonic-gate 			msgctl32.len = msgctl.len;
493*7c478bd9Sstevel@tonic-gate 			msgctl32.maxlen = msgctl.maxlen;
494*7c478bd9Sstevel@tonic-gate 			msgctl32.buf = (caddr32_t)(uintptr_t)msgctl.buf;
495*7c478bd9Sstevel@tonic-gate 			if (copyout(&msgctl32, ctl, sizeof (msgctl32))) {
496*7c478bd9Sstevel@tonic-gate 				releasef(fdes);
497*7c478bd9Sstevel@tonic-gate 				return (EFAULT);
498*7c478bd9Sstevel@tonic-gate 			}
499*7c478bd9Sstevel@tonic-gate 		}
500*7c478bd9Sstevel@tonic-gate 		if (data != NULL) {
501*7c478bd9Sstevel@tonic-gate 			/* XX64 - range check */
502*7c478bd9Sstevel@tonic-gate 			msgdata32.len = msgdata.len;
503*7c478bd9Sstevel@tonic-gate 			msgdata32.maxlen = msgdata.maxlen;
504*7c478bd9Sstevel@tonic-gate 			msgdata32.buf = (caddr32_t)(uintptr_t)msgdata.buf;
505*7c478bd9Sstevel@tonic-gate 			if (copyout(&msgdata32, data, sizeof (msgdata32))) {
506*7c478bd9Sstevel@tonic-gate 				releasef(fdes);
507*7c478bd9Sstevel@tonic-gate 				return (EFAULT);
508*7c478bd9Sstevel@tonic-gate 			}
509*7c478bd9Sstevel@tonic-gate 		}
510*7c478bd9Sstevel@tonic-gate 		releasef(fdes);
511*7c478bd9Sstevel@tonic-gate 		return (0);
512*7c478bd9Sstevel@tonic-gate 	}
513*7c478bd9Sstevel@tonic-gate 
514*7c478bd9Sstevel@tonic-gate 	/*
515*7c478bd9Sstevel@tonic-gate 	 * FWRITE case
516*7c478bd9Sstevel@tonic-gate 	 */
517*7c478bd9Sstevel@tonic-gate 	if (ctl == NULL)
518*7c478bd9Sstevel@tonic-gate 		msgctl.len = -1;
519*7c478bd9Sstevel@tonic-gate 	if (data == NULL)
520*7c478bd9Sstevel@tonic-gate 		msgdata.len = -1;
521*7c478bd9Sstevel@tonic-gate 	flag = fp->f_flag;
522*7c478bd9Sstevel@tonic-gate 	if (vp->v_type == VSOCK) {
523*7c478bd9Sstevel@tonic-gate 		error = sock_putmsg(vp, &msgctl, &msgdata, *prip, *flagsp,
524*7c478bd9Sstevel@tonic-gate 		    flag);
525*7c478bd9Sstevel@tonic-gate 	} else {
526*7c478bd9Sstevel@tonic-gate 		error = strputmsg(vp, &msgctl, &msgdata, *prip, *flagsp, flag);
527*7c478bd9Sstevel@tonic-gate 	}
528*7c478bd9Sstevel@tonic-gate 	releasef(fdes);
529*7c478bd9Sstevel@tonic-gate 	if (error == 0 && lwp != NULL)
530*7c478bd9Sstevel@tonic-gate 		lwp->lwp_ru.msgsnd++;
531*7c478bd9Sstevel@tonic-gate 	return (error);
532*7c478bd9Sstevel@tonic-gate }
533*7c478bd9Sstevel@tonic-gate 
534*7c478bd9Sstevel@tonic-gate #endif /* _LP64 && _SYSCALL32 */
535