1 /* $NetBSD: uipc_syscalls.c,v 1.166 2014/04/07 15:35:23 seanb 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.166 2014/04/07 15:35:23 seanb 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 (fp2->f_flag & FNONBLOCK) 238 so2->so_state |= SS_NBIO; 239 else 240 so2->so_state &= ~SS_NBIO; 241 error = soaccept(so2, nam); 242 so2->so_cred = kauth_cred_dup(so->so_cred); 243 sounlock(so); 244 if (error) { 245 /* an error occurred, free the file descriptor and mbuf */ 246 m_freem(nam); 247 mutex_enter(&fp2->f_lock); 248 fp2->f_count++; 249 mutex_exit(&fp2->f_lock); 250 closef(fp2); 251 fd_abort(curproc, NULL, fd); 252 } else { 253 fd_set_exclose(l, fd, (flags & SOCK_CLOEXEC) != 0); 254 fd_affix(curproc, fp2, fd); 255 *name = nam; 256 } 257 fd_putfile(sock); 258 if (__predict_false(mask)) 259 sigsuspendteardown(l); 260 return (error); 261 bad: 262 sounlock(so); 263 m_freem(nam); 264 fd_putfile(sock); 265 fd_abort(curproc, fp2, fd); 266 if (__predict_false(mask)) 267 sigsuspendteardown(l); 268 return (error); 269 } 270 271 int 272 sys_accept(struct lwp *l, const struct sys_accept_args *uap, register_t *retval) 273 { 274 /* { 275 syscallarg(int) s; 276 syscallarg(struct sockaddr *) name; 277 syscallarg(unsigned int *) anamelen; 278 } */ 279 int error, fd; 280 struct mbuf *name; 281 282 error = do_sys_accept(l, SCARG(uap, s), &name, retval, NULL, 0, 0); 283 if (error != 0) 284 return error; 285 error = copyout_sockname(SCARG(uap, name), SCARG(uap, anamelen), 286 MSG_LENUSRSPACE, name); 287 if (name != NULL) 288 m_free(name); 289 if (error != 0) { 290 fd = (int)*retval; 291 if (fd_getfile(fd) != NULL) 292 (void)fd_close(fd); 293 } 294 return error; 295 } 296 297 int 298 sys_paccept(struct lwp *l, const struct sys_paccept_args *uap, 299 register_t *retval) 300 { 301 /* { 302 syscallarg(int) s; 303 syscallarg(struct sockaddr *) name; 304 syscallarg(unsigned int *) anamelen; 305 syscallarg(const sigset_t *) mask; 306 syscallarg(int) flags; 307 } */ 308 int error, fd; 309 struct mbuf *name; 310 sigset_t *mask, amask; 311 312 if (SCARG(uap, mask) != NULL) { 313 error = copyin(SCARG(uap, mask), &amask, sizeof(amask)); 314 if (error) 315 return error; 316 mask = &amask; 317 } else 318 mask = NULL; 319 320 error = do_sys_accept(l, SCARG(uap, s), &name, retval, mask, 321 SCARG(uap, flags), FNONBLOCK); 322 if (error != 0) 323 return error; 324 error = copyout_sockname(SCARG(uap, name), SCARG(uap, anamelen), 325 MSG_LENUSRSPACE, name); 326 if (name != NULL) 327 m_free(name); 328 if (error != 0) { 329 fd = (int)*retval; 330 if (fd_getfile(fd) != NULL) 331 (void)fd_close(fd); 332 } 333 return error; 334 } 335 336 /* ARGSUSED */ 337 int 338 sys_connect(struct lwp *l, const struct sys_connect_args *uap, register_t *retval) 339 { 340 /* { 341 syscallarg(int) s; 342 syscallarg(const struct sockaddr *) name; 343 syscallarg(unsigned int) namelen; 344 } */ 345 int error; 346 struct mbuf *nam; 347 348 error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen), 349 MT_SONAME); 350 if (error) 351 return error; 352 return do_sys_connect(l, SCARG(uap, s), nam); 353 } 354 355 int 356 do_sys_connect(struct lwp *l, int fd, struct mbuf *nam) 357 { 358 struct socket *so; 359 int error; 360 int interrupted = 0; 361 362 if ((error = fd_getsock(fd, &so)) != 0) { 363 m_freem(nam); 364 return (error); 365 } 366 solock(so); 367 MCLAIM(nam, so->so_mowner); 368 if ((so->so_state & SS_ISCONNECTING) != 0) { 369 error = EALREADY; 370 goto out; 371 } 372 373 error = soconnect(so, nam, l); 374 if (error) 375 goto bad; 376 if ((so->so_state & (SS_NBIO|SS_ISCONNECTING)) == 377 (SS_NBIO|SS_ISCONNECTING)) { 378 error = EINPROGRESS; 379 goto out; 380 } 381 while ((so->so_state & SS_ISCONNECTING) != 0 && so->so_error == 0) { 382 error = sowait(so, true, 0); 383 if (__predict_false((so->so_state & SS_ISABORTING) != 0)) { 384 error = EPIPE; 385 interrupted = 1; 386 break; 387 } 388 if (error) { 389 if (error == EINTR || error == ERESTART) 390 interrupted = 1; 391 break; 392 } 393 } 394 if (error == 0) { 395 error = so->so_error; 396 so->so_error = 0; 397 } 398 bad: 399 if (!interrupted) 400 so->so_state &= ~SS_ISCONNECTING; 401 if (error == ERESTART) 402 error = EINTR; 403 out: 404 sounlock(so); 405 fd_putfile(fd); 406 m_freem(nam); 407 return (error); 408 } 409 410 static int 411 makesocket(struct lwp *l, file_t **fp, int *fd, int flags, int type, 412 int domain, int proto, struct socket *soo) 413 { 414 int error; 415 struct socket *so; 416 417 if ((error = socreate(domain, &so, type, proto, l, soo)) != 0) 418 return error; 419 420 if ((error = fd_allocfile(fp, fd)) != 0) { 421 soclose(so); 422 return error; 423 } 424 fd_set_exclose(l, *fd, (flags & SOCK_CLOEXEC) != 0); 425 (*fp)->f_flag = FREAD|FWRITE| 426 ((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)| 427 ((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0); 428 (*fp)->f_type = DTYPE_SOCKET; 429 (*fp)->f_ops = &socketops; 430 (*fp)->f_data = so; 431 if (flags & SOCK_NONBLOCK) 432 so->so_state |= SS_NBIO; 433 return 0; 434 } 435 436 int 437 sys_socketpair(struct lwp *l, const struct sys_socketpair_args *uap, 438 register_t *retval) 439 { 440 /* { 441 syscallarg(int) domain; 442 syscallarg(int) type; 443 syscallarg(int) protocol; 444 syscallarg(int *) rsv; 445 } */ 446 file_t *fp1, *fp2; 447 struct socket *so1, *so2; 448 int fd, error, sv[2]; 449 proc_t *p; 450 int flags = SCARG(uap, type) & SOCK_FLAGS_MASK; 451 int type = SCARG(uap, type) & ~SOCK_FLAGS_MASK; 452 int domain = SCARG(uap, domain); 453 int proto = SCARG(uap, protocol); 454 455 p = curproc; 456 457 error = makesocket(l, &fp1, &fd, flags, type, domain, proto, NULL); 458 if (error) 459 return error; 460 so1 = fp1->f_data; 461 sv[0] = fd; 462 463 error = makesocket(l, &fp2, &fd, flags, type, domain, proto, so1); 464 if (error) 465 goto out; 466 so2 = fp2->f_data; 467 sv[1] = fd; 468 469 solock(so1); 470 error = soconnect2(so1, so2); 471 if (error == 0 && type == SOCK_DGRAM) { 472 /* 473 * Datagram socket connection is asymmetric. 474 */ 475 error = soconnect2(so2, so1); 476 } 477 sounlock(so1); 478 479 if (error == 0) 480 error = copyout(sv, SCARG(uap, rsv), sizeof(sv)); 481 if (error == 0) { 482 fd_affix(p, fp2, sv[1]); 483 fd_affix(p, fp1, sv[0]); 484 return 0; 485 } 486 fd_abort(p, fp2, sv[1]); 487 (void)soclose(so2); 488 out: 489 fd_abort(p, fp1, sv[0]); 490 (void)soclose(so1); 491 return error; 492 } 493 494 int 495 sys_sendto(struct lwp *l, const struct sys_sendto_args *uap, register_t *retval) 496 { 497 /* { 498 syscallarg(int) s; 499 syscallarg(const void *) buf; 500 syscallarg(size_t) len; 501 syscallarg(int) flags; 502 syscallarg(const struct sockaddr *) to; 503 syscallarg(unsigned int) tolen; 504 } */ 505 struct msghdr msg; 506 struct iovec aiov; 507 508 msg.msg_name = __UNCONST(SCARG(uap, to)); /* XXXUNCONST kills const */ 509 msg.msg_namelen = SCARG(uap, tolen); 510 msg.msg_iov = &aiov; 511 msg.msg_iovlen = 1; 512 msg.msg_control = NULL; 513 msg.msg_flags = 0; 514 aiov.iov_base = __UNCONST(SCARG(uap, buf)); /* XXXUNCONST kills const */ 515 aiov.iov_len = SCARG(uap, len); 516 return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags), retval); 517 } 518 519 int 520 sys_sendmsg(struct lwp *l, const struct sys_sendmsg_args *uap, register_t *retval) 521 { 522 /* { 523 syscallarg(int) s; 524 syscallarg(const struct msghdr *) msg; 525 syscallarg(int) flags; 526 } */ 527 struct msghdr msg; 528 int error; 529 530 error = copyin(SCARG(uap, msg), &msg, sizeof(msg)); 531 if (error) 532 return (error); 533 534 msg.msg_flags = MSG_IOVUSRSPACE; 535 return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags), retval); 536 } 537 538 static int 539 do_sys_sendmsg_so(struct lwp *l, int s, struct socket *so, file_t *fp, 540 struct msghdr *mp, int flags, register_t *retsize) 541 { 542 543 struct iovec aiov[UIO_SMALLIOV], *iov = aiov, *tiov, *ktriov = NULL; 544 struct mbuf *to, *control; 545 struct uio auio; 546 size_t len, iovsz; 547 int i, error; 548 549 ktrkuser("msghdr", mp, sizeof *mp); 550 551 /* If the caller passed us stuff in mbufs, we must free them. */ 552 to = (mp->msg_flags & MSG_NAMEMBUF) ? mp->msg_name : NULL; 553 control = (mp->msg_flags & MSG_CONTROLMBUF) ? mp->msg_control : NULL; 554 iovsz = mp->msg_iovlen * sizeof(struct iovec); 555 556 if (mp->msg_flags & MSG_IOVUSRSPACE) { 557 if ((unsigned int)mp->msg_iovlen > UIO_SMALLIOV) { 558 if ((unsigned int)mp->msg_iovlen > IOV_MAX) { 559 error = EMSGSIZE; 560 goto bad; 561 } 562 iov = kmem_alloc(iovsz, KM_SLEEP); 563 } 564 if (mp->msg_iovlen != 0) { 565 error = copyin(mp->msg_iov, iov, iovsz); 566 if (error) 567 goto bad; 568 } 569 mp->msg_iov = iov; 570 } 571 572 auio.uio_iov = mp->msg_iov; 573 auio.uio_iovcnt = mp->msg_iovlen; 574 auio.uio_rw = UIO_WRITE; 575 auio.uio_offset = 0; /* XXX */ 576 auio.uio_resid = 0; 577 KASSERT(l == curlwp); 578 auio.uio_vmspace = l->l_proc->p_vmspace; 579 580 for (i = 0, tiov = mp->msg_iov; i < mp->msg_iovlen; i++, tiov++) { 581 /* 582 * Writes return ssize_t because -1 is returned on error. 583 * Therefore, we must restrict the length to SSIZE_MAX to 584 * avoid garbage return values. 585 */ 586 auio.uio_resid += tiov->iov_len; 587 if (tiov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) { 588 error = EINVAL; 589 goto bad; 590 } 591 } 592 593 if (mp->msg_name && to == NULL) { 594 error = sockargs(&to, mp->msg_name, mp->msg_namelen, 595 MT_SONAME); 596 if (error) 597 goto bad; 598 } 599 600 if (mp->msg_control) { 601 if (mp->msg_controllen < CMSG_ALIGN(sizeof(struct cmsghdr))) { 602 error = EINVAL; 603 goto bad; 604 } 605 if (control == NULL) { 606 error = sockargs(&control, mp->msg_control, 607 mp->msg_controllen, MT_CONTROL); 608 if (error) 609 goto bad; 610 } 611 } 612 613 if (ktrpoint(KTR_GENIO) && iovsz > 0) { 614 ktriov = kmem_alloc(iovsz, KM_SLEEP); 615 memcpy(ktriov, auio.uio_iov, iovsz); 616 } 617 618 if (mp->msg_name) 619 MCLAIM(to, so->so_mowner); 620 if (mp->msg_control) 621 MCLAIM(control, so->so_mowner); 622 623 len = auio.uio_resid; 624 error = (*so->so_send)(so, to, &auio, NULL, control, flags, l); 625 /* Protocol is responsible for freeing 'control' */ 626 control = NULL; 627 628 if (error) { 629 if (auio.uio_resid != len && (error == ERESTART || 630 error == EINTR || error == EWOULDBLOCK)) 631 error = 0; 632 if (error == EPIPE && (fp->f_flag & FNOSIGPIPE) == 0 && 633 (flags & MSG_NOSIGNAL) == 0) { 634 mutex_enter(proc_lock); 635 psignal(l->l_proc, SIGPIPE); 636 mutex_exit(proc_lock); 637 } 638 } 639 if (error == 0) 640 *retsize = len - auio.uio_resid; 641 642 bad: 643 if (ktriov != NULL) { 644 ktrgeniov(s, UIO_WRITE, ktriov, *retsize, error); 645 kmem_free(ktriov, iovsz); 646 } 647 648 if (iov != aiov) 649 kmem_free(iov, iovsz); 650 if (to) 651 m_freem(to); 652 if (control) 653 m_freem(control); 654 655 return (error); 656 } 657 658 int 659 do_sys_sendmsg(struct lwp *l, int s, struct msghdr *mp, int flags, 660 register_t *retsize) 661 { 662 int error; 663 struct socket *so; 664 file_t *fp; 665 666 if ((error = fd_getsock1(s, &so, &fp)) != 0) 667 return error; 668 error = do_sys_sendmsg_so(l, s, so, fp, mp, flags, retsize); 669 fd_putfile(s); 670 return error; 671 } 672 673 int 674 sys_recvfrom(struct lwp *l, const struct sys_recvfrom_args *uap, register_t *retval) 675 { 676 /* { 677 syscallarg(int) s; 678 syscallarg(void *) buf; 679 syscallarg(size_t) len; 680 syscallarg(int) flags; 681 syscallarg(struct sockaddr *) from; 682 syscallarg(unsigned int *) fromlenaddr; 683 } */ 684 struct msghdr msg; 685 struct iovec aiov; 686 int error; 687 struct mbuf *from; 688 689 msg.msg_name = NULL; 690 msg.msg_iov = &aiov; 691 msg.msg_iovlen = 1; 692 aiov.iov_base = SCARG(uap, buf); 693 aiov.iov_len = SCARG(uap, len); 694 msg.msg_control = NULL; 695 msg.msg_flags = SCARG(uap, flags) & MSG_USERFLAGS; 696 697 error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from, NULL, retval); 698 if (error != 0) 699 return error; 700 701 error = copyout_sockname(SCARG(uap, from), SCARG(uap, fromlenaddr), 702 MSG_LENUSRSPACE, from); 703 if (from != NULL) 704 m_free(from); 705 return error; 706 } 707 708 int 709 sys_recvmsg(struct lwp *l, const struct sys_recvmsg_args *uap, register_t *retval) 710 { 711 /* { 712 syscallarg(int) s; 713 syscallarg(struct msghdr *) msg; 714 syscallarg(int) flags; 715 } */ 716 struct msghdr msg; 717 int error; 718 struct mbuf *from, *control; 719 720 error = copyin(SCARG(uap, msg), &msg, sizeof(msg)); 721 if (error) 722 return (error); 723 724 msg.msg_flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE; 725 726 error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from, 727 msg.msg_control != NULL ? &control : NULL, retval); 728 if (error != 0) 729 return error; 730 731 if (msg.msg_control != NULL) 732 error = copyout_msg_control(l, &msg, control); 733 734 if (error == 0) 735 error = copyout_sockname(msg.msg_name, &msg.msg_namelen, 0, 736 from); 737 if (from != NULL) 738 m_free(from); 739 if (error == 0) { 740 ktrkuser("msghdr", &msg, sizeof msg); 741 error = copyout(&msg, SCARG(uap, msg), sizeof(msg)); 742 } 743 744 return (error); 745 } 746 747 int 748 sys_sendmmsg(struct lwp *l, const struct sys_sendmmsg_args *uap, 749 register_t *retval) 750 { 751 /* { 752 syscallarg(int) s; 753 syscallarg(struct mmsghdr *) mmsg; 754 syscallarg(unsigned int) vlen; 755 syscallarg(unsigned int) flags; 756 } */ 757 struct mmsghdr mmsg; 758 struct socket *so; 759 file_t *fp; 760 struct msghdr *msg = &mmsg.msg_hdr; 761 int error, s; 762 unsigned int vlen, flags, dg; 763 764 s = SCARG(uap, s); 765 if ((error = fd_getsock1(s, &so, &fp)) != 0) 766 return error; 767 768 vlen = SCARG(uap, vlen); 769 if (vlen > 1024) 770 vlen = 1024; 771 772 flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE; 773 774 for (dg = 0; dg < vlen;) { 775 error = copyin(SCARG(uap, mmsg) + dg, &mmsg, sizeof(mmsg)); 776 if (error) 777 break; 778 779 msg->msg_flags = flags; 780 781 error = do_sys_sendmsg_so(l, s, so, fp, msg, flags, retval); 782 if (error) 783 break; 784 785 ktrkuser("msghdr", msg, sizeof *msg); 786 mmsg.msg_len = *retval; 787 error = copyout(&mmsg, SCARG(uap, mmsg) + dg, sizeof(mmsg)); 788 if (error) 789 break; 790 dg++; 791 792 } 793 794 *retval = dg; 795 if (error) 796 so->so_error = error; 797 798 fd_putfile(s); 799 800 /* 801 * If we succeeded at least once, return 0, hopefully so->so_error 802 * will catch it next time. 803 */ 804 if (dg) 805 return 0; 806 return error; 807 } 808 809 /* 810 * Adjust for a truncated SCM_RIGHTS control message. 811 * This means closing any file descriptors that aren't present 812 * in the returned buffer. 813 * m is the mbuf holding the (already externalized) SCM_RIGHTS message. 814 */ 815 static void 816 free_rights(struct mbuf *m) 817 { 818 struct cmsghdr *cm; 819 int *fdv; 820 unsigned int nfds, i; 821 822 KASSERT(sizeof(*cm) <= m->m_len); 823 cm = mtod(m, struct cmsghdr *); 824 825 KASSERT(CMSG_ALIGN(sizeof(*cm)) <= cm->cmsg_len); 826 KASSERT(cm->cmsg_len <= m->m_len); 827 nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) / sizeof(int); 828 fdv = (int *)CMSG_DATA(cm); 829 830 for (i = 0; i < nfds; i++) 831 if (fd_getfile(fdv[i]) != NULL) 832 (void)fd_close(fdv[i]); 833 } 834 835 void 836 free_control_mbuf(struct lwp *l, struct mbuf *control, struct mbuf *uncopied) 837 { 838 struct mbuf *next; 839 struct cmsghdr *cmsg; 840 bool do_free_rights = false; 841 842 while (control != NULL) { 843 cmsg = mtod(control, struct cmsghdr *); 844 if (control == uncopied) 845 do_free_rights = true; 846 if (do_free_rights && cmsg->cmsg_level == SOL_SOCKET 847 && cmsg->cmsg_type == SCM_RIGHTS) 848 free_rights(control); 849 next = control->m_next; 850 m_free(control); 851 control = next; 852 } 853 } 854 855 /* Copy socket control/CMSG data to user buffer, frees the mbuf */ 856 int 857 copyout_msg_control(struct lwp *l, struct msghdr *mp, struct mbuf *control) 858 { 859 int i, len, error = 0; 860 struct cmsghdr *cmsg; 861 struct mbuf *m; 862 char *q; 863 864 len = mp->msg_controllen; 865 if (len <= 0 || control == 0) { 866 mp->msg_controllen = 0; 867 free_control_mbuf(l, control, control); 868 return 0; 869 } 870 871 q = (char *)mp->msg_control; 872 873 for (m = control; m != NULL; ) { 874 cmsg = mtod(m, struct cmsghdr *); 875 i = m->m_len; 876 if (len < i) { 877 mp->msg_flags |= MSG_CTRUNC; 878 if (cmsg->cmsg_level == SOL_SOCKET 879 && cmsg->cmsg_type == SCM_RIGHTS) 880 /* Do not truncate me ... */ 881 break; 882 i = len; 883 } 884 error = copyout(mtod(m, void *), q, i); 885 ktrkuser("msgcontrol", mtod(m, void *), i); 886 if (error != 0) { 887 /* We must free all the SCM_RIGHTS */ 888 m = control; 889 break; 890 } 891 m = m->m_next; 892 if (m) 893 i = ALIGN(i); 894 q += i; 895 len -= i; 896 if (len <= 0) 897 break; 898 } 899 900 free_control_mbuf(l, control, m); 901 902 mp->msg_controllen = q - (char *)mp->msg_control; 903 return error; 904 } 905 906 static int 907 do_sys_recvmsg_so(struct lwp *l, int s, struct socket *so, struct msghdr *mp, 908 struct mbuf **from, struct mbuf **control, register_t *retsize) 909 { 910 struct iovec aiov[UIO_SMALLIOV], *iov = aiov, *tiov, *ktriov = NULL; 911 struct uio auio; 912 size_t len, iovsz; 913 int i, error; 914 915 ktrkuser("msghdr", mp, sizeof *mp); 916 917 *from = NULL; 918 if (control != NULL) 919 *control = NULL; 920 921 iovsz = mp->msg_iovlen * sizeof(struct iovec); 922 923 if (mp->msg_flags & MSG_IOVUSRSPACE) { 924 if ((unsigned int)mp->msg_iovlen > UIO_SMALLIOV) { 925 if ((unsigned int)mp->msg_iovlen > IOV_MAX) { 926 error = EMSGSIZE; 927 goto out; 928 } 929 iov = kmem_alloc(iovsz, KM_SLEEP); 930 } 931 if (mp->msg_iovlen != 0) { 932 error = copyin(mp->msg_iov, iov, iovsz); 933 if (error) 934 goto out; 935 } 936 auio.uio_iov = iov; 937 } else 938 auio.uio_iov = mp->msg_iov; 939 auio.uio_iovcnt = mp->msg_iovlen; 940 auio.uio_rw = UIO_READ; 941 auio.uio_offset = 0; /* XXX */ 942 auio.uio_resid = 0; 943 KASSERT(l == curlwp); 944 auio.uio_vmspace = l->l_proc->p_vmspace; 945 946 tiov = auio.uio_iov; 947 for (i = 0; i < mp->msg_iovlen; i++, tiov++) { 948 /* 949 * Reads return ssize_t because -1 is returned on error. 950 * Therefore we must restrict the length to SSIZE_MAX to 951 * avoid garbage return values. 952 */ 953 auio.uio_resid += tiov->iov_len; 954 if (tiov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) { 955 error = EINVAL; 956 goto out; 957 } 958 } 959 960 if (ktrpoint(KTR_GENIO) && iovsz > 0) { 961 ktriov = kmem_alloc(iovsz, KM_SLEEP); 962 memcpy(ktriov, auio.uio_iov, iovsz); 963 } 964 965 len = auio.uio_resid; 966 mp->msg_flags &= MSG_USERFLAGS; 967 error = (*so->so_receive)(so, from, &auio, NULL, control, 968 &mp->msg_flags); 969 len -= auio.uio_resid; 970 *retsize = len; 971 if (error != 0 && len != 0 972 && (error == ERESTART || error == EINTR || error == EWOULDBLOCK)) 973 /* Some data transferred */ 974 error = 0; 975 976 if (ktriov != NULL) { 977 ktrgeniov(s, UIO_READ, ktriov, len, error); 978 kmem_free(ktriov, iovsz); 979 } 980 981 if (error != 0) { 982 m_freem(*from); 983 *from = NULL; 984 if (control != NULL) { 985 free_control_mbuf(l, *control, *control); 986 *control = NULL; 987 } 988 } 989 out: 990 if (iov != aiov) 991 kmem_free(iov, iovsz); 992 return (error); 993 } 994 995 996 int 997 do_sys_recvmsg(struct lwp *l, int s, struct msghdr *mp, struct mbuf **from, 998 struct mbuf **control, register_t *retsize) 999 { 1000 int error; 1001 struct socket *so; 1002 1003 if ((error = fd_getsock(s, &so)) != 0) 1004 return error; 1005 error = do_sys_recvmsg_so(l, s, so, mp, from, control, retsize); 1006 fd_putfile(s); 1007 return error; 1008 } 1009 1010 int 1011 sys_recvmmsg(struct lwp *l, const struct sys_recvmmsg_args *uap, 1012 register_t *retval) 1013 { 1014 /* { 1015 syscallarg(int) s; 1016 syscallarg(struct mmsghdr *) mmsg; 1017 syscallarg(unsigned int) vlen; 1018 syscallarg(unsigned int) flags; 1019 syscallarg(struct timespec *) timeout; 1020 } */ 1021 struct mmsghdr mmsg; 1022 struct socket *so; 1023 struct msghdr *msg = &mmsg.msg_hdr; 1024 int error, s; 1025 struct mbuf *from, *control; 1026 struct timespec ts, now; 1027 unsigned int vlen, flags, dg; 1028 1029 if (SCARG(uap, timeout)) { 1030 if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))) != 0) 1031 return error; 1032 getnanotime(&now); 1033 timespecadd(&now, &ts, &ts); 1034 } 1035 1036 s = SCARG(uap, s); 1037 if ((error = fd_getsock(s, &so)) != 0) 1038 return error; 1039 1040 vlen = SCARG(uap, vlen); 1041 if (vlen > 1024) 1042 vlen = 1024; 1043 1044 from = NULL; 1045 flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE; 1046 1047 for (dg = 0; dg < vlen;) { 1048 error = copyin(SCARG(uap, mmsg) + dg, &mmsg, sizeof(mmsg)); 1049 if (error) 1050 break; 1051 1052 msg->msg_flags = flags & ~MSG_WAITFORONE; 1053 1054 if (from != NULL) { 1055 m_free(from); 1056 from = NULL; 1057 } 1058 1059 error = do_sys_recvmsg_so(l, s, so, msg, &from, 1060 msg->msg_control != NULL ? &control : NULL, retval); 1061 if (error) { 1062 if (error == EAGAIN && dg > 0) 1063 error = 0; 1064 break; 1065 } 1066 1067 if (msg->msg_control != NULL) 1068 error = copyout_msg_control(l, msg, control); 1069 if (error) 1070 break; 1071 1072 error = copyout_sockname(msg->msg_name, &msg->msg_namelen, 0, 1073 from); 1074 if (error) 1075 break; 1076 1077 ktrkuser("msghdr", msg, sizeof *msg); 1078 mmsg.msg_len = *retval; 1079 1080 error = copyout(&mmsg, SCARG(uap, mmsg) + dg, sizeof(mmsg)); 1081 if (error) 1082 break; 1083 1084 dg++; 1085 if (msg->msg_flags & MSG_OOB) 1086 break; 1087 1088 if (SCARG(uap, timeout)) { 1089 getnanotime(&now); 1090 timespecsub(&now, &ts, &now); 1091 if (now.tv_sec > 0) 1092 break; 1093 } 1094 1095 if (flags & MSG_WAITFORONE) 1096 flags |= MSG_DONTWAIT; 1097 1098 } 1099 1100 if (from != NULL) 1101 m_free(from); 1102 1103 *retval = dg; 1104 if (error) 1105 so->so_error = error; 1106 1107 fd_putfile(s); 1108 1109 /* 1110 * If we succeeded at least once, return 0, hopefully so->so_error 1111 * will catch it next time. 1112 */ 1113 if (dg) 1114 return 0; 1115 1116 return error; 1117 } 1118 1119 /* ARGSUSED */ 1120 int 1121 sys_shutdown(struct lwp *l, const struct sys_shutdown_args *uap, register_t *retval) 1122 { 1123 /* { 1124 syscallarg(int) s; 1125 syscallarg(int) how; 1126 } */ 1127 struct socket *so; 1128 int error; 1129 1130 if ((error = fd_getsock(SCARG(uap, s), &so)) != 0) 1131 return (error); 1132 solock(so); 1133 error = soshutdown(so, SCARG(uap, how)); 1134 sounlock(so); 1135 fd_putfile(SCARG(uap, s)); 1136 return (error); 1137 } 1138 1139 /* ARGSUSED */ 1140 int 1141 sys_setsockopt(struct lwp *l, const struct sys_setsockopt_args *uap, register_t *retval) 1142 { 1143 /* { 1144 syscallarg(int) s; 1145 syscallarg(int) level; 1146 syscallarg(int) name; 1147 syscallarg(const void *) val; 1148 syscallarg(unsigned int) valsize; 1149 } */ 1150 struct sockopt sopt; 1151 struct socket *so; 1152 file_t *fp; 1153 int error; 1154 unsigned int len; 1155 1156 len = SCARG(uap, valsize); 1157 if (len > 0 && SCARG(uap, val) == NULL) 1158 return (EINVAL); 1159 1160 if (len > MCLBYTES) 1161 return (EINVAL); 1162 1163 if ((error = fd_getsock1(SCARG(uap, s), &so, &fp)) != 0) 1164 return (error); 1165 1166 sockopt_init(&sopt, SCARG(uap, level), SCARG(uap, name), len); 1167 1168 if (len > 0) { 1169 error = copyin(SCARG(uap, val), sopt.sopt_data, len); 1170 if (error) 1171 goto out; 1172 } 1173 1174 error = sosetopt(so, &sopt); 1175 if (so->so_options & SO_NOSIGPIPE) 1176 atomic_or_uint(&fp->f_flag, FNOSIGPIPE); 1177 else 1178 atomic_and_uint(&fp->f_flag, ~FNOSIGPIPE); 1179 1180 out: 1181 sockopt_destroy(&sopt); 1182 fd_putfile(SCARG(uap, s)); 1183 return (error); 1184 } 1185 1186 /* ARGSUSED */ 1187 int 1188 sys_getsockopt(struct lwp *l, const struct sys_getsockopt_args *uap, register_t *retval) 1189 { 1190 /* { 1191 syscallarg(int) s; 1192 syscallarg(int) level; 1193 syscallarg(int) name; 1194 syscallarg(void *) val; 1195 syscallarg(unsigned int *) avalsize; 1196 } */ 1197 struct sockopt sopt; 1198 struct socket *so; 1199 file_t *fp; 1200 unsigned int valsize, len; 1201 int error; 1202 1203 if (SCARG(uap, val) != NULL) { 1204 error = copyin(SCARG(uap, avalsize), &valsize, sizeof(valsize)); 1205 if (error) 1206 return (error); 1207 } else 1208 valsize = 0; 1209 1210 if ((error = fd_getsock1(SCARG(uap, s), &so, &fp)) != 0) 1211 return (error); 1212 1213 sockopt_init(&sopt, SCARG(uap, level), SCARG(uap, name), 0); 1214 1215 if (fp->f_flag & FNOSIGPIPE) 1216 so->so_options |= SO_NOSIGPIPE; 1217 else 1218 so->so_options &= ~SO_NOSIGPIPE; 1219 error = sogetopt(so, &sopt); 1220 if (error) 1221 goto out; 1222 1223 if (valsize > 0) { 1224 len = min(valsize, sopt.sopt_size); 1225 error = copyout(sopt.sopt_data, SCARG(uap, val), len); 1226 if (error) 1227 goto out; 1228 1229 error = copyout(&len, SCARG(uap, avalsize), sizeof(len)); 1230 if (error) 1231 goto out; 1232 } 1233 1234 out: 1235 sockopt_destroy(&sopt); 1236 fd_putfile(SCARG(uap, s)); 1237 return (error); 1238 } 1239 1240 #ifdef PIPE_SOCKETPAIR 1241 /* ARGSUSED */ 1242 int 1243 pipe1(struct lwp *l, register_t *retval, int flags) 1244 { 1245 file_t *rf, *wf; 1246 struct socket *rso, *wso; 1247 int fd, error; 1248 proc_t *p; 1249 1250 if (flags & ~(O_CLOEXEC|O_NONBLOCK|O_NOSIGPIPE)) 1251 return EINVAL; 1252 p = curproc; 1253 if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, l, NULL)) != 0) 1254 return (error); 1255 if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, l, rso)) != 0) 1256 goto free1; 1257 /* remember this socket pair implements a pipe */ 1258 wso->so_state |= SS_ISAPIPE; 1259 rso->so_state |= SS_ISAPIPE; 1260 if ((error = fd_allocfile(&rf, &fd)) != 0) 1261 goto free2; 1262 retval[0] = fd; 1263 rf->f_flag = FREAD | flags; 1264 rf->f_type = DTYPE_SOCKET; 1265 rf->f_ops = &socketops; 1266 rf->f_data = rso; 1267 if ((error = fd_allocfile(&wf, &fd)) != 0) 1268 goto free3; 1269 wf->f_flag = FWRITE | flags; 1270 wf->f_type = DTYPE_SOCKET; 1271 wf->f_ops = &socketops; 1272 wf->f_data = wso; 1273 retval[1] = fd; 1274 solock(wso); 1275 error = unp_connect2(wso, rso, PRU_CONNECT2); 1276 sounlock(wso); 1277 if (error != 0) 1278 goto free4; 1279 fd_affix(p, wf, (int)retval[1]); 1280 fd_affix(p, rf, (int)retval[0]); 1281 return (0); 1282 free4: 1283 fd_abort(p, wf, (int)retval[1]); 1284 free3: 1285 fd_abort(p, rf, (int)retval[0]); 1286 free2: 1287 (void)soclose(wso); 1288 free1: 1289 (void)soclose(rso); 1290 return (error); 1291 } 1292 #endif /* PIPE_SOCKETPAIR */ 1293 1294 /* 1295 * Get socket name. 1296 */ 1297 int 1298 do_sys_getsockname(struct lwp *l, int fd, int which, struct mbuf **nam) 1299 { 1300 struct socket *so; 1301 struct mbuf *m; 1302 int error; 1303 1304 if ((error = fd_getsock(fd, &so)) != 0) 1305 return error; 1306 1307 m = m_getclr(M_WAIT, MT_SONAME); 1308 MCLAIM(m, so->so_mowner); 1309 1310 solock(so); 1311 if (which == PRU_PEERADDR && (so->so_state & SS_ISCONNECTED) == 0) { 1312 error = ENOTCONN; 1313 } else { 1314 *nam = m; 1315 error = (*so->so_proto->pr_usrreq)(so, which, NULL, m, NULL, 1316 NULL); 1317 } 1318 sounlock(so); 1319 if (error != 0) 1320 m_free(m); 1321 fd_putfile(fd); 1322 return error; 1323 } 1324 1325 int 1326 copyout_sockname(struct sockaddr *asa, unsigned int *alen, int flags, 1327 struct mbuf *addr) 1328 { 1329 int len; 1330 int error; 1331 1332 if (asa == NULL) 1333 /* Assume application not interested */ 1334 return 0; 1335 1336 if (flags & MSG_LENUSRSPACE) { 1337 error = copyin(alen, &len, sizeof(len)); 1338 if (error) 1339 return error; 1340 } else 1341 len = *alen; 1342 if (len < 0) 1343 return EINVAL; 1344 1345 if (addr == NULL) { 1346 len = 0; 1347 error = 0; 1348 } else { 1349 if (len > addr->m_len) 1350 len = addr->m_len; 1351 /* Maybe this ought to copy a chain ? */ 1352 ktrkuser(mbuftypes[MT_SONAME], mtod(addr, void *), len); 1353 error = copyout(mtod(addr, void *), asa, len); 1354 } 1355 1356 if (error == 0) { 1357 if (flags & MSG_LENUSRSPACE) 1358 error = copyout(&len, alen, sizeof(len)); 1359 else 1360 *alen = len; 1361 } 1362 1363 return error; 1364 } 1365 1366 /* 1367 * Get socket name. 1368 */ 1369 /* ARGSUSED */ 1370 int 1371 sys_getsockname(struct lwp *l, const struct sys_getsockname_args *uap, register_t *retval) 1372 { 1373 /* { 1374 syscallarg(int) fdes; 1375 syscallarg(struct sockaddr *) asa; 1376 syscallarg(unsigned int *) alen; 1377 } */ 1378 struct mbuf *m; 1379 int error; 1380 1381 error = do_sys_getsockname(l, SCARG(uap, fdes), PRU_SOCKADDR, &m); 1382 if (error != 0) 1383 return error; 1384 1385 error = copyout_sockname(SCARG(uap, asa), SCARG(uap, alen), 1386 MSG_LENUSRSPACE, m); 1387 if (m != NULL) 1388 m_free(m); 1389 return error; 1390 } 1391 1392 /* 1393 * Get name of peer for connected socket. 1394 */ 1395 /* ARGSUSED */ 1396 int 1397 sys_getpeername(struct lwp *l, const struct sys_getpeername_args *uap, register_t *retval) 1398 { 1399 /* { 1400 syscallarg(int) fdes; 1401 syscallarg(struct sockaddr *) asa; 1402 syscallarg(unsigned int *) alen; 1403 } */ 1404 struct mbuf *m; 1405 int error; 1406 1407 error = do_sys_getsockname(l, SCARG(uap, fdes), PRU_PEERADDR, &m); 1408 if (error != 0) 1409 return error; 1410 1411 error = copyout_sockname(SCARG(uap, asa), SCARG(uap, alen), 1412 MSG_LENUSRSPACE, m); 1413 if (m != NULL) 1414 m_free(m); 1415 return error; 1416 } 1417 1418 /* 1419 * XXX In a perfect world, we wouldn't pass around socket control 1420 * XXX arguments in mbufs, and this could go away. 1421 */ 1422 int 1423 sockargs(struct mbuf **mp, const void *bf, size_t buflen, int type) 1424 { 1425 struct sockaddr *sa; 1426 struct mbuf *m; 1427 int error; 1428 1429 /* 1430 * We can't allow socket names > UCHAR_MAX in length, since that 1431 * will overflow sa_len. Control data more than a page size in 1432 * length is just too much. 1433 */ 1434 if (buflen > (type == MT_SONAME ? UCHAR_MAX : PAGE_SIZE)) 1435 return (EINVAL); 1436 1437 /* Allocate an mbuf to hold the arguments. */ 1438 m = m_get(M_WAIT, type); 1439 /* can't claim. don't who to assign it to. */ 1440 if (buflen > MLEN) { 1441 /* 1442 * Won't fit into a regular mbuf, so we allocate just 1443 * enough external storage to hold the argument. 1444 */ 1445 MEXTMALLOC(m, buflen, M_WAITOK); 1446 } 1447 m->m_len = buflen; 1448 error = copyin(bf, mtod(m, void *), buflen); 1449 if (error) { 1450 (void) m_free(m); 1451 return (error); 1452 } 1453 ktrkuser(mbuftypes[type], mtod(m, void *), buflen); 1454 *mp = m; 1455 if (type == MT_SONAME) { 1456 sa = mtod(m, struct sockaddr *); 1457 #if BYTE_ORDER != BIG_ENDIAN 1458 /* 1459 * 4.3BSD compat thing - need to stay, since bind(2), 1460 * connect(2), sendto(2) were not versioned for COMPAT_43. 1461 */ 1462 if (sa->sa_family == 0 && sa->sa_len < AF_MAX) 1463 sa->sa_family = sa->sa_len; 1464 #endif 1465 sa->sa_len = buflen; 1466 } 1467 return (0); 1468 } 1469