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