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; 577 socklen_t size_val; 578 579 size_val = sizeof(optval); 580 error = kern_getsockopt(td, s, IPPROTO_IP, IP_HDRINCL, 581 &optval, UIO_SYSSPACE, &size_val); 582 if (error) 583 return (error); 584 585 return (optval == 0); 586 } 587 588 struct linux_sendto_args { 589 int s; 590 l_uintptr_t msg; 591 int len; 592 int flags; 593 l_uintptr_t to; 594 int tolen; 595 }; 596 597 /* 598 * Updated sendto() when IP_HDRINCL is set: 599 * tweak endian-dependent fields in the IP packet. 600 */ 601 static int 602 linux_sendto_hdrincl(struct thread *td, struct linux_sendto_args *linux_args) 603 { 604 /* 605 * linux_ip_copysize defines how many bytes we should copy 606 * from the beginning of the IP packet before we customize it for BSD. 607 * It should include all the fields we modify (ip_len and ip_off). 608 */ 609 #define linux_ip_copysize 8 610 611 struct ip *packet; 612 struct msghdr msg; 613 struct iovec aiov[1]; 614 int error; 615 616 /* Check that the packet isn't too big or too small. */ 617 if (linux_args->len < linux_ip_copysize || 618 linux_args->len > IP_MAXPACKET) 619 return (EINVAL); 620 621 packet = (struct ip *)malloc(linux_args->len, M_TEMP, M_WAITOK); 622 623 /* Make kernel copy of the packet to be sent */ 624 if ((error = copyin(PTRIN(linux_args->msg), packet, 625 linux_args->len))) 626 goto goout; 627 628 /* Convert fields from Linux to BSD raw IP socket format */ 629 packet->ip_len = linux_args->len; 630 packet->ip_off = ntohs(packet->ip_off); 631 632 /* Prepare the msghdr and iovec structures describing the new packet */ 633 msg.msg_name = PTRIN(linux_args->to); 634 msg.msg_namelen = linux_args->tolen; 635 msg.msg_iov = aiov; 636 msg.msg_iovlen = 1; 637 msg.msg_control = NULL; 638 msg.msg_flags = 0; 639 aiov[0].iov_base = (char *)packet; 640 aiov[0].iov_len = linux_args->len; 641 error = linux_sendit(td, linux_args->s, &msg, linux_args->flags, 642 NULL, UIO_SYSSPACE); 643 goout: 644 free(packet, M_TEMP); 645 return (error); 646 } 647 648 struct linux_socket_args { 649 int domain; 650 int type; 651 int protocol; 652 }; 653 654 static int 655 linux_socket(struct thread *td, struct linux_socket_args *args) 656 { 657 struct socket_args /* { 658 int domain; 659 int type; 660 int protocol; 661 } */ bsd_args; 662 int retval_socket, socket_flags; 663 664 bsd_args.protocol = args->protocol; 665 socket_flags = args->type & ~LINUX_SOCK_TYPE_MASK; 666 if (socket_flags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK)) 667 return (EINVAL); 668 bsd_args.type = args->type & LINUX_SOCK_TYPE_MASK; 669 if (bsd_args.type < 0 || bsd_args.type > LINUX_SOCK_MAX) 670 return (EINVAL); 671 bsd_args.domain = linux_to_bsd_domain(args->domain); 672 if (bsd_args.domain == -1) 673 return (EAFNOSUPPORT); 674 675 retval_socket = sys_socket(td, &bsd_args); 676 if (retval_socket) 677 return (retval_socket); 678 679 retval_socket = linux_set_socket_flags(td, td->td_retval[0], 680 socket_flags); 681 if (retval_socket) { 682 (void)kern_close(td, td->td_retval[0]); 683 goto out; 684 } 685 686 if (bsd_args.type == SOCK_RAW 687 && (bsd_args.protocol == IPPROTO_RAW || bsd_args.protocol == 0) 688 && bsd_args.domain == PF_INET) { 689 /* It's a raw IP socket: set the IP_HDRINCL option. */ 690 int hdrincl; 691 692 hdrincl = 1; 693 /* We ignore any error returned by kern_setsockopt() */ 694 kern_setsockopt(td, td->td_retval[0], IPPROTO_IP, IP_HDRINCL, 695 &hdrincl, UIO_SYSSPACE, sizeof(hdrincl)); 696 } 697 #ifdef INET6 698 /* 699 * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by default 700 * and some apps depend on this. So, set V6ONLY to 0 for Linux apps. 701 * For simplicity we do this unconditionally of the net.inet6.ip6.v6only 702 * sysctl value. 703 */ 704 if (bsd_args.domain == PF_INET6) { 705 int v6only; 706 707 v6only = 0; 708 /* We ignore any error returned by setsockopt() */ 709 kern_setsockopt(td, td->td_retval[0], IPPROTO_IPV6, IPV6_V6ONLY, 710 &v6only, UIO_SYSSPACE, sizeof(v6only)); 711 } 712 #endif 713 714 out: 715 return (retval_socket); 716 } 717 718 struct linux_bind_args { 719 int s; 720 l_uintptr_t name; 721 int namelen; 722 }; 723 724 static int 725 linux_bind(struct thread *td, struct linux_bind_args *args) 726 { 727 struct sockaddr *sa; 728 int error; 729 730 error = linux_getsockaddr(&sa, PTRIN(args->name), 731 args->namelen); 732 if (error) 733 return (error); 734 735 error = kern_bindat(td, AT_FDCWD, args->s, sa); 736 free(sa, M_SONAME); 737 if (error == EADDRNOTAVAIL && args->namelen != sizeof(struct sockaddr_in)) 738 return (EINVAL); 739 return (error); 740 } 741 742 struct linux_connect_args { 743 int s; 744 l_uintptr_t name; 745 int namelen; 746 }; 747 int linux_connect(struct thread *, struct linux_connect_args *); 748 749 int 750 linux_connect(struct thread *td, struct linux_connect_args *args) 751 { 752 cap_rights_t rights; 753 struct socket *so; 754 struct sockaddr *sa; 755 u_int fflag; 756 int error; 757 758 error = linux_getsockaddr(&sa, (struct osockaddr *)PTRIN(args->name), 759 args->namelen); 760 if (error) 761 return (error); 762 763 error = kern_connectat(td, AT_FDCWD, args->s, sa); 764 free(sa, M_SONAME); 765 if (error != EISCONN) 766 return (error); 767 768 /* 769 * Linux doesn't return EISCONN the first time it occurs, 770 * when on a non-blocking socket. Instead it returns the 771 * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD. 772 * 773 * XXXRW: Instead of using fgetsock(), check that it is a 774 * socket and use the file descriptor reference instead of 775 * creating a new one. 776 */ 777 error = fgetsock(td, args->s, cap_rights_init(&rights, CAP_CONNECT), 778 &so, &fflag); 779 if (error == 0) { 780 error = EISCONN; 781 if (fflag & FNONBLOCK) { 782 SOCK_LOCK(so); 783 if (so->so_emuldata == 0) 784 error = so->so_error; 785 so->so_emuldata = (void *)1; 786 SOCK_UNLOCK(so); 787 } 788 fputsock(so); 789 } 790 return (error); 791 } 792 793 struct linux_listen_args { 794 int s; 795 int backlog; 796 }; 797 798 static int 799 linux_listen(struct thread *td, struct linux_listen_args *args) 800 { 801 struct listen_args /* { 802 int s; 803 int backlog; 804 } */ bsd_args; 805 806 bsd_args.s = args->s; 807 bsd_args.backlog = args->backlog; 808 return (sys_listen(td, &bsd_args)); 809 } 810 811 static int 812 linux_accept_common(struct thread *td, int s, l_uintptr_t addr, 813 l_uintptr_t namelen, int flags) 814 { 815 struct accept_args /* { 816 int s; 817 struct sockaddr * __restrict name; 818 socklen_t * __restrict anamelen; 819 } */ bsd_args; 820 int error; 821 822 if (flags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK)) 823 return (EINVAL); 824 825 bsd_args.s = s; 826 /* XXX: */ 827 bsd_args.name = (struct sockaddr * __restrict)PTRIN(addr); 828 bsd_args.anamelen = PTRIN(namelen);/* XXX */ 829 error = sys_accept(td, &bsd_args); 830 bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.name); 831 if (error) { 832 if (error == EFAULT && namelen != sizeof(struct sockaddr_in)) 833 return (EINVAL); 834 return (error); 835 } 836 837 /* 838 * linux appears not to copy flags from the parent socket to the 839 * accepted one, so we must clear the flags in the new descriptor 840 * and apply the requested flags. 841 */ 842 error = kern_fcntl(td, td->td_retval[0], F_SETFL, 0); 843 if (error) 844 goto out; 845 error = linux_set_socket_flags(td, td->td_retval[0], flags); 846 if (error) 847 goto out; 848 if (addr) 849 error = linux_sa_put(PTRIN(addr)); 850 851 out: 852 if (error) { 853 (void)kern_close(td, td->td_retval[0]); 854 td->td_retval[0] = 0; 855 } 856 return (error); 857 } 858 859 struct linux_accept_args { 860 int s; 861 l_uintptr_t addr; 862 l_uintptr_t namelen; 863 }; 864 865 static int 866 linux_accept(struct thread *td, struct linux_accept_args *args) 867 { 868 869 return (linux_accept_common(td, args->s, args->addr, 870 args->namelen, 0)); 871 } 872 873 struct linux_accept4_args { 874 int s; 875 l_uintptr_t addr; 876 l_uintptr_t namelen; 877 int flags; 878 }; 879 880 static int 881 linux_accept4(struct thread *td, struct linux_accept4_args *args) 882 { 883 884 return (linux_accept_common(td, args->s, args->addr, 885 args->namelen, args->flags)); 886 } 887 888 struct linux_getsockname_args { 889 int s; 890 l_uintptr_t addr; 891 l_uintptr_t namelen; 892 }; 893 894 static int 895 linux_getsockname(struct thread *td, struct linux_getsockname_args *args) 896 { 897 struct getsockname_args /* { 898 int fdes; 899 struct sockaddr * __restrict asa; 900 socklen_t * __restrict alen; 901 } */ bsd_args; 902 int error; 903 904 bsd_args.fdes = args->s; 905 /* XXX: */ 906 bsd_args.asa = (struct sockaddr * __restrict)PTRIN(args->addr); 907 bsd_args.alen = PTRIN(args->namelen); /* XXX */ 908 error = sys_getsockname(td, &bsd_args); 909 bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.asa); 910 if (error) 911 return (error); 912 error = linux_sa_put(PTRIN(args->addr)); 913 if (error) 914 return (error); 915 return (0); 916 } 917 918 struct linux_getpeername_args { 919 int s; 920 l_uintptr_t addr; 921 l_uintptr_t namelen; 922 }; 923 924 static int 925 linux_getpeername(struct thread *td, struct linux_getpeername_args *args) 926 { 927 struct getpeername_args /* { 928 int fdes; 929 caddr_t asa; 930 int *alen; 931 } */ bsd_args; 932 int error; 933 934 bsd_args.fdes = args->s; 935 bsd_args.asa = (struct sockaddr *)PTRIN(args->addr); 936 bsd_args.alen = (socklen_t *)PTRIN(args->namelen); 937 error = sys_getpeername(td, &bsd_args); 938 bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.asa); 939 if (error) 940 return (error); 941 error = linux_sa_put(PTRIN(args->addr)); 942 if (error) 943 return (error); 944 return (0); 945 } 946 947 struct linux_socketpair_args { 948 int domain; 949 int type; 950 int protocol; 951 l_uintptr_t rsv; 952 }; 953 954 static int 955 linux_socketpair(struct thread *td, struct linux_socketpair_args *args) 956 { 957 struct socketpair_args /* { 958 int domain; 959 int type; 960 int protocol; 961 int *rsv; 962 } */ bsd_args; 963 int error, socket_flags; 964 int sv[2]; 965 966 bsd_args.domain = linux_to_bsd_domain(args->domain); 967 if (bsd_args.domain != PF_LOCAL) 968 return (EAFNOSUPPORT); 969 970 socket_flags = args->type & ~LINUX_SOCK_TYPE_MASK; 971 if (socket_flags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK)) 972 return (EINVAL); 973 bsd_args.type = args->type & LINUX_SOCK_TYPE_MASK; 974 if (bsd_args.type < 0 || bsd_args.type > LINUX_SOCK_MAX) 975 return (EINVAL); 976 977 if (args->protocol != 0 && args->protocol != PF_UNIX) 978 979 /* 980 * Use of PF_UNIX as protocol argument is not right, 981 * but Linux does it. 982 * Do not map PF_UNIX as its Linux value is identical 983 * to FreeBSD one. 984 */ 985 return (EPROTONOSUPPORT); 986 else 987 bsd_args.protocol = 0; 988 bsd_args.rsv = (int *)PTRIN(args->rsv); 989 error = kern_socketpair(td, bsd_args.domain, bsd_args.type, 990 bsd_args.protocol, sv); 991 if (error) 992 return (error); 993 error = linux_set_socket_flags(td, sv[0], socket_flags); 994 if (error) 995 goto out; 996 error = linux_set_socket_flags(td, sv[1], socket_flags); 997 if (error) 998 goto out; 999 1000 error = copyout(sv, bsd_args.rsv, 2 * sizeof(int)); 1001 1002 out: 1003 if (error) { 1004 (void)kern_close(td, sv[0]); 1005 (void)kern_close(td, sv[1]); 1006 } 1007 return (error); 1008 } 1009 1010 struct linux_send_args { 1011 int s; 1012 l_uintptr_t msg; 1013 int len; 1014 int flags; 1015 }; 1016 1017 static int 1018 linux_send(struct thread *td, struct linux_send_args *args) 1019 { 1020 struct sendto_args /* { 1021 int s; 1022 caddr_t buf; 1023 int len; 1024 int flags; 1025 caddr_t to; 1026 int tolen; 1027 } */ bsd_args; 1028 1029 bsd_args.s = args->s; 1030 bsd_args.buf = (caddr_t)PTRIN(args->msg); 1031 bsd_args.len = args->len; 1032 bsd_args.flags = args->flags; 1033 bsd_args.to = NULL; 1034 bsd_args.tolen = 0; 1035 return sys_sendto(td, &bsd_args); 1036 } 1037 1038 struct linux_recv_args { 1039 int s; 1040 l_uintptr_t msg; 1041 int len; 1042 int flags; 1043 }; 1044 1045 static int 1046 linux_recv(struct thread *td, struct linux_recv_args *args) 1047 { 1048 struct recvfrom_args /* { 1049 int s; 1050 caddr_t buf; 1051 int len; 1052 int flags; 1053 struct sockaddr *from; 1054 socklen_t fromlenaddr; 1055 } */ bsd_args; 1056 1057 bsd_args.s = args->s; 1058 bsd_args.buf = (caddr_t)PTRIN(args->msg); 1059 bsd_args.len = args->len; 1060 bsd_args.flags = linux_to_bsd_msg_flags(args->flags); 1061 bsd_args.from = NULL; 1062 bsd_args.fromlenaddr = 0; 1063 return (sys_recvfrom(td, &bsd_args)); 1064 } 1065 1066 static int 1067 linux_sendto(struct thread *td, struct linux_sendto_args *args) 1068 { 1069 struct msghdr msg; 1070 struct iovec aiov; 1071 int error; 1072 1073 if (linux_check_hdrincl(td, args->s) == 0) 1074 /* IP_HDRINCL set, tweak the packet before sending */ 1075 return (linux_sendto_hdrincl(td, args)); 1076 1077 msg.msg_name = PTRIN(args->to); 1078 msg.msg_namelen = args->tolen; 1079 msg.msg_iov = &aiov; 1080 msg.msg_iovlen = 1; 1081 msg.msg_control = NULL; 1082 msg.msg_flags = 0; 1083 aiov.iov_base = PTRIN(args->msg); 1084 aiov.iov_len = args->len; 1085 error = linux_sendit(td, args->s, &msg, args->flags, NULL, 1086 UIO_USERSPACE); 1087 return (error); 1088 } 1089 1090 struct linux_recvfrom_args { 1091 int s; 1092 l_uintptr_t buf; 1093 int len; 1094 int flags; 1095 l_uintptr_t from; 1096 l_uintptr_t fromlen; 1097 }; 1098 1099 static int 1100 linux_recvfrom(struct thread *td, struct linux_recvfrom_args *args) 1101 { 1102 struct recvfrom_args /* { 1103 int s; 1104 caddr_t buf; 1105 size_t len; 1106 int flags; 1107 struct sockaddr * __restrict from; 1108 socklen_t * __restrict fromlenaddr; 1109 } */ bsd_args; 1110 size_t len; 1111 int error; 1112 1113 if ((error = copyin(PTRIN(args->fromlen), &len, sizeof(size_t)))) 1114 return (error); 1115 1116 bsd_args.s = args->s; 1117 bsd_args.buf = PTRIN(args->buf); 1118 bsd_args.len = args->len; 1119 bsd_args.flags = linux_to_bsd_msg_flags(args->flags); 1120 /* XXX: */ 1121 bsd_args.from = (struct sockaddr * __restrict)PTRIN(args->from); 1122 bsd_args.fromlenaddr = PTRIN(args->fromlen);/* XXX */ 1123 1124 linux_to_bsd_sockaddr((struct sockaddr *)bsd_args.from, len); 1125 error = sys_recvfrom(td, &bsd_args); 1126 bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.from); 1127 1128 if (error) 1129 return (error); 1130 if (args->from) { 1131 error = linux_sa_put((struct osockaddr *) 1132 PTRIN(args->from)); 1133 if (error) 1134 return (error); 1135 } 1136 return (0); 1137 } 1138 1139 struct linux_sendmsg_args { 1140 int s; 1141 l_uintptr_t msg; 1142 int flags; 1143 }; 1144 1145 static int 1146 linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args) 1147 { 1148 struct cmsghdr *cmsg; 1149 struct cmsgcred cmcred; 1150 struct mbuf *control; 1151 struct msghdr msg; 1152 struct l_cmsghdr linux_cmsg; 1153 struct l_cmsghdr *ptr_cmsg; 1154 struct l_msghdr linux_msg; 1155 struct iovec *iov; 1156 socklen_t datalen; 1157 struct sockaddr *sa; 1158 sa_family_t sa_family; 1159 void *data; 1160 int error; 1161 1162 error = copyin(PTRIN(args->msg), &linux_msg, sizeof(linux_msg)); 1163 if (error) 1164 return (error); 1165 1166 /* 1167 * Some Linux applications (ping) define a non-NULL control data 1168 * pointer, but a msg_controllen of 0, which is not allowed in the 1169 * FreeBSD system call interface. NULL the msg_control pointer in 1170 * order to handle this case. This should be checked, but allows the 1171 * Linux ping to work. 1172 */ 1173 if (PTRIN(linux_msg.msg_control) != NULL && linux_msg.msg_controllen == 0) 1174 linux_msg.msg_control = PTROUT(NULL); 1175 1176 error = linux_to_bsd_msghdr(&msg, &linux_msg); 1177 if (error) 1178 return (error); 1179 1180 #ifdef COMPAT_LINUX32 1181 error = linux32_copyiniov(PTRIN(msg.msg_iov), msg.msg_iovlen, 1182 &iov, EMSGSIZE); 1183 #else 1184 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE); 1185 #endif 1186 if (error) 1187 return (error); 1188 1189 control = NULL; 1190 cmsg = NULL; 1191 1192 if ((ptr_cmsg = LINUX_CMSG_FIRSTHDR(&linux_msg)) != NULL) { 1193 error = kern_getsockname(td, args->s, &sa, &datalen); 1194 if (error) 1195 goto bad; 1196 sa_family = sa->sa_family; 1197 free(sa, M_SONAME); 1198 1199 error = ENOBUFS; 1200 cmsg = malloc(CMSG_HDRSZ, M_TEMP, M_WAITOK | M_ZERO); 1201 control = m_get(M_WAITOK, MT_CONTROL); 1202 if (control == NULL) 1203 goto bad; 1204 1205 do { 1206 error = copyin(ptr_cmsg, &linux_cmsg, 1207 sizeof(struct l_cmsghdr)); 1208 if (error) 1209 goto bad; 1210 1211 error = EINVAL; 1212 if (linux_cmsg.cmsg_len < sizeof(struct l_cmsghdr)) 1213 goto bad; 1214 1215 /* 1216 * Now we support only SCM_RIGHTS and SCM_CRED, 1217 * so return EINVAL in any other cmsg_type 1218 */ 1219 cmsg->cmsg_type = 1220 linux_to_bsd_cmsg_type(linux_cmsg.cmsg_type); 1221 cmsg->cmsg_level = 1222 linux_to_bsd_sockopt_level(linux_cmsg.cmsg_level); 1223 if (cmsg->cmsg_type == -1 1224 || cmsg->cmsg_level != SOL_SOCKET) 1225 goto bad; 1226 1227 /* 1228 * Some applications (e.g. pulseaudio) attempt to 1229 * send ancillary data even if the underlying protocol 1230 * doesn't support it which is not allowed in the 1231 * FreeBSD system call interface. 1232 */ 1233 if (sa_family != AF_UNIX) 1234 continue; 1235 1236 data = LINUX_CMSG_DATA(ptr_cmsg); 1237 datalen = linux_cmsg.cmsg_len - L_CMSG_HDRSZ; 1238 1239 switch (cmsg->cmsg_type) 1240 { 1241 case SCM_RIGHTS: 1242 break; 1243 1244 case SCM_CREDS: 1245 data = &cmcred; 1246 datalen = sizeof(cmcred); 1247 1248 /* 1249 * The lower levels will fill in the structure 1250 */ 1251 bzero(data, datalen); 1252 break; 1253 } 1254 1255 cmsg->cmsg_len = CMSG_LEN(datalen); 1256 1257 error = ENOBUFS; 1258 if (!m_append(control, CMSG_HDRSZ, (c_caddr_t)cmsg)) 1259 goto bad; 1260 if (!m_append(control, datalen, (c_caddr_t)data)) 1261 goto bad; 1262 } while ((ptr_cmsg = LINUX_CMSG_NXTHDR(&linux_msg, ptr_cmsg))); 1263 1264 if (m_length(control, NULL) == 0) { 1265 m_freem(control); 1266 control = NULL; 1267 } 1268 } 1269 1270 msg.msg_iov = iov; 1271 msg.msg_flags = 0; 1272 error = linux_sendit(td, args->s, &msg, args->flags, control, 1273 UIO_USERSPACE); 1274 1275 bad: 1276 free(iov, M_IOV); 1277 if (cmsg) 1278 free(cmsg, M_TEMP); 1279 return (error); 1280 } 1281 1282 struct linux_recvmsg_args { 1283 int s; 1284 l_uintptr_t msg; 1285 int flags; 1286 }; 1287 1288 static int 1289 linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args) 1290 { 1291 struct cmsghdr *cm; 1292 struct cmsgcred *cmcred; 1293 struct msghdr msg; 1294 struct l_cmsghdr *linux_cmsg = NULL; 1295 struct l_ucred linux_ucred; 1296 socklen_t datalen, outlen; 1297 struct l_msghdr linux_msg; 1298 struct iovec *iov, *uiov; 1299 struct mbuf *control = NULL; 1300 struct mbuf **controlp; 1301 caddr_t outbuf; 1302 void *data; 1303 int error, i, fd, fds, *fdp; 1304 1305 error = copyin(PTRIN(args->msg), &linux_msg, sizeof(linux_msg)); 1306 if (error) 1307 return (error); 1308 1309 error = linux_to_bsd_msghdr(&msg, &linux_msg); 1310 if (error) 1311 return (error); 1312 1313 #ifdef COMPAT_LINUX32 1314 error = linux32_copyiniov(PTRIN(msg.msg_iov), msg.msg_iovlen, 1315 &iov, EMSGSIZE); 1316 #else 1317 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE); 1318 #endif 1319 if (error) 1320 return (error); 1321 1322 if (msg.msg_name) { 1323 error = linux_to_bsd_sockaddr((struct sockaddr *)msg.msg_name, 1324 msg.msg_namelen); 1325 if (error) 1326 goto bad; 1327 } 1328 1329 uiov = msg.msg_iov; 1330 msg.msg_iov = iov; 1331 controlp = (msg.msg_control != NULL) ? &control : NULL; 1332 error = kern_recvit(td, args->s, &msg, UIO_USERSPACE, controlp); 1333 msg.msg_iov = uiov; 1334 if (error) 1335 goto bad; 1336 1337 error = bsd_to_linux_msghdr(&msg, &linux_msg); 1338 if (error) 1339 goto bad; 1340 1341 if (linux_msg.msg_name) { 1342 error = bsd_to_linux_sockaddr((struct sockaddr *) 1343 PTRIN(linux_msg.msg_name)); 1344 if (error) 1345 goto bad; 1346 } 1347 if (linux_msg.msg_name && linux_msg.msg_namelen > 2) { 1348 error = linux_sa_put(PTRIN(linux_msg.msg_name)); 1349 if (error) 1350 goto bad; 1351 } 1352 1353 outbuf = PTRIN(linux_msg.msg_control); 1354 outlen = 0; 1355 1356 if (control) { 1357 linux_cmsg = malloc(L_CMSG_HDRSZ, M_TEMP, M_WAITOK | M_ZERO); 1358 1359 msg.msg_control = mtod(control, struct cmsghdr *); 1360 msg.msg_controllen = control->m_len; 1361 1362 cm = CMSG_FIRSTHDR(&msg); 1363 1364 while (cm != NULL) { 1365 linux_cmsg->cmsg_type = 1366 bsd_to_linux_cmsg_type(cm->cmsg_type); 1367 linux_cmsg->cmsg_level = 1368 bsd_to_linux_sockopt_level(cm->cmsg_level); 1369 if (linux_cmsg->cmsg_type == -1 1370 || cm->cmsg_level != SOL_SOCKET) 1371 { 1372 error = EINVAL; 1373 goto bad; 1374 } 1375 1376 data = CMSG_DATA(cm); 1377 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 1378 1379 switch (cm->cmsg_type) 1380 { 1381 case SCM_RIGHTS: 1382 if (args->flags & LINUX_MSG_CMSG_CLOEXEC) { 1383 fds = datalen / sizeof(int); 1384 fdp = data; 1385 for (i = 0; i < fds; i++) { 1386 fd = *fdp++; 1387 (void)kern_fcntl(td, fd, 1388 F_SETFD, FD_CLOEXEC); 1389 } 1390 } 1391 break; 1392 1393 case SCM_CREDS: 1394 /* 1395 * Currently LOCAL_CREDS is never in 1396 * effect for Linux so no need to worry 1397 * about sockcred 1398 */ 1399 if (datalen != sizeof(*cmcred)) { 1400 error = EMSGSIZE; 1401 goto bad; 1402 } 1403 cmcred = (struct cmsgcred *)data; 1404 bzero(&linux_ucred, sizeof(linux_ucred)); 1405 linux_ucred.pid = cmcred->cmcred_pid; 1406 linux_ucred.uid = cmcred->cmcred_uid; 1407 linux_ucred.gid = cmcred->cmcred_gid; 1408 data = &linux_ucred; 1409 datalen = sizeof(linux_ucred); 1410 break; 1411 } 1412 1413 if (outlen + LINUX_CMSG_LEN(datalen) > 1414 linux_msg.msg_controllen) { 1415 if (outlen == 0) { 1416 error = EMSGSIZE; 1417 goto bad; 1418 } else { 1419 linux_msg.msg_flags |= 1420 LINUX_MSG_CTRUNC; 1421 goto out; 1422 } 1423 } 1424 1425 linux_cmsg->cmsg_len = LINUX_CMSG_LEN(datalen); 1426 1427 error = copyout(linux_cmsg, outbuf, L_CMSG_HDRSZ); 1428 if (error) 1429 goto bad; 1430 outbuf += L_CMSG_HDRSZ; 1431 1432 error = copyout(data, outbuf, datalen); 1433 if (error) 1434 goto bad; 1435 1436 outbuf += LINUX_CMSG_ALIGN(datalen); 1437 outlen += LINUX_CMSG_LEN(datalen); 1438 1439 cm = CMSG_NXTHDR(&msg, cm); 1440 } 1441 } 1442 1443 out: 1444 linux_msg.msg_controllen = outlen; 1445 error = copyout(&linux_msg, PTRIN(args->msg), sizeof(linux_msg)); 1446 1447 bad: 1448 free(iov, M_IOV); 1449 m_freem(control); 1450 free(linux_cmsg, M_TEMP); 1451 1452 return (error); 1453 } 1454 1455 struct linux_shutdown_args { 1456 int s; 1457 int how; 1458 }; 1459 1460 static int 1461 linux_shutdown(struct thread *td, struct linux_shutdown_args *args) 1462 { 1463 struct shutdown_args /* { 1464 int s; 1465 int how; 1466 } */ bsd_args; 1467 1468 bsd_args.s = args->s; 1469 bsd_args.how = args->how; 1470 return (sys_shutdown(td, &bsd_args)); 1471 } 1472 1473 struct linux_setsockopt_args { 1474 int s; 1475 int level; 1476 int optname; 1477 l_uintptr_t optval; 1478 int optlen; 1479 }; 1480 1481 static int 1482 linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args) 1483 { 1484 struct setsockopt_args /* { 1485 int s; 1486 int level; 1487 int name; 1488 caddr_t val; 1489 int valsize; 1490 } */ bsd_args; 1491 l_timeval linux_tv; 1492 struct timeval tv; 1493 int error, name; 1494 1495 bsd_args.s = args->s; 1496 bsd_args.level = linux_to_bsd_sockopt_level(args->level); 1497 switch (bsd_args.level) { 1498 case SOL_SOCKET: 1499 name = linux_to_bsd_so_sockopt(args->optname); 1500 switch (name) { 1501 case SO_RCVTIMEO: 1502 /* FALLTHROUGH */ 1503 case SO_SNDTIMEO: 1504 error = copyin(PTRIN(args->optval), &linux_tv, 1505 sizeof(linux_tv)); 1506 if (error) 1507 return (error); 1508 tv.tv_sec = linux_tv.tv_sec; 1509 tv.tv_usec = linux_tv.tv_usec; 1510 return (kern_setsockopt(td, args->s, bsd_args.level, 1511 name, &tv, UIO_SYSSPACE, sizeof(tv))); 1512 /* NOTREACHED */ 1513 break; 1514 default: 1515 break; 1516 } 1517 break; 1518 case IPPROTO_IP: 1519 name = linux_to_bsd_ip_sockopt(args->optname); 1520 break; 1521 case IPPROTO_TCP: 1522 name = linux_to_bsd_tcp_sockopt(args->optname); 1523 break; 1524 default: 1525 name = -1; 1526 break; 1527 } 1528 if (name == -1) 1529 return (ENOPROTOOPT); 1530 1531 bsd_args.name = name; 1532 bsd_args.val = PTRIN(args->optval); 1533 bsd_args.valsize = args->optlen; 1534 1535 if (name == IPV6_NEXTHOP) { 1536 linux_to_bsd_sockaddr((struct sockaddr *)bsd_args.val, 1537 bsd_args.valsize); 1538 error = sys_setsockopt(td, &bsd_args); 1539 bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.val); 1540 } else 1541 error = sys_setsockopt(td, &bsd_args); 1542 1543 return (error); 1544 } 1545 1546 struct linux_getsockopt_args { 1547 int s; 1548 int level; 1549 int optname; 1550 l_uintptr_t optval; 1551 l_uintptr_t optlen; 1552 }; 1553 1554 static int 1555 linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args) 1556 { 1557 struct getsockopt_args /* { 1558 int s; 1559 int level; 1560 int name; 1561 caddr_t val; 1562 int *avalsize; 1563 } */ bsd_args; 1564 l_timeval linux_tv; 1565 struct timeval tv; 1566 socklen_t tv_len, xulen; 1567 struct xucred xu; 1568 struct l_ucred lxu; 1569 int error, name; 1570 1571 bsd_args.s = args->s; 1572 bsd_args.level = linux_to_bsd_sockopt_level(args->level); 1573 switch (bsd_args.level) { 1574 case SOL_SOCKET: 1575 name = linux_to_bsd_so_sockopt(args->optname); 1576 switch (name) { 1577 case SO_RCVTIMEO: 1578 /* FALLTHROUGH */ 1579 case SO_SNDTIMEO: 1580 tv_len = sizeof(tv); 1581 error = kern_getsockopt(td, args->s, bsd_args.level, 1582 name, &tv, UIO_SYSSPACE, &tv_len); 1583 if (error) 1584 return (error); 1585 linux_tv.tv_sec = tv.tv_sec; 1586 linux_tv.tv_usec = tv.tv_usec; 1587 return (copyout(&linux_tv, PTRIN(args->optval), 1588 sizeof(linux_tv))); 1589 /* NOTREACHED */ 1590 break; 1591 case LOCAL_PEERCRED: 1592 if (args->optlen != sizeof(lxu)) 1593 return (EINVAL); 1594 xulen = sizeof(xu); 1595 error = kern_getsockopt(td, args->s, bsd_args.level, 1596 name, &xu, UIO_SYSSPACE, &xulen); 1597 if (error) 1598 return (error); 1599 /* 1600 * XXX Use 0 for pid as the FreeBSD does not cache peer pid. 1601 */ 1602 lxu.pid = 0; 1603 lxu.uid = xu.cr_uid; 1604 lxu.gid = xu.cr_gid; 1605 return (copyout(&lxu, PTRIN(args->optval), sizeof(lxu))); 1606 /* NOTREACHED */ 1607 break; 1608 default: 1609 break; 1610 } 1611 break; 1612 case IPPROTO_IP: 1613 name = linux_to_bsd_ip_sockopt(args->optname); 1614 break; 1615 case IPPROTO_TCP: 1616 name = linux_to_bsd_tcp_sockopt(args->optname); 1617 break; 1618 default: 1619 name = -1; 1620 break; 1621 } 1622 if (name == -1) 1623 return (EINVAL); 1624 1625 bsd_args.name = name; 1626 bsd_args.val = PTRIN(args->optval); 1627 bsd_args.avalsize = PTRIN(args->optlen); 1628 1629 if (name == IPV6_NEXTHOP) { 1630 error = sys_getsockopt(td, &bsd_args); 1631 bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.val); 1632 } else 1633 error = sys_getsockopt(td, &bsd_args); 1634 1635 return (error); 1636 } 1637 1638 /* Argument list sizes for linux_socketcall */ 1639 1640 #define LINUX_AL(x) ((x) * sizeof(l_ulong)) 1641 1642 static const unsigned char lxs_args[] = { 1643 LINUX_AL(0) /* unused*/, LINUX_AL(3) /* socket */, 1644 LINUX_AL(3) /* bind */, LINUX_AL(3) /* connect */, 1645 LINUX_AL(2) /* listen */, LINUX_AL(3) /* accept */, 1646 LINUX_AL(3) /* getsockname */, LINUX_AL(3) /* getpeername */, 1647 LINUX_AL(4) /* socketpair */, LINUX_AL(4) /* send */, 1648 LINUX_AL(4) /* recv */, LINUX_AL(6) /* sendto */, 1649 LINUX_AL(6) /* recvfrom */, LINUX_AL(2) /* shutdown */, 1650 LINUX_AL(5) /* setsockopt */, LINUX_AL(5) /* getsockopt */, 1651 LINUX_AL(3) /* sendmsg */, LINUX_AL(3) /* recvmsg */, 1652 LINUX_AL(4) /* accept4 */ 1653 }; 1654 1655 #define LINUX_AL_SIZE sizeof(lxs_args) / sizeof(lxs_args[0]) - 1 1656 1657 int 1658 linux_socketcall(struct thread *td, struct linux_socketcall_args *args) 1659 { 1660 l_ulong a[6]; 1661 void *arg; 1662 int error; 1663 1664 if (args->what < LINUX_SOCKET || args->what > LINUX_AL_SIZE) 1665 return (EINVAL); 1666 error = copyin(PTRIN(args->args), a, lxs_args[args->what]); 1667 if (error) 1668 return (error); 1669 1670 arg = a; 1671 switch (args->what) { 1672 case LINUX_SOCKET: 1673 return (linux_socket(td, arg)); 1674 case LINUX_BIND: 1675 return (linux_bind(td, arg)); 1676 case LINUX_CONNECT: 1677 return (linux_connect(td, arg)); 1678 case LINUX_LISTEN: 1679 return (linux_listen(td, arg)); 1680 case LINUX_ACCEPT: 1681 return (linux_accept(td, arg)); 1682 case LINUX_GETSOCKNAME: 1683 return (linux_getsockname(td, arg)); 1684 case LINUX_GETPEERNAME: 1685 return (linux_getpeername(td, arg)); 1686 case LINUX_SOCKETPAIR: 1687 return (linux_socketpair(td, arg)); 1688 case LINUX_SEND: 1689 return (linux_send(td, arg)); 1690 case LINUX_RECV: 1691 return (linux_recv(td, arg)); 1692 case LINUX_SENDTO: 1693 return (linux_sendto(td, arg)); 1694 case LINUX_RECVFROM: 1695 return (linux_recvfrom(td, arg)); 1696 case LINUX_SHUTDOWN: 1697 return (linux_shutdown(td, arg)); 1698 case LINUX_SETSOCKOPT: 1699 return (linux_setsockopt(td, arg)); 1700 case LINUX_GETSOCKOPT: 1701 return (linux_getsockopt(td, arg)); 1702 case LINUX_SENDMSG: 1703 return (linux_sendmsg(td, arg)); 1704 case LINUX_RECVMSG: 1705 return (linux_recvmsg(td, arg)); 1706 case LINUX_ACCEPT4: 1707 return (linux_accept4(td, arg)); 1708 } 1709 1710 uprintf("LINUX: 'socket' typ=%d not implemented\n", args->what); 1711 return (ENOSYS); 1712 } 1713