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