1 /* 2 * Copyright 2009-2010, Axel Dörfler, axeld@pinc-software.de. 3 * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de. 4 * Distributed under the terms of the MIT License. 5 */ 6 7 8 #include <sys/socket.h> 9 10 #include <errno.h> 11 #include <limits.h> 12 13 #include <module.h> 14 15 #include <AutoDeleter.h> 16 #include <AutoDeleterDrivers.h> 17 18 #include <syscall_utils.h> 19 20 #include <fd.h> 21 #include <kernel.h> 22 #include <lock.h> 23 #include <syscall_restart.h> 24 #include <util/AutoLock.h> 25 #include <util/iovec_support.h> 26 #include <vfs.h> 27 28 #include <net_stack_interface.h> 29 #include <net_stat.h> 30 31 32 #define MAX_SOCKET_ADDRESS_LENGTH (sizeof(sockaddr_storage)) 33 #define MAX_SOCKET_OPTION_LENGTH 128 34 #define MAX_ANCILLARY_DATA_LENGTH 1024 35 36 #define GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor) \ 37 do { \ 38 status_t getError = get_socket_descriptor(fd, kernel, descriptor); \ 39 if (getError != B_OK) \ 40 return getError; \ 41 } while (false) 42 43 #define FD_SOCKET(descriptor) ((net_socket*)descriptor->cookie) 44 45 46 static net_stack_interface_module_info* sStackInterface = NULL; 47 static int32 sStackInterfaceConsumers = 0; 48 static rw_lock sLock = RW_LOCK_INITIALIZER("stack interface"); 49 50 51 static net_stack_interface_module_info* 52 get_stack_interface_module() 53 { 54 atomic_add(&sStackInterfaceConsumers, 1); 55 56 ReadLocker readLocker(sLock); 57 if (sStackInterface != NULL) 58 return sStackInterface; 59 60 readLocker.Unlock(); 61 WriteLocker writeLocker(sLock); 62 if (sStackInterface != NULL) 63 return sStackInterface; 64 65 // load module 66 net_stack_interface_module_info* module; 67 // TODO: Add driver settings option to load the userland net stack. 68 status_t error = get_module(NET_STACK_INTERFACE_MODULE_NAME, 69 (module_info**)&module); 70 if (error == B_OK) 71 sStackInterface = module; 72 73 if (sStackInterface == NULL) 74 atomic_add(&sStackInterfaceConsumers, -1); 75 76 return sStackInterface; 77 } 78 79 80 static void 81 put_stack_interface_module() 82 { 83 if (atomic_add(&sStackInterfaceConsumers, -1) != 1) 84 return; 85 86 // Keep the stack loaded on non-KDEBUG kernels. 87 #if KDEBUG 88 WriteLocker _(sLock); 89 if (atomic_get(&sStackInterfaceConsumers) > 0) 90 return; 91 if (sStackInterface == NULL) 92 return; 93 94 put_module(NET_STACK_INTERFACE_MODULE_NAME); 95 sStackInterface = NULL; 96 #endif 97 } 98 99 100 static status_t 101 prepare_userland_address_result(struct sockaddr* userAddress, 102 socklen_t* _addressLength, socklen_t& addressLength, bool addressRequired) 103 { 104 // check parameters 105 if (_addressLength == NULL) 106 return B_BAD_VALUE; 107 if (userAddress == NULL) { 108 if (addressRequired) 109 return B_BAD_VALUE; 110 } else if (!IS_USER_ADDRESS(userAddress) 111 || !IS_USER_ADDRESS(_addressLength)) { 112 return B_BAD_ADDRESS; 113 } 114 115 // copy the buffer size from userland 116 addressLength = 0; 117 if (userAddress != NULL 118 && user_memcpy(&addressLength, _addressLength, sizeof(socklen_t)) 119 != B_OK) { 120 return B_BAD_ADDRESS; 121 } 122 123 if (addressLength > MAX_SOCKET_ADDRESS_LENGTH) 124 addressLength = MAX_SOCKET_ADDRESS_LENGTH; 125 126 return B_OK; 127 } 128 129 130 static status_t 131 copy_address_to_userland(const void* address, socklen_t addressLength, 132 sockaddr* userAddress, socklen_t userAddressBufferSize, 133 socklen_t* userAddressLength) 134 { 135 // copy address size and address back to userland 136 if (user_memcpy(userAddressLength, &addressLength, 137 sizeof(socklen_t)) != B_OK 138 || (userAddress != NULL 139 && user_memcpy(userAddress, address, 140 min_c(addressLength, userAddressBufferSize)) != B_OK)) { 141 return B_BAD_ADDRESS; 142 } 143 144 return B_OK; 145 } 146 147 148 static status_t 149 prepare_userland_msghdr(const msghdr* userMessage, msghdr& message, 150 iovec*& userVecs, MemoryDeleter& vecsDeleter, void*& userAddress, 151 char* address) 152 { 153 if (userMessage == NULL) 154 return B_BAD_VALUE; 155 156 // copy message from userland 157 if (!IS_USER_ADDRESS(userMessage) 158 || user_memcpy(&message, userMessage, sizeof(msghdr)) != B_OK) { 159 return B_BAD_ADDRESS; 160 } 161 162 userVecs = message.msg_iov; 163 userAddress = message.msg_name; 164 165 // copy iovecs from userland 166 if (message.msg_iovlen < 0 || message.msg_iovlen > IOV_MAX) 167 return EMSGSIZE; 168 if (userVecs != NULL && message.msg_iovlen > 0) { 169 iovec* vecs = (iovec*)malloc(sizeof(iovec) * message.msg_iovlen); 170 if (vecs == NULL) 171 return B_NO_MEMORY; 172 vecsDeleter.SetTo(vecs); 173 174 status_t error = get_iovecs_from_user(message.msg_iov, message.msg_iovlen, vecs); 175 if (error != B_OK) 176 return error; 177 message.msg_iov = vecs; 178 } else { 179 message.msg_iov = NULL; 180 message.msg_iovlen = 0; 181 } 182 183 // prepare the address field 184 userAddress = message.msg_name; 185 if (userAddress != NULL) { 186 if (!IS_USER_ADDRESS(message.msg_name)) 187 return B_BAD_ADDRESS; 188 if (message.msg_namelen > MAX_SOCKET_ADDRESS_LENGTH) 189 message.msg_namelen = MAX_SOCKET_ADDRESS_LENGTH; 190 191 message.msg_name = address; 192 } 193 194 return B_OK; 195 } 196 197 198 // #pragma mark - socket file descriptor 199 200 201 static status_t 202 socket_read(struct file_descriptor *descriptor, off_t pos, void *buffer, 203 size_t *_length) 204 { 205 ssize_t bytesRead = sStackInterface->recv(FD_SOCKET(descriptor), buffer, 206 *_length, 0); 207 *_length = bytesRead >= 0 ? bytesRead : 0; 208 return bytesRead >= 0 ? B_OK : bytesRead; 209 } 210 211 212 static status_t 213 socket_write(struct file_descriptor *descriptor, off_t pos, const void *buffer, 214 size_t *_length) 215 { 216 ssize_t bytesWritten = sStackInterface->send(FD_SOCKET(descriptor), buffer, 217 *_length, 0); 218 *_length = bytesWritten >= 0 ? bytesWritten : 0; 219 return bytesWritten >= 0 ? B_OK : bytesWritten; 220 } 221 222 223 static ssize_t 224 socket_readv(struct file_descriptor *descriptor, off_t pos, 225 const struct iovec *vecs, int count) 226 { 227 struct msghdr message = {}; 228 message.msg_iov = (struct iovec*)vecs; 229 message.msg_iovlen = count; 230 return sStackInterface->recvmsg(FD_SOCKET(descriptor), &message, 0); 231 } 232 233 234 static ssize_t 235 socket_writev(struct file_descriptor *descriptor, off_t pos, 236 const struct iovec *vecs, int count) 237 { 238 struct msghdr message = {}; 239 message.msg_iov = (struct iovec*)vecs; 240 message.msg_iovlen = count; 241 return sStackInterface->sendmsg(FD_SOCKET(descriptor), &message, 0); 242 } 243 244 245 static status_t 246 socket_ioctl(struct file_descriptor *descriptor, ulong op, void *buffer, 247 size_t length) 248 { 249 return sStackInterface->ioctl(FD_SOCKET(descriptor), op, buffer, length); 250 } 251 252 253 static status_t 254 socket_set_flags(struct file_descriptor *descriptor, int flags) 255 { 256 // we ignore O_APPEND, but O_NONBLOCK we need to translate 257 uint32 op = (flags & O_NONBLOCK) != 0 258 ? B_SET_NONBLOCKING_IO : B_SET_BLOCKING_IO; 259 260 return sStackInterface->ioctl(FD_SOCKET(descriptor), op, NULL, 0); 261 } 262 263 264 static status_t 265 socket_select(struct file_descriptor *descriptor, uint8 event, 266 struct selectsync *sync) 267 { 268 return sStackInterface->select(FD_SOCKET(descriptor), event, sync); 269 } 270 271 272 static status_t 273 socket_deselect(struct file_descriptor *descriptor, uint8 event, 274 struct selectsync *sync) 275 { 276 return sStackInterface->deselect(FD_SOCKET(descriptor), event, sync); 277 } 278 279 280 static status_t 281 socket_read_stat(struct file_descriptor *descriptor, struct stat *st) 282 { 283 st->st_dev = 0; 284 st->st_ino = (addr_t)descriptor->cookie; 285 st->st_mode = S_IFSOCK | 0666; 286 st->st_nlink = 1; 287 st->st_uid = 0; 288 st->st_gid = 0; 289 st->st_size = 0; 290 st->st_rdev = 0; 291 st->st_blksize = 1024; // use MTU for datagram sockets? 292 st->st_type = 0; 293 294 timespec now; 295 now.tv_sec = time(NULL); 296 now.tv_nsec = 0; 297 298 st->st_atim = now; 299 st->st_mtim = now; 300 st->st_ctim = now; 301 st->st_crtim = now; 302 303 return B_OK; 304 } 305 306 307 static status_t 308 socket_close(struct file_descriptor *descriptor) 309 { 310 return sStackInterface->close(FD_SOCKET(descriptor)); 311 } 312 313 314 static void 315 socket_free(struct file_descriptor *descriptor) 316 { 317 sStackInterface->free(FD_SOCKET(descriptor)); 318 put_stack_interface_module(); 319 } 320 321 322 static struct fd_ops sSocketFDOps = { 323 &socket_close, 324 &socket_free, 325 &socket_read, 326 &socket_write, 327 &socket_readv, 328 &socket_writev, 329 NULL, // fd_seek 330 &socket_ioctl, 331 &socket_set_flags, 332 &socket_select, 333 &socket_deselect, 334 NULL, // fd_read_dir 335 NULL, // fd_rewind_dir 336 &socket_read_stat, 337 NULL, // fd_write_stat 338 }; 339 340 341 static status_t 342 get_socket_descriptor(int fd, bool kernel, file_descriptor*& descriptor) 343 { 344 if (fd < 0) 345 return EBADF; 346 347 descriptor = get_fd(get_current_io_context(kernel), fd); 348 if (descriptor == NULL) 349 return EBADF; 350 351 if (descriptor->ops != &sSocketFDOps) { 352 put_fd(descriptor); 353 return ENOTSOCK; 354 } 355 356 return B_OK; 357 } 358 359 360 static int 361 create_socket_fd(net_socket* socket, bool kernel) 362 { 363 // Get the socket's non-blocking flag, so we can set the respective 364 // open mode flag. 365 int32 nonBlock; 366 socklen_t nonBlockLen = sizeof(int32); 367 status_t error = sStackInterface->getsockopt(socket, SOL_SOCKET, 368 SO_NONBLOCK, &nonBlock, &nonBlockLen); 369 if (error != B_OK) 370 return error; 371 372 // allocate a file descriptor 373 file_descriptor* descriptor = alloc_fd(); 374 if (descriptor == NULL) 375 return B_NO_MEMORY; 376 377 // init it 378 descriptor->ops = &sSocketFDOps; 379 descriptor->cookie = socket; 380 descriptor->open_mode = O_RDWR | (nonBlock ? O_NONBLOCK : 0); 381 382 // publish it 383 int fd = new_fd(get_current_io_context(kernel), descriptor); 384 if (fd < 0) { 385 descriptor->ops = NULL; 386 put_fd(descriptor); 387 } 388 389 return fd; 390 } 391 392 393 // #pragma mark - common sockets API implementation 394 395 396 static int 397 common_socket(int family, int type, int protocol, bool kernel) 398 { 399 if (!get_stack_interface_module()) 400 return B_UNSUPPORTED; 401 402 // create the socket 403 net_socket* socket; 404 status_t error = sStackInterface->open(family, type, protocol, &socket); 405 if (error != B_OK) { 406 put_stack_interface_module(); 407 return error; 408 } 409 410 // allocate the FD 411 int fd = create_socket_fd(socket, kernel); 412 if (fd < 0) { 413 sStackInterface->close(socket); 414 sStackInterface->free(socket); 415 put_stack_interface_module(); 416 } 417 418 return fd; 419 } 420 421 422 static status_t 423 common_bind(int fd, const struct sockaddr *address, socklen_t addressLength, 424 bool kernel) 425 { 426 file_descriptor* descriptor; 427 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 428 FileDescriptorPutter _(descriptor); 429 430 return sStackInterface->bind(FD_SOCKET(descriptor), address, addressLength); 431 } 432 433 434 static status_t 435 common_shutdown(int fd, int how, bool kernel) 436 { 437 file_descriptor* descriptor; 438 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 439 FileDescriptorPutter _(descriptor); 440 441 return sStackInterface->shutdown(FD_SOCKET(descriptor), how); 442 } 443 444 445 static status_t 446 common_connect(int fd, const struct sockaddr *address, 447 socklen_t addressLength, bool kernel) 448 { 449 file_descriptor* descriptor; 450 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 451 FileDescriptorPutter _(descriptor); 452 453 return sStackInterface->connect(FD_SOCKET(descriptor), address, 454 addressLength); 455 } 456 457 458 static status_t 459 common_listen(int fd, int backlog, bool kernel) 460 { 461 file_descriptor* descriptor; 462 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 463 FileDescriptorPutter _(descriptor); 464 465 return sStackInterface->listen(FD_SOCKET(descriptor), backlog); 466 } 467 468 469 static int 470 common_accept(int fd, struct sockaddr *address, socklen_t *_addressLength, 471 bool kernel) 472 { 473 file_descriptor* descriptor; 474 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 475 FileDescriptorPutter _(descriptor); 476 477 net_socket* acceptedSocket; 478 status_t error = sStackInterface->accept(FD_SOCKET(descriptor), address, 479 _addressLength, &acceptedSocket); 480 if (error != B_OK) 481 return error; 482 483 // allocate the FD 484 int acceptedFD = create_socket_fd(acceptedSocket, kernel); 485 if (acceptedFD < 0) { 486 sStackInterface->close(acceptedSocket); 487 sStackInterface->free(acceptedSocket); 488 } else { 489 // we need a reference for the new FD 490 get_stack_interface_module(); 491 } 492 493 return acceptedFD; 494 } 495 496 497 static ssize_t 498 common_recv(int fd, void *data, size_t length, int flags, bool kernel) 499 { 500 file_descriptor* descriptor; 501 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 502 FileDescriptorPutter _(descriptor); 503 504 return sStackInterface->recv(FD_SOCKET(descriptor), data, length, flags); 505 } 506 507 508 static ssize_t 509 common_recvfrom(int fd, void *data, size_t length, int flags, 510 struct sockaddr *address, socklen_t *_addressLength, bool kernel) 511 { 512 file_descriptor* descriptor; 513 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 514 FileDescriptorPutter _(descriptor); 515 516 return sStackInterface->recvfrom(FD_SOCKET(descriptor), data, length, 517 flags, address, _addressLength); 518 } 519 520 521 static ssize_t 522 common_recvmsg(int fd, struct msghdr *message, int flags, bool kernel) 523 { 524 file_descriptor* descriptor; 525 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 526 FileDescriptorPutter _(descriptor); 527 528 return sStackInterface->recvmsg(FD_SOCKET(descriptor), message, flags); 529 } 530 531 532 static ssize_t 533 common_send(int fd, const void *data, size_t length, int flags, bool kernel) 534 { 535 file_descriptor* descriptor; 536 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 537 FileDescriptorPutter _(descriptor); 538 539 return sStackInterface->send(FD_SOCKET(descriptor), data, length, flags); 540 } 541 542 543 static ssize_t 544 common_sendto(int fd, const void *data, size_t length, int flags, 545 const struct sockaddr *address, socklen_t addressLength, bool kernel) 546 { 547 file_descriptor* descriptor; 548 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 549 FileDescriptorPutter _(descriptor); 550 551 return sStackInterface->sendto(FD_SOCKET(descriptor), data, length, flags, 552 address, addressLength); 553 } 554 555 556 static ssize_t 557 common_sendmsg(int fd, const struct msghdr *message, int flags, bool kernel) 558 { 559 file_descriptor* descriptor; 560 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 561 FileDescriptorPutter _(descriptor); 562 563 return sStackInterface->sendmsg(FD_SOCKET(descriptor), message, flags); 564 } 565 566 567 static status_t 568 common_getsockopt(int fd, int level, int option, void *value, 569 socklen_t *_length, bool kernel) 570 { 571 file_descriptor* descriptor; 572 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 573 FileDescriptorPutter _(descriptor); 574 575 return sStackInterface->getsockopt(FD_SOCKET(descriptor), level, option, 576 value, _length); 577 } 578 579 580 static status_t 581 common_setsockopt(int fd, int level, int option, const void *value, 582 socklen_t length, bool kernel) 583 { 584 file_descriptor* descriptor; 585 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 586 FileDescriptorPutter _(descriptor); 587 588 return sStackInterface->setsockopt(FD_SOCKET(descriptor), level, option, 589 value, length); 590 } 591 592 593 static status_t 594 common_getpeername(int fd, struct sockaddr *address, 595 socklen_t *_addressLength, bool kernel) 596 { 597 file_descriptor* descriptor; 598 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 599 FileDescriptorPutter _(descriptor); 600 601 return sStackInterface->getpeername(FD_SOCKET(descriptor), address, 602 _addressLength); 603 } 604 605 606 static status_t 607 common_getsockname(int fd, struct sockaddr *address, 608 socklen_t *_addressLength, bool kernel) 609 { 610 file_descriptor* descriptor; 611 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 612 FileDescriptorPutter _(descriptor); 613 614 return sStackInterface->getsockname(FD_SOCKET(descriptor), address, 615 _addressLength); 616 } 617 618 619 static int 620 common_sockatmark(int fd, bool kernel) 621 { 622 file_descriptor* descriptor; 623 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 624 FileDescriptorPutter _(descriptor); 625 626 return sStackInterface->sockatmark(FD_SOCKET(descriptor)); 627 } 628 629 630 static status_t 631 common_socketpair(int family, int type, int protocol, int fds[2], bool kernel) 632 { 633 if (!get_stack_interface_module()) 634 return B_UNSUPPORTED; 635 636 net_socket* sockets[2]; 637 status_t error = sStackInterface->socketpair(family, type, protocol, 638 sockets); 639 if (error != B_OK) { 640 put_stack_interface_module(); 641 return error; 642 } 643 644 // allocate the FDs 645 for (int i = 0; i < 2; i++) { 646 fds[i] = create_socket_fd(sockets[i], kernel); 647 if (fds[i] < 0) { 648 sStackInterface->close(sockets[i]); 649 sStackInterface->free(sockets[i]); 650 put_stack_interface_module(); 651 return fds[i]; 652 } 653 } 654 655 // We need another reference for the second socket 656 get_stack_interface_module(); 657 return B_OK; 658 } 659 660 661 static status_t 662 common_get_next_socket_stat(int family, uint32 *cookie, struct net_stat *stat) 663 { 664 if (!get_stack_interface_module()) 665 return B_UNSUPPORTED; 666 667 status_t status = sStackInterface->get_next_socket_stat(family, cookie, 668 stat); 669 670 put_stack_interface_module(); 671 return status; 672 } 673 674 675 // #pragma mark - kernel sockets API 676 677 678 int 679 socket(int family, int type, int protocol) 680 { 681 SyscallFlagUnsetter _; 682 RETURN_AND_SET_ERRNO(common_socket(family, type, protocol, true)); 683 } 684 685 686 int 687 bind(int socket, const struct sockaddr *address, socklen_t addressLength) 688 { 689 SyscallFlagUnsetter _; 690 RETURN_AND_SET_ERRNO(common_bind(socket, address, addressLength, true)); 691 } 692 693 694 int 695 shutdown(int socket, int how) 696 { 697 SyscallFlagUnsetter _; 698 RETURN_AND_SET_ERRNO(common_shutdown(socket, how, true)); 699 } 700 701 702 int 703 connect(int socket, const struct sockaddr *address, socklen_t addressLength) 704 { 705 SyscallFlagUnsetter _; 706 RETURN_AND_SET_ERRNO(common_connect(socket, address, addressLength, true)); 707 } 708 709 710 int 711 listen(int socket, int backlog) 712 { 713 SyscallFlagUnsetter _; 714 RETURN_AND_SET_ERRNO(common_listen(socket, backlog, true)); 715 } 716 717 718 int 719 accept(int socket, struct sockaddr *address, socklen_t *_addressLength) 720 { 721 SyscallFlagUnsetter _; 722 RETURN_AND_SET_ERRNO(common_accept(socket, address, _addressLength, true)); 723 } 724 725 726 ssize_t 727 recv(int socket, void *data, size_t length, int flags) 728 { 729 SyscallFlagUnsetter _; 730 RETURN_AND_SET_ERRNO(common_recv(socket, data, length, flags, true)); 731 } 732 733 734 ssize_t 735 recvfrom(int socket, void *data, size_t length, int flags, 736 struct sockaddr *address, socklen_t *_addressLength) 737 { 738 SyscallFlagUnsetter _; 739 RETURN_AND_SET_ERRNO(common_recvfrom(socket, data, length, flags, address, 740 _addressLength, true)); 741 } 742 743 744 ssize_t 745 recvmsg(int socket, struct msghdr *message, int flags) 746 { 747 SyscallFlagUnsetter _; 748 RETURN_AND_SET_ERRNO(common_recvmsg(socket, message, flags, true)); 749 } 750 751 752 ssize_t 753 send(int socket, const void *data, size_t length, int flags) 754 { 755 SyscallFlagUnsetter _; 756 RETURN_AND_SET_ERRNO(common_send(socket, data, length, flags, true)); 757 } 758 759 760 ssize_t 761 sendto(int socket, const void *data, size_t length, int flags, 762 const struct sockaddr *address, socklen_t addressLength) 763 { 764 SyscallFlagUnsetter _; 765 RETURN_AND_SET_ERRNO(common_sendto(socket, data, length, flags, address, 766 addressLength, true)); 767 } 768 769 770 ssize_t 771 sendmsg(int socket, const struct msghdr *message, int flags) 772 { 773 SyscallFlagUnsetter _; 774 RETURN_AND_SET_ERRNO(common_sendmsg(socket, message, flags, true)); 775 } 776 777 778 int 779 getsockopt(int socket, int level, int option, void *value, socklen_t *_length) 780 { 781 SyscallFlagUnsetter _; 782 RETURN_AND_SET_ERRNO(common_getsockopt(socket, level, option, value, 783 _length, true)); 784 } 785 786 787 int 788 setsockopt(int socket, int level, int option, const void *value, 789 socklen_t length) 790 { 791 SyscallFlagUnsetter _; 792 RETURN_AND_SET_ERRNO(common_setsockopt(socket, level, option, value, 793 length, true)); 794 } 795 796 797 int 798 getpeername(int socket, struct sockaddr *address, socklen_t *_addressLength) 799 { 800 SyscallFlagUnsetter _; 801 RETURN_AND_SET_ERRNO(common_getpeername(socket, address, _addressLength, 802 true)); 803 } 804 805 806 int 807 getsockname(int socket, struct sockaddr *address, socklen_t *_addressLength) 808 { 809 SyscallFlagUnsetter _; 810 RETURN_AND_SET_ERRNO(common_getsockname(socket, address, _addressLength, 811 true)); 812 } 813 814 815 int 816 sockatmark(int socket) 817 { 818 SyscallFlagUnsetter _; 819 RETURN_AND_SET_ERRNO(common_sockatmark(socket, true)); 820 } 821 822 823 int 824 socketpair(int family, int type, int protocol, int socketVector[2]) 825 { 826 SyscallFlagUnsetter _; 827 RETURN_AND_SET_ERRNO(common_socketpair(family, type, protocol, 828 socketVector, true)); 829 } 830 831 832 // #pragma mark - syscalls 833 834 835 int 836 _user_socket(int family, int type, int protocol) 837 { 838 SyscallRestartWrapper<int> result; 839 return result = common_socket(family, type, protocol, false); 840 } 841 842 843 status_t 844 _user_bind(int socket, const struct sockaddr *userAddress, 845 socklen_t addressLength) 846 { 847 // check parameters and copy address from userland 848 if (userAddress == NULL || addressLength > MAX_SOCKET_ADDRESS_LENGTH) 849 return B_BAD_VALUE; 850 851 sockaddr_storage address; 852 memset(&address, 0, sizeof(address)); 853 if (!IS_USER_ADDRESS(userAddress) 854 || user_memcpy(&address, userAddress, addressLength) != B_OK) { 855 return B_BAD_ADDRESS; 856 } 857 858 address.ss_len = addressLength; 859 // make sure the sa_len field is set correctly 860 861 SyscallRestartWrapper<status_t> error; 862 return error = common_bind(socket, (sockaddr*)&address, addressLength, 863 false); 864 } 865 866 867 status_t 868 _user_shutdown_socket(int socket, int how) 869 { 870 SyscallRestartWrapper<status_t> error; 871 return error = common_shutdown(socket, how, false); 872 } 873 874 875 status_t 876 _user_connect(int socket, const struct sockaddr *userAddress, 877 socklen_t addressLength) 878 { 879 // check parameters and copy address from userland 880 if (userAddress == NULL || addressLength > MAX_SOCKET_ADDRESS_LENGTH) 881 return B_BAD_VALUE; 882 883 sockaddr_storage address; 884 memset(&address, 0, sizeof(address)); 885 if (!IS_USER_ADDRESS(userAddress) 886 || user_memcpy(&address, userAddress, addressLength) != B_OK) { 887 return B_BAD_ADDRESS; 888 } 889 890 address.ss_len = addressLength; 891 // make sure the sa_len field is set correctly 892 893 SyscallRestartWrapper<status_t> error; 894 895 return error = common_connect(socket, (sockaddr*)&address, addressLength, 896 false); 897 } 898 899 900 status_t 901 _user_listen(int socket, int backlog) 902 { 903 SyscallRestartWrapper<status_t> error; 904 return error = common_listen(socket, backlog, false); 905 } 906 907 908 int 909 _user_accept(int socket, struct sockaddr *userAddress, 910 socklen_t *_addressLength) 911 { 912 // check parameters 913 socklen_t addressLength = 0; 914 status_t error = prepare_userland_address_result(userAddress, 915 _addressLength, addressLength, false); 916 if (error != B_OK) 917 return error; 918 919 // accept() 920 SyscallRestartWrapper<int> result; 921 922 char address[MAX_SOCKET_ADDRESS_LENGTH]; 923 socklen_t userAddressBufferSize = addressLength; 924 result = common_accept(socket, 925 userAddress != NULL ? (sockaddr*)address : NULL, &addressLength, false); 926 927 // copy address size and address back to userland 928 if (copy_address_to_userland(address, addressLength, userAddress, 929 userAddressBufferSize, _addressLength) != B_OK) { 930 _user_close(result); 931 return B_BAD_ADDRESS; 932 } 933 934 return result; 935 } 936 937 938 ssize_t 939 _user_recv(int socket, void *data, size_t length, int flags) 940 { 941 if (data == NULL || !is_user_address_range(data, length)) 942 return B_BAD_ADDRESS; 943 944 SyscallRestartWrapper<ssize_t> result; 945 return result = common_recv(socket, data, length, flags, false); 946 } 947 948 949 ssize_t 950 _user_recvfrom(int socket, void *data, size_t length, int flags, 951 struct sockaddr *userAddress, socklen_t *_addressLength) 952 { 953 if (data == NULL || !is_user_address_range(data, length)) 954 return B_BAD_ADDRESS; 955 956 // check parameters 957 socklen_t addressLength = 0; 958 status_t error = prepare_userland_address_result(userAddress, 959 _addressLength, addressLength, false); 960 if (error != B_OK) 961 return error; 962 963 // recvfrom() 964 SyscallRestartWrapper<ssize_t> result; 965 966 char address[MAX_SOCKET_ADDRESS_LENGTH]; 967 socklen_t userAddressBufferSize = addressLength; 968 result = common_recvfrom(socket, data, length, flags, 969 userAddress != NULL ? (sockaddr*)address : NULL, &addressLength, false); 970 if (result < 0) 971 return result; 972 973 // copy address size and address back to userland 974 if (copy_address_to_userland(address, addressLength, userAddress, 975 userAddressBufferSize, _addressLength) != B_OK) { 976 return B_BAD_ADDRESS; 977 } 978 979 return result; 980 } 981 982 983 ssize_t 984 _user_recvmsg(int socket, struct msghdr *userMessage, int flags) 985 { 986 // copy message from userland 987 msghdr message; 988 iovec* userVecs; 989 MemoryDeleter vecsDeleter; 990 void* userAddress; 991 char address[MAX_SOCKET_ADDRESS_LENGTH]; 992 993 status_t error = prepare_userland_msghdr(userMessage, message, userVecs, 994 vecsDeleter, userAddress, address); 995 if (error != B_OK) 996 return error; 997 998 // prepare a buffer for ancillary data 999 MemoryDeleter ancillaryDeleter; 1000 void* ancillary = NULL; 1001 void* userAncillary = message.msg_control; 1002 if (userAncillary != NULL) { 1003 if (!IS_USER_ADDRESS(userAncillary)) 1004 return B_BAD_ADDRESS; 1005 if (message.msg_controllen < 0) 1006 return B_BAD_VALUE; 1007 if (message.msg_controllen > MAX_ANCILLARY_DATA_LENGTH) 1008 message.msg_controllen = MAX_ANCILLARY_DATA_LENGTH; 1009 1010 message.msg_control = ancillary = malloc(message.msg_controllen); 1011 if (message.msg_control == NULL) 1012 return B_NO_MEMORY; 1013 1014 ancillaryDeleter.SetTo(ancillary); 1015 } 1016 1017 // recvmsg() 1018 SyscallRestartWrapper<ssize_t> result; 1019 1020 result = common_recvmsg(socket, &message, flags, false); 1021 if (result < 0) 1022 return result; 1023 1024 // copy the address, the ancillary data, and the message header back to 1025 // userland 1026 message.msg_name = userAddress; 1027 message.msg_iov = userVecs; 1028 message.msg_control = userAncillary; 1029 if ((userAddress != NULL && user_memcpy(userAddress, address, 1030 message.msg_namelen) != B_OK) 1031 || (userAncillary != NULL && user_memcpy(userAncillary, ancillary, 1032 message.msg_controllen) != B_OK) 1033 || user_memcpy(userMessage, &message, sizeof(msghdr)) != B_OK) { 1034 return B_BAD_ADDRESS; 1035 } 1036 1037 return result; 1038 } 1039 1040 1041 ssize_t 1042 _user_send(int socket, const void *data, size_t length, int flags) 1043 { 1044 if (data == NULL || !is_user_address_range(data, length)) 1045 return B_BAD_ADDRESS; 1046 1047 SyscallRestartWrapper<ssize_t> result; 1048 return result = common_send(socket, data, length, flags, false); 1049 } 1050 1051 1052 ssize_t 1053 _user_sendto(int socket, const void *data, size_t length, int flags, 1054 const struct sockaddr *userAddress, socklen_t addressLength) 1055 { 1056 if (data == NULL || !is_user_address_range(data, length)) 1057 return B_BAD_ADDRESS; 1058 1059 if (addressLength <= 0 1060 || addressLength > MAX_SOCKET_ADDRESS_LENGTH) { 1061 return B_BAD_VALUE; 1062 } 1063 1064 // copy address from userland 1065 char address[MAX_SOCKET_ADDRESS_LENGTH]; 1066 if (userAddress != NULL) { 1067 if (!IS_USER_ADDRESS(userAddress) 1068 || user_memcpy(address, userAddress, addressLength) != B_OK) { 1069 return B_BAD_ADDRESS; 1070 } 1071 } else { 1072 addressLength = 0; 1073 } 1074 1075 // sendto() 1076 SyscallRestartWrapper<ssize_t> result; 1077 1078 return result = common_sendto(socket, data, length, flags, 1079 userAddress != NULL ? (sockaddr*)address : NULL, addressLength, false); 1080 } 1081 1082 1083 ssize_t 1084 _user_sendmsg(int socket, const struct msghdr *userMessage, int flags) 1085 { 1086 // copy message from userland 1087 msghdr message; 1088 iovec* userVecs; 1089 MemoryDeleter vecsDeleter; 1090 void* userAddress; 1091 char address[MAX_SOCKET_ADDRESS_LENGTH]; 1092 1093 status_t error = prepare_userland_msghdr(userMessage, message, userVecs, 1094 vecsDeleter, userAddress, address); 1095 if (error != B_OK) 1096 return error; 1097 1098 // copy the address from userland 1099 if (userAddress != NULL 1100 && user_memcpy(address, userAddress, message.msg_namelen) != B_OK) { 1101 return B_BAD_ADDRESS; 1102 } 1103 1104 // copy ancillary data from userland 1105 MemoryDeleter ancillaryDeleter; 1106 void* userAncillary = message.msg_control; 1107 if (userAncillary != NULL) { 1108 if (!IS_USER_ADDRESS(userAncillary)) 1109 return B_BAD_ADDRESS; 1110 if (message.msg_controllen < 0 1111 || message.msg_controllen > MAX_ANCILLARY_DATA_LENGTH) { 1112 return B_BAD_VALUE; 1113 } 1114 1115 message.msg_control = malloc(message.msg_controllen); 1116 if (message.msg_control == NULL) 1117 return B_NO_MEMORY; 1118 ancillaryDeleter.SetTo(message.msg_control); 1119 1120 if (user_memcpy(message.msg_control, userAncillary, 1121 message.msg_controllen) != B_OK) { 1122 return B_BAD_ADDRESS; 1123 } 1124 } 1125 1126 // sendmsg() 1127 SyscallRestartWrapper<ssize_t> result; 1128 1129 return result = common_sendmsg(socket, &message, flags, false); 1130 } 1131 1132 1133 status_t 1134 _user_getsockopt(int socket, int level, int option, void *userValue, 1135 socklen_t *_length) 1136 { 1137 // check params 1138 if (userValue == NULL || _length == NULL) 1139 return B_BAD_VALUE; 1140 if (!IS_USER_ADDRESS(userValue) || !IS_USER_ADDRESS(_length)) 1141 return B_BAD_ADDRESS; 1142 1143 // copy length from userland 1144 socklen_t length; 1145 if (user_memcpy(&length, _length, sizeof(socklen_t)) != B_OK) 1146 return B_BAD_ADDRESS; 1147 1148 if (length > MAX_SOCKET_OPTION_LENGTH) 1149 return B_BAD_VALUE; 1150 1151 // getsockopt() 1152 char value[MAX_SOCKET_OPTION_LENGTH]; 1153 SyscallRestartWrapper<status_t> error; 1154 error = common_getsockopt(socket, level, option, value, &length, 1155 false); 1156 if (error != B_OK) 1157 return error; 1158 1159 // copy value back to userland 1160 if (user_memcpy(userValue, value, length) != B_OK) 1161 return B_BAD_ADDRESS; 1162 1163 return B_OK; 1164 } 1165 1166 1167 status_t 1168 _user_setsockopt(int socket, int level, int option, const void *userValue, 1169 socklen_t length) 1170 { 1171 // check params 1172 if (userValue == NULL || length > MAX_SOCKET_OPTION_LENGTH) 1173 return B_BAD_VALUE; 1174 1175 // copy value from userland 1176 char value[MAX_SOCKET_OPTION_LENGTH]; 1177 if (!IS_USER_ADDRESS(userValue) 1178 || user_memcpy(value, userValue, length) != B_OK) { 1179 return B_BAD_ADDRESS; 1180 } 1181 1182 // setsockopt(); 1183 SyscallRestartWrapper<status_t> error; 1184 return error = common_setsockopt(socket, level, option, value, length, 1185 false); 1186 } 1187 1188 1189 status_t 1190 _user_getpeername(int socket, struct sockaddr *userAddress, 1191 socklen_t *_addressLength) 1192 { 1193 // check parameters 1194 socklen_t addressLength = 0; 1195 SyscallRestartWrapper<status_t> error; 1196 error = prepare_userland_address_result(userAddress, _addressLength, 1197 addressLength, true); 1198 if (error != B_OK) 1199 return error; 1200 1201 // getpeername() 1202 char address[MAX_SOCKET_ADDRESS_LENGTH]; 1203 socklen_t userAddressBufferSize = addressLength; 1204 error = common_getpeername(socket, (sockaddr*)address, &addressLength, 1205 false); 1206 if (error != B_OK) 1207 return error; 1208 1209 // copy address size and address back to userland 1210 if (copy_address_to_userland(address, addressLength, userAddress, 1211 userAddressBufferSize, _addressLength) != B_OK) { 1212 return B_BAD_ADDRESS; 1213 } 1214 1215 return B_OK; 1216 } 1217 1218 1219 status_t 1220 _user_getsockname(int socket, struct sockaddr *userAddress, 1221 socklen_t *_addressLength) 1222 { 1223 // check parameters 1224 socklen_t addressLength = 0; 1225 SyscallRestartWrapper<status_t> error; 1226 error = prepare_userland_address_result(userAddress, _addressLength, 1227 addressLength, true); 1228 if (error != B_OK) 1229 return error; 1230 1231 // getsockname() 1232 char address[MAX_SOCKET_ADDRESS_LENGTH]; 1233 socklen_t userAddressBufferSize = addressLength; 1234 error = common_getsockname(socket, (sockaddr*)address, &addressLength, 1235 false); 1236 if (error != B_OK) 1237 return error; 1238 1239 // copy address size and address back to userland 1240 if (copy_address_to_userland(address, addressLength, userAddress, 1241 userAddressBufferSize, _addressLength) != B_OK) { 1242 return B_BAD_ADDRESS; 1243 } 1244 1245 return B_OK; 1246 } 1247 1248 1249 int 1250 _user_sockatmark(int socket) 1251 { 1252 SyscallRestartWrapper<status_t> error; 1253 return error = common_sockatmark(socket, false); 1254 } 1255 1256 1257 status_t 1258 _user_socketpair(int family, int type, int protocol, int *userSocketVector) 1259 { 1260 // check parameters 1261 if (userSocketVector == NULL) 1262 return B_BAD_VALUE; 1263 if (!IS_USER_ADDRESS(userSocketVector)) 1264 return B_BAD_ADDRESS; 1265 1266 // socketpair() 1267 int socketVector[2]; 1268 SyscallRestartWrapper<status_t> error; 1269 error = common_socketpair(family, type, protocol, socketVector, false); 1270 if (error != B_OK) 1271 return error; 1272 1273 // copy FDs back to userland 1274 if (user_memcpy(userSocketVector, socketVector, 1275 sizeof(socketVector)) != B_OK) { 1276 _user_close(socketVector[0]); 1277 _user_close(socketVector[1]); 1278 return B_BAD_ADDRESS; 1279 } 1280 1281 return B_OK; 1282 } 1283 1284 1285 status_t 1286 _user_get_next_socket_stat(int family, uint32 *_cookie, struct net_stat *_stat) 1287 { 1288 // check parameters and copy cookie from userland 1289 if (_cookie == NULL || _stat == NULL) 1290 return B_BAD_VALUE; 1291 1292 uint32 cookie; 1293 if (!IS_USER_ADDRESS(_stat) || !IS_USER_ADDRESS(_cookie) 1294 || user_memcpy(&cookie, _cookie, sizeof(cookie)) != B_OK) { 1295 return B_BAD_ADDRESS; 1296 } 1297 1298 net_stat stat; 1299 SyscallRestartWrapper<status_t> error; 1300 error = common_get_next_socket_stat(family, &cookie, &stat); 1301 if (error != B_OK) 1302 return error; 1303 1304 // copy cookie and data back to userland 1305 if (user_memcpy(_cookie, &cookie, sizeof(cookie)) != B_OK 1306 || user_memcpy(_stat, &stat, sizeof(net_stat)) != B_OK) { 1307 return B_BAD_ADDRESS; 1308 } 1309 1310 return B_OK; 1311 } 1312