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