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