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