2 * 43BSD_SOCKET.C - 4.3BSD compatibility socket syscalls
4 * Copyright (c) 1982, 1986, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
35 * $DragonFly: src/sys/emulation/43bsd/43bsd_socket.c,v 1.10 2007/01/28 06:31:00 y0netan1 Exp $
36 * from: DragonFly kern/uipc_syscalls.c,v 1.13
38 * The original versions of these syscalls used to live in
39 * kern/uipc_syscalls.c. These are heavily modified to use the
43 #include "opt_compat.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/sysproto.h>
49 #include <sys/kern_syscall.h>
50 #include <sys/malloc.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
57 #include <sys/mplock2.h>
59 #include "43bsd_socket.h"
62 * System call interface to the socket abstraction.
66 compat_43_getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len)
72 if (len > SOCK_MAXADDRLEN)
74 if (len < offsetof(struct sockaddr, sa_data[0]))
76 MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK);
77 error = copyin(uaddr, sa, len);
82 * Convert to the 4.4BSD sockaddr structure.
84 sa->sa_family = sa->sa_len;
92 compat_43_copyout_sockaddr(struct sockaddr *sa, caddr_t uaddr, int sa_len)
96 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
97 error = copyout(sa, uaddr, sa_len);
106 sys_oaccept(struct accept_args *uap)
108 struct sockaddr *sa = NULL;
113 error = copyin(uap->anamelen, &sa_len, sizeof(sa_len));
118 error = kern_accept(uap->s, 0, &sa, &sa_len,
119 &uap->sysmsg_iresult);
124 * return a namelen of zero for older code which
125 * might ignore the return value from accept.
128 copyout(&sa_len, uap->anamelen, sizeof(*uap->anamelen));
130 compat_43_copyout_sockaddr(sa, uap->name, sa_len);
132 error = copyout(&sa_len, uap->anamelen,
133 sizeof(*uap->anamelen));
140 error = kern_accept(uap->s, 0, NULL, 0, &uap->sysmsg_iresult);
150 sys_ogetsockname(struct getsockname_args *uap)
152 struct sockaddr *sa = NULL;
155 error = copyin(uap->alen, &sa_len, sizeof(sa_len));
160 error = kern_getsockname(uap->fdes, &sa, &sa_len);
164 error = compat_43_copyout_sockaddr(sa, uap->asa, sa_len);
166 error = copyout(&sa_len, uap->alen, sizeof(*uap->alen));
177 sys_ogetpeername(struct ogetpeername_args *uap)
179 struct sockaddr *sa = NULL;
182 error = copyin(uap->alen, &sa_len, sizeof(sa_len));
187 error = kern_getpeername(uap->fdes, &sa, &sa_len);
191 error = compat_43_copyout_sockaddr(sa, uap->asa, sa_len);
194 error = copyout(&sa_len, uap->alen, sizeof(*uap->alen));
204 sys_osend(struct osend_args *uap)
206 struct thread *td = curthread;
211 aiov.iov_base = uap->buf;
212 aiov.iov_len = uap->len;
213 auio.uio_iov = &aiov;
216 auio.uio_resid = uap->len;
217 auio.uio_segflg = UIO_USERSPACE;
218 auio.uio_rw = UIO_WRITE;
222 error = kern_sendmsg(uap->s, NULL, &auio, NULL, uap->flags,
223 &uap->sysmsg_szresult);
233 sys_osendmsg(struct osendmsg_args *uap)
235 struct thread *td = curthread;
238 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
239 struct sockaddr *sa = NULL;
240 struct mbuf *control = NULL;
244 error = copyin(uap->msg, (caddr_t)&msg, sizeof (msg));
249 * Conditionally copyin msg.msg_name.
252 error = compat_43_getsockaddr(&sa, msg.msg_name,
261 error = iovec_copyin(msg.msg_iov, &iov, aiov, msg.msg_iovlen,
266 auio.uio_iovcnt = msg.msg_iovlen;
268 auio.uio_segflg = UIO_USERSPACE;
269 auio.uio_rw = UIO_WRITE;
273 * Conditionally copyin msg.msg_control.
275 if (msg.msg_control) {
276 if (msg.msg_controllen < 0 || msg.msg_controllen > MLEN) {
280 control = m_get(MB_WAIT, MT_CONTROL);
281 if (control == NULL) {
285 control->m_len = msg.msg_controllen;
286 error = copyin(msg.msg_control, mtod(control, caddr_t),
293 * In 4.3BSD, the only type of ancillary data was
294 * access rights and this data did not use a header
295 * to identify it's type. Thus, we must prepend the
296 * control data with the proper cmsghdr structure
297 * so that the kernel recognizes it as access rights.
299 M_PREPEND(control, sizeof(*cm), MB_WAIT);
300 if (control == NULL) {
304 cm = mtod(control, struct cmsghdr *);
305 cm->cmsg_len = control->m_len;
306 cm->cmsg_level = SOL_SOCKET;
307 cm->cmsg_type = SCM_RIGHTS;
312 error = kern_sendmsg(uap->s, sa, &auio, control, uap->flags,
313 &uap->sysmsg_szresult);
317 iovec_free(&iov, aiov);
328 sys_orecv(struct orecv_args *uap)
330 struct thread *td = curthread;
335 aiov.iov_base = uap->buf;
336 aiov.iov_len = uap->len;
337 auio.uio_iov = &aiov;
340 auio.uio_resid = uap->len;
341 auio.uio_segflg = UIO_USERSPACE;
342 auio.uio_rw = UIO_READ;
346 error = kern_recvmsg(uap->s, NULL, &auio, NULL, &uap->flags,
347 &uap->sysmsg_szresult);
357 sys_orecvfrom(struct recvfrom_args *uap)
359 struct thread *td = curthread;
362 struct sockaddr *sa = NULL;
365 if (uap->from && uap->fromlenaddr) {
366 error = copyin(uap->fromlenaddr, &fromlen, sizeof(fromlen));
374 aiov.iov_base = uap->buf;
375 aiov.iov_len = uap->len;
376 auio.uio_iov = &aiov;
379 auio.uio_resid = uap->len;
380 auio.uio_segflg = UIO_USERSPACE;
381 auio.uio_rw = UIO_READ;
385 error = kern_recvmsg(uap->s, uap->from ? &sa : NULL, &auio, NULL,
386 &uap->flags, &uap->sysmsg_szresult);
389 if (error == 0 && uap->from) {
391 fromlen = MIN(fromlen, sa->sa_len);
392 error = compat_43_copyout_sockaddr(sa, uap->from,
398 * Old recvfrom didn't signal an error if this
399 * next copyout failed.
401 copyout(&fromlen, uap->fromlenaddr, sizeof(fromlen));
413 sys_orecvmsg(struct orecvmsg_args *uap)
415 struct thread *td = curthread;
418 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
419 struct mbuf *m, *control = NULL;
420 struct sockaddr *sa = NULL;
422 socklen_t *ufromlenp, *ucontrollenp;
423 int error, fromlen, controllen, len, flags, *uflagsp;
426 * This copyin handles everything except the iovec.
428 error = copyin(uap->msg, &msg, sizeof(struct omsghdr));
432 if (msg.msg_name && msg.msg_namelen < 0)
434 if (msg.msg_control && msg.msg_controllen < 0)
437 ufromlenp = (socklen_t *)((caddr_t)uap->msg + offsetof(struct msghdr,
439 ucontrollenp = (socklen_t *)((caddr_t)uap->msg + offsetof(struct msghdr,
441 uflagsp = (int *)((caddr_t)uap->msg + offsetof(struct msghdr,
447 error = iovec_copyin(msg.msg_iov, &iov, aiov, msg.msg_iovlen,
452 auio.uio_iovcnt = msg.msg_iovlen;
454 auio.uio_segflg = UIO_USERSPACE;
455 auio.uio_rw = UIO_READ;
458 flags = msg.msg_flags;
461 error = kern_recvmsg(uap->s, (msg.msg_name ? &sa : NULL), &auio,
462 (msg.msg_control ? &control : NULL), &flags,
463 &uap->sysmsg_szresult);
467 * Copyout msg.msg_name and msg.msg_namelen.
469 if (error == 0 && msg.msg_name) {
471 fromlen = MIN(msg.msg_namelen, sa->sa_len);
472 error = compat_43_copyout_sockaddr(sa, msg.msg_name,
478 * Old recvfrom didn't signal an error if this
479 * next copyout failed.
481 copyout(&fromlen, ufromlenp, sizeof(*ufromlenp));
485 * Copyout msg.msg_control and msg.msg_controllen.
487 if (error == 0 && msg.msg_control) {
489 * If we receive access rights, trim the cmsghdr; anything
492 if (mtod((struct mbuf *)msg.msg_control,
493 struct cmsghdr *)->cmsg_level != SOL_SOCKET ||
494 mtod((struct mbuf *)msg.msg_control,
495 struct cmsghdr *)->cmsg_type != SCM_RIGHTS) {
497 error = copyout(&temp, ucontrollenp,
498 sizeof(*ucontrollenp));
501 ((struct mbuf *)msg.msg_control)->m_len -=
502 sizeof(struct cmsghdr);
503 ((struct mbuf *)msg.msg_control)->m_data +=
504 sizeof(struct cmsghdr);
506 len = msg.msg_controllen;
508 ctlbuf = (caddr_t)msg.msg_control;
510 while(m && len > 0) {
513 if (len >= m->m_len) {
516 msg.msg_flags |= MSG_CTRUNC;
520 error = copyout(mtod(m, caddr_t), ctlbuf,
529 controllen = ctlbuf - (caddr_t)msg.msg_control;
530 error = copyout(&controllen, ucontrollenp,
531 sizeof(*ucontrollenp));
535 error = copyout(&flags, uflagsp, sizeof(*uflagsp));
540 iovec_free(&iov, aiov);