Home | History | Annotate | Line # | Download | only in common
uipc_syscalls_43.c revision 1.20
      1 /*	$NetBSD: uipc_syscalls_43.c,v 1.20 2003/06/28 14:21:17 darrenr Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1990, 1993
      5  *	The Regents of the University of California.  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  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)uipc_syscalls.c	8.4 (Berkeley) 2/21/94
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: uipc_syscalls_43.c,v 1.20 2003/06/28 14:21:17 darrenr Exp $");
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/filedesc.h>
     44 #include <sys/kernel.h>
     45 #include <sys/proc.h>
     46 #include <sys/file.h>
     47 #include <sys/socket.h>
     48 #include <sys/socketvar.h>
     49 #include <sys/stat.h>
     50 #include <sys/ioctl.h>
     51 #include <sys/fcntl.h>
     52 #include <sys/malloc.h>
     53 #include <sys/syslog.h>
     54 #include <sys/unistd.h>
     55 #include <sys/resourcevar.h>
     56 #include <sys/mbuf.h>		/* for MLEN */
     57 
     58 #include <sys/mount.h>
     59 #include <sys/sa.h>
     60 #include <sys/syscallargs.h>
     61 
     62 #include <compat/common/compat_util.h>
     63 
     64 #include <uvm/uvm_extern.h>
     65 
     66 /*
     67  * Following 4.3 syscalls were not versioned, even through they should
     68  * have been:
     69  * connect(2), bind(2), sendto(2)
     70  */
     71 
     72 static int compat_43_sa_put(caddr_t);
     73 
     74 int
     75 compat_43_sys_accept(struct lwp *l, void *v, register_t *retval)
     76 {
     77 	struct compat_43_sys_accept_args /* {
     78 		syscallarg(int) s;
     79 		syscallarg(caddr_t) name;
     80 		syscallarg(int *) anamelen;
     81 	} */ *uap = v;
     82 	int error;
     83 
     84 	if ((error = sys_accept(l, v, retval)) != 0)
     85 		return error;
     86 
     87 	if (SCARG(uap, name)
     88 	    && (error = compat_43_sa_put(SCARG(uap, name))))
     89 		return (error);
     90 
     91 	return 0;
     92 }
     93 
     94 int
     95 compat_43_sys_getpeername(struct lwp *l, void *v, register_t *retval)
     96 {
     97 	struct compat_43_sys_getpeername_args /* {
     98 		syscallarg(int) fdes;
     99 		syscallarg(caddr_t) asa;
    100 		syscallarg(int *) alen;
    101 	} */ *uap = v;
    102 
    103 	int error;
    104 
    105 	if ((error = sys_getpeername(l, v, retval)) != 0)
    106 		return error;
    107 
    108 	if ((error = compat_43_sa_put(SCARG(uap, asa))))
    109 		return (error);
    110 
    111 	return 0;
    112 }
    113 
    114 int
    115 compat_43_sys_getsockname(struct lwp *l, void *v, register_t *retval)
    116 {
    117 	struct compat_43_sys_getsockname_args /* {
    118 		syscallarg(int) fdes;
    119 		syscallarg(caddr_t) asa;
    120 		syscallarg(int *) alen;
    121 	} */ *uap = v;
    122 	int error;
    123 
    124 	if ((error = sys_getsockname(l, v, retval)) != 0)
    125 		return error;
    126 
    127 	if ((error = compat_43_sa_put(SCARG(uap, asa))))
    128 		return (error);
    129 
    130 	return 0;
    131 }
    132 
    133 int
    134 compat_43_sys_recv(struct lwp *l, void *v, register_t *retval)
    135 {
    136 	struct compat_43_sys_recv_args /* {
    137 		syscallarg(int) s;
    138 		syscallarg(caddr_t) buf;
    139 		syscallarg(int) len;
    140 		syscallarg(int) flags;
    141 	} */ *uap = v;
    142 	struct sys_recvfrom_args bra;
    143 
    144 	SCARG(&bra, s) = SCARG(uap, s);
    145 	SCARG(&bra, buf) = SCARG(uap, buf);
    146 	SCARG(&bra, len) = (size_t) SCARG(uap, len);
    147 	SCARG(&bra, flags) = SCARG(uap, flags);
    148 	SCARG(&bra, from) = NULL;
    149 	SCARG(&bra, fromlenaddr) = NULL;
    150 
    151 	return (sys_recvfrom(l, &bra, retval));
    152 }
    153 
    154 int
    155 compat_43_sys_recvfrom(struct lwp *l, void *v, register_t *retval)
    156 {
    157 	struct compat_43_sys_recvfrom_args /* {
    158 		syscallarg(int) s;
    159 		syscallarg(caddr_t) buf;
    160 		syscallarg(size_t) len;
    161 		syscallarg(int) flags;
    162 		syscallarg(caddr_t) from;
    163 		syscallarg(int *) fromlenaddr;
    164 	} */ *uap = v;
    165 	int error;
    166 
    167 	if ((error = sys_recvfrom(l, v, retval)))
    168 		return (error);
    169 
    170 	if (SCARG(uap, from) && (error = compat_43_sa_put(SCARG(uap, from))))
    171 		return (error);
    172 
    173 	return (0);
    174 }
    175 
    176 /*
    177  * Old recvmsg. Arrange necessary structures, calls generic code and
    178  * adjusts results accordingly.
    179  */
    180 int
    181 compat_43_sys_recvmsg(struct lwp *l, void *v, register_t *retval)
    182 {
    183 	struct compat_43_sys_recvmsg_args /* {
    184 		syscallarg(int) s;
    185 		syscallarg(struct omsghdr *) msg;
    186 		syscallarg(int) flags;
    187 	} */ *uap = v;
    188 	struct proc *p = l->l_proc;
    189 	struct omsghdr omsg;
    190 	struct msghdr msg;
    191 	struct iovec aiov[UIO_SMALLIOV], *iov;
    192 	int error;
    193 
    194 	error = copyin((caddr_t)SCARG(uap, msg), (caddr_t)&omsg,
    195 	    sizeof (struct omsghdr));
    196 	if (error)
    197 		return (error);
    198 	if ((u_int)omsg.msg_iovlen > UIO_SMALLIOV) {
    199 		if ((u_int)omsg.msg_iovlen > IOV_MAX)
    200 			return (EMSGSIZE);
    201 		iov = malloc(sizeof(struct iovec) * omsg.msg_iovlen,
    202 		    M_IOV, M_WAITOK);
    203 	} else
    204 		iov = aiov;
    205 
    206 	error = copyin((caddr_t)omsg.msg_iov, (caddr_t)iov,
    207 	    (unsigned)(omsg.msg_iovlen * sizeof (struct iovec)));
    208 	if (error)
    209 		goto done;
    210 
    211 	msg.msg_name	= omsg.msg_name;
    212 	msg.msg_namelen = omsg.msg_namelen;
    213 	msg.msg_iovlen	= omsg.msg_iovlen;
    214 	msg.msg_iov	= iov;
    215 	msg.msg_flags	= SCARG(uap, flags);
    216 
    217 	/*
    218 	 * If caller passes accrights, arrange things for generic code to
    219 	 * DTRT.
    220 	 */
    221 	if (omsg.msg_accrights && omsg.msg_accrightslen) {
    222 		caddr_t sg = stackgap_init(p, 0);
    223 		struct cmsg *ucmsg;
    224 
    225 		/* it was this way in 4.4BSD */
    226 		if ((u_int) omsg.msg_accrightslen > MLEN)
    227 			return (EINVAL);
    228 
    229 		ucmsg = stackgap_alloc(p, &sg, CMSG_SPACE(omsg.msg_accrightslen));
    230 		if (ucmsg == NULL)
    231 			return (EMSGSIZE);
    232 
    233 		msg.msg_control = ucmsg;
    234 		msg.msg_controllen = CMSG_SPACE(omsg.msg_accrightslen);
    235 	} else {
    236 		msg.msg_control = NULL;
    237 		msg.msg_controllen = 0;
    238 	}
    239 
    240 	error = recvit(l, SCARG(uap, s), &msg,
    241 	    (caddr_t)&SCARG(uap, msg)->msg_namelen, retval);
    242 
    243 	/*
    244 	 * If there is any control information and it's SCM_RIGHTS,
    245 	 * pass it back to the program.
    246 	 */
    247 	if (!error && omsg.msg_accrights && msg.msg_controllen > 0) {
    248 		struct cmsghdr *cmsg;
    249 
    250 		/* safe - msg.msg_controllen set by kernel */
    251 		cmsg = (struct cmsghdr *) malloc(msg.msg_controllen,
    252 		    M_TEMP, M_WAITOK);
    253 
    254 		error = copyin(msg.msg_control, cmsg, msg.msg_controllen);
    255 		if (error) {
    256 			free(cmsg, M_TEMP);
    257 			return (error);
    258 		}
    259 
    260 		if (cmsg->cmsg_level != SOL_SOCKET
    261 		    || cmsg->cmsg_type != SCM_RIGHTS
    262 		    || copyout(CMSG_DATA(cmsg), omsg.msg_accrights,
    263 			    cmsg->cmsg_len)) {
    264 			omsg.msg_accrightslen = 0;
    265 		}
    266 
    267 		free(cmsg, M_TEMP);
    268 
    269 		if (!error) {
    270 			error = copyout(&cmsg->cmsg_len,
    271 			    &SCARG(uap, msg)->msg_accrightslen, sizeof(int));
    272 		}
    273 	}
    274 
    275 	if (!error && omsg.msg_name) {
    276 		int namelen;
    277 
    278 		if ((error = copyin(&SCARG(uap, msg)->msg_namelen, &namelen, sizeof(int)) == 0)
    279 		    && namelen > 0)
    280 			error = compat_43_sa_put(omsg.msg_name);
    281 	}
    282 
    283 done:
    284 	if (iov != aiov)
    285 		free(iov, M_IOV);
    286 	return (error);
    287 }
    288 
    289 int
    290 compat_43_sys_send(struct lwp *l, void *v, register_t *retval)
    291 {
    292 	struct compat_43_sys_send_args /* {
    293 		syscallarg(int) s;
    294 		syscallarg(caddr_t) buf;
    295 		syscallarg(int) len;
    296 		syscallarg(int) flags;
    297 	} */ *uap = v;
    298 	struct sys_sendto_args bsa;
    299 
    300 	SCARG(&bsa, s)		= SCARG(uap, s);
    301 	SCARG(&bsa, buf)	= SCARG(uap, buf);
    302 	SCARG(&bsa, len)	= SCARG(uap, len);
    303 	SCARG(&bsa, flags)	= SCARG(uap, flags);
    304 	SCARG(&bsa, to)		= NULL;
    305 	SCARG(&bsa, tolen)	= 0;
    306 
    307 	return (sys_sendto(l, &bsa, retval));
    308 }
    309 
    310 /*
    311  * Old sendmsg. Arrange necessary structures, call generic code and
    312  * adjust the results accordingly for old code.
    313  */
    314 int
    315 compat_43_sys_sendmsg(struct lwp *l, void *v, register_t *retval)
    316 {
    317 	struct compat_43_sys_sendmsg_args /* {
    318 		syscallarg(int) s;
    319 		syscallarg(caddr_t) msg;
    320 		syscallarg(int) flags;
    321 	} */ *uap = v;
    322 	struct proc *p = l->l_proc;
    323 	struct omsghdr omsg;
    324 	struct msghdr msg;
    325 	struct iovec aiov[UIO_SMALLIOV], *iov;
    326 	int error;
    327 	caddr_t sg = stackgap_init(p, 0);
    328 
    329 	error = copyin(SCARG(uap, msg), (caddr_t)&omsg,
    330 	    sizeof (struct omsghdr));
    331 	if (error)
    332 		return (error);
    333 	if ((u_int)omsg.msg_iovlen > UIO_SMALLIOV) {
    334 		if ((u_int)omsg.msg_iovlen > IOV_MAX)
    335 			return (EMSGSIZE);
    336 		iov = malloc(sizeof(struct iovec) * omsg.msg_iovlen,
    337 		    M_IOV, M_WAITOK);
    338 	} else
    339 		iov = aiov;
    340 	error = copyin((caddr_t)omsg.msg_iov, (caddr_t)iov,
    341 	    (unsigned)(omsg.msg_iovlen * sizeof (struct iovec)));
    342 	if (error)
    343 		goto done;
    344 
    345 	if (omsg.msg_name) {
    346 		struct osockaddr *osa;
    347 		struct sockaddr *sa, *usa;
    348 
    349 		if ((u_int) omsg.msg_namelen > UCHAR_MAX)
    350 			return (EINVAL);
    351 
    352 		osa = malloc(omsg.msg_namelen, M_TEMP, M_WAITOK);
    353 
    354 		if ((error = copyin(omsg.msg_name, osa, omsg.msg_namelen))) {
    355 			free(osa, M_TEMP);
    356 			return (error);
    357 		}
    358 
    359 		sa = (struct sockaddr *) osa;
    360 		sa->sa_family = osa->sa_family;
    361 		sa->sa_len = omsg.msg_namelen;
    362 
    363 		usa = stackgap_alloc(p, &sg, omsg.msg_namelen);
    364 		if (!usa) {
    365 			free(osa, M_TEMP);
    366 			return (ENOMEM);
    367 		}
    368 
    369 		(void) copyout(sa, usa, omsg.msg_namelen);
    370 		free(osa, M_TEMP);
    371 
    372 		msg.msg_name = usa;
    373 		msg.msg_namelen = omsg.msg_namelen;
    374 	} else {
    375 		msg.msg_name = NULL;
    376 		msg.msg_namelen = 0;
    377 	}
    378 	msg.msg_iovlen	= omsg.msg_iovlen;
    379 	msg.msg_iov	= iov;
    380 	msg.msg_flags	= 0;
    381 
    382 	if (omsg.msg_accrights && omsg.msg_accrightslen != 0) {
    383 		struct cmsghdr *cmsg, *ucmsg;
    384 
    385 		/* it was this way in 4.4BSD */
    386 		if ((u_int) omsg.msg_accrightslen > MLEN)
    387 			return (EINVAL);
    388 
    389 		cmsg = malloc(CMSG_SPACE(omsg.msg_accrightslen), M_TEMP,
    390 		    M_WAITOK);
    391 		cmsg->cmsg_len		= CMSG_SPACE(omsg.msg_accrightslen);
    392 		cmsg->cmsg_level	= SOL_SOCKET;
    393 		cmsg->cmsg_type 	= SCM_RIGHTS;
    394 
    395 		error = copyin(omsg.msg_accrights, CMSG_DATA(cmsg),
    396 		    omsg.msg_accrightslen);
    397 		if (error) {
    398 			free(cmsg, M_TEMP);
    399 			return (error);
    400 		}
    401 
    402 		ucmsg = stackgap_alloc(p, &sg, CMSG_SPACE(omsg.msg_accrightslen));
    403 		if (!ucmsg) {
    404 			free(cmsg, M_TEMP);
    405 			return (EMSGSIZE);
    406 		}
    407 
    408 		(void) copyout(cmsg, ucmsg, CMSG_SPACE(omsg.msg_accrightslen));
    409 		free(cmsg, M_TEMP);
    410 
    411 		msg.msg_control = ucmsg;
    412 		msg.msg_controllen = CMSG_SPACE(omsg.msg_accrightslen);
    413 	} else {
    414 		msg.msg_control = NULL;
    415 		msg.msg_controllen = 0;
    416 	}
    417 
    418 	error = sendit(l, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
    419 done:
    420 	if (iov != aiov)
    421 		FREE(iov, M_IOV);
    422 	return (error);
    423 }
    424 
    425 static int
    426 compat_43_sa_put(from)
    427 	caddr_t from;
    428 {
    429 	struct osockaddr *osa = (struct osockaddr *) from;
    430 	struct sockaddr sa;
    431 	struct osockaddr *kosa;
    432 	int error, len;
    433 
    434 	/*
    435 	 * Only read/write the sockaddr family and length, the rest is
    436 	 * not changed.
    437 	 */
    438 	len = sizeof(sa.sa_len) + sizeof(sa.sa_family);
    439 
    440 	error = copyin((caddr_t) osa, (caddr_t) &sa, len);
    441 	if (error)
    442 		return (error);
    443 
    444 	/* Note: we convert from sockaddr sa_family to osockaddr one here */
    445 	kosa = (struct osockaddr *) &sa;
    446 	kosa->sa_family = sa.sa_family;
    447 	error = copyout(kosa, osa, len);
    448 	if (error)
    449 		return (error);
    450 
    451 	return (0);
    452 }
    453