1 /* $NetBSD: uipc_syscalls.c,v 1.161 2013/06/03 23:45:31 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1982, 1986, 1989, 1990, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. Neither the name of the University nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 * 60 * @(#)uipc_syscalls.c 8.6 (Berkeley) 2/14/95 61 */ 62 63 #include <sys/cdefs.h> 64 __KERNEL_RCSID(0, "$NetBSD: uipc_syscalls.c,v 1.161 2013/06/03 23:45:31 christos Exp $"); 65 66 #include "opt_pipe.h" 67 68 #include <sys/param.h> 69 #include <sys/systm.h> 70 #include <sys/filedesc.h> 71 #include <sys/proc.h> 72 #include <sys/file.h> 73 #include <sys/buf.h> 74 #define MBUFTYPES 75 #include <sys/mbuf.h> 76 #include <sys/protosw.h> 77 #include <sys/socket.h> 78 #include <sys/socketvar.h> 79 #include <sys/signalvar.h> 80 #include <sys/un.h> 81 #include <sys/ktrace.h> 82 #include <sys/event.h> 83 #include <sys/atomic.h> 84 #include <sys/kauth.h> 85 86 #include <sys/mount.h> 87 #include <sys/syscallargs.h> 88 89 /* 90 * System call interface to the socket abstraction. 91 */ 92 extern const struct fileops socketops; 93 94 int 95 sys___socket30(struct lwp *l, const struct sys___socket30_args *uap, register_t *retval) 96 { 97 /* { 98 syscallarg(int) domain; 99 syscallarg(int) type; 100 syscallarg(int) protocol; 101 } */ 102 int fd, error; 103 104 error = fsocreate(SCARG(uap, domain), NULL, SCARG(uap, type), 105 SCARG(uap, protocol), l, &fd); 106 if (error == 0) 107 *retval = fd; 108 return error; 109 } 110 111 /* ARGSUSED */ 112 int 113 sys_bind(struct lwp *l, const struct sys_bind_args *uap, register_t *retval) 114 { 115 /* { 116 syscallarg(int) s; 117 syscallarg(const struct sockaddr *) name; 118 syscallarg(unsigned int) namelen; 119 } */ 120 struct mbuf *nam; 121 int error; 122 123 error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen), 124 MT_SONAME); 125 if (error) 126 return error; 127 128 return do_sys_bind(l, SCARG(uap, s), nam); 129 } 130 131 int 132 do_sys_bind(struct lwp *l, int fd, struct mbuf *nam) 133 { 134 struct socket *so; 135 int error; 136 137 if ((error = fd_getsock(fd, &so)) != 0) { 138 m_freem(nam); 139 return (error); 140 } 141 MCLAIM(nam, so->so_mowner); 142 error = sobind(so, nam, l); 143 m_freem(nam); 144 fd_putfile(fd); 145 return error; 146 } 147 148 /* ARGSUSED */ 149 int 150 sys_listen(struct lwp *l, const struct sys_listen_args *uap, register_t *retval) 151 { 152 /* { 153 syscallarg(int) s; 154 syscallarg(int) backlog; 155 } */ 156 struct socket *so; 157 int error; 158 159 if ((error = fd_getsock(SCARG(uap, s), &so)) != 0) 160 return (error); 161 error = solisten(so, SCARG(uap, backlog), l); 162 fd_putfile(SCARG(uap, s)); 163 return error; 164 } 165 166 int 167 do_sys_accept(struct lwp *l, int sock, struct mbuf **name, register_t *new_sock, 168 const sigset_t *mask, int flags, int clrflags) 169 { 170 file_t *fp, *fp2; 171 struct mbuf *nam; 172 int error, fd; 173 struct socket *so, *so2; 174 short wakeup_state = 0; 175 176 if ((fp = fd_getfile(sock)) == NULL) 177 return (EBADF); 178 if (fp->f_type != DTYPE_SOCKET) { 179 fd_putfile(sock); 180 return (ENOTSOCK); 181 } 182 if ((error = fd_allocfile(&fp2, &fd)) != 0) { 183 fd_putfile(sock); 184 return (error); 185 } 186 nam = m_get(M_WAIT, MT_SONAME); 187 *new_sock = fd; 188 so = fp->f_data; 189 solock(so); 190 191 if (__predict_false(mask)) 192 sigsuspendsetup(l, mask); 193 194 if (!(so->so_proto->pr_flags & PR_LISTEN)) { 195 error = EOPNOTSUPP; 196 goto bad; 197 } 198 if ((so->so_options & SO_ACCEPTCONN) == 0) { 199 error = EINVAL; 200 goto bad; 201 } 202 if ((so->so_state & SS_NBIO) && so->so_qlen == 0) { 203 error = EWOULDBLOCK; 204 goto bad; 205 } 206 while (so->so_qlen == 0 && so->so_error == 0) { 207 if (so->so_state & SS_CANTRCVMORE) { 208 so->so_error = ECONNABORTED; 209 break; 210 } 211 if (wakeup_state & SS_RESTARTSYS) { 212 error = ERESTART; 213 goto bad; 214 } 215 error = sowait(so, true, 0); 216 if (error) { 217 goto bad; 218 } 219 wakeup_state = so->so_state; 220 } 221 if (so->so_error) { 222 error = so->so_error; 223 so->so_error = 0; 224 goto bad; 225 } 226 /* connection has been removed from the listen queue */ 227 KNOTE(&so->so_rcv.sb_sel.sel_klist, NOTE_SUBMIT); 228 so2 = TAILQ_FIRST(&so->so_q); 229 if (soqremque(so2, 1) == 0) 230 panic("accept"); 231 fp2->f_type = DTYPE_SOCKET; 232 fp2->f_flag = (fp->f_flag & ~clrflags) | 233 ((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)| 234 ((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0); 235 fp2->f_ops = &socketops; 236 fp2->f_data = so2; 237 if (flags & SOCK_NONBLOCK) 238 so2->so_state |= SS_NBIO; 239 error = soaccept(so2, nam); 240 so2->so_cred = kauth_cred_dup(so->so_cred); 241 sounlock(so); 242 if (error) { 243 /* an error occurred, free the file descriptor and mbuf */ 244 m_freem(nam); 245 mutex_enter(&fp2->f_lock); 246 fp2->f_count++; 247 mutex_exit(&fp2->f_lock); 248 closef(fp2); 249 fd_abort(curproc, NULL, fd); 250 } else { 251 fd_set_exclose(l, fd, (flags & SOCK_CLOEXEC) != 0); 252 fd_affix(curproc, fp2, fd); 253 *name = nam; 254 } 255 fd_putfile(sock); 256 if (__predict_false(mask)) 257 sigsuspendteardown(l); 258 return (error); 259 bad: 260 sounlock(so); 261 m_freem(nam); 262 fd_putfile(sock); 263 fd_abort(curproc, fp2, fd); 264 if (__predict_false(mask)) 265 sigsuspendteardown(l); 266 return (error); 267 } 268 269 int 270 sys_accept(struct lwp *l, const struct sys_accept_args *uap, register_t *retval) 271 { 272 /* { 273 syscallarg(int) s; 274 syscallarg(struct sockaddr *) name; 275 syscallarg(unsigned int *) anamelen; 276 } */ 277 int error, fd; 278 struct mbuf *name; 279 280 error = do_sys_accept(l, SCARG(uap, s), &name, retval, NULL, 0, 0); 281 if (error != 0) 282 return error; 283 error = copyout_sockname(SCARG(uap, name), SCARG(uap, anamelen), 284 MSG_LENUSRSPACE, name); 285 if (name != NULL) 286 m_free(name); 287 if (error != 0) { 288 fd = (int)*retval; 289 if (fd_getfile(fd) != NULL) 290 (void)fd_close(fd); 291 } 292 return error; 293 } 294 295 int 296 sys_paccept(struct lwp *l, const struct sys_paccept_args *uap, 297 register_t *retval) 298 { 299 /* { 300 syscallarg(int) s; 301 syscallarg(struct sockaddr *) name; 302 syscallarg(unsigned int *) anamelen; 303 syscallarg(const sigset_t *) mask; 304 syscallarg(int) flags; 305 } */ 306 int error, fd; 307 struct mbuf *name; 308 sigset_t *mask, amask; 309 310 if (SCARG(uap, mask) != NULL) { 311 error = copyin(SCARG(uap, mask), &amask, sizeof(amask)); 312 if (error) 313 return error; 314 mask = &amask; 315 } else 316 mask = NULL; 317 318 error = do_sys_accept(l, SCARG(uap, s), &name, retval, mask, 319 SCARG(uap, flags), FNONBLOCK); 320 if (error != 0) 321 return error; 322 error = copyout_sockname(SCARG(uap, name), SCARG(uap, anamelen), 323 MSG_LENUSRSPACE, name); 324 if (name != NULL) 325 m_free(name); 326 if (error != 0) { 327 fd = (int)*retval; 328 if (fd_getfile(fd) != NULL) 329 (void)fd_close(fd); 330 } 331 return error; 332 } 333 334 /* ARGSUSED */ 335 int 336 sys_connect(struct lwp *l, const struct sys_connect_args *uap, register_t *retval) 337 { 338 /* { 339 syscallarg(int) s; 340 syscallarg(const struct sockaddr *) name; 341 syscallarg(unsigned int) namelen; 342 } */ 343 int error; 344 struct mbuf *nam; 345 346 error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen), 347 MT_SONAME); 348 if (error) 349 return error; 350 return do_sys_connect(l, SCARG(uap, s), nam); 351 } 352 353 int 354 do_sys_connect(struct lwp *l, int fd, struct mbuf *nam) 355 { 356 struct socket *so; 357 int error; 358 int interrupted = 0; 359 360 if ((error = fd_getsock(fd, &so)) != 0) { 361 m_freem(nam); 362 return (error); 363 } 364 solock(so); 365 MCLAIM(nam, so->so_mowner); 366 if ((so->so_state & SS_ISCONNECTING) != 0) { 367 error = EALREADY; 368 goto out; 369 } 370 371 error = soconnect(so, nam, l); 372 if (error) 373 goto bad; 374 if ((so->so_state & (SS_NBIO|SS_ISCONNECTING)) == 375 (SS_NBIO|SS_ISCONNECTING)) { 376 error = EINPROGRESS; 377 goto out; 378 } 379 while ((so->so_state & SS_ISCONNECTING) != 0 && so->so_error == 0) { 380 error = sowait(so, true, 0); 381 if (__predict_false((so->so_state & SS_ISABORTING) != 0)) { 382 error = EPIPE; 383 interrupted = 1; 384 break; 385 } 386 if (error) { 387 if (error == EINTR || error == ERESTART) 388 interrupted = 1; 389 break; 390 } 391 } 392 if (error == 0) { 393 error = so->so_error; 394 so->so_error = 0; 395 } 396 bad: 397 if (!interrupted) 398 so->so_state &= ~SS_ISCONNECTING; 399 if (error == ERESTART) 400 error = EINTR; 401 out: 402 sounlock(so); 403 fd_putfile(fd); 404 m_freem(nam); 405 return (error); 406 } 407 408 static int 409 makesocket(struct lwp *l, file_t **fp, int *fd, int flags, int type, 410 int domain, int proto, struct socket *soo) 411 { 412 int error; 413 struct socket *so; 414 415 if ((error = socreate(domain, &so, type, proto, l, soo)) != 0) 416 return error; 417 418 if ((error = fd_allocfile(fp, fd)) != 0) { 419 soclose(so); 420 return error; 421 } 422 fd_set_exclose(l, *fd, (flags & SOCK_CLOEXEC) != 0); 423 (*fp)->f_flag = FREAD|FWRITE| 424 ((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)| 425 ((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0); 426 (*fp)->f_type = DTYPE_SOCKET; 427 (*fp)->f_ops = &socketops; 428 (*fp)->f_data = so; 429 if (flags & SOCK_NONBLOCK) 430 so->so_state |= SS_NBIO; 431 return 0; 432 } 433 434 int 435 sys_socketpair(struct lwp *l, const struct sys_socketpair_args *uap, 436 register_t *retval) 437 { 438 /* { 439 syscallarg(int) domain; 440 syscallarg(int) type; 441 syscallarg(int) protocol; 442 syscallarg(int *) rsv; 443 } */ 444 file_t *fp1, *fp2; 445 struct socket *so1, *so2; 446 int fd, error, sv[2]; 447 proc_t *p; 448 int flags = SCARG(uap, type) & SOCK_FLAGS_MASK; 449 int type = SCARG(uap, type) & ~SOCK_FLAGS_MASK; 450 int domain = SCARG(uap, domain); 451 int proto = SCARG(uap, protocol); 452 453 p = curproc; 454 455 error = makesocket(l, &fp1, &fd, flags, type, domain, proto, NULL); 456 if (error) 457 return error; 458 so1 = fp1->f_data; 459 sv[0] = fd; 460 461 error = makesocket(l, &fp2, &fd, flags, type, domain, proto, so1); 462 if (error) 463 goto out; 464 so2 = fp2->f_data; 465 sv[1] = fd; 466 467 solock(so1); 468 error = soconnect2(so1, so2); 469 if (error == 0 && type == SOCK_DGRAM) { 470 /* 471 * Datagram socket connection is asymmetric. 472 */ 473 error = soconnect2(so2, so1); 474 } 475 sounlock(so1); 476 477 if (error == 0) 478 error = copyout(sv, SCARG(uap, rsv), sizeof(sv)); 479 if (error == 0) { 480 fd_affix(p, fp2, sv[1]); 481 fd_affix(p, fp1, sv[0]); 482 return 0; 483 } 484 fd_abort(p, fp2, sv[1]); 485 (void)soclose(so2); 486 out: 487 fd_abort(p, fp1, sv[0]); 488 (void)soclose(so1); 489 return error; 490 } 491 492 int 493 sys_sendto(struct lwp *l, const struct sys_sendto_args *uap, register_t *retval) 494 { 495 /* { 496 syscallarg(int) s; 497 syscallarg(const void *) buf; 498 syscallarg(size_t) len; 499 syscallarg(int) flags; 500 syscallarg(const struct sockaddr *) to; 501 syscallarg(unsigned int) tolen; 502 } */ 503 struct msghdr msg; 504 struct iovec aiov; 505 506 msg.msg_name = __UNCONST(SCARG(uap, to)); /* XXXUNCONST kills const */ 507 msg.msg_namelen = SCARG(uap, tolen); 508 msg.msg_iov = &aiov; 509 msg.msg_iovlen = 1; 510 msg.msg_control = NULL; 511 msg.msg_flags = 0; 512 aiov.iov_base = __UNCONST(SCARG(uap, buf)); /* XXXUNCONST kills const */ 513 aiov.iov_len = SCARG(uap, len); 514 return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags), retval); 515 } 516 517 int 518 sys_sendmsg(struct lwp *l, const struct sys_sendmsg_args *uap, register_t *retval) 519 { 520 /* { 521 syscallarg(int) s; 522 syscallarg(const struct msghdr *) msg; 523 syscallarg(int) flags; 524 } */ 525 struct msghdr msg; 526 int error; 527 528 error = copyin(SCARG(uap, msg), &msg, sizeof(msg)); 529 if (error) 530 return (error); 531 532 msg.msg_flags = MSG_IOVUSRSPACE; 533 return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags), retval); 534 } 535 536 static int 537 do_sys_sendmsg_so(struct lwp *l, int s, struct socket *so, file_t *fp, 538 struct msghdr *mp, int flags, register_t *retsize) 539 { 540 541 struct iovec aiov[UIO_SMALLIOV], *iov = aiov, *tiov, *ktriov = NULL; 542 struct mbuf *to, *control; 543 struct uio auio; 544 size_t len, iovsz; 545 int i, error; 546 547 ktrkuser("msghdr", mp, sizeof *mp); 548 549 /* If the caller passed us stuff in mbufs, we must free them. */ 550 to = (mp->msg_flags & MSG_NAMEMBUF) ? mp->msg_name : NULL; 551 control = (mp->msg_flags & MSG_CONTROLMBUF) ? mp->msg_control : NULL; 552 iovsz = mp->msg_iovlen * sizeof(struct iovec); 553 554 if (mp->msg_flags & MSG_IOVUSRSPACE) { 555 if ((unsigned int)mp->msg_iovlen > UIO_SMALLIOV) { 556 if ((unsigned int)mp->msg_iovlen > IOV_MAX) { 557 error = EMSGSIZE; 558 goto bad; 559 } 560 iov = kmem_alloc(iovsz, KM_SLEEP); 561 } 562 if (mp->msg_iovlen != 0) { 563 error = copyin(mp->msg_iov, iov, iovsz); 564 if (error) 565 goto bad; 566 } 567 mp->msg_iov = iov; 568 } 569 570 auio.uio_iov = mp->msg_iov; 571 auio.uio_iovcnt = mp->msg_iovlen; 572 auio.uio_rw = UIO_WRITE; 573 auio.uio_offset = 0; /* XXX */ 574 auio.uio_resid = 0; 575 KASSERT(l == curlwp); 576 auio.uio_vmspace = l->l_proc->p_vmspace; 577 578 for (i = 0, tiov = mp->msg_iov; i < mp->msg_iovlen; i++, tiov++) { 579 /* 580 * Writes return ssize_t because -1 is returned on error. 581 * Therefore, we must restrict the length to SSIZE_MAX to 582 * avoid garbage return values. 583 */ 584 auio.uio_resid += tiov->iov_len; 585 if (tiov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) { 586 error = EINVAL; 587 goto bad; 588 } 589 } 590 591 if (mp->msg_name && to == NULL) { 592 error = sockargs(&to, mp->msg_name, mp->msg_namelen, 593 MT_SONAME); 594 if (error) 595 goto bad; 596 } 597 598 if (mp->msg_control) { 599 if (mp->msg_controllen < CMSG_ALIGN(sizeof(struct cmsghdr))) { 600 error = EINVAL; 601 goto bad; 602 } 603 if (control == NULL) { 604 error = sockargs(&control, mp->msg_control, 605 mp->msg_controllen, MT_CONTROL); 606 if (error) 607 goto bad; 608 } 609 } 610 611 if (ktrpoint(KTR_GENIO) && iovsz > 0) { 612 ktriov = kmem_alloc(iovsz, KM_SLEEP); 613 memcpy(ktriov, auio.uio_iov, iovsz); 614 } 615 616 if (mp->msg_name) 617 MCLAIM(to, so->so_mowner); 618 if (mp->msg_control) 619 MCLAIM(control, so->so_mowner); 620 621 len = auio.uio_resid; 622 error = (*so->so_send)(so, to, &auio, NULL, control, flags, l); 623 /* Protocol is responsible for freeing 'control' */ 624 control = NULL; 625 626 if (error) { 627 if (auio.uio_resid != len && (error == ERESTART || 628 error == EINTR || error == EWOULDBLOCK)) 629 error = 0; 630 if (error == EPIPE && (fp->f_flag & FNOSIGPIPE) == 0 && 631 (flags & MSG_NOSIGNAL) == 0) { 632 mutex_enter(proc_lock); 633 psignal(l->l_proc, SIGPIPE); 634 mutex_exit(proc_lock); 635 } 636 } 637 if (error == 0) 638 *retsize = len - auio.uio_resid; 639 640 bad: 641 if (ktriov != NULL) { 642 ktrgeniov(s, UIO_WRITE, ktriov, *retsize, error); 643 kmem_free(ktriov, iovsz); 644 } 645 646 if (iov != aiov) 647 kmem_free(iov, iovsz); 648 if (to) 649 m_freem(to); 650 if (control) 651 m_freem(control); 652 653 return (error); 654 } 655 656 int 657 do_sys_sendmsg(struct lwp *l, int s, struct msghdr *mp, int flags, 658 register_t *retsize) 659 { 660 int error; 661 struct socket *so; 662 file_t *fp; 663 664 if ((error = fd_getsock1(s, &so, &fp)) != 0) 665 return error; 666 error = do_sys_sendmsg_so(l, s, so, fp, mp, flags, retsize); 667 fd_putfile(s); 668 return error; 669 } 670 671 int 672 sys_recvfrom(struct lwp *l, const struct sys_recvfrom_args *uap, register_t *retval) 673 { 674 /* { 675 syscallarg(int) s; 676 syscallarg(void *) buf; 677 syscallarg(size_t) len; 678 syscallarg(int) flags; 679 syscallarg(struct sockaddr *) from; 680 syscallarg(unsigned int *) fromlenaddr; 681 } */ 682 struct msghdr msg; 683 struct iovec aiov; 684 int error; 685 struct mbuf *from; 686 687 msg.msg_name = NULL; 688 msg.msg_iov = &aiov; 689 msg.msg_iovlen = 1; 690 aiov.iov_base = SCARG(uap, buf); 691 aiov.iov_len = SCARG(uap, len); 692 msg.msg_control = NULL; 693 msg.msg_flags = SCARG(uap, flags) & MSG_USERFLAGS; 694 695 error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from, NULL, retval); 696 if (error != 0) 697 return error; 698 699 error = copyout_sockname(SCARG(uap, from), SCARG(uap, fromlenaddr), 700 MSG_LENUSRSPACE, from); 701 if (from != NULL) 702 m_free(from); 703 return error; 704 } 705 706 int 707 sys_recvmsg(struct lwp *l, const struct sys_recvmsg_args *uap, register_t *retval) 708 { 709 /* { 710 syscallarg(int) s; 711 syscallarg(struct msghdr *) msg; 712 syscallarg(int) flags; 713 } */ 714 struct msghdr msg; 715 int error; 716 struct mbuf *from, *control; 717 718 error = copyin(SCARG(uap, msg), &msg, sizeof(msg)); 719 if (error) 720 return (error); 721 722 msg.msg_flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE; 723 724 error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from, 725 msg.msg_control != NULL ? &control : NULL, retval); 726 if (error != 0) 727 return error; 728 729 if (msg.msg_control != NULL) 730 error = copyout_msg_control(l, &msg, control); 731 732 if (error == 0) 733 error = copyout_sockname(msg.msg_name, &msg.msg_namelen, 0, 734 from); 735 if (from != NULL) 736 m_free(from); 737 if (error == 0) { 738 ktrkuser("msghdr", &msg, sizeof msg); 739 error = copyout(&msg, SCARG(uap, msg), sizeof(msg)); 740 } 741 742 return (error); 743 } 744 745 int 746 sys_sendmmsg(struct lwp *l, const struct sys_sendmmsg_args *uap, 747 register_t *retval) 748 { 749 /* { 750 syscallarg(int) s; 751 syscallarg(struct mmsghdr *) mmsg; 752 syscallarg(unsigned int) vlen; 753 syscallarg(unsigned int) flags; 754 } */ 755 struct mmsghdr mmsg; 756 struct socket *so; 757 file_t *fp; 758 struct msghdr *msg = &mmsg.msg_hdr; 759 int error, s; 760 unsigned int vlen, flags, dg; 761 762 s = SCARG(uap, s); 763 if ((error = fd_getsock1(s, &so, &fp)) != 0) 764 return error; 765 766 vlen = SCARG(uap, vlen); 767 if (vlen > 1024) 768 vlen = 1024; 769 770 flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE; 771 772 for (dg = 0; dg < vlen;) { 773 error = copyin(SCARG(uap, mmsg) + dg, &mmsg, sizeof(mmsg)); 774 if (error) 775 break; 776 777 msg->msg_flags = flags; 778 779 error = do_sys_sendmsg_so(l, s, so, fp, msg, flags, retval); 780 if (error) 781 break; 782 783 ktrkuser("msghdr", msg, sizeof *msg); 784 mmsg.msg_len = *retval; 785 error = copyout(&mmsg, SCARG(uap, mmsg) + dg, sizeof(mmsg)); 786 if (error) 787 break; 788 dg++; 789 790 } 791 792 *retval = dg; 793 if (error) 794 so->so_error = error; 795 796 fd_putfile(s); 797 798 /* 799 * If we succeeded at least once, return 0, hopefully so->so_error 800 * will catch it next time. 801 */ 802 if (dg) 803 return 0; 804 return error; 805 } 806 807 /* 808 * Adjust for a truncated SCM_RIGHTS control message. 809 * This means closing any file descriptors that aren't present 810 * in the returned buffer. 811 * m is the mbuf holding the (already externalized) SCM_RIGHTS message. 812 */ 813 static void 814 free_rights(struct mbuf *m) 815 { 816 struct cmsghdr *cm; 817 int *fdv; 818 unsigned int nfds, i; 819 820 KASSERT(sizeof(*cm) <= m->m_len); 821 cm = mtod(m, struct cmsghdr *); 822 823 KASSERT(CMSG_ALIGN(sizeof(*cm)) <= cm->cmsg_len); 824 KASSERT(cm->cmsg_len <= m->m_len); 825 nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) / sizeof(int); 826 fdv = (int *)CMSG_DATA(cm); 827 828 for (i = 0; i < nfds; i++) 829 if (fd_getfile(fdv[i]) != NULL) 830 (void)fd_close(fdv[i]); 831 } 832 833 void 834 free_control_mbuf(struct lwp *l, struct mbuf *control, struct mbuf *uncopied) 835 { 836 struct mbuf *next; 837 struct cmsghdr *cmsg; 838 bool do_free_rights = false; 839 840 while (control != NULL) { 841 cmsg = mtod(control, struct cmsghdr *); 842 if (control == uncopied) 843 do_free_rights = true; 844 if (do_free_rights && cmsg->cmsg_level == SOL_SOCKET 845 && cmsg->cmsg_type == SCM_RIGHTS) 846 free_rights(control); 847 next = control->m_next; 848 m_free(control); 849 control = next; 850 } 851 } 852 853 /* Copy socket control/CMSG data to user buffer, frees the mbuf */ 854 int 855 copyout_msg_control(struct lwp *l, struct msghdr *mp, struct mbuf *control) 856 { 857 int i, len, error = 0; 858 struct cmsghdr *cmsg; 859 struct mbuf *m; 860 char *q; 861 862 len = mp->msg_controllen; 863 if (len <= 0 || control == 0) { 864 mp->msg_controllen = 0; 865 free_control_mbuf(l, control, control); 866 return 0; 867 } 868 869 q = (char *)mp->msg_control; 870 871 for (m = control; m != NULL; ) { 872 cmsg = mtod(m, struct cmsghdr *); 873 i = m->m_len; 874 if (len < i) { 875 mp->msg_flags |= MSG_CTRUNC; 876 if (cmsg->cmsg_level == SOL_SOCKET 877 && cmsg->cmsg_type == SCM_RIGHTS) 878 /* Do not truncate me ... */ 879 break; 880 i = len; 881 } 882 error = copyout(mtod(m, void *), q, i); 883 ktrkuser("msgcontrol", mtod(m, void *), i); 884 if (error != 0) { 885 /* We must free all the SCM_RIGHTS */ 886 m = control; 887 break; 888 } 889 m = m->m_next; 890 if (m) 891 i = ALIGN(i); 892 q += i; 893 len -= i; 894 if (len <= 0) 895 break; 896 } 897 898 free_control_mbuf(l, control, m); 899 900 mp->msg_controllen = q - (char *)mp->msg_control; 901 return error; 902 } 903 904 static int 905 do_sys_recvmsg_so(struct lwp *l, int s, struct socket *so, struct msghdr *mp, 906 struct mbuf **from, struct mbuf **control, register_t *retsize) 907 { 908 struct iovec aiov[UIO_SMALLIOV], *iov = aiov, *tiov, *ktriov = NULL; 909 struct uio auio; 910 size_t len, iovsz; 911 int i, error; 912 913 ktrkuser("msghdr", mp, sizeof *mp); 914 915 *from = NULL; 916 if (control != NULL) 917 *control = NULL; 918 919 iovsz = mp->msg_iovlen * sizeof(struct iovec); 920 921 if (mp->msg_flags & MSG_IOVUSRSPACE) { 922 if ((unsigned int)mp->msg_iovlen > UIO_SMALLIOV) { 923 if ((unsigned int)mp->msg_iovlen > IOV_MAX) { 924 error = EMSGSIZE; 925 goto out; 926 } 927 iov = kmem_alloc(iovsz, KM_SLEEP); 928 } 929 if (mp->msg_iovlen != 0) { 930 error = copyin(mp->msg_iov, iov, iovsz); 931 if (error) 932 goto out; 933 } 934 auio.uio_iov = iov; 935 } else 936 auio.uio_iov = mp->msg_iov; 937 auio.uio_iovcnt = mp->msg_iovlen; 938 auio.uio_rw = UIO_READ; 939 auio.uio_offset = 0; /* XXX */ 940 auio.uio_resid = 0; 941 KASSERT(l == curlwp); 942 auio.uio_vmspace = l->l_proc->p_vmspace; 943 944 tiov = auio.uio_iov; 945 for (i = 0; i < mp->msg_iovlen; i++, tiov++) { 946 /* 947 * Reads return ssize_t because -1 is returned on error. 948 * Therefore we must restrict the length to SSIZE_MAX to 949 * avoid garbage return values. 950 */ 951 auio.uio_resid += tiov->iov_len; 952 if (tiov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) { 953 error = EINVAL; 954 goto out; 955 } 956 } 957 958 if (ktrpoint(KTR_GENIO) && iovsz > 0) { 959 ktriov = kmem_alloc(iovsz, KM_SLEEP); 960 memcpy(ktriov, auio.uio_iov, iovsz); 961 } 962 963 len = auio.uio_resid; 964 mp->msg_flags &= MSG_USERFLAGS; 965 error = (*so->so_receive)(so, from, &auio, NULL, control, 966 &mp->msg_flags); 967 len -= auio.uio_resid; 968 *retsize = len; 969 if (error != 0 && len != 0 970 && (error == ERESTART || error == EINTR || error == EWOULDBLOCK)) 971 /* Some data transferred */ 972 error = 0; 973 974 if (ktriov != NULL) { 975 ktrgeniov(s, UIO_READ, ktriov, len, error); 976 kmem_free(ktriov, iovsz); 977 } 978 979 if (error != 0) { 980 m_freem(*from); 981 *from = NULL; 982 if (control != NULL) { 983 free_control_mbuf(l, *control, *control); 984 *control = NULL; 985 } 986 } 987 out: 988 if (iov != aiov) 989 kmem_free(iov, iovsz); 990 return (error); 991 } 992 993 994 int 995 do_sys_recvmsg(struct lwp *l, int s, struct msghdr *mp, struct mbuf **from, 996 struct mbuf **control, register_t *retsize) 997 { 998 int error; 999 struct socket *so; 1000 1001 if ((error = fd_getsock(s, &so)) != 0) 1002 return error; 1003 error = do_sys_recvmsg_so(l, s, so, mp, from, control, retsize); 1004 fd_putfile(s); 1005 return error; 1006 } 1007 1008 int 1009 sys_recvmmsg(struct lwp *l, const struct sys_recvmmsg_args *uap, 1010 register_t *retval) 1011 { 1012 /* { 1013 syscallarg(int) s; 1014 syscallarg(struct mmsghdr *) mmsg; 1015 syscallarg(unsigned int) vlen; 1016 syscallarg(unsigned int) flags; 1017 syscallarg(struct timespec *) timeout; 1018 } */ 1019 struct mmsghdr mmsg; 1020 struct socket *so; 1021 struct msghdr *msg = &mmsg.msg_hdr; 1022 int error, s; 1023 struct mbuf *from, *control; 1024 struct timespec ts, now; 1025 unsigned int vlen, flags, dg; 1026 1027 if (SCARG(uap, timeout)) { 1028 if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))) != 0) 1029 return error; 1030 getnanotime(&now); 1031 timespecadd(&now, &ts, &ts); 1032 } 1033 1034 s = SCARG(uap, s); 1035 if ((error = fd_getsock(s, &so)) != 0) 1036 return error; 1037 1038 vlen = SCARG(uap, vlen); 1039 if (vlen > 1024) 1040 vlen = 1024; 1041 1042 from = NULL; 1043 flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE; 1044 1045 for (dg = 0; dg < vlen;) { 1046 error = copyin(SCARG(uap, mmsg) + dg, &mmsg, sizeof(mmsg)); 1047 if (error) 1048 break; 1049 1050 msg->msg_flags = flags & ~MSG_WAITFORONE; 1051 1052 if (from != NULL) { 1053 m_free(from); 1054 from = NULL; 1055 } 1056 1057 error = do_sys_recvmsg_so(l, s, so, msg, &from, 1058 msg->msg_control != NULL ? &control : NULL, retval); 1059 if (error) 1060 break; 1061 1062 if (msg->msg_control != NULL) 1063 error = copyout_msg_control(l, msg, control); 1064 if (error) 1065 break; 1066 1067 error = copyout_sockname(msg->msg_name, &msg->msg_namelen, 0, 1068 from); 1069 if (error) 1070 break; 1071 1072 ktrkuser("msghdr", msg, sizeof *msg); 1073 mmsg.msg_len = *retval; 1074 1075 error = copyout(&mmsg, SCARG(uap, mmsg) + dg, sizeof(mmsg)); 1076 if (error) 1077 break; 1078 1079 dg++; 1080 if (msg->msg_flags & MSG_OOB) 1081 break; 1082 1083 if (SCARG(uap, timeout)) { 1084 getnanotime(&now); 1085 timespecsub(&now, &ts, &now); 1086 if (now.tv_sec > 0) 1087 break; 1088 } 1089 1090 if (flags & MSG_WAITFORONE) 1091 flags |= MSG_DONTWAIT; 1092 1093 } 1094 1095 if (from != NULL) 1096 m_free(from); 1097 1098 *retval = dg; 1099 if (error) 1100 so->so_error = error; 1101 1102 fd_putfile(s); 1103 1104 /* 1105 * If we succeeded at least once, return 0, hopefully so->so_error 1106 * will catch it next time. 1107 */ 1108 if (dg) 1109 return 0; 1110 1111 return error; 1112 } 1113 1114 /* ARGSUSED */ 1115 int 1116 sys_shutdown(struct lwp *l, const struct sys_shutdown_args *uap, register_t *retval) 1117 { 1118 /* { 1119 syscallarg(int) s; 1120 syscallarg(int) how; 1121 } */ 1122 struct socket *so; 1123 int error; 1124 1125 if ((error = fd_getsock(SCARG(uap, s), &so)) != 0) 1126 return (error); 1127 solock(so); 1128 error = soshutdown(so, SCARG(uap, how)); 1129 sounlock(so); 1130 fd_putfile(SCARG(uap, s)); 1131 return (error); 1132 } 1133 1134 /* ARGSUSED */ 1135 int 1136 sys_setsockopt(struct lwp *l, const struct sys_setsockopt_args *uap, register_t *retval) 1137 { 1138 /* { 1139 syscallarg(int) s; 1140 syscallarg(int) level; 1141 syscallarg(int) name; 1142 syscallarg(const void *) val; 1143 syscallarg(unsigned int) valsize; 1144 } */ 1145 struct sockopt sopt; 1146 struct socket *so; 1147 file_t *fp; 1148 int error; 1149 unsigned int len; 1150 1151 len = SCARG(uap, valsize); 1152 if (len > 0 && SCARG(uap, val) == NULL) 1153 return (EINVAL); 1154 1155 if (len > MCLBYTES) 1156 return (EINVAL); 1157 1158 if ((error = fd_getsock1(SCARG(uap, s), &so, &fp)) != 0) 1159 return (error); 1160 1161 sockopt_init(&sopt, SCARG(uap, level), SCARG(uap, name), len); 1162 1163 if (len > 0) { 1164 error = copyin(SCARG(uap, val), sopt.sopt_data, len); 1165 if (error) 1166 goto out; 1167 } 1168 1169 error = sosetopt(so, &sopt); 1170 if (so->so_options & SO_NOSIGPIPE) 1171 atomic_or_uint(&fp->f_flag, FNOSIGPIPE); 1172 else 1173 atomic_and_uint(&fp->f_flag, ~FNOSIGPIPE); 1174 1175 out: 1176 sockopt_destroy(&sopt); 1177 fd_putfile(SCARG(uap, s)); 1178 return (error); 1179 } 1180 1181 /* ARGSUSED */ 1182 int 1183 sys_getsockopt(struct lwp *l, const struct sys_getsockopt_args *uap, register_t *retval) 1184 { 1185 /* { 1186 syscallarg(int) s; 1187 syscallarg(int) level; 1188 syscallarg(int) name; 1189 syscallarg(void *) val; 1190 syscallarg(unsigned int *) avalsize; 1191 } */ 1192 struct sockopt sopt; 1193 struct socket *so; 1194 file_t *fp; 1195 unsigned int valsize, len; 1196 int error; 1197 1198 if (SCARG(uap, val) != NULL) { 1199 error = copyin(SCARG(uap, avalsize), &valsize, sizeof(valsize)); 1200 if (error) 1201 return (error); 1202 } else 1203 valsize = 0; 1204 1205 if ((error = fd_getsock1(SCARG(uap, s), &so, &fp)) != 0) 1206 return (error); 1207 1208 sockopt_init(&sopt, SCARG(uap, level), SCARG(uap, name), 0); 1209 1210 if (fp->f_flag & FNOSIGPIPE) 1211 so->so_options |= SO_NOSIGPIPE; 1212 else 1213 so->so_options &= ~SO_NOSIGPIPE; 1214 error = sogetopt(so, &sopt); 1215 if (error) 1216 goto out; 1217 1218 if (valsize > 0) { 1219 len = min(valsize, sopt.sopt_size); 1220 error = copyout(sopt.sopt_data, SCARG(uap, val), len); 1221 if (error) 1222 goto out; 1223 1224 error = copyout(&len, SCARG(uap, avalsize), sizeof(len)); 1225 if (error) 1226 goto out; 1227 } 1228 1229 out: 1230 sockopt_destroy(&sopt); 1231 fd_putfile(SCARG(uap, s)); 1232 return (error); 1233 } 1234 1235 #ifdef PIPE_SOCKETPAIR 1236 /* ARGSUSED */ 1237 int 1238 pipe1(struct lwp *l, register_t *retval, int flags) 1239 { 1240 file_t *rf, *wf; 1241 struct socket *rso, *wso; 1242 int fd, error; 1243 proc_t *p; 1244 1245 if (flags & ~(O_CLOEXEC|O_NONBLOCK|O_NOSIGPIPE)) 1246 return EINVAL; 1247 p = curproc; 1248 if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, l, NULL)) != 0) 1249 return (error); 1250 if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, l, rso)) != 0) 1251 goto free1; 1252 /* remember this socket pair implements a pipe */ 1253 wso->so_state |= SS_ISAPIPE; 1254 rso->so_state |= SS_ISAPIPE; 1255 if ((error = fd_allocfile(&rf, &fd)) != 0) 1256 goto free2; 1257 retval[0] = fd; 1258 rf->f_flag = FREAD | flags; 1259 rf->f_type = DTYPE_SOCKET; 1260 rf->f_ops = &socketops; 1261 rf->f_data = rso; 1262 if ((error = fd_allocfile(&wf, &fd)) != 0) 1263 goto free3; 1264 wf->f_flag = FWRITE | flags; 1265 wf->f_type = DTYPE_SOCKET; 1266 wf->f_ops = &socketops; 1267 wf->f_data = wso; 1268 retval[1] = fd; 1269 solock(wso); 1270 error = unp_connect2(wso, rso, PRU_CONNECT2); 1271 sounlock(wso); 1272 if (error != 0) 1273 goto free4; 1274 fd_affix(p, wf, (int)retval[1]); 1275 fd_affix(p, rf, (int)retval[0]); 1276 return (0); 1277 free4: 1278 fd_abort(p, wf, (int)retval[1]); 1279 free3: 1280 fd_abort(p, rf, (int)retval[0]); 1281 free2: 1282 (void)soclose(wso); 1283 free1: 1284 (void)soclose(rso); 1285 return (error); 1286 } 1287 #endif /* PIPE_SOCKETPAIR */ 1288 1289 /* 1290 * Get socket name. 1291 */ 1292 /* ARGSUSED */ 1293 int 1294 do_sys_getsockname(struct lwp *l, int fd, int which, struct mbuf **nam) 1295 { 1296 struct socket *so; 1297 struct mbuf *m; 1298 int error; 1299 1300 if ((error = fd_getsock(fd, &so)) != 0) 1301 return error; 1302 1303 m = m_getclr(M_WAIT, MT_SONAME); 1304 MCLAIM(m, so->so_mowner); 1305 1306 solock(so); 1307 if (which == PRU_PEERADDR 1308 && (so->so_state & (SS_ISCONNECTED | SS_ISCONFIRMING)) == 0) { 1309 error = ENOTCONN; 1310 } else { 1311 *nam = m; 1312 error = (*so->so_proto->pr_usrreq)(so, which, NULL, m, NULL, 1313 NULL); 1314 } 1315 sounlock(so); 1316 if (error != 0) 1317 m_free(m); 1318 fd_putfile(fd); 1319 return error; 1320 } 1321 1322 int 1323 copyout_sockname(struct sockaddr *asa, unsigned int *alen, int flags, 1324 struct mbuf *addr) 1325 { 1326 int len; 1327 int error; 1328 1329 if (asa == NULL) 1330 /* Assume application not interested */ 1331 return 0; 1332 1333 if (flags & MSG_LENUSRSPACE) { 1334 error = copyin(alen, &len, sizeof(len)); 1335 if (error) 1336 return error; 1337 } else 1338 len = *alen; 1339 if (len < 0) 1340 return EINVAL; 1341 1342 if (addr == NULL) { 1343 len = 0; 1344 error = 0; 1345 } else { 1346 if (len > addr->m_len) 1347 len = addr->m_len; 1348 /* Maybe this ought to copy a chain ? */ 1349 ktrkuser(mbuftypes[MT_SONAME], mtod(addr, void *), len); 1350 error = copyout(mtod(addr, void *), asa, len); 1351 } 1352 1353 if (error == 0) { 1354 if (flags & MSG_LENUSRSPACE) 1355 error = copyout(&len, alen, sizeof(len)); 1356 else 1357 *alen = len; 1358 } 1359 1360 return error; 1361 } 1362 1363 /* 1364 * Get socket name. 1365 */ 1366 /* ARGSUSED */ 1367 int 1368 sys_getsockname(struct lwp *l, const struct sys_getsockname_args *uap, register_t *retval) 1369 { 1370 /* { 1371 syscallarg(int) fdes; 1372 syscallarg(struct sockaddr *) asa; 1373 syscallarg(unsigned int *) alen; 1374 } */ 1375 struct mbuf *m; 1376 int error; 1377 1378 error = do_sys_getsockname(l, SCARG(uap, fdes), PRU_SOCKADDR, &m); 1379 if (error != 0) 1380 return error; 1381 1382 error = copyout_sockname(SCARG(uap, asa), SCARG(uap, alen), 1383 MSG_LENUSRSPACE, m); 1384 if (m != NULL) 1385 m_free(m); 1386 return error; 1387 } 1388 1389 /* 1390 * Get name of peer for connected socket. 1391 */ 1392 /* ARGSUSED */ 1393 int 1394 sys_getpeername(struct lwp *l, const struct sys_getpeername_args *uap, register_t *retval) 1395 { 1396 /* { 1397 syscallarg(int) fdes; 1398 syscallarg(struct sockaddr *) asa; 1399 syscallarg(unsigned int *) alen; 1400 } */ 1401 struct mbuf *m; 1402 int error; 1403 1404 error = do_sys_getsockname(l, SCARG(uap, fdes), PRU_PEERADDR, &m); 1405 if (error != 0) 1406 return error; 1407 1408 error = copyout_sockname(SCARG(uap, asa), SCARG(uap, alen), 1409 MSG_LENUSRSPACE, m); 1410 if (m != NULL) 1411 m_free(m); 1412 return error; 1413 } 1414 1415 /* 1416 * XXX In a perfect world, we wouldn't pass around socket control 1417 * XXX arguments in mbufs, and this could go away. 1418 */ 1419 int 1420 sockargs(struct mbuf **mp, const void *bf, size_t buflen, int type) 1421 { 1422 struct sockaddr *sa; 1423 struct mbuf *m; 1424 int error; 1425 1426 /* 1427 * We can't allow socket names > UCHAR_MAX in length, since that 1428 * will overflow sa_len. Control data more than a page size in 1429 * length is just too much. 1430 */ 1431 if (buflen > (type == MT_SONAME ? UCHAR_MAX : PAGE_SIZE)) 1432 return (EINVAL); 1433 1434 /* Allocate an mbuf to hold the arguments. */ 1435 m = m_get(M_WAIT, type); 1436 /* can't claim. don't who to assign it to. */ 1437 if (buflen > MLEN) { 1438 /* 1439 * Won't fit into a regular mbuf, so we allocate just 1440 * enough external storage to hold the argument. 1441 */ 1442 MEXTMALLOC(m, buflen, M_WAITOK); 1443 } 1444 m->m_len = buflen; 1445 error = copyin(bf, mtod(m, void *), buflen); 1446 if (error) { 1447 (void) m_free(m); 1448 return (error); 1449 } 1450 ktrkuser(mbuftypes[type], mtod(m, void *), buflen); 1451 *mp = m; 1452 if (type == MT_SONAME) { 1453 sa = mtod(m, struct sockaddr *); 1454 #if BYTE_ORDER != BIG_ENDIAN 1455 /* 1456 * 4.3BSD compat thing - need to stay, since bind(2), 1457 * connect(2), sendto(2) were not versioned for COMPAT_43. 1458 */ 1459 if (sa->sa_family == 0 && sa->sa_len < AF_MAX) 1460 sa->sa_family = sa->sa_len; 1461 #endif 1462 sa->sa_len = buflen; 1463 } 1464 return (0); 1465 } 1466