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