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