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