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