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