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