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