1 /* $NetBSD: linux_socket.c,v 1.142 2018/05/10 01:32:24 ozaki-r Exp $ */ 2 3 /*- 4 * Copyright (c) 1995, 1998, 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Frank van der Linden and Eric Haszlakiewicz. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Functions in multiarch: 34 * linux_sys_socketcall : linux_socketcall.c 35 */ 36 37 #include <sys/cdefs.h> 38 __KERNEL_RCSID(0, "$NetBSD: linux_socket.c,v 1.142 2018/05/10 01:32:24 ozaki-r Exp $"); 39 40 #if defined(_KERNEL_OPT) 41 #include "opt_inet.h" 42 #endif /* defined(_KERNEL_OPT) */ 43 44 #include <sys/param.h> 45 #include <sys/kernel.h> 46 #include <sys/systm.h> 47 #include <sys/buf.h> 48 #include <sys/ioctl.h> 49 #include <sys/tty.h> 50 #include <sys/file.h> 51 #include <sys/filedesc.h> 52 #include <sys/select.h> 53 #include <sys/socket.h> 54 #include <sys/socketvar.h> 55 #include <sys/domain.h> 56 #include <net/if.h> 57 #include <net/if_dl.h> 58 #include <net/if_types.h> 59 #include <netinet/in.h> 60 #include <netinet/tcp.h> 61 #include <sys/mount.h> 62 #include <sys/proc.h> 63 #include <sys/vnode.h> 64 #include <sys/device.h> 65 #include <sys/protosw.h> 66 #include <sys/mbuf.h> 67 #include <sys/syslog.h> 68 #include <sys/exec.h> 69 #include <sys/kauth.h> 70 #include <sys/syscallargs.h> 71 #include <sys/ktrace.h> 72 73 #include <lib/libkern/libkern.h> 74 75 #include <netinet/ip6.h> 76 #include <netinet6/ip6_var.h> 77 78 #include <compat/sys/socket.h> 79 #include <compat/sys/sockio.h> 80 81 #include <compat/linux/common/linux_types.h> 82 #include <compat/linux/common/linux_util.h> 83 #include <compat/linux/common/linux_signal.h> 84 #include <compat/linux/common/linux_ioctl.h> 85 #include <compat/linux/common/linux_sched.h> 86 #include <compat/linux/common/linux_socket.h> 87 #include <compat/linux/common/linux_fcntl.h> 88 #if !defined(__alpha__) && !defined(__amd64__) 89 #include <compat/linux/common/linux_socketcall.h> 90 #endif 91 #include <compat/linux/common/linux_sockio.h> 92 #include <compat/linux/common/linux_ipc.h> 93 #include <compat/linux/common/linux_sem.h> 94 95 #include <compat/linux/linux_syscallargs.h> 96 97 #ifdef DEBUG_LINUX 98 #define DPRINTF(a) uprintf a 99 #else 100 #define DPRINTF(a) 101 #endif 102 103 /* 104 * The calls in this file are entered either via the linux_socketcall() 105 * interface or, on the Alpha, as individual syscalls. The 106 * linux_socketcall function does any massaging of arguments so that all 107 * the calls in here need not think that they are anything other 108 * than a normal syscall. 109 */ 110 111 static int linux_to_bsd_domain(int); 112 static int bsd_to_linux_domain(int); 113 static int linux_to_bsd_type(int); 114 int linux_to_bsd_sopt_level(int); 115 int linux_to_bsd_so_sockopt(int); 116 int linux_to_bsd_ip_sockopt(int); 117 int linux_to_bsd_ipv6_sockopt(int); 118 int linux_to_bsd_tcp_sockopt(int); 119 int linux_to_bsd_udp_sockopt(int); 120 int linux_getifname(struct lwp *, register_t *, void *); 121 int linux_getifconf(struct lwp *, register_t *, void *); 122 int linux_getifhwaddr(struct lwp *, register_t *, u_int, void *); 123 static int linux_get_sa(struct lwp *, int, struct sockaddr_big *, 124 const struct osockaddr *, socklen_t); 125 static int linux_sa_put(struct osockaddr *osa); 126 static int linux_to_bsd_msg_flags(int); 127 static int bsd_to_linux_msg_flags(int); 128 static void linux_to_bsd_msghdr(const struct linux_msghdr *, struct msghdr *); 129 static void bsd_to_linux_msghdr(const struct msghdr *, struct linux_msghdr *); 130 131 static const int linux_to_bsd_domain_[LINUX_AF_MAX] = { 132 AF_UNSPEC, 133 AF_UNIX, 134 AF_INET, 135 AF_CCITT, /* LINUX_AF_AX25 */ 136 AF_IPX, 137 AF_APPLETALK, 138 -1, /* LINUX_AF_NETROM */ 139 -1, /* LINUX_AF_BRIDGE */ 140 -1, /* LINUX_AF_ATMPVC */ 141 AF_CCITT, /* LINUX_AF_X25 */ 142 AF_INET6, 143 -1, /* LINUX_AF_ROSE */ 144 AF_DECnet, 145 -1, /* LINUX_AF_NETBEUI */ 146 -1, /* LINUX_AF_SECURITY */ 147 pseudo_AF_KEY, 148 AF_ROUTE, /* LINUX_AF_NETLINK */ 149 -1, /* LINUX_AF_PACKET */ 150 -1, /* LINUX_AF_ASH */ 151 -1, /* LINUX_AF_ECONET */ 152 -1, /* LINUX_AF_ATMSVC */ 153 AF_SNA, 154 /* rest up to LINUX_AF_MAX-1 is not allocated */ 155 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 156 }; 157 158 static const int bsd_to_linux_domain_[AF_MAX] = { 159 LINUX_AF_UNSPEC, 160 LINUX_AF_UNIX, 161 LINUX_AF_INET, 162 -1, /* AF_IMPLINK */ 163 -1, /* AF_PUP */ 164 -1, /* AF_CHAOS */ 165 -1, /* AF_NS */ 166 -1, /* AF_ISO */ 167 -1, /* AF_ECMA */ 168 -1, /* AF_DATAKIT */ 169 LINUX_AF_AX25, /* AF_CCITT */ 170 LINUX_AF_SNA, 171 LINUX_AF_DECnet, 172 -1, /* AF_DLI */ 173 -1, /* AF_LAT */ 174 -1, /* AF_HYLINK */ 175 LINUX_AF_APPLETALK, 176 LINUX_AF_NETLINK, 177 -1, /* AF_LINK */ 178 -1, /* AF_XTP */ 179 -1, /* AF_COIP */ 180 -1, /* AF_CNT */ 181 -1, /* pseudo_AF_RTIP */ 182 LINUX_AF_IPX, 183 LINUX_AF_INET6, 184 -1, /* pseudo_AF_PIP */ 185 -1, /* AF_ISDN */ 186 -1, /* AF_NATM */ 187 -1, /* AF_ARP */ 188 LINUX_pseudo_AF_KEY, 189 -1, /* pseudo_AF_HDRCMPLT */ 190 }; 191 192 static const struct { 193 int bfl; 194 int lfl; 195 } bsd_to_linux_msg_flags_[] = { 196 {MSG_OOB, LINUX_MSG_OOB}, 197 {MSG_PEEK, LINUX_MSG_PEEK}, 198 {MSG_DONTROUTE, LINUX_MSG_DONTROUTE}, 199 {MSG_EOR, LINUX_MSG_EOR}, 200 {MSG_TRUNC, LINUX_MSG_TRUNC}, 201 {MSG_CTRUNC, LINUX_MSG_CTRUNC}, 202 {MSG_WAITALL, LINUX_MSG_WAITALL}, 203 {MSG_DONTWAIT, LINUX_MSG_DONTWAIT}, 204 {MSG_BCAST, 0}, /* not supported, clear */ 205 {MSG_MCAST, 0}, /* not supported, clear */ 206 {MSG_NOSIGNAL, LINUX_MSG_NOSIGNAL}, 207 {-1, /* not supp */ LINUX_MSG_PROBE}, 208 {-1, /* not supp */ LINUX_MSG_FIN}, 209 {-1, /* not supp */ LINUX_MSG_SYN}, 210 {-1, /* not supp */ LINUX_MSG_CONFIRM}, 211 {-1, /* not supp */ LINUX_MSG_RST}, 212 {-1, /* not supp */ LINUX_MSG_ERRQUEUE}, 213 {-1, /* not supp */ LINUX_MSG_MORE}, 214 }; 215 216 /* 217 * Convert between Linux and BSD socket domain values 218 */ 219 static int 220 linux_to_bsd_domain(int ldom) 221 { 222 if (ldom < 0 || ldom >= LINUX_AF_MAX) 223 return (-1); 224 225 return linux_to_bsd_domain_[ldom]; 226 } 227 228 /* 229 * Convert between BSD and Linux socket domain values 230 */ 231 static int 232 bsd_to_linux_domain(int bdom) 233 { 234 if (bdom < 0 || bdom >= AF_MAX) 235 return (-1); 236 237 return bsd_to_linux_domain_[bdom]; 238 } 239 240 static int 241 linux_to_bsd_type(int ltype) 242 { 243 int type, flags; 244 245 /* Real types are identical between Linux and NetBSD */ 246 type = ltype & LINUX_SOCK_TYPE_MASK; 247 248 /* But flags are not .. */ 249 flags = ltype & ~LINUX_SOCK_TYPE_MASK; 250 if (flags & ~(LINUX_SOCK_CLOEXEC|LINUX_SOCK_NONBLOCK)) 251 return -1; 252 253 if (flags & LINUX_SOCK_CLOEXEC) 254 type |= SOCK_CLOEXEC; 255 if (flags & LINUX_SOCK_NONBLOCK) 256 type |= SOCK_NONBLOCK; 257 258 return type; 259 } 260 261 static int 262 linux_to_bsd_msg_flags(int lflag) 263 { 264 int i, lfl, bfl; 265 int bflag = 0; 266 267 if (lflag == 0) 268 return (0); 269 270 for(i = 0; i < __arraycount(bsd_to_linux_msg_flags_); i++) { 271 bfl = bsd_to_linux_msg_flags_[i].bfl; 272 lfl = bsd_to_linux_msg_flags_[i].lfl; 273 274 if (lfl == 0) 275 continue; 276 277 if (lflag & lfl) { 278 if (bfl < 0) 279 return (-1); 280 281 bflag |= bfl; 282 } 283 } 284 285 return (bflag); 286 } 287 288 static int 289 bsd_to_linux_msg_flags(int bflag) 290 { 291 int i, lfl, bfl; 292 int lflag = 0; 293 294 if (bflag == 0) 295 return (0); 296 297 for(i = 0; i < __arraycount(bsd_to_linux_msg_flags_); i++) { 298 bfl = bsd_to_linux_msg_flags_[i].bfl; 299 lfl = bsd_to_linux_msg_flags_[i].lfl; 300 301 if (bfl <= 0) 302 continue; 303 304 if (bflag & bfl) { 305 if (lfl < 0) 306 return (-1); 307 308 lflag |= lfl; 309 } 310 } 311 312 return (lflag); 313 } 314 315 int 316 linux_sys_socket(struct lwp *l, const struct linux_sys_socket_args *uap, register_t *retval) 317 { 318 /* { 319 syscallarg(int) domain; 320 syscallarg(int) type; 321 syscallarg(int) protocol; 322 } */ 323 struct sys___socket30_args bsa; 324 int error; 325 326 327 SCARG(&bsa, protocol) = SCARG(uap, protocol); 328 SCARG(&bsa, domain) = linux_to_bsd_domain(SCARG(uap, domain)); 329 if (SCARG(&bsa, domain) == -1) 330 return EINVAL; 331 SCARG(&bsa, type) = linux_to_bsd_type(SCARG(uap, type)); 332 if (SCARG(&bsa, type) == -1) 333 return EINVAL; 334 /* 335 * Apparently linux uses this to talk to ISDN sockets. If we fail 336 * now programs seems to handle it, but if we don't we are going 337 * to fail when we bind and programs don't handle this well. 338 */ 339 if (SCARG(&bsa, domain) == AF_ROUTE && SCARG(&bsa, type) == SOCK_RAW) 340 return ENOTSUP; 341 error = sys___socket30(l, &bsa, retval); 342 343 #ifdef INET6 344 /* 345 * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by 346 * default and some apps depend on this. So, set V6ONLY to 0 347 * for Linux apps if the sysctl value is set to 1. 348 */ 349 if (!error && ip6_v6only && SCARG(&bsa, domain) == PF_INET6) { 350 struct socket *so; 351 352 if (fd_getsock(*retval, &so) == 0) { 353 int val = 0; 354 355 /* ignore error */ 356 (void)so_setsockopt(l, so, IPPROTO_IPV6, IPV6_V6ONLY, 357 &val, sizeof(val)); 358 359 fd_putfile(*retval); 360 } 361 } 362 #endif 363 364 return (error); 365 } 366 367 int 368 linux_sys_socketpair(struct lwp *l, const struct linux_sys_socketpair_args *uap, register_t *retval) 369 { 370 /* { 371 syscallarg(int) domain; 372 syscallarg(int) type; 373 syscallarg(int) protocol; 374 syscallarg(int *) rsv; 375 } */ 376 struct sys_socketpair_args bsa; 377 378 SCARG(&bsa, domain) = linux_to_bsd_domain(SCARG(uap, domain)); 379 if (SCARG(&bsa, domain) == -1) 380 return EINVAL; 381 SCARG(&bsa, type) = linux_to_bsd_type(SCARG(uap, type)); 382 if (SCARG(&bsa, type) == -1) 383 return EINVAL; 384 SCARG(&bsa, protocol) = SCARG(uap, protocol); 385 SCARG(&bsa, rsv) = SCARG(uap, rsv); 386 387 return sys_socketpair(l, &bsa, retval); 388 } 389 390 int 391 linux_sys_sendto(struct lwp *l, const struct linux_sys_sendto_args *uap, register_t *retval) 392 { 393 /* { 394 syscallarg(int) s; 395 syscallarg(void *) msg; 396 syscallarg(int) len; 397 syscallarg(int) flags; 398 syscallarg(struct osockaddr *) to; 399 syscallarg(int) tolen; 400 } */ 401 struct msghdr msg; 402 struct iovec aiov; 403 struct sockaddr_big nam; 404 struct mbuf *m; 405 int bflags; 406 int error; 407 408 /* Translate message flags. */ 409 bflags = linux_to_bsd_msg_flags(SCARG(uap, flags)); 410 if (bflags < 0) 411 /* Some supported flag */ 412 return EINVAL; 413 414 msg.msg_flags = 0; 415 msg.msg_name = NULL; 416 msg.msg_control = NULL; 417 418 if (SCARG(uap, tolen)) { 419 /* Read in and convert the sockaddr */ 420 error = linux_get_sa(l, SCARG(uap, s), &nam, SCARG(uap, to), 421 SCARG(uap, tolen)); 422 if (error) 423 return error; 424 error = sockargs(&m, &nam, nam.sb_len, UIO_SYSSPACE, MT_SONAME); 425 if (error) 426 return error; 427 msg.msg_flags |= MSG_NAMEMBUF; 428 msg.msg_name = m; 429 msg.msg_namelen = nam.sb_len; 430 } 431 432 msg.msg_iov = &aiov; 433 msg.msg_iovlen = 1; 434 aiov.iov_base = __UNCONST(SCARG(uap, msg)); 435 aiov.iov_len = SCARG(uap, len); 436 437 return do_sys_sendmsg(l, SCARG(uap, s), &msg, bflags, retval); 438 } 439 440 static void 441 linux_to_bsd_msghdr(const struct linux_msghdr *lmsg, struct msghdr *bmsg) 442 { 443 bmsg->msg_name = lmsg->msg_name; 444 bmsg->msg_namelen = lmsg->msg_namelen; 445 bmsg->msg_iov = lmsg->msg_iov; 446 bmsg->msg_iovlen = lmsg->msg_iovlen; 447 bmsg->msg_control = lmsg->msg_control; 448 bmsg->msg_controllen = lmsg->msg_controllen; 449 bmsg->msg_flags = lmsg->msg_flags; 450 } 451 452 static void 453 bsd_to_linux_msghdr(const struct msghdr *bmsg, struct linux_msghdr *lmsg) 454 { 455 lmsg->msg_name = bmsg->msg_name; 456 lmsg->msg_namelen = bmsg->msg_namelen; 457 lmsg->msg_iov = bmsg->msg_iov; 458 lmsg->msg_iovlen = bmsg->msg_iovlen; 459 lmsg->msg_control = bmsg->msg_control; 460 lmsg->msg_controllen = bmsg->msg_controllen; 461 lmsg->msg_flags = bmsg->msg_flags; 462 } 463 464 int 465 linux_sys_sendmsg(struct lwp *l, const struct linux_sys_sendmsg_args *uap, register_t *retval) 466 { 467 /* { 468 syscallarg(int) s; 469 syscallarg(struct linux_msghdr *) msg; 470 syscallarg(u_int) flags; 471 } */ 472 struct msghdr msg; 473 struct linux_msghdr lmsg; 474 int error; 475 int bflags; 476 struct sockaddr_big nam; 477 u_int8_t *control; 478 struct mbuf *ctl_mbuf = NULL; 479 480 error = copyin(SCARG(uap, msg), &lmsg, sizeof(lmsg)); 481 if (error) 482 return error; 483 linux_to_bsd_msghdr(&lmsg, &msg); 484 485 msg.msg_flags = MSG_IOVUSRSPACE; 486 487 /* 488 * Translate message flags. 489 */ 490 bflags = linux_to_bsd_msg_flags(SCARG(uap, flags)); 491 if (bflags < 0) 492 /* Some supported flag */ 493 return EINVAL; 494 495 if (lmsg.msg_name) { 496 /* Read in and convert the sockaddr */ 497 error = linux_get_sa(l, SCARG(uap, s), &nam, msg.msg_name, 498 msg.msg_namelen); 499 if (error) 500 return (error); 501 msg.msg_name = &nam; 502 } 503 504 /* 505 * Handle cmsg if there is any. 506 */ 507 if (LINUX_CMSG_FIRSTHDR(&lmsg)) { 508 struct linux_cmsghdr l_cmsg, *l_cc; 509 struct cmsghdr *cmsg; 510 ssize_t resid = msg.msg_controllen; 511 size_t clen, cidx = 0, cspace; 512 513 ctl_mbuf = m_get(M_WAIT, MT_CONTROL); 514 clen = MLEN; 515 control = mtod(ctl_mbuf, void *); 516 517 l_cc = LINUX_CMSG_FIRSTHDR(&lmsg); 518 do { 519 error = copyin(l_cc, &l_cmsg, sizeof(l_cmsg)); 520 if (error) 521 goto done; 522 523 /* 524 * Sanity check the control message length. 525 */ 526 if (l_cmsg.cmsg_len > resid 527 || l_cmsg.cmsg_len < sizeof l_cmsg) { 528 error = EINVAL; 529 goto done; 530 } 531 532 /* 533 * Refuse unsupported control messages, and 534 * translate fields as appropriate. 535 */ 536 switch (l_cmsg.cmsg_level) { 537 case LINUX_SOL_SOCKET: 538 /* It only differs on some archs */ 539 if (LINUX_SOL_SOCKET != SOL_SOCKET) 540 l_cmsg.cmsg_level = SOL_SOCKET; 541 542 switch(l_cmsg.cmsg_type) { 543 case LINUX_SCM_RIGHTS: 544 /* Linux SCM_RIGHTS is same as NetBSD */ 545 break; 546 547 case LINUX_SCM_CREDENTIALS: 548 /* no native equivalent, just drop it */ 549 m_free(ctl_mbuf); 550 ctl_mbuf = NULL; 551 msg.msg_control = NULL; 552 msg.msg_controllen = 0; 553 goto skipcmsg; 554 555 default: 556 /* other types not supported */ 557 error = EINVAL; 558 goto done; 559 } 560 break; 561 default: 562 /* pray and leave intact */ 563 break; 564 } 565 566 cspace = CMSG_SPACE(l_cmsg.cmsg_len - sizeof(l_cmsg)); 567 568 /* Check the buffer is big enough */ 569 if (__predict_false(cidx + cspace > clen)) { 570 u_int8_t *nc; 571 572 clen = cidx + cspace; 573 if (clen >= PAGE_SIZE) { 574 error = EINVAL; 575 goto done; 576 } 577 nc = realloc(clen <= MLEN ? NULL : control, 578 clen, M_TEMP, M_WAITOK); 579 if (!nc) { 580 error = ENOMEM; 581 goto done; 582 } 583 if (cidx <= MLEN) 584 /* Old buffer was in mbuf... */ 585 memcpy(nc, control, cidx); 586 control = nc; 587 } 588 589 /* Copy header */ 590 cmsg = (void *)&control[cidx]; 591 cmsg->cmsg_len = l_cmsg.cmsg_len + LINUX_CMSG_ALIGN_DELTA; 592 cmsg->cmsg_level = l_cmsg.cmsg_level; 593 cmsg->cmsg_type = l_cmsg.cmsg_type; 594 595 /* Zero area between header and data */ 596 memset(cmsg + 1, 0, 597 CMSG_ALIGN(sizeof(*cmsg)) - sizeof(*cmsg)); 598 599 /* Copyin the data */ 600 error = copyin(LINUX_CMSG_DATA(l_cc), 601 CMSG_DATA(cmsg), 602 l_cmsg.cmsg_len - sizeof(l_cmsg)); 603 if (error) 604 goto done; 605 606 resid -= LINUX_CMSG_ALIGN(l_cmsg.cmsg_len); 607 cidx += cspace; 608 } while ((l_cc = LINUX_CMSG_NXTHDR(&msg, l_cc)) && resid > 0); 609 610 /* If we allocated a buffer, attach to mbuf */ 611 if (cidx > MLEN) { 612 MEXTADD(ctl_mbuf, control, clen, M_MBUF, NULL, NULL); 613 ctl_mbuf->m_flags |= M_EXT_RW; 614 } 615 control = NULL; 616 ctl_mbuf->m_len = cidx; 617 618 msg.msg_control = ctl_mbuf; 619 msg.msg_flags |= MSG_CONTROLMBUF; 620 621 ktrkuser("mbcontrol", mtod(ctl_mbuf, void *), 622 msg.msg_controllen); 623 } 624 625 skipcmsg: 626 error = do_sys_sendmsg(l, SCARG(uap, s), &msg, bflags, retval); 627 /* Freed internally */ 628 ctl_mbuf = NULL; 629 630 done: 631 if (ctl_mbuf != NULL) { 632 if (control != NULL && control != mtod(ctl_mbuf, void *)) 633 free(control, M_MBUF); 634 m_free(ctl_mbuf); 635 } 636 return (error); 637 } 638 639 int 640 linux_sys_recvfrom(struct lwp *l, const struct linux_sys_recvfrom_args *uap, register_t *retval) 641 { 642 /* { 643 syscallarg(int) s; 644 syscallarg(void *) buf; 645 syscallarg(int) len; 646 syscallarg(int) flags; 647 syscallarg(struct osockaddr *) from; 648 syscallarg(int *) fromlenaddr; 649 } */ 650 int error; 651 struct sys_recvfrom_args bra; 652 653 SCARG(&bra, s) = SCARG(uap, s); 654 SCARG(&bra, buf) = SCARG(uap, buf); 655 SCARG(&bra, len) = SCARG(uap, len); 656 SCARG(&bra, flags) = SCARG(uap, flags); 657 SCARG(&bra, from) = (struct sockaddr *) SCARG(uap, from); 658 SCARG(&bra, fromlenaddr) = (socklen_t *)SCARG(uap, fromlenaddr); 659 660 if ((error = sys_recvfrom(l, &bra, retval))) 661 return (error); 662 663 if (SCARG(uap, from) && (error = linux_sa_put(SCARG(uap, from)))) 664 return (error); 665 666 return (0); 667 } 668 669 static int 670 linux_copyout_msg_control(struct lwp *l, struct msghdr *mp, struct mbuf *control) 671 { 672 int dlen, error = 0; 673 struct cmsghdr *cmsg; 674 struct linux_cmsghdr linux_cmsg; 675 struct mbuf *m; 676 char *q, *q_end; 677 678 if (mp->msg_controllen <= 0 || control == 0) { 679 mp->msg_controllen = 0; 680 free_control_mbuf(l, control, control); 681 return 0; 682 } 683 684 ktrkuser("msgcontrol", mtod(control, void *), mp->msg_controllen); 685 686 q = (char *)mp->msg_control; 687 q_end = q + mp->msg_controllen; 688 689 for (m = control; m != NULL; ) { 690 cmsg = mtod(m, struct cmsghdr *); 691 692 /* 693 * Fixup cmsg. We handle two things: 694 * 0. different sizeof cmsg_len. 695 * 1. different values for level/type on some archs 696 * 2. different alignment of CMSG_DATA on some archs 697 */ 698 linux_cmsg.cmsg_len = cmsg->cmsg_len - LINUX_CMSG_ALIGN_DELTA; 699 linux_cmsg.cmsg_level = cmsg->cmsg_level; 700 linux_cmsg.cmsg_type = cmsg->cmsg_type; 701 702 dlen = q_end - q; 703 if (linux_cmsg.cmsg_len > dlen) { 704 /* Not enough room for the parameter */ 705 dlen -= sizeof linux_cmsg; 706 if (dlen <= 0) 707 /* Discard if header wont fit */ 708 break; 709 mp->msg_flags |= MSG_CTRUNC; 710 if (linux_cmsg.cmsg_level == SOL_SOCKET 711 && linux_cmsg.cmsg_type == SCM_RIGHTS) 712 /* Do not truncate me ... */ 713 break; 714 } else 715 dlen = linux_cmsg.cmsg_len - sizeof linux_cmsg; 716 717 switch (linux_cmsg.cmsg_level) { 718 case SOL_SOCKET: 719 linux_cmsg.cmsg_level = LINUX_SOL_SOCKET; 720 switch (linux_cmsg.cmsg_type) { 721 case SCM_RIGHTS: 722 /* Linux SCM_RIGHTS is same as NetBSD */ 723 break; 724 725 default: 726 /* other types not supported */ 727 error = EINVAL; 728 goto done; 729 } 730 /* machine dependent ! */ 731 break; 732 default: 733 /* pray and leave intact */ 734 break; 735 } 736 737 /* There can be padding between the header and data... */ 738 error = copyout(&linux_cmsg, q, sizeof linux_cmsg); 739 if (error != 0) { 740 error = copyout(CCMSG_DATA(cmsg), q + sizeof linux_cmsg, 741 dlen); 742 } 743 if (error != 0) { 744 /* We must free all the SCM_RIGHTS */ 745 m = control; 746 break; 747 } 748 m = m->m_next; 749 if (m == NULL || q + LINUX_CMSG_SPACE(dlen) > q_end) { 750 q += LINUX_CMSG_LEN(dlen); 751 break; 752 } 753 q += LINUX_CMSG_SPACE(dlen); 754 } 755 756 done: 757 free_control_mbuf(l, control, m); 758 759 mp->msg_controllen = q - (char *)mp->msg_control; 760 return error; 761 } 762 763 int 764 linux_sys_recvmsg(struct lwp *l, const struct linux_sys_recvmsg_args *uap, register_t *retval) 765 { 766 /* { 767 syscallarg(int) s; 768 syscallarg(struct linux_msghdr *) msg; 769 syscallarg(u_int) flags; 770 } */ 771 struct msghdr msg; 772 struct linux_msghdr lmsg; 773 int error; 774 struct mbuf *from, *control; 775 776 error = copyin(SCARG(uap, msg), &lmsg, sizeof(lmsg)); 777 if (error) 778 return (error); 779 linux_to_bsd_msghdr(&lmsg, &msg); 780 781 msg.msg_flags = linux_to_bsd_msg_flags(SCARG(uap, flags)); 782 if (msg.msg_flags < 0) { 783 /* Some unsupported flag */ 784 return (EINVAL); 785 } 786 msg.msg_flags |= MSG_IOVUSRSPACE; 787 788 error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from, 789 msg.msg_control != NULL ? &control : NULL, retval); 790 if (error != 0) 791 return error; 792 793 if (msg.msg_control != NULL) 794 error = linux_copyout_msg_control(l, &msg, control); 795 796 if (error == 0 && from != 0) { 797 mtod(from, struct osockaddr *)->sa_family = 798 bsd_to_linux_domain(mtod(from, struct sockaddr *)->sa_family); 799 error = copyout_sockname(msg.msg_name, &msg.msg_namelen, 0, 800 from); 801 } else 802 msg.msg_namelen = 0; 803 804 if (from != NULL) 805 m_free(from); 806 807 if (error == 0) { 808 msg.msg_flags = bsd_to_linux_msg_flags(msg.msg_flags); 809 if (msg.msg_flags < 0) 810 /* Some flag unsupported by Linux */ 811 error = EINVAL; 812 else { 813 ktrkuser("msghdr", &msg, sizeof(msg)); 814 bsd_to_linux_msghdr(&msg, &lmsg); 815 error = copyout(&lmsg, SCARG(uap, msg), sizeof(lmsg)); 816 } 817 } 818 819 return (error); 820 } 821 822 /* 823 * Convert socket option level from Linux to NetBSD value. Only SOL_SOCKET 824 * is different, the rest matches IPPROTO_* on both systems. 825 */ 826 int 827 linux_to_bsd_sopt_level(int llevel) 828 { 829 830 switch (llevel) { 831 case LINUX_SOL_SOCKET: 832 return SOL_SOCKET; 833 case LINUX_SOL_IP: 834 return IPPROTO_IP; 835 #ifdef INET6 836 case LINUX_SOL_IPV6: 837 return IPPROTO_IPV6; 838 #endif 839 case LINUX_SOL_TCP: 840 return IPPROTO_TCP; 841 case LINUX_SOL_UDP: 842 return IPPROTO_UDP; 843 default: 844 return -1; 845 } 846 } 847 848 /* 849 * Convert Linux socket level socket option numbers to NetBSD values. 850 */ 851 int 852 linux_to_bsd_so_sockopt(int lopt) 853 { 854 855 switch (lopt) { 856 case LINUX_SO_DEBUG: 857 return SO_DEBUG; 858 case LINUX_SO_REUSEADDR: 859 /* 860 * Linux does not implement SO_REUSEPORT, but allows reuse of a 861 * host:port pair through SO_REUSEADDR even if the address is not a 862 * multicast-address. Effectively, this means that we should use 863 * SO_REUSEPORT to allow Linux applications to not exit with 864 * EADDRINUSE 865 */ 866 return SO_REUSEPORT; 867 case LINUX_SO_TYPE: 868 return SO_TYPE; 869 case LINUX_SO_ERROR: 870 return SO_ERROR; 871 case LINUX_SO_DONTROUTE: 872 return SO_DONTROUTE; 873 case LINUX_SO_BROADCAST: 874 return SO_BROADCAST; 875 case LINUX_SO_SNDBUF: 876 return SO_SNDBUF; 877 case LINUX_SO_RCVBUF: 878 return SO_RCVBUF; 879 case LINUX_SO_SNDLOWAT: 880 return SO_SNDLOWAT; 881 case LINUX_SO_RCVLOWAT: 882 return SO_RCVLOWAT; 883 case LINUX_SO_KEEPALIVE: 884 return SO_KEEPALIVE; 885 case LINUX_SO_OOBINLINE: 886 return SO_OOBINLINE; 887 case LINUX_SO_LINGER: 888 return SO_LINGER; 889 case LINUX_SO_ACCEPTCONN: 890 return SO_ACCEPTCONN; 891 case LINUX_SO_PRIORITY: 892 case LINUX_SO_NO_CHECK: 893 default: 894 return -1; 895 } 896 } 897 898 /* 899 * Convert Linux IP level socket option number to NetBSD values. 900 */ 901 int 902 linux_to_bsd_ip_sockopt(int lopt) 903 { 904 905 switch (lopt) { 906 case LINUX_IP_TOS: 907 return IP_TOS; 908 case LINUX_IP_TTL: 909 return IP_TTL; 910 case LINUX_IP_HDRINCL: 911 return IP_HDRINCL; 912 case LINUX_IP_MULTICAST_TTL: 913 return IP_MULTICAST_TTL; 914 case LINUX_IP_MULTICAST_LOOP: 915 return IP_MULTICAST_LOOP; 916 case LINUX_IP_MULTICAST_IF: 917 return IP_MULTICAST_IF; 918 case LINUX_IP_ADD_MEMBERSHIP: 919 return IP_ADD_MEMBERSHIP; 920 case LINUX_IP_DROP_MEMBERSHIP: 921 return IP_DROP_MEMBERSHIP; 922 default: 923 return -1; 924 } 925 } 926 927 /* 928 * Convert Linux IPV6 level socket option number to NetBSD values. 929 */ 930 #ifdef INET6 931 int 932 linux_to_bsd_ipv6_sockopt(int lopt) 933 { 934 935 switch (lopt) { 936 case LINUX_IPV6_V6ONLY: 937 return IPV6_V6ONLY; 938 default: 939 return -1; 940 } 941 } 942 #endif 943 944 /* 945 * Convert Linux TCP level socket option number to NetBSD values. 946 */ 947 int 948 linux_to_bsd_tcp_sockopt(int lopt) 949 { 950 951 switch (lopt) { 952 case LINUX_TCP_NODELAY: 953 return TCP_NODELAY; 954 case LINUX_TCP_MAXSEG: 955 return TCP_MAXSEG; 956 default: 957 return -1; 958 } 959 } 960 961 /* 962 * Convert Linux UDP level socket option number to NetBSD values. 963 */ 964 int 965 linux_to_bsd_udp_sockopt(int lopt) 966 { 967 968 switch (lopt) { 969 default: 970 return -1; 971 } 972 } 973 974 /* 975 * Another reasonably straightforward function: setsockopt(2). 976 * The level and option numbers are converted; the values passed 977 * are not (yet) converted, the ones currently implemented don't 978 * need conversion, as they are the same on both systems. 979 */ 980 int 981 linux_sys_setsockopt(struct lwp *l, const struct linux_sys_setsockopt_args *uap, register_t *retval) 982 { 983 /* { 984 syscallarg(int) s; 985 syscallarg(int) level; 986 syscallarg(int) optname; 987 syscallarg(void *) optval; 988 syscallarg(int) optlen; 989 } */ 990 struct sys_setsockopt_args bsa; 991 int name; 992 993 SCARG(&bsa, s) = SCARG(uap, s); 994 SCARG(&bsa, level) = linux_to_bsd_sopt_level(SCARG(uap, level)); 995 SCARG(&bsa, val) = SCARG(uap, optval); 996 SCARG(&bsa, valsize) = SCARG(uap, optlen); 997 998 /* 999 * Linux supports only SOL_SOCKET for AF_LOCAL domain sockets 1000 * and returns EOPNOTSUPP for other levels 1001 */ 1002 if (SCARG(&bsa, level) != SOL_SOCKET) { 1003 struct socket *so; 1004 int error, family; 1005 1006 /* fd_getsock() will use the descriptor for us */ 1007 if ((error = fd_getsock(SCARG(&bsa, s), &so)) != 0) 1008 return error; 1009 family = so->so_proto->pr_domain->dom_family; 1010 fd_putfile(SCARG(&bsa, s)); 1011 1012 if (family == AF_LOCAL) 1013 return EOPNOTSUPP; 1014 } 1015 1016 switch (SCARG(&bsa, level)) { 1017 case SOL_SOCKET: 1018 name = linux_to_bsd_so_sockopt(SCARG(uap, optname)); 1019 break; 1020 case IPPROTO_IP: 1021 name = linux_to_bsd_ip_sockopt(SCARG(uap, optname)); 1022 break; 1023 #ifdef INET6 1024 case IPPROTO_IPV6: 1025 name = linux_to_bsd_ipv6_sockopt(SCARG(uap, optname)); 1026 break; 1027 #endif 1028 case IPPROTO_TCP: 1029 name = linux_to_bsd_tcp_sockopt(SCARG(uap, optname)); 1030 break; 1031 case IPPROTO_UDP: 1032 name = linux_to_bsd_udp_sockopt(SCARG(uap, optname)); 1033 break; 1034 default: 1035 return EINVAL; 1036 } 1037 1038 if (name == -1) 1039 return EINVAL; 1040 SCARG(&bsa, name) = name; 1041 1042 return sys_setsockopt(l, &bsa, retval); 1043 } 1044 1045 /* 1046 * getsockopt(2) is very much the same as setsockopt(2) (see above) 1047 */ 1048 int 1049 linux_sys_getsockopt(struct lwp *l, const struct linux_sys_getsockopt_args *uap, register_t *retval) 1050 { 1051 /* { 1052 syscallarg(int) s; 1053 syscallarg(int) level; 1054 syscallarg(int) optname; 1055 syscallarg(void *) optval; 1056 syscallarg(int *) optlen; 1057 } */ 1058 struct sys_getsockopt_args bga; 1059 int name; 1060 1061 SCARG(&bga, s) = SCARG(uap, s); 1062 SCARG(&bga, level) = linux_to_bsd_sopt_level(SCARG(uap, level)); 1063 SCARG(&bga, val) = SCARG(uap, optval); 1064 SCARG(&bga, avalsize) = (socklen_t *)SCARG(uap, optlen); 1065 1066 switch (SCARG(&bga, level)) { 1067 case SOL_SOCKET: 1068 name = linux_to_bsd_so_sockopt(SCARG(uap, optname)); 1069 break; 1070 case IPPROTO_IP: 1071 name = linux_to_bsd_ip_sockopt(SCARG(uap, optname)); 1072 break; 1073 #ifdef INET6 1074 case IPPROTO_IPV6: 1075 name = linux_to_bsd_ipv6_sockopt(SCARG(uap, optname)); 1076 break; 1077 #endif 1078 case IPPROTO_TCP: 1079 name = linux_to_bsd_tcp_sockopt(SCARG(uap, optname)); 1080 break; 1081 case IPPROTO_UDP: 1082 name = linux_to_bsd_udp_sockopt(SCARG(uap, optname)); 1083 break; 1084 default: 1085 return EINVAL; 1086 } 1087 1088 if (name == -1) 1089 return EINVAL; 1090 SCARG(&bga, name) = name; 1091 1092 return sys_getsockopt(l, &bga, retval); 1093 } 1094 1095 int 1096 linux_getifname(struct lwp *l, register_t *retval, void *data) 1097 { 1098 struct ifnet *ifp; 1099 struct linux_ifreq ifr; 1100 int error; 1101 int s; 1102 1103 error = copyin(data, &ifr, sizeof(ifr)); 1104 if (error) 1105 return error; 1106 1107 s = pserialize_read_enter(); 1108 ifp = if_byindex(ifr.ifr_ifru.ifru_ifindex); 1109 if (ifp == NULL) { 1110 pserialize_read_exit(s); 1111 return ENODEV; 1112 } 1113 1114 strncpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)); 1115 pserialize_read_exit(s); 1116 1117 return copyout(&ifr, data, sizeof(ifr)); 1118 } 1119 1120 int 1121 linux_getifconf(struct lwp *l, register_t *retval, void *data) 1122 { 1123 struct linux_ifreq ifr, *ifrp = NULL; 1124 struct linux_ifconf ifc; 1125 struct ifnet *ifp; 1126 struct sockaddr *sa; 1127 struct osockaddr *osa; 1128 int space = 0, error; 1129 const int sz = (int)sizeof(ifr); 1130 bool docopy; 1131 int s; 1132 int bound; 1133 struct psref psref; 1134 1135 error = copyin(data, &ifc, sizeof(ifc)); 1136 if (error) 1137 return error; 1138 1139 docopy = ifc.ifc_req != NULL; 1140 if (docopy) { 1141 space = ifc.ifc_len; 1142 ifrp = ifc.ifc_req; 1143 } 1144 1145 bound = curlwp_bind(); 1146 s = pserialize_read_enter(); 1147 IFNET_READER_FOREACH(ifp) { 1148 struct ifaddr *ifa; 1149 if_acquire(ifp, &psref); 1150 pserialize_read_exit(s); 1151 1152 (void)strncpy(ifr.ifr_name, ifp->if_xname, 1153 sizeof(ifr.ifr_name)); 1154 if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0') { 1155 error = ENAMETOOLONG; 1156 goto release_exit; 1157 } 1158 1159 s = pserialize_read_enter(); 1160 IFADDR_READER_FOREACH(ifa, ifp) { 1161 struct psref psref_ifa; 1162 ifa_acquire(ifa, &psref_ifa); 1163 pserialize_read_exit(s); 1164 1165 sa = ifa->ifa_addr; 1166 if (sa->sa_family != AF_INET || 1167 sa->sa_len > sizeof(*osa)) 1168 goto next; 1169 memcpy(&ifr.ifr_addr, sa, sa->sa_len); 1170 osa = (struct osockaddr *)&ifr.ifr_addr; 1171 osa->sa_family = sa->sa_family; 1172 if (space >= sz) { 1173 error = copyout(&ifr, ifrp, sz); 1174 if (error != 0) { 1175 ifa_release(ifa, &psref_ifa); 1176 goto release_exit; 1177 } 1178 ifrp++; 1179 } 1180 space -= sz; 1181 next: 1182 s = pserialize_read_enter(); 1183 ifa_release(ifa, &psref_ifa); 1184 } 1185 1186 KASSERT(pserialize_in_read_section()); 1187 if_release(ifp, &psref); 1188 } 1189 pserialize_read_exit(s); 1190 curlwp_bindx(bound); 1191 1192 if (docopy) 1193 ifc.ifc_len -= space; 1194 else 1195 ifc.ifc_len = -space; 1196 1197 return copyout(&ifc, data, sizeof(ifc)); 1198 1199 release_exit: 1200 if_release(ifp, &psref); 1201 curlwp_bindx(bound); 1202 return error; 1203 } 1204 1205 int 1206 linux_getifhwaddr(struct lwp *l, register_t *retval, u_int fd, 1207 void *data) 1208 { 1209 /* Not the full structure, just enough to map what we do here */ 1210 struct linux_ifreq lreq; 1211 file_t *fp; 1212 struct ifaddr *ifa; 1213 struct ifnet *ifp; 1214 struct sockaddr_dl *sadl; 1215 int error, found; 1216 int index, ifnum; 1217 int s; 1218 1219 /* 1220 * We can't emulate this ioctl by calling sys_ioctl() to run 1221 * SIOCGIFCONF, because the user buffer is not of the right 1222 * type to take those results. We can't use kernel buffers to 1223 * receive the results, as the implementation of sys_ioctl() 1224 * and ifconf() [which implements SIOCGIFCONF] use 1225 * copyin()/copyout() which will fail on kernel addresses. 1226 * 1227 * So, we must duplicate code from sys_ioctl() and ifconf(). Ugh. 1228 */ 1229 1230 if ((fp = fd_getfile(fd)) == NULL) 1231 return (EBADF); 1232 1233 KERNEL_LOCK(1, NULL); 1234 1235 if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 1236 error = EBADF; 1237 goto out; 1238 } 1239 1240 error = copyin(data, &lreq, sizeof(lreq)); 1241 if (error) 1242 goto out; 1243 lreq.ifr_name[LINUX_IFNAMSIZ-1] = '\0'; /* just in case */ 1244 1245 /* 1246 * Try real interface name first, then fake "ethX" 1247 */ 1248 found = 0; 1249 s = pserialize_read_enter(); 1250 IFNET_READER_FOREACH(ifp) { 1251 if (found) 1252 break; 1253 if (strcmp(lreq.ifr_name, ifp->if_xname)) 1254 /* not this interface */ 1255 continue; 1256 1257 found=1; 1258 if (IFADDR_READER_EMPTY(ifp)) { 1259 pserialize_read_exit(s); 1260 error = ENODEV; 1261 goto out; 1262 } 1263 IFADDR_READER_FOREACH(ifa, ifp) { 1264 sadl = satosdl(ifa->ifa_addr); 1265 /* only return ethernet addresses */ 1266 /* XXX what about FDDI, etc. ? */ 1267 if (sadl->sdl_family != AF_LINK || 1268 sadl->sdl_type != IFT_ETHER) 1269 continue; 1270 memcpy(&lreq.ifr_hwaddr.sa_data, CLLADDR(sadl), 1271 MIN(sadl->sdl_alen, 1272 sizeof(lreq.ifr_hwaddr.sa_data))); 1273 lreq.ifr_hwaddr.sa_family = 1274 sadl->sdl_family; 1275 pserialize_read_exit(s); 1276 1277 error = copyout(&lreq, data, sizeof(lreq)); 1278 goto out; 1279 } 1280 } 1281 pserialize_read_exit(s); 1282 1283 if (strncmp(lreq.ifr_name, "eth", 3) != 0) { 1284 /* unknown interface, not even an "eth*" name */ 1285 error = ENODEV; 1286 goto out; 1287 } 1288 1289 for (ifnum = 0, index = 3; 1290 index < LINUX_IFNAMSIZ && lreq.ifr_name[index] != '\0'; 1291 index++) { 1292 ifnum *= 10; 1293 ifnum += lreq.ifr_name[index] - '0'; 1294 } 1295 1296 error = EINVAL; /* in case we don't find one */ 1297 s = pserialize_read_enter(); 1298 IFNET_READER_FOREACH(ifp) { 1299 memcpy(lreq.ifr_name, ifp->if_xname, 1300 MIN(LINUX_IFNAMSIZ, IFNAMSIZ)); 1301 IFADDR_READER_FOREACH(ifa, ifp) { 1302 sadl = satosdl(ifa->ifa_addr); 1303 /* only return ethernet addresses */ 1304 /* XXX what about FDDI, etc. ? */ 1305 if (sadl->sdl_family != AF_LINK || 1306 sadl->sdl_type != IFT_ETHER) 1307 continue; 1308 if (ifnum--) 1309 /* not the reqested iface */ 1310 continue; 1311 memcpy(&lreq.ifr_hwaddr.sa_data, 1312 CLLADDR(sadl), 1313 MIN(sadl->sdl_alen, 1314 sizeof(lreq.ifr_hwaddr.sa_data))); 1315 lreq.ifr_hwaddr.sa_family = 1316 sadl->sdl_family; 1317 pserialize_read_exit(s); 1318 1319 error = copyout(&lreq, data, sizeof(lreq)); 1320 goto out; 1321 } 1322 } 1323 pserialize_read_exit(s); 1324 1325 out: 1326 KERNEL_UNLOCK_ONE(NULL); 1327 fd_putfile(fd); 1328 return error; 1329 } 1330 1331 int 1332 linux_ioctl_socket(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval) 1333 { 1334 /* { 1335 syscallarg(int) fd; 1336 syscallarg(u_long) com; 1337 syscallarg(void *) data; 1338 } */ 1339 u_long com; 1340 int error = 0, isdev = 0, dosys = 1; 1341 struct sys_ioctl_args ia; 1342 file_t *fp; 1343 struct vnode *vp; 1344 int (*ioctlf)(file_t *, u_long, void *); 1345 struct ioctl_pt pt; 1346 1347 if ((fp = fd_getfile(SCARG(uap, fd))) == NULL) 1348 return (EBADF); 1349 1350 if (fp->f_type == DTYPE_VNODE) { 1351 vp = (struct vnode *)fp->f_data; 1352 isdev = vp->v_type == VCHR; 1353 } 1354 1355 /* 1356 * Don't try to interpret socket ioctl calls that are done 1357 * on a device filedescriptor, just pass them through, to 1358 * emulate Linux behaviour. Use PTIOCLINUX so that the 1359 * device will only handle these if it's prepared to do 1360 * so, to avoid unexpected things from happening. 1361 */ 1362 if (isdev) { 1363 dosys = 0; 1364 ioctlf = fp->f_ops->fo_ioctl; 1365 pt.com = SCARG(uap, com); 1366 pt.data = SCARG(uap, data); 1367 error = ioctlf(fp, PTIOCLINUX, &pt); 1368 /* 1369 * XXX hack: if the function returns EJUSTRETURN, 1370 * it has stuffed a sysctl return value in pt.data. 1371 */ 1372 if (error == EJUSTRETURN) { 1373 retval[0] = (register_t)pt.data; 1374 error = 0; 1375 } 1376 goto out; 1377 } 1378 1379 com = SCARG(uap, com); 1380 retval[0] = 0; 1381 1382 switch (com) { 1383 case LINUX_SIOCGIFNAME: 1384 error = linux_getifname(l, retval, SCARG(uap, data)); 1385 dosys = 0; 1386 break; 1387 case LINUX_SIOCGIFCONF: 1388 error = linux_getifconf(l, retval, SCARG(uap, data)); 1389 dosys = 0; 1390 break; 1391 case LINUX_SIOCGIFFLAGS: 1392 SCARG(&ia, com) = OSIOCGIFFLAGS; 1393 break; 1394 case LINUX_SIOCSIFFLAGS: 1395 SCARG(&ia, com) = OSIOCSIFFLAGS; 1396 break; 1397 case LINUX_SIOCGIFADDR: 1398 SCARG(&ia, com) = OOSIOCGIFADDR; 1399 break; 1400 case LINUX_SIOCGIFDSTADDR: 1401 SCARG(&ia, com) = OOSIOCGIFDSTADDR; 1402 break; 1403 case LINUX_SIOCGIFBRDADDR: 1404 SCARG(&ia, com) = OOSIOCGIFBRDADDR; 1405 break; 1406 case LINUX_SIOCGIFNETMASK: 1407 SCARG(&ia, com) = OOSIOCGIFNETMASK; 1408 break; 1409 case LINUX_SIOCGIFMTU: 1410 SCARG(&ia, com) = OSIOCGIFMTU; 1411 break; 1412 case LINUX_SIOCADDMULTI: 1413 SCARG(&ia, com) = OSIOCADDMULTI; 1414 break; 1415 case LINUX_SIOCDELMULTI: 1416 SCARG(&ia, com) = OSIOCDELMULTI; 1417 break; 1418 case LINUX_SIOCGIFHWADDR: 1419 error = linux_getifhwaddr(l, retval, SCARG(uap, fd), 1420 SCARG(uap, data)); 1421 dosys = 0; 1422 break; 1423 default: 1424 error = EINVAL; 1425 } 1426 1427 out: 1428 fd_putfile(SCARG(uap, fd)); 1429 1430 if (error ==0 && dosys) { 1431 SCARG(&ia, fd) = SCARG(uap, fd); 1432 SCARG(&ia, data) = SCARG(uap, data); 1433 error = sys_ioctl(curlwp, &ia, retval); 1434 } 1435 1436 return error; 1437 } 1438 1439 int 1440 linux_sys_connect(struct lwp *l, const struct linux_sys_connect_args *uap, register_t *retval) 1441 { 1442 /* { 1443 syscallarg(int) s; 1444 syscallarg(const struct sockaddr *) name; 1445 syscallarg(int) namelen; 1446 } */ 1447 int error; 1448 struct sockaddr_big sb; 1449 1450 error = linux_get_sa(l, SCARG(uap, s), &sb, SCARG(uap, name), 1451 SCARG(uap, namelen)); 1452 if (error) 1453 return (error); 1454 1455 error = do_sys_connect(l, SCARG(uap, s), (struct sockaddr *)&sb); 1456 1457 if (error == EISCONN) { 1458 struct socket *so; 1459 int state, prflags; 1460 1461 /* fd_getsock() will use the descriptor for us */ 1462 if (fd_getsock(SCARG(uap, s), &so) != 0) 1463 return EISCONN; 1464 1465 solock(so); 1466 state = so->so_state; 1467 prflags = so->so_proto->pr_flags; 1468 sounlock(so); 1469 fd_putfile(SCARG(uap, s)); 1470 /* 1471 * We should only let this call succeed once per 1472 * non-blocking connect; however we don't have 1473 * a convenient place to keep that state.. 1474 */ 1475 if ((state & (SS_ISCONNECTED|SS_NBIO)) == 1476 (SS_ISCONNECTED|SS_NBIO) && 1477 (prflags & PR_CONNREQUIRED)) 1478 return 0; 1479 } 1480 1481 return (error); 1482 } 1483 1484 int 1485 linux_sys_bind(struct lwp *l, const struct linux_sys_bind_args *uap, register_t *retval) 1486 { 1487 /* { 1488 syscallarg(int) s; 1489 syscallarg(const struct osockaddr *) name; 1490 syscallarg(int) namelen; 1491 } */ 1492 int error; 1493 struct sockaddr_big sb; 1494 1495 error = linux_get_sa(l, SCARG(uap, s), &sb, SCARG(uap, name), 1496 SCARG(uap, namelen)); 1497 if (error) 1498 return (error); 1499 1500 return do_sys_bind(l, SCARG(uap, s), (struct sockaddr *)&sb); 1501 } 1502 1503 int 1504 linux_sys_getsockname(struct lwp *l, const struct linux_sys_getsockname_args *uap, register_t *retval) 1505 { 1506 /* { 1507 syscallarg(int) fdes; 1508 syscallarg(void *) asa; 1509 syscallarg(int *) alen; 1510 } */ 1511 int error; 1512 1513 if ((error = sys_getsockname(l, (const void *)uap, retval)) != 0) 1514 return (error); 1515 1516 if ((error = linux_sa_put((struct osockaddr *)SCARG(uap, asa)))) 1517 return (error); 1518 1519 return (0); 1520 } 1521 1522 int 1523 linux_sys_getpeername(struct lwp *l, const struct linux_sys_getpeername_args *uap, register_t *retval) 1524 { 1525 /* { 1526 syscallarg(int) fdes; 1527 syscallarg(void *) asa; 1528 syscallarg(int *) alen; 1529 } */ 1530 int error; 1531 1532 if ((error = sys_getpeername(l, (const void *)uap, retval)) != 0) 1533 return (error); 1534 1535 if ((error = linux_sa_put((struct osockaddr *)SCARG(uap, asa)))) 1536 return (error); 1537 1538 return (0); 1539 } 1540 1541 /* 1542 * Copy the osockaddr structure pointed to by name to sb, adjust 1543 * family and convert to sockaddr. 1544 */ 1545 static int 1546 linux_get_sa(struct lwp *l, int s, struct sockaddr_big *sb, 1547 const struct osockaddr *name, socklen_t namelen) 1548 { 1549 int error, bdom; 1550 1551 if (namelen > UCHAR_MAX || 1552 namelen <= offsetof(struct sockaddr_big, sb_data)) 1553 return EINVAL; 1554 1555 error = copyin(name, sb, namelen); 1556 if (error) 1557 return error; 1558 1559 bdom = linux_to_bsd_domain(sb->sb_family); 1560 if (bdom == -1) 1561 return EINVAL; 1562 1563 /* 1564 * If the family is unspecified, use address family of the socket. 1565 * This avoid triggering strict family checks in netinet/in_pcb.c et.al. 1566 */ 1567 if (bdom == AF_UNSPEC) { 1568 struct socket *so; 1569 1570 /* fd_getsock() will use the descriptor for us */ 1571 if ((error = fd_getsock(s, &so)) != 0) 1572 return error; 1573 1574 bdom = so->so_proto->pr_domain->dom_family; 1575 fd_putfile(s); 1576 } 1577 1578 /* 1579 * Older Linux IPv6 code uses obsolete RFC2133 struct sockaddr_in6, 1580 * which lacks the scope id compared with RFC2553 one. If we detect 1581 * the situation, reject the address and write a message to system log. 1582 * 1583 * Still accept addresses for which the scope id is not used. 1584 */ 1585 if (bdom == AF_INET6 && 1586 namelen == sizeof(struct sockaddr_in6) - sizeof(uint32_t)) { 1587 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sb; 1588 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) && 1589 (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) || 1590 IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) || 1591 IN6_IS_ADDR_V4COMPAT(&sin6->sin6_addr) || 1592 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) || 1593 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))) { 1594 struct proc *p = l->l_proc; 1595 int uid = l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1; 1596 1597 log(LOG_DEBUG, 1598 "pid %d (%s), uid %d: obsolete pre-RFC2553 " 1599 "sockaddr_in6 rejected", 1600 p->p_pid, p->p_comm, uid); 1601 return EINVAL; 1602 } 1603 namelen = sizeof(struct sockaddr_in6); 1604 sin6->sin6_scope_id = 0; 1605 } 1606 1607 if (bdom == AF_INET) 1608 namelen = sizeof(struct sockaddr_in); 1609 1610 sb->sb_family = bdom; 1611 sb->sb_len = namelen; 1612 ktrkuser("mbsoname", sb, namelen); 1613 return 0; 1614 } 1615 1616 static int 1617 linux_sa_put(struct osockaddr *osa) 1618 { 1619 struct sockaddr sa; 1620 struct osockaddr *kosa; 1621 int error, bdom, len; 1622 1623 /* 1624 * Only read/write the sockaddr family and length part, the rest is 1625 * not changed. 1626 */ 1627 len = sizeof(sa.sa_len) + sizeof(sa.sa_family); 1628 1629 error = copyin(osa, &sa, len); 1630 if (error) 1631 return (error); 1632 1633 bdom = bsd_to_linux_domain(sa.sa_family); 1634 if (bdom == -1) 1635 return (EINVAL); 1636 1637 /* Note: we convert from sockaddr to osockaddr here, too */ 1638 kosa = (struct osockaddr *) &sa; 1639 kosa->sa_family = bdom; 1640 error = copyout(kosa, osa, len); 1641 if (error) 1642 return (error); 1643 1644 return (0); 1645 } 1646 1647 #ifndef __amd64__ 1648 int 1649 linux_sys_recv(struct lwp *l, const struct linux_sys_recv_args *uap, register_t *retval) 1650 { 1651 /* { 1652 syscallarg(int) s; 1653 syscallarg(void *) buf; 1654 syscallarg(int) len; 1655 syscallarg(int) flags; 1656 } */ 1657 struct sys_recvfrom_args bra; 1658 1659 1660 SCARG(&bra, s) = SCARG(uap, s); 1661 SCARG(&bra, buf) = SCARG(uap, buf); 1662 SCARG(&bra, len) = (size_t) SCARG(uap, len); 1663 SCARG(&bra, flags) = SCARG(uap, flags); 1664 SCARG(&bra, from) = NULL; 1665 SCARG(&bra, fromlenaddr) = NULL; 1666 1667 return (sys_recvfrom(l, &bra, retval)); 1668 } 1669 1670 int 1671 linux_sys_send(struct lwp *l, const struct linux_sys_send_args *uap, register_t *retval) 1672 { 1673 /* { 1674 syscallarg(int) s; 1675 syscallarg(void *) buf; 1676 syscallarg(int) len; 1677 syscallarg(int) flags; 1678 } */ 1679 struct sys_sendto_args bsa; 1680 1681 SCARG(&bsa, s) = SCARG(uap, s); 1682 SCARG(&bsa, buf) = SCARG(uap, buf); 1683 SCARG(&bsa, len) = SCARG(uap, len); 1684 SCARG(&bsa, flags) = SCARG(uap, flags); 1685 SCARG(&bsa, to) = NULL; 1686 SCARG(&bsa, tolen) = 0; 1687 1688 return (sys_sendto(l, &bsa, retval)); 1689 } 1690 #endif 1691 1692 int 1693 linux_sys_accept(struct lwp *l, const struct linux_sys_accept_args *uap, register_t *retval) 1694 { 1695 /* { 1696 syscallarg(int) s; 1697 syscallarg(struct osockaddr *) name; 1698 syscallarg(int *) anamelen; 1699 } */ 1700 int error; 1701 struct sys_accept_args baa; 1702 1703 SCARG(&baa, s) = SCARG(uap, s); 1704 SCARG(&baa, name) = (struct sockaddr *) SCARG(uap, name); 1705 SCARG(&baa, anamelen) = (unsigned int *) SCARG(uap, anamelen); 1706 1707 if ((error = sys_accept(l, &baa, retval))) 1708 return (error); 1709 1710 if (SCARG(uap, name) && (error = linux_sa_put(SCARG(uap, name)))) 1711 return (error); 1712 1713 return (0); 1714 } 1715 1716 int 1717 linux_sys_accept4(struct lwp *l, const struct linux_sys_accept4_args *uap, register_t *retval) 1718 { 1719 /* { 1720 syscallarg(int) s; 1721 syscallarg(struct osockaddr *) name; 1722 syscallarg(int *) anamelen; 1723 syscallarg(int) flags; 1724 } */ 1725 int error, flags; 1726 struct sockaddr_big name; 1727 1728 if ((flags = linux_to_bsd_type(SCARG(uap, flags))) == -1) 1729 return EINVAL; 1730 1731 name.sb_len = UCHAR_MAX; 1732 error = do_sys_accept(l, SCARG(uap, s), (struct sockaddr *)&name, 1733 retval, NULL, flags, 0); 1734 if (error != 0) 1735 return error; 1736 1737 error = copyout_sockname_sb((struct sockaddr *)SCARG(uap, name), 1738 SCARG(uap, anamelen), MSG_LENUSRSPACE, &name); 1739 if (error != 0) { 1740 int fd = (int)*retval; 1741 if (fd_getfile(fd) != NULL) 1742 (void)fd_close(fd); 1743 return error; 1744 } 1745 if (SCARG(uap, name) && (error = linux_sa_put(SCARG(uap, name)))) 1746 return error; 1747 1748 return 0; 1749 } 1750 1751 int 1752 linux_sys_sendmmsg(struct lwp *l, const struct linux_sys_sendmmsg_args *uap, 1753 register_t *retval) 1754 { 1755 /* { 1756 syscallarg(int) s; 1757 syscallarg(struct linux_mmsghdr *) msgvec; 1758 syscallarg(unsigned int) vlen; 1759 syscallarg(unsigned int) flags; 1760 } */ 1761 struct linux_mmsghdr lmsg; 1762 struct mmsghdr bmsg; 1763 struct socket *so; 1764 file_t *fp; 1765 struct msghdr *msg = &bmsg.msg_hdr; 1766 int error, s; 1767 unsigned int vlen, flags, dg; 1768 1769 if ((flags = linux_to_bsd_msg_flags(SCARG(uap, flags))) == -1) 1770 return EINVAL; 1771 1772 flags = (flags & MSG_USERFLAGS) | MSG_IOVUSRSPACE; 1773 1774 s = SCARG(uap, s); 1775 if ((error = fd_getsock1(s, &so, &fp)) != 0) 1776 return error; 1777 1778 vlen = SCARG(uap, vlen); 1779 if (vlen > 1024) 1780 vlen = 1024; 1781 1782 for (dg = 0; dg < vlen;) { 1783 error = copyin(SCARG(uap, msgvec) + dg, &lmsg, sizeof(lmsg)); 1784 if (error) 1785 break; 1786 linux_to_bsd_msghdr(&lmsg.msg_hdr, &bmsg.msg_hdr); 1787 1788 msg->msg_flags = flags; 1789 1790 error = do_sys_sendmsg_so(l, s, so, fp, msg, flags, retval); 1791 if (error) 1792 break; 1793 1794 ktrkuser("msghdr", msg, sizeof *msg); 1795 lmsg.msg_len = *retval; 1796 error = copyout(&lmsg, SCARG(uap, msgvec) + dg, sizeof(lmsg)); 1797 if (error) 1798 break; 1799 dg++; 1800 1801 } 1802 1803 *retval = dg; 1804 if (error) 1805 so->so_error = error; 1806 1807 fd_putfile(s); 1808 1809 /* 1810 * If we succeeded at least once, return 0, hopefully so->so_error 1811 * will catch it next time. 1812 */ 1813 if (dg) 1814 return 0; 1815 return error; 1816 } 1817 1818 int 1819 linux_sys_recvmmsg(struct lwp *l, const struct linux_sys_recvmmsg_args *uap, 1820 register_t *retval) 1821 { 1822 /* { 1823 syscallarg(int) s; 1824 syscallarg(struct linux_mmsghdr *) msgvec; 1825 syscallarg(unsigned int) vlen; 1826 syscallarg(unsigned int) flags; 1827 syscallarg(struct linux_timespec *) timeout; 1828 } */ 1829 struct linux_mmsghdr lmsg; 1830 struct mmsghdr bmsg; 1831 struct socket *so; 1832 struct msghdr *msg = &bmsg.msg_hdr; 1833 int error, s; 1834 struct mbuf *from, *control; 1835 struct timespec ts, now; 1836 struct linux_timespec lts; 1837 unsigned int vlen, flags, dg; 1838 1839 if (SCARG(uap, timeout)) { 1840 error = copyin(SCARG(uap, timeout), <s, sizeof(lts)); 1841 return error; 1842 ts.tv_sec = lts.tv_sec; 1843 ts.tv_nsec = lts.tv_nsec; 1844 getnanotime(&now); 1845 timespecadd(&now, &ts, &ts); 1846 } 1847 1848 s = SCARG(uap, s); 1849 if ((error = fd_getsock(s, &so)) != 0) 1850 return error; 1851 1852 vlen = SCARG(uap, vlen); 1853 if (vlen > 1024) 1854 vlen = 1024; 1855 1856 from = NULL; 1857 flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE; 1858 1859 for (dg = 0; dg < vlen;) { 1860 error = copyin(SCARG(uap, msgvec) + dg, &lmsg, sizeof(lmsg)); 1861 if (error) 1862 break; 1863 linux_to_bsd_msghdr(&lmsg.msg_hdr, &bmsg.msg_hdr); 1864 msg->msg_flags = flags & ~MSG_WAITFORONE; 1865 1866 if (from != NULL) { 1867 m_free(from); 1868 from = NULL; 1869 } 1870 1871 error = do_sys_recvmsg_so(l, s, so, msg, &from, 1872 msg->msg_control != NULL ? &control : NULL, retval); 1873 if (error) { 1874 if (error == EAGAIN && dg > 0) 1875 error = 0; 1876 break; 1877 } 1878 1879 if (msg->msg_control != NULL) 1880 error = linux_copyout_msg_control(l, msg, control); 1881 if (error) 1882 break; 1883 1884 if (from != NULL) { 1885 mtod(from, struct osockaddr *)->sa_family = 1886 bsd_to_linux_domain(mtod(from, 1887 struct sockaddr *)->sa_family); 1888 error = copyout_sockname(msg->msg_name, 1889 &msg->msg_namelen, 0, from); 1890 if (error) 1891 break; 1892 } 1893 1894 1895 lmsg.msg_len = *retval; 1896 ktrkuser("msghdr", msg, sizeof(*msg)); 1897 bsd_to_linux_msghdr(msg, &lmsg.msg_hdr); 1898 error = copyout(&lmsg, SCARG(uap, msgvec) + dg, sizeof(lmsg)); 1899 if (error) 1900 break; 1901 1902 dg++; 1903 if (msg->msg_flags & MSG_OOB) 1904 break; 1905 1906 if (SCARG(uap, timeout)) { 1907 getnanotime(&now); 1908 timespecsub(&now, &ts, &now); 1909 if (now.tv_sec > 0) 1910 break; 1911 } 1912 1913 if (flags & MSG_WAITFORONE) 1914 flags |= MSG_DONTWAIT; 1915 1916 } 1917 1918 if (from != NULL) 1919 m_free(from); 1920 1921 *retval = dg; 1922 if (error) 1923 so->so_error = error; 1924 1925 fd_putfile(s); 1926 1927 /* 1928 * If we succeeded at least once, return 0, hopefully so->so_error 1929 * will catch it next time. 1930 */ 1931 if (dg) 1932 return 0; 1933 1934 return error; 1935 } 1936