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