1 /* $NetBSD: linux_socket.c,v 1.149 2019/09/08 18:46:32 maxv 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.149 2019/09/08 18:46:32 maxv 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 if (control != mtod(ctl_mbuf, void *)) 550 free(control, M_MBUF); 551 m_free(ctl_mbuf); 552 ctl_mbuf = NULL; 553 msg.msg_control = NULL; 554 msg.msg_controllen = 0; 555 goto skipcmsg; 556 557 default: 558 /* other types not supported */ 559 error = EINVAL; 560 goto done; 561 } 562 break; 563 default: 564 /* pray and leave intact */ 565 break; 566 } 567 568 cspace = CMSG_SPACE(l_cmsg.cmsg_len - sizeof(l_cmsg)); 569 570 /* Check the buffer is big enough */ 571 if (__predict_false(cidx + cspace > clen)) { 572 u_int8_t *nc; 573 size_t nclen; 574 575 nclen = cidx + cspace; 576 if (nclen >= PAGE_SIZE) { 577 error = EINVAL; 578 goto done; 579 } 580 nc = realloc(clen <= MLEN ? NULL : control, 581 nclen, M_TEMP, M_WAITOK); 582 if (!nc) { 583 error = ENOMEM; 584 goto done; 585 } 586 if (cidx <= MLEN) 587 /* Old buffer was in mbuf... */ 588 memcpy(nc, control, cidx); 589 control = nc; 590 clen = nclen; 591 } 592 593 /* Copy header */ 594 cmsg = (void *)&control[cidx]; 595 cmsg->cmsg_len = l_cmsg.cmsg_len + LINUX_CMSG_ALIGN_DELTA; 596 cmsg->cmsg_level = l_cmsg.cmsg_level; 597 cmsg->cmsg_type = l_cmsg.cmsg_type; 598 599 /* Zero area between header and data */ 600 memset(cmsg + 1, 0, 601 CMSG_ALIGN(sizeof(*cmsg)) - sizeof(*cmsg)); 602 603 /* Copyin the data */ 604 error = copyin(LINUX_CMSG_DATA(l_cc), 605 CMSG_DATA(cmsg), 606 l_cmsg.cmsg_len - sizeof(l_cmsg)); 607 if (error) 608 goto done; 609 610 resid -= LINUX_CMSG_ALIGN(l_cmsg.cmsg_len); 611 cidx += cspace; 612 } while ((l_cc = LINUX_CMSG_NXTHDR(&msg, l_cc, &l_cmsg)) && resid > 0); 613 614 /* If we allocated a buffer, attach to mbuf */ 615 if (cidx > MLEN) { 616 MEXTADD(ctl_mbuf, control, clen, M_MBUF, NULL, NULL); 617 ctl_mbuf->m_flags |= M_EXT_RW; 618 } 619 control = NULL; 620 ctl_mbuf->m_len = cidx; 621 622 msg.msg_control = ctl_mbuf; 623 msg.msg_flags |= MSG_CONTROLMBUF; 624 625 ktrkuser("mbcontrol", mtod(ctl_mbuf, void *), 626 msg.msg_controllen); 627 } 628 629 skipcmsg: 630 error = do_sys_sendmsg(l, SCARG(uap, s), &msg, bflags, retval); 631 /* Freed internally */ 632 ctl_mbuf = NULL; 633 634 done: 635 if (ctl_mbuf != NULL) { 636 if (control != NULL && control != mtod(ctl_mbuf, void *)) 637 free(control, M_MBUF); 638 m_free(ctl_mbuf); 639 } 640 return (error); 641 } 642 643 int 644 linux_sys_recvfrom(struct lwp *l, const struct linux_sys_recvfrom_args *uap, register_t *retval) 645 { 646 /* { 647 syscallarg(int) s; 648 syscallarg(void *) buf; 649 syscallarg(int) len; 650 syscallarg(int) flags; 651 syscallarg(struct osockaddr *) from; 652 syscallarg(int *) fromlenaddr; 653 } */ 654 int error; 655 struct sys_recvfrom_args bra; 656 657 SCARG(&bra, s) = SCARG(uap, s); 658 SCARG(&bra, buf) = SCARG(uap, buf); 659 SCARG(&bra, len) = SCARG(uap, len); 660 SCARG(&bra, flags) = SCARG(uap, flags); 661 SCARG(&bra, from) = (struct sockaddr *) SCARG(uap, from); 662 SCARG(&bra, fromlenaddr) = (socklen_t *)SCARG(uap, fromlenaddr); 663 664 if ((error = sys_recvfrom(l, &bra, retval))) 665 return (error); 666 667 if (SCARG(uap, from) && (error = linux_sa_put(SCARG(uap, from)))) 668 return (error); 669 670 return (0); 671 } 672 673 static int 674 linux_copyout_msg_control(struct lwp *l, struct msghdr *mp, struct mbuf *control) 675 { 676 int dlen, error = 0; 677 struct cmsghdr *cmsg; 678 struct linux_cmsghdr linux_cmsg; 679 struct mbuf *m; 680 char *q, *q_end; 681 682 if (mp->msg_controllen <= 0 || control == 0) { 683 mp->msg_controllen = 0; 684 free_control_mbuf(l, control, control); 685 return 0; 686 } 687 688 ktrkuser("msgcontrol", mtod(control, void *), mp->msg_controllen); 689 690 q = (char *)mp->msg_control; 691 q_end = q + mp->msg_controllen; 692 693 for (m = control; m != NULL; ) { 694 cmsg = mtod(m, struct cmsghdr *); 695 696 /* 697 * Fixup cmsg. We handle two things: 698 * 0. different sizeof cmsg_len. 699 * 1. different values for level/type on some archs 700 * 2. different alignment of CMSG_DATA on some archs 701 */ 702 linux_cmsg.cmsg_len = cmsg->cmsg_len - LINUX_CMSG_ALIGN_DELTA; 703 linux_cmsg.cmsg_level = cmsg->cmsg_level; 704 linux_cmsg.cmsg_type = cmsg->cmsg_type; 705 706 dlen = q_end - q; 707 if (linux_cmsg.cmsg_len > dlen) { 708 /* Not enough room for the parameter */ 709 dlen -= sizeof linux_cmsg; 710 if (dlen <= 0) 711 /* Discard if header wont fit */ 712 break; 713 mp->msg_flags |= MSG_CTRUNC; 714 if (linux_cmsg.cmsg_level == SOL_SOCKET 715 && linux_cmsg.cmsg_type == SCM_RIGHTS) 716 /* Do not truncate me ... */ 717 break; 718 } else 719 dlen = linux_cmsg.cmsg_len - sizeof linux_cmsg; 720 721 switch (linux_cmsg.cmsg_level) { 722 case SOL_SOCKET: 723 linux_cmsg.cmsg_level = LINUX_SOL_SOCKET; 724 switch (linux_cmsg.cmsg_type) { 725 case SCM_RIGHTS: 726 /* Linux SCM_RIGHTS is same as NetBSD */ 727 break; 728 729 default: 730 /* other types not supported */ 731 error = EINVAL; 732 goto done; 733 } 734 /* machine dependent ! */ 735 break; 736 default: 737 /* pray and leave intact */ 738 break; 739 } 740 741 /* There can be padding between the header and data... */ 742 error = copyout(&linux_cmsg, q, sizeof linux_cmsg); 743 if (error != 0) { 744 error = copyout(CCMSG_DATA(cmsg), q + sizeof linux_cmsg, 745 dlen); 746 } 747 if (error != 0) { 748 /* We must free all the SCM_RIGHTS */ 749 m = control; 750 break; 751 } 752 m = m->m_next; 753 if (m == NULL || q + LINUX_CMSG_SPACE(dlen) > q_end) { 754 q += LINUX_CMSG_LEN(dlen); 755 break; 756 } 757 q += LINUX_CMSG_SPACE(dlen); 758 } 759 760 done: 761 free_control_mbuf(l, control, m); 762 763 mp->msg_controllen = q - (char *)mp->msg_control; 764 return error; 765 } 766 767 int 768 linux_sys_recvmsg(struct lwp *l, const struct linux_sys_recvmsg_args *uap, register_t *retval) 769 { 770 /* { 771 syscallarg(int) s; 772 syscallarg(struct linux_msghdr *) msg; 773 syscallarg(u_int) flags; 774 } */ 775 struct msghdr msg; 776 struct linux_msghdr lmsg; 777 int error; 778 struct mbuf *from, *control; 779 780 error = copyin(SCARG(uap, msg), &lmsg, sizeof(lmsg)); 781 if (error) 782 return (error); 783 linux_to_bsd_msghdr(&lmsg, &msg); 784 785 msg.msg_flags = linux_to_bsd_msg_flags(SCARG(uap, flags)); 786 if (msg.msg_flags < 0) { 787 /* Some unsupported flag */ 788 return (EINVAL); 789 } 790 msg.msg_flags |= MSG_IOVUSRSPACE; 791 792 error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from, 793 msg.msg_control != NULL ? &control : NULL, retval); 794 if (error != 0) 795 return error; 796 797 if (msg.msg_control != NULL) 798 error = linux_copyout_msg_control(l, &msg, control); 799 800 if (error == 0 && from != 0) { 801 mtod(from, struct osockaddr *)->sa_family = 802 bsd_to_linux_domain(mtod(from, struct sockaddr *)->sa_family); 803 error = copyout_sockname(msg.msg_name, &msg.msg_namelen, 0, 804 from); 805 } else 806 msg.msg_namelen = 0; 807 808 if (from != NULL) 809 m_free(from); 810 811 if (error == 0) { 812 msg.msg_flags = bsd_to_linux_msg_flags(msg.msg_flags); 813 if (msg.msg_flags < 0) 814 /* Some flag unsupported by Linux */ 815 error = EINVAL; 816 else { 817 ktrkuser("msghdr", &msg, sizeof(msg)); 818 bsd_to_linux_msghdr(&msg, &lmsg); 819 error = copyout(&lmsg, SCARG(uap, msg), sizeof(lmsg)); 820 } 821 } 822 823 return (error); 824 } 825 826 /* 827 * Convert socket option level from Linux to NetBSD value. Only SOL_SOCKET 828 * is different, the rest matches IPPROTO_* on both systems. 829 */ 830 int 831 linux_to_bsd_sopt_level(int llevel) 832 { 833 834 switch (llevel) { 835 case LINUX_SOL_SOCKET: 836 return SOL_SOCKET; 837 case LINUX_SOL_IP: 838 return IPPROTO_IP; 839 #ifdef INET6 840 case LINUX_SOL_IPV6: 841 return IPPROTO_IPV6; 842 #endif 843 case LINUX_SOL_TCP: 844 return IPPROTO_TCP; 845 case LINUX_SOL_UDP: 846 return IPPROTO_UDP; 847 default: 848 return -1; 849 } 850 } 851 852 /* 853 * Convert Linux socket level socket option numbers to NetBSD values. 854 */ 855 int 856 linux_to_bsd_so_sockopt(int lopt) 857 { 858 859 switch (lopt) { 860 case LINUX_SO_DEBUG: 861 return SO_DEBUG; 862 case LINUX_SO_REUSEADDR: 863 /* 864 * Linux does not implement SO_REUSEPORT, but allows reuse of a 865 * host:port pair through SO_REUSEADDR even if the address is not a 866 * multicast-address. Effectively, this means that we should use 867 * SO_REUSEPORT to allow Linux applications to not exit with 868 * EADDRINUSE 869 */ 870 return SO_REUSEPORT; 871 case LINUX_SO_TYPE: 872 return SO_TYPE; 873 case LINUX_SO_ERROR: 874 return SO_ERROR; 875 case LINUX_SO_DONTROUTE: 876 return SO_DONTROUTE; 877 case LINUX_SO_BROADCAST: 878 return SO_BROADCAST; 879 case LINUX_SO_SNDBUF: 880 return SO_SNDBUF; 881 case LINUX_SO_RCVBUF: 882 return SO_RCVBUF; 883 case LINUX_SO_SNDLOWAT: 884 return SO_SNDLOWAT; 885 case LINUX_SO_RCVLOWAT: 886 return SO_RCVLOWAT; 887 case LINUX_SO_KEEPALIVE: 888 return SO_KEEPALIVE; 889 case LINUX_SO_OOBINLINE: 890 return SO_OOBINLINE; 891 case LINUX_SO_LINGER: 892 return SO_LINGER; 893 case LINUX_SO_ACCEPTCONN: 894 return SO_ACCEPTCONN; 895 case LINUX_SO_PRIORITY: 896 case LINUX_SO_NO_CHECK: 897 default: 898 return -1; 899 } 900 } 901 902 /* 903 * Convert Linux IP level socket option number to NetBSD values. 904 */ 905 int 906 linux_to_bsd_ip_sockopt(int lopt) 907 { 908 909 switch (lopt) { 910 case LINUX_IP_TOS: 911 return IP_TOS; 912 case LINUX_IP_TTL: 913 return IP_TTL; 914 case LINUX_IP_HDRINCL: 915 return IP_HDRINCL; 916 case LINUX_IP_MULTICAST_TTL: 917 return IP_MULTICAST_TTL; 918 case LINUX_IP_MULTICAST_LOOP: 919 return IP_MULTICAST_LOOP; 920 case LINUX_IP_MULTICAST_IF: 921 return IP_MULTICAST_IF; 922 case LINUX_IP_ADD_MEMBERSHIP: 923 return IP_ADD_MEMBERSHIP; 924 case LINUX_IP_DROP_MEMBERSHIP: 925 return IP_DROP_MEMBERSHIP; 926 default: 927 return -1; 928 } 929 } 930 931 /* 932 * Convert Linux IPV6 level socket option number to NetBSD values. 933 */ 934 #ifdef INET6 935 int 936 linux_to_bsd_ipv6_sockopt(int lopt) 937 { 938 939 switch (lopt) { 940 case LINUX_IPV6_V6ONLY: 941 return IPV6_V6ONLY; 942 default: 943 return -1; 944 } 945 } 946 #endif 947 948 /* 949 * Convert Linux TCP level socket option number to NetBSD values. 950 */ 951 int 952 linux_to_bsd_tcp_sockopt(int lopt) 953 { 954 955 switch (lopt) { 956 case LINUX_TCP_NODELAY: 957 return TCP_NODELAY; 958 case LINUX_TCP_MAXSEG: 959 return TCP_MAXSEG; 960 default: 961 return -1; 962 } 963 } 964 965 /* 966 * Convert Linux UDP level socket option number to NetBSD values. 967 */ 968 int 969 linux_to_bsd_udp_sockopt(int lopt) 970 { 971 972 switch (lopt) { 973 default: 974 return -1; 975 } 976 } 977 978 /* 979 * Another reasonably straightforward function: setsockopt(2). 980 * The level and option numbers are converted; the values passed 981 * are not (yet) converted, the ones currently implemented don't 982 * need conversion, as they are the same on both systems. 983 */ 984 int 985 linux_sys_setsockopt(struct lwp *l, const struct linux_sys_setsockopt_args *uap, register_t *retval) 986 { 987 /* { 988 syscallarg(int) s; 989 syscallarg(int) level; 990 syscallarg(int) optname; 991 syscallarg(void *) optval; 992 syscallarg(int) optlen; 993 } */ 994 struct sys_setsockopt_args bsa; 995 int name; 996 997 SCARG(&bsa, s) = SCARG(uap, s); 998 SCARG(&bsa, level) = linux_to_bsd_sopt_level(SCARG(uap, level)); 999 SCARG(&bsa, val) = SCARG(uap, optval); 1000 SCARG(&bsa, valsize) = SCARG(uap, optlen); 1001 1002 /* 1003 * Linux supports only SOL_SOCKET for AF_LOCAL domain sockets 1004 * and returns EOPNOTSUPP for other levels 1005 */ 1006 if (SCARG(&bsa, level) != SOL_SOCKET) { 1007 struct socket *so; 1008 int error, family; 1009 1010 /* fd_getsock() will use the descriptor for us */ 1011 if ((error = fd_getsock(SCARG(&bsa, s), &so)) != 0) 1012 return error; 1013 family = so->so_proto->pr_domain->dom_family; 1014 fd_putfile(SCARG(&bsa, s)); 1015 1016 if (family == AF_LOCAL) 1017 return EOPNOTSUPP; 1018 } 1019 1020 switch (SCARG(&bsa, level)) { 1021 case SOL_SOCKET: 1022 name = linux_to_bsd_so_sockopt(SCARG(uap, optname)); 1023 break; 1024 case IPPROTO_IP: 1025 name = linux_to_bsd_ip_sockopt(SCARG(uap, optname)); 1026 break; 1027 #ifdef INET6 1028 case IPPROTO_IPV6: 1029 name = linux_to_bsd_ipv6_sockopt(SCARG(uap, optname)); 1030 break; 1031 #endif 1032 case IPPROTO_TCP: 1033 name = linux_to_bsd_tcp_sockopt(SCARG(uap, optname)); 1034 break; 1035 case IPPROTO_UDP: 1036 name = linux_to_bsd_udp_sockopt(SCARG(uap, optname)); 1037 break; 1038 default: 1039 return EINVAL; 1040 } 1041 1042 if (name == -1) 1043 return EINVAL; 1044 SCARG(&bsa, name) = name; 1045 1046 return sys_setsockopt(l, &bsa, retval); 1047 } 1048 1049 /* 1050 * getsockopt(2) is very much the same as setsockopt(2) (see above) 1051 */ 1052 int 1053 linux_sys_getsockopt(struct lwp *l, const struct linux_sys_getsockopt_args *uap, register_t *retval) 1054 { 1055 /* { 1056 syscallarg(int) s; 1057 syscallarg(int) level; 1058 syscallarg(int) optname; 1059 syscallarg(void *) optval; 1060 syscallarg(int *) optlen; 1061 } */ 1062 struct sys_getsockopt_args bga; 1063 int name; 1064 1065 SCARG(&bga, s) = SCARG(uap, s); 1066 SCARG(&bga, level) = linux_to_bsd_sopt_level(SCARG(uap, level)); 1067 SCARG(&bga, val) = SCARG(uap, optval); 1068 SCARG(&bga, avalsize) = (socklen_t *)SCARG(uap, optlen); 1069 1070 switch (SCARG(&bga, level)) { 1071 case SOL_SOCKET: 1072 name = linux_to_bsd_so_sockopt(SCARG(uap, optname)); 1073 break; 1074 case IPPROTO_IP: 1075 name = linux_to_bsd_ip_sockopt(SCARG(uap, optname)); 1076 break; 1077 #ifdef INET6 1078 case IPPROTO_IPV6: 1079 name = linux_to_bsd_ipv6_sockopt(SCARG(uap, optname)); 1080 break; 1081 #endif 1082 case IPPROTO_TCP: 1083 name = linux_to_bsd_tcp_sockopt(SCARG(uap, optname)); 1084 break; 1085 case IPPROTO_UDP: 1086 name = linux_to_bsd_udp_sockopt(SCARG(uap, optname)); 1087 break; 1088 default: 1089 return EINVAL; 1090 } 1091 1092 if (name == -1) 1093 return EINVAL; 1094 SCARG(&bga, name) = name; 1095 1096 return sys_getsockopt(l, &bga, retval); 1097 } 1098 1099 int 1100 linux_getifname(struct lwp *l, register_t *retval, void *data) 1101 { 1102 struct ifnet *ifp; 1103 struct linux_ifreq ifr; 1104 int error; 1105 int s; 1106 1107 error = copyin(data, &ifr, sizeof(ifr)); 1108 if (error) 1109 return error; 1110 1111 s = pserialize_read_enter(); 1112 ifp = if_byindex(ifr.ifr_ifru.ifru_ifindex); 1113 if (ifp == NULL) { 1114 pserialize_read_exit(s); 1115 return ENODEV; 1116 } 1117 1118 strncpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)); 1119 pserialize_read_exit(s); 1120 1121 return copyout(&ifr, data, sizeof(ifr)); 1122 } 1123 1124 int 1125 linux_getifconf(struct lwp *l, register_t *retval, void *data) 1126 { 1127 struct linux_ifreq ifr, *ifrp = NULL; 1128 struct linux_ifconf ifc; 1129 struct ifnet *ifp; 1130 struct sockaddr *sa; 1131 struct osockaddr *osa; 1132 int space = 0, error; 1133 const int sz = (int)sizeof(ifr); 1134 bool docopy; 1135 int s; 1136 int bound; 1137 struct psref psref; 1138 1139 error = copyin(data, &ifc, sizeof(ifc)); 1140 if (error) 1141 return error; 1142 1143 memset(&ifr, 0, sizeof(ifr)); 1144 docopy = ifc.ifc_req != NULL; 1145 if (docopy) { 1146 space = ifc.ifc_len; 1147 ifrp = ifc.ifc_req; 1148 } 1149 1150 bound = curlwp_bind(); 1151 s = pserialize_read_enter(); 1152 IFNET_READER_FOREACH(ifp) { 1153 struct ifaddr *ifa; 1154 if_acquire(ifp, &psref); 1155 pserialize_read_exit(s); 1156 1157 (void)strncpy(ifr.ifr_name, ifp->if_xname, 1158 sizeof(ifr.ifr_name)); 1159 if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0') { 1160 error = ENAMETOOLONG; 1161 goto release_exit; 1162 } 1163 1164 s = pserialize_read_enter(); 1165 IFADDR_READER_FOREACH(ifa, ifp) { 1166 struct psref psref_ifa; 1167 ifa_acquire(ifa, &psref_ifa); 1168 pserialize_read_exit(s); 1169 1170 sa = ifa->ifa_addr; 1171 if (sa->sa_family != AF_INET || 1172 sa->sa_len > sizeof(*osa)) 1173 goto next; 1174 memcpy(&ifr.ifr_addr, sa, sa->sa_len); 1175 osa = (struct osockaddr *)&ifr.ifr_addr; 1176 osa->sa_family = sa->sa_family; 1177 if (space >= sz) { 1178 error = copyout(&ifr, ifrp, sz); 1179 if (error != 0) { 1180 ifa_release(ifa, &psref_ifa); 1181 goto release_exit; 1182 } 1183 ifrp++; 1184 } 1185 space -= sz; 1186 next: 1187 s = pserialize_read_enter(); 1188 ifa_release(ifa, &psref_ifa); 1189 } 1190 1191 KASSERT(pserialize_in_read_section()); 1192 if_release(ifp, &psref); 1193 } 1194 pserialize_read_exit(s); 1195 curlwp_bindx(bound); 1196 1197 if (docopy) 1198 ifc.ifc_len -= space; 1199 else 1200 ifc.ifc_len = -space; 1201 1202 return copyout(&ifc, data, sizeof(ifc)); 1203 1204 release_exit: 1205 if_release(ifp, &psref); 1206 curlwp_bindx(bound); 1207 return error; 1208 } 1209 1210 int 1211 linux_getifhwaddr(struct lwp *l, register_t *retval, u_int fd, 1212 void *data) 1213 { 1214 /* Not the full structure, just enough to map what we do here */ 1215 struct linux_ifreq lreq; 1216 file_t *fp; 1217 struct ifaddr *ifa; 1218 struct ifnet *ifp; 1219 struct sockaddr_dl *sadl; 1220 int error, found; 1221 int index, ifnum; 1222 int s; 1223 1224 /* 1225 * We can't emulate this ioctl by calling sys_ioctl() to run 1226 * SIOCGIFCONF, because the user buffer is not of the right 1227 * type to take those results. We can't use kernel buffers to 1228 * receive the results, as the implementation of sys_ioctl() 1229 * and ifconf() [which implements SIOCGIFCONF] use 1230 * copyin()/copyout() which will fail on kernel addresses. 1231 * 1232 * So, we must duplicate code from sys_ioctl() and ifconf(). Ugh. 1233 */ 1234 1235 if ((fp = fd_getfile(fd)) == NULL) 1236 return (EBADF); 1237 1238 KERNEL_LOCK(1, NULL); 1239 1240 if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 1241 error = EBADF; 1242 goto out; 1243 } 1244 1245 error = copyin(data, &lreq, sizeof(lreq)); 1246 if (error) 1247 goto out; 1248 lreq.ifr_name[LINUX_IFNAMSIZ-1] = '\0'; /* just in case */ 1249 1250 /* 1251 * Try real interface name first, then fake "ethX" 1252 */ 1253 found = 0; 1254 s = pserialize_read_enter(); 1255 IFNET_READER_FOREACH(ifp) { 1256 if (found) 1257 break; 1258 if (strcmp(lreq.ifr_name, ifp->if_xname)) 1259 /* not this interface */ 1260 continue; 1261 1262 found=1; 1263 if (IFADDR_READER_EMPTY(ifp)) { 1264 pserialize_read_exit(s); 1265 error = ENODEV; 1266 goto out; 1267 } 1268 IFADDR_READER_FOREACH(ifa, ifp) { 1269 sadl = satosdl(ifa->ifa_addr); 1270 /* only return ethernet addresses */ 1271 /* XXX what about FDDI, etc. ? */ 1272 if (sadl->sdl_family != AF_LINK || 1273 sadl->sdl_type != IFT_ETHER) 1274 continue; 1275 memcpy(&lreq.ifr_hwaddr.sa_data, CLLADDR(sadl), 1276 MIN(sadl->sdl_alen, 1277 sizeof(lreq.ifr_hwaddr.sa_data))); 1278 lreq.ifr_hwaddr.sa_family = 1279 sadl->sdl_family; 1280 pserialize_read_exit(s); 1281 1282 error = copyout(&lreq, data, sizeof(lreq)); 1283 goto out; 1284 } 1285 } 1286 pserialize_read_exit(s); 1287 1288 if (strncmp(lreq.ifr_name, "eth", 3) != 0) { 1289 /* unknown interface, not even an "eth*" name */ 1290 error = ENODEV; 1291 goto out; 1292 } 1293 1294 for (ifnum = 0, index = 3; 1295 index < LINUX_IFNAMSIZ && lreq.ifr_name[index] != '\0'; 1296 index++) { 1297 ifnum *= 10; 1298 ifnum += lreq.ifr_name[index] - '0'; 1299 } 1300 1301 error = EINVAL; /* in case we don't find one */ 1302 s = pserialize_read_enter(); 1303 IFNET_READER_FOREACH(ifp) { 1304 memcpy(lreq.ifr_name, ifp->if_xname, 1305 MIN(LINUX_IFNAMSIZ, IFNAMSIZ)); 1306 IFADDR_READER_FOREACH(ifa, ifp) { 1307 sadl = satosdl(ifa->ifa_addr); 1308 /* only return ethernet addresses */ 1309 /* XXX what about FDDI, etc. ? */ 1310 if (sadl->sdl_family != AF_LINK || 1311 sadl->sdl_type != IFT_ETHER) 1312 continue; 1313 if (ifnum--) 1314 /* not the reqested iface */ 1315 continue; 1316 memcpy(&lreq.ifr_hwaddr.sa_data, 1317 CLLADDR(sadl), 1318 MIN(sadl->sdl_alen, 1319 sizeof(lreq.ifr_hwaddr.sa_data))); 1320 lreq.ifr_hwaddr.sa_family = 1321 sadl->sdl_family; 1322 pserialize_read_exit(s); 1323 1324 error = copyout(&lreq, data, sizeof(lreq)); 1325 goto out; 1326 } 1327 } 1328 pserialize_read_exit(s); 1329 1330 out: 1331 KERNEL_UNLOCK_ONE(NULL); 1332 fd_putfile(fd); 1333 return error; 1334 } 1335 1336 int 1337 linux_ioctl_socket(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval) 1338 { 1339 /* { 1340 syscallarg(int) fd; 1341 syscallarg(u_long) com; 1342 syscallarg(void *) data; 1343 } */ 1344 u_long com; 1345 int error = 0, isdev = 0, dosys = 1; 1346 struct sys_ioctl_args ia; 1347 file_t *fp; 1348 struct vnode *vp; 1349 int (*ioctlf)(file_t *, u_long, void *); 1350 struct ioctl_pt pt; 1351 1352 if ((fp = fd_getfile(SCARG(uap, fd))) == NULL) 1353 return (EBADF); 1354 1355 if (fp->f_type == DTYPE_VNODE) { 1356 vp = (struct vnode *)fp->f_data; 1357 isdev = vp->v_type == VCHR; 1358 } 1359 1360 /* 1361 * Don't try to interpret socket ioctl calls that are done 1362 * on a device filedescriptor, just pass them through, to 1363 * emulate Linux behaviour. Use PTIOCLINUX so that the 1364 * device will only handle these if it's prepared to do 1365 * so, to avoid unexpected things from happening. 1366 */ 1367 if (isdev) { 1368 dosys = 0; 1369 ioctlf = fp->f_ops->fo_ioctl; 1370 pt.com = SCARG(uap, com); 1371 pt.data = SCARG(uap, data); 1372 error = ioctlf(fp, PTIOCLINUX, &pt); 1373 /* 1374 * XXX hack: if the function returns EJUSTRETURN, 1375 * it has stuffed a sysctl return value in pt.data. 1376 */ 1377 if (error == EJUSTRETURN) { 1378 retval[0] = (register_t)pt.data; 1379 error = 0; 1380 } 1381 goto out; 1382 } 1383 1384 com = SCARG(uap, com); 1385 retval[0] = 0; 1386 1387 switch (com) { 1388 case LINUX_SIOCGIFNAME: 1389 error = linux_getifname(l, retval, SCARG(uap, data)); 1390 dosys = 0; 1391 break; 1392 case LINUX_SIOCGIFCONF: 1393 error = linux_getifconf(l, retval, SCARG(uap, data)); 1394 dosys = 0; 1395 break; 1396 case LINUX_SIOCGIFFLAGS: 1397 SCARG(&ia, com) = OSIOCGIFFLAGS; 1398 break; 1399 case LINUX_SIOCSIFFLAGS: 1400 SCARG(&ia, com) = OSIOCSIFFLAGS; 1401 break; 1402 case LINUX_SIOCGIFADDR: 1403 SCARG(&ia, com) = OOSIOCGIFADDR; 1404 break; 1405 case LINUX_SIOCGIFDSTADDR: 1406 SCARG(&ia, com) = OOSIOCGIFDSTADDR; 1407 break; 1408 case LINUX_SIOCGIFBRDADDR: 1409 SCARG(&ia, com) = OOSIOCGIFBRDADDR; 1410 break; 1411 case LINUX_SIOCGIFNETMASK: 1412 SCARG(&ia, com) = OOSIOCGIFNETMASK; 1413 break; 1414 case LINUX_SIOCGIFMTU: 1415 SCARG(&ia, com) = OSIOCGIFMTU; 1416 break; 1417 case LINUX_SIOCADDMULTI: 1418 SCARG(&ia, com) = OSIOCADDMULTI; 1419 break; 1420 case LINUX_SIOCDELMULTI: 1421 SCARG(&ia, com) = OSIOCDELMULTI; 1422 break; 1423 case LINUX_SIOCGIFHWADDR: 1424 error = linux_getifhwaddr(l, retval, SCARG(uap, fd), 1425 SCARG(uap, data)); 1426 dosys = 0; 1427 break; 1428 default: 1429 error = EINVAL; 1430 } 1431 1432 out: 1433 fd_putfile(SCARG(uap, fd)); 1434 1435 if (error ==0 && dosys) { 1436 SCARG(&ia, fd) = SCARG(uap, fd); 1437 SCARG(&ia, data) = SCARG(uap, data); 1438 error = sys_ioctl(curlwp, &ia, retval); 1439 } 1440 1441 return error; 1442 } 1443 1444 int 1445 linux_sys_connect(struct lwp *l, const struct linux_sys_connect_args *uap, register_t *retval) 1446 { 1447 /* { 1448 syscallarg(int) s; 1449 syscallarg(const struct sockaddr *) name; 1450 syscallarg(int) namelen; 1451 } */ 1452 int error; 1453 struct sockaddr_big sb; 1454 1455 error = linux_get_sa(l, SCARG(uap, s), &sb, SCARG(uap, name), 1456 SCARG(uap, namelen)); 1457 if (error) 1458 return (error); 1459 1460 error = do_sys_connect(l, SCARG(uap, s), (struct sockaddr *)&sb); 1461 1462 if (error == EISCONN) { 1463 struct socket *so; 1464 int state, prflags; 1465 1466 /* fd_getsock() will use the descriptor for us */ 1467 if (fd_getsock(SCARG(uap, s), &so) != 0) 1468 return EISCONN; 1469 1470 solock(so); 1471 state = so->so_state; 1472 prflags = so->so_proto->pr_flags; 1473 sounlock(so); 1474 fd_putfile(SCARG(uap, s)); 1475 /* 1476 * We should only let this call succeed once per 1477 * non-blocking connect; however we don't have 1478 * a convenient place to keep that state.. 1479 */ 1480 if ((state & (SS_ISCONNECTED|SS_NBIO)) == 1481 (SS_ISCONNECTED|SS_NBIO) && 1482 (prflags & PR_CONNREQUIRED)) 1483 return 0; 1484 } 1485 1486 return (error); 1487 } 1488 1489 int 1490 linux_sys_bind(struct lwp *l, const struct linux_sys_bind_args *uap, register_t *retval) 1491 { 1492 /* { 1493 syscallarg(int) s; 1494 syscallarg(const struct osockaddr *) name; 1495 syscallarg(int) namelen; 1496 } */ 1497 int error; 1498 struct sockaddr_big sb; 1499 1500 error = linux_get_sa(l, SCARG(uap, s), &sb, SCARG(uap, name), 1501 SCARG(uap, namelen)); 1502 if (error) 1503 return (error); 1504 1505 return do_sys_bind(l, SCARG(uap, s), (struct sockaddr *)&sb); 1506 } 1507 1508 int 1509 linux_sys_getsockname(struct lwp *l, const struct linux_sys_getsockname_args *uap, register_t *retval) 1510 { 1511 /* { 1512 syscallarg(int) fdes; 1513 syscallarg(void *) asa; 1514 syscallarg(int *) alen; 1515 } */ 1516 int error; 1517 1518 if ((error = sys_getsockname(l, (const void *)uap, retval)) != 0) 1519 return (error); 1520 1521 if ((error = linux_sa_put((struct osockaddr *)SCARG(uap, asa)))) 1522 return (error); 1523 1524 return (0); 1525 } 1526 1527 int 1528 linux_sys_getpeername(struct lwp *l, const struct linux_sys_getpeername_args *uap, register_t *retval) 1529 { 1530 /* { 1531 syscallarg(int) fdes; 1532 syscallarg(void *) asa; 1533 syscallarg(int *) alen; 1534 } */ 1535 int error; 1536 1537 if ((error = sys_getpeername(l, (const void *)uap, retval)) != 0) 1538 return (error); 1539 1540 if ((error = linux_sa_put((struct osockaddr *)SCARG(uap, asa)))) 1541 return (error); 1542 1543 return (0); 1544 } 1545 1546 /* 1547 * Copy the osockaddr structure pointed to by name to sb, adjust 1548 * family and convert to sockaddr. 1549 */ 1550 static int 1551 linux_get_sa(struct lwp *l, int s, struct sockaddr_big *sb, 1552 const struct osockaddr *name, socklen_t namelen) 1553 { 1554 int error, bdom; 1555 1556 if (namelen > UCHAR_MAX || 1557 namelen <= offsetof(struct sockaddr_big, sb_data)) 1558 return EINVAL; 1559 1560 error = copyin(name, sb, namelen); 1561 if (error) 1562 return error; 1563 1564 bdom = linux_to_bsd_domain(sb->sb_family); 1565 if (bdom == -1) 1566 return EINVAL; 1567 1568 /* 1569 * If the family is unspecified, use address family of the socket. 1570 * This avoid triggering strict family checks in netinet/in_pcb.c et.al. 1571 */ 1572 if (bdom == AF_UNSPEC) { 1573 struct socket *so; 1574 1575 /* fd_getsock() will use the descriptor for us */ 1576 if ((error = fd_getsock(s, &so)) != 0) 1577 return error; 1578 1579 bdom = so->so_proto->pr_domain->dom_family; 1580 fd_putfile(s); 1581 } 1582 1583 /* 1584 * Older Linux IPv6 code uses obsolete RFC2133 struct sockaddr_in6, 1585 * which lacks the scope id compared with RFC2553 one. If we detect 1586 * the situation, reject the address and write a message to system log. 1587 * 1588 * Still accept addresses for which the scope id is not used. 1589 */ 1590 if (bdom == AF_INET6 && 1591 namelen == sizeof(struct sockaddr_in6) - sizeof(uint32_t)) { 1592 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sb; 1593 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) && 1594 (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) || 1595 IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) || 1596 IN6_IS_ADDR_V4COMPAT(&sin6->sin6_addr) || 1597 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) || 1598 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))) { 1599 struct proc *p = l->l_proc; 1600 int uid = l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1; 1601 1602 log(LOG_DEBUG, 1603 "pid %d (%s), uid %d: obsolete pre-RFC2553 " 1604 "sockaddr_in6 rejected", 1605 p->p_pid, p->p_comm, uid); 1606 return EINVAL; 1607 } 1608 namelen = sizeof(struct sockaddr_in6); 1609 sin6->sin6_scope_id = 0; 1610 } 1611 1612 sb->sb_family = bdom; 1613 sb->sb_len = namelen; 1614 ktrkuser("mbsoname", sb, namelen); 1615 return 0; 1616 } 1617 1618 static int 1619 linux_sa_put(struct osockaddr *osa) 1620 { 1621 struct sockaddr sa; 1622 struct osockaddr *kosa; 1623 int error, bdom, len; 1624 1625 /* 1626 * Only read/write the sockaddr family and length part, the rest is 1627 * not changed. 1628 */ 1629 len = sizeof(sa.sa_len) + sizeof(sa.sa_family); 1630 1631 error = copyin(osa, &sa, len); 1632 if (error) 1633 return (error); 1634 1635 bdom = bsd_to_linux_domain(sa.sa_family); 1636 if (bdom == -1) 1637 return (EINVAL); 1638 1639 /* Note: we convert from sockaddr to osockaddr here, too */ 1640 kosa = (struct osockaddr *) &sa; 1641 kosa->sa_family = bdom; 1642 error = copyout(kosa, osa, len); 1643 if (error) 1644 return (error); 1645 1646 return (0); 1647 } 1648 1649 #ifndef __amd64__ 1650 int 1651 linux_sys_recv(struct lwp *l, const struct linux_sys_recv_args *uap, register_t *retval) 1652 { 1653 /* { 1654 syscallarg(int) s; 1655 syscallarg(void *) buf; 1656 syscallarg(int) len; 1657 syscallarg(int) flags; 1658 } */ 1659 struct sys_recvfrom_args bra; 1660 1661 1662 SCARG(&bra, s) = SCARG(uap, s); 1663 SCARG(&bra, buf) = SCARG(uap, buf); 1664 SCARG(&bra, len) = (size_t) SCARG(uap, len); 1665 SCARG(&bra, flags) = SCARG(uap, flags); 1666 SCARG(&bra, from) = NULL; 1667 SCARG(&bra, fromlenaddr) = NULL; 1668 1669 return (sys_recvfrom(l, &bra, retval)); 1670 } 1671 1672 int 1673 linux_sys_send(struct lwp *l, const struct linux_sys_send_args *uap, register_t *retval) 1674 { 1675 /* { 1676 syscallarg(int) s; 1677 syscallarg(void *) buf; 1678 syscallarg(int) len; 1679 syscallarg(int) flags; 1680 } */ 1681 struct sys_sendto_args bsa; 1682 1683 SCARG(&bsa, s) = SCARG(uap, s); 1684 SCARG(&bsa, buf) = SCARG(uap, buf); 1685 SCARG(&bsa, len) = SCARG(uap, len); 1686 SCARG(&bsa, flags) = SCARG(uap, flags); 1687 SCARG(&bsa, to) = NULL; 1688 SCARG(&bsa, tolen) = 0; 1689 1690 return (sys_sendto(l, &bsa, retval)); 1691 } 1692 #endif 1693 1694 int 1695 linux_sys_accept(struct lwp *l, const struct linux_sys_accept_args *uap, register_t *retval) 1696 { 1697 /* { 1698 syscallarg(int) s; 1699 syscallarg(struct osockaddr *) name; 1700 syscallarg(int *) anamelen; 1701 } */ 1702 int error; 1703 struct sys_accept_args baa; 1704 1705 SCARG(&baa, s) = SCARG(uap, s); 1706 SCARG(&baa, name) = (struct sockaddr *) SCARG(uap, name); 1707 SCARG(&baa, anamelen) = (unsigned int *) SCARG(uap, anamelen); 1708 1709 if ((error = sys_accept(l, &baa, retval))) 1710 return (error); 1711 1712 if (SCARG(uap, name) && (error = linux_sa_put(SCARG(uap, name)))) 1713 return (error); 1714 1715 return (0); 1716 } 1717 1718 int 1719 linux_sys_accept4(struct lwp *l, const struct linux_sys_accept4_args *uap, register_t *retval) 1720 { 1721 /* { 1722 syscallarg(int) s; 1723 syscallarg(struct osockaddr *) name; 1724 syscallarg(int *) anamelen; 1725 syscallarg(int) flags; 1726 } */ 1727 int error, flags; 1728 struct sockaddr_big name; 1729 1730 if ((flags = linux_to_bsd_type(SCARG(uap, flags))) == -1) 1731 return EINVAL; 1732 1733 name.sb_len = UCHAR_MAX; 1734 error = do_sys_accept(l, SCARG(uap, s), (struct sockaddr *)&name, 1735 retval, NULL, flags, 0); 1736 if (error != 0) 1737 return error; 1738 1739 error = copyout_sockname_sb((struct sockaddr *)SCARG(uap, name), 1740 SCARG(uap, anamelen), MSG_LENUSRSPACE, &name); 1741 if (error != 0) { 1742 int fd = (int)*retval; 1743 if (fd_getfile(fd) != NULL) 1744 (void)fd_close(fd); 1745 return error; 1746 } 1747 if (SCARG(uap, name) && (error = linux_sa_put(SCARG(uap, name)))) 1748 return error; 1749 1750 return 0; 1751 } 1752 1753 int 1754 linux_sys_sendmmsg(struct lwp *l, const struct linux_sys_sendmmsg_args *uap, 1755 register_t *retval) 1756 { 1757 /* { 1758 syscallarg(int) s; 1759 syscallarg(struct linux_mmsghdr *) msgvec; 1760 syscallarg(unsigned int) vlen; 1761 syscallarg(unsigned int) flags; 1762 } */ 1763 struct linux_mmsghdr lmsg; 1764 struct mmsghdr bmsg; 1765 struct socket *so; 1766 file_t *fp; 1767 struct msghdr *msg = &bmsg.msg_hdr; 1768 int error, s; 1769 unsigned int vlen, flags, dg; 1770 1771 if ((flags = linux_to_bsd_msg_flags(SCARG(uap, flags))) == -1) 1772 return EINVAL; 1773 1774 flags = (flags & MSG_USERFLAGS) | MSG_IOVUSRSPACE; 1775 1776 s = SCARG(uap, s); 1777 if ((error = fd_getsock1(s, &so, &fp)) != 0) 1778 return error; 1779 1780 vlen = SCARG(uap, vlen); 1781 if (vlen > 1024) 1782 vlen = 1024; 1783 1784 for (dg = 0; dg < vlen;) { 1785 error = copyin(SCARG(uap, msgvec) + dg, &lmsg, sizeof(lmsg)); 1786 if (error) 1787 break; 1788 linux_to_bsd_msghdr(&lmsg.msg_hdr, &bmsg.msg_hdr); 1789 1790 msg->msg_flags = flags; 1791 1792 error = do_sys_sendmsg_so(l, s, so, fp, msg, flags, retval); 1793 if (error) 1794 break; 1795 1796 ktrkuser("msghdr", msg, sizeof *msg); 1797 lmsg.msg_len = *retval; 1798 error = copyout(&lmsg, SCARG(uap, msgvec) + dg, sizeof(lmsg)); 1799 if (error) 1800 break; 1801 dg++; 1802 1803 } 1804 1805 *retval = dg; 1806 1807 fd_putfile(s); 1808 1809 /* 1810 * If we succeeded at least once, return 0. 1811 */ 1812 if (dg) 1813 return 0; 1814 return error; 1815 } 1816 1817 int 1818 linux_sys_recvmmsg(struct lwp *l, const struct linux_sys_recvmmsg_args *uap, 1819 register_t *retval) 1820 { 1821 /* { 1822 syscallarg(int) s; 1823 syscallarg(struct linux_mmsghdr *) msgvec; 1824 syscallarg(unsigned int) vlen; 1825 syscallarg(unsigned int) flags; 1826 syscallarg(struct linux_timespec *) timeout; 1827 } */ 1828 struct linux_mmsghdr lmsg; 1829 struct mmsghdr bmsg; 1830 struct socket *so; 1831 struct msghdr *msg = &bmsg.msg_hdr; 1832 int error, s; 1833 struct mbuf *from, *control; 1834 struct timespec ts = {0}, now; 1835 struct linux_timespec lts; 1836 unsigned int vlen, flags, dg; 1837 1838 if (SCARG(uap, timeout)) { 1839 error = copyin(SCARG(uap, timeout), <s, sizeof(lts)); 1840 return error; 1841 ts.tv_sec = lts.tv_sec; 1842 ts.tv_nsec = lts.tv_nsec; 1843 getnanotime(&now); 1844 timespecadd(&now, &ts, &ts); 1845 } 1846 1847 s = SCARG(uap, s); 1848 if ((error = fd_getsock(s, &so)) != 0) 1849 return error; 1850 1851 /* 1852 * If so->so_rerror holds a deferred error return it now. 1853 */ 1854 if (so->so_rerror) { 1855 error = so->so_rerror; 1856 so->so_rerror = 0; 1857 fd_putfile(s); 1858 return error; 1859 } 1860 1861 vlen = SCARG(uap, vlen); 1862 if (vlen > 1024) 1863 vlen = 1024; 1864 1865 from = NULL; 1866 flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE; 1867 1868 for (dg = 0; dg < vlen;) { 1869 error = copyin(SCARG(uap, msgvec) + dg, &lmsg, sizeof(lmsg)); 1870 if (error) 1871 break; 1872 linux_to_bsd_msghdr(&lmsg.msg_hdr, &bmsg.msg_hdr); 1873 msg->msg_flags = flags & ~MSG_WAITFORONE; 1874 1875 if (from != NULL) { 1876 m_free(from); 1877 from = NULL; 1878 } 1879 1880 error = do_sys_recvmsg_so(l, s, so, msg, &from, 1881 msg->msg_control != NULL ? &control : NULL, retval); 1882 if (error) { 1883 if (error == EAGAIN && dg > 0) 1884 error = 0; 1885 break; 1886 } 1887 1888 if (msg->msg_control != NULL) 1889 error = linux_copyout_msg_control(l, msg, control); 1890 if (error) 1891 break; 1892 1893 if (from != NULL) { 1894 mtod(from, struct osockaddr *)->sa_family = 1895 bsd_to_linux_domain(mtod(from, 1896 struct sockaddr *)->sa_family); 1897 error = copyout_sockname(msg->msg_name, 1898 &msg->msg_namelen, 0, from); 1899 if (error) 1900 break; 1901 } 1902 1903 1904 lmsg.msg_len = *retval; 1905 ktrkuser("msghdr", msg, sizeof(*msg)); 1906 bsd_to_linux_msghdr(msg, &lmsg.msg_hdr); 1907 error = copyout(&lmsg, SCARG(uap, msgvec) + dg, sizeof(lmsg)); 1908 if (error) 1909 break; 1910 1911 dg++; 1912 if (msg->msg_flags & MSG_OOB) 1913 break; 1914 1915 if (SCARG(uap, timeout)) { 1916 getnanotime(&now); 1917 timespecsub(&now, &ts, &now); 1918 if (now.tv_sec > 0) 1919 break; 1920 } 1921 1922 if (flags & MSG_WAITFORONE) 1923 flags |= MSG_DONTWAIT; 1924 1925 } 1926 1927 if (from != NULL) 1928 m_free(from); 1929 1930 *retval = dg; 1931 1932 /* 1933 * If we succeeded at least once, return 0, hopefully so->so_rerror 1934 * will catch it next time. 1935 */ 1936 if (error && dg > 0) { 1937 so->so_rerror = error; 1938 error = 0; 1939 } 1940 1941 fd_putfile(s); 1942 1943 return error; 1944 } 1945