1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1986, 1989, 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include "opt_capsicum.h" 38 #include "opt_inet.h" 39 #include "opt_inet6.h" 40 #include "opt_ktrace.h" 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/capsicum.h> 45 #include <sys/kernel.h> 46 #include <sys/lock.h> 47 #include <sys/mutex.h> 48 #include <sys/sysproto.h> 49 #include <sys/malloc.h> 50 #include <sys/filedesc.h> 51 #include <sys/proc.h> 52 #include <sys/filio.h> 53 #include <sys/jail.h> 54 #include <sys/mbuf.h> 55 #include <sys/protosw.h> 56 #include <sys/rwlock.h> 57 #include <sys/socket.h> 58 #include <sys/socketvar.h> 59 #include <sys/syscallsubr.h> 60 #include <sys/uio.h> 61 #include <sys/un.h> 62 #include <sys/unpcb.h> 63 #ifdef KTRACE 64 #include <sys/ktrace.h> 65 #endif 66 #ifdef COMPAT_FREEBSD32 67 #include <compat/freebsd32/freebsd32_util.h> 68 #endif 69 70 #include <net/vnet.h> 71 72 #include <security/audit/audit.h> 73 #include <security/mac/mac_framework.h> 74 75 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags); 76 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp); 77 78 static int accept1(struct thread *td, int s, struct sockaddr *uname, 79 socklen_t *anamelen, int flags); 80 static int getsockname1(struct thread *td, struct getsockname_args *uap, 81 int compat); 82 static int getpeername1(struct thread *td, struct getpeername_args *uap, 83 int compat); 84 static int sockargs(struct mbuf **, char *, socklen_t, int); 85 86 /* 87 * Convert a user file descriptor to a kernel file entry and check if required 88 * capability rights are present. 89 * If required copy of current set of capability rights is returned. 90 * A reference on the file entry is held upon returning. 91 */ 92 int 93 getsock_cap(struct thread *td, int fd, cap_rights_t *rightsp, 94 struct file **fpp, u_int *fflagp, struct filecaps *havecapsp) 95 { 96 struct file *fp; 97 int error; 98 99 error = fget_cap(td, fd, rightsp, &fp, havecapsp); 100 if (error != 0) 101 return (error); 102 if (fp->f_type != DTYPE_SOCKET) { 103 fdrop(fp, td); 104 if (havecapsp != NULL) 105 filecaps_free(havecapsp); 106 return (ENOTSOCK); 107 } 108 if (fflagp != NULL) 109 *fflagp = fp->f_flag; 110 *fpp = fp; 111 return (0); 112 } 113 114 /* 115 * System call interface to the socket abstraction. 116 */ 117 #if defined(COMPAT_43) 118 #define COMPAT_OLDSOCK 119 #endif 120 121 int 122 sys_socket(struct thread *td, struct socket_args *uap) 123 { 124 125 return (kern_socket(td, uap->domain, uap->type, uap->protocol)); 126 } 127 128 int 129 kern_socket(struct thread *td, int domain, int type, int protocol) 130 { 131 struct socket *so; 132 struct file *fp; 133 int fd, error, oflag, fflag; 134 135 AUDIT_ARG_SOCKET(domain, type, protocol); 136 137 oflag = 0; 138 fflag = 0; 139 if ((type & SOCK_CLOEXEC) != 0) { 140 type &= ~SOCK_CLOEXEC; 141 oflag |= O_CLOEXEC; 142 } 143 if ((type & SOCK_NONBLOCK) != 0) { 144 type &= ~SOCK_NONBLOCK; 145 fflag |= FNONBLOCK; 146 } 147 148 #ifdef MAC 149 error = mac_socket_check_create(td->td_ucred, domain, type, protocol); 150 if (error != 0) 151 return (error); 152 #endif 153 error = falloc(td, &fp, &fd, oflag); 154 if (error != 0) 155 return (error); 156 /* An extra reference on `fp' has been held for us by falloc(). */ 157 error = socreate(domain, &so, type, protocol, td->td_ucred, td); 158 if (error != 0) { 159 fdclose(td, fp, fd); 160 } else { 161 finit(fp, FREAD | FWRITE | fflag, DTYPE_SOCKET, so, &socketops); 162 if ((fflag & FNONBLOCK) != 0) 163 (void) fo_ioctl(fp, FIONBIO, &fflag, td->td_ucred, td); 164 td->td_retval[0] = fd; 165 } 166 fdrop(fp, td); 167 return (error); 168 } 169 170 int 171 sys_bind(struct thread *td, struct bind_args *uap) 172 { 173 struct sockaddr *sa; 174 int error; 175 176 error = getsockaddr(&sa, uap->name, uap->namelen); 177 if (error == 0) { 178 error = kern_bindat(td, AT_FDCWD, uap->s, sa); 179 free(sa, M_SONAME); 180 } 181 return (error); 182 } 183 184 int 185 kern_bindat(struct thread *td, int dirfd, int fd, struct sockaddr *sa) 186 { 187 struct socket *so; 188 struct file *fp; 189 int error; 190 191 #ifdef CAPABILITY_MODE 192 if (IN_CAPABILITY_MODE(td) && (dirfd == AT_FDCWD)) 193 return (ECAPMODE); 194 #endif 195 196 AUDIT_ARG_FD(fd); 197 AUDIT_ARG_SOCKADDR(td, dirfd, sa); 198 error = getsock_cap(td, fd, &cap_bind_rights, 199 &fp, NULL, NULL); 200 if (error != 0) 201 return (error); 202 so = fp->f_data; 203 #ifdef KTRACE 204 if (KTRPOINT(td, KTR_STRUCT)) 205 ktrsockaddr(sa); 206 #endif 207 #ifdef MAC 208 error = mac_socket_check_bind(td->td_ucred, so, sa); 209 if (error == 0) { 210 #endif 211 if (dirfd == AT_FDCWD) 212 error = sobind(so, sa, td); 213 else 214 error = sobindat(dirfd, so, sa, td); 215 #ifdef MAC 216 } 217 #endif 218 fdrop(fp, td); 219 return (error); 220 } 221 222 int 223 sys_bindat(struct thread *td, struct bindat_args *uap) 224 { 225 struct sockaddr *sa; 226 int error; 227 228 error = getsockaddr(&sa, uap->name, uap->namelen); 229 if (error == 0) { 230 error = kern_bindat(td, uap->fd, uap->s, sa); 231 free(sa, M_SONAME); 232 } 233 return (error); 234 } 235 236 int 237 sys_listen(struct thread *td, struct listen_args *uap) 238 { 239 240 return (kern_listen(td, uap->s, uap->backlog)); 241 } 242 243 int 244 kern_listen(struct thread *td, int s, int backlog) 245 { 246 struct socket *so; 247 struct file *fp; 248 int error; 249 250 AUDIT_ARG_FD(s); 251 error = getsock_cap(td, s, &cap_listen_rights, 252 &fp, NULL, NULL); 253 if (error == 0) { 254 so = fp->f_data; 255 #ifdef MAC 256 error = mac_socket_check_listen(td->td_ucred, so); 257 if (error == 0) 258 #endif 259 error = solisten(so, backlog, td); 260 fdrop(fp, td); 261 } 262 return (error); 263 } 264 265 /* 266 * accept1() 267 */ 268 static int 269 accept1(td, s, uname, anamelen, flags) 270 struct thread *td; 271 int s; 272 struct sockaddr *uname; 273 socklen_t *anamelen; 274 int flags; 275 { 276 struct sockaddr *name; 277 socklen_t namelen; 278 struct file *fp; 279 int error; 280 281 if (uname == NULL) 282 return (kern_accept4(td, s, NULL, NULL, flags, NULL)); 283 284 error = copyin(anamelen, &namelen, sizeof (namelen)); 285 if (error != 0) 286 return (error); 287 288 error = kern_accept4(td, s, &name, &namelen, flags, &fp); 289 290 if (error != 0) 291 return (error); 292 293 if (error == 0 && uname != NULL) { 294 #ifdef COMPAT_OLDSOCK 295 if (flags & ACCEPT4_COMPAT) 296 ((struct osockaddr *)name)->sa_family = 297 name->sa_family; 298 #endif 299 error = copyout(name, uname, namelen); 300 } 301 if (error == 0) 302 error = copyout(&namelen, anamelen, 303 sizeof(namelen)); 304 if (error != 0) 305 fdclose(td, fp, td->td_retval[0]); 306 fdrop(fp, td); 307 free(name, M_SONAME); 308 return (error); 309 } 310 311 int 312 kern_accept(struct thread *td, int s, struct sockaddr **name, 313 socklen_t *namelen, struct file **fp) 314 { 315 return (kern_accept4(td, s, name, namelen, ACCEPT4_INHERIT, fp)); 316 } 317 318 int 319 kern_accept4(struct thread *td, int s, struct sockaddr **name, 320 socklen_t *namelen, int flags, struct file **fp) 321 { 322 struct file *headfp, *nfp = NULL; 323 struct sockaddr *sa = NULL; 324 struct socket *head, *so; 325 struct filecaps fcaps; 326 u_int fflag; 327 pid_t pgid; 328 int error, fd, tmp; 329 330 if (name != NULL) 331 *name = NULL; 332 333 AUDIT_ARG_FD(s); 334 error = getsock_cap(td, s, &cap_accept_rights, 335 &headfp, &fflag, &fcaps); 336 if (error != 0) 337 return (error); 338 head = headfp->f_data; 339 if ((head->so_options & SO_ACCEPTCONN) == 0) { 340 error = EINVAL; 341 goto done; 342 } 343 #ifdef MAC 344 error = mac_socket_check_accept(td->td_ucred, head); 345 if (error != 0) 346 goto done; 347 #endif 348 error = falloc_caps(td, &nfp, &fd, 349 (flags & SOCK_CLOEXEC) ? O_CLOEXEC : 0, &fcaps); 350 if (error != 0) 351 goto done; 352 SOCK_LOCK(head); 353 if (!SOLISTENING(head)) { 354 SOCK_UNLOCK(head); 355 error = EINVAL; 356 goto noconnection; 357 } 358 359 error = solisten_dequeue(head, &so, flags); 360 if (error != 0) 361 goto noconnection; 362 363 /* An extra reference on `nfp' has been held for us by falloc(). */ 364 td->td_retval[0] = fd; 365 366 /* Connection has been removed from the listen queue. */ 367 KNOTE_UNLOCKED(&head->so_rdsel.si_note, 0); 368 369 if (flags & ACCEPT4_INHERIT) { 370 pgid = fgetown(&head->so_sigio); 371 if (pgid != 0) 372 fsetown(pgid, &so->so_sigio); 373 } else { 374 fflag &= ~(FNONBLOCK | FASYNC); 375 if (flags & SOCK_NONBLOCK) 376 fflag |= FNONBLOCK; 377 } 378 379 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops); 380 /* Sync socket nonblocking/async state with file flags */ 381 tmp = fflag & FNONBLOCK; 382 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td); 383 tmp = fflag & FASYNC; 384 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td); 385 error = soaccept(so, &sa); 386 if (error != 0) 387 goto noconnection; 388 if (sa == NULL) { 389 if (name) 390 *namelen = 0; 391 goto done; 392 } 393 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, sa); 394 if (name) { 395 /* check sa_len before it is destroyed */ 396 if (*namelen > sa->sa_len) 397 *namelen = sa->sa_len; 398 #ifdef KTRACE 399 if (KTRPOINT(td, KTR_STRUCT)) 400 ktrsockaddr(sa); 401 #endif 402 *name = sa; 403 sa = NULL; 404 } 405 noconnection: 406 free(sa, M_SONAME); 407 408 /* 409 * close the new descriptor, assuming someone hasn't ripped it 410 * out from under us. 411 */ 412 if (error != 0) 413 fdclose(td, nfp, fd); 414 415 /* 416 * Release explicitly held references before returning. We return 417 * a reference on nfp to the caller on success if they request it. 418 */ 419 done: 420 if (nfp == NULL) 421 filecaps_free(&fcaps); 422 if (fp != NULL) { 423 if (error == 0) { 424 *fp = nfp; 425 nfp = NULL; 426 } else 427 *fp = NULL; 428 } 429 if (nfp != NULL) 430 fdrop(nfp, td); 431 fdrop(headfp, td); 432 return (error); 433 } 434 435 int 436 sys_accept(td, uap) 437 struct thread *td; 438 struct accept_args *uap; 439 { 440 441 return (accept1(td, uap->s, uap->name, uap->anamelen, ACCEPT4_INHERIT)); 442 } 443 444 int 445 sys_accept4(td, uap) 446 struct thread *td; 447 struct accept4_args *uap; 448 { 449 450 if (uap->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) 451 return (EINVAL); 452 453 return (accept1(td, uap->s, uap->name, uap->anamelen, uap->flags)); 454 } 455 456 #ifdef COMPAT_OLDSOCK 457 int 458 oaccept(td, uap) 459 struct thread *td; 460 struct accept_args *uap; 461 { 462 463 return (accept1(td, uap->s, uap->name, uap->anamelen, 464 ACCEPT4_INHERIT | ACCEPT4_COMPAT)); 465 } 466 #endif /* COMPAT_OLDSOCK */ 467 468 int 469 sys_connect(struct thread *td, struct connect_args *uap) 470 { 471 struct sockaddr *sa; 472 int error; 473 474 error = getsockaddr(&sa, uap->name, uap->namelen); 475 if (error == 0) { 476 error = kern_connectat(td, AT_FDCWD, uap->s, sa); 477 free(sa, M_SONAME); 478 } 479 return (error); 480 } 481 482 int 483 kern_connectat(struct thread *td, int dirfd, int fd, struct sockaddr *sa) 484 { 485 struct socket *so; 486 struct file *fp; 487 int error, interrupted = 0; 488 489 #ifdef CAPABILITY_MODE 490 if (IN_CAPABILITY_MODE(td) && (dirfd == AT_FDCWD)) 491 return (ECAPMODE); 492 #endif 493 494 AUDIT_ARG_FD(fd); 495 AUDIT_ARG_SOCKADDR(td, dirfd, sa); 496 error = getsock_cap(td, fd, &cap_connect_rights, 497 &fp, NULL, NULL); 498 if (error != 0) 499 return (error); 500 so = fp->f_data; 501 if (so->so_state & SS_ISCONNECTING) { 502 error = EALREADY; 503 goto done1; 504 } 505 #ifdef KTRACE 506 if (KTRPOINT(td, KTR_STRUCT)) 507 ktrsockaddr(sa); 508 #endif 509 #ifdef MAC 510 error = mac_socket_check_connect(td->td_ucred, so, sa); 511 if (error != 0) 512 goto bad; 513 #endif 514 if (dirfd == AT_FDCWD) 515 error = soconnect(so, sa, td); 516 else 517 error = soconnectat(dirfd, so, sa, td); 518 if (error != 0) 519 goto bad; 520 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) { 521 error = EINPROGRESS; 522 goto done1; 523 } 524 SOCK_LOCK(so); 525 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) { 526 error = msleep(&so->so_timeo, &so->so_lock, PSOCK | PCATCH, 527 "connec", 0); 528 if (error != 0) { 529 if (error == EINTR || error == ERESTART) 530 interrupted = 1; 531 break; 532 } 533 } 534 if (error == 0) { 535 error = so->so_error; 536 so->so_error = 0; 537 } 538 SOCK_UNLOCK(so); 539 bad: 540 if (!interrupted) 541 so->so_state &= ~SS_ISCONNECTING; 542 if (error == ERESTART) 543 error = EINTR; 544 done1: 545 fdrop(fp, td); 546 return (error); 547 } 548 549 int 550 sys_connectat(struct thread *td, struct connectat_args *uap) 551 { 552 struct sockaddr *sa; 553 int error; 554 555 error = getsockaddr(&sa, uap->name, uap->namelen); 556 if (error == 0) { 557 error = kern_connectat(td, uap->fd, uap->s, sa); 558 free(sa, M_SONAME); 559 } 560 return (error); 561 } 562 563 int 564 kern_socketpair(struct thread *td, int domain, int type, int protocol, 565 int *rsv) 566 { 567 struct file *fp1, *fp2; 568 struct socket *so1, *so2; 569 int fd, error, oflag, fflag; 570 571 AUDIT_ARG_SOCKET(domain, type, protocol); 572 573 oflag = 0; 574 fflag = 0; 575 if ((type & SOCK_CLOEXEC) != 0) { 576 type &= ~SOCK_CLOEXEC; 577 oflag |= O_CLOEXEC; 578 } 579 if ((type & SOCK_NONBLOCK) != 0) { 580 type &= ~SOCK_NONBLOCK; 581 fflag |= FNONBLOCK; 582 } 583 #ifdef MAC 584 /* We might want to have a separate check for socket pairs. */ 585 error = mac_socket_check_create(td->td_ucred, domain, type, 586 protocol); 587 if (error != 0) 588 return (error); 589 #endif 590 error = socreate(domain, &so1, type, protocol, td->td_ucred, td); 591 if (error != 0) 592 return (error); 593 error = socreate(domain, &so2, type, protocol, td->td_ucred, td); 594 if (error != 0) 595 goto free1; 596 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */ 597 error = falloc(td, &fp1, &fd, oflag); 598 if (error != 0) 599 goto free2; 600 rsv[0] = fd; 601 fp1->f_data = so1; /* so1 already has ref count */ 602 error = falloc(td, &fp2, &fd, oflag); 603 if (error != 0) 604 goto free3; 605 fp2->f_data = so2; /* so2 already has ref count */ 606 rsv[1] = fd; 607 error = soconnect2(so1, so2); 608 if (error != 0) 609 goto free4; 610 if (type == SOCK_DGRAM) { 611 /* 612 * Datagram socket connection is asymmetric. 613 */ 614 error = soconnect2(so2, so1); 615 if (error != 0) 616 goto free4; 617 } else if (so1->so_proto->pr_flags & PR_CONNREQUIRED) { 618 struct unpcb *unp, *unp2; 619 unp = sotounpcb(so1); 620 unp2 = sotounpcb(so2); 621 /* 622 * No need to lock the unps, because the sockets are brand-new. 623 * No other threads can be using them yet 624 */ 625 unp_copy_peercred(td, unp, unp2, unp); 626 } 627 finit(fp1, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp1->f_data, 628 &socketops); 629 finit(fp2, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp2->f_data, 630 &socketops); 631 if ((fflag & FNONBLOCK) != 0) { 632 (void) fo_ioctl(fp1, FIONBIO, &fflag, td->td_ucred, td); 633 (void) fo_ioctl(fp2, FIONBIO, &fflag, td->td_ucred, td); 634 } 635 fdrop(fp1, td); 636 fdrop(fp2, td); 637 return (0); 638 free4: 639 fdclose(td, fp2, rsv[1]); 640 fdrop(fp2, td); 641 free3: 642 fdclose(td, fp1, rsv[0]); 643 fdrop(fp1, td); 644 free2: 645 if (so2 != NULL) 646 (void)soclose(so2); 647 free1: 648 if (so1 != NULL) 649 (void)soclose(so1); 650 return (error); 651 } 652 653 int 654 sys_socketpair(struct thread *td, struct socketpair_args *uap) 655 { 656 int error, sv[2]; 657 658 error = kern_socketpair(td, uap->domain, uap->type, 659 uap->protocol, sv); 660 if (error != 0) 661 return (error); 662 error = copyout(sv, uap->rsv, 2 * sizeof(int)); 663 if (error != 0) { 664 (void)kern_close(td, sv[0]); 665 (void)kern_close(td, sv[1]); 666 } 667 return (error); 668 } 669 670 static int 671 sendit(struct thread *td, int s, struct msghdr *mp, int flags) 672 { 673 struct mbuf *control; 674 struct sockaddr *to; 675 int error; 676 677 #ifdef CAPABILITY_MODE 678 if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL)) 679 return (ECAPMODE); 680 #endif 681 682 if (mp->msg_name != NULL) { 683 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen); 684 if (error != 0) { 685 to = NULL; 686 goto bad; 687 } 688 mp->msg_name = to; 689 } else { 690 to = NULL; 691 } 692 693 if (mp->msg_control) { 694 if (mp->msg_controllen < sizeof(struct cmsghdr) 695 #ifdef COMPAT_OLDSOCK 696 && mp->msg_flags != MSG_COMPAT 697 #endif 698 ) { 699 error = EINVAL; 700 goto bad; 701 } 702 error = sockargs(&control, mp->msg_control, 703 mp->msg_controllen, MT_CONTROL); 704 if (error != 0) 705 goto bad; 706 #ifdef COMPAT_OLDSOCK 707 if (mp->msg_flags == MSG_COMPAT) { 708 struct cmsghdr *cm; 709 710 M_PREPEND(control, sizeof(*cm), M_WAITOK); 711 cm = mtod(control, struct cmsghdr *); 712 cm->cmsg_len = control->m_len; 713 cm->cmsg_level = SOL_SOCKET; 714 cm->cmsg_type = SCM_RIGHTS; 715 } 716 #endif 717 } else { 718 control = NULL; 719 } 720 721 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE); 722 723 bad: 724 free(to, M_SONAME); 725 return (error); 726 } 727 728 int 729 kern_sendit(struct thread *td, int s, struct msghdr *mp, int flags, 730 struct mbuf *control, enum uio_seg segflg) 731 { 732 struct file *fp; 733 struct uio auio; 734 struct iovec *iov; 735 struct socket *so; 736 cap_rights_t *rights; 737 #ifdef KTRACE 738 struct uio *ktruio = NULL; 739 #endif 740 ssize_t len; 741 int i, error; 742 743 AUDIT_ARG_FD(s); 744 rights = &cap_send_rights; 745 if (mp->msg_name != NULL) { 746 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, mp->msg_name); 747 rights = &cap_send_connect_rights; 748 } 749 error = getsock_cap(td, s, rights, &fp, NULL, NULL); 750 if (error != 0) { 751 m_freem(control); 752 return (error); 753 } 754 so = (struct socket *)fp->f_data; 755 756 #ifdef KTRACE 757 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT)) 758 ktrsockaddr(mp->msg_name); 759 #endif 760 #ifdef MAC 761 if (mp->msg_name != NULL) { 762 error = mac_socket_check_connect(td->td_ucred, so, 763 mp->msg_name); 764 if (error != 0) { 765 m_freem(control); 766 goto bad; 767 } 768 } 769 error = mac_socket_check_send(td->td_ucred, so); 770 if (error != 0) { 771 m_freem(control); 772 goto bad; 773 } 774 #endif 775 776 auio.uio_iov = mp->msg_iov; 777 auio.uio_iovcnt = mp->msg_iovlen; 778 auio.uio_segflg = segflg; 779 auio.uio_rw = UIO_WRITE; 780 auio.uio_td = td; 781 auio.uio_offset = 0; /* XXX */ 782 auio.uio_resid = 0; 783 iov = mp->msg_iov; 784 for (i = 0; i < mp->msg_iovlen; i++, iov++) { 785 if ((auio.uio_resid += iov->iov_len) < 0) { 786 error = EINVAL; 787 m_freem(control); 788 goto bad; 789 } 790 } 791 #ifdef KTRACE 792 if (KTRPOINT(td, KTR_GENIO)) 793 ktruio = cloneuio(&auio); 794 #endif 795 len = auio.uio_resid; 796 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td); 797 if (error != 0) { 798 if (auio.uio_resid != len && (error == ERESTART || 799 error == EINTR || error == EWOULDBLOCK)) 800 error = 0; 801 /* Generation of SIGPIPE can be controlled per socket */ 802 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) && 803 !(flags & MSG_NOSIGNAL)) { 804 PROC_LOCK(td->td_proc); 805 tdsignal(td, SIGPIPE); 806 PROC_UNLOCK(td->td_proc); 807 } 808 } 809 if (error == 0) 810 td->td_retval[0] = len - auio.uio_resid; 811 #ifdef KTRACE 812 if (ktruio != NULL) { 813 ktruio->uio_resid = td->td_retval[0]; 814 ktrgenio(s, UIO_WRITE, ktruio, error); 815 } 816 #endif 817 bad: 818 fdrop(fp, td); 819 return (error); 820 } 821 822 int 823 sys_sendto(struct thread *td, struct sendto_args *uap) 824 { 825 struct msghdr msg; 826 struct iovec aiov; 827 828 msg.msg_name = uap->to; 829 msg.msg_namelen = uap->tolen; 830 msg.msg_iov = &aiov; 831 msg.msg_iovlen = 1; 832 msg.msg_control = 0; 833 #ifdef COMPAT_OLDSOCK 834 msg.msg_flags = 0; 835 #endif 836 aiov.iov_base = uap->buf; 837 aiov.iov_len = uap->len; 838 return (sendit(td, uap->s, &msg, uap->flags)); 839 } 840 841 #ifdef COMPAT_OLDSOCK 842 int 843 osend(struct thread *td, struct osend_args *uap) 844 { 845 struct msghdr msg; 846 struct iovec aiov; 847 848 msg.msg_name = 0; 849 msg.msg_namelen = 0; 850 msg.msg_iov = &aiov; 851 msg.msg_iovlen = 1; 852 aiov.iov_base = uap->buf; 853 aiov.iov_len = uap->len; 854 msg.msg_control = 0; 855 msg.msg_flags = 0; 856 return (sendit(td, uap->s, &msg, uap->flags)); 857 } 858 859 int 860 osendmsg(struct thread *td, struct osendmsg_args *uap) 861 { 862 struct msghdr msg; 863 struct iovec *iov; 864 int error; 865 866 error = copyin(uap->msg, &msg, sizeof (struct omsghdr)); 867 if (error != 0) 868 return (error); 869 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE); 870 if (error != 0) 871 return (error); 872 msg.msg_iov = iov; 873 msg.msg_flags = MSG_COMPAT; 874 error = sendit(td, uap->s, &msg, uap->flags); 875 free(iov, M_IOV); 876 return (error); 877 } 878 #endif 879 880 int 881 sys_sendmsg(struct thread *td, struct sendmsg_args *uap) 882 { 883 struct msghdr msg; 884 struct iovec *iov; 885 int error; 886 887 error = copyin(uap->msg, &msg, sizeof (msg)); 888 if (error != 0) 889 return (error); 890 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE); 891 if (error != 0) 892 return (error); 893 msg.msg_iov = iov; 894 #ifdef COMPAT_OLDSOCK 895 msg.msg_flags = 0; 896 #endif 897 error = sendit(td, uap->s, &msg, uap->flags); 898 free(iov, M_IOV); 899 return (error); 900 } 901 902 int 903 kern_recvit(struct thread *td, int s, struct msghdr *mp, enum uio_seg fromseg, 904 struct mbuf **controlp) 905 { 906 struct uio auio; 907 struct iovec *iov; 908 struct mbuf *m, *control = NULL; 909 caddr_t ctlbuf; 910 struct file *fp; 911 struct socket *so; 912 struct sockaddr *fromsa = NULL; 913 #ifdef KTRACE 914 struct uio *ktruio = NULL; 915 #endif 916 ssize_t len; 917 int error, i; 918 919 if (controlp != NULL) 920 *controlp = NULL; 921 922 AUDIT_ARG_FD(s); 923 error = getsock_cap(td, s, &cap_recv_rights, 924 &fp, NULL, NULL); 925 if (error != 0) 926 return (error); 927 so = fp->f_data; 928 929 #ifdef MAC 930 error = mac_socket_check_receive(td->td_ucred, so); 931 if (error != 0) { 932 fdrop(fp, td); 933 return (error); 934 } 935 #endif 936 937 auio.uio_iov = mp->msg_iov; 938 auio.uio_iovcnt = mp->msg_iovlen; 939 auio.uio_segflg = UIO_USERSPACE; 940 auio.uio_rw = UIO_READ; 941 auio.uio_td = td; 942 auio.uio_offset = 0; /* XXX */ 943 auio.uio_resid = 0; 944 iov = mp->msg_iov; 945 for (i = 0; i < mp->msg_iovlen; i++, iov++) { 946 if ((auio.uio_resid += iov->iov_len) < 0) { 947 fdrop(fp, td); 948 return (EINVAL); 949 } 950 } 951 #ifdef KTRACE 952 if (KTRPOINT(td, KTR_GENIO)) 953 ktruio = cloneuio(&auio); 954 #endif 955 len = auio.uio_resid; 956 error = soreceive(so, &fromsa, &auio, NULL, 957 (mp->msg_control || controlp) ? &control : NULL, 958 &mp->msg_flags); 959 if (error != 0) { 960 if (auio.uio_resid != len && (error == ERESTART || 961 error == EINTR || error == EWOULDBLOCK)) 962 error = 0; 963 } 964 if (fromsa != NULL) 965 AUDIT_ARG_SOCKADDR(td, AT_FDCWD, fromsa); 966 #ifdef KTRACE 967 if (ktruio != NULL) { 968 ktruio->uio_resid = len - auio.uio_resid; 969 ktrgenio(s, UIO_READ, ktruio, error); 970 } 971 #endif 972 if (error != 0) 973 goto out; 974 td->td_retval[0] = len - auio.uio_resid; 975 if (mp->msg_name) { 976 len = mp->msg_namelen; 977 if (len <= 0 || fromsa == NULL) 978 len = 0; 979 else { 980 /* save sa_len before it is destroyed by MSG_COMPAT */ 981 len = MIN(len, fromsa->sa_len); 982 #ifdef COMPAT_OLDSOCK 983 if (mp->msg_flags & MSG_COMPAT) 984 ((struct osockaddr *)fromsa)->sa_family = 985 fromsa->sa_family; 986 #endif 987 if (fromseg == UIO_USERSPACE) { 988 error = copyout(fromsa, mp->msg_name, 989 (unsigned)len); 990 if (error != 0) 991 goto out; 992 } else 993 bcopy(fromsa, mp->msg_name, len); 994 } 995 mp->msg_namelen = len; 996 } 997 if (mp->msg_control && controlp == NULL) { 998 #ifdef COMPAT_OLDSOCK 999 /* 1000 * We assume that old recvmsg calls won't receive access 1001 * rights and other control info, esp. as control info 1002 * is always optional and those options didn't exist in 4.3. 1003 * If we receive rights, trim the cmsghdr; anything else 1004 * is tossed. 1005 */ 1006 if (control && mp->msg_flags & MSG_COMPAT) { 1007 if (mtod(control, struct cmsghdr *)->cmsg_level != 1008 SOL_SOCKET || 1009 mtod(control, struct cmsghdr *)->cmsg_type != 1010 SCM_RIGHTS) { 1011 mp->msg_controllen = 0; 1012 goto out; 1013 } 1014 control->m_len -= sizeof (struct cmsghdr); 1015 control->m_data += sizeof (struct cmsghdr); 1016 } 1017 #endif 1018 len = mp->msg_controllen; 1019 m = control; 1020 mp->msg_controllen = 0; 1021 ctlbuf = mp->msg_control; 1022 1023 while (m && len > 0) { 1024 unsigned int tocopy; 1025 1026 if (len >= m->m_len) 1027 tocopy = m->m_len; 1028 else { 1029 mp->msg_flags |= MSG_CTRUNC; 1030 tocopy = len; 1031 } 1032 1033 if ((error = copyout(mtod(m, caddr_t), 1034 ctlbuf, tocopy)) != 0) 1035 goto out; 1036 1037 ctlbuf += tocopy; 1038 len -= tocopy; 1039 m = m->m_next; 1040 } 1041 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control; 1042 } 1043 out: 1044 fdrop(fp, td); 1045 #ifdef KTRACE 1046 if (fromsa && KTRPOINT(td, KTR_STRUCT)) 1047 ktrsockaddr(fromsa); 1048 #endif 1049 free(fromsa, M_SONAME); 1050 1051 if (error == 0 && controlp != NULL) 1052 *controlp = control; 1053 else if (control) 1054 m_freem(control); 1055 1056 return (error); 1057 } 1058 1059 static int 1060 recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp) 1061 { 1062 int error; 1063 1064 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL); 1065 if (error != 0) 1066 return (error); 1067 if (namelenp != NULL) { 1068 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t)); 1069 #ifdef COMPAT_OLDSOCK 1070 if (mp->msg_flags & MSG_COMPAT) 1071 error = 0; /* old recvfrom didn't check */ 1072 #endif 1073 } 1074 return (error); 1075 } 1076 1077 int 1078 sys_recvfrom(struct thread *td, struct recvfrom_args *uap) 1079 { 1080 struct msghdr msg; 1081 struct iovec aiov; 1082 int error; 1083 1084 if (uap->fromlenaddr) { 1085 error = copyin(uap->fromlenaddr, 1086 &msg.msg_namelen, sizeof (msg.msg_namelen)); 1087 if (error != 0) 1088 goto done2; 1089 } else { 1090 msg.msg_namelen = 0; 1091 } 1092 msg.msg_name = uap->from; 1093 msg.msg_iov = &aiov; 1094 msg.msg_iovlen = 1; 1095 aiov.iov_base = uap->buf; 1096 aiov.iov_len = uap->len; 1097 msg.msg_control = 0; 1098 msg.msg_flags = uap->flags; 1099 error = recvit(td, uap->s, &msg, uap->fromlenaddr); 1100 done2: 1101 return (error); 1102 } 1103 1104 #ifdef COMPAT_OLDSOCK 1105 int 1106 orecvfrom(struct thread *td, struct recvfrom_args *uap) 1107 { 1108 1109 uap->flags |= MSG_COMPAT; 1110 return (sys_recvfrom(td, uap)); 1111 } 1112 #endif 1113 1114 #ifdef COMPAT_OLDSOCK 1115 int 1116 orecv(struct thread *td, struct orecv_args *uap) 1117 { 1118 struct msghdr msg; 1119 struct iovec aiov; 1120 1121 msg.msg_name = 0; 1122 msg.msg_namelen = 0; 1123 msg.msg_iov = &aiov; 1124 msg.msg_iovlen = 1; 1125 aiov.iov_base = uap->buf; 1126 aiov.iov_len = uap->len; 1127 msg.msg_control = 0; 1128 msg.msg_flags = uap->flags; 1129 return (recvit(td, uap->s, &msg, NULL)); 1130 } 1131 1132 /* 1133 * Old recvmsg. This code takes advantage of the fact that the old msghdr 1134 * overlays the new one, missing only the flags, and with the (old) access 1135 * rights where the control fields are now. 1136 */ 1137 int 1138 orecvmsg(struct thread *td, struct orecvmsg_args *uap) 1139 { 1140 struct msghdr msg; 1141 struct iovec *iov; 1142 int error; 1143 1144 error = copyin(uap->msg, &msg, sizeof (struct omsghdr)); 1145 if (error != 0) 1146 return (error); 1147 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE); 1148 if (error != 0) 1149 return (error); 1150 msg.msg_flags = uap->flags | MSG_COMPAT; 1151 msg.msg_iov = iov; 1152 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen); 1153 if (msg.msg_controllen && error == 0) 1154 error = copyout(&msg.msg_controllen, 1155 &uap->msg->msg_accrightslen, sizeof (int)); 1156 free(iov, M_IOV); 1157 return (error); 1158 } 1159 #endif 1160 1161 int 1162 sys_recvmsg(struct thread *td, struct recvmsg_args *uap) 1163 { 1164 struct msghdr msg; 1165 struct iovec *uiov, *iov; 1166 int error; 1167 1168 error = copyin(uap->msg, &msg, sizeof (msg)); 1169 if (error != 0) 1170 return (error); 1171 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE); 1172 if (error != 0) 1173 return (error); 1174 msg.msg_flags = uap->flags; 1175 #ifdef COMPAT_OLDSOCK 1176 msg.msg_flags &= ~MSG_COMPAT; 1177 #endif 1178 uiov = msg.msg_iov; 1179 msg.msg_iov = iov; 1180 error = recvit(td, uap->s, &msg, NULL); 1181 if (error == 0) { 1182 msg.msg_iov = uiov; 1183 error = copyout(&msg, uap->msg, sizeof(msg)); 1184 } 1185 free(iov, M_IOV); 1186 return (error); 1187 } 1188 1189 int 1190 sys_shutdown(struct thread *td, struct shutdown_args *uap) 1191 { 1192 1193 return (kern_shutdown(td, uap->s, uap->how)); 1194 } 1195 1196 int 1197 kern_shutdown(struct thread *td, int s, int how) 1198 { 1199 struct socket *so; 1200 struct file *fp; 1201 int error; 1202 1203 AUDIT_ARG_FD(s); 1204 error = getsock_cap(td, s, &cap_shutdown_rights, 1205 &fp, NULL, NULL); 1206 if (error == 0) { 1207 so = fp->f_data; 1208 error = soshutdown(so, how); 1209 /* 1210 * Previous versions did not return ENOTCONN, but 0 in 1211 * case the socket was not connected. Some important 1212 * programs like syslogd up to r279016, 2015-02-19, 1213 * still depend on this behavior. 1214 */ 1215 if (error == ENOTCONN && 1216 td->td_proc->p_osrel < P_OSREL_SHUTDOWN_ENOTCONN) 1217 error = 0; 1218 fdrop(fp, td); 1219 } 1220 return (error); 1221 } 1222 1223 int 1224 sys_setsockopt(struct thread *td, struct setsockopt_args *uap) 1225 { 1226 1227 return (kern_setsockopt(td, uap->s, uap->level, uap->name, 1228 uap->val, UIO_USERSPACE, uap->valsize)); 1229 } 1230 1231 int 1232 kern_setsockopt(struct thread *td, int s, int level, int name, void *val, 1233 enum uio_seg valseg, socklen_t valsize) 1234 { 1235 struct socket *so; 1236 struct file *fp; 1237 struct sockopt sopt; 1238 int error; 1239 1240 if (val == NULL && valsize != 0) 1241 return (EFAULT); 1242 if ((int)valsize < 0) 1243 return (EINVAL); 1244 1245 sopt.sopt_dir = SOPT_SET; 1246 sopt.sopt_level = level; 1247 sopt.sopt_name = name; 1248 sopt.sopt_val = val; 1249 sopt.sopt_valsize = valsize; 1250 switch (valseg) { 1251 case UIO_USERSPACE: 1252 sopt.sopt_td = td; 1253 break; 1254 case UIO_SYSSPACE: 1255 sopt.sopt_td = NULL; 1256 break; 1257 default: 1258 panic("kern_setsockopt called with bad valseg"); 1259 } 1260 1261 AUDIT_ARG_FD(s); 1262 error = getsock_cap(td, s, &cap_setsockopt_rights, 1263 &fp, NULL, NULL); 1264 if (error == 0) { 1265 so = fp->f_data; 1266 error = sosetopt(so, &sopt); 1267 fdrop(fp, td); 1268 } 1269 return(error); 1270 } 1271 1272 int 1273 sys_getsockopt(struct thread *td, struct getsockopt_args *uap) 1274 { 1275 socklen_t valsize; 1276 int error; 1277 1278 if (uap->val) { 1279 error = copyin(uap->avalsize, &valsize, sizeof (valsize)); 1280 if (error != 0) 1281 return (error); 1282 } 1283 1284 error = kern_getsockopt(td, uap->s, uap->level, uap->name, 1285 uap->val, UIO_USERSPACE, &valsize); 1286 1287 if (error == 0) 1288 error = copyout(&valsize, uap->avalsize, sizeof (valsize)); 1289 return (error); 1290 } 1291 1292 /* 1293 * Kernel version of getsockopt. 1294 * optval can be a userland or userspace. optlen is always a kernel pointer. 1295 */ 1296 int 1297 kern_getsockopt(struct thread *td, int s, int level, int name, void *val, 1298 enum uio_seg valseg, socklen_t *valsize) 1299 { 1300 struct socket *so; 1301 struct file *fp; 1302 struct sockopt sopt; 1303 int error; 1304 1305 if (val == NULL) 1306 *valsize = 0; 1307 if ((int)*valsize < 0) 1308 return (EINVAL); 1309 1310 sopt.sopt_dir = SOPT_GET; 1311 sopt.sopt_level = level; 1312 sopt.sopt_name = name; 1313 sopt.sopt_val = val; 1314 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */ 1315 switch (valseg) { 1316 case UIO_USERSPACE: 1317 sopt.sopt_td = td; 1318 break; 1319 case UIO_SYSSPACE: 1320 sopt.sopt_td = NULL; 1321 break; 1322 default: 1323 panic("kern_getsockopt called with bad valseg"); 1324 } 1325 1326 AUDIT_ARG_FD(s); 1327 error = getsock_cap(td, s, &cap_getsockopt_rights, 1328 &fp, NULL, NULL); 1329 if (error == 0) { 1330 so = fp->f_data; 1331 error = sogetopt(so, &sopt); 1332 *valsize = sopt.sopt_valsize; 1333 fdrop(fp, td); 1334 } 1335 return (error); 1336 } 1337 1338 /* 1339 * getsockname1() - Get socket name. 1340 */ 1341 static int 1342 getsockname1(struct thread *td, struct getsockname_args *uap, int compat) 1343 { 1344 struct sockaddr *sa; 1345 socklen_t len; 1346 int error; 1347 1348 error = copyin(uap->alen, &len, sizeof(len)); 1349 if (error != 0) 1350 return (error); 1351 1352 error = kern_getsockname(td, uap->fdes, &sa, &len); 1353 if (error != 0) 1354 return (error); 1355 1356 if (len != 0) { 1357 #ifdef COMPAT_OLDSOCK 1358 if (compat) 1359 ((struct osockaddr *)sa)->sa_family = sa->sa_family; 1360 #endif 1361 error = copyout(sa, uap->asa, (u_int)len); 1362 } 1363 free(sa, M_SONAME); 1364 if (error == 0) 1365 error = copyout(&len, uap->alen, sizeof(len)); 1366 return (error); 1367 } 1368 1369 int 1370 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa, 1371 socklen_t *alen) 1372 { 1373 struct socket *so; 1374 struct file *fp; 1375 socklen_t len; 1376 int error; 1377 1378 AUDIT_ARG_FD(fd); 1379 error = getsock_cap(td, fd, &cap_getsockname_rights, 1380 &fp, NULL, NULL); 1381 if (error != 0) 1382 return (error); 1383 so = fp->f_data; 1384 *sa = NULL; 1385 CURVNET_SET(so->so_vnet); 1386 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa); 1387 CURVNET_RESTORE(); 1388 if (error != 0) 1389 goto bad; 1390 if (*sa == NULL) 1391 len = 0; 1392 else 1393 len = MIN(*alen, (*sa)->sa_len); 1394 *alen = len; 1395 #ifdef KTRACE 1396 if (KTRPOINT(td, KTR_STRUCT)) 1397 ktrsockaddr(*sa); 1398 #endif 1399 bad: 1400 fdrop(fp, td); 1401 if (error != 0 && *sa != NULL) { 1402 free(*sa, M_SONAME); 1403 *sa = NULL; 1404 } 1405 return (error); 1406 } 1407 1408 int 1409 sys_getsockname(struct thread *td, struct getsockname_args *uap) 1410 { 1411 1412 return (getsockname1(td, uap, 0)); 1413 } 1414 1415 #ifdef COMPAT_OLDSOCK 1416 int 1417 ogetsockname(struct thread *td, struct getsockname_args *uap) 1418 { 1419 1420 return (getsockname1(td, uap, 1)); 1421 } 1422 #endif /* COMPAT_OLDSOCK */ 1423 1424 /* 1425 * getpeername1() - Get name of peer for connected socket. 1426 */ 1427 static int 1428 getpeername1(struct thread *td, struct getpeername_args *uap, int compat) 1429 { 1430 struct sockaddr *sa; 1431 socklen_t len; 1432 int error; 1433 1434 error = copyin(uap->alen, &len, sizeof (len)); 1435 if (error != 0) 1436 return (error); 1437 1438 error = kern_getpeername(td, uap->fdes, &sa, &len); 1439 if (error != 0) 1440 return (error); 1441 1442 if (len != 0) { 1443 #ifdef COMPAT_OLDSOCK 1444 if (compat) 1445 ((struct osockaddr *)sa)->sa_family = sa->sa_family; 1446 #endif 1447 error = copyout(sa, uap->asa, (u_int)len); 1448 } 1449 free(sa, M_SONAME); 1450 if (error == 0) 1451 error = copyout(&len, uap->alen, sizeof(len)); 1452 return (error); 1453 } 1454 1455 int 1456 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa, 1457 socklen_t *alen) 1458 { 1459 struct socket *so; 1460 struct file *fp; 1461 socklen_t len; 1462 int error; 1463 1464 AUDIT_ARG_FD(fd); 1465 error = getsock_cap(td, fd, &cap_getpeername_rights, 1466 &fp, NULL, NULL); 1467 if (error != 0) 1468 return (error); 1469 so = fp->f_data; 1470 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) { 1471 error = ENOTCONN; 1472 goto done; 1473 } 1474 *sa = NULL; 1475 CURVNET_SET(so->so_vnet); 1476 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa); 1477 CURVNET_RESTORE(); 1478 if (error != 0) 1479 goto bad; 1480 if (*sa == NULL) 1481 len = 0; 1482 else 1483 len = MIN(*alen, (*sa)->sa_len); 1484 *alen = len; 1485 #ifdef KTRACE 1486 if (KTRPOINT(td, KTR_STRUCT)) 1487 ktrsockaddr(*sa); 1488 #endif 1489 bad: 1490 if (error != 0 && *sa != NULL) { 1491 free(*sa, M_SONAME); 1492 *sa = NULL; 1493 } 1494 done: 1495 fdrop(fp, td); 1496 return (error); 1497 } 1498 1499 int 1500 sys_getpeername(struct thread *td, struct getpeername_args *uap) 1501 { 1502 1503 return (getpeername1(td, uap, 0)); 1504 } 1505 1506 #ifdef COMPAT_OLDSOCK 1507 int 1508 ogetpeername(struct thread *td, struct ogetpeername_args *uap) 1509 { 1510 1511 /* XXX uap should have type `getpeername_args *' to begin with. */ 1512 return (getpeername1(td, (struct getpeername_args *)uap, 1)); 1513 } 1514 #endif /* COMPAT_OLDSOCK */ 1515 1516 static int 1517 sockargs(struct mbuf **mp, char *buf, socklen_t buflen, int type) 1518 { 1519 struct sockaddr *sa; 1520 struct mbuf *m; 1521 int error; 1522 1523 if (buflen > MLEN) { 1524 #ifdef COMPAT_OLDSOCK 1525 if (type == MT_SONAME && buflen <= 112) 1526 buflen = MLEN; /* unix domain compat. hack */ 1527 else 1528 #endif 1529 if (buflen > MCLBYTES) 1530 return (EINVAL); 1531 } 1532 m = m_get2(buflen, M_WAITOK, type, 0); 1533 m->m_len = buflen; 1534 error = copyin(buf, mtod(m, void *), buflen); 1535 if (error != 0) 1536 (void) m_free(m); 1537 else { 1538 *mp = m; 1539 if (type == MT_SONAME) { 1540 sa = mtod(m, struct sockaddr *); 1541 1542 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN 1543 if (sa->sa_family == 0 && sa->sa_len < AF_MAX) 1544 sa->sa_family = sa->sa_len; 1545 #endif 1546 sa->sa_len = buflen; 1547 } 1548 } 1549 return (error); 1550 } 1551 1552 int 1553 getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len) 1554 { 1555 struct sockaddr *sa; 1556 int error; 1557 1558 if (len > SOCK_MAXADDRLEN) 1559 return (ENAMETOOLONG); 1560 if (len < offsetof(struct sockaddr, sa_data[0])) 1561 return (EINVAL); 1562 sa = malloc(len, M_SONAME, M_WAITOK); 1563 error = copyin(uaddr, sa, len); 1564 if (error != 0) { 1565 free(sa, M_SONAME); 1566 } else { 1567 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN 1568 if (sa->sa_family == 0 && sa->sa_len < AF_MAX) 1569 sa->sa_family = sa->sa_len; 1570 #endif 1571 sa->sa_len = len; 1572 *namp = sa; 1573 } 1574 return (error); 1575 } 1576