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, uint32 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 free(descriptor); 366 367 return fd; 368 } 369 370 371 // #pragma mark - common sockets API implementation 372 373 374 static int 375 common_socket(int family, int type, int protocol, bool kernel) 376 { 377 if (!get_stack_interface_module()) 378 return B_UNSUPPORTED; 379 380 // create the socket 381 net_socket* socket; 382 status_t error = sStackInterface->open(family, type, protocol, &socket); 383 if (error != B_OK) { 384 put_stack_interface_module(); 385 return error; 386 } 387 388 // allocate the FD 389 int fd = create_socket_fd(socket, kernel); 390 if (fd < 0) { 391 sStackInterface->close(socket); 392 sStackInterface->free(socket); 393 put_stack_interface_module(); 394 } 395 396 return fd; 397 } 398 399 400 static status_t 401 common_bind(int fd, const struct sockaddr *address, socklen_t addressLength, 402 bool kernel) 403 { 404 file_descriptor* descriptor; 405 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 406 FDPutter _(descriptor); 407 408 return sStackInterface->bind(descriptor->u.socket, address, addressLength); 409 } 410 411 412 static status_t 413 common_shutdown(int fd, int how, bool kernel) 414 { 415 file_descriptor* descriptor; 416 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 417 FDPutter _(descriptor); 418 419 return sStackInterface->shutdown(descriptor->u.socket, how); 420 } 421 422 423 static status_t 424 common_connect(int fd, const struct sockaddr *address, 425 socklen_t addressLength, bool kernel) 426 { 427 file_descriptor* descriptor; 428 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 429 FDPutter _(descriptor); 430 431 return sStackInterface->connect(descriptor->u.socket, address, 432 addressLength); 433 } 434 435 436 static status_t 437 common_listen(int fd, int backlog, bool kernel) 438 { 439 file_descriptor* descriptor; 440 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 441 FDPutter _(descriptor); 442 443 return sStackInterface->listen(descriptor->u.socket, backlog); 444 } 445 446 447 static int 448 common_accept(int fd, struct sockaddr *address, socklen_t *_addressLength, 449 bool kernel) 450 { 451 file_descriptor* descriptor; 452 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 453 FDPutter _(descriptor); 454 455 net_socket* acceptedSocket; 456 status_t error = sStackInterface->accept(descriptor->u.socket, address, 457 _addressLength, &acceptedSocket); 458 if (error != B_OK) 459 return error; 460 461 // allocate the FD 462 int acceptedFD = create_socket_fd(acceptedSocket, kernel); 463 if (acceptedFD < 0) { 464 sStackInterface->close(acceptedSocket); 465 sStackInterface->free(acceptedSocket); 466 } else { 467 // we need a reference for the new FD 468 get_stack_interface_module(); 469 } 470 471 return acceptedFD; 472 } 473 474 475 static ssize_t 476 common_recv(int fd, void *data, size_t length, int flags, bool kernel) 477 { 478 file_descriptor* descriptor; 479 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 480 FDPutter _(descriptor); 481 482 return sStackInterface->recv(descriptor->u.socket, data, length, flags); 483 } 484 485 486 static ssize_t 487 common_recvfrom(int fd, void *data, size_t length, int flags, 488 struct sockaddr *address, socklen_t *_addressLength, bool kernel) 489 { 490 file_descriptor* descriptor; 491 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 492 FDPutter _(descriptor); 493 494 return sStackInterface->recvfrom(descriptor->u.socket, data, length, 495 flags, address, _addressLength); 496 } 497 498 499 static ssize_t 500 common_recvmsg(int fd, struct msghdr *message, int flags, bool kernel) 501 { 502 file_descriptor* descriptor; 503 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 504 FDPutter _(descriptor); 505 506 return sStackInterface->recvmsg(descriptor->u.socket, message, flags); 507 } 508 509 510 static ssize_t 511 common_send(int fd, const void *data, size_t length, int flags, bool kernel) 512 { 513 file_descriptor* descriptor; 514 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 515 FDPutter _(descriptor); 516 517 return sStackInterface->send(descriptor->u.socket, data, length, flags); 518 } 519 520 521 static ssize_t 522 common_sendto(int fd, const void *data, size_t length, int flags, 523 const struct sockaddr *address, socklen_t addressLength, bool kernel) 524 { 525 file_descriptor* descriptor; 526 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 527 FDPutter _(descriptor); 528 529 return sStackInterface->sendto(descriptor->u.socket, data, length, flags, 530 address, addressLength); 531 } 532 533 534 static ssize_t 535 common_sendmsg(int fd, const struct msghdr *message, int flags, bool kernel) 536 { 537 file_descriptor* descriptor; 538 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 539 FDPutter _(descriptor); 540 541 return sStackInterface->sendmsg(descriptor->u.socket, message, flags); 542 } 543 544 545 static status_t 546 common_getsockopt(int fd, int level, int option, void *value, 547 socklen_t *_length, bool kernel) 548 { 549 file_descriptor* descriptor; 550 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 551 FDPutter _(descriptor); 552 553 return sStackInterface->getsockopt(descriptor->u.socket, level, option, 554 value, _length); 555 } 556 557 558 static status_t 559 common_setsockopt(int fd, int level, int option, const void *value, 560 socklen_t length, bool kernel) 561 { 562 file_descriptor* descriptor; 563 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 564 FDPutter _(descriptor); 565 566 return sStackInterface->setsockopt(descriptor->u.socket, level, option, 567 value, length); 568 } 569 570 571 static status_t 572 common_getpeername(int fd, struct sockaddr *address, 573 socklen_t *_addressLength, bool kernel) 574 { 575 file_descriptor* descriptor; 576 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 577 FDPutter _(descriptor); 578 579 return sStackInterface->getpeername(descriptor->u.socket, address, 580 _addressLength); 581 } 582 583 584 static status_t 585 common_getsockname(int fd, struct sockaddr *address, 586 socklen_t *_addressLength, bool kernel) 587 { 588 file_descriptor* descriptor; 589 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 590 FDPutter _(descriptor); 591 592 return sStackInterface->getsockname(descriptor->u.socket, address, 593 _addressLength); 594 } 595 596 597 static int 598 common_sockatmark(int fd, bool kernel) 599 { 600 file_descriptor* descriptor; 601 GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor); 602 FDPutter _(descriptor); 603 604 return sStackInterface->sockatmark(descriptor->u.socket); 605 } 606 607 608 static status_t 609 common_socketpair(int family, int type, int protocol, int fds[2], bool kernel) 610 { 611 if (!get_stack_interface_module()) 612 return B_UNSUPPORTED; 613 614 net_socket* sockets[2]; 615 status_t error = sStackInterface->socketpair(family, type, protocol, 616 sockets); 617 if (error != B_OK) { 618 put_stack_interface_module(); 619 return error; 620 } 621 622 // allocate the FDs 623 for (int i = 0; i < 2; i++) { 624 fds[i] = create_socket_fd(sockets[i], kernel); 625 if (fds[i] < 0) { 626 sStackInterface->close(sockets[i]); 627 sStackInterface->free(sockets[i]); 628 put_stack_interface_module(); 629 return fds[i]; 630 } 631 } 632 633 // We need another reference for the second socket 634 get_stack_interface_module(); 635 return B_OK; 636 } 637 638 639 static status_t 640 common_get_next_socket_stat(int family, uint32 *cookie, struct net_stat *stat) 641 { 642 if (!get_stack_interface_module()) 643 return B_UNSUPPORTED; 644 645 status_t status = sStackInterface->get_next_socket_stat(family, cookie, 646 stat); 647 648 put_stack_interface_module(); 649 return status; 650 } 651 652 653 // #pragma mark - kernel sockets API 654 655 656 int 657 socket(int family, int type, int protocol) 658 { 659 SyscallFlagUnsetter _; 660 RETURN_AND_SET_ERRNO(common_socket(family, type, protocol, true)); 661 } 662 663 664 int 665 bind(int socket, const struct sockaddr *address, socklen_t addressLength) 666 { 667 SyscallFlagUnsetter _; 668 RETURN_AND_SET_ERRNO(common_bind(socket, address, addressLength, true)); 669 } 670 671 672 int 673 shutdown(int socket, int how) 674 { 675 SyscallFlagUnsetter _; 676 RETURN_AND_SET_ERRNO(common_shutdown(socket, how, true)); 677 } 678 679 680 int 681 connect(int socket, const struct sockaddr *address, socklen_t addressLength) 682 { 683 SyscallFlagUnsetter _; 684 RETURN_AND_SET_ERRNO(common_connect(socket, address, addressLength, true)); 685 } 686 687 688 int 689 listen(int socket, int backlog) 690 { 691 SyscallFlagUnsetter _; 692 RETURN_AND_SET_ERRNO(common_listen(socket, backlog, true)); 693 } 694 695 696 int 697 accept(int socket, struct sockaddr *address, socklen_t *_addressLength) 698 { 699 SyscallFlagUnsetter _; 700 RETURN_AND_SET_ERRNO(common_accept(socket, address, _addressLength, true)); 701 } 702 703 704 ssize_t 705 recv(int socket, void *data, size_t length, int flags) 706 { 707 SyscallFlagUnsetter _; 708 RETURN_AND_SET_ERRNO(common_recv(socket, data, length, flags, true)); 709 } 710 711 712 ssize_t 713 recvfrom(int socket, void *data, size_t length, int flags, 714 struct sockaddr *address, socklen_t *_addressLength) 715 { 716 SyscallFlagUnsetter _; 717 RETURN_AND_SET_ERRNO(common_recvfrom(socket, data, length, flags, address, 718 _addressLength, true)); 719 } 720 721 722 ssize_t 723 recvmsg(int socket, struct msghdr *message, int flags) 724 { 725 SyscallFlagUnsetter _; 726 RETURN_AND_SET_ERRNO(common_recvmsg(socket, message, flags, true)); 727 } 728 729 730 ssize_t 731 send(int socket, const void *data, size_t length, int flags) 732 { 733 SyscallFlagUnsetter _; 734 RETURN_AND_SET_ERRNO(common_send(socket, data, length, flags, true)); 735 } 736 737 738 ssize_t 739 sendto(int socket, const void *data, size_t length, int flags, 740 const struct sockaddr *address, socklen_t addressLength) 741 { 742 SyscallFlagUnsetter _; 743 RETURN_AND_SET_ERRNO(common_sendto(socket, data, length, flags, address, 744 addressLength, true)); 745 } 746 747 748 ssize_t 749 sendmsg(int socket, const struct msghdr *message, int flags) 750 { 751 SyscallFlagUnsetter _; 752 RETURN_AND_SET_ERRNO(common_sendmsg(socket, message, flags, true)); 753 } 754 755 756 int 757 getsockopt(int socket, int level, int option, void *value, socklen_t *_length) 758 { 759 SyscallFlagUnsetter _; 760 RETURN_AND_SET_ERRNO(common_getsockopt(socket, level, option, value, 761 _length, true)); 762 } 763 764 765 int 766 setsockopt(int socket, int level, int option, const void *value, 767 socklen_t length) 768 { 769 SyscallFlagUnsetter _; 770 RETURN_AND_SET_ERRNO(common_setsockopt(socket, level, option, value, 771 length, true)); 772 } 773 774 775 int 776 getpeername(int socket, struct sockaddr *address, socklen_t *_addressLength) 777 { 778 SyscallFlagUnsetter _; 779 RETURN_AND_SET_ERRNO(common_getpeername(socket, address, _addressLength, 780 true)); 781 } 782 783 784 int 785 getsockname(int socket, struct sockaddr *address, socklen_t *_addressLength) 786 { 787 SyscallFlagUnsetter _; 788 RETURN_AND_SET_ERRNO(common_getsockname(socket, address, _addressLength, 789 true)); 790 } 791 792 793 int 794 sockatmark(int socket) 795 { 796 SyscallFlagUnsetter _; 797 RETURN_AND_SET_ERRNO(common_sockatmark(socket, true)); 798 } 799 800 801 int 802 socketpair(int family, int type, int protocol, int socketVector[2]) 803 { 804 SyscallFlagUnsetter _; 805 RETURN_AND_SET_ERRNO(common_socketpair(family, type, protocol, 806 socketVector, true)); 807 } 808 809 810 // #pragma mark - syscalls 811 812 813 int 814 _user_socket(int family, int type, int protocol) 815 { 816 SyscallRestartWrapper<int> result; 817 return result = common_socket(family, type, protocol, false); 818 } 819 820 821 status_t 822 _user_bind(int socket, const struct sockaddr *userAddress, 823 socklen_t addressLength) 824 { 825 // check parameters and copy address from userland 826 if (userAddress == NULL || addressLength > MAX_SOCKET_ADDRESS_LENGTH) 827 return B_BAD_VALUE; 828 829 sockaddr_storage address; 830 if (!IS_USER_ADDRESS(userAddress) 831 || user_memcpy(&address, userAddress, addressLength) != B_OK) { 832 return B_BAD_ADDRESS; 833 } 834 835 address.ss_len = addressLength; 836 // make sure the sa_len field is set correctly 837 838 SyscallRestartWrapper<status_t> error; 839 return error = common_bind(socket, (sockaddr*)&address, addressLength, 840 false); 841 } 842 843 844 status_t 845 _user_shutdown_socket(int socket, int how) 846 { 847 SyscallRestartWrapper<status_t> error; 848 return error = common_shutdown(socket, how, false); 849 } 850 851 852 status_t 853 _user_connect(int socket, const struct sockaddr *userAddress, 854 socklen_t addressLength) 855 { 856 // check parameters and copy address from userland 857 if (userAddress == NULL || addressLength > MAX_SOCKET_ADDRESS_LENGTH) 858 return B_BAD_VALUE; 859 860 sockaddr_storage address; 861 if (!IS_USER_ADDRESS(userAddress) 862 || user_memcpy(&address, userAddress, addressLength) != B_OK) { 863 return B_BAD_ADDRESS; 864 } 865 866 address.ss_len = addressLength; 867 // make sure the sa_len field is set correctly 868 869 SyscallRestartWrapper<status_t> error; 870 871 return error = common_connect(socket, (sockaddr*)&address, addressLength, 872 false); 873 } 874 875 876 status_t 877 _user_listen(int socket, int backlog) 878 { 879 SyscallRestartWrapper<status_t> error; 880 return error = common_listen(socket, backlog, false); 881 } 882 883 884 int 885 _user_accept(int socket, struct sockaddr *userAddress, 886 socklen_t *_addressLength) 887 { 888 // check parameters 889 socklen_t addressLength = 0; 890 status_t error = prepare_userland_address_result(userAddress, 891 _addressLength, addressLength, false); 892 if (error != B_OK) 893 return error; 894 895 // accept() 896 SyscallRestartWrapper<int> result; 897 898 char address[MAX_SOCKET_ADDRESS_LENGTH]; 899 socklen_t userAddressBufferSize = addressLength; 900 result = common_accept(socket, 901 userAddress != NULL ? (sockaddr*)address : NULL, &addressLength, false); 902 903 // copy address size and address back to userland 904 if (copy_address_to_userland(address, addressLength, userAddress, 905 userAddressBufferSize, _addressLength) != B_OK) { 906 _user_close(result); 907 return B_BAD_ADDRESS; 908 } 909 910 return result; 911 } 912 913 914 ssize_t 915 _user_recv(int socket, void *data, size_t length, int flags) 916 { 917 if (data == NULL || !IS_USER_ADDRESS(data)) 918 return B_BAD_ADDRESS; 919 920 SyscallRestartWrapper<ssize_t> result; 921 return result = common_recv(socket, data, length, flags, false); 922 } 923 924 925 ssize_t 926 _user_recvfrom(int socket, void *data, size_t length, int flags, 927 struct sockaddr *userAddress, socklen_t *_addressLength) 928 { 929 if (data == NULL || !IS_USER_ADDRESS(data)) 930 return B_BAD_ADDRESS; 931 932 // check parameters 933 socklen_t addressLength = 0; 934 status_t error = prepare_userland_address_result(userAddress, 935 _addressLength, addressLength, false); 936 if (error != B_OK) 937 return error; 938 939 // recvfrom() 940 SyscallRestartWrapper<ssize_t> result; 941 942 char address[MAX_SOCKET_ADDRESS_LENGTH]; 943 socklen_t userAddressBufferSize = addressLength; 944 result = common_recvfrom(socket, data, length, flags, 945 userAddress != NULL ? (sockaddr*)address : NULL, &addressLength, false); 946 if (result < 0) 947 return result; 948 949 // copy address size and address back to userland 950 if (copy_address_to_userland(address, addressLength, userAddress, 951 userAddressBufferSize, _addressLength) != B_OK) { 952 return B_BAD_ADDRESS; 953 } 954 955 return result; 956 } 957 958 959 ssize_t 960 _user_recvmsg(int socket, struct msghdr *userMessage, int flags) 961 { 962 // copy message from userland 963 msghdr message; 964 iovec* userVecs; 965 MemoryDeleter vecsDeleter; 966 void* userAddress; 967 char address[MAX_SOCKET_ADDRESS_LENGTH]; 968 969 status_t error = prepare_userland_msghdr(userMessage, message, userVecs, 970 vecsDeleter, userAddress, address); 971 if (error != B_OK) 972 return error; 973 974 // prepare a buffer for ancillary data 975 MemoryDeleter ancillaryDeleter; 976 void* ancillary = NULL; 977 void* userAncillary = message.msg_control; 978 if (userAncillary != NULL) { 979 if (!IS_USER_ADDRESS(userAncillary)) 980 return B_BAD_ADDRESS; 981 if (message.msg_controllen < 0) 982 return B_BAD_VALUE; 983 if (message.msg_controllen > MAX_ANCILLARY_DATA_LENGTH) 984 message.msg_controllen = MAX_ANCILLARY_DATA_LENGTH; 985 986 message.msg_control = ancillary = malloc(message.msg_controllen); 987 if (message.msg_control == NULL) 988 return B_NO_MEMORY; 989 990 ancillaryDeleter.SetTo(ancillary); 991 } 992 993 // recvmsg() 994 SyscallRestartWrapper<ssize_t> result; 995 996 result = common_recvmsg(socket, &message, flags, false); 997 if (result < 0) 998 return result; 999 1000 // copy the address, the ancillary data, and the message header back to 1001 // userland 1002 message.msg_name = userAddress; 1003 message.msg_iov = userVecs; 1004 message.msg_control = userAncillary; 1005 if ((userAddress != NULL && user_memcpy(userAddress, address, 1006 message.msg_namelen) != B_OK) 1007 || (userAncillary != NULL && user_memcpy(userAncillary, ancillary, 1008 message.msg_controllen) != B_OK) 1009 || user_memcpy(userMessage, &message, sizeof(msghdr)) != B_OK) { 1010 return B_BAD_ADDRESS; 1011 } 1012 1013 return result; 1014 } 1015 1016 1017 ssize_t 1018 _user_send(int socket, const void *data, size_t length, int flags) 1019 { 1020 if (data == NULL || !IS_USER_ADDRESS(data)) 1021 return B_BAD_ADDRESS; 1022 1023 SyscallRestartWrapper<ssize_t> result; 1024 return result = common_send(socket, data, length, flags, false); 1025 } 1026 1027 1028 ssize_t 1029 _user_sendto(int socket, const void *data, size_t length, int flags, 1030 const struct sockaddr *userAddress, socklen_t addressLength) 1031 { 1032 if (data == NULL || !IS_USER_ADDRESS(data)) 1033 return B_BAD_ADDRESS; 1034 1035 // TODO: If this is a connection-mode socket, the address parameter is 1036 // supposed to be ignored. 1037 if (userAddress == NULL || addressLength <= 0 1038 || addressLength > MAX_SOCKET_ADDRESS_LENGTH) { 1039 return B_BAD_VALUE; 1040 } 1041 1042 // copy address from userland 1043 char address[MAX_SOCKET_ADDRESS_LENGTH]; 1044 if (!IS_USER_ADDRESS(userAddress) 1045 || user_memcpy(address, userAddress, addressLength) != B_OK) { 1046 return B_BAD_ADDRESS; 1047 } 1048 1049 // sendto() 1050 SyscallRestartWrapper<ssize_t> result; 1051 1052 return result = common_sendto(socket, data, length, flags, 1053 (sockaddr*)address, addressLength, false); 1054 } 1055 1056 1057 ssize_t 1058 _user_sendmsg(int socket, const struct msghdr *userMessage, int flags) 1059 { 1060 // copy message from userland 1061 msghdr message; 1062 iovec* userVecs; 1063 MemoryDeleter vecsDeleter; 1064 void* userAddress; 1065 char address[MAX_SOCKET_ADDRESS_LENGTH]; 1066 1067 status_t error = prepare_userland_msghdr(userMessage, message, userVecs, 1068 vecsDeleter, userAddress, address); 1069 if (error != B_OK) 1070 return error; 1071 1072 // copy the address from userland 1073 if (userAddress != NULL 1074 && user_memcpy(address, userAddress, message.msg_namelen) != B_OK) { 1075 return B_BAD_ADDRESS; 1076 } 1077 1078 // copy ancillary data from userland 1079 MemoryDeleter ancillaryDeleter; 1080 void* userAncillary = message.msg_control; 1081 if (userAncillary != NULL) { 1082 if (!IS_USER_ADDRESS(userAncillary)) 1083 return B_BAD_ADDRESS; 1084 if (message.msg_controllen < 0 1085 || message.msg_controllen > MAX_ANCILLARY_DATA_LENGTH) { 1086 return B_BAD_VALUE; 1087 } 1088 1089 message.msg_control = malloc(message.msg_controllen); 1090 if (message.msg_control == NULL) 1091 return B_NO_MEMORY; 1092 ancillaryDeleter.SetTo(message.msg_control); 1093 1094 if (user_memcpy(message.msg_control, userAncillary, 1095 message.msg_controllen) != B_OK) { 1096 return B_BAD_ADDRESS; 1097 } 1098 } 1099 1100 // sendmsg() 1101 SyscallRestartWrapper<ssize_t> result; 1102 1103 return result = common_sendmsg(socket, &message, flags, false); 1104 } 1105 1106 1107 status_t 1108 _user_getsockopt(int socket, int level, int option, void *userValue, 1109 socklen_t *_length) 1110 { 1111 // check params 1112 if (userValue == NULL || _length == NULL) 1113 return B_BAD_VALUE; 1114 if (!IS_USER_ADDRESS(userValue) || !IS_USER_ADDRESS(_length)) 1115 return B_BAD_ADDRESS; 1116 1117 // copy length from userland 1118 socklen_t length; 1119 if (user_memcpy(&length, _length, sizeof(socklen_t)) != B_OK) 1120 return B_BAD_ADDRESS; 1121 1122 if (length > MAX_SOCKET_OPTION_LENGTH) 1123 return B_BAD_VALUE; 1124 1125 // getsockopt() 1126 char value[MAX_SOCKET_OPTION_LENGTH]; 1127 SyscallRestartWrapper<status_t> error; 1128 error = common_getsockopt(socket, level, option, value, &length, 1129 false); 1130 if (error != B_OK) 1131 return error; 1132 1133 // copy value back to userland 1134 if (user_memcpy(userValue, value, length) != B_OK) 1135 return B_BAD_ADDRESS; 1136 1137 return B_OK; 1138 } 1139 1140 1141 status_t 1142 _user_setsockopt(int socket, int level, int option, const void *userValue, 1143 socklen_t length) 1144 { 1145 // check params 1146 if (userValue == NULL || length > MAX_SOCKET_OPTION_LENGTH) 1147 return B_BAD_VALUE; 1148 1149 // copy value from userland 1150 char value[MAX_SOCKET_OPTION_LENGTH]; 1151 if (!IS_USER_ADDRESS(userValue) 1152 || user_memcpy(value, userValue, length) != B_OK) { 1153 return B_BAD_ADDRESS; 1154 } 1155 1156 // setsockopt(); 1157 SyscallRestartWrapper<status_t> error; 1158 return error = common_setsockopt(socket, level, option, value, length, 1159 false); 1160 } 1161 1162 1163 status_t 1164 _user_getpeername(int socket, struct sockaddr *userAddress, 1165 socklen_t *_addressLength) 1166 { 1167 // check parameters 1168 socklen_t addressLength = 0; 1169 SyscallRestartWrapper<status_t> error; 1170 error = prepare_userland_address_result(userAddress, _addressLength, 1171 addressLength, true); 1172 if (error != B_OK) 1173 return error; 1174 1175 // getpeername() 1176 char address[MAX_SOCKET_ADDRESS_LENGTH]; 1177 socklen_t userAddressBufferSize = addressLength; 1178 error = common_getpeername(socket, (sockaddr*)address, &addressLength, 1179 false); 1180 if (error != B_OK) 1181 return error; 1182 1183 // copy address size and address back to userland 1184 if (copy_address_to_userland(address, addressLength, userAddress, 1185 userAddressBufferSize, _addressLength) != B_OK) { 1186 return B_BAD_ADDRESS; 1187 } 1188 1189 return B_OK; 1190 } 1191 1192 1193 status_t 1194 _user_getsockname(int socket, struct sockaddr *userAddress, 1195 socklen_t *_addressLength) 1196 { 1197 // check parameters 1198 socklen_t addressLength = 0; 1199 SyscallRestartWrapper<status_t> error; 1200 error = prepare_userland_address_result(userAddress, _addressLength, 1201 addressLength, true); 1202 if (error != B_OK) 1203 return error; 1204 1205 // getsockname() 1206 char address[MAX_SOCKET_ADDRESS_LENGTH]; 1207 socklen_t userAddressBufferSize = addressLength; 1208 error = common_getsockname(socket, (sockaddr*)address, &addressLength, 1209 false); 1210 if (error != B_OK) 1211 return error; 1212 1213 // copy address size and address back to userland 1214 if (copy_address_to_userland(address, addressLength, userAddress, 1215 userAddressBufferSize, _addressLength) != B_OK) { 1216 return B_BAD_ADDRESS; 1217 } 1218 1219 return B_OK; 1220 } 1221 1222 1223 int 1224 _user_sockatmark(int socket) 1225 { 1226 SyscallRestartWrapper<status_t> error; 1227 return error = common_sockatmark(socket, false); 1228 } 1229 1230 1231 status_t 1232 _user_socketpair(int family, int type, int protocol, int *userSocketVector) 1233 { 1234 // check parameters 1235 if (userSocketVector == NULL) 1236 return B_BAD_VALUE; 1237 if (!IS_USER_ADDRESS(userSocketVector)) 1238 return B_BAD_ADDRESS; 1239 1240 // socketpair() 1241 int socketVector[2]; 1242 SyscallRestartWrapper<status_t> error; 1243 error = common_socketpair(family, type, protocol, socketVector, false); 1244 if (error != B_OK) 1245 return error; 1246 1247 // copy FDs back to userland 1248 if (user_memcpy(userSocketVector, socketVector, 1249 sizeof(socketVector)) != B_OK) { 1250 _user_close(socketVector[0]); 1251 _user_close(socketVector[1]); 1252 return B_BAD_ADDRESS; 1253 } 1254 1255 return B_OK; 1256 } 1257 1258 1259 status_t 1260 _user_get_next_socket_stat(int family, uint32 *_cookie, struct net_stat *_stat) 1261 { 1262 // check parameters and copy cookie from userland 1263 if (_cookie == NULL || _stat == NULL) 1264 return B_BAD_VALUE; 1265 1266 uint32 cookie; 1267 if (!IS_USER_ADDRESS(_stat) || !IS_USER_ADDRESS(_cookie) 1268 || user_memcpy(&cookie, _cookie, sizeof(cookie)) != B_OK) { 1269 return B_BAD_ADDRESS; 1270 } 1271 1272 net_stat stat; 1273 SyscallRestartWrapper<status_t> error; 1274 error = common_get_next_socket_stat(family, &cookie, &stat); 1275 if (error != B_OK) 1276 return error; 1277 1278 // copy cookie and data back to userland 1279 if (user_memcpy(_cookie, &cookie, sizeof(cookie)) != B_OK 1280 || user_memcpy(_stat, &stat, sizeof(net_stat)) != B_OK) { 1281 return B_BAD_ADDRESS; 1282 } 1283 1284 return B_OK; 1285 } 1286