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 1168 bad: 1169 m_freem(control); 1170 free(iov, M_IOV); 1171 if (cmsg) 1172 free(cmsg, M_LINUX); 1173 return (error); 1174 } 1175 1176 int 1177 linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args) 1178 { 1179 1180 return (linux_sendmsg_common(td, args->s, PTRIN(args->msg), 1181 args->flags)); 1182 } 1183 1184 int 1185 linux_sendmmsg(struct thread *td, struct linux_sendmmsg_args *args) 1186 { 1187 struct l_mmsghdr *msg; 1188 l_uint retval; 1189 int error, datagrams; 1190 1191 if (args->vlen > UIO_MAXIOV) 1192 args->vlen = UIO_MAXIOV; 1193 1194 msg = PTRIN(args->msg); 1195 datagrams = 0; 1196 while (datagrams < args->vlen) { 1197 error = linux_sendmsg_common(td, args->s, &msg->msg_hdr, 1198 args->flags); 1199 if (error != 0) 1200 break; 1201 1202 retval = td->td_retval[0]; 1203 error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len)); 1204 if (error != 0) 1205 break; 1206 ++msg; 1207 ++datagrams; 1208 } 1209 if (error == 0) 1210 td->td_retval[0] = datagrams; 1211 return (error); 1212 } 1213 1214 static int 1215 linux_recvmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr, 1216 l_uint flags, struct msghdr *msg) 1217 { 1218 struct cmsghdr *cm; 1219 struct cmsgcred *cmcred; 1220 struct l_cmsghdr *linux_cmsg = NULL; 1221 struct l_ucred linux_ucred; 1222 socklen_t datalen, outlen; 1223 struct l_msghdr linux_msg; 1224 struct iovec *iov, *uiov; 1225 struct mbuf *control = NULL; 1226 struct mbuf **controlp; 1227 struct timeval *ftmvl; 1228 l_timeval ltmvl; 1229 caddr_t outbuf; 1230 void *data; 1231 int error, i, fd, fds, *fdp; 1232 1233 error = copyin(msghdr, &linux_msg, sizeof(linux_msg)); 1234 if (error != 0) 1235 return (error); 1236 1237 error = linux_to_bsd_msghdr(msg, &linux_msg); 1238 if (error != 0) 1239 return (error); 1240 1241 #ifdef COMPAT_LINUX32 1242 error = linux32_copyiniov(PTRIN(msg->msg_iov), msg->msg_iovlen, 1243 &iov, EMSGSIZE); 1244 #else 1245 error = copyiniov(msg->msg_iov, msg->msg_iovlen, &iov, EMSGSIZE); 1246 #endif 1247 if (error != 0) 1248 return (error); 1249 1250 if (msg->msg_name) { 1251 error = linux_to_bsd_sockaddr((struct sockaddr *)msg->msg_name, 1252 msg->msg_namelen); 1253 if (error != 0) 1254 goto bad; 1255 } 1256 1257 uiov = msg->msg_iov; 1258 msg->msg_iov = iov; 1259 controlp = (msg->msg_control != NULL) ? &control : NULL; 1260 error = kern_recvit(td, s, msg, UIO_USERSPACE, controlp); 1261 msg->msg_iov = uiov; 1262 if (error != 0) 1263 goto bad; 1264 1265 error = bsd_to_linux_msghdr(msg, &linux_msg); 1266 if (error != 0) 1267 goto bad; 1268 1269 if (linux_msg.msg_name) { 1270 error = bsd_to_linux_sockaddr((struct sockaddr *) 1271 PTRIN(linux_msg.msg_name)); 1272 if (error != 0) 1273 goto bad; 1274 } 1275 if (linux_msg.msg_name && linux_msg.msg_namelen > 2) { 1276 error = linux_sa_put(PTRIN(linux_msg.msg_name)); 1277 if (error != 0) 1278 goto bad; 1279 } 1280 1281 outbuf = PTRIN(linux_msg.msg_control); 1282 outlen = 0; 1283 1284 if (control) { 1285 linux_cmsg = malloc(L_CMSG_HDRSZ, M_LINUX, M_WAITOK | M_ZERO); 1286 1287 msg->msg_control = mtod(control, struct cmsghdr *); 1288 msg->msg_controllen = control->m_len; 1289 1290 cm = CMSG_FIRSTHDR(msg); 1291 1292 while (cm != NULL) { 1293 linux_cmsg->cmsg_type = 1294 bsd_to_linux_cmsg_type(cm->cmsg_type); 1295 linux_cmsg->cmsg_level = 1296 bsd_to_linux_sockopt_level(cm->cmsg_level); 1297 if (linux_cmsg->cmsg_type == -1 1298 || cm->cmsg_level != SOL_SOCKET) 1299 { 1300 error = EINVAL; 1301 goto bad; 1302 } 1303 1304 data = CMSG_DATA(cm); 1305 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 1306 1307 switch (cm->cmsg_type) 1308 { 1309 case SCM_RIGHTS: 1310 if (flags & LINUX_MSG_CMSG_CLOEXEC) { 1311 fds = datalen / sizeof(int); 1312 fdp = data; 1313 for (i = 0; i < fds; i++) { 1314 fd = *fdp++; 1315 (void)kern_fcntl(td, fd, 1316 F_SETFD, FD_CLOEXEC); 1317 } 1318 } 1319 break; 1320 1321 case SCM_CREDS: 1322 /* 1323 * Currently LOCAL_CREDS is never in 1324 * effect for Linux so no need to worry 1325 * about sockcred 1326 */ 1327 if (datalen != sizeof(*cmcred)) { 1328 error = EMSGSIZE; 1329 goto bad; 1330 } 1331 cmcred = (struct cmsgcred *)data; 1332 bzero(&linux_ucred, sizeof(linux_ucred)); 1333 linux_ucred.pid = cmcred->cmcred_pid; 1334 linux_ucred.uid = cmcred->cmcred_uid; 1335 linux_ucred.gid = cmcred->cmcred_gid; 1336 data = &linux_ucred; 1337 datalen = sizeof(linux_ucred); 1338 break; 1339 1340 case SCM_TIMESTAMP: 1341 if (datalen != sizeof(struct timeval)) { 1342 error = EMSGSIZE; 1343 goto bad; 1344 } 1345 ftmvl = (struct timeval *)data; 1346 ltmvl.tv_sec = ftmvl->tv_sec; 1347 ltmvl.tv_usec = ftmvl->tv_usec; 1348 data = <mvl; 1349 datalen = sizeof(ltmvl); 1350 break; 1351 } 1352 1353 if (outlen + LINUX_CMSG_LEN(datalen) > 1354 linux_msg.msg_controllen) { 1355 if (outlen == 0) { 1356 error = EMSGSIZE; 1357 goto bad; 1358 } else { 1359 linux_msg.msg_flags |= 1360 LINUX_MSG_CTRUNC; 1361 goto out; 1362 } 1363 } 1364 1365 linux_cmsg->cmsg_len = LINUX_CMSG_LEN(datalen); 1366 1367 error = copyout(linux_cmsg, outbuf, L_CMSG_HDRSZ); 1368 if (error) 1369 goto bad; 1370 outbuf += L_CMSG_HDRSZ; 1371 1372 error = copyout(data, outbuf, datalen); 1373 if (error) 1374 goto bad; 1375 1376 outbuf += LINUX_CMSG_ALIGN(datalen); 1377 outlen += LINUX_CMSG_LEN(datalen); 1378 1379 cm = CMSG_NXTHDR(msg, cm); 1380 } 1381 } 1382 1383 out: 1384 linux_msg.msg_controllen = outlen; 1385 error = copyout(&linux_msg, msghdr, sizeof(linux_msg)); 1386 1387 bad: 1388 free(iov, M_IOV); 1389 m_freem(control); 1390 free(linux_cmsg, M_LINUX); 1391 1392 return (error); 1393 } 1394 1395 int 1396 linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args) 1397 { 1398 struct msghdr bsd_msg; 1399 1400 return (linux_recvmsg_common(td, args->s, PTRIN(args->msg), 1401 args->flags, &bsd_msg)); 1402 } 1403 1404 int 1405 linux_recvmmsg(struct thread *td, struct linux_recvmmsg_args *args) 1406 { 1407 struct l_mmsghdr *msg; 1408 struct msghdr bsd_msg; 1409 struct l_timespec lts; 1410 struct timespec ts, tts; 1411 l_uint retval; 1412 int error, datagrams; 1413 1414 if (args->timeout) { 1415 error = copyin(args->timeout, <s, sizeof(struct l_timespec)); 1416 if (error != 0) 1417 return (error); 1418 error = linux_to_native_timespec(&ts, <s); 1419 if (error != 0) 1420 return (error); 1421 getnanotime(&tts); 1422 timespecadd(&tts, &ts); 1423 } 1424 1425 msg = PTRIN(args->msg); 1426 datagrams = 0; 1427 while (datagrams < args->vlen) { 1428 error = linux_recvmsg_common(td, args->s, &msg->msg_hdr, 1429 args->flags & ~LINUX_MSG_WAITFORONE, &bsd_msg); 1430 if (error != 0) 1431 break; 1432 1433 retval = td->td_retval[0]; 1434 error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len)); 1435 if (error != 0) 1436 break; 1437 ++msg; 1438 ++datagrams; 1439 1440 /* 1441 * MSG_WAITFORONE turns on MSG_DONTWAIT after one packet. 1442 */ 1443 if (args->flags & LINUX_MSG_WAITFORONE) 1444 args->flags |= LINUX_MSG_DONTWAIT; 1445 1446 /* 1447 * See BUGS section of recvmmsg(2). 1448 */ 1449 if (args->timeout) { 1450 getnanotime(&ts); 1451 timespecsub(&ts, &tts); 1452 if (!timespecisset(&ts) || ts.tv_sec > 0) 1453 break; 1454 } 1455 /* Out of band data, return right away. */ 1456 if (bsd_msg.msg_flags & MSG_OOB) 1457 break; 1458 } 1459 if (error == 0) 1460 td->td_retval[0] = datagrams; 1461 return (error); 1462 } 1463 1464 int 1465 linux_shutdown(struct thread *td, struct linux_shutdown_args *args) 1466 { 1467 struct shutdown_args /* { 1468 int s; 1469 int how; 1470 } */ bsd_args; 1471 1472 bsd_args.s = args->s; 1473 bsd_args.how = args->how; 1474 return (sys_shutdown(td, &bsd_args)); 1475 } 1476 1477 int 1478 linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args) 1479 { 1480 struct setsockopt_args /* { 1481 int s; 1482 int level; 1483 int name; 1484 caddr_t val; 1485 int valsize; 1486 } */ bsd_args; 1487 l_timeval linux_tv; 1488 struct timeval tv; 1489 int error, name; 1490 1491 bsd_args.s = args->s; 1492 bsd_args.level = linux_to_bsd_sockopt_level(args->level); 1493 switch (bsd_args.level) { 1494 case SOL_SOCKET: 1495 name = linux_to_bsd_so_sockopt(args->optname); 1496 switch (name) { 1497 case SO_RCVTIMEO: 1498 /* FALLTHROUGH */ 1499 case SO_SNDTIMEO: 1500 error = copyin(PTRIN(args->optval), &linux_tv, 1501 sizeof(linux_tv)); 1502 if (error) 1503 return (error); 1504 tv.tv_sec = linux_tv.tv_sec; 1505 tv.tv_usec = linux_tv.tv_usec; 1506 return (kern_setsockopt(td, args->s, bsd_args.level, 1507 name, &tv, UIO_SYSSPACE, sizeof(tv))); 1508 /* NOTREACHED */ 1509 break; 1510 default: 1511 break; 1512 } 1513 break; 1514 case IPPROTO_IP: 1515 name = linux_to_bsd_ip_sockopt(args->optname); 1516 break; 1517 case IPPROTO_TCP: 1518 name = linux_to_bsd_tcp_sockopt(args->optname); 1519 break; 1520 default: 1521 name = -1; 1522 break; 1523 } 1524 if (name == -1) 1525 return (ENOPROTOOPT); 1526 1527 bsd_args.name = name; 1528 bsd_args.val = PTRIN(args->optval); 1529 bsd_args.valsize = args->optlen; 1530 1531 if (name == IPV6_NEXTHOP) { 1532 linux_to_bsd_sockaddr((struct sockaddr *)bsd_args.val, 1533 bsd_args.valsize); 1534 error = sys_setsockopt(td, &bsd_args); 1535 bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.val); 1536 } else 1537 error = sys_setsockopt(td, &bsd_args); 1538 1539 return (error); 1540 } 1541 1542 int 1543 linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args) 1544 { 1545 struct getsockopt_args /* { 1546 int s; 1547 int level; 1548 int name; 1549 caddr_t val; 1550 int *avalsize; 1551 } */ bsd_args; 1552 l_timeval linux_tv; 1553 struct timeval tv; 1554 socklen_t tv_len, xulen; 1555 struct xucred xu; 1556 struct l_ucred lxu; 1557 int error, name; 1558 1559 bsd_args.s = args->s; 1560 bsd_args.level = linux_to_bsd_sockopt_level(args->level); 1561 switch (bsd_args.level) { 1562 case SOL_SOCKET: 1563 name = linux_to_bsd_so_sockopt(args->optname); 1564 switch (name) { 1565 case SO_RCVTIMEO: 1566 /* FALLTHROUGH */ 1567 case SO_SNDTIMEO: 1568 tv_len = sizeof(tv); 1569 error = kern_getsockopt(td, args->s, bsd_args.level, 1570 name, &tv, UIO_SYSSPACE, &tv_len); 1571 if (error) 1572 return (error); 1573 linux_tv.tv_sec = tv.tv_sec; 1574 linux_tv.tv_usec = tv.tv_usec; 1575 return (copyout(&linux_tv, PTRIN(args->optval), 1576 sizeof(linux_tv))); 1577 /* NOTREACHED */ 1578 break; 1579 case LOCAL_PEERCRED: 1580 if (args->optlen != sizeof(lxu)) 1581 return (EINVAL); 1582 xulen = sizeof(xu); 1583 error = kern_getsockopt(td, args->s, bsd_args.level, 1584 name, &xu, UIO_SYSSPACE, &xulen); 1585 if (error) 1586 return (error); 1587 /* 1588 * XXX Use 0 for pid as the FreeBSD does not cache peer pid. 1589 */ 1590 lxu.pid = 0; 1591 lxu.uid = xu.cr_uid; 1592 lxu.gid = xu.cr_gid; 1593 return (copyout(&lxu, PTRIN(args->optval), sizeof(lxu))); 1594 /* NOTREACHED */ 1595 break; 1596 default: 1597 break; 1598 } 1599 break; 1600 case IPPROTO_IP: 1601 name = linux_to_bsd_ip_sockopt(args->optname); 1602 break; 1603 case IPPROTO_TCP: 1604 name = linux_to_bsd_tcp_sockopt(args->optname); 1605 break; 1606 default: 1607 name = -1; 1608 break; 1609 } 1610 if (name == -1) 1611 return (EINVAL); 1612 1613 bsd_args.name = name; 1614 bsd_args.val = PTRIN(args->optval); 1615 bsd_args.avalsize = PTRIN(args->optlen); 1616 1617 if (name == IPV6_NEXTHOP) { 1618 error = sys_getsockopt(td, &bsd_args); 1619 bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.val); 1620 } else 1621 error = sys_getsockopt(td, &bsd_args); 1622 1623 return (error); 1624 } 1625 1626 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1627 1628 /* Argument list sizes for linux_socketcall */ 1629 1630 #define LINUX_AL(x) ((x) * sizeof(l_ulong)) 1631 1632 static const unsigned char lxs_args[] = { 1633 LINUX_AL(0) /* unused*/, LINUX_AL(3) /* socket */, 1634 LINUX_AL(3) /* bind */, LINUX_AL(3) /* connect */, 1635 LINUX_AL(2) /* listen */, LINUX_AL(3) /* accept */, 1636 LINUX_AL(3) /* getsockname */, LINUX_AL(3) /* getpeername */, 1637 LINUX_AL(4) /* socketpair */, LINUX_AL(4) /* send */, 1638 LINUX_AL(4) /* recv */, LINUX_AL(6) /* sendto */, 1639 LINUX_AL(6) /* recvfrom */, LINUX_AL(2) /* shutdown */, 1640 LINUX_AL(5) /* setsockopt */, LINUX_AL(5) /* getsockopt */, 1641 LINUX_AL(3) /* sendmsg */, LINUX_AL(3) /* recvmsg */, 1642 LINUX_AL(4) /* accept4 */, LINUX_AL(5) /* recvmmsg */, 1643 LINUX_AL(4) /* sendmmsg */ 1644 }; 1645 1646 #define LINUX_AL_SIZE sizeof(lxs_args) / sizeof(lxs_args[0]) - 1 1647 1648 int 1649 linux_socketcall(struct thread *td, struct linux_socketcall_args *args) 1650 { 1651 l_ulong a[6]; 1652 void *arg; 1653 int error; 1654 1655 if (args->what < LINUX_SOCKET || args->what > LINUX_AL_SIZE) 1656 return (EINVAL); 1657 error = copyin(PTRIN(args->args), a, lxs_args[args->what]); 1658 if (error) 1659 return (error); 1660 1661 arg = a; 1662 switch (args->what) { 1663 case LINUX_SOCKET: 1664 return (linux_socket(td, arg)); 1665 case LINUX_BIND: 1666 return (linux_bind(td, arg)); 1667 case LINUX_CONNECT: 1668 return (linux_connect(td, arg)); 1669 case LINUX_LISTEN: 1670 return (linux_listen(td, arg)); 1671 case LINUX_ACCEPT: 1672 return (linux_accept(td, arg)); 1673 case LINUX_GETSOCKNAME: 1674 return (linux_getsockname(td, arg)); 1675 case LINUX_GETPEERNAME: 1676 return (linux_getpeername(td, arg)); 1677 case LINUX_SOCKETPAIR: 1678 return (linux_socketpair(td, arg)); 1679 case LINUX_SEND: 1680 return (linux_send(td, arg)); 1681 case LINUX_RECV: 1682 return (linux_recv(td, arg)); 1683 case LINUX_SENDTO: 1684 return (linux_sendto(td, arg)); 1685 case LINUX_RECVFROM: 1686 return (linux_recvfrom(td, arg)); 1687 case LINUX_SHUTDOWN: 1688 return (linux_shutdown(td, arg)); 1689 case LINUX_SETSOCKOPT: 1690 return (linux_setsockopt(td, arg)); 1691 case LINUX_GETSOCKOPT: 1692 return (linux_getsockopt(td, arg)); 1693 case LINUX_SENDMSG: 1694 return (linux_sendmsg(td, arg)); 1695 case LINUX_RECVMSG: 1696 return (linux_recvmsg(td, arg)); 1697 case LINUX_ACCEPT4: 1698 return (linux_accept4(td, arg)); 1699 case LINUX_RECVMMSG: 1700 return (linux_recvmmsg(td, arg)); 1701 case LINUX_SENDMMSG: 1702 return (linux_sendmmsg(td, arg)); 1703 } 1704 1705 uprintf("LINUX: 'socket' typ=%d not implemented\n", args->what); 1706 return (ENOSYS); 1707 } 1708 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1709