1 /* $NetBSD: linux_socket.c,v 1.64 2006/06/27 09:09:40 pavel Exp $ */ 2 3 /*- 4 * Copyright (c) 1995, 1998 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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * Functions in multiarch: 41 * linux_sys_socketcall : linux_socketcall.c 42 */ 43 44 #include <sys/cdefs.h> 45 __KERNEL_RCSID(0, "$NetBSD: linux_socket.c,v 1.64 2006/06/27 09:09:40 pavel Exp $"); 46 47 #if defined(_KERNEL_OPT) 48 #include "opt_inet.h" 49 #endif 50 51 #include <sys/param.h> 52 #include <sys/kernel.h> 53 #include <sys/systm.h> 54 #include <sys/buf.h> 55 #include <sys/malloc.h> 56 #include <sys/ioctl.h> 57 #include <sys/tty.h> 58 #include <sys/file.h> 59 #include <sys/filedesc.h> 60 #include <sys/select.h> 61 #include <sys/socket.h> 62 #include <sys/socketvar.h> 63 #include <sys/domain.h> 64 #include <net/if.h> 65 #include <net/if_dl.h> 66 #include <net/if_types.h> 67 #include <netinet/in.h> 68 #include <netinet/tcp.h> 69 #include <sys/mount.h> 70 #include <sys/proc.h> 71 #include <sys/vnode.h> 72 #include <sys/device.h> 73 #include <sys/protosw.h> 74 #include <sys/mbuf.h> 75 #include <sys/syslog.h> 76 #include <sys/exec.h> 77 #include <sys/kauth.h> 78 79 #include <sys/sa.h> 80 #include <sys/syscallargs.h> 81 82 #include <lib/libkern/libkern.h> 83 84 #ifdef INET6 85 #include <netinet/ip6.h> 86 #include <netinet6/ip6_var.h> 87 #endif 88 89 #include <compat/sys/socket.h> 90 91 #include <compat/linux/common/linux_types.h> 92 #include <compat/linux/common/linux_util.h> 93 #include <compat/linux/common/linux_signal.h> 94 #include <compat/linux/common/linux_ioctl.h> 95 #include <compat/linux/common/linux_socket.h> 96 #if !defined(__alpha__) && !defined(__amd64__) 97 #include <compat/linux/common/linux_socketcall.h> 98 #endif 99 #include <compat/linux/common/linux_sockio.h> 100 101 #include <compat/linux/linux_syscallargs.h> 102 103 #ifdef DEBUG_LINUX 104 #define DPRINTF(a) uprintf a 105 #else 106 #define DPRINTF(a) 107 #endif 108 109 /* 110 * The calls in this file are entered either via the linux_socketcall() 111 * interface or, on the Alpha, as individual syscalls. The 112 * linux_socketcall function does any massaging of arguments so that all 113 * the calls in here need not think that they are anything other 114 * than a normal syscall. 115 */ 116 117 static int linux_to_bsd_domain __P((int)); 118 static int bsd_to_linux_domain __P((int)); 119 int linux_to_bsd_sopt_level __P((int)); 120 int linux_to_bsd_so_sockopt __P((int)); 121 int linux_to_bsd_ip_sockopt __P((int)); 122 int linux_to_bsd_tcp_sockopt __P((int)); 123 int linux_to_bsd_udp_sockopt __P((int)); 124 int linux_getifhwaddr __P((struct lwp *, register_t *, u_int, void *)); 125 static int linux_sa_get __P((struct lwp *, int, caddr_t *, struct sockaddr **, 126 const struct osockaddr *, socklen_t *)); 127 static int linux_sa_put __P((struct osockaddr *osa)); 128 static int linux_to_bsd_msg_flags __P((int)); 129 static int bsd_to_linux_msg_flags __P((int)); 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 {-1, /* not supp */ LINUX_MSG_PROBE}, 207 {-1, /* not supp */ LINUX_MSG_FIN}, 208 {-1, /* not supp */ LINUX_MSG_SYN}, 209 {-1, /* not supp */ LINUX_MSG_CONFIRM}, 210 {-1, /* not supp */ LINUX_MSG_RST}, 211 {-1, /* not supp */ LINUX_MSG_ERRQUEUE}, 212 {-1, /* not supp */ LINUX_MSG_NOSIGNAL}, 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(ldom) 221 int ldom; 222 { 223 if (ldom < 0 || ldom >= LINUX_AF_MAX) 224 return (-1); 225 226 return linux_to_bsd_domain_[ldom]; 227 } 228 229 /* 230 * Convert between BSD and Linux socket domain values 231 */ 232 static int 233 bsd_to_linux_domain(bdom) 234 int bdom; 235 { 236 if (bdom < 0 || bdom >= AF_MAX) 237 return (-1); 238 239 return bsd_to_linux_domain_[bdom]; 240 } 241 242 static int 243 linux_to_bsd_msg_flags(lflag) 244 int lflag; 245 { 246 int i, lfl, bfl; 247 int bflag = 0; 248 249 if (lflag == 0) 250 return (0); 251 252 for(i = 0; i < __arraycount(bsd_to_linux_msg_flags_); i++) { 253 bfl = bsd_to_linux_msg_flags_[i].bfl; 254 lfl = bsd_to_linux_msg_flags_[i].lfl; 255 256 if (lfl == 0) 257 continue; 258 259 if (lflag & lfl) { 260 if (bfl < 0) 261 return (-1); 262 263 bflag |= bfl; 264 } 265 } 266 267 return (bflag); 268 } 269 270 static int 271 bsd_to_linux_msg_flags(bflag) 272 int bflag; 273 { 274 int i, lfl, bfl; 275 int lflag = 0; 276 277 if (bflag == 0) 278 return (0); 279 280 for(i = 0; i < __arraycount(bsd_to_linux_msg_flags_); i++) { 281 bfl = bsd_to_linux_msg_flags_[i].bfl; 282 lfl = bsd_to_linux_msg_flags_[i].lfl; 283 284 if (bfl <= 0) 285 continue; 286 287 if (bflag & bfl) { 288 if (lfl < 0) 289 return (-1); 290 291 lflag |= lfl; 292 } 293 } 294 295 return (lflag); 296 } 297 298 int 299 linux_sys_socket(l, v, retval) 300 struct lwp *l; 301 void *v; 302 register_t *retval; 303 { 304 struct linux_sys_socket_args /* { 305 syscallarg(int) domain; 306 syscallarg(int) type; 307 syscallarg(int) protocol; 308 } */ *uap = v; 309 struct compat_30_sys_socket_args bsa; 310 int error; 311 312 SCARG(&bsa, protocol) = SCARG(uap, protocol); 313 SCARG(&bsa, type) = SCARG(uap, type); 314 SCARG(&bsa, domain) = linux_to_bsd_domain(SCARG(uap, domain)); 315 if (SCARG(&bsa, domain) == -1) 316 return EINVAL; 317 error = sys___socket30(l, &bsa, retval); 318 319 #ifdef INET6 320 /* 321 * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by 322 * default and some apps depend on this. So, set V6ONLY to 0 323 * for Linux apps if the sysctl value is set to 1. 324 */ 325 if (!error && ip6_v6only && SCARG(&bsa, domain) == PF_INET6) { 326 struct proc *p = l->l_proc; 327 struct file *fp; 328 329 if (getsock(p->p_fd, *retval, &fp) == 0) { 330 struct mbuf *m; 331 332 m = m_get(M_WAIT, MT_SOOPTS); 333 m->m_len = sizeof(int); 334 *mtod(m, int *) = 0; 335 336 /* ignore error */ 337 (void) sosetopt((struct socket *)fp->f_data, 338 IPPROTO_IPV6, IPV6_V6ONLY, m); 339 340 FILE_UNUSE(fp, l); 341 } 342 } 343 #endif 344 345 return (error); 346 } 347 348 int 349 linux_sys_socketpair(l, v, retval) 350 struct lwp *l; 351 void *v; 352 register_t *retval; 353 { 354 struct linux_sys_socketpair_args /* { 355 syscallarg(int) domain; 356 syscallarg(int) type; 357 syscallarg(int) protocol; 358 syscallarg(int *) rsv; 359 } */ *uap = v; 360 struct sys_socketpair_args bsa; 361 362 SCARG(&bsa, domain) = linux_to_bsd_domain(SCARG(uap, domain)); 363 if (SCARG(&bsa, domain) == -1) 364 return EINVAL; 365 SCARG(&bsa, type) = SCARG(uap, type); 366 SCARG(&bsa, protocol) = SCARG(uap, protocol); 367 SCARG(&bsa, rsv) = SCARG(uap, rsv); 368 369 return sys_socketpair(l, &bsa, retval); 370 } 371 372 int 373 linux_sys_sendto(l, v, retval) 374 struct lwp *l; 375 void *v; 376 register_t *retval; 377 { 378 struct linux_sys_sendto_args /* { 379 syscallarg(int) s; 380 syscallarg(void *) msg; 381 syscallarg(int) len; 382 syscallarg(int) flags; 383 syscallarg(struct osockaddr *) to; 384 syscallarg(int) tolen; 385 } */ *uap = v; 386 struct proc *p = l->l_proc; 387 struct sys_sendto_args bsa; 388 socklen_t tolen; 389 390 SCARG(&bsa, s) = SCARG(uap, s); 391 SCARG(&bsa, buf) = SCARG(uap, msg); 392 SCARG(&bsa, len) = (size_t) SCARG(uap, len); 393 SCARG(&bsa, flags) = SCARG(uap, flags); 394 tolen = SCARG(uap, tolen); 395 if (SCARG(uap, to)) { 396 struct sockaddr *sa; 397 int error; 398 caddr_t sg = stackgap_init(p, 0); 399 400 error = linux_sa_get(l, SCARG(uap, s), &sg, &sa, 401 SCARG(uap, to), &tolen); 402 if (error) 403 return (error); 404 405 SCARG(&bsa, to) = sa; 406 } else 407 SCARG(&bsa, to) = NULL; 408 SCARG(&bsa, tolen) = tolen; 409 410 return (sys_sendto(l, &bsa, retval)); 411 } 412 413 int 414 linux_sys_sendmsg(l, v, retval) 415 struct lwp *l; 416 void *v; 417 register_t *retval; 418 { 419 struct linux_sys_sendmsg_args /* { 420 syscallarg(int) s; 421 syscallarg(struct msghdr *) msg; 422 syscallarg(u_int) flags; 423 } */ *uap = v; 424 struct proc *p = l->l_proc; 425 struct msghdr msg; 426 int error; 427 struct iovec aiov[UIO_SMALLIOV], *iov; 428 caddr_t sg = 0; 429 int bflags; 430 u_int8_t *control=NULL; 431 432 error = copyin(SCARG(uap, msg), (caddr_t)&msg, sizeof(msg)); 433 if (error) 434 return (error); 435 if ((unsigned int)msg.msg_iovlen > UIO_SMALLIOV) { 436 if ((unsigned int)msg.msg_iovlen > IOV_MAX) 437 return (EMSGSIZE); 438 iov = malloc(sizeof(struct iovec) * msg.msg_iovlen, 439 M_IOV, M_WAITOK); 440 } else 441 iov = aiov; 442 if ((unsigned int)msg.msg_iovlen > 0) { 443 error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov, 444 (size_t)(msg.msg_iovlen * sizeof(struct iovec))); 445 if (error) 446 goto done; 447 } 448 msg.msg_iov = iov; 449 msg.msg_flags = 0; 450 451 /* Convert the sockaddr if necessary */ 452 if (msg.msg_name) { 453 struct sockaddr *sa; 454 sg = stackgap_init(p, 0); 455 456 error = linux_sa_get(l, SCARG(uap, s), &sg, &sa, 457 (struct osockaddr *) msg.msg_name, &msg.msg_namelen); 458 if (error) 459 goto done; 460 msg.msg_name = sa; 461 } 462 463 /* 464 * Translate message flags. 465 */ 466 bflags = linux_to_bsd_msg_flags(SCARG(uap, flags)); 467 if (bflags < 0) { 468 /* Some supported flag */ 469 error = EINVAL; 470 goto done; 471 } 472 473 /* 474 * Handle cmsg if there is any. 475 */ 476 if (CMSG_FIRSTHDR(&msg)) { 477 struct cmsghdr cmsg, *cc; 478 int changed = 0; 479 ssize_t resid = msg.msg_controllen; 480 size_t clen, cidx = 0, cspace; 481 482 /* 483 * Limit the size even more than what sockargs() would do, 484 * We need to fit into stackgap space. 485 */ 486 if (msg.msg_controllen > (STACKGAPLEN / 2)) { 487 /* Sorry guys! */ 488 error = EMSGSIZE; 489 goto done; 490 } 491 492 control = malloc((clen = msg.msg_controllen), M_TEMP, M_WAITOK); 493 if (!control) { 494 error = ENOMEM; 495 goto done; 496 } 497 498 cc = CMSG_FIRSTHDR(&msg); 499 do { 500 error = copyin(cc, &cmsg, sizeof(cmsg)); 501 if (error) 502 goto done; 503 504 /* 505 * Sanity check the control message length. 506 */ 507 if (cmsg.cmsg_len > resid 508 || cmsg.cmsg_len < sizeof(struct cmsghdr)) { 509 error = EINVAL; 510 goto done; 511 } 512 513 /* 514 * Refuse unsupported control messages, and 515 * translate fields as appropriate. 516 */ 517 switch (cmsg.cmsg_level) { 518 case LINUX_SOL_SOCKET: 519 /* It only differs on some archs */ 520 if (LINUX_SOL_SOCKET != SOL_SOCKET) { 521 cmsg.cmsg_level = SOL_SOCKET; 522 changed = 1; 523 } 524 525 switch(cmsg.cmsg_type) { 526 case LINUX_SCM_RIGHTS: 527 /* Linux SCM_RIGHTS is same as NetBSD */ 528 break; 529 530 default: 531 /* other types not supported */ 532 error = EINVAL; 533 goto done; 534 } 535 break; 536 default: 537 /* pray and leave intact */ 538 break; 539 } 540 541 cspace = CMSG_SPACE(cmsg.cmsg_len - sizeof(cmsg)); 542 543 /* Check the buffer is big enough */ 544 if (__predict_false(cidx + cspace > clen)) { 545 u_int8_t *nc; 546 547 clen = cidx + cspace; 548 nc = realloc(control, clen, M_TEMP, M_WAITOK); 549 if (!nc) { 550 error = ENOMEM; 551 goto done; 552 } 553 control = nc; 554 } 555 556 /* Copy header */ 557 memcpy(&control[cidx], &cmsg, sizeof(cmsg)); 558 559 /* Zero are between header and data */ 560 memset(&control[cidx+sizeof(cmsg)], 0, 561 CMSG_ALIGN(sizeof(cmsg)) - sizeof(cmsg)); 562 563 /* Copyin the data */ 564 error = copyin(LINUX_CMSG_DATA(cc), 565 CMSG_DATA(control), 566 cmsg.cmsg_len - sizeof(cmsg)); 567 if (error) 568 goto done; 569 570 /* 571 * If there is alignment difference, we changed 572 * layout of cmsg. 573 */ 574 if (LINUX_CMSG_ALIGNDIFF) 575 changed = 1; 576 577 resid -= cspace; 578 cidx += cspace; 579 } while ((cc = LINUX_CMSG_NXTHDR(&msg, cc)) && resid > 0); 580 581 /* 582 * If any of the passed control message needed 583 * a change, put the changed data into stackgap 584 * and adjust msg appropriately. 585 */ 586 if (changed) { 587 char *newc; 588 589 /* 590 * Check again the total len is maximum half of 591 * stackgap. The length might change if the 592 * alignment is different. 593 */ 594 if (clen > STACKGAPLEN/2) { 595 error = EMSGSIZE; 596 goto done; 597 } 598 599 /* 600 * Allocate space on stack within stackgap, and 601 * copy changed data there. 602 */ 603 if (!sg) 604 sg = stackgap_init(p, STACKGAPLEN/3); 605 newc = stackgap_alloc(p, &sg, clen); 606 if (!newc) { 607 error = ENOMEM; 608 goto done; 609 } 610 611 error = copyout(control, newc, clen); 612 if (error) 613 goto done; 614 615 msg.msg_control = newc; 616 msg.msg_controllen = clen; 617 } 618 619 free(control, M_TEMP); 620 control = NULL; 621 } 622 623 error = sendit(l, SCARG(uap, s), &msg, bflags, retval); 624 625 done: 626 if (control) 627 free(control, M_TEMP); 628 if (iov != aiov) 629 free(iov, M_IOV); 630 return (error); 631 } 632 633 int 634 linux_sys_recvfrom(l, v, retval) 635 struct lwp *l; 636 void *v; 637 register_t *retval; 638 { 639 struct linux_sys_recvfrom_args /* { 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 } */ *uap = v; 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 int 667 linux_sys_recvmsg(l, v, retval) 668 struct lwp *l; 669 void *v; 670 register_t *retval; 671 { 672 struct linux_sys_recvmsg_args /* { 673 syscallarg(int) s; 674 syscallarg(struct msghdr *) msg; 675 syscallarg(u_int) flags; 676 } */ *uap = v; 677 struct msghdr msg; 678 int error; 679 struct sys_recvmsg_args bsa; 680 int lflags; 681 u_int8_t *ocontrol = NULL; /* XXX: gcc */ 682 socklen_t ocontrollen = 0; 683 684 /* 685 * Data alignment is different on some architectures. If control 686 * message is expected, we must arrange for the control message 687 * to be initially put elsewhere, and copy to target place 688 * with Linux alignment. 689 */ 690 if (LINUX_CMSG_ALIGNDIFF) { 691 error = copyin(SCARG(uap, msg), &msg, sizeof(msg)); 692 if (error) 693 return (error); 694 695 if (CMSG_FIRSTHDR(&msg)) { 696 caddr_t sg; 697 698 /* Need to fit within stackgap */ 699 if (msg.msg_controllen > STACKGAPLEN/2) { 700 /* Sorry guys! */ 701 return (EINVAL); 702 } 703 704 sg = stackgap_init(l->l_proc, STACKGAPLEN/3); 705 706 ocontrol = msg.msg_control; 707 ocontrollen = msg.msg_controllen; 708 709 /* space for at least one message's worth align */ 710 msg.msg_controllen += CMSG_ALIGN(1); 711 712 msg.msg_control = stackgap_alloc(l->l_proc, &sg, 713 msg.msg_controllen); 714 if (!msg.msg_control) 715 return (ENOMEM); 716 717 /* 718 * Okay to overwrite the original structure, it's 719 * supposed to be writable. 720 */ 721 error = copyout(&msg, SCARG(uap, msg), sizeof(msg)); 722 if (error) 723 return (error); 724 } 725 } 726 727 SCARG(&bsa, s) = SCARG(uap, s); 728 SCARG(&bsa, msg) = SCARG(uap, msg); 729 SCARG(&bsa, flags) = linux_to_bsd_msg_flags(SCARG(uap, flags)); 730 731 if (SCARG(&bsa, flags) < 0) { 732 /* Some unsupported flag */ 733 return (EINVAL); 734 } 735 736 if ((error = sys_recvmsg(l, &bsa, retval))) 737 goto done; 738 739 /* Fixup sockaddr */ 740 error = copyin((caddr_t)SCARG(uap, msg), (caddr_t)&msg, 741 sizeof(msg)); 742 if (error) 743 goto done; 744 745 if (msg.msg_name && msg.msg_namelen > 2) { 746 if ((error = linux_sa_put(msg.msg_name))) 747 goto done; 748 } 749 750 /* Fixup msg flags */ 751 lflags = bsd_to_linux_msg_flags(msg.msg_flags); 752 if (lflags < 0) { 753 /* Some flag unsupported by Linux */ 754 error = EINVAL; 755 goto done; 756 } 757 error = copyout(&lflags, (u_int8_t *) SCARG(uap, msg) + 758 offsetof(struct msghdr, msg_flags), sizeof(lflags)); 759 if (error) 760 goto done; 761 762 /* 763 * Fixup cmsg. We handle two things: 764 * 1. different values for level/type on some archs 765 * 2. different alignment of CMSG_DATA on some archs 766 */ 767 if (CMSG_FIRSTHDR(&msg)) { 768 struct cmsghdr cmsg, *cc; 769 int changed = 0; 770 size_t resid = ocontrollen; 771 772 cc = CMSG_FIRSTHDR(&msg); 773 do { 774 error = copyin(cc, &cmsg, sizeof(cmsg)); 775 if (error) 776 goto done; 777 778 switch (cmsg.cmsg_level) { 779 case SOL_SOCKET: 780 if (SOL_SOCKET != LINUX_SOL_SOCKET) { 781 cmsg.cmsg_level = LINUX_SOL_SOCKET; 782 changed = 1; 783 } 784 785 switch (cmsg.cmsg_type) { 786 case SCM_RIGHTS: 787 /* Linux SCM_RIGHTS is same as NetBSD */ 788 break; 789 790 default: 791 /* other types not supported */ 792 error = EINVAL; 793 goto done; 794 } 795 default: 796 /* pray and leave intact */ 797 break; 798 } 799 800 if (LINUX_CMSG_ALIGNDIFF) { 801 int i; 802 u_int8_t d, *sd, *td; 803 804 /* 805 * Sanity check. 806 */ 807 if (cmsg.cmsg_len > resid 808 || cmsg.cmsg_len < sizeof(cmsg)) { 809 error = EINVAL; 810 goto done; 811 } 812 813 /* 814 * Need to copy the cmsg from scratch area 815 * to the original place, converting data 816 * alignment from NetBSD to Linux one. 817 */ 818 error = copyout(&cmsg, ocontrol, sizeof(cmsg)); 819 if (error) 820 goto done; 821 /* zero pad */ 822 for(i=0; i < LINUX_CMSG_ALIGN(sizeof(cmsg)) - sizeof(cmsg); i++) { 823 copyout("",&ocontrol[sizeof(cmsg)+i],1); 824 } 825 826 sd = CMSG_DATA(cc); 827 td = LINUX_CMSG_DATA(ocontrol); 828 829 /* This is not particularily effective, but ..*/ 830 d = '\0'; 831 for(i=0; i < cmsg.cmsg_len - sizeof(cmsg); i++){ 832 copyin(sd++, &d, 1); 833 copyout(&d, td++, 1); 834 } 835 836 resid -= (td - ocontrol); 837 ocontrol = td; 838 } else if (changed) { 839 /* Update cmsghdr in-place */ 840 error = copyout(&cmsg, cc, sizeof(cmsg)); 841 if (error) 842 goto done; 843 changed = 0; 844 } 845 } while((cc = CMSG_NXTHDR(&msg, cc))); 846 } 847 848 done: 849 return (error); 850 } 851 852 /* 853 * Convert socket option level from Linux to NetBSD value. Only SOL_SOCKET 854 * is different, the rest matches IPPROTO_* on both systems. 855 */ 856 int 857 linux_to_bsd_sopt_level(llevel) 858 int llevel; 859 { 860 861 switch (llevel) { 862 case LINUX_SOL_SOCKET: 863 return SOL_SOCKET; 864 case LINUX_SOL_IP: 865 return IPPROTO_IP; 866 case LINUX_SOL_TCP: 867 return IPPROTO_TCP; 868 case LINUX_SOL_UDP: 869 return IPPROTO_UDP; 870 default: 871 return -1; 872 } 873 } 874 875 /* 876 * Convert Linux socket level socket option numbers to NetBSD values. 877 */ 878 int 879 linux_to_bsd_so_sockopt(lopt) 880 int lopt; 881 { 882 883 switch (lopt) { 884 case LINUX_SO_DEBUG: 885 return SO_DEBUG; 886 case LINUX_SO_REUSEADDR: 887 /* 888 * Linux does not implement SO_REUSEPORT, but allows reuse of a 889 * host:port pair through SO_REUSEADDR even if the address is not a 890 * multicast-address. Effectively, this means that we should use 891 * SO_REUSEPORT to allow Linux applications to not exit with 892 * EADDRINUSE 893 */ 894 return SO_REUSEPORT; 895 case LINUX_SO_TYPE: 896 return SO_TYPE; 897 case LINUX_SO_ERROR: 898 return SO_ERROR; 899 case LINUX_SO_DONTROUTE: 900 return SO_DONTROUTE; 901 case LINUX_SO_BROADCAST: 902 return SO_BROADCAST; 903 case LINUX_SO_SNDBUF: 904 return SO_SNDBUF; 905 case LINUX_SO_RCVBUF: 906 return SO_RCVBUF; 907 case LINUX_SO_KEEPALIVE: 908 return SO_KEEPALIVE; 909 case LINUX_SO_OOBINLINE: 910 return SO_OOBINLINE; 911 case LINUX_SO_LINGER: 912 return SO_LINGER; 913 case LINUX_SO_PRIORITY: 914 case LINUX_SO_NO_CHECK: 915 default: 916 return -1; 917 } 918 } 919 920 /* 921 * Convert Linux IP level socket option number to NetBSD values. 922 */ 923 int 924 linux_to_bsd_ip_sockopt(lopt) 925 int lopt; 926 { 927 928 switch (lopt) { 929 case LINUX_IP_TOS: 930 return IP_TOS; 931 case LINUX_IP_TTL: 932 return IP_TTL; 933 case LINUX_IP_MULTICAST_TTL: 934 return IP_MULTICAST_TTL; 935 case LINUX_IP_MULTICAST_LOOP: 936 return IP_MULTICAST_LOOP; 937 case LINUX_IP_MULTICAST_IF: 938 return IP_MULTICAST_IF; 939 case LINUX_IP_ADD_MEMBERSHIP: 940 return IP_ADD_MEMBERSHIP; 941 case LINUX_IP_DROP_MEMBERSHIP: 942 return IP_DROP_MEMBERSHIP; 943 default: 944 return -1; 945 } 946 } 947 948 /* 949 * Convert Linux TCP level socket option number to NetBSD values. 950 */ 951 int 952 linux_to_bsd_tcp_sockopt(lopt) 953 int lopt; 954 { 955 956 switch (lopt) { 957 case LINUX_TCP_NODELAY: 958 return TCP_NODELAY; 959 case LINUX_TCP_MAXSEG: 960 return TCP_MAXSEG; 961 default: 962 return -1; 963 } 964 } 965 966 /* 967 * Convert Linux UDP level socket option number to NetBSD values. 968 */ 969 int 970 linux_to_bsd_udp_sockopt(lopt) 971 int lopt; 972 { 973 974 switch (lopt) { 975 default: 976 return -1; 977 } 978 } 979 980 /* 981 * Another reasonably straightforward function: setsockopt(2). 982 * The level and option numbers are converted; the values passed 983 * are not (yet) converted, the ones currently implemented don't 984 * need conversion, as they are the same on both systems. 985 */ 986 int 987 linux_sys_setsockopt(l, v, retval) 988 struct lwp *l; 989 void *v; 990 register_t *retval; 991 { 992 struct linux_sys_setsockopt_args /* { 993 syscallarg(int) s; 994 syscallarg(int) level; 995 syscallarg(int) optname; 996 syscallarg(void *) optval; 997 syscallarg(int) optlen; 998 } */ *uap = v; 999 struct sys_setsockopt_args bsa; 1000 int name; 1001 1002 SCARG(&bsa, s) = SCARG(uap, s); 1003 SCARG(&bsa, level) = linux_to_bsd_sopt_level(SCARG(uap, level)); 1004 SCARG(&bsa, val) = SCARG(uap, optval); 1005 SCARG(&bsa, valsize) = SCARG(uap, optlen); 1006 1007 switch (SCARG(&bsa, level)) { 1008 case SOL_SOCKET: 1009 name = linux_to_bsd_so_sockopt(SCARG(uap, optname)); 1010 break; 1011 case IPPROTO_IP: 1012 name = linux_to_bsd_ip_sockopt(SCARG(uap, optname)); 1013 break; 1014 case IPPROTO_TCP: 1015 name = linux_to_bsd_tcp_sockopt(SCARG(uap, optname)); 1016 break; 1017 case IPPROTO_UDP: 1018 name = linux_to_bsd_udp_sockopt(SCARG(uap, optname)); 1019 break; 1020 default: 1021 return EINVAL; 1022 } 1023 1024 if (name == -1) 1025 return EINVAL; 1026 SCARG(&bsa, name) = name; 1027 1028 return sys_setsockopt(l, &bsa, retval); 1029 } 1030 1031 /* 1032 * getsockopt(2) is very much the same as setsockopt(2) (see above) 1033 */ 1034 int 1035 linux_sys_getsockopt(l, v, retval) 1036 struct lwp *l; 1037 void *v; 1038 register_t *retval; 1039 { 1040 struct linux_sys_getsockopt_args /* { 1041 syscallarg(int) s; 1042 syscallarg(int) level; 1043 syscallarg(int) optname; 1044 syscallarg(void *) optval; 1045 syscallarg(int *) optlen; 1046 } */ *uap = v; 1047 struct sys_getsockopt_args bga; 1048 int name; 1049 1050 SCARG(&bga, s) = SCARG(uap, s); 1051 SCARG(&bga, level) = linux_to_bsd_sopt_level(SCARG(uap, level)); 1052 SCARG(&bga, val) = SCARG(uap, optval); 1053 SCARG(&bga, avalsize) = (socklen_t *)SCARG(uap, optlen); 1054 1055 switch (SCARG(&bga, level)) { 1056 case SOL_SOCKET: 1057 name = linux_to_bsd_so_sockopt(SCARG(uap, optname)); 1058 break; 1059 case IPPROTO_IP: 1060 name = linux_to_bsd_ip_sockopt(SCARG(uap, optname)); 1061 break; 1062 case IPPROTO_TCP: 1063 name = linux_to_bsd_tcp_sockopt(SCARG(uap, optname)); 1064 break; 1065 case IPPROTO_UDP: 1066 name = linux_to_bsd_udp_sockopt(SCARG(uap, optname)); 1067 break; 1068 default: 1069 return EINVAL; 1070 } 1071 1072 if (name == -1) 1073 return EINVAL; 1074 SCARG(&bga, name) = name; 1075 1076 return sys_getsockopt(l, &bga, retval); 1077 } 1078 1079 #define IF_NAME_LEN 16 1080 1081 int 1082 linux_getifhwaddr(l, retval, fd, data) 1083 struct lwp *l; 1084 register_t *retval; 1085 u_int fd; 1086 void *data; 1087 { 1088 /* Not the full structure, just enough to map what we do here */ 1089 struct linux_ifreq { 1090 char if_name[IF_NAME_LEN]; 1091 struct osockaddr hwaddr; 1092 } lreq; 1093 struct proc *p = l->l_proc; 1094 struct filedesc *fdp; 1095 struct file *fp; 1096 struct ifaddr *ifa; 1097 struct ifnet *ifp; 1098 struct sockaddr_dl *sadl; 1099 int error, found; 1100 int index, ifnum; 1101 1102 /* 1103 * We can't emulate this ioctl by calling sys_ioctl() to run 1104 * SIOCGIFCONF, because the user buffer is not of the right 1105 * type to take those results. We can't use kernel buffers to 1106 * receive the results, as the implementation of sys_ioctl() 1107 * and ifconf() [which implements SIOCGIFCONF] use 1108 * copyin()/copyout() which will fail on kernel addresses. 1109 * 1110 * So, we must duplicate code from sys_ioctl() and ifconf(). Ugh. 1111 */ 1112 1113 fdp = p->p_fd; 1114 if ((fp = fd_getfile(fdp, fd)) == NULL) 1115 return (EBADF); 1116 1117 FILE_USE(fp); 1118 if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 1119 error = EBADF; 1120 goto out; 1121 } 1122 1123 error = copyin(data, (caddr_t)&lreq, sizeof(lreq)); 1124 if (error) 1125 goto out; 1126 lreq.if_name[IF_NAME_LEN-1] = '\0'; /* just in case */ 1127 1128 /* 1129 * Try real interface name first, then fake "ethX" 1130 */ 1131 for (ifp = ifnet.tqh_first, found = 0; 1132 ifp != 0 && !found; 1133 ifp = ifp->if_list.tqe_next) { 1134 if (strcmp(lreq.if_name, ifp->if_xname)) 1135 /* not this interface */ 1136 continue; 1137 found=1; 1138 if ((ifa = ifp->if_addrlist.tqh_first) != 0) { 1139 for (; ifa != 0; ifa = ifa->ifa_list.tqe_next) { 1140 sadl = (struct sockaddr_dl *)ifa->ifa_addr; 1141 /* only return ethernet addresses */ 1142 /* XXX what about FDDI, etc. ? */ 1143 if (sadl->sdl_family != AF_LINK || 1144 sadl->sdl_type != IFT_ETHER) 1145 continue; 1146 memcpy((caddr_t)&lreq.hwaddr.sa_data, 1147 LLADDR(sadl), 1148 MIN(sadl->sdl_alen, 1149 sizeof(lreq.hwaddr.sa_data))); 1150 lreq.hwaddr.sa_family = 1151 sadl->sdl_family; 1152 error = copyout((caddr_t)&lreq, data, 1153 sizeof(lreq)); 1154 goto out; 1155 } 1156 } else { 1157 error = ENODEV; 1158 goto out; 1159 } 1160 } 1161 1162 if (strncmp(lreq.if_name, "eth", 3) == 0) { 1163 for (ifnum = 0, index = 3; 1164 lreq.if_name[index] != '\0' && index < IF_NAME_LEN; 1165 index++) { 1166 ifnum *= 10; 1167 ifnum += lreq.if_name[index] - '0'; 1168 } 1169 1170 error = EINVAL; /* in case we don't find one */ 1171 for (ifp = ifnet.tqh_first, found = 0; 1172 ifp != 0 && !found; 1173 ifp = ifp->if_list.tqe_next) { 1174 memcpy(lreq.if_name, ifp->if_xname, 1175 MIN(IF_NAME_LEN, IFNAMSIZ)); 1176 if ((ifa = ifp->if_addrlist.tqh_first) == 0) 1177 /* no addresses on this interface */ 1178 continue; 1179 else 1180 for (; ifa != 0; ifa = ifa->ifa_list.tqe_next) { 1181 sadl = (struct sockaddr_dl *)ifa->ifa_addr; 1182 /* only return ethernet addresses */ 1183 /* XXX what about FDDI, etc. ? */ 1184 if (sadl->sdl_family != AF_LINK || 1185 sadl->sdl_type != IFT_ETHER) 1186 continue; 1187 if (ifnum--) 1188 /* not the reqested iface */ 1189 continue; 1190 memcpy((caddr_t)&lreq.hwaddr.sa_data, 1191 LLADDR(sadl), 1192 MIN(sadl->sdl_alen, 1193 sizeof(lreq.hwaddr.sa_data))); 1194 lreq.hwaddr.sa_family = 1195 sadl->sdl_family; 1196 error = copyout((caddr_t)&lreq, data, 1197 sizeof(lreq)); 1198 found = 1; 1199 break; 1200 } 1201 } 1202 } else { 1203 /* unknown interface, not even an "eth*" name */ 1204 error = ENODEV; 1205 } 1206 1207 out: 1208 FILE_UNUSE(fp, l); 1209 return error; 1210 } 1211 #undef IF_NAME_LEN 1212 1213 int 1214 linux_ioctl_socket(l, uap, retval) 1215 struct lwp *l; 1216 struct linux_sys_ioctl_args /* { 1217 syscallarg(int) fd; 1218 syscallarg(u_long) com; 1219 syscallarg(caddr_t) data; 1220 } */ *uap; 1221 register_t *retval; 1222 { 1223 struct proc *p = l->l_proc; 1224 u_long com; 1225 int error = 0, isdev = 0, dosys = 1; 1226 struct sys_ioctl_args ia; 1227 struct file *fp; 1228 struct filedesc *fdp; 1229 struct vnode *vp; 1230 int (*ioctlf)(struct file *, u_long, void *, struct lwp *); 1231 struct ioctl_pt pt; 1232 1233 fdp = p->p_fd; 1234 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL) 1235 return (EBADF); 1236 1237 FILE_USE(fp); 1238 1239 if (fp->f_type == DTYPE_VNODE) { 1240 vp = (struct vnode *)fp->f_data; 1241 isdev = vp->v_type == VCHR; 1242 } 1243 1244 /* 1245 * Don't try to interpret socket ioctl calls that are done 1246 * on a device filedescriptor, just pass them through, to 1247 * emulate Linux behaviour. Use PTIOCLINUX so that the 1248 * device will only handle these if it's prepared to do 1249 * so, to avoid unexpected things from happening. 1250 */ 1251 if (isdev) { 1252 dosys = 0; 1253 ioctlf = fp->f_ops->fo_ioctl; 1254 pt.com = SCARG(uap, com); 1255 pt.data = SCARG(uap, data); 1256 error = ioctlf(fp, PTIOCLINUX, (caddr_t)&pt, l); 1257 /* 1258 * XXX hack: if the function returns EJUSTRETURN, 1259 * it has stuffed a sysctl return value in pt.data. 1260 */ 1261 if (error == EJUSTRETURN) { 1262 retval[0] = (register_t)pt.data; 1263 error = 0; 1264 } 1265 goto out; 1266 } 1267 1268 com = SCARG(uap, com); 1269 retval[0] = 0; 1270 1271 switch (com) { 1272 case LINUX_SIOCGIFCONF: 1273 SCARG(&ia, com) = OSIOCGIFCONF; 1274 break; 1275 case LINUX_SIOCGIFFLAGS: 1276 SCARG(&ia, com) = SIOCGIFFLAGS; 1277 break; 1278 case LINUX_SIOCSIFFLAGS: 1279 SCARG(&ia, com) = SIOCSIFFLAGS; 1280 break; 1281 case LINUX_SIOCGIFADDR: 1282 SCARG(&ia, com) = OSIOCGIFADDR; 1283 break; 1284 case LINUX_SIOCGIFDSTADDR: 1285 SCARG(&ia, com) = OSIOCGIFDSTADDR; 1286 break; 1287 case LINUX_SIOCGIFBRDADDR: 1288 SCARG(&ia, com) = OSIOCGIFBRDADDR; 1289 break; 1290 case LINUX_SIOCGIFNETMASK: 1291 SCARG(&ia, com) = OSIOCGIFNETMASK; 1292 break; 1293 case LINUX_SIOCADDMULTI: 1294 SCARG(&ia, com) = SIOCADDMULTI; 1295 break; 1296 case LINUX_SIOCDELMULTI: 1297 SCARG(&ia, com) = SIOCDELMULTI; 1298 break; 1299 case LINUX_SIOCGIFHWADDR: 1300 error = linux_getifhwaddr(l, retval, SCARG(uap, fd), 1301 SCARG(uap, data)); 1302 dosys = 0; 1303 break; 1304 default: 1305 error = EINVAL; 1306 } 1307 1308 out: 1309 FILE_UNUSE(fp, l); 1310 1311 if (error ==0 && dosys) { 1312 SCARG(&ia, fd) = SCARG(uap, fd); 1313 SCARG(&ia, data) = SCARG(uap, data); 1314 /* XXX NJWLWP */ 1315 error = sys_ioctl(curlwp, &ia, retval); 1316 } 1317 1318 return error; 1319 } 1320 1321 int 1322 linux_sys_connect(l, v, retval) 1323 struct lwp *l; 1324 void *v; 1325 register_t *retval; 1326 { 1327 struct linux_sys_connect_args /* { 1328 syscallarg(int) s; 1329 syscallarg(const struct sockaddr *) name; 1330 syscallarg(int) namelen; 1331 } */ *uap = v; 1332 struct proc *p = l->l_proc; 1333 int error; 1334 struct sockaddr *sa; 1335 struct sys_connect_args bca; 1336 caddr_t sg = stackgap_init(p, 0); 1337 socklen_t namlen; 1338 1339 namlen = SCARG(uap, namelen); 1340 error = linux_sa_get(l, SCARG(uap, s), &sg, &sa, 1341 SCARG(uap, name), &namlen); 1342 if (error) 1343 return (error); 1344 1345 SCARG(&bca, s) = SCARG(uap, s); 1346 SCARG(&bca, name) = sa; 1347 SCARG(&bca, namelen) = namlen; 1348 1349 error = sys_connect(l, &bca, retval); 1350 1351 if (error == EISCONN) { 1352 struct file *fp; 1353 struct socket *so; 1354 int s, state, prflags; 1355 1356 /* getsock() will use the descriptor for us */ 1357 if (getsock(p->p_fd, SCARG(uap, s), &fp) != 0) 1358 return EISCONN; 1359 1360 s = splsoftnet(); 1361 so = (struct socket *)fp->f_data; 1362 state = so->so_state; 1363 prflags = so->so_proto->pr_flags; 1364 splx(s); 1365 FILE_UNUSE(fp, l); 1366 /* 1367 * We should only let this call succeed once per 1368 * non-blocking connect; however we don't have 1369 * a convenient place to keep that state.. 1370 */ 1371 if ((state & SS_NBIO) && (state & SS_ISCONNECTED) && 1372 (prflags & PR_CONNREQUIRED)) 1373 return 0; 1374 } 1375 1376 return (error); 1377 } 1378 1379 int 1380 linux_sys_bind(l, v, retval) 1381 struct lwp *l; 1382 void *v; 1383 register_t *retval; 1384 { 1385 struct linux_sys_bind_args /* { 1386 syscallarg(int) s; 1387 syscallarg(const struct osockaddr *) name; 1388 syscallarg(int) namelen; 1389 } */ *uap = v; 1390 struct proc *p = l->l_proc; 1391 int error; 1392 socklen_t namlen; 1393 struct sys_bind_args bsa; 1394 1395 namlen = SCARG(uap, namelen); 1396 SCARG(&bsa, s) = SCARG(uap, s); 1397 if (SCARG(uap, name)) { 1398 struct sockaddr *sa; 1399 caddr_t sg = stackgap_init(p, 0); 1400 1401 error = linux_sa_get(l, SCARG(uap, s), &sg, &sa, 1402 SCARG(uap, name), &namlen); 1403 if (error) 1404 return (error); 1405 1406 SCARG(&bsa, name) = sa; 1407 } else 1408 SCARG(&bsa, name) = NULL; 1409 SCARG(&bsa, namelen) = namlen; 1410 1411 return (sys_bind(l, &bsa, retval)); 1412 } 1413 1414 int 1415 linux_sys_getsockname(l, v, retval) 1416 struct lwp *l; 1417 void *v; 1418 register_t *retval; 1419 { 1420 struct linux_sys_getsockname_args /* { 1421 syscallarg(int) fdes; 1422 syscallarg(caddr_t) asa; 1423 syscallarg(int *) alen; 1424 } */ *uap = v; 1425 int error; 1426 1427 if ((error = sys_getsockname(l, uap, retval)) != 0) 1428 return (error); 1429 1430 if ((error = linux_sa_put((struct osockaddr *)SCARG(uap, asa)))) 1431 return (error); 1432 1433 return (0); 1434 } 1435 1436 int 1437 linux_sys_getpeername(l, v, retval) 1438 struct lwp *l; 1439 void *v; 1440 register_t *retval; 1441 { 1442 struct sys_getpeername_args /* { 1443 syscallarg(int) fdes; 1444 syscallarg(caddr_t) asa; 1445 syscallarg(int *) alen; 1446 } */ *uap = v; 1447 int error; 1448 1449 if ((error = sys_getpeername(l, uap, retval)) != 0) 1450 return (error); 1451 1452 if ((error = linux_sa_put((struct osockaddr *)SCARG(uap, asa)))) 1453 return (error); 1454 1455 return (0); 1456 } 1457 1458 /* 1459 * Copy the osockaddr structure pointed to by osa to kernel, adjust 1460 * family and convert to sockaddr, allocate stackgap and put the 1461 * the converted structure there, address on stackgap returned in sap. 1462 */ 1463 static int 1464 linux_sa_get(l, s, sgp, sap, osa, osalen) 1465 struct lwp *l; 1466 int s; 1467 caddr_t *sgp; 1468 struct sockaddr **sap; 1469 const struct osockaddr *osa; 1470 socklen_t *osalen; 1471 { 1472 int error=0, bdom; 1473 struct sockaddr *sa, *usa; 1474 struct osockaddr *kosa = (struct osockaddr *) &sa; 1475 struct proc *p = l->l_proc; 1476 socklen_t alloclen; 1477 #ifdef INET6 1478 int oldv6size; 1479 struct sockaddr_in6 *sin6; 1480 #endif 1481 1482 if (*osalen < 2 || *osalen > UCHAR_MAX || !osa) { 1483 DPRINTF(("bad osa=%p osalen=%d\n", osa, *osalen)); 1484 return (EINVAL); 1485 } 1486 1487 alloclen = *osalen; 1488 #ifdef INET6 1489 oldv6size = 0; 1490 /* 1491 * Check for old (pre-RFC2553) sockaddr_in6. We may accept it 1492 * if it's a v4-mapped address, so reserve the proper space 1493 * for it. 1494 */ 1495 if (alloclen == sizeof (struct sockaddr_in6) - sizeof (u_int32_t)) { 1496 alloclen = sizeof (struct sockaddr_in6); 1497 oldv6size = 1; 1498 } 1499 #endif 1500 1501 kosa = (struct osockaddr *) malloc(alloclen, M_TEMP, M_WAITOK); 1502 1503 if ((error = copyin(osa, (caddr_t) kosa, *osalen))) { 1504 DPRINTF(("error copying osa %d\n", error)); 1505 goto out; 1506 } 1507 1508 bdom = linux_to_bsd_domain(kosa->sa_family); 1509 if (bdom == -1) { 1510 DPRINTF(("bad linux family=%d\n", kosa->sa_family)); 1511 error = EINVAL; 1512 goto out; 1513 } 1514 1515 #ifdef INET6 1516 /* 1517 * Older Linux IPv6 code uses obsolete RFC2133 struct sockaddr_in6, 1518 * which lacks the scope id compared with RFC2553 one. If we detect 1519 * the situation, reject the address and write a message to system log. 1520 * 1521 * Still accept addresses for which the scope id is not used. 1522 */ 1523 if (oldv6size && bdom == AF_INET6) { 1524 sin6 = (struct sockaddr_in6 *)kosa; 1525 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) || 1526 (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) && 1527 !IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && 1528 !IN6_IS_ADDR_V4COMPAT(&sin6->sin6_addr) && 1529 !IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) && 1530 !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))) { 1531 sin6->sin6_scope_id = 0; 1532 } else { 1533 int uid = p->p_cred ? kauth_cred_geteuid(p->p_cred) : -1; 1534 1535 log(LOG_DEBUG, 1536 "pid %d (%s), uid %d: obsolete pre-RFC2553 " 1537 "sockaddr_in6 rejected", 1538 p->p_pid, p->p_comm, uid); 1539 error = EINVAL; 1540 goto out; 1541 } 1542 } 1543 #endif 1544 1545 /* 1546 * If the family is unspecified, use address family of the 1547 * socket. This avoid triggering COMPAT_43 struct socket family check 1548 * in sockargs() on little-endian machines, and strict family checks 1549 * in netinet/in_pcb.c et.al. 1550 */ 1551 if (bdom == AF_UNSPEC) { 1552 struct file *fp; 1553 struct socket *so; 1554 1555 /* getsock() will use the descriptor for us */ 1556 if ((error = getsock(p->p_fd, s, &fp)) != 0) 1557 goto out; 1558 1559 so = (struct socket *)fp->f_data; 1560 bdom = so->so_proto->pr_domain->dom_family; 1561 1562 FILE_UNUSE(fp, l); 1563 1564 DPRINTF(("AF_UNSPEC family adjusted to %d\n", bdom)); 1565 } 1566 1567 if (bdom == AF_INET) { 1568 alloclen = sizeof(struct sockaddr_in); 1569 } 1570 1571 sa = (struct sockaddr *) kosa; 1572 sa->sa_family = bdom; 1573 sa->sa_len = alloclen; 1574 #ifdef DEBUG_LINUX 1575 DPRINTF(("family %d, len = %d [ ", sa->sa_family, sa->sa_len)); 1576 for (bdom = 0; bdom < sizeof(sa->sa_data); bdom++) 1577 DPRINTF(("%02x ", (unsigned char) sa->sa_data[bdom])); 1578 DPRINTF(("\n")); 1579 #endif 1580 1581 usa = (struct sockaddr *) stackgap_alloc(p, sgp, alloclen); 1582 if (!usa) { 1583 error = ENOMEM; 1584 goto out; 1585 } 1586 1587 if ((error = copyout(sa, usa, alloclen))) { 1588 DPRINTF(("error copying out socket %d\n", error)); 1589 goto out; 1590 } 1591 1592 *sap = usa; 1593 1594 out: 1595 *osalen = alloclen; 1596 free(kosa, M_TEMP); 1597 return (error); 1598 } 1599 1600 static int 1601 linux_sa_put(osa) 1602 struct osockaddr *osa; 1603 { 1604 struct sockaddr sa; 1605 struct osockaddr *kosa; 1606 int error, bdom, len; 1607 1608 /* 1609 * Only read/write the sockaddr family and length part, the rest is 1610 * not changed. 1611 */ 1612 len = sizeof(sa.sa_len) + sizeof(sa.sa_family); 1613 1614 error = copyin((caddr_t) osa, (caddr_t) &sa, len); 1615 if (error) 1616 return (error); 1617 1618 bdom = bsd_to_linux_domain(sa.sa_family); 1619 if (bdom == -1) 1620 return (EINVAL); 1621 1622 /* Note: we convert from sockaddr to osockaddr here, too */ 1623 kosa = (struct osockaddr *) &sa; 1624 kosa->sa_family = bdom; 1625 error = copyout(kosa, osa, len); 1626 if (error) 1627 return (error); 1628 1629 return (0); 1630 } 1631 1632 #ifndef __amd64__ 1633 int 1634 linux_sys_recv(l, v, retval) 1635 struct lwp *l; 1636 void *v; 1637 register_t *retval; 1638 { 1639 struct linux_sys_recv_args /* { 1640 syscallarg(int) s; 1641 syscallarg(void *) buf; 1642 syscallarg(int) len; 1643 syscallarg(int) flags; 1644 } */ *uap = v; 1645 struct sys_recvfrom_args bra; 1646 1647 1648 SCARG(&bra, s) = SCARG(uap, s); 1649 SCARG(&bra, buf) = SCARG(uap, buf); 1650 SCARG(&bra, len) = (size_t) SCARG(uap, len); 1651 SCARG(&bra, flags) = SCARG(uap, flags); 1652 SCARG(&bra, from) = NULL; 1653 SCARG(&bra, fromlenaddr) = NULL; 1654 1655 return (sys_recvfrom(l, &bra, retval)); 1656 } 1657 1658 int 1659 linux_sys_send(l, v, retval) 1660 struct lwp *l; 1661 void *v; 1662 register_t *retval; 1663 { 1664 struct linux_sys_send_args /* { 1665 syscallarg(int) s; 1666 syscallarg(caddr_t) buf; 1667 syscallarg(int) len; 1668 syscallarg(int) flags; 1669 } */ *uap = v; 1670 struct sys_sendto_args bsa; 1671 1672 SCARG(&bsa, s) = SCARG(uap, s); 1673 SCARG(&bsa, buf) = SCARG(uap, buf); 1674 SCARG(&bsa, len) = SCARG(uap, len); 1675 SCARG(&bsa, flags) = SCARG(uap, flags); 1676 SCARG(&bsa, to) = NULL; 1677 SCARG(&bsa, tolen) = 0; 1678 1679 return (sys_sendto(l, &bsa, retval)); 1680 } 1681 #endif 1682 1683 int 1684 linux_sys_accept(l, v, retval) 1685 struct lwp *l; 1686 void *v; 1687 register_t *retval; 1688 { 1689 struct linux_sys_accept_args /* { 1690 syscallarg(int) s; 1691 syscallarg(struct osockaddr *) name; 1692 syscallarg(int *) anamelen; 1693 } */ *uap = v; 1694 int error; 1695 struct sys_accept_args baa; 1696 1697 SCARG(&baa, s) = SCARG(uap, s); 1698 SCARG(&baa, name) = (struct sockaddr *) SCARG(uap, name); 1699 SCARG(&baa, anamelen) = (unsigned int *) SCARG(uap, anamelen); 1700 1701 if ((error = sys_accept(l, &baa, retval))) 1702 return (error); 1703 1704 if (SCARG(uap, name) && (error = linux_sa_put(SCARG(uap, name)))) 1705 return (error); 1706 1707 return (0); 1708 } 1709