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 * The original versions of these syscalls used to live in
36 * kern/uipc_syscalls.c. These are heavily modified to use the
40 #include "opt_compat.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/sysproto.h>
46 #include <sys/kern_syscall.h>
47 #include <sys/malloc.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
54 #include <sys/mplock2.h>
56 #include "43bsd_socket.h"
59 * System call interface to the socket abstraction.
63 compat_43_getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len)
69 if (len > SOCK_MAXADDRLEN)
71 if (len < offsetof(struct sockaddr, sa_data[0]))
73 sa = kmalloc(len, M_SONAME, M_WAITOK);
74 error = copyin(uaddr, sa, len);
79 * Convert to the 4.4BSD sockaddr structure.
81 sa->sa_family = sa->sa_len;
89 compat_43_copyout_sockaddr(struct sockaddr *sa, caddr_t uaddr, int sa_len)
93 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
94 error = copyout(sa, uaddr, sa_len);
103 sys_oaccept(struct accept_args *uap)
105 struct sockaddr *sa = NULL;
110 error = copyin(uap->anamelen, &sa_len, sizeof(sa_len));
115 error = kern_accept(uap->s, 0, &sa, &sa_len,
116 &uap->sysmsg_iresult);
121 * return a namelen of zero for older code which
122 * might ignore the return value from accept.
125 copyout(&sa_len, uap->anamelen, sizeof(*uap->anamelen));
127 compat_43_copyout_sockaddr(sa, uap->name, sa_len);
129 error = copyout(&sa_len, uap->anamelen,
130 sizeof(*uap->anamelen));
137 error = kern_accept(uap->s, 0, NULL, 0, &uap->sysmsg_iresult);
147 sys_ogetsockname(struct getsockname_args *uap)
149 struct sockaddr *sa = NULL;
152 error = copyin(uap->alen, &sa_len, sizeof(sa_len));
157 error = kern_getsockname(uap->fdes, &sa, &sa_len);
161 error = compat_43_copyout_sockaddr(sa, uap->asa, sa_len);
163 error = copyout(&sa_len, uap->alen, sizeof(*uap->alen));
174 sys_ogetpeername(struct ogetpeername_args *uap)
176 struct sockaddr *sa = NULL;
179 error = copyin(uap->alen, &sa_len, sizeof(sa_len));
184 error = kern_getpeername(uap->fdes, &sa, &sa_len);
188 error = compat_43_copyout_sockaddr(sa, uap->asa, sa_len);
191 error = copyout(&sa_len, uap->alen, sizeof(*uap->alen));
201 sys_osend(struct osend_args *uap)
203 struct thread *td = curthread;
208 aiov.iov_base = uap->buf;
209 aiov.iov_len = uap->len;
210 auio.uio_iov = &aiov;
213 auio.uio_resid = uap->len;
214 auio.uio_segflg = UIO_USERSPACE;
215 auio.uio_rw = UIO_WRITE;
219 error = kern_sendmsg(uap->s, NULL, &auio, NULL, uap->flags,
220 &uap->sysmsg_szresult);
230 sys_osendmsg(struct osendmsg_args *uap)
232 struct thread *td = curthread;
235 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
236 struct sockaddr *sa = NULL;
237 struct mbuf *control = NULL;
241 error = copyin(uap->msg, (caddr_t)&msg, sizeof (msg));
246 * Conditionally copyin msg.msg_name.
249 error = compat_43_getsockaddr(&sa, msg.msg_name,
258 error = iovec_copyin(msg.msg_iov, &iov, aiov, msg.msg_iovlen,
263 auio.uio_iovcnt = msg.msg_iovlen;
265 auio.uio_segflg = UIO_USERSPACE;
266 auio.uio_rw = UIO_WRITE;
270 * Conditionally copyin msg.msg_control.
272 if (msg.msg_control) {
273 if (msg.msg_controllen < 0 || msg.msg_controllen > MLEN) {
277 control = m_get(MB_WAIT, MT_CONTROL);
278 if (control == NULL) {
282 control->m_len = msg.msg_controllen;
283 error = copyin(msg.msg_control, mtod(control, caddr_t),
290 * In 4.3BSD, the only type of ancillary data was
291 * access rights and this data did not use a header
292 * to identify it's type. Thus, we must prepend the
293 * control data with the proper cmsghdr structure
294 * so that the kernel recognizes it as access rights.
296 M_PREPEND(control, sizeof(*cm), MB_WAIT);
297 if (control == NULL) {
301 cm = mtod(control, struct cmsghdr *);
302 cm->cmsg_len = control->m_len;
303 cm->cmsg_level = SOL_SOCKET;
304 cm->cmsg_type = SCM_RIGHTS;
309 error = kern_sendmsg(uap->s, sa, &auio, control, uap->flags,
310 &uap->sysmsg_szresult);
314 iovec_free(&iov, aiov);
325 sys_orecv(struct orecv_args *uap)
327 struct thread *td = curthread;
332 aiov.iov_base = uap->buf;
333 aiov.iov_len = uap->len;
334 auio.uio_iov = &aiov;
337 auio.uio_resid = uap->len;
338 auio.uio_segflg = UIO_USERSPACE;
339 auio.uio_rw = UIO_READ;
343 error = kern_recvmsg(uap->s, NULL, &auio, NULL, &uap->flags,
344 &uap->sysmsg_szresult);
354 sys_orecvfrom(struct recvfrom_args *uap)
356 struct thread *td = curthread;
359 struct sockaddr *sa = NULL;
362 if (uap->from && uap->fromlenaddr) {
363 error = copyin(uap->fromlenaddr, &fromlen, sizeof(fromlen));
371 aiov.iov_base = uap->buf;
372 aiov.iov_len = uap->len;
373 auio.uio_iov = &aiov;
376 auio.uio_resid = uap->len;
377 auio.uio_segflg = UIO_USERSPACE;
378 auio.uio_rw = UIO_READ;
382 error = kern_recvmsg(uap->s, uap->from ? &sa : NULL, &auio, NULL,
383 &uap->flags, &uap->sysmsg_szresult);
386 if (error == 0 && uap->from) {
388 fromlen = MIN(fromlen, sa->sa_len);
389 error = compat_43_copyout_sockaddr(sa, uap->from,
395 * Old recvfrom didn't signal an error if this
396 * next copyout failed.
398 copyout(&fromlen, uap->fromlenaddr, sizeof(fromlen));
410 sys_orecvmsg(struct orecvmsg_args *uap)
412 struct thread *td = curthread;
415 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
416 struct mbuf *m, *control = NULL;
417 struct sockaddr *sa = NULL;
419 socklen_t *ufromlenp, *ucontrollenp;
420 int error, fromlen, controllen, len, flags, *uflagsp;
423 * This copyin handles everything except the iovec.
425 error = copyin(uap->msg, &msg, sizeof(struct omsghdr));
429 if (msg.msg_name && msg.msg_namelen < 0)
431 if (msg.msg_control && msg.msg_controllen < 0)
434 ufromlenp = (socklen_t *)((caddr_t)uap->msg + offsetof(struct msghdr,
436 ucontrollenp = (socklen_t *)((caddr_t)uap->msg + offsetof(struct msghdr,
438 uflagsp = (int *)((caddr_t)uap->msg + offsetof(struct msghdr,
444 error = iovec_copyin(msg.msg_iov, &iov, aiov, msg.msg_iovlen,
449 auio.uio_iovcnt = msg.msg_iovlen;
451 auio.uio_segflg = UIO_USERSPACE;
452 auio.uio_rw = UIO_READ;
455 flags = msg.msg_flags;
458 error = kern_recvmsg(uap->s, (msg.msg_name ? &sa : NULL), &auio,
459 (msg.msg_control ? &control : NULL), &flags,
460 &uap->sysmsg_szresult);
464 * Copyout msg.msg_name and msg.msg_namelen.
466 if (error == 0 && msg.msg_name) {
468 fromlen = MIN(msg.msg_namelen, sa->sa_len);
469 error = compat_43_copyout_sockaddr(sa, msg.msg_name,
475 * Old recvfrom didn't signal an error if this
476 * next copyout failed.
478 copyout(&fromlen, ufromlenp, sizeof(*ufromlenp));
482 * Copyout msg.msg_control and msg.msg_controllen.
484 if (error == 0 && msg.msg_control) {
486 * If we receive access rights, trim the cmsghdr; anything
489 if (mtod((struct mbuf *)msg.msg_control,
490 struct cmsghdr *)->cmsg_level != SOL_SOCKET ||
491 mtod((struct mbuf *)msg.msg_control,
492 struct cmsghdr *)->cmsg_type != SCM_RIGHTS) {
494 error = copyout(&temp, ucontrollenp,
495 sizeof(*ucontrollenp));
498 ((struct mbuf *)msg.msg_control)->m_len -=
499 sizeof(struct cmsghdr);
500 ((struct mbuf *)msg.msg_control)->m_data +=
501 sizeof(struct cmsghdr);
503 len = msg.msg_controllen;
505 ctlbuf = (caddr_t)msg.msg_control;
507 while(m && len > 0) {
510 if (len >= m->m_len) {
513 msg.msg_flags |= MSG_CTRUNC;
517 error = copyout(mtod(m, caddr_t), ctlbuf,
526 controllen = ctlbuf - (caddr_t)msg.msg_control;
527 error = copyout(&controllen, ucontrollenp,
528 sizeof(*ucontrollenp));
532 error = copyout(&flags, uflagsp, sizeof(*uflagsp));
537 iovec_free(&iov, aiov);