1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 1995 Søren Schmidt 5 * 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 /* XXX we use functions that might not exist. */ 33 #include "opt_compat.h" 34 #include "opt_inet6.h" 35 36 #include <sys/param.h> 37 #include <sys/proc.h> 38 #include <sys/systm.h> 39 #include <sys/sysproto.h> 40 #include <sys/capsicum.h> 41 #include <sys/fcntl.h> 42 #include <sys/file.h> 43 #include <sys/limits.h> 44 #include <sys/lock.h> 45 #include <sys/malloc.h> 46 #include <sys/mutex.h> 47 #include <sys/mbuf.h> 48 #include <sys/socket.h> 49 #include <sys/socketvar.h> 50 #include <sys/syscallsubr.h> 51 #include <sys/uio.h> 52 #include <sys/stat.h> 53 #include <sys/syslog.h> 54 #include <sys/un.h> 55 #include <sys/unistd.h> 56 57 #include <security/audit/audit.h> 58 59 #include <net/if.h> 60 #include <net/vnet.h> 61 #include <netinet/in.h> 62 #include <netinet/in_systm.h> 63 #include <netinet/ip.h> 64 #include <netinet/tcp.h> 65 #ifdef INET6 66 #include <netinet/ip6.h> 67 #include <netinet6/ip6_var.h> 68 #endif 69 70 #ifdef COMPAT_LINUX32 71 #include <machine/../linux32/linux.h> 72 #include <machine/../linux32/linux32_proto.h> 73 #else 74 #include <machine/../linux/linux.h> 75 #include <machine/../linux/linux_proto.h> 76 #endif 77 #include <compat/linux/linux_common.h> 78 #include <compat/linux/linux_file.h> 79 #include <compat/linux/linux_mib.h> 80 #include <compat/linux/linux_socket.h> 81 #include <compat/linux/linux_timer.h> 82 #include <compat/linux/linux_util.h> 83 84 static int linux_sendmsg_common(struct thread *, l_int, struct l_msghdr *, 85 l_uint); 86 static int linux_recvmsg_common(struct thread *, l_int, struct l_msghdr *, 87 l_uint, struct msghdr *); 88 static int linux_set_socket_flags(int, int *); 89 90 91 static int 92 linux_to_bsd_sockopt_level(int level) 93 { 94 95 if (level == LINUX_SOL_SOCKET) 96 return (SOL_SOCKET); 97 /* Remaining values are RFC-defined protocol numbers. */ 98 return (level); 99 } 100 101 static int 102 bsd_to_linux_sockopt_level(int level) 103 { 104 105 if (level == SOL_SOCKET) 106 return (LINUX_SOL_SOCKET); 107 return (level); 108 } 109 110 static int 111 linux_to_bsd_ip_sockopt(int opt) 112 { 113 114 switch (opt) { 115 case LINUX_IP_TOS: 116 return (IP_TOS); 117 case LINUX_IP_TTL: 118 return (IP_TTL); 119 case LINUX_IP_OPTIONS: 120 return (IP_OPTIONS); 121 case LINUX_IP_MULTICAST_IF: 122 return (IP_MULTICAST_IF); 123 case LINUX_IP_MULTICAST_TTL: 124 return (IP_MULTICAST_TTL); 125 case LINUX_IP_MULTICAST_LOOP: 126 return (IP_MULTICAST_LOOP); 127 case LINUX_IP_ADD_MEMBERSHIP: 128 return (IP_ADD_MEMBERSHIP); 129 case LINUX_IP_DROP_MEMBERSHIP: 130 return (IP_DROP_MEMBERSHIP); 131 case LINUX_IP_HDRINCL: 132 return (IP_HDRINCL); 133 } 134 return (-1); 135 } 136 137 static int 138 linux_to_bsd_ip6_sockopt(int opt) 139 { 140 141 switch (opt) { 142 case LINUX_IPV6_NEXTHOP: 143 return (IPV6_NEXTHOP); 144 case LINUX_IPV6_UNICAST_HOPS: 145 return (IPV6_UNICAST_HOPS); 146 case LINUX_IPV6_MULTICAST_IF: 147 return (IPV6_MULTICAST_IF); 148 case LINUX_IPV6_MULTICAST_HOPS: 149 return (IPV6_MULTICAST_HOPS); 150 case LINUX_IPV6_MULTICAST_LOOP: 151 return (IPV6_MULTICAST_LOOP); 152 case LINUX_IPV6_ADD_MEMBERSHIP: 153 return (IPV6_JOIN_GROUP); 154 case LINUX_IPV6_DROP_MEMBERSHIP: 155 return (IPV6_LEAVE_GROUP); 156 case LINUX_IPV6_V6ONLY: 157 return (IPV6_V6ONLY); 158 case LINUX_IPV6_DONTFRAG: 159 return (IPV6_DONTFRAG); 160 #if 0 161 case LINUX_IPV6_CHECKSUM: 162 return (IPV6_CHECKSUM); 163 case LINUX_IPV6_RECVPKTINFO: 164 return (IPV6_RECVPKTINFO); 165 case LINUX_IPV6_PKTINFO: 166 return (IPV6_PKTINFO); 167 case LINUX_IPV6_RECVHOPLIMIT: 168 return (IPV6_RECVHOPLIMIT); 169 case LINUX_IPV6_HOPLIMIT: 170 return (IPV6_HOPLIMIT); 171 case LINUX_IPV6_RECVHOPOPTS: 172 return (IPV6_RECVHOPOPTS); 173 case LINUX_IPV6_HOPOPTS: 174 return (IPV6_HOPOPTS); 175 case LINUX_IPV6_RTHDRDSTOPTS: 176 return (IPV6_RTHDRDSTOPTS); 177 case LINUX_IPV6_RECVRTHDR: 178 return (IPV6_RECVRTHDR); 179 case LINUX_IPV6_RTHDR: 180 return (IPV6_RTHDR); 181 case LINUX_IPV6_RECVDSTOPTS: 182 return (IPV6_RECVDSTOPTS); 183 case LINUX_IPV6_DSTOPTS: 184 return (IPV6_DSTOPTS); 185 case LINUX_IPV6_RECVPATHMTU: 186 return (IPV6_RECVPATHMTU); 187 case LINUX_IPV6_PATHMTU: 188 return (IPV6_PATHMTU); 189 #endif 190 } 191 return (-1); 192 } 193 194 static int 195 linux_to_bsd_so_sockopt(int opt) 196 { 197 198 switch (opt) { 199 case LINUX_SO_DEBUG: 200 return (SO_DEBUG); 201 case LINUX_SO_REUSEADDR: 202 return (SO_REUSEADDR); 203 case LINUX_SO_TYPE: 204 return (SO_TYPE); 205 case LINUX_SO_ERROR: 206 return (SO_ERROR); 207 case LINUX_SO_DONTROUTE: 208 return (SO_DONTROUTE); 209 case LINUX_SO_BROADCAST: 210 return (SO_BROADCAST); 211 case LINUX_SO_SNDBUF: 212 case LINUX_SO_SNDBUFFORCE: 213 return (SO_SNDBUF); 214 case LINUX_SO_RCVBUF: 215 case LINUX_SO_RCVBUFFORCE: 216 return (SO_RCVBUF); 217 case LINUX_SO_KEEPALIVE: 218 return (SO_KEEPALIVE); 219 case LINUX_SO_OOBINLINE: 220 return (SO_OOBINLINE); 221 case LINUX_SO_LINGER: 222 return (SO_LINGER); 223 case LINUX_SO_REUSEPORT: 224 return (SO_REUSEPORT_LB); 225 case LINUX_SO_PEERCRED: 226 return (LOCAL_PEERCRED); 227 case LINUX_SO_RCVLOWAT: 228 return (SO_RCVLOWAT); 229 case LINUX_SO_SNDLOWAT: 230 return (SO_SNDLOWAT); 231 case LINUX_SO_RCVTIMEO: 232 return (SO_RCVTIMEO); 233 case LINUX_SO_SNDTIMEO: 234 return (SO_SNDTIMEO); 235 case LINUX_SO_TIMESTAMP: 236 return (SO_TIMESTAMP); 237 case LINUX_SO_ACCEPTCONN: 238 return (SO_ACCEPTCONN); 239 } 240 return (-1); 241 } 242 243 static int 244 linux_to_bsd_tcp_sockopt(int opt) 245 { 246 247 switch (opt) { 248 case LINUX_TCP_NODELAY: 249 return (TCP_NODELAY); 250 case LINUX_TCP_MAXSEG: 251 return (TCP_MAXSEG); 252 case LINUX_TCP_CORK: 253 return (TCP_NOPUSH); 254 case LINUX_TCP_KEEPIDLE: 255 return (TCP_KEEPIDLE); 256 case LINUX_TCP_KEEPINTVL: 257 return (TCP_KEEPINTVL); 258 case LINUX_TCP_KEEPCNT: 259 return (TCP_KEEPCNT); 260 case LINUX_TCP_MD5SIG: 261 return (TCP_MD5SIG); 262 } 263 return (-1); 264 } 265 266 static int 267 linux_to_bsd_msg_flags(int flags) 268 { 269 int ret_flags = 0; 270 271 if (flags & LINUX_MSG_OOB) 272 ret_flags |= MSG_OOB; 273 if (flags & LINUX_MSG_PEEK) 274 ret_flags |= MSG_PEEK; 275 if (flags & LINUX_MSG_DONTROUTE) 276 ret_flags |= MSG_DONTROUTE; 277 if (flags & LINUX_MSG_CTRUNC) 278 ret_flags |= MSG_CTRUNC; 279 if (flags & LINUX_MSG_TRUNC) 280 ret_flags |= MSG_TRUNC; 281 if (flags & LINUX_MSG_DONTWAIT) 282 ret_flags |= MSG_DONTWAIT; 283 if (flags & LINUX_MSG_EOR) 284 ret_flags |= MSG_EOR; 285 if (flags & LINUX_MSG_WAITALL) 286 ret_flags |= MSG_WAITALL; 287 if (flags & LINUX_MSG_NOSIGNAL) 288 ret_flags |= MSG_NOSIGNAL; 289 #if 0 /* not handled */ 290 if (flags & LINUX_MSG_PROXY) 291 ; 292 if (flags & LINUX_MSG_FIN) 293 ; 294 if (flags & LINUX_MSG_SYN) 295 ; 296 if (flags & LINUX_MSG_CONFIRM) 297 ; 298 if (flags & LINUX_MSG_RST) 299 ; 300 if (flags & LINUX_MSG_ERRQUEUE) 301 ; 302 #endif 303 return (ret_flags); 304 } 305 306 static int 307 linux_to_bsd_cmsg_type(int cmsg_type) 308 { 309 310 switch (cmsg_type) { 311 case LINUX_SCM_RIGHTS: 312 return (SCM_RIGHTS); 313 case LINUX_SCM_CREDENTIALS: 314 return (SCM_CREDS); 315 } 316 return (-1); 317 } 318 319 static int 320 bsd_to_linux_cmsg_type(int cmsg_type) 321 { 322 323 switch (cmsg_type) { 324 case SCM_RIGHTS: 325 return (LINUX_SCM_RIGHTS); 326 case SCM_CREDS: 327 return (LINUX_SCM_CREDENTIALS); 328 case SCM_TIMESTAMP: 329 return (LINUX_SCM_TIMESTAMP); 330 } 331 return (-1); 332 } 333 334 static int 335 linux_to_bsd_msghdr(struct msghdr *bhdr, const struct l_msghdr *lhdr) 336 { 337 if (lhdr->msg_controllen > INT_MAX) 338 return (ENOBUFS); 339 340 bhdr->msg_name = PTRIN(lhdr->msg_name); 341 bhdr->msg_namelen = lhdr->msg_namelen; 342 bhdr->msg_iov = PTRIN(lhdr->msg_iov); 343 bhdr->msg_iovlen = lhdr->msg_iovlen; 344 bhdr->msg_control = PTRIN(lhdr->msg_control); 345 346 /* 347 * msg_controllen is skipped since BSD and LINUX control messages 348 * are potentially different sizes (e.g. the cred structure used 349 * by SCM_CREDS is different between the two operating system). 350 * 351 * The caller can set it (if necessary) after converting all the 352 * control messages. 353 */ 354 355 bhdr->msg_flags = linux_to_bsd_msg_flags(lhdr->msg_flags); 356 return (0); 357 } 358 359 static int 360 bsd_to_linux_msghdr(const struct msghdr *bhdr, struct l_msghdr *lhdr) 361 { 362 lhdr->msg_name = PTROUT(bhdr->msg_name); 363 lhdr->msg_namelen = bhdr->msg_namelen; 364 lhdr->msg_iov = PTROUT(bhdr->msg_iov); 365 lhdr->msg_iovlen = bhdr->msg_iovlen; 366 lhdr->msg_control = PTROUT(bhdr->msg_control); 367 368 /* 369 * msg_controllen is skipped since BSD and LINUX control messages 370 * are potentially different sizes (e.g. the cred structure used 371 * by SCM_CREDS is different between the two operating system). 372 * 373 * The caller can set it (if necessary) after converting all the 374 * control messages. 375 */ 376 377 /* msg_flags skipped */ 378 return (0); 379 } 380 381 static int 382 linux_set_socket_flags(int lflags, int *flags) 383 { 384 385 if (lflags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK)) 386 return (EINVAL); 387 if (lflags & LINUX_SOCK_NONBLOCK) 388 *flags |= SOCK_NONBLOCK; 389 if (lflags & LINUX_SOCK_CLOEXEC) 390 *flags |= SOCK_CLOEXEC; 391 return (0); 392 } 393 394 static int 395 linux_sendit(struct thread *td, int s, struct msghdr *mp, int flags, 396 struct mbuf *control, enum uio_seg segflg) 397 { 398 struct sockaddr *to; 399 int error, len; 400 401 if (mp->msg_name != NULL) { 402 len = mp->msg_namelen; 403 error = linux_to_bsd_sockaddr(mp->msg_name, &to, &len); 404 if (error != 0) 405 return (error); 406 mp->msg_name = to; 407 } else 408 to = NULL; 409 410 error = kern_sendit(td, s, mp, linux_to_bsd_msg_flags(flags), control, 411 segflg); 412 413 if (to) 414 free(to, M_SONAME); 415 return (error); 416 } 417 418 /* Return 0 if IP_HDRINCL is set for the given socket. */ 419 static int 420 linux_check_hdrincl(struct thread *td, int s) 421 { 422 int error, optval; 423 socklen_t size_val; 424 425 size_val = sizeof(optval); 426 error = kern_getsockopt(td, s, IPPROTO_IP, IP_HDRINCL, 427 &optval, UIO_SYSSPACE, &size_val); 428 if (error != 0) 429 return (error); 430 431 return (optval == 0); 432 } 433 434 /* 435 * Updated sendto() when IP_HDRINCL is set: 436 * tweak endian-dependent fields in the IP packet. 437 */ 438 static int 439 linux_sendto_hdrincl(struct thread *td, struct linux_sendto_args *linux_args) 440 { 441 /* 442 * linux_ip_copysize defines how many bytes we should copy 443 * from the beginning of the IP packet before we customize it for BSD. 444 * It should include all the fields we modify (ip_len and ip_off). 445 */ 446 #define linux_ip_copysize 8 447 448 struct ip *packet; 449 struct msghdr msg; 450 struct iovec aiov[1]; 451 int error; 452 453 /* Check that the packet isn't too big or too small. */ 454 if (linux_args->len < linux_ip_copysize || 455 linux_args->len > IP_MAXPACKET) 456 return (EINVAL); 457 458 packet = (struct ip *)malloc(linux_args->len, M_LINUX, M_WAITOK); 459 460 /* Make kernel copy of the packet to be sent */ 461 if ((error = copyin(PTRIN(linux_args->msg), packet, 462 linux_args->len))) 463 goto goout; 464 465 /* Convert fields from Linux to BSD raw IP socket format */ 466 packet->ip_len = linux_args->len; 467 packet->ip_off = ntohs(packet->ip_off); 468 469 /* Prepare the msghdr and iovec structures describing the new packet */ 470 msg.msg_name = PTRIN(linux_args->to); 471 msg.msg_namelen = linux_args->tolen; 472 msg.msg_iov = aiov; 473 msg.msg_iovlen = 1; 474 msg.msg_control = NULL; 475 msg.msg_flags = 0; 476 aiov[0].iov_base = (char *)packet; 477 aiov[0].iov_len = linux_args->len; 478 error = linux_sendit(td, linux_args->s, &msg, linux_args->flags, 479 NULL, UIO_SYSSPACE); 480 goout: 481 free(packet, M_LINUX); 482 return (error); 483 } 484 485 int 486 linux_socket(struct thread *td, struct linux_socket_args *args) 487 { 488 int domain, retval_socket, type; 489 490 type = args->type & LINUX_SOCK_TYPE_MASK; 491 if (type < 0 || type > LINUX_SOCK_MAX) 492 return (EINVAL); 493 retval_socket = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK, 494 &type); 495 if (retval_socket != 0) 496 return (retval_socket); 497 domain = linux_to_bsd_domain(args->domain); 498 if (domain == -1) 499 return (EAFNOSUPPORT); 500 501 retval_socket = kern_socket(td, domain, type, args->protocol); 502 if (retval_socket) 503 return (retval_socket); 504 505 if (type == SOCK_RAW 506 && (args->protocol == IPPROTO_RAW || args->protocol == 0) 507 && domain == PF_INET) { 508 /* It's a raw IP socket: set the IP_HDRINCL option. */ 509 int hdrincl; 510 511 hdrincl = 1; 512 /* We ignore any error returned by kern_setsockopt() */ 513 kern_setsockopt(td, td->td_retval[0], IPPROTO_IP, IP_HDRINCL, 514 &hdrincl, UIO_SYSSPACE, sizeof(hdrincl)); 515 } 516 #ifdef INET6 517 /* 518 * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by default 519 * and some apps depend on this. So, set V6ONLY to 0 for Linux apps. 520 * For simplicity we do this unconditionally of the net.inet6.ip6.v6only 521 * sysctl value. 522 */ 523 if (domain == PF_INET6) { 524 int v6only; 525 526 v6only = 0; 527 /* We ignore any error returned by setsockopt() */ 528 kern_setsockopt(td, td->td_retval[0], IPPROTO_IPV6, IPV6_V6ONLY, 529 &v6only, UIO_SYSSPACE, sizeof(v6only)); 530 } 531 #endif 532 533 return (retval_socket); 534 } 535 536 int 537 linux_bind(struct thread *td, struct linux_bind_args *args) 538 { 539 struct sockaddr *sa; 540 int error; 541 542 error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa, 543 &args->namelen); 544 if (error != 0) 545 return (error); 546 547 error = kern_bindat(td, AT_FDCWD, args->s, sa); 548 free(sa, M_SONAME); 549 550 /* XXX */ 551 if (error == EADDRNOTAVAIL && args->namelen != sizeof(struct sockaddr_in)) 552 return (EINVAL); 553 return (error); 554 } 555 556 int 557 linux_connect(struct thread *td, struct linux_connect_args *args) 558 { 559 struct socket *so; 560 struct sockaddr *sa; 561 struct file *fp; 562 u_int fflag; 563 int error; 564 565 error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa, 566 &args->namelen); 567 if (error != 0) 568 return (error); 569 570 error = kern_connectat(td, AT_FDCWD, args->s, sa); 571 free(sa, M_SONAME); 572 if (error != EISCONN) 573 return (error); 574 575 /* 576 * Linux doesn't return EISCONN the first time it occurs, 577 * when on a non-blocking socket. Instead it returns the 578 * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD. 579 */ 580 error = getsock_cap(td, args->s, &cap_connect_rights, 581 &fp, &fflag, NULL); 582 if (error != 0) 583 return (error); 584 585 error = EISCONN; 586 so = fp->f_data; 587 if (fflag & FNONBLOCK) { 588 SOCK_LOCK(so); 589 if (so->so_emuldata == 0) 590 error = so->so_error; 591 so->so_emuldata = (void *)1; 592 SOCK_UNLOCK(so); 593 } 594 fdrop(fp, td); 595 596 return (error); 597 } 598 599 int 600 linux_listen(struct thread *td, struct linux_listen_args *args) 601 { 602 603 return (kern_listen(td, args->s, args->backlog)); 604 } 605 606 static int 607 linux_accept_common(struct thread *td, int s, l_uintptr_t addr, 608 l_uintptr_t namelen, int flags) 609 { 610 struct l_sockaddr *lsa; 611 struct sockaddr *sa; 612 struct file *fp; 613 int bflags, len; 614 struct socket *so; 615 int error, error1; 616 617 bflags = 0; 618 error = linux_set_socket_flags(flags, &bflags); 619 if (error != 0) 620 return (error); 621 622 sa = NULL; 623 if (PTRIN(addr) == NULL) { 624 len = 0; 625 error = kern_accept4(td, s, NULL, NULL, bflags, NULL); 626 } else { 627 error = copyin(PTRIN(namelen), &len, sizeof(len)); 628 if (error != 0) 629 return (error); 630 if (len < 0) 631 return (EINVAL); 632 error = kern_accept4(td, s, &sa, &len, bflags, &fp); 633 if (error == 0) 634 fdrop(fp, td); 635 } 636 637 if (error != 0) { 638 /* 639 * XXX. This is wrong, different sockaddr structures 640 * have different sizes. 641 */ 642 if (error == EFAULT && namelen != sizeof(struct sockaddr_in)) 643 { 644 error = EINVAL; 645 goto out; 646 } 647 if (error == EINVAL) { 648 error1 = getsock_cap(td, s, &cap_accept_rights, &fp, NULL, NULL); 649 if (error1 != 0) { 650 error = error1; 651 goto out; 652 } 653 so = fp->f_data; 654 if (so->so_type == SOCK_DGRAM) 655 error = EOPNOTSUPP; 656 fdrop(fp, td); 657 } 658 goto out; 659 } 660 661 if (len != 0 && error == 0) { 662 error = bsd_to_linux_sockaddr(sa, &lsa, len); 663 if (error == 0) 664 error = copyout(lsa, PTRIN(addr), len); 665 free(lsa, M_SONAME); 666 } 667 668 free(sa, M_SONAME); 669 670 out: 671 if (error != 0) { 672 (void)kern_close(td, td->td_retval[0]); 673 td->td_retval[0] = 0; 674 } 675 return (error); 676 } 677 678 int 679 linux_accept(struct thread *td, struct linux_accept_args *args) 680 { 681 682 return (linux_accept_common(td, args->s, args->addr, 683 args->namelen, 0)); 684 } 685 686 int 687 linux_accept4(struct thread *td, struct linux_accept4_args *args) 688 { 689 690 return (linux_accept_common(td, args->s, args->addr, 691 args->namelen, args->flags)); 692 } 693 694 int 695 linux_getsockname(struct thread *td, struct linux_getsockname_args *args) 696 { 697 struct l_sockaddr *lsa; 698 struct sockaddr *sa; 699 int len, error; 700 701 error = copyin(PTRIN(args->namelen), &len, sizeof(len)); 702 if (error != 0) 703 return (error); 704 705 error = kern_getsockname(td, args->s, &sa, &len); 706 if (error != 0) 707 return (error); 708 709 if (len != 0) { 710 error = bsd_to_linux_sockaddr(sa, &lsa, len); 711 if (error == 0) 712 error = copyout(lsa, PTRIN(args->addr), 713 len); 714 free(lsa, M_SONAME); 715 } 716 717 free(sa, M_SONAME); 718 if (error == 0) 719 error = copyout(&len, PTRIN(args->namelen), sizeof(len)); 720 return (error); 721 } 722 723 int 724 linux_getpeername(struct thread *td, struct linux_getpeername_args *args) 725 { 726 struct l_sockaddr *lsa; 727 struct sockaddr *sa; 728 int len, error; 729 730 error = copyin(PTRIN(args->namelen), &len, sizeof(len)); 731 if (error != 0) 732 return (error); 733 if (len < 0) 734 return (EINVAL); 735 736 error = kern_getpeername(td, args->s, &sa, &len); 737 if (error != 0) 738 return (error); 739 740 if (len != 0) { 741 error = bsd_to_linux_sockaddr(sa, &lsa, len); 742 if (error == 0) 743 error = copyout(lsa, PTRIN(args->addr), 744 len); 745 free(lsa, M_SONAME); 746 } 747 748 free(sa, M_SONAME); 749 if (error == 0) 750 error = copyout(&len, PTRIN(args->namelen), sizeof(len)); 751 return (error); 752 } 753 754 int 755 linux_socketpair(struct thread *td, struct linux_socketpair_args *args) 756 { 757 int domain, error, sv[2], type; 758 759 domain = linux_to_bsd_domain(args->domain); 760 if (domain != PF_LOCAL) 761 return (EAFNOSUPPORT); 762 type = args->type & LINUX_SOCK_TYPE_MASK; 763 if (type < 0 || type > LINUX_SOCK_MAX) 764 return (EINVAL); 765 error = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK, 766 &type); 767 if (error != 0) 768 return (error); 769 if (args->protocol != 0 && args->protocol != PF_UNIX) { 770 771 /* 772 * Use of PF_UNIX as protocol argument is not right, 773 * but Linux does it. 774 * Do not map PF_UNIX as its Linux value is identical 775 * to FreeBSD one. 776 */ 777 return (EPROTONOSUPPORT); 778 } 779 error = kern_socketpair(td, domain, type, 0, sv); 780 if (error != 0) 781 return (error); 782 error = copyout(sv, PTRIN(args->rsv), 2 * sizeof(int)); 783 if (error != 0) { 784 (void)kern_close(td, sv[0]); 785 (void)kern_close(td, sv[1]); 786 } 787 return (error); 788 } 789 790 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 791 struct linux_send_args { 792 register_t s; 793 register_t msg; 794 register_t len; 795 register_t flags; 796 }; 797 798 static int 799 linux_send(struct thread *td, struct linux_send_args *args) 800 { 801 struct sendto_args /* { 802 int s; 803 caddr_t buf; 804 int len; 805 int flags; 806 caddr_t to; 807 int tolen; 808 } */ bsd_args; 809 struct file *fp; 810 int error, fflag; 811 812 bsd_args.s = args->s; 813 bsd_args.buf = (caddr_t)PTRIN(args->msg); 814 bsd_args.len = args->len; 815 bsd_args.flags = args->flags; 816 bsd_args.to = NULL; 817 bsd_args.tolen = 0; 818 error = sys_sendto(td, &bsd_args); 819 if (error == ENOTCONN) { 820 /* 821 * Linux doesn't return ENOTCONN for non-blocking sockets. 822 * Instead it returns the EAGAIN. 823 */ 824 error = getsock_cap(td, args->s, &cap_send_rights, &fp, 825 &fflag, NULL); 826 if (error == 0) { 827 if (fflag & FNONBLOCK) 828 error = EAGAIN; 829 fdrop(fp, td); 830 } 831 } 832 return (error); 833 } 834 835 struct linux_recv_args { 836 register_t s; 837 register_t msg; 838 register_t len; 839 register_t flags; 840 }; 841 842 static int 843 linux_recv(struct thread *td, struct linux_recv_args *args) 844 { 845 struct recvfrom_args /* { 846 int s; 847 caddr_t buf; 848 int len; 849 int flags; 850 struct sockaddr *from; 851 socklen_t fromlenaddr; 852 } */ bsd_args; 853 854 bsd_args.s = args->s; 855 bsd_args.buf = (caddr_t)PTRIN(args->msg); 856 bsd_args.len = args->len; 857 bsd_args.flags = linux_to_bsd_msg_flags(args->flags); 858 bsd_args.from = NULL; 859 bsd_args.fromlenaddr = 0; 860 return (sys_recvfrom(td, &bsd_args)); 861 } 862 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 863 864 int 865 linux_sendto(struct thread *td, struct linux_sendto_args *args) 866 { 867 struct msghdr msg; 868 struct iovec aiov; 869 870 if (linux_check_hdrincl(td, args->s) == 0) 871 /* IP_HDRINCL set, tweak the packet before sending */ 872 return (linux_sendto_hdrincl(td, args)); 873 874 msg.msg_name = PTRIN(args->to); 875 msg.msg_namelen = args->tolen; 876 msg.msg_iov = &aiov; 877 msg.msg_iovlen = 1; 878 msg.msg_control = NULL; 879 msg.msg_flags = 0; 880 aiov.iov_base = PTRIN(args->msg); 881 aiov.iov_len = args->len; 882 return (linux_sendit(td, args->s, &msg, args->flags, NULL, 883 UIO_USERSPACE)); 884 } 885 886 int 887 linux_recvfrom(struct thread *td, struct linux_recvfrom_args *args) 888 { 889 struct l_sockaddr *lsa; 890 struct sockaddr *sa; 891 struct msghdr msg; 892 struct iovec aiov; 893 int error, fromlen; 894 895 if (PTRIN(args->fromlen) != NULL) { 896 error = copyin(PTRIN(args->fromlen), &fromlen, 897 sizeof(fromlen)); 898 if (error != 0) 899 return (error); 900 if (fromlen < 0) 901 return (EINVAL); 902 sa = malloc(fromlen, M_SONAME, M_WAITOK); 903 } else { 904 fromlen = 0; 905 sa = NULL; 906 } 907 908 msg.msg_name = sa; 909 msg.msg_namelen = fromlen; 910 msg.msg_iov = &aiov; 911 msg.msg_iovlen = 1; 912 aiov.iov_base = PTRIN(args->buf); 913 aiov.iov_len = args->len; 914 msg.msg_control = 0; 915 msg.msg_flags = linux_to_bsd_msg_flags(args->flags); 916 917 error = kern_recvit(td, args->s, &msg, UIO_SYSSPACE, NULL); 918 if (error != 0) 919 goto out; 920 921 if (PTRIN(args->from) != NULL) { 922 error = bsd_to_linux_sockaddr(sa, &lsa, msg.msg_namelen); 923 if (error == 0) 924 error = copyout(lsa, PTRIN(args->from), 925 msg.msg_namelen); 926 free(lsa, M_SONAME); 927 } 928 929 if (error == 0 && PTRIN(args->fromlen) != NULL) 930 error = copyout(&msg.msg_namelen, PTRIN(args->fromlen), 931 sizeof(msg.msg_namelen)); 932 out: 933 free(sa, M_SONAME); 934 return (error); 935 } 936 937 static int 938 linux_sendmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr, 939 l_uint flags) 940 { 941 struct cmsghdr *cmsg; 942 struct mbuf *control; 943 struct msghdr msg; 944 struct l_cmsghdr linux_cmsg; 945 struct l_cmsghdr *ptr_cmsg; 946 struct l_msghdr linux_msghdr; 947 struct iovec *iov; 948 socklen_t datalen; 949 struct sockaddr *sa; 950 struct socket *so; 951 sa_family_t sa_family; 952 struct file *fp; 953 void *data; 954 l_size_t len; 955 l_size_t clen; 956 int error, fflag; 957 958 error = copyin(msghdr, &linux_msghdr, sizeof(linux_msghdr)); 959 if (error != 0) 960 return (error); 961 962 /* 963 * Some Linux applications (ping) define a non-NULL control data 964 * pointer, but a msg_controllen of 0, which is not allowed in the 965 * FreeBSD system call interface. NULL the msg_control pointer in 966 * order to handle this case. This should be checked, but allows the 967 * Linux ping to work. 968 */ 969 if (PTRIN(linux_msghdr.msg_control) != NULL && 970 linux_msghdr.msg_controllen == 0) 971 linux_msghdr.msg_control = PTROUT(NULL); 972 973 error = linux_to_bsd_msghdr(&msg, &linux_msghdr); 974 if (error != 0) 975 return (error); 976 977 #ifdef COMPAT_LINUX32 978 error = linux32_copyiniov(PTRIN(msg.msg_iov), msg.msg_iovlen, 979 &iov, EMSGSIZE); 980 #else 981 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE); 982 #endif 983 if (error != 0) 984 return (error); 985 986 control = NULL; 987 988 error = kern_getsockname(td, s, &sa, &datalen); 989 if (error != 0) 990 goto bad; 991 sa_family = sa->sa_family; 992 free(sa, M_SONAME); 993 994 if (flags & LINUX_MSG_OOB) { 995 error = EOPNOTSUPP; 996 if (sa_family == AF_UNIX) 997 goto bad; 998 999 error = getsock_cap(td, s, &cap_send_rights, &fp, 1000 &fflag, NULL); 1001 if (error != 0) 1002 goto bad; 1003 so = fp->f_data; 1004 if (so->so_type != SOCK_STREAM) 1005 error = EOPNOTSUPP; 1006 fdrop(fp, td); 1007 if (error != 0) 1008 goto bad; 1009 } 1010 1011 if (linux_msghdr.msg_controllen >= sizeof(struct l_cmsghdr)) { 1012 1013 error = ENOBUFS; 1014 control = m_get(M_WAITOK, MT_CONTROL); 1015 MCLGET(control, M_WAITOK); 1016 data = mtod(control, void *); 1017 datalen = 0; 1018 1019 ptr_cmsg = PTRIN(linux_msghdr.msg_control); 1020 clen = linux_msghdr.msg_controllen; 1021 do { 1022 error = copyin(ptr_cmsg, &linux_cmsg, 1023 sizeof(struct l_cmsghdr)); 1024 if (error != 0) 1025 goto bad; 1026 1027 error = EINVAL; 1028 if (linux_cmsg.cmsg_len < sizeof(struct l_cmsghdr) || 1029 linux_cmsg.cmsg_len > clen) 1030 goto bad; 1031 1032 if (datalen + CMSG_HDRSZ > MCLBYTES) 1033 goto bad; 1034 1035 /* 1036 * Now we support only SCM_RIGHTS and SCM_CRED, 1037 * so return EINVAL in any other cmsg_type 1038 */ 1039 cmsg = data; 1040 cmsg->cmsg_type = 1041 linux_to_bsd_cmsg_type(linux_cmsg.cmsg_type); 1042 cmsg->cmsg_level = 1043 linux_to_bsd_sockopt_level(linux_cmsg.cmsg_level); 1044 if (cmsg->cmsg_type == -1 1045 || cmsg->cmsg_level != SOL_SOCKET) { 1046 linux_msg(curthread, 1047 "unsupported sendmsg cmsg level %d type %d", 1048 linux_cmsg.cmsg_level, linux_cmsg.cmsg_type); 1049 goto bad; 1050 } 1051 1052 /* 1053 * Some applications (e.g. pulseaudio) attempt to 1054 * send ancillary data even if the underlying protocol 1055 * doesn't support it which is not allowed in the 1056 * FreeBSD system call interface. 1057 */ 1058 if (sa_family != AF_UNIX) 1059 continue; 1060 1061 if (cmsg->cmsg_type == SCM_CREDS) { 1062 len = sizeof(struct cmsgcred); 1063 if (datalen + CMSG_SPACE(len) > MCLBYTES) 1064 goto bad; 1065 1066 /* 1067 * The lower levels will fill in the structure 1068 */ 1069 memset(CMSG_DATA(data), 0, len); 1070 } else { 1071 len = linux_cmsg.cmsg_len - L_CMSG_HDRSZ; 1072 if (datalen + CMSG_SPACE(len) < datalen || 1073 datalen + CMSG_SPACE(len) > MCLBYTES) 1074 goto bad; 1075 1076 error = copyin(LINUX_CMSG_DATA(ptr_cmsg), 1077 CMSG_DATA(data), len); 1078 if (error != 0) 1079 goto bad; 1080 } 1081 1082 cmsg->cmsg_len = CMSG_LEN(len); 1083 data = (char *)data + CMSG_SPACE(len); 1084 datalen += CMSG_SPACE(len); 1085 1086 if (clen <= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len)) 1087 break; 1088 1089 clen -= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len); 1090 ptr_cmsg = (struct l_cmsghdr *)((char *)ptr_cmsg + 1091 LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len)); 1092 } while(clen >= sizeof(struct l_cmsghdr)); 1093 1094 control->m_len = datalen; 1095 if (datalen == 0) { 1096 m_freem(control); 1097 control = NULL; 1098 } 1099 } 1100 1101 msg.msg_iov = iov; 1102 msg.msg_flags = 0; 1103 error = linux_sendit(td, s, &msg, flags, control, UIO_USERSPACE); 1104 control = NULL; 1105 1106 bad: 1107 m_freem(control); 1108 free(iov, M_IOV); 1109 return (error); 1110 } 1111 1112 int 1113 linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args) 1114 { 1115 1116 return (linux_sendmsg_common(td, args->s, PTRIN(args->msg), 1117 args->flags)); 1118 } 1119 1120 int 1121 linux_sendmmsg(struct thread *td, struct linux_sendmmsg_args *args) 1122 { 1123 struct l_mmsghdr *msg; 1124 l_uint retval; 1125 int error, datagrams; 1126 1127 if (args->vlen > UIO_MAXIOV) 1128 args->vlen = UIO_MAXIOV; 1129 1130 msg = PTRIN(args->msg); 1131 datagrams = 0; 1132 while (datagrams < args->vlen) { 1133 error = linux_sendmsg_common(td, args->s, &msg->msg_hdr, 1134 args->flags); 1135 if (error != 0) 1136 break; 1137 1138 retval = td->td_retval[0]; 1139 error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len)); 1140 if (error != 0) 1141 break; 1142 ++msg; 1143 ++datagrams; 1144 } 1145 if (error == 0) 1146 td->td_retval[0] = datagrams; 1147 return (error); 1148 } 1149 1150 static int 1151 linux_recvmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr, 1152 l_uint flags, struct msghdr *msg) 1153 { 1154 struct cmsghdr *cm; 1155 struct cmsgcred *cmcred; 1156 struct l_cmsghdr *linux_cmsg = NULL; 1157 struct l_ucred linux_ucred; 1158 socklen_t datalen, maxlen, outlen; 1159 struct l_msghdr linux_msghdr; 1160 struct iovec *iov, *uiov; 1161 struct mbuf *control = NULL; 1162 struct mbuf **controlp; 1163 struct timeval *ftmvl; 1164 struct l_sockaddr *lsa; 1165 struct sockaddr *sa; 1166 l_timeval ltmvl; 1167 caddr_t outbuf; 1168 void *data; 1169 int error, i, fd, fds, *fdp; 1170 1171 error = copyin(msghdr, &linux_msghdr, sizeof(linux_msghdr)); 1172 if (error != 0) 1173 return (error); 1174 1175 error = linux_to_bsd_msghdr(msg, &linux_msghdr); 1176 if (error != 0) 1177 return (error); 1178 1179 #ifdef COMPAT_LINUX32 1180 error = linux32_copyiniov(PTRIN(msg->msg_iov), msg->msg_iovlen, 1181 &iov, EMSGSIZE); 1182 #else 1183 error = copyiniov(msg->msg_iov, msg->msg_iovlen, &iov, EMSGSIZE); 1184 #endif 1185 if (error != 0) 1186 return (error); 1187 1188 if (msg->msg_name) { 1189 sa = malloc(msg->msg_namelen, M_SONAME, M_WAITOK); 1190 msg->msg_name = sa; 1191 } else 1192 sa = NULL; 1193 1194 uiov = msg->msg_iov; 1195 msg->msg_iov = iov; 1196 controlp = (msg->msg_control != NULL) ? &control : NULL; 1197 error = kern_recvit(td, s, msg, UIO_SYSSPACE, controlp); 1198 msg->msg_iov = uiov; 1199 if (error != 0) 1200 goto bad; 1201 1202 if (msg->msg_name) { 1203 msg->msg_name = PTRIN(linux_msghdr.msg_name); 1204 error = bsd_to_linux_sockaddr(sa, &lsa, msg->msg_namelen); 1205 if (error == 0) 1206 error = copyout(lsa, PTRIN(msg->msg_name), 1207 msg->msg_namelen); 1208 free(lsa, M_SONAME); 1209 if (error != 0) 1210 goto bad; 1211 } 1212 1213 error = bsd_to_linux_msghdr(msg, &linux_msghdr); 1214 if (error != 0) 1215 goto bad; 1216 1217 maxlen = linux_msghdr.msg_controllen; 1218 linux_msghdr.msg_controllen = 0; 1219 if (control) { 1220 linux_cmsg = malloc(L_CMSG_HDRSZ, M_LINUX, M_WAITOK | M_ZERO); 1221 1222 msg->msg_control = mtod(control, struct cmsghdr *); 1223 msg->msg_controllen = control->m_len; 1224 1225 cm = CMSG_FIRSTHDR(msg); 1226 outbuf = PTRIN(linux_msghdr.msg_control); 1227 outlen = 0; 1228 while (cm != NULL) { 1229 linux_cmsg->cmsg_type = 1230 bsd_to_linux_cmsg_type(cm->cmsg_type); 1231 linux_cmsg->cmsg_level = 1232 bsd_to_linux_sockopt_level(cm->cmsg_level); 1233 if (linux_cmsg->cmsg_type == -1 || 1234 cm->cmsg_level != SOL_SOCKET) { 1235 linux_msg(curthread, 1236 "unsupported recvmsg cmsg level %d type %d", 1237 cm->cmsg_level, cm->cmsg_type); 1238 error = EINVAL; 1239 goto bad; 1240 } 1241 1242 data = CMSG_DATA(cm); 1243 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 1244 1245 switch (cm->cmsg_type) { 1246 case SCM_RIGHTS: 1247 if (flags & LINUX_MSG_CMSG_CLOEXEC) { 1248 fds = datalen / sizeof(int); 1249 fdp = data; 1250 for (i = 0; i < fds; i++) { 1251 fd = *fdp++; 1252 (void)kern_fcntl(td, fd, 1253 F_SETFD, FD_CLOEXEC); 1254 } 1255 } 1256 break; 1257 1258 case SCM_CREDS: 1259 /* 1260 * Currently LOCAL_CREDS is never in 1261 * effect for Linux so no need to worry 1262 * about sockcred 1263 */ 1264 if (datalen != sizeof(*cmcred)) { 1265 error = EMSGSIZE; 1266 goto bad; 1267 } 1268 cmcred = (struct cmsgcred *)data; 1269 bzero(&linux_ucred, sizeof(linux_ucred)); 1270 linux_ucred.pid = cmcred->cmcred_pid; 1271 linux_ucred.uid = cmcred->cmcred_uid; 1272 linux_ucred.gid = cmcred->cmcred_gid; 1273 data = &linux_ucred; 1274 datalen = sizeof(linux_ucred); 1275 break; 1276 1277 case SCM_TIMESTAMP: 1278 if (datalen != sizeof(struct timeval)) { 1279 error = EMSGSIZE; 1280 goto bad; 1281 } 1282 ftmvl = (struct timeval *)data; 1283 ltmvl.tv_sec = ftmvl->tv_sec; 1284 ltmvl.tv_usec = ftmvl->tv_usec; 1285 data = <mvl; 1286 datalen = sizeof(ltmvl); 1287 break; 1288 } 1289 1290 if (outlen + LINUX_CMSG_LEN(datalen) > maxlen) { 1291 if (outlen == 0) { 1292 error = EMSGSIZE; 1293 goto bad; 1294 } else { 1295 linux_msghdr.msg_flags |= LINUX_MSG_CTRUNC; 1296 m_dispose_extcontrolm(control); 1297 goto out; 1298 } 1299 } 1300 1301 linux_cmsg->cmsg_len = LINUX_CMSG_LEN(datalen); 1302 1303 error = copyout(linux_cmsg, outbuf, L_CMSG_HDRSZ); 1304 if (error != 0) 1305 goto bad; 1306 outbuf += L_CMSG_HDRSZ; 1307 1308 error = copyout(data, outbuf, datalen); 1309 if (error != 0) 1310 goto bad; 1311 1312 outbuf += LINUX_CMSG_ALIGN(datalen); 1313 outlen += LINUX_CMSG_LEN(datalen); 1314 1315 cm = CMSG_NXTHDR(msg, cm); 1316 } 1317 linux_msghdr.msg_controllen = outlen; 1318 } 1319 1320 out: 1321 error = copyout(&linux_msghdr, msghdr, sizeof(linux_msghdr)); 1322 1323 bad: 1324 if (control != NULL) { 1325 if (error != 0) 1326 m_dispose_extcontrolm(control); 1327 m_freem(control); 1328 } 1329 free(iov, M_IOV); 1330 free(linux_cmsg, M_LINUX); 1331 free(sa, M_SONAME); 1332 1333 return (error); 1334 } 1335 1336 int 1337 linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args) 1338 { 1339 struct msghdr bsd_msg; 1340 1341 return (linux_recvmsg_common(td, args->s, PTRIN(args->msg), 1342 args->flags, &bsd_msg)); 1343 } 1344 1345 int 1346 linux_recvmmsg(struct thread *td, struct linux_recvmmsg_args *args) 1347 { 1348 struct l_mmsghdr *msg; 1349 struct msghdr bsd_msg; 1350 struct l_timespec lts; 1351 struct timespec ts, tts; 1352 l_uint retval; 1353 int error, datagrams; 1354 1355 if (args->timeout) { 1356 error = copyin(args->timeout, <s, sizeof(struct l_timespec)); 1357 if (error != 0) 1358 return (error); 1359 error = linux_to_native_timespec(&ts, <s); 1360 if (error != 0) 1361 return (error); 1362 getnanotime(&tts); 1363 timespecadd(&tts, &ts, &tts); 1364 } 1365 1366 msg = PTRIN(args->msg); 1367 datagrams = 0; 1368 while (datagrams < args->vlen) { 1369 error = linux_recvmsg_common(td, args->s, &msg->msg_hdr, 1370 args->flags & ~LINUX_MSG_WAITFORONE, &bsd_msg); 1371 if (error != 0) 1372 break; 1373 1374 retval = td->td_retval[0]; 1375 error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len)); 1376 if (error != 0) 1377 break; 1378 ++msg; 1379 ++datagrams; 1380 1381 /* 1382 * MSG_WAITFORONE turns on MSG_DONTWAIT after one packet. 1383 */ 1384 if (args->flags & LINUX_MSG_WAITFORONE) 1385 args->flags |= LINUX_MSG_DONTWAIT; 1386 1387 /* 1388 * See BUGS section of recvmmsg(2). 1389 */ 1390 if (args->timeout) { 1391 getnanotime(&ts); 1392 timespecsub(&ts, &tts, &ts); 1393 if (!timespecisset(&ts) || ts.tv_sec > 0) 1394 break; 1395 } 1396 /* Out of band data, return right away. */ 1397 if (bsd_msg.msg_flags & MSG_OOB) 1398 break; 1399 } 1400 if (error == 0) 1401 td->td_retval[0] = datagrams; 1402 return (error); 1403 } 1404 1405 int 1406 linux_shutdown(struct thread *td, struct linux_shutdown_args *args) 1407 { 1408 1409 return (kern_shutdown(td, args->s, args->how)); 1410 } 1411 1412 int 1413 linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args) 1414 { 1415 l_timeval linux_tv; 1416 struct sockaddr *sa; 1417 struct timeval tv; 1418 socklen_t len; 1419 int error, level, name; 1420 1421 level = linux_to_bsd_sockopt_level(args->level); 1422 switch (level) { 1423 case SOL_SOCKET: 1424 name = linux_to_bsd_so_sockopt(args->optname); 1425 switch (name) { 1426 case SO_RCVTIMEO: 1427 /* FALLTHROUGH */ 1428 case SO_SNDTIMEO: 1429 error = copyin(PTRIN(args->optval), &linux_tv, 1430 sizeof(linux_tv)); 1431 if (error != 0) 1432 return (error); 1433 tv.tv_sec = linux_tv.tv_sec; 1434 tv.tv_usec = linux_tv.tv_usec; 1435 return (kern_setsockopt(td, args->s, level, 1436 name, &tv, UIO_SYSSPACE, sizeof(tv))); 1437 /* NOTREACHED */ 1438 default: 1439 break; 1440 } 1441 break; 1442 case IPPROTO_IP: 1443 if (args->optname == LINUX_IP_RECVERR && 1444 linux_ignore_ip_recverr) { 1445 /* 1446 * XXX: This is a hack to unbreak DNS resolution 1447 * with glibc 2.30 and above. 1448 */ 1449 return (0); 1450 } 1451 name = linux_to_bsd_ip_sockopt(args->optname); 1452 break; 1453 case IPPROTO_IPV6: 1454 name = linux_to_bsd_ip6_sockopt(args->optname); 1455 break; 1456 case IPPROTO_TCP: 1457 name = linux_to_bsd_tcp_sockopt(args->optname); 1458 break; 1459 default: 1460 name = -1; 1461 break; 1462 } 1463 if (name == -1) { 1464 linux_msg(curthread, 1465 "unsupported setsockopt level %d optname %d", 1466 args->level, args->optname); 1467 return (ENOPROTOOPT); 1468 } 1469 1470 if (name == IPV6_NEXTHOP) { 1471 len = args->optlen; 1472 error = linux_to_bsd_sockaddr(PTRIN(args->optval), &sa, &len); 1473 if (error != 0) 1474 return (error); 1475 1476 error = kern_setsockopt(td, args->s, level, 1477 name, sa, UIO_SYSSPACE, len); 1478 free(sa, M_SONAME); 1479 } else { 1480 error = kern_setsockopt(td, args->s, level, 1481 name, PTRIN(args->optval), UIO_USERSPACE, args->optlen); 1482 } 1483 1484 return (error); 1485 } 1486 1487 int 1488 linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args) 1489 { 1490 l_timeval linux_tv; 1491 struct timeval tv; 1492 socklen_t tv_len, xulen, len; 1493 struct l_sockaddr *lsa; 1494 struct sockaddr *sa; 1495 struct xucred xu; 1496 struct l_ucred lxu; 1497 int error, level, name, newval; 1498 1499 level = linux_to_bsd_sockopt_level(args->level); 1500 switch (level) { 1501 case SOL_SOCKET: 1502 name = linux_to_bsd_so_sockopt(args->optname); 1503 switch (name) { 1504 case SO_RCVTIMEO: 1505 /* FALLTHROUGH */ 1506 case SO_SNDTIMEO: 1507 tv_len = sizeof(tv); 1508 error = kern_getsockopt(td, args->s, level, 1509 name, &tv, UIO_SYSSPACE, &tv_len); 1510 if (error != 0) 1511 return (error); 1512 linux_tv.tv_sec = tv.tv_sec; 1513 linux_tv.tv_usec = tv.tv_usec; 1514 return (copyout(&linux_tv, PTRIN(args->optval), 1515 sizeof(linux_tv))); 1516 /* NOTREACHED */ 1517 case LOCAL_PEERCRED: 1518 if (args->optlen < sizeof(lxu)) 1519 return (EINVAL); 1520 /* 1521 * LOCAL_PEERCRED is not served at the SOL_SOCKET level, 1522 * but by the Unix socket's level 0. 1523 */ 1524 level = 0; 1525 xulen = sizeof(xu); 1526 error = kern_getsockopt(td, args->s, level, 1527 name, &xu, UIO_SYSSPACE, &xulen); 1528 if (error != 0) 1529 return (error); 1530 lxu.pid = xu.cr_pid; 1531 lxu.uid = xu.cr_uid; 1532 lxu.gid = xu.cr_gid; 1533 return (copyout(&lxu, PTRIN(args->optval), sizeof(lxu))); 1534 /* NOTREACHED */ 1535 case SO_ERROR: 1536 len = sizeof(newval); 1537 error = kern_getsockopt(td, args->s, level, 1538 name, &newval, UIO_SYSSPACE, &len); 1539 if (error != 0) 1540 return (error); 1541 newval = -SV_ABI_ERRNO(td->td_proc, newval); 1542 return (copyout(&newval, PTRIN(args->optval), len)); 1543 /* NOTREACHED */ 1544 default: 1545 break; 1546 } 1547 break; 1548 case IPPROTO_IP: 1549 name = linux_to_bsd_ip_sockopt(args->optname); 1550 break; 1551 case IPPROTO_IPV6: 1552 name = linux_to_bsd_ip6_sockopt(args->optname); 1553 break; 1554 case IPPROTO_TCP: 1555 name = linux_to_bsd_tcp_sockopt(args->optname); 1556 break; 1557 default: 1558 name = -1; 1559 break; 1560 } 1561 if (name == -1) { 1562 linux_msg(curthread, 1563 "unsupported getsockopt level %d optname %d", 1564 args->level, args->optname); 1565 return (EINVAL); 1566 } 1567 1568 if (name == IPV6_NEXTHOP) { 1569 error = copyin(PTRIN(args->optlen), &len, sizeof(len)); 1570 if (error != 0) 1571 return (error); 1572 sa = malloc(len, M_SONAME, M_WAITOK); 1573 1574 error = kern_getsockopt(td, args->s, level, 1575 name, sa, UIO_SYSSPACE, &len); 1576 if (error != 0) 1577 goto out; 1578 1579 error = bsd_to_linux_sockaddr(sa, &lsa, len); 1580 if (error == 0) 1581 error = copyout(lsa, PTRIN(args->optval), len); 1582 free(lsa, M_SONAME); 1583 if (error == 0) 1584 error = copyout(&len, PTRIN(args->optlen), 1585 sizeof(len)); 1586 out: 1587 free(sa, M_SONAME); 1588 } else { 1589 if (args->optval) { 1590 error = copyin(PTRIN(args->optlen), &len, sizeof(len)); 1591 if (error != 0) 1592 return (error); 1593 } 1594 error = kern_getsockopt(td, args->s, level, 1595 name, PTRIN(args->optval), UIO_USERSPACE, &len); 1596 if (error == 0) 1597 error = copyout(&len, PTRIN(args->optlen), 1598 sizeof(len)); 1599 } 1600 1601 return (error); 1602 } 1603 1604 static int 1605 linux_sendfile_common(struct thread *td, l_int out, l_int in, 1606 l_loff_t *offset, l_size_t count) 1607 { 1608 off_t bytes_read; 1609 int error; 1610 l_loff_t current_offset; 1611 struct file *fp; 1612 1613 AUDIT_ARG_FD(in); 1614 error = fget_read(td, in, &cap_pread_rights, &fp); 1615 if (error != 0) 1616 return (error); 1617 1618 if (offset != NULL) { 1619 current_offset = *offset; 1620 } else { 1621 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ? 1622 fo_seek(fp, 0, SEEK_CUR, td) : ESPIPE; 1623 if (error != 0) 1624 goto drop; 1625 current_offset = td->td_uretoff.tdu_off; 1626 } 1627 1628 bytes_read = 0; 1629 1630 /* Linux cannot have 0 count. */ 1631 if (count <= 0 || current_offset < 0) { 1632 error = EINVAL; 1633 goto drop; 1634 } 1635 1636 error = fo_sendfile(fp, out, NULL, NULL, current_offset, count, 1637 &bytes_read, 0, td); 1638 if (error != 0) 1639 goto drop; 1640 current_offset += bytes_read; 1641 1642 if (offset != NULL) { 1643 *offset = current_offset; 1644 } else { 1645 error = fo_seek(fp, current_offset, SEEK_SET, td); 1646 if (error != 0) 1647 goto drop; 1648 } 1649 1650 td->td_retval[0] = (ssize_t)bytes_read; 1651 drop: 1652 fdrop(fp, td); 1653 return (error); 1654 } 1655 1656 int 1657 linux_sendfile(struct thread *td, struct linux_sendfile_args *arg) 1658 { 1659 /* 1660 * Differences between FreeBSD and Linux sendfile: 1661 * - Linux doesn't send anything when count is 0 (FreeBSD uses 0 to 1662 * mean send the whole file.) In linux_sendfile given fds are still 1663 * checked for validity when the count is 0. 1664 * - Linux can send to any fd whereas FreeBSD only supports sockets. 1665 * The same restriction follows for linux_sendfile. 1666 * - Linux doesn't have an equivalent for FreeBSD's flags and sf_hdtr. 1667 * - Linux takes an offset pointer and updates it to the read location. 1668 * FreeBSD takes in an offset and a 'bytes read' parameter which is 1669 * only filled if it isn't NULL. We use this parameter to update the 1670 * offset pointer if it exists. 1671 * - Linux sendfile returns bytes read on success while FreeBSD 1672 * returns 0. We use the 'bytes read' parameter to get this value. 1673 */ 1674 1675 l_loff_t offset64; 1676 l_long offset; 1677 int ret; 1678 int error; 1679 1680 if (arg->offset != NULL) { 1681 error = copyin(arg->offset, &offset, sizeof(offset)); 1682 if (error != 0) 1683 return (error); 1684 offset64 = (l_loff_t)offset; 1685 } 1686 1687 ret = linux_sendfile_common(td, arg->out, arg->in, 1688 arg->offset != NULL ? &offset64 : NULL, arg->count); 1689 1690 if (arg->offset != NULL) { 1691 #if defined(__i386__) || defined(__arm__) || \ 1692 (defined(__amd64__) && defined(COMPAT_LINUX32)) 1693 if (offset64 > INT32_MAX) 1694 return (EOVERFLOW); 1695 #endif 1696 offset = (l_long)offset64; 1697 error = copyout(&offset, arg->offset, sizeof(offset)); 1698 if (error != 0) 1699 return (error); 1700 } 1701 1702 return (ret); 1703 } 1704 1705 #if defined(__i386__) || defined(__arm__) || \ 1706 (defined(__amd64__) && defined(COMPAT_LINUX32)) 1707 1708 int 1709 linux_sendfile64(struct thread *td, struct linux_sendfile64_args *arg) 1710 { 1711 l_loff_t offset; 1712 int ret; 1713 int error; 1714 1715 if (arg->offset != NULL) { 1716 error = copyin(arg->offset, &offset, sizeof(offset)); 1717 if (error != 0) 1718 return (error); 1719 } 1720 1721 ret = linux_sendfile_common(td, arg->out, arg->in, 1722 arg->offset != NULL ? &offset : NULL, arg->count); 1723 1724 if (arg->offset != NULL) { 1725 error = copyout(&offset, arg->offset, sizeof(offset)); 1726 if (error != 0) 1727 return (error); 1728 } 1729 1730 return (ret); 1731 } 1732 1733 /* Argument list sizes for linux_socketcall */ 1734 static const unsigned char lxs_args_cnt[] = { 1735 0 /* unused*/, 3 /* socket */, 1736 3 /* bind */, 3 /* connect */, 1737 2 /* listen */, 3 /* accept */, 1738 3 /* getsockname */, 3 /* getpeername */, 1739 4 /* socketpair */, 4 /* send */, 1740 4 /* recv */, 6 /* sendto */, 1741 6 /* recvfrom */, 2 /* shutdown */, 1742 5 /* setsockopt */, 5 /* getsockopt */, 1743 3 /* sendmsg */, 3 /* recvmsg */, 1744 4 /* accept4 */, 5 /* recvmmsg */, 1745 4 /* sendmmsg */, 4 /* sendfile */ 1746 }; 1747 #define LINUX_ARGS_CNT (nitems(lxs_args_cnt) - 1) 1748 #define LINUX_ARG_SIZE(x) (lxs_args_cnt[x] * sizeof(l_ulong)) 1749 1750 int 1751 linux_socketcall(struct thread *td, struct linux_socketcall_args *args) 1752 { 1753 l_ulong a[6]; 1754 #if defined(__amd64__) && defined(COMPAT_LINUX32) 1755 register_t l_args[6]; 1756 #endif 1757 void *arg; 1758 int error; 1759 1760 if (args->what < LINUX_SOCKET || args->what > LINUX_ARGS_CNT) 1761 return (EINVAL); 1762 error = copyin(PTRIN(args->args), a, LINUX_ARG_SIZE(args->what)); 1763 if (error != 0) 1764 return (error); 1765 1766 #if defined(__amd64__) && defined(COMPAT_LINUX32) 1767 for (int i = 0; i < lxs_args_cnt[args->what]; ++i) 1768 l_args[i] = a[i]; 1769 arg = l_args; 1770 #else 1771 arg = a; 1772 #endif 1773 switch (args->what) { 1774 case LINUX_SOCKET: 1775 return (linux_socket(td, arg)); 1776 case LINUX_BIND: 1777 return (linux_bind(td, arg)); 1778 case LINUX_CONNECT: 1779 return (linux_connect(td, arg)); 1780 case LINUX_LISTEN: 1781 return (linux_listen(td, arg)); 1782 case LINUX_ACCEPT: 1783 return (linux_accept(td, arg)); 1784 case LINUX_GETSOCKNAME: 1785 return (linux_getsockname(td, arg)); 1786 case LINUX_GETPEERNAME: 1787 return (linux_getpeername(td, arg)); 1788 case LINUX_SOCKETPAIR: 1789 return (linux_socketpair(td, arg)); 1790 case LINUX_SEND: 1791 return (linux_send(td, arg)); 1792 case LINUX_RECV: 1793 return (linux_recv(td, arg)); 1794 case LINUX_SENDTO: 1795 return (linux_sendto(td, arg)); 1796 case LINUX_RECVFROM: 1797 return (linux_recvfrom(td, arg)); 1798 case LINUX_SHUTDOWN: 1799 return (linux_shutdown(td, arg)); 1800 case LINUX_SETSOCKOPT: 1801 return (linux_setsockopt(td, arg)); 1802 case LINUX_GETSOCKOPT: 1803 return (linux_getsockopt(td, arg)); 1804 case LINUX_SENDMSG: 1805 return (linux_sendmsg(td, arg)); 1806 case LINUX_RECVMSG: 1807 return (linux_recvmsg(td, arg)); 1808 case LINUX_ACCEPT4: 1809 return (linux_accept4(td, arg)); 1810 case LINUX_RECVMMSG: 1811 return (linux_recvmmsg(td, arg)); 1812 case LINUX_SENDMMSG: 1813 return (linux_sendmmsg(td, arg)); 1814 case LINUX_SENDFILE: 1815 return (linux_sendfile(td, arg)); 1816 } 1817 1818 linux_msg(td, "socket type %d not implemented", args->what); 1819 return (ENOSYS); 1820 } 1821 #endif /* __i386__ || __arm__ || (__amd64__ && COMPAT_LINUX32) */ 1822