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