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